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 1418de4b2f3SWanlong Gao /* CPU hot plug notifier */ 1428de4b2f3SWanlong Gao struct notifier_block nb; 1432ac46030SMichael S. Tsirkin 1442ac46030SMichael S. Tsirkin /* Control VQ buffers: protected by the rtnl lock */ 1452ac46030SMichael S. Tsirkin struct virtio_net_ctrl_hdr ctrl_hdr; 1462ac46030SMichael S. Tsirkin virtio_net_ctrl_ack ctrl_status; 1472ac46030SMichael S. Tsirkin u8 ctrl_promisc; 1482ac46030SMichael S. Tsirkin u8 ctrl_allmulti; 14916032be5SNikolay Aleksandrov 15016032be5SNikolay Aleksandrov /* Ethtool settings */ 15116032be5SNikolay Aleksandrov u8 duplex; 15216032be5SNikolay Aleksandrov u32 speed; 153296f96fcSRusty Russell }; 154296f96fcSRusty Russell 1559ab86bbcSShirley Ma struct padded_vnet_hdr { 156012873d0SMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf hdr; 1579ab86bbcSShirley Ma /* 158012873d0SMichael S. Tsirkin * hdr is in a separate sg buffer, and data sg buffer shares same page 159012873d0SMichael S. Tsirkin * with this header sg. This padding makes next sg 16 byte aligned 160012873d0SMichael S. Tsirkin * after the header. 1619ab86bbcSShirley Ma */ 162012873d0SMichael S. Tsirkin char padding[4]; 1639ab86bbcSShirley Ma }; 1649ab86bbcSShirley Ma 165986a4f4dSJason Wang /* Converting between virtqueue no. and kernel tx/rx queue no. 166986a4f4dSJason Wang * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq 167986a4f4dSJason Wang */ 168986a4f4dSJason Wang static int vq2txq(struct virtqueue *vq) 169986a4f4dSJason Wang { 1709d0ca6edSRusty Russell return (vq->index - 1) / 2; 171986a4f4dSJason Wang } 172986a4f4dSJason Wang 173986a4f4dSJason Wang static int txq2vq(int txq) 174986a4f4dSJason Wang { 175986a4f4dSJason Wang return txq * 2 + 1; 176986a4f4dSJason Wang } 177986a4f4dSJason Wang 178986a4f4dSJason Wang static int vq2rxq(struct virtqueue *vq) 179986a4f4dSJason Wang { 1809d0ca6edSRusty Russell return vq->index / 2; 181986a4f4dSJason Wang } 182986a4f4dSJason Wang 183986a4f4dSJason Wang static int rxq2vq(int rxq) 184986a4f4dSJason Wang { 185986a4f4dSJason Wang return rxq * 2; 186986a4f4dSJason Wang } 187986a4f4dSJason Wang 188012873d0SMichael S. Tsirkin static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb) 189296f96fcSRusty Russell { 190012873d0SMichael S. Tsirkin return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb; 191296f96fcSRusty Russell } 192296f96fcSRusty Russell 1939ab86bbcSShirley Ma /* 1949ab86bbcSShirley Ma * private is used to chain pages for big packets, put the whole 1959ab86bbcSShirley Ma * most recent used list in the beginning for reuse 1969ab86bbcSShirley Ma */ 197e9d7417bSJason Wang static void give_pages(struct receive_queue *rq, struct page *page) 198fb6813f4SRusty Russell { 1999ab86bbcSShirley Ma struct page *end; 2009ab86bbcSShirley Ma 201e9d7417bSJason Wang /* Find end of list, sew whole thing into vi->rq.pages. */ 2029ab86bbcSShirley Ma for (end = page; end->private; end = (struct page *)end->private); 203e9d7417bSJason Wang end->private = (unsigned long)rq->pages; 204e9d7417bSJason Wang rq->pages = page; 205fb6813f4SRusty Russell } 206fb6813f4SRusty Russell 207e9d7417bSJason Wang static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask) 208fb6813f4SRusty Russell { 209e9d7417bSJason Wang struct page *p = rq->pages; 210fb6813f4SRusty Russell 2119ab86bbcSShirley Ma if (p) { 212e9d7417bSJason Wang rq->pages = (struct page *)p->private; 2139ab86bbcSShirley Ma /* clear private here, it is used to chain pages */ 2149ab86bbcSShirley Ma p->private = 0; 2159ab86bbcSShirley Ma } else 216fb6813f4SRusty Russell p = alloc_page(gfp_mask); 217fb6813f4SRusty Russell return p; 218fb6813f4SRusty Russell } 219fb6813f4SRusty Russell 220e9d7417bSJason Wang static void skb_xmit_done(struct virtqueue *vq) 221296f96fcSRusty Russell { 222e9d7417bSJason Wang struct virtnet_info *vi = vq->vdev->priv; 223296f96fcSRusty Russell 2242cb9c6baSRusty Russell /* Suppress further interrupts. */ 225e9d7417bSJason Wang virtqueue_disable_cb(vq); 22611a3a154SRusty Russell 227363f1514SRusty Russell /* We were probably waiting for more output buffers. */ 228986a4f4dSJason Wang netif_wake_subqueue(vi->dev, vq2txq(vq)); 229296f96fcSRusty Russell } 230296f96fcSRusty Russell 231ab7db917SMichael Dalton static unsigned int mergeable_ctx_to_buf_truesize(unsigned long mrg_ctx) 232ab7db917SMichael Dalton { 233ab7db917SMichael Dalton unsigned int truesize = mrg_ctx & (MERGEABLE_BUFFER_ALIGN - 1); 234ab7db917SMichael Dalton return (truesize + 1) * MERGEABLE_BUFFER_ALIGN; 235ab7db917SMichael Dalton } 236ab7db917SMichael Dalton 237ab7db917SMichael Dalton static void *mergeable_ctx_to_buf_address(unsigned long mrg_ctx) 238ab7db917SMichael Dalton { 239ab7db917SMichael Dalton return (void *)(mrg_ctx & -MERGEABLE_BUFFER_ALIGN); 240ab7db917SMichael Dalton 241ab7db917SMichael Dalton } 242ab7db917SMichael Dalton 243ab7db917SMichael Dalton static unsigned long mergeable_buf_to_ctx(void *buf, unsigned int truesize) 244ab7db917SMichael Dalton { 245ab7db917SMichael Dalton unsigned int size = truesize / MERGEABLE_BUFFER_ALIGN; 246ab7db917SMichael Dalton return (unsigned long)buf | (size - 1); 247ab7db917SMichael Dalton } 248ab7db917SMichael Dalton 2493464645aSMike Waychison /* Called from bottom half context */ 250946fa564SMichael S. Tsirkin static struct sk_buff *page_to_skb(struct virtnet_info *vi, 251946fa564SMichael S. Tsirkin struct receive_queue *rq, 2522613af0eSMichael Dalton struct page *page, unsigned int offset, 2532613af0eSMichael Dalton unsigned int len, unsigned int truesize) 2549ab86bbcSShirley Ma { 2559ab86bbcSShirley Ma struct sk_buff *skb; 256012873d0SMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf *hdr; 2572613af0eSMichael Dalton unsigned int copy, hdr_len, hdr_padded_len; 2589ab86bbcSShirley Ma char *p; 2599ab86bbcSShirley Ma 2602613af0eSMichael Dalton p = page_address(page) + offset; 2619ab86bbcSShirley Ma 2629ab86bbcSShirley Ma /* copy small packet so we can reuse these pages for small data */ 263c67f5db8SPaolo Abeni skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN); 2649ab86bbcSShirley Ma if (unlikely(!skb)) 2659ab86bbcSShirley Ma return NULL; 2669ab86bbcSShirley Ma 2679ab86bbcSShirley Ma hdr = skb_vnet_hdr(skb); 2689ab86bbcSShirley Ma 269012873d0SMichael S. Tsirkin hdr_len = vi->hdr_len; 270012873d0SMichael S. Tsirkin if (vi->mergeable_rx_bufs) 271012873d0SMichael S. Tsirkin hdr_padded_len = sizeof *hdr; 272012873d0SMichael S. Tsirkin else 2732613af0eSMichael Dalton hdr_padded_len = sizeof(struct padded_vnet_hdr); 2743f2c31d9SMark McLoughlin 2759ab86bbcSShirley Ma memcpy(hdr, p, hdr_len); 2763f2c31d9SMark McLoughlin 2779ab86bbcSShirley Ma len -= hdr_len; 2782613af0eSMichael Dalton offset += hdr_padded_len; 2792613af0eSMichael Dalton p += hdr_padded_len; 2803f2c31d9SMark McLoughlin 2813f2c31d9SMark McLoughlin copy = len; 2823f2c31d9SMark McLoughlin if (copy > skb_tailroom(skb)) 2833f2c31d9SMark McLoughlin copy = skb_tailroom(skb); 2843f2c31d9SMark McLoughlin memcpy(skb_put(skb, copy), p, copy); 2853f2c31d9SMark McLoughlin 2863f2c31d9SMark McLoughlin len -= copy; 2879ab86bbcSShirley Ma offset += copy; 2883f2c31d9SMark McLoughlin 2892613af0eSMichael Dalton if (vi->mergeable_rx_bufs) { 2902613af0eSMichael Dalton if (len) 2912613af0eSMichael Dalton skb_add_rx_frag(skb, 0, page, offset, len, truesize); 2922613af0eSMichael Dalton else 2932613af0eSMichael Dalton put_page(page); 2942613af0eSMichael Dalton return skb; 2952613af0eSMichael Dalton } 2962613af0eSMichael Dalton 297e878d78bSSasha Levin /* 298e878d78bSSasha Levin * Verify that we can indeed put this data into a skb. 299e878d78bSSasha Levin * This is here to handle cases when the device erroneously 300e878d78bSSasha Levin * tries to receive more than is possible. This is usually 301e878d78bSSasha Levin * the case of a broken device. 302e878d78bSSasha Levin */ 303e878d78bSSasha Levin if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) { 304be443899SAmerigo Wang net_dbg_ratelimited("%s: too much data\n", skb->dev->name); 305e878d78bSSasha Levin dev_kfree_skb(skb); 306e878d78bSSasha Levin return NULL; 307e878d78bSSasha Levin } 3082613af0eSMichael Dalton BUG_ON(offset >= PAGE_SIZE); 3099ab86bbcSShirley Ma while (len) { 3102613af0eSMichael Dalton unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len); 3112613af0eSMichael Dalton skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset, 3122613af0eSMichael Dalton frag_size, truesize); 3132613af0eSMichael Dalton len -= frag_size; 3149ab86bbcSShirley Ma page = (struct page *)page->private; 3159ab86bbcSShirley Ma offset = 0; 3163f2c31d9SMark McLoughlin } 3173f2c31d9SMark McLoughlin 3189ab86bbcSShirley Ma if (page) 319e9d7417bSJason Wang give_pages(rq, page); 3203f2c31d9SMark McLoughlin 3219ab86bbcSShirley Ma return skb; 3229ab86bbcSShirley Ma } 3239ab86bbcSShirley Ma 324012873d0SMichael S. Tsirkin static struct sk_buff *receive_small(struct virtnet_info *vi, void *buf, unsigned int len) 325f121159dSMichael S. Tsirkin { 326f121159dSMichael S. Tsirkin struct sk_buff * skb = buf; 327f121159dSMichael S. Tsirkin 328012873d0SMichael S. Tsirkin len -= vi->hdr_len; 329f121159dSMichael S. Tsirkin skb_trim(skb, len); 330f121159dSMichael S. Tsirkin 331f121159dSMichael S. Tsirkin return skb; 332f121159dSMichael S. Tsirkin } 333f121159dSMichael S. Tsirkin 334f121159dSMichael S. Tsirkin static struct sk_buff *receive_big(struct net_device *dev, 335946fa564SMichael S. Tsirkin struct virtnet_info *vi, 336f121159dSMichael S. Tsirkin struct receive_queue *rq, 337f121159dSMichael S. Tsirkin void *buf, 338f121159dSMichael S. Tsirkin unsigned int len) 339f121159dSMichael S. Tsirkin { 340f121159dSMichael S. Tsirkin struct page *page = buf; 341946fa564SMichael S. Tsirkin struct sk_buff *skb = page_to_skb(vi, rq, page, 0, len, PAGE_SIZE); 342f121159dSMichael S. Tsirkin 343f121159dSMichael S. Tsirkin if (unlikely(!skb)) 344f121159dSMichael S. Tsirkin goto err; 345f121159dSMichael S. Tsirkin 346f121159dSMichael S. Tsirkin return skb; 347f121159dSMichael S. Tsirkin 348f121159dSMichael S. Tsirkin err: 349f121159dSMichael S. Tsirkin dev->stats.rx_dropped++; 350f121159dSMichael S. Tsirkin give_pages(rq, page); 351f121159dSMichael S. Tsirkin return NULL; 352f121159dSMichael S. Tsirkin } 353f121159dSMichael S. Tsirkin 3548fc3b9e9SMichael S. Tsirkin static struct sk_buff *receive_mergeable(struct net_device *dev, 355fdd819b2SMichael S. Tsirkin struct virtnet_info *vi, 3568fc3b9e9SMichael S. Tsirkin struct receive_queue *rq, 357ab7db917SMichael Dalton unsigned long ctx, 3588fc3b9e9SMichael S. Tsirkin unsigned int len) 3599ab86bbcSShirley Ma { 360ab7db917SMichael Dalton void *buf = mergeable_ctx_to_buf_address(ctx); 361012873d0SMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf *hdr = buf; 362012873d0SMichael S. Tsirkin u16 num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers); 3638fc3b9e9SMichael S. Tsirkin struct page *page = virt_to_head_page(buf); 3648fc3b9e9SMichael S. Tsirkin int offset = buf - page_address(page); 365ab7db917SMichael Dalton unsigned int truesize = max(len, mergeable_ctx_to_buf_truesize(ctx)); 366ab7db917SMichael Dalton 367946fa564SMichael S. Tsirkin struct sk_buff *head_skb = page_to_skb(vi, rq, page, offset, len, 368946fa564SMichael S. Tsirkin truesize); 3692613af0eSMichael Dalton struct sk_buff *curr_skb = head_skb; 3709ab86bbcSShirley Ma 3718fc3b9e9SMichael S. Tsirkin if (unlikely(!curr_skb)) 3728fc3b9e9SMichael S. Tsirkin goto err_skb; 3739ab86bbcSShirley Ma while (--num_buf) { 3748fc3b9e9SMichael S. Tsirkin int num_skb_frags; 3758fc3b9e9SMichael S. Tsirkin 376ab7db917SMichael Dalton ctx = (unsigned long)virtqueue_get_buf(rq->vq, &len); 377ab7db917SMichael Dalton if (unlikely(!ctx)) { 3788fc3b9e9SMichael S. Tsirkin pr_debug("%s: rx error: %d buffers out of %d missing\n", 379fdd819b2SMichael S. Tsirkin dev->name, num_buf, 380012873d0SMichael S. Tsirkin virtio16_to_cpu(vi->vdev, 381012873d0SMichael S. Tsirkin hdr->num_buffers)); 3828fc3b9e9SMichael S. Tsirkin dev->stats.rx_length_errors++; 3838fc3b9e9SMichael S. Tsirkin goto err_buf; 3843f2c31d9SMark McLoughlin } 3858fc3b9e9SMichael S. Tsirkin 386ab7db917SMichael Dalton buf = mergeable_ctx_to_buf_address(ctx); 3878fc3b9e9SMichael S. Tsirkin page = virt_to_head_page(buf); 3888fc3b9e9SMichael S. Tsirkin 3898fc3b9e9SMichael S. Tsirkin num_skb_frags = skb_shinfo(curr_skb)->nr_frags; 3902613af0eSMichael Dalton if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) { 3912613af0eSMichael Dalton struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC); 3928fc3b9e9SMichael S. Tsirkin 3938fc3b9e9SMichael S. Tsirkin if (unlikely(!nskb)) 3948fc3b9e9SMichael S. Tsirkin goto err_skb; 3952613af0eSMichael Dalton if (curr_skb == head_skb) 3962613af0eSMichael Dalton skb_shinfo(curr_skb)->frag_list = nskb; 3972613af0eSMichael Dalton else 3982613af0eSMichael Dalton curr_skb->next = nskb; 3992613af0eSMichael Dalton curr_skb = nskb; 4002613af0eSMichael Dalton head_skb->truesize += nskb->truesize; 4012613af0eSMichael Dalton num_skb_frags = 0; 4022613af0eSMichael Dalton } 403ab7db917SMichael Dalton truesize = max(len, mergeable_ctx_to_buf_truesize(ctx)); 4042613af0eSMichael Dalton if (curr_skb != head_skb) { 4052613af0eSMichael Dalton head_skb->data_len += len; 4062613af0eSMichael Dalton head_skb->len += len; 407fb51879dSMichael Dalton head_skb->truesize += truesize; 4082613af0eSMichael Dalton } 4098fc3b9e9SMichael S. Tsirkin offset = buf - page_address(page); 410ba275241SJason Wang if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) { 411ba275241SJason Wang put_page(page); 412ba275241SJason Wang skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1, 413fb51879dSMichael Dalton len, truesize); 414ba275241SJason Wang } else { 4152613af0eSMichael Dalton skb_add_rx_frag(curr_skb, num_skb_frags, page, 416fb51879dSMichael Dalton offset, len, truesize); 417ba275241SJason Wang } 4188fc3b9e9SMichael S. Tsirkin } 4198fc3b9e9SMichael S. Tsirkin 4205377d758SJohannes Berg ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len); 4218fc3b9e9SMichael S. Tsirkin return head_skb; 4228fc3b9e9SMichael S. Tsirkin 4238fc3b9e9SMichael S. Tsirkin err_skb: 4248fc3b9e9SMichael S. Tsirkin put_page(page); 4258fc3b9e9SMichael S. Tsirkin while (--num_buf) { 426ab7db917SMichael Dalton ctx = (unsigned long)virtqueue_get_buf(rq->vq, &len); 427ab7db917SMichael Dalton if (unlikely(!ctx)) { 4288fc3b9e9SMichael S. Tsirkin pr_debug("%s: rx error: %d buffers missing\n", 4298fc3b9e9SMichael S. Tsirkin dev->name, num_buf); 4308fc3b9e9SMichael S. Tsirkin dev->stats.rx_length_errors++; 4318fc3b9e9SMichael S. Tsirkin break; 4328fc3b9e9SMichael S. Tsirkin } 433ab7db917SMichael Dalton page = virt_to_head_page(mergeable_ctx_to_buf_address(ctx)); 4348fc3b9e9SMichael S. Tsirkin put_page(page); 4353f2c31d9SMark McLoughlin } 4368fc3b9e9SMichael S. Tsirkin err_buf: 4378fc3b9e9SMichael S. Tsirkin dev->stats.rx_dropped++; 4388fc3b9e9SMichael S. Tsirkin dev_kfree_skb(head_skb); 4398fc3b9e9SMichael S. Tsirkin return NULL; 4409ab86bbcSShirley Ma } 4419ab86bbcSShirley Ma 442946fa564SMichael S. Tsirkin static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq, 443946fa564SMichael S. Tsirkin void *buf, unsigned int len) 4449ab86bbcSShirley Ma { 445e9d7417bSJason Wang struct net_device *dev = vi->dev; 44658472a76SEric Dumazet struct virtnet_stats *stats = this_cpu_ptr(vi->stats); 4479ab86bbcSShirley Ma struct sk_buff *skb; 448012873d0SMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf *hdr; 4499ab86bbcSShirley Ma 450bcff3162SMichael S. Tsirkin if (unlikely(len < vi->hdr_len + ETH_HLEN)) { 4519ab86bbcSShirley Ma pr_debug("%s: short packet %i\n", dev->name, len); 4529ab86bbcSShirley Ma dev->stats.rx_length_errors++; 453ab7db917SMichael Dalton if (vi->mergeable_rx_bufs) { 454ab7db917SMichael Dalton unsigned long ctx = (unsigned long)buf; 455ab7db917SMichael Dalton void *base = mergeable_ctx_to_buf_address(ctx); 456ab7db917SMichael Dalton put_page(virt_to_head_page(base)); 457ab7db917SMichael Dalton } else if (vi->big_packets) { 45898bfd23cSMichael Dalton give_pages(rq, buf); 459ab7db917SMichael Dalton } else { 4609ab86bbcSShirley Ma dev_kfree_skb(buf); 461ab7db917SMichael Dalton } 4629ab86bbcSShirley Ma return; 4639ab86bbcSShirley Ma } 4649ab86bbcSShirley Ma 465f121159dSMichael S. Tsirkin if (vi->mergeable_rx_bufs) 466fdd819b2SMichael S. Tsirkin skb = receive_mergeable(dev, vi, rq, (unsigned long)buf, len); 467f121159dSMichael S. Tsirkin else if (vi->big_packets) 468946fa564SMichael S. Tsirkin skb = receive_big(dev, vi, rq, buf, len); 469f121159dSMichael S. Tsirkin else 470012873d0SMichael S. Tsirkin skb = receive_small(vi, buf, len); 471f121159dSMichael S. Tsirkin 4728fc3b9e9SMichael S. Tsirkin if (unlikely(!skb)) 4732613af0eSMichael Dalton return; 4743f2c31d9SMark McLoughlin 4759ab86bbcSShirley Ma hdr = skb_vnet_hdr(skb); 4763fa2a1dfSstephen hemminger 47783a27052SEric Dumazet u64_stats_update_begin(&stats->rx_syncp); 4783fa2a1dfSstephen hemminger stats->rx_bytes += skb->len; 4793fa2a1dfSstephen hemminger stats->rx_packets++; 48083a27052SEric Dumazet u64_stats_update_end(&stats->rx_syncp); 481296f96fcSRusty Russell 482*e858fae2SMike Rapoport if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID) 48310a8d94aSJason Wang skb->ip_summed = CHECKSUM_UNNECESSARY; 484296f96fcSRusty Russell 48523cde76dSMark McLoughlin skb->protocol = eth_type_trans(skb, dev); 48623cde76dSMark McLoughlin pr_debug("Receiving skb proto 0x%04x len %i type %i\n", 48723cde76dSMark McLoughlin ntohs(skb->protocol), skb->len, skb->pkt_type); 48823cde76dSMark McLoughlin 489*e858fae2SMike Rapoport if (virtio_net_hdr_to_skb(skb, &hdr->hdr, 490*e858fae2SMike Rapoport virtio_is_little_endian(vi->vdev))) { 491*e858fae2SMike Rapoport net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n", 492*e858fae2SMike Rapoport dev->name, hdr->hdr.gso_type, 493fdd819b2SMichael S. Tsirkin hdr->hdr.gso_size); 494296f96fcSRusty Russell goto frame_err; 495296f96fcSRusty Russell } 496296f96fcSRusty Russell 4970fbd050aSEric Dumazet napi_gro_receive(&rq->napi, skb); 498296f96fcSRusty Russell return; 499296f96fcSRusty Russell 500296f96fcSRusty Russell frame_err: 501296f96fcSRusty Russell dev->stats.rx_frame_errors++; 502296f96fcSRusty Russell dev_kfree_skb(skb); 503296f96fcSRusty Russell } 504296f96fcSRusty Russell 505946fa564SMichael S. Tsirkin static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq, 506946fa564SMichael S. Tsirkin gfp_t gfp) 507296f96fcSRusty Russell { 508296f96fcSRusty Russell struct sk_buff *skb; 509012873d0SMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf *hdr; 5109ab86bbcSShirley Ma int err; 5113f2c31d9SMark McLoughlin 5125061de36SMichael Dalton skb = __netdev_alloc_skb_ip_align(vi->dev, GOOD_PACKET_LEN, gfp); 5139ab86bbcSShirley Ma if (unlikely(!skb)) 5149ab86bbcSShirley Ma return -ENOMEM; 515296f96fcSRusty Russell 5165061de36SMichael Dalton skb_put(skb, GOOD_PACKET_LEN); 5173f2c31d9SMark McLoughlin 5183f2c31d9SMark McLoughlin hdr = skb_vnet_hdr(skb); 519547c890cSJason Wang sg_init_table(rq->sg, 2); 520012873d0SMichael S. Tsirkin sg_set_buf(rq->sg, hdr, vi->hdr_len); 521e9d7417bSJason Wang skb_to_sgvec(skb, rq->sg + 1, 0, skb->len); 52297402b96SHerbert Xu 5239dc7b9e4SRusty Russell err = virtqueue_add_inbuf(rq->vq, rq->sg, 2, skb, gfp); 5249ab86bbcSShirley Ma if (err < 0) 5259ab86bbcSShirley Ma dev_kfree_skb(skb); 52697402b96SHerbert Xu 5279ab86bbcSShirley Ma return err; 52897402b96SHerbert Xu } 52997402b96SHerbert Xu 530012873d0SMichael S. Tsirkin static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq, 531012873d0SMichael S. Tsirkin gfp_t gfp) 5329ab86bbcSShirley Ma { 5339ab86bbcSShirley Ma struct page *first, *list = NULL; 5349ab86bbcSShirley Ma char *p; 5359ab86bbcSShirley Ma int i, err, offset; 536296f96fcSRusty Russell 537a5835440SRusty Russell sg_init_table(rq->sg, MAX_SKB_FRAGS + 2); 538a5835440SRusty Russell 539e9d7417bSJason Wang /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */ 5409ab86bbcSShirley Ma for (i = MAX_SKB_FRAGS + 1; i > 1; --i) { 541e9d7417bSJason Wang first = get_a_page(rq, gfp); 5429ab86bbcSShirley Ma if (!first) { 5439ab86bbcSShirley Ma if (list) 544e9d7417bSJason Wang give_pages(rq, list); 5459ab86bbcSShirley Ma return -ENOMEM; 546296f96fcSRusty Russell } 547e9d7417bSJason Wang sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE); 5489ab86bbcSShirley Ma 5499ab86bbcSShirley Ma /* chain new page in list head to match sg */ 5509ab86bbcSShirley Ma first->private = (unsigned long)list; 5519ab86bbcSShirley Ma list = first; 5529ab86bbcSShirley Ma } 5539ab86bbcSShirley Ma 554e9d7417bSJason Wang first = get_a_page(rq, gfp); 5559ab86bbcSShirley Ma if (!first) { 556e9d7417bSJason Wang give_pages(rq, list); 5579ab86bbcSShirley Ma return -ENOMEM; 5589ab86bbcSShirley Ma } 5599ab86bbcSShirley Ma p = page_address(first); 5609ab86bbcSShirley Ma 561e9d7417bSJason Wang /* rq->sg[0], rq->sg[1] share the same page */ 562012873d0SMichael S. Tsirkin /* a separated rq->sg[0] for header - required in case !any_header_sg */ 563012873d0SMichael S. Tsirkin sg_set_buf(&rq->sg[0], p, vi->hdr_len); 5649ab86bbcSShirley Ma 565e9d7417bSJason Wang /* rq->sg[1] for data packet, from offset */ 5669ab86bbcSShirley Ma offset = sizeof(struct padded_vnet_hdr); 567e9d7417bSJason Wang sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset); 5689ab86bbcSShirley Ma 5699ab86bbcSShirley Ma /* chain first in list head */ 5709ab86bbcSShirley Ma first->private = (unsigned long)list; 5719dc7b9e4SRusty Russell err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2, 572aa989f5eSMichael S. Tsirkin first, gfp); 5739ab86bbcSShirley Ma if (err < 0) 574e9d7417bSJason Wang give_pages(rq, first); 5759ab86bbcSShirley Ma 5769ab86bbcSShirley Ma return err; 5779ab86bbcSShirley Ma } 5789ab86bbcSShirley Ma 5795377d758SJohannes Berg static unsigned int get_mergeable_buf_len(struct ewma_pkt_len *avg_pkt_len) 5809ab86bbcSShirley Ma { 581ab7db917SMichael Dalton const size_t hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf); 582fbf28d78SMichael Dalton unsigned int len; 583fbf28d78SMichael Dalton 5845377d758SJohannes Berg len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len), 585fbf28d78SMichael Dalton GOOD_PACKET_LEN, PAGE_SIZE - hdr_len); 586fbf28d78SMichael Dalton return ALIGN(len, MERGEABLE_BUFFER_ALIGN); 587fbf28d78SMichael Dalton } 588fbf28d78SMichael Dalton 589fbf28d78SMichael Dalton static int add_recvbuf_mergeable(struct receive_queue *rq, gfp_t gfp) 590fbf28d78SMichael Dalton { 591fb51879dSMichael Dalton struct page_frag *alloc_frag = &rq->alloc_frag; 592fb51879dSMichael Dalton char *buf; 593ab7db917SMichael Dalton unsigned long ctx; 5949ab86bbcSShirley Ma int err; 595fb51879dSMichael Dalton unsigned int len, hole; 5969ab86bbcSShirley Ma 597fbf28d78SMichael Dalton len = get_mergeable_buf_len(&rq->mrg_avg_pkt_len); 598ab7db917SMichael Dalton if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp))) 5999ab86bbcSShirley Ma return -ENOMEM; 600ab7db917SMichael Dalton 601fb51879dSMichael Dalton buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset; 602ab7db917SMichael Dalton ctx = mergeable_buf_to_ctx(buf, len); 603fb51879dSMichael Dalton get_page(alloc_frag->page); 604fb51879dSMichael Dalton alloc_frag->offset += len; 605fb51879dSMichael Dalton hole = alloc_frag->size - alloc_frag->offset; 606ab7db917SMichael Dalton if (hole < len) { 607ab7db917SMichael Dalton /* To avoid internal fragmentation, if there is very likely not 608ab7db917SMichael Dalton * enough space for another buffer, add the remaining space to 609ab7db917SMichael Dalton * the current buffer. This extra space is not included in 610ab7db917SMichael Dalton * the truesize stored in ctx. 611ab7db917SMichael Dalton */ 612fb51879dSMichael Dalton len += hole; 613fb51879dSMichael Dalton alloc_frag->offset += hole; 614fb51879dSMichael Dalton } 6159ab86bbcSShirley Ma 616fb51879dSMichael Dalton sg_init_one(rq->sg, buf, len); 617ab7db917SMichael Dalton err = virtqueue_add_inbuf(rq->vq, rq->sg, 1, (void *)ctx, gfp); 6189ab86bbcSShirley Ma if (err < 0) 6192613af0eSMichael Dalton put_page(virt_to_head_page(buf)); 6209ab86bbcSShirley Ma 6219ab86bbcSShirley Ma return err; 622296f96fcSRusty Russell } 623296f96fcSRusty Russell 624b2baed69SRusty Russell /* 625b2baed69SRusty Russell * Returns false if we couldn't fill entirely (OOM). 626b2baed69SRusty Russell * 627b2baed69SRusty Russell * Normally run in the receive path, but can also be run from ndo_open 628b2baed69SRusty Russell * before we're receiving packets, or from refill_work which is 629b2baed69SRusty Russell * careful to disable receiving (using napi_disable). 630b2baed69SRusty Russell */ 631946fa564SMichael S. Tsirkin static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq, 632946fa564SMichael S. Tsirkin gfp_t gfp) 6333f2c31d9SMark McLoughlin { 6343f2c31d9SMark McLoughlin int err; 6351788f495SMichael S. Tsirkin bool oom; 6363f2c31d9SMark McLoughlin 637fb51879dSMichael Dalton gfp |= __GFP_COLD; 6380aea51c3SAmit Shah do { 6399ab86bbcSShirley Ma if (vi->mergeable_rx_bufs) 640e9d7417bSJason Wang err = add_recvbuf_mergeable(rq, gfp); 6419ab86bbcSShirley Ma else if (vi->big_packets) 642012873d0SMichael S. Tsirkin err = add_recvbuf_big(vi, rq, gfp); 6439ab86bbcSShirley Ma else 644946fa564SMichael S. Tsirkin err = add_recvbuf_small(vi, rq, gfp); 6453f2c31d9SMark McLoughlin 6461788f495SMichael S. Tsirkin oom = err == -ENOMEM; 6479ed4cb07SRusty Russell if (err) 6483f2c31d9SMark McLoughlin break; 649b7dfde95SLinus Torvalds } while (rq->vq->num_free); 650681daee2SJason Wang virtqueue_kick(rq->vq); 6513161e453SRusty Russell return !oom; 6523f2c31d9SMark McLoughlin } 6533f2c31d9SMark McLoughlin 65418445c4dSRusty Russell static void skb_recv_done(struct virtqueue *rvq) 655296f96fcSRusty Russell { 656296f96fcSRusty Russell struct virtnet_info *vi = rvq->vdev->priv; 657986a4f4dSJason Wang struct receive_queue *rq = &vi->rq[vq2rxq(rvq)]; 658e9d7417bSJason Wang 65918445c4dSRusty Russell /* Schedule NAPI, Suppress further interrupts if successful. */ 660e9d7417bSJason Wang if (napi_schedule_prep(&rq->napi)) { 6611915a712SMichael S. Tsirkin virtqueue_disable_cb(rvq); 662e9d7417bSJason Wang __napi_schedule(&rq->napi); 66318445c4dSRusty Russell } 664296f96fcSRusty Russell } 665296f96fcSRusty Russell 666e9d7417bSJason Wang static void virtnet_napi_enable(struct receive_queue *rq) 6673e9d08ecSBruce Rogers { 668e9d7417bSJason Wang napi_enable(&rq->napi); 6693e9d08ecSBruce Rogers 6703e9d08ecSBruce Rogers /* If all buffers were filled by other side before we napi_enabled, we 6713e9d08ecSBruce Rogers * won't get another interrupt, so process any outstanding packets 6723e9d08ecSBruce Rogers * now. virtnet_poll wants re-enable the queue, so we disable here. 6733e9d08ecSBruce Rogers * We synchronize against interrupts via NAPI_STATE_SCHED */ 674e9d7417bSJason Wang if (napi_schedule_prep(&rq->napi)) { 675e9d7417bSJason Wang virtqueue_disable_cb(rq->vq); 676ec13ee80SMichael S. Tsirkin local_bh_disable(); 677e9d7417bSJason Wang __napi_schedule(&rq->napi); 678ec13ee80SMichael S. Tsirkin local_bh_enable(); 6793e9d08ecSBruce Rogers } 6803e9d08ecSBruce Rogers } 6813e9d08ecSBruce Rogers 6823161e453SRusty Russell static void refill_work(struct work_struct *work) 6833161e453SRusty Russell { 684e9d7417bSJason Wang struct virtnet_info *vi = 685e9d7417bSJason Wang container_of(work, struct virtnet_info, refill.work); 6863161e453SRusty Russell bool still_empty; 687986a4f4dSJason Wang int i; 6883161e453SRusty Russell 68955257d72SSasha Levin for (i = 0; i < vi->curr_queue_pairs; i++) { 690986a4f4dSJason Wang struct receive_queue *rq = &vi->rq[i]; 691986a4f4dSJason Wang 692986a4f4dSJason Wang napi_disable(&rq->napi); 693946fa564SMichael S. Tsirkin still_empty = !try_fill_recv(vi, rq, GFP_KERNEL); 694986a4f4dSJason Wang virtnet_napi_enable(rq); 6953161e453SRusty Russell 6963161e453SRusty Russell /* In theory, this can happen: if we don't get any buffers in 697986a4f4dSJason Wang * we will *never* try to fill again. 698986a4f4dSJason Wang */ 6993161e453SRusty Russell if (still_empty) 7003b07e9caSTejun Heo schedule_delayed_work(&vi->refill, HZ/2); 7013161e453SRusty Russell } 702986a4f4dSJason Wang } 7033161e453SRusty Russell 7042ffa7598SJason Wang static int virtnet_receive(struct receive_queue *rq, int budget) 705296f96fcSRusty Russell { 706e9d7417bSJason Wang struct virtnet_info *vi = rq->vq->vdev->priv; 7072ffa7598SJason Wang unsigned int len, received = 0; 7089ab86bbcSShirley Ma void *buf; 709296f96fcSRusty Russell 710296f96fcSRusty Russell while (received < budget && 711e9d7417bSJason Wang (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) { 712946fa564SMichael S. Tsirkin receive_buf(vi, rq, buf, len); 713296f96fcSRusty Russell received++; 714296f96fcSRusty Russell } 715296f96fcSRusty Russell 716be121f46SJason Wang if (rq->vq->num_free > virtqueue_get_vring_size(rq->vq) / 2) { 717946fa564SMichael S. Tsirkin if (!try_fill_recv(vi, rq, GFP_ATOMIC)) 7183b07e9caSTejun Heo schedule_delayed_work(&vi->refill, 0); 7193161e453SRusty Russell } 720296f96fcSRusty Russell 7212ffa7598SJason Wang return received; 7222ffa7598SJason Wang } 7232ffa7598SJason Wang 7242ffa7598SJason Wang static int virtnet_poll(struct napi_struct *napi, int budget) 7252ffa7598SJason Wang { 7262ffa7598SJason Wang struct receive_queue *rq = 7272ffa7598SJason Wang container_of(napi, struct receive_queue, napi); 728faadb05fSLi RongQing unsigned int r, received; 7292ffa7598SJason Wang 730faadb05fSLi RongQing received = virtnet_receive(rq, budget); 7312ffa7598SJason Wang 7328329d98eSRusty Russell /* Out of packets? */ 7338329d98eSRusty Russell if (received < budget) { 734cbdadbbfSMichael S. Tsirkin r = virtqueue_enable_cb_prepare(rq->vq); 7350fbd050aSEric Dumazet napi_complete_done(napi, received); 736cbdadbbfSMichael S. Tsirkin if (unlikely(virtqueue_poll(rq->vq, r)) && 7378e95a202SJoe Perches napi_schedule_prep(napi)) { 738e9d7417bSJason Wang virtqueue_disable_cb(rq->vq); 739288379f0SBen Hutchings __napi_schedule(napi); 740296f96fcSRusty Russell } 7414265f161SChristian Borntraeger } 742296f96fcSRusty Russell 743296f96fcSRusty Russell return received; 744296f96fcSRusty Russell } 745296f96fcSRusty Russell 74691815639SJason Wang #ifdef CONFIG_NET_RX_BUSY_POLL 74791815639SJason Wang /* must be called with local_bh_disable()d */ 74891815639SJason Wang static int virtnet_busy_poll(struct napi_struct *napi) 74991815639SJason Wang { 75091815639SJason Wang struct receive_queue *rq = 75191815639SJason Wang container_of(napi, struct receive_queue, napi); 75291815639SJason Wang struct virtnet_info *vi = rq->vq->vdev->priv; 75391815639SJason Wang int r, received = 0, budget = 4; 75491815639SJason Wang 75591815639SJason Wang if (!(vi->status & VIRTIO_NET_S_LINK_UP)) 75691815639SJason Wang return LL_FLUSH_FAILED; 75791815639SJason Wang 75891815639SJason Wang if (!napi_schedule_prep(napi)) 75991815639SJason Wang return LL_FLUSH_BUSY; 76091815639SJason Wang 76191815639SJason Wang virtqueue_disable_cb(rq->vq); 76291815639SJason Wang 76391815639SJason Wang again: 76491815639SJason Wang received += virtnet_receive(rq, budget); 76591815639SJason Wang 76691815639SJason Wang r = virtqueue_enable_cb_prepare(rq->vq); 76791815639SJason Wang clear_bit(NAPI_STATE_SCHED, &napi->state); 76891815639SJason Wang if (unlikely(virtqueue_poll(rq->vq, r)) && 76991815639SJason Wang napi_schedule_prep(napi)) { 77091815639SJason Wang virtqueue_disable_cb(rq->vq); 77191815639SJason Wang if (received < budget) { 77291815639SJason Wang budget -= received; 77391815639SJason Wang goto again; 77491815639SJason Wang } else { 77591815639SJason Wang __napi_schedule(napi); 77691815639SJason Wang } 77791815639SJason Wang } 77891815639SJason Wang 77991815639SJason Wang return received; 78091815639SJason Wang } 78191815639SJason Wang #endif /* CONFIG_NET_RX_BUSY_POLL */ 78291815639SJason Wang 783986a4f4dSJason Wang static int virtnet_open(struct net_device *dev) 784986a4f4dSJason Wang { 785986a4f4dSJason Wang struct virtnet_info *vi = netdev_priv(dev); 786986a4f4dSJason Wang int i; 787986a4f4dSJason Wang 788e4166625SJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 789e4166625SJason Wang if (i < vi->curr_queue_pairs) 790986a4f4dSJason Wang /* Make sure we have some buffers: if oom use wq. */ 791946fa564SMichael S. Tsirkin if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL)) 792986a4f4dSJason Wang schedule_delayed_work(&vi->refill, 0); 793986a4f4dSJason Wang virtnet_napi_enable(&vi->rq[i]); 794986a4f4dSJason Wang } 795986a4f4dSJason Wang 796986a4f4dSJason Wang return 0; 797986a4f4dSJason Wang } 798986a4f4dSJason Wang 799b7dfde95SLinus Torvalds static void free_old_xmit_skbs(struct send_queue *sq) 800296f96fcSRusty Russell { 801296f96fcSRusty Russell struct sk_buff *skb; 8026ee57bccSMichael S. Tsirkin unsigned int len; 803e9d7417bSJason Wang struct virtnet_info *vi = sq->vq->vdev->priv; 80458472a76SEric Dumazet struct virtnet_stats *stats = this_cpu_ptr(vi->stats); 805296f96fcSRusty Russell 806e9d7417bSJason Wang while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) { 807296f96fcSRusty Russell pr_debug("Sent skb %p\n", skb); 8083fa2a1dfSstephen hemminger 80983a27052SEric Dumazet u64_stats_update_begin(&stats->tx_syncp); 8103fa2a1dfSstephen hemminger stats->tx_bytes += skb->len; 8113fa2a1dfSstephen hemminger stats->tx_packets++; 81283a27052SEric Dumazet u64_stats_update_end(&stats->tx_syncp); 8133fa2a1dfSstephen hemminger 814ed79bab8SEric Dumazet dev_kfree_skb_any(skb); 815296f96fcSRusty Russell } 816296f96fcSRusty Russell } 817296f96fcSRusty Russell 818e9d7417bSJason Wang static int xmit_skb(struct send_queue *sq, struct sk_buff *skb) 819296f96fcSRusty Russell { 820012873d0SMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf *hdr; 821296f96fcSRusty Russell const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest; 822e9d7417bSJason Wang struct virtnet_info *vi = sq->vq->vdev->priv; 8237bedc7dcSMichael S. Tsirkin unsigned num_sg; 824012873d0SMichael S. Tsirkin unsigned hdr_len = vi->hdr_len; 825e7428e95SMichael S. Tsirkin bool can_push; 826296f96fcSRusty Russell 827e174961cSJohannes Berg pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest); 828e7428e95SMichael S. Tsirkin 829e7428e95SMichael S. Tsirkin can_push = vi->any_header_sg && 830e7428e95SMichael S. Tsirkin !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) && 831e7428e95SMichael S. Tsirkin !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len; 832e7428e95SMichael S. Tsirkin /* Even if we can, don't push here yet as this would skew 833e7428e95SMichael S. Tsirkin * csum_start offset below. */ 834e7428e95SMichael S. Tsirkin if (can_push) 835012873d0SMichael S. Tsirkin hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len); 836e7428e95SMichael S. Tsirkin else 837e7428e95SMichael S. Tsirkin hdr = skb_vnet_hdr(skb); 838296f96fcSRusty Russell 839*e858fae2SMike Rapoport if (virtio_net_hdr_from_skb(skb, &hdr->hdr, 840*e858fae2SMike Rapoport virtio_is_little_endian(vi->vdev))) 841296f96fcSRusty Russell BUG(); 842296f96fcSRusty Russell 843e7428e95SMichael S. Tsirkin if (vi->mergeable_rx_bufs) 844012873d0SMichael S. Tsirkin hdr->num_buffers = 0; 8453f2c31d9SMark McLoughlin 846547c890cSJason Wang sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2)); 847e7428e95SMichael S. Tsirkin if (can_push) { 848e7428e95SMichael S. Tsirkin __skb_push(skb, hdr_len); 849e7428e95SMichael S. Tsirkin num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len); 850e7428e95SMichael S. Tsirkin /* Pull header back to avoid skew in tx bytes calculations. */ 851e7428e95SMichael S. Tsirkin __skb_pull(skb, hdr_len); 852e7428e95SMichael S. Tsirkin } else { 853e7428e95SMichael S. Tsirkin sg_set_buf(sq->sg, hdr, hdr_len); 854b7dfde95SLinus Torvalds num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len) + 1; 855e7428e95SMichael S. Tsirkin } 8569dc7b9e4SRusty Russell return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC); 85711a3a154SRusty Russell } 85811a3a154SRusty Russell 859424efe9cSStephen Hemminger static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev) 86099ffc696SRusty Russell { 86199ffc696SRusty Russell struct virtnet_info *vi = netdev_priv(dev); 862986a4f4dSJason Wang int qnum = skb_get_queue_mapping(skb); 863986a4f4dSJason Wang struct send_queue *sq = &vi->sq[qnum]; 8649ed4cb07SRusty Russell int err; 8654b7fd2e6SMichael S. Tsirkin struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum); 8664b7fd2e6SMichael S. Tsirkin bool kick = !skb->xmit_more; 8672cb9c6baSRusty Russell 8682cb9c6baSRusty Russell /* Free up any pending old buffers before queueing new ones. */ 869e9d7417bSJason Wang free_old_xmit_skbs(sq); 87099ffc696SRusty Russell 871074c3582SJacob Keller /* timestamp packet in software */ 872074c3582SJacob Keller skb_tx_timestamp(skb); 873074c3582SJacob Keller 87403f191baSMichael S. Tsirkin /* Try to transmit */ 875b7dfde95SLinus Torvalds err = xmit_skb(sq, skb); 87699ffc696SRusty Russell 8779ed4cb07SRusty Russell /* This should not happen! */ 878681daee2SJason Wang if (unlikely(err)) { 87958eba97dSRusty Russell dev->stats.tx_fifo_errors++; 8802e57b79cSRick Jones if (net_ratelimit()) 88158eba97dSRusty Russell dev_warn(&dev->dev, 882b7dfde95SLinus Torvalds "Unexpected TXQ (%d) queue failure: %d\n", qnum, err); 88358eba97dSRusty Russell dev->stats.tx_dropped++; 88485e94525SEric W. Biederman dev_kfree_skb_any(skb); 88558eba97dSRusty Russell return NETDEV_TX_OK; 886296f96fcSRusty Russell } 88703f191baSMichael S. Tsirkin 88848925e37SRusty Russell /* Don't wait up for transmitted skbs to be freed. */ 88948925e37SRusty Russell skb_orphan(skb); 89048925e37SRusty Russell nf_reset(skb); 89148925e37SRusty Russell 89260302ff6SMichael S. Tsirkin /* If running out of space, stop queue to avoid getting packets that we 89360302ff6SMichael S. Tsirkin * are then unable to transmit. 89460302ff6SMichael S. Tsirkin * An alternative would be to force queuing layer to requeue the skb by 89560302ff6SMichael S. Tsirkin * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be 89660302ff6SMichael S. Tsirkin * returned in a normal path of operation: it means that driver is not 89760302ff6SMichael S. Tsirkin * maintaining the TX queue stop/start state properly, and causes 89860302ff6SMichael S. Tsirkin * the stack to do a non-trivial amount of useless work. 89960302ff6SMichael S. Tsirkin * Since most packets only take 1 or 2 ring slots, stopping the queue 90060302ff6SMichael S. Tsirkin * early means 16 slots are typically wasted. 901d631b94eSstephen hemminger */ 902b7dfde95SLinus Torvalds if (sq->vq->num_free < 2+MAX_SKB_FRAGS) { 903986a4f4dSJason Wang netif_stop_subqueue(dev, qnum); 904e9d7417bSJason Wang if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) { 90548925e37SRusty Russell /* More just got used, free them then recheck. */ 906b7dfde95SLinus Torvalds free_old_xmit_skbs(sq); 907b7dfde95SLinus Torvalds if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) { 908986a4f4dSJason Wang netif_start_subqueue(dev, qnum); 909e9d7417bSJason Wang virtqueue_disable_cb(sq->vq); 91048925e37SRusty Russell } 91148925e37SRusty Russell } 91248925e37SRusty Russell } 91348925e37SRusty Russell 9144b7fd2e6SMichael S. Tsirkin if (kick || netif_xmit_stopped(txq)) 915c223a078SDavid S. Miller virtqueue_kick(sq->vq); 9160b725a2cSDavid S. Miller 9170b725a2cSDavid S. Miller return NETDEV_TX_OK; 918c223a078SDavid S. Miller } 919c223a078SDavid S. Miller 92040cbfc37SAmos Kong /* 92140cbfc37SAmos Kong * Send command via the control virtqueue and check status. Commands 92240cbfc37SAmos Kong * supported by the hypervisor, as indicated by feature bits, should 923788a8b6dSstephen hemminger * never fail unless improperly formatted. 92440cbfc37SAmos Kong */ 92540cbfc37SAmos Kong static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd, 926d24bae32Sstephen hemminger struct scatterlist *out) 92740cbfc37SAmos Kong { 928f7bc9594SRusty Russell struct scatterlist *sgs[4], hdr, stat; 929d24bae32Sstephen hemminger unsigned out_num = 0, tmp; 93040cbfc37SAmos Kong 93140cbfc37SAmos Kong /* Caller should know better */ 932f7bc9594SRusty Russell BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)); 93340cbfc37SAmos Kong 9342ac46030SMichael S. Tsirkin vi->ctrl_status = ~0; 9352ac46030SMichael S. Tsirkin vi->ctrl_hdr.class = class; 9362ac46030SMichael S. Tsirkin vi->ctrl_hdr.cmd = cmd; 937f7bc9594SRusty Russell /* Add header */ 9382ac46030SMichael S. Tsirkin sg_init_one(&hdr, &vi->ctrl_hdr, sizeof(vi->ctrl_hdr)); 939f7bc9594SRusty Russell sgs[out_num++] = &hdr; 94040cbfc37SAmos Kong 941f7bc9594SRusty Russell if (out) 942f7bc9594SRusty Russell sgs[out_num++] = out; 94340cbfc37SAmos Kong 944f7bc9594SRusty Russell /* Add return status. */ 9452ac46030SMichael S. Tsirkin sg_init_one(&stat, &vi->ctrl_status, sizeof(vi->ctrl_status)); 946d24bae32Sstephen hemminger sgs[out_num] = &stat; 94740cbfc37SAmos Kong 948d24bae32Sstephen hemminger BUG_ON(out_num + 1 > ARRAY_SIZE(sgs)); 949a7c58146SRusty Russell virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC); 95040cbfc37SAmos Kong 95167975901SHeinz Graalfs if (unlikely(!virtqueue_kick(vi->cvq))) 9522ac46030SMichael S. Tsirkin return vi->ctrl_status == VIRTIO_NET_OK; 95340cbfc37SAmos Kong 95440cbfc37SAmos Kong /* Spin for a response, the kick causes an ioport write, trapping 95540cbfc37SAmos Kong * into the hypervisor, so the request should be handled immediately. 95640cbfc37SAmos Kong */ 957047b9b94SHeinz Graalfs while (!virtqueue_get_buf(vi->cvq, &tmp) && 958047b9b94SHeinz Graalfs !virtqueue_is_broken(vi->cvq)) 95940cbfc37SAmos Kong cpu_relax(); 96040cbfc37SAmos Kong 9612ac46030SMichael S. Tsirkin return vi->ctrl_status == VIRTIO_NET_OK; 96240cbfc37SAmos Kong } 96340cbfc37SAmos Kong 9649c46f6d4SAlex Williamson static int virtnet_set_mac_address(struct net_device *dev, void *p) 9659c46f6d4SAlex Williamson { 9669c46f6d4SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 9679c46f6d4SAlex Williamson struct virtio_device *vdev = vi->vdev; 968f2f2c8b4SJiri Pirko int ret; 9697e58d5aeSAmos Kong struct sockaddr *addr = p; 9707e58d5aeSAmos Kong struct scatterlist sg; 9719c46f6d4SAlex Williamson 9727e58d5aeSAmos Kong ret = eth_prepare_mac_addr_change(dev, p); 973f2f2c8b4SJiri Pirko if (ret) 974f2f2c8b4SJiri Pirko return ret; 9759c46f6d4SAlex Williamson 9767e58d5aeSAmos Kong if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) { 9777e58d5aeSAmos Kong sg_init_one(&sg, addr->sa_data, dev->addr_len); 9787e58d5aeSAmos Kong if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, 979d24bae32Sstephen hemminger VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) { 9807e58d5aeSAmos Kong dev_warn(&vdev->dev, 9817e58d5aeSAmos Kong "Failed to set mac address by vq command.\n"); 9827e58d5aeSAmos Kong return -EINVAL; 9837e58d5aeSAmos Kong } 9847e93a02fSMichael S. Tsirkin } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) && 9857e93a02fSMichael S. Tsirkin !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) { 986855e0c52SRusty Russell unsigned int i; 987855e0c52SRusty Russell 988855e0c52SRusty Russell /* Naturally, this has an atomicity problem. */ 989855e0c52SRusty Russell for (i = 0; i < dev->addr_len; i++) 990855e0c52SRusty Russell virtio_cwrite8(vdev, 991855e0c52SRusty Russell offsetof(struct virtio_net_config, mac) + 992855e0c52SRusty Russell i, addr->sa_data[i]); 9937e58d5aeSAmos Kong } 9947e58d5aeSAmos Kong 9957e58d5aeSAmos Kong eth_commit_mac_addr_change(dev, p); 9969c46f6d4SAlex Williamson 9979c46f6d4SAlex Williamson return 0; 9989c46f6d4SAlex Williamson } 9999c46f6d4SAlex Williamson 10003fa2a1dfSstephen hemminger static struct rtnl_link_stats64 *virtnet_stats(struct net_device *dev, 10013fa2a1dfSstephen hemminger struct rtnl_link_stats64 *tot) 10023fa2a1dfSstephen hemminger { 10033fa2a1dfSstephen hemminger struct virtnet_info *vi = netdev_priv(dev); 10043fa2a1dfSstephen hemminger int cpu; 10053fa2a1dfSstephen hemminger unsigned int start; 10063fa2a1dfSstephen hemminger 10073fa2a1dfSstephen hemminger for_each_possible_cpu(cpu) { 100858472a76SEric Dumazet struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu); 10093fa2a1dfSstephen hemminger u64 tpackets, tbytes, rpackets, rbytes; 10103fa2a1dfSstephen hemminger 10113fa2a1dfSstephen hemminger do { 101257a7744eSEric W. Biederman start = u64_stats_fetch_begin_irq(&stats->tx_syncp); 10133fa2a1dfSstephen hemminger tpackets = stats->tx_packets; 10143fa2a1dfSstephen hemminger tbytes = stats->tx_bytes; 101557a7744eSEric W. Biederman } while (u64_stats_fetch_retry_irq(&stats->tx_syncp, start)); 101683a27052SEric Dumazet 101783a27052SEric Dumazet do { 101857a7744eSEric W. Biederman start = u64_stats_fetch_begin_irq(&stats->rx_syncp); 10193fa2a1dfSstephen hemminger rpackets = stats->rx_packets; 10203fa2a1dfSstephen hemminger rbytes = stats->rx_bytes; 102157a7744eSEric W. Biederman } while (u64_stats_fetch_retry_irq(&stats->rx_syncp, start)); 10223fa2a1dfSstephen hemminger 10233fa2a1dfSstephen hemminger tot->rx_packets += rpackets; 10243fa2a1dfSstephen hemminger tot->tx_packets += tpackets; 10253fa2a1dfSstephen hemminger tot->rx_bytes += rbytes; 10263fa2a1dfSstephen hemminger tot->tx_bytes += tbytes; 10273fa2a1dfSstephen hemminger } 10283fa2a1dfSstephen hemminger 10293fa2a1dfSstephen hemminger tot->tx_dropped = dev->stats.tx_dropped; 1030021ac8d3SRick Jones tot->tx_fifo_errors = dev->stats.tx_fifo_errors; 10313fa2a1dfSstephen hemminger tot->rx_dropped = dev->stats.rx_dropped; 10323fa2a1dfSstephen hemminger tot->rx_length_errors = dev->stats.rx_length_errors; 10333fa2a1dfSstephen hemminger tot->rx_frame_errors = dev->stats.rx_frame_errors; 10343fa2a1dfSstephen hemminger 10353fa2a1dfSstephen hemminger return tot; 10363fa2a1dfSstephen hemminger } 10373fa2a1dfSstephen hemminger 1038da74e89dSAmit Shah #ifdef CONFIG_NET_POLL_CONTROLLER 1039da74e89dSAmit Shah static void virtnet_netpoll(struct net_device *dev) 1040da74e89dSAmit Shah { 1041da74e89dSAmit Shah struct virtnet_info *vi = netdev_priv(dev); 1042986a4f4dSJason Wang int i; 1043da74e89dSAmit Shah 1044986a4f4dSJason Wang for (i = 0; i < vi->curr_queue_pairs; i++) 1045986a4f4dSJason Wang napi_schedule(&vi->rq[i].napi); 1046da74e89dSAmit Shah } 1047da74e89dSAmit Shah #endif 1048da74e89dSAmit Shah 1049586d17c5SJason Wang static void virtnet_ack_link_announce(struct virtnet_info *vi) 1050586d17c5SJason Wang { 1051586d17c5SJason Wang rtnl_lock(); 1052586d17c5SJason Wang if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE, 1053d24bae32Sstephen hemminger VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL)) 1054586d17c5SJason Wang dev_warn(&vi->dev->dev, "Failed to ack link announce.\n"); 1055586d17c5SJason Wang rtnl_unlock(); 1056586d17c5SJason Wang } 1057586d17c5SJason Wang 1058986a4f4dSJason Wang static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs) 1059986a4f4dSJason Wang { 1060986a4f4dSJason Wang struct scatterlist sg; 1061986a4f4dSJason Wang struct virtio_net_ctrl_mq s; 1062986a4f4dSJason Wang struct net_device *dev = vi->dev; 1063986a4f4dSJason Wang 1064986a4f4dSJason Wang if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ)) 1065986a4f4dSJason Wang return 0; 1066986a4f4dSJason Wang 1067fdd819b2SMichael S. Tsirkin s.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs); 1068986a4f4dSJason Wang sg_init_one(&sg, &s, sizeof(s)); 1069986a4f4dSJason Wang 1070986a4f4dSJason Wang if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ, 1071d24bae32Sstephen hemminger VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) { 1072986a4f4dSJason Wang dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n", 1073986a4f4dSJason Wang queue_pairs); 1074986a4f4dSJason Wang return -EINVAL; 107555257d72SSasha Levin } else { 1076986a4f4dSJason Wang vi->curr_queue_pairs = queue_pairs; 107735ed159bSJason Wang /* virtnet_open() will refill when device is going to up. */ 107835ed159bSJason Wang if (dev->flags & IFF_UP) 10799b9cd802SJason Wang schedule_delayed_work(&vi->refill, 0); 108055257d72SSasha Levin } 1081986a4f4dSJason Wang 1082986a4f4dSJason Wang return 0; 1083986a4f4dSJason Wang } 1084986a4f4dSJason Wang 1085296f96fcSRusty Russell static int virtnet_close(struct net_device *dev) 1086296f96fcSRusty Russell { 1087296f96fcSRusty Russell struct virtnet_info *vi = netdev_priv(dev); 1088986a4f4dSJason Wang int i; 1089296f96fcSRusty Russell 1090b2baed69SRusty Russell /* Make sure refill_work doesn't re-enable napi! */ 1091b2baed69SRusty Russell cancel_delayed_work_sync(&vi->refill); 1092986a4f4dSJason Wang 1093986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) 1094986a4f4dSJason Wang napi_disable(&vi->rq[i].napi); 1095296f96fcSRusty Russell 1096296f96fcSRusty Russell return 0; 1097296f96fcSRusty Russell } 1098296f96fcSRusty Russell 10992af7698eSAlex Williamson static void virtnet_set_rx_mode(struct net_device *dev) 11002af7698eSAlex Williamson { 11012af7698eSAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 1102f565a7c2SAlex Williamson struct scatterlist sg[2]; 1103f565a7c2SAlex Williamson struct virtio_net_ctrl_mac *mac_data; 1104ccffad25SJiri Pirko struct netdev_hw_addr *ha; 110532e7bfc4SJiri Pirko int uc_count; 11064cd24eafSJiri Pirko int mc_count; 1107f565a7c2SAlex Williamson void *buf; 1108f565a7c2SAlex Williamson int i; 11092af7698eSAlex Williamson 1110788a8b6dSstephen hemminger /* We can't dynamically set ndo_set_rx_mode, so return gracefully */ 11112af7698eSAlex Williamson if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX)) 11122af7698eSAlex Williamson return; 11132af7698eSAlex Williamson 11142ac46030SMichael S. Tsirkin vi->ctrl_promisc = ((dev->flags & IFF_PROMISC) != 0); 11152ac46030SMichael S. Tsirkin vi->ctrl_allmulti = ((dev->flags & IFF_ALLMULTI) != 0); 11162af7698eSAlex Williamson 11172ac46030SMichael S. Tsirkin sg_init_one(sg, &vi->ctrl_promisc, sizeof(vi->ctrl_promisc)); 11182af7698eSAlex Williamson 11192af7698eSAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, 1120d24bae32Sstephen hemminger VIRTIO_NET_CTRL_RX_PROMISC, sg)) 11212af7698eSAlex Williamson dev_warn(&dev->dev, "Failed to %sable promisc mode.\n", 11222ac46030SMichael S. Tsirkin vi->ctrl_promisc ? "en" : "dis"); 11232af7698eSAlex Williamson 11242ac46030SMichael S. Tsirkin sg_init_one(sg, &vi->ctrl_allmulti, sizeof(vi->ctrl_allmulti)); 11252af7698eSAlex Williamson 11262af7698eSAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, 1127d24bae32Sstephen hemminger VIRTIO_NET_CTRL_RX_ALLMULTI, sg)) 11282af7698eSAlex Williamson dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n", 11292ac46030SMichael S. Tsirkin vi->ctrl_allmulti ? "en" : "dis"); 1130f565a7c2SAlex Williamson 113132e7bfc4SJiri Pirko uc_count = netdev_uc_count(dev); 11324cd24eafSJiri Pirko mc_count = netdev_mc_count(dev); 1133f565a7c2SAlex Williamson /* MAC filter - use one buffer for both lists */ 11344cd24eafSJiri Pirko buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) + 1135f565a7c2SAlex Williamson (2 * sizeof(mac_data->entries)), GFP_ATOMIC); 11364cd24eafSJiri Pirko mac_data = buf; 1137e68ed8f0SJoe Perches if (!buf) 1138f565a7c2SAlex Williamson return; 1139f565a7c2SAlex Williamson 114023e258e1SAlex Williamson sg_init_table(sg, 2); 114123e258e1SAlex Williamson 1142f565a7c2SAlex Williamson /* Store the unicast list and count in the front of the buffer */ 1143fdd819b2SMichael S. Tsirkin mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count); 1144ccffad25SJiri Pirko i = 0; 114532e7bfc4SJiri Pirko netdev_for_each_uc_addr(ha, dev) 1146ccffad25SJiri Pirko memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN); 1147f565a7c2SAlex Williamson 1148f565a7c2SAlex Williamson sg_set_buf(&sg[0], mac_data, 114932e7bfc4SJiri Pirko sizeof(mac_data->entries) + (uc_count * ETH_ALEN)); 1150f565a7c2SAlex Williamson 1151f565a7c2SAlex Williamson /* multicast list and count fill the end */ 115232e7bfc4SJiri Pirko mac_data = (void *)&mac_data->macs[uc_count][0]; 1153f565a7c2SAlex Williamson 1154fdd819b2SMichael S. Tsirkin mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count); 1155567ec874SJiri Pirko i = 0; 115622bedad3SJiri Pirko netdev_for_each_mc_addr(ha, dev) 115722bedad3SJiri Pirko memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN); 1158f565a7c2SAlex Williamson 1159f565a7c2SAlex Williamson sg_set_buf(&sg[1], mac_data, 11604cd24eafSJiri Pirko sizeof(mac_data->entries) + (mc_count * ETH_ALEN)); 1161f565a7c2SAlex Williamson 1162f565a7c2SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, 1163d24bae32Sstephen hemminger VIRTIO_NET_CTRL_MAC_TABLE_SET, sg)) 116499e872aeSThomas Huth dev_warn(&dev->dev, "Failed to set MAC filter table.\n"); 1165f565a7c2SAlex Williamson 1166f565a7c2SAlex Williamson kfree(buf); 11672af7698eSAlex Williamson } 11682af7698eSAlex Williamson 116980d5c368SPatrick McHardy static int virtnet_vlan_rx_add_vid(struct net_device *dev, 117080d5c368SPatrick McHardy __be16 proto, u16 vid) 11710bde9569SAlex Williamson { 11720bde9569SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 11730bde9569SAlex Williamson struct scatterlist sg; 11740bde9569SAlex Williamson 117523e258e1SAlex Williamson sg_init_one(&sg, &vid, sizeof(vid)); 11760bde9569SAlex Williamson 11770bde9569SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, 1178d24bae32Sstephen hemminger VIRTIO_NET_CTRL_VLAN_ADD, &sg)) 11790bde9569SAlex Williamson dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid); 11808e586137SJiri Pirko return 0; 11810bde9569SAlex Williamson } 11820bde9569SAlex Williamson 118380d5c368SPatrick McHardy static int virtnet_vlan_rx_kill_vid(struct net_device *dev, 118480d5c368SPatrick McHardy __be16 proto, u16 vid) 11850bde9569SAlex Williamson { 11860bde9569SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 11870bde9569SAlex Williamson struct scatterlist sg; 11880bde9569SAlex Williamson 118923e258e1SAlex Williamson sg_init_one(&sg, &vid, sizeof(vid)); 11900bde9569SAlex Williamson 11910bde9569SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, 1192d24bae32Sstephen hemminger VIRTIO_NET_CTRL_VLAN_DEL, &sg)) 11930bde9569SAlex Williamson dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid); 11948e586137SJiri Pirko return 0; 11950bde9569SAlex Williamson } 11960bde9569SAlex Williamson 11978898c21cSWanlong Gao static void virtnet_clean_affinity(struct virtnet_info *vi, long hcpu) 1198986a4f4dSJason Wang { 1199986a4f4dSJason Wang int i; 12008898c21cSWanlong Gao 12018898c21cSWanlong Gao if (vi->affinity_hint_set) { 12028898c21cSWanlong Gao for (i = 0; i < vi->max_queue_pairs; i++) { 12038898c21cSWanlong Gao virtqueue_set_affinity(vi->rq[i].vq, -1); 12048898c21cSWanlong Gao virtqueue_set_affinity(vi->sq[i].vq, -1); 12058898c21cSWanlong Gao } 12068898c21cSWanlong Gao 12078898c21cSWanlong Gao vi->affinity_hint_set = false; 12088898c21cSWanlong Gao } 12098898c21cSWanlong Gao } 12108898c21cSWanlong Gao 12118898c21cSWanlong Gao static void virtnet_set_affinity(struct virtnet_info *vi) 1212986a4f4dSJason Wang { 1213986a4f4dSJason Wang int i; 121447be2479SWanlong Gao int cpu; 1215986a4f4dSJason Wang 1216986a4f4dSJason Wang /* In multiqueue mode, when the number of cpu is equal to the number of 1217986a4f4dSJason Wang * queue pairs, we let the queue pairs to be private to one cpu by 1218986a4f4dSJason Wang * setting the affinity hint to eliminate the contention. 1219986a4f4dSJason Wang */ 12208898c21cSWanlong Gao if (vi->curr_queue_pairs == 1 || 12218898c21cSWanlong Gao vi->max_queue_pairs != num_online_cpus()) { 12228898c21cSWanlong Gao virtnet_clean_affinity(vi, -1); 1223986a4f4dSJason Wang return; 1224986a4f4dSJason Wang } 1225986a4f4dSJason Wang 122647be2479SWanlong Gao i = 0; 122747be2479SWanlong Gao for_each_online_cpu(cpu) { 1228986a4f4dSJason Wang virtqueue_set_affinity(vi->rq[i].vq, cpu); 1229986a4f4dSJason Wang virtqueue_set_affinity(vi->sq[i].vq, cpu); 12309bb8ca86SJason Wang netif_set_xps_queue(vi->dev, cpumask_of(cpu), i); 123147be2479SWanlong Gao i++; 1232986a4f4dSJason Wang } 1233986a4f4dSJason Wang 1234986a4f4dSJason Wang vi->affinity_hint_set = true; 123547be2479SWanlong Gao } 1236986a4f4dSJason Wang 12378de4b2f3SWanlong Gao static int virtnet_cpu_callback(struct notifier_block *nfb, 12388de4b2f3SWanlong Gao unsigned long action, void *hcpu) 12398de4b2f3SWanlong Gao { 12408de4b2f3SWanlong Gao struct virtnet_info *vi = container_of(nfb, struct virtnet_info, nb); 12418de4b2f3SWanlong Gao 12428de4b2f3SWanlong Gao switch(action & ~CPU_TASKS_FROZEN) { 12438de4b2f3SWanlong Gao case CPU_ONLINE: 12448de4b2f3SWanlong Gao case CPU_DOWN_FAILED: 12458de4b2f3SWanlong Gao case CPU_DEAD: 12468de4b2f3SWanlong Gao virtnet_set_affinity(vi); 12478de4b2f3SWanlong Gao break; 12488de4b2f3SWanlong Gao case CPU_DOWN_PREPARE: 12498de4b2f3SWanlong Gao virtnet_clean_affinity(vi, (long)hcpu); 12508de4b2f3SWanlong Gao break; 12518de4b2f3SWanlong Gao default: 12528de4b2f3SWanlong Gao break; 12538de4b2f3SWanlong Gao } 12543ab098dfSJason Wang 12558de4b2f3SWanlong Gao return NOTIFY_OK; 1256a9ea3fc6SHerbert Xu } 1257a9ea3fc6SHerbert Xu 12588f9f4668SRick Jones static void virtnet_get_ringparam(struct net_device *dev, 12598f9f4668SRick Jones struct ethtool_ringparam *ring) 12608f9f4668SRick Jones { 12618f9f4668SRick Jones struct virtnet_info *vi = netdev_priv(dev); 12628f9f4668SRick Jones 1263986a4f4dSJason Wang ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq); 1264986a4f4dSJason Wang ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq); 12658f9f4668SRick Jones ring->rx_pending = ring->rx_max_pending; 12668f9f4668SRick Jones ring->tx_pending = ring->tx_max_pending; 12678f9f4668SRick Jones } 12688f9f4668SRick Jones 126966846048SRick Jones 127066846048SRick Jones static void virtnet_get_drvinfo(struct net_device *dev, 127166846048SRick Jones struct ethtool_drvinfo *info) 127266846048SRick Jones { 127366846048SRick Jones struct virtnet_info *vi = netdev_priv(dev); 127466846048SRick Jones struct virtio_device *vdev = vi->vdev; 127566846048SRick Jones 127666846048SRick Jones strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver)); 127766846048SRick Jones strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version)); 127866846048SRick Jones strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info)); 127966846048SRick Jones 128066846048SRick Jones } 128166846048SRick Jones 1282d73bcd2cSJason Wang /* TODO: Eliminate OOO packets during switching */ 1283d73bcd2cSJason Wang static int virtnet_set_channels(struct net_device *dev, 1284d73bcd2cSJason Wang struct ethtool_channels *channels) 1285d73bcd2cSJason Wang { 1286d73bcd2cSJason Wang struct virtnet_info *vi = netdev_priv(dev); 1287d73bcd2cSJason Wang u16 queue_pairs = channels->combined_count; 1288d73bcd2cSJason Wang int err; 1289d73bcd2cSJason Wang 1290d73bcd2cSJason Wang /* We don't support separate rx/tx channels. 1291d73bcd2cSJason Wang * We don't allow setting 'other' channels. 1292d73bcd2cSJason Wang */ 1293d73bcd2cSJason Wang if (channels->rx_count || channels->tx_count || channels->other_count) 1294d73bcd2cSJason Wang return -EINVAL; 1295d73bcd2cSJason Wang 1296c18e9cd6SAmos Kong if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0) 1297d73bcd2cSJason Wang return -EINVAL; 1298d73bcd2cSJason Wang 129947be2479SWanlong Gao get_online_cpus(); 1300d73bcd2cSJason Wang err = virtnet_set_queues(vi, queue_pairs); 1301d73bcd2cSJason Wang if (!err) { 1302d73bcd2cSJason Wang netif_set_real_num_tx_queues(dev, queue_pairs); 1303d73bcd2cSJason Wang netif_set_real_num_rx_queues(dev, queue_pairs); 1304d73bcd2cSJason Wang 13058898c21cSWanlong Gao virtnet_set_affinity(vi); 1306d73bcd2cSJason Wang } 130747be2479SWanlong Gao put_online_cpus(); 1308d73bcd2cSJason Wang 1309d73bcd2cSJason Wang return err; 1310d73bcd2cSJason Wang } 1311d73bcd2cSJason Wang 1312d73bcd2cSJason Wang static void virtnet_get_channels(struct net_device *dev, 1313d73bcd2cSJason Wang struct ethtool_channels *channels) 1314d73bcd2cSJason Wang { 1315d73bcd2cSJason Wang struct virtnet_info *vi = netdev_priv(dev); 1316d73bcd2cSJason Wang 1317d73bcd2cSJason Wang channels->combined_count = vi->curr_queue_pairs; 1318d73bcd2cSJason Wang channels->max_combined = vi->max_queue_pairs; 1319d73bcd2cSJason Wang channels->max_other = 0; 1320d73bcd2cSJason Wang channels->rx_count = 0; 1321d73bcd2cSJason Wang channels->tx_count = 0; 1322d73bcd2cSJason Wang channels->other_count = 0; 1323d73bcd2cSJason Wang } 1324d73bcd2cSJason Wang 132516032be5SNikolay Aleksandrov /* Check if the user is trying to change anything besides speed/duplex */ 132616032be5SNikolay Aleksandrov static bool virtnet_validate_ethtool_cmd(const struct ethtool_cmd *cmd) 132716032be5SNikolay Aleksandrov { 132816032be5SNikolay Aleksandrov struct ethtool_cmd diff1 = *cmd; 132916032be5SNikolay Aleksandrov struct ethtool_cmd diff2 = {}; 133016032be5SNikolay Aleksandrov 13310cf3ace9SNikolay Aleksandrov /* cmd is always set so we need to clear it, validate the port type 13320cf3ace9SNikolay Aleksandrov * and also without autonegotiation we can ignore advertising 13330cf3ace9SNikolay Aleksandrov */ 133416032be5SNikolay Aleksandrov ethtool_cmd_speed_set(&diff1, 0); 13350cf3ace9SNikolay Aleksandrov diff2.port = PORT_OTHER; 133616032be5SNikolay Aleksandrov diff1.advertising = 0; 133716032be5SNikolay Aleksandrov diff1.duplex = 0; 133816032be5SNikolay Aleksandrov diff1.cmd = 0; 133916032be5SNikolay Aleksandrov 134016032be5SNikolay Aleksandrov return !memcmp(&diff1, &diff2, sizeof(diff1)); 134116032be5SNikolay Aleksandrov } 134216032be5SNikolay Aleksandrov 134316032be5SNikolay Aleksandrov static int virtnet_set_settings(struct net_device *dev, struct ethtool_cmd *cmd) 134416032be5SNikolay Aleksandrov { 134516032be5SNikolay Aleksandrov struct virtnet_info *vi = netdev_priv(dev); 134616032be5SNikolay Aleksandrov u32 speed; 134716032be5SNikolay Aleksandrov 134816032be5SNikolay Aleksandrov speed = ethtool_cmd_speed(cmd); 134916032be5SNikolay Aleksandrov /* don't allow custom speed and duplex */ 135016032be5SNikolay Aleksandrov if (!ethtool_validate_speed(speed) || 135116032be5SNikolay Aleksandrov !ethtool_validate_duplex(cmd->duplex) || 135216032be5SNikolay Aleksandrov !virtnet_validate_ethtool_cmd(cmd)) 135316032be5SNikolay Aleksandrov return -EINVAL; 135416032be5SNikolay Aleksandrov vi->speed = speed; 135516032be5SNikolay Aleksandrov vi->duplex = cmd->duplex; 135616032be5SNikolay Aleksandrov 135716032be5SNikolay Aleksandrov return 0; 135816032be5SNikolay Aleksandrov } 135916032be5SNikolay Aleksandrov 136016032be5SNikolay Aleksandrov static int virtnet_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) 136116032be5SNikolay Aleksandrov { 136216032be5SNikolay Aleksandrov struct virtnet_info *vi = netdev_priv(dev); 136316032be5SNikolay Aleksandrov 136416032be5SNikolay Aleksandrov ethtool_cmd_speed_set(cmd, vi->speed); 136516032be5SNikolay Aleksandrov cmd->duplex = vi->duplex; 136616032be5SNikolay Aleksandrov cmd->port = PORT_OTHER; 136716032be5SNikolay Aleksandrov 136816032be5SNikolay Aleksandrov return 0; 136916032be5SNikolay Aleksandrov } 137016032be5SNikolay Aleksandrov 137116032be5SNikolay Aleksandrov static void virtnet_init_settings(struct net_device *dev) 137216032be5SNikolay Aleksandrov { 137316032be5SNikolay Aleksandrov struct virtnet_info *vi = netdev_priv(dev); 137416032be5SNikolay Aleksandrov 137516032be5SNikolay Aleksandrov vi->speed = SPEED_UNKNOWN; 137616032be5SNikolay Aleksandrov vi->duplex = DUPLEX_UNKNOWN; 137716032be5SNikolay Aleksandrov } 137816032be5SNikolay Aleksandrov 13790fc0b732SStephen Hemminger static const struct ethtool_ops virtnet_ethtool_ops = { 138066846048SRick Jones .get_drvinfo = virtnet_get_drvinfo, 13819f4d26d0SMark McLoughlin .get_link = ethtool_op_get_link, 13828f9f4668SRick Jones .get_ringparam = virtnet_get_ringparam, 1383d73bcd2cSJason Wang .set_channels = virtnet_set_channels, 1384d73bcd2cSJason Wang .get_channels = virtnet_get_channels, 1385074c3582SJacob Keller .get_ts_info = ethtool_op_get_ts_info, 138616032be5SNikolay Aleksandrov .get_settings = virtnet_get_settings, 138716032be5SNikolay Aleksandrov .set_settings = virtnet_set_settings, 1388a9ea3fc6SHerbert Xu }; 1389a9ea3fc6SHerbert Xu 139039da5814SMark McLoughlin #define MIN_MTU 68 139139da5814SMark McLoughlin #define MAX_MTU 65535 139239da5814SMark McLoughlin 139339da5814SMark McLoughlin static int virtnet_change_mtu(struct net_device *dev, int new_mtu) 139439da5814SMark McLoughlin { 139539da5814SMark McLoughlin if (new_mtu < MIN_MTU || new_mtu > MAX_MTU) 139639da5814SMark McLoughlin return -EINVAL; 139739da5814SMark McLoughlin dev->mtu = new_mtu; 139839da5814SMark McLoughlin return 0; 139939da5814SMark McLoughlin } 140039da5814SMark McLoughlin 140176288b4eSStephen Hemminger static const struct net_device_ops virtnet_netdev = { 140276288b4eSStephen Hemminger .ndo_open = virtnet_open, 140376288b4eSStephen Hemminger .ndo_stop = virtnet_close, 140476288b4eSStephen Hemminger .ndo_start_xmit = start_xmit, 140576288b4eSStephen Hemminger .ndo_validate_addr = eth_validate_addr, 14069c46f6d4SAlex Williamson .ndo_set_mac_address = virtnet_set_mac_address, 14072af7698eSAlex Williamson .ndo_set_rx_mode = virtnet_set_rx_mode, 140876288b4eSStephen Hemminger .ndo_change_mtu = virtnet_change_mtu, 14093fa2a1dfSstephen hemminger .ndo_get_stats64 = virtnet_stats, 14101824a989SAlex Williamson .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid, 14111824a989SAlex Williamson .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid, 141276288b4eSStephen Hemminger #ifdef CONFIG_NET_POLL_CONTROLLER 141376288b4eSStephen Hemminger .ndo_poll_controller = virtnet_netpoll, 141476288b4eSStephen Hemminger #endif 141591815639SJason Wang #ifdef CONFIG_NET_RX_BUSY_POLL 141691815639SJason Wang .ndo_busy_poll = virtnet_busy_poll, 141791815639SJason Wang #endif 141876288b4eSStephen Hemminger }; 141976288b4eSStephen Hemminger 1420586d17c5SJason Wang static void virtnet_config_changed_work(struct work_struct *work) 14219f4d26d0SMark McLoughlin { 1422586d17c5SJason Wang struct virtnet_info *vi = 1423586d17c5SJason Wang container_of(work, struct virtnet_info, config_work); 14249f4d26d0SMark McLoughlin u16 v; 14259f4d26d0SMark McLoughlin 1426855e0c52SRusty Russell if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS, 1427855e0c52SRusty Russell struct virtio_net_config, status, &v) < 0) 1428507613bfSMichael S. Tsirkin return; 1429586d17c5SJason Wang 1430586d17c5SJason Wang if (v & VIRTIO_NET_S_ANNOUNCE) { 1431ee89bab1SAmerigo Wang netdev_notify_peers(vi->dev); 1432586d17c5SJason Wang virtnet_ack_link_announce(vi); 1433586d17c5SJason Wang } 14349f4d26d0SMark McLoughlin 14359f4d26d0SMark McLoughlin /* Ignore unknown (future) status bits */ 14369f4d26d0SMark McLoughlin v &= VIRTIO_NET_S_LINK_UP; 14379f4d26d0SMark McLoughlin 14389f4d26d0SMark McLoughlin if (vi->status == v) 1439507613bfSMichael S. Tsirkin return; 14409f4d26d0SMark McLoughlin 14419f4d26d0SMark McLoughlin vi->status = v; 14429f4d26d0SMark McLoughlin 14439f4d26d0SMark McLoughlin if (vi->status & VIRTIO_NET_S_LINK_UP) { 14449f4d26d0SMark McLoughlin netif_carrier_on(vi->dev); 1445986a4f4dSJason Wang netif_tx_wake_all_queues(vi->dev); 14469f4d26d0SMark McLoughlin } else { 14479f4d26d0SMark McLoughlin netif_carrier_off(vi->dev); 1448986a4f4dSJason Wang netif_tx_stop_all_queues(vi->dev); 14499f4d26d0SMark McLoughlin } 14509f4d26d0SMark McLoughlin } 14519f4d26d0SMark McLoughlin 14529f4d26d0SMark McLoughlin static void virtnet_config_changed(struct virtio_device *vdev) 14539f4d26d0SMark McLoughlin { 14549f4d26d0SMark McLoughlin struct virtnet_info *vi = vdev->priv; 14559f4d26d0SMark McLoughlin 14563b07e9caSTejun Heo schedule_work(&vi->config_work); 14579f4d26d0SMark McLoughlin } 14589f4d26d0SMark McLoughlin 1459986a4f4dSJason Wang static void virtnet_free_queues(struct virtnet_info *vi) 1460986a4f4dSJason Wang { 1461d4fb84eeSAndrey Vagin int i; 1462d4fb84eeSAndrey Vagin 1463ab3971b1SJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1464ab3971b1SJason Wang napi_hash_del(&vi->rq[i].napi); 1465d4fb84eeSAndrey Vagin netif_napi_del(&vi->rq[i].napi); 1466ab3971b1SJason Wang } 1467d4fb84eeSAndrey Vagin 1468986a4f4dSJason Wang kfree(vi->rq); 1469986a4f4dSJason Wang kfree(vi->sq); 1470986a4f4dSJason Wang } 1471986a4f4dSJason Wang 1472986a4f4dSJason Wang static void free_receive_bufs(struct virtnet_info *vi) 1473986a4f4dSJason Wang { 1474986a4f4dSJason Wang int i; 1475986a4f4dSJason Wang 1476986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1477986a4f4dSJason Wang while (vi->rq[i].pages) 1478986a4f4dSJason Wang __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0); 1479986a4f4dSJason Wang } 1480986a4f4dSJason Wang } 1481986a4f4dSJason Wang 1482fb51879dSMichael Dalton static void free_receive_page_frags(struct virtnet_info *vi) 1483fb51879dSMichael Dalton { 1484fb51879dSMichael Dalton int i; 1485fb51879dSMichael Dalton for (i = 0; i < vi->max_queue_pairs; i++) 1486fb51879dSMichael Dalton if (vi->rq[i].alloc_frag.page) 1487fb51879dSMichael Dalton put_page(vi->rq[i].alloc_frag.page); 1488fb51879dSMichael Dalton } 1489fb51879dSMichael Dalton 1490986a4f4dSJason Wang static void free_unused_bufs(struct virtnet_info *vi) 1491986a4f4dSJason Wang { 1492986a4f4dSJason Wang void *buf; 1493986a4f4dSJason Wang int i; 1494986a4f4dSJason Wang 1495986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1496986a4f4dSJason Wang struct virtqueue *vq = vi->sq[i].vq; 1497986a4f4dSJason Wang while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) 1498986a4f4dSJason Wang dev_kfree_skb(buf); 1499986a4f4dSJason Wang } 1500986a4f4dSJason Wang 1501986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1502986a4f4dSJason Wang struct virtqueue *vq = vi->rq[i].vq; 1503986a4f4dSJason Wang 1504986a4f4dSJason Wang while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) { 1505ab7db917SMichael Dalton if (vi->mergeable_rx_bufs) { 1506ab7db917SMichael Dalton unsigned long ctx = (unsigned long)buf; 1507ab7db917SMichael Dalton void *base = mergeable_ctx_to_buf_address(ctx); 1508ab7db917SMichael Dalton put_page(virt_to_head_page(base)); 1509ab7db917SMichael Dalton } else if (vi->big_packets) { 1510fa9fac17SAndrey Vagin give_pages(&vi->rq[i], buf); 1511ab7db917SMichael Dalton } else { 1512986a4f4dSJason Wang dev_kfree_skb(buf); 1513986a4f4dSJason Wang } 1514986a4f4dSJason Wang } 1515986a4f4dSJason Wang } 1516ab7db917SMichael Dalton } 1517986a4f4dSJason Wang 1518e9d7417bSJason Wang static void virtnet_del_vqs(struct virtnet_info *vi) 1519e9d7417bSJason Wang { 1520e9d7417bSJason Wang struct virtio_device *vdev = vi->vdev; 1521e9d7417bSJason Wang 15228898c21cSWanlong Gao virtnet_clean_affinity(vi, -1); 1523986a4f4dSJason Wang 1524e9d7417bSJason Wang vdev->config->del_vqs(vdev); 1525986a4f4dSJason Wang 1526986a4f4dSJason Wang virtnet_free_queues(vi); 1527986a4f4dSJason Wang } 1528986a4f4dSJason Wang 1529986a4f4dSJason Wang static int virtnet_find_vqs(struct virtnet_info *vi) 1530986a4f4dSJason Wang { 1531986a4f4dSJason Wang vq_callback_t **callbacks; 1532986a4f4dSJason Wang struct virtqueue **vqs; 1533986a4f4dSJason Wang int ret = -ENOMEM; 1534986a4f4dSJason Wang int i, total_vqs; 1535986a4f4dSJason Wang const char **names; 1536986a4f4dSJason Wang 1537986a4f4dSJason Wang /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by 1538986a4f4dSJason Wang * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by 1539986a4f4dSJason Wang * possible control vq. 1540986a4f4dSJason Wang */ 1541986a4f4dSJason Wang total_vqs = vi->max_queue_pairs * 2 + 1542986a4f4dSJason Wang virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ); 1543986a4f4dSJason Wang 1544986a4f4dSJason Wang /* Allocate space for find_vqs parameters */ 1545986a4f4dSJason Wang vqs = kzalloc(total_vqs * sizeof(*vqs), GFP_KERNEL); 1546986a4f4dSJason Wang if (!vqs) 1547986a4f4dSJason Wang goto err_vq; 1548986a4f4dSJason Wang callbacks = kmalloc(total_vqs * sizeof(*callbacks), GFP_KERNEL); 1549986a4f4dSJason Wang if (!callbacks) 1550986a4f4dSJason Wang goto err_callback; 1551986a4f4dSJason Wang names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL); 1552986a4f4dSJason Wang if (!names) 1553986a4f4dSJason Wang goto err_names; 1554986a4f4dSJason Wang 1555986a4f4dSJason Wang /* Parameters for control virtqueue, if any */ 1556986a4f4dSJason Wang if (vi->has_cvq) { 1557986a4f4dSJason Wang callbacks[total_vqs - 1] = NULL; 1558986a4f4dSJason Wang names[total_vqs - 1] = "control"; 1559986a4f4dSJason Wang } 1560986a4f4dSJason Wang 1561986a4f4dSJason Wang /* Allocate/initialize parameters for send/receive virtqueues */ 1562986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1563986a4f4dSJason Wang callbacks[rxq2vq(i)] = skb_recv_done; 1564986a4f4dSJason Wang callbacks[txq2vq(i)] = skb_xmit_done; 1565986a4f4dSJason Wang sprintf(vi->rq[i].name, "input.%d", i); 1566986a4f4dSJason Wang sprintf(vi->sq[i].name, "output.%d", i); 1567986a4f4dSJason Wang names[rxq2vq(i)] = vi->rq[i].name; 1568986a4f4dSJason Wang names[txq2vq(i)] = vi->sq[i].name; 1569986a4f4dSJason Wang } 1570986a4f4dSJason Wang 1571986a4f4dSJason Wang ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks, 1572986a4f4dSJason Wang names); 1573986a4f4dSJason Wang if (ret) 1574986a4f4dSJason Wang goto err_find; 1575986a4f4dSJason Wang 1576986a4f4dSJason Wang if (vi->has_cvq) { 1577986a4f4dSJason Wang vi->cvq = vqs[total_vqs - 1]; 1578986a4f4dSJason Wang if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN)) 1579f646968fSPatrick McHardy vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER; 1580986a4f4dSJason Wang } 1581986a4f4dSJason Wang 1582986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1583986a4f4dSJason Wang vi->rq[i].vq = vqs[rxq2vq(i)]; 1584986a4f4dSJason Wang vi->sq[i].vq = vqs[txq2vq(i)]; 1585986a4f4dSJason Wang } 1586986a4f4dSJason Wang 1587986a4f4dSJason Wang kfree(names); 1588986a4f4dSJason Wang kfree(callbacks); 1589986a4f4dSJason Wang kfree(vqs); 1590986a4f4dSJason Wang 1591986a4f4dSJason Wang return 0; 1592986a4f4dSJason Wang 1593986a4f4dSJason Wang err_find: 1594986a4f4dSJason Wang kfree(names); 1595986a4f4dSJason Wang err_names: 1596986a4f4dSJason Wang kfree(callbacks); 1597986a4f4dSJason Wang err_callback: 1598986a4f4dSJason Wang kfree(vqs); 1599986a4f4dSJason Wang err_vq: 1600986a4f4dSJason Wang return ret; 1601986a4f4dSJason Wang } 1602986a4f4dSJason Wang 1603986a4f4dSJason Wang static int virtnet_alloc_queues(struct virtnet_info *vi) 1604986a4f4dSJason Wang { 1605986a4f4dSJason Wang int i; 1606986a4f4dSJason Wang 1607986a4f4dSJason Wang vi->sq = kzalloc(sizeof(*vi->sq) * vi->max_queue_pairs, GFP_KERNEL); 1608986a4f4dSJason Wang if (!vi->sq) 1609986a4f4dSJason Wang goto err_sq; 1610986a4f4dSJason Wang vi->rq = kzalloc(sizeof(*vi->rq) * vi->max_queue_pairs, GFP_KERNEL); 1611008d4278SAmerigo Wang if (!vi->rq) 1612986a4f4dSJason Wang goto err_rq; 1613986a4f4dSJason Wang 1614986a4f4dSJason Wang INIT_DELAYED_WORK(&vi->refill, refill_work); 1615986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1616986a4f4dSJason Wang vi->rq[i].pages = NULL; 1617986a4f4dSJason Wang netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll, 1618986a4f4dSJason Wang napi_weight); 1619986a4f4dSJason Wang 1620986a4f4dSJason Wang sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg)); 16215377d758SJohannes Berg ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len); 1622986a4f4dSJason Wang sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg)); 1623986a4f4dSJason Wang } 1624986a4f4dSJason Wang 1625986a4f4dSJason Wang return 0; 1626986a4f4dSJason Wang 1627986a4f4dSJason Wang err_rq: 1628986a4f4dSJason Wang kfree(vi->sq); 1629986a4f4dSJason Wang err_sq: 1630986a4f4dSJason Wang return -ENOMEM; 1631e9d7417bSJason Wang } 1632e9d7417bSJason Wang 16333f9c10b0SAmit Shah static int init_vqs(struct virtnet_info *vi) 16343f9c10b0SAmit Shah { 1635986a4f4dSJason Wang int ret; 16363f9c10b0SAmit Shah 1637986a4f4dSJason Wang /* Allocate send & receive queues */ 1638986a4f4dSJason Wang ret = virtnet_alloc_queues(vi); 1639986a4f4dSJason Wang if (ret) 1640986a4f4dSJason Wang goto err; 16413f9c10b0SAmit Shah 1642986a4f4dSJason Wang ret = virtnet_find_vqs(vi); 1643986a4f4dSJason Wang if (ret) 1644986a4f4dSJason Wang goto err_free; 16453f9c10b0SAmit Shah 164647be2479SWanlong Gao get_online_cpus(); 16478898c21cSWanlong Gao virtnet_set_affinity(vi); 164847be2479SWanlong Gao put_online_cpus(); 164947be2479SWanlong Gao 16503f9c10b0SAmit Shah return 0; 1651986a4f4dSJason Wang 1652986a4f4dSJason Wang err_free: 1653986a4f4dSJason Wang virtnet_free_queues(vi); 1654986a4f4dSJason Wang err: 1655986a4f4dSJason Wang return ret; 16563f9c10b0SAmit Shah } 16573f9c10b0SAmit Shah 1658fbf28d78SMichael Dalton #ifdef CONFIG_SYSFS 1659fbf28d78SMichael Dalton static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue, 1660fbf28d78SMichael Dalton struct rx_queue_attribute *attribute, char *buf) 1661fbf28d78SMichael Dalton { 1662fbf28d78SMichael Dalton struct virtnet_info *vi = netdev_priv(queue->dev); 1663fbf28d78SMichael Dalton unsigned int queue_index = get_netdev_rx_queue_index(queue); 16645377d758SJohannes Berg struct ewma_pkt_len *avg; 1665fbf28d78SMichael Dalton 1666fbf28d78SMichael Dalton BUG_ON(queue_index >= vi->max_queue_pairs); 1667fbf28d78SMichael Dalton avg = &vi->rq[queue_index].mrg_avg_pkt_len; 1668fbf28d78SMichael Dalton return sprintf(buf, "%u\n", get_mergeable_buf_len(avg)); 1669fbf28d78SMichael Dalton } 1670fbf28d78SMichael Dalton 1671fbf28d78SMichael Dalton static struct rx_queue_attribute mergeable_rx_buffer_size_attribute = 1672fbf28d78SMichael Dalton __ATTR_RO(mergeable_rx_buffer_size); 1673fbf28d78SMichael Dalton 1674fbf28d78SMichael Dalton static struct attribute *virtio_net_mrg_rx_attrs[] = { 1675fbf28d78SMichael Dalton &mergeable_rx_buffer_size_attribute.attr, 1676fbf28d78SMichael Dalton NULL 1677fbf28d78SMichael Dalton }; 1678fbf28d78SMichael Dalton 1679fbf28d78SMichael Dalton static const struct attribute_group virtio_net_mrg_rx_group = { 1680fbf28d78SMichael Dalton .name = "virtio_net", 1681fbf28d78SMichael Dalton .attrs = virtio_net_mrg_rx_attrs 1682fbf28d78SMichael Dalton }; 1683fbf28d78SMichael Dalton #endif 1684fbf28d78SMichael Dalton 1685892d6eb1SJason Wang static bool virtnet_fail_on_feature(struct virtio_device *vdev, 1686892d6eb1SJason Wang unsigned int fbit, 1687892d6eb1SJason Wang const char *fname, const char *dname) 1688892d6eb1SJason Wang { 1689892d6eb1SJason Wang if (!virtio_has_feature(vdev, fbit)) 1690892d6eb1SJason Wang return false; 1691892d6eb1SJason Wang 1692892d6eb1SJason Wang dev_err(&vdev->dev, "device advertises feature %s but not %s", 1693892d6eb1SJason Wang fname, dname); 1694892d6eb1SJason Wang 1695892d6eb1SJason Wang return true; 1696892d6eb1SJason Wang } 1697892d6eb1SJason Wang 1698892d6eb1SJason Wang #define VIRTNET_FAIL_ON(vdev, fbit, dbit) \ 1699892d6eb1SJason Wang virtnet_fail_on_feature(vdev, fbit, #fbit, dbit) 1700892d6eb1SJason Wang 1701892d6eb1SJason Wang static bool virtnet_validate_features(struct virtio_device *vdev) 1702892d6eb1SJason Wang { 1703892d6eb1SJason Wang if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) && 1704892d6eb1SJason Wang (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX, 1705892d6eb1SJason Wang "VIRTIO_NET_F_CTRL_VQ") || 1706892d6eb1SJason Wang VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN, 1707892d6eb1SJason Wang "VIRTIO_NET_F_CTRL_VQ") || 1708892d6eb1SJason Wang VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE, 1709892d6eb1SJason Wang "VIRTIO_NET_F_CTRL_VQ") || 1710892d6eb1SJason Wang VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") || 1711892d6eb1SJason Wang VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR, 1712892d6eb1SJason Wang "VIRTIO_NET_F_CTRL_VQ"))) { 1713892d6eb1SJason Wang return false; 1714892d6eb1SJason Wang } 1715892d6eb1SJason Wang 1716892d6eb1SJason Wang return true; 1717892d6eb1SJason Wang } 1718892d6eb1SJason Wang 1719296f96fcSRusty Russell static int virtnet_probe(struct virtio_device *vdev) 1720296f96fcSRusty Russell { 1721986a4f4dSJason Wang int i, err; 1722296f96fcSRusty Russell struct net_device *dev; 1723296f96fcSRusty Russell struct virtnet_info *vi; 1724986a4f4dSJason Wang u16 max_queue_pairs; 172514de9d11SAaron Conole int mtu; 1726986a4f4dSJason Wang 17276ba42248SMichael S. Tsirkin if (!vdev->config->get) { 17286ba42248SMichael S. Tsirkin dev_err(&vdev->dev, "%s failure: config access disabled\n", 17296ba42248SMichael S. Tsirkin __func__); 17306ba42248SMichael S. Tsirkin return -EINVAL; 17316ba42248SMichael S. Tsirkin } 17326ba42248SMichael S. Tsirkin 1733892d6eb1SJason Wang if (!virtnet_validate_features(vdev)) 1734892d6eb1SJason Wang return -EINVAL; 1735892d6eb1SJason Wang 1736986a4f4dSJason Wang /* Find if host supports multiqueue virtio_net device */ 1737855e0c52SRusty Russell err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ, 1738855e0c52SRusty Russell struct virtio_net_config, 1739855e0c52SRusty Russell max_virtqueue_pairs, &max_queue_pairs); 1740986a4f4dSJason Wang 1741986a4f4dSJason Wang /* We need at least 2 queue's */ 1742986a4f4dSJason Wang if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN || 1743986a4f4dSJason Wang max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX || 1744986a4f4dSJason Wang !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) 1745986a4f4dSJason Wang max_queue_pairs = 1; 1746296f96fcSRusty Russell 1747296f96fcSRusty Russell /* Allocate ourselves a network device with room for our info */ 1748986a4f4dSJason Wang dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs); 1749296f96fcSRusty Russell if (!dev) 1750296f96fcSRusty Russell return -ENOMEM; 1751296f96fcSRusty Russell 1752296f96fcSRusty Russell /* Set up network device as normal. */ 1753f2f2c8b4SJiri Pirko dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE; 175476288b4eSStephen Hemminger dev->netdev_ops = &virtnet_netdev; 1755296f96fcSRusty Russell dev->features = NETIF_F_HIGHDMA; 17563fa2a1dfSstephen hemminger 17577ad24ea4SWilfried Klaebe dev->ethtool_ops = &virtnet_ethtool_ops; 1758296f96fcSRusty Russell SET_NETDEV_DEV(dev, &vdev->dev); 1759296f96fcSRusty Russell 1760296f96fcSRusty Russell /* Do we support "hardware" checksums? */ 176198e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) { 1762296f96fcSRusty Russell /* This opens up the world of extra features. */ 176348900cb6SJason Wang dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG; 176498e778c9SMichał Mirosław if (csum) 176548900cb6SJason Wang dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG; 176698e778c9SMichał Mirosław 176798e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) { 1768e3e3c423SVlad Yasevich dev->hw_features |= NETIF_F_TSO | NETIF_F_UFO 176934a48579SRusty Russell | NETIF_F_TSO_ECN | NETIF_F_TSO6; 177034a48579SRusty Russell } 17715539ae96SRusty Russell /* Individual feature bits: what can host handle? */ 177298e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4)) 177398e778c9SMichał Mirosław dev->hw_features |= NETIF_F_TSO; 177498e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6)) 177598e778c9SMichał Mirosław dev->hw_features |= NETIF_F_TSO6; 177698e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN)) 177798e778c9SMichał Mirosław dev->hw_features |= NETIF_F_TSO_ECN; 1778e3e3c423SVlad Yasevich if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO)) 1779e3e3c423SVlad Yasevich dev->hw_features |= NETIF_F_UFO; 178098e778c9SMichał Mirosław 178141f2f127SJason Wang dev->features |= NETIF_F_GSO_ROBUST; 178241f2f127SJason Wang 178398e778c9SMichał Mirosław if (gso) 1784e3e3c423SVlad Yasevich dev->features |= dev->hw_features & (NETIF_F_ALL_TSO|NETIF_F_UFO); 178598e778c9SMichał Mirosław /* (!csum && gso) case will be fixed by register_netdev() */ 1786296f96fcSRusty Russell } 17874f49129bSThomas Huth if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM)) 17884f49129bSThomas Huth dev->features |= NETIF_F_RXCSUM; 1789296f96fcSRusty Russell 17904fda8302SJason Wang dev->vlan_features = dev->features; 17914fda8302SJason Wang 1792296f96fcSRusty Russell /* Configuration may specify what MAC to use. Otherwise random. */ 1793855e0c52SRusty Russell if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) 1794855e0c52SRusty Russell virtio_cread_bytes(vdev, 1795a586d4f6SRusty Russell offsetof(struct virtio_net_config, mac), 1796855e0c52SRusty Russell dev->dev_addr, dev->addr_len); 1797855e0c52SRusty Russell else 1798f2cedb63SDanny Kukawka eth_hw_addr_random(dev); 1799296f96fcSRusty Russell 1800296f96fcSRusty Russell /* Set up our device-specific information */ 1801296f96fcSRusty Russell vi = netdev_priv(dev); 1802296f96fcSRusty Russell vi->dev = dev; 1803296f96fcSRusty Russell vi->vdev = vdev; 1804d9d5dcc8SChristian Borntraeger vdev->priv = vi; 18053fa2a1dfSstephen hemminger vi->stats = alloc_percpu(struct virtnet_stats); 18063fa2a1dfSstephen hemminger err = -ENOMEM; 18073fa2a1dfSstephen hemminger if (vi->stats == NULL) 18083fa2a1dfSstephen hemminger goto free; 18093fa2a1dfSstephen hemminger 1810827da44cSJohn Stultz for_each_possible_cpu(i) { 1811827da44cSJohn Stultz struct virtnet_stats *virtnet_stats; 1812827da44cSJohn Stultz virtnet_stats = per_cpu_ptr(vi->stats, i); 1813827da44cSJohn Stultz u64_stats_init(&virtnet_stats->tx_syncp); 1814827da44cSJohn Stultz u64_stats_init(&virtnet_stats->rx_syncp); 1815827da44cSJohn Stultz } 1816827da44cSJohn Stultz 1817586d17c5SJason Wang INIT_WORK(&vi->config_work, virtnet_config_changed_work); 1818296f96fcSRusty Russell 181997402b96SHerbert Xu /* If we can receive ANY GSO packets, we must allocate large ones. */ 18208e95a202SJoe Perches if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) || 18218e95a202SJoe Perches virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) || 1822e3e3c423SVlad Yasevich virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) || 1823e3e3c423SVlad Yasevich virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO)) 182497402b96SHerbert Xu vi->big_packets = true; 182597402b96SHerbert Xu 18263f2c31d9SMark McLoughlin if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF)) 18273f2c31d9SMark McLoughlin vi->mergeable_rx_bufs = true; 18283f2c31d9SMark McLoughlin 1829d04302b3SMichael S. Tsirkin if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) || 1830d04302b3SMichael S. Tsirkin virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) 1831012873d0SMichael S. Tsirkin vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf); 1832012873d0SMichael S. Tsirkin else 1833012873d0SMichael S. Tsirkin vi->hdr_len = sizeof(struct virtio_net_hdr); 1834012873d0SMichael S. Tsirkin 183575993300SMichael S. Tsirkin if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) || 183675993300SMichael S. Tsirkin virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) 1837e7428e95SMichael S. Tsirkin vi->any_header_sg = true; 1838e7428e95SMichael S. Tsirkin 1839986a4f4dSJason Wang if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) 1840986a4f4dSJason Wang vi->has_cvq = true; 1841986a4f4dSJason Wang 184214de9d11SAaron Conole if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) { 184314de9d11SAaron Conole mtu = virtio_cread16(vdev, 184414de9d11SAaron Conole offsetof(struct virtio_net_config, 184514de9d11SAaron Conole mtu)); 184614de9d11SAaron Conole if (virtnet_change_mtu(dev, mtu)) 184714de9d11SAaron Conole __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU); 184814de9d11SAaron Conole } 184914de9d11SAaron Conole 1850012873d0SMichael S. Tsirkin if (vi->any_header_sg) 1851012873d0SMichael S. Tsirkin dev->needed_headroom = vi->hdr_len; 18526ebbc1a6SZhangjie \(HZ\) 1853986a4f4dSJason Wang /* Use single tx/rx queue pair as default */ 1854986a4f4dSJason Wang vi->curr_queue_pairs = 1; 1855986a4f4dSJason Wang vi->max_queue_pairs = max_queue_pairs; 1856986a4f4dSJason Wang 1857986a4f4dSJason Wang /* Allocate/initialize the rx/tx queues, and invoke find_vqs */ 18583f9c10b0SAmit Shah err = init_vqs(vi); 1859d2a7dddaSMichael S. Tsirkin if (err) 18609bb8ca86SJason Wang goto free_stats; 1861d2a7dddaSMichael S. Tsirkin 1862fbf28d78SMichael Dalton #ifdef CONFIG_SYSFS 1863fbf28d78SMichael Dalton if (vi->mergeable_rx_bufs) 1864fbf28d78SMichael Dalton dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group; 1865fbf28d78SMichael Dalton #endif 18660f13b66bSZhi Yong Wu netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs); 18670f13b66bSZhi Yong Wu netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs); 1868986a4f4dSJason Wang 186916032be5SNikolay Aleksandrov virtnet_init_settings(dev); 187016032be5SNikolay Aleksandrov 1871296f96fcSRusty Russell err = register_netdev(dev); 1872296f96fcSRusty Russell if (err) { 1873296f96fcSRusty Russell pr_debug("virtio_net: registering device failed\n"); 1874d2a7dddaSMichael S. Tsirkin goto free_vqs; 1875296f96fcSRusty Russell } 1876b3369c1fSRusty Russell 18774baf1e33SMichael S. Tsirkin virtio_device_ready(vdev); 18784baf1e33SMichael S. Tsirkin 18798de4b2f3SWanlong Gao vi->nb.notifier_call = &virtnet_cpu_callback; 18808de4b2f3SWanlong Gao err = register_hotcpu_notifier(&vi->nb); 18818de4b2f3SWanlong Gao if (err) { 18828de4b2f3SWanlong Gao pr_debug("virtio_net: registering cpu notifier failed\n"); 1883f00e35e2Swangyunjian goto free_unregister_netdev; 18848de4b2f3SWanlong Gao } 18858de4b2f3SWanlong Gao 1886167c25e4SJason Wang /* Assume link up if device can't report link status, 1887167c25e4SJason Wang otherwise get link status from config. */ 1888167c25e4SJason Wang if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) { 1889167c25e4SJason Wang netif_carrier_off(dev); 18903b07e9caSTejun Heo schedule_work(&vi->config_work); 1891167c25e4SJason Wang } else { 1892167c25e4SJason Wang vi->status = VIRTIO_NET_S_LINK_UP; 18934783256eSPantelis Koukousoulas netif_carrier_on(dev); 1894167c25e4SJason Wang } 18959f4d26d0SMark McLoughlin 1896986a4f4dSJason Wang pr_debug("virtnet: registered device %s with %d RX and TX vq's\n", 1897986a4f4dSJason Wang dev->name, max_queue_pairs); 1898986a4f4dSJason Wang 1899296f96fcSRusty Russell return 0; 1900296f96fcSRusty Russell 1901f00e35e2Swangyunjian free_unregister_netdev: 190202465555SMichael S. Tsirkin vi->vdev->config->reset(vdev); 190302465555SMichael S. Tsirkin 1904b3369c1fSRusty Russell unregister_netdev(dev); 1905d2a7dddaSMichael S. Tsirkin free_vqs: 1906986a4f4dSJason Wang cancel_delayed_work_sync(&vi->refill); 1907fb51879dSMichael Dalton free_receive_page_frags(vi); 1908e9d7417bSJason Wang virtnet_del_vqs(vi); 19093fa2a1dfSstephen hemminger free_stats: 19103fa2a1dfSstephen hemminger free_percpu(vi->stats); 1911296f96fcSRusty Russell free: 1912296f96fcSRusty Russell free_netdev(dev); 1913296f96fcSRusty Russell return err; 1914296f96fcSRusty Russell } 1915296f96fcSRusty Russell 191604486ed0SAmit Shah static void remove_vq_common(struct virtnet_info *vi) 1917296f96fcSRusty Russell { 191804486ed0SAmit Shah vi->vdev->config->reset(vi->vdev); 1919830a8a97SShirley Ma 1920830a8a97SShirley Ma /* Free unused buffers in both send and recv, if any. */ 19219ab86bbcSShirley Ma free_unused_bufs(vi); 1922fb6813f4SRusty Russell 1923986a4f4dSJason Wang free_receive_bufs(vi); 1924d2a7dddaSMichael S. Tsirkin 1925fb51879dSMichael Dalton free_receive_page_frags(vi); 1926fb51879dSMichael Dalton 1927986a4f4dSJason Wang virtnet_del_vqs(vi); 192804486ed0SAmit Shah } 192904486ed0SAmit Shah 19308cc085d6SBill Pemberton static void virtnet_remove(struct virtio_device *vdev) 193104486ed0SAmit Shah { 193204486ed0SAmit Shah struct virtnet_info *vi = vdev->priv; 193304486ed0SAmit Shah 19348de4b2f3SWanlong Gao unregister_hotcpu_notifier(&vi->nb); 19358de4b2f3SWanlong Gao 1936102a2786SMichael S. Tsirkin /* Make sure no work handler is accessing the device. */ 1937102a2786SMichael S. Tsirkin flush_work(&vi->config_work); 1938586d17c5SJason Wang 193904486ed0SAmit Shah unregister_netdev(vi->dev); 194004486ed0SAmit Shah 194104486ed0SAmit Shah remove_vq_common(vi); 1942fb6813f4SRusty Russell 19432e66f55bSKrishna Kumar free_percpu(vi->stats); 194474b2553fSRusty Russell free_netdev(vi->dev); 1945296f96fcSRusty Russell } 1946296f96fcSRusty Russell 194789107000SAaron Lu #ifdef CONFIG_PM_SLEEP 19480741bcb5SAmit Shah static int virtnet_freeze(struct virtio_device *vdev) 19490741bcb5SAmit Shah { 19500741bcb5SAmit Shah struct virtnet_info *vi = vdev->priv; 1951986a4f4dSJason Wang int i; 19520741bcb5SAmit Shah 1953ec9debbdSJason Wang unregister_hotcpu_notifier(&vi->nb); 1954ec9debbdSJason Wang 1955102a2786SMichael S. Tsirkin /* Make sure no work handler is accessing the device */ 1956102a2786SMichael S. Tsirkin flush_work(&vi->config_work); 1957586d17c5SJason Wang 19580741bcb5SAmit Shah netif_device_detach(vi->dev); 19590741bcb5SAmit Shah cancel_delayed_work_sync(&vi->refill); 19600741bcb5SAmit Shah 196191815639SJason Wang if (netif_running(vi->dev)) { 1962ab3971b1SJason Wang for (i = 0; i < vi->max_queue_pairs; i++) 1963986a4f4dSJason Wang napi_disable(&vi->rq[i].napi); 196491815639SJason Wang } 19650741bcb5SAmit Shah 19660741bcb5SAmit Shah remove_vq_common(vi); 19670741bcb5SAmit Shah 19680741bcb5SAmit Shah return 0; 19690741bcb5SAmit Shah } 19700741bcb5SAmit Shah 19710741bcb5SAmit Shah static int virtnet_restore(struct virtio_device *vdev) 19720741bcb5SAmit Shah { 19730741bcb5SAmit Shah struct virtnet_info *vi = vdev->priv; 1974986a4f4dSJason Wang int err, i; 19750741bcb5SAmit Shah 19760741bcb5SAmit Shah err = init_vqs(vi); 19770741bcb5SAmit Shah if (err) 19780741bcb5SAmit Shah return err; 19790741bcb5SAmit Shah 1980e53fbd11SMichael S. Tsirkin virtio_device_ready(vdev); 1981e53fbd11SMichael S. Tsirkin 19826cd4ce00SJason Wang if (netif_running(vi->dev)) { 198355257d72SSasha Levin for (i = 0; i < vi->curr_queue_pairs; i++) 1984946fa564SMichael S. Tsirkin if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL)) 19853b07e9caSTejun Heo schedule_delayed_work(&vi->refill, 0); 19860741bcb5SAmit Shah 19876cd4ce00SJason Wang for (i = 0; i < vi->max_queue_pairs; i++) 19886cd4ce00SJason Wang virtnet_napi_enable(&vi->rq[i]); 19896cd4ce00SJason Wang } 19906cd4ce00SJason Wang 19916cd4ce00SJason Wang netif_device_attach(vi->dev); 19926cd4ce00SJason Wang 199335ed159bSJason Wang rtnl_lock(); 1994986a4f4dSJason Wang virtnet_set_queues(vi, vi->curr_queue_pairs); 199535ed159bSJason Wang rtnl_unlock(); 1996986a4f4dSJason Wang 1997ec9debbdSJason Wang err = register_hotcpu_notifier(&vi->nb); 1998ec9debbdSJason Wang if (err) 1999ec9debbdSJason Wang return err; 2000ec9debbdSJason Wang 20010741bcb5SAmit Shah return 0; 20020741bcb5SAmit Shah } 20030741bcb5SAmit Shah #endif 20040741bcb5SAmit Shah 2005296f96fcSRusty Russell static struct virtio_device_id id_table[] = { 2006296f96fcSRusty Russell { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID }, 2007296f96fcSRusty Russell { 0 }, 2008296f96fcSRusty Russell }; 2009296f96fcSRusty Russell 2010c45a6816SRusty Russell static unsigned int features[] = { 20115e4fe5c4SMark McLoughlin VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, 20125e4fe5c4SMark McLoughlin VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC, 2013e3e3c423SVlad Yasevich VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, 201497402b96SHerbert Xu VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, 2015e3e3c423SVlad Yasevich VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, 20162a41f71dSAlex Williamson VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, 20170bde9569SAlex Williamson VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, 2018986a4f4dSJason Wang VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, 20197e58d5aeSAmos Kong VIRTIO_NET_F_CTRL_MAC_ADDR, 2020e7428e95SMichael S. Tsirkin VIRTIO_F_ANY_LAYOUT, 202114de9d11SAaron Conole VIRTIO_NET_F_MTU, 2022c45a6816SRusty Russell }; 2023c45a6816SRusty Russell 202422402529SUwe Kleine-König static struct virtio_driver virtio_net_driver = { 2025c45a6816SRusty Russell .feature_table = features, 2026c45a6816SRusty Russell .feature_table_size = ARRAY_SIZE(features), 2027296f96fcSRusty Russell .driver.name = KBUILD_MODNAME, 2028296f96fcSRusty Russell .driver.owner = THIS_MODULE, 2029296f96fcSRusty Russell .id_table = id_table, 2030296f96fcSRusty Russell .probe = virtnet_probe, 20318cc085d6SBill Pemberton .remove = virtnet_remove, 20329f4d26d0SMark McLoughlin .config_changed = virtnet_config_changed, 203389107000SAaron Lu #ifdef CONFIG_PM_SLEEP 20340741bcb5SAmit Shah .freeze = virtnet_freeze, 20350741bcb5SAmit Shah .restore = virtnet_restore, 20360741bcb5SAmit Shah #endif 2037296f96fcSRusty Russell }; 2038296f96fcSRusty Russell 2039b2a17029SRusty Russell module_virtio_driver(virtio_net_driver); 2040296f96fcSRusty Russell 2041296f96fcSRusty Russell MODULE_DEVICE_TABLE(virtio, id_table); 2042296f96fcSRusty Russell MODULE_DESCRIPTION("Virtio network driver"); 2043296f96fcSRusty Russell MODULE_LICENSE("GPL"); 2044