summaryrefslogtreecommitdiff
path: root/drivers/net/imx-shmem-net.c
blob: e75b71abf2cf883dec87b6e8e2b9f1b50b629702 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
// SPDX-License-Identifier: GPL-2.0
/* Copyright 2016 Mans Rullgard <mans@mansr.com>
 * Copyright 2019 NXP
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/io.h>
#include <linux/bitops.h>
#include <linux/interrupt.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/ethtool.h>
#include <linux/rtnetlink.h>
#include <linux/virtio_ring.h>
#include <linux/platform_device.h>
#include <linux/of_device.h>
#include <linux/of_reserved_mem.h>
#include <linux/clk.h>
#include <linux/mutex.h>

#ifdef CONFIG_IMX_SCU
#include <linux/firmware/imx/sci.h>
#endif
#include <linux/mailbox_client.h>
#include <linux/mailbox_controller.h>
#include <linux/list.h>
#include <linux/bitops.h>

#define DRV_NAME "imx-shmem-net"

#define IMX_SHMEM_INTX_ENABLE	0x1

#define IMX_SHM_NET_STATE_RESET	0
#define IMX_SHM_NET_STATE_INIT	1
#define IMX_SHM_NET_STATE_READY	2
#define IMX_SHM_NET_STATE_RUN	3

#define IMX_SHM_NET_FLAG_RUN	0

#define IMX_SHM_NET_MTU_MIN 256
#define IMX_SHM_NET_MTU_MAX 65535
#define IMX_SHM_NET_MTU_DEF 16384

#define IMX_SHM_NET_FRAME_SIZE(s) ALIGN(18 + (s), SMP_CACHE_BYTES)

#define IMX_SHM_NET_VQ_ALIGN 64

/* (queue_size + vring_size) * 2 for TX&RX */
#define IMX_SHM_NET_DMA_SIZE (((IMX_SHM_NET_MTU_DEF * 16) + (64 * 1024)) * 2)

/* MU's Receiver Register 1 Full mask */
#define MU_SR_RF1_MASK1		BIT(26)
/* MU timeout in milliseconds when sending message */
#define IMX_SHM_NET_MU_TIMEOUT 2000
/* connected partition default id */
#define IMX_SHM_NET_DEFAULT_PART 3

#define SC_IRQ_GROUP_REBOOTED   5U      /* Partition reboot complete */

static const char * const state_name[] = {
	"RESET",
	"INIT",
	"READY",
	"RUN"
};

enum e_mess_state {
	MESS_STATE_NEW,
	MESS_STATE_PROCESSED,
};

struct imx_shmem_regs {
	u32 intxctrl;
	u32 istat;
	u32 ivpos;
	u32 doorbell;
	u32 lstate;
	u32 rstate;
};

struct imx_shm_net_queue {
	struct vring vr;
	u32 free_head;
	u32 num_free;
	u32 num_added;
	u16 last_avail_idx;
	u16 last_used_idx;

	void *data;
	void *end;
	u32 size;
	u32 head;
	u32 tail;
};

struct imx_shm_net_stats {
	u32 interrupts;
	u32 tx_packets;
	u32 tx_notify;
	u32 tx_pause;
	u32 rx_packets;
	u32 rx_notify;
	u32 napi_poll;
	u32 napi_complete;
	u32 napi_poll_n[10];
};

struct imx_shm_net {
	struct imx_shm_net_queue rx;
	struct imx_shm_net_queue tx;

	u32 vrsize;
	u32 qlen;
	u32 qsize;

	spinlock_t tx_free_lock;	/* protect available buffers */
	spinlock_t tx_clean_lock;	/* protect used buffers */

	struct napi_struct napi;

	unsigned long flags;

	struct workqueue_struct *state_wq;
	struct work_struct state_work;

	struct imx_shm_net_stats stats;

	struct imx_shmem_regs regs;
	void *shm;
	phys_addr_t shmaddr;
	resource_size_t shmlen;

	struct platform_device *pdev;

	void __iomem *mu_base;
	struct mutex state_lock;	/* protect state variable */
	struct clk *mu_clk;
	u32 remote_message;
	enum e_mess_state message_state; /* remote msg. processing status */
	u32 mub_partition;
	struct notifier_block pnotifier;
	struct list_head isn_node;
	struct mbox_client cl;
	struct mbox_chan *tx_ch;
	struct mbox_chan *rx_ch;
};

/* the head of the list of imx_shm_net devices */
static struct list_head imx_shm_net_head;

static void *imx_shm_net_desc_data(struct imx_shm_net *in,
				   struct imx_shm_net_queue *q,
				   struct vring_desc *desc,
				   u32 *len)
{
	u64 offs = READ_ONCE(desc->addr);
	u32 dlen = READ_ONCE(desc->len);
	u16 flags = READ_ONCE(desc->flags);
	void *data;

	if (flags)
		return NULL;

	if (offs >= in->shmlen)
		return NULL;

	data = in->shm + offs;

	if (data < q->data || data >= q->end)
		return NULL;

	if (dlen > q->end - data)
		return NULL;

	*len = dlen;

	return data;
}

