148925e37SRusty Russell /* A network driver using virtio. 2296f96fcSRusty Russell * 3296f96fcSRusty Russell * Copyright 2007 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation 4296f96fcSRusty Russell * 5296f96fcSRusty Russell * This program is free software; you can redistribute it and/or modify 6296f96fcSRusty Russell * it under the terms of the GNU General Public License as published by 7296f96fcSRusty Russell * the Free Software Foundation; either version 2 of the License, or 8296f96fcSRusty Russell * (at your option) any later version. 9296f96fcSRusty Russell * 10296f96fcSRusty Russell * This program is distributed in the hope that it will be useful, 11296f96fcSRusty Russell * but WITHOUT ANY WARRANTY; without even the implied warranty of 12296f96fcSRusty Russell * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13296f96fcSRusty Russell * GNU General Public License for more details. 14296f96fcSRusty Russell * 15296f96fcSRusty Russell * You should have received a copy of the GNU General Public License 16adf8d3ffSJeff Kirsher * along with this program; if not, see <http://www.gnu.org/licenses/>. 17296f96fcSRusty Russell */ 18296f96fcSRusty Russell //#define DEBUG 19296f96fcSRusty Russell #include <linux/netdevice.h> 20296f96fcSRusty Russell #include <linux/etherdevice.h> 21a9ea3fc6SHerbert Xu #include <linux/ethtool.h> 22296f96fcSRusty Russell #include <linux/module.h> 23296f96fcSRusty Russell #include <linux/virtio.h> 24296f96fcSRusty Russell #include <linux/virtio_net.h> 25296f96fcSRusty Russell #include <linux/scatterlist.h> 26e918085aSAlex Williamson #include <linux/if_vlan.h> 275a0e3ad6STejun Heo #include <linux/slab.h> 288de4b2f3SWanlong Gao #include <linux/cpu.h> 29ab7db917SMichael Dalton #include <linux/average.h> 30296f96fcSRusty Russell 31d34710e3SAmerigo Wang static int napi_weight = NAPI_POLL_WEIGHT; 326c0cd7c0SDor Laor module_param(napi_weight, int, 0444); 336c0cd7c0SDor Laor 34eb939922SRusty Russell static bool csum = true, gso = true; 3534a48579SRusty Russell module_param(csum, bool, 0444); 3634a48579SRusty Russell module_param(gso, bool, 0444); 3734a48579SRusty Russell 38296f96fcSRusty Russell /* FIXME: MTU in config. */ 395061de36SMichael Dalton #define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN) 403f2c31d9SMark McLoughlin #define GOOD_COPY_LEN 128 41296f96fcSRusty Russell 42ab7db917SMichael Dalton /* Weight used for the RX packet size EWMA. The average packet size is used to 43ab7db917SMichael Dalton * determine the packet buffer size when refilling RX rings. As the entire RX 44ab7db917SMichael Dalton * ring may be refilled at once, the weight is chosen so that the EWMA will be 45ab7db917SMichael Dalton * insensitive to short-term, transient changes in packet size. 46ab7db917SMichael Dalton */ 47ab7db917SMichael Dalton #define RECEIVE_AVG_WEIGHT 64 48ab7db917SMichael Dalton 49ab7db917SMichael Dalton /* Minimum alignment for mergeable packet buffers. */ 50ab7db917SMichael Dalton #define MERGEABLE_BUFFER_ALIGN max(L1_CACHE_BYTES, 256) 51ab7db917SMichael Dalton 5266846048SRick Jones #define VIRTNET_DRIVER_VERSION "1.0.0" 532a41f71dSAlex Williamson 543fa2a1dfSstephen hemminger struct virtnet_stats { 5583a27052SEric Dumazet struct u64_stats_sync tx_syncp; 5683a27052SEric Dumazet struct u64_stats_sync rx_syncp; 573fa2a1dfSstephen hemminger u64 tx_bytes; 583fa2a1dfSstephen hemminger u64 tx_packets; 593fa2a1dfSstephen hemminger 603fa2a1dfSstephen hemminger u64 rx_bytes; 613fa2a1dfSstephen hemminger u64 rx_packets; 623fa2a1dfSstephen hemminger }; 633fa2a1dfSstephen hemminger 64e9d7417bSJason Wang /* Internal representation of a send virtqueue */ 65e9d7417bSJason Wang struct send_queue { 66e9d7417bSJason Wang /* Virtqueue associated with this send _queue */ 67e9d7417bSJason Wang struct virtqueue *vq; 68e9d7417bSJason Wang 69e9d7417bSJason Wang /* TX: fragments + linear part + virtio header */ 70e9d7417bSJason Wang struct scatterlist sg[MAX_SKB_FRAGS + 2]; 71986a4f4dSJason Wang 72986a4f4dSJason Wang /* Name of the send queue: output.$index */ 73986a4f4dSJason Wang char name[40]; 74e9d7417bSJason Wang }; 75e9d7417bSJason Wang 76e9d7417bSJason Wang /* Internal representation of a receive virtqueue */ 77e9d7417bSJason Wang struct receive_queue { 78e9d7417bSJason Wang /* Virtqueue associated with this receive_queue */ 79e9d7417bSJason Wang struct virtqueue *vq; 80e9d7417bSJason Wang 81296f96fcSRusty Russell struct napi_struct napi; 82296f96fcSRusty Russell 83e9d7417bSJason Wang /* Chain pages by the private ptr. */ 84e9d7417bSJason Wang struct page *pages; 85e9d7417bSJason Wang 86ab7db917SMichael Dalton /* Average packet length for mergeable receive buffers. */ 87ab7db917SMichael Dalton struct ewma mrg_avg_pkt_len; 88ab7db917SMichael Dalton 89fb51879dSMichael Dalton /* Page frag for packet buffer allocation. */ 90fb51879dSMichael Dalton struct page_frag alloc_frag; 91fb51879dSMichael Dalton 92e9d7417bSJason Wang /* RX: fragments + linear part + virtio header */ 93e9d7417bSJason Wang struct scatterlist sg[MAX_SKB_FRAGS + 2]; 94986a4f4dSJason Wang 95986a4f4dSJason Wang /* Name of this receive queue: input.$index */ 96986a4f4dSJason Wang char name[40]; 97e9d7417bSJason Wang }; 98e9d7417bSJason Wang 99e9d7417bSJason Wang struct virtnet_info { 100e9d7417bSJason Wang struct virtio_device *vdev; 101e9d7417bSJason Wang struct virtqueue *cvq; 102e9d7417bSJason Wang struct net_device *dev; 103986a4f4dSJason Wang struct send_queue *sq; 104986a4f4dSJason Wang struct receive_queue *rq; 105e9d7417bSJason Wang unsigned int status; 106e9d7417bSJason Wang 107986a4f4dSJason Wang /* Max # of queue pairs supported by the device */ 108986a4f4dSJason Wang u16 max_queue_pairs; 109986a4f4dSJason Wang 110986a4f4dSJason Wang /* # of queue pairs currently used by the driver */ 111986a4f4dSJason Wang u16 curr_queue_pairs; 112986a4f4dSJason Wang 11397402b96SHerbert Xu /* I like... big packets and I cannot lie! */ 11497402b96SHerbert Xu bool big_packets; 11597402b96SHerbert Xu 1163f2c31d9SMark McLoughlin /* Host will merge rx buffers for big packets (shake it! shake it!) */ 1173f2c31d9SMark McLoughlin bool mergeable_rx_bufs; 1183f2c31d9SMark McLoughlin 119986a4f4dSJason Wang /* Has control virtqueue */ 120986a4f4dSJason Wang bool has_cvq; 121986a4f4dSJason Wang 122e7428e95SMichael S. Tsirkin /* Host can handle any s/g split between our header and packet data */ 123e7428e95SMichael S. Tsirkin bool any_header_sg; 124e7428e95SMichael S. Tsirkin 125586d17c5SJason Wang /* enable config space updates */ 126586d17c5SJason Wang bool config_enable; 127586d17c5SJason Wang 1283fa2a1dfSstephen hemminger /* Active statistics */ 1293fa2a1dfSstephen hemminger struct virtnet_stats __percpu *stats; 1303fa2a1dfSstephen hemminger 1313161e453SRusty Russell /* Work struct for refilling if we run low on memory. */ 1323161e453SRusty Russell struct delayed_work refill; 1333161e453SRusty Russell 134586d17c5SJason Wang /* Work struct for config space updates */ 135586d17c5SJason Wang struct work_struct config_work; 136586d17c5SJason Wang 137586d17c5SJason Wang /* Lock for config space updates */ 138586d17c5SJason Wang struct mutex config_lock; 139986a4f4dSJason Wang 140986a4f4dSJason Wang /* Does the affinity hint is set for virtqueues? */ 141986a4f4dSJason Wang bool affinity_hint_set; 14247be2479SWanlong Gao 1438de4b2f3SWanlong Gao /* CPU hot plug notifier */ 1448de4b2f3SWanlong Gao struct notifier_block nb; 145296f96fcSRusty Russell }; 146296f96fcSRusty Russell 147b3f24698SRusty Russell struct skb_vnet_hdr { 148b3f24698SRusty Russell union { 149b3f24698SRusty Russell struct virtio_net_hdr hdr; 150b3f24698SRusty Russell struct virtio_net_hdr_mrg_rxbuf mhdr; 151b3f24698SRusty Russell }; 152b3f24698SRusty Russell }; 153b3f24698SRusty Russell 1549ab86bbcSShirley Ma struct padded_vnet_hdr { 1559ab86bbcSShirley Ma struct virtio_net_hdr hdr; 1569ab86bbcSShirley Ma /* 1579ab86bbcSShirley Ma * virtio_net_hdr should be in a separated sg buffer because of a 1589ab86bbcSShirley Ma * QEMU bug, and data sg buffer shares same page with this header sg. 1599ab86bbcSShirley Ma * This padding makes next sg 16 byte aligned after virtio_net_hdr. 1609ab86bbcSShirley Ma */ 1619ab86bbcSShirley Ma char padding[6]; 1629ab86bbcSShirley Ma }; 1639ab86bbcSShirley Ma 164986a4f4dSJason Wang /* Converting between virtqueue no. and kernel tx/rx queue no. 165986a4f4dSJason Wang * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq 166986a4f4dSJason Wang */ 167986a4f4dSJason Wang static int vq2txq(struct virtqueue *vq) 168986a4f4dSJason Wang { 1699d0ca6edSRusty Russell return (vq->index - 1) / 2; 170986a4f4dSJason Wang } 171986a4f4dSJason Wang 172986a4f4dSJason Wang static int txq2vq(int txq) 173986a4f4dSJason Wang { 174986a4f4dSJason Wang return txq * 2 + 1; 175986a4f4dSJason Wang } 176986a4f4dSJason Wang 177986a4f4dSJason Wang static int vq2rxq(struct virtqueue *vq) 178986a4f4dSJason Wang { 1799d0ca6edSRusty Russell return vq->index / 2; 180986a4f4dSJason Wang } 181986a4f4dSJason Wang 182986a4f4dSJason Wang static int rxq2vq(int rxq) 183986a4f4dSJason Wang { 184986a4f4dSJason Wang return rxq * 2; 185986a4f4dSJason Wang } 186986a4f4dSJason Wang 187b3f24698SRusty Russell static inline struct skb_vnet_hdr *skb_vnet_hdr(struct sk_buff *skb) 188296f96fcSRusty Russell { 189b3f24698SRusty Russell return (struct skb_vnet_hdr *)skb->cb; 190296f96fcSRusty Russell } 191296f96fcSRusty Russell 1929ab86bbcSShirley Ma /* 1939ab86bbcSShirley Ma * private is used to chain pages for big packets, put the whole 1949ab86bbcSShirley Ma * most recent used list in the beginning for reuse 1959ab86bbcSShirley Ma */ 196e9d7417bSJason Wang static void give_pages(struct receive_queue *rq, struct page *page) 197fb6813f4SRusty Russell { 1989ab86bbcSShirley Ma struct page *end; 1999ab86bbcSShirley Ma 200e9d7417bSJason Wang /* Find end of list, sew whole thing into vi->rq.pages. */ 2019ab86bbcSShirley Ma for (end = page; end->private; end = (struct page *)end->private); 202e9d7417bSJason Wang end->private = (unsigned long)rq->pages; 203e9d7417bSJason Wang rq->pages = page; 204fb6813f4SRusty Russell } 205fb6813f4SRusty Russell 206e9d7417bSJason Wang static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask) 207fb6813f4SRusty Russell { 208e9d7417bSJason Wang struct page *p = rq->pages; 209fb6813f4SRusty Russell 2109ab86bbcSShirley Ma if (p) { 211e9d7417bSJason Wang rq->pages = (struct page *)p->private; 2129ab86bbcSShirley Ma /* clear private here, it is used to chain pages */ 2139ab86bbcSShirley Ma p->private = 0; 2149ab86bbcSShirley Ma } else 215fb6813f4SRusty Russell p = alloc_page(gfp_mask); 216fb6813f4SRusty Russell return p; 217fb6813f4SRusty Russell } 218fb6813f4SRusty Russell 219e9d7417bSJason Wang static void skb_xmit_done(struct virtqueue *vq) 220296f96fcSRusty Russell { 221e9d7417bSJason Wang struct virtnet_info *vi = vq->vdev->priv; 222296f96fcSRusty Russell 2232cb9c6baSRusty Russell /* Suppress further interrupts. */ 224e9d7417bSJason Wang virtqueue_disable_cb(vq); 22511a3a154SRusty Russell 226363f1514SRusty Russell /* We were probably waiting for more output buffers. */ 227986a4f4dSJason Wang netif_wake_subqueue(vi->dev, vq2txq(vq)); 228296f96fcSRusty Russell } 229296f96fcSRusty Russell 230ab7db917SMichael Dalton static unsigned int mergeable_ctx_to_buf_truesize(unsigned long mrg_ctx) 231ab7db917SMichael Dalton { 232ab7db917SMichael Dalton unsigned int truesize = mrg_ctx & (MERGEABLE_BUFFER_ALIGN - 1); 233ab7db917SMichael Dalton return (truesize + 1) * MERGEABLE_BUFFER_ALIGN; 234ab7db917SMichael Dalton } 235ab7db917SMichael Dalton 236ab7db917SMichael Dalton static void *mergeable_ctx_to_buf_address(unsigned long mrg_ctx) 237ab7db917SMichael Dalton { 238ab7db917SMichael Dalton return (void *)(mrg_ctx & -MERGEABLE_BUFFER_ALIGN); 239ab7db917SMichael Dalton 240ab7db917SMichael Dalton } 241ab7db917SMichael Dalton 242ab7db917SMichael Dalton static unsigned long mergeable_buf_to_ctx(void *buf, unsigned int truesize) 243ab7db917SMichael Dalton { 244ab7db917SMichael Dalton unsigned int size = truesize / MERGEABLE_BUFFER_ALIGN; 245ab7db917SMichael Dalton return (unsigned long)buf | (size - 1); 246ab7db917SMichael Dalton } 247ab7db917SMichael Dalton 2483464645aSMike Waychison /* Called from bottom half context */ 249e9d7417bSJason Wang static struct sk_buff *page_to_skb(struct receive_queue *rq, 2502613af0eSMichael Dalton struct page *page, unsigned int offset, 2512613af0eSMichael Dalton unsigned int len, unsigned int truesize) 2529ab86bbcSShirley Ma { 253e9d7417bSJason Wang struct virtnet_info *vi = rq->vq->vdev->priv; 2549ab86bbcSShirley Ma struct sk_buff *skb; 2559ab86bbcSShirley Ma struct skb_vnet_hdr *hdr; 2562613af0eSMichael Dalton unsigned int copy, hdr_len, hdr_padded_len; 2579ab86bbcSShirley Ma char *p; 2589ab86bbcSShirley Ma 2592613af0eSMichael Dalton p = page_address(page) + offset; 2609ab86bbcSShirley Ma 2619ab86bbcSShirley Ma /* copy small packet so we can reuse these pages for small data */ 2629ab86bbcSShirley Ma skb = netdev_alloc_skb_ip_align(vi->dev, GOOD_COPY_LEN); 2639ab86bbcSShirley Ma if (unlikely(!skb)) 2649ab86bbcSShirley Ma return NULL; 2659ab86bbcSShirley Ma 2669ab86bbcSShirley Ma hdr = skb_vnet_hdr(skb); 2679ab86bbcSShirley Ma 2683f2c31d9SMark McLoughlin if (vi->mergeable_rx_bufs) { 2699ab86bbcSShirley Ma hdr_len = sizeof hdr->mhdr; 2702613af0eSMichael Dalton hdr_padded_len = sizeof hdr->mhdr; 2719ab86bbcSShirley Ma } else { 2729ab86bbcSShirley Ma hdr_len = sizeof hdr->hdr; 2732613af0eSMichael Dalton hdr_padded_len = sizeof(struct padded_vnet_hdr); 2749ab86bbcSShirley Ma } 2753f2c31d9SMark McLoughlin 2769ab86bbcSShirley Ma memcpy(hdr, p, hdr_len); 2773f2c31d9SMark McLoughlin 2789ab86bbcSShirley Ma len -= hdr_len; 2792613af0eSMichael Dalton offset += hdr_padded_len; 2802613af0eSMichael Dalton p += hdr_padded_len; 2813f2c31d9SMark McLoughlin 2823f2c31d9SMark McLoughlin copy = len; 2833f2c31d9SMark McLoughlin if (copy > skb_tailroom(skb)) 2843f2c31d9SMark McLoughlin copy = skb_tailroom(skb); 2853f2c31d9SMark McLoughlin memcpy(skb_put(skb, copy), p, copy); 2863f2c31d9SMark McLoughlin 2873f2c31d9SMark McLoughlin len -= copy; 2889ab86bbcSShirley Ma offset += copy; 2893f2c31d9SMark McLoughlin 2902613af0eSMichael Dalton if (vi->mergeable_rx_bufs) { 2912613af0eSMichael Dalton if (len) 2922613af0eSMichael Dalton skb_add_rx_frag(skb, 0, page, offset, len, truesize); 2932613af0eSMichael Dalton else 2942613af0eSMichael Dalton put_page(page); 2952613af0eSMichael Dalton return skb; 2962613af0eSMichael Dalton } 2972613af0eSMichael Dalton 298e878d78bSSasha Levin /* 299e878d78bSSasha Levin * Verify that we can indeed put this data into a skb. 300e878d78bSSasha Levin * This is here to handle cases when the device erroneously 301e878d78bSSasha Levin * tries to receive more than is possible. This is usually 302e878d78bSSasha Levin * the case of a broken device. 303e878d78bSSasha Levin */ 304e878d78bSSasha Levin if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) { 305be443899SAmerigo Wang net_dbg_ratelimited("%s: too much data\n", skb->dev->name); 306e878d78bSSasha Levin dev_kfree_skb(skb); 307e878d78bSSasha Levin return NULL; 308e878d78bSSasha Levin } 3092613af0eSMichael Dalton BUG_ON(offset >= PAGE_SIZE); 3109ab86bbcSShirley Ma while (len) { 3112613af0eSMichael Dalton unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len); 3122613af0eSMichael Dalton skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset, 3132613af0eSMichael Dalton frag_size, truesize); 3142613af0eSMichael Dalton len -= frag_size; 3159ab86bbcSShirley Ma page = (struct page *)page->private; 3169ab86bbcSShirley Ma offset = 0; 3173f2c31d9SMark McLoughlin } 3183f2c31d9SMark McLoughlin 3199ab86bbcSShirley Ma if (page) 320e9d7417bSJason Wang give_pages(rq, page); 3213f2c31d9SMark McLoughlin 3229ab86bbcSShirley Ma return skb; 3239ab86bbcSShirley Ma } 3249ab86bbcSShirley Ma 325f121159dSMichael S. Tsirkin static struct sk_buff *receive_small(void *buf, unsigned int len) 326f121159dSMichael S. Tsirkin { 327f121159dSMichael S. Tsirkin struct sk_buff * skb = buf; 328f121159dSMichael S. Tsirkin 329f121159dSMichael S. Tsirkin len -= sizeof(struct virtio_net_hdr); 330f121159dSMichael S. Tsirkin skb_trim(skb, len); 331f121159dSMichael S. Tsirkin 332f121159dSMichael S. Tsirkin return skb; 333f121159dSMichael S. Tsirkin } 334f121159dSMichael S. Tsirkin 335f121159dSMichael S. Tsirkin static struct sk_buff *receive_big(struct net_device *dev, 336f121159dSMichael S. Tsirkin struct receive_queue *rq, 337f121159dSMichael S. Tsirkin void *buf, 338f121159dSMichael S. Tsirkin unsigned int len) 339f121159dSMichael S. Tsirkin { 340f121159dSMichael S. Tsirkin struct page *page = buf; 341f121159dSMichael S. Tsirkin struct sk_buff *skb = page_to_skb(rq, page, 0, len, PAGE_SIZE); 342f121159dSMichael S. Tsirkin 343f121159dSMichael S. Tsirkin if (unlikely(!skb)) 344f121159dSMichael S. Tsirkin goto err; 345f121159dSMichael S. Tsirkin 346f121159dSMichael S. Tsirkin return skb; 347f121159dSMichael S. Tsirkin 348f121159dSMichael S. Tsirkin err: 349f121159dSMichael S. Tsirkin dev->stats.rx_dropped++; 350f121159dSMichael S. Tsirkin give_pages(rq, page); 351f121159dSMichael S. Tsirkin return NULL; 352f121159dSMichael S. Tsirkin } 353f121159dSMichael S. Tsirkin 3548fc3b9e9SMichael S. Tsirkin static struct sk_buff *receive_mergeable(struct net_device *dev, 3558fc3b9e9SMichael S. Tsirkin struct receive_queue *rq, 356ab7db917SMichael Dalton unsigned long ctx, 3578fc3b9e9SMichael S. Tsirkin unsigned int len) 3589ab86bbcSShirley Ma { 359ab7db917SMichael Dalton void *buf = mergeable_ctx_to_buf_address(ctx); 3608fc3b9e9SMichael S. Tsirkin struct skb_vnet_hdr *hdr = buf; 3618fc3b9e9SMichael S. Tsirkin int num_buf = hdr->mhdr.num_buffers; 3628fc3b9e9SMichael S. Tsirkin struct page *page = virt_to_head_page(buf); 3638fc3b9e9SMichael S. Tsirkin int offset = buf - page_address(page); 364ab7db917SMichael Dalton unsigned int truesize = max(len, mergeable_ctx_to_buf_truesize(ctx)); 365ab7db917SMichael Dalton 366fb51879dSMichael Dalton struct sk_buff *head_skb = page_to_skb(rq, page, offset, len, truesize); 3672613af0eSMichael Dalton struct sk_buff *curr_skb = head_skb; 3689ab86bbcSShirley Ma 3698fc3b9e9SMichael S. Tsirkin if (unlikely(!curr_skb)) 3708fc3b9e9SMichael S. Tsirkin goto err_skb; 3719ab86bbcSShirley Ma while (--num_buf) { 3728fc3b9e9SMichael S. Tsirkin int num_skb_frags; 3738fc3b9e9SMichael S. Tsirkin 374ab7db917SMichael Dalton ctx = (unsigned long)virtqueue_get_buf(rq->vq, &len); 375ab7db917SMichael Dalton if (unlikely(!ctx)) { 3768fc3b9e9SMichael S. Tsirkin pr_debug("%s: rx error: %d buffers out of %d missing\n", 3778fc3b9e9SMichael S. Tsirkin dev->name, num_buf, hdr->mhdr.num_buffers); 3788fc3b9e9SMichael S. Tsirkin dev->stats.rx_length_errors++; 3798fc3b9e9SMichael S. Tsirkin goto err_buf; 3803f2c31d9SMark McLoughlin } 3818fc3b9e9SMichael S. Tsirkin 382ab7db917SMichael Dalton buf = mergeable_ctx_to_buf_address(ctx); 3838fc3b9e9SMichael S. Tsirkin page = virt_to_head_page(buf); 3848fc3b9e9SMichael S. Tsirkin 3858fc3b9e9SMichael S. Tsirkin num_skb_frags = skb_shinfo(curr_skb)->nr_frags; 3862613af0eSMichael Dalton if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) { 3872613af0eSMichael Dalton struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC); 3888fc3b9e9SMichael S. Tsirkin 3898fc3b9e9SMichael S. Tsirkin if (unlikely(!nskb)) 3908fc3b9e9SMichael S. Tsirkin goto err_skb; 3912613af0eSMichael Dalton if (curr_skb == head_skb) 3922613af0eSMichael Dalton skb_shinfo(curr_skb)->frag_list = nskb; 3932613af0eSMichael Dalton else 3942613af0eSMichael Dalton curr_skb->next = nskb; 3952613af0eSMichael Dalton curr_skb = nskb; 3962613af0eSMichael Dalton head_skb->truesize += nskb->truesize; 3972613af0eSMichael Dalton num_skb_frags = 0; 3982613af0eSMichael Dalton } 399ab7db917SMichael Dalton truesize = max(len, mergeable_ctx_to_buf_truesize(ctx)); 4002613af0eSMichael Dalton if (curr_skb != head_skb) { 4012613af0eSMichael Dalton head_skb->data_len += len; 4022613af0eSMichael Dalton head_skb->len += len; 403fb51879dSMichael Dalton head_skb->truesize += truesize; 4042613af0eSMichael Dalton } 4058fc3b9e9SMichael S. Tsirkin offset = buf - page_address(page); 406ba275241SJason Wang if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) { 407ba275241SJason Wang put_page(page); 408ba275241SJason Wang skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1, 409fb51879dSMichael Dalton len, truesize); 410ba275241SJason Wang } else { 4112613af0eSMichael Dalton skb_add_rx_frag(curr_skb, num_skb_frags, page, 412fb51879dSMichael Dalton offset, len, truesize); 413ba275241SJason Wang } 4148fc3b9e9SMichael S. Tsirkin } 4158fc3b9e9SMichael S. Tsirkin 416ab7db917SMichael Dalton ewma_add(&rq->mrg_avg_pkt_len, head_skb->len); 4178fc3b9e9SMichael S. Tsirkin return head_skb; 4188fc3b9e9SMichael S. Tsirkin 4198fc3b9e9SMichael S. Tsirkin err_skb: 4208fc3b9e9SMichael S. Tsirkin put_page(page); 4218fc3b9e9SMichael S. Tsirkin while (--num_buf) { 422ab7db917SMichael Dalton ctx = (unsigned long)virtqueue_get_buf(rq->vq, &len); 423ab7db917SMichael Dalton if (unlikely(!ctx)) { 4248fc3b9e9SMichael S. Tsirkin pr_debug("%s: rx error: %d buffers missing\n", 4258fc3b9e9SMichael S. Tsirkin dev->name, num_buf); 4268fc3b9e9SMichael S. Tsirkin dev->stats.rx_length_errors++; 4278fc3b9e9SMichael S. Tsirkin break; 4288fc3b9e9SMichael S. Tsirkin } 429ab7db917SMichael Dalton page = virt_to_head_page(mergeable_ctx_to_buf_address(ctx)); 4308fc3b9e9SMichael S. Tsirkin put_page(page); 4313f2c31d9SMark McLoughlin } 4328fc3b9e9SMichael S. Tsirkin err_buf: 4338fc3b9e9SMichael S. Tsirkin dev->stats.rx_dropped++; 4348fc3b9e9SMichael S. Tsirkin dev_kfree_skb(head_skb); 4358fc3b9e9SMichael S. Tsirkin return NULL; 4369ab86bbcSShirley Ma } 4379ab86bbcSShirley Ma 438e9d7417bSJason Wang static void receive_buf(struct receive_queue *rq, void *buf, unsigned int len) 4399ab86bbcSShirley Ma { 440e9d7417bSJason Wang struct virtnet_info *vi = rq->vq->vdev->priv; 441e9d7417bSJason Wang struct net_device *dev = vi->dev; 44258472a76SEric Dumazet struct virtnet_stats *stats = this_cpu_ptr(vi->stats); 4439ab86bbcSShirley Ma struct sk_buff *skb; 4449ab86bbcSShirley Ma struct skb_vnet_hdr *hdr; 4459ab86bbcSShirley Ma 4469ab86bbcSShirley Ma if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) { 4479ab86bbcSShirley Ma pr_debug("%s: short packet %i\n", dev->name, len); 4489ab86bbcSShirley Ma dev->stats.rx_length_errors++; 449ab7db917SMichael Dalton if (vi->mergeable_rx_bufs) { 450ab7db917SMichael Dalton unsigned long ctx = (unsigned long)buf; 451ab7db917SMichael Dalton void *base = mergeable_ctx_to_buf_address(ctx); 452ab7db917SMichael Dalton put_page(virt_to_head_page(base)); 453ab7db917SMichael Dalton } else if (vi->big_packets) { 45498bfd23cSMichael Dalton give_pages(rq, buf); 455ab7db917SMichael Dalton } else { 4569ab86bbcSShirley Ma dev_kfree_skb(buf); 457ab7db917SMichael Dalton } 4589ab86bbcSShirley Ma return; 4599ab86bbcSShirley Ma } 4609ab86bbcSShirley Ma 461f121159dSMichael S. Tsirkin if (vi->mergeable_rx_bufs) 462ab7db917SMichael Dalton skb = receive_mergeable(dev, rq, (unsigned long)buf, len); 463f121159dSMichael S. Tsirkin else if (vi->big_packets) 464f121159dSMichael S. Tsirkin skb = receive_big(dev, rq, buf, len); 465f121159dSMichael S. Tsirkin else 466f121159dSMichael S. Tsirkin skb = receive_small(buf, len); 467f121159dSMichael S. Tsirkin 4688fc3b9e9SMichael S. Tsirkin if (unlikely(!skb)) 4692613af0eSMichael Dalton return; 4703f2c31d9SMark McLoughlin 4719ab86bbcSShirley Ma hdr = skb_vnet_hdr(skb); 4723fa2a1dfSstephen hemminger 47383a27052SEric Dumazet u64_stats_update_begin(&stats->rx_syncp); 4743fa2a1dfSstephen hemminger stats->rx_bytes += skb->len; 4753fa2a1dfSstephen hemminger stats->rx_packets++; 47683a27052SEric Dumazet u64_stats_update_end(&stats->rx_syncp); 477296f96fcSRusty Russell 478b3f24698SRusty Russell if (hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) { 479296f96fcSRusty Russell pr_debug("Needs csum!\n"); 480b3f24698SRusty Russell if (!skb_partial_csum_set(skb, 481b3f24698SRusty Russell hdr->hdr.csum_start, 482b3f24698SRusty Russell hdr->hdr.csum_offset)) 483296f96fcSRusty Russell goto frame_err; 48410a8d94aSJason Wang } else if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID) { 48510a8d94aSJason Wang skb->ip_summed = CHECKSUM_UNNECESSARY; 486296f96fcSRusty Russell } 487296f96fcSRusty Russell 48823cde76dSMark McLoughlin skb->protocol = eth_type_trans(skb, dev); 48923cde76dSMark McLoughlin pr_debug("Receiving skb proto 0x%04x len %i type %i\n", 49023cde76dSMark McLoughlin ntohs(skb->protocol), skb->len, skb->pkt_type); 49123cde76dSMark McLoughlin 492b3f24698SRusty Russell if (hdr->hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) { 493296f96fcSRusty Russell pr_debug("GSO!\n"); 494b3f24698SRusty Russell switch (hdr->hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) { 495296f96fcSRusty Russell case VIRTIO_NET_HDR_GSO_TCPV4: 496c9af6db4SPravin B Shelar skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4; 497296f96fcSRusty Russell break; 498296f96fcSRusty Russell case VIRTIO_NET_HDR_GSO_UDP: 499c9af6db4SPravin B Shelar skb_shinfo(skb)->gso_type = SKB_GSO_UDP; 500296f96fcSRusty Russell break; 501296f96fcSRusty Russell case VIRTIO_NET_HDR_GSO_TCPV6: 502c9af6db4SPravin B Shelar skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6; 503296f96fcSRusty Russell break; 504296f96fcSRusty Russell default: 505be443899SAmerigo Wang net_warn_ratelimited("%s: bad gso type %u.\n", 506b3f24698SRusty Russell dev->name, hdr->hdr.gso_type); 507296f96fcSRusty Russell goto frame_err; 508296f96fcSRusty Russell } 509296f96fcSRusty Russell 510b3f24698SRusty Russell if (hdr->hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN) 511c9af6db4SPravin B Shelar skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN; 51234a48579SRusty Russell 513b3f24698SRusty Russell skb_shinfo(skb)->gso_size = hdr->hdr.gso_size; 514296f96fcSRusty Russell if (skb_shinfo(skb)->gso_size == 0) { 515be443899SAmerigo Wang net_warn_ratelimited("%s: zero gso size.\n", dev->name); 516296f96fcSRusty Russell goto frame_err; 517296f96fcSRusty Russell } 518296f96fcSRusty Russell 519296f96fcSRusty Russell /* Header must be checked, and gso_segs computed. */ 520296f96fcSRusty Russell skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY; 521296f96fcSRusty Russell skb_shinfo(skb)->gso_segs = 0; 522296f96fcSRusty Russell } 523296f96fcSRusty Russell 524296f96fcSRusty Russell netif_receive_skb(skb); 525296f96fcSRusty Russell return; 526296f96fcSRusty Russell 527296f96fcSRusty Russell frame_err: 528296f96fcSRusty Russell dev->stats.rx_frame_errors++; 529296f96fcSRusty Russell dev_kfree_skb(skb); 530296f96fcSRusty Russell } 531296f96fcSRusty Russell 532e9d7417bSJason Wang static int add_recvbuf_small(struct receive_queue *rq, gfp_t gfp) 533296f96fcSRusty Russell { 534e9d7417bSJason Wang struct virtnet_info *vi = rq->vq->vdev->priv; 535296f96fcSRusty Russell struct sk_buff *skb; 536b3f24698SRusty Russell struct skb_vnet_hdr *hdr; 5379ab86bbcSShirley Ma int err; 5383f2c31d9SMark McLoughlin 5395061de36SMichael Dalton skb = __netdev_alloc_skb_ip_align(vi->dev, GOOD_PACKET_LEN, gfp); 5409ab86bbcSShirley Ma if (unlikely(!skb)) 5419ab86bbcSShirley Ma return -ENOMEM; 542296f96fcSRusty Russell 5435061de36SMichael Dalton skb_put(skb, GOOD_PACKET_LEN); 5443f2c31d9SMark McLoughlin 5453f2c31d9SMark McLoughlin hdr = skb_vnet_hdr(skb); 546e9d7417bSJason Wang sg_set_buf(rq->sg, &hdr->hdr, sizeof hdr->hdr); 54797402b96SHerbert Xu 548e9d7417bSJason Wang skb_to_sgvec(skb, rq->sg + 1, 0, skb->len); 54997402b96SHerbert Xu 5509dc7b9e4SRusty Russell err = virtqueue_add_inbuf(rq->vq, rq->sg, 2, skb, gfp); 5519ab86bbcSShirley Ma if (err < 0) 5529ab86bbcSShirley Ma dev_kfree_skb(skb); 55397402b96SHerbert Xu 5549ab86bbcSShirley Ma return err; 55597402b96SHerbert Xu } 55697402b96SHerbert Xu 557e9d7417bSJason Wang static int add_recvbuf_big(struct receive_queue *rq, gfp_t gfp) 5589ab86bbcSShirley Ma { 5599ab86bbcSShirley Ma struct page *first, *list = NULL; 5609ab86bbcSShirley Ma char *p; 5619ab86bbcSShirley Ma int i, err, offset; 562296f96fcSRusty Russell 563e9d7417bSJason Wang /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */ 5649ab86bbcSShirley Ma for (i = MAX_SKB_FRAGS + 1; i > 1; --i) { 565e9d7417bSJason Wang first = get_a_page(rq, gfp); 5669ab86bbcSShirley Ma if (!first) { 5679ab86bbcSShirley Ma if (list) 568e9d7417bSJason Wang give_pages(rq, list); 5699ab86bbcSShirley Ma return -ENOMEM; 570296f96fcSRusty Russell } 571e9d7417bSJason Wang sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE); 5729ab86bbcSShirley Ma 5739ab86bbcSShirley Ma /* chain new page in list head to match sg */ 5749ab86bbcSShirley Ma first->private = (unsigned long)list; 5759ab86bbcSShirley Ma list = first; 5769ab86bbcSShirley Ma } 5779ab86bbcSShirley Ma 578e9d7417bSJason Wang first = get_a_page(rq, gfp); 5799ab86bbcSShirley Ma if (!first) { 580e9d7417bSJason Wang give_pages(rq, list); 5819ab86bbcSShirley Ma return -ENOMEM; 5829ab86bbcSShirley Ma } 5839ab86bbcSShirley Ma p = page_address(first); 5849ab86bbcSShirley Ma 585e9d7417bSJason Wang /* rq->sg[0], rq->sg[1] share the same page */ 586e9d7417bSJason Wang /* a separated rq->sg[0] for virtio_net_hdr only due to QEMU bug */ 587e9d7417bSJason Wang sg_set_buf(&rq->sg[0], p, sizeof(struct virtio_net_hdr)); 5889ab86bbcSShirley Ma 589e9d7417bSJason Wang /* rq->sg[1] for data packet, from offset */ 5909ab86bbcSShirley Ma offset = sizeof(struct padded_vnet_hdr); 591e9d7417bSJason Wang sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset); 5929ab86bbcSShirley Ma 5939ab86bbcSShirley Ma /* chain first in list head */ 5949ab86bbcSShirley Ma first->private = (unsigned long)list; 5959dc7b9e4SRusty Russell err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2, 596aa989f5eSMichael S. Tsirkin first, gfp); 5979ab86bbcSShirley Ma if (err < 0) 598e9d7417bSJason Wang give_pages(rq, first); 5999ab86bbcSShirley Ma 6009ab86bbcSShirley Ma return err; 6019ab86bbcSShirley Ma } 6029ab86bbcSShirley Ma 603fbf28d78SMichael Dalton static unsigned int get_mergeable_buf_len(struct ewma *avg_pkt_len) 6049ab86bbcSShirley Ma { 605ab7db917SMichael Dalton const size_t hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf); 606fbf28d78SMichael Dalton unsigned int len; 607fbf28d78SMichael Dalton 608fbf28d78SMichael Dalton len = hdr_len + clamp_t(unsigned int, ewma_read(avg_pkt_len), 609fbf28d78SMichael Dalton GOOD_PACKET_LEN, PAGE_SIZE - hdr_len); 610fbf28d78SMichael Dalton return ALIGN(len, MERGEABLE_BUFFER_ALIGN); 611fbf28d78SMichael Dalton } 612fbf28d78SMichael Dalton 613fbf28d78SMichael Dalton static int add_recvbuf_mergeable(struct receive_queue *rq, gfp_t gfp) 614fbf28d78SMichael Dalton { 615fb51879dSMichael Dalton struct page_frag *alloc_frag = &rq->alloc_frag; 616fb51879dSMichael Dalton char *buf; 617ab7db917SMichael Dalton unsigned long ctx; 6189ab86bbcSShirley Ma int err; 619fb51879dSMichael Dalton unsigned int len, hole; 6209ab86bbcSShirley Ma 621fbf28d78SMichael Dalton len = get_mergeable_buf_len(&rq->mrg_avg_pkt_len); 622ab7db917SMichael Dalton if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp))) 6239ab86bbcSShirley Ma return -ENOMEM; 624ab7db917SMichael Dalton 625fb51879dSMichael Dalton buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset; 626ab7db917SMichael Dalton ctx = mergeable_buf_to_ctx(buf, len); 627fb51879dSMichael Dalton get_page(alloc_frag->page); 628fb51879dSMichael Dalton alloc_frag->offset += len; 629fb51879dSMichael Dalton hole = alloc_frag->size - alloc_frag->offset; 630ab7db917SMichael Dalton if (hole < len) { 631ab7db917SMichael Dalton /* To avoid internal fragmentation, if there is very likely not 632ab7db917SMichael Dalton * enough space for another buffer, add the remaining space to 633ab7db917SMichael Dalton * the current buffer. This extra space is not included in 634ab7db917SMichael Dalton * the truesize stored in ctx. 635ab7db917SMichael Dalton */ 636fb51879dSMichael Dalton len += hole; 637fb51879dSMichael Dalton alloc_frag->offset += hole; 638fb51879dSMichael Dalton } 6399ab86bbcSShirley Ma 640fb51879dSMichael Dalton sg_init_one(rq->sg, buf, len); 641ab7db917SMichael Dalton err = virtqueue_add_inbuf(rq->vq, rq->sg, 1, (void *)ctx, gfp); 6429ab86bbcSShirley Ma if (err < 0) 6432613af0eSMichael Dalton put_page(virt_to_head_page(buf)); 6449ab86bbcSShirley Ma 6459ab86bbcSShirley Ma return err; 646296f96fcSRusty Russell } 647296f96fcSRusty Russell 648b2baed69SRusty Russell /* 649b2baed69SRusty Russell * Returns false if we couldn't fill entirely (OOM). 650b2baed69SRusty Russell * 651b2baed69SRusty Russell * Normally run in the receive path, but can also be run from ndo_open 652b2baed69SRusty Russell * before we're receiving packets, or from refill_work which is 653b2baed69SRusty Russell * careful to disable receiving (using napi_disable). 654b2baed69SRusty Russell */ 655e9d7417bSJason Wang static bool try_fill_recv(struct receive_queue *rq, gfp_t gfp) 6563f2c31d9SMark McLoughlin { 657e9d7417bSJason Wang struct virtnet_info *vi = rq->vq->vdev->priv; 6583f2c31d9SMark McLoughlin int err; 6591788f495SMichael S. Tsirkin bool oom; 6603f2c31d9SMark McLoughlin 661fb51879dSMichael Dalton gfp |= __GFP_COLD; 6620aea51c3SAmit Shah do { 6639ab86bbcSShirley Ma if (vi->mergeable_rx_bufs) 664e9d7417bSJason Wang err = add_recvbuf_mergeable(rq, gfp); 6659ab86bbcSShirley Ma else if (vi->big_packets) 666e9d7417bSJason Wang err = add_recvbuf_big(rq, gfp); 6679ab86bbcSShirley Ma else 668e9d7417bSJason Wang err = add_recvbuf_small(rq, gfp); 6693f2c31d9SMark McLoughlin 6701788f495SMichael S. Tsirkin oom = err == -ENOMEM; 6719ed4cb07SRusty Russell if (err) 6723f2c31d9SMark McLoughlin break; 673b7dfde95SLinus Torvalds } while (rq->vq->num_free); 674*681daee2SJason Wang virtqueue_kick(rq->vq); 6753161e453SRusty Russell return !oom; 6763f2c31d9SMark McLoughlin } 6773f2c31d9SMark McLoughlin 67818445c4dSRusty Russell static void skb_recv_done(struct virtqueue *rvq) 679296f96fcSRusty Russell { 680296f96fcSRusty Russell struct virtnet_info *vi = rvq->vdev->priv; 681986a4f4dSJason Wang struct receive_queue *rq = &vi->rq[vq2rxq(rvq)]; 682e9d7417bSJason Wang 68318445c4dSRusty Russell /* Schedule NAPI, Suppress further interrupts if successful. */ 684e9d7417bSJason Wang if (napi_schedule_prep(&rq->napi)) { 6851915a712SMichael S. Tsirkin virtqueue_disable_cb(rvq); 686e9d7417bSJason Wang __napi_schedule(&rq->napi); 68718445c4dSRusty Russell } 688296f96fcSRusty Russell } 689296f96fcSRusty Russell 690e9d7417bSJason Wang static void virtnet_napi_enable(struct receive_queue *rq) 6913e9d08ecSBruce Rogers { 692e9d7417bSJason Wang napi_enable(&rq->napi); 6933e9d08ecSBruce Rogers 6943e9d08ecSBruce Rogers /* If all buffers were filled by other side before we napi_enabled, we 6953e9d08ecSBruce Rogers * won't get another interrupt, so process any outstanding packets 6963e9d08ecSBruce Rogers * now. virtnet_poll wants re-enable the queue, so we disable here. 6973e9d08ecSBruce Rogers * We synchronize against interrupts via NAPI_STATE_SCHED */ 698e9d7417bSJason Wang if (napi_schedule_prep(&rq->napi)) { 699e9d7417bSJason Wang virtqueue_disable_cb(rq->vq); 700ec13ee80SMichael S. Tsirkin local_bh_disable(); 701e9d7417bSJason Wang __napi_schedule(&rq->napi); 702ec13ee80SMichael S. Tsirkin local_bh_enable(); 7033e9d08ecSBruce Rogers } 7043e9d08ecSBruce Rogers } 7053e9d08ecSBruce Rogers 7063161e453SRusty Russell static void refill_work(struct work_struct *work) 7073161e453SRusty Russell { 708e9d7417bSJason Wang struct virtnet_info *vi = 709e9d7417bSJason Wang container_of(work, struct virtnet_info, refill.work); 7103161e453SRusty Russell bool still_empty; 711986a4f4dSJason Wang int i; 7123161e453SRusty Russell 71355257d72SSasha Levin for (i = 0; i < vi->curr_queue_pairs; i++) { 714986a4f4dSJason Wang struct receive_queue *rq = &vi->rq[i]; 715986a4f4dSJason Wang 716986a4f4dSJason Wang napi_disable(&rq->napi); 717986a4f4dSJason Wang still_empty = !try_fill_recv(rq, GFP_KERNEL); 718986a4f4dSJason Wang virtnet_napi_enable(rq); 7193161e453SRusty Russell 7203161e453SRusty Russell /* In theory, this can happen: if we don't get any buffers in 721986a4f4dSJason Wang * we will *never* try to fill again. 722986a4f4dSJason Wang */ 7233161e453SRusty Russell if (still_empty) 7243b07e9caSTejun Heo schedule_delayed_work(&vi->refill, HZ/2); 7253161e453SRusty Russell } 726986a4f4dSJason Wang } 7273161e453SRusty Russell 728296f96fcSRusty Russell static int virtnet_poll(struct napi_struct *napi, int budget) 729296f96fcSRusty Russell { 730e9d7417bSJason Wang struct receive_queue *rq = 731e9d7417bSJason Wang container_of(napi, struct receive_queue, napi); 732e9d7417bSJason Wang struct virtnet_info *vi = rq->vq->vdev->priv; 7339ab86bbcSShirley Ma void *buf; 734cbdadbbfSMichael S. Tsirkin unsigned int r, len, received = 0; 735296f96fcSRusty Russell 736296f96fcSRusty Russell again: 737296f96fcSRusty Russell while (received < budget && 738e9d7417bSJason Wang (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) { 739e9d7417bSJason Wang receive_buf(rq, buf, len); 740296f96fcSRusty Russell received++; 741296f96fcSRusty Russell } 742296f96fcSRusty Russell 743be121f46SJason Wang if (rq->vq->num_free > virtqueue_get_vring_size(rq->vq) / 2) { 744e9d7417bSJason Wang if (!try_fill_recv(rq, GFP_ATOMIC)) 7453b07e9caSTejun Heo schedule_delayed_work(&vi->refill, 0); 7463161e453SRusty Russell } 747296f96fcSRusty Russell 7488329d98eSRusty Russell /* Out of packets? */ 7498329d98eSRusty Russell if (received < budget) { 750cbdadbbfSMichael S. Tsirkin r = virtqueue_enable_cb_prepare(rq->vq); 751288379f0SBen Hutchings napi_complete(napi); 752cbdadbbfSMichael S. Tsirkin if (unlikely(virtqueue_poll(rq->vq, r)) && 7538e95a202SJoe Perches napi_schedule_prep(napi)) { 754e9d7417bSJason Wang virtqueue_disable_cb(rq->vq); 755288379f0SBen Hutchings __napi_schedule(napi); 756296f96fcSRusty Russell goto again; 757296f96fcSRusty Russell } 7584265f161SChristian Borntraeger } 759296f96fcSRusty Russell 760296f96fcSRusty Russell return received; 761296f96fcSRusty Russell } 762296f96fcSRusty Russell 763986a4f4dSJason Wang static int virtnet_open(struct net_device *dev) 764986a4f4dSJason Wang { 765986a4f4dSJason Wang struct virtnet_info *vi = netdev_priv(dev); 766986a4f4dSJason Wang int i; 767986a4f4dSJason Wang 768e4166625SJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 769e4166625SJason Wang if (i < vi->curr_queue_pairs) 770986a4f4dSJason Wang /* Make sure we have some buffers: if oom use wq. */ 771986a4f4dSJason Wang if (!try_fill_recv(&vi->rq[i], GFP_KERNEL)) 772986a4f4dSJason Wang schedule_delayed_work(&vi->refill, 0); 773986a4f4dSJason Wang virtnet_napi_enable(&vi->rq[i]); 774986a4f4dSJason Wang } 775986a4f4dSJason Wang 776986a4f4dSJason Wang return 0; 777986a4f4dSJason Wang } 778986a4f4dSJason Wang 779b7dfde95SLinus Torvalds static void free_old_xmit_skbs(struct send_queue *sq) 780296f96fcSRusty Russell { 781296f96fcSRusty Russell struct sk_buff *skb; 7826ee57bccSMichael S. Tsirkin unsigned int len; 783e9d7417bSJason Wang struct virtnet_info *vi = sq->vq->vdev->priv; 78458472a76SEric Dumazet struct virtnet_stats *stats = this_cpu_ptr(vi->stats); 785296f96fcSRusty Russell 786e9d7417bSJason Wang while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) { 787296f96fcSRusty Russell pr_debug("Sent skb %p\n", skb); 7883fa2a1dfSstephen hemminger 78983a27052SEric Dumazet u64_stats_update_begin(&stats->tx_syncp); 7903fa2a1dfSstephen hemminger stats->tx_bytes += skb->len; 7913fa2a1dfSstephen hemminger stats->tx_packets++; 79283a27052SEric Dumazet u64_stats_update_end(&stats->tx_syncp); 7933fa2a1dfSstephen hemminger 794ed79bab8SEric Dumazet dev_kfree_skb_any(skb); 795296f96fcSRusty Russell } 796296f96fcSRusty Russell } 797296f96fcSRusty Russell 798e9d7417bSJason Wang static int xmit_skb(struct send_queue *sq, struct sk_buff *skb) 799296f96fcSRusty Russell { 800e7428e95SMichael S. Tsirkin struct skb_vnet_hdr *hdr; 801296f96fcSRusty Russell const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest; 802e9d7417bSJason Wang struct virtnet_info *vi = sq->vq->vdev->priv; 8037bedc7dcSMichael S. Tsirkin unsigned num_sg; 804e7428e95SMichael S. Tsirkin unsigned hdr_len; 805e7428e95SMichael S. Tsirkin bool can_push; 806296f96fcSRusty Russell 807e174961cSJohannes Berg pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest); 808e7428e95SMichael S. Tsirkin if (vi->mergeable_rx_bufs) 809e7428e95SMichael S. Tsirkin hdr_len = sizeof hdr->mhdr; 810e7428e95SMichael S. Tsirkin else 811e7428e95SMichael S. Tsirkin hdr_len = sizeof hdr->hdr; 812e7428e95SMichael S. Tsirkin 813e7428e95SMichael S. Tsirkin can_push = vi->any_header_sg && 814e7428e95SMichael S. Tsirkin !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) && 815e7428e95SMichael S. Tsirkin !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len; 816e7428e95SMichael S. Tsirkin /* Even if we can, don't push here yet as this would skew 817e7428e95SMichael S. Tsirkin * csum_start offset below. */ 818e7428e95SMichael S. Tsirkin if (can_push) 819e7428e95SMichael S. Tsirkin hdr = (struct skb_vnet_hdr *)(skb->data - hdr_len); 820e7428e95SMichael S. Tsirkin else 821e7428e95SMichael S. Tsirkin hdr = skb_vnet_hdr(skb); 822296f96fcSRusty Russell 823296f96fcSRusty Russell if (skb->ip_summed == CHECKSUM_PARTIAL) { 824b3f24698SRusty Russell hdr->hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM; 82555508d60SMichał Mirosław hdr->hdr.csum_start = skb_checksum_start_offset(skb); 826b3f24698SRusty Russell hdr->hdr.csum_offset = skb->csum_offset; 827296f96fcSRusty Russell } else { 828b3f24698SRusty Russell hdr->hdr.flags = 0; 829b3f24698SRusty Russell hdr->hdr.csum_offset = hdr->hdr.csum_start = 0; 830296f96fcSRusty Russell } 831296f96fcSRusty Russell 832296f96fcSRusty Russell if (skb_is_gso(skb)) { 833b3f24698SRusty Russell hdr->hdr.hdr_len = skb_headlen(skb); 834b3f24698SRusty Russell hdr->hdr.gso_size = skb_shinfo(skb)->gso_size; 83534a48579SRusty Russell if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) 836b3f24698SRusty Russell hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4; 837296f96fcSRusty Russell else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) 838b3f24698SRusty Russell hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6; 839296f96fcSRusty Russell else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP) 840b3f24698SRusty Russell hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP; 841296f96fcSRusty Russell else 842296f96fcSRusty Russell BUG(); 84334a48579SRusty Russell if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN) 844b3f24698SRusty Russell hdr->hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN; 845296f96fcSRusty Russell } else { 846b3f24698SRusty Russell hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE; 847b3f24698SRusty Russell hdr->hdr.gso_size = hdr->hdr.hdr_len = 0; 848296f96fcSRusty Russell } 849296f96fcSRusty Russell 850e7428e95SMichael S. Tsirkin if (vi->mergeable_rx_bufs) 851b3f24698SRusty Russell hdr->mhdr.num_buffers = 0; 8523f2c31d9SMark McLoughlin 853e7428e95SMichael S. Tsirkin if (can_push) { 854e7428e95SMichael S. Tsirkin __skb_push(skb, hdr_len); 855e7428e95SMichael S. Tsirkin num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len); 856e7428e95SMichael S. Tsirkin /* Pull header back to avoid skew in tx bytes calculations. */ 857e7428e95SMichael S. Tsirkin __skb_pull(skb, hdr_len); 858e7428e95SMichael S. Tsirkin } else { 859e7428e95SMichael S. Tsirkin sg_set_buf(sq->sg, hdr, hdr_len); 860b7dfde95SLinus Torvalds num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len) + 1; 861e7428e95SMichael S. Tsirkin } 8629dc7b9e4SRusty Russell return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC); 86311a3a154SRusty Russell } 86411a3a154SRusty Russell 865424efe9cSStephen Hemminger static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev) 86699ffc696SRusty Russell { 86799ffc696SRusty Russell struct virtnet_info *vi = netdev_priv(dev); 868986a4f4dSJason Wang int qnum = skb_get_queue_mapping(skb); 869986a4f4dSJason Wang struct send_queue *sq = &vi->sq[qnum]; 8709ed4cb07SRusty Russell int err; 8712cb9c6baSRusty Russell 8722cb9c6baSRusty Russell /* Free up any pending old buffers before queueing new ones. */ 873e9d7417bSJason Wang free_old_xmit_skbs(sq); 87499ffc696SRusty Russell 87503f191baSMichael S. Tsirkin /* Try to transmit */ 876b7dfde95SLinus Torvalds err = xmit_skb(sq, skb); 87799ffc696SRusty Russell 8789ed4cb07SRusty Russell /* This should not happen! */ 879*681daee2SJason Wang if (unlikely(err)) { 88058eba97dSRusty Russell dev->stats.tx_fifo_errors++; 8812e57b79cSRick Jones if (net_ratelimit()) 88258eba97dSRusty Russell dev_warn(&dev->dev, 883b7dfde95SLinus Torvalds "Unexpected TXQ (%d) queue failure: %d\n", qnum, err); 88458eba97dSRusty Russell dev->stats.tx_dropped++; 88558eba97dSRusty Russell kfree_skb(skb); 88658eba97dSRusty Russell return NETDEV_TX_OK; 887296f96fcSRusty Russell } 888*681daee2SJason Wang virtqueue_kick(sq->vq); 88903f191baSMichael S. Tsirkin 89048925e37SRusty Russell /* Don't wait up for transmitted skbs to be freed. */ 89148925e37SRusty Russell skb_orphan(skb); 89248925e37SRusty Russell nf_reset(skb); 89348925e37SRusty Russell 89448925e37SRusty Russell /* Apparently nice girls don't return TX_BUSY; stop the queue 89548925e37SRusty Russell * before it gets out of hand. Naturally, this wastes entries. */ 896b7dfde95SLinus Torvalds if (sq->vq->num_free < 2+MAX_SKB_FRAGS) { 897986a4f4dSJason Wang netif_stop_subqueue(dev, qnum); 898e9d7417bSJason Wang if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) { 89948925e37SRusty Russell /* More just got used, free them then recheck. */ 900b7dfde95SLinus Torvalds free_old_xmit_skbs(sq); 901b7dfde95SLinus Torvalds if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) { 902986a4f4dSJason Wang netif_start_subqueue(dev, qnum); 903e9d7417bSJason Wang virtqueue_disable_cb(sq->vq); 90448925e37SRusty Russell } 90548925e37SRusty Russell } 90648925e37SRusty Russell } 90748925e37SRusty Russell 90848925e37SRusty Russell return NETDEV_TX_OK; 90948925e37SRusty Russell } 91048925e37SRusty Russell 91140cbfc37SAmos Kong /* 91240cbfc37SAmos Kong * Send command via the control virtqueue and check status. Commands 91340cbfc37SAmos Kong * supported by the hypervisor, as indicated by feature bits, should 914788a8b6dSstephen hemminger * never fail unless improperly formatted. 91540cbfc37SAmos Kong */ 91640cbfc37SAmos Kong static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd, 917d24bae32Sstephen hemminger struct scatterlist *out) 91840cbfc37SAmos Kong { 919f7bc9594SRusty Russell struct scatterlist *sgs[4], hdr, stat; 92040cbfc37SAmos Kong struct virtio_net_ctrl_hdr ctrl; 92140cbfc37SAmos Kong virtio_net_ctrl_ack status = ~0; 922d24bae32Sstephen hemminger unsigned out_num = 0, tmp; 92340cbfc37SAmos Kong 92440cbfc37SAmos Kong /* Caller should know better */ 925f7bc9594SRusty Russell BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)); 92640cbfc37SAmos Kong 92740cbfc37SAmos Kong ctrl.class = class; 92840cbfc37SAmos Kong ctrl.cmd = cmd; 929f7bc9594SRusty Russell /* Add header */ 930f7bc9594SRusty Russell sg_init_one(&hdr, &ctrl, sizeof(ctrl)); 931f7bc9594SRusty Russell sgs[out_num++] = &hdr; 93240cbfc37SAmos Kong 933f7bc9594SRusty Russell if (out) 934f7bc9594SRusty Russell sgs[out_num++] = out; 93540cbfc37SAmos Kong 936f7bc9594SRusty Russell /* Add return status. */ 937f7bc9594SRusty Russell sg_init_one(&stat, &status, sizeof(status)); 938d24bae32Sstephen hemminger sgs[out_num] = &stat; 93940cbfc37SAmos Kong 940d24bae32Sstephen hemminger BUG_ON(out_num + 1 > ARRAY_SIZE(sgs)); 941d24bae32Sstephen hemminger BUG_ON(virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC) < 0); 94240cbfc37SAmos Kong 94367975901SHeinz Graalfs if (unlikely(!virtqueue_kick(vi->cvq))) 94467975901SHeinz Graalfs return status == VIRTIO_NET_OK; 94540cbfc37SAmos Kong 94640cbfc37SAmos Kong /* Spin for a response, the kick causes an ioport write, trapping 94740cbfc37SAmos Kong * into the hypervisor, so the request should be handled immediately. 94840cbfc37SAmos Kong */ 949047b9b94SHeinz Graalfs while (!virtqueue_get_buf(vi->cvq, &tmp) && 950047b9b94SHeinz Graalfs !virtqueue_is_broken(vi->cvq)) 95140cbfc37SAmos Kong cpu_relax(); 95240cbfc37SAmos Kong 95340cbfc37SAmos Kong return status == VIRTIO_NET_OK; 95440cbfc37SAmos Kong } 95540cbfc37SAmos Kong 9569c46f6d4SAlex Williamson static int virtnet_set_mac_address(struct net_device *dev, void *p) 9579c46f6d4SAlex Williamson { 9589c46f6d4SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 9599c46f6d4SAlex Williamson struct virtio_device *vdev = vi->vdev; 960f2f2c8b4SJiri Pirko int ret; 9617e58d5aeSAmos Kong struct sockaddr *addr = p; 9627e58d5aeSAmos Kong struct scatterlist sg; 9639c46f6d4SAlex Williamson 9647e58d5aeSAmos Kong ret = eth_prepare_mac_addr_change(dev, p); 965f2f2c8b4SJiri Pirko if (ret) 966f2f2c8b4SJiri Pirko return ret; 9679c46f6d4SAlex Williamson 9687e58d5aeSAmos Kong if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) { 9697e58d5aeSAmos Kong sg_init_one(&sg, addr->sa_data, dev->addr_len); 9707e58d5aeSAmos Kong if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, 971d24bae32Sstephen hemminger VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) { 9727e58d5aeSAmos Kong dev_warn(&vdev->dev, 9737e58d5aeSAmos Kong "Failed to set mac address by vq command.\n"); 9747e58d5aeSAmos Kong return -EINVAL; 9757e58d5aeSAmos Kong } 9767e58d5aeSAmos Kong } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) { 977855e0c52SRusty Russell unsigned int i; 978855e0c52SRusty Russell 979855e0c52SRusty Russell /* Naturally, this has an atomicity problem. */ 980855e0c52SRusty Russell for (i = 0; i < dev->addr_len; i++) 981855e0c52SRusty Russell virtio_cwrite8(vdev, 982855e0c52SRusty Russell offsetof(struct virtio_net_config, mac) + 983855e0c52SRusty Russell i, addr->sa_data[i]); 9847e58d5aeSAmos Kong } 9857e58d5aeSAmos Kong 9867e58d5aeSAmos Kong eth_commit_mac_addr_change(dev, p); 9879c46f6d4SAlex Williamson 9889c46f6d4SAlex Williamson return 0; 9899c46f6d4SAlex Williamson } 9909c46f6d4SAlex Williamson 9913fa2a1dfSstephen hemminger static struct rtnl_link_stats64 *virtnet_stats(struct net_device *dev, 9923fa2a1dfSstephen hemminger struct rtnl_link_stats64 *tot) 9933fa2a1dfSstephen hemminger { 9943fa2a1dfSstephen hemminger struct virtnet_info *vi = netdev_priv(dev); 9953fa2a1dfSstephen hemminger int cpu; 9963fa2a1dfSstephen hemminger unsigned int start; 9973fa2a1dfSstephen hemminger 9983fa2a1dfSstephen hemminger for_each_possible_cpu(cpu) { 99958472a76SEric Dumazet struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu); 10003fa2a1dfSstephen hemminger u64 tpackets, tbytes, rpackets, rbytes; 10013fa2a1dfSstephen hemminger 10023fa2a1dfSstephen hemminger do { 1003e3906486SKevin Groeneveld start = u64_stats_fetch_begin_bh(&stats->tx_syncp); 10043fa2a1dfSstephen hemminger tpackets = stats->tx_packets; 10053fa2a1dfSstephen hemminger tbytes = stats->tx_bytes; 1006e3906486SKevin Groeneveld } while (u64_stats_fetch_retry_bh(&stats->tx_syncp, start)); 100783a27052SEric Dumazet 100883a27052SEric Dumazet do { 1009e3906486SKevin Groeneveld start = u64_stats_fetch_begin_bh(&stats->rx_syncp); 10103fa2a1dfSstephen hemminger rpackets = stats->rx_packets; 10113fa2a1dfSstephen hemminger rbytes = stats->rx_bytes; 1012e3906486SKevin Groeneveld } while (u64_stats_fetch_retry_bh(&stats->rx_syncp, start)); 10133fa2a1dfSstephen hemminger 10143fa2a1dfSstephen hemminger tot->rx_packets += rpackets; 10153fa2a1dfSstephen hemminger tot->tx_packets += tpackets; 10163fa2a1dfSstephen hemminger tot->rx_bytes += rbytes; 10173fa2a1dfSstephen hemminger tot->tx_bytes += tbytes; 10183fa2a1dfSstephen hemminger } 10193fa2a1dfSstephen hemminger 10203fa2a1dfSstephen hemminger tot->tx_dropped = dev->stats.tx_dropped; 1021021ac8d3SRick Jones tot->tx_fifo_errors = dev->stats.tx_fifo_errors; 10223fa2a1dfSstephen hemminger tot->rx_dropped = dev->stats.rx_dropped; 10233fa2a1dfSstephen hemminger tot->rx_length_errors = dev->stats.rx_length_errors; 10243fa2a1dfSstephen hemminger tot->rx_frame_errors = dev->stats.rx_frame_errors; 10253fa2a1dfSstephen hemminger 10263fa2a1dfSstephen hemminger return tot; 10273fa2a1dfSstephen hemminger } 10283fa2a1dfSstephen hemminger 1029da74e89dSAmit Shah #ifdef CONFIG_NET_POLL_CONTROLLER 1030da74e89dSAmit Shah static void virtnet_netpoll(struct net_device *dev) 1031da74e89dSAmit Shah { 1032da74e89dSAmit Shah struct virtnet_info *vi = netdev_priv(dev); 1033986a4f4dSJason Wang int i; 1034da74e89dSAmit Shah 1035986a4f4dSJason Wang for (i = 0; i < vi->curr_queue_pairs; i++) 1036986a4f4dSJason Wang napi_schedule(&vi->rq[i].napi); 1037da74e89dSAmit Shah } 1038da74e89dSAmit Shah #endif 1039da74e89dSAmit Shah 1040586d17c5SJason Wang static void virtnet_ack_link_announce(struct virtnet_info *vi) 1041586d17c5SJason Wang { 1042586d17c5SJason Wang rtnl_lock(); 1043586d17c5SJason Wang if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE, 1044d24bae32Sstephen hemminger VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL)) 1045586d17c5SJason Wang dev_warn(&vi->dev->dev, "Failed to ack link announce.\n"); 1046586d17c5SJason Wang rtnl_unlock(); 1047586d17c5SJason Wang } 1048586d17c5SJason Wang 1049986a4f4dSJason Wang static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs) 1050986a4f4dSJason Wang { 1051986a4f4dSJason Wang struct scatterlist sg; 1052986a4f4dSJason Wang struct virtio_net_ctrl_mq s; 1053986a4f4dSJason Wang struct net_device *dev = vi->dev; 1054986a4f4dSJason Wang 1055986a4f4dSJason Wang if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ)) 1056986a4f4dSJason Wang return 0; 1057986a4f4dSJason Wang 1058986a4f4dSJason Wang s.virtqueue_pairs = queue_pairs; 1059986a4f4dSJason Wang sg_init_one(&sg, &s, sizeof(s)); 1060986a4f4dSJason Wang 1061986a4f4dSJason Wang if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ, 1062d24bae32Sstephen hemminger VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) { 1063986a4f4dSJason Wang dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n", 1064986a4f4dSJason Wang queue_pairs); 1065986a4f4dSJason Wang return -EINVAL; 106655257d72SSasha Levin } else { 1067986a4f4dSJason Wang vi->curr_queue_pairs = queue_pairs; 106835ed159bSJason Wang /* virtnet_open() will refill when device is going to up. */ 106935ed159bSJason Wang if (dev->flags & IFF_UP) 10709b9cd802SJason Wang schedule_delayed_work(&vi->refill, 0); 107155257d72SSasha Levin } 1072986a4f4dSJason Wang 1073986a4f4dSJason Wang return 0; 1074986a4f4dSJason Wang } 1075986a4f4dSJason Wang 1076296f96fcSRusty Russell static int virtnet_close(struct net_device *dev) 1077296f96fcSRusty Russell { 1078296f96fcSRusty Russell struct virtnet_info *vi = netdev_priv(dev); 1079986a4f4dSJason Wang int i; 1080296f96fcSRusty Russell 1081b2baed69SRusty Russell /* Make sure refill_work doesn't re-enable napi! */ 1082b2baed69SRusty Russell cancel_delayed_work_sync(&vi->refill); 1083986a4f4dSJason Wang 1084986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) 1085986a4f4dSJason Wang napi_disable(&vi->rq[i].napi); 1086296f96fcSRusty Russell 1087296f96fcSRusty Russell return 0; 1088296f96fcSRusty Russell } 1089296f96fcSRusty Russell 10902af7698eSAlex Williamson static void virtnet_set_rx_mode(struct net_device *dev) 10912af7698eSAlex Williamson { 10922af7698eSAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 1093f565a7c2SAlex Williamson struct scatterlist sg[2]; 10942af7698eSAlex Williamson u8 promisc, allmulti; 1095f565a7c2SAlex Williamson struct virtio_net_ctrl_mac *mac_data; 1096ccffad25SJiri Pirko struct netdev_hw_addr *ha; 109732e7bfc4SJiri Pirko int uc_count; 10984cd24eafSJiri Pirko int mc_count; 1099f565a7c2SAlex Williamson void *buf; 1100f565a7c2SAlex Williamson int i; 11012af7698eSAlex Williamson 1102788a8b6dSstephen hemminger /* We can't dynamically set ndo_set_rx_mode, so return gracefully */ 11032af7698eSAlex Williamson if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX)) 11042af7698eSAlex Williamson return; 11052af7698eSAlex Williamson 1106f565a7c2SAlex Williamson promisc = ((dev->flags & IFF_PROMISC) != 0); 1107f565a7c2SAlex Williamson allmulti = ((dev->flags & IFF_ALLMULTI) != 0); 11082af7698eSAlex Williamson 110923e258e1SAlex Williamson sg_init_one(sg, &promisc, sizeof(promisc)); 11102af7698eSAlex Williamson 11112af7698eSAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, 1112d24bae32Sstephen hemminger VIRTIO_NET_CTRL_RX_PROMISC, sg)) 11132af7698eSAlex Williamson dev_warn(&dev->dev, "Failed to %sable promisc mode.\n", 11142af7698eSAlex Williamson promisc ? "en" : "dis"); 11152af7698eSAlex Williamson 111623e258e1SAlex Williamson sg_init_one(sg, &allmulti, sizeof(allmulti)); 11172af7698eSAlex Williamson 11182af7698eSAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, 1119d24bae32Sstephen hemminger VIRTIO_NET_CTRL_RX_ALLMULTI, sg)) 11202af7698eSAlex Williamson dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n", 11212af7698eSAlex Williamson allmulti ? "en" : "dis"); 1122f565a7c2SAlex Williamson 112332e7bfc4SJiri Pirko uc_count = netdev_uc_count(dev); 11244cd24eafSJiri Pirko mc_count = netdev_mc_count(dev); 1125f565a7c2SAlex Williamson /* MAC filter - use one buffer for both lists */ 11264cd24eafSJiri Pirko buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) + 1127f565a7c2SAlex Williamson (2 * sizeof(mac_data->entries)), GFP_ATOMIC); 11284cd24eafSJiri Pirko mac_data = buf; 1129e68ed8f0SJoe Perches if (!buf) 1130f565a7c2SAlex Williamson return; 1131f565a7c2SAlex Williamson 113223e258e1SAlex Williamson sg_init_table(sg, 2); 113323e258e1SAlex Williamson 1134f565a7c2SAlex Williamson /* Store the unicast list and count in the front of the buffer */ 113532e7bfc4SJiri Pirko mac_data->entries = uc_count; 1136ccffad25SJiri Pirko i = 0; 113732e7bfc4SJiri Pirko netdev_for_each_uc_addr(ha, dev) 1138ccffad25SJiri Pirko memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN); 1139f565a7c2SAlex Williamson 1140f565a7c2SAlex Williamson sg_set_buf(&sg[0], mac_data, 114132e7bfc4SJiri Pirko sizeof(mac_data->entries) + (uc_count * ETH_ALEN)); 1142f565a7c2SAlex Williamson 1143f565a7c2SAlex Williamson /* multicast list and count fill the end */ 114432e7bfc4SJiri Pirko mac_data = (void *)&mac_data->macs[uc_count][0]; 1145f565a7c2SAlex Williamson 11464cd24eafSJiri Pirko mac_data->entries = mc_count; 1147567ec874SJiri Pirko i = 0; 114822bedad3SJiri Pirko netdev_for_each_mc_addr(ha, dev) 114922bedad3SJiri Pirko memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN); 1150f565a7c2SAlex Williamson 1151f565a7c2SAlex Williamson sg_set_buf(&sg[1], mac_data, 11524cd24eafSJiri Pirko sizeof(mac_data->entries) + (mc_count * ETH_ALEN)); 1153f565a7c2SAlex Williamson 1154f565a7c2SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, 1155d24bae32Sstephen hemminger VIRTIO_NET_CTRL_MAC_TABLE_SET, sg)) 115699e872aeSThomas Huth dev_warn(&dev->dev, "Failed to set MAC filter table.\n"); 1157f565a7c2SAlex Williamson 1158f565a7c2SAlex Williamson kfree(buf); 11592af7698eSAlex Williamson } 11602af7698eSAlex Williamson 116180d5c368SPatrick McHardy static int virtnet_vlan_rx_add_vid(struct net_device *dev, 116280d5c368SPatrick McHardy __be16 proto, u16 vid) 11630bde9569SAlex Williamson { 11640bde9569SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 11650bde9569SAlex Williamson struct scatterlist sg; 11660bde9569SAlex Williamson 116723e258e1SAlex Williamson sg_init_one(&sg, &vid, sizeof(vid)); 11680bde9569SAlex Williamson 11690bde9569SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, 1170d24bae32Sstephen hemminger VIRTIO_NET_CTRL_VLAN_ADD, &sg)) 11710bde9569SAlex Williamson dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid); 11728e586137SJiri Pirko return 0; 11730bde9569SAlex Williamson } 11740bde9569SAlex Williamson 117580d5c368SPatrick McHardy static int virtnet_vlan_rx_kill_vid(struct net_device *dev, 117680d5c368SPatrick McHardy __be16 proto, u16 vid) 11770bde9569SAlex Williamson { 11780bde9569SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 11790bde9569SAlex Williamson struct scatterlist sg; 11800bde9569SAlex Williamson 118123e258e1SAlex Williamson sg_init_one(&sg, &vid, sizeof(vid)); 11820bde9569SAlex Williamson 11830bde9569SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, 1184d24bae32Sstephen hemminger VIRTIO_NET_CTRL_VLAN_DEL, &sg)) 11850bde9569SAlex Williamson dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid); 11868e586137SJiri Pirko return 0; 11870bde9569SAlex Williamson } 11880bde9569SAlex Williamson 11898898c21cSWanlong Gao static void virtnet_clean_affinity(struct virtnet_info *vi, long hcpu) 1190986a4f4dSJason Wang { 1191986a4f4dSJason Wang int i; 11928898c21cSWanlong Gao 11938898c21cSWanlong Gao if (vi->affinity_hint_set) { 11948898c21cSWanlong Gao for (i = 0; i < vi->max_queue_pairs; i++) { 11958898c21cSWanlong Gao virtqueue_set_affinity(vi->rq[i].vq, -1); 11968898c21cSWanlong Gao virtqueue_set_affinity(vi->sq[i].vq, -1); 11978898c21cSWanlong Gao } 11988898c21cSWanlong Gao 11998898c21cSWanlong Gao vi->affinity_hint_set = false; 12008898c21cSWanlong Gao } 12018898c21cSWanlong Gao } 12028898c21cSWanlong Gao 12038898c21cSWanlong Gao static void virtnet_set_affinity(struct virtnet_info *vi) 1204986a4f4dSJason Wang { 1205986a4f4dSJason Wang int i; 120647be2479SWanlong Gao int cpu; 1207986a4f4dSJason Wang 1208986a4f4dSJason Wang /* In multiqueue mode, when the number of cpu is equal to the number of 1209986a4f4dSJason Wang * queue pairs, we let the queue pairs to be private to one cpu by 1210986a4f4dSJason Wang * setting the affinity hint to eliminate the contention. 1211986a4f4dSJason Wang */ 12128898c21cSWanlong Gao if (vi->curr_queue_pairs == 1 || 12138898c21cSWanlong Gao vi->max_queue_pairs != num_online_cpus()) { 12148898c21cSWanlong Gao virtnet_clean_affinity(vi, -1); 1215986a4f4dSJason Wang return; 1216986a4f4dSJason Wang } 1217986a4f4dSJason Wang 121847be2479SWanlong Gao i = 0; 121947be2479SWanlong Gao for_each_online_cpu(cpu) { 1220986a4f4dSJason Wang virtqueue_set_affinity(vi->rq[i].vq, cpu); 1221986a4f4dSJason Wang virtqueue_set_affinity(vi->sq[i].vq, cpu); 12229bb8ca86SJason Wang netif_set_xps_queue(vi->dev, cpumask_of(cpu), i); 122347be2479SWanlong Gao i++; 1224986a4f4dSJason Wang } 1225986a4f4dSJason Wang 1226986a4f4dSJason Wang vi->affinity_hint_set = true; 122747be2479SWanlong Gao } 1228986a4f4dSJason Wang 12298de4b2f3SWanlong Gao static int virtnet_cpu_callback(struct notifier_block *nfb, 12308de4b2f3SWanlong Gao unsigned long action, void *hcpu) 12318de4b2f3SWanlong Gao { 12328de4b2f3SWanlong Gao struct virtnet_info *vi = container_of(nfb, struct virtnet_info, nb); 12338de4b2f3SWanlong Gao 12348de4b2f3SWanlong Gao switch(action & ~CPU_TASKS_FROZEN) { 12358de4b2f3SWanlong Gao case CPU_ONLINE: 12368de4b2f3SWanlong Gao case CPU_DOWN_FAILED: 12378de4b2f3SWanlong Gao case CPU_DEAD: 12388de4b2f3SWanlong Gao virtnet_set_affinity(vi); 12398de4b2f3SWanlong Gao break; 12408de4b2f3SWanlong Gao case CPU_DOWN_PREPARE: 12418de4b2f3SWanlong Gao virtnet_clean_affinity(vi, (long)hcpu); 12428de4b2f3SWanlong Gao break; 12438de4b2f3SWanlong Gao default: 12448de4b2f3SWanlong Gao break; 12458de4b2f3SWanlong Gao } 12463ab098dfSJason Wang 12478de4b2f3SWanlong Gao return NOTIFY_OK; 1248a9ea3fc6SHerbert Xu } 1249a9ea3fc6SHerbert Xu 12508f9f4668SRick Jones static void virtnet_get_ringparam(struct net_device *dev, 12518f9f4668SRick Jones struct ethtool_ringparam *ring) 12528f9f4668SRick Jones { 12538f9f4668SRick Jones struct virtnet_info *vi = netdev_priv(dev); 12548f9f4668SRick Jones 1255986a4f4dSJason Wang ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq); 1256986a4f4dSJason Wang ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq); 12578f9f4668SRick Jones ring->rx_pending = ring->rx_max_pending; 12588f9f4668SRick Jones ring->tx_pending = ring->tx_max_pending; 12598f9f4668SRick Jones } 12608f9f4668SRick Jones 126166846048SRick Jones 126266846048SRick Jones static void virtnet_get_drvinfo(struct net_device *dev, 126366846048SRick Jones struct ethtool_drvinfo *info) 126466846048SRick Jones { 126566846048SRick Jones struct virtnet_info *vi = netdev_priv(dev); 126666846048SRick Jones struct virtio_device *vdev = vi->vdev; 126766846048SRick Jones 126866846048SRick Jones strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver)); 126966846048SRick Jones strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version)); 127066846048SRick Jones strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info)); 127166846048SRick Jones 127266846048SRick Jones } 127366846048SRick Jones 1274d73bcd2cSJason Wang /* TODO: Eliminate OOO packets during switching */ 1275d73bcd2cSJason Wang static int virtnet_set_channels(struct net_device *dev, 1276d73bcd2cSJason Wang struct ethtool_channels *channels) 1277d73bcd2cSJason Wang { 1278d73bcd2cSJason Wang struct virtnet_info *vi = netdev_priv(dev); 1279d73bcd2cSJason Wang u16 queue_pairs = channels->combined_count; 1280d73bcd2cSJason Wang int err; 1281d73bcd2cSJason Wang 1282d73bcd2cSJason Wang /* We don't support separate rx/tx channels. 1283d73bcd2cSJason Wang * We don't allow setting 'other' channels. 1284d73bcd2cSJason Wang */ 1285d73bcd2cSJason Wang if (channels->rx_count || channels->tx_count || channels->other_count) 1286d73bcd2cSJason Wang return -EINVAL; 1287d73bcd2cSJason Wang 1288d73bcd2cSJason Wang if (queue_pairs > vi->max_queue_pairs) 1289d73bcd2cSJason Wang return -EINVAL; 1290d73bcd2cSJason Wang 129147be2479SWanlong Gao get_online_cpus(); 1292d73bcd2cSJason Wang err = virtnet_set_queues(vi, queue_pairs); 1293d73bcd2cSJason Wang if (!err) { 1294d73bcd2cSJason Wang netif_set_real_num_tx_queues(dev, queue_pairs); 1295d73bcd2cSJason Wang netif_set_real_num_rx_queues(dev, queue_pairs); 1296d73bcd2cSJason Wang 12978898c21cSWanlong Gao virtnet_set_affinity(vi); 1298d73bcd2cSJason Wang } 129947be2479SWanlong Gao put_online_cpus(); 1300d73bcd2cSJason Wang 1301d73bcd2cSJason Wang return err; 1302d73bcd2cSJason Wang } 1303d73bcd2cSJason Wang 1304d73bcd2cSJason Wang static void virtnet_get_channels(struct net_device *dev, 1305d73bcd2cSJason Wang struct ethtool_channels *channels) 1306d73bcd2cSJason Wang { 1307d73bcd2cSJason Wang struct virtnet_info *vi = netdev_priv(dev); 1308d73bcd2cSJason Wang 1309d73bcd2cSJason Wang channels->combined_count = vi->curr_queue_pairs; 1310d73bcd2cSJason Wang channels->max_combined = vi->max_queue_pairs; 1311d73bcd2cSJason Wang channels->max_other = 0; 1312d73bcd2cSJason Wang channels->rx_count = 0; 1313d73bcd2cSJason Wang channels->tx_count = 0; 1314d73bcd2cSJason Wang channels->other_count = 0; 1315d73bcd2cSJason Wang } 1316d73bcd2cSJason Wang 13170fc0b732SStephen Hemminger static const struct ethtool_ops virtnet_ethtool_ops = { 131866846048SRick Jones .get_drvinfo = virtnet_get_drvinfo, 13199f4d26d0SMark McLoughlin .get_link = ethtool_op_get_link, 13208f9f4668SRick Jones .get_ringparam = virtnet_get_ringparam, 1321d73bcd2cSJason Wang .set_channels = virtnet_set_channels, 1322d73bcd2cSJason Wang .get_channels = virtnet_get_channels, 1323a9ea3fc6SHerbert Xu }; 1324a9ea3fc6SHerbert Xu 132539da5814SMark McLoughlin #define MIN_MTU 68 132639da5814SMark McLoughlin #define MAX_MTU 65535 132739da5814SMark McLoughlin 132839da5814SMark McLoughlin static int virtnet_change_mtu(struct net_device *dev, int new_mtu) 132939da5814SMark McLoughlin { 133039da5814SMark McLoughlin if (new_mtu < MIN_MTU || new_mtu > MAX_MTU) 133139da5814SMark McLoughlin return -EINVAL; 133239da5814SMark McLoughlin dev->mtu = new_mtu; 133339da5814SMark McLoughlin return 0; 133439da5814SMark McLoughlin } 133539da5814SMark McLoughlin 133676288b4eSStephen Hemminger static const struct net_device_ops virtnet_netdev = { 133776288b4eSStephen Hemminger .ndo_open = virtnet_open, 133876288b4eSStephen Hemminger .ndo_stop = virtnet_close, 133976288b4eSStephen Hemminger .ndo_start_xmit = start_xmit, 134076288b4eSStephen Hemminger .ndo_validate_addr = eth_validate_addr, 13419c46f6d4SAlex Williamson .ndo_set_mac_address = virtnet_set_mac_address, 13422af7698eSAlex Williamson .ndo_set_rx_mode = virtnet_set_rx_mode, 134376288b4eSStephen Hemminger .ndo_change_mtu = virtnet_change_mtu, 13443fa2a1dfSstephen hemminger .ndo_get_stats64 = virtnet_stats, 13451824a989SAlex Williamson .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid, 13461824a989SAlex Williamson .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid, 134776288b4eSStephen Hemminger #ifdef CONFIG_NET_POLL_CONTROLLER 134876288b4eSStephen Hemminger .ndo_poll_controller = virtnet_netpoll, 134976288b4eSStephen Hemminger #endif 135076288b4eSStephen Hemminger }; 135176288b4eSStephen Hemminger 1352586d17c5SJason Wang static void virtnet_config_changed_work(struct work_struct *work) 13539f4d26d0SMark McLoughlin { 1354586d17c5SJason Wang struct virtnet_info *vi = 1355586d17c5SJason Wang container_of(work, struct virtnet_info, config_work); 13569f4d26d0SMark McLoughlin u16 v; 13579f4d26d0SMark McLoughlin 1358586d17c5SJason Wang mutex_lock(&vi->config_lock); 1359586d17c5SJason Wang if (!vi->config_enable) 1360586d17c5SJason Wang goto done; 1361586d17c5SJason Wang 1362855e0c52SRusty Russell if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS, 1363855e0c52SRusty Russell struct virtio_net_config, status, &v) < 0) 1364586d17c5SJason Wang goto done; 1365586d17c5SJason Wang 1366586d17c5SJason Wang if (v & VIRTIO_NET_S_ANNOUNCE) { 1367ee89bab1SAmerigo Wang netdev_notify_peers(vi->dev); 1368586d17c5SJason Wang virtnet_ack_link_announce(vi); 1369586d17c5SJason Wang } 13709f4d26d0SMark McLoughlin 13719f4d26d0SMark McLoughlin /* Ignore unknown (future) status bits */ 13729f4d26d0SMark McLoughlin v &= VIRTIO_NET_S_LINK_UP; 13739f4d26d0SMark McLoughlin 13749f4d26d0SMark McLoughlin if (vi->status == v) 1375586d17c5SJason Wang goto done; 13769f4d26d0SMark McLoughlin 13779f4d26d0SMark McLoughlin vi->status = v; 13789f4d26d0SMark McLoughlin 13799f4d26d0SMark McLoughlin if (vi->status & VIRTIO_NET_S_LINK_UP) { 13809f4d26d0SMark McLoughlin netif_carrier_on(vi->dev); 1381986a4f4dSJason Wang netif_tx_wake_all_queues(vi->dev); 13829f4d26d0SMark McLoughlin } else { 13839f4d26d0SMark McLoughlin netif_carrier_off(vi->dev); 1384986a4f4dSJason Wang netif_tx_stop_all_queues(vi->dev); 13859f4d26d0SMark McLoughlin } 1386586d17c5SJason Wang done: 1387586d17c5SJason Wang mutex_unlock(&vi->config_lock); 13889f4d26d0SMark McLoughlin } 13899f4d26d0SMark McLoughlin 13909f4d26d0SMark McLoughlin static void virtnet_config_changed(struct virtio_device *vdev) 13919f4d26d0SMark McLoughlin { 13929f4d26d0SMark McLoughlin struct virtnet_info *vi = vdev->priv; 13939f4d26d0SMark McLoughlin 13943b07e9caSTejun Heo schedule_work(&vi->config_work); 13959f4d26d0SMark McLoughlin } 13969f4d26d0SMark McLoughlin 1397986a4f4dSJason Wang static void virtnet_free_queues(struct virtnet_info *vi) 1398986a4f4dSJason Wang { 1399d4fb84eeSAndrey Vagin int i; 1400d4fb84eeSAndrey Vagin 1401d4fb84eeSAndrey Vagin for (i = 0; i < vi->max_queue_pairs; i++) 1402d4fb84eeSAndrey Vagin netif_napi_del(&vi->rq[i].napi); 1403d4fb84eeSAndrey Vagin 1404986a4f4dSJason Wang kfree(vi->rq); 1405986a4f4dSJason Wang kfree(vi->sq); 1406986a4f4dSJason Wang } 1407986a4f4dSJason Wang 1408986a4f4dSJason Wang static void free_receive_bufs(struct virtnet_info *vi) 1409986a4f4dSJason Wang { 1410986a4f4dSJason Wang int i; 1411986a4f4dSJason Wang 1412986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1413986a4f4dSJason Wang while (vi->rq[i].pages) 1414986a4f4dSJason Wang __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0); 1415986a4f4dSJason Wang } 1416986a4f4dSJason Wang } 1417986a4f4dSJason Wang 1418fb51879dSMichael Dalton static void free_receive_page_frags(struct virtnet_info *vi) 1419fb51879dSMichael Dalton { 1420fb51879dSMichael Dalton int i; 1421fb51879dSMichael Dalton for (i = 0; i < vi->max_queue_pairs; i++) 1422fb51879dSMichael Dalton if (vi->rq[i].alloc_frag.page) 1423fb51879dSMichael Dalton put_page(vi->rq[i].alloc_frag.page); 1424fb51879dSMichael Dalton } 1425fb51879dSMichael Dalton 1426986a4f4dSJason Wang static void free_unused_bufs(struct virtnet_info *vi) 1427986a4f4dSJason Wang { 1428986a4f4dSJason Wang void *buf; 1429986a4f4dSJason Wang int i; 1430986a4f4dSJason Wang 1431986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1432986a4f4dSJason Wang struct virtqueue *vq = vi->sq[i].vq; 1433986a4f4dSJason Wang while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) 1434986a4f4dSJason Wang dev_kfree_skb(buf); 1435986a4f4dSJason Wang } 1436986a4f4dSJason Wang 1437986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1438986a4f4dSJason Wang struct virtqueue *vq = vi->rq[i].vq; 1439986a4f4dSJason Wang 1440986a4f4dSJason Wang while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) { 1441ab7db917SMichael Dalton if (vi->mergeable_rx_bufs) { 1442ab7db917SMichael Dalton unsigned long ctx = (unsigned long)buf; 1443ab7db917SMichael Dalton void *base = mergeable_ctx_to_buf_address(ctx); 1444ab7db917SMichael Dalton put_page(virt_to_head_page(base)); 1445ab7db917SMichael Dalton } else if (vi->big_packets) { 1446fa9fac17SAndrey Vagin give_pages(&vi->rq[i], buf); 1447ab7db917SMichael Dalton } else { 1448986a4f4dSJason Wang dev_kfree_skb(buf); 1449986a4f4dSJason Wang } 1450986a4f4dSJason Wang } 1451986a4f4dSJason Wang } 1452ab7db917SMichael Dalton } 1453986a4f4dSJason Wang 1454e9d7417bSJason Wang static void virtnet_del_vqs(struct virtnet_info *vi) 1455e9d7417bSJason Wang { 1456e9d7417bSJason Wang struct virtio_device *vdev = vi->vdev; 1457e9d7417bSJason Wang 14588898c21cSWanlong Gao virtnet_clean_affinity(vi, -1); 1459986a4f4dSJason Wang 1460e9d7417bSJason Wang vdev->config->del_vqs(vdev); 1461986a4f4dSJason Wang 1462986a4f4dSJason Wang virtnet_free_queues(vi); 1463986a4f4dSJason Wang } 1464986a4f4dSJason Wang 1465986a4f4dSJason Wang static int virtnet_find_vqs(struct virtnet_info *vi) 1466986a4f4dSJason Wang { 1467986a4f4dSJason Wang vq_callback_t **callbacks; 1468986a4f4dSJason Wang struct virtqueue **vqs; 1469986a4f4dSJason Wang int ret = -ENOMEM; 1470986a4f4dSJason Wang int i, total_vqs; 1471986a4f4dSJason Wang const char **names; 1472986a4f4dSJason Wang 1473986a4f4dSJason Wang /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by 1474986a4f4dSJason Wang * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by 1475986a4f4dSJason Wang * possible control vq. 1476986a4f4dSJason Wang */ 1477986a4f4dSJason Wang total_vqs = vi->max_queue_pairs * 2 + 1478986a4f4dSJason Wang virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ); 1479986a4f4dSJason Wang 1480986a4f4dSJason Wang /* Allocate space for find_vqs parameters */ 1481986a4f4dSJason Wang vqs = kzalloc(total_vqs * sizeof(*vqs), GFP_KERNEL); 1482986a4f4dSJason Wang if (!vqs) 1483986a4f4dSJason Wang goto err_vq; 1484986a4f4dSJason Wang callbacks = kmalloc(total_vqs * sizeof(*callbacks), GFP_KERNEL); 1485986a4f4dSJason Wang if (!callbacks) 1486986a4f4dSJason Wang goto err_callback; 1487986a4f4dSJason Wang names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL); 1488986a4f4dSJason Wang if (!names) 1489986a4f4dSJason Wang goto err_names; 1490986a4f4dSJason Wang 1491986a4f4dSJason Wang /* Parameters for control virtqueue, if any */ 1492986a4f4dSJason Wang if (vi->has_cvq) { 1493986a4f4dSJason Wang callbacks[total_vqs - 1] = NULL; 1494986a4f4dSJason Wang names[total_vqs - 1] = "control"; 1495986a4f4dSJason Wang } 1496986a4f4dSJason Wang 1497986a4f4dSJason Wang /* Allocate/initialize parameters for send/receive virtqueues */ 1498986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1499986a4f4dSJason Wang callbacks[rxq2vq(i)] = skb_recv_done; 1500986a4f4dSJason Wang callbacks[txq2vq(i)] = skb_xmit_done; 1501986a4f4dSJason Wang sprintf(vi->rq[i].name, "input.%d", i); 1502986a4f4dSJason Wang sprintf(vi->sq[i].name, "output.%d", i); 1503986a4f4dSJason Wang names[rxq2vq(i)] = vi->rq[i].name; 1504986a4f4dSJason Wang names[txq2vq(i)] = vi->sq[i].name; 1505986a4f4dSJason Wang } 1506986a4f4dSJason Wang 1507986a4f4dSJason Wang ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks, 1508986a4f4dSJason Wang names); 1509986a4f4dSJason Wang if (ret) 1510986a4f4dSJason Wang goto err_find; 1511986a4f4dSJason Wang 1512986a4f4dSJason Wang if (vi->has_cvq) { 1513986a4f4dSJason Wang vi->cvq = vqs[total_vqs - 1]; 1514986a4f4dSJason Wang if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN)) 1515f646968fSPatrick McHardy vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER; 1516986a4f4dSJason Wang } 1517986a4f4dSJason Wang 1518986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1519986a4f4dSJason Wang vi->rq[i].vq = vqs[rxq2vq(i)]; 1520986a4f4dSJason Wang vi->sq[i].vq = vqs[txq2vq(i)]; 1521986a4f4dSJason Wang } 1522986a4f4dSJason Wang 1523986a4f4dSJason Wang kfree(names); 1524986a4f4dSJason Wang kfree(callbacks); 1525986a4f4dSJason Wang kfree(vqs); 1526986a4f4dSJason Wang 1527986a4f4dSJason Wang return 0; 1528986a4f4dSJason Wang 1529986a4f4dSJason Wang err_find: 1530986a4f4dSJason Wang kfree(names); 1531986a4f4dSJason Wang err_names: 1532986a4f4dSJason Wang kfree(callbacks); 1533986a4f4dSJason Wang err_callback: 1534986a4f4dSJason Wang kfree(vqs); 1535986a4f4dSJason Wang err_vq: 1536986a4f4dSJason Wang return ret; 1537986a4f4dSJason Wang } 1538986a4f4dSJason Wang 1539986a4f4dSJason Wang static int virtnet_alloc_queues(struct virtnet_info *vi) 1540986a4f4dSJason Wang { 1541986a4f4dSJason Wang int i; 1542986a4f4dSJason Wang 1543986a4f4dSJason Wang vi->sq = kzalloc(sizeof(*vi->sq) * vi->max_queue_pairs, GFP_KERNEL); 1544986a4f4dSJason Wang if (!vi->sq) 1545986a4f4dSJason Wang goto err_sq; 1546986a4f4dSJason Wang vi->rq = kzalloc(sizeof(*vi->rq) * vi->max_queue_pairs, GFP_KERNEL); 1547008d4278SAmerigo Wang if (!vi->rq) 1548986a4f4dSJason Wang goto err_rq; 1549986a4f4dSJason Wang 1550986a4f4dSJason Wang INIT_DELAYED_WORK(&vi->refill, refill_work); 1551986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1552986a4f4dSJason Wang vi->rq[i].pages = NULL; 1553986a4f4dSJason Wang netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll, 1554986a4f4dSJason Wang napi_weight); 1555986a4f4dSJason Wang 1556986a4f4dSJason Wang sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg)); 1557ab7db917SMichael Dalton ewma_init(&vi->rq[i].mrg_avg_pkt_len, 1, RECEIVE_AVG_WEIGHT); 1558986a4f4dSJason Wang sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg)); 1559986a4f4dSJason Wang } 1560986a4f4dSJason Wang 1561986a4f4dSJason Wang return 0; 1562986a4f4dSJason Wang 1563986a4f4dSJason Wang err_rq: 1564986a4f4dSJason Wang kfree(vi->sq); 1565986a4f4dSJason Wang err_sq: 1566986a4f4dSJason Wang return -ENOMEM; 1567e9d7417bSJason Wang } 1568e9d7417bSJason Wang 15693f9c10b0SAmit Shah static int init_vqs(struct virtnet_info *vi) 15703f9c10b0SAmit Shah { 1571986a4f4dSJason Wang int ret; 15723f9c10b0SAmit Shah 1573986a4f4dSJason Wang /* Allocate send & receive queues */ 1574986a4f4dSJason Wang ret = virtnet_alloc_queues(vi); 1575986a4f4dSJason Wang if (ret) 1576986a4f4dSJason Wang goto err; 15773f9c10b0SAmit Shah 1578986a4f4dSJason Wang ret = virtnet_find_vqs(vi); 1579986a4f4dSJason Wang if (ret) 1580986a4f4dSJason Wang goto err_free; 15813f9c10b0SAmit Shah 158247be2479SWanlong Gao get_online_cpus(); 15838898c21cSWanlong Gao virtnet_set_affinity(vi); 158447be2479SWanlong Gao put_online_cpus(); 158547be2479SWanlong Gao 15863f9c10b0SAmit Shah return 0; 1587986a4f4dSJason Wang 1588986a4f4dSJason Wang err_free: 1589986a4f4dSJason Wang virtnet_free_queues(vi); 1590986a4f4dSJason Wang err: 1591986a4f4dSJason Wang return ret; 15923f9c10b0SAmit Shah } 15933f9c10b0SAmit Shah 1594fbf28d78SMichael Dalton #ifdef CONFIG_SYSFS 1595fbf28d78SMichael Dalton static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue, 1596fbf28d78SMichael Dalton struct rx_queue_attribute *attribute, char *buf) 1597fbf28d78SMichael Dalton { 1598fbf28d78SMichael Dalton struct virtnet_info *vi = netdev_priv(queue->dev); 1599fbf28d78SMichael Dalton unsigned int queue_index = get_netdev_rx_queue_index(queue); 1600fbf28d78SMichael Dalton struct ewma *avg; 1601fbf28d78SMichael Dalton 1602fbf28d78SMichael Dalton BUG_ON(queue_index >= vi->max_queue_pairs); 1603fbf28d78SMichael Dalton avg = &vi->rq[queue_index].mrg_avg_pkt_len; 1604fbf28d78SMichael Dalton return sprintf(buf, "%u\n", get_mergeable_buf_len(avg)); 1605fbf28d78SMichael Dalton } 1606fbf28d78SMichael Dalton 1607fbf28d78SMichael Dalton static struct rx_queue_attribute mergeable_rx_buffer_size_attribute = 1608fbf28d78SMichael Dalton __ATTR_RO(mergeable_rx_buffer_size); 1609fbf28d78SMichael Dalton 1610fbf28d78SMichael Dalton static struct attribute *virtio_net_mrg_rx_attrs[] = { 1611fbf28d78SMichael Dalton &mergeable_rx_buffer_size_attribute.attr, 1612fbf28d78SMichael Dalton NULL 1613fbf28d78SMichael Dalton }; 1614fbf28d78SMichael Dalton 1615fbf28d78SMichael Dalton static const struct attribute_group virtio_net_mrg_rx_group = { 1616fbf28d78SMichael Dalton .name = "virtio_net", 1617fbf28d78SMichael Dalton .attrs = virtio_net_mrg_rx_attrs 1618fbf28d78SMichael Dalton }; 1619fbf28d78SMichael Dalton #endif 1620fbf28d78SMichael Dalton 1621296f96fcSRusty Russell static int virtnet_probe(struct virtio_device *vdev) 1622296f96fcSRusty Russell { 1623986a4f4dSJason Wang int i, err; 1624296f96fcSRusty Russell struct net_device *dev; 1625296f96fcSRusty Russell struct virtnet_info *vi; 1626986a4f4dSJason Wang u16 max_queue_pairs; 1627986a4f4dSJason Wang 1628986a4f4dSJason Wang /* Find if host supports multiqueue virtio_net device */ 1629855e0c52SRusty Russell err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ, 1630855e0c52SRusty Russell struct virtio_net_config, 1631855e0c52SRusty Russell max_virtqueue_pairs, &max_queue_pairs); 1632986a4f4dSJason Wang 1633986a4f4dSJason Wang /* We need at least 2 queue's */ 1634986a4f4dSJason Wang if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN || 1635986a4f4dSJason Wang max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX || 1636986a4f4dSJason Wang !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) 1637986a4f4dSJason Wang max_queue_pairs = 1; 1638296f96fcSRusty Russell 1639296f96fcSRusty Russell /* Allocate ourselves a network device with room for our info */ 1640986a4f4dSJason Wang dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs); 1641296f96fcSRusty Russell if (!dev) 1642296f96fcSRusty Russell return -ENOMEM; 1643296f96fcSRusty Russell 1644296f96fcSRusty Russell /* Set up network device as normal. */ 1645f2f2c8b4SJiri Pirko dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE; 164676288b4eSStephen Hemminger dev->netdev_ops = &virtnet_netdev; 1647296f96fcSRusty Russell dev->features = NETIF_F_HIGHDMA; 16483fa2a1dfSstephen hemminger 1649a9ea3fc6SHerbert Xu SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops); 1650296f96fcSRusty Russell SET_NETDEV_DEV(dev, &vdev->dev); 1651296f96fcSRusty Russell 1652296f96fcSRusty Russell /* Do we support "hardware" checksums? */ 165398e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) { 1654296f96fcSRusty Russell /* This opens up the world of extra features. */ 165598e778c9SMichał Mirosław dev->hw_features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST; 165698e778c9SMichał Mirosław if (csum) 1657296f96fcSRusty Russell dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST; 165898e778c9SMichał Mirosław 165998e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) { 166098e778c9SMichał Mirosław dev->hw_features |= NETIF_F_TSO | NETIF_F_UFO 166134a48579SRusty Russell | NETIF_F_TSO_ECN | NETIF_F_TSO6; 166234a48579SRusty Russell } 16635539ae96SRusty Russell /* Individual feature bits: what can host handle? */ 166498e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4)) 166598e778c9SMichał Mirosław dev->hw_features |= NETIF_F_TSO; 166698e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6)) 166798e778c9SMichał Mirosław dev->hw_features |= NETIF_F_TSO6; 166898e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN)) 166998e778c9SMichał Mirosław dev->hw_features |= NETIF_F_TSO_ECN; 167098e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO)) 167198e778c9SMichał Mirosław dev->hw_features |= NETIF_F_UFO; 167298e778c9SMichał Mirosław 167398e778c9SMichał Mirosław if (gso) 167498e778c9SMichał Mirosław dev->features |= dev->hw_features & (NETIF_F_ALL_TSO|NETIF_F_UFO); 167598e778c9SMichał Mirosław /* (!csum && gso) case will be fixed by register_netdev() */ 1676296f96fcSRusty Russell } 16774f49129bSThomas Huth if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM)) 16784f49129bSThomas Huth dev->features |= NETIF_F_RXCSUM; 1679296f96fcSRusty Russell 16804fda8302SJason Wang dev->vlan_features = dev->features; 16814fda8302SJason Wang 1682296f96fcSRusty Russell /* Configuration may specify what MAC to use. Otherwise random. */ 1683855e0c52SRusty Russell if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) 1684855e0c52SRusty Russell virtio_cread_bytes(vdev, 1685a586d4f6SRusty Russell offsetof(struct virtio_net_config, mac), 1686855e0c52SRusty Russell dev->dev_addr, dev->addr_len); 1687855e0c52SRusty Russell else 1688f2cedb63SDanny Kukawka eth_hw_addr_random(dev); 1689296f96fcSRusty Russell 1690296f96fcSRusty Russell /* Set up our device-specific information */ 1691296f96fcSRusty Russell vi = netdev_priv(dev); 1692296f96fcSRusty Russell vi->dev = dev; 1693296f96fcSRusty Russell vi->vdev = vdev; 1694d9d5dcc8SChristian Borntraeger vdev->priv = vi; 16953fa2a1dfSstephen hemminger vi->stats = alloc_percpu(struct virtnet_stats); 16963fa2a1dfSstephen hemminger err = -ENOMEM; 16973fa2a1dfSstephen hemminger if (vi->stats == NULL) 16983fa2a1dfSstephen hemminger goto free; 16993fa2a1dfSstephen hemminger 1700827da44cSJohn Stultz for_each_possible_cpu(i) { 1701827da44cSJohn Stultz struct virtnet_stats *virtnet_stats; 1702827da44cSJohn Stultz virtnet_stats = per_cpu_ptr(vi->stats, i); 1703827da44cSJohn Stultz u64_stats_init(&virtnet_stats->tx_syncp); 1704827da44cSJohn Stultz u64_stats_init(&virtnet_stats->rx_syncp); 1705827da44cSJohn Stultz } 1706827da44cSJohn Stultz 1707586d17c5SJason Wang mutex_init(&vi->config_lock); 1708586d17c5SJason Wang vi->config_enable = true; 1709586d17c5SJason Wang INIT_WORK(&vi->config_work, virtnet_config_changed_work); 1710296f96fcSRusty Russell 171197402b96SHerbert Xu /* If we can receive ANY GSO packets, we must allocate large ones. */ 17128e95a202SJoe Perches if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) || 17138e95a202SJoe Perches virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) || 17140e7ede80SJason Wang virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) || 17150e7ede80SJason Wang virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO)) 171697402b96SHerbert Xu vi->big_packets = true; 171797402b96SHerbert Xu 17183f2c31d9SMark McLoughlin if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF)) 17193f2c31d9SMark McLoughlin vi->mergeable_rx_bufs = true; 17203f2c31d9SMark McLoughlin 1721e7428e95SMichael S. Tsirkin if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT)) 1722e7428e95SMichael S. Tsirkin vi->any_header_sg = true; 1723e7428e95SMichael S. Tsirkin 1724986a4f4dSJason Wang if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) 1725986a4f4dSJason Wang vi->has_cvq = true; 1726986a4f4dSJason Wang 1727986a4f4dSJason Wang /* Use single tx/rx queue pair as default */ 1728986a4f4dSJason Wang vi->curr_queue_pairs = 1; 1729986a4f4dSJason Wang vi->max_queue_pairs = max_queue_pairs; 1730986a4f4dSJason Wang 1731986a4f4dSJason Wang /* Allocate/initialize the rx/tx queues, and invoke find_vqs */ 17323f9c10b0SAmit Shah err = init_vqs(vi); 1733d2a7dddaSMichael S. Tsirkin if (err) 17349bb8ca86SJason Wang goto free_stats; 1735d2a7dddaSMichael S. Tsirkin 1736fbf28d78SMichael Dalton #ifdef CONFIG_SYSFS 1737fbf28d78SMichael Dalton if (vi->mergeable_rx_bufs) 1738fbf28d78SMichael Dalton dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group; 1739fbf28d78SMichael Dalton #endif 17400f13b66bSZhi Yong Wu netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs); 17410f13b66bSZhi Yong Wu netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs); 1742986a4f4dSJason Wang 1743296f96fcSRusty Russell err = register_netdev(dev); 1744296f96fcSRusty Russell if (err) { 1745296f96fcSRusty Russell pr_debug("virtio_net: registering device failed\n"); 1746d2a7dddaSMichael S. Tsirkin goto free_vqs; 1747296f96fcSRusty Russell } 1748b3369c1fSRusty Russell 1749b3369c1fSRusty Russell /* Last of all, set up some receive buffers. */ 175055257d72SSasha Levin for (i = 0; i < vi->curr_queue_pairs; i++) { 1751986a4f4dSJason Wang try_fill_recv(&vi->rq[i], GFP_KERNEL); 1752b3369c1fSRusty Russell 1753b3369c1fSRusty Russell /* If we didn't even get one input buffer, we're useless. */ 1754be121f46SJason Wang if (vi->rq[i].vq->num_free == 1755be121f46SJason Wang virtqueue_get_vring_size(vi->rq[i].vq)) { 1756986a4f4dSJason Wang free_unused_bufs(vi); 1757b3369c1fSRusty Russell err = -ENOMEM; 1758986a4f4dSJason Wang goto free_recv_bufs; 1759986a4f4dSJason Wang } 1760b3369c1fSRusty Russell } 1761b3369c1fSRusty Russell 17628de4b2f3SWanlong Gao vi->nb.notifier_call = &virtnet_cpu_callback; 17638de4b2f3SWanlong Gao err = register_hotcpu_notifier(&vi->nb); 17648de4b2f3SWanlong Gao if (err) { 17658de4b2f3SWanlong Gao pr_debug("virtio_net: registering cpu notifier failed\n"); 17668de4b2f3SWanlong Gao goto free_recv_bufs; 17678de4b2f3SWanlong Gao } 17688de4b2f3SWanlong Gao 1769167c25e4SJason Wang /* Assume link up if device can't report link status, 1770167c25e4SJason Wang otherwise get link status from config. */ 1771167c25e4SJason Wang if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) { 1772167c25e4SJason Wang netif_carrier_off(dev); 17733b07e9caSTejun Heo schedule_work(&vi->config_work); 1774167c25e4SJason Wang } else { 1775167c25e4SJason Wang vi->status = VIRTIO_NET_S_LINK_UP; 17764783256eSPantelis Koukousoulas netif_carrier_on(dev); 1777167c25e4SJason Wang } 17789f4d26d0SMark McLoughlin 1779986a4f4dSJason Wang pr_debug("virtnet: registered device %s with %d RX and TX vq's\n", 1780986a4f4dSJason Wang dev->name, max_queue_pairs); 1781986a4f4dSJason Wang 1782296f96fcSRusty Russell return 0; 1783296f96fcSRusty Russell 1784986a4f4dSJason Wang free_recv_bufs: 1785986a4f4dSJason Wang free_receive_bufs(vi); 1786b3369c1fSRusty Russell unregister_netdev(dev); 1787d2a7dddaSMichael S. Tsirkin free_vqs: 1788986a4f4dSJason Wang cancel_delayed_work_sync(&vi->refill); 1789fb51879dSMichael Dalton free_receive_page_frags(vi); 1790e9d7417bSJason Wang virtnet_del_vqs(vi); 17913fa2a1dfSstephen hemminger free_stats: 17923fa2a1dfSstephen hemminger free_percpu(vi->stats); 1793296f96fcSRusty Russell free: 1794296f96fcSRusty Russell free_netdev(dev); 1795296f96fcSRusty Russell return err; 1796296f96fcSRusty Russell } 1797296f96fcSRusty Russell 179804486ed0SAmit Shah static void remove_vq_common(struct virtnet_info *vi) 1799296f96fcSRusty Russell { 180004486ed0SAmit Shah vi->vdev->config->reset(vi->vdev); 1801830a8a97SShirley Ma 1802830a8a97SShirley Ma /* Free unused buffers in both send and recv, if any. */ 18039ab86bbcSShirley Ma free_unused_bufs(vi); 1804fb6813f4SRusty Russell 1805986a4f4dSJason Wang free_receive_bufs(vi); 1806d2a7dddaSMichael S. Tsirkin 1807fb51879dSMichael Dalton free_receive_page_frags(vi); 1808fb51879dSMichael Dalton 1809986a4f4dSJason Wang virtnet_del_vqs(vi); 181004486ed0SAmit Shah } 181104486ed0SAmit Shah 18128cc085d6SBill Pemberton static void virtnet_remove(struct virtio_device *vdev) 181304486ed0SAmit Shah { 181404486ed0SAmit Shah struct virtnet_info *vi = vdev->priv; 181504486ed0SAmit Shah 18168de4b2f3SWanlong Gao unregister_hotcpu_notifier(&vi->nb); 18178de4b2f3SWanlong Gao 1818586d17c5SJason Wang /* Prevent config work handler from accessing the device. */ 1819586d17c5SJason Wang mutex_lock(&vi->config_lock); 1820586d17c5SJason Wang vi->config_enable = false; 1821586d17c5SJason Wang mutex_unlock(&vi->config_lock); 1822586d17c5SJason Wang 182304486ed0SAmit Shah unregister_netdev(vi->dev); 182404486ed0SAmit Shah 182504486ed0SAmit Shah remove_vq_common(vi); 1826fb6813f4SRusty Russell 1827586d17c5SJason Wang flush_work(&vi->config_work); 1828586d17c5SJason Wang 18292e66f55bSKrishna Kumar free_percpu(vi->stats); 183074b2553fSRusty Russell free_netdev(vi->dev); 1831296f96fcSRusty Russell } 1832296f96fcSRusty Russell 183389107000SAaron Lu #ifdef CONFIG_PM_SLEEP 18340741bcb5SAmit Shah static int virtnet_freeze(struct virtio_device *vdev) 18350741bcb5SAmit Shah { 18360741bcb5SAmit Shah struct virtnet_info *vi = vdev->priv; 1837986a4f4dSJason Wang int i; 18380741bcb5SAmit Shah 1839ec9debbdSJason Wang unregister_hotcpu_notifier(&vi->nb); 1840ec9debbdSJason Wang 1841586d17c5SJason Wang /* Prevent config work handler from accessing the device */ 1842586d17c5SJason Wang mutex_lock(&vi->config_lock); 1843586d17c5SJason Wang vi->config_enable = false; 1844586d17c5SJason Wang mutex_unlock(&vi->config_lock); 1845586d17c5SJason Wang 18460741bcb5SAmit Shah netif_device_detach(vi->dev); 18470741bcb5SAmit Shah cancel_delayed_work_sync(&vi->refill); 18480741bcb5SAmit Shah 18490741bcb5SAmit Shah if (netif_running(vi->dev)) 1850986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1851986a4f4dSJason Wang napi_disable(&vi->rq[i].napi); 1852986a4f4dSJason Wang netif_napi_del(&vi->rq[i].napi); 1853986a4f4dSJason Wang } 18540741bcb5SAmit Shah 18550741bcb5SAmit Shah remove_vq_common(vi); 18560741bcb5SAmit Shah 1857586d17c5SJason Wang flush_work(&vi->config_work); 1858586d17c5SJason Wang 18590741bcb5SAmit Shah return 0; 18600741bcb5SAmit Shah } 18610741bcb5SAmit Shah 18620741bcb5SAmit Shah static int virtnet_restore(struct virtio_device *vdev) 18630741bcb5SAmit Shah { 18640741bcb5SAmit Shah struct virtnet_info *vi = vdev->priv; 1865986a4f4dSJason Wang int err, i; 18660741bcb5SAmit Shah 18670741bcb5SAmit Shah err = init_vqs(vi); 18680741bcb5SAmit Shah if (err) 18690741bcb5SAmit Shah return err; 18700741bcb5SAmit Shah 18716cd4ce00SJason Wang if (netif_running(vi->dev)) { 187255257d72SSasha Levin for (i = 0; i < vi->curr_queue_pairs; i++) 1873986a4f4dSJason Wang if (!try_fill_recv(&vi->rq[i], GFP_KERNEL)) 18743b07e9caSTejun Heo schedule_delayed_work(&vi->refill, 0); 18750741bcb5SAmit Shah 18766cd4ce00SJason Wang for (i = 0; i < vi->max_queue_pairs; i++) 18776cd4ce00SJason Wang virtnet_napi_enable(&vi->rq[i]); 18786cd4ce00SJason Wang } 18796cd4ce00SJason Wang 18806cd4ce00SJason Wang netif_device_attach(vi->dev); 18816cd4ce00SJason Wang 1882586d17c5SJason Wang mutex_lock(&vi->config_lock); 1883586d17c5SJason Wang vi->config_enable = true; 1884586d17c5SJason Wang mutex_unlock(&vi->config_lock); 1885586d17c5SJason Wang 188635ed159bSJason Wang rtnl_lock(); 1887986a4f4dSJason Wang virtnet_set_queues(vi, vi->curr_queue_pairs); 188835ed159bSJason Wang rtnl_unlock(); 1889986a4f4dSJason Wang 1890ec9debbdSJason Wang err = register_hotcpu_notifier(&vi->nb); 1891ec9debbdSJason Wang if (err) 1892ec9debbdSJason Wang return err; 1893ec9debbdSJason Wang 18940741bcb5SAmit Shah return 0; 18950741bcb5SAmit Shah } 18960741bcb5SAmit Shah #endif 18970741bcb5SAmit Shah 1898296f96fcSRusty Russell static struct virtio_device_id id_table[] = { 1899296f96fcSRusty Russell { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID }, 1900296f96fcSRusty Russell { 0 }, 1901296f96fcSRusty Russell }; 1902296f96fcSRusty Russell 1903c45a6816SRusty Russell static unsigned int features[] = { 19045e4fe5c4SMark McLoughlin VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, 19055e4fe5c4SMark McLoughlin VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC, 1906c45a6816SRusty Russell VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, 190797402b96SHerbert Xu VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, 19085c516751SSridhar Samudrala VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, 19092a41f71dSAlex Williamson VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, 19100bde9569SAlex Williamson VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, 1911986a4f4dSJason Wang VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, 19127e58d5aeSAmos Kong VIRTIO_NET_F_CTRL_MAC_ADDR, 1913e7428e95SMichael S. Tsirkin VIRTIO_F_ANY_LAYOUT, 1914c45a6816SRusty Russell }; 1915c45a6816SRusty Russell 191622402529SUwe Kleine-König static struct virtio_driver virtio_net_driver = { 1917c45a6816SRusty Russell .feature_table = features, 1918c45a6816SRusty Russell .feature_table_size = ARRAY_SIZE(features), 1919296f96fcSRusty Russell .driver.name = KBUILD_MODNAME, 1920296f96fcSRusty Russell .driver.owner = THIS_MODULE, 1921296f96fcSRusty Russell .id_table = id_table, 1922296f96fcSRusty Russell .probe = virtnet_probe, 19238cc085d6SBill Pemberton .remove = virtnet_remove, 19249f4d26d0SMark McLoughlin .config_changed = virtnet_config_changed, 192589107000SAaron Lu #ifdef CONFIG_PM_SLEEP 19260741bcb5SAmit Shah .freeze = virtnet_freeze, 19270741bcb5SAmit Shah .restore = virtnet_restore, 19280741bcb5SAmit Shah #endif 1929296f96fcSRusty Russell }; 1930296f96fcSRusty Russell 1931b2a17029SRusty Russell module_virtio_driver(virtio_net_driver); 1932296f96fcSRusty Russell 1933296f96fcSRusty Russell MODULE_DEVICE_TABLE(virtio, id_table); 1934296f96fcSRusty Russell MODULE_DESCRIPTION("Virtio network driver"); 1935296f96fcSRusty Russell MODULE_LICENSE("GPL"); 1936