summaryrefslogtreecommitdiff
path: root/drivers/soc/aspeed/aspeed-mctp.c
blob: befee29501b30510ffd97bec93f91d1b5baa1151 (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
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
// SPDX-License-Identifier: GPL-2.0
// Copyright (c) 2020, Intel Corporation.

#include <linux/aspeed-mctp.h>
#include <linux/bitfield.h>
#include <linux/dma-mapping.h>
#include <linux/interrupt.h>
#include <linux/init.h>
#include <linux/io.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/list_sort.h>
#include <linux/mfd/syscon.h>
#include <linux/miscdevice.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/of_platform.h>
#include <linux/pci.h>
#include <linux/poll.h>
#include <linux/ptr_ring.h>
#include <linux/regmap.h>
#include <linux/reset.h>
#include <linux/slab.h>
#include <linux/swab.h>
#include <linux/uaccess.h>
#include <linux/workqueue.h>

#include <uapi/linux/aspeed-mctp.h>

/* AST2600 MCTP Controller registers */
#define ASPEED_MCTP_CTRL	0x000
#define  TX_CMD_TRIGGER		BIT(0)
#define  RX_CMD_READY		BIT(4)
#define  MATCHING_EID		BIT(9)

#define ASPEED_MCTP_TX_CMD	0x004
#define ASPEED_MCTP_RX_CMD	0x008

#define ASPEED_MCTP_INT_STS	0x00c
#define ASPEED_MCTP_INT_EN	0x010
#define  TX_CMD_SENT_INT	BIT(0)
#define  TX_CMD_LAST_INT	BIT(1)
#define  TX_CMD_WRONG_INT	BIT(2)
#define  RX_CMD_RECEIVE_INT	BIT(8)
#define  RX_CMD_NO_MORE_INT	BIT(9)

#define ASPEED_MCTP_EID		0x014
#define ASPEED_MCTP_OBFF_CTRL	0x018

#define ASPEED_MCTP_ENGINE_CTRL		0x01c
#define  TX_MAX_PAYLOAD_SIZE_SHIFT	0
#define  TX_MAX_PAYLOAD_SIZE_MASK	GENMASK(1, TX_MAX_PAYLOAD_SIZE_SHIFT)
#define  TX_MAX_PAYLOAD_SIZE(x) \
	(((x) << TX_MAX_PAYLOAD_SIZE_SHIFT) & TX_MAX_PAYLOAD_SIZE_MASK)
#define  RX_MAX_PAYLOAD_SIZE_SHIFT	4
#define  RX_MAX_PAYLOAD_SIZE_MASK	GENMASK(5, RX_MAX_PAYLOAD_SIZE_SHIFT)
#define  RX_MAX_PAYLOAD_SIZE(x) \
	(((x) << RX_MAX_PAYLOAD_SIZE_SHIFT) & RX_MAX_PAYLOAD_SIZE_MASK)
#define  FIFO_LAYOUT_SHIFT		8
#define  FIFO_LAYOUT_MASK		GENMASK(9, FIFO_LAYOUT_SHIFT)
#define  FIFO_LAYOUT(x) \
	(((x) << FIFO_LAYOUT_SHIFT) & FIFO_LAYOUT_MASK)

#define ASPEED_MCTP_RX_BUF_ADDR		0x020
#define ASPEED_MCTP_RX_BUF_SIZE		0x024
#define ASPEED_MCTP_RX_BUF_RD_PTR	0x028
#define  UPDATE_RX_RD_PTR		BIT(31)
#define  RX_BUF_RD_PTR_MASK		GENMASK(11, 0)
#define ASPEED_MCTP_RX_BUF_WR_PTR	0x02c
#define  RX_BUF_WR_PTR_MASK		GENMASK(11, 0)

#define ASPEED_MCTP_TX_BUF_ADDR		0x030
#define ASPEED_MCTP_TX_BUF_SIZE		0x034
#define ASPEED_MCTP_TX_BUF_RD_PTR	0x038
#define  UPDATE_TX_RD_PTR		BIT(31)
#define  TX_BUF_RD_PTR_MASK		GENMASK(11, 0)
#define ASPEED_MCTP_TX_BUF_WR_PTR	0x03c
#define  TX_BUF_WR_PTR_MASK		GENMASK(11, 0)

#define ADDR_LEN	(BIT(26) - 1)
#define DATA_ADDR(x)	(((x) >> 4) & ADDR_LEN)

/* TX command */
#define TX_LAST_CMD		BIT(31)
#define TX_DATA_ADDR_SHIFT	4
#define TX_DATA_ADDR_MASK	GENMASK(30, TX_DATA_ADDR_SHIFT)
#define TX_DATA_ADDR(x) \
	((DATA_ADDR(x) << TX_DATA_ADDR_SHIFT) & TX_DATA_ADDR_MASK)
#define TX_RESERVED_1_MASK	GENMASK(1, 0) /* must be 1 */
#define TX_RESERVED_1		1
#define TX_STOP_AFTER_CMD	BIT(16)
#define TX_INTERRUPT_AFTER_CMD	BIT(15)
#define TX_PACKET_SIZE_SHIFT	2
#define TX_PACKET_SIZE_MASK	GENMASK(12, TX_PACKET_SIZE_SHIFT)
#define TX_PACKET_SIZE(x) \
	(((x) << TX_PACKET_SIZE_SHIFT) & TX_PACKET_SIZE_MASK)
#define TX_RESERVED_0_MASK	GENMASK(1, 0) /* MBZ */
#define TX_RESERVED_0		0

/* RX command */
#define RX_INTERRUPT_AFTER_CMD	BIT(2)
#define RX_DATA_ADDR_SHIFT	4
#define RX_DATA_ADDR_MASK	GENMASK(30, RX_DATA_ADDR_SHIFT)
#define RX_DATA_ADDR(x) \
	((DATA_ADDR(x) << RX_DATA_ADDR_SHIFT) & RX_DATA_ADDR_MASK)

/* HW buffer sizes */
#define TX_PACKET_COUNT		48
#define RX_PACKET_COUNT		96
#define TX_MAX_PACKET_COUNT	(TX_BUF_RD_PTR_MASK + 1)
#define RX_MAX_PACKET_COUNT	(RX_BUF_RD_PTR_MASK + 1)

#define TX_CMD_BUF_SIZE \
	PAGE_ALIGN(TX_PACKET_COUNT * sizeof(struct aspeed_mctp_tx_cmd))
#define TX_DATA_BUF_SIZE \
	 PAGE_ALIGN(TX_PACKET_COUNT * sizeof(struct mctp_pcie_packet_data))
#define RX_CMD_BUF_SIZE PAGE_ALIGN(RX_PACKET_COUNT * sizeof(u32))
#define RX_DATA_BUF_SIZE \
	PAGE_ALIGN(RX_PACKET_COUNT * sizeof(struct mctp_pcie_packet_data))

/* Per client packet cache sizes */
#define RX_RING_COUNT		64
#define TX_RING_COUNT		64

/* PCIe Host Controller registers */
#define ASPEED_PCIE_MISC_STS_1	0x0c4

/* PCI address definitions */
#define PCI_DEV_NUM_MASK	GENMASK(4, 0)
#define PCI_BUS_NUM_SHIFT	5
#define PCI_BUS_NUM_MASK	GENMASK(12, PCI_BUS_NUM_SHIFT)
#define GET_PCI_DEV_NUM(x)	((x) & PCI_DEV_NUM_MASK)
#define GET_PCI_BUS_NUM(x)	(((x) & PCI_BUS_NUM_MASK) >> PCI_BUS_NUM_SHIFT)

/* MCTP header definitions */
#define MCTP_HDR_SRC_EID_OFFSET		14
#define MCTP_HDR_TAG_OFFSET		15
#define MCTP_HDR_SOM			BIT(7)
#define MCTP_HDR_EOM			BIT(6)
#define MCTP_HDR_SOM_EOM		(MCTP_HDR_SOM | MCTP_HDR_EOM)
#define MCTP_HDR_TYPE_OFFSET		16
#define MCTP_HDR_TYPE_CONTROL		0
#define MCTP_HDR_TYPE_VDM_PCI		0x7e
#define MCTP_HDR_TYPE_SPDM		0x5
#define MCTP_HDR_TYPE_BASE_LAST		MCTP_HDR_TYPE_SPDM
#define MCTP_HDR_VENDOR_OFFSET		17
#define MCTP_HDR_VDM_TYPE_OFFSET	19

/* FIXME: ast2600 supports variable max transmission unit */
#define ASPEED_MCTP_MTU 64

struct aspeed_mctp_tx_cmd {
	u32 tx_lo;
	u32 tx_hi;
};

struct mctp_buffer {
	void *vaddr;
	dma_addr_t dma_handle;
};

struct mctp_channel {
	struct mctp_buffer data;
	struct mctp_buffer cmd;
	struct tasklet_struct tasklet;
	u32 buffer_count;
	u32 rd_ptr;
	u32 wr_ptr;
	bool stopped;
};

struct aspeed_mctp {
	struct device *dev;
	struct regmap *map;
	struct reset_control *reset;
	struct mctp_channel tx;
	struct mctp_channel rx;
	struct list_head clients;
	struct mctp_client *default_client;
	struct list_head mctp_type_handlers;
	/*
	 * clients_lock protects list of clients, list of type handlers
	 * and default client
	 */
	spinlock_t clients_lock;
	struct list_head endpoints;
	size_t endpoints_count;
	/*
	 * endpoints_lock protects list of endpoints
	 */
	struct mutex endpoints_lock;
	struct {
		struct regmap *map;
		struct delayed_work rst_dwork;
		bool need_uevent;
		u16 bdf;
	} pcie;
	struct {
		bool enable;
		bool warmup;
		int packet_counter;
	} rx_runaway_wa;
	u8 eid;
	struct platform_device *peci_mctp;
};

struct mctp_client {
	struct kref ref;
	struct aspeed_mctp *priv;
	struct ptr_ring tx_queue;
	struct ptr_ring rx_queue;
	struct list_head link;
	wait_queue_head_t wait_queue;
};

struct mctp_type_handler {
	u8 mctp_type;
	u16 pci_vendor_id;
	u16 vdm_type;
	u16 vdm_mask;
	struct mctp_client *client;
	struct list_head link;
};

union aspeed_mctp_eid_data_info {
	struct aspeed_mctp_eid_info eid_info;
	struct aspeed_mctp_eid_ext_info eid_ext_info;
};

enum mctp_address_type {
	ASPEED_MCTP_GENERIC_ADDR_FORMAT = 0,
	ASPEED_MCTP_EXTENDED_ADDR_FORMAT = 1
};

struct aspeed_mctp_endpoint {
	union  aspeed_mctp_eid_data_info data;
	struct list_head link;
};

struct kmem_cache *packet_cache;

void *aspeed_mctp_packet_alloc(gfp_t flags)
{
	return kmem_cache_alloc(packet_cache, flags);
}
EXPORT_SYMBOL_GPL(aspeed_mctp_packet_alloc);

void aspeed_mctp_packet_free(void *packet)
{
	kmem_cache_free(packet_cache, packet);
}
EXPORT_SYMBOL_GPL(aspeed_mctp_packet_free);

/*
 * HW produces and expects VDM header in little endian and payload in network order.
 * To allow userspace to use network order for the whole packet, PCIe VDM header needs
 * to be swapped.
 */
static void aspeed_mctp_swap_pcie_vdm_hdr(struct mctp_pcie_packet_data *data)
{
	int i;

	for (i = 0; i < PCIE_VDM_HDR_SIZE_DW; i++)
		data->hdr[i] = swab32(data->hdr[i]);
}

static void aspeed_mctp_rx_trigger(struct mctp_channel *rx)
{
	struct aspeed_mctp *priv = container_of(rx, typeof(*priv), rx);

	/*
	 * Even though rx_buf_addr doesn't change, if we don't do the write
	 * here, the HW doesn't trigger RX. We're also clearing the
	 * RX_CMD_READY bit, otherwise we're observing a rare case where
	 * trigger isn't registered by the HW, and we're ending up with stuck
	 * HW (not reacting to wr_ptr writes).
	 * Also, note that we're writing 0 as wr_ptr. If we're writing other
	 * value, the HW behaves in a bizarre way that's hard to explain...
	 */
	regmap_update_bits(priv->map, ASPEED_MCTP_CTRL, RX_CMD_READY, 0);
	regmap_write(priv->map, ASPEED_MCTP_RX_BUF_ADDR, rx->cmd.dma_handle);
	regmap_write(priv->map, ASPEED_MCTP_RX_BUF_WR_PTR, 0);

	/* After re-enabling RX we need to restart WA logic */
	if (priv->rx_runaway_wa.enable) {
		priv->rx_runaway_wa.warmup = true;
		priv->rx_runaway_wa.packet_counter = 0;
		priv->rx.buffer_count = RX_PACKET_COUNT;
	}

	regmap_update_bits(priv->map, ASPEED_MCTP_CTRL, RX_CMD_READY,
			   RX_CMD_READY);
}

static void aspeed_mctp_tx_trigger(struct mctp_channel *tx, bool notify)
{
	struct aspeed_mctp *priv = container_of(tx, typeof(*priv), tx);

	if (notify) {
		struct aspeed_mctp_tx_cmd *last_cmd;

		last_cmd = (struct aspeed_mctp_tx_cmd *)tx->cmd.vaddr +
			   (tx->wr_ptr - 1) % TX_PACKET_COUNT;
		last_cmd->tx_lo |= TX_INTERRUPT_AFTER_CMD;
	}

	regmap_write(priv->map, ASPEED_MCTP_TX_BUF_WR_PTR, tx->wr_ptr);
	regmap_update_bits(priv->map, ASPEED_MCTP_CTRL, TX_CMD_TRIGGER,
			   TX_CMD_TRIGGER);
}

static void aspeed_mctp_emit_tx_cmd(struct mctp_channel *tx,
				    struct mctp_pcie_packet *packet)
{
	struct aspeed_mctp_tx_cmd *tx_cmd =
		(struct aspeed_mctp_tx_cmd *)tx->cmd.vaddr + tx->wr_ptr;
	u32 packet_sz_dw = packet->size / sizeof(u32) -
		sizeof(packet->data.hdr) / sizeof(u32);
	u32 offset = tx->wr_ptr * sizeof(packet->data);

	aspeed_mctp_swap_pcie_vdm_hdr(&packet->data);

	memcpy((u8 *)tx->data.vaddr + offset, &packet->data,
	       sizeof(packet->data));

	tx_cmd->tx_lo = TX_PACKET_SIZE(packet_sz_dw);
	tx_cmd->tx_hi = TX_RESERVED_1;
	tx_cmd->tx_hi |= TX_DATA_ADDR(tx->data.dma_handle + offset);

	tx->wr_ptr = (tx->wr_ptr + 1) % TX_PACKET_COUNT;
}

static struct mctp_client *aspeed_mctp_client_alloc(struct aspeed_mctp *priv)
{
	struct mctp_client *client;

	client = kzalloc(sizeof(*client), GFP_KERNEL);
	if (!client)
		goto out;

	kref_init(&client->ref);
	client->priv = priv;
	ptr_ring_init(&client->tx_queue, TX_RING_COUNT, GFP_KERNEL);
	ptr_ring_init(&client->rx_queue, RX_RING_COUNT, GFP_ATOMIC);

out:
	return client;
}

static void aspeed_mctp_client_free(struct kref *ref)
{
	struct mctp_client *client = container_of(ref, typeof(*client), ref);

	ptr_ring_cleanup(&client->rx_queue, &aspeed_mctp_packet_free);
	ptr_ring_cleanup(&client->tx_queue, &aspeed_mctp_packet_free);

	kfree(client);
}

static void aspeed_mctp_client_get(struct mctp_client *client)
{
	lockdep_assert_held(&client->priv->clients_lock);

	kref_get(&client->ref);
}

static void aspeed_mctp_client_put(struct mctp_client *client)
{
	kref_put(&client->ref, &aspeed_mctp_client_free);
}

static struct mctp_client *
aspeed_mctp_find_handler(struct aspeed_mctp *priv,
			 struct mctp_pcie_packet *packet)
{
	struct mctp_type_handler *handler;
	u8 *hdr = (u8 *)packet->data.hdr;
	struct mctp_client *client = NULL;
	u8 mctp_type, som_eom;
	u16 vendor = 0;
	u16 vdm_type = 0;

	lockdep_assert_held(&priv->clients_lock);

	/*
	 * Middle and EOM fragments cannot be matched to MCTP type.
	 * For consistency do not match type for any fragmented messages.
	 */
	som_eom = hdr[MCTP_HDR_TAG_OFFSET] & MCTP_HDR_SOM_EOM;
	if (som_eom != MCTP_HDR_SOM_EOM)
		return NULL;

	mctp_type = hdr[MCTP_HDR_TYPE_OFFSET];
	if (mctp_type == MCTP_HDR_TYPE_VDM_PCI) {
		vendor = *((u16 *)&hdr[MCTP_HDR_VENDOR_OFFSET]);
		vdm_type = *((u16 *)&hdr[MCTP_HDR_VDM_TYPE_OFFSET]);
	}

	list_for_each_entry(handler, &priv->mctp_type_handlers, link) {
		if (handler->mctp_type == mctp_type &&
		    handler->pci_vendor_id == vendor &&
		    handler->vdm_type == (vdm_type & handler->vdm_mask)) {
			dev_dbg(priv->dev, "Found client for type %x vdm %x\n",
				mctp_type, handler->vdm_type);
			client = handler->client;
			break;
		}
	}
	return client;
}

static void aspeed_mctp_dispatch_packet(struct aspeed_mctp *priv,
					struct mctp_pcie_packet *packet)
{
	struct mctp_client *client;
	int ret;

	spin_lock(&priv->clients_lock);

	client = aspeed_mctp_find_handler(priv, packet);

	if (!client)
		client = priv->default_client;

	if (client)
		aspeed_mctp_client_get(client);

	spin_unlock(&priv->clients_lock);

	if (client) {
		ret = ptr_ring_produce(&client->rx_queue, packet);
		if (ret) {
			/*
			 * This can happen if client process does not
			 * consume packets fast enough
			 */
			dev_dbg(priv->dev, "Failed to store packet in client RX queue\n");
			aspeed_mctp_packet_free(packet);
		} else {
			wake_up_all(&client->wait_queue);
		}
		aspeed_mctp_client_put(client);
	} else {
		dev_dbg(priv->dev, "Failed to dispatch RX packet\n");
		aspeed_mctp_packet_free(packet);
	}
}

static void aspeed_mctp_tx_tasklet(unsigned long data)
{
	struct mctp_channel *tx = (struct mctp_channel *)data;
	struct aspeed_mctp *priv = container_of(tx, typeof(*priv), tx);
	struct mctp_client *client;
	bool trigger = false;
	bool full = false;
	u32 rd_ptr;

	regmap_write(priv->map, ASPEED_MCTP_TX_BUF_RD_PTR, UPDATE_RX_RD_PTR);
	regmap_read(priv->map, ASPEED_MCTP_TX_BUF_RD_PTR, &rd_ptr);
	rd_ptr &= TX_BUF_RD_PTR_MASK;

	spin_lock(&priv->clients_lock);

	list_for_each_entry(client, &priv->clients, link) {
		while (!(full = (tx->wr_ptr + 1) % TX_PACKET_COUNT == rd_ptr)) {
			struct mctp_pcie_packet *packet;

			packet = ptr_ring_consume(&client->tx_queue);
			if (!packet)
				break;

			aspeed_mctp_emit_tx_cmd(tx, packet);
			aspeed_mctp_packet_free(packet);
			trigger = true;
		}
	}

	spin_unlock(&priv->clients_lock);

	if (trigger)
		aspeed_mctp_tx_trigger(tx, full);
}

static void aspeed_mctp_rx_tasklet(unsigned long data)
{
	struct mctp_channel *rx = (struct mctp_channel *)data;
	struct aspeed_mctp *priv = container_of(rx, typeof(*priv), rx);
	struct mctp_pcie_packet *rx_packet;
	struct mctp_pcie_packet_data *rx_buf;
	u32 hw_read_ptr;
	u32 *hdr;

	/* Trigger HW read pointer update, must be done before RX loop */
	regmap_write(priv->map, ASPEED_MCTP_RX_BUF_RD_PTR, UPDATE_RX_RD_PTR);

	/*
	 * XXX: Using rd_ptr obtained from HW is unreliable so we need to
	 * maintain the state of buffer on our own by peeking into the buffer
	 * and checking where the packet was written.
	 */
	rx_buf = (struct mctp_pcie_packet_data *)rx->data.vaddr;
	hdr = (u32 *)&rx_buf[rx->wr_ptr];

	if (priv->rx_runaway_wa.warmup && !*hdr) {
		u32 tmp_wr_ptr = rx->wr_ptr;

		/*
		 * HACK: Right after start the RX hardware can put received
		 * packet into an unexpected offset - in order to locate
		 * received packet driver has to scan all RX data buffers.
		 */
		do {
			tmp_wr_ptr = (tmp_wr_ptr + 1) % RX_PACKET_COUNT;

			hdr = (u32 *)&rx_buf[tmp_wr_ptr];
		} while (!*hdr && tmp_wr_ptr != rx->wr_ptr);

		if (tmp_wr_ptr != rx->wr_ptr) {
			dev_dbg(priv->dev, "Runaway RX packet found %d -> %d\n",
				rx->wr_ptr, tmp_wr_ptr);
			rx->wr_ptr = tmp_wr_ptr;
		}

		/*
		 * Once we receive RX_PACKET_COUNT packets, hardware is
		 * guaranteed to use (RX_PACKET_COUNT - 4) buffers. Decrease
		 * buffer count by 4, then we can turn off scanning of RX
		 * buffers. RX buffer scanning should be enabled every time
		 * RX hardware is started.
		 * This is just a performance optimization - we could keep
		 * scanning RX buffers forever, but under heavy traffic it is
		 * fairly common that rx_tasklet is executed while RX buffer
		 * ring is empty.
		 */
		if (priv->rx_runaway_wa.packet_counter > RX_PACKET_COUNT) {
			priv->rx_runaway_wa.warmup = false;
			rx->buffer_count = RX_PACKET_COUNT - 4;
		}
	}

	while (*hdr != 0) {
		rx_packet = aspeed_mctp_packet_alloc(GFP_ATOMIC);
		if (rx_packet) {
			memcpy(&rx_packet->data, hdr, sizeof(rx_packet->data));

			aspeed_mctp_swap_pcie_vdm_hdr(&rx_packet->data);

			aspeed_mctp_dispatch_packet(priv, rx_packet);
		} else {
			dev_dbg(priv->dev, "Failed to allocate RX packet\n");
		}

		*hdr = 0;
		rx->wr_ptr = (rx->wr_ptr + 1) % rx->buffer_count;
		hdr = (u32 *)&rx_buf[rx->wr_ptr];

		priv->rx_runaway_wa.packet_counter++;
	}

	/*
	 * Update HW write pointer, this can be done only after driver consumes
	 * packets from RX ring.
	 */
	regmap_read(priv->map, ASPEED_MCTP_RX_BUF_RD_PTR, &hw_read_ptr);
	hw_read_ptr &= RX_BUF_RD_PTR_MASK;
	regmap_write(priv->map, ASPEED_MCTP_RX_BUF_WR_PTR, (hw_read_ptr));

	dev_dbg(priv->dev, "RX hw ptr %02d, sw ptr %2d\n",
		hw_read_ptr, rx->wr_ptr);

	/* Kick RX if it was stopped due to ring full condition */
	if (rx->stopped) {
		regmap_update_bits(priv->map, ASPEED_MCTP_CTRL, RX_CMD_READY,
				   RX_CMD_READY);
		rx->stopped = false;
	}
}

static void aspeed_mctp_rx_chan_init(struct mctp_channel *rx)
{
	struct aspeed_mctp *priv = container_of(rx, typeof(*priv), rx);
	u32 *rx_cmd = (u32 *)rx->cmd.vaddr;
	u32 data_size = sizeof(struct mctp_pcie_packet_data);
	u32 hw_rx_count = RX_PACKET_COUNT;
	int i;

	for (i = 0; i < RX_PACKET_COUNT; i++) {
		*rx_cmd = RX_DATA_ADDR(rx->data.dma_handle + data_size * i);
		*rx_cmd |= RX_INTERRUPT_AFTER_CMD;
		rx_cmd++;
	}

	rx->buffer_count = RX_PACKET_COUNT;

	/*
	 * TODO: Once read pointer runaway bug is fixed in some future AST2x00
	 * stepping then add chip revision detection and turn on this
	 * workaround only when needed
	 */
	priv->rx_runaway_wa.enable = true;

	/*
	 * Hardware does not wrap around ASPEED_MCTP_RX_BUF_SIZE
	 * correctly - we have to set number of buffers to n/4 -1
	 */
	if (priv->rx_runaway_wa.enable)
		hw_rx_count = (RX_PACKET_COUNT / 4 - 1);

	regmap_write(priv->map, ASPEED_MCTP_RX_BUF_SIZE, hw_rx_count);
}

static void aspeed_mctp_tx_chan_init(struct mctp_channel *tx)
{
	struct aspeed_mctp *priv = container_of(tx, typeof(*priv), tx);

	tx->wr_ptr = 0;
	regmap_update_bits(priv->map, ASPEED_MCTP_CTRL, TX_CMD_TRIGGER, 0);
	regmap_write(priv->map, ASPEED_MCTP_TX_BUF_SIZE, TX_PACKET_COUNT);
	regmap_write(priv->map, ASPEED_MCTP_TX_BUF_WR_PTR, 0);
	regmap_write(priv->map, ASPEED_MCTP_TX_BUF_ADDR, tx->cmd.dma_handle);
}

struct mctp_client *aspeed_mctp_create_client(struct aspeed_mctp *priv)
{
	struct mctp_client *client;

	client = aspeed_mctp_client_alloc(priv);
	if (!client)
		return NULL;

	init_waitqueue_head(&client->wait_queue);

	spin_lock_bh(&priv->clients_lock);
	list_add_tail(&client->link, &priv->clients);
	spin_unlock_bh(&priv->clients_lock);

	return client;
}
EXPORT_SYMBOL_GPL(aspeed_mctp_create_client);

static int aspeed_mctp_open(struct inode *inode, struct file *file)
{
	struct miscdevice *misc = file->private_data;
	struct platform_device *pdev = to_platform_device(misc->parent);
	struct aspeed_mctp *priv = platform_get_drvdata(pdev);
	struct mctp_client *client;

	client = aspeed_mctp_create_client(priv);
	if (!client)
		return -ENOMEM;

	file->private_data = client;

	return 0;
}

void aspeed_mctp_delete_client(struct mctp_client *client)
{
	struct aspeed_mctp *priv = client->priv;
	struct mctp_type_handler *handler, *tmp;

	spin_lock_bh(&priv->clients_lock);

	list_del(&client->link);

	if (priv->default_client == client)
		priv->default_client = NULL;

	list_for_each_entry_safe(handler, tmp, &priv->mctp_type_handlers,
				 link) {
		if (handler->client == client) {
			list_del(&handler->link);
			kfree(handler);
		}
	}
	spin_unlock_bh(&priv->clients_lock);

	/* Disable the tasklet to appease lockdep */
	local_bh_disable();
	aspeed_mctp_client_put(client);
	local_bh_enable();
}
EXPORT_SYMBOL_GPL(aspeed_mctp_delete_client);

static int aspeed_mctp_release(struct inode *inode, struct file *file)
{
	struct mctp_client *client = file->private_data;

	aspeed_mctp_delete_client(client);

	return 0;
}

static u16 _get_bdf(struct aspeed_mctp *priv)
{
	u16 bdf;

	bdf = READ_ONCE(priv->pcie.bdf);
	smp_rmb(); /* enforce ordering between flush and producer */

	return bdf;
}

static void _set_bdf(struct aspeed_mctp *priv, u16 bdf)
{
	smp_wmb(); /* enforce ordering between flush and producer */
	WRITE_ONCE(priv->pcie.bdf, bdf);
}

#define LEN_MASK_HI GENMASK(9, 8)
#define LEN_MASK_LO GENMASK(7, 0)
#define PCI_VDM_HDR_LEN_MASK_LO GENMASK(31, 24)
#define PCI_VDM_HDR_LEN_MASK_HI GENMASK(17, 16)
#define PCIE_VDM_HDR_REQUESTER_BDF_MASK GENMASK(31, 16)

int aspeed_mctp_send_packet(struct mctp_client *client,
			    struct mctp_pcie_packet *packet)
{
	struct aspeed_mctp *priv = client->priv;
	u32 *hdr_dw = (u32 *)packet->data.hdr;
	u8 *hdr = (u8 *)packet->data.hdr;
	u16 packet_data_sz_dw;
	u16 pci_data_len_dw;
	int ret;
	u16 bdf;

	bdf = _get_bdf(priv);
	if (bdf == 0)
		return -EIO;

	/*
	 * If the data size is different from contents of PCIe VDM header,
	 * aspeed_mctp_tx_cmd will be programmed incorrectly. This may cause
	 * MCTP HW to stop working.
	 */
	pci_data_len_dw = FIELD_PREP(LEN_MASK_LO, FIELD_GET(PCI_VDM_HDR_LEN_MASK_LO, hdr_dw[0])) |
			FIELD_PREP(LEN_MASK_HI, FIELD_GET(PCI_VDM_HDR_LEN_MASK_HI, hdr_dw[0]));
	if (pci_data_len_dw == 0) /* According to PCIe Spec, 0 means 1024 DW */
		pci_data_len_dw = SZ_1K;

	packet_data_sz_dw = packet->size / sizeof(u32) - sizeof(packet->data.hdr) / sizeof(u32);
	if (packet_data_sz_dw != pci_data_len_dw)
		return -EINVAL;

	be32p_replace_bits(&hdr_dw[1], bdf, PCIE_VDM_HDR_REQUESTER_BDF_MASK);

	/*
	 * XXX Don't update EID for MCTP Control messages - old EID may
	 * interfere with MCTP discovery flow.
	 */
	if (priv->eid && hdr[MCTP_HDR_TYPE_OFFSET] != MCTP_HDR_TYPE_CONTROL)
		hdr[MCTP_HDR_SRC_EID_OFFSET] = priv->eid;

	ret = ptr_ring_produce_bh(&client->tx_queue, packet);
	if (!ret)
		tasklet_hi_schedule(&priv->tx.tasklet);

	return ret;
}
EXPORT_SYMBOL_GPL(aspeed_mctp_send_packet);

struct mctp_pcie_packet *aspeed_mctp_receive_packet(struct mctp_client *client,
						    unsigned long timeout)
{
	struct aspeed_mctp *priv = client->priv;
	u16 bdf = _get_bdf(priv);
	int ret;

	if (bdf == 0)
		return ERR_PTR(-EIO);

	ret = wait_event_interruptible_timeout(client->wait_queue,
					       __ptr_ring_peek(&client->rx_queue),
					       timeout);
	if (ret < 0)
		return ERR_PTR(ret);
	else if (ret == 0)
		return ERR_PTR(-ETIME);

	return ptr_ring_consume_bh(&client->rx_queue);
}
EXPORT_SYMBOL_GPL(aspeed_mctp_receive_packet);

void aspeed_mctp_flush_rx_queue(struct mctp_client *client)
{
	struct mctp_pcie_packet *packet;

	while ((packet = ptr_ring_consume_bh(&client->rx_queue)))
		aspeed_mctp_packet_free(packet);
}
EXPORT_SYMBOL_GPL(aspeed_mctp_flush_rx_queue);

static ssize_t aspeed_mctp_read(struct file *file, char __user *buf,
				size_t count, loff_t *ppos)
{
	struct mctp_client *client = file->private_data;
	struct aspeed_mctp *priv = client->priv;
	struct mctp_pcie_packet *rx_packet;
	u16 bdf;

	if (count < PCIE_MCTP_MIN_PACKET_SIZE)
		return -EINVAL;

	bdf = _get_bdf(priv);
	if (bdf == 0)
		return -EIO;

	if (count > sizeof(rx_packet->data))
		count = sizeof(rx_packet->data);

	rx_packet = ptr_ring_consume_bh(&client->rx_queue);
	if (!rx_packet)
		return -EAGAIN;

	if (copy_to_user(buf, &rx_packet->data, count)) {
		dev_err(priv->dev, "copy to user failed\n");
		count = -EFAULT;
	}

	aspeed_mctp_packet_free(rx_packet);

	return count;
}

static void aspeed_mctp_flush_tx_queue(struct mctp_client *client)
{
	struct mctp_pcie_packet *packet;

	while ((packet = ptr_ring_consume_bh(&client->tx_queue)))
		aspeed_mctp_packet_free(packet);
}

static void aspeed_mctp_flush_all_tx_queues(struct aspeed_mctp *priv)
{
	struct mctp_client *client;

	spin_lock_bh(&priv->clients_lock);
	list_for_each_entry(client, &priv->clients, link)
		aspeed_mctp_flush_tx_queue(client);
	spin_unlock_bh(&priv->clients_lock);
}

static ssize_t aspeed_mctp_write(struct file *file, const char __user *buf,
				 size_t count, loff_t *ppos)
{
	struct mctp_client *client = file->private_data;
	struct aspeed_mctp *priv = client->priv;
	struct mctp_pcie_packet *tx_packet;
	int ret;

	if (count < PCIE_MCTP_MIN_PACKET_SIZE)
		return -EINVAL;

	if (count > sizeof(tx_packet->data))
		return -ENOSPC;

	tx_packet = aspeed_mctp_packet_alloc(GFP_KERNEL);
	if (!tx_packet) {
		ret = -ENOMEM;
		goto out;
	}

	if (copy_from_user(&tx_packet->data, buf, count)) {
		dev_err(priv->dev, "copy from user failed\n");
		ret = -EFAULT;
		goto out_packet;
	}

	tx_packet->size = count;

	ret = aspeed_mctp_send_packet(client, tx_packet);
	if (ret)
		goto out_packet;

	return count;

out_packet:
	aspeed_mctp_packet_free(tx_packet);
out:
	return ret;
}

int aspeed_mctp_add_type_handler(struct mctp_client *client, u8 mctp_type,
				 u16 pci_vendor_id, u16 vdm_type, u16 vdm_mask)
{
	struct aspeed_mctp *priv = client->priv;
	struct mctp_type_handler *handler, *new_handler;
	int ret = 0;

	if (mctp_type <= MCTP_HDR_TYPE_BASE_LAST) {
		/* Vendor, type and type mask must be zero for types 0-5 */
		if (pci_vendor_id != 0 || vdm_type != 0 || vdm_mask != 0)
			return -EINVAL;
	} else if (mctp_type == MCTP_HDR_TYPE_VDM_PCI) {
		/* For Vendor Defined PCI type the the vendor ID must be nonzero */
		if (pci_vendor_id == 0 || pci_vendor_id == 0xffff)
			return -EINVAL;
	} else {
		return -EINVAL;
	}

	new_handler = kzalloc(sizeof(*new_handler), GFP_KERNEL);
	if (!new_handler)
		return -ENOMEM;
	new_handler->mctp_type = mctp_type;
	new_handler->pci_vendor_id = pci_vendor_id;
	new_handler->vdm_type = vdm_type & vdm_mask;
	new_handler->vdm_mask = vdm_mask;
	new_handler->client = client;

	spin_lock_bh(&priv->clients_lock);
	list_for_each_entry(handler, &priv->mctp_type_handlers, link) {
		if (handler->mctp_type == new_handler->mctp_type &&
		    handler->pci_vendor_id == new_handler->pci_vendor_id &&
		    handler->vdm_type == new_handler->vdm_type) {
			if (handler->client != new_handler->client)
				ret = -EBUSY;
			kfree(new_handler);
			goto out_unlock;
		}
	}
	list_add_tail(&new_handler->link, &priv->mctp_type_handlers);
out_unlock:
	spin_unlock_bh(&priv->clients_lock);

	return ret;
}
EXPORT_SYMBOL_GPL(aspeed_mctp_add_type_handler);

int aspeed_mctp_remove_type_handler(struct mctp_client *client,
				    u8 mctp_type, u16 pci_vendor_id,
				    u16 vdm_type, u16 vdm_mask)
{
	struct aspeed_mctp *priv = client->priv;
	struct mctp_type_handler *handler, *tmp;
	int ret = -EINVAL;

	vdm_type &= vdm_mask;

	spin_lock_bh(&priv->clients_lock);
	list_for_each_entry_safe(handler, tmp, &priv->mctp_type_handlers,
				 link) {
		if (handler->client == client &&
		    handler->mctp_type == mctp_type &&
		    handler->pci_vendor_id == pci_vendor_id &&
		    handler->vdm_type == vdm_type) {
			list_del(&handler->link);
			kfree(handler);
			ret = 0;
			break;
		}
	}
	spin_unlock_bh(&priv->clients_lock);
	return ret;
}

static int aspeed_mctp_register_default_handler(struct mctp_client *client)
{
	struct aspeed_mctp *priv = client->priv;
	int ret = 0;

	spin_lock_bh(&priv->clients_lock);

	if (!priv->default_client)
		priv->default_client = client;
	else if (priv->default_client != client)
		ret = -EBUSY;

	spin_unlock_bh(&priv->clients_lock);

	return ret;
}

static int
aspeed_mctp_register_type_handler(struct mctp_client *client,
				  void __user *userbuf)
{
	struct aspeed_mctp *priv = client->priv;
	struct aspeed_mctp_type_handler_ioctl handler;

	if (copy_from_user(&handler, userbuf, sizeof(handler))) {
		dev_err(priv->dev, "copy from user failed\n");
		return -EFAULT;
	}

	return aspeed_mctp_add_type_handler(client, handler.mctp_type,
					    handler.pci_vendor_id,
					    handler.vendor_type,
					    handler.vendor_type_mask);
}

static int
aspeed_mctp_unregister_type_handler(struct mctp_client *client,
				    void __user *userbuf)
{
	struct aspeed_mctp *priv = client->priv;
	struct aspeed_mctp_type_handler_ioctl handler;

	if (copy_from_user(&handler, userbuf, sizeof(handler))) {
		dev_err(priv->dev, "copy from user failed\n");
		return -EFAULT;
	}

	return aspeed_mctp_remove_type_handler(client, handler.mctp_type,
					       handler.pci_vendor_id,
					       handler.vendor_type,
					       handler.vendor_type_mask);
}

static int
aspeed_mctp_filter_eid(struct aspeed_mctp *priv, void __user *userbuf)
{
	struct aspeed_mctp_filter_eid eid;

	if (copy_from_user(&eid, userbuf, sizeof(eid))) {
		dev_err(priv->dev, "copy from user failed\n");
		return -EFAULT;
	}

	if (eid.enable) {
		regmap_write(priv->map, ASPEED_MCTP_EID, eid.eid);
		regmap_update_bits(priv->map, ASPEED_MCTP_CTRL,
				   MATCHING_EID, MATCHING_EID);
	} else {
		regmap_update_bits(priv->map, ASPEED_MCTP_CTRL,
				   MATCHING_EID, 0);
	}
	return 0;
}

static int aspeed_mctp_get_bdf(struct aspeed_mctp *priv, void __user *userbuf)
{
	struct aspeed_mctp_get_bdf bdf = { _get_bdf(priv) };

	if (copy_to_user(userbuf, &bdf, sizeof(bdf))) {
		dev_err(priv->dev, "copy to user failed\n");
		return -EFAULT;
	}
	return 0;
}

static int
aspeed_mctp_get_medium_id(struct aspeed_mctp *priv, void __user *userbuf)
{
	struct aspeed_mctp_get_medium_id id = { 0x09 }; /* PCIe revision 2.0 */

	if (copy_to_user(userbuf, &id, sizeof(id))) {
		dev_err(priv->dev, "copy to user failed\n");
		return -EFAULT;
	}
	return 0;
}

static int
aspeed_mctp_get_mtu(struct aspeed_mctp *priv, void __user *userbuf)
{
	struct aspeed_mctp_get_mtu id = { ASPEED_MCTP_MTU };

	if (copy_to_user(userbuf, &id, sizeof(id))) {
		dev_err(priv->dev, "copy to user failed\n");
		return -EFAULT;
	}
	return 0;
}

int aspeed_mctp_get_eid_bdf(struct mctp_client *client, u8 eid, u16 *bdf)
{
	struct aspeed_mctp_endpoint *endpoint;
	int ret = -ENOENT;

	mutex_lock(&client->priv->endpoints_lock);
	list_for_each_entry(endpoint, &client->priv->endpoints, link) {
		if (endpoint->data.eid_info.eid == eid) {
			*bdf = endpoint->data.eid_info.bdf;
			ret = 0;
			break;
		}
	}
	mutex_unlock(&client->priv->endpoints_lock);

	return ret;
}
EXPORT_SYMBOL_GPL(aspeed_mctp_get_eid_bdf);

int aspeed_mctp_get_eid(struct mctp_client *client, u16 bdf,
			u8 domain_id, u8 *eid)
{
	struct aspeed_mctp_endpoint *endpoint;
	int ret = -ENOENT;

	mutex_lock(&client->priv->endpoints_lock);

	list_for_each_entry(endpoint, &client->priv->endpoints, link) {
		if (endpoint->data.eid_ext_info.domain_id == domain_id &&
		    endpoint->data.eid_ext_info.bdf == bdf) {
			*eid = endpoint->data.eid_ext_info.eid;
			ret = 0;
			break;
		}
	}

	mutex_unlock(&client->priv->endpoints_lock);
	return ret;
}
EXPORT_SYMBOL_GPL(aspeed_mctp_get_eid);

static int
aspeed_mctp_get_eid_info(struct aspeed_mctp *priv, void __user *userbuf,
			 enum mctp_address_type addr_format)
{
	int count = 0;
	int ret = 0;
	struct aspeed_mctp_get_eid_info get_eid;
	struct aspeed_mctp_endpoint *endpoint;
	void *user_ptr;
	size_t count_to_copy;

	if (copy_from_user(&get_eid, userbuf, sizeof(get_eid))) {
		dev_err(priv->dev, "copy from user failed\n");
		return -EFAULT;
	}

	mutex_lock(&priv->endpoints_lock);

	if (get_eid.count == 0) {
		count = priv->endpoints_count;
		goto out_unlock;
	}

	user_ptr = u64_to_user_ptr(get_eid.ptr);
	count_to_copy = get_eid.count > priv->endpoints_count ?
					priv->endpoints_count : get_eid.count;
	list_for_each_entry(endpoint, &priv->endpoints, link) {
		if (endpoint->data.eid_info.eid < get_eid.start_eid)
			continue;
		if (count >= count_to_copy)
			break;

		if (addr_format == ASPEED_MCTP_EXTENDED_ADDR_FORMAT)
			ret = copy_to_user(&(((struct aspeed_mctp_eid_ext_info *)
								user_ptr)[count]),
							&endpoint->data,
							sizeof(struct aspeed_mctp_eid_ext_info));
		else
			ret = copy_to_user(&(((struct aspeed_mctp_eid_info *)
								user_ptr)[count]),
							&endpoint->data,
							sizeof(struct aspeed_mctp_eid_info));

		if (ret) {
			dev_err(priv->dev, "copy to user failed\n");
			ret = -EFAULT;
			goto out_unlock;
		}
		count++;
	}

out_unlock:
	get_eid.count = count;
	if (copy_to_user(userbuf, &get_eid, sizeof(get_eid))) {
		dev_err(priv->dev, "copy to user failed\n");
		ret = -EFAULT;
	}

	mutex_unlock(&priv->endpoints_lock);
	return ret;
}

static int
eid_info_cmp(void *priv, const struct list_head *a, const struct list_head *b)
{
	struct aspeed_mctp_endpoint *endpoint_a;
	struct aspeed_mctp_endpoint *endpoint_b;

	if (a == b)
		return 0;

	endpoint_a = list_entry(a, typeof(*endpoint_a), link);
	endpoint_b = list_entry(b, typeof(*endpoint_b), link);

	if (endpoint_a->data.eid_info.eid < endpoint_b->data.eid_info.eid)
		return -1;
	else if (endpoint_a->data.eid_info.eid > endpoint_b->data.eid_info.eid)
		return 1;

	return 0;
}

static void aspeed_mctp_eid_info_list_remove(struct list_head *list)
{
	struct aspeed_mctp_endpoint *endpoint;
	struct aspeed_mctp_endpoint *tmp;

	list_for_each_entry_safe(endpoint, tmp, list, link) {
		list_del(&endpoint->link);
		kfree(endpoint);
	}
}

static bool
aspeed_mctp_eid_info_list_valid(struct list_head *list)
{
	struct aspeed_mctp_endpoint *endpoint;
	struct aspeed_mctp_endpoint *next;

	list_for_each_entry(endpoint, list, link) {
		next = list_next_entry(endpoint, link);
		if (&next->link == list)
			break;

		/* duplicted eids */
		if (next->data.eid_info.eid == endpoint->data.eid_info.eid)
			return false;
	}

	return true;
}

static int
aspeed_mctp_set_eid_info(struct aspeed_mctp *priv, void __user *userbuf,
			 enum mctp_address_type addr_format)
{
	struct list_head list = LIST_HEAD_INIT(list);
	struct aspeed_mctp_set_eid_info set_eid;
	void *user_ptr;
	struct aspeed_mctp_endpoint *endpoint;
	int ret = 0;
	u8 eid = 0;
	size_t i;

	if (copy_from_user(&set_eid, userbuf, sizeof(set_eid))) {
		dev_err(priv->dev, "copy from user failed\n");
		return -EFAULT;
	}

	if (set_eid.count > ASPEED_MCTP_EID_INFO_MAX)
		return -EINVAL;

	user_ptr = u64_to_user_ptr(set_eid.ptr);
	for (i = 0; i < set_eid.count; i++) {
		endpoint = kzalloc(sizeof(*endpoint), GFP_KERNEL);
		if (!endpoint) {
			ret = -ENOMEM;
			goto out;
		}
		memset(endpoint, 0, sizeof(*endpoint));

		if (addr_format == ASPEED_MCTP_EXTENDED_ADDR_FORMAT)
			ret = copy_from_user(&endpoint->data,
					     &(((struct aspeed_mctp_eid_ext_info *)
								user_ptr)[i]),
					     sizeof(struct aspeed_mctp_eid_ext_info));
		else
			ret = copy_from_user(&endpoint->data,
					     &(((struct aspeed_mctp_eid_info *)
								user_ptr)[i]),
						 sizeof(struct aspeed_mctp_eid_info));

		if (ret) {
			dev_err(priv->dev, "copy from user failed\n");
			kfree(endpoint);
			ret = -EFAULT;
			goto out;
		}

		/* Detect self EID */
		if (_get_bdf(priv) == endpoint->data.eid_info.bdf) {
			/*
			 * XXX Use smallest EID with matching BDF.
			 * On some platforms there could be multiple endpoints
			 * with same BDF in routing table.
			 */
			if (eid == 0 || endpoint->data.eid_info.eid < eid)
				eid = endpoint->data.eid_info.eid;
		}

	list_add_tail(&endpoint->link, &list);
	}

	list_sort(NULL, &list, eid_info_cmp);
	if (!aspeed_mctp_eid_info_list_valid(&list)) {
		ret = -EINVAL;
		goto out;
	}

	mutex_lock(&priv->endpoints_lock);
	if (list_empty(&priv->endpoints))
		list_splice_init(&list, &priv->endpoints);
	else
		list_swap(&list, &priv->endpoints);
	priv->endpoints_count = set_eid.count;
	priv->eid = eid;
	mutex_unlock(&priv->endpoints_lock);
out:
	aspeed_mctp_eid_info_list_remove(&list);
	return ret;
}

static int aspeed_mctp_set_own_eid(struct aspeed_mctp *priv, void __user *userbuf)
{
	struct aspeed_mctp_set_own_eid data;

	if (copy_from_user(&data, userbuf, sizeof(data))) {
		dev_err(priv->dev, "copy from user failed\n");
		return -EFAULT;
	}

	priv->eid = data.eid;

	return 0;
}

static long
aspeed_mctp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
	struct mctp_client *client = file->private_data;
	struct aspeed_mctp *priv = client->priv;
	void __user *userbuf = (void __user *)arg;
	int ret;

	switch (cmd) {
	case ASPEED_MCTP_IOCTL_FILTER_EID:
		ret = aspeed_mctp_filter_eid(priv, userbuf);
	break;

	case ASPEED_MCTP_IOCTL_GET_BDF:
		ret = aspeed_mctp_get_bdf(priv, userbuf);
	break;

	case ASPEED_MCTP_IOCTL_GET_MEDIUM_ID:
		ret = aspeed_mctp_get_medium_id(priv, userbuf);
	break;

	case ASPEED_MCTP_IOCTL_GET_MTU:
		ret = aspeed_mctp_get_mtu(priv, userbuf);
	break;

	case ASPEED_MCTP_IOCTL_REGISTER_DEFAULT_HANDLER:
		ret = aspeed_mctp_register_default_handler(client);
	break;

	case ASPEED_MCTP_IOCTL_REGISTER_TYPE_HANDLER:
		ret = aspeed_mctp_register_type_handler(client, userbuf);
	break;

	case ASPEED_MCTP_IOCTL_UNREGISTER_TYPE_HANDLER:
		ret = aspeed_mctp_unregister_type_handler(client, userbuf);
	break;

	case ASPEED_MCTP_IOCTL_GET_EID_INFO:
		ret = aspeed_mctp_get_eid_info(priv, userbuf, ASPEED_MCTP_GENERIC_ADDR_FORMAT);
	break;

	case ASPEED_MCTP_IOCTL_GET_EID_EXT_INFO:
		ret = aspeed_mctp_get_eid_info(priv, userbuf, ASPEED_MCTP_EXTENDED_ADDR_FORMAT);
	break;

	case ASPEED_MCTP_IOCTL_SET_EID_INFO:
		ret = aspeed_mctp_set_eid_info(priv, userbuf, ASPEED_MCTP_GENERIC_ADDR_FORMAT);
	break;

	case ASPEED_MCTP_IOCTL_SET_EID_EXT_INFO:
		ret = aspeed_mctp_set_eid_info(priv, userbuf, ASPEED_MCTP_EXTENDED_ADDR_FORMAT);
	break;

	case ASPEED_MCTP_IOCTL_SET_OWN_EID:
		ret = aspeed_mctp_set_own_eid(priv, userbuf);
	break;

	default:
		dev_err(priv->dev, "Command not found\n");
		ret = -ENOTTY;
	}

	return ret;
}

