1af873fceSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 20d2e1a29SErwan Yvin /* 30d2e1a29SErwan Yvin * Copyright (C) ST-Ericsson AB 2013 401d779a1SSjur Brændeland * Authors: Vicram Arv 501d779a1SSjur Brændeland * Dmitry Tarnyagin <dmitry.tarnyagin@lockless.no> 601d779a1SSjur Brændeland * Sjur Brendeland 70d2e1a29SErwan Yvin */ 80d2e1a29SErwan Yvin #include <linux/module.h> 90d2e1a29SErwan Yvin #include <linux/if_arp.h> 100d2e1a29SErwan Yvin #include <linux/virtio.h> 110d2e1a29SErwan Yvin #include <linux/vringh.h> 120d2e1a29SErwan Yvin #include <linux/debugfs.h> 130d2e1a29SErwan Yvin #include <linux/spinlock.h> 140d2e1a29SErwan Yvin #include <linux/genalloc.h> 150d2e1a29SErwan Yvin #include <linux/interrupt.h> 160d2e1a29SErwan Yvin #include <linux/netdevice.h> 170d2e1a29SErwan Yvin #include <linux/rtnetlink.h> 180d2e1a29SErwan Yvin #include <linux/virtio_ids.h> 190d2e1a29SErwan Yvin #include <linux/virtio_caif.h> 200d2e1a29SErwan Yvin #include <linux/virtio_ring.h> 210d2e1a29SErwan Yvin #include <linux/dma-mapping.h> 220d2e1a29SErwan Yvin #include <net/caif/caif_dev.h> 230d2e1a29SErwan Yvin #include <linux/virtio_config.h> 240d2e1a29SErwan Yvin 250d2e1a29SErwan Yvin MODULE_LICENSE("GPL v2"); 2601d779a1SSjur Brændeland MODULE_AUTHOR("Vicram Arv"); 2701d779a1SSjur Brændeland MODULE_AUTHOR("Sjur Brendeland"); 280d2e1a29SErwan Yvin MODULE_DESCRIPTION("Virtio CAIF Driver"); 290d2e1a29SErwan Yvin 300d2e1a29SErwan Yvin /* NAPI schedule quota */ 310d2e1a29SErwan Yvin #define CFV_DEFAULT_QUOTA 32 320d2e1a29SErwan Yvin 330d2e1a29SErwan Yvin /* Defaults used if virtio config space is unavailable */ 340d2e1a29SErwan Yvin #define CFV_DEF_MTU_SIZE 4096 350d2e1a29SErwan Yvin #define CFV_DEF_HEADROOM 32 360d2e1a29SErwan Yvin #define CFV_DEF_TAILROOM 32 370d2e1a29SErwan Yvin 380d2e1a29SErwan Yvin /* Required IP header alignment */ 390d2e1a29SErwan Yvin #define IP_HDR_ALIGN 4 400d2e1a29SErwan Yvin 410d2e1a29SErwan Yvin /* struct cfv_napi_contxt - NAPI context info 420d2e1a29SErwan Yvin * @riov: IOV holding data read from the ring. Note that riov may 430d2e1a29SErwan Yvin * still hold data when cfv_rx_poll() returns. 440d2e1a29SErwan Yvin * @head: Last descriptor ID we received from vringh_getdesc_kern. 450d2e1a29SErwan Yvin * We use this to put descriptor back on the used ring. USHRT_MAX is 460d2e1a29SErwan Yvin * used to indicate invalid head-id. 470d2e1a29SErwan Yvin */ 480d2e1a29SErwan Yvin struct cfv_napi_context { 490d2e1a29SErwan Yvin struct vringh_kiov riov; 500d2e1a29SErwan Yvin unsigned short head; 510d2e1a29SErwan Yvin }; 520d2e1a29SErwan Yvin 530d2e1a29SErwan Yvin /* struct cfv_stats - statistics for debugfs 540d2e1a29SErwan Yvin * @rx_napi_complete: Number of NAPI completions (RX) 550d2e1a29SErwan Yvin * @rx_napi_resched: Number of calls where the full quota was used (RX) 560d2e1a29SErwan Yvin * @rx_nomem: Number of SKB alloc failures (RX) 570d2e1a29SErwan Yvin * @rx_kicks: Number of RX kicks 580d2e1a29SErwan Yvin * @tx_full_ring: Number times TX ring was full 590d2e1a29SErwan Yvin * @tx_no_mem: Number of times TX went out of memory 600d2e1a29SErwan Yvin * @tx_flow_on: Number of flow on (TX) 610d2e1a29SErwan Yvin * @tx_kicks: Number of TX kicks 620d2e1a29SErwan Yvin */ 630d2e1a29SErwan Yvin struct cfv_stats { 640d2e1a29SErwan Yvin u32 rx_napi_complete; 650d2e1a29SErwan Yvin u32 rx_napi_resched; 660d2e1a29SErwan Yvin u32 rx_nomem; 670d2e1a29SErwan Yvin u32 rx_kicks; 680d2e1a29SErwan Yvin u32 tx_full_ring; 690d2e1a29SErwan Yvin u32 tx_no_mem; 700d2e1a29SErwan Yvin u32 tx_flow_on; 710d2e1a29SErwan Yvin u32 tx_kicks; 720d2e1a29SErwan Yvin }; 730d2e1a29SErwan Yvin 740d2e1a29SErwan Yvin /* struct cfv_info - Caif Virtio control structure 750d2e1a29SErwan Yvin * @cfdev: caif common header 760d2e1a29SErwan Yvin * @vdev: Associated virtio device 770d2e1a29SErwan Yvin * @vr_rx: rx/downlink host vring 780d2e1a29SErwan Yvin * @vq_tx: tx/uplink virtqueue 790d2e1a29SErwan Yvin * @ndev: CAIF link layer device 800d2e1a29SErwan Yvin * @watermark_tx: indicates number of free descriptors we need 810d2e1a29SErwan Yvin * to reopen the tx-queues after overload. 820d2e1a29SErwan Yvin * @tx_lock: protects vq_tx from concurrent use 830d2e1a29SErwan Yvin * @tx_release_tasklet: Tasklet for freeing consumed TX buffers 840d2e1a29SErwan Yvin * @napi: Napi context used in cfv_rx_poll() 850d2e1a29SErwan Yvin * @ctx: Context data used in cfv_rx_poll() 860d2e1a29SErwan Yvin * @tx_hr: transmit headroom 870d2e1a29SErwan Yvin * @rx_hr: receive headroom 880d2e1a29SErwan Yvin * @tx_tr: transmit tail room 890d2e1a29SErwan Yvin * @rx_tr: receive tail room 900d2e1a29SErwan Yvin * @mtu: transmit max size 910d2e1a29SErwan Yvin * @mru: receive max size 920d2e1a29SErwan Yvin * @allocsz: size of dma memory reserved for TX buffers 930d2e1a29SErwan Yvin * @alloc_addr: virtual address to dma memory for TX buffers 940d2e1a29SErwan Yvin * @alloc_dma: dma address to dma memory for TX buffers 950d2e1a29SErwan Yvin * @genpool: Gen Pool used for allocating TX buffers 960d2e1a29SErwan Yvin * @reserved_mem: Pointer to memory reserve allocated from genpool 970d2e1a29SErwan Yvin * @reserved_size: Size of memory reserve allocated from genpool 980d2e1a29SErwan Yvin * @stats: Statistics exposed in sysfs 990d2e1a29SErwan Yvin * @debugfs: Debugfs dentry for statistic counters 1000d2e1a29SErwan Yvin */ 1010d2e1a29SErwan Yvin struct cfv_info { 1020d2e1a29SErwan Yvin struct caif_dev_common cfdev; 1030d2e1a29SErwan Yvin struct virtio_device *vdev; 1040d2e1a29SErwan Yvin struct vringh *vr_rx; 1050d2e1a29SErwan Yvin struct virtqueue *vq_tx; 1060d2e1a29SErwan Yvin struct net_device *ndev; 1070d2e1a29SErwan Yvin unsigned int watermark_tx; 1080d2e1a29SErwan Yvin /* Protect access to vq_tx */ 1090d2e1a29SErwan Yvin spinlock_t tx_lock; 1100d2e1a29SErwan Yvin struct tasklet_struct tx_release_tasklet; 1110d2e1a29SErwan Yvin struct napi_struct napi; 1120d2e1a29SErwan Yvin struct cfv_napi_context ctx; 1130d2e1a29SErwan Yvin u16 tx_hr; 1140d2e1a29SErwan Yvin u16 rx_hr; 1150d2e1a29SErwan Yvin u16 tx_tr; 1160d2e1a29SErwan Yvin u16 rx_tr; 1170d2e1a29SErwan Yvin u32 mtu; 1180d2e1a29SErwan Yvin u32 mru; 1190d2e1a29SErwan Yvin size_t allocsz; 1200d2e1a29SErwan Yvin void *alloc_addr; 1210d2e1a29SErwan Yvin dma_addr_t alloc_dma; 1220d2e1a29SErwan Yvin struct gen_pool *genpool; 1230d2e1a29SErwan Yvin unsigned long reserved_mem; 1240d2e1a29SErwan Yvin size_t reserved_size; 1250d2e1a29SErwan Yvin struct cfv_stats stats; 1260d2e1a29SErwan Yvin struct dentry *debugfs; 1270d2e1a29SErwan Yvin }; 1280d2e1a29SErwan Yvin 1290d2e1a29SErwan Yvin /* struct buf_info - maintains transmit buffer data handle 1300d2e1a29SErwan Yvin * @size: size of transmit buffer 1310d2e1a29SErwan Yvin * @dma_handle: handle to allocated dma device memory area 1320d2e1a29SErwan Yvin * @vaddr: virtual address mapping to allocated memory area 1330d2e1a29SErwan Yvin */ 1340d2e1a29SErwan Yvin struct buf_info { 1350d2e1a29SErwan Yvin size_t size; 1360d2e1a29SErwan Yvin u8 *vaddr; 1370d2e1a29SErwan Yvin }; 1380d2e1a29SErwan Yvin 1390d2e1a29SErwan Yvin /* Called from virtio device, in IRQ context */ 1400d2e1a29SErwan Yvin static void cfv_release_cb(struct virtqueue *vq_tx) 1410d2e1a29SErwan Yvin { 1420d2e1a29SErwan Yvin struct cfv_info *cfv = vq_tx->vdev->priv; 1430d2e1a29SErwan Yvin 1440d2e1a29SErwan Yvin ++cfv->stats.tx_kicks; 1450d2e1a29SErwan Yvin tasklet_schedule(&cfv->tx_release_tasklet); 1460d2e1a29SErwan Yvin } 1470d2e1a29SErwan Yvin 1480d2e1a29SErwan Yvin static void free_buf_info(struct cfv_info *cfv, struct buf_info *buf_info) 1490d2e1a29SErwan Yvin { 1500d2e1a29SErwan Yvin if (!buf_info) 1510d2e1a29SErwan Yvin return; 1520d2e1a29SErwan Yvin gen_pool_free(cfv->genpool, (unsigned long) buf_info->vaddr, 1530d2e1a29SErwan Yvin buf_info->size); 1540d2e1a29SErwan Yvin kfree(buf_info); 1550d2e1a29SErwan Yvin } 1560d2e1a29SErwan Yvin 1570d2e1a29SErwan Yvin /* This is invoked whenever the remote processor completed processing 1580d2e1a29SErwan Yvin * a TX msg we just sent, and the buffer is put back to the used ring. 1590d2e1a29SErwan Yvin */ 1600d2e1a29SErwan Yvin static void cfv_release_used_buf(struct virtqueue *vq_tx) 1610d2e1a29SErwan Yvin { 1620d2e1a29SErwan Yvin struct cfv_info *cfv = vq_tx->vdev->priv; 1630d2e1a29SErwan Yvin unsigned long flags; 1640d2e1a29SErwan Yvin 1650d2e1a29SErwan Yvin BUG_ON(vq_tx != cfv->vq_tx); 1660d2e1a29SErwan Yvin 1670d2e1a29SErwan Yvin for (;;) { 1680d2e1a29SErwan Yvin unsigned int len; 1690d2e1a29SErwan Yvin struct buf_info *buf_info; 1700d2e1a29SErwan Yvin 1710d2e1a29SErwan Yvin /* Get used buffer from used ring to recycle used descriptors */ 1720d2e1a29SErwan Yvin spin_lock_irqsave(&cfv->tx_lock, flags); 1730d2e1a29SErwan Yvin buf_info = virtqueue_get_buf(vq_tx, &len); 1740d2e1a29SErwan Yvin spin_unlock_irqrestore(&cfv->tx_lock, flags); 1750d2e1a29SErwan Yvin 1760d2e1a29SErwan Yvin /* Stop looping if there are no more buffers to free */ 1770d2e1a29SErwan Yvin if (!buf_info) 1780d2e1a29SErwan Yvin break; 1790d2e1a29SErwan Yvin 1800d2e1a29SErwan Yvin free_buf_info(cfv, buf_info); 1810d2e1a29SErwan Yvin 1820d2e1a29SErwan Yvin /* watermark_tx indicates if we previously stopped the tx 1830d2e1a29SErwan Yvin * queues. If we have enough free stots in the virtio ring, 1840d2e1a29SErwan Yvin * re-establish memory reserved and open up tx queues. 1850d2e1a29SErwan Yvin */ 1860d2e1a29SErwan Yvin if (cfv->vq_tx->num_free <= cfv->watermark_tx) 1870d2e1a29SErwan Yvin continue; 1880d2e1a29SErwan Yvin 1890d2e1a29SErwan Yvin /* Re-establish memory reserve */ 1900d2e1a29SErwan Yvin if (cfv->reserved_mem == 0 && cfv->genpool) 1910d2e1a29SErwan Yvin cfv->reserved_mem = 1920d2e1a29SErwan Yvin gen_pool_alloc(cfv->genpool, 1930d2e1a29SErwan Yvin cfv->reserved_size); 1940d2e1a29SErwan Yvin 1950d2e1a29SErwan Yvin /* Open up the tx queues */ 1960d2e1a29SErwan Yvin if (cfv->reserved_mem) { 1970d2e1a29SErwan Yvin cfv->watermark_tx = 1980d2e1a29SErwan Yvin virtqueue_get_vring_size(cfv->vq_tx); 1990d2e1a29SErwan Yvin netif_tx_wake_all_queues(cfv->ndev); 2000d2e1a29SErwan Yvin /* Buffers are recycled in cfv_netdev_tx, so 2010d2e1a29SErwan Yvin * disable notifications when queues are opened. 2020d2e1a29SErwan Yvin */ 2030d2e1a29SErwan Yvin virtqueue_disable_cb(cfv->vq_tx); 2040d2e1a29SErwan Yvin ++cfv->stats.tx_flow_on; 2050d2e1a29SErwan Yvin } else { 2060d2e1a29SErwan Yvin /* if no memory reserve, wait for more free slots */ 2070d2e1a29SErwan Yvin WARN_ON(cfv->watermark_tx > 2080d2e1a29SErwan Yvin virtqueue_get_vring_size(cfv->vq_tx)); 2090d2e1a29SErwan Yvin cfv->watermark_tx += 2100d2e1a29SErwan Yvin virtqueue_get_vring_size(cfv->vq_tx) / 4; 2110d2e1a29SErwan Yvin } 2120d2e1a29SErwan Yvin } 2130d2e1a29SErwan Yvin } 2140d2e1a29SErwan Yvin 2150d2e1a29SErwan Yvin /* Allocate a SKB and copy packet data to it */ 2160d2e1a29SErwan Yvin static struct sk_buff *cfv_alloc_and_copy_skb(int *err, 2170d2e1a29SErwan Yvin struct cfv_info *cfv, 2180d2e1a29SErwan Yvin u8 *frm, u32 frm_len) 2190d2e1a29SErwan Yvin { 2200d2e1a29SErwan Yvin struct sk_buff *skb; 2210d2e1a29SErwan Yvin u32 cfpkt_len, pad_len; 2220d2e1a29SErwan Yvin 2230d2e1a29SErwan Yvin *err = 0; 2240d2e1a29SErwan Yvin /* Verify that packet size with down-link header and mtu size */ 2250d2e1a29SErwan Yvin if (frm_len > cfv->mru || frm_len <= cfv->rx_hr + cfv->rx_tr) { 2260d2e1a29SErwan Yvin netdev_err(cfv->ndev, 2270d2e1a29SErwan Yvin "Invalid frmlen:%u mtu:%u hr:%d tr:%d\n", 2280d2e1a29SErwan Yvin frm_len, cfv->mru, cfv->rx_hr, 2290d2e1a29SErwan Yvin cfv->rx_tr); 2300d2e1a29SErwan Yvin *err = -EPROTO; 2310d2e1a29SErwan Yvin return NULL; 2320d2e1a29SErwan Yvin } 2330d2e1a29SErwan Yvin 2340d2e1a29SErwan Yvin cfpkt_len = frm_len - (cfv->rx_hr + cfv->rx_tr); 2350d2e1a29SErwan Yvin pad_len = (unsigned long)(frm + cfv->rx_hr) & (IP_HDR_ALIGN - 1); 2360d2e1a29SErwan Yvin 2370d2e1a29SErwan Yvin skb = netdev_alloc_skb(cfv->ndev, frm_len + pad_len); 2380d2e1a29SErwan Yvin if (!skb) { 2390d2e1a29SErwan Yvin *err = -ENOMEM; 2400d2e1a29SErwan Yvin return NULL; 2410d2e1a29SErwan Yvin } 2420d2e1a29SErwan Yvin 2430d2e1a29SErwan Yvin skb_reserve(skb, cfv->rx_hr + pad_len); 2440d2e1a29SErwan Yvin 24559ae1d12SJohannes Berg skb_put_data(skb, frm + cfv->rx_hr, cfpkt_len); 2460d2e1a29SErwan Yvin return skb; 2470d2e1a29SErwan Yvin } 2480d2e1a29SErwan Yvin 2490d2e1a29SErwan Yvin /* Get packets from the host vring */ 2500d2e1a29SErwan Yvin static int cfv_rx_poll(struct napi_struct *napi, int quota) 2510d2e1a29SErwan Yvin { 2520d2e1a29SErwan Yvin struct cfv_info *cfv = container_of(napi, struct cfv_info, napi); 2530d2e1a29SErwan Yvin int rxcnt = 0; 2540d2e1a29SErwan Yvin int err = 0; 2550d2e1a29SErwan Yvin void *buf; 2560d2e1a29SErwan Yvin struct sk_buff *skb; 2570d2e1a29SErwan Yvin struct vringh_kiov *riov = &cfv->ctx.riov; 2580d2e1a29SErwan Yvin unsigned int skb_len; 2590d2e1a29SErwan Yvin 2600d2e1a29SErwan Yvin do { 2610d2e1a29SErwan Yvin skb = NULL; 2620d2e1a29SErwan Yvin 2630d2e1a29SErwan Yvin /* Put the previous iovec back on the used ring and 2640d2e1a29SErwan Yvin * fetch a new iovec if we have processed all elements. 2650d2e1a29SErwan Yvin */ 2660d2e1a29SErwan Yvin if (riov->i == riov->used) { 2670d2e1a29SErwan Yvin if (cfv->ctx.head != USHRT_MAX) { 2680d2e1a29SErwan Yvin vringh_complete_kern(cfv->vr_rx, 2690d2e1a29SErwan Yvin cfv->ctx.head, 2700d2e1a29SErwan Yvin 0); 2710d2e1a29SErwan Yvin cfv->ctx.head = USHRT_MAX; 2720d2e1a29SErwan Yvin } 2730d2e1a29SErwan Yvin 2740d2e1a29SErwan Yvin err = vringh_getdesc_kern( 2750d2e1a29SErwan Yvin cfv->vr_rx, 2760d2e1a29SErwan Yvin riov, 2770d2e1a29SErwan Yvin NULL, 2780d2e1a29SErwan Yvin &cfv->ctx.head, 2790d2e1a29SErwan Yvin GFP_ATOMIC); 2800d2e1a29SErwan Yvin 2810d2e1a29SErwan Yvin if (err <= 0) 2820d2e1a29SErwan Yvin goto exit; 2830d2e1a29SErwan Yvin } 2840d2e1a29SErwan Yvin 2850d2e1a29SErwan Yvin buf = phys_to_virt((unsigned long) riov->iov[riov->i].iov_base); 2860d2e1a29SErwan Yvin /* TODO: Add check on valid buffer address */ 2870d2e1a29SErwan Yvin 2880d2e1a29SErwan Yvin skb = cfv_alloc_and_copy_skb(&err, cfv, buf, 2890d2e1a29SErwan Yvin riov->iov[riov->i].iov_len); 2900d2e1a29SErwan Yvin if (unlikely(err)) 2910d2e1a29SErwan Yvin goto exit; 2920d2e1a29SErwan Yvin 2930d2e1a29SErwan Yvin /* Push received packet up the stack. */ 2940d2e1a29SErwan Yvin skb_len = skb->len; 2950d2e1a29SErwan Yvin skb->protocol = htons(ETH_P_CAIF); 2960d2e1a29SErwan Yvin skb_reset_mac_header(skb); 2970d2e1a29SErwan Yvin skb->dev = cfv->ndev; 2980d2e1a29SErwan Yvin err = netif_receive_skb(skb); 2990d2e1a29SErwan Yvin if (unlikely(err)) { 3000d2e1a29SErwan Yvin ++cfv->ndev->stats.rx_dropped; 3010d2e1a29SErwan Yvin } else { 3020d2e1a29SErwan Yvin ++cfv->ndev->stats.rx_packets; 3030d2e1a29SErwan Yvin cfv->ndev->stats.rx_bytes += skb_len; 3040d2e1a29SErwan Yvin } 3050d2e1a29SErwan Yvin 3060d2e1a29SErwan Yvin ++riov->i; 3070d2e1a29SErwan Yvin ++rxcnt; 3080d2e1a29SErwan Yvin } while (rxcnt < quota); 3090d2e1a29SErwan Yvin 3100d2e1a29SErwan Yvin ++cfv->stats.rx_napi_resched; 3110d2e1a29SErwan Yvin goto out; 3120d2e1a29SErwan Yvin 3130d2e1a29SErwan Yvin exit: 3140d2e1a29SErwan Yvin switch (err) { 3150d2e1a29SErwan Yvin case 0: 3160d2e1a29SErwan Yvin ++cfv->stats.rx_napi_complete; 3170d2e1a29SErwan Yvin 318ae8102b8SWang Hai /* Really out of packets? (stolen from virtio_net)*/ 3190d2e1a29SErwan Yvin napi_complete(napi); 320b2273be8SSjur Brændeland if (unlikely(!vringh_notify_enable_kern(cfv->vr_rx)) && 3210d2e1a29SErwan Yvin napi_schedule_prep(napi)) { 3220d2e1a29SErwan Yvin vringh_notify_disable_kern(cfv->vr_rx); 3230d2e1a29SErwan Yvin __napi_schedule(napi); 3240d2e1a29SErwan Yvin } 3250d2e1a29SErwan Yvin break; 3260d2e1a29SErwan Yvin 3270d2e1a29SErwan Yvin case -ENOMEM: 3280d2e1a29SErwan Yvin ++cfv->stats.rx_nomem; 3290d2e1a29SErwan Yvin dev_kfree_skb(skb); 3300d2e1a29SErwan Yvin /* Stop NAPI poll on OOM, we hope to be polled later */ 3310d2e1a29SErwan Yvin napi_complete(napi); 3320d2e1a29SErwan Yvin vringh_notify_enable_kern(cfv->vr_rx); 3330d2e1a29SErwan Yvin break; 3340d2e1a29SErwan Yvin 3350d2e1a29SErwan Yvin default: 3360d2e1a29SErwan Yvin /* We're doomed, any modem fault is fatal */ 3370d2e1a29SErwan Yvin netdev_warn(cfv->ndev, "Bad ring, disable device\n"); 3380d2e1a29SErwan Yvin cfv->ndev->stats.rx_dropped = riov->used - riov->i; 3390d2e1a29SErwan Yvin napi_complete(napi); 3400d2e1a29SErwan Yvin vringh_notify_disable_kern(cfv->vr_rx); 3410d2e1a29SErwan Yvin netif_carrier_off(cfv->ndev); 3420d2e1a29SErwan Yvin break; 3430d2e1a29SErwan Yvin } 3440d2e1a29SErwan Yvin out: 3450d2e1a29SErwan Yvin if (rxcnt && vringh_need_notify_kern(cfv->vr_rx) > 0) 3460d2e1a29SErwan Yvin vringh_notify(cfv->vr_rx); 3470d2e1a29SErwan Yvin return rxcnt; 3480d2e1a29SErwan Yvin } 3490d2e1a29SErwan Yvin 3500d2e1a29SErwan Yvin static void cfv_recv(struct virtio_device *vdev, struct vringh *vr_rx) 3510d2e1a29SErwan Yvin { 3520d2e1a29SErwan Yvin struct cfv_info *cfv = vdev->priv; 3530d2e1a29SErwan Yvin 3540d2e1a29SErwan Yvin ++cfv->stats.rx_kicks; 3550d2e1a29SErwan Yvin vringh_notify_disable_kern(cfv->vr_rx); 3560d2e1a29SErwan Yvin napi_schedule(&cfv->napi); 3570d2e1a29SErwan Yvin } 3580d2e1a29SErwan Yvin 3590d2e1a29SErwan Yvin static void cfv_destroy_genpool(struct cfv_info *cfv) 3600d2e1a29SErwan Yvin { 3610d2e1a29SErwan Yvin if (cfv->alloc_addr) 3620d2e1a29SErwan Yvin dma_free_coherent(cfv->vdev->dev.parent->parent, 3630d2e1a29SErwan Yvin cfv->allocsz, cfv->alloc_addr, 3640d2e1a29SErwan Yvin cfv->alloc_dma); 3650d2e1a29SErwan Yvin 3660d2e1a29SErwan Yvin if (!cfv->genpool) 3670d2e1a29SErwan Yvin return; 3680d2e1a29SErwan Yvin gen_pool_free(cfv->genpool, cfv->reserved_mem, 3690d2e1a29SErwan Yvin cfv->reserved_size); 3700d2e1a29SErwan Yvin gen_pool_destroy(cfv->genpool); 3710d2e1a29SErwan Yvin cfv->genpool = NULL; 3720d2e1a29SErwan Yvin } 3730d2e1a29SErwan Yvin 3740d2e1a29SErwan Yvin static int cfv_create_genpool(struct cfv_info *cfv) 3750d2e1a29SErwan Yvin { 3760d2e1a29SErwan Yvin int err; 3770d2e1a29SErwan Yvin 3780d2e1a29SErwan Yvin /* dma_alloc can only allocate whole pages, and we need a more 3790d2e1a29SErwan Yvin * fine graned allocation so we use genpool. We ask for space needed 3800d2e1a29SErwan Yvin * by IP and a full ring. If the dma allcoation fails we retry with a 3810d2e1a29SErwan Yvin * smaller allocation size. 3820d2e1a29SErwan Yvin */ 3830d2e1a29SErwan Yvin err = -ENOMEM; 3840d2e1a29SErwan Yvin cfv->allocsz = (virtqueue_get_vring_size(cfv->vq_tx) * 3850d2e1a29SErwan Yvin (ETH_DATA_LEN + cfv->tx_hr + cfv->tx_tr) * 11)/10; 3860d2e1a29SErwan Yvin if (cfv->allocsz <= (num_possible_cpus() + 1) * cfv->ndev->mtu) 3870d2e1a29SErwan Yvin return -EINVAL; 3880d2e1a29SErwan Yvin 3890d2e1a29SErwan Yvin for (;;) { 3900d2e1a29SErwan Yvin if (cfv->allocsz <= num_possible_cpus() * cfv->ndev->mtu) { 3910d2e1a29SErwan Yvin netdev_info(cfv->ndev, "Not enough device memory\n"); 3920d2e1a29SErwan Yvin return -ENOMEM; 3930d2e1a29SErwan Yvin } 3940d2e1a29SErwan Yvin 3950d2e1a29SErwan Yvin cfv->alloc_addr = dma_alloc_coherent( 3960d2e1a29SErwan Yvin cfv->vdev->dev.parent->parent, 3970d2e1a29SErwan Yvin cfv->allocsz, &cfv->alloc_dma, 3980d2e1a29SErwan Yvin GFP_ATOMIC); 3990d2e1a29SErwan Yvin if (cfv->alloc_addr) 4000d2e1a29SErwan Yvin break; 4010d2e1a29SErwan Yvin 4020d2e1a29SErwan Yvin cfv->allocsz = (cfv->allocsz * 3) >> 2; 4030d2e1a29SErwan Yvin } 4040d2e1a29SErwan Yvin 4050d2e1a29SErwan Yvin netdev_dbg(cfv->ndev, "Allocated %zd bytes from dma-memory\n", 4060d2e1a29SErwan Yvin cfv->allocsz); 4070d2e1a29SErwan Yvin 4080d2e1a29SErwan Yvin /* Allocate on 128 bytes boundaries (1 << 7)*/ 4090d2e1a29SErwan Yvin cfv->genpool = gen_pool_create(7, -1); 4100d2e1a29SErwan Yvin if (!cfv->genpool) 4110d2e1a29SErwan Yvin goto err; 4120d2e1a29SErwan Yvin 4130d2e1a29SErwan Yvin err = gen_pool_add_virt(cfv->genpool, (unsigned long)cfv->alloc_addr, 4140d2e1a29SErwan Yvin (phys_addr_t)virt_to_phys(cfv->alloc_addr), 4150d2e1a29SErwan Yvin cfv->allocsz, -1); 4160d2e1a29SErwan Yvin if (err) 4170d2e1a29SErwan Yvin goto err; 4180d2e1a29SErwan Yvin 4190d2e1a29SErwan Yvin /* Reserve some memory for low memory situations. If we hit the roof 4200d2e1a29SErwan Yvin * in the memory pool, we stop TX flow and release the reserve. 4210d2e1a29SErwan Yvin */ 4220d2e1a29SErwan Yvin cfv->reserved_size = num_possible_cpus() * cfv->ndev->mtu; 4230d2e1a29SErwan Yvin cfv->reserved_mem = gen_pool_alloc(cfv->genpool, 4240d2e1a29SErwan Yvin cfv->reserved_size); 4251aef76e9SWei Yongjun if (!cfv->reserved_mem) { 4261aef76e9SWei Yongjun err = -ENOMEM; 4270d2e1a29SErwan Yvin goto err; 4281aef76e9SWei Yongjun } 4290d2e1a29SErwan Yvin 4300d2e1a29SErwan Yvin cfv->watermark_tx = virtqueue_get_vring_size(cfv->vq_tx); 4310d2e1a29SErwan Yvin return 0; 4320d2e1a29SErwan Yvin err: 4330d2e1a29SErwan Yvin cfv_destroy_genpool(cfv); 4340d2e1a29SErwan Yvin return err; 4350d2e1a29SErwan Yvin } 4360d2e1a29SErwan Yvin 4370d2e1a29SErwan Yvin /* Enable the CAIF interface and allocate the memory-pool */ 4380d2e1a29SErwan Yvin static int cfv_netdev_open(struct net_device *netdev) 4390d2e1a29SErwan Yvin { 4400d2e1a29SErwan Yvin struct cfv_info *cfv = netdev_priv(netdev); 4410d2e1a29SErwan Yvin 4420d2e1a29SErwan Yvin if (cfv_create_genpool(cfv)) 4430d2e1a29SErwan Yvin return -ENOMEM; 4440d2e1a29SErwan Yvin 4450d2e1a29SErwan Yvin netif_carrier_on(netdev); 4460d2e1a29SErwan Yvin napi_enable(&cfv->napi); 4470d2e1a29SErwan Yvin 4480d2e1a29SErwan Yvin /* Schedule NAPI to read any pending packets */ 4490d2e1a29SErwan Yvin napi_schedule(&cfv->napi); 4500d2e1a29SErwan Yvin return 0; 4510d2e1a29SErwan Yvin } 4520d2e1a29SErwan Yvin 4530d2e1a29SErwan Yvin /* Disable the CAIF interface and free the memory-pool */ 4540d2e1a29SErwan Yvin static int cfv_netdev_close(struct net_device *netdev) 4550d2e1a29SErwan Yvin { 4560d2e1a29SErwan Yvin struct cfv_info *cfv = netdev_priv(netdev); 4570d2e1a29SErwan Yvin unsigned long flags; 4580d2e1a29SErwan Yvin struct buf_info *buf_info; 4590d2e1a29SErwan Yvin 4600d2e1a29SErwan Yvin /* Disable interrupts, queues and NAPI polling */ 4610d2e1a29SErwan Yvin netif_carrier_off(netdev); 4620d2e1a29SErwan Yvin virtqueue_disable_cb(cfv->vq_tx); 4630d2e1a29SErwan Yvin vringh_notify_disable_kern(cfv->vr_rx); 4640d2e1a29SErwan Yvin napi_disable(&cfv->napi); 4650d2e1a29SErwan Yvin 466ae8102b8SWang Hai /* Release any TX buffers on both used and available rings */ 4670d2e1a29SErwan Yvin cfv_release_used_buf(cfv->vq_tx); 4680d2e1a29SErwan Yvin spin_lock_irqsave(&cfv->tx_lock, flags); 4690d2e1a29SErwan Yvin while ((buf_info = virtqueue_detach_unused_buf(cfv->vq_tx))) 4700d2e1a29SErwan Yvin free_buf_info(cfv, buf_info); 4710d2e1a29SErwan Yvin spin_unlock_irqrestore(&cfv->tx_lock, flags); 4720d2e1a29SErwan Yvin 4730d2e1a29SErwan Yvin /* Release all dma allocated memory and destroy the pool */ 4740d2e1a29SErwan Yvin cfv_destroy_genpool(cfv); 4750d2e1a29SErwan Yvin return 0; 4760d2e1a29SErwan Yvin } 4770d2e1a29SErwan Yvin 4780d2e1a29SErwan Yvin /* Allocate a buffer in dma-memory and copy skb to it */ 4790d2e1a29SErwan Yvin static struct buf_info *cfv_alloc_and_copy_to_shm(struct cfv_info *cfv, 4800d2e1a29SErwan Yvin struct sk_buff *skb, 4810d2e1a29SErwan Yvin struct scatterlist *sg) 4820d2e1a29SErwan Yvin { 4830d2e1a29SErwan Yvin struct caif_payload_info *info = (void *)&skb->cb; 4840d2e1a29SErwan Yvin struct buf_info *buf_info = NULL; 4850d2e1a29SErwan Yvin u8 pad_len, hdr_ofs; 4860d2e1a29SErwan Yvin 4870d2e1a29SErwan Yvin if (!cfv->genpool) 4880d2e1a29SErwan Yvin goto err; 4890d2e1a29SErwan Yvin 4900d2e1a29SErwan Yvin if (unlikely(cfv->tx_hr + skb->len + cfv->tx_tr > cfv->mtu)) { 4910d2e1a29SErwan Yvin netdev_warn(cfv->ndev, "Invalid packet len (%d > %d)\n", 4920d2e1a29SErwan Yvin cfv->tx_hr + skb->len + cfv->tx_tr, cfv->mtu); 4930d2e1a29SErwan Yvin goto err; 4940d2e1a29SErwan Yvin } 4950d2e1a29SErwan Yvin 4960d2e1a29SErwan Yvin buf_info = kmalloc(sizeof(struct buf_info), GFP_ATOMIC); 4970d2e1a29SErwan Yvin if (unlikely(!buf_info)) 4980d2e1a29SErwan Yvin goto err; 4990d2e1a29SErwan Yvin 500ae8102b8SWang Hai /* Make the IP header aligned in the buffer */ 5010d2e1a29SErwan Yvin hdr_ofs = cfv->tx_hr + info->hdr_len; 5020d2e1a29SErwan Yvin pad_len = hdr_ofs & (IP_HDR_ALIGN - 1); 5030d2e1a29SErwan Yvin buf_info->size = cfv->tx_hr + skb->len + cfv->tx_tr + pad_len; 5040d2e1a29SErwan Yvin 5050d2e1a29SErwan Yvin /* allocate dma memory buffer */ 5060d2e1a29SErwan Yvin buf_info->vaddr = (void *)gen_pool_alloc(cfv->genpool, buf_info->size); 5070d2e1a29SErwan Yvin if (unlikely(!buf_info->vaddr)) 5080d2e1a29SErwan Yvin goto err; 5090d2e1a29SErwan Yvin 5100d2e1a29SErwan Yvin /* copy skbuf contents to send buffer */ 5110d2e1a29SErwan Yvin skb_copy_bits(skb, 0, buf_info->vaddr + cfv->tx_hr + pad_len, skb->len); 5120d2e1a29SErwan Yvin sg_init_one(sg, buf_info->vaddr + pad_len, 5130d2e1a29SErwan Yvin skb->len + cfv->tx_hr + cfv->rx_hr); 5140d2e1a29SErwan Yvin 5150d2e1a29SErwan Yvin return buf_info; 5160d2e1a29SErwan Yvin err: 5170d2e1a29SErwan Yvin kfree(buf_info); 5180d2e1a29SErwan Yvin return NULL; 5190d2e1a29SErwan Yvin } 5200d2e1a29SErwan Yvin 5210d2e1a29SErwan Yvin /* Put the CAIF packet on the virtio ring and kick the receiver */ 522b8fc7090SLuc Van Oostenryck static netdev_tx_t cfv_netdev_tx(struct sk_buff *skb, struct net_device *netdev) 5230d2e1a29SErwan Yvin { 5240d2e1a29SErwan Yvin struct cfv_info *cfv = netdev_priv(netdev); 5250d2e1a29SErwan Yvin struct buf_info *buf_info; 5260d2e1a29SErwan Yvin struct scatterlist sg; 5270d2e1a29SErwan Yvin unsigned long flags; 5280d2e1a29SErwan Yvin bool flow_off = false; 5290d2e1a29SErwan Yvin int ret; 5300d2e1a29SErwan Yvin 5310d2e1a29SErwan Yvin /* garbage collect released buffers */ 5320d2e1a29SErwan Yvin cfv_release_used_buf(cfv->vq_tx); 5330d2e1a29SErwan Yvin spin_lock_irqsave(&cfv->tx_lock, flags); 5340d2e1a29SErwan Yvin 5350d2e1a29SErwan Yvin /* Flow-off check takes into account number of cpus to make sure 5360d2e1a29SErwan Yvin * virtqueue will not be overfilled in any possible smp conditions. 5370d2e1a29SErwan Yvin * 5380d2e1a29SErwan Yvin * Flow-on is triggered when sufficient buffers are freed 5390d2e1a29SErwan Yvin */ 5400d2e1a29SErwan Yvin if (unlikely(cfv->vq_tx->num_free <= num_present_cpus())) { 5410d2e1a29SErwan Yvin flow_off = true; 5420d2e1a29SErwan Yvin cfv->stats.tx_full_ring++; 5430d2e1a29SErwan Yvin } 5440d2e1a29SErwan Yvin 5450d2e1a29SErwan Yvin /* If we run out of memory, we release the memory reserve and retry 5460d2e1a29SErwan Yvin * allocation. 5470d2e1a29SErwan Yvin */ 5480d2e1a29SErwan Yvin buf_info = cfv_alloc_and_copy_to_shm(cfv, skb, &sg); 5490d2e1a29SErwan Yvin if (unlikely(!buf_info)) { 5500d2e1a29SErwan Yvin cfv->stats.tx_no_mem++; 5510d2e1a29SErwan Yvin flow_off = true; 5520d2e1a29SErwan Yvin 5530d2e1a29SErwan Yvin if (cfv->reserved_mem && cfv->genpool) { 5540d2e1a29SErwan Yvin gen_pool_free(cfv->genpool, cfv->reserved_mem, 5550d2e1a29SErwan Yvin cfv->reserved_size); 5560d2e1a29SErwan Yvin cfv->reserved_mem = 0; 5570d2e1a29SErwan Yvin buf_info = cfv_alloc_and_copy_to_shm(cfv, skb, &sg); 5580d2e1a29SErwan Yvin } 5590d2e1a29SErwan Yvin } 5600d2e1a29SErwan Yvin 5610d2e1a29SErwan Yvin if (unlikely(flow_off)) { 5620d2e1a29SErwan Yvin /* Turn flow on when a 1/4 of the descriptors are released */ 5630d2e1a29SErwan Yvin cfv->watermark_tx = virtqueue_get_vring_size(cfv->vq_tx) / 4; 5640d2e1a29SErwan Yvin /* Enable notifications of recycled TX buffers */ 5650d2e1a29SErwan Yvin virtqueue_enable_cb(cfv->vq_tx); 5660d2e1a29SErwan Yvin netif_tx_stop_all_queues(netdev); 5670d2e1a29SErwan Yvin } 5680d2e1a29SErwan Yvin 5690d2e1a29SErwan Yvin if (unlikely(!buf_info)) { 5700d2e1a29SErwan Yvin /* If the memory reserve does it's job, this shouldn't happen */ 5710d2e1a29SErwan Yvin netdev_warn(cfv->ndev, "Out of gen_pool memory\n"); 5720d2e1a29SErwan Yvin goto err; 5730d2e1a29SErwan Yvin } 5740d2e1a29SErwan Yvin 57571bcbeccSRusty Russell ret = virtqueue_add_outbuf(cfv->vq_tx, &sg, 1, buf_info, GFP_ATOMIC); 5760d2e1a29SErwan Yvin if (unlikely((ret < 0))) { 5770d2e1a29SErwan Yvin /* If flow control works, this shouldn't happen */ 5780d2e1a29SErwan Yvin netdev_warn(cfv->ndev, "Failed adding buffer to TX vring:%d\n", 5790d2e1a29SErwan Yvin ret); 5800d2e1a29SErwan Yvin goto err; 5810d2e1a29SErwan Yvin } 5820d2e1a29SErwan Yvin 5830d2e1a29SErwan Yvin /* update netdev statistics */ 5840d2e1a29SErwan Yvin cfv->ndev->stats.tx_packets++; 5850d2e1a29SErwan Yvin cfv->ndev->stats.tx_bytes += skb->len; 5860d2e1a29SErwan Yvin spin_unlock_irqrestore(&cfv->tx_lock, flags); 5870d2e1a29SErwan Yvin 5880d2e1a29SErwan Yvin /* tell the remote processor it has a pending message to read */ 5890d2e1a29SErwan Yvin virtqueue_kick(cfv->vq_tx); 5900d2e1a29SErwan Yvin 5910d2e1a29SErwan Yvin dev_kfree_skb(skb); 5920d2e1a29SErwan Yvin return NETDEV_TX_OK; 5930d2e1a29SErwan Yvin err: 5940d2e1a29SErwan Yvin spin_unlock_irqrestore(&cfv->tx_lock, flags); 5950d2e1a29SErwan Yvin cfv->ndev->stats.tx_dropped++; 5960d2e1a29SErwan Yvin free_buf_info(cfv, buf_info); 5970d2e1a29SErwan Yvin dev_kfree_skb(skb); 5980d2e1a29SErwan Yvin return NETDEV_TX_OK; 5990d2e1a29SErwan Yvin } 6000d2e1a29SErwan Yvin 601ca5ae9e4SEmil Renner Berthing static void cfv_tx_release_tasklet(struct tasklet_struct *t) 6020d2e1a29SErwan Yvin { 603ca5ae9e4SEmil Renner Berthing struct cfv_info *cfv = from_tasklet(cfv, t, tx_release_tasklet); 6040d2e1a29SErwan Yvin cfv_release_used_buf(cfv->vq_tx); 6050d2e1a29SErwan Yvin } 6060d2e1a29SErwan Yvin 6070d2e1a29SErwan Yvin static const struct net_device_ops cfv_netdev_ops = { 6080d2e1a29SErwan Yvin .ndo_open = cfv_netdev_open, 6090d2e1a29SErwan Yvin .ndo_stop = cfv_netdev_close, 6100d2e1a29SErwan Yvin .ndo_start_xmit = cfv_netdev_tx, 6110d2e1a29SErwan Yvin }; 6120d2e1a29SErwan Yvin 6130d2e1a29SErwan Yvin static void cfv_netdev_setup(struct net_device *netdev) 6140d2e1a29SErwan Yvin { 6150d2e1a29SErwan Yvin netdev->netdev_ops = &cfv_netdev_ops; 6160d2e1a29SErwan Yvin netdev->type = ARPHRD_CAIF; 6170d2e1a29SErwan Yvin netdev->tx_queue_len = 100; 6180d2e1a29SErwan Yvin netdev->flags = IFF_POINTOPOINT | IFF_NOARP; 6190d2e1a29SErwan Yvin netdev->mtu = CFV_DEF_MTU_SIZE; 620cf124db5SDavid S. Miller netdev->needs_free_netdev = true; 6210d2e1a29SErwan Yvin } 6220d2e1a29SErwan Yvin 6230d2e1a29SErwan Yvin /* Create debugfs counters for the device */ 6240d2e1a29SErwan Yvin static inline void debugfs_init(struct cfv_info *cfv) 6250d2e1a29SErwan Yvin { 62653f6f391SGreg Kroah-Hartman cfv->debugfs = debugfs_create_dir(netdev_name(cfv->ndev), NULL); 6270d2e1a29SErwan Yvin 628d61e4038SJoe Perches debugfs_create_u32("rx-napi-complete", 0400, cfv->debugfs, 6290d2e1a29SErwan Yvin &cfv->stats.rx_napi_complete); 630d61e4038SJoe Perches debugfs_create_u32("rx-napi-resched", 0400, cfv->debugfs, 6310d2e1a29SErwan Yvin &cfv->stats.rx_napi_resched); 632d61e4038SJoe Perches debugfs_create_u32("rx-nomem", 0400, cfv->debugfs, 6330d2e1a29SErwan Yvin &cfv->stats.rx_nomem); 634d61e4038SJoe Perches debugfs_create_u32("rx-kicks", 0400, cfv->debugfs, 6350d2e1a29SErwan Yvin &cfv->stats.rx_kicks); 636d61e4038SJoe Perches debugfs_create_u32("tx-full-ring", 0400, cfv->debugfs, 6370d2e1a29SErwan Yvin &cfv->stats.tx_full_ring); 638d61e4038SJoe Perches debugfs_create_u32("tx-no-mem", 0400, cfv->debugfs, 6390d2e1a29SErwan Yvin &cfv->stats.tx_no_mem); 640d61e4038SJoe Perches debugfs_create_u32("tx-kicks", 0400, cfv->debugfs, 6410d2e1a29SErwan Yvin &cfv->stats.tx_kicks); 642d61e4038SJoe Perches debugfs_create_u32("tx-flow-on", 0400, cfv->debugfs, 6430d2e1a29SErwan Yvin &cfv->stats.tx_flow_on); 6440d2e1a29SErwan Yvin } 6450d2e1a29SErwan Yvin 6460d2e1a29SErwan Yvin /* Setup CAIF for the a virtio device */ 6470d2e1a29SErwan Yvin static int cfv_probe(struct virtio_device *vdev) 6480d2e1a29SErwan Yvin { 6490d2e1a29SErwan Yvin vq_callback_t *vq_cbs = cfv_release_cb; 6500d2e1a29SErwan Yvin vrh_callback_t *vrh_cbs = cfv_recv; 6510d2e1a29SErwan Yvin const char *names = "output"; 6520d2e1a29SErwan Yvin const char *cfv_netdev_name = "cfvrt"; 6530d2e1a29SErwan Yvin struct net_device *netdev; 6540d2e1a29SErwan Yvin struct cfv_info *cfv; 655f1638a4cSJing Xiangfeng int err; 6560d2e1a29SErwan Yvin 6570d2e1a29SErwan Yvin netdev = alloc_netdev(sizeof(struct cfv_info), cfv_netdev_name, 658c835a677STom Gundersen NET_NAME_UNKNOWN, cfv_netdev_setup); 6590d2e1a29SErwan Yvin if (!netdev) 6600d2e1a29SErwan Yvin return -ENOMEM; 6610d2e1a29SErwan Yvin 6620d2e1a29SErwan Yvin cfv = netdev_priv(netdev); 6630d2e1a29SErwan Yvin cfv->vdev = vdev; 6640d2e1a29SErwan Yvin cfv->ndev = netdev; 6650d2e1a29SErwan Yvin 6660d2e1a29SErwan Yvin spin_lock_init(&cfv->tx_lock); 6670d2e1a29SErwan Yvin 6680d2e1a29SErwan Yvin /* Get the RX virtio ring. This is a "host side vring". */ 669a8c7687bSSjur Brændeland err = -ENODEV; 670a8c7687bSSjur Brændeland if (!vdev->vringh_config || !vdev->vringh_config->find_vrhs) 671a8c7687bSSjur Brændeland goto err; 672a8c7687bSSjur Brændeland 6730d2e1a29SErwan Yvin err = vdev->vringh_config->find_vrhs(vdev, 1, &cfv->vr_rx, &vrh_cbs); 6740d2e1a29SErwan Yvin if (err) 6750d2e1a29SErwan Yvin goto err; 6760d2e1a29SErwan Yvin 6770d2e1a29SErwan Yvin /* Get the TX virtio ring. This is a "guest side vring". */ 6789b2bbdb2SMichael S. Tsirkin err = virtio_find_vqs(vdev, 1, &cfv->vq_tx, &vq_cbs, &names, NULL); 6790d2e1a29SErwan Yvin if (err) 6800d2e1a29SErwan Yvin goto err; 6810d2e1a29SErwan Yvin 6820d2e1a29SErwan Yvin /* Get the CAIF configuration from virtio config space, if available */ 6830d2e1a29SErwan Yvin if (vdev->config->get) { 684855e0c52SRusty Russell virtio_cread(vdev, struct virtio_caif_transf_config, headroom, 685855e0c52SRusty Russell &cfv->tx_hr); 686855e0c52SRusty Russell virtio_cread(vdev, struct virtio_caif_transf_config, headroom, 687855e0c52SRusty Russell &cfv->rx_hr); 688855e0c52SRusty Russell virtio_cread(vdev, struct virtio_caif_transf_config, tailroom, 689855e0c52SRusty Russell &cfv->tx_tr); 690855e0c52SRusty Russell virtio_cread(vdev, struct virtio_caif_transf_config, tailroom, 691855e0c52SRusty Russell &cfv->rx_tr); 692855e0c52SRusty Russell virtio_cread(vdev, struct virtio_caif_transf_config, mtu, 693855e0c52SRusty Russell &cfv->mtu); 694855e0c52SRusty Russell virtio_cread(vdev, struct virtio_caif_transf_config, mtu, 695855e0c52SRusty Russell &cfv->mru); 6960d2e1a29SErwan Yvin } else { 6970d2e1a29SErwan Yvin cfv->tx_hr = CFV_DEF_HEADROOM; 6980d2e1a29SErwan Yvin cfv->rx_hr = CFV_DEF_HEADROOM; 6990d2e1a29SErwan Yvin cfv->tx_tr = CFV_DEF_TAILROOM; 7000d2e1a29SErwan Yvin cfv->rx_tr = CFV_DEF_TAILROOM; 7010d2e1a29SErwan Yvin cfv->mtu = CFV_DEF_MTU_SIZE; 7020d2e1a29SErwan Yvin cfv->mru = CFV_DEF_MTU_SIZE; 7030d2e1a29SErwan Yvin } 7040d2e1a29SErwan Yvin 7050d2e1a29SErwan Yvin netdev->needed_headroom = cfv->tx_hr; 7060d2e1a29SErwan Yvin netdev->needed_tailroom = cfv->tx_tr; 7070d2e1a29SErwan Yvin 7080d2e1a29SErwan Yvin /* Disable buffer release interrupts unless we have stopped TX queues */ 7090d2e1a29SErwan Yvin virtqueue_disable_cb(cfv->vq_tx); 7100d2e1a29SErwan Yvin 7110d2e1a29SErwan Yvin netdev->mtu = cfv->mtu - cfv->tx_tr; 7120d2e1a29SErwan Yvin vdev->priv = cfv; 7130d2e1a29SErwan Yvin 7140d2e1a29SErwan Yvin /* Initialize NAPI poll context data */ 7150d2e1a29SErwan Yvin vringh_kiov_init(&cfv->ctx.riov, NULL, 0); 7160d2e1a29SErwan Yvin cfv->ctx.head = USHRT_MAX; 717*be8af67fSJakub Kicinski netif_napi_add_weight(netdev, &cfv->napi, cfv_rx_poll, 718*be8af67fSJakub Kicinski CFV_DEFAULT_QUOTA); 7190d2e1a29SErwan Yvin 720ca5ae9e4SEmil Renner Berthing tasklet_setup(&cfv->tx_release_tasklet, cfv_tx_release_tasklet); 7210d2e1a29SErwan Yvin 7220d2e1a29SErwan Yvin /* Carrier is off until netdevice is opened */ 7230d2e1a29SErwan Yvin netif_carrier_off(netdev); 7240d2e1a29SErwan Yvin 7250d2e1a29SErwan Yvin /* register Netdev */ 7260d2e1a29SErwan Yvin err = register_netdev(netdev); 7270d2e1a29SErwan Yvin if (err) { 7280d2e1a29SErwan Yvin dev_err(&vdev->dev, "Unable to register netdev (%d)\n", err); 7290d2e1a29SErwan Yvin goto err; 7300d2e1a29SErwan Yvin } 7310d2e1a29SErwan Yvin 7320d2e1a29SErwan Yvin debugfs_init(cfv); 7330d2e1a29SErwan Yvin 7340d2e1a29SErwan Yvin return 0; 7350d2e1a29SErwan Yvin err: 7360d2e1a29SErwan Yvin netdev_warn(cfv->ndev, "CAIF Virtio probe failed:%d\n", err); 7370d2e1a29SErwan Yvin 7380d2e1a29SErwan Yvin if (cfv->vr_rx) 7390d2e1a29SErwan Yvin vdev->vringh_config->del_vrhs(cfv->vdev); 7400d2e1a29SErwan Yvin if (cfv->vdev) 7410d2e1a29SErwan Yvin vdev->config->del_vqs(cfv->vdev); 7420d2e1a29SErwan Yvin free_netdev(netdev); 7430d2e1a29SErwan Yvin return err; 7440d2e1a29SErwan Yvin } 7450d2e1a29SErwan Yvin 7460d2e1a29SErwan Yvin static void cfv_remove(struct virtio_device *vdev) 7470d2e1a29SErwan Yvin { 7480d2e1a29SErwan Yvin struct cfv_info *cfv = vdev->priv; 7490d2e1a29SErwan Yvin 7500d2e1a29SErwan Yvin rtnl_lock(); 7510d2e1a29SErwan Yvin dev_close(cfv->ndev); 7520d2e1a29SErwan Yvin rtnl_unlock(); 7530d2e1a29SErwan Yvin 7540d2e1a29SErwan Yvin tasklet_kill(&cfv->tx_release_tasklet); 7550d2e1a29SErwan Yvin debugfs_remove_recursive(cfv->debugfs); 7560d2e1a29SErwan Yvin 7570d2e1a29SErwan Yvin vringh_kiov_cleanup(&cfv->ctx.riov); 758d9679d00SMichael S. Tsirkin virtio_reset_device(vdev); 7590d2e1a29SErwan Yvin vdev->vringh_config->del_vrhs(cfv->vdev); 7600d2e1a29SErwan Yvin cfv->vr_rx = NULL; 7610d2e1a29SErwan Yvin vdev->config->del_vqs(cfv->vdev); 7620d2e1a29SErwan Yvin unregister_netdev(cfv->ndev); 7630d2e1a29SErwan Yvin } 7640d2e1a29SErwan Yvin 7650d2e1a29SErwan Yvin static struct virtio_device_id id_table[] = { 7660d2e1a29SErwan Yvin { VIRTIO_ID_CAIF, VIRTIO_DEV_ANY_ID }, 7670d2e1a29SErwan Yvin { 0 }, 7680d2e1a29SErwan Yvin }; 7690d2e1a29SErwan Yvin 7700d2e1a29SErwan Yvin static unsigned int features[] = { 7710d2e1a29SErwan Yvin }; 7720d2e1a29SErwan Yvin 7730d2e1a29SErwan Yvin static struct virtio_driver caif_virtio_driver = { 7740d2e1a29SErwan Yvin .feature_table = features, 7750d2e1a29SErwan Yvin .feature_table_size = ARRAY_SIZE(features), 7760d2e1a29SErwan Yvin .driver.name = KBUILD_MODNAME, 7770d2e1a29SErwan Yvin .driver.owner = THIS_MODULE, 7780d2e1a29SErwan Yvin .id_table = id_table, 7790d2e1a29SErwan Yvin .probe = cfv_probe, 7800d2e1a29SErwan Yvin .remove = cfv_remove, 7810d2e1a29SErwan Yvin }; 7820d2e1a29SErwan Yvin 7830d2e1a29SErwan Yvin module_virtio_driver(caif_virtio_driver); 7840d2e1a29SErwan Yvin MODULE_DEVICE_TABLE(virtio, id_table); 785