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
|
/* $NetBSD: virtio.c,v 1.78 2023/04/21 02:17:32 yamaguchi Exp $ */
/*
* Copyright (c) 2020 The NetBSD Foundation, Inc.
* Copyright (c) 2012 Stefan Fritsch, Alexander Fiveg.
* Copyright (c) 2010 Minoura Makoto.
* 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, 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.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: virtio.c,v 1.78 2023/04/21 02:17:32 yamaguchi Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/atomic.h>
#include <sys/bus.h>
#include <sys/device.h>
#include <sys/kmem.h>
#include <sys/module.h>
#define VIRTIO_PRIVATE
#include <dev/pci/virtioreg.h> /* XXX: move to non-pci */
#include <dev/pci/virtiovar.h> /* XXX: move to non-pci */
#define MINSEG_INDIRECT 2 /* use indirect if nsegs >= this value */
/*
* The maximum descriptor size is 2^15. Use that value as the end of
* descriptor chain terminator since it will never be a valid index
* in the descriptor table.
*/
#define VRING_DESC_CHAIN_END 32768
/* incomplete list */
static const char *virtio_device_name[] = {
"unknown (0)", /* 0 */
"network", /* 1 */
"block", /* 2 */
"console", /* 3 */
"entropy", /* 4 */
"memory balloon", /* 5 */
"I/O memory", /* 6 */
"remote processor messaging", /* 7 */
"SCSI", /* 8 */
"9P transport", /* 9 */
};
#define NDEVNAMES __arraycount(virtio_device_name)
static void virtio_reset_vq(struct virtio_softc *,
struct virtqueue *);
void
virtio_set_status(struct virtio_softc *sc, int status)
{
sc->sc_ops->set_status(sc, status);
}
/*
* Reset the device.
*/
/*
* To reset the device to a known state, do following:
* virtio_reset(sc); // this will stop the device activity
* <dequeue finished requests>; // virtio_dequeue() still can be called
* <revoke pending requests in the vqs if any>;
* virtio_reinit_start(sc); // dequeue prohibitted
* newfeatures = virtio_negotiate_features(sc, requestedfeatures);
* <some other initialization>;
* virtio_reinit_end(sc); // device activated; enqueue allowed
* Once attached, feature negotiation can only be allowed after virtio_reset.
*/
void
virtio_reset(struct virtio_softc *sc)
{
virtio_device_reset(sc);
}
int
virtio_reinit_start(struct virtio_softc *sc)
{
int i, r;
virtio_set_status(sc, VIRTIO_CONFIG_DEVICE_STATUS_ACK);
virtio_set_status(sc, VIRTIO_CONFIG_DEVICE_STATUS_DRIVER);
for (i = 0; i < sc->sc_nvqs; i++) {
int n;
struct virtqueue *vq = &sc->sc_vqs[i];
n = sc->sc_ops->read_queue_size(sc, vq->vq_index);
if (n == 0) /* vq disappeared */
continue;
if (n != vq->vq_num) {
panic("%s: virtqueue size changed, vq index %d\n",
device_xname(sc->sc_dev),
vq->vq_index);
}
virtio_reset_vq(sc, vq);
sc->sc_ops->setup_queue(sc, vq->vq_index,
vq->vq_dmamap->dm_segs[0].ds_addr);
}
r = sc->sc_ops->setup_interrupts(sc, 1);
if (r != 0)
goto fail;
return 0;
fail:
virtio_set_status(sc, VIRTIO_CONFIG_DEVICE_STATUS_FAILED);
return 1;
}
void
virtio_reinit_end(struct virtio_softc *sc)
{
virtio_set_status(sc, VIRTIO_CONFIG_DEVICE_STATUS_DRIVER_OK);
}
/*
* Feature negotiation.
*/
void
virtio_negotiate_features(struct virtio_softc *sc, uint64_t guest_features)
{
if (!(device_cfdata(sc->sc_dev)->cf_flags & 1) &&
!(device_cfdata(sc->sc_child)->cf_flags & 1)) /* XXX */
guest_features |= VIRTIO_F_RING_INDIRECT_DESC;
sc->sc_ops->neg_features(sc, guest_features);
if (sc->sc_active_features & VIRTIO_F_RING_INDIRECT_DESC)
sc->sc_indirect = true;
else
sc->sc_indirect = false;
}
/*
* Device configuration registers readers/writers
*/
#if 0
#define DPRINTFR(n, fmt, val, index, num) \
printf("\n%s (", n); \
for (int i = 0; i < num; i++) \
printf("%02x ", bus_space_read_1(sc->sc_devcfg_iot, sc->sc_devcfg_ioh, index+i)); \
printf(") -> "); printf(fmt, val); printf("\n");
#define DPRINTFR2(n, fmt, val_s, val_n) \
printf("%s ", n); \
printf("\n stream "); printf(fmt, val_s); printf(" norm "); printf(fmt, val_n); printf("\n");
#else
#define DPRINTFR(n, fmt, val, index, num)
#define DPRINTFR2(n, fmt, val_s, val_n)
#endif
uint8_t
virtio_read_device_config_1(struct virtio_softc *sc, int index)
{
bus_space_tag_t iot = sc->sc_devcfg_iot;
bus_space_handle_t ioh = sc->sc_devcfg_ioh;
uint8_t val;
val = bus_space_read_1(iot, ioh, index);
DPRINTFR("read_1", "%02x", val, index, 1);
return val;
}
uint16_t
virtio_read_device_config_2(struct virtio_softc *sc, int index)
{
bus_space_tag_t iot = sc->sc_devcfg_iot;
bus_space_handle_t ioh = sc->sc_devcfg_ioh;
uint16_t val;
val = bus_space_read_2(iot, ioh, index);
if (BYTE_ORDER != sc->sc_bus_endian)
val = bswap16(val);
DPRINTFR("read_2", "%04x", val, index, 2);
DPRINTFR2("read_2", "%04x",
bus_space_read_stream_2(sc->sc_devcfg_iot, sc->sc_devcfg_ioh,
index),
bus_space_read_2(sc->sc_devcfg_iot, sc->sc_devcfg_ioh, index));
return val;
}
uint32_t
virtio_read_device_config_4(struct virtio_softc *sc, int index)
{
bus_space_tag_t iot = sc->sc_devcfg_iot;
bus_space_handle_t ioh = sc->sc_devcfg_ioh;
uint32_t val;
val = bus_space_read_4(iot, ioh, index);
if (BYTE_ORDER != sc->sc_bus_endian)
val = bswap32(val);
DPRINTFR("read_4", "%08x", val, index, 4);
DPRINTFR2("read_4", "%08x",
bus_space_read_stream_4(sc->sc_devcfg_iot, sc->sc_devcfg_ioh,
index),
bus_space_read_4(sc->sc_devcfg_iot, sc->sc_devcfg_ioh, index));
return val;
}
/*
* The Virtio spec explicitly tells that reading and writing 8 bytes are not
* considered atomic and no triggers may be connected to reading or writing
* it. We access it using two 32 reads. See virtio spec 4.1.3.1.
*/
uint64_t
virtio_read_device_config_8(struct virtio_softc *sc, int index)
{
bus_space_tag_t iot = sc->sc_devcfg_iot;
bus_space_handle_t ioh = sc->sc_devcfg_ioh;
union {
uint64_t u64;
uint32_t l[2];
} v;
uint64_t val;
v.l[0] = bus_space_read_4(iot, ioh, index);
v.l[1] = bus_space_read_4(iot, ioh, index + 4);
if (sc->sc_bus_endian != sc->sc_struct_endian) {
v.l[0] = bswap32(v.l[0]);
v.l[1] = bswap32(v.l[1]);
}
val = v.u64;
if (BYTE_ORDER != sc->sc_struct_endian)
val = bswap64(val);
DPRINTFR("read_8", "%08"PRIx64, val, index, 8);
DPRINTFR2("read_8 low ", "%08x",
bus_space_read_stream_4(sc->sc_devcfg_iot, sc->sc_devcfg_ioh,
index),
bus_space_read_4(sc->sc_devcfg_iot, sc->sc_devcfg_ioh, index));
DPRINTFR2("read_8 high ", "%08x",
bus_space_read_stream_4(sc->sc_devcfg_iot, sc->sc_devcfg_ioh,
index + 4),
bus_space_read_4(sc->sc_devcfg_iot, sc->sc_devcfg_ioh, index + 4));
return val;
}
/*
* In the older virtio spec, device config registers are host endian. On newer
* they are little endian. Some newer devices however explicitly specify their
* register to always be little endian. These functions cater for these.
*/
uint16_t
virtio_read_device_config_le_2(struct virtio_softc *sc, int index)
{
bus_space_tag_t iot = sc->sc_devcfg_iot;
bus_space_handle_t ioh = sc->sc_devcfg_ioh;
uint16_t val;
val = bus_space_read_2(iot, ioh, index);
if (sc->sc_bus_endian != LITTLE_ENDIAN)
val = bswap16(val);
DPRINTFR("read_le_2", "%04x", val, index, 2);
DPRINTFR2("read_le_2", "%04x",
bus_space_read_stream_2(sc->sc_devcfg_iot, sc->sc_devcfg_ioh, 0),
bus_space_read_2(sc->sc_devcfg_iot, sc->sc_devcfg_ioh, 0));
return val;
}
uint32_t
virtio_read_device_config_le_4(struct virtio_softc *sc, int index)
{
bus_space_tag_t iot = sc->sc_devcfg_iot;
bus_space_handle_t ioh = sc->sc_devcfg_ioh;
uint32_t val;
val = bus_space_read_4(iot, ioh, index);
if (sc->sc_bus_endian != LITTLE_ENDIAN)
val = bswap32(val);
DPRINTFR("read_le_4", "%08x", val, index, 4);
DPRINTFR2("read_le_4", "%08x",
bus_space_read_stream_4(sc->sc_devcfg_iot, sc->sc_devcfg_ioh, 0),
bus_space_read_4(sc->sc_devcfg_iot, sc->sc_devcfg_ioh, 0));
return val;
}
void
virtio_write_device_config_1(struct virtio_softc *sc, int index, uint8_t value)
{
bus_space_tag_t iot = sc->sc_devcfg_iot;
bus_space_handle_t ioh = sc->sc_devcfg_ioh;
bus_space_write_1(iot, ioh, index, value);
}
void
virtio_write_device_config_2(struct virtio_softc *sc, int index,
uint16_t value)
{
bus_space_tag_t iot = sc->sc_devcfg_iot;
bus_space_handle_t ioh = sc->sc_devcfg_ioh;
if (BYTE_ORDER != sc->sc_bus_endian)
value = bswap16(value);
bus_space_write_2(iot, ioh, index, value);
}
void
virtio_write_device_config_4(struct virtio_softc *sc, int index,
uint32_t value)
{
bus_space_tag_t iot = sc->sc_devcfg_iot;
bus_space_handle_t ioh = sc->sc_devcfg_ioh;
if (BYTE_ORDER != sc->sc_bus_endian)
value = bswap32(value);
bus_space_write_4(iot, ioh, index, value);
}
/*
* The Virtio spec explicitly tells that reading and writing 8 bytes are not
* considered atomic and no triggers may be connected to reading or writing
* it. We access it using two 32 bit writes. For good measure it is stated to
* always write lsb first just in case of a hypervisor bug. See See virtio
* spec 4.1.3.1.
*/
void
virtio_write_device_config_8(struct virtio_softc *sc, int index,
uint64_t value)
{
bus_space_tag_t iot = sc->sc_devcfg_iot;
bus_space_handle_t ioh = sc->sc_devcfg_ioh;
union {
uint64_t u64;
uint32_t l[2];
} v;
if (BYTE_ORDER != sc->sc_struct_endian)
value = bswap64(value);
v.u64 = value;
if (sc->sc_bus_endian != sc->sc_struct_endian) {
v.l[0] = bswap32(v.l[0]);
v.l[1] = bswap32(v.l[1]);
}
if (sc->sc_struct_endian == LITTLE_ENDIAN) {
bus_space_write_4(iot, ioh, index, v.l[0]);
bus_space_write_4(iot, ioh, index + 4, v.l[1]);
} else {
bus_space_write_4(iot, ioh, index + 4, v.l[1]);
bus_space_write_4(iot, ioh, index, v.l[0]);
}
}
/*
* In the older virtio spec, device config registers are host endian. On newer
* they are little endian. Some newer devices however explicitly specify their
* register to always be little endian. These functions cater for these.
*/
void
virtio_write_device_config_le_2(struct virtio_softc *sc, int index,
uint16_t value)
{
bus_space_tag_t iot = sc->sc_devcfg_iot;
bus_space_handle_t ioh = sc->sc_devcfg_ioh;
if (sc->sc_bus_endian != LITTLE_ENDIAN)
value = bswap16(value);
bus_space_write_2(iot, ioh, index, value);
}
void
virtio_write_device_config_le_4(struct virtio_softc *sc, int index,
uint32_t value)
{
bus_space_tag_t iot = sc->sc_devcfg_iot;
bus_space_handle_t ioh = sc->sc_devcfg_ioh;
if (sc->sc_bus_endian != LITTLE_ENDIAN)
value = bswap32(value);
bus_space_write_4(iot, ioh, index, value);
}
/*
* data structures endian helpers
*/
uint16_t
virtio_rw16(struct virtio_softc *sc, uint16_t val)
{
KASSERT(sc);
return BYTE_ORDER != sc->sc_struct_endian ? bswap16(val) : val;
}
uint32_t
virtio_rw32(struct virtio_softc *sc, uint32_t val)
{
KASSERT(sc);
return BYTE_ORDER != sc->sc_struct_endian ? bswap32(val) : val;
}
uint64_t
virtio_rw64(struct virtio_softc *sc, uint64_t val)
{
KASSERT(sc);
return BYTE_ORDER != sc->sc_struct_endian ? bswap64(val) : val;
}
/*
* Interrupt handler.
*/
static void
virtio_soft_intr(void *arg)
{
struct virtio_softc *sc = arg;
KASSERT(sc->sc_intrhand != NULL);
(*sc->sc_intrhand)(sc);
}
/* set to vq->vq_intrhand in virtio_init_vq_vqdone() */
static int
virtio_vq_done(void *xvq)
{
struct virtqueue *vq = xvq;
return vq->vq_done(vq);
}
static int
virtio_vq_intr(struct virtio_softc *sc)
{
struct virtqueue *vq;
int i, r = 0;
for (i = 0; i < sc->sc_nvqs; i++) {
vq = &sc->sc_vqs[i];
if (virtio_vq_is_enqueued(sc, vq) == 1) {
r |= (*vq->vq_intrhand)(vq->vq_intrhand_arg);
}
}
return r;
}
/*
* dmamap sync operations for a virtqueue.
*/
static inline void
vq_sync_descs(struct virtio_softc *sc, struct virtqueue *vq, int ops)
{
/* availoffset == sizeof(vring_desc) * vq_num */
bus_dmamap_sync(sc->sc_dmat, vq->vq_dmamap, 0, vq->vq_availoffset,
ops);
}
static inline void
vq_sync_aring_all(struct virtio_softc *sc, struct virtqueue *vq, int ops)
{
uint16_t hdrlen = offsetof(struct vring_avail, ring);
size_t payloadlen = vq->vq_num * sizeof(uint16_t);
size_t usedlen = 0;
if (sc->sc_active_features & VIRTIO_F_RING_EVENT_IDX)
usedlen = sizeof(uint16_t);
bus_dmamap_sync(sc->sc_dmat, vq->vq_dmamap,
vq->vq_availoffset, hdrlen + payloadlen + usedlen, ops);
}
static inline void
vq_sync_aring_header(struct virtio_softc *sc, struct virtqueue *vq, int ops)
{
uint16_t hdrlen = offsetof(struct vring_avail, ring);
bus_dmamap_sync(sc->sc_dmat, vq->vq_dmamap,
vq->vq_availoffset, hdrlen, ops);
}
static inline void
vq_sync_aring_payload(struct virtio_softc *sc, struct virtqueue *vq, int ops)
{
uint16_t hdrlen = offsetof(struct vring_avail, ring);
size_t payloadlen = vq->vq_num * sizeof(uint16_t);
bus_dmamap_sync(sc->sc_dmat, vq->vq_dmamap,
vq->vq_availoffset + hdrlen, payloadlen, ops);
}
static inline void
vq_sync_aring_used(struct virtio_softc *sc, struct virtqueue *vq, int ops)
{
uint16_t hdrlen = offsetof(struct vring_avail, ring);
size_t payloadlen = vq->vq_num * sizeof(uint16_t);
size_t usedlen = sizeof(uint16_t);
if ((sc->sc_active_features & VIRTIO_F_RING_EVENT_IDX) == 0)
return;
bus_dmamap_sync(sc->sc_dmat, vq->vq_dmamap,
vq->vq_availoffset + hdrlen + payloadlen, usedlen, ops);
}
static inline void
vq_sync_uring_all(struct virtio_softc *sc, struct virtqueue *vq, int ops)
{
uint16_t hdrlen = offsetof(struct vring_used, ring);
size_t payloadlen = vq->vq_num * sizeof(struct vring_used_elem);
size_t availlen = 0;
if (sc->sc_active_features & VIRTIO_F_RING_EVENT_IDX)
availlen = sizeof(uint16_t);
bus_dmamap_sync(sc->sc_dmat, vq->vq_dmamap,
vq->vq_usedoffset, hdrlen + payloadlen + availlen, ops);
}
static inline void
vq_sync_uring_header(struct virtio_softc *sc, struct virtqueue *vq, int ops)
{
uint16_t hdrlen = offsetof(struct vring_used, ring);
bus_dmamap_sync(sc->sc_dmat, vq->vq_dmamap,
vq->vq_usedoffset, hdrlen, ops);
}
static inline void
vq_sync_uring_payload(struct virtio_softc *sc, struct virtqueue *vq, int ops)
{
uint16_t hdrlen = offsetof(struct vring_used, ring);
size_t payloadlen = vq->vq_num * sizeof(struct vring_used_elem);
bus_dmamap_sync(sc->sc_dmat, vq->vq_dmamap,
vq->vq_usedoffset + hdrlen, payloadlen, ops);
}
static inline void
vq_sync_uring_avail(struct virtio_softc *sc, struct virtqueue *vq, int ops)
{
uint16_t hdrlen = offsetof(struct vring_used, ring);
size_t payloadlen = vq->vq_num * sizeof(struct vring_used_elem);
size_t availlen = sizeof(uint16_t);
if ((sc->sc_active_features & VIRTIO_F_RING_EVENT_IDX) == 0)
return;
bus_dmamap_sync(sc->sc_dmat, vq->vq_dmamap,
vq->vq_usedoffset + hdrlen + payloadlen, availlen, ops);
}
static inline void
vq_sync_indirect(struct virtio_softc *sc, struct virtqueue *vq, int slot,
int ops)
{
int offset = vq->vq_indirectoffset +
sizeof(struct vring_desc) * vq->vq_maxnsegs * slot;
bus_dmamap_sync(sc->sc_dmat, vq->vq_dmamap,
offset, sizeof(struct vring_desc) * vq->vq_maxnsegs, ops);
}
bool
virtio_vq_is_enqueued(struct virtio_softc *sc, struct virtqueue *vq)
{
if (vq->vq_queued) {
vq->vq_queued = 0;
vq_sync_aring_all(sc, vq, BUS_DMASYNC_POSTWRITE);
}
vq_sync_uring_header(sc, vq, BUS_DMASYNC_POSTREAD);
if (vq->vq_used_idx == virtio_rw16(sc, vq->vq_used->idx))
return 0;
vq_sync_uring_payload(sc, vq, BUS_DMASYNC_POSTREAD);
return 1;
}
/*
* Increase the event index in order to delay interrupts.
*/
int
virtio_postpone_intr(struct virtio_softc *sc, struct virtqueue *vq,
uint16_t nslots)
{
uint16_t idx, nused;
idx = vq->vq_used_idx + nslots;
/* set the new event index: avail_ring->used_event = idx */
*vq->vq_used_event = virtio_rw16(sc, idx);
vq_sync_aring_used(vq->vq_owner, vq, BUS_DMASYNC_PREWRITE);
vq->vq_queued++;
nused = (uint16_t)
(virtio_rw16(sc, vq->vq_used->idx) - vq->vq_used_idx);
KASSERT(nused <= vq->vq_num);
return nslots < nused;
}
/*
* Postpone interrupt until 3/4 of the available descriptors have been
* consumed.
*/
int
virtio_postpone_intr_smart(struct virtio_softc *sc, struct virtqueue *vq)
{
uint16_t nslots;
nslots = (uint16_t)
(virtio_rw16(sc, vq->vq_avail->idx) - vq->vq_used_idx) * 3 / 4;
return virtio_postpone_intr(sc, vq, nslots);
}
/*
* Postpone interrupt until all of the available descriptors have been
* consumed.
*/
int
virtio_postpone_intr_far(struct virtio_softc *sc, struct virtqueue *vq)
{
uint16_t nslots;
nslots = (uint16_t)
(virtio_rw16(sc, vq->vq_avail->idx) - vq->vq_used_idx);
return virtio_postpone_intr(sc, vq, nslots);
}
/*
* Start/stop vq interrupt. No guarantee.
*/
void
virtio_stop_vq_intr(struct virtio_softc *sc, struct virtqueue *vq)
{
if (sc->sc_active_features & VIRTIO_F_RING_EVENT_IDX) {
/*
* No way to disable the interrupt completely with
* RingEventIdx. Instead advance used_event by half the
* possible value. This won't happen soon and is far enough in
* the past to not trigger a spurios interrupt.
*/
*vq->vq_used_event = virtio_rw16(sc, vq->vq_used_idx + 0x8000);
vq_sync_aring_used(sc, vq, BUS_DMASYNC_PREWRITE);
} else {
vq->vq_avail->flags |=
virtio_rw16(sc, VRING_AVAIL_F_NO_INTERRUPT);
vq_sync_aring_header(sc, vq, BUS_DMASYNC_PREWRITE);
}
vq->vq_queued++;
}
int
virtio_start_vq_intr(struct virtio_softc *sc, struct virtqueue *vq)
{
if (sc->sc_active_features & VIRTIO_F_RING_EVENT_IDX) {
/*
* If event index feature is negotiated, enabling interrupts
* is done through setting the latest consumed index in the
* used_event field
*/
*vq->vq_used_event = virtio_rw16(sc, vq->vq_used_idx);
vq_sync_aring_used(sc, vq, BUS_DMASYNC_PREWRITE);
} else {
vq->vq_avail->flags &=
~virtio_rw16(sc, VRING_AVAIL_F_NO_INTERRUPT);
vq_sync_aring_header(sc, vq, BUS_DMASYNC_PREWRITE);
}
vq->vq_queued++;
vq_sync_uring_header(sc, vq, BUS_DMASYNC_POSTREAD);
if (vq->vq_used_idx == virtio_rw16(sc, vq->vq_used->idx))
return 0;
vq_sync_uring_payload(sc, vq, BUS_DMASYNC_POSTREAD);
return 1;
}
/*
* Initialize vq structure.
*/
/*
* Reset virtqueue parameters
*/
static void
virtio_reset_vq(struct virtio_softc *sc, struct virtqueue *vq)
{
struct vring_desc *vds;
int i, j;
int vq_size = vq->vq_num;
memset(vq->vq_vaddr, 0, vq->vq_bytesize);
/* build the descriptor chain for free slot management */
vds = vq->vq_desc;
for (i = 0; i < vq_size - 1; i++) {
vds[i].next = virtio_rw16(sc, i + 1);
}
vds[i].next = virtio_rw16(sc, VRING_DESC_CHAIN_END);
vq->vq_free_idx = 0;
/* build the indirect descriptor chain */
if (vq->vq_indirect != NULL) {
struct vring_desc *vd;
for (i = 0; i < vq_size; i++) {
vd = vq->vq_indirect;
vd += vq->vq_maxnsegs * i;
for (j = 0; j < vq->vq_maxnsegs - 1; j++) {
vd[j].next = virtio_rw16(sc, j + 1);
}
}
}
/* enqueue/dequeue status */
vq->vq_avail_idx = 0;
vq->vq_used_idx = 0;
vq->vq_queued = 0;
vq_sync_uring_all(sc, vq, BUS_DMASYNC_PREREAD);
vq->vq_queued++;
}
/* Initialize vq */
void
virtio_init_vq_vqdone(struct virtio_softc *sc, struct virtqueue *vq,
int index, int (*vq_done)(struct virtqueue *))
{
virtio_init_vq(sc, vq, index, virtio_vq_done, vq);
vq->vq_done = vq_done;
}
void
virtio_init_vq(struct virtio_softc *sc, struct virtqueue *vq, int index,
int (*func)(void *), void *arg)
{
memset(vq, 0, sizeof(*vq));
vq->vq_owner = sc;
vq->vq_num = sc->sc_ops->read_queue_size(sc, index);
vq->vq_index = index;
vq->vq_intrhand = func;
vq->vq_intrhand_arg = arg;
}
/*
* Allocate/free a vq.
*/
int
virtio_alloc_vq(struct virtio_softc *sc, struct virtqueue *vq,
int maxsegsize, int maxnsegs, const char *name)
{
bus_size_t size_desc, size_avail, size_used, size_indirect;
bus_size_t allocsize = 0, size_desc_avail;
int rsegs, r, hdrlen;
unsigned int vq_num;
#define VIRTQUEUE_ALIGN(n) roundup(n, VIRTIO_PAGE_SIZE)
vq_num = vq->vq_num;
if (vq_num == 0) {
aprint_error_dev(sc->sc_dev,
"virtqueue not exist, index %d for %s\n",
vq->vq_index, name);
goto err;
}
hdrlen = sc->sc_active_features & VIRTIO_F_RING_EVENT_IDX ? 3 : 2;
size_desc = sizeof(vq->vq_desc[0]) * vq_num;
size_avail = sizeof(uint16_t) * hdrlen
+ sizeof(vq->vq_avail[0].ring[0]) * vq_num;
size_used = sizeof(uint16_t) *hdrlen
+ sizeof(vq->vq_used[0].ring[0]) * vq_num;
size_indirect = (sc->sc_indirect && maxnsegs >= MINSEG_INDIRECT) ?
sizeof(struct vring_desc) * maxnsegs * vq_num : 0;
size_desc_avail = VIRTQUEUE_ALIGN(size_desc + size_avail);
size_used = VIRTQUEUE_ALIGN(size_used);
allocsize = size_desc_avail + size_used + size_indirect;
/* alloc and map the memory */
r = bus_dmamem_alloc(sc->sc_dmat, allocsize, VIRTIO_PAGE_SIZE, 0,
&vq->vq_segs[0], 1, &rsegs, BUS_DMA_WAITOK);
if (r != 0) {
aprint_error_dev(sc->sc_dev,
"virtqueue %d for %s allocation failed, "
"error code %d\n", vq->vq_index, name, r);
goto err;
}
r = bus_dmamem_map(sc->sc_dmat, &vq->vq_segs[0], rsegs, allocsize,
&vq->vq_vaddr, BUS_DMA_WAITOK);
if (r != 0) {
aprint_error_dev(sc->sc_dev,
"virtqueue %d for %s map failed, "
"error code %d\n", vq->vq_index, name, r);
goto err;
}
r = bus_dmamap_create(sc->sc_dmat, allocsize, 1, allocsize, 0,
BUS_DMA_WAITOK, &vq->vq_dmamap);
if (r != 0) {
aprint_error_dev(sc->sc_dev,
"virtqueue %d for %s dmamap creation failed, "
"error code %d\n", vq->vq_index, name, r);
goto err;
}
r = bus_dmamap_load(sc->sc_dmat, vq->vq_dmamap,
vq->vq_vaddr, allocsize, NULL, BUS_DMA_WAITOK);
if (r != 0) {
aprint_error_dev(sc->sc_dev,
"virtqueue %d for %s dmamap load failed, "
"error code %d\n", vq->vq_index, name, r);
goto err;
}
vq->vq_bytesize = allocsize;
vq->vq_maxsegsize = maxsegsize;
vq->vq_maxnsegs = maxnsegs;
#define VIRTIO_PTR(base, offset) (void *)((intptr_t)(base) + (offset))
/* initialize vring pointers */
vq->vq_desc = VIRTIO_PTR(vq->vq_vaddr, 0);
vq->vq_availoffset = size_desc;
vq->vq_avail = VIRTIO_PTR(vq->vq_vaddr, vq->vq_availoffset);
vq->vq_used_event = VIRTIO_PTR(vq->vq_avail,
offsetof(struct vring_avail, ring[vq_num]));
vq->vq_usedoffset = size_desc_avail;
vq->vq_used = VIRTIO_PTR(vq->vq_vaddr, vq->vq_usedoffset);
vq->vq_avail_event = VIRTIO_PTR(vq->vq_used,
offsetof(struct vring_used, ring[vq_num]));
if (size_indirect > 0) {
vq->vq_indirectoffset = size_desc_avail + size_used;
vq->vq_indirect = VIRTIO_PTR(vq->vq_vaddr,
vq->vq_indirectoffset);
}
#undef VIRTIO_PTR
vq->vq_descx = kmem_zalloc(sizeof(vq->vq_descx[0]) * vq_num,
KM_SLEEP);
mutex_init(&vq->vq_freedesc_lock, MUTEX_SPIN, sc->sc_ipl);
mutex_init(&vq->vq_aring_lock, MUTEX_SPIN, sc->sc_ipl);
mutex_init(&vq->vq_uring_lock, MUTEX_SPIN, sc->sc_ipl);
virtio_reset_vq(sc, vq);
aprint_verbose_dev(sc->sc_dev,
"allocated %" PRIuBUSSIZE " byte for virtqueue %d for %s, "
"size %d\n", allocsize, vq->vq_index, name, vq_num);
if (size_indirect > 0)
aprint_verbose_dev(sc->sc_dev,
"using %" PRIuBUSSIZE " byte (%d entries) indirect "
"descriptors\n", size_indirect, maxnsegs * vq_num);
return 0;
err:
sc->sc_ops->setup_queue(sc, vq->vq_index, 0);
if (vq->vq_dmamap)
bus_dmamap_destroy(sc->sc_dmat, vq->vq_dmamap);
if (vq->vq_vaddr)
bus_dmamem_unmap(sc->sc_dmat, vq->vq_vaddr, allocsize);
if (vq->vq_segs[0].ds_addr)
bus_dmamem_free(sc->sc_dmat, &vq->vq_segs[0], 1);
memset(vq, 0, sizeof(*vq));
return -1;
}
int
virtio_free_vq(struct virtio_softc *sc, struct virtqueue *vq)
{
uint16_t s;
size_t i;
if (vq->vq_vaddr == NULL)
return 0;
/* device must be already deactivated */
/* confirm the vq is empty */
s = vq->vq_free_idx;
i = 0;
while (s != virtio_rw16(sc, VRING_DESC_CHAIN_END)) {
s = vq->vq_desc[s].next;
i++;
}
if (i != vq->vq_num) {
printf("%s: freeing non-empty vq, index %d\n",
device_xname(sc->sc_dev), vq->vq_index);
return EBUSY;
}
/* tell device that there's no virtqueue any longer */
sc->sc_ops->setup_queue(sc, vq->vq_index, 0);
vq_sync_aring_all(sc, vq, BUS_DMASYNC_POSTWRITE);
kmem_free(vq->vq_descx, sizeof(vq->vq_descx[0]) * vq->vq_num);
bus_dmamap_unload(sc->sc_dmat, vq->vq_dmamap);
bus_dmamap_destroy(sc->sc_dmat, vq->vq_dmamap);
bus_dmamem_unmap(sc->sc_dmat, vq->vq_vaddr, vq->vq_bytesize);
bus_dmamem_free(sc->sc_dmat, &vq->vq_segs[0], 1);
mutex_destroy(&vq->vq_freedesc_lock);
mutex_destroy(&vq->vq_uring_lock);
mutex_destroy(&vq->vq_aring_lock);
memset(vq, 0, sizeof(*vq));
return 0;
}
/*
* Free descriptor management.
*/
static int
vq_alloc_slot_locked(struct virtio_softc *sc, struct virtqueue *vq,
size_t nslots)
{
struct vring_desc *vd;
uint16_t head, tail;
size_t i;
KASSERT(mutex_owned(&vq->vq_freedesc_lock));
head = tail = virtio_rw16(sc, vq->vq_free_idx);
for (i = 0; i < nslots - 1; i++) {
if (tail == VRING_DESC_CHAIN_END)
return VRING_DESC_CHAIN_END;
vd = &vq->vq_desc[tail];
vd->flags = virtio_rw16(sc, VRING_DESC_F_NEXT);
tail = virtio_rw16(sc, vd->next);
}
if (tail == VRING_DESC_CHAIN_END)
return VRING_DESC_CHAIN_END;
vd = &vq->vq_desc[tail];
vd->flags = virtio_rw16(sc, 0);
vq->vq_free_idx = vd->next;
return head;
}
static uint16_t
vq_alloc_slot(struct virtio_softc *sc, struct virtqueue *vq, size_t nslots)
{
uint16_t rv;
mutex_enter(&vq->vq_freedesc_lock);
rv = vq_alloc_slot_locked(sc, vq, nslots);
mutex_exit(&vq->vq_freedesc_lock);
return rv;
}
static void
vq_free_slot(struct virtio_softc *sc, struct virtqueue *vq, uint16_t slot)
{
struct vring_desc *vd;
uint16_t s;
mutex_enter(&vq->vq_freedesc_lock);
vd = &vq->vq_desc[slot];
while ((vd->flags & virtio_rw16(sc, VRING_DESC_F_NEXT)) != 0) {
s = virtio_rw16(sc, vd->next);
vd = &vq->vq_desc[s];
}
vd->next = vq->vq_free_idx;
vq->vq_free_idx = virtio_rw16(sc, slot);
mutex_exit(&vq->vq_freedesc_lock);
}
/*
* Enqueue several dmamaps as a single request.
*/
/*
* Typical usage:
* <queue size> number of followings are stored in arrays
* - command blocks (in dmamem) should be pre-allocated and mapped
* - dmamaps for command blocks should be pre-allocated and loaded
* - dmamaps for payload should be pre-allocated
* r = virtio_enqueue_prep(sc, vq, &slot); // allocate a slot
* if (r) // currently 0 or EAGAIN
* return r;
* r = bus_dmamap_load(dmat, dmamap_payload[slot], data, count, ..);
* if (r) {
* virtio_enqueue_abort(sc, vq, slot);
* return r;
* }
* r = virtio_enqueue_reserve(sc, vq, slot,
* dmamap_payload[slot]->dm_nsegs + 1);
* // ^ +1 for command
* if (r) { // currently 0 or EAGAIN
* bus_dmamap_unload(dmat, dmamap_payload[slot]);
* return r; // do not call abort()
* }
* <setup and prepare commands>
* bus_dmamap_sync(dmat, dmamap_cmd[slot],... BUS_DMASYNC_PREWRITE);
* bus_dmamap_sync(dmat, dmamap_payload[slot],...);
* virtio_enqueue(sc, vq, slot, dmamap_cmd[slot], false);
* virtio_enqueue(sc, vq, slot, dmamap_payload[slot], iswrite);
* virtio_enqueue_commit(sc, vq, slot, true);
*/
/*
* enqueue_prep: allocate a slot number
*/
int
virtio_enqueue_prep(struct virtio_softc *sc, struct virtqueue *vq, int *slotp)
{
uint16_t slot;
KASSERT(slotp != NULL);
slot = vq_alloc_slot(sc, vq, 1);
if (slot == VRING_DESC_CHAIN_END)
return EAGAIN;
*slotp = slot;
return 0;
}
/*
* enqueue_reserve: allocate remaining slots and build the descriptor chain.
*/
int
virtio_enqueue_reserve(struct virtio_softc *sc, struct virtqueue *vq,
int slot, int nsegs)
{
struct vring_desc *vd;
struct vring_desc_extra *vdx;
int i;
KASSERT(1 <= nsegs && nsegs <= vq->vq_num);
vdx = &vq->vq_descx[slot];
vd = &vq->vq_desc[slot];
KASSERT((vd->flags & virtio_rw16(sc, VRING_DESC_F_NEXT)) == 0);
if ((vq->vq_indirect != NULL) &&
(nsegs >= MINSEG_INDIRECT) &&
(nsegs <= vq->vq_maxnsegs))
vdx->use_indirect = true;
else
vdx->use_indirect = false;
if (vdx->use_indirect) {
uint64_t addr;
addr = vq->vq_dmamap->dm_segs[0].ds_addr
+ vq->vq_indirectoffset;
addr += sizeof(struct vring_desc)
* vq->vq_maxnsegs * slot;
vd->addr = virtio_rw64(sc, addr);
vd->len = virtio_rw32(sc, sizeof(struct vring_desc) * nsegs);
vd->flags = virtio_rw16(sc, VRING_DESC_F_INDIRECT);
vd = &vq->vq_indirect[vq->vq_maxnsegs * slot];
vdx->desc_base = vd;
vdx->desc_free_idx = 0;
for (i = 0; i < nsegs - 1; i++) {
vd[i].flags = virtio_rw16(sc, VRING_DESC_F_NEXT);
}
vd[i].flags = virtio_rw16(sc, 0);
} else {
if (nsegs > 1) {
uint16_t s;
s = vq_alloc_slot(sc, vq, nsegs - 1);
if (s == VRING_DESC_CHAIN_END) {
vq_free_slot(sc, vq, slot);
return EAGAIN;
}
vd->next = virtio_rw16(sc, s);
vd->flags = virtio_rw16(sc, VRING_DESC_F_NEXT);
}
vdx->desc_base = &vq->vq_desc[0];
vdx->desc_free_idx = slot;
}
return 0;
}
/*
* enqueue: enqueue a single dmamap.
*/
int
virtio_enqueue(struct virtio_softc *sc, struct virtqueue *vq, int slot,
bus_dmamap_t dmamap, bool write)
{
struct vring_desc *vds;
struct vring_desc_extra *vdx;
uint16_t s;
int i;
KASSERT(dmamap->dm_nsegs > 0);
vdx = &vq->vq_descx[slot];
vds = vdx->desc_base;
s = vdx->desc_free_idx;
KASSERT(vds != NULL);
for (i = 0; i < dmamap->dm_nsegs; i++) {
KASSERT(s != VRING_DESC_CHAIN_END);
vds[s].addr = virtio_rw64(sc, dmamap->dm_segs[i].ds_addr);
vds[s].len = virtio_rw32(sc, dmamap->dm_segs[i].ds_len);
if (!write)
vds[s].flags |= virtio_rw16(sc, VRING_DESC_F_WRITE);
if ((vds[s].flags & virtio_rw16(sc, VRING_DESC_F_NEXT)) == 0) {
s = VRING_DESC_CHAIN_END;
} else {
s = virtio_rw16(sc, vds[s].next);
}
}
vdx->desc_free_idx = s;
return 0;
}
int
virtio_enqueue_p(struct virtio_softc *sc, struct virtqueue *vq, int slot,
bus_dmamap_t dmamap, bus_addr_t start, bus_size_t len,
bool write)
{
struct vring_desc_extra *vdx;
struct vring_desc *vds;
uint16_t s;
vdx = &vq->vq_descx[slot];
vds = vdx->desc_base;
s = vdx->desc_free_idx;
KASSERT(s != VRING_DESC_CHAIN_END);
KASSERT(vds != NULL);
KASSERT(dmamap->dm_nsegs == 1); /* XXX */
KASSERT(dmamap->dm_segs[0].ds_len > start);
KASSERT(dmamap->dm_segs[0].ds_len >= start + len);
vds[s].addr = virtio_rw64(sc, dmamap->dm_segs[0].ds_addr + start);
vds[s].len = virtio_rw32(sc, len);
if (!write)
vds[s].flags |= virtio_rw16(sc, VRING_DESC_F_WRITE);
if ((vds[s].flags & virtio_rw16(sc, VRING_DESC_F_NEXT)) == 0) {
s = VRING_DESC_CHAIN_END;
} else {
s = virtio_rw16(sc, vds[s].next);
}
vdx->desc_free_idx = s;
return 0;
}
/*
* enqueue_commit: add it to the aring.
*/
int
virtio_enqueue_commit(struct virtio_softc *sc, struct virtqueue *vq, int slot,
bool notifynow)
{
if (slot < 0) {
mutex_enter(&vq->vq_aring_lock);
goto notify;
}
vq_sync_descs(sc, vq, BUS_DMASYNC_PREWRITE);
if (vq->vq_descx[slot].use_indirect)
vq_sync_indirect(sc, vq, slot, BUS_DMASYNC_PREWRITE);
mutex_enter(&vq->vq_aring_lock);
vq->vq_avail->ring[(vq->vq_avail_idx++) % vq->vq_num] =
virtio_rw16(sc, slot);
notify:
if (notifynow) {
uint16_t o, n, t;
uint16_t flags;
o = virtio_rw16(sc, vq->vq_avail->idx) - 1;
n = vq->vq_avail_idx;
/*
* Prepare for `device->CPU' (host->guest) transfer
* into the buffer. This must happen before we commit
* the vq->vq_avail->idx update to ensure we're not
* still using the buffer in case program-prior loads
* or stores in it get delayed past the store to
* vq->vq_avail->idx.
*/
vq_sync_uring_all(sc, vq, BUS_DMASYNC_PREREAD);
/* ensure payload is published, then avail idx */
vq_sync_aring_payload(sc, vq, BUS_DMASYNC_PREWRITE);
vq->vq_avail->idx = virtio_rw16(sc, vq->vq_avail_idx);
vq_sync_aring_header(sc, vq, BUS_DMASYNC_PREWRITE);
vq->vq_queued++;
if (sc->sc_active_features & VIRTIO_F_RING_EVENT_IDX) {
vq_sync_uring_avail(sc, vq, BUS_DMASYNC_POSTREAD);
t = virtio_rw16(sc, *vq->vq_avail_event) + 1;
if ((uint16_t) (n - t) < (uint16_t) (n - o))
sc->sc_ops->kick(sc, vq->vq_index);
} else {
vq_sync_uring_header(sc, vq, BUS_DMASYNC_POSTREAD);
flags = virtio_rw16(sc, vq->vq_used->flags);
if (!(flags & VRING_USED_F_NO_NOTIFY))
sc->sc_ops->kick(sc, vq->vq_index);
}
}
mutex_exit(&vq->vq_aring_lock);
return 0;
}
/*
* enqueue_abort: rollback.
*/
int
virtio_enqueue_abort(struct virtio_softc *sc, struct virtqueue *vq, int slot)
{
struct vring_desc_extra *vdx;
vdx = &vq->vq_descx[slot];
vdx->desc_free_idx = VRING_DESC_CHAIN_END;
vdx->desc_base = NULL;
vq_free_slot(sc, vq, slot);
return 0;
}
/*
* Dequeue a request.
*/
/*
* dequeue: dequeue a request from uring; dmamap_sync for uring is
* already done in the interrupt handler.
*/
int
virtio_dequeue(struct virtio_softc *sc, struct virtqueue *vq,
int *slotp, int *lenp)
{
uint16_t slot, usedidx;
if (vq->vq_used_idx == virtio_rw16(sc, vq->vq_used->idx))
return ENOENT;
mutex_enter(&vq->vq_uring_lock);
usedidx = vq->vq_used_idx++;
mutex_exit(&vq->vq_uring_lock);
usedidx %= vq->vq_num;
slot = virtio_rw32(sc, vq->vq_used->ring[usedidx].id);
if (vq->vq_descx[slot].use_indirect)
vq_sync_indirect(sc, vq, slot, BUS_DMASYNC_POSTWRITE);
if (slotp)
*slotp = slot;
if (lenp)
*lenp = virtio_rw32(sc, vq->vq_used->ring[usedidx].len);
return 0;
}
/*
* dequeue_commit: complete dequeue; the slot is recycled for future use.
* if you forget to call this the slot will be leaked.
*/
int
virtio_dequeue_commit(struct virtio_softc *sc, struct virtqueue *vq, int slot)
{
struct vring_desc_extra *vdx;
vdx = &vq->vq_descx[slot];
vdx->desc_base = NULL;
vdx->desc_free_idx = VRING_DESC_CHAIN_END;
vq_free_slot(sc, vq, slot);
return 0;
}
/*
* Attach a child, fill all the members.
*/
void
virtio_child_attach_start(struct virtio_softc *sc, device_t child, int ipl,
uint64_t req_features, const char *feat_bits)
{
char buf[1024];
KASSERT(sc->sc_child == NULL);
KASSERT(sc->sc_child_state == VIRTIO_NO_CHILD);
sc->sc_child = child;
sc->sc_ipl = ipl;
virtio_negotiate_features(sc, req_features);
snprintb(buf, sizeof(buf), feat_bits, sc->sc_active_features);
aprint_normal(": features: %s\n", buf);
aprint_naive("\n");
}
int
virtio_child_attach_finish(struct virtio_softc *sc,
struct virtqueue *vqs, size_t nvqs,
virtio_callback config_change,
int req_flags)
{
size_t i;
int r;
#ifdef DIAGNOSTIC
KASSERT(nvqs > 0);
#define VIRTIO_ASSERT_FLAGS (VIRTIO_F_INTR_SOFTINT | VIRTIO_F_INTR_PERVQ)
KASSERT((req_flags & VIRTIO_ASSERT_FLAGS) != VIRTIO_ASSERT_FLAGS);
#undef VIRTIO_ASSERT_FLAGS
for (i = 0; i < nvqs; i++){
KASSERT(vqs[i].vq_index == i);
KASSERT(vqs[i].vq_intrhand != NULL);
KASSERT(vqs[i].vq_done == NULL ||
vqs[i].vq_intrhand == virtio_vq_done);
}
#endif
sc->sc_vqs = vqs;
sc->sc_nvqs = nvqs;
sc->sc_config_change = config_change;
sc->sc_intrhand = virtio_vq_intr;
sc->sc_flags = req_flags;
/* set the vq address */
for (i = 0; i < nvqs; i++) {
sc->sc_ops->setup_queue(sc, vqs[i].vq_index,
vqs[i].vq_dmamap->dm_segs[0].ds_addr);
}
r = sc->sc_ops->alloc_interrupts(sc);
if (r != 0) {
aprint_error_dev(sc->sc_dev,
"failed to allocate interrupts\n");
goto fail;
}
r = sc->sc_ops->setup_interrupts(sc, 0);
if (r != 0) {
aprint_error_dev(sc->sc_dev, "failed to setup interrupts\n");
goto fail;
}
KASSERT(sc->sc_soft_ih == NULL);
if (sc->sc_flags & VIRTIO_F_INTR_SOFTINT) {
u_int flags = SOFTINT_NET;
if (sc->sc_flags & VIRTIO_F_INTR_MPSAFE)
flags |= SOFTINT_MPSAFE;
sc->sc_soft_ih = softint_establish(flags, virtio_soft_intr,
sc);
if (sc->sc_soft_ih == NULL) {
sc->sc_ops->free_interrupts(sc);
aprint_error_dev(sc->sc_dev,
"failed to establish soft interrupt\n");
goto fail;
}
}
sc->sc_child_state = VIRTIO_CHILD_ATTACH_FINISHED;
virtio_set_status(sc, VIRTIO_CONFIG_DEVICE_STATUS_DRIVER_OK);
return 0;
fail:
if (sc->sc_soft_ih) {
softint_disestablish(sc->sc_soft_ih);
sc->sc_soft_ih = NULL;
}
sc->sc_ops->free_interrupts(sc);
virtio_set_status(sc, VIRTIO_CONFIG_DEVICE_STATUS_FAILED);
return 1;
}
void
virtio_child_detach(struct virtio_softc *sc)
{
/* already detached */
if (sc->sc_child == NULL)
return;
virtio_device_reset(sc);
sc->sc_ops->free_interrupts(sc);
if (sc->sc_soft_ih) {
softint_disestablish(sc->sc_soft_ih);
sc->sc_soft_ih = NULL;
}
sc->sc_vqs = NULL;
sc->sc_child = NULL;
}
void
virtio_child_attach_failed(struct virtio_softc *sc)
{
virtio_child_detach(sc);
virtio_set_status(sc, VIRTIO_CONFIG_DEVICE_STATUS_FAILED);
sc->sc_child_state = VIRTIO_CHILD_ATTACH_FAILED;
}
bus_dma_tag_t
virtio_dmat(struct virtio_softc *sc)
{
return sc->sc_dmat;
}
device_t
virtio_child(struct virtio_softc *sc)
{
return sc->sc_child;
}
int
virtio_intrhand(struct virtio_softc *sc)
{
return (*sc->sc_intrhand)(sc);
}
uint64_t
virtio_features(struct virtio_softc *sc)
{
return sc->sc_active_features;
}
int
virtio_attach_failed(struct virtio_softc *sc)
{
device_t self = sc->sc_dev;
/* no error if its not connected, but its failed */
if (sc->sc_childdevid == 0)
return 1;
if (sc->sc_child == NULL) {
switch (sc->sc_child_state) {
case VIRTIO_CHILD_ATTACH_FAILED:
aprint_error_dev(self,
"virtio configuration failed\n");
break;
case VIRTIO_NO_CHILD:
aprint_error_dev(self,
"no matching child driver; not configured\n");
break;
default:
/* sanity check */
aprint_error_dev(self,
"virtio internal error, "
"child driver is not configured\n");
break;
}
return 1;
}
/* sanity check */
if (sc->sc_child_state != VIRTIO_CHILD_ATTACH_FINISHED) {
aprint_error_dev(self, "virtio internal error, child driver "
"signaled OK but didn't initialize interrupts\n");
return 1;
}
return 0;
}
void
virtio_print_device_type(device_t self, int id, int revision)
{
aprint_normal_dev(self, "%s device (id %d, rev. 0x%02x)\n",
(id < NDEVNAMES ? virtio_device_name[id] : "Unknown"),
id,
revision);
}
MODULE(MODULE_CLASS_DRIVER, virtio, NULL);
#ifdef _MODULE
#include "ioconf.c"
#endif
static int
virtio_modcmd(modcmd_t cmd, void *opaque)
{
int error = 0;
#ifdef _MODULE
switch (cmd) {
case MODULE_CMD_INIT:
error = config_init_component(cfdriver_ioconf_virtio,
cfattach_ioconf_virtio, cfdata_ioconf_virtio);
break;
case MODULE_CMD_FINI:
error = config_fini_component(cfdriver_ioconf_virtio,
cfattach_ioconf_virtio, cfdata_ioconf_virtio);
break;
default:
error = ENOTTY;
break;
}
#endif
return error;
}
|