static __poll_t aspeed_mctp_poll(struct file *file,
				 struct poll_table_struct *pt)
{
	struct mctp_client *client = file->private_data;
	__poll_t ret = 0;

	poll_wait(file, &client->wait_queue, pt);

	if (!ptr_ring_full_bh(&client->tx_queue))
		ret |= EPOLLOUT;

	if (__ptr_ring_peek(&client->rx_queue))
		ret |= EPOLLIN;

	return ret;
}

static const struct file_operations aspeed_mctp_fops = {
	.owner = THIS_MODULE,
	.open = aspeed_mctp_open,
	.release = aspeed_mctp_release,
	.read = aspeed_mctp_read,
	.write = aspeed_mctp_write,
	.unlocked_ioctl = aspeed_mctp_ioctl,
	.poll = aspeed_mctp_poll,
};

static const struct regmap_config aspeed_mctp_regmap_cfg = {
	.reg_bits	= 32,
	.reg_stride	= 4,
	.val_bits	= 32,
	.max_register	= ASPEED_MCTP_TX_BUF_WR_PTR,
};

struct device_type aspeed_mctp_type = {
	.name		= "aspeed-mctp",
};

static struct miscdevice aspeed_mctp_miscdev = {
	.minor = MISC_DYNAMIC_MINOR,
	.name = "aspeed-mctp",
	.fops = &aspeed_mctp_fops,
};

