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
|
/* $NetBSD: nvme.c,v 1.31 2017/10/28 04:53:55 riastradh Exp $ */
/* $OpenBSD: nvme.c,v 1.49 2016/04/18 05:59:50 dlg Exp $ */
/*
* Copyright (c) 2014 David Gwynne <dlg@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: nvme.c,v 1.31 2017/10/28 04:53:55 riastradh Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/atomic.h>
#include <sys/bus.h>
#include <sys/buf.h>
#include <sys/conf.h>
#include <sys/device.h>
#include <sys/kmem.h>
#include <sys/once.h>
#include <sys/proc.h>
#include <sys/queue.h>
#include <sys/mutex.h>
#include <uvm/uvm_extern.h>
#include <dev/ic/nvmereg.h>
#include <dev/ic/nvmevar.h>
#include <dev/ic/nvmeio.h>
#include "ioconf.h"
int nvme_adminq_size = 32;
int nvme_ioq_size = 1024;
static int nvme_print(void *, const char *);
static int nvme_ready(struct nvme_softc *, uint32_t);
static int nvme_enable(struct nvme_softc *, u_int);
static int nvme_disable(struct nvme_softc *);
static int nvme_shutdown(struct nvme_softc *);
#ifdef NVME_DEBUG
static void nvme_dumpregs(struct nvme_softc *);
#endif
static int nvme_identify(struct nvme_softc *, u_int);
static void nvme_fill_identify(struct nvme_queue *, struct nvme_ccb *,
void *);
static int nvme_ccbs_alloc(struct nvme_queue *, uint16_t);
static void nvme_ccbs_free(struct nvme_queue *);
static struct nvme_ccb *
nvme_ccb_get(struct nvme_queue *);
static void nvme_ccb_put(struct nvme_queue *, struct nvme_ccb *);
static int nvme_poll(struct nvme_softc *, struct nvme_queue *,
struct nvme_ccb *, void (*)(struct nvme_queue *,
struct nvme_ccb *, void *), int);
static void nvme_poll_fill(struct nvme_queue *, struct nvme_ccb *, void *);
static void nvme_poll_done(struct nvme_queue *, struct nvme_ccb *,
struct nvme_cqe *);
static void nvme_sqe_fill(struct nvme_queue *, struct nvme_ccb *, void *);
static void nvme_empty_done(struct nvme_queue *, struct nvme_ccb *,
struct nvme_cqe *);
static struct nvme_queue *
nvme_q_alloc(struct nvme_softc *, uint16_t, u_int, u_int);
static int nvme_q_create(struct nvme_softc *, struct nvme_queue *);
static int nvme_q_delete(struct nvme_softc *, struct nvme_queue *);
static void nvme_q_submit(struct nvme_softc *, struct nvme_queue *,
struct nvme_ccb *, void (*)(struct nvme_queue *,
struct nvme_ccb *, void *));
static int nvme_q_complete(struct nvme_softc *, struct nvme_queue *q);
static void nvme_q_free(struct nvme_softc *, struct nvme_queue *);
static struct nvme_dmamem *
nvme_dmamem_alloc(struct nvme_softc *, size_t);
static void nvme_dmamem_free(struct nvme_softc *, struct nvme_dmamem *);
static void nvme_dmamem_sync(struct nvme_softc *, struct nvme_dmamem *,
int);
static void nvme_ns_io_fill(struct nvme_queue *, struct nvme_ccb *,
void *);
static void nvme_ns_io_done(struct nvme_queue *, struct nvme_ccb *,
struct nvme_cqe *);
static void nvme_ns_sync_fill(struct nvme_queue *, struct nvme_ccb *,
void *);
static void nvme_ns_sync_done(struct nvme_queue *, struct nvme_ccb *,
struct nvme_cqe *);
static void nvme_getcache_fill(struct nvme_queue *, struct nvme_ccb *,
void *);
static void nvme_getcache_done(struct nvme_queue *, struct nvme_ccb *,
struct nvme_cqe *);
static void nvme_pt_fill(struct nvme_queue *, struct nvme_ccb *,
void *);
static void nvme_pt_done(struct nvme_queue *, struct nvme_ccb *,
struct nvme_cqe *);
static int nvme_command_passthrough(struct nvme_softc *,
struct nvme_pt_command *, uint16_t, struct lwp *, bool);
static int nvme_get_number_of_queues(struct nvme_softc *, u_int *);
#define NVME_TIMO_QOP 5 /* queue create and delete timeout */
#define NVME_TIMO_IDENT 10 /* probe identify timeout */
#define NVME_TIMO_PT -1 /* passthrough cmd timeout */
#define NVME_TIMO_SY 60 /* sync cache timeout */
#define nvme_read4(_s, _r) \
bus_space_read_4((_s)->sc_iot, (_s)->sc_ioh, (_r))
#define nvme_write4(_s, _r, _v) \
bus_space_write_4((_s)->sc_iot, (_s)->sc_ioh, (_r), (_v))
/*
* Some controllers, at least Apple NVMe, always require split
* transfers, so don't use bus_space_{read,write}_8() on LP64.
*/
static inline uint64_t
nvme_read8(struct nvme_softc *sc, bus_size_t r)
{
uint64_t v;
uint32_t *a = (uint32_t *)&v;
#if _BYTE_ORDER == _LITTLE_ENDIAN
a[0] = nvme_read4(sc, r);
a[1] = nvme_read4(sc, r + 4);
#else /* _BYTE_ORDER == _LITTLE_ENDIAN */
a[1] = nvme_read4(sc, r);
a[0] = nvme_read4(sc, r + 4);
#endif
return v;
}
static inline void
nvme_write8(struct nvme_softc *sc, bus_size_t r, uint64_t v)
{
uint32_t *a = (uint32_t *)&v;
#if _BYTE_ORDER == _LITTLE_ENDIAN
nvme_write4(sc, r, a[0]);
nvme_write4(sc, r + 4, a[1]);
#else /* _BYTE_ORDER == _LITTLE_ENDIAN */
nvme_write4(sc, r, a[1]);
nvme_write4(sc, r + 4, a[0]);
#endif
}
#define nvme_barrier(_s, _r, _l, _f) \
bus_space_barrier((_s)->sc_iot, (_s)->sc_ioh, (_r), (_l), (_f))
#ifdef NVME_DEBUG
static __used void
nvme_dumpregs(struct nvme_softc *sc)
{
uint64_t r8;
uint32_t r4;
#define DEVNAME(_sc) device_xname((_sc)->sc_dev)
r8 = nvme_read8(sc, NVME_CAP);
printf("%s: cap 0x%016"PRIx64"\n", DEVNAME(sc), nvme_read8(sc, NVME_CAP));
printf("%s: mpsmax %u (%u)\n", DEVNAME(sc),
(u_int)NVME_CAP_MPSMAX(r8), (1 << NVME_CAP_MPSMAX(r8)));
printf("%s: mpsmin %u (%u)\n", DEVNAME(sc),
(u_int)NVME_CAP_MPSMIN(r8), (1 << NVME_CAP_MPSMIN(r8)));
printf("%s: css %"PRIu64"\n", DEVNAME(sc), NVME_CAP_CSS(r8));
printf("%s: nssrs %"PRIu64"\n", DEVNAME(sc), NVME_CAP_NSSRS(r8));
printf("%s: dstrd %"PRIu64"\n", DEVNAME(sc), NVME_CAP_DSTRD(r8));
printf("%s: to %"PRIu64" msec\n", DEVNAME(sc), NVME_CAP_TO(r8));
printf("%s: ams %"PRIu64"\n", DEVNAME(sc), NVME_CAP_AMS(r8));
printf("%s: cqr %"PRIu64"\n", DEVNAME(sc), NVME_CAP_CQR(r8));
printf("%s: mqes %"PRIu64"\n", DEVNAME(sc), NVME_CAP_MQES(r8));
printf("%s: vs 0x%04x\n", DEVNAME(sc), nvme_read4(sc, NVME_VS));
r4 = nvme_read4(sc, NVME_CC);
printf("%s: cc 0x%04x\n", DEVNAME(sc), r4);
printf("%s: iocqes %u (%u)\n", DEVNAME(sc), NVME_CC_IOCQES_R(r4),
(1 << NVME_CC_IOCQES_R(r4)));
printf("%s: iosqes %u (%u)\n", DEVNAME(sc), NVME_CC_IOSQES_R(r4),
(1 << NVME_CC_IOSQES_R(r4)));
printf("%s: shn %u\n", DEVNAME(sc), NVME_CC_SHN_R(r4));
printf("%s: ams %u\n", DEVNAME(sc), NVME_CC_AMS_R(r4));
printf("%s: mps %u (%u)\n", DEVNAME(sc), NVME_CC_MPS_R(r4),
(1 << NVME_CC_MPS_R(r4)));
printf("%s: css %u\n", DEVNAME(sc), NVME_CC_CSS_R(r4));
printf("%s: en %u\n", DEVNAME(sc), ISSET(r4, NVME_CC_EN) ? 1 : 0);
r4 = nvme_read4(sc, NVME_CSTS);
printf("%s: csts 0x%08x\n", DEVNAME(sc), r4);
printf("%s: rdy %u\n", DEVNAME(sc), r4 & NVME_CSTS_RDY);
printf("%s: cfs %u\n", DEVNAME(sc), r4 & NVME_CSTS_CFS);
printf("%s: shst %x\n", DEVNAME(sc), r4 & NVME_CSTS_SHST_MASK);
r4 = nvme_read4(sc, NVME_AQA);
printf("%s: aqa 0x%08x\n", DEVNAME(sc), r4);
printf("%s: acqs %u\n", DEVNAME(sc), NVME_AQA_ACQS_R(r4));
printf("%s: asqs %u\n", DEVNAME(sc), NVME_AQA_ASQS_R(r4));
printf("%s: asq 0x%016"PRIx64"\n", DEVNAME(sc), nvme_read8(sc, NVME_ASQ));
printf("%s: acq 0x%016"PRIx64"\n", DEVNAME(sc), nvme_read8(sc, NVME_ACQ));
#undef DEVNAME
}
#endif /* NVME_DEBUG */
static int
nvme_ready(struct nvme_softc *sc, uint32_t rdy)
{
u_int i = 0;
uint32_t cc;
cc = nvme_read4(sc, NVME_CC);
if (((cc & NVME_CC_EN) != 0) != (rdy != 0)) {
aprint_error_dev(sc->sc_dev,
"controller enabled status expected %d, found to be %d\n",
(rdy != 0), ((cc & NVME_CC_EN) != 0));
return ENXIO;
}
while ((nvme_read4(sc, NVME_CSTS) & NVME_CSTS_RDY) != rdy) {
if (i++ > sc->sc_rdy_to)
return ENXIO;
delay(1000);
nvme_barrier(sc, NVME_CSTS, 4, BUS_SPACE_BARRIER_READ);
}
return 0;
}
static int
nvme_enable(struct nvme_softc *sc, u_int mps)
{
uint32_t cc, csts;
cc = nvme_read4(sc, NVME_CC);
csts = nvme_read4(sc, NVME_CSTS);
if (ISSET(cc, NVME_CC_EN)) {
aprint_error_dev(sc->sc_dev, "controller unexpectedly enabled, failed to stay disabled\n");
if (ISSET(csts, NVME_CSTS_RDY))
return 1;
goto waitready;
}
nvme_write8(sc, NVME_ASQ, NVME_DMA_DVA(sc->sc_admin_q->q_sq_dmamem));
nvme_barrier(sc, 0, sc->sc_ios, BUS_SPACE_BARRIER_WRITE);
delay(5000);
nvme_write8(sc, NVME_ACQ, NVME_DMA_DVA(sc->sc_admin_q->q_cq_dmamem));
nvme_barrier(sc, 0, sc->sc_ios, BUS_SPACE_BARRIER_WRITE);
delay(5000);
nvme_write4(sc, NVME_AQA, NVME_AQA_ACQS(sc->sc_admin_q->q_entries) |
NVME_AQA_ASQS(sc->sc_admin_q->q_entries));
nvme_barrier(sc, 0, sc->sc_ios, BUS_SPACE_BARRIER_WRITE);
delay(5000);
CLR(cc, NVME_CC_IOCQES_MASK | NVME_CC_IOSQES_MASK | NVME_CC_SHN_MASK |
NVME_CC_AMS_MASK | NVME_CC_MPS_MASK | NVME_CC_CSS_MASK);
SET(cc, NVME_CC_IOSQES(ffs(64) - 1) | NVME_CC_IOCQES(ffs(16) - 1));
SET(cc, NVME_CC_SHN(NVME_CC_SHN_NONE));
SET(cc, NVME_CC_CSS(NVME_CC_CSS_NVM));
SET(cc, NVME_CC_AMS(NVME_CC_AMS_RR));
SET(cc, NVME_CC_MPS(mps));
SET(cc, NVME_CC_EN);
nvme_write4(sc, NVME_CC, cc);
nvme_barrier(sc, 0, sc->sc_ios,
BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
delay(5000);
waitready:
return nvme_ready(sc, NVME_CSTS_RDY);
}
static int
nvme_disable(struct nvme_softc *sc)
{
uint32_t cc, csts;
cc = nvme_read4(sc, NVME_CC);
csts = nvme_read4(sc, NVME_CSTS);
if (ISSET(cc, NVME_CC_EN) && !ISSET(csts, NVME_CSTS_RDY))
nvme_ready(sc, NVME_CSTS_RDY);
CLR(cc, NVME_CC_EN);
nvme_write4(sc, NVME_CC, cc);
nvme_barrier(sc, 0, sc->sc_ios, BUS_SPACE_BARRIER_READ);
delay(5000);
return nvme_ready(sc, 0);
}
int
nvme_attach(struct nvme_softc *sc)
{
uint64_t cap;
uint32_t reg;
u_int dstrd;
u_int mps = PAGE_SHIFT;
u_int ioq_allocated;
uint16_t adminq_entries = nvme_adminq_size;
uint16_t ioq_entries = nvme_ioq_size;
int i;
reg = nvme_read4(sc, NVME_VS);
if (reg == 0xffffffff) {
aprint_error_dev(sc->sc_dev, "invalid mapping\n");
return 1;
}
if (NVME_VS_TER(reg) == 0)
aprint_normal_dev(sc->sc_dev, "NVMe %d.%d\n", NVME_VS_MJR(reg),
NVME_VS_MNR(reg));
else
aprint_normal_dev(sc->sc_dev, "NVMe %d.%d.%d\n", NVME_VS_MJR(reg),
NVME_VS_MNR(reg), NVME_VS_TER(reg));
cap = nvme_read8(sc, NVME_CAP);
dstrd = NVME_CAP_DSTRD(cap);
if (NVME_CAP_MPSMIN(cap) > PAGE_SHIFT) {
aprint_error_dev(sc->sc_dev, "NVMe minimum page size %u "
"is greater than CPU page size %u\n",
1 << NVME_CAP_MPSMIN(cap), 1 << PAGE_SHIFT);
return 1;
}
if (NVME_CAP_MPSMAX(cap) < mps)
mps = NVME_CAP_MPSMAX(cap);
if (ioq_entries > NVME_CAP_MQES(cap))
ioq_entries = NVME_CAP_MQES(cap);
/* set initial values to be used for admin queue during probe */
sc->sc_rdy_to = NVME_CAP_TO(cap);
sc->sc_mps = 1 << mps;
sc->sc_mdts = MAXPHYS;
sc->sc_max_sgl = 2;
if (nvme_disable(sc) != 0) {
aprint_error_dev(sc->sc_dev, "unable to disable controller\n");
return 1;
}
sc->sc_admin_q = nvme_q_alloc(sc, NVME_ADMIN_Q, adminq_entries, dstrd);
if (sc->sc_admin_q == NULL) {
aprint_error_dev(sc->sc_dev,
"unable to allocate admin queue\n");
return 1;
}
if (sc->sc_intr_establish(sc, NVME_ADMIN_Q, sc->sc_admin_q))
goto free_admin_q;
if (nvme_enable(sc, mps) != 0) {
aprint_error_dev(sc->sc_dev, "unable to enable controller\n");
goto disestablish_admin_q;
}
if (nvme_identify(sc, NVME_CAP_MPSMIN(cap)) != 0) {
aprint_error_dev(sc->sc_dev, "unable to identify controller\n");
goto disable;
}
/* we know how big things are now */
sc->sc_max_sgl = sc->sc_mdts / sc->sc_mps;
/* reallocate ccbs of admin queue with new max sgl. */
nvme_ccbs_free(sc->sc_admin_q);
nvme_ccbs_alloc(sc->sc_admin_q, sc->sc_admin_q->q_entries);
if (sc->sc_use_mq) {
/* Limit the number of queues to the number allocated in HW */
if (nvme_get_number_of_queues(sc, &ioq_allocated) != 0) {
aprint_error_dev(sc->sc_dev,
"unable to get number of queues\n");
goto disable;
}
if (sc->sc_nq > ioq_allocated)
sc->sc_nq = ioq_allocated;
}
sc->sc_q = kmem_zalloc(sizeof(*sc->sc_q) * sc->sc_nq, KM_SLEEP);
for (i = 0; i < sc->sc_nq; i++) {
sc->sc_q[i] = nvme_q_alloc(sc, i + 1, ioq_entries, dstrd);
if (sc->sc_q[i] == NULL) {
aprint_error_dev(sc->sc_dev,
"unable to allocate io queue\n");
goto free_q;
}
if (nvme_q_create(sc, sc->sc_q[i]) != 0) {
aprint_error_dev(sc->sc_dev,
"unable to create io queue\n");
nvme_q_free(sc, sc->sc_q[i]);
goto free_q;
}
}
if (!sc->sc_use_mq)
nvme_write4(sc, NVME_INTMC, 1);
/* probe subdevices */
sc->sc_namespaces = kmem_zalloc(sizeof(*sc->sc_namespaces) * sc->sc_nn,
KM_SLEEP);
nvme_rescan(sc->sc_dev, "nvme", &i);
return 0;
free_q:
while (--i >= 0) {
nvme_q_delete(sc, sc->sc_q[i]);
nvme_q_free(sc, sc->sc_q[i]);
}
disable:
nvme_disable(sc);
disestablish_admin_q:
sc->sc_intr_disestablish(sc, NVME_ADMIN_Q);
free_admin_q:
nvme_q_free(sc, sc->sc_admin_q);
return 1;
}
int
nvme_rescan(device_t self, const char *attr, const int *flags)
{
struct nvme_softc *sc = device_private(self);
struct nvme_attach_args naa;
uint64_t cap;
int ioq_entries = nvme_ioq_size;
int i;
cap = nvme_read8(sc, NVME_CAP);
if (ioq_entries > NVME_CAP_MQES(cap))
ioq_entries = NVME_CAP_MQES(cap);
for (i = 0; i < sc->sc_nn; i++) {
if (sc->sc_namespaces[i].dev)
continue;
memset(&naa, 0, sizeof(naa));
naa.naa_nsid = i + 1;
naa.naa_qentries = (ioq_entries - 1) * sc->sc_nq;
naa.naa_maxphys = sc->sc_mdts;
sc->sc_namespaces[i].dev = config_found(sc->sc_dev, &naa,
nvme_print);
}
return 0;
}
static int
nvme_print(void *aux, const char *pnp)
{
struct nvme_attach_args *naa = aux;
if (pnp)
aprint_normal("at %s", pnp);
if (naa->naa_nsid > 0)
aprint_normal(" nsid %d", naa->naa_nsid);
return UNCONF;
}
int
nvme_detach(struct nvme_softc *sc, int flags)
{
int i, error;
error = config_detach_children(sc->sc_dev, flags);
if (error)
return error;
error = nvme_shutdown(sc);
if (error)
return error;
/* from now on we are committed to detach, following will never fail */
for (i = 0; i < sc->sc_nq; i++)
nvme_q_free(sc, sc->sc_q[i]);
kmem_free(sc->sc_q, sizeof(*sc->sc_q) * sc->sc_nq);
nvme_q_free(sc, sc->sc_admin_q);
return 0;
}
static int
nvme_shutdown(struct nvme_softc *sc)
{
uint32_t cc, csts;
bool disabled = false;
int i;
if (!sc->sc_use_mq)
nvme_write4(sc, NVME_INTMS, 1);
for (i = 0; i < sc->sc_nq; i++) {
if (nvme_q_delete(sc, sc->sc_q[i]) != 0) {
aprint_error_dev(sc->sc_dev,
"unable to delete io queue %d, disabling\n", i + 1);
disabled = true;
}
}
sc->sc_intr_disestablish(sc, NVME_ADMIN_Q);
if (disabled)
goto disable;
cc = nvme_read4(sc, NVME_CC);
CLR(cc, NVME_CC_SHN_MASK);
SET(cc, NVME_CC_SHN(NVME_CC_SHN_NORMAL));
nvme_write4(sc, NVME_CC, cc);
for (i = 0; i < 4000; i++) {
nvme_barrier(sc, 0, sc->sc_ios,
BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
csts = nvme_read4(sc, NVME_CSTS);
if ((csts & NVME_CSTS_SHST_MASK) == NVME_CSTS_SHST_DONE)
return 0;
delay(1000);
}
aprint_error_dev(sc->sc_dev, "unable to shudown, disabling\n");
disable:
nvme_disable(sc);
return 0;
}
void
nvme_childdet(device_t self, device_t child)
{
struct nvme_softc *sc = device_private(self);
int i;
for (i = 0; i < sc->sc_nn; i++) {
if (sc->sc_namespaces[i].dev == child) {
/* Already freed ns->ident. */
sc->sc_namespaces[i].dev = NULL;
break;
}
}
}
int
nvme_ns_identify(struct nvme_softc *sc, uint16_t nsid)
{
struct nvme_sqe sqe;
struct nvm_identify_namespace *identify;
struct nvme_dmamem *mem;
struct nvme_ccb *ccb;
struct nvme_namespace *ns;
int rv;
KASSERT(nsid > 0);
ccb = nvme_ccb_get(sc->sc_admin_q);
KASSERT(ccb != NULL); /* it's a bug if we don't have spare ccb here */
mem = nvme_dmamem_alloc(sc, sizeof(*identify));
if (mem == NULL)
return ENOMEM;
memset(&sqe, 0, sizeof(sqe));
sqe.opcode = NVM_ADMIN_IDENTIFY;
htolem32(&sqe.nsid, nsid);
htolem64(&sqe.entry.prp[0], NVME_DMA_DVA(mem));
htolem32(&sqe.cdw10, 0);
ccb->ccb_done = nvme_empty_done;
ccb->ccb_cookie = &sqe;
nvme_dmamem_sync(sc, mem, BUS_DMASYNC_PREREAD);
rv = nvme_poll(sc, sc->sc_admin_q, ccb, nvme_sqe_fill, NVME_TIMO_IDENT);
nvme_dmamem_sync(sc, mem, BUS_DMASYNC_POSTREAD);
nvme_ccb_put(sc->sc_admin_q, ccb);
if (rv != 0) {
rv = EIO;
goto done;
}
/* commit */
identify = kmem_zalloc(sizeof(*identify), KM_SLEEP);
*identify = *((volatile struct nvm_identify_namespace *)NVME_DMA_KVA(mem));
//memcpy(identify, NVME_DMA_KVA(mem), sizeof(*identify));
ns = nvme_ns_get(sc, nsid);
KASSERT(ns);
ns->ident = identify;
done:
nvme_dmamem_free(sc, mem);
return rv;
}
int
nvme_ns_dobio(struct nvme_softc *sc, uint16_t nsid, void *cookie,
struct buf *bp, void *data, size_t datasize,
int secsize, daddr_t blkno, int flags, nvme_nnc_done nnc_done)
{
struct nvme_queue *q = nvme_get_q(sc);
struct nvme_ccb *ccb;
bus_dmamap_t dmap;
int i, error;
ccb = nvme_ccb_get(q);
if (ccb == NULL)
return EAGAIN;
ccb->ccb_done = nvme_ns_io_done;
ccb->ccb_cookie = cookie;
/* namespace context */
ccb->nnc_nsid = nsid;
ccb->nnc_flags = flags;
ccb->nnc_buf = bp;
ccb->nnc_datasize = datasize;
ccb->nnc_secsize = secsize;
ccb->nnc_blkno = blkno;
ccb->nnc_done = nnc_done;
dmap = ccb->ccb_dmamap;
error = bus_dmamap_load(sc->sc_dmat, dmap, data,
datasize, NULL,
(ISSET(flags, NVME_NS_CTX_F_POLL) ?
BUS_DMA_NOWAIT : BUS_DMA_WAITOK) |
(ISSET(flags, NVME_NS_CTX_F_READ) ?
BUS_DMA_READ : BUS_DMA_WRITE));
if (error) {
nvme_ccb_put(q, ccb);
return error;
}
bus_dmamap_sync(sc->sc_dmat, dmap, 0, dmap->dm_mapsize,
ISSET(flags, NVME_NS_CTX_F_READ) ?
BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
if (dmap->dm_nsegs > 2) {
for (i = 1; i < dmap->dm_nsegs; i++) {
htolem64(&ccb->ccb_prpl[i - 1],
dmap->dm_segs[i].ds_addr);
}
bus_dmamap_sync(sc->sc_dmat,
NVME_DMA_MAP(q->q_ccb_prpls),
ccb->ccb_prpl_off,
sizeof(*ccb->ccb_prpl) * (dmap->dm_nsegs - 1),
BUS_DMASYNC_PREWRITE);
}
if (ISSET(flags, NVME_NS_CTX_F_POLL)) {
if (nvme_poll(sc, q, ccb, nvme_ns_io_fill, NVME_TIMO_PT) != 0)
return EIO;
return 0;
}
nvme_q_submit(sc, q, ccb, nvme_ns_io_fill);
return 0;
}
static void
nvme_ns_io_fill(struct nvme_queue *q, struct nvme_ccb *ccb, void *slot)
{
struct nvme_sqe_io *sqe = slot;
bus_dmamap_t dmap = ccb->ccb_dmamap;
sqe->opcode = ISSET(ccb->nnc_flags, NVME_NS_CTX_F_READ) ?
NVM_CMD_READ : NVM_CMD_WRITE;
htolem32(&sqe->nsid, ccb->nnc_nsid);
htolem64(&sqe->entry.prp[0], dmap->dm_segs[0].ds_addr);
switch (dmap->dm_nsegs) {
case 1:
break;
case 2:
htolem64(&sqe->entry.prp[1], dmap->dm_segs[1].ds_addr);
break;
default:
/* the prp list is already set up and synced */
htolem64(&sqe->entry.prp[1], ccb->ccb_prpl_dva);
break;
}
htolem64(&sqe->slba, ccb->nnc_blkno);
if (ISSET(ccb->nnc_flags, NVME_NS_CTX_F_FUA))
htolem16(&sqe->ioflags, NVM_SQE_IO_FUA);
/* guaranteed by upper layers, but check just in case */
KASSERT((ccb->nnc_datasize % ccb->nnc_secsize) == 0);
htolem16(&sqe->nlb, (ccb->nnc_datasize / ccb->nnc_secsize) - 1);
}
static void
nvme_ns_io_done(struct nvme_queue *q, struct nvme_ccb *ccb,
struct nvme_cqe *cqe)
{
struct nvme_softc *sc = q->q_sc;
bus_dmamap_t dmap = ccb->ccb_dmamap;
void *nnc_cookie = ccb->ccb_cookie;
nvme_nnc_done nnc_done = ccb->nnc_done;
struct buf *bp = ccb->nnc_buf;
if (dmap->dm_nsegs > 2) {
bus_dmamap_sync(sc->sc_dmat,
NVME_DMA_MAP(q->q_ccb_prpls),
ccb->ccb_prpl_off,
sizeof(*ccb->ccb_prpl) * (dmap->dm_nsegs - 1),
BUS_DMASYNC_POSTWRITE);
}
bus_dmamap_sync(sc->sc_dmat, dmap, 0, dmap->dm_mapsize,
ISSET(ccb->nnc_flags, NVME_NS_CTX_F_READ) ?
BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
bus_dmamap_unload(sc->sc_dmat, dmap);
nvme_ccb_put(q, ccb);
nnc_done(nnc_cookie, bp, lemtoh16(&cqe->flags), lemtoh32(&cqe->cdw0));
}
/*
* If there is no volatile write cache, it makes no sense to issue
* flush commands or query for the status.
*/
bool
nvme_has_volatile_write_cache(struct nvme_softc *sc)
{
/* sc_identify is filled during attachment */
return ((sc->sc_identify.vwc & NVME_ID_CTRLR_VWC_PRESENT) != 0);
}
int
nvme_ns_sync(struct nvme_softc *sc, uint16_t nsid, void *cookie,
int flags, nvme_nnc_done nnc_done)
{
struct nvme_queue *q = nvme_get_q(sc);
struct nvme_ccb *ccb;
ccb = nvme_ccb_get(q);
if (ccb == NULL)
return EAGAIN;
ccb->ccb_done = nvme_ns_sync_done;
ccb->ccb_cookie = cookie;
/* namespace context */
ccb->nnc_nsid = nsid;
ccb->nnc_flags = flags;
ccb->nnc_done = nnc_done;
if (ISSET(flags, NVME_NS_CTX_F_POLL)) {
if (nvme_poll(sc, q, ccb, nvme_ns_sync_fill, NVME_TIMO_SY) != 0)
return EIO;
return 0;
}
nvme_q_submit(sc, q, ccb, nvme_ns_sync_fill);
return 0;
}
static void
nvme_ns_sync_fill(struct nvme_queue *q, struct nvme_ccb *ccb, void *slot)
{
struct nvme_sqe *sqe = slot;
sqe->opcode = NVM_CMD_FLUSH;
htolem32(&sqe->nsid, ccb->nnc_nsid);
}
static void
nvme_ns_sync_done(struct nvme_queue *q, struct nvme_ccb *ccb,
struct nvme_cqe *cqe)
{
void *cookie = ccb->ccb_cookie;
nvme_nnc_done nnc_done = ccb->nnc_done;
nvme_ccb_put(q, ccb);
nnc_done(cookie, NULL, lemtoh16(&cqe->flags), lemtoh32(&cqe->cdw0));
}
/*
* Get status of volatile write cache. Always asynchronous.
*/
int
nvme_admin_getcache(struct nvme_softc *sc, void *cookie, nvme_nnc_done nnc_done)
{
struct nvme_ccb *ccb;
struct nvme_queue *q = sc->sc_admin_q;
ccb = nvme_ccb_get(q);
if (ccb == NULL)
return EAGAIN;
ccb->ccb_done = nvme_getcache_done;
ccb->ccb_cookie = cookie;
/* namespace context */
ccb->nnc_flags = 0;
ccb->nnc_done = nnc_done;
nvme_q_submit(sc, q, ccb, nvme_getcache_fill);
return 0;
}
static void
nvme_getcache_fill(struct nvme_queue *q, struct nvme_ccb *ccb, void *slot)
{
struct nvme_sqe *sqe = slot;
sqe->opcode = NVM_ADMIN_GET_FEATURES;
sqe->cdw10 = NVM_FEATURE_VOLATILE_WRITE_CACHE;
}
static void
nvme_getcache_done(struct nvme_queue *q, struct nvme_ccb *ccb,
struct nvme_cqe *cqe)
{
void *cookie = ccb->ccb_cookie;
nvme_nnc_done nnc_done = ccb->nnc_done;
nvme_ccb_put(q, ccb);
nnc_done(cookie, NULL, lemtoh16(&cqe->flags), lemtoh32(&cqe->cdw0));
}
void
nvme_ns_free(struct nvme_softc *sc, uint16_t nsid)
{
struct nvme_namespace *ns;
struct nvm_identify_namespace *identify;
ns = nvme_ns_get(sc, nsid);
KASSERT(ns);
identify = ns->ident;
ns->ident = NULL;
if (identify != NULL)
kmem_free(identify, sizeof(*identify));
}
static void
nvme_pt_fill(struct nvme_queue *q, struct nvme_ccb *ccb, void *slot)
{
struct nvme_softc *sc = q->q_sc;
struct nvme_sqe *sqe = slot;
struct nvme_pt_command *pt = ccb->ccb_cookie;
bus_dmamap_t dmap = ccb->ccb_dmamap;
int i;
sqe->opcode = pt->cmd.opcode;
htolem32(&sqe->nsid, pt->cmd.nsid);
if (pt->buf != NULL && pt->len > 0) {
htolem64(&sqe->entry.prp[0], dmap->dm_segs[0].ds_addr);
switch (dmap->dm_nsegs) {
case 1:
break;
case 2:
htolem64(&sqe->entry.prp[1], dmap->dm_segs[1].ds_addr);
break;
default:
for (i = 1; i < dmap->dm_nsegs; i++) {
htolem64(&ccb->ccb_prpl[i - 1],
dmap->dm_segs[i].ds_addr);
}
bus_dmamap_sync(sc->sc_dmat,
NVME_DMA_MAP(q->q_ccb_prpls),
ccb->ccb_prpl_off,
sizeof(*ccb->ccb_prpl) * (dmap->dm_nsegs - 1),
BUS_DMASYNC_PREWRITE);
htolem64(&sqe->entry.prp[1], ccb->ccb_prpl_dva);
break;
}
}
htolem32(&sqe->cdw10, pt->cmd.cdw10);
htolem32(&sqe->cdw11, pt->cmd.cdw11);
htolem32(&sqe->cdw12, pt->cmd.cdw12);
htolem32(&sqe->cdw13, pt->cmd.cdw13);
htolem32(&sqe->cdw14, pt->cmd.cdw14);
htolem32(&sqe->cdw15, pt->cmd.cdw15);
}
static void
nvme_pt_done(struct nvme_queue *q, struct nvme_ccb *ccb, struct nvme_cqe *cqe)
{
struct nvme_softc *sc = q->q_sc;
struct nvme_pt_command *pt = ccb->ccb_cookie;
bus_dmamap_t dmap = ccb->ccb_dmamap;
if (pt->buf != NULL && pt->len > 0) {
if (dmap->dm_nsegs > 2) {
bus_dmamap_sync(sc->sc_dmat,
NVME_DMA_MAP(q->q_ccb_prpls),
ccb->ccb_prpl_off,
sizeof(*ccb->ccb_prpl) * (dmap->dm_nsegs - 1),
BUS_DMASYNC_POSTWRITE);
}
bus_dmamap_sync(sc->sc_dmat, dmap, 0, dmap->dm_mapsize,
pt->is_read ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
bus_dmamap_unload(sc->sc_dmat, dmap);
}
pt->cpl.cdw0 = lemtoh32(&cqe->cdw0);
pt->cpl.flags = lemtoh16(&cqe->flags) & ~NVME_CQE_PHASE;
}
static int
nvme_command_passthrough(struct nvme_softc *sc, struct nvme_pt_command *pt,
uint16_t nsid, struct lwp *l, bool is_adminq)
{
struct nvme_queue *q;
struct nvme_ccb *ccb;
void *buf = NULL;
int error;
/* limit command size to maximum data transfer size */
if ((pt->buf == NULL && pt->len > 0) ||
(pt->buf != NULL && (pt->len == 0 || pt->len > sc->sc_mdts)))
return EINVAL;
q = is_adminq ? sc->sc_admin_q : nvme_get_q(sc);
ccb = nvme_ccb_get(q);
if (ccb == NULL)
return EBUSY;
if (pt->buf != NULL) {
KASSERT(pt->len > 0);
buf = kmem_alloc(pt->len, KM_SLEEP);
if (!pt->is_read) {
error = copyin(pt->buf, buf, pt->len);
if (error)
goto kmem_free;
}
error = bus_dmamap_load(sc->sc_dmat, ccb->ccb_dmamap, buf,
pt->len, NULL,
BUS_DMA_WAITOK |
(pt->is_read ? BUS_DMA_READ : BUS_DMA_WRITE));
if (error)
goto kmem_free;
bus_dmamap_sync(sc->sc_dmat, ccb->ccb_dmamap,
0, ccb->ccb_dmamap->dm_mapsize,
pt->is_read ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
}
ccb->ccb_done = nvme_pt_done;
ccb->ccb_cookie = pt;
pt->cmd.nsid = nsid;
if (nvme_poll(sc, q, ccb, nvme_pt_fill, NVME_TIMO_PT)) {
error = EIO;
goto out;
}
error = 0;
out:
if (buf != NULL) {
if (error == 0 && pt->is_read)
error = copyout(buf, pt->buf, pt->len);
kmem_free:
kmem_free(buf, pt->len);
}
nvme_ccb_put(q, ccb);
return error;
}
static void
nvme_q_submit(struct nvme_softc *sc, struct nvme_queue *q, struct nvme_ccb *ccb,
void (*fill)(struct nvme_queue *, struct nvme_ccb *, void *))
{
struct nvme_sqe *sqe = NVME_DMA_KVA(q->q_sq_dmamem);
uint32_t tail;
mutex_enter(&q->q_sq_mtx);
tail = q->q_sq_tail;
if (++q->q_sq_tail >= q->q_entries)
q->q_sq_tail = 0;
sqe += tail;
bus_dmamap_sync(sc->sc_dmat, NVME_DMA_MAP(q->q_sq_dmamem),
sizeof(*sqe) * tail, sizeof(*sqe), BUS_DMASYNC_POSTWRITE);
memset(sqe, 0, sizeof(*sqe));
(*fill)(q, ccb, sqe);
sqe->cid = ccb->ccb_id;
bus_dmamap_sync(sc->sc_dmat, NVME_DMA_MAP(q->q_sq_dmamem),
sizeof(*sqe) * tail, sizeof(*sqe), BUS_DMASYNC_PREWRITE);
nvme_write4(sc, q->q_sqtdbl, q->q_sq_tail);
mutex_exit(&q->q_sq_mtx);
}
struct nvme_poll_state {
struct nvme_sqe s;
struct nvme_cqe c;
};
static int
nvme_poll(struct nvme_softc *sc, struct nvme_queue *q, struct nvme_ccb *ccb,
void (*fill)(struct nvme_queue *, struct nvme_ccb *, void *), int timo_sec)
{
struct nvme_poll_state state;
void (*done)(struct nvme_queue *, struct nvme_ccb *, struct nvme_cqe *);
void *cookie;
uint16_t flags;
int step = 10;
int maxloop = timo_sec * 1000000 / step;
int error = 0;
memset(&state, 0, sizeof(state));
(*fill)(q, ccb, &state.s);
done = ccb->ccb_done;
cookie = ccb->ccb_cookie;
ccb->ccb_done = nvme_poll_done;
ccb->ccb_cookie = &state;
nvme_q_submit(sc, q, ccb, nvme_poll_fill);
while (!ISSET(state.c.flags, htole16(NVME_CQE_PHASE))) {
if (nvme_q_complete(sc, q) == 0)
delay(step);
if (timo_sec >= 0 && --maxloop <= 0) {
error = ETIMEDOUT;
break;
}
}
ccb->ccb_cookie = cookie;
done(q, ccb, &state.c);
if (error == 0) {
flags = lemtoh16(&state.c.flags);
return flags & ~NVME_CQE_PHASE;
} else {
return 1;
}
}
static void
nvme_poll_fill(struct nvme_queue *q, struct nvme_ccb *ccb, void *slot)
{
struct nvme_sqe *sqe = slot;
struct nvme_poll_state *state = ccb->ccb_cookie;
*sqe = state->s;
}
static void
nvme_poll_done(struct nvme_queue *q, struct nvme_ccb *ccb,
struct nvme_cqe *cqe)
{
struct nvme_poll_state *state = ccb->ccb_cookie;
SET(cqe->flags, htole16(NVME_CQE_PHASE));
state->c = *cqe;
}
static void
nvme_sqe_fill(struct nvme_queue *q, struct nvme_ccb *ccb, void *slot)
{
struct nvme_sqe *src = ccb->ccb_cookie;
struct nvme_sqe *dst = slot;
*dst = *src;
}
static void
nvme_empty_done(struct nvme_queue *q, struct nvme_ccb *ccb,
struct nvme_cqe *cqe)
{
}
static int
nvme_q_complete(struct nvme_softc *sc, struct nvme_queue *q)
{
struct nvme_ccb *ccb;
struct nvme_cqe *ring = NVME_DMA_KVA(q->q_cq_dmamem), *cqe;
uint16_t flags;
int rv = 0;
mutex_enter(&q->q_cq_mtx);
nvme_dmamem_sync(sc, q->q_cq_dmamem, BUS_DMASYNC_POSTREAD);
for (;;) {
cqe = &ring[q->q_cq_head];
flags = lemtoh16(&cqe->flags);
if ((flags & NVME_CQE_PHASE) != q->q_cq_phase)
break;
ccb = &q->q_ccbs[cqe->cid];
if (++q->q_cq_head >= q->q_entries) {
q->q_cq_head = 0;
q->q_cq_phase ^= NVME_CQE_PHASE;
}
#ifdef DEBUG
/*
* If we get spurious completion notification, something
* is seriously hosed up. Very likely DMA to some random
* memory place happened, so just bail out.
*/
if ((intptr_t)ccb->ccb_cookie == NVME_CCB_FREE) {
panic("%s: invalid ccb detected",
device_xname(sc->sc_dev));
/* NOTREACHED */
}
#endif
rv++;
/*
* Unlock the mutex before calling the ccb_done callback
* and re-lock afterwards. The callback triggers lddone()
* which schedules another i/o, and also calls nvme_ccb_put().
* Unlock/relock avoids possibility of deadlock.
*/
mutex_exit(&q->q_cq_mtx);
ccb->ccb_done(q, ccb, cqe);
mutex_enter(&q->q_cq_mtx);
}
nvme_dmamem_sync(sc, q->q_cq_dmamem, BUS_DMASYNC_PREREAD);
if (rv)
nvme_write4(sc, q->q_cqhdbl, q->q_cq_head);
mutex_exit(&q->q_cq_mtx);
if (rv) {
mutex_enter(&q->q_ccb_mtx);
q->q_nccbs_avail += rv;
mutex_exit(&q->q_ccb_mtx);
}
return rv;
}
static int
nvme_identify(struct nvme_softc *sc, u_int mps)
{
char sn[41], mn[81], fr[17];
struct nvm_identify_controller *identify;
struct nvme_dmamem *mem;
struct nvme_ccb *ccb;
u_int mdts;
int rv = 1;
ccb = nvme_ccb_get(sc->sc_admin_q);
KASSERT(ccb != NULL); /* it's a bug if we don't have spare ccb here */
mem = nvme_dmamem_alloc(sc, sizeof(*identify));
if (mem == NULL)
return 1;
ccb->ccb_done = nvme_empty_done;
ccb->ccb_cookie = mem;
nvme_dmamem_sync(sc, mem, BUS_DMASYNC_PREREAD);
rv = nvme_poll(sc, sc->sc_admin_q, ccb, nvme_fill_identify,
NVME_TIMO_IDENT);
nvme_dmamem_sync(sc, mem, BUS_DMASYNC_POSTREAD);
nvme_ccb_put(sc->sc_admin_q, ccb);
if (rv != 0)
goto done;
identify = NVME_DMA_KVA(mem);
strnvisx(sn, sizeof(sn), (const char *)identify->sn,
sizeof(identify->sn), VIS_TRIM|VIS_SAFE|VIS_OCTAL);
strnvisx(mn, sizeof(mn), (const char *)identify->mn,
sizeof(identify->mn), VIS_TRIM|VIS_SAFE|VIS_OCTAL);
strnvisx(fr, sizeof(fr), (const char *)identify->fr,
sizeof(identify->fr), VIS_TRIM|VIS_SAFE|VIS_OCTAL);
aprint_normal_dev(sc->sc_dev, "%s, firmware %s, serial %s\n", mn, fr,
sn);
if (identify->mdts > 0) {
mdts = (1 << identify->mdts) * (1 << mps);
if (mdts < sc->sc_mdts)
sc->sc_mdts = mdts;
}
sc->sc_nn = lemtoh32(&identify->nn);
memcpy(&sc->sc_identify, identify, sizeof(sc->sc_identify));
done:
nvme_dmamem_free(sc, mem);
return rv;
}
static int
nvme_q_create(struct nvme_softc *sc, struct nvme_queue *q)
{
struct nvme_sqe_q sqe;
struct nvme_ccb *ccb;
int rv;
if (sc->sc_use_mq && sc->sc_intr_establish(sc, q->q_id, q) != 0)
return 1;
ccb = nvme_ccb_get(sc->sc_admin_q);
KASSERT(ccb != NULL);
ccb->ccb_done = nvme_empty_done;
ccb->ccb_cookie = &sqe;
memset(&sqe, 0, sizeof(sqe));
sqe.opcode = NVM_ADMIN_ADD_IOCQ;
htolem64(&sqe.prp1, NVME_DMA_DVA(q->q_cq_dmamem));
htolem16(&sqe.qsize, q->q_entries - 1);
htolem16(&sqe.qid, q->q_id);
sqe.qflags = NVM_SQE_CQ_IEN | NVM_SQE_Q_PC;
if (sc->sc_use_mq)
htolem16(&sqe.cqid, q->q_id); /* qid == vector */
rv = nvme_poll(sc, sc->sc_admin_q, ccb, nvme_sqe_fill, NVME_TIMO_QOP);
if (rv != 0)
goto fail;
ccb->ccb_done = nvme_empty_done;
ccb->ccb_cookie = &sqe;
memset(&sqe, 0, sizeof(sqe));
sqe.opcode = NVM_ADMIN_ADD_IOSQ;
htolem64(&sqe.prp1, NVME_DMA_DVA(q->q_sq_dmamem));
htolem16(&sqe.qsize, q->q_entries - 1);
htolem16(&sqe.qid, q->q_id);
htolem16(&sqe.cqid, q->q_id);
sqe.qflags = NVM_SQE_Q_PC;
rv = nvme_poll(sc, sc->sc_admin_q, ccb, nvme_sqe_fill, NVME_TIMO_QOP);
if (rv != 0)
goto fail;
fail:
nvme_ccb_put(sc->sc_admin_q, ccb);
return rv;
}
static int
nvme_q_delete(struct nvme_softc *sc, struct nvme_queue *q)
{
struct nvme_sqe_q sqe;
struct nvme_ccb *ccb;
int rv;
ccb = nvme_ccb_get(sc->sc_admin_q);
KASSERT(ccb != NULL);
ccb->ccb_done = nvme_empty_done;
ccb->ccb_cookie = &sqe;
memset(&sqe, 0, sizeof(sqe));
sqe.opcode = NVM_ADMIN_DEL_IOSQ;
htolem16(&sqe.qid, q->q_id);
rv = nvme_poll(sc, sc->sc_admin_q, ccb, nvme_sqe_fill, NVME_TIMO_QOP);
if (rv != 0)
goto fail;
ccb->ccb_done = nvme_empty_done;
ccb->ccb_cookie = &sqe;
memset(&sqe, 0, sizeof(sqe));
sqe.opcode = NVM_ADMIN_DEL_IOCQ;
htolem16(&sqe.qid, q->q_id);
rv = nvme_poll(sc, sc->sc_admin_q, ccb, nvme_sqe_fill, NVME_TIMO_QOP);
if (rv != 0)
goto fail;
fail:
nvme_ccb_put(sc->sc_admin_q, ccb);
if (rv == 0 && sc->sc_use_mq) {
if (sc->sc_intr_disestablish(sc, q->q_id))
rv = 1;
}
return rv;
}
static void
nvme_fill_identify(struct nvme_queue *q, struct nvme_ccb *ccb, void *slot)
{
struct nvme_sqe *sqe = slot;
struct nvme_dmamem *mem = ccb->ccb_cookie;
sqe->opcode = NVM_ADMIN_IDENTIFY;
htolem64(&sqe->entry.prp[0], NVME_DMA_DVA(mem));
htolem32(&sqe->cdw10, 1);
}
static int
nvme_get_number_of_queues(struct nvme_softc *sc, u_int *nqap)
{
struct nvme_pt_command pt;
struct nvme_ccb *ccb;
uint16_t ncqa, nsqa;
int rv;
ccb = nvme_ccb_get(sc->sc_admin_q);
KASSERT(ccb != NULL); /* it's a bug if we don't have spare ccb here */
memset(&pt, 0, sizeof(pt));
pt.cmd.opcode = NVM_ADMIN_GET_FEATURES;
pt.cmd.cdw10 = NVM_FEATURE_NUMBER_OF_QUEUES;
ccb->ccb_done = nvme_pt_done;
ccb->ccb_cookie = &pt;
rv = nvme_poll(sc, sc->sc_admin_q, ccb, nvme_pt_fill, NVME_TIMO_QOP);
nvme_ccb_put(sc->sc_admin_q, ccb);
if (rv != 0) {
*nqap = 0;
return EIO;
}
ncqa = pt.cpl.cdw0 >> 16;
nsqa = pt.cpl.cdw0 & 0xffff;
*nqap = MIN(ncqa, nsqa) + 1;
return 0;
}
static int
nvme_ccbs_alloc(struct nvme_queue *q, uint16_t nccbs)
{
struct nvme_softc *sc = q->q_sc;
struct nvme_ccb *ccb;
bus_addr_t off;
uint64_t *prpl;
u_int i;
mutex_init(&q->q_ccb_mtx, MUTEX_DEFAULT, IPL_BIO);
SIMPLEQ_INIT(&q->q_ccb_list);
q->q_ccbs = kmem_alloc(sizeof(*ccb) * nccbs, KM_SLEEP);
q->q_nccbs = nccbs;
q->q_nccbs_avail = nccbs;
q->q_ccb_prpls = nvme_dmamem_alloc(sc,
sizeof(*prpl) * sc->sc_max_sgl * nccbs);
prpl = NVME_DMA_KVA(q->q_ccb_prpls);
off = 0;
for (i = 0; i < nccbs; i++) {
ccb = &q->q_ccbs[i];
if (bus_dmamap_create(sc->sc_dmat, sc->sc_mdts,
sc->sc_max_sgl + 1 /* we get a free prp in the sqe */,
sc->sc_mps, sc->sc_mps, BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW,
&ccb->ccb_dmamap) != 0)
goto free_maps;
ccb->ccb_id = i;
ccb->ccb_prpl = prpl;
ccb->ccb_prpl_off = off;
ccb->ccb_prpl_dva = NVME_DMA_DVA(q->q_ccb_prpls) + off;
SIMPLEQ_INSERT_TAIL(&q->q_ccb_list, ccb, ccb_entry);
prpl += sc->sc_max_sgl;
off += sizeof(*prpl) * sc->sc_max_sgl;
}
return 0;
free_maps:
nvme_ccbs_free(q);
return 1;
}
static struct nvme_ccb *
nvme_ccb_get(struct nvme_queue *q)
{
struct nvme_ccb *ccb = NULL;
mutex_enter(&q->q_ccb_mtx);
if (q->q_nccbs_avail > 0) {
ccb = SIMPLEQ_FIRST(&q->q_ccb_list);
KASSERT(ccb != NULL);
q->q_nccbs_avail--;
SIMPLEQ_REMOVE_HEAD(&q->q_ccb_list, ccb_entry);
#ifdef DEBUG
ccb->ccb_cookie = NULL;
#endif
}
mutex_exit(&q->q_ccb_mtx);
return ccb;
}
static void
nvme_ccb_put(struct nvme_queue *q, struct nvme_ccb *ccb)
{
mutex_enter(&q->q_ccb_mtx);
#ifdef DEBUG
ccb->ccb_cookie = (void *)NVME_CCB_FREE;
#endif
SIMPLEQ_INSERT_HEAD(&q->q_ccb_list, ccb, ccb_entry);
mutex_exit(&q->q_ccb_mtx);
}
static void
nvme_ccbs_free(struct nvme_queue *q)
{
struct nvme_softc *sc = q->q_sc;
struct nvme_ccb *ccb;
mutex_enter(&q->q_ccb_mtx);
while ((ccb = SIMPLEQ_FIRST(&q->q_ccb_list)) != NULL) {
SIMPLEQ_REMOVE_HEAD(&q->q_ccb_list, ccb_entry);
bus_dmamap_destroy(sc->sc_dmat, ccb->ccb_dmamap);
}
mutex_exit(&q->q_ccb_mtx);
nvme_dmamem_free(sc, q->q_ccb_prpls);
kmem_free(q->q_ccbs, sizeof(*ccb) * q->q_nccbs);
q->q_ccbs = NULL;
mutex_destroy(&q->q_ccb_mtx);
}
static struct nvme_queue *
nvme_q_alloc(struct nvme_softc *sc, uint16_t id, u_int entries, u_int dstrd)
{
struct nvme_queue *q;
q = kmem_alloc(sizeof(*q), KM_SLEEP);
q->q_sc = sc;
q->q_sq_dmamem = nvme_dmamem_alloc(sc,
sizeof(struct nvme_sqe) * entries);
if (q->q_sq_dmamem == NULL)
goto free;
q->q_cq_dmamem = nvme_dmamem_alloc(sc,
sizeof(struct nvme_cqe) * entries);
if (q->q_cq_dmamem == NULL)
goto free_sq;
memset(NVME_DMA_KVA(q->q_sq_dmamem), 0, NVME_DMA_LEN(q->q_sq_dmamem));
memset(NVME_DMA_KVA(q->q_cq_dmamem), 0, NVME_DMA_LEN(q->q_cq_dmamem));
mutex_init(&q->q_sq_mtx, MUTEX_DEFAULT, IPL_BIO);
mutex_init(&q->q_cq_mtx, MUTEX_DEFAULT, IPL_BIO);
q->q_sqtdbl = NVME_SQTDBL(id, dstrd);
q->q_cqhdbl = NVME_CQHDBL(id, dstrd);
q->q_id = id;
q->q_entries = entries;
q->q_sq_tail = 0;
q->q_cq_head = 0;
q->q_cq_phase = NVME_CQE_PHASE;
nvme_dmamem_sync(sc, q->q_sq_dmamem, BUS_DMASYNC_PREWRITE);
nvme_dmamem_sync(sc, q->q_cq_dmamem, BUS_DMASYNC_PREREAD);
/*
* Due to definition of full and empty queue (queue is empty
* when head == tail, full when tail is one less then head),
* we can actually only have (entries - 1) in-flight commands.
*/
if (nvme_ccbs_alloc(q, entries - 1) != 0) {
aprint_error_dev(sc->sc_dev, "unable to allocate ccbs\n");
goto free_cq;
}
return q;
free_cq:
nvme_dmamem_free(sc, q->q_cq_dmamem);
free_sq:
nvme_dmamem_free(sc, q->q_sq_dmamem);
free:
kmem_free(q, sizeof(*q));
return NULL;
}
static void
nvme_q_free(struct nvme_softc *sc, struct nvme_queue *q)
{
nvme_ccbs_free(q);
mutex_destroy(&q->q_sq_mtx);
mutex_destroy(&q->q_cq_mtx);
nvme_dmamem_sync(sc, q->q_cq_dmamem, BUS_DMASYNC_POSTREAD);
nvme_dmamem_sync(sc, q->q_sq_dmamem, BUS_DMASYNC_POSTWRITE);
nvme_dmamem_free(sc, q->q_cq_dmamem);
nvme_dmamem_free(sc, q->q_sq_dmamem);
kmem_free(q, sizeof(*q));
}
int
nvme_intr(void *xsc)
{
struct nvme_softc *sc = xsc;
/*
* INTx is level triggered, controller deasserts the interrupt only
* when we advance command queue head via write to the doorbell.
* Tell the controller to block the interrupts while we process
* the queue(s).
*/
nvme_write4(sc, NVME_INTMS, 1);
softint_schedule(sc->sc_softih[0]);
/* don't know, might not have been for us */
return 1;
}
void
nvme_softintr_intx(void *xq)
{
struct nvme_queue *q = xq;
struct nvme_softc *sc = q->q_sc;
nvme_q_complete(sc, sc->sc_admin_q);
if (sc->sc_q != NULL)
nvme_q_complete(sc, sc->sc_q[0]);
/*
* Processing done, tell controller to issue interrupts again. There
* is no race, as NVMe spec requires the controller to maintain state,
* and assert the interrupt whenever there are unacknowledged
* completion queue entries.
*/
nvme_write4(sc, NVME_INTMC, 1);
}
int
nvme_intr_msi(void *xq)
{
struct nvme_queue *q = xq;
KASSERT(q && q->q_sc && q->q_sc->sc_softih
&& q->q_sc->sc_softih[q->q_id]);
/*
* MSI/MSI-X are edge triggered, so can handover processing to softint
* without masking the interrupt.
*/
softint_schedule(q->q_sc->sc_softih[q->q_id]);
return 1;
}
void
nvme_softintr_msi(void *xq)
{
struct nvme_queue *q = xq;
struct nvme_softc *sc = q->q_sc;
nvme_q_complete(sc, q);
}
static struct nvme_dmamem *
nvme_dmamem_alloc(struct nvme_softc *sc, size_t size)
{
struct nvme_dmamem *ndm;
int nsegs;
ndm = kmem_zalloc(sizeof(*ndm), KM_SLEEP);
if (ndm == NULL)
return NULL;
ndm->ndm_size = size;
if (bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW, &ndm->ndm_map) != 0)
goto ndmfree;
if (bus_dmamem_alloc(sc->sc_dmat, size, sc->sc_mps, 0, &ndm->ndm_seg,
1, &nsegs, BUS_DMA_WAITOK) != 0)
goto destroy;
if (bus_dmamem_map(sc->sc_dmat, &ndm->ndm_seg, nsegs, size,
&ndm->ndm_kva, BUS_DMA_WAITOK) != 0)
goto free;
memset(ndm->ndm_kva, 0, size);
if (bus_dmamap_load(sc->sc_dmat, ndm->ndm_map, ndm->ndm_kva, size,
NULL, BUS_DMA_WAITOK) != 0)
goto unmap;
return ndm;
unmap:
bus_dmamem_unmap(sc->sc_dmat, ndm->ndm_kva, size);
free:
bus_dmamem_free(sc->sc_dmat, &ndm->ndm_seg, 1);
destroy:
bus_dmamap_destroy(sc->sc_dmat, ndm->ndm_map);
ndmfree:
kmem_free(ndm, sizeof(*ndm));
return NULL;
}
static void
nvme_dmamem_sync(struct nvme_softc *sc, struct nvme_dmamem *mem, int ops)
{
bus_dmamap_sync(sc->sc_dmat, NVME_DMA_MAP(mem),
0, NVME_DMA_LEN(mem), ops);
}
void
nvme_dmamem_free(struct nvme_softc *sc, struct nvme_dmamem *ndm)
{
bus_dmamap_unload(sc->sc_dmat, ndm->ndm_map);
bus_dmamem_unmap(sc->sc_dmat, ndm->ndm_kva, ndm->ndm_size);
bus_dmamem_free(sc->sc_dmat, &ndm->ndm_seg, 1);
bus_dmamap_destroy(sc->sc_dmat, ndm->ndm_map);
kmem_free(ndm, sizeof(*ndm));
}
/*
* ioctl
*/
dev_type_open(nvmeopen);
dev_type_close(nvmeclose);
dev_type_ioctl(nvmeioctl);
const struct cdevsw nvme_cdevsw = {
.d_open = nvmeopen,
.d_close = nvmeclose,
.d_read = noread,
.d_write = nowrite,
.d_ioctl = nvmeioctl,
.d_stop = nostop,
.d_tty = notty,
.d_poll = nopoll,
.d_mmap = nommap,
.d_kqfilter = nokqfilter,
.d_discard = nodiscard,
.d_flag = D_OTHER,
};
/*
* Accept an open operation on the control device.
*/
int
nvmeopen(dev_t dev, int flag, int mode, struct lwp *l)
{
struct nvme_softc *sc;
int unit = minor(dev) / 0x10000;
int nsid = minor(dev) & 0xffff;
int nsidx;
if ((sc = device_lookup_private(&nvme_cd, unit)) == NULL)
return ENXIO;
if ((sc->sc_flags & NVME_F_ATTACHED) == 0)
return ENXIO;
if (nsid == 0) {
/* controller */
if (ISSET(sc->sc_flags, NVME_F_OPEN))
return EBUSY;
SET(sc->sc_flags, NVME_F_OPEN);
} else {
/* namespace */
nsidx = nsid - 1;
if (nsidx >= sc->sc_nn || sc->sc_namespaces[nsidx].dev == NULL)
return ENXIO;
if (ISSET(sc->sc_namespaces[nsidx].flags, NVME_NS_F_OPEN))
return EBUSY;
SET(sc->sc_namespaces[nsidx].flags, NVME_NS_F_OPEN);
}
return 0;
}
/*
* Accept the last close on the control device.
*/
int
nvmeclose(dev_t dev, int flag, int mode, struct lwp *l)
{
struct nvme_softc *sc;
int unit = minor(dev) / 0x10000;
int nsid = minor(dev) & 0xffff;
int nsidx;
sc = device_lookup_private(&nvme_cd, unit);
if (sc == NULL)
return ENXIO;
if (nsid == 0) {
/* controller */
CLR(sc->sc_flags, NVME_F_OPEN);
} else {
/* namespace */
nsidx = nsid - 1;
if (nsidx >= sc->sc_nn)
return ENXIO;
CLR(sc->sc_namespaces[nsidx].flags, NVME_NS_F_OPEN);
}
return 0;
}
/*
* Handle control operations.
*/
int
nvmeioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
{
struct nvme_softc *sc;
int unit = minor(dev) / 0x10000;
int nsid = minor(dev) & 0xffff;
struct nvme_pt_command *pt;
sc = device_lookup_private(&nvme_cd, unit);
if (sc == NULL)
return ENXIO;
switch (cmd) {
case NVME_PASSTHROUGH_CMD:
pt = data;
return nvme_command_passthrough(sc, data,
nsid == 0 ? pt->cmd.nsid : nsid, l, nsid == 0);
}
return ENOTTY;
}
|