On Thu, Dec 18, 2025 at 05:09:41PM +0800, Xiong Weimin wrote:
> From: xiongweimin <[email protected]>
> 
> This commit introduces a new driver for RDMA over virtio, enabling
> RDMA capabilities in virtualized environments. The driver consists
> of the following main components:
> 
> 1. Driver registration with the virtio subsystem and device discovery.
> 2. Device probe and remove handlers for managing the device lifecycle.
> 3. Initialization of the InfiniBand device attributes by reading the
>    virtio configuration space, including conversion from little-endian
>    to CPU byte order and capability mapping.
> 4. Setup of virtqueues for:
>    - Control commands (no callback)
>    - Completion queues (with callback for CQ events)
>    - Send and receive queues for queue pairs (no callbacks)
> 5. Integration with the network device layer for RoCE support.
> 6. Registration with the InfiniBand core subsystem.
> 7. Comprehensive error handling during initialization and a symmetric
>    teardown process.
> 
> Key features:
> - Support for multiple virtqueues based on device capabilities (max_cq, 
> max_qp)
> - Fast doorbell optimization when notify_offset_multiplier equals PAGE_SIZE
> - Safe resource management with rollback on failure
> 
> Signed-off-by: Xiong Weimin <[email protected]>

<...>

> +/**
> + * vrdma_init_netdev - Attempt to find paired virtio-net device on same PCI 
> slot
> + * @vrdev: The vRDMA device
> + *
> + * WARNING: This is a non-standard hack for development/emulation 
> environments.
> + *          Do not use in production or upstream drivers.

I'm impressed how much AI advanced in code generation. Please recheck
everything that was generated.

> + *
> + * Returns 0 on success, or negative errno.
> + */
> +int vrdma_init_netdev(struct vrdma_dev *vrdev)
> +{
> +    struct pci_dev *pdev_net;
> +    struct virtio_pci_device *vp_dev;
> +    struct virtio_pci_device *vnet_pdev;
> +    void *priv;
> +    struct net_device *netdev;
> +
> +    if (!vrdev || !vrdev->vdev) {
> +        pr_err("%s: invalid vrdev or vdev\n", __func__);
> +        return -EINVAL;
> +    }
> +
> +    vp_dev = to_vp_device(vrdev->vdev);
> +
> +    /* Find the PCI device at function 0 of the same slot */
> +    pdev_net = pci_get_slot(vp_dev->pci_dev->bus,
> +                            PCI_DEVFN(PCI_SLOT(vp_dev->pci_dev->devfn), 0));
> +    if (!pdev_net) {
> +        pr_err("Failed to find PCI device at fn=0 of slot %x\n",
> +               PCI_SLOT(vp_dev->pci_dev->devfn));
> +        return -ENODEV;
> +    }
> +
> +    /* Optional: Validate it's a known virtio-net device */
> +    if (pdev_net->vendor != PCI_VENDOR_ID_REDHAT_QUMRANET ||
> +        pdev_net->device != 0x1041) {
> +        pr_warn("PCI device %04x:%04x is not expected virtio-net (1041) 
> device\n",
> +                pdev_net->vendor, pdev_net->device);
> +        pci_dev_put(pdev_net);
> +        return -ENODEV;
> +    }
> +
> +    /* Get the virtio_pci_device from drvdata */
> +    vnet_pdev = pci_get_drvdata(pdev_net);
> +    if (!vnet_pdev || !vnet_pdev->vdev.priv) {
> +        pr_err("No driver data or priv for virtio-net device\n");
> +        pci_dev_put(pdev_net);
> +        return -ENODEV;
> +    }
> +
> +    priv = vnet_pdev->vdev.priv;
> +     vrdev->netdev = priv - ALIGN(sizeof(struct net_device), NETDEV_ALIGN);
> +    netdev = vrdev->netdev; 
> +
> +    if (!netdev || !netdev->netdev_ops) {
> +        pr_err("Invalid net_device retrieved from virtio-net\n");
> +        pci_dev_put(pdev_net);
> +        return -ENODEV;
> +    }
> +
> +    /* Hold reference so netdev won't disappear */
> +    dev_hold(netdev);
> +
> +    pci_dev_put(pdev_net);  /* Release reference from pci_get_slot */
> +
> +    return 0;
> +}

AI was right here. It is awful hack.

Thanks

Reply via email to