static void aspeed_mctp_send_pcie_uevent(struct kobject *kobj, bool ready)
{
	char *pcie_not_ready_event[] = { ASPEED_MCTP_READY "=0", NULL };
	char *pcie_ready_event[] = { ASPEED_MCTP_READY "=1", NULL };

	kobject_uevent_env(kobj, KOBJ_CHANGE,
			   ready ? pcie_ready_event : pcie_not_ready_event);
}

static u16 aspeed_mctp_pcie_setup(struct aspeed_mctp *priv)
{
	u32 reg;
	u16 bdf;

	regmap_read(priv->pcie.map, ASPEED_PCIE_MISC_STS_1, &reg);

	bdf = PCI_DEVID(GET_PCI_BUS_NUM(reg), GET_PCI_DEV_NUM(reg));
	if (bdf != 0)
		cancel_delayed_work(&priv->pcie.rst_dwork);
	else
		schedule_delayed_work(&priv->pcie.rst_dwork,
				      msecs_to_jiffies(1000));

	return bdf;
}

static void aspeed_mctp_irq_enable(struct aspeed_mctp *priv)
{
	u32 enable = TX_CMD_SENT_INT | TX_CMD_WRONG_INT |
		     RX_CMD_RECEIVE_INT | RX_CMD_NO_MORE_INT;

	regmap_write(priv->map, ASPEED_MCTP_INT_EN, enable);
}

