forked from xl0/illumos-virtio
/
virtio.c
1455 lines (1214 loc) · 36.5 KB
/
virtio.c
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
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2011 Nexenta Systems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Based on the NetBSD virtio driver by Minoura Makoto. */
/*
* 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/conf.h>
#include <sys/kmem.h>
#include <sys/debug.h>
#include <sys/modctl.h>
#include <sys/autoconf.h>
#include <sys/ddi_impldefs.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/sunndi.h>
#include <sys/avintr.h>
#include <sys/spl.h>
#include <sys/promif.h>
#include <sys/list.h>
#include <sys/bootconf.h>
#include <sys/bootsvcs.h>
#include <sys/sysmacros.h>
#include <sys/pci.h>
#include "util.h"
#include "virtiovar.h"
#include "virtioreg.h"
#define NDEVNAMES (sizeof (virtio_device_name) / sizeof (char *))
#define MINSEG_INDIRECT 2 /* use indirect if nsegs >= this value */
void
virtio_set_status(struct virtio_softc *sc, unsigned int status)
{
int old = 0;
if (status != 0)
old = ddi_get8(sc->sc_ioh,
(uint8_t *)(sc->sc_io_addr +
VIRTIO_CONFIG_DEVICE_STATUS));
ddi_put8(sc->sc_ioh,
(uint8_t *)(sc->sc_io_addr + VIRTIO_CONFIG_DEVICE_STATUS),
status | old);
}
/*
* Negotiate features, save the result in sc->sc_features
*/
uint32_t
virtio_negotiate_features(struct virtio_softc *sc, uint32_t guest_features)
{
uint32_t host_features;
uint32_t features;
host_features = ddi_get32(sc->sc_ioh,
/* LINTED E_BAD_PTR_CAST_ALIGN */
(uint32_t *)(sc->sc_io_addr + VIRTIO_CONFIG_DEVICE_FEATURES));
dev_err(sc->sc_dev, CE_NOTE, "host features: %x, guest features: %x",
host_features, guest_features);
features = host_features & guest_features;
ddi_put32(sc->sc_ioh,
/* LINTED E_BAD_PTR_CAST_ALIGN */
(uint32_t *)(sc->sc_io_addr + VIRTIO_CONFIG_GUEST_FEATURES),
features);
sc->sc_features = features;
return (host_features);
}
size_t
virtio_show_features(uint32_t features,
char *buf, size_t len)
{
char *orig_buf = buf;
char *bufend = buf + len;
/* LINTED E_PTRDIFF_OVERFLOW */
buf += snprintf(buf, bufend - buf, "Generic ( ");
if (features & VIRTIO_F_RING_INDIRECT_DESC)
/* LINTED E_PTRDIFF_OVERFLOW */
buf += snprintf(buf, bufend - buf, "INDIRECT_DESC ");
/* LINTED E_PTRDIFF_OVERFLOW */
buf += snprintf(buf, bufend - buf, ") ");
/* LINTED E_PTRDIFF_OVERFLOW */
return (buf - orig_buf);
}
boolean_t
virtio_has_feature(struct virtio_softc *sc, uint32_t feature)
{
return (sc->sc_features & feature);
}
/*
* Device configuration registers.
*/
uint8_t
virtio_read_device_config_1(struct virtio_softc *sc, unsigned int index)
{
ASSERT(sc->sc_config_offset);
return ddi_get8(sc->sc_ioh,
(uint8_t *)(sc->sc_io_addr + sc->sc_config_offset + index));
}
uint16_t
virtio_read_device_config_2(struct virtio_softc *sc, unsigned int index)
{
ASSERT(sc->sc_config_offset);
return ddi_get16(sc->sc_ioh,
/* LINTED E_BAD_PTR_CAST_ALIGN */
(uint16_t *)(sc->sc_io_addr + sc->sc_config_offset + index));
}
uint32_t
virtio_read_device_config_4(struct virtio_softc *sc, unsigned int index)
{
ASSERT(sc->sc_config_offset);
return ddi_get32(sc->sc_ioh,
/* LINTED E_BAD_PTR_CAST_ALIGN */
(uint32_t *)(sc->sc_io_addr + sc->sc_config_offset + index));
}
uint64_t
virtio_read_device_config_8(struct virtio_softc *sc, unsigned int index)
{
uint64_t r;
ASSERT(sc->sc_config_offset);
r = ddi_get32(sc->sc_ioh,
/* LINTED E_BAD_PTR_CAST_ALIGN */
(uint32_t *)(sc->sc_io_addr + sc->sc_config_offset +
index + sizeof (uint32_t)));
r <<= 32;
r += ddi_get32(sc->sc_ioh,
/* LINTED E_BAD_PTR_CAST_ALIGN */
(uint32_t *)(sc->sc_io_addr + sc->sc_config_offset + index));
return (r);
}
void
virtio_write_device_config_1(struct virtio_softc *sc,
unsigned int index, uint8_t value)
{
ASSERT(sc->sc_config_offset);
ddi_put8(sc->sc_ioh,
(uint8_t *)(sc->sc_io_addr + sc->sc_config_offset + index), value);
}
void
virtio_write_device_config_2(struct virtio_softc *sc,
unsigned int index, uint16_t value)
{
ASSERT(sc->sc_config_offset);
ddi_put16(sc->sc_ioh,
/* LINTED E_BAD_PTR_CAST_ALIGN */
(uint16_t *)(sc->sc_io_addr + sc->sc_config_offset + index), value);
}
void
virtio_write_device_config_4(struct virtio_softc *sc,
unsigned int index, uint32_t value)
{
ASSERT(sc->sc_config_offset);
ddi_put32(sc->sc_ioh,
/* LINTED E_BAD_PTR_CAST_ALIGN */
(uint32_t *)(sc->sc_io_addr + sc->sc_config_offset + index), value);
}
void
virtio_write_device_config_8(struct virtio_softc *sc,
unsigned int index, uint64_t value)
{
ASSERT(sc->sc_config_offset);
ddi_put32(sc->sc_ioh,
/* LINTED E_BAD_PTR_CAST_ALIGN */
(uint32_t *)(sc->sc_io_addr + sc->sc_config_offset + index),
value & 0xFFFFFFFF);
ddi_put32(sc->sc_ioh,
/* LINTED E_BAD_PTR_CAST_ALIGN */
(uint32_t *)(sc->sc_io_addr + sc->sc_config_offset +
index + sizeof (uint32_t)), value >> 32);
}
/*
* Start/stop vq interrupt. No guarantee.
*/
void
virtio_stop_vq_intr(struct virtqueue *vq)
{
vq->vq_avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
}
void
virtio_start_vq_intr(struct virtqueue *vq)
{
vq->vq_avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
}
static ddi_dma_attr_t virtio_vq_dma_attr = {
DMA_ATTR_V0, /* Version number */
0, /* low address */
/*
* high address. Has to fit into 32 bits
* after page-shifting
*/
0x00000FFFFFFFFFFF,
0xFFFFFFFF, /* counter register max */
VIRTIO_PAGE_SIZE, /* page alignment required */
0x3F, /* burst sizes: 1 - 32 */
0x1, /* minimum transfer size */
0xFFFFFFFF, /* max transfer size */
0xFFFFFFFF, /* address register max */
1, /* no scatter-gather */
1, /* device operates on bytes */
0, /* attr flag: set to 0 */
};
static ddi_dma_attr_t virtio_vq_indirect_dma_attr = {
DMA_ATTR_V0, /* Version number */
0, /* low address */
0xFFFFFFFFFFFFFFFF, /* high address */
0xFFFFFFFF, /* counter register max */
1, /* No specific alignment */
0x3F, /* burst sizes: 1 - 32 */
0x1, /* minimum transfer size */
0xFFFFFFFF, /* max transfer size */
0xFFFFFFFF, /* address register max */
1, /* no scatter-gather */
1, /* device operates on bytes */
0, /* attr flag: set to 0 */
};
/* Same for direct and indirect descriptors. */
static ddi_device_acc_attr_t virtio_vq_devattr = {
DDI_DEVICE_ATTR_V0,
DDI_NEVERSWAP_ACC,
DDI_STORECACHING_OK_ACC,
DDI_DEFAULT_ACC
};
static void
virtio_free_indirect(struct vq_entry *entry)
{
(void) ddi_dma_unbind_handle(entry->qe_indirect_dma_handle);
ddi_dma_mem_free(&entry->qe_indirect_dma_acch);
ddi_dma_free_handle(&entry->qe_indirect_dma_handle);
entry->qe_indirect_descs = NULL;
}
static int
virtio_alloc_indirect(struct virtio_softc *sc, struct vq_entry *entry)
{
int allocsize, num;
size_t len;
unsigned int ncookies;
int ret;
num = entry->qe_queue->vq_indirect_num;
ASSERT(num > 1);
allocsize = sizeof (struct vring_desc) * num;
ret = ddi_dma_alloc_handle(sc->sc_dev, &virtio_vq_indirect_dma_attr,
DDI_DMA_SLEEP, NULL, &entry->qe_indirect_dma_handle);
if (ret) {
dev_err(sc->sc_dev, CE_WARN,
"Failed to allocate dma handle for indirect descritpors,"
" entry %d, vq %d", entry->qe_index,
entry->qe_queue->vq_index);
goto out_alloc_handle;
}
ret = ddi_dma_mem_alloc(entry->qe_indirect_dma_handle,
allocsize, &virtio_vq_devattr,
DDI_DMA_CONSISTENT, DDI_DMA_SLEEP, NULL,
(caddr_t *)&entry->qe_indirect_descs, &len,
&entry->qe_indirect_dma_acch);
if (ret) {
dev_err(sc->sc_dev, CE_WARN,
"Failed to alocate dma memory for indirect descriptors,"
" entry %d, vq %d,", entry->qe_index,
entry->qe_queue->vq_index);
goto out_alloc;
}
(void) memset(entry->qe_indirect_descs, 0xff, allocsize);
ret = ddi_dma_addr_bind_handle(entry->qe_indirect_dma_handle, NULL,
(caddr_t)entry->qe_indirect_descs, len,
DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
DDI_DMA_SLEEP, NULL, &entry->qe_indirect_dma_cookie, &ncookies);
if (ret != DDI_DMA_MAPPED) {
dev_err(sc->sc_dev, CE_WARN,
"Failed to bind dma memory for indirect descriptrors,"
"entry %d, vq %d", entry->qe_index,
entry->qe_queue->vq_index);
goto out_bind;
}
/* We asked for a single segment */
ASSERT(ncookies == 1);
return (0);
out_bind:
ddi_dma_mem_free(&entry->qe_indirect_dma_acch);
out_alloc:
ddi_dma_free_handle(&entry->qe_indirect_dma_handle);
out_alloc_handle:
return (ret);
}
/*
* Initialize the vq structure.
*/
static int
virtio_init_vq(struct virtio_softc *sc, struct virtqueue *vq)
{
int ret;
uint16_t i;
int vq_size = vq->vq_num;
int indirect_num = vq->vq_indirect_num;
/* free slot management */
list_create(&vq->vq_freelist, sizeof (struct vq_entry),
offsetof(struct vq_entry, qe_list));
for (i = 0; i < vq_size; i++) {
struct vq_entry *entry = &vq->vq_entries[i];
list_insert_tail(&vq->vq_freelist, entry);
entry->qe_index = i;
entry->qe_desc = &vq->vq_descs[i];
entry->qe_queue = vq;
entry->qe_guard1 = QE_POISON1_FREE;
entry->qe_guard2 = QE_POISON2_FREE;
if (indirect_num) {
ret = virtio_alloc_indirect(sc, entry);
if (ret)
goto out_indirect;
}
}
mutex_init(&vq->vq_freelist_lock, "virtio-freelist",
MUTEX_DRIVER, DDI_INTR_PRI(sc->sc_intr_prio));
mutex_init(&vq->vq_avail_lock, "virtio-avail",
MUTEX_DRIVER, DDI_INTR_PRI(sc->sc_intr_prio));
mutex_init(&vq->vq_used_lock, "virtio-used",
MUTEX_DRIVER, DDI_INTR_PRI(sc->sc_intr_prio));
return (0);
out_indirect:
for (i = 0; i < vq_size; i++) {
struct vq_entry *entry = &vq->vq_entries[i];
if (entry->qe_indirect_descs)
virtio_free_indirect(entry);
}
return (ret);
}
/*
* Allocate/free a vq.
*/
struct virtqueue *
virtio_alloc_vq(struct virtio_softc *sc,
unsigned int index,
unsigned int size,
unsigned int indirect_num,
const char *name)
{
int vq_size, allocsize1, allocsize2, allocsize = 0;
int r;
unsigned int ncookies;
size_t len;
struct virtqueue *vq;
#define VIRTQUEUE_ALIGN(n) (((n)+(VIRTIO_PAGE_SIZE-1)) & \
~(VIRTIO_PAGE_SIZE-1))
ddi_put16(sc->sc_ioh,
/* LINTED E_BAD_PTR_CAST_ALIGN */
(uint16_t *)(sc->sc_io_addr + VIRTIO_CONFIG_QUEUE_SELECT), index);
vq_size = ddi_get16(sc->sc_ioh,
/* LINTED E_BAD_PTR_CAST_ALIGN */
(uint16_t *)(sc->sc_io_addr + VIRTIO_CONFIG_QUEUE_SIZE));
if (vq_size == 0) {
dev_err(sc->sc_dev, CE_WARN,
"virtqueue dest not exist, index %d for %s\n", index, name);
goto out;
}
vq = kmem_zalloc(sizeof (struct virtqueue), KM_SLEEP);
if (!vq)
goto out;
/* size 0 => use native vq size, good for receive queues. */
if (size)
vq_size = MIN(vq_size, size);
/* allocsize1: descriptor table + avail ring + pad */
allocsize1 = VIRTQUEUE_ALIGN(sizeof (struct vring_desc) * vq_size +
sizeof (struct vring_avail) +
sizeof (uint16_t) * vq_size);
/* allocsize2: used ring + pad */
allocsize2 = VIRTQUEUE_ALIGN(sizeof (struct vring_used)
+ sizeof (struct vring_used_elem) * vq_size);
allocsize = allocsize1 + allocsize2;
r = ddi_dma_alloc_handle(sc->sc_dev, &virtio_vq_dma_attr,
DDI_DMA_SLEEP, NULL, &vq->vq_dma_handle);
if (r) {
dev_err(sc->sc_dev, CE_WARN,
"Failed to allocate dma handle for vq %d", index);
goto out_alloc_handle;
}
r = ddi_dma_mem_alloc(vq->vq_dma_handle, allocsize, &virtio_vq_devattr,
DDI_DMA_CONSISTENT, DDI_DMA_SLEEP, NULL,
(caddr_t *)&vq->vq_vaddr, &len, &vq->vq_dma_acch);
if (r) {
dev_err(sc->sc_dev, CE_WARN,
"Failed to alocate dma memory for vq %d", index);
goto out_alloc;
}
r = ddi_dma_addr_bind_handle(vq->vq_dma_handle, NULL,
(caddr_t)vq->vq_vaddr, len,
DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
DDI_DMA_SLEEP, NULL, &vq->vq_dma_cookie, &ncookies);
if (r != DDI_DMA_MAPPED) {
dev_err(sc->sc_dev, CE_WARN,
"Failed to bind dma memory for vq %d", index);
goto out_bind;
}
(void) memset(vq->vq_vaddr, 0, allocsize);
(void) ddi_dma_sync(vq->vq_dma_handle, 0,
allocsize, DDI_DMA_SYNC_FORDEV);
/* We asked for a single segment */
ASSERT(ncookies == 1);
/* and page-ligned buffers. */
ASSERT(vq->vq_dma_cookie.dmac_laddress % VIRTIO_PAGE_SIZE == 0);
/* set the vq address */
ddi_put32(sc->sc_ioh,
/* LINTED E_BAD_PTR_CAST_ALIGN */
(uint32_t *)(sc->sc_io_addr + VIRTIO_CONFIG_QUEUE_ADDRESS),
(vq->vq_dma_cookie.dmac_laddress / VIRTIO_PAGE_SIZE));
/* remember addresses and offsets for later use */
vq->vq_owner = sc;
vq->vq_num = vq_size;
vq->vq_index = index;
vq->vq_descs = vq->vq_vaddr;
vq->vq_availoffset = sizeof (struct vring_desc)*vq_size;
vq->vq_avail = (void *)(((char *)vq->vq_descs) + vq->vq_availoffset);
vq->vq_usedoffset = allocsize1;
vq->vq_used = (void *)(((char *)vq->vq_descs) + vq->vq_usedoffset);
ASSERT(indirect_num == 0 ||
virtio_has_feature(sc, VIRTIO_F_RING_INDIRECT_DESC));
vq->vq_indirect_num = indirect_num;
/* free slot management */
vq->vq_entries = kmem_zalloc(sizeof (struct vq_entry) * vq_size,
KM_SLEEP);
if (!vq->vq_entries) {
dev_err(sc->sc_dev, CE_NOTE,
"Failed to allocate slot array for vq %d", index);
goto out_zalloc;
}
r = virtio_init_vq(sc, vq);
if (r)
goto out_init;
dev_err(sc->sc_dev, CE_NOTE,
"Allocated %d entries for vq %d:%s (%d incdirect descs)",
vq_size, index, name, indirect_num * vq_size);
return (vq);
out_init:
kmem_free(vq->vq_entries, sizeof (struct vq_entry) * vq_size);
out_zalloc:
(void) ddi_dma_unbind_handle(vq->vq_dma_handle);
out_bind:
ddi_dma_mem_free(&vq->vq_dma_acch);
out_alloc:
ddi_dma_free_handle(&vq->vq_dma_handle);
out_alloc_handle:
kmem_free(vq, sizeof (struct virtqueue));
out:
return (NULL);
}
void
virtio_free_vq(struct virtqueue *vq)
{
struct virtio_softc *sc = vq->vq_owner;
int i;
/* tell device that there's no virtqueue any longer */
ddi_put16(sc->sc_ioh,
/* LINTED E_BAD_PTR_CAST_ALIGN */
(uint16_t *)(sc->sc_io_addr + VIRTIO_CONFIG_QUEUE_SELECT),
vq->vq_index);
ddi_put32(sc->sc_ioh,
/* LINTED E_BAD_PTR_CAST_ALIGN */
(uint32_t *)(sc->sc_io_addr + VIRTIO_CONFIG_QUEUE_ADDRESS), 0);
/* Free the indirect descriptors, if any. */
for (i = 0; i < vq->vq_num; i++) {
struct vq_entry *entry = &vq->vq_entries[i];
if (entry->qe_indirect_descs)
virtio_free_indirect(entry);
}
kmem_free(vq->vq_entries, sizeof (struct vq_entry) * vq->vq_num);
(void) ddi_dma_unbind_handle(vq->vq_dma_handle);
ddi_dma_mem_free(&vq->vq_dma_acch);
ddi_dma_free_handle(&vq->vq_dma_handle);
mutex_destroy(&vq->vq_used_lock);
mutex_destroy(&vq->vq_avail_lock);
mutex_destroy(&vq->vq_freelist_lock);
kmem_free(vq, sizeof (struct virtqueue));
}
/*
* Free descriptor management.
*/
struct vq_entry *
vq_alloc_entry(struct virtqueue *vq)
{
struct vq_entry *qe;
mutex_enter(&vq->vq_freelist_lock);
if (list_is_empty(&vq->vq_freelist)) {
mutex_exit(&vq->vq_freelist_lock);
return (NULL);
}
qe = list_remove_head(&vq->vq_freelist);
ASSERT(vq->vq_used_entries >= 0);
vq->vq_used_entries++;
mutex_exit(&vq->vq_freelist_lock);
ASSERT(qe->qe_guard1 == QE_POISON1_FREE);
ASSERT(qe->qe_guard2 == QE_POISON2_FREE);
qe->qe_guard1 = QE_POISON1_USED;
qe->qe_guard2 = QE_POISON2_USED;
qe->qe_next = NULL;
qe->qe_indirect_next = 0;
(void) memset(qe->qe_desc, 0, sizeof (struct vring_desc));
return (qe);
}
void
vq_free_entry(struct virtqueue *vq, struct vq_entry *qe)
{
mutex_enter(&vq->vq_freelist_lock);
ASSERT(qe->qe_guard1 == QE_POISON1_USED);
ASSERT(qe->qe_guard2 == QE_POISON2_USED);
qe->qe_guard1 = QE_POISON1_FREE;
qe->qe_guard2 = QE_POISON2_FREE;
list_insert_head(&vq->vq_freelist, qe);
vq->vq_used_entries--;
ASSERT(vq->vq_used_entries >= 0);
mutex_exit(&vq->vq_freelist_lock);
}
/*
* We (intentionally) don't have a global vq mutex, so you are
* responsible for external locking to avoid allocting/freeing any
* entries before using the returned value. Have fun.
*/
uint_t
vq_num_used(struct virtqueue *vq)
{
/* vq->vq_freelist_lock would not help here. */
return (vq->vq_used_entries);
}
static inline void
virtio_ve_set_desc(struct vring_desc *desc, uint64_t paddr, uint32_t len,
boolean_t write)
{
desc->addr = paddr;
desc->len = len;
desc->next = 0;
desc->flags = 0;
/* 'write' - from the driver's point of view */
if (!write)
desc->flags = VRING_DESC_F_WRITE;
}
void
virtio_ve_set(struct vq_entry *qe, uint64_t paddr, uint32_t len,
boolean_t write)
{
virtio_ve_set_desc(qe->qe_desc, paddr, len, write);
}
void
virtio_ve_add_indirect_buf(struct vq_entry *qe, uint64_t paddr, uint32_t len,
boolean_t write)
{
struct vring_desc *indirect_desc;
ASSERT(qe->qe_queue->vq_indirect_num);
ASSERT(qe->qe_indirect_next <= qe->qe_queue->vq_indirect_num);
indirect_desc = &qe->qe_indirect_descs[qe->qe_indirect_next];
virtio_ve_set_desc(indirect_desc, paddr, len, write);
qe->qe_indirect_next++;
}
void
virtio_ve_add_cookie(struct vq_entry *qe, ddi_dma_handle_t dma_handle,
ddi_dma_cookie_t dma_cookie, unsigned int ncookies, boolean_t write)
{
int i;
for (i = 0; i < ncookies; i++) {
virtio_ve_add_indirect_buf(qe, dma_cookie.dmac_laddress,
dma_cookie.dmac_size, write);
ddi_dma_nextcookie(dma_handle, &dma_cookie);
}
}
static void
virtio_notify(struct virtqueue *vq)
{
struct virtio_softc *vsc = vq->vq_owner;
/* Find out if we need to notify the device. */
(void) ddi_dma_sync(vq->vq_dma_handle, vq->vq_usedoffset,
sizeof (struct vring_used), DDI_DMA_SYNC_FORKERNEL);
if (!(vq->vq_used->flags & VRING_USED_F_NO_NOTIFY))
ddi_put16(vsc->sc_ioh,
/* LINTED E_BAD_PTR_CAST_ALIGN */
(uint16_t *)(vsc->sc_io_addr +
VIRTIO_CONFIG_QUEUE_NOTIFY),
vq->vq_index);
}
void
virtio_sync_vq(struct virtqueue *vq)
{
/* Just sync the whole avail ring. */
(void) ddi_dma_sync(vq->vq_dma_handle,
vq->vq_availoffset + sizeof (struct vring_avail),
sizeof (uint16_t) * vq->vq_num,
DDI_DMA_SYNC_FORDEV);
/*
* Yes, we need to make sure the device sees the idx update after
* it sees the ring update.
*/
vq->vq_avail->idx = vq->vq_avail_idx;
/* Sync the idx and flags */
(void) ddi_dma_sync(vq->vq_dma_handle, vq->vq_availoffset,
sizeof (struct vring_avail), DDI_DMA_SYNC_FORDEV);
virtio_notify(vq);
}
static void
virtio_ve_sync_desc(struct vq_entry *qe, unsigned int direction)
{
struct virtqueue *vq = qe->qe_queue;
/* Sync the descriptor */
/* (The descriptor array is located at the start of the vq memory) */
(void) ddi_dma_sync(vq->vq_dma_handle,
sizeof (struct vring_desc) * qe->qe_index,
sizeof (struct vring_desc),
direction);
/* Sync the indirect descriptors as well */
if (qe->qe_indirect_next)
(void) ddi_dma_sync(qe->qe_indirect_dma_handle,
0, sizeof (struct vring_desc) * qe->qe_indirect_next,
direction);
}
void
virtio_push_chain(struct vq_entry *qe, boolean_t sync)
{
struct virtqueue *vq = qe->qe_queue;
struct vq_entry *head = qe;
struct vring_desc *desc;
int idx;
ASSERT(qe);
/*
* Bind the descs together, paddr and len should be already
* set with virtio_ve_set
*/
do {
ASSERT(qe->qe_guard1 == QE_POISON1_USED);
ASSERT(qe->qe_guard2 == QE_POISON2_USED);
/* Bind the indirect descriptors */
if (qe->qe_indirect_next > 1) {
uint16_t i = 0;
/*
* Set the pointer/flags to the
* first indirect descriptor
*/
virtio_ve_set_desc(qe->qe_desc,
qe->qe_indirect_dma_cookie.dmac_laddress,
sizeof (struct vring_desc) * qe->qe_indirect_next,
B_FALSE);
qe->qe_desc->flags |= VRING_DESC_F_INDIRECT;
/* For all but the last one, add the next index/flag */
do {
desc = &qe->qe_indirect_descs[i];
i++;
desc->flags |= VRING_DESC_F_NEXT;
desc->next = i;
} while (i < qe->qe_indirect_next - 1);
}
if (qe->qe_next) {
qe->qe_desc->flags |= VRING_DESC_F_NEXT;
qe->qe_desc->next = qe->qe_next->qe_index;
}
/* Done with the descriptors, sync. */
virtio_ve_sync_desc(qe, DDI_DMA_SYNC_FORDEV);
qe = qe->qe_next;
} while (qe);
mutex_enter(&vq->vq_avail_lock);
idx = vq->vq_avail_idx;
vq->vq_avail_idx++;
vq->vq_avail->ring[idx % vq->vq_num] = head->qe_index;
/* Notify the device, if needed. */
if (sync)
virtio_sync_vq(vq);
mutex_exit(&vq->vq_avail_lock);
}
/* Get a chain of descriptors from the used ring, if one is available. */
struct vq_entry *
virtio_pull_chain(struct virtqueue *vq, uint32_t *len)
{
struct vq_entry *head;
struct vq_entry *tmp;
int slot;
int usedidx;
mutex_enter(&vq->vq_used_lock);
/*
* Sync idx (and flags), but only if we don't have a backlog
* from the previous sync.
*/
if (vq->vq_used_idx == vq->vq_used->idx) {
(void) ddi_dma_sync(vq->vq_dma_handle, vq->vq_usedoffset,
sizeof (struct vring_used), DDI_DMA_SYNC_FORKERNEL);
/* Still nothing? Bye. */
if (vq->vq_used_idx == vq->vq_used->idx) {
mutex_exit(&vq->vq_used_lock);
return (NULL);
}
}
usedidx = vq->vq_used_idx;
vq->vq_used_idx++;
mutex_exit(&vq->vq_used_lock);
usedidx %= vq->vq_num;
/* Sync the ring entry */
(void) ddi_dma_sync(vq->vq_dma_handle,
vq->vq_usedoffset + sizeof (struct vring_used) +
sizeof (struct vring_used_elem) * usedidx,
sizeof (struct vring_used_elem), DDI_DMA_SYNC_FORKERNEL);
slot = vq->vq_used->ring[usedidx].id;
*len = vq->vq_used->ring[usedidx].len;
head = tmp = &vq->vq_entries[slot];
/* Sync the descriptor chain. */
do {
ASSERT(tmp->qe_guard1 == QE_POISON1_USED);
ASSERT(tmp->qe_guard2 == QE_POISON2_USED);
virtio_ve_sync_desc(tmp, DDI_DMA_SYNC_FORKERNEL);
ASSERT((!tmp->qe_next) ||
tmp->qe_next->qe_index == tmp->qe_desc->next);
tmp = tmp->qe_next;
} while (tmp);
return (head);
}
void
virtio_free_chain(struct vq_entry *qe)
{
struct vq_entry *tmp;
struct virtqueue *vq = qe->qe_queue;
ASSERT(qe);
do {
ASSERT(qe->qe_guard1 == QE_POISON1_USED);
ASSERT(qe->qe_guard2 == QE_POISON2_USED);
ASSERT(qe->qe_queue == vq);
tmp = qe->qe_next;
vq_free_entry(vq, qe);
qe = tmp;
} while (tmp);
}
void
virtio_ventry_stick(struct vq_entry *first, struct vq_entry *second)
{
first->qe_next = second;
}
static int virtio_register_msi(struct virtio_softc *sc,
struct virtio_int_handler *config_handler,
struct virtio_int_handler vq_handlers[],
int intr_types)
{
int count, actual;
int int_type;
int i;
int handler_count;
int ret;
/* If both MSI and MSI-x are reported, prefer MSI-x. */
int_type = DDI_INTR_TYPE_MSI;
if (intr_types & DDI_INTR_TYPE_MSIX)
int_type = DDI_INTR_TYPE_MSIX;
/* Walk the handler table to get the number of handlers. */
for (handler_count = 0;
vq_handlers && vq_handlers[handler_count].vh_func;
handler_count++)
;
/* +1 if there is a config change handler. */
if (config_handler)
handler_count++;
/* Number of MSIs supported by the device. */
ret = ddi_intr_get_nintrs(sc->sc_dev, int_type, &count);
if (ret) {
dev_err(sc->sc_dev, CE_WARN, "ddi_intr_get_nintrs failed");
goto out_nomsi;
}
/*
* Those who try to register more handlers then the device
* supports shall suffer.
*/
ASSERT(handler_count <= count);
sc->sc_intr_htable = kmem_zalloc(
sizeof (ddi_intr_handle_t) * handler_count, KM_SLEEP);
if (!sc->sc_intr_htable) {
dev_err(sc->sc_dev, CE_WARN, "Failed to allocate MSI handles");
goto out_nomsi;
}
ret = ddi_intr_alloc(sc->sc_dev, sc->sc_intr_htable, int_type, 0,
handler_count, &actual, DDI_INTR_ALLOC_NORMAL);
if (ret) {
dev_err(sc->sc_dev, CE_WARN, "Failed to allocate MSI: %d", ret);
goto out_msi_alloc;
}
if (actual != handler_count) {
dev_err(sc->sc_dev, CE_WARN,
"Not enough MSI available: need %d, available %d",
handler_count, actual);
goto out_msi_available;
}
sc->sc_intr_num = handler_count;
sc->sc_intr_config = B_FALSE;
if (config_handler) {
sc->sc_intr_config = B_TRUE;
}
/* Assume they are all same priority */
ret = ddi_intr_get_pri(sc->sc_intr_htable[0], &sc->sc_intr_prio);
if (ret) {
dev_err(sc->sc_dev, CE_WARN, "ddi_intr_get_pri failed");
goto out_msi_prio;
}
/* Add the vq handlers */
for (i = 0; vq_handlers[i].vh_func; i++) {
ret = ddi_intr_add_handler(sc->sc_intr_htable[i],
vq_handlers[i].vh_func,
sc, vq_handlers[i].vh_priv);
if (ret) {
dev_err(sc->sc_dev, CE_WARN,
"ddi_intr_add_handler failed");
/* Remove the handlers that succeeded. */