Lines Matching full:cn
104 vtpci_read_isr(struct vtpci_common *cn)
106 return (VIRTIO_PCI_READ_ISR(cn->vtpci_dev));
110 vtpci_get_vq_size(struct vtpci_common *cn, int idx)
112 return (VIRTIO_PCI_GET_VQ_SIZE(cn->vtpci_dev, idx));
116 vtpci_get_vq_notify_off(struct vtpci_common *cn, int idx)
118 return (VIRTIO_PCI_GET_VQ_NOTIFY_OFF(cn->vtpci_dev, idx));
122 vtpci_set_vq(struct vtpci_common *cn, struct virtqueue *vq)
124 VIRTIO_PCI_SET_VQ(cn->vtpci_dev, vq);
128 vtpci_disable_vq(struct vtpci_common *cn, int idx)
130 VIRTIO_PCI_DISABLE_VQ(cn->vtpci_dev, idx);
134 vtpci_register_cfg_msix(struct vtpci_common *cn, struct vtpci_interrupt *intr)
136 return (VIRTIO_PCI_REGISTER_CFG_MSIX(cn->vtpci_dev, intr));
140 vtpci_register_vq_msix(struct vtpci_common *cn, int idx,
143 return (VIRTIO_PCI_REGISTER_VQ_MSIX(cn->vtpci_dev, idx, intr));
147 vtpci_init(struct vtpci_common *cn, device_t dev, bool modern)
150 cn->vtpci_dev = dev;
155 cn->vtpci_flags |= VTPCI_FLAG_MODERN;
157 cn->vtpci_flags |= VTPCI_FLAG_NO_MSI;
159 cn->vtpci_flags |= VTPCI_FLAG_NO_MSIX;
161 vtpci_setup_sysctl(cn);
165 vtpci_add_child(struct vtpci_common *cn)
169 dev = cn->vtpci_dev;
177 cn->vtpci_child_dev = child;
183 vtpci_delete_child(struct vtpci_common *cn)
188 dev = cn->vtpci_dev;
190 child = cn->vtpci_child_dev;
195 cn->vtpci_child_dev = NULL;
202 vtpci_child_detached(struct vtpci_common *cn)
205 vtpci_release_child_resources(cn);
207 cn->vtpci_child_feat_desc = NULL;
208 cn->vtpci_host_features = 0;
209 cn->vtpci_features = 0;
213 vtpci_reinit(struct vtpci_common *cn)
217 for (idx = 0; idx < cn->vtpci_nvqs; idx++) {
218 error = vtpci_reinit_virtqueue(cn, idx);
223 if (vtpci_is_msix_enabled(cn)) {
224 error = vtpci_set_host_msix_vectors(cn);
233 vtpci_describe_features(struct vtpci_common *cn, const char *msg,
238 dev = cn->vtpci_dev;
239 child = cn->vtpci_child_dev;
244 virtio_describe(dev, msg, features, cn->vtpci_child_feat_desc);
248 vtpci_negotiate_features(struct vtpci_common *cn,
253 cn->vtpci_host_features = host_features;
254 vtpci_describe_features(cn, "host", host_features);
263 cn->vtpci_features = features;
264 vtpci_describe_features(cn, "negotiated", features);
270 vtpci_with_feature(struct vtpci_common *cn, uint64_t feature)
272 return ((cn->vtpci_features & feature) != 0);
276 vtpci_read_ivar(struct vtpci_common *cn, int index, uintptr_t *result)
281 dev = cn->vtpci_dev;
298 *result = vtpci_is_modern(cn);
308 vtpci_write_ivar(struct vtpci_common *cn, int index, uintptr_t value)
316 cn->vtpci_child_feat_desc = (void *) value;
326 vtpci_alloc_virtqueues(struct vtpci_common *cn, int nvqs,
332 dev = cn->vtpci_dev;
341 if (cn->vtpci_nvqs != 0)
346 cn->vtpci_vqs = malloc(nvqs * sizeof(struct vtpci_virtqueue),
348 if (cn->vtpci_vqs == NULL)
358 vqx = &cn->vtpci_vqs[idx];
361 size = vtpci_get_vq_size(cn, idx);
362 notify_offset = vtpci_get_vq_notify_off(cn, idx);
372 vtpci_set_vq(cn, vq);
377 cn->vtpci_nvqs++;
381 vtpci_free_virtqueues(cn);
387 vtpci_alloc_msix(struct vtpci_common *cn, int nvectors)
392 dev = cn->vtpci_dev;
403 cn->vtpci_nmsix_resources = required;
413 vtpci_alloc_msi(struct vtpci_common *cn)
418 dev = cn->vtpci_dev;
435 vtpci_alloc_intr_msix_pervq(struct vtpci_common *cn)
439 if (vtpci_disable_msix != 0 || cn->vtpci_flags & VTPCI_FLAG_NO_MSIX)
442 for (nvectors = 0, i = 0; i < cn->vtpci_nvqs; i++) {
443 if (cn->vtpci_vqs[i].vtv_no_intr == 0)
447 error = vtpci_alloc_msix(cn, nvectors);
451 cn->vtpci_flags |= VTPCI_FLAG_MSIX;
457 vtpci_alloc_intr_msix_shared(struct vtpci_common *cn)
461 if (vtpci_disable_msix != 0 || cn->vtpci_flags & VTPCI_FLAG_NO_MSIX)
464 error = vtpci_alloc_msix(cn, 1);
468 cn->vtpci_flags |= VTPCI_FLAG_MSIX | VTPCI_FLAG_SHARED_MSIX;
474 vtpci_alloc_intr_msi(struct vtpci_common *cn)
479 if (cn->vtpci_flags & VTPCI_FLAG_NO_MSI)
482 error = vtpci_alloc_msi(cn);
486 cn->vtpci_flags |= VTPCI_FLAG_MSI;
492 vtpci_alloc_intr_intx(struct vtpci_common *cn)
495 cn->vtpci_flags |= VTPCI_FLAG_INTX;
501 vtpci_alloc_interrupt(struct vtpci_common *cn, int rid, int flags,
506 irq = bus_alloc_resource_any(cn->vtpci_dev, SYS_RES_IRQ, &rid, flags);
517 vtpci_free_interrupt(struct vtpci_common *cn, struct vtpci_interrupt *intr)
521 dev = cn->vtpci_dev;
537 vtpci_free_interrupts(struct vtpci_common *cn)
542 vtpci_free_interrupt(cn, &cn->vtpci_device_interrupt);
544 if (cn->vtpci_nmsix_resources != 0) {
545 nvq_intrs = cn->vtpci_nmsix_resources - 1;
546 cn->vtpci_nmsix_resources = 0;
548 if ((intr = cn->vtpci_msix_vq_interrupts) != NULL) {
550 vtpci_free_interrupt(cn, intr);
552 free(cn->vtpci_msix_vq_interrupts, M_DEVBUF);
553 cn->vtpci_msix_vq_interrupts = NULL;
557 if (cn->vtpci_flags & (VTPCI_FLAG_MSI | VTPCI_FLAG_MSIX))
558 pci_release_msi(cn->vtpci_dev);
560 cn->vtpci_flags &= ~VTPCI_FLAG_ITYPE_MASK;
564 vtpci_free_virtqueues(struct vtpci_common *cn)
569 for (idx = 0; idx < cn->vtpci_nvqs; idx++) {
570 vtpci_disable_vq(cn, idx);
572 vqx = &cn->vtpci_vqs[idx];
577 free(cn->vtpci_vqs, M_DEVBUF);
578 cn->vtpci_vqs = NULL;
579 cn->vtpci_nvqs = 0;
583 vtpci_release_child_resources(struct vtpci_common *cn)
586 vtpci_free_interrupts(cn);
587 vtpci_free_virtqueues(cn);
591 vtpci_cleanup_setup_intr_attempt(struct vtpci_common *cn)
595 if (cn->vtpci_flags & VTPCI_FLAG_MSIX) {
596 vtpci_register_cfg_msix(cn, NULL);
598 for (idx = 0; idx < cn->vtpci_nvqs; idx++)
599 vtpci_register_vq_msix(cn, idx, NULL);
602 vtpci_free_interrupts(cn);
606 vtpci_alloc_intr_resources(struct vtpci_common *cn)
613 if (cn->vtpci_flags & VTPCI_FLAG_INTX) {
624 intr = &cn->vtpci_device_interrupt;
626 error = vtpci_alloc_interrupt(cn, rid, flags, intr);
627 if (error || cn->vtpci_flags & (VTPCI_FLAG_INTX | VTPCI_FLAG_MSI))
635 nvq_intrs = cn->vtpci_nmsix_resources - 1;
637 cn->vtpci_msix_vq_interrupts = malloc(nvq_intrs *
639 if (cn->vtpci_msix_vq_interrupts == NULL)
642 intr = cn->vtpci_msix_vq_interrupts;
645 error = vtpci_alloc_interrupt(cn, rid, flags, intr);
654 vtpci_setup_intx_interrupt(struct vtpci_common *cn, enum intr_type type)
659 intr = &cn->vtpci_device_interrupt;
661 error = bus_setup_intr(cn->vtpci_dev, intr->vti_irq, type, NULL,
662 vtpci_intx_intr, cn, &intr->vti_handler);
668 vtpci_setup_pervq_msix_interrupts(struct vtpci_common *cn, enum intr_type type)
674 intr = cn->vtpci_msix_vq_interrupts;
676 for (i = 0; i < cn->vtpci_nvqs; i++) {
677 vqx = &cn->vtpci_vqs[i];
682 error = bus_setup_intr(cn->vtpci_dev, intr->vti_irq, type,
695 vtpci_set_host_msix_vectors(struct vtpci_common *cn)
700 intr = &cn->vtpci_device_interrupt;
701 error = vtpci_register_cfg_msix(cn, intr);
705 intr = cn->vtpci_msix_vq_interrupts;
706 for (idx = 0; idx < cn->vtpci_nvqs; idx++) {
707 if (cn->vtpci_vqs[idx].vtv_no_intr)
712 error = vtpci_register_vq_msix(cn, idx, tintr);
720 if (!cn->vtpci_vqs[idx].vtv_no_intr &&
721 (cn->vtpci_flags & VTPCI_FLAG_SHARED_MSIX) == 0)
729 vtpci_setup_msix_interrupts(struct vtpci_common *cn, enum intr_type type)
734 intr = &cn->vtpci_device_interrupt;
736 error = bus_setup_intr(cn->vtpci_dev, intr->vti_irq, type, NULL,
737 vtpci_config_intr, cn, &intr->vti_handler);
741 if (cn->vtpci_flags & VTPCI_FLAG_SHARED_MSIX) {
742 intr = &cn->vtpci_msix_vq_interrupts[0];
744 error = bus_setup_intr(cn->vtpci_dev, intr->vti_irq, type,
745 vtpci_vq_shared_intr_filter, vtpci_vq_shared_intr, cn,
748 error = vtpci_setup_pervq_msix_interrupts(cn, type);
750 return (error ? error : vtpci_set_host_msix_vectors(cn));
754 vtpci_setup_intrs(struct vtpci_common *cn, enum intr_type type)
759 KASSERT(cn->vtpci_flags & VTPCI_FLAG_ITYPE_MASK,
760 ("%s: no interrupt type selected %#x", __func__, cn->vtpci_flags));
762 error = vtpci_alloc_intr_resources(cn);
766 if (cn->vtpci_flags & VTPCI_FLAG_INTX)
767 error = vtpci_setup_intx_interrupt(cn, type);
768 else if (cn->vtpci_flags & VTPCI_FLAG_MSI)
769 error = vtpci_setup_msi_interrupt(cn, type);
771 error = vtpci_setup_msix_interrupts(cn, type);
777 vtpci_setup_interrupts(struct vtpci_common *cn, enum intr_type type)
782 dev = cn->vtpci_dev;
791 error = vtpci_alloc_intr_msix_pervq(cn);
794 error = vtpci_alloc_intr_msix_shared(cn);
797 error = vtpci_alloc_intr_msi(cn);
800 error = vtpci_alloc_intr_intx(cn);
808 if (error == 0 && vtpci_setup_intrs(cn, type) == 0)
811 vtpci_cleanup_setup_intr_attempt(cn);
815 if (cn->vtpci_flags & VTPCI_FLAG_INTX)
817 else if (cn->vtpci_flags & VTPCI_FLAG_MSI)
819 else if (cn->vtpci_flags & VTPCI_FLAG_SHARED_MSIX)
829 vtpci_reinit_virtqueue(struct vtpci_common *cn, int idx)
835 vqx = &cn->vtpci_vqs[idx];
840 error = virtqueue_reinit(vq, vtpci_get_vq_size(cn, idx));
842 vtpci_set_vq(cn, vq);
850 struct vtpci_common *cn;
855 cn = xcn;
856 isr = vtpci_read_isr(cn);
859 vtpci_config_intr(cn);
862 vqx = &cn->vtpci_vqs[0];
863 for (i = 0; i < cn->vtpci_nvqs; i++, vqx++) {
873 struct vtpci_common *cn;
877 cn = xcn;
878 vqx = &cn->vtpci_vqs[0];
881 for (i = 0; i < cn->vtpci_nvqs; i++, vqx++) {
892 struct vtpci_common *cn;
896 cn = xcn;
897 vqx = &cn->vtpci_vqs[0];
899 for (i = 0; i < cn->vtpci_nvqs; i++, vqx++) {
929 struct vtpci_common *cn;
932 cn = xcn;
933 child = cn->vtpci_child_dev;
940 vtpci_feature_sysctl(struct sysctl_req *req, struct vtpci_common *cn,
950 error = virtio_describe_sbuf(sb, features, cn->vtpci_child_feat_desc);
959 struct vtpci_common *cn;
961 cn = arg1;
963 return (vtpci_feature_sysctl(req, cn, cn->vtpci_host_features));
969 struct vtpci_common *cn;
971 cn = arg1;
973 return (vtpci_feature_sysctl(req, cn, cn->vtpci_features));
977 vtpci_setup_sysctl(struct vtpci_common *cn)
984 dev = cn->vtpci_dev;
990 CTLFLAG_RD, &cn->vtpci_nvqs, 0, "Number of virtqueues");
993 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, cn, 0,
996 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, cn, 0,