static void aspeed_mctp_irq_disable(struct aspeed_mctp *priv)
{
	regmap_write(priv->map, ASPEED_MCTP_INT_EN, 0);
}

static void aspeed_mctp_reset_work(struct work_struct *work)
{
	struct aspeed_mctp *priv = container_of(work, typeof(*priv),
						pcie.rst_dwork.work);
	struct kobject *kobj = &aspeed_mctp_miscdev.this_device->kobj;
	u16 bdf;

	if (priv->pcie.need_uevent) {
		aspeed_mctp_send_pcie_uevent(kobj, false);
		priv->pcie.need_uevent = false;
	}

	bdf = aspeed_mctp_pcie_setup(priv);
	if (bdf) {
		aspeed_mctp_flush_all_tx_queues(priv);
		aspeed_mctp_irq_enable(priv);
		aspeed_mctp_rx_trigger(&priv->rx);
		_set_bdf(priv, bdf);
		aspeed_mctp_send_pcie_uevent(kobj, true);
	}
}

static void aspeed_mctp_channels_init(struct aspeed_mctp *priv)
{
	aspeed_mctp_rx_chan_init(&priv->rx);
	aspeed_mctp_tx_chan_init(&priv->tx);
}

static irqreturn_t aspeed_mctp_irq_handler(int irq, void *arg)
{
	struct aspeed_mctp *priv = arg;
	u32 handled = 0;
	u32 status;

	regmap_read(priv->map, ASPEED_MCTP_INT_STS, &status);
	regmap_write(priv->map, ASPEED_MCTP_INT_STS, status);

	if (status & TX_CMD_SENT_INT) {
		tasklet_hi_schedule(&priv->tx.tasklet);

		handled |= TX_CMD_SENT_INT;
	}

	if (status & TX_CMD_WRONG_INT) {
		/* TODO: print the actual command */
		dev_warn(priv->dev, "TX wrong");

		handled |= TX_CMD_WRONG_INT;
	}

	if (status & RX_CMD_RECEIVE_INT) {
		tasklet_hi_schedule(&priv->rx.tasklet);

		handled |= RX_CMD_RECEIVE_INT;
	}

	if (status & RX_CMD_NO_MORE_INT) {
		dev_dbg(priv->dev, "RX full");
		priv->rx.stopped = true;
		tasklet_hi_schedule(&priv->rx.tasklet);

		handled |= RX_CMD_NO_MORE_INT;
	}

	if (!handled)
		return IRQ_NONE;

	return IRQ_HANDLED;
}