static void imx_shm_net_init_queue(struct imx_shm_net *in,
				   struct imx_shm_net_queue *q,
				   void *mem, unsigned int len)
{
	memset(q, 0, sizeof(*q));

	vring_init(&q->vr, len, mem, IMX_SHM_NET_VQ_ALIGN);
	q->data = mem + in->vrsize;
	q->end = q->data + in->qsize;
	q->size = in->qsize;
}

static void imx_shm_net_init_queues(struct net_device *ndev)
{
	struct imx_shm_net *in = netdev_priv(ndev);
	int ivpos = in->regs.ivpos;
	void *tx;
	void *rx;
	int i;

	tx = in->shm +  ivpos * in->shmlen / 2;
	rx = in->shm + !ivpos * in->shmlen / 2;

	memset(tx, 0, in->shmlen / 2);

	imx_shm_net_init_queue(in, &in->rx, rx, in->qlen);
	imx_shm_net_init_queue(in, &in->tx, tx, in->qlen);

	swap(in->rx.vr.used, in->tx.vr.used);

	in->tx.num_free = in->tx.vr.num;

	for (i = 0; i < in->tx.vr.num - 1; i++)
		in->tx.vr.desc[i].next = i + 1;
}

static int imx_shm_net_calc_qsize(struct net_device *ndev)
{
	struct imx_shm_net *in = netdev_priv(ndev);
	unsigned int vrsize;
	unsigned int qsize;
	unsigned int qlen;

	for (qlen = 4096; qlen > 32; qlen >>= 1) {
		vrsize = vring_size(qlen, IMX_SHM_NET_VQ_ALIGN);
		vrsize = ALIGN(vrsize, IMX_SHM_NET_VQ_ALIGN);
		if (vrsize < in->shmlen / 16)
			break;
	}

	if (vrsize > in->shmlen / 2)
		return -EINVAL;

	qsize = in->shmlen / 2 - vrsize;

	if (qsize < 4 * IMX_SHM_NET_MTU_MIN)
		return -EINVAL;

	in->vrsize = vrsize;
	in->qlen = qlen;
	in->qsize = qsize;

	return 0;
}

static void imx_shm_net_notify_tx(struct imx_shm_net *in, unsigned int num)
{
	u16 evt, old, new;
	int ret;

	/* memory barrier */
	virt_mb();

	evt = READ_ONCE(vring_avail_event(&in->tx.vr));
	old = in->tx.last_avail_idx - num;
	new = in->tx.last_avail_idx;

	if (vring_need_event(evt, new, old)) {
		ret = mbox_send_message(in->tx_ch, &in->regs.lstate);
		if (ret < 0)
			dev_err(&in->pdev->dev, "%s send message error=%d!\n",
				__func__, ret);

		in->stats.tx_notify++;
	}
}

static void imx_shm_net_enable_rx_irq(struct imx_shm_net *in)
{
	vring_avail_event(&in->rx.vr) = in->rx.last_avail_idx;
	/* memory barrier */
	virt_wmb();
}

static void imx_shm_net_notify_rx(struct imx_shm_net *in, unsigned int num)
{
	u16 evt, old, new;
	int ret;

	/* memory barrier */
	virt_mb();

	evt = vring_used_event(&in->rx.vr);
	old = in->rx.last_used_idx - num;
	new = in->rx.last_used_idx;

	if (vring_need_event(evt, new, old)) {
		ret = mbox_send_message(in->tx_ch, &in->regs.lstate);
		if (ret < 0)
			dev_err(&in->pdev->dev, "%s send message error=%d!\n",
				__func__, ret);

		in->stats.rx_notify++;
	}
}

static void imx_shm_net_enable_tx_irq(struct imx_shm_net *in)
{
	vring_used_event(&in->tx.vr) = in->tx.last_used_idx;
	/* memory barrier */
	virt_wmb();
}

static bool imx_shm_net_rx_avail(struct imx_shm_net *in)
{
	/* memory barrier */
	virt_mb();
	return READ_ONCE(in->rx.vr.avail->idx) != in->rx.last_avail_idx;
}

static size_t imx_shm_net_tx_space(struct imx_shm_net *in)
{
	struct imx_shm_net_queue *tx = &in->tx;
	u32 tail = tx->tail;
	u32 head = tx->head;
	u32 space;

	if (head < tail)
		space = tail - head;
	else
		space = max(tx->size - head, tail);

	return space;
}

static bool imx_shm_net_tx_ok(struct imx_shm_net *in, unsigned int mtu)
{
	return in->tx.num_free >= 2 &&
		imx_shm_net_tx_space(in) >= 2 * IMX_SHM_NET_FRAME_SIZE(mtu);
}

static u32 imx_shm_net_tx_advance(struct imx_shm_net_queue *q, u32 *pos,
				  u32 len)
{
	u32 p = *pos;

	len = IMX_SHM_NET_FRAME_SIZE(len);

	if (q->size - p < len)
		p = 0;
	*pos = p + len;

	return p;
}

