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 435377d758SJohannes Berg /* RX packet size EWMA. The average packet size is used to determine the packet 445377d758SJohannes Berg * buffer size when refilling RX rings. As the entire RX ring may be refilled 455377d758SJohannes Berg * at once, the weight is chosen so that the EWMA will be insensitive to short- 465377d758SJohannes Berg * term, transient changes in packet size. 47ab7db917SMichael Dalton */ 485377d758SJohannes Berg DECLARE_EWMA(pkt_len, 1, 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. */ 885377d758SJohannes Berg struct ewma_pkt_len 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 126012873d0SMichael S. Tsirkin /* Packet virtio header size */ 127012873d0SMichael S. Tsirkin u8 hdr_len; 128012873d0SMichael S. Tsirkin 1293fa2a1dfSstephen hemminger /* Active statistics */ 1303fa2a1dfSstephen hemminger struct virtnet_stats __percpu *stats; 1313fa2a1dfSstephen hemminger 1323161e453SRusty Russell /* Work struct for refilling if we run low on memory. */ 1333161e453SRusty Russell struct delayed_work refill; 1343161e453SRusty Russell 135586d17c5SJason Wang /* Work struct for config space updates */ 136586d17c5SJason Wang struct work_struct config_work; 137586d17c5SJason Wang 138986a4f4dSJason Wang /* Does the affinity hint is set for virtqueues? */ 139986a4f4dSJason Wang bool affinity_hint_set; 14047be2479SWanlong Gao 1418017c279SSebastian Andrzej Siewior /* CPU hotplug instances for online & dead */ 1428017c279SSebastian Andrzej Siewior struct hlist_node node; 1438017c279SSebastian Andrzej Siewior struct hlist_node node_dead; 1442ac46030SMichael S. Tsirkin 1452ac46030SMichael S. Tsirkin /* Control VQ buffers: protected by the rtnl lock */ 1462ac46030SMichael S. Tsirkin struct virtio_net_ctrl_hdr ctrl_hdr; 1472ac46030SMichael S. Tsirkin virtio_net_ctrl_ack ctrl_status; 148a725ee3eSAndy Lutomirski struct virtio_net_ctrl_mq ctrl_mq; 1492ac46030SMichael S. Tsirkin u8 ctrl_promisc; 1502ac46030SMichael S. Tsirkin u8 ctrl_allmulti; 151a725ee3eSAndy Lutomirski u16 ctrl_vid; 15216032be5SNikolay Aleksandrov 15316032be5SNikolay Aleksandrov /* Ethtool settings */ 15416032be5SNikolay Aleksandrov u8 duplex; 15516032be5SNikolay Aleksandrov u32 speed; 156296f96fcSRusty Russell }; 157296f96fcSRusty Russell 1589ab86bbcSShirley Ma struct padded_vnet_hdr { 159012873d0SMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf hdr; 1609ab86bbcSShirley Ma /* 161012873d0SMichael S. Tsirkin * hdr is in a separate sg buffer, and data sg buffer shares same page 162012873d0SMichael S. Tsirkin * with this header sg. This padding makes next sg 16 byte aligned 163012873d0SMichael S. Tsirkin * after the header. 1649ab86bbcSShirley Ma */ 165012873d0SMichael S. Tsirkin char padding[4]; 1669ab86bbcSShirley Ma }; 1679ab86bbcSShirley Ma 168986a4f4dSJason Wang /* Converting between virtqueue no. and kernel tx/rx queue no. 169986a4f4dSJason Wang * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq 170986a4f4dSJason Wang */ 171986a4f4dSJason Wang static int vq2txq(struct virtqueue *vq) 172986a4f4dSJason Wang { 1739d0ca6edSRusty Russell return (vq->index - 1) / 2; 174986a4f4dSJason Wang } 175986a4f4dSJason Wang 176986a4f4dSJason Wang static int txq2vq(int txq) 177986a4f4dSJason Wang { 178986a4f4dSJason Wang return txq * 2 + 1; 179986a4f4dSJason Wang } 180986a4f4dSJason Wang 181986a4f4dSJason Wang static int vq2rxq(struct virtqueue *vq) 182986a4f4dSJason Wang { 1839d0ca6edSRusty Russell return vq->index / 2; 184986a4f4dSJason Wang } 185986a4f4dSJason Wang 186986a4f4dSJason Wang static int rxq2vq(int rxq) 187986a4f4dSJason Wang { 188986a4f4dSJason Wang return rxq * 2; 189986a4f4dSJason Wang } 190986a4f4dSJason Wang 191012873d0SMichael S. Tsirkin static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb) 192296f96fcSRusty Russell { 193012873d0SMichael S. Tsirkin return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb; 194296f96fcSRusty Russell } 195296f96fcSRusty Russell 1969ab86bbcSShirley Ma /* 1979ab86bbcSShirley Ma * private is used to chain pages for big packets, put the whole 1989ab86bbcSShirley Ma * most recent used list in the beginning for reuse 1999ab86bbcSShirley Ma */ 200e9d7417bSJason Wang static void give_pages(struct receive_queue *rq, struct page *page) 201fb6813f4SRusty Russell { 2029ab86bbcSShirley Ma struct page *end; 2039ab86bbcSShirley Ma 204e9d7417bSJason Wang /* Find end of list, sew whole thing into vi->rq.pages. */ 2059ab86bbcSShirley Ma for (end = page; end->private; end = (struct page *)end->private); 206e9d7417bSJason Wang end->private = (unsigned long)rq->pages; 207e9d7417bSJason Wang rq->pages = page; 208fb6813f4SRusty Russell } 209fb6813f4SRusty Russell 210e9d7417bSJason Wang static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask) 211fb6813f4SRusty Russell { 212e9d7417bSJason Wang struct page *p = rq->pages; 213fb6813f4SRusty Russell 2149ab86bbcSShirley Ma if (p) { 215e9d7417bSJason Wang rq->pages = (struct page *)p->private; 2169ab86bbcSShirley Ma /* clear private here, it is used to chain pages */ 2179ab86bbcSShirley Ma p->private = 0; 2189ab86bbcSShirley Ma } else 219fb6813f4SRusty Russell p = alloc_page(gfp_mask); 220fb6813f4SRusty Russell return p; 221fb6813f4SRusty Russell } 222fb6813f4SRusty Russell 223e9d7417bSJason Wang static void skb_xmit_done(struct virtqueue *vq) 224296f96fcSRusty Russell { 225e9d7417bSJason Wang struct virtnet_info *vi = vq->vdev->priv; 226296f96fcSRusty Russell 2272cb9c6baSRusty Russell /* Suppress further interrupts. */ 228e9d7417bSJason Wang virtqueue_disable_cb(vq); 22911a3a154SRusty Russell 230363f1514SRusty Russell /* We were probably waiting for more output buffers. */ 231986a4f4dSJason Wang netif_wake_subqueue(vi->dev, vq2txq(vq)); 232296f96fcSRusty Russell } 233296f96fcSRusty Russell 234ab7db917SMichael Dalton static unsigned int mergeable_ctx_to_buf_truesize(unsigned long mrg_ctx) 235ab7db917SMichael Dalton { 236ab7db917SMichael Dalton unsigned int truesize = mrg_ctx & (MERGEABLE_BUFFER_ALIGN - 1); 237ab7db917SMichael Dalton return (truesize + 1) * MERGEABLE_BUFFER_ALIGN; 238ab7db917SMichael Dalton } 239ab7db917SMichael Dalton 240ab7db917SMichael Dalton static void *mergeable_ctx_to_buf_address(unsigned long mrg_ctx) 241ab7db917SMichael Dalton { 242ab7db917SMichael Dalton return (void *)(mrg_ctx & -MERGEABLE_BUFFER_ALIGN); 243ab7db917SMichael Dalton 244ab7db917SMichael Dalton } 245ab7db917SMichael Dalton 246ab7db917SMichael Dalton static unsigned long mergeable_buf_to_ctx(void *buf, unsigned int truesize) 247ab7db917SMichael Dalton { 248ab7db917SMichael Dalton unsigned int size = truesize / MERGEABLE_BUFFER_ALIGN; 249ab7db917SMichael Dalton return (unsigned long)buf | (size - 1); 250ab7db917SMichael Dalton } 251ab7db917SMichael Dalton 2523464645aSMike Waychison /* Called from bottom half context */ 253946fa564SMichael S. Tsirkin static struct sk_buff *page_to_skb(struct virtnet_info *vi, 254946fa564SMichael S. Tsirkin struct receive_queue *rq, 2552613af0eSMichael Dalton struct page *page, unsigned int offset, 2562613af0eSMichael Dalton unsigned int len, unsigned int truesize) 2579ab86bbcSShirley Ma { 2589ab86bbcSShirley Ma struct sk_buff *skb; 259012873d0SMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf *hdr; 2602613af0eSMichael Dalton unsigned int copy, hdr_len, hdr_padded_len; 2619ab86bbcSShirley Ma char *p; 2629ab86bbcSShirley Ma 2632613af0eSMichael Dalton p = page_address(page) + offset; 2649ab86bbcSShirley Ma 2659ab86bbcSShirley Ma /* copy small packet so we can reuse these pages for small data */ 266c67f5db8SPaolo Abeni skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN); 2679ab86bbcSShirley Ma if (unlikely(!skb)) 2689ab86bbcSShirley Ma return NULL; 2699ab86bbcSShirley Ma 2709ab86bbcSShirley Ma hdr = skb_vnet_hdr(skb); 2719ab86bbcSShirley Ma 272012873d0SMichael S. Tsirkin hdr_len = vi->hdr_len; 273012873d0SMichael S. Tsirkin if (vi->mergeable_rx_bufs) 274012873d0SMichael S. Tsirkin hdr_padded_len = sizeof *hdr; 275012873d0SMichael S. Tsirkin else 2762613af0eSMichael Dalton hdr_padded_len = sizeof(struct padded_vnet_hdr); 2773f2c31d9SMark McLoughlin 2789ab86bbcSShirley Ma memcpy(hdr, p, hdr_len); 2793f2c31d9SMark McLoughlin 2809ab86bbcSShirley Ma len -= hdr_len; 2812613af0eSMichael Dalton offset += hdr_padded_len; 2822613af0eSMichael Dalton p += hdr_padded_len; 2833f2c31d9SMark McLoughlin 2843f2c31d9SMark McLoughlin copy = len; 2853f2c31d9SMark McLoughlin if (copy > skb_tailroom(skb)) 2863f2c31d9SMark McLoughlin copy = skb_tailroom(skb); 2873f2c31d9SMark McLoughlin memcpy(skb_put(skb, copy), p, copy); 2883f2c31d9SMark McLoughlin 2893f2c31d9SMark McLoughlin len -= copy; 2909ab86bbcSShirley Ma offset += copy; 2913f2c31d9SMark McLoughlin 2922613af0eSMichael Dalton if (vi->mergeable_rx_bufs) { 2932613af0eSMichael Dalton if (len) 2942613af0eSMichael Dalton skb_add_rx_frag(skb, 0, page, offset, len, truesize); 2952613af0eSMichael Dalton else 2962613af0eSMichael Dalton put_page(page); 2972613af0eSMichael Dalton return skb; 2982613af0eSMichael Dalton } 2992613af0eSMichael Dalton 300e878d78bSSasha Levin /* 301e878d78bSSasha Levin * Verify that we can indeed put this data into a skb. 302e878d78bSSasha Levin * This is here to handle cases when the device erroneously 303e878d78bSSasha Levin * tries to receive more than is possible. This is usually 304e878d78bSSasha Levin * the case of a broken device. 305e878d78bSSasha Levin */ 306e878d78bSSasha Levin if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) { 307be443899SAmerigo Wang net_dbg_ratelimited("%s: too much data\n", skb->dev->name); 308e878d78bSSasha Levin dev_kfree_skb(skb); 309e878d78bSSasha Levin return NULL; 310e878d78bSSasha Levin } 3112613af0eSMichael Dalton BUG_ON(offset >= PAGE_SIZE); 3129ab86bbcSShirley Ma while (len) { 3132613af0eSMichael Dalton unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len); 3142613af0eSMichael Dalton skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset, 3152613af0eSMichael Dalton frag_size, truesize); 3162613af0eSMichael Dalton len -= frag_size; 3179ab86bbcSShirley Ma page = (struct page *)page->private; 3189ab86bbcSShirley Ma offset = 0; 3193f2c31d9SMark McLoughlin } 3203f2c31d9SMark McLoughlin 3219ab86bbcSShirley Ma if (page) 322e9d7417bSJason Wang give_pages(rq, page); 3233f2c31d9SMark McLoughlin 3249ab86bbcSShirley Ma return skb; 3259ab86bbcSShirley Ma } 3269ab86bbcSShirley Ma 327012873d0SMichael S. Tsirkin static struct sk_buff *receive_small(struct virtnet_info *vi, void *buf, unsigned int len) 328f121159dSMichael S. Tsirkin { 329f121159dSMichael S. Tsirkin struct sk_buff * skb = buf; 330f121159dSMichael S. Tsirkin 331012873d0SMichael S. Tsirkin len -= vi->hdr_len; 332f121159dSMichael S. Tsirkin skb_trim(skb, len); 333f121159dSMichael S. Tsirkin 334f121159dSMichael S. Tsirkin return skb; 335f121159dSMichael S. Tsirkin } 336f121159dSMichael S. Tsirkin 337f121159dSMichael S. Tsirkin static struct sk_buff *receive_big(struct net_device *dev, 338946fa564SMichael S. Tsirkin struct virtnet_info *vi, 339f121159dSMichael S. Tsirkin struct receive_queue *rq, 340f121159dSMichael S. Tsirkin void *buf, 341f121159dSMichael S. Tsirkin unsigned int len) 342f121159dSMichael S. Tsirkin { 343f121159dSMichael S. Tsirkin struct page *page = buf; 344946fa564SMichael S. Tsirkin struct sk_buff *skb = page_to_skb(vi, rq, page, 0, len, PAGE_SIZE); 345f121159dSMichael S. Tsirkin 346f121159dSMichael S. Tsirkin if (unlikely(!skb)) 347f121159dSMichael S. Tsirkin goto err; 348f121159dSMichael S. Tsirkin 349f121159dSMichael S. Tsirkin return skb; 350f121159dSMichael S. Tsirkin 351f121159dSMichael S. Tsirkin err: 352f121159dSMichael S. Tsirkin dev->stats.rx_dropped++; 353f121159dSMichael S. Tsirkin give_pages(rq, page); 354f121159dSMichael S. Tsirkin return NULL; 355f121159dSMichael S. Tsirkin } 356f121159dSMichael S. Tsirkin 3578fc3b9e9SMichael S. Tsirkin static struct sk_buff *receive_mergeable(struct net_device *dev, 358fdd819b2SMichael S. Tsirkin struct virtnet_info *vi, 3598fc3b9e9SMichael S. Tsirkin struct receive_queue *rq, 360ab7db917SMichael Dalton unsigned long ctx, 3618fc3b9e9SMichael S. Tsirkin unsigned int len) 3629ab86bbcSShirley Ma { 363ab7db917SMichael Dalton void *buf = mergeable_ctx_to_buf_address(ctx); 364012873d0SMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf *hdr = buf; 365012873d0SMichael S. Tsirkin u16 num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers); 3668fc3b9e9SMichael S. Tsirkin struct page *page = virt_to_head_page(buf); 3678fc3b9e9SMichael S. Tsirkin int offset = buf - page_address(page); 368ab7db917SMichael Dalton unsigned int truesize = max(len, mergeable_ctx_to_buf_truesize(ctx)); 369ab7db917SMichael Dalton 370946fa564SMichael S. Tsirkin struct sk_buff *head_skb = page_to_skb(vi, rq, page, offset, len, 371946fa564SMichael S. Tsirkin truesize); 3722613af0eSMichael Dalton struct sk_buff *curr_skb = head_skb; 3739ab86bbcSShirley Ma 3748fc3b9e9SMichael S. Tsirkin if (unlikely(!curr_skb)) 3758fc3b9e9SMichael S. Tsirkin goto err_skb; 3769ab86bbcSShirley Ma while (--num_buf) { 3778fc3b9e9SMichael S. Tsirkin int num_skb_frags; 3788fc3b9e9SMichael S. Tsirkin 379ab7db917SMichael Dalton ctx = (unsigned long)virtqueue_get_buf(rq->vq, &len); 380ab7db917SMichael Dalton if (unlikely(!ctx)) { 3818fc3b9e9SMichael S. Tsirkin pr_debug("%s: rx error: %d buffers out of %d missing\n", 382fdd819b2SMichael S. Tsirkin dev->name, num_buf, 383012873d0SMichael S. Tsirkin virtio16_to_cpu(vi->vdev, 384012873d0SMichael S. Tsirkin hdr->num_buffers)); 3858fc3b9e9SMichael S. Tsirkin dev->stats.rx_length_errors++; 3868fc3b9e9SMichael S. Tsirkin goto err_buf; 3873f2c31d9SMark McLoughlin } 3888fc3b9e9SMichael S. Tsirkin 389ab7db917SMichael Dalton buf = mergeable_ctx_to_buf_address(ctx); 3908fc3b9e9SMichael S. Tsirkin page = virt_to_head_page(buf); 3918fc3b9e9SMichael S. Tsirkin 3928fc3b9e9SMichael S. Tsirkin num_skb_frags = skb_shinfo(curr_skb)->nr_frags; 3932613af0eSMichael Dalton if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) { 3942613af0eSMichael Dalton struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC); 3958fc3b9e9SMichael S. Tsirkin 3968fc3b9e9SMichael S. Tsirkin if (unlikely(!nskb)) 3978fc3b9e9SMichael S. Tsirkin goto err_skb; 3982613af0eSMichael Dalton if (curr_skb == head_skb) 3992613af0eSMichael Dalton skb_shinfo(curr_skb)->frag_list = nskb; 4002613af0eSMichael Dalton else 4012613af0eSMichael Dalton curr_skb->next = nskb; 4022613af0eSMichael Dalton curr_skb = nskb; 4032613af0eSMichael Dalton head_skb->truesize += nskb->truesize; 4042613af0eSMichael Dalton num_skb_frags = 0; 4052613af0eSMichael Dalton } 406ab7db917SMichael Dalton truesize = max(len, mergeable_ctx_to_buf_truesize(ctx)); 4072613af0eSMichael Dalton if (curr_skb != head_skb) { 4082613af0eSMichael Dalton head_skb->data_len += len; 4092613af0eSMichael Dalton head_skb->len += len; 410fb51879dSMichael Dalton head_skb->truesize += truesize; 4112613af0eSMichael Dalton } 4128fc3b9e9SMichael S. Tsirkin offset = buf - page_address(page); 413ba275241SJason Wang if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) { 414ba275241SJason Wang put_page(page); 415ba275241SJason Wang skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1, 416fb51879dSMichael Dalton len, truesize); 417ba275241SJason Wang } else { 4182613af0eSMichael Dalton skb_add_rx_frag(curr_skb, num_skb_frags, page, 419fb51879dSMichael Dalton offset, len, truesize); 420ba275241SJason Wang } 4218fc3b9e9SMichael S. Tsirkin } 4228fc3b9e9SMichael S. Tsirkin 4235377d758SJohannes Berg ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len); 4248fc3b9e9SMichael S. Tsirkin return head_skb; 4258fc3b9e9SMichael S. Tsirkin 4268fc3b9e9SMichael S. Tsirkin err_skb: 4278fc3b9e9SMichael S. Tsirkin put_page(page); 4288fc3b9e9SMichael S. Tsirkin while (--num_buf) { 429ab7db917SMichael Dalton ctx = (unsigned long)virtqueue_get_buf(rq->vq, &len); 430ab7db917SMichael Dalton if (unlikely(!ctx)) { 4318fc3b9e9SMichael S. Tsirkin pr_debug("%s: rx error: %d buffers missing\n", 4328fc3b9e9SMichael S. Tsirkin dev->name, num_buf); 4338fc3b9e9SMichael S. Tsirkin dev->stats.rx_length_errors++; 4348fc3b9e9SMichael S. Tsirkin break; 4358fc3b9e9SMichael S. Tsirkin } 436ab7db917SMichael Dalton page = virt_to_head_page(mergeable_ctx_to_buf_address(ctx)); 4378fc3b9e9SMichael S. Tsirkin put_page(page); 4383f2c31d9SMark McLoughlin } 4398fc3b9e9SMichael S. Tsirkin err_buf: 4408fc3b9e9SMichael S. Tsirkin dev->stats.rx_dropped++; 4418fc3b9e9SMichael S. Tsirkin dev_kfree_skb(head_skb); 4428fc3b9e9SMichael S. Tsirkin return NULL; 4439ab86bbcSShirley Ma } 4449ab86bbcSShirley Ma 445946fa564SMichael S. Tsirkin static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq, 446946fa564SMichael S. Tsirkin void *buf, unsigned int len) 4479ab86bbcSShirley Ma { 448e9d7417bSJason Wang struct net_device *dev = vi->dev; 44958472a76SEric Dumazet struct virtnet_stats *stats = this_cpu_ptr(vi->stats); 4509ab86bbcSShirley Ma struct sk_buff *skb; 451012873d0SMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf *hdr; 4529ab86bbcSShirley Ma 453bcff3162SMichael S. Tsirkin if (unlikely(len < vi->hdr_len + ETH_HLEN)) { 4549ab86bbcSShirley Ma pr_debug("%s: short packet %i\n", dev->name, len); 4559ab86bbcSShirley Ma dev->stats.rx_length_errors++; 456ab7db917SMichael Dalton if (vi->mergeable_rx_bufs) { 457ab7db917SMichael Dalton unsigned long ctx = (unsigned long)buf; 458ab7db917SMichael Dalton void *base = mergeable_ctx_to_buf_address(ctx); 459ab7db917SMichael Dalton put_page(virt_to_head_page(base)); 460ab7db917SMichael Dalton } else if (vi->big_packets) { 46198bfd23cSMichael Dalton give_pages(rq, buf); 462ab7db917SMichael Dalton } else { 4639ab86bbcSShirley Ma dev_kfree_skb(buf); 464ab7db917SMichael Dalton } 4659ab86bbcSShirley Ma return; 4669ab86bbcSShirley Ma } 4679ab86bbcSShirley Ma 468f121159dSMichael S. Tsirkin if (vi->mergeable_rx_bufs) 469fdd819b2SMichael S. Tsirkin skb = receive_mergeable(dev, vi, rq, (unsigned long)buf, len); 470f121159dSMichael S. Tsirkin else if (vi->big_packets) 471946fa564SMichael S. Tsirkin skb = receive_big(dev, vi, rq, buf, len); 472f121159dSMichael S. Tsirkin else 473012873d0SMichael S. Tsirkin skb = receive_small(vi, buf, len); 474f121159dSMichael S. Tsirkin 4758fc3b9e9SMichael S. Tsirkin if (unlikely(!skb)) 4762613af0eSMichael Dalton return; 4773f2c31d9SMark McLoughlin 4789ab86bbcSShirley Ma hdr = skb_vnet_hdr(skb); 4793fa2a1dfSstephen hemminger 48083a27052SEric Dumazet u64_stats_update_begin(&stats->rx_syncp); 4813fa2a1dfSstephen hemminger stats->rx_bytes += skb->len; 4823fa2a1dfSstephen hemminger stats->rx_packets++; 48383a27052SEric Dumazet u64_stats_update_end(&stats->rx_syncp); 484296f96fcSRusty Russell 485e858fae2SMike Rapoport if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID) 48610a8d94aSJason Wang skb->ip_summed = CHECKSUM_UNNECESSARY; 487296f96fcSRusty Russell 488e858fae2SMike Rapoport if (virtio_net_hdr_to_skb(skb, &hdr->hdr, 489e858fae2SMike Rapoport virtio_is_little_endian(vi->vdev))) { 490e858fae2SMike Rapoport net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n", 491e858fae2SMike Rapoport dev->name, hdr->hdr.gso_type, 492fdd819b2SMichael S. Tsirkin hdr->hdr.gso_size); 493296f96fcSRusty Russell goto frame_err; 494296f96fcSRusty Russell } 495296f96fcSRusty Russell 496d1dc06dcSMike Rapoport skb->protocol = eth_type_trans(skb, dev); 497d1dc06dcSMike Rapoport pr_debug("Receiving skb proto 0x%04x len %i type %i\n", 498d1dc06dcSMike Rapoport ntohs(skb->protocol), skb->len, skb->pkt_type); 499d1dc06dcSMike Rapoport 5000fbd050aSEric Dumazet napi_gro_receive(&rq->napi, skb); 501296f96fcSRusty Russell return; 502296f96fcSRusty Russell 503296f96fcSRusty Russell frame_err: 504296f96fcSRusty Russell dev->stats.rx_frame_errors++; 505296f96fcSRusty Russell dev_kfree_skb(skb); 506296f96fcSRusty Russell } 507296f96fcSRusty Russell 508946fa564SMichael S. Tsirkin static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq, 509946fa564SMichael S. Tsirkin gfp_t gfp) 510296f96fcSRusty Russell { 511296f96fcSRusty Russell struct sk_buff *skb; 512012873d0SMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf *hdr; 5139ab86bbcSShirley Ma int err; 5143f2c31d9SMark McLoughlin 5155061de36SMichael Dalton skb = __netdev_alloc_skb_ip_align(vi->dev, GOOD_PACKET_LEN, gfp); 5169ab86bbcSShirley Ma if (unlikely(!skb)) 5179ab86bbcSShirley Ma return -ENOMEM; 518296f96fcSRusty Russell 5195061de36SMichael Dalton skb_put(skb, GOOD_PACKET_LEN); 5203f2c31d9SMark McLoughlin 5213f2c31d9SMark McLoughlin hdr = skb_vnet_hdr(skb); 522547c890cSJason Wang sg_init_table(rq->sg, 2); 523012873d0SMichael S. Tsirkin sg_set_buf(rq->sg, hdr, vi->hdr_len); 524e9d7417bSJason Wang skb_to_sgvec(skb, rq->sg + 1, 0, skb->len); 52597402b96SHerbert Xu 5269dc7b9e4SRusty Russell err = virtqueue_add_inbuf(rq->vq, rq->sg, 2, skb, gfp); 5279ab86bbcSShirley Ma if (err < 0) 5289ab86bbcSShirley Ma dev_kfree_skb(skb); 52997402b96SHerbert Xu 5309ab86bbcSShirley Ma return err; 53197402b96SHerbert Xu } 53297402b96SHerbert Xu 533012873d0SMichael S. Tsirkin static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq, 534012873d0SMichael S. Tsirkin gfp_t gfp) 5359ab86bbcSShirley Ma { 5369ab86bbcSShirley Ma struct page *first, *list = NULL; 5379ab86bbcSShirley Ma char *p; 5389ab86bbcSShirley Ma int i, err, offset; 539296f96fcSRusty Russell 540a5835440SRusty Russell sg_init_table(rq->sg, MAX_SKB_FRAGS + 2); 541a5835440SRusty Russell 542e9d7417bSJason Wang /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */ 5439ab86bbcSShirley Ma for (i = MAX_SKB_FRAGS + 1; i > 1; --i) { 544e9d7417bSJason Wang first = get_a_page(rq, gfp); 5459ab86bbcSShirley Ma if (!first) { 5469ab86bbcSShirley Ma if (list) 547e9d7417bSJason Wang give_pages(rq, list); 5489ab86bbcSShirley Ma return -ENOMEM; 549296f96fcSRusty Russell } 550e9d7417bSJason Wang sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE); 5519ab86bbcSShirley Ma 5529ab86bbcSShirley Ma /* chain new page in list head to match sg */ 5539ab86bbcSShirley Ma first->private = (unsigned long)list; 5549ab86bbcSShirley Ma list = first; 5559ab86bbcSShirley Ma } 5569ab86bbcSShirley Ma 557e9d7417bSJason Wang first = get_a_page(rq, gfp); 5589ab86bbcSShirley Ma if (!first) { 559e9d7417bSJason Wang give_pages(rq, list); 5609ab86bbcSShirley Ma return -ENOMEM; 5619ab86bbcSShirley Ma } 5629ab86bbcSShirley Ma p = page_address(first); 5639ab86bbcSShirley Ma 564e9d7417bSJason Wang /* rq->sg[0], rq->sg[1] share the same page */ 565012873d0SMichael S. Tsirkin /* a separated rq->sg[0] for header - required in case !any_header_sg */ 566012873d0SMichael S. Tsirkin sg_set_buf(&rq->sg[0], p, vi->hdr_len); 5679ab86bbcSShirley Ma 568e9d7417bSJason Wang /* rq->sg[1] for data packet, from offset */ 5699ab86bbcSShirley Ma offset = sizeof(struct padded_vnet_hdr); 570e9d7417bSJason Wang sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset); 5719ab86bbcSShirley Ma 5729ab86bbcSShirley Ma /* chain first in list head */ 5739ab86bbcSShirley Ma first->private = (unsigned long)list; 5749dc7b9e4SRusty Russell err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2, 575aa989f5eSMichael S. Tsirkin first, gfp); 5769ab86bbcSShirley Ma if (err < 0) 577e9d7417bSJason Wang give_pages(rq, first); 5789ab86bbcSShirley Ma 5799ab86bbcSShirley Ma return err; 5809ab86bbcSShirley Ma } 5819ab86bbcSShirley Ma 5825377d758SJohannes Berg static unsigned int get_mergeable_buf_len(struct ewma_pkt_len *avg_pkt_len) 5839ab86bbcSShirley Ma { 584ab7db917SMichael Dalton const size_t hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf); 585fbf28d78SMichael Dalton unsigned int len; 586fbf28d78SMichael Dalton 5875377d758SJohannes Berg len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len), 588fbf28d78SMichael Dalton GOOD_PACKET_LEN, PAGE_SIZE - hdr_len); 589fbf28d78SMichael Dalton return ALIGN(len, MERGEABLE_BUFFER_ALIGN); 590fbf28d78SMichael Dalton } 591fbf28d78SMichael Dalton 592fbf28d78SMichael Dalton static int add_recvbuf_mergeable(struct receive_queue *rq, gfp_t gfp) 593fbf28d78SMichael Dalton { 594fb51879dSMichael Dalton struct page_frag *alloc_frag = &rq->alloc_frag; 595fb51879dSMichael Dalton char *buf; 596ab7db917SMichael Dalton unsigned long ctx; 5979ab86bbcSShirley Ma int err; 598fb51879dSMichael Dalton unsigned int len, hole; 5999ab86bbcSShirley Ma 600fbf28d78SMichael Dalton len = get_mergeable_buf_len(&rq->mrg_avg_pkt_len); 601ab7db917SMichael Dalton if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp))) 6029ab86bbcSShirley Ma return -ENOMEM; 603ab7db917SMichael Dalton 604fb51879dSMichael Dalton buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset; 605ab7db917SMichael Dalton ctx = mergeable_buf_to_ctx(buf, len); 606fb51879dSMichael Dalton get_page(alloc_frag->page); 607fb51879dSMichael Dalton alloc_frag->offset += len; 608fb51879dSMichael Dalton hole = alloc_frag->size - alloc_frag->offset; 609ab7db917SMichael Dalton if (hole < len) { 610ab7db917SMichael Dalton /* To avoid internal fragmentation, if there is very likely not 611ab7db917SMichael Dalton * enough space for another buffer, add the remaining space to 612ab7db917SMichael Dalton * the current buffer. This extra space is not included in 613ab7db917SMichael Dalton * the truesize stored in ctx. 614ab7db917SMichael Dalton */ 615fb51879dSMichael Dalton len += hole; 616fb51879dSMichael Dalton alloc_frag->offset += hole; 617fb51879dSMichael Dalton } 6189ab86bbcSShirley Ma 619fb51879dSMichael Dalton sg_init_one(rq->sg, buf, len); 620ab7db917SMichael Dalton err = virtqueue_add_inbuf(rq->vq, rq->sg, 1, (void *)ctx, gfp); 6219ab86bbcSShirley Ma if (err < 0) 6222613af0eSMichael Dalton put_page(virt_to_head_page(buf)); 6239ab86bbcSShirley Ma 6249ab86bbcSShirley Ma return err; 625296f96fcSRusty Russell } 626296f96fcSRusty Russell 627b2baed69SRusty Russell /* 628b2baed69SRusty Russell * Returns false if we couldn't fill entirely (OOM). 629b2baed69SRusty Russell * 630b2baed69SRusty Russell * Normally run in the receive path, but can also be run from ndo_open 631b2baed69SRusty Russell * before we're receiving packets, or from refill_work which is 632b2baed69SRusty Russell * careful to disable receiving (using napi_disable). 633b2baed69SRusty Russell */ 634946fa564SMichael S. Tsirkin static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq, 635946fa564SMichael S. Tsirkin gfp_t gfp) 6363f2c31d9SMark McLoughlin { 6373f2c31d9SMark McLoughlin int err; 6381788f495SMichael S. Tsirkin bool oom; 6393f2c31d9SMark McLoughlin 640fb51879dSMichael Dalton gfp |= __GFP_COLD; 6410aea51c3SAmit Shah do { 6429ab86bbcSShirley Ma if (vi->mergeable_rx_bufs) 643e9d7417bSJason Wang err = add_recvbuf_mergeable(rq, gfp); 6449ab86bbcSShirley Ma else if (vi->big_packets) 645012873d0SMichael S. Tsirkin err = add_recvbuf_big(vi, rq, gfp); 6469ab86bbcSShirley Ma else 647946fa564SMichael S. Tsirkin err = add_recvbuf_small(vi, rq, gfp); 6483f2c31d9SMark McLoughlin 6491788f495SMichael S. Tsirkin oom = err == -ENOMEM; 6509ed4cb07SRusty Russell if (err) 6513f2c31d9SMark McLoughlin break; 652b7dfde95SLinus Torvalds } while (rq->vq->num_free); 653681daee2SJason Wang virtqueue_kick(rq->vq); 6543161e453SRusty Russell return !oom; 6553f2c31d9SMark McLoughlin } 6563f2c31d9SMark McLoughlin 65718445c4dSRusty Russell static void skb_recv_done(struct virtqueue *rvq) 658296f96fcSRusty Russell { 659296f96fcSRusty Russell struct virtnet_info *vi = rvq->vdev->priv; 660986a4f4dSJason Wang struct receive_queue *rq = &vi->rq[vq2rxq(rvq)]; 661e9d7417bSJason Wang 66218445c4dSRusty Russell /* Schedule NAPI, Suppress further interrupts if successful. */ 663e9d7417bSJason Wang if (napi_schedule_prep(&rq->napi)) { 6641915a712SMichael S. Tsirkin virtqueue_disable_cb(rvq); 665e9d7417bSJason Wang __napi_schedule(&rq->napi); 66618445c4dSRusty Russell } 667296f96fcSRusty Russell } 668296f96fcSRusty Russell 669e9d7417bSJason Wang static void virtnet_napi_enable(struct receive_queue *rq) 6703e9d08ecSBruce Rogers { 671e9d7417bSJason Wang napi_enable(&rq->napi); 6723e9d08ecSBruce Rogers 6733e9d08ecSBruce Rogers /* If all buffers were filled by other side before we napi_enabled, we 6743e9d08ecSBruce Rogers * won't get another interrupt, so process any outstanding packets 6753e9d08ecSBruce Rogers * now. virtnet_poll wants re-enable the queue, so we disable here. 6763e9d08ecSBruce Rogers * We synchronize against interrupts via NAPI_STATE_SCHED */ 677e9d7417bSJason Wang if (napi_schedule_prep(&rq->napi)) { 678e9d7417bSJason Wang virtqueue_disable_cb(rq->vq); 679ec13ee80SMichael S. Tsirkin local_bh_disable(); 680e9d7417bSJason Wang __napi_schedule(&rq->napi); 681ec13ee80SMichael S. Tsirkin local_bh_enable(); 6823e9d08ecSBruce Rogers } 6833e9d08ecSBruce Rogers } 6843e9d08ecSBruce Rogers 6853161e453SRusty Russell static void refill_work(struct work_struct *work) 6863161e453SRusty Russell { 687e9d7417bSJason Wang struct virtnet_info *vi = 688e9d7417bSJason Wang container_of(work, struct virtnet_info, refill.work); 6893161e453SRusty Russell bool still_empty; 690986a4f4dSJason Wang int i; 6913161e453SRusty Russell 69255257d72SSasha Levin for (i = 0; i < vi->curr_queue_pairs; i++) { 693986a4f4dSJason Wang struct receive_queue *rq = &vi->rq[i]; 694986a4f4dSJason Wang 695986a4f4dSJason Wang napi_disable(&rq->napi); 696946fa564SMichael S. Tsirkin still_empty = !try_fill_recv(vi, rq, GFP_KERNEL); 697986a4f4dSJason Wang virtnet_napi_enable(rq); 6983161e453SRusty Russell 6993161e453SRusty Russell /* In theory, this can happen: if we don't get any buffers in 700986a4f4dSJason Wang * we will *never* try to fill again. 701986a4f4dSJason Wang */ 7023161e453SRusty Russell if (still_empty) 7033b07e9caSTejun Heo schedule_delayed_work(&vi->refill, HZ/2); 7043161e453SRusty Russell } 705986a4f4dSJason Wang } 7063161e453SRusty Russell 7072ffa7598SJason Wang static int virtnet_receive(struct receive_queue *rq, int budget) 708296f96fcSRusty Russell { 709e9d7417bSJason Wang struct virtnet_info *vi = rq->vq->vdev->priv; 7102ffa7598SJason Wang unsigned int len, received = 0; 7119ab86bbcSShirley Ma void *buf; 712296f96fcSRusty Russell 713296f96fcSRusty Russell while (received < budget && 714e9d7417bSJason Wang (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) { 715946fa564SMichael S. Tsirkin receive_buf(vi, rq, buf, len); 716296f96fcSRusty Russell received++; 717296f96fcSRusty Russell } 718296f96fcSRusty Russell 719be121f46SJason Wang if (rq->vq->num_free > virtqueue_get_vring_size(rq->vq) / 2) { 720946fa564SMichael S. Tsirkin if (!try_fill_recv(vi, rq, GFP_ATOMIC)) 7213b07e9caSTejun Heo schedule_delayed_work(&vi->refill, 0); 7223161e453SRusty Russell } 723296f96fcSRusty Russell 7242ffa7598SJason Wang return received; 7252ffa7598SJason Wang } 7262ffa7598SJason Wang 7272ffa7598SJason Wang static int virtnet_poll(struct napi_struct *napi, int budget) 7282ffa7598SJason Wang { 7292ffa7598SJason Wang struct receive_queue *rq = 7302ffa7598SJason Wang container_of(napi, struct receive_queue, napi); 731faadb05fSLi RongQing unsigned int r, received; 7322ffa7598SJason Wang 733faadb05fSLi RongQing received = virtnet_receive(rq, budget); 7342ffa7598SJason Wang 7358329d98eSRusty Russell /* Out of packets? */ 7368329d98eSRusty Russell if (received < budget) { 737cbdadbbfSMichael S. Tsirkin r = virtqueue_enable_cb_prepare(rq->vq); 7380fbd050aSEric Dumazet napi_complete_done(napi, received); 739cbdadbbfSMichael S. Tsirkin if (unlikely(virtqueue_poll(rq->vq, r)) && 7408e95a202SJoe Perches napi_schedule_prep(napi)) { 741e9d7417bSJason Wang virtqueue_disable_cb(rq->vq); 742288379f0SBen Hutchings __napi_schedule(napi); 743296f96fcSRusty Russell } 7444265f161SChristian Borntraeger } 745296f96fcSRusty Russell 746296f96fcSRusty Russell return received; 747296f96fcSRusty Russell } 748296f96fcSRusty Russell 74991815639SJason Wang #ifdef CONFIG_NET_RX_BUSY_POLL 75091815639SJason Wang /* must be called with local_bh_disable()d */ 75191815639SJason Wang static int virtnet_busy_poll(struct napi_struct *napi) 75291815639SJason Wang { 75391815639SJason Wang struct receive_queue *rq = 75491815639SJason Wang container_of(napi, struct receive_queue, napi); 75591815639SJason Wang struct virtnet_info *vi = rq->vq->vdev->priv; 75691815639SJason Wang int r, received = 0, budget = 4; 75791815639SJason Wang 75891815639SJason Wang if (!(vi->status & VIRTIO_NET_S_LINK_UP)) 75991815639SJason Wang return LL_FLUSH_FAILED; 76091815639SJason Wang 76191815639SJason Wang if (!napi_schedule_prep(napi)) 76291815639SJason Wang return LL_FLUSH_BUSY; 76391815639SJason Wang 76491815639SJason Wang virtqueue_disable_cb(rq->vq); 76591815639SJason Wang 76691815639SJason Wang again: 76791815639SJason Wang received += virtnet_receive(rq, budget); 76891815639SJason Wang 76991815639SJason Wang r = virtqueue_enable_cb_prepare(rq->vq); 77091815639SJason Wang clear_bit(NAPI_STATE_SCHED, &napi->state); 77191815639SJason Wang if (unlikely(virtqueue_poll(rq->vq, r)) && 77291815639SJason Wang napi_schedule_prep(napi)) { 77391815639SJason Wang virtqueue_disable_cb(rq->vq); 77491815639SJason Wang if (received < budget) { 77591815639SJason Wang budget -= received; 77691815639SJason Wang goto again; 77791815639SJason Wang } else { 77891815639SJason Wang __napi_schedule(napi); 77991815639SJason Wang } 78091815639SJason Wang } 78191815639SJason Wang 78291815639SJason Wang return received; 78391815639SJason Wang } 78491815639SJason Wang #endif /* CONFIG_NET_RX_BUSY_POLL */ 78591815639SJason Wang 786986a4f4dSJason Wang static int virtnet_open(struct net_device *dev) 787986a4f4dSJason Wang { 788986a4f4dSJason Wang struct virtnet_info *vi = netdev_priv(dev); 789986a4f4dSJason Wang int i; 790986a4f4dSJason Wang 791e4166625SJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 792e4166625SJason Wang if (i < vi->curr_queue_pairs) 793986a4f4dSJason Wang /* Make sure we have some buffers: if oom use wq. */ 794946fa564SMichael S. Tsirkin if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL)) 795986a4f4dSJason Wang schedule_delayed_work(&vi->refill, 0); 796986a4f4dSJason Wang virtnet_napi_enable(&vi->rq[i]); 797986a4f4dSJason Wang } 798986a4f4dSJason Wang 799986a4f4dSJason Wang return 0; 800986a4f4dSJason Wang } 801986a4f4dSJason Wang 802b7dfde95SLinus Torvalds static void free_old_xmit_skbs(struct send_queue *sq) 803296f96fcSRusty Russell { 804296f96fcSRusty Russell struct sk_buff *skb; 8056ee57bccSMichael S. Tsirkin unsigned int len; 806e9d7417bSJason Wang struct virtnet_info *vi = sq->vq->vdev->priv; 80758472a76SEric Dumazet struct virtnet_stats *stats = this_cpu_ptr(vi->stats); 808296f96fcSRusty Russell 809e9d7417bSJason Wang while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) { 810296f96fcSRusty Russell pr_debug("Sent skb %p\n", skb); 8113fa2a1dfSstephen hemminger 81283a27052SEric Dumazet u64_stats_update_begin(&stats->tx_syncp); 8133fa2a1dfSstephen hemminger stats->tx_bytes += skb->len; 8143fa2a1dfSstephen hemminger stats->tx_packets++; 81583a27052SEric Dumazet u64_stats_update_end(&stats->tx_syncp); 8163fa2a1dfSstephen hemminger 817ed79bab8SEric Dumazet dev_kfree_skb_any(skb); 818296f96fcSRusty Russell } 819296f96fcSRusty Russell } 820296f96fcSRusty Russell 821e9d7417bSJason Wang static int xmit_skb(struct send_queue *sq, struct sk_buff *skb) 822296f96fcSRusty Russell { 823012873d0SMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf *hdr; 824296f96fcSRusty Russell const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest; 825e9d7417bSJason Wang struct virtnet_info *vi = sq->vq->vdev->priv; 8267bedc7dcSMichael S. Tsirkin unsigned num_sg; 827012873d0SMichael S. Tsirkin unsigned hdr_len = vi->hdr_len; 828e7428e95SMichael S. Tsirkin bool can_push; 829296f96fcSRusty Russell 830e174961cSJohannes Berg pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest); 831e7428e95SMichael S. Tsirkin 832e7428e95SMichael S. Tsirkin can_push = vi->any_header_sg && 833e7428e95SMichael S. Tsirkin !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) && 834e7428e95SMichael S. Tsirkin !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len; 835e7428e95SMichael S. Tsirkin /* Even if we can, don't push here yet as this would skew 836e7428e95SMichael S. Tsirkin * csum_start offset below. */ 837e7428e95SMichael S. Tsirkin if (can_push) 838012873d0SMichael S. Tsirkin hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len); 839e7428e95SMichael S. Tsirkin else 840e7428e95SMichael S. Tsirkin hdr = skb_vnet_hdr(skb); 841296f96fcSRusty Russell 842e858fae2SMike Rapoport if (virtio_net_hdr_from_skb(skb, &hdr->hdr, 843e858fae2SMike Rapoport virtio_is_little_endian(vi->vdev))) 844296f96fcSRusty Russell BUG(); 845296f96fcSRusty Russell 846e7428e95SMichael S. Tsirkin if (vi->mergeable_rx_bufs) 847012873d0SMichael S. Tsirkin hdr->num_buffers = 0; 8483f2c31d9SMark McLoughlin 849547c890cSJason Wang sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2)); 850e7428e95SMichael S. Tsirkin if (can_push) { 851e7428e95SMichael S. Tsirkin __skb_push(skb, hdr_len); 852e7428e95SMichael S. Tsirkin num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len); 853e7428e95SMichael S. Tsirkin /* Pull header back to avoid skew in tx bytes calculations. */ 854e7428e95SMichael S. Tsirkin __skb_pull(skb, hdr_len); 855e7428e95SMichael S. Tsirkin } else { 856e7428e95SMichael S. Tsirkin sg_set_buf(sq->sg, hdr, hdr_len); 857b7dfde95SLinus Torvalds num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len) + 1; 858e7428e95SMichael S. Tsirkin } 8599dc7b9e4SRusty Russell return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC); 86011a3a154SRusty Russell } 86111a3a154SRusty Russell 862424efe9cSStephen Hemminger static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev) 86399ffc696SRusty Russell { 86499ffc696SRusty Russell struct virtnet_info *vi = netdev_priv(dev); 865986a4f4dSJason Wang int qnum = skb_get_queue_mapping(skb); 866986a4f4dSJason Wang struct send_queue *sq = &vi->sq[qnum]; 8679ed4cb07SRusty Russell int err; 8684b7fd2e6SMichael S. Tsirkin struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum); 8694b7fd2e6SMichael S. Tsirkin bool kick = !skb->xmit_more; 8702cb9c6baSRusty Russell 8712cb9c6baSRusty Russell /* Free up any pending old buffers before queueing new ones. */ 872e9d7417bSJason Wang free_old_xmit_skbs(sq); 87399ffc696SRusty Russell 874074c3582SJacob Keller /* timestamp packet in software */ 875074c3582SJacob Keller skb_tx_timestamp(skb); 876074c3582SJacob Keller 87703f191baSMichael S. Tsirkin /* Try to transmit */ 878b7dfde95SLinus Torvalds err = xmit_skb(sq, skb); 87999ffc696SRusty Russell 8809ed4cb07SRusty Russell /* This should not happen! */ 881681daee2SJason Wang if (unlikely(err)) { 88258eba97dSRusty Russell dev->stats.tx_fifo_errors++; 8832e57b79cSRick Jones if (net_ratelimit()) 88458eba97dSRusty Russell dev_warn(&dev->dev, 885b7dfde95SLinus Torvalds "Unexpected TXQ (%d) queue failure: %d\n", qnum, err); 88658eba97dSRusty Russell dev->stats.tx_dropped++; 88785e94525SEric W. Biederman dev_kfree_skb_any(skb); 88858eba97dSRusty Russell return NETDEV_TX_OK; 889296f96fcSRusty Russell } 89003f191baSMichael S. Tsirkin 89148925e37SRusty Russell /* Don't wait up for transmitted skbs to be freed. */ 89248925e37SRusty Russell skb_orphan(skb); 89348925e37SRusty Russell nf_reset(skb); 89448925e37SRusty Russell 89560302ff6SMichael S. Tsirkin /* If running out of space, stop queue to avoid getting packets that we 89660302ff6SMichael S. Tsirkin * are then unable to transmit. 89760302ff6SMichael S. Tsirkin * An alternative would be to force queuing layer to requeue the skb by 89860302ff6SMichael S. Tsirkin * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be 89960302ff6SMichael S. Tsirkin * returned in a normal path of operation: it means that driver is not 90060302ff6SMichael S. Tsirkin * maintaining the TX queue stop/start state properly, and causes 90160302ff6SMichael S. Tsirkin * the stack to do a non-trivial amount of useless work. 90260302ff6SMichael S. Tsirkin * Since most packets only take 1 or 2 ring slots, stopping the queue 90360302ff6SMichael S. Tsirkin * early means 16 slots are typically wasted. 904d631b94eSstephen hemminger */ 905b7dfde95SLinus Torvalds if (sq->vq->num_free < 2+MAX_SKB_FRAGS) { 906986a4f4dSJason Wang netif_stop_subqueue(dev, qnum); 907e9d7417bSJason Wang if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) { 90848925e37SRusty Russell /* More just got used, free them then recheck. */ 909b7dfde95SLinus Torvalds free_old_xmit_skbs(sq); 910b7dfde95SLinus Torvalds if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) { 911986a4f4dSJason Wang netif_start_subqueue(dev, qnum); 912e9d7417bSJason Wang virtqueue_disable_cb(sq->vq); 91348925e37SRusty Russell } 91448925e37SRusty Russell } 91548925e37SRusty Russell } 91648925e37SRusty Russell 9174b7fd2e6SMichael S. Tsirkin if (kick || netif_xmit_stopped(txq)) 918c223a078SDavid S. Miller virtqueue_kick(sq->vq); 9190b725a2cSDavid S. Miller 9200b725a2cSDavid S. Miller return NETDEV_TX_OK; 921c223a078SDavid S. Miller } 922c223a078SDavid S. Miller 92340cbfc37SAmos Kong /* 92440cbfc37SAmos Kong * Send command via the control virtqueue and check status. Commands 92540cbfc37SAmos Kong * supported by the hypervisor, as indicated by feature bits, should 926788a8b6dSstephen hemminger * never fail unless improperly formatted. 92740cbfc37SAmos Kong */ 92840cbfc37SAmos Kong static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd, 929d24bae32Sstephen hemminger struct scatterlist *out) 93040cbfc37SAmos Kong { 931f7bc9594SRusty Russell struct scatterlist *sgs[4], hdr, stat; 932d24bae32Sstephen hemminger unsigned out_num = 0, tmp; 93340cbfc37SAmos Kong 93440cbfc37SAmos Kong /* Caller should know better */ 935f7bc9594SRusty Russell BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)); 93640cbfc37SAmos Kong 9372ac46030SMichael S. Tsirkin vi->ctrl_status = ~0; 9382ac46030SMichael S. Tsirkin vi->ctrl_hdr.class = class; 9392ac46030SMichael S. Tsirkin vi->ctrl_hdr.cmd = cmd; 940f7bc9594SRusty Russell /* Add header */ 9412ac46030SMichael S. Tsirkin sg_init_one(&hdr, &vi->ctrl_hdr, sizeof(vi->ctrl_hdr)); 942f7bc9594SRusty Russell sgs[out_num++] = &hdr; 94340cbfc37SAmos Kong 944f7bc9594SRusty Russell if (out) 945f7bc9594SRusty Russell sgs[out_num++] = out; 94640cbfc37SAmos Kong 947f7bc9594SRusty Russell /* Add return status. */ 9482ac46030SMichael S. Tsirkin sg_init_one(&stat, &vi->ctrl_status, sizeof(vi->ctrl_status)); 949d24bae32Sstephen hemminger sgs[out_num] = &stat; 95040cbfc37SAmos Kong 951d24bae32Sstephen hemminger BUG_ON(out_num + 1 > ARRAY_SIZE(sgs)); 952a7c58146SRusty Russell virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC); 95340cbfc37SAmos Kong 95467975901SHeinz Graalfs if (unlikely(!virtqueue_kick(vi->cvq))) 9552ac46030SMichael S. Tsirkin return vi->ctrl_status == VIRTIO_NET_OK; 95640cbfc37SAmos Kong 95740cbfc37SAmos Kong /* Spin for a response, the kick causes an ioport write, trapping 95840cbfc37SAmos Kong * into the hypervisor, so the request should be handled immediately. 95940cbfc37SAmos Kong */ 960047b9b94SHeinz Graalfs while (!virtqueue_get_buf(vi->cvq, &tmp) && 961047b9b94SHeinz Graalfs !virtqueue_is_broken(vi->cvq)) 96240cbfc37SAmos Kong cpu_relax(); 96340cbfc37SAmos Kong 9642ac46030SMichael S. Tsirkin return vi->ctrl_status == VIRTIO_NET_OK; 96540cbfc37SAmos Kong } 96640cbfc37SAmos Kong 9679c46f6d4SAlex Williamson static int virtnet_set_mac_address(struct net_device *dev, void *p) 9689c46f6d4SAlex Williamson { 9699c46f6d4SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 9709c46f6d4SAlex Williamson struct virtio_device *vdev = vi->vdev; 971f2f2c8b4SJiri Pirko int ret; 972*e37e2ff3SAndy Lutomirski struct sockaddr *addr; 9737e58d5aeSAmos Kong struct scatterlist sg; 9749c46f6d4SAlex Williamson 975*e37e2ff3SAndy Lutomirski addr = kmalloc(sizeof(*addr), GFP_KERNEL); 976*e37e2ff3SAndy Lutomirski if (!addr) 977*e37e2ff3SAndy Lutomirski return -ENOMEM; 978*e37e2ff3SAndy Lutomirski memcpy(addr, p, sizeof(*addr)); 979*e37e2ff3SAndy Lutomirski 980*e37e2ff3SAndy Lutomirski ret = eth_prepare_mac_addr_change(dev, addr); 981f2f2c8b4SJiri Pirko if (ret) 982*e37e2ff3SAndy Lutomirski goto out; 9839c46f6d4SAlex Williamson 9847e58d5aeSAmos Kong if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) { 9857e58d5aeSAmos Kong sg_init_one(&sg, addr->sa_data, dev->addr_len); 9867e58d5aeSAmos Kong if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, 987d24bae32Sstephen hemminger VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) { 9887e58d5aeSAmos Kong dev_warn(&vdev->dev, 9897e58d5aeSAmos Kong "Failed to set mac address by vq command.\n"); 990*e37e2ff3SAndy Lutomirski ret = -EINVAL; 991*e37e2ff3SAndy Lutomirski goto out; 9927e58d5aeSAmos Kong } 9937e93a02fSMichael S. Tsirkin } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) && 9947e93a02fSMichael S. Tsirkin !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) { 995855e0c52SRusty Russell unsigned int i; 996855e0c52SRusty Russell 997855e0c52SRusty Russell /* Naturally, this has an atomicity problem. */ 998855e0c52SRusty Russell for (i = 0; i < dev->addr_len; i++) 999855e0c52SRusty Russell virtio_cwrite8(vdev, 1000855e0c52SRusty Russell offsetof(struct virtio_net_config, mac) + 1001855e0c52SRusty Russell i, addr->sa_data[i]); 10027e58d5aeSAmos Kong } 10037e58d5aeSAmos Kong 10047e58d5aeSAmos Kong eth_commit_mac_addr_change(dev, p); 1005*e37e2ff3SAndy Lutomirski ret = 0; 10069c46f6d4SAlex Williamson 1007*e37e2ff3SAndy Lutomirski out: 1008*e37e2ff3SAndy Lutomirski kfree(addr); 1009*e37e2ff3SAndy Lutomirski return ret; 10109c46f6d4SAlex Williamson } 10119c46f6d4SAlex Williamson 10123fa2a1dfSstephen hemminger static struct rtnl_link_stats64 *virtnet_stats(struct net_device *dev, 10133fa2a1dfSstephen hemminger struct rtnl_link_stats64 *tot) 10143fa2a1dfSstephen hemminger { 10153fa2a1dfSstephen hemminger struct virtnet_info *vi = netdev_priv(dev); 10163fa2a1dfSstephen hemminger int cpu; 10173fa2a1dfSstephen hemminger unsigned int start; 10183fa2a1dfSstephen hemminger 10193fa2a1dfSstephen hemminger for_each_possible_cpu(cpu) { 102058472a76SEric Dumazet struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu); 10213fa2a1dfSstephen hemminger u64 tpackets, tbytes, rpackets, rbytes; 10223fa2a1dfSstephen hemminger 10233fa2a1dfSstephen hemminger do { 102457a7744eSEric W. Biederman start = u64_stats_fetch_begin_irq(&stats->tx_syncp); 10253fa2a1dfSstephen hemminger tpackets = stats->tx_packets; 10263fa2a1dfSstephen hemminger tbytes = stats->tx_bytes; 102757a7744eSEric W. Biederman } while (u64_stats_fetch_retry_irq(&stats->tx_syncp, start)); 102883a27052SEric Dumazet 102983a27052SEric Dumazet do { 103057a7744eSEric W. Biederman start = u64_stats_fetch_begin_irq(&stats->rx_syncp); 10313fa2a1dfSstephen hemminger rpackets = stats->rx_packets; 10323fa2a1dfSstephen hemminger rbytes = stats->rx_bytes; 103357a7744eSEric W. Biederman } while (u64_stats_fetch_retry_irq(&stats->rx_syncp, start)); 10343fa2a1dfSstephen hemminger 10353fa2a1dfSstephen hemminger tot->rx_packets += rpackets; 10363fa2a1dfSstephen hemminger tot->tx_packets += tpackets; 10373fa2a1dfSstephen hemminger tot->rx_bytes += rbytes; 10383fa2a1dfSstephen hemminger tot->tx_bytes += tbytes; 10393fa2a1dfSstephen hemminger } 10403fa2a1dfSstephen hemminger 10413fa2a1dfSstephen hemminger tot->tx_dropped = dev->stats.tx_dropped; 1042021ac8d3SRick Jones tot->tx_fifo_errors = dev->stats.tx_fifo_errors; 10433fa2a1dfSstephen hemminger tot->rx_dropped = dev->stats.rx_dropped; 10443fa2a1dfSstephen hemminger tot->rx_length_errors = dev->stats.rx_length_errors; 10453fa2a1dfSstephen hemminger tot->rx_frame_errors = dev->stats.rx_frame_errors; 10463fa2a1dfSstephen hemminger 10473fa2a1dfSstephen hemminger return tot; 10483fa2a1dfSstephen hemminger } 10493fa2a1dfSstephen hemminger 1050da74e89dSAmit Shah #ifdef CONFIG_NET_POLL_CONTROLLER 1051da74e89dSAmit Shah static void virtnet_netpoll(struct net_device *dev) 1052da74e89dSAmit Shah { 1053da74e89dSAmit Shah struct virtnet_info *vi = netdev_priv(dev); 1054986a4f4dSJason Wang int i; 1055da74e89dSAmit Shah 1056986a4f4dSJason Wang for (i = 0; i < vi->curr_queue_pairs; i++) 1057986a4f4dSJason Wang napi_schedule(&vi->rq[i].napi); 1058da74e89dSAmit Shah } 1059da74e89dSAmit Shah #endif 1060da74e89dSAmit Shah 1061586d17c5SJason Wang static void virtnet_ack_link_announce(struct virtnet_info *vi) 1062586d17c5SJason Wang { 1063586d17c5SJason Wang rtnl_lock(); 1064586d17c5SJason Wang if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE, 1065d24bae32Sstephen hemminger VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL)) 1066586d17c5SJason Wang dev_warn(&vi->dev->dev, "Failed to ack link announce.\n"); 1067586d17c5SJason Wang rtnl_unlock(); 1068586d17c5SJason Wang } 1069586d17c5SJason Wang 1070986a4f4dSJason Wang static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs) 1071986a4f4dSJason Wang { 1072986a4f4dSJason Wang struct scatterlist sg; 1073986a4f4dSJason Wang struct net_device *dev = vi->dev; 1074986a4f4dSJason Wang 1075986a4f4dSJason Wang if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ)) 1076986a4f4dSJason Wang return 0; 1077986a4f4dSJason Wang 1078a725ee3eSAndy Lutomirski vi->ctrl_mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs); 1079a725ee3eSAndy Lutomirski sg_init_one(&sg, &vi->ctrl_mq, sizeof(vi->ctrl_mq)); 1080986a4f4dSJason Wang 1081986a4f4dSJason Wang if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ, 1082d24bae32Sstephen hemminger VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) { 1083986a4f4dSJason Wang dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n", 1084986a4f4dSJason Wang queue_pairs); 1085986a4f4dSJason Wang return -EINVAL; 108655257d72SSasha Levin } else { 1087986a4f4dSJason Wang vi->curr_queue_pairs = queue_pairs; 108835ed159bSJason Wang /* virtnet_open() will refill when device is going to up. */ 108935ed159bSJason Wang if (dev->flags & IFF_UP) 10909b9cd802SJason Wang schedule_delayed_work(&vi->refill, 0); 109155257d72SSasha Levin } 1092986a4f4dSJason Wang 1093986a4f4dSJason Wang return 0; 1094986a4f4dSJason Wang } 1095986a4f4dSJason Wang 1096296f96fcSRusty Russell static int virtnet_close(struct net_device *dev) 1097296f96fcSRusty Russell { 1098296f96fcSRusty Russell struct virtnet_info *vi = netdev_priv(dev); 1099986a4f4dSJason Wang int i; 1100296f96fcSRusty Russell 1101b2baed69SRusty Russell /* Make sure refill_work doesn't re-enable napi! */ 1102b2baed69SRusty Russell cancel_delayed_work_sync(&vi->refill); 1103986a4f4dSJason Wang 1104986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) 1105986a4f4dSJason Wang napi_disable(&vi->rq[i].napi); 1106296f96fcSRusty Russell 1107296f96fcSRusty Russell return 0; 1108296f96fcSRusty Russell } 1109296f96fcSRusty Russell 11102af7698eSAlex Williamson static void virtnet_set_rx_mode(struct net_device *dev) 11112af7698eSAlex Williamson { 11122af7698eSAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 1113f565a7c2SAlex Williamson struct scatterlist sg[2]; 1114f565a7c2SAlex Williamson struct virtio_net_ctrl_mac *mac_data; 1115ccffad25SJiri Pirko struct netdev_hw_addr *ha; 111632e7bfc4SJiri Pirko int uc_count; 11174cd24eafSJiri Pirko int mc_count; 1118f565a7c2SAlex Williamson void *buf; 1119f565a7c2SAlex Williamson int i; 11202af7698eSAlex Williamson 1121788a8b6dSstephen hemminger /* We can't dynamically set ndo_set_rx_mode, so return gracefully */ 11222af7698eSAlex Williamson if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX)) 11232af7698eSAlex Williamson return; 11242af7698eSAlex Williamson 11252ac46030SMichael S. Tsirkin vi->ctrl_promisc = ((dev->flags & IFF_PROMISC) != 0); 11262ac46030SMichael S. Tsirkin vi->ctrl_allmulti = ((dev->flags & IFF_ALLMULTI) != 0); 11272af7698eSAlex Williamson 11282ac46030SMichael S. Tsirkin sg_init_one(sg, &vi->ctrl_promisc, sizeof(vi->ctrl_promisc)); 11292af7698eSAlex Williamson 11302af7698eSAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, 1131d24bae32Sstephen hemminger VIRTIO_NET_CTRL_RX_PROMISC, sg)) 11322af7698eSAlex Williamson dev_warn(&dev->dev, "Failed to %sable promisc mode.\n", 11332ac46030SMichael S. Tsirkin vi->ctrl_promisc ? "en" : "dis"); 11342af7698eSAlex Williamson 11352ac46030SMichael S. Tsirkin sg_init_one(sg, &vi->ctrl_allmulti, sizeof(vi->ctrl_allmulti)); 11362af7698eSAlex Williamson 11372af7698eSAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, 1138d24bae32Sstephen hemminger VIRTIO_NET_CTRL_RX_ALLMULTI, sg)) 11392af7698eSAlex Williamson dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n", 11402ac46030SMichael S. Tsirkin vi->ctrl_allmulti ? "en" : "dis"); 1141f565a7c2SAlex Williamson 114232e7bfc4SJiri Pirko uc_count = netdev_uc_count(dev); 11434cd24eafSJiri Pirko mc_count = netdev_mc_count(dev); 1144f565a7c2SAlex Williamson /* MAC filter - use one buffer for both lists */ 11454cd24eafSJiri Pirko buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) + 1146f565a7c2SAlex Williamson (2 * sizeof(mac_data->entries)), GFP_ATOMIC); 11474cd24eafSJiri Pirko mac_data = buf; 1148e68ed8f0SJoe Perches if (!buf) 1149f565a7c2SAlex Williamson return; 1150f565a7c2SAlex Williamson 115123e258e1SAlex Williamson sg_init_table(sg, 2); 115223e258e1SAlex Williamson 1153f565a7c2SAlex Williamson /* Store the unicast list and count in the front of the buffer */ 1154fdd819b2SMichael S. Tsirkin mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count); 1155ccffad25SJiri Pirko i = 0; 115632e7bfc4SJiri Pirko netdev_for_each_uc_addr(ha, dev) 1157ccffad25SJiri Pirko memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN); 1158f565a7c2SAlex Williamson 1159f565a7c2SAlex Williamson sg_set_buf(&sg[0], mac_data, 116032e7bfc4SJiri Pirko sizeof(mac_data->entries) + (uc_count * ETH_ALEN)); 1161f565a7c2SAlex Williamson 1162f565a7c2SAlex Williamson /* multicast list and count fill the end */ 116332e7bfc4SJiri Pirko mac_data = (void *)&mac_data->macs[uc_count][0]; 1164f565a7c2SAlex Williamson 1165fdd819b2SMichael S. Tsirkin mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count); 1166567ec874SJiri Pirko i = 0; 116722bedad3SJiri Pirko netdev_for_each_mc_addr(ha, dev) 116822bedad3SJiri Pirko memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN); 1169f565a7c2SAlex Williamson 1170f565a7c2SAlex Williamson sg_set_buf(&sg[1], mac_data, 11714cd24eafSJiri Pirko sizeof(mac_data->entries) + (mc_count * ETH_ALEN)); 1172f565a7c2SAlex Williamson 1173f565a7c2SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, 1174d24bae32Sstephen hemminger VIRTIO_NET_CTRL_MAC_TABLE_SET, sg)) 117599e872aeSThomas Huth dev_warn(&dev->dev, "Failed to set MAC filter table.\n"); 1176f565a7c2SAlex Williamson 1177f565a7c2SAlex Williamson kfree(buf); 11782af7698eSAlex Williamson } 11792af7698eSAlex Williamson 118080d5c368SPatrick McHardy static int virtnet_vlan_rx_add_vid(struct net_device *dev, 118180d5c368SPatrick McHardy __be16 proto, u16 vid) 11820bde9569SAlex Williamson { 11830bde9569SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 11840bde9569SAlex Williamson struct scatterlist sg; 11850bde9569SAlex Williamson 1186a725ee3eSAndy Lutomirski vi->ctrl_vid = vid; 1187a725ee3eSAndy Lutomirski sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid)); 11880bde9569SAlex Williamson 11890bde9569SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, 1190d24bae32Sstephen hemminger VIRTIO_NET_CTRL_VLAN_ADD, &sg)) 11910bde9569SAlex Williamson dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid); 11928e586137SJiri Pirko return 0; 11930bde9569SAlex Williamson } 11940bde9569SAlex Williamson 119580d5c368SPatrick McHardy static int virtnet_vlan_rx_kill_vid(struct net_device *dev, 119680d5c368SPatrick McHardy __be16 proto, u16 vid) 11970bde9569SAlex Williamson { 11980bde9569SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 11990bde9569SAlex Williamson struct scatterlist sg; 12000bde9569SAlex Williamson 1201a725ee3eSAndy Lutomirski vi->ctrl_vid = vid; 1202a725ee3eSAndy Lutomirski sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid)); 12030bde9569SAlex Williamson 12040bde9569SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, 1205d24bae32Sstephen hemminger VIRTIO_NET_CTRL_VLAN_DEL, &sg)) 12060bde9569SAlex Williamson dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid); 12078e586137SJiri Pirko return 0; 12080bde9569SAlex Williamson } 12090bde9569SAlex Williamson 12108898c21cSWanlong Gao static void virtnet_clean_affinity(struct virtnet_info *vi, long hcpu) 1211986a4f4dSJason Wang { 1212986a4f4dSJason Wang int i; 12138898c21cSWanlong Gao 12148898c21cSWanlong Gao if (vi->affinity_hint_set) { 12158898c21cSWanlong Gao for (i = 0; i < vi->max_queue_pairs; i++) { 12168898c21cSWanlong Gao virtqueue_set_affinity(vi->rq[i].vq, -1); 12178898c21cSWanlong Gao virtqueue_set_affinity(vi->sq[i].vq, -1); 12188898c21cSWanlong Gao } 12198898c21cSWanlong Gao 12208898c21cSWanlong Gao vi->affinity_hint_set = false; 12218898c21cSWanlong Gao } 12228898c21cSWanlong Gao } 12238898c21cSWanlong Gao 12248898c21cSWanlong Gao static void virtnet_set_affinity(struct virtnet_info *vi) 1225986a4f4dSJason Wang { 1226986a4f4dSJason Wang int i; 122747be2479SWanlong Gao int cpu; 1228986a4f4dSJason Wang 1229986a4f4dSJason Wang /* In multiqueue mode, when the number of cpu is equal to the number of 1230986a4f4dSJason Wang * queue pairs, we let the queue pairs to be private to one cpu by 1231986a4f4dSJason Wang * setting the affinity hint to eliminate the contention. 1232986a4f4dSJason Wang */ 12338898c21cSWanlong Gao if (vi->curr_queue_pairs == 1 || 12348898c21cSWanlong Gao vi->max_queue_pairs != num_online_cpus()) { 12358898c21cSWanlong Gao virtnet_clean_affinity(vi, -1); 1236986a4f4dSJason Wang return; 1237986a4f4dSJason Wang } 1238986a4f4dSJason Wang 123947be2479SWanlong Gao i = 0; 124047be2479SWanlong Gao for_each_online_cpu(cpu) { 1241986a4f4dSJason Wang virtqueue_set_affinity(vi->rq[i].vq, cpu); 1242986a4f4dSJason Wang virtqueue_set_affinity(vi->sq[i].vq, cpu); 12439bb8ca86SJason Wang netif_set_xps_queue(vi->dev, cpumask_of(cpu), i); 124447be2479SWanlong Gao i++; 1245986a4f4dSJason Wang } 1246986a4f4dSJason Wang 1247986a4f4dSJason Wang vi->affinity_hint_set = true; 124847be2479SWanlong Gao } 1249986a4f4dSJason Wang 12508017c279SSebastian Andrzej Siewior static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node) 12518de4b2f3SWanlong Gao { 12528017c279SSebastian Andrzej Siewior struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, 12538017c279SSebastian Andrzej Siewior node); 12548de4b2f3SWanlong Gao virtnet_set_affinity(vi); 12558017c279SSebastian Andrzej Siewior return 0; 12568de4b2f3SWanlong Gao } 12573ab098dfSJason Wang 12588017c279SSebastian Andrzej Siewior static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node) 12598017c279SSebastian Andrzej Siewior { 12608017c279SSebastian Andrzej Siewior struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, 12618017c279SSebastian Andrzej Siewior node_dead); 12628017c279SSebastian Andrzej Siewior virtnet_set_affinity(vi); 12638017c279SSebastian Andrzej Siewior return 0; 12648017c279SSebastian Andrzej Siewior } 12658017c279SSebastian Andrzej Siewior 12668017c279SSebastian Andrzej Siewior static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node) 12678017c279SSebastian Andrzej Siewior { 12688017c279SSebastian Andrzej Siewior struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info, 12698017c279SSebastian Andrzej Siewior node); 12708017c279SSebastian Andrzej Siewior 12718017c279SSebastian Andrzej Siewior virtnet_clean_affinity(vi, cpu); 12728017c279SSebastian Andrzej Siewior return 0; 12738017c279SSebastian Andrzej Siewior } 12748017c279SSebastian Andrzej Siewior 12758017c279SSebastian Andrzej Siewior static enum cpuhp_state virtionet_online; 12768017c279SSebastian Andrzej Siewior 12778017c279SSebastian Andrzej Siewior static int virtnet_cpu_notif_add(struct virtnet_info *vi) 12788017c279SSebastian Andrzej Siewior { 12798017c279SSebastian Andrzej Siewior int ret; 12808017c279SSebastian Andrzej Siewior 12818017c279SSebastian Andrzej Siewior ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node); 12828017c279SSebastian Andrzej Siewior if (ret) 12838017c279SSebastian Andrzej Siewior return ret; 12848017c279SSebastian Andrzej Siewior ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD, 12858017c279SSebastian Andrzej Siewior &vi->node_dead); 12868017c279SSebastian Andrzej Siewior if (!ret) 12878017c279SSebastian Andrzej Siewior return ret; 12888017c279SSebastian Andrzej Siewior cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node); 12898017c279SSebastian Andrzej Siewior return ret; 12908017c279SSebastian Andrzej Siewior } 12918017c279SSebastian Andrzej Siewior 12928017c279SSebastian Andrzej Siewior static void virtnet_cpu_notif_remove(struct virtnet_info *vi) 12938017c279SSebastian Andrzej Siewior { 12948017c279SSebastian Andrzej Siewior cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node); 12958017c279SSebastian Andrzej Siewior cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD, 12968017c279SSebastian Andrzej Siewior &vi->node_dead); 1297a9ea3fc6SHerbert Xu } 1298a9ea3fc6SHerbert Xu 12998f9f4668SRick Jones static void virtnet_get_ringparam(struct net_device *dev, 13008f9f4668SRick Jones struct ethtool_ringparam *ring) 13018f9f4668SRick Jones { 13028f9f4668SRick Jones struct virtnet_info *vi = netdev_priv(dev); 13038f9f4668SRick Jones 1304986a4f4dSJason Wang ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq); 1305986a4f4dSJason Wang ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq); 13068f9f4668SRick Jones ring->rx_pending = ring->rx_max_pending; 13078f9f4668SRick Jones ring->tx_pending = ring->tx_max_pending; 13088f9f4668SRick Jones } 13098f9f4668SRick Jones 131066846048SRick Jones 131166846048SRick Jones static void virtnet_get_drvinfo(struct net_device *dev, 131266846048SRick Jones struct ethtool_drvinfo *info) 131366846048SRick Jones { 131466846048SRick Jones struct virtnet_info *vi = netdev_priv(dev); 131566846048SRick Jones struct virtio_device *vdev = vi->vdev; 131666846048SRick Jones 131766846048SRick Jones strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver)); 131866846048SRick Jones strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version)); 131966846048SRick Jones strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info)); 132066846048SRick Jones 132166846048SRick Jones } 132266846048SRick Jones 1323d73bcd2cSJason Wang /* TODO: Eliminate OOO packets during switching */ 1324d73bcd2cSJason Wang static int virtnet_set_channels(struct net_device *dev, 1325d73bcd2cSJason Wang struct ethtool_channels *channels) 1326d73bcd2cSJason Wang { 1327d73bcd2cSJason Wang struct virtnet_info *vi = netdev_priv(dev); 1328d73bcd2cSJason Wang u16 queue_pairs = channels->combined_count; 1329d73bcd2cSJason Wang int err; 1330d73bcd2cSJason Wang 1331d73bcd2cSJason Wang /* We don't support separate rx/tx channels. 1332d73bcd2cSJason Wang * We don't allow setting 'other' channels. 1333d73bcd2cSJason Wang */ 1334d73bcd2cSJason Wang if (channels->rx_count || channels->tx_count || channels->other_count) 1335d73bcd2cSJason Wang return -EINVAL; 1336d73bcd2cSJason Wang 1337c18e9cd6SAmos Kong if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0) 1338d73bcd2cSJason Wang return -EINVAL; 1339d73bcd2cSJason Wang 134047be2479SWanlong Gao get_online_cpus(); 1341d73bcd2cSJason Wang err = virtnet_set_queues(vi, queue_pairs); 1342d73bcd2cSJason Wang if (!err) { 1343d73bcd2cSJason Wang netif_set_real_num_tx_queues(dev, queue_pairs); 1344d73bcd2cSJason Wang netif_set_real_num_rx_queues(dev, queue_pairs); 1345d73bcd2cSJason Wang 13468898c21cSWanlong Gao virtnet_set_affinity(vi); 1347d73bcd2cSJason Wang } 134847be2479SWanlong Gao put_online_cpus(); 1349d73bcd2cSJason Wang 1350d73bcd2cSJason Wang return err; 1351d73bcd2cSJason Wang } 1352d73bcd2cSJason Wang 1353d73bcd2cSJason Wang static void virtnet_get_channels(struct net_device *dev, 1354d73bcd2cSJason Wang struct ethtool_channels *channels) 1355d73bcd2cSJason Wang { 1356d73bcd2cSJason Wang struct virtnet_info *vi = netdev_priv(dev); 1357d73bcd2cSJason Wang 1358d73bcd2cSJason Wang channels->combined_count = vi->curr_queue_pairs; 1359d73bcd2cSJason Wang channels->max_combined = vi->max_queue_pairs; 1360d73bcd2cSJason Wang channels->max_other = 0; 1361d73bcd2cSJason Wang channels->rx_count = 0; 1362d73bcd2cSJason Wang channels->tx_count = 0; 1363d73bcd2cSJason Wang channels->other_count = 0; 1364d73bcd2cSJason Wang } 1365d73bcd2cSJason Wang 136616032be5SNikolay Aleksandrov /* Check if the user is trying to change anything besides speed/duplex */ 136716032be5SNikolay Aleksandrov static bool virtnet_validate_ethtool_cmd(const struct ethtool_cmd *cmd) 136816032be5SNikolay Aleksandrov { 136916032be5SNikolay Aleksandrov struct ethtool_cmd diff1 = *cmd; 137016032be5SNikolay Aleksandrov struct ethtool_cmd diff2 = {}; 137116032be5SNikolay Aleksandrov 13720cf3ace9SNikolay Aleksandrov /* cmd is always set so we need to clear it, validate the port type 13730cf3ace9SNikolay Aleksandrov * and also without autonegotiation we can ignore advertising 13740cf3ace9SNikolay Aleksandrov */ 137516032be5SNikolay Aleksandrov ethtool_cmd_speed_set(&diff1, 0); 13760cf3ace9SNikolay Aleksandrov diff2.port = PORT_OTHER; 137716032be5SNikolay Aleksandrov diff1.advertising = 0; 137816032be5SNikolay Aleksandrov diff1.duplex = 0; 137916032be5SNikolay Aleksandrov diff1.cmd = 0; 138016032be5SNikolay Aleksandrov 138116032be5SNikolay Aleksandrov return !memcmp(&diff1, &diff2, sizeof(diff1)); 138216032be5SNikolay Aleksandrov } 138316032be5SNikolay Aleksandrov 138416032be5SNikolay Aleksandrov static int virtnet_set_settings(struct net_device *dev, struct ethtool_cmd *cmd) 138516032be5SNikolay Aleksandrov { 138616032be5SNikolay Aleksandrov struct virtnet_info *vi = netdev_priv(dev); 138716032be5SNikolay Aleksandrov u32 speed; 138816032be5SNikolay Aleksandrov 138916032be5SNikolay Aleksandrov speed = ethtool_cmd_speed(cmd); 139016032be5SNikolay Aleksandrov /* don't allow custom speed and duplex */ 139116032be5SNikolay Aleksandrov if (!ethtool_validate_speed(speed) || 139216032be5SNikolay Aleksandrov !ethtool_validate_duplex(cmd->duplex) || 139316032be5SNikolay Aleksandrov !virtnet_validate_ethtool_cmd(cmd)) 139416032be5SNikolay Aleksandrov return -EINVAL; 139516032be5SNikolay Aleksandrov vi->speed = speed; 139616032be5SNikolay Aleksandrov vi->duplex = cmd->duplex; 139716032be5SNikolay Aleksandrov 139816032be5SNikolay Aleksandrov return 0; 139916032be5SNikolay Aleksandrov } 140016032be5SNikolay Aleksandrov 140116032be5SNikolay Aleksandrov static int virtnet_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) 140216032be5SNikolay Aleksandrov { 140316032be5SNikolay Aleksandrov struct virtnet_info *vi = netdev_priv(dev); 140416032be5SNikolay Aleksandrov 140516032be5SNikolay Aleksandrov ethtool_cmd_speed_set(cmd, vi->speed); 140616032be5SNikolay Aleksandrov cmd->duplex = vi->duplex; 140716032be5SNikolay Aleksandrov cmd->port = PORT_OTHER; 140816032be5SNikolay Aleksandrov 140916032be5SNikolay Aleksandrov return 0; 141016032be5SNikolay Aleksandrov } 141116032be5SNikolay Aleksandrov 141216032be5SNikolay Aleksandrov static void virtnet_init_settings(struct net_device *dev) 141316032be5SNikolay Aleksandrov { 141416032be5SNikolay Aleksandrov struct virtnet_info *vi = netdev_priv(dev); 141516032be5SNikolay Aleksandrov 141616032be5SNikolay Aleksandrov vi->speed = SPEED_UNKNOWN; 141716032be5SNikolay Aleksandrov vi->duplex = DUPLEX_UNKNOWN; 141816032be5SNikolay Aleksandrov } 141916032be5SNikolay Aleksandrov 14200fc0b732SStephen Hemminger static const struct ethtool_ops virtnet_ethtool_ops = { 142166846048SRick Jones .get_drvinfo = virtnet_get_drvinfo, 14229f4d26d0SMark McLoughlin .get_link = ethtool_op_get_link, 14238f9f4668SRick Jones .get_ringparam = virtnet_get_ringparam, 1424d73bcd2cSJason Wang .set_channels = virtnet_set_channels, 1425d73bcd2cSJason Wang .get_channels = virtnet_get_channels, 1426074c3582SJacob Keller .get_ts_info = ethtool_op_get_ts_info, 142716032be5SNikolay Aleksandrov .get_settings = virtnet_get_settings, 142816032be5SNikolay Aleksandrov .set_settings = virtnet_set_settings, 1429a9ea3fc6SHerbert Xu }; 1430a9ea3fc6SHerbert Xu 143139da5814SMark McLoughlin #define MIN_MTU 68 143239da5814SMark McLoughlin #define MAX_MTU 65535 143339da5814SMark McLoughlin 143439da5814SMark McLoughlin static int virtnet_change_mtu(struct net_device *dev, int new_mtu) 143539da5814SMark McLoughlin { 143639da5814SMark McLoughlin if (new_mtu < MIN_MTU || new_mtu > MAX_MTU) 143739da5814SMark McLoughlin return -EINVAL; 143839da5814SMark McLoughlin dev->mtu = new_mtu; 143939da5814SMark McLoughlin return 0; 144039da5814SMark McLoughlin } 144139da5814SMark McLoughlin 144276288b4eSStephen Hemminger static const struct net_device_ops virtnet_netdev = { 144376288b4eSStephen Hemminger .ndo_open = virtnet_open, 144476288b4eSStephen Hemminger .ndo_stop = virtnet_close, 144576288b4eSStephen Hemminger .ndo_start_xmit = start_xmit, 144676288b4eSStephen Hemminger .ndo_validate_addr = eth_validate_addr, 14479c46f6d4SAlex Williamson .ndo_set_mac_address = virtnet_set_mac_address, 14482af7698eSAlex Williamson .ndo_set_rx_mode = virtnet_set_rx_mode, 144976288b4eSStephen Hemminger .ndo_change_mtu = virtnet_change_mtu, 14503fa2a1dfSstephen hemminger .ndo_get_stats64 = virtnet_stats, 14511824a989SAlex Williamson .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid, 14521824a989SAlex Williamson .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid, 145376288b4eSStephen Hemminger #ifdef CONFIG_NET_POLL_CONTROLLER 145476288b4eSStephen Hemminger .ndo_poll_controller = virtnet_netpoll, 145576288b4eSStephen Hemminger #endif 145691815639SJason Wang #ifdef CONFIG_NET_RX_BUSY_POLL 145791815639SJason Wang .ndo_busy_poll = virtnet_busy_poll, 145891815639SJason Wang #endif 145976288b4eSStephen Hemminger }; 146076288b4eSStephen Hemminger 1461586d17c5SJason Wang static void virtnet_config_changed_work(struct work_struct *work) 14629f4d26d0SMark McLoughlin { 1463586d17c5SJason Wang struct virtnet_info *vi = 1464586d17c5SJason Wang container_of(work, struct virtnet_info, config_work); 14659f4d26d0SMark McLoughlin u16 v; 14669f4d26d0SMark McLoughlin 1467855e0c52SRusty Russell if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS, 1468855e0c52SRusty Russell struct virtio_net_config, status, &v) < 0) 1469507613bfSMichael S. Tsirkin return; 1470586d17c5SJason Wang 1471586d17c5SJason Wang if (v & VIRTIO_NET_S_ANNOUNCE) { 1472ee89bab1SAmerigo Wang netdev_notify_peers(vi->dev); 1473586d17c5SJason Wang virtnet_ack_link_announce(vi); 1474586d17c5SJason Wang } 14759f4d26d0SMark McLoughlin 14769f4d26d0SMark McLoughlin /* Ignore unknown (future) status bits */ 14779f4d26d0SMark McLoughlin v &= VIRTIO_NET_S_LINK_UP; 14789f4d26d0SMark McLoughlin 14799f4d26d0SMark McLoughlin if (vi->status == v) 1480507613bfSMichael S. Tsirkin return; 14819f4d26d0SMark McLoughlin 14829f4d26d0SMark McLoughlin vi->status = v; 14839f4d26d0SMark McLoughlin 14849f4d26d0SMark McLoughlin if (vi->status & VIRTIO_NET_S_LINK_UP) { 14859f4d26d0SMark McLoughlin netif_carrier_on(vi->dev); 1486986a4f4dSJason Wang netif_tx_wake_all_queues(vi->dev); 14879f4d26d0SMark McLoughlin } else { 14889f4d26d0SMark McLoughlin netif_carrier_off(vi->dev); 1489986a4f4dSJason Wang netif_tx_stop_all_queues(vi->dev); 14909f4d26d0SMark McLoughlin } 14919f4d26d0SMark McLoughlin } 14929f4d26d0SMark McLoughlin 14939f4d26d0SMark McLoughlin static void virtnet_config_changed(struct virtio_device *vdev) 14949f4d26d0SMark McLoughlin { 14959f4d26d0SMark McLoughlin struct virtnet_info *vi = vdev->priv; 14969f4d26d0SMark McLoughlin 14973b07e9caSTejun Heo schedule_work(&vi->config_work); 14989f4d26d0SMark McLoughlin } 14999f4d26d0SMark McLoughlin 1500986a4f4dSJason Wang static void virtnet_free_queues(struct virtnet_info *vi) 1501986a4f4dSJason Wang { 1502d4fb84eeSAndrey Vagin int i; 1503d4fb84eeSAndrey Vagin 1504ab3971b1SJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1505ab3971b1SJason Wang napi_hash_del(&vi->rq[i].napi); 1506d4fb84eeSAndrey Vagin netif_napi_del(&vi->rq[i].napi); 1507ab3971b1SJason Wang } 1508d4fb84eeSAndrey Vagin 1509963abe5cSEric Dumazet /* We called napi_hash_del() before netif_napi_del(), 1510963abe5cSEric Dumazet * we need to respect an RCU grace period before freeing vi->rq 1511963abe5cSEric Dumazet */ 1512963abe5cSEric Dumazet synchronize_net(); 1513963abe5cSEric Dumazet 1514986a4f4dSJason Wang kfree(vi->rq); 1515986a4f4dSJason Wang kfree(vi->sq); 1516986a4f4dSJason Wang } 1517986a4f4dSJason Wang 1518986a4f4dSJason Wang static void free_receive_bufs(struct virtnet_info *vi) 1519986a4f4dSJason Wang { 1520986a4f4dSJason Wang int i; 1521986a4f4dSJason Wang 1522986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1523986a4f4dSJason Wang while (vi->rq[i].pages) 1524986a4f4dSJason Wang __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0); 1525986a4f4dSJason Wang } 1526986a4f4dSJason Wang } 1527986a4f4dSJason Wang 1528fb51879dSMichael Dalton static void free_receive_page_frags(struct virtnet_info *vi) 1529fb51879dSMichael Dalton { 1530fb51879dSMichael Dalton int i; 1531fb51879dSMichael Dalton for (i = 0; i < vi->max_queue_pairs; i++) 1532fb51879dSMichael Dalton if (vi->rq[i].alloc_frag.page) 1533fb51879dSMichael Dalton put_page(vi->rq[i].alloc_frag.page); 1534fb51879dSMichael Dalton } 1535fb51879dSMichael Dalton 1536986a4f4dSJason Wang static void free_unused_bufs(struct virtnet_info *vi) 1537986a4f4dSJason Wang { 1538986a4f4dSJason Wang void *buf; 1539986a4f4dSJason Wang int i; 1540986a4f4dSJason Wang 1541986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1542986a4f4dSJason Wang struct virtqueue *vq = vi->sq[i].vq; 1543986a4f4dSJason Wang while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) 1544986a4f4dSJason Wang dev_kfree_skb(buf); 1545986a4f4dSJason Wang } 1546986a4f4dSJason Wang 1547986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1548986a4f4dSJason Wang struct virtqueue *vq = vi->rq[i].vq; 1549986a4f4dSJason Wang 1550986a4f4dSJason Wang while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) { 1551ab7db917SMichael Dalton if (vi->mergeable_rx_bufs) { 1552ab7db917SMichael Dalton unsigned long ctx = (unsigned long)buf; 1553ab7db917SMichael Dalton void *base = mergeable_ctx_to_buf_address(ctx); 1554ab7db917SMichael Dalton put_page(virt_to_head_page(base)); 1555ab7db917SMichael Dalton } else if (vi->big_packets) { 1556fa9fac17SAndrey Vagin give_pages(&vi->rq[i], buf); 1557ab7db917SMichael Dalton } else { 1558986a4f4dSJason Wang dev_kfree_skb(buf); 1559986a4f4dSJason Wang } 1560986a4f4dSJason Wang } 1561986a4f4dSJason Wang } 1562ab7db917SMichael Dalton } 1563986a4f4dSJason Wang 1564e9d7417bSJason Wang static void virtnet_del_vqs(struct virtnet_info *vi) 1565e9d7417bSJason Wang { 1566e9d7417bSJason Wang struct virtio_device *vdev = vi->vdev; 1567e9d7417bSJason Wang 15688898c21cSWanlong Gao virtnet_clean_affinity(vi, -1); 1569986a4f4dSJason Wang 1570e9d7417bSJason Wang vdev->config->del_vqs(vdev); 1571986a4f4dSJason Wang 1572986a4f4dSJason Wang virtnet_free_queues(vi); 1573986a4f4dSJason Wang } 1574986a4f4dSJason Wang 1575986a4f4dSJason Wang static int virtnet_find_vqs(struct virtnet_info *vi) 1576986a4f4dSJason Wang { 1577986a4f4dSJason Wang vq_callback_t **callbacks; 1578986a4f4dSJason Wang struct virtqueue **vqs; 1579986a4f4dSJason Wang int ret = -ENOMEM; 1580986a4f4dSJason Wang int i, total_vqs; 1581986a4f4dSJason Wang const char **names; 1582986a4f4dSJason Wang 1583986a4f4dSJason Wang /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by 1584986a4f4dSJason Wang * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by 1585986a4f4dSJason Wang * possible control vq. 1586986a4f4dSJason Wang */ 1587986a4f4dSJason Wang total_vqs = vi->max_queue_pairs * 2 + 1588986a4f4dSJason Wang virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ); 1589986a4f4dSJason Wang 1590986a4f4dSJason Wang /* Allocate space for find_vqs parameters */ 1591986a4f4dSJason Wang vqs = kzalloc(total_vqs * sizeof(*vqs), GFP_KERNEL); 1592986a4f4dSJason Wang if (!vqs) 1593986a4f4dSJason Wang goto err_vq; 1594986a4f4dSJason Wang callbacks = kmalloc(total_vqs * sizeof(*callbacks), GFP_KERNEL); 1595986a4f4dSJason Wang if (!callbacks) 1596986a4f4dSJason Wang goto err_callback; 1597986a4f4dSJason Wang names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL); 1598986a4f4dSJason Wang if (!names) 1599986a4f4dSJason Wang goto err_names; 1600986a4f4dSJason Wang 1601986a4f4dSJason Wang /* Parameters for control virtqueue, if any */ 1602986a4f4dSJason Wang if (vi->has_cvq) { 1603986a4f4dSJason Wang callbacks[total_vqs - 1] = NULL; 1604986a4f4dSJason Wang names[total_vqs - 1] = "control"; 1605986a4f4dSJason Wang } 1606986a4f4dSJason Wang 1607986a4f4dSJason Wang /* Allocate/initialize parameters for send/receive virtqueues */ 1608986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1609986a4f4dSJason Wang callbacks[rxq2vq(i)] = skb_recv_done; 1610986a4f4dSJason Wang callbacks[txq2vq(i)] = skb_xmit_done; 1611986a4f4dSJason Wang sprintf(vi->rq[i].name, "input.%d", i); 1612986a4f4dSJason Wang sprintf(vi->sq[i].name, "output.%d", i); 1613986a4f4dSJason Wang names[rxq2vq(i)] = vi->rq[i].name; 1614986a4f4dSJason Wang names[txq2vq(i)] = vi->sq[i].name; 1615986a4f4dSJason Wang } 1616986a4f4dSJason Wang 1617986a4f4dSJason Wang ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks, 1618986a4f4dSJason Wang names); 1619986a4f4dSJason Wang if (ret) 1620986a4f4dSJason Wang goto err_find; 1621986a4f4dSJason Wang 1622986a4f4dSJason Wang if (vi->has_cvq) { 1623986a4f4dSJason Wang vi->cvq = vqs[total_vqs - 1]; 1624986a4f4dSJason Wang if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN)) 1625f646968fSPatrick McHardy vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER; 1626986a4f4dSJason Wang } 1627986a4f4dSJason Wang 1628986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1629986a4f4dSJason Wang vi->rq[i].vq = vqs[rxq2vq(i)]; 1630986a4f4dSJason Wang vi->sq[i].vq = vqs[txq2vq(i)]; 1631986a4f4dSJason Wang } 1632986a4f4dSJason Wang 1633986a4f4dSJason Wang kfree(names); 1634986a4f4dSJason Wang kfree(callbacks); 1635986a4f4dSJason Wang kfree(vqs); 1636986a4f4dSJason Wang 1637986a4f4dSJason Wang return 0; 1638986a4f4dSJason Wang 1639986a4f4dSJason Wang err_find: 1640986a4f4dSJason Wang kfree(names); 1641986a4f4dSJason Wang err_names: 1642986a4f4dSJason Wang kfree(callbacks); 1643986a4f4dSJason Wang err_callback: 1644986a4f4dSJason Wang kfree(vqs); 1645986a4f4dSJason Wang err_vq: 1646986a4f4dSJason Wang return ret; 1647986a4f4dSJason Wang } 1648986a4f4dSJason Wang 1649986a4f4dSJason Wang static int virtnet_alloc_queues(struct virtnet_info *vi) 1650986a4f4dSJason Wang { 1651986a4f4dSJason Wang int i; 1652986a4f4dSJason Wang 1653986a4f4dSJason Wang vi->sq = kzalloc(sizeof(*vi->sq) * vi->max_queue_pairs, GFP_KERNEL); 1654986a4f4dSJason Wang if (!vi->sq) 1655986a4f4dSJason Wang goto err_sq; 1656986a4f4dSJason Wang vi->rq = kzalloc(sizeof(*vi->rq) * vi->max_queue_pairs, GFP_KERNEL); 1657008d4278SAmerigo Wang if (!vi->rq) 1658986a4f4dSJason Wang goto err_rq; 1659986a4f4dSJason Wang 1660986a4f4dSJason Wang INIT_DELAYED_WORK(&vi->refill, refill_work); 1661986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1662986a4f4dSJason Wang vi->rq[i].pages = NULL; 1663986a4f4dSJason Wang netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll, 1664986a4f4dSJason Wang napi_weight); 1665986a4f4dSJason Wang 1666986a4f4dSJason Wang sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg)); 16675377d758SJohannes Berg ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len); 1668986a4f4dSJason Wang sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg)); 1669986a4f4dSJason Wang } 1670986a4f4dSJason Wang 1671986a4f4dSJason Wang return 0; 1672986a4f4dSJason Wang 1673986a4f4dSJason Wang err_rq: 1674986a4f4dSJason Wang kfree(vi->sq); 1675986a4f4dSJason Wang err_sq: 1676986a4f4dSJason Wang return -ENOMEM; 1677e9d7417bSJason Wang } 1678e9d7417bSJason Wang 16793f9c10b0SAmit Shah static int init_vqs(struct virtnet_info *vi) 16803f9c10b0SAmit Shah { 1681986a4f4dSJason Wang int ret; 16823f9c10b0SAmit Shah 1683986a4f4dSJason Wang /* Allocate send & receive queues */ 1684986a4f4dSJason Wang ret = virtnet_alloc_queues(vi); 1685986a4f4dSJason Wang if (ret) 1686986a4f4dSJason Wang goto err; 16873f9c10b0SAmit Shah 1688986a4f4dSJason Wang ret = virtnet_find_vqs(vi); 1689986a4f4dSJason Wang if (ret) 1690986a4f4dSJason Wang goto err_free; 16913f9c10b0SAmit Shah 169247be2479SWanlong Gao get_online_cpus(); 16938898c21cSWanlong Gao virtnet_set_affinity(vi); 169447be2479SWanlong Gao put_online_cpus(); 169547be2479SWanlong Gao 16963f9c10b0SAmit Shah return 0; 1697986a4f4dSJason Wang 1698986a4f4dSJason Wang err_free: 1699986a4f4dSJason Wang virtnet_free_queues(vi); 1700986a4f4dSJason Wang err: 1701986a4f4dSJason Wang return ret; 17023f9c10b0SAmit Shah } 17033f9c10b0SAmit Shah 1704fbf28d78SMichael Dalton #ifdef CONFIG_SYSFS 1705fbf28d78SMichael Dalton static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue, 1706fbf28d78SMichael Dalton struct rx_queue_attribute *attribute, char *buf) 1707fbf28d78SMichael Dalton { 1708fbf28d78SMichael Dalton struct virtnet_info *vi = netdev_priv(queue->dev); 1709fbf28d78SMichael Dalton unsigned int queue_index = get_netdev_rx_queue_index(queue); 17105377d758SJohannes Berg struct ewma_pkt_len *avg; 1711fbf28d78SMichael Dalton 1712fbf28d78SMichael Dalton BUG_ON(queue_index >= vi->max_queue_pairs); 1713fbf28d78SMichael Dalton avg = &vi->rq[queue_index].mrg_avg_pkt_len; 1714fbf28d78SMichael Dalton return sprintf(buf, "%u\n", get_mergeable_buf_len(avg)); 1715fbf28d78SMichael Dalton } 1716fbf28d78SMichael Dalton 1717fbf28d78SMichael Dalton static struct rx_queue_attribute mergeable_rx_buffer_size_attribute = 1718fbf28d78SMichael Dalton __ATTR_RO(mergeable_rx_buffer_size); 1719fbf28d78SMichael Dalton 1720fbf28d78SMichael Dalton static struct attribute *virtio_net_mrg_rx_attrs[] = { 1721fbf28d78SMichael Dalton &mergeable_rx_buffer_size_attribute.attr, 1722fbf28d78SMichael Dalton NULL 1723fbf28d78SMichael Dalton }; 1724fbf28d78SMichael Dalton 1725fbf28d78SMichael Dalton static const struct attribute_group virtio_net_mrg_rx_group = { 1726fbf28d78SMichael Dalton .name = "virtio_net", 1727fbf28d78SMichael Dalton .attrs = virtio_net_mrg_rx_attrs 1728fbf28d78SMichael Dalton }; 1729fbf28d78SMichael Dalton #endif 1730fbf28d78SMichael Dalton 1731892d6eb1SJason Wang static bool virtnet_fail_on_feature(struct virtio_device *vdev, 1732892d6eb1SJason Wang unsigned int fbit, 1733892d6eb1SJason Wang const char *fname, const char *dname) 1734892d6eb1SJason Wang { 1735892d6eb1SJason Wang if (!virtio_has_feature(vdev, fbit)) 1736892d6eb1SJason Wang return false; 1737892d6eb1SJason Wang 1738892d6eb1SJason Wang dev_err(&vdev->dev, "device advertises feature %s but not %s", 1739892d6eb1SJason Wang fname, dname); 1740892d6eb1SJason Wang 1741892d6eb1SJason Wang return true; 1742892d6eb1SJason Wang } 1743892d6eb1SJason Wang 1744892d6eb1SJason Wang #define VIRTNET_FAIL_ON(vdev, fbit, dbit) \ 1745892d6eb1SJason Wang virtnet_fail_on_feature(vdev, fbit, #fbit, dbit) 1746892d6eb1SJason Wang 1747892d6eb1SJason Wang static bool virtnet_validate_features(struct virtio_device *vdev) 1748892d6eb1SJason Wang { 1749892d6eb1SJason Wang if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) && 1750892d6eb1SJason Wang (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX, 1751892d6eb1SJason Wang "VIRTIO_NET_F_CTRL_VQ") || 1752892d6eb1SJason Wang VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN, 1753892d6eb1SJason Wang "VIRTIO_NET_F_CTRL_VQ") || 1754892d6eb1SJason Wang VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE, 1755892d6eb1SJason Wang "VIRTIO_NET_F_CTRL_VQ") || 1756892d6eb1SJason Wang VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") || 1757892d6eb1SJason Wang VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR, 1758892d6eb1SJason Wang "VIRTIO_NET_F_CTRL_VQ"))) { 1759892d6eb1SJason Wang return false; 1760892d6eb1SJason Wang } 1761892d6eb1SJason Wang 1762892d6eb1SJason Wang return true; 1763892d6eb1SJason Wang } 1764892d6eb1SJason Wang 1765296f96fcSRusty Russell static int virtnet_probe(struct virtio_device *vdev) 1766296f96fcSRusty Russell { 1767986a4f4dSJason Wang int i, err; 1768296f96fcSRusty Russell struct net_device *dev; 1769296f96fcSRusty Russell struct virtnet_info *vi; 1770986a4f4dSJason Wang u16 max_queue_pairs; 177114de9d11SAaron Conole int mtu; 1772986a4f4dSJason Wang 17736ba42248SMichael S. Tsirkin if (!vdev->config->get) { 17746ba42248SMichael S. Tsirkin dev_err(&vdev->dev, "%s failure: config access disabled\n", 17756ba42248SMichael S. Tsirkin __func__); 17766ba42248SMichael S. Tsirkin return -EINVAL; 17776ba42248SMichael S. Tsirkin } 17786ba42248SMichael S. Tsirkin 1779892d6eb1SJason Wang if (!virtnet_validate_features(vdev)) 1780892d6eb1SJason Wang return -EINVAL; 1781892d6eb1SJason Wang 1782986a4f4dSJason Wang /* Find if host supports multiqueue virtio_net device */ 1783855e0c52SRusty Russell err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ, 1784855e0c52SRusty Russell struct virtio_net_config, 1785855e0c52SRusty Russell max_virtqueue_pairs, &max_queue_pairs); 1786986a4f4dSJason Wang 1787986a4f4dSJason Wang /* We need at least 2 queue's */ 1788986a4f4dSJason Wang if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN || 1789986a4f4dSJason Wang max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX || 1790986a4f4dSJason Wang !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) 1791986a4f4dSJason Wang max_queue_pairs = 1; 1792296f96fcSRusty Russell 1793296f96fcSRusty Russell /* Allocate ourselves a network device with room for our info */ 1794986a4f4dSJason Wang dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs); 1795296f96fcSRusty Russell if (!dev) 1796296f96fcSRusty Russell return -ENOMEM; 1797296f96fcSRusty Russell 1798296f96fcSRusty Russell /* Set up network device as normal. */ 1799f2f2c8b4SJiri Pirko dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE; 180076288b4eSStephen Hemminger dev->netdev_ops = &virtnet_netdev; 1801296f96fcSRusty Russell dev->features = NETIF_F_HIGHDMA; 18023fa2a1dfSstephen hemminger 18037ad24ea4SWilfried Klaebe dev->ethtool_ops = &virtnet_ethtool_ops; 1804296f96fcSRusty Russell SET_NETDEV_DEV(dev, &vdev->dev); 1805296f96fcSRusty Russell 1806296f96fcSRusty Russell /* Do we support "hardware" checksums? */ 180798e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) { 1808296f96fcSRusty Russell /* This opens up the world of extra features. */ 180948900cb6SJason Wang dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG; 181098e778c9SMichał Mirosław if (csum) 181148900cb6SJason Wang dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG; 181298e778c9SMichał Mirosław 181398e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) { 1814e3e3c423SVlad Yasevich dev->hw_features |= NETIF_F_TSO | NETIF_F_UFO 181534a48579SRusty Russell | NETIF_F_TSO_ECN | NETIF_F_TSO6; 181634a48579SRusty Russell } 18175539ae96SRusty Russell /* Individual feature bits: what can host handle? */ 181898e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4)) 181998e778c9SMichał Mirosław dev->hw_features |= NETIF_F_TSO; 182098e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6)) 182198e778c9SMichał Mirosław dev->hw_features |= NETIF_F_TSO6; 182298e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN)) 182398e778c9SMichał Mirosław dev->hw_features |= NETIF_F_TSO_ECN; 1824e3e3c423SVlad Yasevich if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO)) 1825e3e3c423SVlad Yasevich dev->hw_features |= NETIF_F_UFO; 182698e778c9SMichał Mirosław 182741f2f127SJason Wang dev->features |= NETIF_F_GSO_ROBUST; 182841f2f127SJason Wang 182998e778c9SMichał Mirosław if (gso) 1830e3e3c423SVlad Yasevich dev->features |= dev->hw_features & (NETIF_F_ALL_TSO|NETIF_F_UFO); 183198e778c9SMichał Mirosław /* (!csum && gso) case will be fixed by register_netdev() */ 1832296f96fcSRusty Russell } 18334f49129bSThomas Huth if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM)) 18344f49129bSThomas Huth dev->features |= NETIF_F_RXCSUM; 1835296f96fcSRusty Russell 18364fda8302SJason Wang dev->vlan_features = dev->features; 18374fda8302SJason Wang 1838296f96fcSRusty Russell /* Configuration may specify what MAC to use. Otherwise random. */ 1839855e0c52SRusty Russell if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) 1840855e0c52SRusty Russell virtio_cread_bytes(vdev, 1841a586d4f6SRusty Russell offsetof(struct virtio_net_config, mac), 1842855e0c52SRusty Russell dev->dev_addr, dev->addr_len); 1843855e0c52SRusty Russell else 1844f2cedb63SDanny Kukawka eth_hw_addr_random(dev); 1845296f96fcSRusty Russell 1846296f96fcSRusty Russell /* Set up our device-specific information */ 1847296f96fcSRusty Russell vi = netdev_priv(dev); 1848296f96fcSRusty Russell vi->dev = dev; 1849296f96fcSRusty Russell vi->vdev = vdev; 1850d9d5dcc8SChristian Borntraeger vdev->priv = vi; 18513fa2a1dfSstephen hemminger vi->stats = alloc_percpu(struct virtnet_stats); 18523fa2a1dfSstephen hemminger err = -ENOMEM; 18533fa2a1dfSstephen hemminger if (vi->stats == NULL) 18543fa2a1dfSstephen hemminger goto free; 18553fa2a1dfSstephen hemminger 1856827da44cSJohn Stultz for_each_possible_cpu(i) { 1857827da44cSJohn Stultz struct virtnet_stats *virtnet_stats; 1858827da44cSJohn Stultz virtnet_stats = per_cpu_ptr(vi->stats, i); 1859827da44cSJohn Stultz u64_stats_init(&virtnet_stats->tx_syncp); 1860827da44cSJohn Stultz u64_stats_init(&virtnet_stats->rx_syncp); 1861827da44cSJohn Stultz } 1862827da44cSJohn Stultz 1863586d17c5SJason Wang INIT_WORK(&vi->config_work, virtnet_config_changed_work); 1864296f96fcSRusty Russell 186597402b96SHerbert Xu /* If we can receive ANY GSO packets, we must allocate large ones. */ 18668e95a202SJoe Perches if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) || 18678e95a202SJoe Perches virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) || 1868e3e3c423SVlad Yasevich virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) || 1869e3e3c423SVlad Yasevich virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO)) 187097402b96SHerbert Xu vi->big_packets = true; 187197402b96SHerbert Xu 18723f2c31d9SMark McLoughlin if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF)) 18733f2c31d9SMark McLoughlin vi->mergeable_rx_bufs = true; 18743f2c31d9SMark McLoughlin 1875d04302b3SMichael S. Tsirkin if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) || 1876d04302b3SMichael S. Tsirkin virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) 1877012873d0SMichael S. Tsirkin vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf); 1878012873d0SMichael S. Tsirkin else 1879012873d0SMichael S. Tsirkin vi->hdr_len = sizeof(struct virtio_net_hdr); 1880012873d0SMichael S. Tsirkin 188175993300SMichael S. Tsirkin if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) || 188275993300SMichael S. Tsirkin virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) 1883e7428e95SMichael S. Tsirkin vi->any_header_sg = true; 1884e7428e95SMichael S. Tsirkin 1885986a4f4dSJason Wang if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) 1886986a4f4dSJason Wang vi->has_cvq = true; 1887986a4f4dSJason Wang 188814de9d11SAaron Conole if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) { 188914de9d11SAaron Conole mtu = virtio_cread16(vdev, 189014de9d11SAaron Conole offsetof(struct virtio_net_config, 189114de9d11SAaron Conole mtu)); 189214de9d11SAaron Conole if (virtnet_change_mtu(dev, mtu)) 189314de9d11SAaron Conole __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU); 189414de9d11SAaron Conole } 189514de9d11SAaron Conole 1896012873d0SMichael S. Tsirkin if (vi->any_header_sg) 1897012873d0SMichael S. Tsirkin dev->needed_headroom = vi->hdr_len; 18986ebbc1a6SZhangjie \(HZ\) 1899986a4f4dSJason Wang /* Use single tx/rx queue pair as default */ 1900986a4f4dSJason Wang vi->curr_queue_pairs = 1; 1901986a4f4dSJason Wang vi->max_queue_pairs = max_queue_pairs; 1902986a4f4dSJason Wang 1903986a4f4dSJason Wang /* Allocate/initialize the rx/tx queues, and invoke find_vqs */ 19043f9c10b0SAmit Shah err = init_vqs(vi); 1905d2a7dddaSMichael S. Tsirkin if (err) 19069bb8ca86SJason Wang goto free_stats; 1907d2a7dddaSMichael S. Tsirkin 1908fbf28d78SMichael Dalton #ifdef CONFIG_SYSFS 1909fbf28d78SMichael Dalton if (vi->mergeable_rx_bufs) 1910fbf28d78SMichael Dalton dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group; 1911fbf28d78SMichael Dalton #endif 19120f13b66bSZhi Yong Wu netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs); 19130f13b66bSZhi Yong Wu netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs); 1914986a4f4dSJason Wang 191516032be5SNikolay Aleksandrov virtnet_init_settings(dev); 191616032be5SNikolay Aleksandrov 1917296f96fcSRusty Russell err = register_netdev(dev); 1918296f96fcSRusty Russell if (err) { 1919296f96fcSRusty Russell pr_debug("virtio_net: registering device failed\n"); 1920d2a7dddaSMichael S. Tsirkin goto free_vqs; 1921296f96fcSRusty Russell } 1922b3369c1fSRusty Russell 19234baf1e33SMichael S. Tsirkin virtio_device_ready(vdev); 19244baf1e33SMichael S. Tsirkin 19258017c279SSebastian Andrzej Siewior err = virtnet_cpu_notif_add(vi); 19268de4b2f3SWanlong Gao if (err) { 19278de4b2f3SWanlong Gao pr_debug("virtio_net: registering cpu notifier failed\n"); 1928f00e35e2Swangyunjian goto free_unregister_netdev; 19298de4b2f3SWanlong Gao } 19308de4b2f3SWanlong Gao 1931167c25e4SJason Wang /* Assume link up if device can't report link status, 1932167c25e4SJason Wang otherwise get link status from config. */ 1933167c25e4SJason Wang if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) { 1934167c25e4SJason Wang netif_carrier_off(dev); 19353b07e9caSTejun Heo schedule_work(&vi->config_work); 1936167c25e4SJason Wang } else { 1937167c25e4SJason Wang vi->status = VIRTIO_NET_S_LINK_UP; 19384783256eSPantelis Koukousoulas netif_carrier_on(dev); 1939167c25e4SJason Wang } 19409f4d26d0SMark McLoughlin 1941986a4f4dSJason Wang pr_debug("virtnet: registered device %s with %d RX and TX vq's\n", 1942986a4f4dSJason Wang dev->name, max_queue_pairs); 1943986a4f4dSJason Wang 1944296f96fcSRusty Russell return 0; 1945296f96fcSRusty Russell 1946f00e35e2Swangyunjian free_unregister_netdev: 194702465555SMichael S. Tsirkin vi->vdev->config->reset(vdev); 194802465555SMichael S. Tsirkin 1949b3369c1fSRusty Russell unregister_netdev(dev); 1950d2a7dddaSMichael S. Tsirkin free_vqs: 1951986a4f4dSJason Wang cancel_delayed_work_sync(&vi->refill); 1952fb51879dSMichael Dalton free_receive_page_frags(vi); 1953e9d7417bSJason Wang virtnet_del_vqs(vi); 19543fa2a1dfSstephen hemminger free_stats: 19553fa2a1dfSstephen hemminger free_percpu(vi->stats); 1956296f96fcSRusty Russell free: 1957296f96fcSRusty Russell free_netdev(dev); 1958296f96fcSRusty Russell return err; 1959296f96fcSRusty Russell } 1960296f96fcSRusty Russell 196104486ed0SAmit Shah static void remove_vq_common(struct virtnet_info *vi) 1962296f96fcSRusty Russell { 196304486ed0SAmit Shah vi->vdev->config->reset(vi->vdev); 1964830a8a97SShirley Ma 1965830a8a97SShirley Ma /* Free unused buffers in both send and recv, if any. */ 19669ab86bbcSShirley Ma free_unused_bufs(vi); 1967fb6813f4SRusty Russell 1968986a4f4dSJason Wang free_receive_bufs(vi); 1969d2a7dddaSMichael S. Tsirkin 1970fb51879dSMichael Dalton free_receive_page_frags(vi); 1971fb51879dSMichael Dalton 1972986a4f4dSJason Wang virtnet_del_vqs(vi); 197304486ed0SAmit Shah } 197404486ed0SAmit Shah 19758cc085d6SBill Pemberton static void virtnet_remove(struct virtio_device *vdev) 197604486ed0SAmit Shah { 197704486ed0SAmit Shah struct virtnet_info *vi = vdev->priv; 197804486ed0SAmit Shah 19798017c279SSebastian Andrzej Siewior virtnet_cpu_notif_remove(vi); 19808de4b2f3SWanlong Gao 1981102a2786SMichael S. Tsirkin /* Make sure no work handler is accessing the device. */ 1982102a2786SMichael S. Tsirkin flush_work(&vi->config_work); 1983586d17c5SJason Wang 198404486ed0SAmit Shah unregister_netdev(vi->dev); 198504486ed0SAmit Shah 198604486ed0SAmit Shah remove_vq_common(vi); 1987fb6813f4SRusty Russell 19882e66f55bSKrishna Kumar free_percpu(vi->stats); 198974b2553fSRusty Russell free_netdev(vi->dev); 1990296f96fcSRusty Russell } 1991296f96fcSRusty Russell 199289107000SAaron Lu #ifdef CONFIG_PM_SLEEP 19930741bcb5SAmit Shah static int virtnet_freeze(struct virtio_device *vdev) 19940741bcb5SAmit Shah { 19950741bcb5SAmit Shah struct virtnet_info *vi = vdev->priv; 1996986a4f4dSJason Wang int i; 19970741bcb5SAmit Shah 19988017c279SSebastian Andrzej Siewior virtnet_cpu_notif_remove(vi); 1999ec9debbdSJason Wang 2000102a2786SMichael S. Tsirkin /* Make sure no work handler is accessing the device */ 2001102a2786SMichael S. Tsirkin flush_work(&vi->config_work); 2002586d17c5SJason Wang 20030741bcb5SAmit Shah netif_device_detach(vi->dev); 20040741bcb5SAmit Shah cancel_delayed_work_sync(&vi->refill); 20050741bcb5SAmit Shah 200691815639SJason Wang if (netif_running(vi->dev)) { 2007ab3971b1SJason Wang for (i = 0; i < vi->max_queue_pairs; i++) 2008986a4f4dSJason Wang napi_disable(&vi->rq[i].napi); 200991815639SJason Wang } 20100741bcb5SAmit Shah 20110741bcb5SAmit Shah remove_vq_common(vi); 20120741bcb5SAmit Shah 20130741bcb5SAmit Shah return 0; 20140741bcb5SAmit Shah } 20150741bcb5SAmit Shah 20160741bcb5SAmit Shah static int virtnet_restore(struct virtio_device *vdev) 20170741bcb5SAmit Shah { 20180741bcb5SAmit Shah struct virtnet_info *vi = vdev->priv; 2019986a4f4dSJason Wang int err, i; 20200741bcb5SAmit Shah 20210741bcb5SAmit Shah err = init_vqs(vi); 20220741bcb5SAmit Shah if (err) 20230741bcb5SAmit Shah return err; 20240741bcb5SAmit Shah 2025e53fbd11SMichael S. Tsirkin virtio_device_ready(vdev); 2026e53fbd11SMichael S. Tsirkin 20276cd4ce00SJason Wang if (netif_running(vi->dev)) { 202855257d72SSasha Levin for (i = 0; i < vi->curr_queue_pairs; i++) 2029946fa564SMichael S. Tsirkin if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL)) 20303b07e9caSTejun Heo schedule_delayed_work(&vi->refill, 0); 20310741bcb5SAmit Shah 20326cd4ce00SJason Wang for (i = 0; i < vi->max_queue_pairs; i++) 20336cd4ce00SJason Wang virtnet_napi_enable(&vi->rq[i]); 20346cd4ce00SJason Wang } 20356cd4ce00SJason Wang 20366cd4ce00SJason Wang netif_device_attach(vi->dev); 20376cd4ce00SJason Wang 203835ed159bSJason Wang rtnl_lock(); 2039986a4f4dSJason Wang virtnet_set_queues(vi, vi->curr_queue_pairs); 204035ed159bSJason Wang rtnl_unlock(); 2041986a4f4dSJason Wang 20428017c279SSebastian Andrzej Siewior err = virtnet_cpu_notif_add(vi); 2043ec9debbdSJason Wang if (err) 2044ec9debbdSJason Wang return err; 2045ec9debbdSJason Wang 20460741bcb5SAmit Shah return 0; 20470741bcb5SAmit Shah } 20480741bcb5SAmit Shah #endif 20490741bcb5SAmit Shah 2050296f96fcSRusty Russell static struct virtio_device_id id_table[] = { 2051296f96fcSRusty Russell { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID }, 2052296f96fcSRusty Russell { 0 }, 2053296f96fcSRusty Russell }; 2054296f96fcSRusty Russell 2055f3358507SMichael S. Tsirkin #define VIRTNET_FEATURES \ 2056f3358507SMichael S. Tsirkin VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \ 2057f3358507SMichael S. Tsirkin VIRTIO_NET_F_MAC, \ 2058f3358507SMichael S. Tsirkin VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \ 2059f3358507SMichael S. Tsirkin VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \ 2060f3358507SMichael S. Tsirkin VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \ 2061f3358507SMichael S. Tsirkin VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \ 2062f3358507SMichael S. Tsirkin VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \ 2063f3358507SMichael S. Tsirkin VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \ 2064f3358507SMichael S. Tsirkin VIRTIO_NET_F_CTRL_MAC_ADDR, \ 2065f3358507SMichael S. Tsirkin VIRTIO_NET_F_MTU 2066f3358507SMichael S. Tsirkin 2067c45a6816SRusty Russell static unsigned int features[] = { 2068f3358507SMichael S. Tsirkin VIRTNET_FEATURES, 2069f3358507SMichael S. Tsirkin }; 2070f3358507SMichael S. Tsirkin 2071f3358507SMichael S. Tsirkin static unsigned int features_legacy[] = { 2072f3358507SMichael S. Tsirkin VIRTNET_FEATURES, 2073f3358507SMichael S. Tsirkin VIRTIO_NET_F_GSO, 2074e7428e95SMichael S. Tsirkin VIRTIO_F_ANY_LAYOUT, 2075c45a6816SRusty Russell }; 2076c45a6816SRusty Russell 207722402529SUwe Kleine-König static struct virtio_driver virtio_net_driver = { 2078c45a6816SRusty Russell .feature_table = features, 2079c45a6816SRusty Russell .feature_table_size = ARRAY_SIZE(features), 2080f3358507SMichael S. Tsirkin .feature_table_legacy = features_legacy, 2081f3358507SMichael S. Tsirkin .feature_table_size_legacy = ARRAY_SIZE(features_legacy), 2082296f96fcSRusty Russell .driver.name = KBUILD_MODNAME, 2083296f96fcSRusty Russell .driver.owner = THIS_MODULE, 2084296f96fcSRusty Russell .id_table = id_table, 2085296f96fcSRusty Russell .probe = virtnet_probe, 20868cc085d6SBill Pemberton .remove = virtnet_remove, 20879f4d26d0SMark McLoughlin .config_changed = virtnet_config_changed, 208889107000SAaron Lu #ifdef CONFIG_PM_SLEEP 20890741bcb5SAmit Shah .freeze = virtnet_freeze, 20900741bcb5SAmit Shah .restore = virtnet_restore, 20910741bcb5SAmit Shah #endif 2092296f96fcSRusty Russell }; 2093296f96fcSRusty Russell 20948017c279SSebastian Andrzej Siewior static __init int virtio_net_driver_init(void) 20958017c279SSebastian Andrzej Siewior { 20968017c279SSebastian Andrzej Siewior int ret; 20978017c279SSebastian Andrzej Siewior 20988017c279SSebastian Andrzej Siewior ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "AP_VIRT_NET_ONLINE", 20998017c279SSebastian Andrzej Siewior virtnet_cpu_online, 21008017c279SSebastian Andrzej Siewior virtnet_cpu_down_prep); 21018017c279SSebastian Andrzej Siewior if (ret < 0) 21028017c279SSebastian Andrzej Siewior goto out; 21038017c279SSebastian Andrzej Siewior virtionet_online = ret; 21048017c279SSebastian Andrzej Siewior ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "VIRT_NET_DEAD", 21058017c279SSebastian Andrzej Siewior NULL, virtnet_cpu_dead); 21068017c279SSebastian Andrzej Siewior if (ret) 21078017c279SSebastian Andrzej Siewior goto err_dead; 21088017c279SSebastian Andrzej Siewior 21098017c279SSebastian Andrzej Siewior ret = register_virtio_driver(&virtio_net_driver); 21108017c279SSebastian Andrzej Siewior if (ret) 21118017c279SSebastian Andrzej Siewior goto err_virtio; 21128017c279SSebastian Andrzej Siewior return 0; 21138017c279SSebastian Andrzej Siewior err_virtio: 21148017c279SSebastian Andrzej Siewior cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD); 21158017c279SSebastian Andrzej Siewior err_dead: 21168017c279SSebastian Andrzej Siewior cpuhp_remove_multi_state(virtionet_online); 21178017c279SSebastian Andrzej Siewior out: 21188017c279SSebastian Andrzej Siewior return ret; 21198017c279SSebastian Andrzej Siewior } 21208017c279SSebastian Andrzej Siewior module_init(virtio_net_driver_init); 21218017c279SSebastian Andrzej Siewior 21228017c279SSebastian Andrzej Siewior static __exit void virtio_net_driver_exit(void) 21238017c279SSebastian Andrzej Siewior { 21248017c279SSebastian Andrzej Siewior cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD); 21258017c279SSebastian Andrzej Siewior cpuhp_remove_multi_state(virtionet_online); 21268017c279SSebastian Andrzej Siewior unregister_virtio_driver(&virtio_net_driver); 21278017c279SSebastian Andrzej Siewior } 21288017c279SSebastian Andrzej Siewior module_exit(virtio_net_driver_exit); 2129296f96fcSRusty Russell 2130296f96fcSRusty Russell MODULE_DEVICE_TABLE(virtio, id_table); 2131296f96fcSRusty Russell MODULE_DESCRIPTION("Virtio network driver"); 2132296f96fcSRusty Russell MODULE_LICENSE("GPL"); 2133