static irqreturn_t aspeed_mctp_pcie_rst_irq_handler(int irq, void *arg)
{
	struct aspeed_mctp *priv = arg;

	aspeed_mctp_channels_init(priv);

	priv->pcie.need_uevent = true;
	_set_bdf(priv, 0);
	priv->eid = 0;

	schedule_delayed_work(&priv->pcie.rst_dwork, 0);

	return IRQ_HANDLED;
}

static void aspeed_mctp_drv_init(struct aspeed_mctp *priv)
{
	INIT_LIST_HEAD(&priv->clients);
	INIT_LIST_HEAD(&priv->mctp_type_handlers);
	INIT_LIST_HEAD(&priv->endpoints);

	spin_lock_init(&priv->clients_lock);
	mutex_init(&priv->endpoints_lock);

	INIT_DELAYED_WORK(&priv->pcie.rst_dwork, aspeed_mctp_reset_work);

	tasklet_init(&priv->tx.tasklet, aspeed_mctp_tx_tasklet,
		     (unsigned long)&priv->tx);
	tasklet_init(&priv->rx.tasklet, aspeed_mctp_rx_tasklet,
		     (unsigned long)&priv->rx);
}

static void aspeed_mctp_drv_fini(struct aspeed_mctp *priv)
{
	aspeed_mctp_eid_info_list_remove(&priv->endpoints);
	tasklet_disable(&priv->tx.tasklet);
	tasklet_kill(&priv->tx.tasklet);
	tasklet_disable(&priv->rx.tasklet);
	tasklet_kill(&priv->rx.tasklet);

	cancel_delayed_work_sync(&priv->pcie.rst_dwork);
}