static int imx_shm_net_tx_frame(struct net_device *ndev, struct sk_buff *skb,
				bool xmit_more)
{
	struct imx_shm_net *in = netdev_priv(ndev);
	struct imx_shm_net_queue *tx = &in->tx;
	struct vring *vr = &tx->vr;
	struct vring_desc *desc;
	unsigned int desc_idx;
	unsigned int avail;
	u32 head;
	void *buf;

	BUG_ON(tx->num_free < 1);

	spin_lock(&in->tx_free_lock);
	desc_idx = tx->free_head;
	desc = &vr->desc[desc_idx];
	tx->free_head = desc->next;
	tx->num_free--;
	spin_unlock(&in->tx_free_lock);

	head = imx_shm_net_tx_advance(tx, &tx->head, skb->len);

	buf = tx->data + head;
	skb_copy_and_csum_dev(skb, buf);

	desc->addr = buf - in->shm;
	desc->len = skb->len;
	desc->flags = 0;

	avail = tx->last_avail_idx++ & (vr->num - 1);
	vr->avail->ring[avail] = desc_idx;
	tx->num_added++;

	if (!xmit_more) {
		/* memory barrier */
		virt_store_release(&vr->avail->idx, tx->last_avail_idx);
		imx_shm_net_notify_tx(in, tx->num_added);
		tx->num_added = 0;
	}

	return 0;
}

static void imx_shm_net_tx_clean(struct net_device *ndev)
{
	struct imx_shm_net *in = netdev_priv(ndev);
	struct imx_shm_net_queue *tx = &in->tx;
	struct vring_used_elem *used;
	struct vring *vr = &tx->vr;
	struct vring_desc *desc;
	struct vring_desc *fdesc;
	unsigned int num;
	u16 used_idx;
	u16 last;
	u32 fhead;

	if (!spin_trylock(&in->tx_clean_lock))
		return;

	/* memory barrier */
	used_idx = virt_load_acquire(&vr->used->idx);
	last = tx->last_used_idx;

	fdesc = NULL;
	fhead = 0;
	num = 0;

	while (last != used_idx) {
		void *data;
		u32 len;
		u32 tail;

		used = vr->used->ring + (last % vr->num);
		if (used->id >= vr->num || used->len != 1) {
			netdev_err(ndev, "invalid tx used->id %d ->len %d\n",
				   used->id, used->len);
			break;
		}

		desc = &vr->desc[used->id];

		data = imx_shm_net_desc_data(in, &in->tx, desc, &len);
		if (!data) {
			netdev_err(ndev, "bad tx descriptor, data == NULL\n");
			break;
		}

		tail = imx_shm_net_tx_advance(tx, &tx->tail, len);
		if (data != tx->data + tail) {
			netdev_err(ndev, "bad tx descriptor\n");
			break;
		}

		if (!num)
			fdesc = desc;
		else
			desc->next = fhead;

		fhead = used->id;
		last++;
		num++;
	}

	tx->last_used_idx = last;

	spin_unlock(&in->tx_clean_lock);

	if (num) {
		spin_lock(&in->tx_free_lock);
		fdesc->next = tx->free_head;
		tx->free_head = fhead;
		tx->num_free += num;
		BUG_ON(tx->num_free > vr->num);
		spin_unlock(&in->tx_free_lock);
	}
}

static struct vring_desc *imx_shm_net_rx_desc(struct net_device *ndev)
{
	struct imx_shm_net *in = netdev_priv(ndev);
	struct imx_shm_net_queue *rx = &in->rx;
	struct vring *vr = &rx->vr;
	unsigned int avail;
	u16 avail_idx;

	/* memory barrier */
	avail_idx = virt_load_acquire(&vr->avail->idx);

	if (avail_idx == rx->last_avail_idx)
		return NULL;

	avail = vr->avail->ring[rx->last_avail_idx++ & (vr->num - 1)];
	if (avail >= vr->num) {
		netdev_err(ndev, "invalid rx avail %d\n", avail);
		return NULL;
	}

	return &vr->desc[avail];
}

static void imx_shm_net_rx_finish(struct imx_shm_net *in,
				  struct vring_desc *desc)
{
	struct imx_shm_net_queue *rx = &in->rx;
	struct vring *vr = &rx->vr;
	unsigned int desc_id = desc - vr->desc;
	unsigned int used;

	used = rx->last_used_idx++ & (vr->num - 1);
	vr->used->ring[used].id = desc_id;
	vr->used->ring[used].len = 1;

	/* memory barrier */
	virt_store_release(&vr->used->idx, rx->last_used_idx);
}

