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
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
|
/*-
* Copyright (c) 2012, Bryan Venteicher <bryanv@daemoninthecloset.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice unmodified, this list of conditions, and the following
* disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* Driver for VirtIO SCSI devices. */
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/kthread.h>
#include <sys/malloc.h>
#include <sys/module.h>
#include <sys/sglist.h>
#include <sys/sysctl.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/callout.h>
#include <sys/queue.h>
#include <sys/sbuf.h>
#include <machine/stdarg.h>
#include <machine/bus.h>
#include <machine/resource.h>
#include <sys/bus.h>
#include <sys/rman.h>
#include <cam/cam.h>
#include <cam/cam_ccb.h>
#include <cam/cam_sim.h>
#include <cam/cam_periph.h>
#include <cam/cam_xpt_sim.h>
#include <cam/cam_debug.h>
#include <cam/scsi/scsi_all.h>
#include <cam/scsi/scsi_message.h>
#include <dev/virtio/virtio.h>
#include <dev/virtio/virtqueue.h>
#include <dev/virtio/scsi/virtio_scsi.h>
#include <dev/virtio/scsi/virtio_scsivar.h>
#include "virtio_if.h"
static int vtscsi_modevent(module_t, int, void *);
static int vtscsi_probe(device_t);
static int vtscsi_attach(device_t);
static int vtscsi_detach(device_t);
static int vtscsi_suspend(device_t);
static int vtscsi_resume(device_t);
static void vtscsi_negotiate_features(struct vtscsi_softc *);
static int vtscsi_maximum_segments(struct vtscsi_softc *, int);
static int vtscsi_alloc_virtqueues(struct vtscsi_softc *);
static void vtscsi_write_device_config(struct vtscsi_softc *);
static int vtscsi_reinit(struct vtscsi_softc *);
static int vtscsi_alloc_cam(struct vtscsi_softc *);
static int vtscsi_register_cam(struct vtscsi_softc *);
static void vtscsi_free_cam(struct vtscsi_softc *);
static void vtscsi_cam_async(void *, uint32_t, struct cam_path *, void *);
static int vtscsi_register_async(struct vtscsi_softc *);
static void vtscsi_deregister_async(struct vtscsi_softc *);
static void vtscsi_cam_action(struct cam_sim *, union ccb *);
static void vtscsi_cam_poll(struct cam_sim *);
static void vtscsi_cam_scsi_io(struct vtscsi_softc *, struct cam_sim *,
union ccb *);
static void vtscsi_cam_get_tran_settings(struct vtscsi_softc *,
union ccb *);
static void vtscsi_cam_reset_bus(struct vtscsi_softc *, union ccb *);
static void vtscsi_cam_reset_dev(struct vtscsi_softc *, union ccb *);
static void vtscsi_cam_abort(struct vtscsi_softc *, union ccb *);
static void vtscsi_cam_path_inquiry(struct vtscsi_softc *,
struct cam_sim *, union ccb *);
static int vtscsi_sg_append_scsi_buf(struct vtscsi_softc *,
struct sglist *, struct ccb_scsiio *);
static int vtscsi_fill_scsi_cmd_sglist(struct vtscsi_softc *,
struct vtscsi_request *, int *, int *);
static int vtscsi_execute_scsi_cmd(struct vtscsi_softc *,
struct vtscsi_request *);
static int vtscsi_start_scsi_cmd(struct vtscsi_softc *, union ccb *);
static void vtscsi_complete_abort_timedout_scsi_cmd(struct vtscsi_softc *,
struct vtscsi_request *);
static int vtscsi_abort_timedout_scsi_cmd(struct vtscsi_softc *,
struct vtscsi_request *);
static void vtscsi_timedout_scsi_cmd(void *);
static cam_status vtscsi_scsi_cmd_cam_status(struct virtio_scsi_cmd_resp *);
static cam_status vtscsi_complete_scsi_cmd_response(struct vtscsi_softc *,
struct ccb_scsiio *, struct virtio_scsi_cmd_resp *);
static void vtscsi_complete_scsi_cmd(struct vtscsi_softc *,
struct vtscsi_request *);
static void vtscsi_poll_ctrl_req(struct vtscsi_softc *,
struct vtscsi_request *);
static int vtscsi_execute_ctrl_req(struct vtscsi_softc *,
struct vtscsi_request *, struct sglist *, int, int, int);
static void vtscsi_complete_abort_task_cmd(struct vtscsi_softc *c,
struct vtscsi_request *);
static int vtscsi_execute_abort_task_cmd(struct vtscsi_softc *,
struct vtscsi_request *);
static int vtscsi_execute_reset_dev_cmd(struct vtscsi_softc *,
struct vtscsi_request *);
static void vtscsi_get_request_lun(uint8_t [], target_id_t *, lun_id_t *);
static void vtscsi_set_request_lun(struct ccb_hdr *, uint8_t []);
static void vtscsi_init_scsi_cmd_req(struct ccb_scsiio *,
struct virtio_scsi_cmd_req *);
static void vtscsi_init_ctrl_tmf_req(struct ccb_hdr *, uint32_t,
uintptr_t, struct virtio_scsi_ctrl_tmf_req *);
static void vtscsi_freeze_simq(struct vtscsi_softc *, int);
static int vtscsi_thaw_simq(struct vtscsi_softc *, int);
static void vtscsi_announce(struct vtscsi_softc *, uint32_t, target_id_t,
lun_id_t);
static void vtscsi_execute_rescan(struct vtscsi_softc *, target_id_t,
lun_id_t);
static void vtscsi_execute_rescan_bus(struct vtscsi_softc *);
static void vtscsi_handle_event(struct vtscsi_softc *,
struct virtio_scsi_event *);
static int vtscsi_enqueue_event_buf(struct vtscsi_softc *,
struct virtio_scsi_event *);
static int vtscsi_init_event_vq(struct vtscsi_softc *);
static void vtscsi_reinit_event_vq(struct vtscsi_softc *);
static void vtscsi_drain_event_vq(struct vtscsi_softc *);
static void vtscsi_complete_vqs_locked(struct vtscsi_softc *);
static void vtscsi_complete_vqs(struct vtscsi_softc *);
static void vtscsi_drain_vqs(struct vtscsi_softc *);
static void vtscsi_cancel_request(struct vtscsi_softc *,
struct vtscsi_request *);
static void vtscsi_drain_vq(struct vtscsi_softc *, struct virtqueue *);
static void vtscsi_stop(struct vtscsi_softc *);
static int vtscsi_reset_bus(struct vtscsi_softc *);
static void vtscsi_init_request(struct vtscsi_softc *,
struct vtscsi_request *);
static int vtscsi_alloc_requests(struct vtscsi_softc *);
static void vtscsi_free_requests(struct vtscsi_softc *);
static void vtscsi_enqueue_request(struct vtscsi_softc *,
struct vtscsi_request *);
static struct vtscsi_request * vtscsi_dequeue_request(struct vtscsi_softc *);
static void vtscsi_complete_request(struct vtscsi_request *);
static void vtscsi_complete_vq(struct vtscsi_softc *, struct virtqueue *);
static void vtscsi_control_vq_intr(void *);
static void vtscsi_event_vq_intr(void *);
static void vtscsi_request_vq_intr(void *);
static void vtscsi_disable_vqs_intr(struct vtscsi_softc *);
static void vtscsi_enable_vqs_intr(struct vtscsi_softc *);
static void vtscsi_get_tunables(struct vtscsi_softc *);
static void vtscsi_add_sysctl(struct vtscsi_softc *);
static void vtscsi_printf_req(struct vtscsi_request *, const char *,
const char *, ...);
/* Global tunables. */
/*
* The current QEMU VirtIO SCSI implementation does not cancel in-flight
* IO during virtio_stop(). So in-flight requests still complete after the
* device reset. We would have to wait for all the in-flight IO to complete,
* which defeats the typical purpose of a bus reset. We could simulate the
* bus reset with either I_T_NEXUS_RESET of all the targets, or with
* LOGICAL_UNIT_RESET of all the LUNs (assuming there is space in the
* control virtqueue). But this isn't very useful if things really go off
* the rails, so default to disabled for now.
*/
static int vtscsi_bus_reset_disable = 1;
TUNABLE_INT("hw.vtscsi.bus_reset_disable", &vtscsi_bus_reset_disable);
static struct virtio_feature_desc vtscsi_feature_desc[] = {
{ VIRTIO_SCSI_F_INOUT, "InOut" },
{ VIRTIO_SCSI_F_HOTPLUG, "Hotplug" },
{ 0, NULL }
};
static device_method_t vtscsi_methods[] = {
/* Device methods. */
DEVMETHOD(device_probe, vtscsi_probe),
DEVMETHOD(device_attach, vtscsi_attach),
DEVMETHOD(device_detach, vtscsi_detach),
DEVMETHOD(device_suspend, vtscsi_suspend),
DEVMETHOD(device_resume, vtscsi_resume),
DEVMETHOD_END
};
static driver_t vtscsi_driver = {
"vtscsi",
vtscsi_methods,
sizeof(struct vtscsi_softc)
};
static devclass_t vtscsi_devclass;
DRIVER_MODULE(virtio_scsi, virtio_pci, vtscsi_driver, vtscsi_devclass,
vtscsi_modevent, 0);
MODULE_VERSION(virtio_scsi, 1);
MODULE_DEPEND(virtio_scsi, virtio, 1, 1, 1);
MODULE_DEPEND(virtio_scsi, cam, 1, 1, 1);
static int
vtscsi_modevent(module_t mod, int type, void *unused)
{
int error;
switch (type) {
case MOD_LOAD:
case MOD_QUIESCE:
case MOD_UNLOAD:
case MOD_SHUTDOWN:
error = 0;
break;
default:
error = EOPNOTSUPP;
break;
}
return (error);
}
static int
vtscsi_probe(device_t dev)
{
if (virtio_get_device_type(dev) != VIRTIO_ID_SCSI)
return (ENXIO);
device_set_desc(dev, "VirtIO SCSI Adapter");
return (BUS_PROBE_DEFAULT);
}
static int
vtscsi_attach(device_t dev)
{
struct vtscsi_softc *sc;
struct virtio_scsi_config scsicfg;
int error;
sc = device_get_softc(dev);
sc->vtscsi_dev = dev;
VTSCSI_LOCK_INIT(sc, device_get_nameunit(dev));
TAILQ_INIT(&sc->vtscsi_req_free);
vtscsi_get_tunables(sc);
vtscsi_add_sysctl(sc);
virtio_set_feature_desc(dev, vtscsi_feature_desc);
vtscsi_negotiate_features(sc);
if (virtio_with_feature(dev, VIRTIO_RING_F_INDIRECT_DESC))
sc->vtscsi_flags |= VTSCSI_FLAG_INDIRECT;
if (virtio_with_feature(dev, VIRTIO_SCSI_F_INOUT))
sc->vtscsi_flags |= VTSCSI_FLAG_BIDIRECTIONAL;
if (virtio_with_feature(dev, VIRTIO_SCSI_F_HOTPLUG))
sc->vtscsi_flags |= VTSCSI_FLAG_HOTPLUG;
virtio_read_device_config(dev, 0, &scsicfg,
sizeof(struct virtio_scsi_config));
sc->vtscsi_max_channel = scsicfg.max_channel;
sc->vtscsi_max_target = scsicfg.max_target;
sc->vtscsi_max_lun = scsicfg.max_lun;
sc->vtscsi_event_buf_size = scsicfg.event_info_size;
vtscsi_write_device_config(sc);
sc->vtscsi_max_nsegs = vtscsi_maximum_segments(sc, scsicfg.seg_max);
sc->vtscsi_sglist = sglist_alloc(sc->vtscsi_max_nsegs, M_NOWAIT);
if (sc->vtscsi_sglist == NULL) {
error = ENOMEM;
device_printf(dev, "cannot allocate sglist\n");
goto fail;
}
error = vtscsi_alloc_virtqueues(sc);
if (error) {
device_printf(dev, "cannot allocate virtqueues\n");
goto fail;
}
error = vtscsi_init_event_vq(sc);
if (error) {
device_printf(dev, "cannot populate the eventvq\n");
goto fail;
}
error = vtscsi_alloc_requests(sc);
if (error) {
device_printf(dev, "cannot allocate requests\n");
goto fail;
}
error = vtscsi_alloc_cam(sc);
if (error) {
device_printf(dev, "cannot allocate CAM structures\n");
goto fail;
}
error = virtio_setup_intr(dev, INTR_TYPE_CAM);
if (error) {
device_printf(dev, "cannot setup virtqueue interrupts\n");
goto fail;
}
vtscsi_enable_vqs_intr(sc);
/*
* Register with CAM after interrupts are enabled so we will get
* notified of the probe responses.
*/
error = vtscsi_register_cam(sc);
if (error) {
device_printf(dev, "cannot register with CAM\n");
goto fail;
}
fail:
if (error)
vtscsi_detach(dev);
return (error);
}
static int
vtscsi_detach(device_t dev)
{
struct vtscsi_softc *sc;
sc = device_get_softc(dev);
VTSCSI_LOCK(sc);
sc->vtscsi_flags |= VTSCSI_FLAG_DETACH;
if (device_is_attached(dev))
vtscsi_stop(sc);
VTSCSI_UNLOCK(sc);
vtscsi_complete_vqs(sc);
vtscsi_drain_vqs(sc);
vtscsi_free_cam(sc);
vtscsi_free_requests(sc);
if (sc->vtscsi_sglist != NULL) {
sglist_free(sc->vtscsi_sglist);
sc->vtscsi_sglist = NULL;
}
VTSCSI_LOCK_DESTROY(sc);
return (0);
}
static int
vtscsi_suspend(device_t dev)
{
return (0);
}
static int
vtscsi_resume(device_t dev)
{
return (0);
}
static void
vtscsi_negotiate_features(struct vtscsi_softc *sc)
{
device_t dev;
uint64_t features;
dev = sc->vtscsi_dev;
features = virtio_negotiate_features(dev, VTSCSI_FEATURES);
sc->vtscsi_features = features;
}
static int
vtscsi_maximum_segments(struct vtscsi_softc *sc, int seg_max)
{
int nsegs;
nsegs = VTSCSI_MIN_SEGMENTS;
if (seg_max > 0) {
nsegs += MIN(seg_max, MAXPHYS / PAGE_SIZE + 1);
if (sc->vtscsi_flags & VTSCSI_FLAG_INDIRECT)
nsegs = MIN(nsegs, VIRTIO_MAX_INDIRECT);
} else
nsegs += 1;
return (nsegs);
}
static int
vtscsi_alloc_virtqueues(struct vtscsi_softc *sc)
{
device_t dev;
struct vq_alloc_info vq_info[3];
int nvqs;
dev = sc->vtscsi_dev;
nvqs = 3;
VQ_ALLOC_INFO_INIT(&vq_info[0], 0, vtscsi_control_vq_intr, sc,
&sc->vtscsi_control_vq, "%s control", device_get_nameunit(dev));
VQ_ALLOC_INFO_INIT(&vq_info[1], 0, vtscsi_event_vq_intr, sc,
&sc->vtscsi_event_vq, "%s event", device_get_nameunit(dev));
VQ_ALLOC_INFO_INIT(&vq_info[2], sc->vtscsi_max_nsegs,
vtscsi_request_vq_intr, sc, &sc->vtscsi_request_vq,
"%s request", device_get_nameunit(dev));
return (virtio_alloc_virtqueues(dev, 0, nvqs, vq_info));
}
static void
vtscsi_write_device_config(struct vtscsi_softc *sc)
{
virtio_write_dev_config_4(sc->vtscsi_dev,
offsetof(struct virtio_scsi_config, sense_size),
VIRTIO_SCSI_SENSE_SIZE);
/*
* This is the size in the virtio_scsi_cmd_req structure. Note
* this value (32) is larger than the maximum CAM CDB size (16).
*/
virtio_write_dev_config_4(sc->vtscsi_dev,
offsetof(struct virtio_scsi_config, cdb_size),
VIRTIO_SCSI_CDB_SIZE);
}
static int
vtscsi_reinit(struct vtscsi_softc *sc)
{
device_t dev;
int error;
dev = sc->vtscsi_dev;
error = virtio_reinit(dev, sc->vtscsi_features);
if (error == 0) {
vtscsi_write_device_config(sc);
vtscsi_reinit_event_vq(sc);
virtio_reinit_complete(dev);
vtscsi_enable_vqs_intr(sc);
}
vtscsi_dprintf(sc, VTSCSI_TRACE, "error=%d\n", error);
return (error);
}
static int
vtscsi_alloc_cam(struct vtscsi_softc *sc)
{
device_t dev;
struct cam_devq *devq;
int openings;
dev = sc->vtscsi_dev;
openings = sc->vtscsi_nrequests - VTSCSI_RESERVED_REQUESTS;
devq = cam_simq_alloc(openings);
if (devq == NULL) {
device_printf(dev, "cannot allocate SIM queue\n");
return (ENOMEM);
}
sc->vtscsi_sim = cam_sim_alloc(vtscsi_cam_action, vtscsi_cam_poll,
"vtscsi", sc, device_get_unit(dev), VTSCSI_MTX(sc), 1,
openings, devq);
if (sc->vtscsi_sim == NULL) {
cam_simq_free(devq);
device_printf(dev, "cannot allocate SIM\n");
return (ENOMEM);
}
return (0);
}
static int
vtscsi_register_cam(struct vtscsi_softc *sc)
{
device_t dev;
int registered, error;
dev = sc->vtscsi_dev;
registered = 0;
VTSCSI_LOCK(sc);
if (xpt_bus_register(sc->vtscsi_sim, dev, 0) != CAM_SUCCESS) {
error = ENOMEM;
device_printf(dev, "cannot register XPT bus\n");
goto fail;
}
registered = 1;
if (xpt_create_path(&sc->vtscsi_path, NULL,
cam_sim_path(sc->vtscsi_sim), CAM_TARGET_WILDCARD,
CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
error = ENOMEM;
device_printf(dev, "cannot create bus path\n");
goto fail;
}
VTSCSI_UNLOCK(sc);
/*
* The async register apparently needs to be done without
* the lock held, otherwise it can recurse on the lock.
*/
if (vtscsi_register_async(sc) != CAM_REQ_CMP) {
error = EIO;
device_printf(dev, "cannot register async callback\n");
VTSCSI_LOCK(sc);
goto fail;
}
return (0);
fail:
if (sc->vtscsi_path != NULL) {
xpt_free_path(sc->vtscsi_path);
sc->vtscsi_path = NULL;
}
if (registered != 0)
xpt_bus_deregister(cam_sim_path(sc->vtscsi_sim));
VTSCSI_UNLOCK(sc);
return (error);
}
static void
vtscsi_free_cam(struct vtscsi_softc *sc)
{
VTSCSI_LOCK(sc);
if (sc->vtscsi_path != NULL) {
vtscsi_deregister_async(sc);
xpt_free_path(sc->vtscsi_path);
sc->vtscsi_path = NULL;
xpt_bus_deregister(cam_sim_path(sc->vtscsi_sim));
}
if (sc->vtscsi_sim != NULL) {
cam_sim_free(sc->vtscsi_sim, 1);
sc->vtscsi_sim = NULL;
}
VTSCSI_UNLOCK(sc);
}
static void
vtscsi_cam_async(void *cb_arg, uint32_t code, struct cam_path *path, void *arg)
{
struct cam_sim *sim;
struct vtscsi_softc *sc;
sim = cb_arg;
sc = cam_sim_softc(sim);
vtscsi_dprintf(sc, VTSCSI_TRACE, "code=%u\n", code);
/*
* TODO Once QEMU supports event reporting, we should
* (un)subscribe to events here.
*/
switch (code) {
case AC_FOUND_DEVICE:
break;
case AC_LOST_DEVICE:
break;
}
}
static int
vtscsi_register_async(struct vtscsi_softc *sc)
{
struct ccb_setasync csa;
VTSCSI_LOCK_NOTOWNED(sc);
xpt_setup_ccb(&csa.ccb_h, sc->vtscsi_path, 5);
csa.ccb_h.func_code = XPT_SASYNC_CB;
csa.event_enable = AC_LOST_DEVICE | AC_FOUND_DEVICE;
csa.callback = vtscsi_cam_async;
csa.callback_arg = sc->vtscsi_sim;
xpt_action((union ccb *) &csa);
return (csa.ccb_h.status);
}
static void
vtscsi_deregister_async(struct vtscsi_softc *sc)
{
struct ccb_setasync csa;
xpt_setup_ccb(&csa.ccb_h, sc->vtscsi_path, 5);
csa.ccb_h.func_code = XPT_SASYNC_CB;
csa.event_enable = 0;
csa.callback = vtscsi_cam_async;
csa.callback_arg = sc->vtscsi_sim;
xpt_action((union ccb *) &csa);
}
static void
vtscsi_cam_action(struct cam_sim *sim, union ccb *ccb)
{
struct vtscsi_softc *sc;
struct ccb_hdr *ccbh;
sc = cam_sim_softc(sim);
ccbh = &ccb->ccb_h;
VTSCSI_LOCK_OWNED(sc);
if (sc->vtscsi_flags & VTSCSI_FLAG_DETACH) {
/*
* The VTSCSI_MTX is briefly dropped between setting
* VTSCSI_FLAG_DETACH and deregistering with CAM, so
* drop any CCBs that come in during that window.
*/
ccbh->status = CAM_NO_HBA;
xpt_done(ccb);
return;
}
switch (ccbh->func_code) {
case XPT_SCSI_IO:
vtscsi_cam_scsi_io(sc, sim, ccb);
break;
case XPT_SET_TRAN_SETTINGS:
ccbh->status = CAM_FUNC_NOTAVAIL;
xpt_done(ccb);
break;
case XPT_GET_TRAN_SETTINGS:
vtscsi_cam_get_tran_settings(sc, ccb);
break;
case XPT_RESET_BUS:
vtscsi_cam_reset_bus(sc, ccb);
break;
case XPT_RESET_DEV:
vtscsi_cam_reset_dev(sc, ccb);
break;
case XPT_ABORT:
vtscsi_cam_abort(sc, ccb);
break;
case XPT_CALC_GEOMETRY:
cam_calc_geometry(&ccb->ccg, 1);
xpt_done(ccb);
break;
case XPT_PATH_INQ:
vtscsi_cam_path_inquiry(sc, sim, ccb);
break;
default:
vtscsi_dprintf(sc, VTSCSI_ERROR,
"invalid ccb=%p func=%#x\n", ccb, ccbh->func_code);
ccbh->status = CAM_REQ_INVALID;
xpt_done(ccb);
break;
}
}
static void
vtscsi_cam_poll(struct cam_sim *sim)
{
struct vtscsi_softc *sc;
sc = cam_sim_softc(sim);
vtscsi_complete_vqs_locked(sc);
}
static void
vtscsi_cam_scsi_io(struct vtscsi_softc *sc, struct cam_sim *sim,
union ccb *ccb)
{
struct ccb_hdr *ccbh;
struct ccb_scsiio *csio;
int error;
ccbh = &ccb->ccb_h;
csio = &ccb->csio;
if (csio->cdb_len > VIRTIO_SCSI_CDB_SIZE) {
error = EINVAL;
ccbh->status = CAM_REQ_INVALID;
goto done;
}
if ((ccbh->flags & CAM_DIR_MASK) == CAM_DIR_BOTH &&
(sc->vtscsi_flags & VTSCSI_FLAG_BIDIRECTIONAL) == 0) {
error = EINVAL;
ccbh->status = CAM_REQ_INVALID;
goto done;
}
error = vtscsi_start_scsi_cmd(sc, ccb);
done:
if (error) {
vtscsi_dprintf(sc, VTSCSI_ERROR,
"error=%d ccb=%p status=%#x\n", error, ccb, ccbh->status);
xpt_done(ccb);
}
}
static void
vtscsi_cam_get_tran_settings(struct vtscsi_softc *sc, union ccb *ccb)
{
struct ccb_trans_settings *cts;
struct ccb_trans_settings_scsi *scsi;
cts = &ccb->cts;
scsi = &cts->proto_specific.scsi;
cts->protocol = PROTO_SCSI;
cts->protocol_version = SCSI_REV_SPC3;
cts->transport = XPORT_SAS;
cts->transport_version = 0;
scsi->valid = CTS_SCSI_VALID_TQ;
scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
ccb->ccb_h.status = CAM_REQ_CMP;
xpt_done(ccb);
}
static void
vtscsi_cam_reset_bus(struct vtscsi_softc *sc, union ccb *ccb)
{
int error;
error = vtscsi_reset_bus(sc);
if (error == 0)
ccb->ccb_h.status = CAM_REQ_CMP;
else
ccb->ccb_h.status = CAM_REQ_CMP_ERR;
vtscsi_dprintf(sc, VTSCSI_TRACE, "error=%d ccb=%p status=%#x\n",
error, ccb, ccb->ccb_h.status);
xpt_done(ccb);
}
static void
vtscsi_cam_reset_dev(struct vtscsi_softc *sc, union ccb *ccb)
{
struct ccb_hdr *ccbh;
struct vtscsi_request *req;
int error;
ccbh = &ccb->ccb_h;
req = vtscsi_dequeue_request(sc);
if (req == NULL) {
error = EAGAIN;
vtscsi_freeze_simq(sc, VTSCSI_REQUEST);
goto fail;
}
req->vsr_ccb = ccb;
error = vtscsi_execute_reset_dev_cmd(sc, req);
if (error == 0)
return;
vtscsi_enqueue_request(sc, req);
fail:
vtscsi_dprintf(sc, VTSCSI_ERROR, "error=%d req=%p ccb=%p\n",
error, req, ccb);
if (error == EAGAIN)
ccbh->status = CAM_RESRC_UNAVAIL;
else
ccbh->status = CAM_REQ_CMP_ERR;
xpt_done(ccb);
}
static void
vtscsi_cam_abort(struct vtscsi_softc *sc, union ccb *ccb)
{
struct vtscsi_request *req;
struct ccb_hdr *ccbh;
int error;
ccbh = &ccb->ccb_h;
req = vtscsi_dequeue_request(sc);
if (req == NULL) {
error = EAGAIN;
vtscsi_freeze_simq(sc, VTSCSI_REQUEST);
goto fail;
}
req->vsr_ccb = ccb;
error = vtscsi_execute_abort_task_cmd(sc, req);
if (error == 0)
return;
vtscsi_enqueue_request(sc, req);
fail:
vtscsi_dprintf(sc, VTSCSI_ERROR, "error=%d req=%p ccb=%p\n",
error, req, ccb);
if (error == EAGAIN)
ccbh->status = CAM_RESRC_UNAVAIL;
else
ccbh->status = CAM_REQ_CMP_ERR;
xpt_done(ccb);
}
static void
vtscsi_cam_path_inquiry(struct vtscsi_softc *sc, struct cam_sim *sim,
union ccb *ccb)
{
device_t dev;
struct ccb_pathinq *cpi;
dev = sc->vtscsi_dev;
cpi = &ccb->cpi;
vtscsi_dprintf(sc, VTSCSI_TRACE, "sim=%p ccb=%p\n", sim, ccb);
cpi->version_num = 1;
cpi->hba_inquiry = PI_TAG_ABLE;
cpi->target_sprt = 0;
cpi->hba_misc = PIM_SEQSCAN;
if (vtscsi_bus_reset_disable != 0)
cpi->hba_misc |= PIM_NOBUSRESET;
cpi->hba_eng_cnt = 0;
cpi->max_target = sc->vtscsi_max_target;
cpi->max_lun = sc->vtscsi_max_lun;
cpi->initiator_id = VTSCSI_INITIATOR_ID;
strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
strncpy(cpi->hba_vid, "VirtIO", HBA_IDLEN);
strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
cpi->unit_number = cam_sim_unit(sim);
cpi->bus_id = cam_sim_bus(sim);
cpi->base_transfer_speed = 300000;
cpi->protocol = PROTO_SCSI;
cpi->protocol_version = SCSI_REV_SPC3;
cpi->transport = XPORT_SAS;
cpi->transport_version = 0;
cpi->maxio = (sc->vtscsi_max_nsegs - VTSCSI_MIN_SEGMENTS - 1) *
PAGE_SIZE;
cpi->hba_vendor = virtio_get_vendor(dev);
cpi->hba_device = virtio_get_device(dev);
cpi->hba_subvendor = virtio_get_subvendor(dev);
cpi->hba_subdevice = virtio_get_subdevice(dev);
ccb->ccb_h.status = CAM_REQ_CMP;
xpt_done(ccb);
}
static int
vtscsi_sg_append_scsi_buf(struct vtscsi_softc *sc, struct sglist *sg,
struct ccb_scsiio *csio)
{
struct ccb_hdr *ccbh;
struct bus_dma_segment *dseg;
int i, error;
ccbh = &csio->ccb_h;
error = 0;
switch ((ccbh->flags & CAM_DATA_MASK)) {
case CAM_DATA_VADDR:
error = sglist_append(sg, csio->data_ptr, csio->dxfer_len);
break;
case CAM_DATA_PADDR:
error = sglist_append_phys(sg,
(vm_paddr_t)(vm_offset_t) csio->data_ptr, csio->dxfer_len);
break;
case CAM_DATA_SG:
for (i = 0; i < csio->sglist_cnt && error == 0; i++) {
dseg = &((struct bus_dma_segment *)csio->data_ptr)[i];
error = sglist_append(sg,
(void *)(vm_offset_t) dseg->ds_addr, dseg->ds_len);
}
break;
case CAM_DATA_SG_PADDR:
for (i = 0; i < csio->sglist_cnt && error == 0; i++) {
dseg = &((struct bus_dma_segment *)csio->data_ptr)[i];
error = sglist_append_phys(sg,
(vm_paddr_t) dseg->ds_addr, dseg->ds_len);
}
break;
default:
error = EINVAL;
break;
}
return (error);
}
static int
vtscsi_fill_scsi_cmd_sglist(struct vtscsi_softc *sc, struct vtscsi_request *req,
int *readable, int *writable)
{
struct sglist *sg;
struct ccb_hdr *ccbh;
struct ccb_scsiio *csio;
struct virtio_scsi_cmd_req *cmd_req;
struct virtio_scsi_cmd_resp *cmd_resp;
int error;
sg = sc->vtscsi_sglist;
csio = &req->vsr_ccb->csio;
ccbh = &csio->ccb_h;
cmd_req = &req->vsr_cmd_req;
cmd_resp = &req->vsr_cmd_resp;
sglist_reset(sg);
sglist_append(sg, cmd_req, sizeof(struct virtio_scsi_cmd_req));
if ((ccbh->flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
error = vtscsi_sg_append_scsi_buf(sc, sg, csio);
/* At least one segment must be left for the response. */
if (error || sg->sg_nseg == sg->sg_maxseg)
goto fail;
}
*readable = sg->sg_nseg;
sglist_append(sg, cmd_resp, sizeof(struct virtio_scsi_cmd_resp));
if ((ccbh->flags & CAM_DIR_MASK) == CAM_DIR_IN) {
error = vtscsi_sg_append_scsi_buf(sc, sg, csio);
if (error)
goto fail;
}
*writable = sg->sg_nseg - *readable;
vtscsi_dprintf(sc, VTSCSI_TRACE, "req=%p ccb=%p readable=%d "
"writable=%d\n", req, ccbh, *readable, *writable);
return (0);
fail:
/*
* This should never happen unless maxio was incorrectly set.
*/
vtscsi_set_ccb_status(ccbh, CAM_REQ_TOO_BIG, 0);
vtscsi_dprintf(sc, VTSCSI_ERROR, "error=%d req=%p ccb=%p "
"nseg=%d maxseg=%d\n",
error, req, ccbh, sg->sg_nseg, sg->sg_maxseg);
return (EFBIG);
}
static int
vtscsi_execute_scsi_cmd(struct vtscsi_softc *sc, struct vtscsi_request *req)
{
struct sglist *sg;
struct virtqueue *vq;
struct ccb_scsiio *csio;
struct ccb_hdr *ccbh;
struct virtio_scsi_cmd_req *cmd_req;
struct virtio_scsi_cmd_resp *cmd_resp;
int readable, writable, error;
sg = sc->vtscsi_sglist;
vq = sc->vtscsi_request_vq;
csio = &req->vsr_ccb->csio;
ccbh = &csio->ccb_h;
cmd_req = &req->vsr_cmd_req;
cmd_resp = &req->vsr_cmd_resp;
vtscsi_init_scsi_cmd_req(csio, cmd_req);
error = vtscsi_fill_scsi_cmd_sglist(sc, req, &readable, &writable);
if (error)
return (error);
req->vsr_complete = vtscsi_complete_scsi_cmd;
cmd_resp->response = -1;
error = virtqueue_enqueue(vq, req, sg, readable, writable);
if (error) {
vtscsi_dprintf(sc, VTSCSI_ERROR,
"enqueue error=%d req=%p ccb=%p\n", error, req, ccbh);
ccbh->status = CAM_REQUEUE_REQ;
vtscsi_freeze_simq(sc, VTSCSI_REQUEST_VQ);
return (error);
}
ccbh->status |= CAM_SIM_QUEUED;
ccbh->ccbh_vtscsi_req = req;
virtqueue_notify(vq);
if (ccbh->timeout != CAM_TIME_INFINITY) {
req->vsr_flags |= VTSCSI_REQ_FLAG_TIMEOUT_SET;
callout_reset(&req->vsr_callout, ccbh->timeout * hz / 1000,
vtscsi_timedout_scsi_cmd, req);
}
vtscsi_dprintf_req(req, VTSCSI_TRACE, "enqueued req=%p ccb=%p\n",
req, ccbh);
return (0);
}
static int
vtscsi_start_scsi_cmd(struct vtscsi_softc *sc, union ccb *ccb)
{
struct vtscsi_request *req;
int error;
req = vtscsi_dequeue_request(sc);
if (req == NULL) {
ccb->ccb_h.status = CAM_REQUEUE_REQ;
vtscsi_freeze_simq(sc, VTSCSI_REQUEST);
return (ENOBUFS);
}
req->vsr_ccb = ccb;
error = vtscsi_execute_scsi_cmd(sc, req);
if (error)
vtscsi_enqueue_request(sc, req);
return (error);
}
static void
vtscsi_complete_abort_timedout_scsi_cmd(struct vtscsi_softc *sc,
struct vtscsi_request *req)
{
struct virtio_scsi_ctrl_tmf_resp *tmf_resp;
struct vtscsi_request *to_req;
uint8_t response;
tmf_resp = &req->vsr_tmf_resp;
response = tmf_resp->response;
to_req = req->vsr_timedout_req;
vtscsi_dprintf(sc, VTSCSI_TRACE, "req=%p to_req=%p response=%d\n",
req, to_req, response);
vtscsi_enqueue_request(sc, req);
/*
* The timedout request could have completed between when the
* abort task was sent and when the host processed it.
*/
if (to_req->vsr_state != VTSCSI_REQ_STATE_TIMEDOUT)
return;
/* The timedout request was successfully aborted. */
if (response == VIRTIO_SCSI_S_FUNCTION_COMPLETE)
return;
/* Don't bother if the device is going away. */
if (sc->vtscsi_flags & VTSCSI_FLAG_DETACH)
return;
/* The timedout request will be aborted by the reset. */
if (sc->vtscsi_flags & VTSCSI_FLAG_RESET)
return;
vtscsi_reset_bus(sc);
}
static int
vtscsi_abort_timedout_scsi_cmd(struct vtscsi_softc *sc,
struct vtscsi_request *to_req)
{
struct sglist *sg;
struct ccb_hdr *to_ccbh;
struct vtscsi_request *req;
struct virtio_scsi_ctrl_tmf_req *tmf_req;
struct virtio_scsi_ctrl_tmf_resp *tmf_resp;
int error;
sg = sc->vtscsi_sglist;
to_ccbh = &to_req->vsr_ccb->ccb_h;
req = vtscsi_dequeue_request(sc);
if (req == NULL) {
error = ENOBUFS;
goto fail;
}
tmf_req = &req->vsr_tmf_req;
tmf_resp = &req->vsr_tmf_resp;
vtscsi_init_ctrl_tmf_req(to_ccbh, VIRTIO_SCSI_T_TMF_ABORT_TASK,
(uintptr_t) to_ccbh, tmf_req);
sglist_reset(sg);
sglist_append(sg, tmf_req, sizeof(struct virtio_scsi_ctrl_tmf_req));
sglist_append(sg, tmf_resp, sizeof(struct virtio_scsi_ctrl_tmf_resp));
req->vsr_timedout_req = to_req;
req->vsr_complete = vtscsi_complete_abort_timedout_scsi_cmd;
tmf_resp->response = -1;
error = vtscsi_execute_ctrl_req(sc, req, sg, 1, 1,
VTSCSI_EXECUTE_ASYNC);
if (error == 0)
return (0);
vtscsi_enqueue_request(sc, req);
fail:
vtscsi_dprintf(sc, VTSCSI_ERROR, "error=%d req=%p "
"timedout req=%p ccb=%p\n", error, req, to_req, to_ccbh);
return (error);
}
static void
vtscsi_timedout_scsi_cmd(void *xreq)
{
struct vtscsi_softc *sc;
struct vtscsi_request *to_req;
to_req = xreq;
sc = to_req->vsr_softc;
vtscsi_dprintf(sc, VTSCSI_INFO, "timedout req=%p ccb=%p state=%#x\n",
to_req, to_req->vsr_ccb, to_req->vsr_state);
/* Don't bother if the device is going away. */
if (sc->vtscsi_flags & VTSCSI_FLAG_DETACH)
return;
/*
* Bail if the request is not in use. We likely raced when
* stopping the callout handler or it has already been aborted.
*/
if (to_req->vsr_state != VTSCSI_REQ_STATE_INUSE ||
(to_req->vsr_flags & VTSCSI_REQ_FLAG_TIMEOUT_SET) == 0)
return;
/*
* Complete the request queue in case the timedout request is
* actually just pending.
*/
vtscsi_complete_vq(sc, sc->vtscsi_request_vq);
if (to_req->vsr_state == VTSCSI_REQ_STATE_FREE)
return;
sc->vtscsi_stats.scsi_cmd_timeouts++;
to_req->vsr_state = VTSCSI_REQ_STATE_TIMEDOUT;
if (vtscsi_abort_timedout_scsi_cmd(sc, to_req) == 0)
return;
vtscsi_dprintf(sc, VTSCSI_ERROR, "resetting bus\n");
vtscsi_reset_bus(sc);
}
static cam_status
vtscsi_scsi_cmd_cam_status(struct virtio_scsi_cmd_resp *cmd_resp)
{
cam_status status;
switch (cmd_resp->response) {
case VIRTIO_SCSI_S_OK:
status = CAM_REQ_CMP;
break;
case VIRTIO_SCSI_S_OVERRUN:
status = CAM_DATA_RUN_ERR;
break;
case VIRTIO_SCSI_S_ABORTED:
status = CAM_REQ_ABORTED;
break;
case VIRTIO_SCSI_S_BAD_TARGET:
status = CAM_TID_INVALID;
break;
case VIRTIO_SCSI_S_RESET:
status = CAM_SCSI_BUS_RESET;
break;
case VIRTIO_SCSI_S_BUSY:
status = CAM_SCSI_BUSY;
break;
case VIRTIO_SCSI_S_TRANSPORT_FAILURE:
case VIRTIO_SCSI_S_TARGET_FAILURE:
case VIRTIO_SCSI_S_NEXUS_FAILURE:
status = CAM_SCSI_IT_NEXUS_LOST;
break;
default: /* VIRTIO_SCSI_S_FAILURE */
status = CAM_REQ_CMP_ERR;
break;
}
return (status);
}
static cam_status
vtscsi_complete_scsi_cmd_response(struct vtscsi_softc *sc,
struct ccb_scsiio *csio, struct virtio_scsi_cmd_resp *cmd_resp)
{
cam_status status;
csio->scsi_status = cmd_resp->status;
csio->resid = cmd_resp->resid;
if (csio->scsi_status == SCSI_STATUS_OK)
status = CAM_REQ_CMP;
else
status = CAM_SCSI_STATUS_ERROR;
if (cmd_resp->sense_len > 0) {
status |= CAM_AUTOSNS_VALID;
if (cmd_resp->sense_len < csio->sense_len)
csio->sense_resid = csio->sense_len -
cmd_resp->sense_len;
else
csio->sense_resid = 0;
bzero(&csio->sense_data, sizeof(csio->sense_data));
memcpy(cmd_resp->sense, &csio->sense_data,
csio->sense_len - csio->sense_resid);
}
vtscsi_dprintf(sc, status == CAM_REQ_CMP ? VTSCSI_TRACE : VTSCSI_ERROR,
"ccb=%p scsi_status=%#x resid=%u sense_resid=%u\n",
csio, csio->scsi_status, csio->resid, csio->sense_resid);
return (status);
}
static void
vtscsi_complete_scsi_cmd(struct vtscsi_softc *sc, struct vtscsi_request *req)
{
struct ccb_hdr *ccbh;
struct ccb_scsiio *csio;
struct virtio_scsi_cmd_resp *cmd_resp;
cam_status status;
csio = &req->vsr_ccb->csio;
ccbh = &csio->ccb_h;
cmd_resp = &req->vsr_cmd_resp;
KASSERT(ccbh->ccbh_vtscsi_req == req,
("ccb %p req mismatch %p/%p", ccbh, ccbh->ccbh_vtscsi_req, req));
if (req->vsr_flags & VTSCSI_REQ_FLAG_TIMEOUT_SET)
callout_stop(&req->vsr_callout);
status = vtscsi_scsi_cmd_cam_status(cmd_resp);
if (status == CAM_REQ_ABORTED) {
if (req->vsr_state == VTSCSI_REQ_STATE_TIMEDOUT)
status = CAM_CMD_TIMEOUT;
} else if (status == CAM_REQ_CMP)
status = vtscsi_complete_scsi_cmd_response(sc, csio, cmd_resp);
if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
status |= CAM_DEV_QFRZN;
xpt_freeze_devq(ccbh->path, 1);
}
if (vtscsi_thaw_simq(sc, VTSCSI_REQUEST | VTSCSI_REQUEST_VQ) != 0)
status |= CAM_RELEASE_SIMQ;
vtscsi_dprintf(sc, VTSCSI_TRACE, "req=%p ccb=%p status=%#x\n",
req, ccbh, status);
ccbh->status = status;
xpt_done(req->vsr_ccb);
vtscsi_enqueue_request(sc, req);
}
static void
vtscsi_poll_ctrl_req(struct vtscsi_softc *sc, struct vtscsi_request *req)
{
/* XXX We probably shouldn't poll forever. */
req->vsr_flags |= VTSCSI_REQ_FLAG_POLLED;
do
vtscsi_complete_vq(sc, sc->vtscsi_control_vq);
while ((req->vsr_flags & VTSCSI_REQ_FLAG_COMPLETE) == 0);
req->vsr_flags &= ~VTSCSI_REQ_FLAG_POLLED;
}
static int
vtscsi_execute_ctrl_req(struct vtscsi_softc *sc, struct vtscsi_request *req,
struct sglist *sg, int readable, int writable, int flag)
{
struct virtqueue *vq;
int error;
vq = sc->vtscsi_control_vq;
MPASS(flag == VTSCSI_EXECUTE_POLL || req->vsr_complete != NULL);
error = virtqueue_enqueue(vq, req, sg, readable, writable);
if (error) {
/*
* Return EAGAIN when the virtqueue does not have enough
* descriptors available.
*/
if (error == ENOSPC || error == EMSGSIZE)
error = EAGAIN;
return (error);
}
virtqueue_notify(vq);
if (flag == VTSCSI_EXECUTE_POLL)
vtscsi_poll_ctrl_req(sc, req);
return (0);
}
static void
vtscsi_complete_abort_task_cmd(struct vtscsi_softc *sc,
struct vtscsi_request *req)
{
union ccb *ccb;
struct ccb_hdr *ccbh;
struct virtio_scsi_ctrl_tmf_resp *tmf_resp;
ccb = req->vsr_ccb;
ccbh = &ccb->ccb_h;
tmf_resp = &req->vsr_tmf_resp;
switch (tmf_resp->response) {
case VIRTIO_SCSI_S_FUNCTION_COMPLETE:
ccbh->status = CAM_REQ_CMP;
break;
case VIRTIO_SCSI_S_FUNCTION_REJECTED:
ccbh->status = CAM_UA_ABORT;
break;
default:
ccbh->status = CAM_REQ_CMP_ERR;
break;
}
xpt_done(ccb);
vtscsi_enqueue_request(sc, req);
}
static int
vtscsi_execute_abort_task_cmd(struct vtscsi_softc *sc,
struct vtscsi_request *req)
{
struct sglist *sg;
struct ccb_abort *cab;
struct ccb_hdr *ccbh;
struct ccb_hdr *abort_ccbh;
struct vtscsi_request *abort_req;
struct virtio_scsi_ctrl_tmf_req *tmf_req;
struct virtio_scsi_ctrl_tmf_resp *tmf_resp;
int error;
sg = sc->vtscsi_sglist;
cab = &req->vsr_ccb->cab;
ccbh = &cab->ccb_h;
tmf_req = &req->vsr_tmf_req;
tmf_resp = &req->vsr_tmf_resp;
/* CCB header and request that's to be aborted. */
abort_ccbh = &cab->abort_ccb->ccb_h;
abort_req = abort_ccbh->ccbh_vtscsi_req;
if (abort_ccbh->func_code != XPT_SCSI_IO || abort_req == NULL) {
error = EINVAL;
goto fail;
}
/* Only attempt to abort requests that could be in-flight. */
if (abort_req->vsr_state != VTSCSI_REQ_STATE_INUSE) {
error = EALREADY;
goto fail;
}
abort_req->vsr_state = VTSCSI_REQ_STATE_ABORTED;
if (abort_req->vsr_flags & VTSCSI_REQ_FLAG_TIMEOUT_SET)
callout_stop(&abort_req->vsr_callout);
vtscsi_init_ctrl_tmf_req(ccbh, VIRTIO_SCSI_T_TMF_ABORT_TASK,
(uintptr_t) abort_ccbh, tmf_req);
sglist_reset(sg);
sglist_append(sg, tmf_req, sizeof(struct virtio_scsi_ctrl_tmf_req));
sglist_append(sg, tmf_resp, sizeof(struct virtio_scsi_ctrl_tmf_resp));
req->vsr_complete = vtscsi_complete_abort_task_cmd;
tmf_resp->response = -1;
error = vtscsi_execute_ctrl_req(sc, req, sg, 1, 1,
VTSCSI_EXECUTE_ASYNC);
fail:
vtscsi_dprintf(sc, VTSCSI_TRACE, "error=%d req=%p abort_ccb=%p "
"abort_req=%p\n", error, req, abort_ccbh, abort_req);
return (error);
}
static void
vtscsi_complete_reset_dev_cmd(struct vtscsi_softc *sc,
struct vtscsi_request *req)
{
union ccb *ccb;
struct ccb_hdr *ccbh;
struct virtio_scsi_ctrl_tmf_resp *tmf_resp;
ccb = req->vsr_ccb;
ccbh = &ccb->ccb_h;
tmf_resp = &req->vsr_tmf_resp;
vtscsi_dprintf(sc, VTSCSI_TRACE, "req=%p ccb=%p response=%d\n",
req, ccb, tmf_resp->response);
if (tmf_resp->response == VIRTIO_SCSI_S_FUNCTION_COMPLETE) {
ccbh->status = CAM_REQ_CMP;
vtscsi_announce(sc, AC_SENT_BDR, ccbh->target_id,
ccbh->target_lun);
} else
ccbh->status = CAM_REQ_CMP_ERR;
xpt_done(ccb);
vtscsi_enqueue_request(sc, req);
}
static int
vtscsi_execute_reset_dev_cmd(struct vtscsi_softc *sc,
struct vtscsi_request *req)
{
struct sglist *sg;
struct ccb_resetdev *crd;
struct ccb_hdr *ccbh;
struct virtio_scsi_ctrl_tmf_req *tmf_req;
struct virtio_scsi_ctrl_tmf_resp *tmf_resp;
uint32_t subtype;
int error;
sg = sc->vtscsi_sglist;
crd = &req->vsr_ccb->crd;
ccbh = &crd->ccb_h;
tmf_req = &req->vsr_tmf_req;
tmf_resp = &req->vsr_tmf_resp;
if (ccbh->target_lun == CAM_LUN_WILDCARD)
subtype = VIRTIO_SCSI_T_TMF_I_T_NEXUS_RESET;
else
subtype = VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET;
vtscsi_init_ctrl_tmf_req(ccbh, subtype, 0, tmf_req);
sglist_reset(sg);
sglist_append(sg, tmf_req, sizeof(struct virtio_scsi_ctrl_tmf_req));
sglist_append(sg, tmf_resp, sizeof(struct virtio_scsi_ctrl_tmf_resp));
req->vsr_complete = vtscsi_complete_reset_dev_cmd;
tmf_resp->response = -1;
error = vtscsi_execute_ctrl_req(sc, req, sg, 1, 1,
VTSCSI_EXECUTE_ASYNC);
vtscsi_dprintf(sc, VTSCSI_TRACE, "error=%d req=%p ccb=%p\n",
error, req, ccbh);
return (error);
}
static void
vtscsi_get_request_lun(uint8_t lun[], target_id_t *target_id, lun_id_t *lun_id)
{
*target_id = lun[1];
*lun_id = (lun[2] << 8) | lun[3];
}
static void
vtscsi_set_request_lun(struct ccb_hdr *ccbh, uint8_t lun[])
{
lun[0] = 1;
lun[1] = ccbh->target_id;
lun[2] = 0x40 | ((ccbh->target_lun >> 8) & 0x3F);
lun[3] = (ccbh->target_lun >> 8) & 0xFF;
}
static void
vtscsi_init_scsi_cmd_req(struct ccb_scsiio *csio,
struct virtio_scsi_cmd_req *cmd_req)
{
uint8_t attr;
switch (csio->tag_action) {
case MSG_HEAD_OF_Q_TAG:
attr = VIRTIO_SCSI_S_HEAD;
break;
case MSG_ORDERED_Q_TAG:
attr = VIRTIO_SCSI_S_ORDERED;
break;
case MSG_ACA_TASK:
attr = VIRTIO_SCSI_S_ACA;
break;
default: /* MSG_SIMPLE_Q_TAG */
attr = VIRTIO_SCSI_S_SIMPLE;
break;
}
vtscsi_set_request_lun(&csio->ccb_h, cmd_req->lun);
cmd_req->tag = (uintptr_t) csio;
cmd_req->task_attr = attr;
memcpy(cmd_req->cdb,
csio->ccb_h.flags & CAM_CDB_POINTER ?
csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes,
csio->cdb_len);
}
static void
vtscsi_init_ctrl_tmf_req(struct ccb_hdr *ccbh, uint32_t subtype,
uintptr_t tag, struct virtio_scsi_ctrl_tmf_req *tmf_req)
{
vtscsi_set_request_lun(ccbh, tmf_req->lun);
tmf_req->type = VIRTIO_SCSI_T_TMF;
tmf_req->subtype = subtype;
tmf_req->tag = tag;
}
static void
vtscsi_freeze_simq(struct vtscsi_softc *sc, int reason)
{
int frozen;
frozen = sc->vtscsi_frozen;
if (reason & VTSCSI_REQUEST &&
(sc->vtscsi_frozen & VTSCSI_FROZEN_NO_REQUESTS) == 0)
sc->vtscsi_frozen |= VTSCSI_FROZEN_NO_REQUESTS;
if (reason & VTSCSI_REQUEST_VQ &&
(sc->vtscsi_frozen & VTSCSI_FROZEN_REQUEST_VQ_FULL) == 0)
sc->vtscsi_frozen |= VTSCSI_FROZEN_REQUEST_VQ_FULL;
/* Freeze the SIMQ if transitioned to frozen. */
if (frozen == 0 && sc->vtscsi_frozen != 0) {
vtscsi_dprintf(sc, VTSCSI_INFO, "SIMQ frozen\n");
xpt_freeze_simq(sc->vtscsi_sim, 1);
}
}
static int
vtscsi_thaw_simq(struct vtscsi_softc *sc, int reason)
{
int thawed;
if (sc->vtscsi_frozen == 0 || reason == 0)
return (0);
if (reason & VTSCSI_REQUEST &&
sc->vtscsi_frozen & VTSCSI_FROZEN_NO_REQUESTS)
sc->vtscsi_frozen &= ~VTSCSI_FROZEN_NO_REQUESTS;
if (reason & VTSCSI_REQUEST_VQ &&
sc->vtscsi_frozen & VTSCSI_FROZEN_REQUEST_VQ_FULL)
sc->vtscsi_frozen &= ~VTSCSI_FROZEN_REQUEST_VQ_FULL;
thawed = sc->vtscsi_frozen == 0;
if (thawed != 0)
vtscsi_dprintf(sc, VTSCSI_INFO, "SIMQ thawed\n");
return (thawed);
}
static void
vtscsi_announce(struct vtscsi_softc *sc, uint32_t ac_code,
target_id_t target_id, lun_id_t lun_id)
{
struct cam_path *path;
/* Use the wildcard path from our softc for bus announcements. */
if (target_id == CAM_TARGET_WILDCARD && lun_id == CAM_LUN_WILDCARD) {
xpt_async(ac_code, sc->vtscsi_path, NULL);
return;
}
if (xpt_create_path(&path, NULL, cam_sim_path(sc->vtscsi_sim),
target_id, lun_id) != CAM_REQ_CMP) {
vtscsi_dprintf(sc, VTSCSI_ERROR, "cannot create path\n");
return;
}
xpt_async(ac_code, path, NULL);
xpt_free_path(path);
}
static void
vtscsi_execute_rescan(struct vtscsi_softc *sc, target_id_t target_id,
lun_id_t lun_id)
{
union ccb *ccb;
cam_status status;
ccb = xpt_alloc_ccb_nowait();
if (ccb == NULL) {
vtscsi_dprintf(sc, VTSCSI_ERROR, "cannot allocate CCB\n");
return;
}
status = xpt_create_path(&ccb->ccb_h.path, NULL,
cam_sim_path(sc->vtscsi_sim), target_id, lun_id);
if (status != CAM_REQ_CMP) {
xpt_free_ccb(ccb);
return;
}
xpt_rescan(ccb);
}
static void
vtscsi_execute_rescan_bus(struct vtscsi_softc *sc)
{
vtscsi_execute_rescan(sc, CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
}
static void
vtscsi_transport_reset_event(struct vtscsi_softc *sc,
struct virtio_scsi_event *event)
{
target_id_t target_id;
lun_id_t lun_id;
vtscsi_get_request_lun(event->lun, &target_id, &lun_id);
switch (event->reason) {
case VIRTIO_SCSI_EVT_RESET_RESCAN:
case VIRTIO_SCSI_EVT_RESET_REMOVED:
vtscsi_execute_rescan(sc, target_id, lun_id);
break;
default:
device_printf(sc->vtscsi_dev,
"unhandled transport event reason: %d\n", event->reason);
break;
}
}
static void
vtscsi_handle_event(struct vtscsi_softc *sc, struct virtio_scsi_event *event)
{
int error;
if ((event->event & VIRTIO_SCSI_T_EVENTS_MISSED) == 0) {
switch (event->event) {
case VIRTIO_SCSI_T_TRANSPORT_RESET:
vtscsi_transport_reset_event(sc, event);
break;
default:
device_printf(sc->vtscsi_dev,
"unhandled event: %d\n", event->event);
break;
}
} else
vtscsi_execute_rescan_bus(sc);
/*
* This should always be successful since the buffer
* was just dequeued.
*/
error = vtscsi_enqueue_event_buf(sc, event);
KASSERT(error == 0,
("cannot requeue event buffer: %d", error));
}
static int
vtscsi_enqueue_event_buf(struct vtscsi_softc *sc,
struct virtio_scsi_event *event)
{
struct sglist *sg;
struct virtqueue *vq;
int size, error;
sg = sc->vtscsi_sglist;
vq = sc->vtscsi_event_vq;
size = sc->vtscsi_event_buf_size;
bzero(event, size);
sglist_reset(sg);
error = sglist_append(sg, event, size);
if (error)
return (error);
error = virtqueue_enqueue(vq, event, sg, 0, sg->sg_nseg);
if (error)
return (error);
virtqueue_notify(vq);
return (0);
}
static int
vtscsi_init_event_vq(struct vtscsi_softc *sc)
{
struct virtio_scsi_event *event;
int i, size, error;
/*
* The first release of QEMU with VirtIO SCSI support would crash
* when attempting to notify the event virtqueue. This was fixed
* when hotplug support was added.
*/
if (sc->vtscsi_flags & VTSCSI_FLAG_HOTPLUG)
size = sc->vtscsi_event_buf_size;
else
size = 0;
if (size < sizeof(struct virtio_scsi_event))
return (0);
for (i = 0; i < VTSCSI_NUM_EVENT_BUFS; i++) {
event = &sc->vtscsi_event_bufs[i];
error = vtscsi_enqueue_event_buf(sc, event);
if (error)
break;
}
/*
* Even just one buffer is enough. Missed events are
* denoted with the VIRTIO_SCSI_T_EVENTS_MISSED flag.
*/
if (i > 0)
error = 0;
return (error);
}
static void
vtscsi_reinit_event_vq(struct vtscsi_softc *sc)
{
struct virtio_scsi_event *event;
int i, error;
if ((sc->vtscsi_flags & VTSCSI_FLAG_HOTPLUG) == 0 ||
sc->vtscsi_event_buf_size < sizeof(struct virtio_scsi_event))
return;
for (i = 0; i < VTSCSI_NUM_EVENT_BUFS; i++) {
event = &sc->vtscsi_event_bufs[i];
error = vtscsi_enqueue_event_buf(sc, event);
if (error)
break;
}
KASSERT(i > 0, ("cannot reinit event vq: %d", error));
}
static void
vtscsi_drain_event_vq(struct vtscsi_softc *sc)
{
struct virtqueue *vq;
int last;
vq = sc->vtscsi_event_vq;
last = 0;
while (virtqueue_drain(vq, &last) != NULL)
;
KASSERT(virtqueue_empty(vq), ("eventvq not empty"));
}
static void
vtscsi_complete_vqs_locked(struct vtscsi_softc *sc)
{
VTSCSI_LOCK_OWNED(sc);
if (sc->vtscsi_request_vq != NULL)
vtscsi_complete_vq(sc, sc->vtscsi_request_vq);
if (sc->vtscsi_control_vq != NULL)
vtscsi_complete_vq(sc, sc->vtscsi_control_vq);
}
static void
vtscsi_complete_vqs(struct vtscsi_softc *sc)
{
VTSCSI_LOCK(sc);
vtscsi_complete_vqs_locked(sc);
VTSCSI_UNLOCK(sc);
}
static void
vtscsi_cancel_request(struct vtscsi_softc *sc, struct vtscsi_request *req)
{
union ccb *ccb;
int detach;
ccb = req->vsr_ccb;
vtscsi_dprintf(sc, VTSCSI_TRACE, "req=%p ccb=%p\n", req, ccb);
/*
* The callout must be drained when detaching since the request is
* about to be freed. The VTSCSI_MTX must not be held for this in
* case the callout is pending because there is a deadlock potential.
* Otherwise, the virtqueue is being drained because of a bus reset
* so we only need to attempt to stop the callouts.
*/
detach = (sc->vtscsi_flags & VTSCSI_FLAG_DETACH) != 0;
if (detach != 0)
VTSCSI_LOCK_NOTOWNED(sc);
else
VTSCSI_LOCK_OWNED(sc);
if (req->vsr_flags & VTSCSI_REQ_FLAG_TIMEOUT_SET) {
if (detach != 0)
callout_drain(&req->vsr_callout);
else
callout_stop(&req->vsr_callout);
}
if (ccb != NULL) {
if (detach != 0) {
VTSCSI_LOCK(sc);
ccb->ccb_h.status = CAM_NO_HBA;
} else
ccb->ccb_h.status = CAM_REQUEUE_REQ;
xpt_done(ccb);
if (detach != 0)
VTSCSI_UNLOCK(sc);
}
vtscsi_enqueue_request(sc, req);
}
static void
vtscsi_drain_vq(struct vtscsi_softc *sc, struct virtqueue *vq)
{
struct vtscsi_request *req;
int last;
last = 0;
vtscsi_dprintf(sc, VTSCSI_TRACE, "vq=%p\n", vq);
while ((req = virtqueue_drain(vq, &last)) != NULL)
vtscsi_cancel_request(sc, req);
KASSERT(virtqueue_empty(vq), ("virtqueue not empty"));
}
static void
vtscsi_drain_vqs(struct vtscsi_softc *sc)
{
if (sc->vtscsi_control_vq != NULL)
vtscsi_drain_vq(sc, sc->vtscsi_control_vq);
if (sc->vtscsi_request_vq != NULL)
vtscsi_drain_vq(sc, sc->vtscsi_request_vq);
if (sc->vtscsi_event_vq != NULL)
vtscsi_drain_event_vq(sc);
}
static void
vtscsi_stop(struct vtscsi_softc *sc)
{
vtscsi_disable_vqs_intr(sc);
virtio_stop(sc->vtscsi_dev);
}
static int
vtscsi_reset_bus(struct vtscsi_softc *sc)
{
int error;
VTSCSI_LOCK_OWNED(sc);
if (vtscsi_bus_reset_disable != 0) {
device_printf(sc->vtscsi_dev, "bus reset disabled\n");
return (0);
}
sc->vtscsi_flags |= VTSCSI_FLAG_RESET;
/*
* vtscsi_stop() will cause the in-flight requests to be canceled.
* Those requests are then completed here so CAM will retry them
* after the reset is complete.
*/
vtscsi_stop(sc);
vtscsi_complete_vqs_locked(sc);
/* Rid the virtqueues of any remaining requests. */
vtscsi_drain_vqs(sc);
/*
* Any resource shortage that froze the SIMQ cannot persist across
* a bus reset so ensure it gets thawed here.
*/
if (vtscsi_thaw_simq(sc, VTSCSI_REQUEST | VTSCSI_REQUEST_VQ) != 0)
xpt_release_simq(sc->vtscsi_sim, 0);
error = vtscsi_reinit(sc);
if (error) {
device_printf(sc->vtscsi_dev,
"reinitialization failed, stopping device...\n");
vtscsi_stop(sc);
} else
vtscsi_announce(sc, AC_BUS_RESET, CAM_TARGET_WILDCARD,
CAM_LUN_WILDCARD);
sc->vtscsi_flags &= ~VTSCSI_FLAG_RESET;
return (error);
}
static void
vtscsi_init_request(struct vtscsi_softc *sc, struct vtscsi_request *req)
{
#ifdef INVARIANTS
int req_nsegs, resp_nsegs;
req_nsegs = sglist_count(&req->vsr_ureq, sizeof(req->vsr_ureq));
resp_nsegs = sglist_count(&req->vsr_uresp, sizeof(req->vsr_uresp));
KASSERT(req_nsegs == 1, ("request crossed page boundary"));
KASSERT(resp_nsegs == 1, ("response crossed page boundary"));
#endif
req->vsr_softc = sc;
callout_init_mtx(&req->vsr_callout, VTSCSI_MTX(sc), 0);
}
static int
vtscsi_alloc_requests(struct vtscsi_softc *sc)
{
struct vtscsi_request *req;
int i, nreqs;
/*
* Commands destined for either the request or control queues come
* from the same SIM queue. Use the size of the request virtqueue
* as it (should) be much more frequently used. Some additional
* requests are allocated for internal (TMF) use.
*/
nreqs = virtqueue_size(sc->vtscsi_request_vq);
if ((sc->vtscsi_flags & VTSCSI_FLAG_INDIRECT) == 0)
nreqs /= VTSCSI_MIN_SEGMENTS;
nreqs += VTSCSI_RESERVED_REQUESTS;
for (i = 0; i < nreqs; i++) {
req = malloc(sizeof(struct vtscsi_request), M_DEVBUF,
M_NOWAIT);
if (req == NULL)
return (ENOMEM);
vtscsi_init_request(sc, req);
sc->vtscsi_nrequests++;
vtscsi_enqueue_request(sc, req);
}
return (0);
}
static void
vtscsi_free_requests(struct vtscsi_softc *sc)
{
struct vtscsi_request *req;
while ((req = vtscsi_dequeue_request(sc)) != NULL) {
KASSERT(callout_active(&req->vsr_callout) == 0,
("request callout still active"));
sc->vtscsi_nrequests--;
free(req, M_DEVBUF);
}
KASSERT(sc->vtscsi_nrequests == 0, ("leaked requests: %d",
sc->vtscsi_nrequests));
}
static void
vtscsi_enqueue_request(struct vtscsi_softc *sc, struct vtscsi_request *req)
{
KASSERT(req->vsr_softc == sc,
("non-matching request vsr_softc %p/%p", req->vsr_softc, sc));
vtscsi_dprintf(sc, VTSCSI_TRACE, "req=%p\n", req);
/* A request is available so the SIMQ could be released. */
if (vtscsi_thaw_simq(sc, VTSCSI_REQUEST) != 0)
xpt_release_simq(sc->vtscsi_sim, 1);
req->vsr_ccb = NULL;
req->vsr_complete = NULL;
req->vsr_ptr0 = NULL;
req->vsr_state = VTSCSI_REQ_STATE_FREE;
req->vsr_flags = 0;
bzero(&req->vsr_ureq, sizeof(req->vsr_ureq));
bzero(&req->vsr_uresp, sizeof(req->vsr_uresp));
/*
* We insert at the tail of the queue in order to make it
* very unlikely a request will be reused if we race with
* stopping its callout handler.
*/
TAILQ_INSERT_TAIL(&sc->vtscsi_req_free, req, vsr_link);
}
static struct vtscsi_request *
vtscsi_dequeue_request(struct vtscsi_softc *sc)
{
struct vtscsi_request *req;
req = TAILQ_FIRST(&sc->vtscsi_req_free);
if (req != NULL) {
req->vsr_state = VTSCSI_REQ_STATE_INUSE;
TAILQ_REMOVE(&sc->vtscsi_req_free, req, vsr_link);
} else
sc->vtscsi_stats.dequeue_no_requests++;
vtscsi_dprintf(sc, VTSCSI_TRACE, "req=%p\n", req);
return (req);
}
static void
vtscsi_complete_request(struct vtscsi_request *req)
{
if (req->vsr_flags & VTSCSI_REQ_FLAG_POLLED)
req->vsr_flags |= VTSCSI_REQ_FLAG_COMPLETE;
if (req->vsr_complete != NULL)
req->vsr_complete(req->vsr_softc, req);
}
static void
vtscsi_complete_vq(struct vtscsi_softc *sc, struct virtqueue *vq)
{
struct vtscsi_request *req;
VTSCSI_LOCK_OWNED(sc);
while ((req = virtqueue_dequeue(vq, NULL)) != NULL)
vtscsi_complete_request(req);
}
static void
vtscsi_control_vq_intr(void *xsc)
{
struct vtscsi_softc *sc;
struct virtqueue *vq;
sc = xsc;
vq = sc->vtscsi_control_vq;
again:
VTSCSI_LOCK(sc);
vtscsi_complete_vq(sc, sc->vtscsi_control_vq);
if (virtqueue_enable_intr(vq) != 0) {
virtqueue_disable_intr(vq);
VTSCSI_UNLOCK(sc);
goto again;
}
VTSCSI_UNLOCK(sc);
}
static void
vtscsi_event_vq_intr(void *xsc)
{
struct vtscsi_softc *sc;
struct virtqueue *vq;
struct virtio_scsi_event *event;
sc = xsc;
vq = sc->vtscsi_event_vq;
again:
VTSCSI_LOCK(sc);
while ((event = virtqueue_dequeue(vq, NULL)) != NULL)
vtscsi_handle_event(sc, event);
if (virtqueue_enable_intr(vq) != 0) {
virtqueue_disable_intr(vq);
VTSCSI_UNLOCK(sc);
goto again;
}
VTSCSI_UNLOCK(sc);
}
static void
vtscsi_request_vq_intr(void *xsc)
{
struct vtscsi_softc *sc;
struct virtqueue *vq;
sc = xsc;
vq = sc->vtscsi_request_vq;
again:
VTSCSI_LOCK(sc);
vtscsi_complete_vq(sc, sc->vtscsi_request_vq);
if (virtqueue_enable_intr(vq) != 0) {
virtqueue_disable_intr(vq);
VTSCSI_UNLOCK(sc);
goto again;
}
VTSCSI_UNLOCK(sc);
}
static void
vtscsi_disable_vqs_intr(struct vtscsi_softc *sc)
{
virtqueue_disable_intr(sc->vtscsi_control_vq);
virtqueue_disable_intr(sc->vtscsi_event_vq);
virtqueue_disable_intr(sc->vtscsi_request_vq);
}
static void
vtscsi_enable_vqs_intr(struct vtscsi_softc *sc)
{
virtqueue_enable_intr(sc->vtscsi_control_vq);
virtqueue_enable_intr(sc->vtscsi_event_vq);
virtqueue_enable_intr(sc->vtscsi_request_vq);
}
static void
vtscsi_get_tunables(struct vtscsi_softc *sc)
{
char tmpstr[64];
TUNABLE_INT_FETCH("hw.vtscsi.debug_level", &sc->vtscsi_debug);
snprintf(tmpstr, sizeof(tmpstr), "dev.vtscsi.%d.debug_level",
device_get_unit(sc->vtscsi_dev));
TUNABLE_INT_FETCH(tmpstr, &sc->vtscsi_debug);
}
static void
vtscsi_add_sysctl(struct vtscsi_softc *sc)
{
device_t dev;
struct vtscsi_statistics *stats;
struct sysctl_ctx_list *ctx;
struct sysctl_oid *tree;
struct sysctl_oid_list *child;
dev = sc->vtscsi_dev;
stats = &sc->vtscsi_stats;
ctx = device_get_sysctl_ctx(dev);
tree = device_get_sysctl_tree(dev);
child = SYSCTL_CHILDREN(tree);
SYSCTL_ADD_INT(ctx, child, OID_AUTO, "debug_level",
CTLFLAG_RW, &sc->vtscsi_debug, 0,
"Debug level");
SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "scsi_cmd_timeouts",
CTLFLAG_RD, &stats->scsi_cmd_timeouts,
"SCSI command timeouts");
SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "dequeue_no_requests",
CTLFLAG_RD, &stats->dequeue_no_requests,
"No available requests to dequeue");
}
static void
vtscsi_printf_req(struct vtscsi_request *req, const char *func,
const char *fmt, ...)
{
struct vtscsi_softc *sc;
union ccb *ccb;
struct sbuf sb;
va_list ap;
char str[192];
char path_str[64];
if (req == NULL)
return;
sc = req->vsr_softc;
ccb = req->vsr_ccb;
va_start(ap, fmt);
sbuf_new(&sb, str, sizeof(str), 0);
if (ccb == NULL) {
sbuf_printf(&sb, "(noperiph:%s%d:%u): ",
cam_sim_name(sc->vtscsi_sim), cam_sim_unit(sc->vtscsi_sim),
cam_sim_bus(sc->vtscsi_sim));
} else {
xpt_path_string(ccb->ccb_h.path, path_str, sizeof(path_str));
sbuf_cat(&sb, path_str);
if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
scsi_command_string(&ccb->csio, &sb);
sbuf_printf(&sb, "length %d ", ccb->csio.dxfer_len);
}
}
sbuf_vprintf(&sb, fmt, ap);
va_end(ap);
sbuf_finish(&sb);
printf("%s: %s: %s", device_get_nameunit(sc->vtscsi_dev), func,
sbuf_data(&sb));
}
|