static int aspeed_mctp_resources_init(struct aspeed_mctp *priv)
{
	struct platform_device *pdev = to_platform_device(priv->dev);
	void __iomem *regs;

	regs = devm_platform_ioremap_resource(pdev, 0);
	if (IS_ERR(regs)) {
		dev_err(priv->dev, "Failed to get regmap!\n");
		return PTR_ERR(regs);
	}

	priv->map = devm_regmap_init_mmio(priv->dev, regs,
					  &aspeed_mctp_regmap_cfg);
	if (IS_ERR(priv->map))
		return PTR_ERR(priv->map);

	priv->reset = devm_reset_control_get(priv->dev, 0);
	if (IS_ERR(priv->reset)) {
		dev_err(priv->dev, "Failed to get reset!\n");
		return PTR_ERR(priv->reset);
	}

	priv->pcie.map =
		syscon_regmap_lookup_by_phandle(priv->dev->of_node,
						"aspeed,pcieh");
	if (IS_ERR(priv->pcie.map)) {
		dev_err(priv->dev, "Failed to find PCIe Host regmap!\n");
		return PTR_ERR(priv->pcie.map);
	}

	platform_set_drvdata(pdev, priv);

	return 0;
}

static int aspeed_mctp_dma_init(struct aspeed_mctp *priv)
{
	struct mctp_channel *tx = &priv->tx;
	struct mctp_channel *rx = &priv->rx;
	int ret = -ENOMEM;

	BUILD_BUG_ON(TX_PACKET_COUNT >= TX_MAX_PACKET_COUNT);
	BUILD_BUG_ON(RX_PACKET_COUNT >= RX_MAX_PACKET_COUNT);

	tx->cmd.vaddr = dma_alloc_coherent(priv->dev, TX_CMD_BUF_SIZE,
					   &tx->cmd.dma_handle, GFP_KERNEL);

	if (!tx->cmd.vaddr)
		return ret;

	tx->data.vaddr = dma_alloc_coherent(priv->dev, TX_DATA_BUF_SIZE,
					    &tx->data.dma_handle, GFP_KERNEL);

	if (!tx->data.vaddr)
		goto out_tx_data;

	rx->cmd.vaddr = dma_alloc_coherent(priv->dev, RX_CMD_BUF_SIZE,
					   &rx->cmd.dma_handle, GFP_KERNEL);

	if (!rx->cmd.vaddr)
		goto out_tx_cmd;

	rx->data.vaddr = dma_alloc_coherent(priv->dev, RX_DATA_BUF_SIZE,
					    &rx->data.dma_handle, GFP_KERNEL);

	if (!rx->data.vaddr)
		goto out_rx_data;

	return 0;
out_rx_data:
	dma_free_coherent(priv->dev, RX_CMD_BUF_SIZE, rx->cmd.vaddr,
			  rx->cmd.dma_handle);

out_tx_cmd:
	dma_free_coherent(priv->dev, TX_DATA_BUF_SIZE, tx->data.vaddr,
			  tx->data.dma_handle);

out_tx_data:
	dma_free_coherent(priv->dev, TX_CMD_BUF_SIZE, tx->cmd.vaddr,
			  tx->cmd.dma_handle);
	return ret;
}