static int imx_shm_net_poll(struct napi_struct *napi, int budget)
{
	struct net_device *ndev = napi->dev;
	struct imx_shm_net *in = container_of(napi, struct imx_shm_net, napi);
	int received = 0;

	in->stats.napi_poll++;

	imx_shm_net_tx_clean(ndev);

	while (received < budget) {
		struct vring_desc *desc;
		struct sk_buff *skb;
		void *data;
		u32 len;

		desc = imx_shm_net_rx_desc(ndev);
		if (!desc)
			break;

		data = imx_shm_net_desc_data(in, &in->rx, desc, &len);
		if (!data) {
			netdev_err(ndev, "bad rx descriptor\n");
			break;
		}

		skb = napi_alloc_skb(napi, len);

		if (skb) {
			memcpy(skb_put(skb, len), data, len);
			skb->protocol = eth_type_trans(skb, ndev);
			napi_gro_receive(napi, skb);
		}

		ndev->stats.rx_packets++;
		ndev->stats.rx_bytes += len;

		imx_shm_net_rx_finish(in, desc);
		received++;
	}

	if (received < budget) {
		in->stats.napi_complete++;
		napi_complete_done(napi, received);
		imx_shm_net_enable_rx_irq(in);
		if (imx_shm_net_rx_avail(in))
			napi_schedule(napi);
	}

	if (received)
		imx_shm_net_notify_rx(in, received);

	in->stats.rx_packets += received;
	in->stats.napi_poll_n[received ? 1 + min(ilog2(received), 8) : 0]++;

	if (imx_shm_net_tx_ok(in, ndev->mtu))
		netif_wake_queue(ndev);

	return received;
}

static netdev_tx_t imx_shm_net_xmit(struct sk_buff *skb,
				    struct net_device *ndev)
{
	struct imx_shm_net *in = netdev_priv(ndev);
	bool xmit_more = netdev_xmit_more();

	imx_shm_net_tx_clean(ndev);

	if (!imx_shm_net_tx_ok(in, ndev->mtu)) {
		imx_shm_net_enable_tx_irq(in);
		netif_stop_queue(ndev);
		xmit_more = false;
		in->stats.tx_pause++;
	}

	imx_shm_net_tx_frame(ndev, skb, xmit_more);

	in->stats.tx_packets++;
	ndev->stats.tx_packets++;
	ndev->stats.tx_bytes += skb->len;

	dev_consume_skb_any(skb);

	return NETDEV_TX_OK;
}

static void imx_shm_net_set_state(struct imx_shm_net *in, u32 state)
{
	int ret;

	/* memory barrier */
	virt_wmb();
	WRITE_ONCE(in->regs.lstate, state);
	dev_dbg(&in->pdev->dev, "%s %s\n", __func__, state_name[state]);
	ret = mbox_send_message(in->tx_ch, &state);
	if (ret < 0)
		dev_err(&in->pdev->dev, "%s send message error=%d!\n",
			__func__, ret);
}

static void imx_shm_net_run(struct net_device *ndev)
{
	struct imx_shm_net *in = netdev_priv(ndev);

	if (in->regs.lstate < IMX_SHM_NET_STATE_READY)
		return;

	if (!netif_running(ndev))
		return;

	if (test_and_set_bit(IMX_SHM_NET_FLAG_RUN, &in->flags))
		return;

	netif_start_queue(ndev);
	napi_enable(&in->napi);
	napi_schedule(&in->napi);
	imx_shm_net_set_state(in, IMX_SHM_NET_STATE_RUN);
}

static void imx_shm_net_do_stop(struct net_device *ndev)
{
	struct imx_shm_net *in = netdev_priv(ndev);

	imx_shm_net_set_state(in, IMX_SHM_NET_STATE_RESET);

	if (!test_and_clear_bit(IMX_SHM_NET_FLAG_RUN, &in->flags))
		return;

	netif_stop_queue(ndev);
	napi_disable(&in->napi);
}

static void imx_shm_net_state_change(struct work_struct *work)
{
	struct imx_shm_net *in = container_of(work, struct imx_shm_net,
			state_work);
	struct net_device *ndev = in->napi.dev;
	u32 rstate;

	mutex_lock(&in->state_lock);
	rstate = in->remote_message;
	if (in->message_state == MESS_STATE_PROCESSED)
		dev_dbg(&in->pdev->dev,
			"imx-shmem-net: ERROR message already processed!\n");

	in->message_state = MESS_STATE_PROCESSED;
	mutex_unlock(&in->state_lock);

	dev_dbg(&in->pdev->dev, "%s: rstate=%s lstate=%s\n",
		__func__, state_name[rstate], state_name[in->regs.lstate]);
	if (rstate != in->regs.rstate)
		dev_dbg(&in->pdev->dev, "rstate changed from %s to %s\n",
			state_name[in->regs.rstate], state_name[rstate]);

	switch (in->regs.lstate) {
	case IMX_SHM_NET_STATE_RESET:
		/* Wait for the remote to leave READY/RUN */
		/* before transitioning to INIT. */
		if (rstate < IMX_SHM_NET_STATE_READY)
			imx_shm_net_set_state(in, IMX_SHM_NET_STATE_INIT);
		break;

	case IMX_SHM_NET_STATE_INIT:
		/* Wait for the remote to leave RESET before performing the */
		/* initialization and moving to READY. */
		if (rstate > IMX_SHM_NET_STATE_RESET) {
			imx_shm_net_init_queues(ndev);
			imx_shm_net_set_state(in, IMX_SHM_NET_STATE_READY);

			rtnl_lock();
			call_netdevice_notifiers(NETDEV_CHANGEADDR, ndev);
			rtnl_unlock();
		}
		break;

	case IMX_SHM_NET_STATE_READY:
		/* Link is up and we are running */
		/* once the remote is in READY or RUN. */
		if (rstate >= IMX_SHM_NET_STATE_READY) {
			netif_carrier_on(ndev);
			imx_shm_net_run(ndev);
			break;
		}
		fallthrough;
	case IMX_SHM_NET_STATE_RUN:
		/* If the remote goes to RESET, */
		/* we need to follow immediately. */
		if (rstate == IMX_SHM_NET_STATE_RESET) {
			netif_carrier_off(ndev);
			imx_shm_net_do_stop(ndev);
		}
		break;
	}

	/* memory barrier */
	virt_wmb();
	WRITE_ONCE(in->regs.rstate, rstate);
}

