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> 3091815639SJason Wang #include <net/busy_poll.h> 31296f96fcSRusty Russell 32d34710e3SAmerigo Wang static int napi_weight = NAPI_POLL_WEIGHT; 336c0cd7c0SDor Laor module_param(napi_weight, int, 0444); 346c0cd7c0SDor Laor 35eb939922SRusty Russell static bool csum = true, gso = true; 3634a48579SRusty Russell module_param(csum, bool, 0444); 3734a48579SRusty Russell module_param(gso, bool, 0444); 3834a48579SRusty Russell 39296f96fcSRusty Russell /* FIXME: MTU in config. */ 405061de36SMichael Dalton #define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN) 413f2c31d9SMark McLoughlin #define GOOD_COPY_LEN 128 42296f96fcSRusty Russell 43ab7db917SMichael Dalton /* Weight used for the RX packet size EWMA. The average packet size is used to 44ab7db917SMichael Dalton * determine the packet buffer size when refilling RX rings. As the entire RX 45ab7db917SMichael Dalton * ring may be refilled at once, the weight is chosen so that the EWMA will be 46ab7db917SMichael Dalton * insensitive to short-term, transient changes in packet size. 47ab7db917SMichael Dalton */ 48ab7db917SMichael Dalton #define RECEIVE_AVG_WEIGHT 64 49ab7db917SMichael Dalton 50ab7db917SMichael Dalton /* Minimum alignment for mergeable packet buffers. */ 51ab7db917SMichael Dalton #define MERGEABLE_BUFFER_ALIGN max(L1_CACHE_BYTES, 256) 52ab7db917SMichael Dalton 5366846048SRick Jones #define VIRTNET_DRIVER_VERSION "1.0.0" 542a41f71dSAlex Williamson 553fa2a1dfSstephen hemminger struct virtnet_stats { 5683a27052SEric Dumazet struct u64_stats_sync tx_syncp; 5783a27052SEric Dumazet struct u64_stats_sync rx_syncp; 583fa2a1dfSstephen hemminger u64 tx_bytes; 593fa2a1dfSstephen hemminger u64 tx_packets; 603fa2a1dfSstephen hemminger 613fa2a1dfSstephen hemminger u64 rx_bytes; 623fa2a1dfSstephen hemminger u64 rx_packets; 633fa2a1dfSstephen hemminger }; 643fa2a1dfSstephen hemminger 65e9d7417bSJason Wang /* Internal representation of a send virtqueue */ 66e9d7417bSJason Wang struct send_queue { 67e9d7417bSJason Wang /* Virtqueue associated with this send _queue */ 68e9d7417bSJason Wang struct virtqueue *vq; 69e9d7417bSJason Wang 70e9d7417bSJason Wang /* TX: fragments + linear part + virtio header */ 71e9d7417bSJason Wang struct scatterlist sg[MAX_SKB_FRAGS + 2]; 72986a4f4dSJason Wang 73986a4f4dSJason Wang /* Name of the send queue: output.$index */ 74986a4f4dSJason Wang char name[40]; 75e9d7417bSJason Wang }; 76e9d7417bSJason Wang 77e9d7417bSJason Wang /* Internal representation of a receive virtqueue */ 78e9d7417bSJason Wang struct receive_queue { 79e9d7417bSJason Wang /* Virtqueue associated with this receive_queue */ 80e9d7417bSJason Wang struct virtqueue *vq; 81e9d7417bSJason Wang 82296f96fcSRusty Russell struct napi_struct napi; 83296f96fcSRusty Russell 84e9d7417bSJason Wang /* Chain pages by the private ptr. */ 85e9d7417bSJason Wang struct page *pages; 86e9d7417bSJason Wang 87ab7db917SMichael Dalton /* Average packet length for mergeable receive buffers. */ 88ab7db917SMichael Dalton struct ewma mrg_avg_pkt_len; 89ab7db917SMichael Dalton 90fb51879dSMichael Dalton /* Page frag for packet buffer allocation. */ 91fb51879dSMichael Dalton struct page_frag alloc_frag; 92fb51879dSMichael Dalton 93e9d7417bSJason Wang /* RX: fragments + linear part + virtio header */ 94e9d7417bSJason Wang struct scatterlist sg[MAX_SKB_FRAGS + 2]; 95986a4f4dSJason Wang 96986a4f4dSJason Wang /* Name of this receive queue: input.$index */ 97986a4f4dSJason Wang char name[40]; 98e9d7417bSJason Wang }; 99e9d7417bSJason Wang 100e9d7417bSJason Wang struct virtnet_info { 101e9d7417bSJason Wang struct virtio_device *vdev; 102e9d7417bSJason Wang struct virtqueue *cvq; 103e9d7417bSJason Wang struct net_device *dev; 104986a4f4dSJason Wang struct send_queue *sq; 105986a4f4dSJason Wang struct receive_queue *rq; 106e9d7417bSJason Wang unsigned int status; 107e9d7417bSJason Wang 108986a4f4dSJason Wang /* Max # of queue pairs supported by the device */ 109986a4f4dSJason Wang u16 max_queue_pairs; 110986a4f4dSJason Wang 111986a4f4dSJason Wang /* # of queue pairs currently used by the driver */ 112986a4f4dSJason Wang u16 curr_queue_pairs; 113986a4f4dSJason Wang 11497402b96SHerbert Xu /* I like... big packets and I cannot lie! */ 11597402b96SHerbert Xu bool big_packets; 11697402b96SHerbert Xu 1173f2c31d9SMark McLoughlin /* Host will merge rx buffers for big packets (shake it! shake it!) */ 1183f2c31d9SMark McLoughlin bool mergeable_rx_bufs; 1193f2c31d9SMark McLoughlin 120986a4f4dSJason Wang /* Has control virtqueue */ 121986a4f4dSJason Wang bool has_cvq; 122986a4f4dSJason Wang 123e7428e95SMichael S. Tsirkin /* Host can handle any s/g split between our header and packet data */ 124e7428e95SMichael S. Tsirkin bool any_header_sg; 125e7428e95SMichael S. Tsirkin 1263fa2a1dfSstephen hemminger /* Active statistics */ 1273fa2a1dfSstephen hemminger struct virtnet_stats __percpu *stats; 1283fa2a1dfSstephen hemminger 1293161e453SRusty Russell /* Work struct for refilling if we run low on memory. */ 1303161e453SRusty Russell struct delayed_work refill; 1313161e453SRusty Russell 132586d17c5SJason Wang /* Work struct for config space updates */ 133586d17c5SJason Wang struct work_struct config_work; 134586d17c5SJason Wang 135986a4f4dSJason Wang /* Does the affinity hint is set for virtqueues? */ 136986a4f4dSJason Wang bool affinity_hint_set; 13747be2479SWanlong Gao 1388de4b2f3SWanlong Gao /* CPU hot plug notifier */ 1398de4b2f3SWanlong Gao struct notifier_block nb; 140296f96fcSRusty Russell }; 141296f96fcSRusty Russell 142b3f24698SRusty Russell struct skb_vnet_hdr { 143b3f24698SRusty Russell union { 144b3f24698SRusty Russell struct virtio_net_hdr hdr; 145b3f24698SRusty Russell struct virtio_net_hdr_mrg_rxbuf mhdr; 146b3f24698SRusty Russell }; 147b3f24698SRusty Russell }; 148b3f24698SRusty Russell 1499ab86bbcSShirley Ma struct padded_vnet_hdr { 1509ab86bbcSShirley Ma struct virtio_net_hdr hdr; 1519ab86bbcSShirley Ma /* 1529ab86bbcSShirley Ma * virtio_net_hdr should be in a separated sg buffer because of a 1539ab86bbcSShirley Ma * QEMU bug, and data sg buffer shares same page with this header sg. 1549ab86bbcSShirley Ma * This padding makes next sg 16 byte aligned after virtio_net_hdr. 1559ab86bbcSShirley Ma */ 1569ab86bbcSShirley Ma char padding[6]; 1579ab86bbcSShirley Ma }; 1589ab86bbcSShirley Ma 159986a4f4dSJason Wang /* Converting between virtqueue no. and kernel tx/rx queue no. 160986a4f4dSJason Wang * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq 161986a4f4dSJason Wang */ 162986a4f4dSJason Wang static int vq2txq(struct virtqueue *vq) 163986a4f4dSJason Wang { 1649d0ca6edSRusty Russell return (vq->index - 1) / 2; 165986a4f4dSJason Wang } 166986a4f4dSJason Wang 167986a4f4dSJason Wang static int txq2vq(int txq) 168986a4f4dSJason Wang { 169986a4f4dSJason Wang return txq * 2 + 1; 170986a4f4dSJason Wang } 171986a4f4dSJason Wang 172986a4f4dSJason Wang static int vq2rxq(struct virtqueue *vq) 173986a4f4dSJason Wang { 1749d0ca6edSRusty Russell return vq->index / 2; 175986a4f4dSJason Wang } 176986a4f4dSJason Wang 177986a4f4dSJason Wang static int rxq2vq(int rxq) 178986a4f4dSJason Wang { 179986a4f4dSJason Wang return rxq * 2; 180986a4f4dSJason Wang } 181986a4f4dSJason Wang 182b3f24698SRusty Russell static inline struct skb_vnet_hdr *skb_vnet_hdr(struct sk_buff *skb) 183296f96fcSRusty Russell { 184b3f24698SRusty Russell return (struct skb_vnet_hdr *)skb->cb; 185296f96fcSRusty Russell } 186296f96fcSRusty Russell 1879ab86bbcSShirley Ma /* 1889ab86bbcSShirley Ma * private is used to chain pages for big packets, put the whole 1899ab86bbcSShirley Ma * most recent used list in the beginning for reuse 1909ab86bbcSShirley Ma */ 191e9d7417bSJason Wang static void give_pages(struct receive_queue *rq, struct page *page) 192fb6813f4SRusty Russell { 1939ab86bbcSShirley Ma struct page *end; 1949ab86bbcSShirley Ma 195e9d7417bSJason Wang /* Find end of list, sew whole thing into vi->rq.pages. */ 1969ab86bbcSShirley Ma for (end = page; end->private; end = (struct page *)end->private); 197e9d7417bSJason Wang end->private = (unsigned long)rq->pages; 198e9d7417bSJason Wang rq->pages = page; 199fb6813f4SRusty Russell } 200fb6813f4SRusty Russell 201e9d7417bSJason Wang static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask) 202fb6813f4SRusty Russell { 203e9d7417bSJason Wang struct page *p = rq->pages; 204fb6813f4SRusty Russell 2059ab86bbcSShirley Ma if (p) { 206e9d7417bSJason Wang rq->pages = (struct page *)p->private; 2079ab86bbcSShirley Ma /* clear private here, it is used to chain pages */ 2089ab86bbcSShirley Ma p->private = 0; 2099ab86bbcSShirley Ma } else 210fb6813f4SRusty Russell p = alloc_page(gfp_mask); 211fb6813f4SRusty Russell return p; 212fb6813f4SRusty Russell } 213fb6813f4SRusty Russell 214e9d7417bSJason Wang static void skb_xmit_done(struct virtqueue *vq) 215296f96fcSRusty Russell { 216e9d7417bSJason Wang struct virtnet_info *vi = vq->vdev->priv; 217296f96fcSRusty Russell 2182cb9c6baSRusty Russell /* Suppress further interrupts. */ 219e9d7417bSJason Wang virtqueue_disable_cb(vq); 22011a3a154SRusty Russell 221363f1514SRusty Russell /* We were probably waiting for more output buffers. */ 222986a4f4dSJason Wang netif_wake_subqueue(vi->dev, vq2txq(vq)); 223296f96fcSRusty Russell } 224296f96fcSRusty Russell 225ab7db917SMichael Dalton static unsigned int mergeable_ctx_to_buf_truesize(unsigned long mrg_ctx) 226ab7db917SMichael Dalton { 227ab7db917SMichael Dalton unsigned int truesize = mrg_ctx & (MERGEABLE_BUFFER_ALIGN - 1); 228ab7db917SMichael Dalton return (truesize + 1) * MERGEABLE_BUFFER_ALIGN; 229ab7db917SMichael Dalton } 230ab7db917SMichael Dalton 231ab7db917SMichael Dalton static void *mergeable_ctx_to_buf_address(unsigned long mrg_ctx) 232ab7db917SMichael Dalton { 233ab7db917SMichael Dalton return (void *)(mrg_ctx & -MERGEABLE_BUFFER_ALIGN); 234ab7db917SMichael Dalton 235ab7db917SMichael Dalton } 236ab7db917SMichael Dalton 237ab7db917SMichael Dalton static unsigned long mergeable_buf_to_ctx(void *buf, unsigned int truesize) 238ab7db917SMichael Dalton { 239ab7db917SMichael Dalton unsigned int size = truesize / MERGEABLE_BUFFER_ALIGN; 240ab7db917SMichael Dalton return (unsigned long)buf | (size - 1); 241ab7db917SMichael Dalton } 242ab7db917SMichael Dalton 2433464645aSMike Waychison /* Called from bottom half context */ 244e9d7417bSJason Wang static struct sk_buff *page_to_skb(struct receive_queue *rq, 2452613af0eSMichael Dalton struct page *page, unsigned int offset, 2462613af0eSMichael Dalton unsigned int len, unsigned int truesize) 2479ab86bbcSShirley Ma { 248e9d7417bSJason Wang struct virtnet_info *vi = rq->vq->vdev->priv; 2499ab86bbcSShirley Ma struct sk_buff *skb; 2509ab86bbcSShirley Ma struct skb_vnet_hdr *hdr; 2512613af0eSMichael Dalton unsigned int copy, hdr_len, hdr_padded_len; 2529ab86bbcSShirley Ma char *p; 2539ab86bbcSShirley Ma 2542613af0eSMichael Dalton p = page_address(page) + offset; 2559ab86bbcSShirley Ma 2569ab86bbcSShirley Ma /* copy small packet so we can reuse these pages for small data */ 2579ab86bbcSShirley Ma skb = netdev_alloc_skb_ip_align(vi->dev, GOOD_COPY_LEN); 2589ab86bbcSShirley Ma if (unlikely(!skb)) 2599ab86bbcSShirley Ma return NULL; 2609ab86bbcSShirley Ma 2619ab86bbcSShirley Ma hdr = skb_vnet_hdr(skb); 2629ab86bbcSShirley Ma 2633f2c31d9SMark McLoughlin if (vi->mergeable_rx_bufs) { 2649ab86bbcSShirley Ma hdr_len = sizeof hdr->mhdr; 2652613af0eSMichael Dalton hdr_padded_len = sizeof hdr->mhdr; 2669ab86bbcSShirley Ma } else { 2679ab86bbcSShirley Ma hdr_len = sizeof hdr->hdr; 2682613af0eSMichael Dalton hdr_padded_len = sizeof(struct padded_vnet_hdr); 2699ab86bbcSShirley Ma } 2703f2c31d9SMark McLoughlin 2719ab86bbcSShirley Ma memcpy(hdr, p, hdr_len); 2723f2c31d9SMark McLoughlin 2739ab86bbcSShirley Ma len -= hdr_len; 2742613af0eSMichael Dalton offset += hdr_padded_len; 2752613af0eSMichael Dalton p += hdr_padded_len; 2763f2c31d9SMark McLoughlin 2773f2c31d9SMark McLoughlin copy = len; 2783f2c31d9SMark McLoughlin if (copy > skb_tailroom(skb)) 2793f2c31d9SMark McLoughlin copy = skb_tailroom(skb); 2803f2c31d9SMark McLoughlin memcpy(skb_put(skb, copy), p, copy); 2813f2c31d9SMark McLoughlin 2823f2c31d9SMark McLoughlin len -= copy; 2839ab86bbcSShirley Ma offset += copy; 2843f2c31d9SMark McLoughlin 2852613af0eSMichael Dalton if (vi->mergeable_rx_bufs) { 2862613af0eSMichael Dalton if (len) 2872613af0eSMichael Dalton skb_add_rx_frag(skb, 0, page, offset, len, truesize); 2882613af0eSMichael Dalton else 2892613af0eSMichael Dalton put_page(page); 2902613af0eSMichael Dalton return skb; 2912613af0eSMichael Dalton } 2922613af0eSMichael Dalton 293e878d78bSSasha Levin /* 294e878d78bSSasha Levin * Verify that we can indeed put this data into a skb. 295e878d78bSSasha Levin * This is here to handle cases when the device erroneously 296e878d78bSSasha Levin * tries to receive more than is possible. This is usually 297e878d78bSSasha Levin * the case of a broken device. 298e878d78bSSasha Levin */ 299e878d78bSSasha Levin if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) { 300be443899SAmerigo Wang net_dbg_ratelimited("%s: too much data\n", skb->dev->name); 301e878d78bSSasha Levin dev_kfree_skb(skb); 302e878d78bSSasha Levin return NULL; 303e878d78bSSasha Levin } 3042613af0eSMichael Dalton BUG_ON(offset >= PAGE_SIZE); 3059ab86bbcSShirley Ma while (len) { 3062613af0eSMichael Dalton unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len); 3072613af0eSMichael Dalton skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset, 3082613af0eSMichael Dalton frag_size, truesize); 3092613af0eSMichael Dalton len -= frag_size; 3109ab86bbcSShirley Ma page = (struct page *)page->private; 3119ab86bbcSShirley Ma offset = 0; 3123f2c31d9SMark McLoughlin } 3133f2c31d9SMark McLoughlin 3149ab86bbcSShirley Ma if (page) 315e9d7417bSJason Wang give_pages(rq, page); 3163f2c31d9SMark McLoughlin 3179ab86bbcSShirley Ma return skb; 3189ab86bbcSShirley Ma } 3199ab86bbcSShirley Ma 320f121159dSMichael S. Tsirkin static struct sk_buff *receive_small(void *buf, unsigned int len) 321f121159dSMichael S. Tsirkin { 322f121159dSMichael S. Tsirkin struct sk_buff * skb = buf; 323f121159dSMichael S. Tsirkin 324f121159dSMichael S. Tsirkin len -= sizeof(struct virtio_net_hdr); 325f121159dSMichael S. Tsirkin skb_trim(skb, len); 326f121159dSMichael S. Tsirkin 327f121159dSMichael S. Tsirkin return skb; 328f121159dSMichael S. Tsirkin } 329f121159dSMichael S. Tsirkin 330f121159dSMichael S. Tsirkin static struct sk_buff *receive_big(struct net_device *dev, 331f121159dSMichael S. Tsirkin struct receive_queue *rq, 332f121159dSMichael S. Tsirkin void *buf, 333f121159dSMichael S. Tsirkin unsigned int len) 334f121159dSMichael S. Tsirkin { 335f121159dSMichael S. Tsirkin struct page *page = buf; 336f121159dSMichael S. Tsirkin struct sk_buff *skb = page_to_skb(rq, page, 0, len, PAGE_SIZE); 337f121159dSMichael S. Tsirkin 338f121159dSMichael S. Tsirkin if (unlikely(!skb)) 339f121159dSMichael S. Tsirkin goto err; 340f121159dSMichael S. Tsirkin 341f121159dSMichael S. Tsirkin return skb; 342f121159dSMichael S. Tsirkin 343f121159dSMichael S. Tsirkin err: 344f121159dSMichael S. Tsirkin dev->stats.rx_dropped++; 345f121159dSMichael S. Tsirkin give_pages(rq, page); 346f121159dSMichael S. Tsirkin return NULL; 347f121159dSMichael S. Tsirkin } 348f121159dSMichael S. Tsirkin 3498fc3b9e9SMichael S. Tsirkin static struct sk_buff *receive_mergeable(struct net_device *dev, 3508fc3b9e9SMichael S. Tsirkin struct receive_queue *rq, 351ab7db917SMichael Dalton unsigned long ctx, 3528fc3b9e9SMichael S. Tsirkin unsigned int len) 3539ab86bbcSShirley Ma { 354ab7db917SMichael Dalton void *buf = mergeable_ctx_to_buf_address(ctx); 3558fc3b9e9SMichael S. Tsirkin struct skb_vnet_hdr *hdr = buf; 3568fc3b9e9SMichael S. Tsirkin int num_buf = hdr->mhdr.num_buffers; 3578fc3b9e9SMichael S. Tsirkin struct page *page = virt_to_head_page(buf); 3588fc3b9e9SMichael S. Tsirkin int offset = buf - page_address(page); 359ab7db917SMichael Dalton unsigned int truesize = max(len, mergeable_ctx_to_buf_truesize(ctx)); 360ab7db917SMichael Dalton 361fb51879dSMichael Dalton struct sk_buff *head_skb = page_to_skb(rq, page, offset, len, truesize); 3622613af0eSMichael Dalton struct sk_buff *curr_skb = head_skb; 3639ab86bbcSShirley Ma 3648fc3b9e9SMichael S. Tsirkin if (unlikely(!curr_skb)) 3658fc3b9e9SMichael S. Tsirkin goto err_skb; 3669ab86bbcSShirley Ma while (--num_buf) { 3678fc3b9e9SMichael S. Tsirkin int num_skb_frags; 3688fc3b9e9SMichael S. Tsirkin 369ab7db917SMichael Dalton ctx = (unsigned long)virtqueue_get_buf(rq->vq, &len); 370ab7db917SMichael Dalton if (unlikely(!ctx)) { 3718fc3b9e9SMichael S. Tsirkin pr_debug("%s: rx error: %d buffers out of %d missing\n", 3728fc3b9e9SMichael S. Tsirkin dev->name, num_buf, hdr->mhdr.num_buffers); 3738fc3b9e9SMichael S. Tsirkin dev->stats.rx_length_errors++; 3748fc3b9e9SMichael S. Tsirkin goto err_buf; 3753f2c31d9SMark McLoughlin } 3768fc3b9e9SMichael S. Tsirkin 377ab7db917SMichael Dalton buf = mergeable_ctx_to_buf_address(ctx); 3788fc3b9e9SMichael S. Tsirkin page = virt_to_head_page(buf); 3798fc3b9e9SMichael S. Tsirkin 3808fc3b9e9SMichael S. Tsirkin num_skb_frags = skb_shinfo(curr_skb)->nr_frags; 3812613af0eSMichael Dalton if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) { 3822613af0eSMichael Dalton struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC); 3838fc3b9e9SMichael S. Tsirkin 3848fc3b9e9SMichael S. Tsirkin if (unlikely(!nskb)) 3858fc3b9e9SMichael S. Tsirkin goto err_skb; 3862613af0eSMichael Dalton if (curr_skb == head_skb) 3872613af0eSMichael Dalton skb_shinfo(curr_skb)->frag_list = nskb; 3882613af0eSMichael Dalton else 3892613af0eSMichael Dalton curr_skb->next = nskb; 3902613af0eSMichael Dalton curr_skb = nskb; 3912613af0eSMichael Dalton head_skb->truesize += nskb->truesize; 3922613af0eSMichael Dalton num_skb_frags = 0; 3932613af0eSMichael Dalton } 394ab7db917SMichael Dalton truesize = max(len, mergeable_ctx_to_buf_truesize(ctx)); 3952613af0eSMichael Dalton if (curr_skb != head_skb) { 3962613af0eSMichael Dalton head_skb->data_len += len; 3972613af0eSMichael Dalton head_skb->len += len; 398fb51879dSMichael Dalton head_skb->truesize += truesize; 3992613af0eSMichael Dalton } 4008fc3b9e9SMichael S. Tsirkin offset = buf - page_address(page); 401ba275241SJason Wang if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) { 402ba275241SJason Wang put_page(page); 403ba275241SJason Wang skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1, 404fb51879dSMichael Dalton len, truesize); 405ba275241SJason Wang } else { 4062613af0eSMichael Dalton skb_add_rx_frag(curr_skb, num_skb_frags, page, 407fb51879dSMichael Dalton offset, len, truesize); 408ba275241SJason Wang } 4098fc3b9e9SMichael S. Tsirkin } 4108fc3b9e9SMichael S. Tsirkin 411ab7db917SMichael Dalton ewma_add(&rq->mrg_avg_pkt_len, head_skb->len); 4128fc3b9e9SMichael S. Tsirkin return head_skb; 4138fc3b9e9SMichael S. Tsirkin 4148fc3b9e9SMichael S. Tsirkin err_skb: 4158fc3b9e9SMichael S. Tsirkin put_page(page); 4168fc3b9e9SMichael S. Tsirkin while (--num_buf) { 417ab7db917SMichael Dalton ctx = (unsigned long)virtqueue_get_buf(rq->vq, &len); 418ab7db917SMichael Dalton if (unlikely(!ctx)) { 4198fc3b9e9SMichael S. Tsirkin pr_debug("%s: rx error: %d buffers missing\n", 4208fc3b9e9SMichael S. Tsirkin dev->name, num_buf); 4218fc3b9e9SMichael S. Tsirkin dev->stats.rx_length_errors++; 4228fc3b9e9SMichael S. Tsirkin break; 4238fc3b9e9SMichael S. Tsirkin } 424ab7db917SMichael Dalton page = virt_to_head_page(mergeable_ctx_to_buf_address(ctx)); 4258fc3b9e9SMichael S. Tsirkin put_page(page); 4263f2c31d9SMark McLoughlin } 4278fc3b9e9SMichael S. Tsirkin err_buf: 4288fc3b9e9SMichael S. Tsirkin dev->stats.rx_dropped++; 4298fc3b9e9SMichael S. Tsirkin dev_kfree_skb(head_skb); 4308fc3b9e9SMichael S. Tsirkin return NULL; 4319ab86bbcSShirley Ma } 4329ab86bbcSShirley Ma 433e9d7417bSJason Wang static void receive_buf(struct receive_queue *rq, void *buf, unsigned int len) 4349ab86bbcSShirley Ma { 435e9d7417bSJason Wang struct virtnet_info *vi = rq->vq->vdev->priv; 436e9d7417bSJason Wang struct net_device *dev = vi->dev; 43758472a76SEric Dumazet struct virtnet_stats *stats = this_cpu_ptr(vi->stats); 4389ab86bbcSShirley Ma struct sk_buff *skb; 4399ab86bbcSShirley Ma struct skb_vnet_hdr *hdr; 4409ab86bbcSShirley Ma 4419ab86bbcSShirley Ma if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) { 4429ab86bbcSShirley Ma pr_debug("%s: short packet %i\n", dev->name, len); 4439ab86bbcSShirley Ma dev->stats.rx_length_errors++; 444ab7db917SMichael Dalton if (vi->mergeable_rx_bufs) { 445ab7db917SMichael Dalton unsigned long ctx = (unsigned long)buf; 446ab7db917SMichael Dalton void *base = mergeable_ctx_to_buf_address(ctx); 447ab7db917SMichael Dalton put_page(virt_to_head_page(base)); 448ab7db917SMichael Dalton } else if (vi->big_packets) { 44998bfd23cSMichael Dalton give_pages(rq, buf); 450ab7db917SMichael Dalton } else { 4519ab86bbcSShirley Ma dev_kfree_skb(buf); 452ab7db917SMichael Dalton } 4539ab86bbcSShirley Ma return; 4549ab86bbcSShirley Ma } 4559ab86bbcSShirley Ma 456f121159dSMichael S. Tsirkin if (vi->mergeable_rx_bufs) 457ab7db917SMichael Dalton skb = receive_mergeable(dev, rq, (unsigned long)buf, len); 458f121159dSMichael S. Tsirkin else if (vi->big_packets) 459f121159dSMichael S. Tsirkin skb = receive_big(dev, rq, buf, len); 460f121159dSMichael S. Tsirkin else 461f121159dSMichael S. Tsirkin skb = receive_small(buf, len); 462f121159dSMichael S. Tsirkin 4638fc3b9e9SMichael S. Tsirkin if (unlikely(!skb)) 4642613af0eSMichael Dalton return; 4653f2c31d9SMark McLoughlin 4669ab86bbcSShirley Ma hdr = skb_vnet_hdr(skb); 4673fa2a1dfSstephen hemminger 46883a27052SEric Dumazet u64_stats_update_begin(&stats->rx_syncp); 4693fa2a1dfSstephen hemminger stats->rx_bytes += skb->len; 4703fa2a1dfSstephen hemminger stats->rx_packets++; 47183a27052SEric Dumazet u64_stats_update_end(&stats->rx_syncp); 472296f96fcSRusty Russell 473b3f24698SRusty Russell if (hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) { 474296f96fcSRusty Russell pr_debug("Needs csum!\n"); 475b3f24698SRusty Russell if (!skb_partial_csum_set(skb, 476b3f24698SRusty Russell hdr->hdr.csum_start, 477b3f24698SRusty Russell hdr->hdr.csum_offset)) 478296f96fcSRusty Russell goto frame_err; 47910a8d94aSJason Wang } else if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID) { 48010a8d94aSJason Wang skb->ip_summed = CHECKSUM_UNNECESSARY; 481296f96fcSRusty Russell } 482296f96fcSRusty Russell 48323cde76dSMark McLoughlin skb->protocol = eth_type_trans(skb, dev); 48423cde76dSMark McLoughlin pr_debug("Receiving skb proto 0x%04x len %i type %i\n", 48523cde76dSMark McLoughlin ntohs(skb->protocol), skb->len, skb->pkt_type); 48623cde76dSMark McLoughlin 487b3f24698SRusty Russell if (hdr->hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) { 488296f96fcSRusty Russell pr_debug("GSO!\n"); 489b3f24698SRusty Russell switch (hdr->hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) { 490296f96fcSRusty Russell case VIRTIO_NET_HDR_GSO_TCPV4: 491c9af6db4SPravin B Shelar skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4; 492296f96fcSRusty Russell break; 493296f96fcSRusty Russell case VIRTIO_NET_HDR_GSO_UDP: 494c9af6db4SPravin B Shelar skb_shinfo(skb)->gso_type = SKB_GSO_UDP; 495296f96fcSRusty Russell break; 496296f96fcSRusty Russell case VIRTIO_NET_HDR_GSO_TCPV6: 497c9af6db4SPravin B Shelar skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6; 498296f96fcSRusty Russell break; 499296f96fcSRusty Russell default: 500be443899SAmerigo Wang net_warn_ratelimited("%s: bad gso type %u.\n", 501b3f24698SRusty Russell dev->name, hdr->hdr.gso_type); 502296f96fcSRusty Russell goto frame_err; 503296f96fcSRusty Russell } 504296f96fcSRusty Russell 505b3f24698SRusty Russell if (hdr->hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN) 506c9af6db4SPravin B Shelar skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN; 50734a48579SRusty Russell 508b3f24698SRusty Russell skb_shinfo(skb)->gso_size = hdr->hdr.gso_size; 509296f96fcSRusty Russell if (skb_shinfo(skb)->gso_size == 0) { 510be443899SAmerigo Wang net_warn_ratelimited("%s: zero gso size.\n", dev->name); 511296f96fcSRusty Russell goto frame_err; 512296f96fcSRusty Russell } 513296f96fcSRusty Russell 514296f96fcSRusty Russell /* Header must be checked, and gso_segs computed. */ 515296f96fcSRusty Russell skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY; 516296f96fcSRusty Russell skb_shinfo(skb)->gso_segs = 0; 517296f96fcSRusty Russell } 518296f96fcSRusty Russell 51991815639SJason Wang skb_mark_napi_id(skb, &rq->napi); 52091815639SJason Wang 521296f96fcSRusty Russell netif_receive_skb(skb); 522296f96fcSRusty Russell return; 523296f96fcSRusty Russell 524296f96fcSRusty Russell frame_err: 525296f96fcSRusty Russell dev->stats.rx_frame_errors++; 526296f96fcSRusty Russell dev_kfree_skb(skb); 527296f96fcSRusty Russell } 528296f96fcSRusty Russell 529e9d7417bSJason Wang static int add_recvbuf_small(struct receive_queue *rq, gfp_t gfp) 530296f96fcSRusty Russell { 531e9d7417bSJason Wang struct virtnet_info *vi = rq->vq->vdev->priv; 532296f96fcSRusty Russell struct sk_buff *skb; 533b3f24698SRusty Russell struct skb_vnet_hdr *hdr; 5349ab86bbcSShirley Ma int err; 5353f2c31d9SMark McLoughlin 5365061de36SMichael Dalton skb = __netdev_alloc_skb_ip_align(vi->dev, GOOD_PACKET_LEN, gfp); 5379ab86bbcSShirley Ma if (unlikely(!skb)) 5389ab86bbcSShirley Ma return -ENOMEM; 539296f96fcSRusty Russell 5405061de36SMichael Dalton skb_put(skb, GOOD_PACKET_LEN); 5413f2c31d9SMark McLoughlin 5423f2c31d9SMark McLoughlin hdr = skb_vnet_hdr(skb); 543e9d7417bSJason Wang sg_set_buf(rq->sg, &hdr->hdr, sizeof hdr->hdr); 54497402b96SHerbert Xu 545e9d7417bSJason Wang skb_to_sgvec(skb, rq->sg + 1, 0, skb->len); 54697402b96SHerbert Xu 5479dc7b9e4SRusty Russell err = virtqueue_add_inbuf(rq->vq, rq->sg, 2, skb, gfp); 5489ab86bbcSShirley Ma if (err < 0) 5499ab86bbcSShirley Ma dev_kfree_skb(skb); 55097402b96SHerbert Xu 5519ab86bbcSShirley Ma return err; 55297402b96SHerbert Xu } 55397402b96SHerbert Xu 554e9d7417bSJason Wang static int add_recvbuf_big(struct receive_queue *rq, gfp_t gfp) 5559ab86bbcSShirley Ma { 5569ab86bbcSShirley Ma struct page *first, *list = NULL; 5579ab86bbcSShirley Ma char *p; 5589ab86bbcSShirley Ma int i, err, offset; 559296f96fcSRusty Russell 560e9d7417bSJason Wang /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */ 5619ab86bbcSShirley Ma for (i = MAX_SKB_FRAGS + 1; i > 1; --i) { 562e9d7417bSJason Wang first = get_a_page(rq, gfp); 5639ab86bbcSShirley Ma if (!first) { 5649ab86bbcSShirley Ma if (list) 565e9d7417bSJason Wang give_pages(rq, list); 5669ab86bbcSShirley Ma return -ENOMEM; 567296f96fcSRusty Russell } 568e9d7417bSJason Wang sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE); 5699ab86bbcSShirley Ma 5709ab86bbcSShirley Ma /* chain new page in list head to match sg */ 5719ab86bbcSShirley Ma first->private = (unsigned long)list; 5729ab86bbcSShirley Ma list = first; 5739ab86bbcSShirley Ma } 5749ab86bbcSShirley Ma 575e9d7417bSJason Wang first = get_a_page(rq, gfp); 5769ab86bbcSShirley Ma if (!first) { 577e9d7417bSJason Wang give_pages(rq, list); 5789ab86bbcSShirley Ma return -ENOMEM; 5799ab86bbcSShirley Ma } 5809ab86bbcSShirley Ma p = page_address(first); 5819ab86bbcSShirley Ma 582e9d7417bSJason Wang /* rq->sg[0], rq->sg[1] share the same page */ 583e9d7417bSJason Wang /* a separated rq->sg[0] for virtio_net_hdr only due to QEMU bug */ 584e9d7417bSJason Wang sg_set_buf(&rq->sg[0], p, sizeof(struct virtio_net_hdr)); 5859ab86bbcSShirley Ma 586e9d7417bSJason Wang /* rq->sg[1] for data packet, from offset */ 5879ab86bbcSShirley Ma offset = sizeof(struct padded_vnet_hdr); 588e9d7417bSJason Wang sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset); 5899ab86bbcSShirley Ma 5909ab86bbcSShirley Ma /* chain first in list head */ 5919ab86bbcSShirley Ma first->private = (unsigned long)list; 5929dc7b9e4SRusty Russell err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2, 593aa989f5eSMichael S. Tsirkin first, gfp); 5949ab86bbcSShirley Ma if (err < 0) 595e9d7417bSJason Wang give_pages(rq, first); 5969ab86bbcSShirley Ma 5979ab86bbcSShirley Ma return err; 5989ab86bbcSShirley Ma } 5999ab86bbcSShirley Ma 600fbf28d78SMichael Dalton static unsigned int get_mergeable_buf_len(struct ewma *avg_pkt_len) 6019ab86bbcSShirley Ma { 602ab7db917SMichael Dalton const size_t hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf); 603fbf28d78SMichael Dalton unsigned int len; 604fbf28d78SMichael Dalton 605fbf28d78SMichael Dalton len = hdr_len + clamp_t(unsigned int, ewma_read(avg_pkt_len), 606fbf28d78SMichael Dalton GOOD_PACKET_LEN, PAGE_SIZE - hdr_len); 607fbf28d78SMichael Dalton return ALIGN(len, MERGEABLE_BUFFER_ALIGN); 608fbf28d78SMichael Dalton } 609fbf28d78SMichael Dalton 610fbf28d78SMichael Dalton static int add_recvbuf_mergeable(struct receive_queue *rq, gfp_t gfp) 611fbf28d78SMichael Dalton { 612fb51879dSMichael Dalton struct page_frag *alloc_frag = &rq->alloc_frag; 613fb51879dSMichael Dalton char *buf; 614ab7db917SMichael Dalton unsigned long ctx; 6159ab86bbcSShirley Ma int err; 616fb51879dSMichael Dalton unsigned int len, hole; 6179ab86bbcSShirley Ma 618fbf28d78SMichael Dalton len = get_mergeable_buf_len(&rq->mrg_avg_pkt_len); 619ab7db917SMichael Dalton if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp))) 6209ab86bbcSShirley Ma return -ENOMEM; 621ab7db917SMichael Dalton 622fb51879dSMichael Dalton buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset; 623ab7db917SMichael Dalton ctx = mergeable_buf_to_ctx(buf, len); 624fb51879dSMichael Dalton get_page(alloc_frag->page); 625fb51879dSMichael Dalton alloc_frag->offset += len; 626fb51879dSMichael Dalton hole = alloc_frag->size - alloc_frag->offset; 627ab7db917SMichael Dalton if (hole < len) { 628ab7db917SMichael Dalton /* To avoid internal fragmentation, if there is very likely not 629ab7db917SMichael Dalton * enough space for another buffer, add the remaining space to 630ab7db917SMichael Dalton * the current buffer. This extra space is not included in 631ab7db917SMichael Dalton * the truesize stored in ctx. 632ab7db917SMichael Dalton */ 633fb51879dSMichael Dalton len += hole; 634fb51879dSMichael Dalton alloc_frag->offset += hole; 635fb51879dSMichael Dalton } 6369ab86bbcSShirley Ma 637fb51879dSMichael Dalton sg_init_one(rq->sg, buf, len); 638ab7db917SMichael Dalton err = virtqueue_add_inbuf(rq->vq, rq->sg, 1, (void *)ctx, gfp); 6399ab86bbcSShirley Ma if (err < 0) 6402613af0eSMichael Dalton put_page(virt_to_head_page(buf)); 6419ab86bbcSShirley Ma 6429ab86bbcSShirley Ma return err; 643296f96fcSRusty Russell } 644296f96fcSRusty Russell 645b2baed69SRusty Russell /* 646b2baed69SRusty Russell * Returns false if we couldn't fill entirely (OOM). 647b2baed69SRusty Russell * 648b2baed69SRusty Russell * Normally run in the receive path, but can also be run from ndo_open 649b2baed69SRusty Russell * before we're receiving packets, or from refill_work which is 650b2baed69SRusty Russell * careful to disable receiving (using napi_disable). 651b2baed69SRusty Russell */ 652e9d7417bSJason Wang static bool try_fill_recv(struct receive_queue *rq, gfp_t gfp) 6533f2c31d9SMark McLoughlin { 654e9d7417bSJason Wang struct virtnet_info *vi = rq->vq->vdev->priv; 6553f2c31d9SMark McLoughlin int err; 6561788f495SMichael S. Tsirkin bool oom; 6573f2c31d9SMark McLoughlin 658fb51879dSMichael Dalton gfp |= __GFP_COLD; 6590aea51c3SAmit Shah do { 6609ab86bbcSShirley Ma if (vi->mergeable_rx_bufs) 661e9d7417bSJason Wang err = add_recvbuf_mergeable(rq, gfp); 6629ab86bbcSShirley Ma else if (vi->big_packets) 663e9d7417bSJason Wang err = add_recvbuf_big(rq, gfp); 6649ab86bbcSShirley Ma else 665e9d7417bSJason Wang err = add_recvbuf_small(rq, gfp); 6663f2c31d9SMark McLoughlin 6671788f495SMichael S. Tsirkin oom = err == -ENOMEM; 6689ed4cb07SRusty Russell if (err) 6693f2c31d9SMark McLoughlin break; 670b7dfde95SLinus Torvalds } while (rq->vq->num_free); 671681daee2SJason Wang virtqueue_kick(rq->vq); 6723161e453SRusty Russell return !oom; 6733f2c31d9SMark McLoughlin } 6743f2c31d9SMark McLoughlin 67518445c4dSRusty Russell static void skb_recv_done(struct virtqueue *rvq) 676296f96fcSRusty Russell { 677296f96fcSRusty Russell struct virtnet_info *vi = rvq->vdev->priv; 678986a4f4dSJason Wang struct receive_queue *rq = &vi->rq[vq2rxq(rvq)]; 679e9d7417bSJason Wang 68018445c4dSRusty Russell /* Schedule NAPI, Suppress further interrupts if successful. */ 681e9d7417bSJason Wang if (napi_schedule_prep(&rq->napi)) { 6821915a712SMichael S. Tsirkin virtqueue_disable_cb(rvq); 683e9d7417bSJason Wang __napi_schedule(&rq->napi); 68418445c4dSRusty Russell } 685296f96fcSRusty Russell } 686296f96fcSRusty Russell 687e9d7417bSJason Wang static void virtnet_napi_enable(struct receive_queue *rq) 6883e9d08ecSBruce Rogers { 689e9d7417bSJason Wang napi_enable(&rq->napi); 6903e9d08ecSBruce Rogers 6913e9d08ecSBruce Rogers /* If all buffers were filled by other side before we napi_enabled, we 6923e9d08ecSBruce Rogers * won't get another interrupt, so process any outstanding packets 6933e9d08ecSBruce Rogers * now. virtnet_poll wants re-enable the queue, so we disable here. 6943e9d08ecSBruce Rogers * We synchronize against interrupts via NAPI_STATE_SCHED */ 695e9d7417bSJason Wang if (napi_schedule_prep(&rq->napi)) { 696e9d7417bSJason Wang virtqueue_disable_cb(rq->vq); 697ec13ee80SMichael S. Tsirkin local_bh_disable(); 698e9d7417bSJason Wang __napi_schedule(&rq->napi); 699ec13ee80SMichael S. Tsirkin local_bh_enable(); 7003e9d08ecSBruce Rogers } 7013e9d08ecSBruce Rogers } 7023e9d08ecSBruce Rogers 7033161e453SRusty Russell static void refill_work(struct work_struct *work) 7043161e453SRusty Russell { 705e9d7417bSJason Wang struct virtnet_info *vi = 706e9d7417bSJason Wang container_of(work, struct virtnet_info, refill.work); 7073161e453SRusty Russell bool still_empty; 708986a4f4dSJason Wang int i; 7093161e453SRusty Russell 71055257d72SSasha Levin for (i = 0; i < vi->curr_queue_pairs; i++) { 711986a4f4dSJason Wang struct receive_queue *rq = &vi->rq[i]; 712986a4f4dSJason Wang 713986a4f4dSJason Wang napi_disable(&rq->napi); 714986a4f4dSJason Wang still_empty = !try_fill_recv(rq, GFP_KERNEL); 715986a4f4dSJason Wang virtnet_napi_enable(rq); 7163161e453SRusty Russell 7173161e453SRusty Russell /* In theory, this can happen: if we don't get any buffers in 718986a4f4dSJason Wang * we will *never* try to fill again. 719986a4f4dSJason Wang */ 7203161e453SRusty Russell if (still_empty) 7213b07e9caSTejun Heo schedule_delayed_work(&vi->refill, HZ/2); 7223161e453SRusty Russell } 723986a4f4dSJason Wang } 7243161e453SRusty Russell 7252ffa7598SJason Wang static int virtnet_receive(struct receive_queue *rq, int budget) 726296f96fcSRusty Russell { 727e9d7417bSJason Wang struct virtnet_info *vi = rq->vq->vdev->priv; 7282ffa7598SJason Wang unsigned int len, received = 0; 7299ab86bbcSShirley Ma void *buf; 730296f96fcSRusty Russell 731296f96fcSRusty Russell while (received < budget && 732e9d7417bSJason Wang (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) { 733e9d7417bSJason Wang receive_buf(rq, buf, len); 734296f96fcSRusty Russell received++; 735296f96fcSRusty Russell } 736296f96fcSRusty Russell 737be121f46SJason Wang if (rq->vq->num_free > virtqueue_get_vring_size(rq->vq) / 2) { 738e9d7417bSJason Wang if (!try_fill_recv(rq, GFP_ATOMIC)) 7393b07e9caSTejun Heo schedule_delayed_work(&vi->refill, 0); 7403161e453SRusty Russell } 741296f96fcSRusty Russell 7422ffa7598SJason Wang return received; 7432ffa7598SJason Wang } 7442ffa7598SJason Wang 7452ffa7598SJason Wang static int virtnet_poll(struct napi_struct *napi, int budget) 7462ffa7598SJason Wang { 7472ffa7598SJason Wang struct receive_queue *rq = 7482ffa7598SJason Wang container_of(napi, struct receive_queue, napi); 7492ffa7598SJason Wang unsigned int r, received = 0; 7502ffa7598SJason Wang 7512ffa7598SJason Wang again: 7522ffa7598SJason Wang received += virtnet_receive(rq, budget - received); 7532ffa7598SJason Wang 7548329d98eSRusty Russell /* Out of packets? */ 7558329d98eSRusty Russell if (received < budget) { 756cbdadbbfSMichael S. Tsirkin r = virtqueue_enable_cb_prepare(rq->vq); 757288379f0SBen Hutchings napi_complete(napi); 758cbdadbbfSMichael S. Tsirkin if (unlikely(virtqueue_poll(rq->vq, r)) && 7598e95a202SJoe Perches napi_schedule_prep(napi)) { 760e9d7417bSJason Wang virtqueue_disable_cb(rq->vq); 761288379f0SBen Hutchings __napi_schedule(napi); 762296f96fcSRusty Russell goto again; 763296f96fcSRusty Russell } 7644265f161SChristian Borntraeger } 765296f96fcSRusty Russell 766296f96fcSRusty Russell return received; 767296f96fcSRusty Russell } 768296f96fcSRusty Russell 76991815639SJason Wang #ifdef CONFIG_NET_RX_BUSY_POLL 77091815639SJason Wang /* must be called with local_bh_disable()d */ 77191815639SJason Wang static int virtnet_busy_poll(struct napi_struct *napi) 77291815639SJason Wang { 77391815639SJason Wang struct receive_queue *rq = 77491815639SJason Wang container_of(napi, struct receive_queue, napi); 77591815639SJason Wang struct virtnet_info *vi = rq->vq->vdev->priv; 77691815639SJason Wang int r, received = 0, budget = 4; 77791815639SJason Wang 77891815639SJason Wang if (!(vi->status & VIRTIO_NET_S_LINK_UP)) 77991815639SJason Wang return LL_FLUSH_FAILED; 78091815639SJason Wang 78191815639SJason Wang if (!napi_schedule_prep(napi)) 78291815639SJason Wang return LL_FLUSH_BUSY; 78391815639SJason Wang 78491815639SJason Wang virtqueue_disable_cb(rq->vq); 78591815639SJason Wang 78691815639SJason Wang again: 78791815639SJason Wang received += virtnet_receive(rq, budget); 78891815639SJason Wang 78991815639SJason Wang r = virtqueue_enable_cb_prepare(rq->vq); 79091815639SJason Wang clear_bit(NAPI_STATE_SCHED, &napi->state); 79191815639SJason Wang if (unlikely(virtqueue_poll(rq->vq, r)) && 79291815639SJason Wang napi_schedule_prep(napi)) { 79391815639SJason Wang virtqueue_disable_cb(rq->vq); 79491815639SJason Wang if (received < budget) { 79591815639SJason Wang budget -= received; 79691815639SJason Wang goto again; 79791815639SJason Wang } else { 79891815639SJason Wang __napi_schedule(napi); 79991815639SJason Wang } 80091815639SJason Wang } 80191815639SJason Wang 80291815639SJason Wang return received; 80391815639SJason Wang } 80491815639SJason Wang #endif /* CONFIG_NET_RX_BUSY_POLL */ 80591815639SJason Wang 806986a4f4dSJason Wang static int virtnet_open(struct net_device *dev) 807986a4f4dSJason Wang { 808986a4f4dSJason Wang struct virtnet_info *vi = netdev_priv(dev); 809986a4f4dSJason Wang int i; 810986a4f4dSJason Wang 811e4166625SJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 812e4166625SJason Wang if (i < vi->curr_queue_pairs) 813986a4f4dSJason Wang /* Make sure we have some buffers: if oom use wq. */ 814986a4f4dSJason Wang if (!try_fill_recv(&vi->rq[i], GFP_KERNEL)) 815986a4f4dSJason Wang schedule_delayed_work(&vi->refill, 0); 816986a4f4dSJason Wang virtnet_napi_enable(&vi->rq[i]); 817986a4f4dSJason Wang } 818986a4f4dSJason Wang 819986a4f4dSJason Wang return 0; 820986a4f4dSJason Wang } 821986a4f4dSJason Wang 822b7dfde95SLinus Torvalds static void free_old_xmit_skbs(struct send_queue *sq) 823296f96fcSRusty Russell { 824296f96fcSRusty Russell struct sk_buff *skb; 8256ee57bccSMichael S. Tsirkin unsigned int len; 826e9d7417bSJason Wang struct virtnet_info *vi = sq->vq->vdev->priv; 82758472a76SEric Dumazet struct virtnet_stats *stats = this_cpu_ptr(vi->stats); 828296f96fcSRusty Russell 829e9d7417bSJason Wang while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) { 830296f96fcSRusty Russell pr_debug("Sent skb %p\n", skb); 8313fa2a1dfSstephen hemminger 83283a27052SEric Dumazet u64_stats_update_begin(&stats->tx_syncp); 8333fa2a1dfSstephen hemminger stats->tx_bytes += skb->len; 8343fa2a1dfSstephen hemminger stats->tx_packets++; 83583a27052SEric Dumazet u64_stats_update_end(&stats->tx_syncp); 8363fa2a1dfSstephen hemminger 837ed79bab8SEric Dumazet dev_kfree_skb_any(skb); 838296f96fcSRusty Russell } 839296f96fcSRusty Russell } 840296f96fcSRusty Russell 841e9d7417bSJason Wang static int xmit_skb(struct send_queue *sq, struct sk_buff *skb) 842296f96fcSRusty Russell { 843e7428e95SMichael S. Tsirkin struct skb_vnet_hdr *hdr; 844296f96fcSRusty Russell const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest; 845e9d7417bSJason Wang struct virtnet_info *vi = sq->vq->vdev->priv; 8467bedc7dcSMichael S. Tsirkin unsigned num_sg; 847e7428e95SMichael S. Tsirkin unsigned hdr_len; 848e7428e95SMichael S. Tsirkin bool can_push; 849296f96fcSRusty Russell 850e174961cSJohannes Berg pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest); 851e7428e95SMichael S. Tsirkin if (vi->mergeable_rx_bufs) 852e7428e95SMichael S. Tsirkin hdr_len = sizeof hdr->mhdr; 853e7428e95SMichael S. Tsirkin else 854e7428e95SMichael S. Tsirkin hdr_len = sizeof hdr->hdr; 855e7428e95SMichael S. Tsirkin 856e7428e95SMichael S. Tsirkin can_push = vi->any_header_sg && 857e7428e95SMichael S. Tsirkin !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) && 858e7428e95SMichael S. Tsirkin !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len; 859e7428e95SMichael S. Tsirkin /* Even if we can, don't push here yet as this would skew 860e7428e95SMichael S. Tsirkin * csum_start offset below. */ 861e7428e95SMichael S. Tsirkin if (can_push) 862e7428e95SMichael S. Tsirkin hdr = (struct skb_vnet_hdr *)(skb->data - hdr_len); 863e7428e95SMichael S. Tsirkin else 864e7428e95SMichael S. Tsirkin hdr = skb_vnet_hdr(skb); 865296f96fcSRusty Russell 866296f96fcSRusty Russell if (skb->ip_summed == CHECKSUM_PARTIAL) { 867b3f24698SRusty Russell hdr->hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM; 86855508d60SMichał Mirosław hdr->hdr.csum_start = skb_checksum_start_offset(skb); 869b3f24698SRusty Russell hdr->hdr.csum_offset = skb->csum_offset; 870296f96fcSRusty Russell } else { 871b3f24698SRusty Russell hdr->hdr.flags = 0; 872b3f24698SRusty Russell hdr->hdr.csum_offset = hdr->hdr.csum_start = 0; 873296f96fcSRusty Russell } 874296f96fcSRusty Russell 875296f96fcSRusty Russell if (skb_is_gso(skb)) { 876b3f24698SRusty Russell hdr->hdr.hdr_len = skb_headlen(skb); 877b3f24698SRusty Russell hdr->hdr.gso_size = skb_shinfo(skb)->gso_size; 87834a48579SRusty Russell if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) 879b3f24698SRusty Russell hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4; 880296f96fcSRusty Russell else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) 881b3f24698SRusty Russell hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6; 882296f96fcSRusty Russell else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP) 883b3f24698SRusty Russell hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP; 884296f96fcSRusty Russell else 885296f96fcSRusty Russell BUG(); 88634a48579SRusty Russell if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN) 887b3f24698SRusty Russell hdr->hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN; 888296f96fcSRusty Russell } else { 889b3f24698SRusty Russell hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE; 890b3f24698SRusty Russell hdr->hdr.gso_size = hdr->hdr.hdr_len = 0; 891296f96fcSRusty Russell } 892296f96fcSRusty Russell 893e7428e95SMichael S. Tsirkin if (vi->mergeable_rx_bufs) 894b3f24698SRusty Russell hdr->mhdr.num_buffers = 0; 8953f2c31d9SMark McLoughlin 896e7428e95SMichael S. Tsirkin if (can_push) { 897e7428e95SMichael S. Tsirkin __skb_push(skb, hdr_len); 898e7428e95SMichael S. Tsirkin num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len); 899e7428e95SMichael S. Tsirkin /* Pull header back to avoid skew in tx bytes calculations. */ 900e7428e95SMichael S. Tsirkin __skb_pull(skb, hdr_len); 901e7428e95SMichael S. Tsirkin } else { 902e7428e95SMichael S. Tsirkin sg_set_buf(sq->sg, hdr, hdr_len); 903b7dfde95SLinus Torvalds num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len) + 1; 904e7428e95SMichael S. Tsirkin } 9059dc7b9e4SRusty Russell return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC); 90611a3a154SRusty Russell } 90711a3a154SRusty Russell 908424efe9cSStephen Hemminger static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev) 90999ffc696SRusty Russell { 91099ffc696SRusty Russell struct virtnet_info *vi = netdev_priv(dev); 911986a4f4dSJason Wang int qnum = skb_get_queue_mapping(skb); 912986a4f4dSJason Wang struct send_queue *sq = &vi->sq[qnum]; 9139ed4cb07SRusty Russell int err; 9142cb9c6baSRusty Russell 9152cb9c6baSRusty Russell /* Free up any pending old buffers before queueing new ones. */ 916e9d7417bSJason Wang free_old_xmit_skbs(sq); 91799ffc696SRusty Russell 91803f191baSMichael S. Tsirkin /* Try to transmit */ 919b7dfde95SLinus Torvalds err = xmit_skb(sq, skb); 92099ffc696SRusty Russell 9219ed4cb07SRusty Russell /* This should not happen! */ 922681daee2SJason Wang if (unlikely(err)) { 92358eba97dSRusty Russell dev->stats.tx_fifo_errors++; 9242e57b79cSRick Jones if (net_ratelimit()) 92558eba97dSRusty Russell dev_warn(&dev->dev, 926b7dfde95SLinus Torvalds "Unexpected TXQ (%d) queue failure: %d\n", qnum, err); 92758eba97dSRusty Russell dev->stats.tx_dropped++; 92885e94525SEric W. Biederman dev_kfree_skb_any(skb); 92958eba97dSRusty Russell return NETDEV_TX_OK; 930296f96fcSRusty Russell } 931681daee2SJason Wang virtqueue_kick(sq->vq); 93203f191baSMichael S. Tsirkin 93348925e37SRusty Russell /* Don't wait up for transmitted skbs to be freed. */ 93448925e37SRusty Russell skb_orphan(skb); 93548925e37SRusty Russell nf_reset(skb); 93648925e37SRusty Russell 93748925e37SRusty Russell /* Apparently nice girls don't return TX_BUSY; stop the queue 93848925e37SRusty Russell * before it gets out of hand. Naturally, this wastes entries. */ 939b7dfde95SLinus Torvalds if (sq->vq->num_free < 2+MAX_SKB_FRAGS) { 940986a4f4dSJason Wang netif_stop_subqueue(dev, qnum); 941e9d7417bSJason Wang if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) { 94248925e37SRusty Russell /* More just got used, free them then recheck. */ 943b7dfde95SLinus Torvalds free_old_xmit_skbs(sq); 944b7dfde95SLinus Torvalds if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) { 945986a4f4dSJason Wang netif_start_subqueue(dev, qnum); 946e9d7417bSJason Wang virtqueue_disable_cb(sq->vq); 94748925e37SRusty Russell } 94848925e37SRusty Russell } 94948925e37SRusty Russell } 95048925e37SRusty Russell 95148925e37SRusty Russell return NETDEV_TX_OK; 95248925e37SRusty Russell } 95348925e37SRusty Russell 95440cbfc37SAmos Kong /* 95540cbfc37SAmos Kong * Send command via the control virtqueue and check status. Commands 95640cbfc37SAmos Kong * supported by the hypervisor, as indicated by feature bits, should 957788a8b6dSstephen hemminger * never fail unless improperly formatted. 95840cbfc37SAmos Kong */ 95940cbfc37SAmos Kong static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd, 960d24bae32Sstephen hemminger struct scatterlist *out) 96140cbfc37SAmos Kong { 962f7bc9594SRusty Russell struct scatterlist *sgs[4], hdr, stat; 96340cbfc37SAmos Kong struct virtio_net_ctrl_hdr ctrl; 96440cbfc37SAmos Kong virtio_net_ctrl_ack status = ~0; 965d24bae32Sstephen hemminger unsigned out_num = 0, tmp; 96640cbfc37SAmos Kong 96740cbfc37SAmos Kong /* Caller should know better */ 968f7bc9594SRusty Russell BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)); 96940cbfc37SAmos Kong 97040cbfc37SAmos Kong ctrl.class = class; 97140cbfc37SAmos Kong ctrl.cmd = cmd; 972f7bc9594SRusty Russell /* Add header */ 973f7bc9594SRusty Russell sg_init_one(&hdr, &ctrl, sizeof(ctrl)); 974f7bc9594SRusty Russell sgs[out_num++] = &hdr; 97540cbfc37SAmos Kong 976f7bc9594SRusty Russell if (out) 977f7bc9594SRusty Russell sgs[out_num++] = out; 97840cbfc37SAmos Kong 979f7bc9594SRusty Russell /* Add return status. */ 980f7bc9594SRusty Russell sg_init_one(&stat, &status, sizeof(status)); 981d24bae32Sstephen hemminger sgs[out_num] = &stat; 98240cbfc37SAmos Kong 983d24bae32Sstephen hemminger BUG_ON(out_num + 1 > ARRAY_SIZE(sgs)); 984a7c58146SRusty Russell virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC); 98540cbfc37SAmos Kong 98667975901SHeinz Graalfs if (unlikely(!virtqueue_kick(vi->cvq))) 98767975901SHeinz Graalfs return status == VIRTIO_NET_OK; 98840cbfc37SAmos Kong 98940cbfc37SAmos Kong /* Spin for a response, the kick causes an ioport write, trapping 99040cbfc37SAmos Kong * into the hypervisor, so the request should be handled immediately. 99140cbfc37SAmos Kong */ 992047b9b94SHeinz Graalfs while (!virtqueue_get_buf(vi->cvq, &tmp) && 993047b9b94SHeinz Graalfs !virtqueue_is_broken(vi->cvq)) 99440cbfc37SAmos Kong cpu_relax(); 99540cbfc37SAmos Kong 99640cbfc37SAmos Kong return status == VIRTIO_NET_OK; 99740cbfc37SAmos Kong } 99840cbfc37SAmos Kong 9999c46f6d4SAlex Williamson static int virtnet_set_mac_address(struct net_device *dev, void *p) 10009c46f6d4SAlex Williamson { 10019c46f6d4SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 10029c46f6d4SAlex Williamson struct virtio_device *vdev = vi->vdev; 1003f2f2c8b4SJiri Pirko int ret; 10047e58d5aeSAmos Kong struct sockaddr *addr = p; 10057e58d5aeSAmos Kong struct scatterlist sg; 10069c46f6d4SAlex Williamson 10077e58d5aeSAmos Kong ret = eth_prepare_mac_addr_change(dev, p); 1008f2f2c8b4SJiri Pirko if (ret) 1009f2f2c8b4SJiri Pirko return ret; 10109c46f6d4SAlex Williamson 10117e58d5aeSAmos Kong if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) { 10127e58d5aeSAmos Kong sg_init_one(&sg, addr->sa_data, dev->addr_len); 10137e58d5aeSAmos Kong if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, 1014d24bae32Sstephen hemminger VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) { 10157e58d5aeSAmos Kong dev_warn(&vdev->dev, 10167e58d5aeSAmos Kong "Failed to set mac address by vq command.\n"); 10177e58d5aeSAmos Kong return -EINVAL; 10187e58d5aeSAmos Kong } 10197e58d5aeSAmos Kong } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) { 1020855e0c52SRusty Russell unsigned int i; 1021855e0c52SRusty Russell 1022855e0c52SRusty Russell /* Naturally, this has an atomicity problem. */ 1023855e0c52SRusty Russell for (i = 0; i < dev->addr_len; i++) 1024855e0c52SRusty Russell virtio_cwrite8(vdev, 1025855e0c52SRusty Russell offsetof(struct virtio_net_config, mac) + 1026855e0c52SRusty Russell i, addr->sa_data[i]); 10277e58d5aeSAmos Kong } 10287e58d5aeSAmos Kong 10297e58d5aeSAmos Kong eth_commit_mac_addr_change(dev, p); 10309c46f6d4SAlex Williamson 10319c46f6d4SAlex Williamson return 0; 10329c46f6d4SAlex Williamson } 10339c46f6d4SAlex Williamson 10343fa2a1dfSstephen hemminger static struct rtnl_link_stats64 *virtnet_stats(struct net_device *dev, 10353fa2a1dfSstephen hemminger struct rtnl_link_stats64 *tot) 10363fa2a1dfSstephen hemminger { 10373fa2a1dfSstephen hemminger struct virtnet_info *vi = netdev_priv(dev); 10383fa2a1dfSstephen hemminger int cpu; 10393fa2a1dfSstephen hemminger unsigned int start; 10403fa2a1dfSstephen hemminger 10413fa2a1dfSstephen hemminger for_each_possible_cpu(cpu) { 104258472a76SEric Dumazet struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu); 10433fa2a1dfSstephen hemminger u64 tpackets, tbytes, rpackets, rbytes; 10443fa2a1dfSstephen hemminger 10453fa2a1dfSstephen hemminger do { 104657a7744eSEric W. Biederman start = u64_stats_fetch_begin_irq(&stats->tx_syncp); 10473fa2a1dfSstephen hemminger tpackets = stats->tx_packets; 10483fa2a1dfSstephen hemminger tbytes = stats->tx_bytes; 104957a7744eSEric W. Biederman } while (u64_stats_fetch_retry_irq(&stats->tx_syncp, start)); 105083a27052SEric Dumazet 105183a27052SEric Dumazet do { 105257a7744eSEric W. Biederman start = u64_stats_fetch_begin_irq(&stats->rx_syncp); 10533fa2a1dfSstephen hemminger rpackets = stats->rx_packets; 10543fa2a1dfSstephen hemminger rbytes = stats->rx_bytes; 105557a7744eSEric W. Biederman } while (u64_stats_fetch_retry_irq(&stats->rx_syncp, start)); 10563fa2a1dfSstephen hemminger 10573fa2a1dfSstephen hemminger tot->rx_packets += rpackets; 10583fa2a1dfSstephen hemminger tot->tx_packets += tpackets; 10593fa2a1dfSstephen hemminger tot->rx_bytes += rbytes; 10603fa2a1dfSstephen hemminger tot->tx_bytes += tbytes; 10613fa2a1dfSstephen hemminger } 10623fa2a1dfSstephen hemminger 10633fa2a1dfSstephen hemminger tot->tx_dropped = dev->stats.tx_dropped; 1064021ac8d3SRick Jones tot->tx_fifo_errors = dev->stats.tx_fifo_errors; 10653fa2a1dfSstephen hemminger tot->rx_dropped = dev->stats.rx_dropped; 10663fa2a1dfSstephen hemminger tot->rx_length_errors = dev->stats.rx_length_errors; 10673fa2a1dfSstephen hemminger tot->rx_frame_errors = dev->stats.rx_frame_errors; 10683fa2a1dfSstephen hemminger 10693fa2a1dfSstephen hemminger return tot; 10703fa2a1dfSstephen hemminger } 10713fa2a1dfSstephen hemminger 1072da74e89dSAmit Shah #ifdef CONFIG_NET_POLL_CONTROLLER 1073da74e89dSAmit Shah static void virtnet_netpoll(struct net_device *dev) 1074da74e89dSAmit Shah { 1075da74e89dSAmit Shah struct virtnet_info *vi = netdev_priv(dev); 1076986a4f4dSJason Wang int i; 1077da74e89dSAmit Shah 1078986a4f4dSJason Wang for (i = 0; i < vi->curr_queue_pairs; i++) 1079986a4f4dSJason Wang napi_schedule(&vi->rq[i].napi); 1080da74e89dSAmit Shah } 1081da74e89dSAmit Shah #endif 1082da74e89dSAmit Shah 1083586d17c5SJason Wang static void virtnet_ack_link_announce(struct virtnet_info *vi) 1084586d17c5SJason Wang { 1085586d17c5SJason Wang rtnl_lock(); 1086586d17c5SJason Wang if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE, 1087d24bae32Sstephen hemminger VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL)) 1088586d17c5SJason Wang dev_warn(&vi->dev->dev, "Failed to ack link announce.\n"); 1089586d17c5SJason Wang rtnl_unlock(); 1090586d17c5SJason Wang } 1091586d17c5SJason Wang 1092986a4f4dSJason Wang static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs) 1093986a4f4dSJason Wang { 1094986a4f4dSJason Wang struct scatterlist sg; 1095986a4f4dSJason Wang struct virtio_net_ctrl_mq s; 1096986a4f4dSJason Wang struct net_device *dev = vi->dev; 1097986a4f4dSJason Wang 1098986a4f4dSJason Wang if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ)) 1099986a4f4dSJason Wang return 0; 1100986a4f4dSJason Wang 1101986a4f4dSJason Wang s.virtqueue_pairs = queue_pairs; 1102986a4f4dSJason Wang sg_init_one(&sg, &s, sizeof(s)); 1103986a4f4dSJason Wang 1104986a4f4dSJason Wang if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ, 1105d24bae32Sstephen hemminger VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) { 1106986a4f4dSJason Wang dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n", 1107986a4f4dSJason Wang queue_pairs); 1108986a4f4dSJason Wang return -EINVAL; 110955257d72SSasha Levin } else { 1110986a4f4dSJason Wang vi->curr_queue_pairs = queue_pairs; 111135ed159bSJason Wang /* virtnet_open() will refill when device is going to up. */ 111235ed159bSJason Wang if (dev->flags & IFF_UP) 11139b9cd802SJason Wang schedule_delayed_work(&vi->refill, 0); 111455257d72SSasha Levin } 1115986a4f4dSJason Wang 1116986a4f4dSJason Wang return 0; 1117986a4f4dSJason Wang } 1118986a4f4dSJason Wang 1119296f96fcSRusty Russell static int virtnet_close(struct net_device *dev) 1120296f96fcSRusty Russell { 1121296f96fcSRusty Russell struct virtnet_info *vi = netdev_priv(dev); 1122986a4f4dSJason Wang int i; 1123296f96fcSRusty Russell 1124b2baed69SRusty Russell /* Make sure refill_work doesn't re-enable napi! */ 1125b2baed69SRusty Russell cancel_delayed_work_sync(&vi->refill); 1126986a4f4dSJason Wang 1127986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) 1128986a4f4dSJason Wang napi_disable(&vi->rq[i].napi); 1129296f96fcSRusty Russell 1130296f96fcSRusty Russell return 0; 1131296f96fcSRusty Russell } 1132296f96fcSRusty Russell 11332af7698eSAlex Williamson static void virtnet_set_rx_mode(struct net_device *dev) 11342af7698eSAlex Williamson { 11352af7698eSAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 1136f565a7c2SAlex Williamson struct scatterlist sg[2]; 11372af7698eSAlex Williamson u8 promisc, allmulti; 1138f565a7c2SAlex Williamson struct virtio_net_ctrl_mac *mac_data; 1139ccffad25SJiri Pirko struct netdev_hw_addr *ha; 114032e7bfc4SJiri Pirko int uc_count; 11414cd24eafSJiri Pirko int mc_count; 1142f565a7c2SAlex Williamson void *buf; 1143f565a7c2SAlex Williamson int i; 11442af7698eSAlex Williamson 1145788a8b6dSstephen hemminger /* We can't dynamically set ndo_set_rx_mode, so return gracefully */ 11462af7698eSAlex Williamson if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX)) 11472af7698eSAlex Williamson return; 11482af7698eSAlex Williamson 1149f565a7c2SAlex Williamson promisc = ((dev->flags & IFF_PROMISC) != 0); 1150f565a7c2SAlex Williamson allmulti = ((dev->flags & IFF_ALLMULTI) != 0); 11512af7698eSAlex Williamson 115223e258e1SAlex Williamson sg_init_one(sg, &promisc, sizeof(promisc)); 11532af7698eSAlex Williamson 11542af7698eSAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, 1155d24bae32Sstephen hemminger VIRTIO_NET_CTRL_RX_PROMISC, sg)) 11562af7698eSAlex Williamson dev_warn(&dev->dev, "Failed to %sable promisc mode.\n", 11572af7698eSAlex Williamson promisc ? "en" : "dis"); 11582af7698eSAlex Williamson 115923e258e1SAlex Williamson sg_init_one(sg, &allmulti, sizeof(allmulti)); 11602af7698eSAlex Williamson 11612af7698eSAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, 1162d24bae32Sstephen hemminger VIRTIO_NET_CTRL_RX_ALLMULTI, sg)) 11632af7698eSAlex Williamson dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n", 11642af7698eSAlex Williamson allmulti ? "en" : "dis"); 1165f565a7c2SAlex Williamson 116632e7bfc4SJiri Pirko uc_count = netdev_uc_count(dev); 11674cd24eafSJiri Pirko mc_count = netdev_mc_count(dev); 1168f565a7c2SAlex Williamson /* MAC filter - use one buffer for both lists */ 11694cd24eafSJiri Pirko buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) + 1170f565a7c2SAlex Williamson (2 * sizeof(mac_data->entries)), GFP_ATOMIC); 11714cd24eafSJiri Pirko mac_data = buf; 1172e68ed8f0SJoe Perches if (!buf) 1173f565a7c2SAlex Williamson return; 1174f565a7c2SAlex Williamson 117523e258e1SAlex Williamson sg_init_table(sg, 2); 117623e258e1SAlex Williamson 1177f565a7c2SAlex Williamson /* Store the unicast list and count in the front of the buffer */ 117832e7bfc4SJiri Pirko mac_data->entries = uc_count; 1179ccffad25SJiri Pirko i = 0; 118032e7bfc4SJiri Pirko netdev_for_each_uc_addr(ha, dev) 1181ccffad25SJiri Pirko memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN); 1182f565a7c2SAlex Williamson 1183f565a7c2SAlex Williamson sg_set_buf(&sg[0], mac_data, 118432e7bfc4SJiri Pirko sizeof(mac_data->entries) + (uc_count * ETH_ALEN)); 1185f565a7c2SAlex Williamson 1186f565a7c2SAlex Williamson /* multicast list and count fill the end */ 118732e7bfc4SJiri Pirko mac_data = (void *)&mac_data->macs[uc_count][0]; 1188f565a7c2SAlex Williamson 11894cd24eafSJiri Pirko mac_data->entries = mc_count; 1190567ec874SJiri Pirko i = 0; 119122bedad3SJiri Pirko netdev_for_each_mc_addr(ha, dev) 119222bedad3SJiri Pirko memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN); 1193f565a7c2SAlex Williamson 1194f565a7c2SAlex Williamson sg_set_buf(&sg[1], mac_data, 11954cd24eafSJiri Pirko sizeof(mac_data->entries) + (mc_count * ETH_ALEN)); 1196f565a7c2SAlex Williamson 1197f565a7c2SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, 1198d24bae32Sstephen hemminger VIRTIO_NET_CTRL_MAC_TABLE_SET, sg)) 119999e872aeSThomas Huth dev_warn(&dev->dev, "Failed to set MAC filter table.\n"); 1200f565a7c2SAlex Williamson 1201f565a7c2SAlex Williamson kfree(buf); 12022af7698eSAlex Williamson } 12032af7698eSAlex Williamson 120480d5c368SPatrick McHardy static int virtnet_vlan_rx_add_vid(struct net_device *dev, 120580d5c368SPatrick McHardy __be16 proto, u16 vid) 12060bde9569SAlex Williamson { 12070bde9569SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 12080bde9569SAlex Williamson struct scatterlist sg; 12090bde9569SAlex Williamson 121023e258e1SAlex Williamson sg_init_one(&sg, &vid, sizeof(vid)); 12110bde9569SAlex Williamson 12120bde9569SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, 1213d24bae32Sstephen hemminger VIRTIO_NET_CTRL_VLAN_ADD, &sg)) 12140bde9569SAlex Williamson dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid); 12158e586137SJiri Pirko return 0; 12160bde9569SAlex Williamson } 12170bde9569SAlex Williamson 121880d5c368SPatrick McHardy static int virtnet_vlan_rx_kill_vid(struct net_device *dev, 121980d5c368SPatrick McHardy __be16 proto, u16 vid) 12200bde9569SAlex Williamson { 12210bde9569SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 12220bde9569SAlex Williamson struct scatterlist sg; 12230bde9569SAlex Williamson 122423e258e1SAlex Williamson sg_init_one(&sg, &vid, sizeof(vid)); 12250bde9569SAlex Williamson 12260bde9569SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, 1227d24bae32Sstephen hemminger VIRTIO_NET_CTRL_VLAN_DEL, &sg)) 12280bde9569SAlex Williamson dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid); 12298e586137SJiri Pirko return 0; 12300bde9569SAlex Williamson } 12310bde9569SAlex Williamson 12328898c21cSWanlong Gao static void virtnet_clean_affinity(struct virtnet_info *vi, long hcpu) 1233986a4f4dSJason Wang { 1234986a4f4dSJason Wang int i; 12358898c21cSWanlong Gao 12368898c21cSWanlong Gao if (vi->affinity_hint_set) { 12378898c21cSWanlong Gao for (i = 0; i < vi->max_queue_pairs; i++) { 12388898c21cSWanlong Gao virtqueue_set_affinity(vi->rq[i].vq, -1); 12398898c21cSWanlong Gao virtqueue_set_affinity(vi->sq[i].vq, -1); 12408898c21cSWanlong Gao } 12418898c21cSWanlong Gao 12428898c21cSWanlong Gao vi->affinity_hint_set = false; 12438898c21cSWanlong Gao } 12448898c21cSWanlong Gao } 12458898c21cSWanlong Gao 12468898c21cSWanlong Gao static void virtnet_set_affinity(struct virtnet_info *vi) 1247986a4f4dSJason Wang { 1248986a4f4dSJason Wang int i; 124947be2479SWanlong Gao int cpu; 1250986a4f4dSJason Wang 1251986a4f4dSJason Wang /* In multiqueue mode, when the number of cpu is equal to the number of 1252986a4f4dSJason Wang * queue pairs, we let the queue pairs to be private to one cpu by 1253986a4f4dSJason Wang * setting the affinity hint to eliminate the contention. 1254986a4f4dSJason Wang */ 12558898c21cSWanlong Gao if (vi->curr_queue_pairs == 1 || 12568898c21cSWanlong Gao vi->max_queue_pairs != num_online_cpus()) { 12578898c21cSWanlong Gao virtnet_clean_affinity(vi, -1); 1258986a4f4dSJason Wang return; 1259986a4f4dSJason Wang } 1260986a4f4dSJason Wang 126147be2479SWanlong Gao i = 0; 126247be2479SWanlong Gao for_each_online_cpu(cpu) { 1263986a4f4dSJason Wang virtqueue_set_affinity(vi->rq[i].vq, cpu); 1264986a4f4dSJason Wang virtqueue_set_affinity(vi->sq[i].vq, cpu); 12659bb8ca86SJason Wang netif_set_xps_queue(vi->dev, cpumask_of(cpu), i); 126647be2479SWanlong Gao i++; 1267986a4f4dSJason Wang } 1268986a4f4dSJason Wang 1269986a4f4dSJason Wang vi->affinity_hint_set = true; 127047be2479SWanlong Gao } 1271986a4f4dSJason Wang 12728de4b2f3SWanlong Gao static int virtnet_cpu_callback(struct notifier_block *nfb, 12738de4b2f3SWanlong Gao unsigned long action, void *hcpu) 12748de4b2f3SWanlong Gao { 12758de4b2f3SWanlong Gao struct virtnet_info *vi = container_of(nfb, struct virtnet_info, nb); 12768de4b2f3SWanlong Gao 12778de4b2f3SWanlong Gao switch(action & ~CPU_TASKS_FROZEN) { 12788de4b2f3SWanlong Gao case CPU_ONLINE: 12798de4b2f3SWanlong Gao case CPU_DOWN_FAILED: 12808de4b2f3SWanlong Gao case CPU_DEAD: 12818de4b2f3SWanlong Gao virtnet_set_affinity(vi); 12828de4b2f3SWanlong Gao break; 12838de4b2f3SWanlong Gao case CPU_DOWN_PREPARE: 12848de4b2f3SWanlong Gao virtnet_clean_affinity(vi, (long)hcpu); 12858de4b2f3SWanlong Gao break; 12868de4b2f3SWanlong Gao default: 12878de4b2f3SWanlong Gao break; 12888de4b2f3SWanlong Gao } 12893ab098dfSJason Wang 12908de4b2f3SWanlong Gao return NOTIFY_OK; 1291a9ea3fc6SHerbert Xu } 1292a9ea3fc6SHerbert Xu 12938f9f4668SRick Jones static void virtnet_get_ringparam(struct net_device *dev, 12948f9f4668SRick Jones struct ethtool_ringparam *ring) 12958f9f4668SRick Jones { 12968f9f4668SRick Jones struct virtnet_info *vi = netdev_priv(dev); 12978f9f4668SRick Jones 1298986a4f4dSJason Wang ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq); 1299986a4f4dSJason Wang ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq); 13008f9f4668SRick Jones ring->rx_pending = ring->rx_max_pending; 13018f9f4668SRick Jones ring->tx_pending = ring->tx_max_pending; 13028f9f4668SRick Jones } 13038f9f4668SRick Jones 130466846048SRick Jones 130566846048SRick Jones static void virtnet_get_drvinfo(struct net_device *dev, 130666846048SRick Jones struct ethtool_drvinfo *info) 130766846048SRick Jones { 130866846048SRick Jones struct virtnet_info *vi = netdev_priv(dev); 130966846048SRick Jones struct virtio_device *vdev = vi->vdev; 131066846048SRick Jones 131166846048SRick Jones strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver)); 131266846048SRick Jones strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version)); 131366846048SRick Jones strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info)); 131466846048SRick Jones 131566846048SRick Jones } 131666846048SRick Jones 1317d73bcd2cSJason Wang /* TODO: Eliminate OOO packets during switching */ 1318d73bcd2cSJason Wang static int virtnet_set_channels(struct net_device *dev, 1319d73bcd2cSJason Wang struct ethtool_channels *channels) 1320d73bcd2cSJason Wang { 1321d73bcd2cSJason Wang struct virtnet_info *vi = netdev_priv(dev); 1322d73bcd2cSJason Wang u16 queue_pairs = channels->combined_count; 1323d73bcd2cSJason Wang int err; 1324d73bcd2cSJason Wang 1325d73bcd2cSJason Wang /* We don't support separate rx/tx channels. 1326d73bcd2cSJason Wang * We don't allow setting 'other' channels. 1327d73bcd2cSJason Wang */ 1328d73bcd2cSJason Wang if (channels->rx_count || channels->tx_count || channels->other_count) 1329d73bcd2cSJason Wang return -EINVAL; 1330d73bcd2cSJason Wang 1331c18e9cd6SAmos Kong if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0) 1332d73bcd2cSJason Wang return -EINVAL; 1333d73bcd2cSJason Wang 133447be2479SWanlong Gao get_online_cpus(); 1335d73bcd2cSJason Wang err = virtnet_set_queues(vi, queue_pairs); 1336d73bcd2cSJason Wang if (!err) { 1337d73bcd2cSJason Wang netif_set_real_num_tx_queues(dev, queue_pairs); 1338d73bcd2cSJason Wang netif_set_real_num_rx_queues(dev, queue_pairs); 1339d73bcd2cSJason Wang 13408898c21cSWanlong Gao virtnet_set_affinity(vi); 1341d73bcd2cSJason Wang } 134247be2479SWanlong Gao put_online_cpus(); 1343d73bcd2cSJason Wang 1344d73bcd2cSJason Wang return err; 1345d73bcd2cSJason Wang } 1346d73bcd2cSJason Wang 1347d73bcd2cSJason Wang static void virtnet_get_channels(struct net_device *dev, 1348d73bcd2cSJason Wang struct ethtool_channels *channels) 1349d73bcd2cSJason Wang { 1350d73bcd2cSJason Wang struct virtnet_info *vi = netdev_priv(dev); 1351d73bcd2cSJason Wang 1352d73bcd2cSJason Wang channels->combined_count = vi->curr_queue_pairs; 1353d73bcd2cSJason Wang channels->max_combined = vi->max_queue_pairs; 1354d73bcd2cSJason Wang channels->max_other = 0; 1355d73bcd2cSJason Wang channels->rx_count = 0; 1356d73bcd2cSJason Wang channels->tx_count = 0; 1357d73bcd2cSJason Wang channels->other_count = 0; 1358d73bcd2cSJason Wang } 1359d73bcd2cSJason Wang 13600fc0b732SStephen Hemminger static const struct ethtool_ops virtnet_ethtool_ops = { 136166846048SRick Jones .get_drvinfo = virtnet_get_drvinfo, 13629f4d26d0SMark McLoughlin .get_link = ethtool_op_get_link, 13638f9f4668SRick Jones .get_ringparam = virtnet_get_ringparam, 1364d73bcd2cSJason Wang .set_channels = virtnet_set_channels, 1365d73bcd2cSJason Wang .get_channels = virtnet_get_channels, 1366a9ea3fc6SHerbert Xu }; 1367a9ea3fc6SHerbert Xu 136839da5814SMark McLoughlin #define MIN_MTU 68 136939da5814SMark McLoughlin #define MAX_MTU 65535 137039da5814SMark McLoughlin 137139da5814SMark McLoughlin static int virtnet_change_mtu(struct net_device *dev, int new_mtu) 137239da5814SMark McLoughlin { 137339da5814SMark McLoughlin if (new_mtu < MIN_MTU || new_mtu > MAX_MTU) 137439da5814SMark McLoughlin return -EINVAL; 137539da5814SMark McLoughlin dev->mtu = new_mtu; 137639da5814SMark McLoughlin return 0; 137739da5814SMark McLoughlin } 137839da5814SMark McLoughlin 137976288b4eSStephen Hemminger static const struct net_device_ops virtnet_netdev = { 138076288b4eSStephen Hemminger .ndo_open = virtnet_open, 138176288b4eSStephen Hemminger .ndo_stop = virtnet_close, 138276288b4eSStephen Hemminger .ndo_start_xmit = start_xmit, 138376288b4eSStephen Hemminger .ndo_validate_addr = eth_validate_addr, 13849c46f6d4SAlex Williamson .ndo_set_mac_address = virtnet_set_mac_address, 13852af7698eSAlex Williamson .ndo_set_rx_mode = virtnet_set_rx_mode, 138676288b4eSStephen Hemminger .ndo_change_mtu = virtnet_change_mtu, 13873fa2a1dfSstephen hemminger .ndo_get_stats64 = virtnet_stats, 13881824a989SAlex Williamson .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid, 13891824a989SAlex Williamson .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid, 139076288b4eSStephen Hemminger #ifdef CONFIG_NET_POLL_CONTROLLER 139176288b4eSStephen Hemminger .ndo_poll_controller = virtnet_netpoll, 139276288b4eSStephen Hemminger #endif 139391815639SJason Wang #ifdef CONFIG_NET_RX_BUSY_POLL 139491815639SJason Wang .ndo_busy_poll = virtnet_busy_poll, 139591815639SJason Wang #endif 139676288b4eSStephen Hemminger }; 139776288b4eSStephen Hemminger 1398586d17c5SJason Wang static void virtnet_config_changed_work(struct work_struct *work) 13999f4d26d0SMark McLoughlin { 1400586d17c5SJason Wang struct virtnet_info *vi = 1401586d17c5SJason Wang container_of(work, struct virtnet_info, config_work); 14029f4d26d0SMark McLoughlin u16 v; 14039f4d26d0SMark McLoughlin 1404855e0c52SRusty Russell if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS, 1405855e0c52SRusty Russell struct virtio_net_config, status, &v) < 0) 1406507613bfSMichael S. Tsirkin return; 1407586d17c5SJason Wang 1408586d17c5SJason Wang if (v & VIRTIO_NET_S_ANNOUNCE) { 1409ee89bab1SAmerigo Wang netdev_notify_peers(vi->dev); 1410586d17c5SJason Wang virtnet_ack_link_announce(vi); 1411586d17c5SJason Wang } 14129f4d26d0SMark McLoughlin 14139f4d26d0SMark McLoughlin /* Ignore unknown (future) status bits */ 14149f4d26d0SMark McLoughlin v &= VIRTIO_NET_S_LINK_UP; 14159f4d26d0SMark McLoughlin 14169f4d26d0SMark McLoughlin if (vi->status == v) 1417507613bfSMichael S. Tsirkin return; 14189f4d26d0SMark McLoughlin 14199f4d26d0SMark McLoughlin vi->status = v; 14209f4d26d0SMark McLoughlin 14219f4d26d0SMark McLoughlin if (vi->status & VIRTIO_NET_S_LINK_UP) { 14229f4d26d0SMark McLoughlin netif_carrier_on(vi->dev); 1423986a4f4dSJason Wang netif_tx_wake_all_queues(vi->dev); 14249f4d26d0SMark McLoughlin } else { 14259f4d26d0SMark McLoughlin netif_carrier_off(vi->dev); 1426986a4f4dSJason Wang netif_tx_stop_all_queues(vi->dev); 14279f4d26d0SMark McLoughlin } 14289f4d26d0SMark McLoughlin } 14299f4d26d0SMark McLoughlin 14309f4d26d0SMark McLoughlin static void virtnet_config_changed(struct virtio_device *vdev) 14319f4d26d0SMark McLoughlin { 14329f4d26d0SMark McLoughlin struct virtnet_info *vi = vdev->priv; 14339f4d26d0SMark McLoughlin 14343b07e9caSTejun Heo schedule_work(&vi->config_work); 14359f4d26d0SMark McLoughlin } 14369f4d26d0SMark McLoughlin 1437986a4f4dSJason Wang static void virtnet_free_queues(struct virtnet_info *vi) 1438986a4f4dSJason Wang { 1439d4fb84eeSAndrey Vagin int i; 1440d4fb84eeSAndrey Vagin 1441d4fb84eeSAndrey Vagin for (i = 0; i < vi->max_queue_pairs; i++) 1442d4fb84eeSAndrey Vagin netif_napi_del(&vi->rq[i].napi); 1443d4fb84eeSAndrey Vagin 1444986a4f4dSJason Wang kfree(vi->rq); 1445986a4f4dSJason Wang kfree(vi->sq); 1446986a4f4dSJason Wang } 1447986a4f4dSJason Wang 1448986a4f4dSJason Wang static void free_receive_bufs(struct virtnet_info *vi) 1449986a4f4dSJason Wang { 1450986a4f4dSJason Wang int i; 1451986a4f4dSJason Wang 1452986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1453986a4f4dSJason Wang while (vi->rq[i].pages) 1454986a4f4dSJason Wang __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0); 1455986a4f4dSJason Wang } 1456986a4f4dSJason Wang } 1457986a4f4dSJason Wang 1458fb51879dSMichael Dalton static void free_receive_page_frags(struct virtnet_info *vi) 1459fb51879dSMichael Dalton { 1460fb51879dSMichael Dalton int i; 1461fb51879dSMichael Dalton for (i = 0; i < vi->max_queue_pairs; i++) 1462fb51879dSMichael Dalton if (vi->rq[i].alloc_frag.page) 1463fb51879dSMichael Dalton put_page(vi->rq[i].alloc_frag.page); 1464fb51879dSMichael Dalton } 1465fb51879dSMichael Dalton 1466986a4f4dSJason Wang static void free_unused_bufs(struct virtnet_info *vi) 1467986a4f4dSJason Wang { 1468986a4f4dSJason Wang void *buf; 1469986a4f4dSJason Wang int i; 1470986a4f4dSJason Wang 1471986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1472986a4f4dSJason Wang struct virtqueue *vq = vi->sq[i].vq; 1473986a4f4dSJason Wang while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) 1474986a4f4dSJason Wang dev_kfree_skb(buf); 1475986a4f4dSJason Wang } 1476986a4f4dSJason Wang 1477986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1478986a4f4dSJason Wang struct virtqueue *vq = vi->rq[i].vq; 1479986a4f4dSJason Wang 1480986a4f4dSJason Wang while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) { 1481ab7db917SMichael Dalton if (vi->mergeable_rx_bufs) { 1482ab7db917SMichael Dalton unsigned long ctx = (unsigned long)buf; 1483ab7db917SMichael Dalton void *base = mergeable_ctx_to_buf_address(ctx); 1484ab7db917SMichael Dalton put_page(virt_to_head_page(base)); 1485ab7db917SMichael Dalton } else if (vi->big_packets) { 1486fa9fac17SAndrey Vagin give_pages(&vi->rq[i], buf); 1487ab7db917SMichael Dalton } else { 1488986a4f4dSJason Wang dev_kfree_skb(buf); 1489986a4f4dSJason Wang } 1490986a4f4dSJason Wang } 1491986a4f4dSJason Wang } 1492ab7db917SMichael Dalton } 1493986a4f4dSJason Wang 1494e9d7417bSJason Wang static void virtnet_del_vqs(struct virtnet_info *vi) 1495e9d7417bSJason Wang { 1496e9d7417bSJason Wang struct virtio_device *vdev = vi->vdev; 1497e9d7417bSJason Wang 14988898c21cSWanlong Gao virtnet_clean_affinity(vi, -1); 1499986a4f4dSJason Wang 1500e9d7417bSJason Wang vdev->config->del_vqs(vdev); 1501986a4f4dSJason Wang 1502986a4f4dSJason Wang virtnet_free_queues(vi); 1503986a4f4dSJason Wang } 1504986a4f4dSJason Wang 1505986a4f4dSJason Wang static int virtnet_find_vqs(struct virtnet_info *vi) 1506986a4f4dSJason Wang { 1507986a4f4dSJason Wang vq_callback_t **callbacks; 1508986a4f4dSJason Wang struct virtqueue **vqs; 1509986a4f4dSJason Wang int ret = -ENOMEM; 1510986a4f4dSJason Wang int i, total_vqs; 1511986a4f4dSJason Wang const char **names; 1512986a4f4dSJason Wang 1513986a4f4dSJason Wang /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by 1514986a4f4dSJason Wang * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by 1515986a4f4dSJason Wang * possible control vq. 1516986a4f4dSJason Wang */ 1517986a4f4dSJason Wang total_vqs = vi->max_queue_pairs * 2 + 1518986a4f4dSJason Wang virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ); 1519986a4f4dSJason Wang 1520986a4f4dSJason Wang /* Allocate space for find_vqs parameters */ 1521986a4f4dSJason Wang vqs = kzalloc(total_vqs * sizeof(*vqs), GFP_KERNEL); 1522986a4f4dSJason Wang if (!vqs) 1523986a4f4dSJason Wang goto err_vq; 1524986a4f4dSJason Wang callbacks = kmalloc(total_vqs * sizeof(*callbacks), GFP_KERNEL); 1525986a4f4dSJason Wang if (!callbacks) 1526986a4f4dSJason Wang goto err_callback; 1527986a4f4dSJason Wang names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL); 1528986a4f4dSJason Wang if (!names) 1529986a4f4dSJason Wang goto err_names; 1530986a4f4dSJason Wang 1531986a4f4dSJason Wang /* Parameters for control virtqueue, if any */ 1532986a4f4dSJason Wang if (vi->has_cvq) { 1533986a4f4dSJason Wang callbacks[total_vqs - 1] = NULL; 1534986a4f4dSJason Wang names[total_vqs - 1] = "control"; 1535986a4f4dSJason Wang } 1536986a4f4dSJason Wang 1537986a4f4dSJason Wang /* Allocate/initialize parameters for send/receive virtqueues */ 1538986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1539986a4f4dSJason Wang callbacks[rxq2vq(i)] = skb_recv_done; 1540986a4f4dSJason Wang callbacks[txq2vq(i)] = skb_xmit_done; 1541986a4f4dSJason Wang sprintf(vi->rq[i].name, "input.%d", i); 1542986a4f4dSJason Wang sprintf(vi->sq[i].name, "output.%d", i); 1543986a4f4dSJason Wang names[rxq2vq(i)] = vi->rq[i].name; 1544986a4f4dSJason Wang names[txq2vq(i)] = vi->sq[i].name; 1545986a4f4dSJason Wang } 1546986a4f4dSJason Wang 1547986a4f4dSJason Wang ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks, 1548986a4f4dSJason Wang names); 1549986a4f4dSJason Wang if (ret) 1550986a4f4dSJason Wang goto err_find; 1551986a4f4dSJason Wang 1552986a4f4dSJason Wang if (vi->has_cvq) { 1553986a4f4dSJason Wang vi->cvq = vqs[total_vqs - 1]; 1554986a4f4dSJason Wang if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN)) 1555f646968fSPatrick McHardy vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER; 1556986a4f4dSJason Wang } 1557986a4f4dSJason Wang 1558986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1559986a4f4dSJason Wang vi->rq[i].vq = vqs[rxq2vq(i)]; 1560986a4f4dSJason Wang vi->sq[i].vq = vqs[txq2vq(i)]; 1561986a4f4dSJason Wang } 1562986a4f4dSJason Wang 1563986a4f4dSJason Wang kfree(names); 1564986a4f4dSJason Wang kfree(callbacks); 1565986a4f4dSJason Wang kfree(vqs); 1566986a4f4dSJason Wang 1567986a4f4dSJason Wang return 0; 1568986a4f4dSJason Wang 1569986a4f4dSJason Wang err_find: 1570986a4f4dSJason Wang kfree(names); 1571986a4f4dSJason Wang err_names: 1572986a4f4dSJason Wang kfree(callbacks); 1573986a4f4dSJason Wang err_callback: 1574986a4f4dSJason Wang kfree(vqs); 1575986a4f4dSJason Wang err_vq: 1576986a4f4dSJason Wang return ret; 1577986a4f4dSJason Wang } 1578986a4f4dSJason Wang 1579986a4f4dSJason Wang static int virtnet_alloc_queues(struct virtnet_info *vi) 1580986a4f4dSJason Wang { 1581986a4f4dSJason Wang int i; 1582986a4f4dSJason Wang 1583986a4f4dSJason Wang vi->sq = kzalloc(sizeof(*vi->sq) * vi->max_queue_pairs, GFP_KERNEL); 1584986a4f4dSJason Wang if (!vi->sq) 1585986a4f4dSJason Wang goto err_sq; 1586986a4f4dSJason Wang vi->rq = kzalloc(sizeof(*vi->rq) * vi->max_queue_pairs, GFP_KERNEL); 1587008d4278SAmerigo Wang if (!vi->rq) 1588986a4f4dSJason Wang goto err_rq; 1589986a4f4dSJason Wang 1590986a4f4dSJason Wang INIT_DELAYED_WORK(&vi->refill, refill_work); 1591986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1592986a4f4dSJason Wang vi->rq[i].pages = NULL; 1593986a4f4dSJason Wang netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll, 1594986a4f4dSJason Wang napi_weight); 159591815639SJason Wang napi_hash_add(&vi->rq[i].napi); 1596986a4f4dSJason Wang 1597986a4f4dSJason Wang sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg)); 1598ab7db917SMichael Dalton ewma_init(&vi->rq[i].mrg_avg_pkt_len, 1, RECEIVE_AVG_WEIGHT); 1599986a4f4dSJason Wang sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg)); 1600986a4f4dSJason Wang } 1601986a4f4dSJason Wang 1602986a4f4dSJason Wang return 0; 1603986a4f4dSJason Wang 1604986a4f4dSJason Wang err_rq: 1605986a4f4dSJason Wang kfree(vi->sq); 1606986a4f4dSJason Wang err_sq: 1607986a4f4dSJason Wang return -ENOMEM; 1608e9d7417bSJason Wang } 1609e9d7417bSJason Wang 16103f9c10b0SAmit Shah static int init_vqs(struct virtnet_info *vi) 16113f9c10b0SAmit Shah { 1612986a4f4dSJason Wang int ret; 16133f9c10b0SAmit Shah 1614986a4f4dSJason Wang /* Allocate send & receive queues */ 1615986a4f4dSJason Wang ret = virtnet_alloc_queues(vi); 1616986a4f4dSJason Wang if (ret) 1617986a4f4dSJason Wang goto err; 16183f9c10b0SAmit Shah 1619986a4f4dSJason Wang ret = virtnet_find_vqs(vi); 1620986a4f4dSJason Wang if (ret) 1621986a4f4dSJason Wang goto err_free; 16223f9c10b0SAmit Shah 162347be2479SWanlong Gao get_online_cpus(); 16248898c21cSWanlong Gao virtnet_set_affinity(vi); 162547be2479SWanlong Gao put_online_cpus(); 162647be2479SWanlong Gao 16273f9c10b0SAmit Shah return 0; 1628986a4f4dSJason Wang 1629986a4f4dSJason Wang err_free: 1630986a4f4dSJason Wang virtnet_free_queues(vi); 1631986a4f4dSJason Wang err: 1632986a4f4dSJason Wang return ret; 16333f9c10b0SAmit Shah } 16343f9c10b0SAmit Shah 1635fbf28d78SMichael Dalton #ifdef CONFIG_SYSFS 1636fbf28d78SMichael Dalton static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue, 1637fbf28d78SMichael Dalton struct rx_queue_attribute *attribute, char *buf) 1638fbf28d78SMichael Dalton { 1639fbf28d78SMichael Dalton struct virtnet_info *vi = netdev_priv(queue->dev); 1640fbf28d78SMichael Dalton unsigned int queue_index = get_netdev_rx_queue_index(queue); 1641fbf28d78SMichael Dalton struct ewma *avg; 1642fbf28d78SMichael Dalton 1643fbf28d78SMichael Dalton BUG_ON(queue_index >= vi->max_queue_pairs); 1644fbf28d78SMichael Dalton avg = &vi->rq[queue_index].mrg_avg_pkt_len; 1645fbf28d78SMichael Dalton return sprintf(buf, "%u\n", get_mergeable_buf_len(avg)); 1646fbf28d78SMichael Dalton } 1647fbf28d78SMichael Dalton 1648fbf28d78SMichael Dalton static struct rx_queue_attribute mergeable_rx_buffer_size_attribute = 1649fbf28d78SMichael Dalton __ATTR_RO(mergeable_rx_buffer_size); 1650fbf28d78SMichael Dalton 1651fbf28d78SMichael Dalton static struct attribute *virtio_net_mrg_rx_attrs[] = { 1652fbf28d78SMichael Dalton &mergeable_rx_buffer_size_attribute.attr, 1653fbf28d78SMichael Dalton NULL 1654fbf28d78SMichael Dalton }; 1655fbf28d78SMichael Dalton 1656fbf28d78SMichael Dalton static const struct attribute_group virtio_net_mrg_rx_group = { 1657fbf28d78SMichael Dalton .name = "virtio_net", 1658fbf28d78SMichael Dalton .attrs = virtio_net_mrg_rx_attrs 1659fbf28d78SMichael Dalton }; 1660fbf28d78SMichael Dalton #endif 1661fbf28d78SMichael Dalton 1662296f96fcSRusty Russell static int virtnet_probe(struct virtio_device *vdev) 1663296f96fcSRusty Russell { 1664986a4f4dSJason Wang int i, err; 1665296f96fcSRusty Russell struct net_device *dev; 1666296f96fcSRusty Russell struct virtnet_info *vi; 1667986a4f4dSJason Wang u16 max_queue_pairs; 1668986a4f4dSJason Wang 1669986a4f4dSJason Wang /* Find if host supports multiqueue virtio_net device */ 1670855e0c52SRusty Russell err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ, 1671855e0c52SRusty Russell struct virtio_net_config, 1672855e0c52SRusty Russell max_virtqueue_pairs, &max_queue_pairs); 1673986a4f4dSJason Wang 1674986a4f4dSJason Wang /* We need at least 2 queue's */ 1675986a4f4dSJason Wang if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN || 1676986a4f4dSJason Wang max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX || 1677986a4f4dSJason Wang !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) 1678986a4f4dSJason Wang max_queue_pairs = 1; 1679296f96fcSRusty Russell 1680296f96fcSRusty Russell /* Allocate ourselves a network device with room for our info */ 1681986a4f4dSJason Wang dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs); 1682296f96fcSRusty Russell if (!dev) 1683296f96fcSRusty Russell return -ENOMEM; 1684296f96fcSRusty Russell 1685296f96fcSRusty Russell /* Set up network device as normal. */ 1686f2f2c8b4SJiri Pirko dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE; 168776288b4eSStephen Hemminger dev->netdev_ops = &virtnet_netdev; 1688296f96fcSRusty Russell dev->features = NETIF_F_HIGHDMA; 16893fa2a1dfSstephen hemminger 16907ad24ea4SWilfried Klaebe dev->ethtool_ops = &virtnet_ethtool_ops; 1691296f96fcSRusty Russell SET_NETDEV_DEV(dev, &vdev->dev); 1692296f96fcSRusty Russell 1693296f96fcSRusty Russell /* Do we support "hardware" checksums? */ 169498e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) { 1695296f96fcSRusty Russell /* This opens up the world of extra features. */ 169698e778c9SMichał Mirosław dev->hw_features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST; 169798e778c9SMichał Mirosław if (csum) 1698296f96fcSRusty Russell dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST; 169998e778c9SMichał Mirosław 170098e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) { 170198e778c9SMichał Mirosław dev->hw_features |= NETIF_F_TSO | NETIF_F_UFO 170234a48579SRusty Russell | NETIF_F_TSO_ECN | NETIF_F_TSO6; 170334a48579SRusty Russell } 17045539ae96SRusty Russell /* Individual feature bits: what can host handle? */ 170598e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4)) 170698e778c9SMichał Mirosław dev->hw_features |= NETIF_F_TSO; 170798e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6)) 170898e778c9SMichał Mirosław dev->hw_features |= NETIF_F_TSO6; 170998e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN)) 171098e778c9SMichał Mirosław dev->hw_features |= NETIF_F_TSO_ECN; 171198e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO)) 171298e778c9SMichał Mirosław dev->hw_features |= NETIF_F_UFO; 171398e778c9SMichał Mirosław 171498e778c9SMichał Mirosław if (gso) 171598e778c9SMichał Mirosław dev->features |= dev->hw_features & (NETIF_F_ALL_TSO|NETIF_F_UFO); 171698e778c9SMichał Mirosław /* (!csum && gso) case will be fixed by register_netdev() */ 1717296f96fcSRusty Russell } 17184f49129bSThomas Huth if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM)) 17194f49129bSThomas Huth dev->features |= NETIF_F_RXCSUM; 1720296f96fcSRusty Russell 17214fda8302SJason Wang dev->vlan_features = dev->features; 17224fda8302SJason Wang 1723296f96fcSRusty Russell /* Configuration may specify what MAC to use. Otherwise random. */ 1724855e0c52SRusty Russell if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) 1725855e0c52SRusty Russell virtio_cread_bytes(vdev, 1726a586d4f6SRusty Russell offsetof(struct virtio_net_config, mac), 1727855e0c52SRusty Russell dev->dev_addr, dev->addr_len); 1728855e0c52SRusty Russell else 1729f2cedb63SDanny Kukawka eth_hw_addr_random(dev); 1730296f96fcSRusty Russell 1731296f96fcSRusty Russell /* Set up our device-specific information */ 1732296f96fcSRusty Russell vi = netdev_priv(dev); 1733296f96fcSRusty Russell vi->dev = dev; 1734296f96fcSRusty Russell vi->vdev = vdev; 1735d9d5dcc8SChristian Borntraeger vdev->priv = vi; 17363fa2a1dfSstephen hemminger vi->stats = alloc_percpu(struct virtnet_stats); 17373fa2a1dfSstephen hemminger err = -ENOMEM; 17383fa2a1dfSstephen hemminger if (vi->stats == NULL) 17393fa2a1dfSstephen hemminger goto free; 17403fa2a1dfSstephen hemminger 1741827da44cSJohn Stultz for_each_possible_cpu(i) { 1742827da44cSJohn Stultz struct virtnet_stats *virtnet_stats; 1743827da44cSJohn Stultz virtnet_stats = per_cpu_ptr(vi->stats, i); 1744827da44cSJohn Stultz u64_stats_init(&virtnet_stats->tx_syncp); 1745827da44cSJohn Stultz u64_stats_init(&virtnet_stats->rx_syncp); 1746827da44cSJohn Stultz } 1747827da44cSJohn Stultz 1748586d17c5SJason Wang INIT_WORK(&vi->config_work, virtnet_config_changed_work); 1749296f96fcSRusty Russell 175097402b96SHerbert Xu /* If we can receive ANY GSO packets, we must allocate large ones. */ 17518e95a202SJoe Perches if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) || 17528e95a202SJoe Perches virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) || 17530e7ede80SJason Wang virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) || 17540e7ede80SJason Wang virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO)) 175597402b96SHerbert Xu vi->big_packets = true; 175697402b96SHerbert Xu 17573f2c31d9SMark McLoughlin if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF)) 17583f2c31d9SMark McLoughlin vi->mergeable_rx_bufs = true; 17593f2c31d9SMark McLoughlin 1760e7428e95SMichael S. Tsirkin if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT)) 1761e7428e95SMichael S. Tsirkin vi->any_header_sg = true; 1762e7428e95SMichael S. Tsirkin 1763986a4f4dSJason Wang if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) 1764986a4f4dSJason Wang vi->has_cvq = true; 1765986a4f4dSJason Wang 17666ebbc1a6SZhangjie \(HZ\) if (vi->any_header_sg) { 17676ebbc1a6SZhangjie \(HZ\) if (vi->mergeable_rx_bufs) 17686ebbc1a6SZhangjie \(HZ\) dev->needed_headroom = sizeof(struct virtio_net_hdr_mrg_rxbuf); 17696ebbc1a6SZhangjie \(HZ\) else 17706ebbc1a6SZhangjie \(HZ\) dev->needed_headroom = sizeof(struct virtio_net_hdr); 17716ebbc1a6SZhangjie \(HZ\) } 17726ebbc1a6SZhangjie \(HZ\) 1773986a4f4dSJason Wang /* Use single tx/rx queue pair as default */ 1774986a4f4dSJason Wang vi->curr_queue_pairs = 1; 1775986a4f4dSJason Wang vi->max_queue_pairs = max_queue_pairs; 1776986a4f4dSJason Wang 1777986a4f4dSJason Wang /* Allocate/initialize the rx/tx queues, and invoke find_vqs */ 17783f9c10b0SAmit Shah err = init_vqs(vi); 1779d2a7dddaSMichael S. Tsirkin if (err) 17809bb8ca86SJason Wang goto free_stats; 1781d2a7dddaSMichael S. Tsirkin 1782fbf28d78SMichael Dalton #ifdef CONFIG_SYSFS 1783fbf28d78SMichael Dalton if (vi->mergeable_rx_bufs) 1784fbf28d78SMichael Dalton dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group; 1785fbf28d78SMichael Dalton #endif 17860f13b66bSZhi Yong Wu netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs); 17870f13b66bSZhi Yong Wu netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs); 1788986a4f4dSJason Wang 1789296f96fcSRusty Russell err = register_netdev(dev); 1790296f96fcSRusty Russell if (err) { 1791296f96fcSRusty Russell pr_debug("virtio_net: registering device failed\n"); 1792d2a7dddaSMichael S. Tsirkin goto free_vqs; 1793296f96fcSRusty Russell } 1794b3369c1fSRusty Russell 1795*4baf1e33SMichael S. Tsirkin virtio_device_ready(vdev); 1796*4baf1e33SMichael S. Tsirkin 1797b3369c1fSRusty Russell /* Last of all, set up some receive buffers. */ 179855257d72SSasha Levin for (i = 0; i < vi->curr_queue_pairs; i++) { 1799986a4f4dSJason Wang try_fill_recv(&vi->rq[i], GFP_KERNEL); 1800b3369c1fSRusty Russell 1801b3369c1fSRusty Russell /* If we didn't even get one input buffer, we're useless. */ 1802be121f46SJason Wang if (vi->rq[i].vq->num_free == 1803be121f46SJason Wang virtqueue_get_vring_size(vi->rq[i].vq)) { 1804986a4f4dSJason Wang free_unused_bufs(vi); 1805b3369c1fSRusty Russell err = -ENOMEM; 1806986a4f4dSJason Wang goto free_recv_bufs; 1807986a4f4dSJason Wang } 1808b3369c1fSRusty Russell } 1809b3369c1fSRusty Russell 18108de4b2f3SWanlong Gao vi->nb.notifier_call = &virtnet_cpu_callback; 18118de4b2f3SWanlong Gao err = register_hotcpu_notifier(&vi->nb); 18128de4b2f3SWanlong Gao if (err) { 18138de4b2f3SWanlong Gao pr_debug("virtio_net: registering cpu notifier failed\n"); 18148de4b2f3SWanlong Gao goto free_recv_bufs; 18158de4b2f3SWanlong Gao } 18168de4b2f3SWanlong Gao 1817167c25e4SJason Wang /* Assume link up if device can't report link status, 1818167c25e4SJason Wang otherwise get link status from config. */ 1819167c25e4SJason Wang if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) { 1820167c25e4SJason Wang netif_carrier_off(dev); 18213b07e9caSTejun Heo schedule_work(&vi->config_work); 1822167c25e4SJason Wang } else { 1823167c25e4SJason Wang vi->status = VIRTIO_NET_S_LINK_UP; 18244783256eSPantelis Koukousoulas netif_carrier_on(dev); 1825167c25e4SJason Wang } 18269f4d26d0SMark McLoughlin 1827986a4f4dSJason Wang pr_debug("virtnet: registered device %s with %d RX and TX vq's\n", 1828986a4f4dSJason Wang dev->name, max_queue_pairs); 1829986a4f4dSJason Wang 1830296f96fcSRusty Russell return 0; 1831296f96fcSRusty Russell 1832986a4f4dSJason Wang free_recv_bufs: 1833986a4f4dSJason Wang free_receive_bufs(vi); 1834b3369c1fSRusty Russell unregister_netdev(dev); 1835d2a7dddaSMichael S. Tsirkin free_vqs: 1836986a4f4dSJason Wang cancel_delayed_work_sync(&vi->refill); 1837fb51879dSMichael Dalton free_receive_page_frags(vi); 1838e9d7417bSJason Wang virtnet_del_vqs(vi); 18393fa2a1dfSstephen hemminger free_stats: 18403fa2a1dfSstephen hemminger free_percpu(vi->stats); 1841296f96fcSRusty Russell free: 1842296f96fcSRusty Russell free_netdev(dev); 1843296f96fcSRusty Russell return err; 1844296f96fcSRusty Russell } 1845296f96fcSRusty Russell 184604486ed0SAmit Shah static void remove_vq_common(struct virtnet_info *vi) 1847296f96fcSRusty Russell { 184804486ed0SAmit Shah vi->vdev->config->reset(vi->vdev); 1849830a8a97SShirley Ma 1850830a8a97SShirley Ma /* Free unused buffers in both send and recv, if any. */ 18519ab86bbcSShirley Ma free_unused_bufs(vi); 1852fb6813f4SRusty Russell 1853986a4f4dSJason Wang free_receive_bufs(vi); 1854d2a7dddaSMichael S. Tsirkin 1855fb51879dSMichael Dalton free_receive_page_frags(vi); 1856fb51879dSMichael Dalton 1857986a4f4dSJason Wang virtnet_del_vqs(vi); 185804486ed0SAmit Shah } 185904486ed0SAmit Shah 18608cc085d6SBill Pemberton static void virtnet_remove(struct virtio_device *vdev) 186104486ed0SAmit Shah { 186204486ed0SAmit Shah struct virtnet_info *vi = vdev->priv; 186304486ed0SAmit Shah 18648de4b2f3SWanlong Gao unregister_hotcpu_notifier(&vi->nb); 18658de4b2f3SWanlong Gao 1866102a2786SMichael S. Tsirkin /* Make sure no work handler is accessing the device. */ 1867102a2786SMichael S. Tsirkin flush_work(&vi->config_work); 1868586d17c5SJason Wang 186904486ed0SAmit Shah unregister_netdev(vi->dev); 187004486ed0SAmit Shah 187104486ed0SAmit Shah remove_vq_common(vi); 1872fb6813f4SRusty Russell 18732e66f55bSKrishna Kumar free_percpu(vi->stats); 187474b2553fSRusty Russell free_netdev(vi->dev); 1875296f96fcSRusty Russell } 1876296f96fcSRusty Russell 187789107000SAaron Lu #ifdef CONFIG_PM_SLEEP 18780741bcb5SAmit Shah static int virtnet_freeze(struct virtio_device *vdev) 18790741bcb5SAmit Shah { 18800741bcb5SAmit Shah struct virtnet_info *vi = vdev->priv; 1881986a4f4dSJason Wang int i; 18820741bcb5SAmit Shah 1883ec9debbdSJason Wang unregister_hotcpu_notifier(&vi->nb); 1884ec9debbdSJason Wang 1885102a2786SMichael S. Tsirkin /* Make sure no work handler is accessing the device */ 1886102a2786SMichael S. Tsirkin flush_work(&vi->config_work); 1887586d17c5SJason Wang 18880741bcb5SAmit Shah netif_device_detach(vi->dev); 18890741bcb5SAmit Shah cancel_delayed_work_sync(&vi->refill); 18900741bcb5SAmit Shah 189191815639SJason Wang if (netif_running(vi->dev)) { 1892986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1893986a4f4dSJason Wang napi_disable(&vi->rq[i].napi); 189491815639SJason Wang napi_hash_del(&vi->rq[i].napi); 1895986a4f4dSJason Wang netif_napi_del(&vi->rq[i].napi); 1896986a4f4dSJason Wang } 189791815639SJason Wang } 18980741bcb5SAmit Shah 18990741bcb5SAmit Shah remove_vq_common(vi); 19000741bcb5SAmit Shah 19010741bcb5SAmit Shah return 0; 19020741bcb5SAmit Shah } 19030741bcb5SAmit Shah 19040741bcb5SAmit Shah static int virtnet_restore(struct virtio_device *vdev) 19050741bcb5SAmit Shah { 19060741bcb5SAmit Shah struct virtnet_info *vi = vdev->priv; 1907986a4f4dSJason Wang int err, i; 19080741bcb5SAmit Shah 19090741bcb5SAmit Shah err = init_vqs(vi); 19100741bcb5SAmit Shah if (err) 19110741bcb5SAmit Shah return err; 19120741bcb5SAmit Shah 19136cd4ce00SJason Wang if (netif_running(vi->dev)) { 191455257d72SSasha Levin for (i = 0; i < vi->curr_queue_pairs; i++) 1915986a4f4dSJason Wang if (!try_fill_recv(&vi->rq[i], GFP_KERNEL)) 19163b07e9caSTejun Heo schedule_delayed_work(&vi->refill, 0); 19170741bcb5SAmit Shah 19186cd4ce00SJason Wang for (i = 0; i < vi->max_queue_pairs; i++) 19196cd4ce00SJason Wang virtnet_napi_enable(&vi->rq[i]); 19206cd4ce00SJason Wang } 19216cd4ce00SJason Wang 19226cd4ce00SJason Wang netif_device_attach(vi->dev); 19236cd4ce00SJason Wang 192435ed159bSJason Wang rtnl_lock(); 1925986a4f4dSJason Wang virtnet_set_queues(vi, vi->curr_queue_pairs); 192635ed159bSJason Wang rtnl_unlock(); 1927986a4f4dSJason Wang 1928ec9debbdSJason Wang err = register_hotcpu_notifier(&vi->nb); 1929ec9debbdSJason Wang if (err) 1930ec9debbdSJason Wang return err; 1931ec9debbdSJason Wang 19320741bcb5SAmit Shah return 0; 19330741bcb5SAmit Shah } 19340741bcb5SAmit Shah #endif 19350741bcb5SAmit Shah 1936296f96fcSRusty Russell static struct virtio_device_id id_table[] = { 1937296f96fcSRusty Russell { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID }, 1938296f96fcSRusty Russell { 0 }, 1939296f96fcSRusty Russell }; 1940296f96fcSRusty Russell 1941c45a6816SRusty Russell static unsigned int features[] = { 19425e4fe5c4SMark McLoughlin VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, 19435e4fe5c4SMark McLoughlin VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC, 1944c45a6816SRusty Russell VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, 194597402b96SHerbert Xu VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, 19465c516751SSridhar Samudrala VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, 19472a41f71dSAlex Williamson VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, 19480bde9569SAlex Williamson VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, 1949986a4f4dSJason Wang VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, 19507e58d5aeSAmos Kong VIRTIO_NET_F_CTRL_MAC_ADDR, 1951e7428e95SMichael S. Tsirkin VIRTIO_F_ANY_LAYOUT, 1952c45a6816SRusty Russell }; 1953c45a6816SRusty Russell 195422402529SUwe Kleine-König static struct virtio_driver virtio_net_driver = { 1955c45a6816SRusty Russell .feature_table = features, 1956c45a6816SRusty Russell .feature_table_size = ARRAY_SIZE(features), 1957296f96fcSRusty Russell .driver.name = KBUILD_MODNAME, 1958296f96fcSRusty Russell .driver.owner = THIS_MODULE, 1959296f96fcSRusty Russell .id_table = id_table, 1960296f96fcSRusty Russell .probe = virtnet_probe, 19618cc085d6SBill Pemberton .remove = virtnet_remove, 19629f4d26d0SMark McLoughlin .config_changed = virtnet_config_changed, 196389107000SAaron Lu #ifdef CONFIG_PM_SLEEP 19640741bcb5SAmit Shah .freeze = virtnet_freeze, 19650741bcb5SAmit Shah .restore = virtnet_restore, 19660741bcb5SAmit Shah #endif 1967296f96fcSRusty Russell }; 1968296f96fcSRusty Russell 1969b2a17029SRusty Russell module_virtio_driver(virtio_net_driver); 1970296f96fcSRusty Russell 1971296f96fcSRusty Russell MODULE_DEVICE_TABLE(virtio, id_table); 1972296f96fcSRusty Russell MODULE_DESCRIPTION("Virtio network driver"); 1973296f96fcSRusty Russell MODULE_LICENSE("GPL"); 1974