static void aspeed_mctp_dma_fini(struct aspeed_mctp *priv)
{
	struct mctp_channel *tx = &priv->tx;
	struct mctp_channel *rx = &priv->rx;

	dma_free_coherent(priv->dev, TX_CMD_BUF_SIZE, tx->cmd.vaddr,
			  tx->cmd.dma_handle);

	dma_free_coherent(priv->dev, RX_CMD_BUF_SIZE, rx->cmd.vaddr,
			  rx->cmd.dma_handle);

	dma_free_coherent(priv->dev, TX_DATA_BUF_SIZE, tx->data.vaddr,
			  tx->data.dma_handle);

	dma_free_coherent(priv->dev, RX_DATA_BUF_SIZE, rx->data.vaddr,
			  rx->data.dma_handle);
}

static int aspeed_mctp_irq_init(struct aspeed_mctp *priv)
{
	struct platform_device *pdev = to_platform_device(priv->dev);
	int irq, ret;

	irq = platform_get_irq(pdev, 0);
	if (!irq)
		return -ENODEV;

	ret = devm_request_irq(priv->dev, irq, aspeed_mctp_irq_handler,
			       IRQF_SHARED, "aspeed-mctp", priv);
	if (ret)
		return ret;

	irq = platform_get_irq(pdev, 1);
	if (!irq)
		return -ENODEV;

	ret = devm_request_irq(priv->dev, irq,
			       aspeed_mctp_pcie_rst_irq_handler,
			       IRQF_SHARED, "aspeed-mctp", priv);
	if (ret)
		return ret;

	return 0;
}