static void imx_shm_net_check_state(struct net_device *ndev)
{
	struct imx_shm_net *in = netdev_priv(ndev);
	u32 rstate;

	/* for now message carries only the rstate value */
	rstate = in->remote_message;

	if (rstate != in->regs.rstate ||
	    !test_bit(IMX_SHM_NET_FLAG_RUN, &in->flags)) {
		dev_dbg(&ndev->dev, "rstate changed -> queue a work to handle it\n");
		queue_work(in->state_wq, &in->state_work);
	} else {
		in->message_state = MESS_STATE_PROCESSED;
	}
}

static int imx_shm_net_open(struct net_device *ndev)
{
	dev_dbg(&ndev->dev, "calling %s()\n", __func__);
	netdev_reset_queue(ndev);
	ndev->operstate = IF_OPER_UP;
	imx_shm_net_run(ndev);

	return 0;
}

static int imx_shm_net_stop(struct net_device *ndev)
{
	ndev->operstate = IF_OPER_DOWN;
	imx_shm_net_do_stop(ndev);

	return 0;
}

static int imx_shm_net_change_mtu(struct net_device *ndev, int mtu)
{
	struct imx_shm_net *in = netdev_priv(ndev);
	struct imx_shm_net_queue *tx = &in->tx;

	if (mtu < IMX_SHM_NET_MTU_MIN || mtu > IMX_SHM_NET_MTU_MAX)
		return -EINVAL;

	if (in->tx.size / mtu < 4)
		return -EINVAL;

	if (imx_shm_net_tx_space(in) < 2 * IMX_SHM_NET_FRAME_SIZE(mtu))
		return -EBUSY;

	if (in->tx.size - tx->head < IMX_SHM_NET_FRAME_SIZE(mtu) &&
	    tx->head < tx->tail)
		return -EBUSY;

	netif_tx_lock_bh(ndev);
	if (in->tx.size - tx->head < IMX_SHM_NET_FRAME_SIZE(mtu))
		tx->head = 0;
	netif_tx_unlock_bh(ndev);

	ndev->mtu = mtu;

	return 0;
}

#ifdef CONFIG_NET_POLL_CONTROLLER
static void imx_shm_net_poll_controller(struct net_device *ndev)
{
	struct imx_shm_net *in = netdev_priv(ndev);

	napi_schedule(&in->napi);
}
#endif

static const struct net_device_ops imx_shm_net_ops = {
		.ndo_open		= imx_shm_net_open,
		.ndo_stop		= imx_shm_net_stop,
		.ndo_start_xmit		= imx_shm_net_xmit,
		.ndo_change_mtu		= imx_shm_net_change_mtu,
		.ndo_set_mac_address	= eth_mac_addr,
		.ndo_validate_addr	= eth_validate_addr,
#ifdef CONFIG_NET_POLL_CONTROLLER
		.ndo_poll_controller	= imx_shm_net_poll_controller,
#endif
};

static const char imx_shm_net_stats[][ETH_GSTRING_LEN] = {
		"interrupts",
		"tx_packets",
		"tx_notify",
		"tx_pause",
		"rx_packets",
		"rx_notify",
		"napi_poll",
		"napi_complete",
		"napi_poll_0",
		"napi_poll_1",
		"napi_poll_2",
		"napi_poll_4",
		"napi_poll_8",
		"napi_poll_16",
		"napi_poll_32",
		"napi_poll_64",
		"napi_poll_128",
		"napi_poll_256",
};

#define NUM_STATS ARRAY_SIZE(imx_shm_net_stats)

static int imx_shm_net_get_sset_count(struct net_device *ndev, int sset)
{
	if (sset == ETH_SS_STATS)
		return NUM_STATS;

	return -EOPNOTSUPP;
}

static void imx_shm_net_get_strings(struct net_device *ndev, u32 sset, u8 *buf)
{
	if (sset == ETH_SS_STATS)
		memcpy(buf, &imx_shm_net_stats, sizeof(imx_shm_net_stats));
}

static void imx_shm_net_get_ethtool_stats(struct net_device *ndev,
					  struct ethtool_stats *estats, u64 *st)
{
	struct imx_shm_net *in = netdev_priv(ndev);
	unsigned int n = 0;
	unsigned int i;

	st[n++] = in->stats.interrupts;
	st[n++] = in->stats.tx_packets;
	st[n++] = in->stats.tx_notify;
	st[n++] = in->stats.tx_pause;
	st[n++] = in->stats.rx_packets;
	st[n++] = in->stats.rx_notify;
	st[n++] = in->stats.napi_poll;
	st[n++] = in->stats.napi_complete;

