Lines Matching defs: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;
198 vtpci_child_detached(struct vtpci_common *cn)
201 vtpci_release_child_resources(cn);
203 cn->vtpci_child_feat_desc = NULL;
204 cn->vtpci_host_features = 0;
205 cn->vtpci_features = 0;
209 vtpci_reinit(struct vtpci_common *cn)
213 for (idx = 0; idx < cn->vtpci_nvqs; idx++) {
214 error = vtpci_reinit_virtqueue(cn, idx);
219 if (vtpci_is_msix_enabled(cn)) {
220 error = vtpci_set_host_msix_vectors(cn);
229 vtpci_describe_features(struct vtpci_common *cn, const char *msg,
234 dev = cn->vtpci_dev;
235 child = cn->vtpci_child_dev;
240 virtio_describe(dev, msg, features, cn->vtpci_child_feat_desc);
244 vtpci_negotiate_features(struct vtpci_common *cn,
249 cn->vtpci_host_features = host_features;
250 vtpci_describe_features(cn, "host", host_features);
259 cn->vtpci_features = features;
260 vtpci_describe_features(cn, "negotiated", features);
266 vtpci_with_feature(struct vtpci_common *cn, uint64_t feature)
268 return ((cn->vtpci_features & feature) != 0);
272 vtpci_read_ivar(struct vtpci_common *cn, int index, uintptr_t *result)
277 dev = cn->vtpci_dev;
294 *result = vtpci_is_modern(cn);
304 vtpci_write_ivar(struct vtpci_common *cn, int index, uintptr_t value)
312 cn->vtpci_child_feat_desc = (void *) value;
322 vtpci_alloc_virtqueues(struct vtpci_common *cn, int nvqs,
328 dev = cn->vtpci_dev;
337 if (cn->vtpci_nvqs != 0)
342 cn->vtpci_vqs = malloc(nvqs * sizeof(struct vtpci_virtqueue),
344 if (cn->vtpci_vqs == NULL)
354 vqx = &cn->vtpci_vqs[idx];
357 size = vtpci_get_vq_size(cn, idx);
358 notify_offset = vtpci_get_vq_notify_off(cn, idx);
368 vtpci_set_vq(cn, vq);
373 cn->vtpci_nvqs++;
377 vtpci_free_virtqueues(cn);
383 vtpci_alloc_msix(struct vtpci_common *cn, int nvectors)
388 dev = cn->vtpci_dev;
399 cn->vtpci_nmsix_resources = required;
409 vtpci_alloc_msi(struct vtpci_common *cn)
414 dev = cn->vtpci_dev;
431 vtpci_alloc_intr_msix_pervq(struct vtpci_common *cn)
435 if (vtpci_disable_msix != 0 || cn->vtpci_flags & VTPCI_FLAG_NO_MSIX)
438 for (nvectors = 0, i = 0; i < cn->vtpci_nvqs; i++) {
439 if (cn->vtpci_vqs[i].vtv_no_intr == 0)
443 error = vtpci_alloc_msix(cn, nvectors);
447 cn->vtpci_flags |= VTPCI_FLAG_MSIX;
453 vtpci_alloc_intr_msix_shared(struct vtpci_common *cn)
457 if (vtpci_disable_msix != 0 || cn->vtpci_flags & VTPCI_FLAG_NO_MSIX)
460 error = vtpci_alloc_msix(cn, 1);
464 cn->vtpci_flags |= VTPCI_FLAG_MSIX | VTPCI_FLAG_SHARED_MSIX;
470 vtpci_alloc_intr_msi(struct vtpci_common *cn)
475 if (cn->vtpci_flags & VTPCI_FLAG_NO_MSI)
478 error = vtpci_alloc_msi(cn);
482 cn->vtpci_flags |= VTPCI_FLAG_MSI;
488 vtpci_alloc_intr_intx(struct vtpci_common *cn)
491 cn->vtpci_flags |= VTPCI_FLAG_INTX;
497 vtpci_alloc_interrupt(struct vtpci_common *cn, int rid, int flags,
502 irq = bus_alloc_resource_any(cn->vtpci_dev, SYS_RES_IRQ, &rid, flags);
513 vtpci_free_interrupt(struct vtpci_common *cn, struct vtpci_interrupt *intr)
517 dev = cn->vtpci_dev;
533 vtpci_free_interrupts(struct vtpci_common *cn)
538 vtpci_free_interrupt(cn, &cn->vtpci_device_interrupt);
540 if (cn->vtpci_nmsix_resources != 0) {
541 nvq_intrs = cn->vtpci_nmsix_resources - 1;
542 cn->vtpci_nmsix_resources = 0;
544 if ((intr = cn->vtpci_msix_vq_interrupts) != NULL) {
546 vtpci_free_interrupt(cn, intr);
548 free(cn->vtpci_msix_vq_interrupts, M_DEVBUF);
549 cn->vtpci_msix_vq_interrupts = NULL;
553 if (cn->vtpci_flags & (VTPCI_FLAG_MSI | VTPCI_FLAG_MSIX))
554 pci_release_msi(cn->vtpci_dev);
556 cn->vtpci_flags &= ~VTPCI_FLAG_ITYPE_MASK;
560 vtpci_free_virtqueues(struct vtpci_common *cn)
565 for (idx = 0; idx < cn->vtpci_nvqs; idx++) {
566 vtpci_disable_vq(cn, idx);
568 vqx = &cn->vtpci_vqs[idx];
573 free(cn->vtpci_vqs, M_DEVBUF);
574 cn->vtpci_vqs = NULL;
575 cn->vtpci_nvqs = 0;
579 vtpci_release_child_resources(struct vtpci_common *cn)
582 vtpci_free_interrupts(cn);
583 vtpci_free_virtqueues(cn);
587 vtpci_cleanup_setup_intr_attempt(struct vtpci_common *cn)
591 if (cn->vtpci_flags & VTPCI_FLAG_MSIX) {
592 vtpci_register_cfg_msix(cn, NULL);
594 for (idx = 0; idx < cn->vtpci_nvqs; idx++)
595 vtpci_register_vq_msix(cn, idx, NULL);
598 vtpci_free_interrupts(cn);
602 vtpci_alloc_intr_resources(struct vtpci_common *cn)
609 if (cn->vtpci_flags & VTPCI_FLAG_INTX) {
620 intr = &cn->vtpci_device_interrupt;
622 error = vtpci_alloc_interrupt(cn, rid, flags, intr);
623 if (error || cn->vtpci_flags & (VTPCI_FLAG_INTX | VTPCI_FLAG_MSI))
631 nvq_intrs = cn->vtpci_nmsix_resources - 1;
633 cn->vtpci_msix_vq_interrupts = malloc(nvq_intrs *
635 if (cn->vtpci_msix_vq_interrupts == NULL)
638 intr = cn->vtpci_msix_vq_interrupts;
641 error = vtpci_alloc_interrupt(cn, rid, flags, intr);
650 vtpci_setup_intx_interrupt(struct vtpci_common *cn, enum intr_type type)
655 intr = &cn->vtpci_device_interrupt;
657 error = bus_setup_intr(cn->vtpci_dev, intr->vti_irq, type, NULL,
658 vtpci_intx_intr, cn, &intr->vti_handler);
664 vtpci_setup_pervq_msix_interrupts(struct vtpci_common *cn, enum intr_type type)
670 intr = cn->vtpci_msix_vq_interrupts;
672 for (i = 0; i < cn->vtpci_nvqs; i++) {
673 vqx = &cn->vtpci_vqs[i];
678 error = bus_setup_intr(cn->vtpci_dev, intr->vti_irq, type,
691 vtpci_set_host_msix_vectors(struct vtpci_common *cn)
696 intr = &cn->vtpci_device_interrupt;
697 error = vtpci_register_cfg_msix(cn, intr);
701 intr = cn->vtpci_msix_vq_interrupts;
702 for (idx = 0; idx < cn->vtpci_nvqs; idx++) {
703 if (cn->vtpci_vqs[idx].vtv_no_intr)
708 error = vtpci_register_vq_msix(cn, idx, tintr);
716 if (!cn->vtpci_vqs[idx].vtv_no_intr &&
717 (cn->vtpci_flags & VTPCI_FLAG_SHARED_MSIX) == 0)
725 vtpci_setup_msix_interrupts(struct vtpci_common *cn, enum intr_type type)
730 intr = &cn->vtpci_device_interrupt;
732 error = bus_setup_intr(cn->vtpci_dev, intr->vti_irq, type, NULL,
733 vtpci_config_intr, cn, &intr->vti_handler);
737 if (cn->vtpci_flags & VTPCI_FLAG_SHARED_MSIX) {
738 intr = &cn->vtpci_msix_vq_interrupts[0];
740 error = bus_setup_intr(cn->vtpci_dev, intr->vti_irq, type,
741 vtpci_vq_shared_intr_filter, vtpci_vq_shared_intr, cn,
744 error = vtpci_setup_pervq_msix_interrupts(cn, type);
746 return (error ? error : vtpci_set_host_msix_vectors(cn));
750 vtpci_setup_intrs(struct vtpci_common *cn, enum intr_type type)
755 KASSERT(cn->vtpci_flags & VTPCI_FLAG_ITYPE_MASK,
756 ("%s: no interrupt type selected %#x", __func__, cn->vtpci_flags));
758 error = vtpci_alloc_intr_resources(cn);
762 if (cn->vtpci_flags & VTPCI_FLAG_INTX)
763 error = vtpci_setup_intx_interrupt(cn, type);
764 else if (cn->vtpci_flags & VTPCI_FLAG_MSI)
765 error = vtpci_setup_msi_interrupt(cn, type);
767 error = vtpci_setup_msix_interrupts(cn, type);
773 vtpci_setup_interrupts(struct vtpci_common *cn, enum intr_type type)
778 dev = cn->vtpci_dev;
787 error = vtpci_alloc_intr_msix_pervq(cn);
790 error = vtpci_alloc_intr_msix_shared(cn);
793 error = vtpci_alloc_intr_msi(cn);
796 error = vtpci_alloc_intr_intx(cn);
804 if (error == 0 && vtpci_setup_intrs(cn, type) == 0)
807 vtpci_cleanup_setup_intr_attempt(cn);
811 if (cn->vtpci_flags & VTPCI_FLAG_INTX)
813 else if (cn->vtpci_flags & VTPCI_FLAG_MSI)
815 else if (cn->vtpci_flags & VTPCI_FLAG_SHARED_MSIX)
825 vtpci_reinit_virtqueue(struct vtpci_common *cn, int idx)
831 vqx = &cn->vtpci_vqs[idx];
836 error = virtqueue_reinit(vq, vtpci_get_vq_size(cn, idx));
838 vtpci_set_vq(cn, vq);
846 struct vtpci_common *cn;
851 cn = xcn;
852 isr = vtpci_read_isr(cn);
855 vtpci_config_intr(cn);
858 vqx = &cn->vtpci_vqs[0];
859 for (i = 0; i < cn->vtpci_nvqs; i++, vqx++) {
869 struct vtpci_common *cn;
873 cn = xcn;
874 vqx = &cn->vtpci_vqs[0];
877 for (i = 0; i < cn->vtpci_nvqs; i++, vqx++) {
888 struct vtpci_common *cn;
892 cn = xcn;
893 vqx = &cn->vtpci_vqs[0];
895 for (i = 0; i < cn->vtpci_nvqs; i++, vqx++) {
925 struct vtpci_common *cn;
928 cn = xcn;
929 child = cn->vtpci_child_dev;
936 vtpci_feature_sysctl(struct sysctl_req *req, struct vtpci_common *cn,
946 error = virtio_describe_sbuf(sb, features, cn->vtpci_child_feat_desc);
955 struct vtpci_common *cn;
957 cn = arg1;
959 return (vtpci_feature_sysctl(req, cn, cn->vtpci_host_features));
965 struct vtpci_common *cn;
967 cn = arg1;
969 return (vtpci_feature_sysctl(req, cn, cn->vtpci_features));
973 vtpci_setup_sysctl(struct vtpci_common *cn)
980 dev = cn->vtpci_dev;
986 CTLFLAG_RD, &cn->vtpci_nvqs, 0, "Number of virtqueues");
989 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, cn, 0,
992 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, cn, 0,