static void aspeed_mctp_hw_reset(struct aspeed_mctp *priv)
{
	u32 reg;

	/*
	 * XXX: We need to skip the reset when we probe multiple times.
	 * Currently calling reset more than once seems to make the HW upset,
	 * however, we do need to reset once after the first boot before we're
	 * able to use the HW.
	 */
	regmap_read(priv->map, ASPEED_MCTP_TX_BUF_ADDR, &reg);

	if (reg) {
		dev_info(priv->dev,
			 "Already initialized - skipping hardware reset\n");
		return;
	}

	if (reset_control_assert(priv->reset) != 0)
		dev_warn(priv->dev, "Failed to assert reset\n");

	if (reset_control_deassert(priv->reset) != 0)
		dev_warn(priv->dev, "Failed to deassert reset\n");
}

static int aspeed_mctp_probe(struct platform_device *pdev)
{
	struct aspeed_mctp *priv;
	int ret;
	u16 bdf;

	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
	if (!priv) {
		ret = -ENOMEM;
		goto out;
	}
	priv->dev = &pdev->dev;

	aspeed_mctp_drv_init(priv);

	ret = aspeed_mctp_resources_init(priv);
	if (ret) {
		dev_err(priv->dev, "Failed to init resources\n");
		goto out_drv;
	}

	ret = aspeed_mctp_dma_init(priv);
	if (ret) {
		dev_err(priv->dev, "Failed to init DMA\n");
		goto out_drv;
	}

	aspeed_mctp_hw_reset(priv);

	aspeed_mctp_channels_init(priv);

	aspeed_mctp_miscdev.parent = priv->dev;
	ret = misc_register(&aspeed_mctp_miscdev);
	if (ret) {
		dev_err(priv->dev, "Failed to register miscdev\n");
		goto out_dma;
	}
	aspeed_mctp_miscdev.this_device->type = &aspeed_mctp_type;

	ret = aspeed_mctp_irq_init(priv);
	if (ret) {
		dev_err(priv->dev, "Failed to init IRQ!\n");
		goto out_dma;
	}

	aspeed_mctp_irq_enable(priv);

	bdf = aspeed_mctp_pcie_setup(priv);
	if (bdf != 0)
		_set_bdf(priv, bdf);

	priv->peci_mctp =
		platform_device_register_data(priv->dev, "peci-mctp",
					      PLATFORM_DEVID_NONE, NULL, 0);
	if (IS_ERR(priv->peci_mctp))
		dev_err(priv->dev, "Failed to register peci-mctp device\n");

	aspeed_mctp_rx_trigger(&priv->rx);

	return 0;

out_dma:
	aspeed_mctp_dma_fini(priv);
out_drv:
	aspeed_mctp_drv_fini(priv);
out:
	dev_err(&pdev->dev, "Failed to probe Aspeed MCTP: %d\n", ret);
	return ret;
}

static int aspeed_mctp_remove(struct platform_device *pdev)
{
	struct aspeed_mctp *priv = platform_get_drvdata(pdev);

	platform_device_unregister(priv->peci_mctp);

	misc_deregister(&aspeed_mctp_miscdev);

	aspeed_mctp_irq_disable(priv);

	aspeed_mctp_dma_fini(priv);

	aspeed_mctp_drv_fini(priv);

	return 0;
}

static const struct of_device_id aspeed_mctp_match_table[] = {
	{ .compatible = "aspeed,ast2600-mctp" },
	{ }
};

static struct platform_driver aspeed_mctp_driver = {
	.driver	= {
		.name		= "aspeed-mctp",
		.of_match_table	= of_match_ptr(aspeed_mctp_match_table),
	},
	.probe	= aspeed_mctp_probe,
	.remove	= aspeed_mctp_remove,
};

static int __init aspeed_mctp_init(void)
{
	packet_cache =
		kmem_cache_create_usercopy("mctp-packet",
					   sizeof(struct mctp_pcie_packet),
					   0, 0, 0,
					   sizeof(struct mctp_pcie_packet),
					   NULL);
	if (!packet_cache)
		return -ENOMEM;

	return platform_driver_register(&aspeed_mctp_driver);
}

static void __exit aspeed_mctp_exit(void)
{
	platform_driver_unregister(&aspeed_mctp_driver);
	kmem_cache_destroy(packet_cache);
}

module_init(aspeed_mctp_init)
module_exit(aspeed_mctp_exit)

MODULE_DEVICE_TABLE(of, aspeed_mctp_match_table);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Iwona Winiarska <iwona.winiarska@intel.com>");
MODULE_DESCRIPTION("Aspeed AST2600 MCTP driver");