	for (i = 0; i < ARRAY_SIZE(in->stats.napi_poll_n); i++)
		st[n++] = in->stats.napi_poll_n[i];

	memset(&in->stats, 0, sizeof(in->stats));
}

#define IMX_SHM_NET_REGS_LEN	(3 * sizeof(u32) + 6 * sizeof(u16))

static int imx_shm_net_get_regs_len(struct net_device *ndev)
{
	return IMX_SHM_NET_REGS_LEN;
}

static void imx_shm_net_get_regs(struct net_device *ndev,
				 struct ethtool_regs *regs, void *p)
{
	struct imx_shm_net *in = netdev_priv(ndev);
	u32 *reg32 = p;
	u16 *reg16;

	*reg32++ = in->regs.lstate;
	*reg32++ = in->regs.rstate;
	*reg32++ = in->qlen;

	reg16 = (u16 *)reg32;

	*reg16++ = in->tx.vr.avail ? in->tx.vr.avail->idx : 0;
	*reg16++ = in->tx.vr.used ? in->tx.vr.used->idx : 0;
	*reg16++ = in->tx.vr.avail ? vring_avail_event(&in->tx.vr) : 0;

	*reg16++ = in->rx.vr.avail ? in->rx.vr.avail->idx : 0;
	*reg16++ = in->rx.vr.used ? in->rx.vr.used->idx : 0;
	*reg16++ = in->rx.vr.avail ? vring_avail_event(&in->rx.vr) : 0;
}

static int imx_shm_partition_notify(struct notifier_block *nb,
				    unsigned long event, void *group)
{
	struct imx_shm_net *in = NULL;
	struct list_head *pos;
	struct net_device *ndev;

	/* Ignore other irqs */
	if (*(u8 *)group != SC_IRQ_GROUP_REBOOTED)
		return 0;

	pr_debug("%s: Partition reset detected!\n", DRV_NAME);

	/* browse all imx_shm_net devices */
	list_for_each(pos, &imx_shm_net_head) {
		in = list_entry(pos, struct imx_shm_net, isn_node);

		if (event & BIT(in->mub_partition)) {
			mutex_lock(&in->state_lock);
			in->remote_message = IMX_SHM_NET_STATE_RESET;
			in->message_state = MESS_STATE_NEW;
			mutex_unlock(&in->state_lock);

			ndev = platform_get_drvdata(in->pdev);

			imx_shm_net_check_state(ndev);
			napi_schedule_irqoff(&in->napi);

			dev_info(&in->pdev->dev, "Partition %d reset!\n",
				 in->mub_partition);
		}
	}

	return 0;
}

static void imx_shm_rx_callback(struct mbox_client *c, void *msg)
{
	u32 *data = msg;
	struct imx_shm_net *isndev = container_of(c,
			struct imx_shm_net, cl);
	struct net_device *ndev = platform_get_drvdata(isndev->pdev);

	isndev->stats.interrupts++;

	/* get message from receive buffer */
	mutex_lock(&isndev->state_lock);
	isndev->remote_message = *data;
	if (isndev->message_state == MESS_STATE_NEW)
		dev_dbg(&isndev->pdev->dev, "RX message overwritten while not yet processed!");
	isndev->message_state = MESS_STATE_NEW;

	imx_shm_net_check_state(ndev);
	mutex_unlock(&isndev->state_lock);

	napi_schedule_irqoff(&isndev->napi);
}

static int imx_shm_xtr_channel_init(struct imx_shm_net *isndev)
{
	struct platform_device *pdev = isndev->pdev;
	struct device *dev = &pdev->dev;
	struct mbox_client *cl;
	int ret = 0;

	cl = &isndev->cl;
	cl->dev = dev;
	cl->tx_block = false;
	cl->tx_tout = IMX_SHM_NET_MU_TIMEOUT;
	cl->knows_txdone = false;
	cl->rx_callback = imx_shm_rx_callback;

	/* if channels are not initialized yet, do it */
	if (!isndev->tx_ch || (IS_ERR(isndev->tx_ch)))
		isndev->tx_ch = mbox_request_channel_byname(cl, "tx");

	if (IS_ERR(isndev->tx_ch)) {
		ret = PTR_ERR(isndev->tx_ch);
		dev_info(cl->dev, "failed to request mbox tx chan, ret %d\n",
			 ret);
		goto err_out;
	}
	if (!isndev->rx_ch || (IS_ERR(isndev->rx_ch)))
		isndev->rx_ch = mbox_request_channel_byname(cl, "rx");

	if (IS_ERR(isndev->rx_ch)) {
		ret = PTR_ERR(isndev->rx_ch);
		dev_info(cl->dev, "failed to request mbox rx chan, ret %d\n",
			 ret);
		goto err_out;
	}

	return ret;

err_out:
	if (!IS_ERR(isndev->tx_ch))
		mbox_free_channel(isndev->tx_ch);
	if (!IS_ERR(isndev->rx_ch))
		mbox_free_channel(isndev->rx_ch);

	return ret;
}

/* enable reset notification MU */
static int mu_enable_reset_irq(struct net_device *ndev)
{
	int sciErr;
	struct imx_shm_net *isndev;
	struct device_node *np;

	isndev = netdev_priv(ndev);
	np = isndev->pdev->dev.of_node;

	/* Get muB partition id and enable irq in SCFW then */
	if (of_property_read_u32(np, "mub-partition",
				 &isndev->mub_partition))
		isndev->mub_partition = IMX_SHM_NET_DEFAULT_PART;
	dev_dbg(&isndev->pdev->dev, "watching reset from partition %d\n",
		isndev->mub_partition);

#ifdef CONFIG_IMX_SCU
	/* Request for the partition reset interrupt. */
	sciErr = imx_scu_irq_group_enable(SC_IRQ_GROUP_REBOOTED,
					  BIT(isndev->mub_partition), true);
	if (sciErr)
		dev_warn(&isndev->pdev->dev, "Cannot request partition reset interrupt\n");

	isndev->pnotifier.notifier_call = imx_shm_partition_notify;
	sciErr = imx_scu_irq_register_notifier(&isndev->pnotifier);
	if (sciErr) {
		imx_scu_irq_group_enable(SC_IRQ_GROUP_REBOOTED,
					 BIT(isndev->mub_partition), false);
		dev_warn(&isndev->pdev->dev, "Failed to register partition reset notifier\n");
	}
#endif
	return 0;
}

static const struct ethtool_ops imx_shm_net_ethtool_ops = {
		.get_sset_count		= imx_shm_net_get_sset_count,
		.get_strings		= imx_shm_net_get_strings,
		.get_ethtool_stats	= imx_shm_net_get_ethtool_stats,
		.get_regs_len		= imx_shm_net_get_regs_len,
		.get_regs		= imx_shm_net_get_regs,
};

static int imx_shm_net_probe(struct platform_device *pdev)
{
	struct net_device *ndev = NULL;
	struct imx_shm_net *in;
	resource_size_t shmaddr;
	resource_size_t shmlen;
	char *device_name;
	void *shm = NULL;
	u32 ivpos;
	int ret;

	/* check if 1st probe or another attempt after EAGAIN */
	if (pdev->dev.driver_data) {
		dev_dbg(&pdev->dev, "Retrying connection...\n");
		in = netdev_priv(platform_get_drvdata(pdev));
		goto retry;
	}

	if (of_property_read_bool(pdev->dev.of_node, "rxfirst")) {
		ivpos = 1;
		dev_info(&pdev->dev, "queue position is RX first\n");
	} else {
		ivpos = 0;
		dev_info(&pdev->dev, "queue position is TX first\n");
	}

	/* get shared coherent memory for buffers */
	if (of_reserved_mem_device_init(&pdev->dev)) {
		dev_err(&pdev->dev,
			"dev doesn't have specific DMA pool.\n");
		return -ENOMEM;
	}
	shmlen = IMX_SHM_NET_DMA_SIZE;
	shm = dma_alloc_coherent(&pdev->dev, IMX_SHM_NET_DMA_SIZE,
				 &shmaddr, GFP_KERNEL);
	if (!shm || !shmaddr)
		return -ENOMEM;

	dev_info(&pdev->dev, "allocated %d bytes in coherent mem @ 0x%x\n",
		 (uint)shmlen, (uint)shmaddr);

	device_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s[%s]", DRV_NAME,
				     dev_name(&pdev->dev));
	if (!device_name) {
		ret = -ENOMEM;
		goto err_free_dma;
	}

	ndev = alloc_etherdev(sizeof(*in));
	if (!ndev) {
		ret = -ENOMEM;
		goto err_free_dma;
	}
	dev_info(&pdev->dev, "allocated ethernet device %s\n", ndev->name);

	platform_set_drvdata(pdev, ndev);
	SET_NETDEV_DEV(ndev, &pdev->dev);

	/* get struct 'imx_shm_net' stored as private data in ndev */
	in = netdev_priv(ndev);

	list_add(&in->isn_node, &imx_shm_net_head);

	in->shm = shm;
	in->shmaddr = shmaddr;
	in->shmlen = shmlen;
	in->pdev = pdev;
	in->regs.ivpos = ivpos;
	in->regs.rstate = IMX_SHM_NET_STATE_RESET;
	in->remote_message = IMX_SHM_NET_STATE_RESET;
	spin_lock_init(&in->tx_free_lock);
	spin_lock_init(&in->tx_clean_lock);
	mutex_init(&in->state_lock);

	ret = imx_shm_net_calc_qsize(ndev);
	if (ret)
		goto err_free;

	in->state_wq = alloc_ordered_workqueue(device_name, 0);
	if (!in->state_wq) {
		ret = -ENOMEM;
		goto err_free;
	}

	INIT_WORK(&in->state_work, imx_shm_net_state_change);

	eth_random_addr(ndev->dev_addr);
	ndev->netdev_ops = &imx_shm_net_ops;
	ndev->ethtool_ops = &imx_shm_net_ethtool_ops;
	ndev->mtu = min_t(u32, IMX_SHM_NET_MTU_DEF, in->qsize / 16);
	ndev->hw_features = NETIF_F_HW_CSUM | NETIF_F_SG;
	ndev->features = ndev->hw_features;

	netif_carrier_off(ndev);
	netif_napi_add(ndev, &in->napi, imx_shm_net_poll, NAPI_POLL_WEIGHT);

	ret = register_netdev(ndev);
	if (ret)
		goto err_wq;

	/* initialize Mailbox for RX/TX */
	ret = imx_shm_xtr_channel_init(in);
	if (ret) {
		dev_err(&in->pdev->dev, "unable to initialize Mailbox.\n");
		/* MU may not be ready yet, need to try later on */
		ret = -EPROBE_DEFER;
		goto err_unregister;
	}

	/* enable peer's reset notification */
	ret = mu_enable_reset_irq(ndev);
	if (ret)
		goto err_reset_irq;

	dev_info(&in->pdev->dev,
		 "Mailbox is ready for cross core communication!\n");

retry:
	/* notify reset */
	mutex_lock(&in->state_lock);
	in->regs.lstate = IMX_SHM_NET_STATE_RESET;
	in->message_state = MESS_STATE_NEW;
	mutex_unlock(&in->state_lock);

	/* only device with queue position TXfirst sends the first message */
	if (!ivpos) {
		ret = mbox_send_message(in->tx_ch, &in->regs.lstate);
		if (ret < 0)
			dev_err(&pdev->dev, "%s first message error=%d!\n",
				__func__, ret);

		dev_dbg(&pdev->dev, "%s sent first message\n", __func__);
	}

	return 0;

err_reset_irq:
#ifdef CONFIG_IMX_SCU
	imx_scu_irq_unregister_notifier(&in->pnotifier);
#endif

err_unregister:
	unregister_netdev(ndev);

err_wq:
	destroy_workqueue(in->state_wq);

err_free:
	list_del(&in->isn_node);
	free_netdev(ndev);

err_free_dma:
	dma_free_coherent(&pdev->dev, shmlen, shm, shmaddr);

	return ret;
}

static int imx_shm_net_remove(struct platform_device *pdev)
{
	struct net_device *ndev = platform_get_drvdata(pdev);
	struct imx_shm_net *in = netdev_priv(ndev);
	int ret;

	/* notify reset */
	in->regs.lstate = IMX_SHM_NET_STATE_RESET;
	ret = mbox_send_message(in->tx_ch, &in->regs.lstate);
	if (ret < 0)
		dev_err(&in->pdev->dev, "%s send message error=%d!\n",
			__func__, ret);

	if (!IS_ERR(in->tx_ch))
		mbox_free_channel(in->tx_ch);
	if (!IS_ERR(in->rx_ch))
		mbox_free_channel(in->rx_ch);

	imx_scu_irq_unregister_notifier(&in->pnotifier);

	dma_free_coherent(&pdev->dev, in->shmlen, in->shm, in->shmaddr);

	/* remove imx_shm_net's node from list */
	list_del(&in->isn_node);

	unregister_netdev(ndev);
	cancel_work_sync(&in->state_work);
	destroy_workqueue(in->state_wq);
	free_netdev(ndev);

	return 0;
}

#ifdef CONFIG_PM_SLEEP
static int imx_shm_net_pm_suspend(struct device *dev)
{
	struct net_device *ndev = dev_get_drvdata(dev);
	struct imx_shm_net *in = netdev_priv(ndev);

	dev_info(&in->pdev->dev, "entering %s\n", __func__);

	return 0;
}

static int imx_shm_net_pm_resume(struct device *dev)
{
	struct net_device *ndev = dev_get_drvdata(dev);
	struct imx_shm_net *in = netdev_priv(ndev);

	dev_info(&in->pdev->dev, "entering %s\n", __func__);

	return 0;
}
#endif /* CONFIG_PM_SLEEP */

static const struct of_device_id of_isn_id[] = {
	{ .compatible = "fsl,imx-shmem-net", },
	{}
};
MODULE_DEVICE_TABLE(of, of_isn_id);

static const struct dev_pm_ops imx_shm_net_pm_ops = {
	SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(imx_shm_net_pm_suspend,
				      imx_shm_net_pm_resume)
};

static struct platform_driver imx_shm_net_driver = {
	.probe		= imx_shm_net_probe,
	.remove		= imx_shm_net_remove,
	.driver		= {
		.name	= DRV_NAME,
		.owner	= THIS_MODULE,
		.pm	= &imx_shm_net_pm_ops,
		.of_match_table = of_isn_id,
	},
};

static int __init imx_shm_net_init(void)
{
	int ret;

	INIT_LIST_HEAD(&imx_shm_net_head);

	/* Add the device to the platform. */
	ret = platform_driver_register(&imx_shm_net_driver);

	return ret;
}

static void __exit imx_shm_net_cleanup(void)
{
	platform_driver_unregister(&imx_shm_net_driver);
}

module_init(imx_shm_net_init);

module_exit(imx_shm_net_cleanup);

MODULE_AUTHOR("Sebastien Fagard <sebastien.fagard@nxp.com>");
MODULE_LICENSE("GPL");