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 */ 2639ab86bbcSShirley Ma skb = netdev_alloc_skb_ip_align(vi->dev, 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 482b3f24698SRusty Russell if (hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) { 483296f96fcSRusty Russell pr_debug("Needs csum!\n"); 484b3f24698SRusty Russell if (!skb_partial_csum_set(skb, 485fdd819b2SMichael S. Tsirkin virtio16_to_cpu(vi->vdev, hdr->hdr.csum_start), 486fdd819b2SMichael S. Tsirkin virtio16_to_cpu(vi->vdev, hdr->hdr.csum_offset))) 487296f96fcSRusty Russell goto frame_err; 48810a8d94aSJason Wang } else if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID) { 48910a8d94aSJason Wang skb->ip_summed = CHECKSUM_UNNECESSARY; 490296f96fcSRusty Russell } 491296f96fcSRusty Russell 49223cde76dSMark McLoughlin skb->protocol = eth_type_trans(skb, dev); 49323cde76dSMark McLoughlin pr_debug("Receiving skb proto 0x%04x len %i type %i\n", 49423cde76dSMark McLoughlin ntohs(skb->protocol), skb->len, skb->pkt_type); 49523cde76dSMark McLoughlin 496b3f24698SRusty Russell if (hdr->hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) { 497296f96fcSRusty Russell pr_debug("GSO!\n"); 498b3f24698SRusty Russell switch (hdr->hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) { 499296f96fcSRusty Russell case VIRTIO_NET_HDR_GSO_TCPV4: 500c9af6db4SPravin B Shelar skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4; 501296f96fcSRusty Russell break; 502296f96fcSRusty Russell case VIRTIO_NET_HDR_GSO_UDP: 503c9af6db4SPravin B Shelar skb_shinfo(skb)->gso_type = SKB_GSO_UDP; 504296f96fcSRusty Russell break; 505296f96fcSRusty Russell case VIRTIO_NET_HDR_GSO_TCPV6: 506c9af6db4SPravin B Shelar skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6; 507296f96fcSRusty Russell break; 508296f96fcSRusty Russell default: 509be443899SAmerigo Wang net_warn_ratelimited("%s: bad gso type %u.\n", 510b3f24698SRusty Russell dev->name, hdr->hdr.gso_type); 511296f96fcSRusty Russell goto frame_err; 512296f96fcSRusty Russell } 513296f96fcSRusty Russell 514b3f24698SRusty Russell if (hdr->hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN) 515c9af6db4SPravin B Shelar skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN; 51634a48579SRusty Russell 517fdd819b2SMichael S. Tsirkin skb_shinfo(skb)->gso_size = virtio16_to_cpu(vi->vdev, 518fdd819b2SMichael S. Tsirkin hdr->hdr.gso_size); 519296f96fcSRusty Russell if (skb_shinfo(skb)->gso_size == 0) { 520be443899SAmerigo Wang net_warn_ratelimited("%s: zero gso size.\n", dev->name); 521296f96fcSRusty Russell goto frame_err; 522296f96fcSRusty Russell } 523296f96fcSRusty Russell 524296f96fcSRusty Russell /* Header must be checked, and gso_segs computed. */ 525296f96fcSRusty Russell skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY; 526296f96fcSRusty Russell skb_shinfo(skb)->gso_segs = 0; 527296f96fcSRusty Russell } 528296f96fcSRusty Russell 5290fbd050aSEric Dumazet napi_gro_receive(&rq->napi, skb); 530296f96fcSRusty Russell return; 531296f96fcSRusty Russell 532296f96fcSRusty Russell frame_err: 533296f96fcSRusty Russell dev->stats.rx_frame_errors++; 534296f96fcSRusty Russell dev_kfree_skb(skb); 535296f96fcSRusty Russell } 536296f96fcSRusty Russell 537946fa564SMichael S. Tsirkin static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq, 538946fa564SMichael S. Tsirkin gfp_t gfp) 539296f96fcSRusty Russell { 540296f96fcSRusty Russell struct sk_buff *skb; 541012873d0SMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf *hdr; 5429ab86bbcSShirley Ma int err; 5433f2c31d9SMark McLoughlin 5445061de36SMichael Dalton skb = __netdev_alloc_skb_ip_align(vi->dev, GOOD_PACKET_LEN, gfp); 5459ab86bbcSShirley Ma if (unlikely(!skb)) 5469ab86bbcSShirley Ma return -ENOMEM; 547296f96fcSRusty Russell 5485061de36SMichael Dalton skb_put(skb, GOOD_PACKET_LEN); 5493f2c31d9SMark McLoughlin 5503f2c31d9SMark McLoughlin hdr = skb_vnet_hdr(skb); 551547c890cSJason Wang sg_init_table(rq->sg, 2); 552012873d0SMichael S. Tsirkin sg_set_buf(rq->sg, hdr, vi->hdr_len); 553e9d7417bSJason Wang skb_to_sgvec(skb, rq->sg + 1, 0, skb->len); 55497402b96SHerbert Xu 5559dc7b9e4SRusty Russell err = virtqueue_add_inbuf(rq->vq, rq->sg, 2, skb, gfp); 5569ab86bbcSShirley Ma if (err < 0) 5579ab86bbcSShirley Ma dev_kfree_skb(skb); 55897402b96SHerbert Xu 5599ab86bbcSShirley Ma return err; 56097402b96SHerbert Xu } 56197402b96SHerbert Xu 562012873d0SMichael S. Tsirkin static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq, 563012873d0SMichael S. Tsirkin gfp_t gfp) 5649ab86bbcSShirley Ma { 5659ab86bbcSShirley Ma struct page *first, *list = NULL; 5669ab86bbcSShirley Ma char *p; 5679ab86bbcSShirley Ma int i, err, offset; 568296f96fcSRusty Russell 569a5835440SRusty Russell sg_init_table(rq->sg, MAX_SKB_FRAGS + 2); 570a5835440SRusty Russell 571e9d7417bSJason Wang /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */ 5729ab86bbcSShirley Ma for (i = MAX_SKB_FRAGS + 1; i > 1; --i) { 573e9d7417bSJason Wang first = get_a_page(rq, gfp); 5749ab86bbcSShirley Ma if (!first) { 5759ab86bbcSShirley Ma if (list) 576e9d7417bSJason Wang give_pages(rq, list); 5779ab86bbcSShirley Ma return -ENOMEM; 578296f96fcSRusty Russell } 579e9d7417bSJason Wang sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE); 5809ab86bbcSShirley Ma 5819ab86bbcSShirley Ma /* chain new page in list head to match sg */ 5829ab86bbcSShirley Ma first->private = (unsigned long)list; 5839ab86bbcSShirley Ma list = first; 5849ab86bbcSShirley Ma } 5859ab86bbcSShirley Ma 586e9d7417bSJason Wang first = get_a_page(rq, gfp); 5879ab86bbcSShirley Ma if (!first) { 588e9d7417bSJason Wang give_pages(rq, list); 5899ab86bbcSShirley Ma return -ENOMEM; 5909ab86bbcSShirley Ma } 5919ab86bbcSShirley Ma p = page_address(first); 5929ab86bbcSShirley Ma 593e9d7417bSJason Wang /* rq->sg[0], rq->sg[1] share the same page */ 594012873d0SMichael S. Tsirkin /* a separated rq->sg[0] for header - required in case !any_header_sg */ 595012873d0SMichael S. Tsirkin sg_set_buf(&rq->sg[0], p, vi->hdr_len); 5969ab86bbcSShirley Ma 597e9d7417bSJason Wang /* rq->sg[1] for data packet, from offset */ 5989ab86bbcSShirley Ma offset = sizeof(struct padded_vnet_hdr); 599e9d7417bSJason Wang sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset); 6009ab86bbcSShirley Ma 6019ab86bbcSShirley Ma /* chain first in list head */ 6029ab86bbcSShirley Ma first->private = (unsigned long)list; 6039dc7b9e4SRusty Russell err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2, 604aa989f5eSMichael S. Tsirkin first, gfp); 6059ab86bbcSShirley Ma if (err < 0) 606e9d7417bSJason Wang give_pages(rq, first); 6079ab86bbcSShirley Ma 6089ab86bbcSShirley Ma return err; 6099ab86bbcSShirley Ma } 6109ab86bbcSShirley Ma 6115377d758SJohannes Berg static unsigned int get_mergeable_buf_len(struct ewma_pkt_len *avg_pkt_len) 6129ab86bbcSShirley Ma { 613ab7db917SMichael Dalton const size_t hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf); 614fbf28d78SMichael Dalton unsigned int len; 615fbf28d78SMichael Dalton 6165377d758SJohannes Berg len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len), 617fbf28d78SMichael Dalton GOOD_PACKET_LEN, PAGE_SIZE - hdr_len); 618fbf28d78SMichael Dalton return ALIGN(len, MERGEABLE_BUFFER_ALIGN); 619fbf28d78SMichael Dalton } 620fbf28d78SMichael Dalton 621fbf28d78SMichael Dalton static int add_recvbuf_mergeable(struct receive_queue *rq, gfp_t gfp) 622fbf28d78SMichael Dalton { 623fb51879dSMichael Dalton struct page_frag *alloc_frag = &rq->alloc_frag; 624fb51879dSMichael Dalton char *buf; 625ab7db917SMichael Dalton unsigned long ctx; 6269ab86bbcSShirley Ma int err; 627fb51879dSMichael Dalton unsigned int len, hole; 6289ab86bbcSShirley Ma 629fbf28d78SMichael Dalton len = get_mergeable_buf_len(&rq->mrg_avg_pkt_len); 630ab7db917SMichael Dalton if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp))) 6319ab86bbcSShirley Ma return -ENOMEM; 632ab7db917SMichael Dalton 633fb51879dSMichael Dalton buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset; 634ab7db917SMichael Dalton ctx = mergeable_buf_to_ctx(buf, len); 635fb51879dSMichael Dalton get_page(alloc_frag->page); 636fb51879dSMichael Dalton alloc_frag->offset += len; 637fb51879dSMichael Dalton hole = alloc_frag->size - alloc_frag->offset; 638ab7db917SMichael Dalton if (hole < len) { 639ab7db917SMichael Dalton /* To avoid internal fragmentation, if there is very likely not 640ab7db917SMichael Dalton * enough space for another buffer, add the remaining space to 641ab7db917SMichael Dalton * the current buffer. This extra space is not included in 642ab7db917SMichael Dalton * the truesize stored in ctx. 643ab7db917SMichael Dalton */ 644fb51879dSMichael Dalton len += hole; 645fb51879dSMichael Dalton alloc_frag->offset += hole; 646fb51879dSMichael Dalton } 6479ab86bbcSShirley Ma 648fb51879dSMichael Dalton sg_init_one(rq->sg, buf, len); 649ab7db917SMichael Dalton err = virtqueue_add_inbuf(rq->vq, rq->sg, 1, (void *)ctx, gfp); 6509ab86bbcSShirley Ma if (err < 0) 6512613af0eSMichael Dalton put_page(virt_to_head_page(buf)); 6529ab86bbcSShirley Ma 6539ab86bbcSShirley Ma return err; 654296f96fcSRusty Russell } 655296f96fcSRusty Russell 656b2baed69SRusty Russell /* 657b2baed69SRusty Russell * Returns false if we couldn't fill entirely (OOM). 658b2baed69SRusty Russell * 659b2baed69SRusty Russell * Normally run in the receive path, but can also be run from ndo_open 660b2baed69SRusty Russell * before we're receiving packets, or from refill_work which is 661b2baed69SRusty Russell * careful to disable receiving (using napi_disable). 662b2baed69SRusty Russell */ 663946fa564SMichael S. Tsirkin static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq, 664946fa564SMichael S. Tsirkin gfp_t gfp) 6653f2c31d9SMark McLoughlin { 6663f2c31d9SMark McLoughlin int err; 6671788f495SMichael S. Tsirkin bool oom; 6683f2c31d9SMark McLoughlin 669fb51879dSMichael Dalton gfp |= __GFP_COLD; 6700aea51c3SAmit Shah do { 6719ab86bbcSShirley Ma if (vi->mergeable_rx_bufs) 672e9d7417bSJason Wang err = add_recvbuf_mergeable(rq, gfp); 6739ab86bbcSShirley Ma else if (vi->big_packets) 674012873d0SMichael S. Tsirkin err = add_recvbuf_big(vi, rq, gfp); 6759ab86bbcSShirley Ma else 676946fa564SMichael S. Tsirkin err = add_recvbuf_small(vi, rq, gfp); 6773f2c31d9SMark McLoughlin 6781788f495SMichael S. Tsirkin oom = err == -ENOMEM; 6799ed4cb07SRusty Russell if (err) 6803f2c31d9SMark McLoughlin break; 681b7dfde95SLinus Torvalds } while (rq->vq->num_free); 682681daee2SJason Wang virtqueue_kick(rq->vq); 6833161e453SRusty Russell return !oom; 6843f2c31d9SMark McLoughlin } 6853f2c31d9SMark McLoughlin 68618445c4dSRusty Russell static void skb_recv_done(struct virtqueue *rvq) 687296f96fcSRusty Russell { 688296f96fcSRusty Russell struct virtnet_info *vi = rvq->vdev->priv; 689986a4f4dSJason Wang struct receive_queue *rq = &vi->rq[vq2rxq(rvq)]; 690e9d7417bSJason Wang 69118445c4dSRusty Russell /* Schedule NAPI, Suppress further interrupts if successful. */ 692e9d7417bSJason Wang if (napi_schedule_prep(&rq->napi)) { 6931915a712SMichael S. Tsirkin virtqueue_disable_cb(rvq); 694e9d7417bSJason Wang __napi_schedule(&rq->napi); 69518445c4dSRusty Russell } 696296f96fcSRusty Russell } 697296f96fcSRusty Russell 698e9d7417bSJason Wang static void virtnet_napi_enable(struct receive_queue *rq) 6993e9d08ecSBruce Rogers { 700e9d7417bSJason Wang napi_enable(&rq->napi); 7013e9d08ecSBruce Rogers 7023e9d08ecSBruce Rogers /* If all buffers were filled by other side before we napi_enabled, we 7033e9d08ecSBruce Rogers * won't get another interrupt, so process any outstanding packets 7043e9d08ecSBruce Rogers * now. virtnet_poll wants re-enable the queue, so we disable here. 7053e9d08ecSBruce Rogers * We synchronize against interrupts via NAPI_STATE_SCHED */ 706e9d7417bSJason Wang if (napi_schedule_prep(&rq->napi)) { 707e9d7417bSJason Wang virtqueue_disable_cb(rq->vq); 708ec13ee80SMichael S. Tsirkin local_bh_disable(); 709e9d7417bSJason Wang __napi_schedule(&rq->napi); 710ec13ee80SMichael S. Tsirkin local_bh_enable(); 7113e9d08ecSBruce Rogers } 7123e9d08ecSBruce Rogers } 7133e9d08ecSBruce Rogers 7143161e453SRusty Russell static void refill_work(struct work_struct *work) 7153161e453SRusty Russell { 716e9d7417bSJason Wang struct virtnet_info *vi = 717e9d7417bSJason Wang container_of(work, struct virtnet_info, refill.work); 7183161e453SRusty Russell bool still_empty; 719986a4f4dSJason Wang int i; 7203161e453SRusty Russell 72155257d72SSasha Levin for (i = 0; i < vi->curr_queue_pairs; i++) { 722986a4f4dSJason Wang struct receive_queue *rq = &vi->rq[i]; 723986a4f4dSJason Wang 724986a4f4dSJason Wang napi_disable(&rq->napi); 725946fa564SMichael S. Tsirkin still_empty = !try_fill_recv(vi, rq, GFP_KERNEL); 726986a4f4dSJason Wang virtnet_napi_enable(rq); 7273161e453SRusty Russell 7283161e453SRusty Russell /* In theory, this can happen: if we don't get any buffers in 729986a4f4dSJason Wang * we will *never* try to fill again. 730986a4f4dSJason Wang */ 7313161e453SRusty Russell if (still_empty) 7323b07e9caSTejun Heo schedule_delayed_work(&vi->refill, HZ/2); 7333161e453SRusty Russell } 734986a4f4dSJason Wang } 7353161e453SRusty Russell 7362ffa7598SJason Wang static int virtnet_receive(struct receive_queue *rq, int budget) 737296f96fcSRusty Russell { 738e9d7417bSJason Wang struct virtnet_info *vi = rq->vq->vdev->priv; 7392ffa7598SJason Wang unsigned int len, received = 0; 7409ab86bbcSShirley Ma void *buf; 741296f96fcSRusty Russell 742296f96fcSRusty Russell while (received < budget && 743e9d7417bSJason Wang (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) { 744946fa564SMichael S. Tsirkin receive_buf(vi, rq, buf, len); 745296f96fcSRusty Russell received++; 746296f96fcSRusty Russell } 747296f96fcSRusty Russell 748be121f46SJason Wang if (rq->vq->num_free > virtqueue_get_vring_size(rq->vq) / 2) { 749946fa564SMichael S. Tsirkin if (!try_fill_recv(vi, rq, GFP_ATOMIC)) 7503b07e9caSTejun Heo schedule_delayed_work(&vi->refill, 0); 7513161e453SRusty Russell } 752296f96fcSRusty Russell 7532ffa7598SJason Wang return received; 7542ffa7598SJason Wang } 7552ffa7598SJason Wang 7562ffa7598SJason Wang static int virtnet_poll(struct napi_struct *napi, int budget) 7572ffa7598SJason Wang { 7582ffa7598SJason Wang struct receive_queue *rq = 7592ffa7598SJason Wang container_of(napi, struct receive_queue, napi); 760faadb05fSLi RongQing unsigned int r, received; 7612ffa7598SJason Wang 762faadb05fSLi RongQing received = virtnet_receive(rq, budget); 7632ffa7598SJason Wang 7648329d98eSRusty Russell /* Out of packets? */ 7658329d98eSRusty Russell if (received < budget) { 766cbdadbbfSMichael S. Tsirkin r = virtqueue_enable_cb_prepare(rq->vq); 7670fbd050aSEric Dumazet napi_complete_done(napi, received); 768cbdadbbfSMichael S. Tsirkin if (unlikely(virtqueue_poll(rq->vq, r)) && 7698e95a202SJoe Perches napi_schedule_prep(napi)) { 770e9d7417bSJason Wang virtqueue_disable_cb(rq->vq); 771288379f0SBen Hutchings __napi_schedule(napi); 772296f96fcSRusty Russell } 7734265f161SChristian Borntraeger } 774296f96fcSRusty Russell 775296f96fcSRusty Russell return received; 776296f96fcSRusty Russell } 777296f96fcSRusty Russell 77891815639SJason Wang #ifdef CONFIG_NET_RX_BUSY_POLL 77991815639SJason Wang /* must be called with local_bh_disable()d */ 78091815639SJason Wang static int virtnet_busy_poll(struct napi_struct *napi) 78191815639SJason Wang { 78291815639SJason Wang struct receive_queue *rq = 78391815639SJason Wang container_of(napi, struct receive_queue, napi); 78491815639SJason Wang struct virtnet_info *vi = rq->vq->vdev->priv; 78591815639SJason Wang int r, received = 0, budget = 4; 78691815639SJason Wang 78791815639SJason Wang if (!(vi->status & VIRTIO_NET_S_LINK_UP)) 78891815639SJason Wang return LL_FLUSH_FAILED; 78991815639SJason Wang 79091815639SJason Wang if (!napi_schedule_prep(napi)) 79191815639SJason Wang return LL_FLUSH_BUSY; 79291815639SJason Wang 79391815639SJason Wang virtqueue_disable_cb(rq->vq); 79491815639SJason Wang 79591815639SJason Wang again: 79691815639SJason Wang received += virtnet_receive(rq, budget); 79791815639SJason Wang 79891815639SJason Wang r = virtqueue_enable_cb_prepare(rq->vq); 79991815639SJason Wang clear_bit(NAPI_STATE_SCHED, &napi->state); 80091815639SJason Wang if (unlikely(virtqueue_poll(rq->vq, r)) && 80191815639SJason Wang napi_schedule_prep(napi)) { 80291815639SJason Wang virtqueue_disable_cb(rq->vq); 80391815639SJason Wang if (received < budget) { 80491815639SJason Wang budget -= received; 80591815639SJason Wang goto again; 80691815639SJason Wang } else { 80791815639SJason Wang __napi_schedule(napi); 80891815639SJason Wang } 80991815639SJason Wang } 81091815639SJason Wang 81191815639SJason Wang return received; 81291815639SJason Wang } 81391815639SJason Wang #endif /* CONFIG_NET_RX_BUSY_POLL */ 81491815639SJason Wang 815986a4f4dSJason Wang static int virtnet_open(struct net_device *dev) 816986a4f4dSJason Wang { 817986a4f4dSJason Wang struct virtnet_info *vi = netdev_priv(dev); 818986a4f4dSJason Wang int i; 819986a4f4dSJason Wang 820e4166625SJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 821e4166625SJason Wang if (i < vi->curr_queue_pairs) 822986a4f4dSJason Wang /* Make sure we have some buffers: if oom use wq. */ 823946fa564SMichael S. Tsirkin if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL)) 824986a4f4dSJason Wang schedule_delayed_work(&vi->refill, 0); 825986a4f4dSJason Wang virtnet_napi_enable(&vi->rq[i]); 826986a4f4dSJason Wang } 827986a4f4dSJason Wang 828986a4f4dSJason Wang return 0; 829986a4f4dSJason Wang } 830986a4f4dSJason Wang 831b7dfde95SLinus Torvalds static void free_old_xmit_skbs(struct send_queue *sq) 832296f96fcSRusty Russell { 833296f96fcSRusty Russell struct sk_buff *skb; 8346ee57bccSMichael S. Tsirkin unsigned int len; 835e9d7417bSJason Wang struct virtnet_info *vi = sq->vq->vdev->priv; 83658472a76SEric Dumazet struct virtnet_stats *stats = this_cpu_ptr(vi->stats); 837296f96fcSRusty Russell 838e9d7417bSJason Wang while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) { 839296f96fcSRusty Russell pr_debug("Sent skb %p\n", skb); 8403fa2a1dfSstephen hemminger 84183a27052SEric Dumazet u64_stats_update_begin(&stats->tx_syncp); 8423fa2a1dfSstephen hemminger stats->tx_bytes += skb->len; 8433fa2a1dfSstephen hemminger stats->tx_packets++; 84483a27052SEric Dumazet u64_stats_update_end(&stats->tx_syncp); 8453fa2a1dfSstephen hemminger 846ed79bab8SEric Dumazet dev_kfree_skb_any(skb); 847296f96fcSRusty Russell } 848296f96fcSRusty Russell } 849296f96fcSRusty Russell 850e9d7417bSJason Wang static int xmit_skb(struct send_queue *sq, struct sk_buff *skb) 851296f96fcSRusty Russell { 852012873d0SMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf *hdr; 853296f96fcSRusty Russell const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest; 854e9d7417bSJason Wang struct virtnet_info *vi = sq->vq->vdev->priv; 8557bedc7dcSMichael S. Tsirkin unsigned num_sg; 856012873d0SMichael S. Tsirkin unsigned hdr_len = vi->hdr_len; 857e7428e95SMichael S. Tsirkin bool can_push; 858296f96fcSRusty Russell 859e174961cSJohannes Berg pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest); 860e7428e95SMichael S. Tsirkin 861e7428e95SMichael S. Tsirkin can_push = vi->any_header_sg && 862e7428e95SMichael S. Tsirkin !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) && 863e7428e95SMichael S. Tsirkin !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len; 864e7428e95SMichael S. Tsirkin /* Even if we can, don't push here yet as this would skew 865e7428e95SMichael S. Tsirkin * csum_start offset below. */ 866e7428e95SMichael S. Tsirkin if (can_push) 867012873d0SMichael S. Tsirkin hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len); 868e7428e95SMichael S. Tsirkin else 869e7428e95SMichael S. Tsirkin hdr = skb_vnet_hdr(skb); 870296f96fcSRusty Russell 871296f96fcSRusty Russell if (skb->ip_summed == CHECKSUM_PARTIAL) { 872b3f24698SRusty Russell hdr->hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM; 873fdd819b2SMichael S. Tsirkin hdr->hdr.csum_start = cpu_to_virtio16(vi->vdev, 874fdd819b2SMichael S. Tsirkin skb_checksum_start_offset(skb)); 875fdd819b2SMichael S. Tsirkin hdr->hdr.csum_offset = cpu_to_virtio16(vi->vdev, 876fdd819b2SMichael S. Tsirkin skb->csum_offset); 877296f96fcSRusty Russell } else { 878b3f24698SRusty Russell hdr->hdr.flags = 0; 879b3f24698SRusty Russell hdr->hdr.csum_offset = hdr->hdr.csum_start = 0; 880296f96fcSRusty Russell } 881296f96fcSRusty Russell 882296f96fcSRusty Russell if (skb_is_gso(skb)) { 883fdd819b2SMichael S. Tsirkin hdr->hdr.hdr_len = cpu_to_virtio16(vi->vdev, skb_headlen(skb)); 884fdd819b2SMichael S. Tsirkin hdr->hdr.gso_size = cpu_to_virtio16(vi->vdev, 885fdd819b2SMichael S. Tsirkin skb_shinfo(skb)->gso_size); 88634a48579SRusty Russell if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) 887b3f24698SRusty Russell hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4; 888296f96fcSRusty Russell else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) 889b3f24698SRusty Russell hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6; 890e3e3c423SVlad Yasevich else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP) 891e3e3c423SVlad Yasevich hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP; 892296f96fcSRusty Russell else 893296f96fcSRusty Russell BUG(); 89434a48579SRusty Russell if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN) 895b3f24698SRusty Russell hdr->hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN; 896296f96fcSRusty Russell } else { 897b3f24698SRusty Russell hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE; 898b3f24698SRusty Russell hdr->hdr.gso_size = hdr->hdr.hdr_len = 0; 899296f96fcSRusty Russell } 900296f96fcSRusty Russell 901e7428e95SMichael S. Tsirkin if (vi->mergeable_rx_bufs) 902012873d0SMichael S. Tsirkin hdr->num_buffers = 0; 9033f2c31d9SMark McLoughlin 904547c890cSJason Wang sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2)); 905e7428e95SMichael S. Tsirkin if (can_push) { 906e7428e95SMichael S. Tsirkin __skb_push(skb, hdr_len); 907e7428e95SMichael S. Tsirkin num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len); 908e7428e95SMichael S. Tsirkin /* Pull header back to avoid skew in tx bytes calculations. */ 909e7428e95SMichael S. Tsirkin __skb_pull(skb, hdr_len); 910e7428e95SMichael S. Tsirkin } else { 911e7428e95SMichael S. Tsirkin sg_set_buf(sq->sg, hdr, hdr_len); 912b7dfde95SLinus Torvalds num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len) + 1; 913e7428e95SMichael S. Tsirkin } 9149dc7b9e4SRusty Russell return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC); 91511a3a154SRusty Russell } 91611a3a154SRusty Russell 917424efe9cSStephen Hemminger static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev) 91899ffc696SRusty Russell { 91999ffc696SRusty Russell struct virtnet_info *vi = netdev_priv(dev); 920986a4f4dSJason Wang int qnum = skb_get_queue_mapping(skb); 921986a4f4dSJason Wang struct send_queue *sq = &vi->sq[qnum]; 9229ed4cb07SRusty Russell int err; 9234b7fd2e6SMichael S. Tsirkin struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum); 9244b7fd2e6SMichael S. Tsirkin bool kick = !skb->xmit_more; 9252cb9c6baSRusty Russell 9262cb9c6baSRusty Russell /* Free up any pending old buffers before queueing new ones. */ 927e9d7417bSJason Wang free_old_xmit_skbs(sq); 92899ffc696SRusty Russell 929074c3582SJacob Keller /* timestamp packet in software */ 930074c3582SJacob Keller skb_tx_timestamp(skb); 931074c3582SJacob Keller 93203f191baSMichael S. Tsirkin /* Try to transmit */ 933b7dfde95SLinus Torvalds err = xmit_skb(sq, skb); 93499ffc696SRusty Russell 9359ed4cb07SRusty Russell /* This should not happen! */ 936681daee2SJason Wang if (unlikely(err)) { 93758eba97dSRusty Russell dev->stats.tx_fifo_errors++; 9382e57b79cSRick Jones if (net_ratelimit()) 93958eba97dSRusty Russell dev_warn(&dev->dev, 940b7dfde95SLinus Torvalds "Unexpected TXQ (%d) queue failure: %d\n", qnum, err); 94158eba97dSRusty Russell dev->stats.tx_dropped++; 94285e94525SEric W. Biederman dev_kfree_skb_any(skb); 94358eba97dSRusty Russell return NETDEV_TX_OK; 944296f96fcSRusty Russell } 94503f191baSMichael S. Tsirkin 94648925e37SRusty Russell /* Don't wait up for transmitted skbs to be freed. */ 94748925e37SRusty Russell skb_orphan(skb); 94848925e37SRusty Russell nf_reset(skb); 94948925e37SRusty Russell 95060302ff6SMichael S. Tsirkin /* If running out of space, stop queue to avoid getting packets that we 95160302ff6SMichael S. Tsirkin * are then unable to transmit. 95260302ff6SMichael S. Tsirkin * An alternative would be to force queuing layer to requeue the skb by 95360302ff6SMichael S. Tsirkin * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be 95460302ff6SMichael S. Tsirkin * returned in a normal path of operation: it means that driver is not 95560302ff6SMichael S. Tsirkin * maintaining the TX queue stop/start state properly, and causes 95660302ff6SMichael S. Tsirkin * the stack to do a non-trivial amount of useless work. 95760302ff6SMichael S. Tsirkin * Since most packets only take 1 or 2 ring slots, stopping the queue 95860302ff6SMichael S. Tsirkin * early means 16 slots are typically wasted. 959d631b94eSstephen hemminger */ 960b7dfde95SLinus Torvalds if (sq->vq->num_free < 2+MAX_SKB_FRAGS) { 961986a4f4dSJason Wang netif_stop_subqueue(dev, qnum); 962e9d7417bSJason Wang if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) { 96348925e37SRusty Russell /* More just got used, free them then recheck. */ 964b7dfde95SLinus Torvalds free_old_xmit_skbs(sq); 965b7dfde95SLinus Torvalds if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) { 966986a4f4dSJason Wang netif_start_subqueue(dev, qnum); 967e9d7417bSJason Wang virtqueue_disable_cb(sq->vq); 96848925e37SRusty Russell } 96948925e37SRusty Russell } 97048925e37SRusty Russell } 97148925e37SRusty Russell 9724b7fd2e6SMichael S. Tsirkin if (kick || netif_xmit_stopped(txq)) 973c223a078SDavid S. Miller virtqueue_kick(sq->vq); 9740b725a2cSDavid S. Miller 9750b725a2cSDavid S. Miller return NETDEV_TX_OK; 976c223a078SDavid S. Miller } 977c223a078SDavid S. Miller 97840cbfc37SAmos Kong /* 97940cbfc37SAmos Kong * Send command via the control virtqueue and check status. Commands 98040cbfc37SAmos Kong * supported by the hypervisor, as indicated by feature bits, should 981788a8b6dSstephen hemminger * never fail unless improperly formatted. 98240cbfc37SAmos Kong */ 98340cbfc37SAmos Kong static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd, 984d24bae32Sstephen hemminger struct scatterlist *out) 98540cbfc37SAmos Kong { 986f7bc9594SRusty Russell struct scatterlist *sgs[4], hdr, stat; 987d24bae32Sstephen hemminger unsigned out_num = 0, tmp; 98840cbfc37SAmos Kong 98940cbfc37SAmos Kong /* Caller should know better */ 990f7bc9594SRusty Russell BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)); 99140cbfc37SAmos Kong 9922ac46030SMichael S. Tsirkin vi->ctrl_status = ~0; 9932ac46030SMichael S. Tsirkin vi->ctrl_hdr.class = class; 9942ac46030SMichael S. Tsirkin vi->ctrl_hdr.cmd = cmd; 995f7bc9594SRusty Russell /* Add header */ 9962ac46030SMichael S. Tsirkin sg_init_one(&hdr, &vi->ctrl_hdr, sizeof(vi->ctrl_hdr)); 997f7bc9594SRusty Russell sgs[out_num++] = &hdr; 99840cbfc37SAmos Kong 999f7bc9594SRusty Russell if (out) 1000f7bc9594SRusty Russell sgs[out_num++] = out; 100140cbfc37SAmos Kong 1002f7bc9594SRusty Russell /* Add return status. */ 10032ac46030SMichael S. Tsirkin sg_init_one(&stat, &vi->ctrl_status, sizeof(vi->ctrl_status)); 1004d24bae32Sstephen hemminger sgs[out_num] = &stat; 100540cbfc37SAmos Kong 1006d24bae32Sstephen hemminger BUG_ON(out_num + 1 > ARRAY_SIZE(sgs)); 1007a7c58146SRusty Russell virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC); 100840cbfc37SAmos Kong 100967975901SHeinz Graalfs if (unlikely(!virtqueue_kick(vi->cvq))) 10102ac46030SMichael S. Tsirkin return vi->ctrl_status == VIRTIO_NET_OK; 101140cbfc37SAmos Kong 101240cbfc37SAmos Kong /* Spin for a response, the kick causes an ioport write, trapping 101340cbfc37SAmos Kong * into the hypervisor, so the request should be handled immediately. 101440cbfc37SAmos Kong */ 1015047b9b94SHeinz Graalfs while (!virtqueue_get_buf(vi->cvq, &tmp) && 1016047b9b94SHeinz Graalfs !virtqueue_is_broken(vi->cvq)) 101740cbfc37SAmos Kong cpu_relax(); 101840cbfc37SAmos Kong 10192ac46030SMichael S. Tsirkin return vi->ctrl_status == VIRTIO_NET_OK; 102040cbfc37SAmos Kong } 102140cbfc37SAmos Kong 10229c46f6d4SAlex Williamson static int virtnet_set_mac_address(struct net_device *dev, void *p) 10239c46f6d4SAlex Williamson { 10249c46f6d4SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 10259c46f6d4SAlex Williamson struct virtio_device *vdev = vi->vdev; 1026f2f2c8b4SJiri Pirko int ret; 10277e58d5aeSAmos Kong struct sockaddr *addr = p; 10287e58d5aeSAmos Kong struct scatterlist sg; 10299c46f6d4SAlex Williamson 10307e58d5aeSAmos Kong ret = eth_prepare_mac_addr_change(dev, p); 1031f2f2c8b4SJiri Pirko if (ret) 1032f2f2c8b4SJiri Pirko return ret; 10339c46f6d4SAlex Williamson 10347e58d5aeSAmos Kong if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) { 10357e58d5aeSAmos Kong sg_init_one(&sg, addr->sa_data, dev->addr_len); 10367e58d5aeSAmos Kong if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, 1037d24bae32Sstephen hemminger VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) { 10387e58d5aeSAmos Kong dev_warn(&vdev->dev, 10397e58d5aeSAmos Kong "Failed to set mac address by vq command.\n"); 10407e58d5aeSAmos Kong return -EINVAL; 10417e58d5aeSAmos Kong } 10427e93a02fSMichael S. Tsirkin } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) && 10437e93a02fSMichael S. Tsirkin !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) { 1044855e0c52SRusty Russell unsigned int i; 1045855e0c52SRusty Russell 1046855e0c52SRusty Russell /* Naturally, this has an atomicity problem. */ 1047855e0c52SRusty Russell for (i = 0; i < dev->addr_len; i++) 1048855e0c52SRusty Russell virtio_cwrite8(vdev, 1049855e0c52SRusty Russell offsetof(struct virtio_net_config, mac) + 1050855e0c52SRusty Russell i, addr->sa_data[i]); 10517e58d5aeSAmos Kong } 10527e58d5aeSAmos Kong 10537e58d5aeSAmos Kong eth_commit_mac_addr_change(dev, p); 10549c46f6d4SAlex Williamson 10559c46f6d4SAlex Williamson return 0; 10569c46f6d4SAlex Williamson } 10579c46f6d4SAlex Williamson 10583fa2a1dfSstephen hemminger static struct rtnl_link_stats64 *virtnet_stats(struct net_device *dev, 10593fa2a1dfSstephen hemminger struct rtnl_link_stats64 *tot) 10603fa2a1dfSstephen hemminger { 10613fa2a1dfSstephen hemminger struct virtnet_info *vi = netdev_priv(dev); 10623fa2a1dfSstephen hemminger int cpu; 10633fa2a1dfSstephen hemminger unsigned int start; 10643fa2a1dfSstephen hemminger 10653fa2a1dfSstephen hemminger for_each_possible_cpu(cpu) { 106658472a76SEric Dumazet struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu); 10673fa2a1dfSstephen hemminger u64 tpackets, tbytes, rpackets, rbytes; 10683fa2a1dfSstephen hemminger 10693fa2a1dfSstephen hemminger do { 107057a7744eSEric W. Biederman start = u64_stats_fetch_begin_irq(&stats->tx_syncp); 10713fa2a1dfSstephen hemminger tpackets = stats->tx_packets; 10723fa2a1dfSstephen hemminger tbytes = stats->tx_bytes; 107357a7744eSEric W. Biederman } while (u64_stats_fetch_retry_irq(&stats->tx_syncp, start)); 107483a27052SEric Dumazet 107583a27052SEric Dumazet do { 107657a7744eSEric W. Biederman start = u64_stats_fetch_begin_irq(&stats->rx_syncp); 10773fa2a1dfSstephen hemminger rpackets = stats->rx_packets; 10783fa2a1dfSstephen hemminger rbytes = stats->rx_bytes; 107957a7744eSEric W. Biederman } while (u64_stats_fetch_retry_irq(&stats->rx_syncp, start)); 10803fa2a1dfSstephen hemminger 10813fa2a1dfSstephen hemminger tot->rx_packets += rpackets; 10823fa2a1dfSstephen hemminger tot->tx_packets += tpackets; 10833fa2a1dfSstephen hemminger tot->rx_bytes += rbytes; 10843fa2a1dfSstephen hemminger tot->tx_bytes += tbytes; 10853fa2a1dfSstephen hemminger } 10863fa2a1dfSstephen hemminger 10873fa2a1dfSstephen hemminger tot->tx_dropped = dev->stats.tx_dropped; 1088021ac8d3SRick Jones tot->tx_fifo_errors = dev->stats.tx_fifo_errors; 10893fa2a1dfSstephen hemminger tot->rx_dropped = dev->stats.rx_dropped; 10903fa2a1dfSstephen hemminger tot->rx_length_errors = dev->stats.rx_length_errors; 10913fa2a1dfSstephen hemminger tot->rx_frame_errors = dev->stats.rx_frame_errors; 10923fa2a1dfSstephen hemminger 10933fa2a1dfSstephen hemminger return tot; 10943fa2a1dfSstephen hemminger } 10953fa2a1dfSstephen hemminger 1096da74e89dSAmit Shah #ifdef CONFIG_NET_POLL_CONTROLLER 1097da74e89dSAmit Shah static void virtnet_netpoll(struct net_device *dev) 1098da74e89dSAmit Shah { 1099da74e89dSAmit Shah struct virtnet_info *vi = netdev_priv(dev); 1100986a4f4dSJason Wang int i; 1101da74e89dSAmit Shah 1102986a4f4dSJason Wang for (i = 0; i < vi->curr_queue_pairs; i++) 1103986a4f4dSJason Wang napi_schedule(&vi->rq[i].napi); 1104da74e89dSAmit Shah } 1105da74e89dSAmit Shah #endif 1106da74e89dSAmit Shah 1107586d17c5SJason Wang static void virtnet_ack_link_announce(struct virtnet_info *vi) 1108586d17c5SJason Wang { 1109586d17c5SJason Wang rtnl_lock(); 1110586d17c5SJason Wang if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE, 1111d24bae32Sstephen hemminger VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL)) 1112586d17c5SJason Wang dev_warn(&vi->dev->dev, "Failed to ack link announce.\n"); 1113586d17c5SJason Wang rtnl_unlock(); 1114586d17c5SJason Wang } 1115586d17c5SJason Wang 1116986a4f4dSJason Wang static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs) 1117986a4f4dSJason Wang { 1118986a4f4dSJason Wang struct scatterlist sg; 1119986a4f4dSJason Wang struct virtio_net_ctrl_mq s; 1120986a4f4dSJason Wang struct net_device *dev = vi->dev; 1121986a4f4dSJason Wang 1122986a4f4dSJason Wang if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ)) 1123986a4f4dSJason Wang return 0; 1124986a4f4dSJason Wang 1125fdd819b2SMichael S. Tsirkin s.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs); 1126986a4f4dSJason Wang sg_init_one(&sg, &s, sizeof(s)); 1127986a4f4dSJason Wang 1128986a4f4dSJason Wang if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ, 1129d24bae32Sstephen hemminger VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) { 1130986a4f4dSJason Wang dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n", 1131986a4f4dSJason Wang queue_pairs); 1132986a4f4dSJason Wang return -EINVAL; 113355257d72SSasha Levin } else { 1134986a4f4dSJason Wang vi->curr_queue_pairs = queue_pairs; 113535ed159bSJason Wang /* virtnet_open() will refill when device is going to up. */ 113635ed159bSJason Wang if (dev->flags & IFF_UP) 11379b9cd802SJason Wang schedule_delayed_work(&vi->refill, 0); 113855257d72SSasha Levin } 1139986a4f4dSJason Wang 1140986a4f4dSJason Wang return 0; 1141986a4f4dSJason Wang } 1142986a4f4dSJason Wang 1143296f96fcSRusty Russell static int virtnet_close(struct net_device *dev) 1144296f96fcSRusty Russell { 1145296f96fcSRusty Russell struct virtnet_info *vi = netdev_priv(dev); 1146986a4f4dSJason Wang int i; 1147296f96fcSRusty Russell 1148b2baed69SRusty Russell /* Make sure refill_work doesn't re-enable napi! */ 1149b2baed69SRusty Russell cancel_delayed_work_sync(&vi->refill); 1150986a4f4dSJason Wang 1151986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) 1152986a4f4dSJason Wang napi_disable(&vi->rq[i].napi); 1153296f96fcSRusty Russell 1154296f96fcSRusty Russell return 0; 1155296f96fcSRusty Russell } 1156296f96fcSRusty Russell 11572af7698eSAlex Williamson static void virtnet_set_rx_mode(struct net_device *dev) 11582af7698eSAlex Williamson { 11592af7698eSAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 1160f565a7c2SAlex Williamson struct scatterlist sg[2]; 1161f565a7c2SAlex Williamson struct virtio_net_ctrl_mac *mac_data; 1162ccffad25SJiri Pirko struct netdev_hw_addr *ha; 116332e7bfc4SJiri Pirko int uc_count; 11644cd24eafSJiri Pirko int mc_count; 1165f565a7c2SAlex Williamson void *buf; 1166f565a7c2SAlex Williamson int i; 11672af7698eSAlex Williamson 1168788a8b6dSstephen hemminger /* We can't dynamically set ndo_set_rx_mode, so return gracefully */ 11692af7698eSAlex Williamson if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX)) 11702af7698eSAlex Williamson return; 11712af7698eSAlex Williamson 11722ac46030SMichael S. Tsirkin vi->ctrl_promisc = ((dev->flags & IFF_PROMISC) != 0); 11732ac46030SMichael S. Tsirkin vi->ctrl_allmulti = ((dev->flags & IFF_ALLMULTI) != 0); 11742af7698eSAlex Williamson 11752ac46030SMichael S. Tsirkin sg_init_one(sg, &vi->ctrl_promisc, sizeof(vi->ctrl_promisc)); 11762af7698eSAlex Williamson 11772af7698eSAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, 1178d24bae32Sstephen hemminger VIRTIO_NET_CTRL_RX_PROMISC, sg)) 11792af7698eSAlex Williamson dev_warn(&dev->dev, "Failed to %sable promisc mode.\n", 11802ac46030SMichael S. Tsirkin vi->ctrl_promisc ? "en" : "dis"); 11812af7698eSAlex Williamson 11822ac46030SMichael S. Tsirkin sg_init_one(sg, &vi->ctrl_allmulti, sizeof(vi->ctrl_allmulti)); 11832af7698eSAlex Williamson 11842af7698eSAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, 1185d24bae32Sstephen hemminger VIRTIO_NET_CTRL_RX_ALLMULTI, sg)) 11862af7698eSAlex Williamson dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n", 11872ac46030SMichael S. Tsirkin vi->ctrl_allmulti ? "en" : "dis"); 1188f565a7c2SAlex Williamson 118932e7bfc4SJiri Pirko uc_count = netdev_uc_count(dev); 11904cd24eafSJiri Pirko mc_count = netdev_mc_count(dev); 1191f565a7c2SAlex Williamson /* MAC filter - use one buffer for both lists */ 11924cd24eafSJiri Pirko buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) + 1193f565a7c2SAlex Williamson (2 * sizeof(mac_data->entries)), GFP_ATOMIC); 11944cd24eafSJiri Pirko mac_data = buf; 1195e68ed8f0SJoe Perches if (!buf) 1196f565a7c2SAlex Williamson return; 1197f565a7c2SAlex Williamson 119823e258e1SAlex Williamson sg_init_table(sg, 2); 119923e258e1SAlex Williamson 1200f565a7c2SAlex Williamson /* Store the unicast list and count in the front of the buffer */ 1201fdd819b2SMichael S. Tsirkin mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count); 1202ccffad25SJiri Pirko i = 0; 120332e7bfc4SJiri Pirko netdev_for_each_uc_addr(ha, dev) 1204ccffad25SJiri Pirko memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN); 1205f565a7c2SAlex Williamson 1206f565a7c2SAlex Williamson sg_set_buf(&sg[0], mac_data, 120732e7bfc4SJiri Pirko sizeof(mac_data->entries) + (uc_count * ETH_ALEN)); 1208f565a7c2SAlex Williamson 1209f565a7c2SAlex Williamson /* multicast list and count fill the end */ 121032e7bfc4SJiri Pirko mac_data = (void *)&mac_data->macs[uc_count][0]; 1211f565a7c2SAlex Williamson 1212fdd819b2SMichael S. Tsirkin mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count); 1213567ec874SJiri Pirko i = 0; 121422bedad3SJiri Pirko netdev_for_each_mc_addr(ha, dev) 121522bedad3SJiri Pirko memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN); 1216f565a7c2SAlex Williamson 1217f565a7c2SAlex Williamson sg_set_buf(&sg[1], mac_data, 12184cd24eafSJiri Pirko sizeof(mac_data->entries) + (mc_count * ETH_ALEN)); 1219f565a7c2SAlex Williamson 1220f565a7c2SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, 1221d24bae32Sstephen hemminger VIRTIO_NET_CTRL_MAC_TABLE_SET, sg)) 122299e872aeSThomas Huth dev_warn(&dev->dev, "Failed to set MAC filter table.\n"); 1223f565a7c2SAlex Williamson 1224f565a7c2SAlex Williamson kfree(buf); 12252af7698eSAlex Williamson } 12262af7698eSAlex Williamson 122780d5c368SPatrick McHardy static int virtnet_vlan_rx_add_vid(struct net_device *dev, 122880d5c368SPatrick McHardy __be16 proto, u16 vid) 12290bde9569SAlex Williamson { 12300bde9569SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 12310bde9569SAlex Williamson struct scatterlist sg; 12320bde9569SAlex Williamson 123323e258e1SAlex Williamson sg_init_one(&sg, &vid, sizeof(vid)); 12340bde9569SAlex Williamson 12350bde9569SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, 1236d24bae32Sstephen hemminger VIRTIO_NET_CTRL_VLAN_ADD, &sg)) 12370bde9569SAlex Williamson dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid); 12388e586137SJiri Pirko return 0; 12390bde9569SAlex Williamson } 12400bde9569SAlex Williamson 124180d5c368SPatrick McHardy static int virtnet_vlan_rx_kill_vid(struct net_device *dev, 124280d5c368SPatrick McHardy __be16 proto, u16 vid) 12430bde9569SAlex Williamson { 12440bde9569SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 12450bde9569SAlex Williamson struct scatterlist sg; 12460bde9569SAlex Williamson 124723e258e1SAlex Williamson sg_init_one(&sg, &vid, sizeof(vid)); 12480bde9569SAlex Williamson 12490bde9569SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, 1250d24bae32Sstephen hemminger VIRTIO_NET_CTRL_VLAN_DEL, &sg)) 12510bde9569SAlex Williamson dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid); 12528e586137SJiri Pirko return 0; 12530bde9569SAlex Williamson } 12540bde9569SAlex Williamson 12558898c21cSWanlong Gao static void virtnet_clean_affinity(struct virtnet_info *vi, long hcpu) 1256986a4f4dSJason Wang { 1257986a4f4dSJason Wang int i; 12588898c21cSWanlong Gao 12598898c21cSWanlong Gao if (vi->affinity_hint_set) { 12608898c21cSWanlong Gao for (i = 0; i < vi->max_queue_pairs; i++) { 12618898c21cSWanlong Gao virtqueue_set_affinity(vi->rq[i].vq, -1); 12628898c21cSWanlong Gao virtqueue_set_affinity(vi->sq[i].vq, -1); 12638898c21cSWanlong Gao } 12648898c21cSWanlong Gao 12658898c21cSWanlong Gao vi->affinity_hint_set = false; 12668898c21cSWanlong Gao } 12678898c21cSWanlong Gao } 12688898c21cSWanlong Gao 12698898c21cSWanlong Gao static void virtnet_set_affinity(struct virtnet_info *vi) 1270986a4f4dSJason Wang { 1271986a4f4dSJason Wang int i; 127247be2479SWanlong Gao int cpu; 1273986a4f4dSJason Wang 1274986a4f4dSJason Wang /* In multiqueue mode, when the number of cpu is equal to the number of 1275986a4f4dSJason Wang * queue pairs, we let the queue pairs to be private to one cpu by 1276986a4f4dSJason Wang * setting the affinity hint to eliminate the contention. 1277986a4f4dSJason Wang */ 12788898c21cSWanlong Gao if (vi->curr_queue_pairs == 1 || 12798898c21cSWanlong Gao vi->max_queue_pairs != num_online_cpus()) { 12808898c21cSWanlong Gao virtnet_clean_affinity(vi, -1); 1281986a4f4dSJason Wang return; 1282986a4f4dSJason Wang } 1283986a4f4dSJason Wang 128447be2479SWanlong Gao i = 0; 128547be2479SWanlong Gao for_each_online_cpu(cpu) { 1286986a4f4dSJason Wang virtqueue_set_affinity(vi->rq[i].vq, cpu); 1287986a4f4dSJason Wang virtqueue_set_affinity(vi->sq[i].vq, cpu); 12889bb8ca86SJason Wang netif_set_xps_queue(vi->dev, cpumask_of(cpu), i); 128947be2479SWanlong Gao i++; 1290986a4f4dSJason Wang } 1291986a4f4dSJason Wang 1292986a4f4dSJason Wang vi->affinity_hint_set = true; 129347be2479SWanlong Gao } 1294986a4f4dSJason Wang 12958de4b2f3SWanlong Gao static int virtnet_cpu_callback(struct notifier_block *nfb, 12968de4b2f3SWanlong Gao unsigned long action, void *hcpu) 12978de4b2f3SWanlong Gao { 12988de4b2f3SWanlong Gao struct virtnet_info *vi = container_of(nfb, struct virtnet_info, nb); 12998de4b2f3SWanlong Gao 13008de4b2f3SWanlong Gao switch(action & ~CPU_TASKS_FROZEN) { 13018de4b2f3SWanlong Gao case CPU_ONLINE: 13028de4b2f3SWanlong Gao case CPU_DOWN_FAILED: 13038de4b2f3SWanlong Gao case CPU_DEAD: 13048de4b2f3SWanlong Gao virtnet_set_affinity(vi); 13058de4b2f3SWanlong Gao break; 13068de4b2f3SWanlong Gao case CPU_DOWN_PREPARE: 13078de4b2f3SWanlong Gao virtnet_clean_affinity(vi, (long)hcpu); 13088de4b2f3SWanlong Gao break; 13098de4b2f3SWanlong Gao default: 13108de4b2f3SWanlong Gao break; 13118de4b2f3SWanlong Gao } 13123ab098dfSJason Wang 13138de4b2f3SWanlong Gao return NOTIFY_OK; 1314a9ea3fc6SHerbert Xu } 1315a9ea3fc6SHerbert Xu 13168f9f4668SRick Jones static void virtnet_get_ringparam(struct net_device *dev, 13178f9f4668SRick Jones struct ethtool_ringparam *ring) 13188f9f4668SRick Jones { 13198f9f4668SRick Jones struct virtnet_info *vi = netdev_priv(dev); 13208f9f4668SRick Jones 1321986a4f4dSJason Wang ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq); 1322986a4f4dSJason Wang ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq); 13238f9f4668SRick Jones ring->rx_pending = ring->rx_max_pending; 13248f9f4668SRick Jones ring->tx_pending = ring->tx_max_pending; 13258f9f4668SRick Jones } 13268f9f4668SRick Jones 132766846048SRick Jones 132866846048SRick Jones static void virtnet_get_drvinfo(struct net_device *dev, 132966846048SRick Jones struct ethtool_drvinfo *info) 133066846048SRick Jones { 133166846048SRick Jones struct virtnet_info *vi = netdev_priv(dev); 133266846048SRick Jones struct virtio_device *vdev = vi->vdev; 133366846048SRick Jones 133466846048SRick Jones strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver)); 133566846048SRick Jones strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version)); 133666846048SRick Jones strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info)); 133766846048SRick Jones 133866846048SRick Jones } 133966846048SRick Jones 1340d73bcd2cSJason Wang /* TODO: Eliminate OOO packets during switching */ 1341d73bcd2cSJason Wang static int virtnet_set_channels(struct net_device *dev, 1342d73bcd2cSJason Wang struct ethtool_channels *channels) 1343d73bcd2cSJason Wang { 1344d73bcd2cSJason Wang struct virtnet_info *vi = netdev_priv(dev); 1345d73bcd2cSJason Wang u16 queue_pairs = channels->combined_count; 1346d73bcd2cSJason Wang int err; 1347d73bcd2cSJason Wang 1348d73bcd2cSJason Wang /* We don't support separate rx/tx channels. 1349d73bcd2cSJason Wang * We don't allow setting 'other' channels. 1350d73bcd2cSJason Wang */ 1351d73bcd2cSJason Wang if (channels->rx_count || channels->tx_count || channels->other_count) 1352d73bcd2cSJason Wang return -EINVAL; 1353d73bcd2cSJason Wang 1354c18e9cd6SAmos Kong if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0) 1355d73bcd2cSJason Wang return -EINVAL; 1356d73bcd2cSJason Wang 135747be2479SWanlong Gao get_online_cpus(); 1358d73bcd2cSJason Wang err = virtnet_set_queues(vi, queue_pairs); 1359d73bcd2cSJason Wang if (!err) { 1360d73bcd2cSJason Wang netif_set_real_num_tx_queues(dev, queue_pairs); 1361d73bcd2cSJason Wang netif_set_real_num_rx_queues(dev, queue_pairs); 1362d73bcd2cSJason Wang 13638898c21cSWanlong Gao virtnet_set_affinity(vi); 1364d73bcd2cSJason Wang } 136547be2479SWanlong Gao put_online_cpus(); 1366d73bcd2cSJason Wang 1367d73bcd2cSJason Wang return err; 1368d73bcd2cSJason Wang } 1369d73bcd2cSJason Wang 1370d73bcd2cSJason Wang static void virtnet_get_channels(struct net_device *dev, 1371d73bcd2cSJason Wang struct ethtool_channels *channels) 1372d73bcd2cSJason Wang { 1373d73bcd2cSJason Wang struct virtnet_info *vi = netdev_priv(dev); 1374d73bcd2cSJason Wang 1375d73bcd2cSJason Wang channels->combined_count = vi->curr_queue_pairs; 1376d73bcd2cSJason Wang channels->max_combined = vi->max_queue_pairs; 1377d73bcd2cSJason Wang channels->max_other = 0; 1378d73bcd2cSJason Wang channels->rx_count = 0; 1379d73bcd2cSJason Wang channels->tx_count = 0; 1380d73bcd2cSJason Wang channels->other_count = 0; 1381d73bcd2cSJason Wang } 1382d73bcd2cSJason Wang 138316032be5SNikolay Aleksandrov /* Check if the user is trying to change anything besides speed/duplex */ 138416032be5SNikolay Aleksandrov static bool virtnet_validate_ethtool_cmd(const struct ethtool_cmd *cmd) 138516032be5SNikolay Aleksandrov { 138616032be5SNikolay Aleksandrov struct ethtool_cmd diff1 = *cmd; 138716032be5SNikolay Aleksandrov struct ethtool_cmd diff2 = {}; 138816032be5SNikolay Aleksandrov 1389*0cf3ace9SNikolay Aleksandrov /* cmd is always set so we need to clear it, validate the port type 1390*0cf3ace9SNikolay Aleksandrov * and also without autonegotiation we can ignore advertising 1391*0cf3ace9SNikolay Aleksandrov */ 139216032be5SNikolay Aleksandrov ethtool_cmd_speed_set(&diff1, 0); 1393*0cf3ace9SNikolay Aleksandrov diff2.port = PORT_OTHER; 139416032be5SNikolay Aleksandrov diff1.advertising = 0; 139516032be5SNikolay Aleksandrov diff1.duplex = 0; 139616032be5SNikolay Aleksandrov diff1.cmd = 0; 139716032be5SNikolay Aleksandrov 139816032be5SNikolay Aleksandrov return !memcmp(&diff1, &diff2, sizeof(diff1)); 139916032be5SNikolay Aleksandrov } 140016032be5SNikolay Aleksandrov 140116032be5SNikolay Aleksandrov static int virtnet_set_settings(struct net_device *dev, struct ethtool_cmd *cmd) 140216032be5SNikolay Aleksandrov { 140316032be5SNikolay Aleksandrov struct virtnet_info *vi = netdev_priv(dev); 140416032be5SNikolay Aleksandrov u32 speed; 140516032be5SNikolay Aleksandrov 140616032be5SNikolay Aleksandrov speed = ethtool_cmd_speed(cmd); 140716032be5SNikolay Aleksandrov /* don't allow custom speed and duplex */ 140816032be5SNikolay Aleksandrov if (!ethtool_validate_speed(speed) || 140916032be5SNikolay Aleksandrov !ethtool_validate_duplex(cmd->duplex) || 141016032be5SNikolay Aleksandrov !virtnet_validate_ethtool_cmd(cmd)) 141116032be5SNikolay Aleksandrov return -EINVAL; 141216032be5SNikolay Aleksandrov vi->speed = speed; 141316032be5SNikolay Aleksandrov vi->duplex = cmd->duplex; 141416032be5SNikolay Aleksandrov 141516032be5SNikolay Aleksandrov return 0; 141616032be5SNikolay Aleksandrov } 141716032be5SNikolay Aleksandrov 141816032be5SNikolay Aleksandrov static int virtnet_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) 141916032be5SNikolay Aleksandrov { 142016032be5SNikolay Aleksandrov struct virtnet_info *vi = netdev_priv(dev); 142116032be5SNikolay Aleksandrov 142216032be5SNikolay Aleksandrov ethtool_cmd_speed_set(cmd, vi->speed); 142316032be5SNikolay Aleksandrov cmd->duplex = vi->duplex; 142416032be5SNikolay Aleksandrov cmd->port = PORT_OTHER; 142516032be5SNikolay Aleksandrov 142616032be5SNikolay Aleksandrov return 0; 142716032be5SNikolay Aleksandrov } 142816032be5SNikolay Aleksandrov 142916032be5SNikolay Aleksandrov static void virtnet_init_settings(struct net_device *dev) 143016032be5SNikolay Aleksandrov { 143116032be5SNikolay Aleksandrov struct virtnet_info *vi = netdev_priv(dev); 143216032be5SNikolay Aleksandrov 143316032be5SNikolay Aleksandrov vi->speed = SPEED_UNKNOWN; 143416032be5SNikolay Aleksandrov vi->duplex = DUPLEX_UNKNOWN; 143516032be5SNikolay Aleksandrov } 143616032be5SNikolay Aleksandrov 14370fc0b732SStephen Hemminger static const struct ethtool_ops virtnet_ethtool_ops = { 143866846048SRick Jones .get_drvinfo = virtnet_get_drvinfo, 14399f4d26d0SMark McLoughlin .get_link = ethtool_op_get_link, 14408f9f4668SRick Jones .get_ringparam = virtnet_get_ringparam, 1441d73bcd2cSJason Wang .set_channels = virtnet_set_channels, 1442d73bcd2cSJason Wang .get_channels = virtnet_get_channels, 1443074c3582SJacob Keller .get_ts_info = ethtool_op_get_ts_info, 144416032be5SNikolay Aleksandrov .get_settings = virtnet_get_settings, 144516032be5SNikolay Aleksandrov .set_settings = virtnet_set_settings, 1446a9ea3fc6SHerbert Xu }; 1447a9ea3fc6SHerbert Xu 144839da5814SMark McLoughlin #define MIN_MTU 68 144939da5814SMark McLoughlin #define MAX_MTU 65535 145039da5814SMark McLoughlin 145139da5814SMark McLoughlin static int virtnet_change_mtu(struct net_device *dev, int new_mtu) 145239da5814SMark McLoughlin { 145339da5814SMark McLoughlin if (new_mtu < MIN_MTU || new_mtu > MAX_MTU) 145439da5814SMark McLoughlin return -EINVAL; 145539da5814SMark McLoughlin dev->mtu = new_mtu; 145639da5814SMark McLoughlin return 0; 145739da5814SMark McLoughlin } 145839da5814SMark McLoughlin 145976288b4eSStephen Hemminger static const struct net_device_ops virtnet_netdev = { 146076288b4eSStephen Hemminger .ndo_open = virtnet_open, 146176288b4eSStephen Hemminger .ndo_stop = virtnet_close, 146276288b4eSStephen Hemminger .ndo_start_xmit = start_xmit, 146376288b4eSStephen Hemminger .ndo_validate_addr = eth_validate_addr, 14649c46f6d4SAlex Williamson .ndo_set_mac_address = virtnet_set_mac_address, 14652af7698eSAlex Williamson .ndo_set_rx_mode = virtnet_set_rx_mode, 146676288b4eSStephen Hemminger .ndo_change_mtu = virtnet_change_mtu, 14673fa2a1dfSstephen hemminger .ndo_get_stats64 = virtnet_stats, 14681824a989SAlex Williamson .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid, 14691824a989SAlex Williamson .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid, 147076288b4eSStephen Hemminger #ifdef CONFIG_NET_POLL_CONTROLLER 147176288b4eSStephen Hemminger .ndo_poll_controller = virtnet_netpoll, 147276288b4eSStephen Hemminger #endif 147391815639SJason Wang #ifdef CONFIG_NET_RX_BUSY_POLL 147491815639SJason Wang .ndo_busy_poll = virtnet_busy_poll, 147591815639SJason Wang #endif 147676288b4eSStephen Hemminger }; 147776288b4eSStephen Hemminger 1478586d17c5SJason Wang static void virtnet_config_changed_work(struct work_struct *work) 14799f4d26d0SMark McLoughlin { 1480586d17c5SJason Wang struct virtnet_info *vi = 1481586d17c5SJason Wang container_of(work, struct virtnet_info, config_work); 14829f4d26d0SMark McLoughlin u16 v; 14839f4d26d0SMark McLoughlin 1484855e0c52SRusty Russell if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS, 1485855e0c52SRusty Russell struct virtio_net_config, status, &v) < 0) 1486507613bfSMichael S. Tsirkin return; 1487586d17c5SJason Wang 1488586d17c5SJason Wang if (v & VIRTIO_NET_S_ANNOUNCE) { 1489ee89bab1SAmerigo Wang netdev_notify_peers(vi->dev); 1490586d17c5SJason Wang virtnet_ack_link_announce(vi); 1491586d17c5SJason Wang } 14929f4d26d0SMark McLoughlin 14939f4d26d0SMark McLoughlin /* Ignore unknown (future) status bits */ 14949f4d26d0SMark McLoughlin v &= VIRTIO_NET_S_LINK_UP; 14959f4d26d0SMark McLoughlin 14969f4d26d0SMark McLoughlin if (vi->status == v) 1497507613bfSMichael S. Tsirkin return; 14989f4d26d0SMark McLoughlin 14999f4d26d0SMark McLoughlin vi->status = v; 15009f4d26d0SMark McLoughlin 15019f4d26d0SMark McLoughlin if (vi->status & VIRTIO_NET_S_LINK_UP) { 15029f4d26d0SMark McLoughlin netif_carrier_on(vi->dev); 1503986a4f4dSJason Wang netif_tx_wake_all_queues(vi->dev); 15049f4d26d0SMark McLoughlin } else { 15059f4d26d0SMark McLoughlin netif_carrier_off(vi->dev); 1506986a4f4dSJason Wang netif_tx_stop_all_queues(vi->dev); 15079f4d26d0SMark McLoughlin } 15089f4d26d0SMark McLoughlin } 15099f4d26d0SMark McLoughlin 15109f4d26d0SMark McLoughlin static void virtnet_config_changed(struct virtio_device *vdev) 15119f4d26d0SMark McLoughlin { 15129f4d26d0SMark McLoughlin struct virtnet_info *vi = vdev->priv; 15139f4d26d0SMark McLoughlin 15143b07e9caSTejun Heo schedule_work(&vi->config_work); 15159f4d26d0SMark McLoughlin } 15169f4d26d0SMark McLoughlin 1517986a4f4dSJason Wang static void virtnet_free_queues(struct virtnet_info *vi) 1518986a4f4dSJason Wang { 1519d4fb84eeSAndrey Vagin int i; 1520d4fb84eeSAndrey Vagin 1521ab3971b1SJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1522ab3971b1SJason Wang napi_hash_del(&vi->rq[i].napi); 1523d4fb84eeSAndrey Vagin netif_napi_del(&vi->rq[i].napi); 1524ab3971b1SJason Wang } 1525d4fb84eeSAndrey Vagin 1526986a4f4dSJason Wang kfree(vi->rq); 1527986a4f4dSJason Wang kfree(vi->sq); 1528986a4f4dSJason Wang } 1529986a4f4dSJason Wang 1530986a4f4dSJason Wang static void free_receive_bufs(struct virtnet_info *vi) 1531986a4f4dSJason Wang { 1532986a4f4dSJason Wang int i; 1533986a4f4dSJason Wang 1534986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1535986a4f4dSJason Wang while (vi->rq[i].pages) 1536986a4f4dSJason Wang __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0); 1537986a4f4dSJason Wang } 1538986a4f4dSJason Wang } 1539986a4f4dSJason Wang 1540fb51879dSMichael Dalton static void free_receive_page_frags(struct virtnet_info *vi) 1541fb51879dSMichael Dalton { 1542fb51879dSMichael Dalton int i; 1543fb51879dSMichael Dalton for (i = 0; i < vi->max_queue_pairs; i++) 1544fb51879dSMichael Dalton if (vi->rq[i].alloc_frag.page) 1545fb51879dSMichael Dalton put_page(vi->rq[i].alloc_frag.page); 1546fb51879dSMichael Dalton } 1547fb51879dSMichael Dalton 1548986a4f4dSJason Wang static void free_unused_bufs(struct virtnet_info *vi) 1549986a4f4dSJason Wang { 1550986a4f4dSJason Wang void *buf; 1551986a4f4dSJason Wang int i; 1552986a4f4dSJason Wang 1553986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1554986a4f4dSJason Wang struct virtqueue *vq = vi->sq[i].vq; 1555986a4f4dSJason Wang while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) 1556986a4f4dSJason Wang dev_kfree_skb(buf); 1557986a4f4dSJason Wang } 1558986a4f4dSJason Wang 1559986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1560986a4f4dSJason Wang struct virtqueue *vq = vi->rq[i].vq; 1561986a4f4dSJason Wang 1562986a4f4dSJason Wang while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) { 1563ab7db917SMichael Dalton if (vi->mergeable_rx_bufs) { 1564ab7db917SMichael Dalton unsigned long ctx = (unsigned long)buf; 1565ab7db917SMichael Dalton void *base = mergeable_ctx_to_buf_address(ctx); 1566ab7db917SMichael Dalton put_page(virt_to_head_page(base)); 1567ab7db917SMichael Dalton } else if (vi->big_packets) { 1568fa9fac17SAndrey Vagin give_pages(&vi->rq[i], buf); 1569ab7db917SMichael Dalton } else { 1570986a4f4dSJason Wang dev_kfree_skb(buf); 1571986a4f4dSJason Wang } 1572986a4f4dSJason Wang } 1573986a4f4dSJason Wang } 1574ab7db917SMichael Dalton } 1575986a4f4dSJason Wang 1576e9d7417bSJason Wang static void virtnet_del_vqs(struct virtnet_info *vi) 1577e9d7417bSJason Wang { 1578e9d7417bSJason Wang struct virtio_device *vdev = vi->vdev; 1579e9d7417bSJason Wang 15808898c21cSWanlong Gao virtnet_clean_affinity(vi, -1); 1581986a4f4dSJason Wang 1582e9d7417bSJason Wang vdev->config->del_vqs(vdev); 1583986a4f4dSJason Wang 1584986a4f4dSJason Wang virtnet_free_queues(vi); 1585986a4f4dSJason Wang } 1586986a4f4dSJason Wang 1587986a4f4dSJason Wang static int virtnet_find_vqs(struct virtnet_info *vi) 1588986a4f4dSJason Wang { 1589986a4f4dSJason Wang vq_callback_t **callbacks; 1590986a4f4dSJason Wang struct virtqueue **vqs; 1591986a4f4dSJason Wang int ret = -ENOMEM; 1592986a4f4dSJason Wang int i, total_vqs; 1593986a4f4dSJason Wang const char **names; 1594986a4f4dSJason Wang 1595986a4f4dSJason Wang /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by 1596986a4f4dSJason Wang * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by 1597986a4f4dSJason Wang * possible control vq. 1598986a4f4dSJason Wang */ 1599986a4f4dSJason Wang total_vqs = vi->max_queue_pairs * 2 + 1600986a4f4dSJason Wang virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ); 1601986a4f4dSJason Wang 1602986a4f4dSJason Wang /* Allocate space for find_vqs parameters */ 1603986a4f4dSJason Wang vqs = kzalloc(total_vqs * sizeof(*vqs), GFP_KERNEL); 1604986a4f4dSJason Wang if (!vqs) 1605986a4f4dSJason Wang goto err_vq; 1606986a4f4dSJason Wang callbacks = kmalloc(total_vqs * sizeof(*callbacks), GFP_KERNEL); 1607986a4f4dSJason Wang if (!callbacks) 1608986a4f4dSJason Wang goto err_callback; 1609986a4f4dSJason Wang names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL); 1610986a4f4dSJason Wang if (!names) 1611986a4f4dSJason Wang goto err_names; 1612986a4f4dSJason Wang 1613986a4f4dSJason Wang /* Parameters for control virtqueue, if any */ 1614986a4f4dSJason Wang if (vi->has_cvq) { 1615986a4f4dSJason Wang callbacks[total_vqs - 1] = NULL; 1616986a4f4dSJason Wang names[total_vqs - 1] = "control"; 1617986a4f4dSJason Wang } 1618986a4f4dSJason Wang 1619986a4f4dSJason Wang /* Allocate/initialize parameters for send/receive virtqueues */ 1620986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1621986a4f4dSJason Wang callbacks[rxq2vq(i)] = skb_recv_done; 1622986a4f4dSJason Wang callbacks[txq2vq(i)] = skb_xmit_done; 1623986a4f4dSJason Wang sprintf(vi->rq[i].name, "input.%d", i); 1624986a4f4dSJason Wang sprintf(vi->sq[i].name, "output.%d", i); 1625986a4f4dSJason Wang names[rxq2vq(i)] = vi->rq[i].name; 1626986a4f4dSJason Wang names[txq2vq(i)] = vi->sq[i].name; 1627986a4f4dSJason Wang } 1628986a4f4dSJason Wang 1629986a4f4dSJason Wang ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks, 1630986a4f4dSJason Wang names); 1631986a4f4dSJason Wang if (ret) 1632986a4f4dSJason Wang goto err_find; 1633986a4f4dSJason Wang 1634986a4f4dSJason Wang if (vi->has_cvq) { 1635986a4f4dSJason Wang vi->cvq = vqs[total_vqs - 1]; 1636986a4f4dSJason Wang if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN)) 1637f646968fSPatrick McHardy vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER; 1638986a4f4dSJason Wang } 1639986a4f4dSJason Wang 1640986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1641986a4f4dSJason Wang vi->rq[i].vq = vqs[rxq2vq(i)]; 1642986a4f4dSJason Wang vi->sq[i].vq = vqs[txq2vq(i)]; 1643986a4f4dSJason Wang } 1644986a4f4dSJason Wang 1645986a4f4dSJason Wang kfree(names); 1646986a4f4dSJason Wang kfree(callbacks); 1647986a4f4dSJason Wang kfree(vqs); 1648986a4f4dSJason Wang 1649986a4f4dSJason Wang return 0; 1650986a4f4dSJason Wang 1651986a4f4dSJason Wang err_find: 1652986a4f4dSJason Wang kfree(names); 1653986a4f4dSJason Wang err_names: 1654986a4f4dSJason Wang kfree(callbacks); 1655986a4f4dSJason Wang err_callback: 1656986a4f4dSJason Wang kfree(vqs); 1657986a4f4dSJason Wang err_vq: 1658986a4f4dSJason Wang return ret; 1659986a4f4dSJason Wang } 1660986a4f4dSJason Wang 1661986a4f4dSJason Wang static int virtnet_alloc_queues(struct virtnet_info *vi) 1662986a4f4dSJason Wang { 1663986a4f4dSJason Wang int i; 1664986a4f4dSJason Wang 1665986a4f4dSJason Wang vi->sq = kzalloc(sizeof(*vi->sq) * vi->max_queue_pairs, GFP_KERNEL); 1666986a4f4dSJason Wang if (!vi->sq) 1667986a4f4dSJason Wang goto err_sq; 1668986a4f4dSJason Wang vi->rq = kzalloc(sizeof(*vi->rq) * vi->max_queue_pairs, GFP_KERNEL); 1669008d4278SAmerigo Wang if (!vi->rq) 1670986a4f4dSJason Wang goto err_rq; 1671986a4f4dSJason Wang 1672986a4f4dSJason Wang INIT_DELAYED_WORK(&vi->refill, refill_work); 1673986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1674986a4f4dSJason Wang vi->rq[i].pages = NULL; 1675986a4f4dSJason Wang netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll, 1676986a4f4dSJason Wang napi_weight); 1677986a4f4dSJason Wang 1678986a4f4dSJason Wang sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg)); 16795377d758SJohannes Berg ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len); 1680986a4f4dSJason Wang sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg)); 1681986a4f4dSJason Wang } 1682986a4f4dSJason Wang 1683986a4f4dSJason Wang return 0; 1684986a4f4dSJason Wang 1685986a4f4dSJason Wang err_rq: 1686986a4f4dSJason Wang kfree(vi->sq); 1687986a4f4dSJason Wang err_sq: 1688986a4f4dSJason Wang return -ENOMEM; 1689e9d7417bSJason Wang } 1690e9d7417bSJason Wang 16913f9c10b0SAmit Shah static int init_vqs(struct virtnet_info *vi) 16923f9c10b0SAmit Shah { 1693986a4f4dSJason Wang int ret; 16943f9c10b0SAmit Shah 1695986a4f4dSJason Wang /* Allocate send & receive queues */ 1696986a4f4dSJason Wang ret = virtnet_alloc_queues(vi); 1697986a4f4dSJason Wang if (ret) 1698986a4f4dSJason Wang goto err; 16993f9c10b0SAmit Shah 1700986a4f4dSJason Wang ret = virtnet_find_vqs(vi); 1701986a4f4dSJason Wang if (ret) 1702986a4f4dSJason Wang goto err_free; 17033f9c10b0SAmit Shah 170447be2479SWanlong Gao get_online_cpus(); 17058898c21cSWanlong Gao virtnet_set_affinity(vi); 170647be2479SWanlong Gao put_online_cpus(); 170747be2479SWanlong Gao 17083f9c10b0SAmit Shah return 0; 1709986a4f4dSJason Wang 1710986a4f4dSJason Wang err_free: 1711986a4f4dSJason Wang virtnet_free_queues(vi); 1712986a4f4dSJason Wang err: 1713986a4f4dSJason Wang return ret; 17143f9c10b0SAmit Shah } 17153f9c10b0SAmit Shah 1716fbf28d78SMichael Dalton #ifdef CONFIG_SYSFS 1717fbf28d78SMichael Dalton static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue, 1718fbf28d78SMichael Dalton struct rx_queue_attribute *attribute, char *buf) 1719fbf28d78SMichael Dalton { 1720fbf28d78SMichael Dalton struct virtnet_info *vi = netdev_priv(queue->dev); 1721fbf28d78SMichael Dalton unsigned int queue_index = get_netdev_rx_queue_index(queue); 17225377d758SJohannes Berg struct ewma_pkt_len *avg; 1723fbf28d78SMichael Dalton 1724fbf28d78SMichael Dalton BUG_ON(queue_index >= vi->max_queue_pairs); 1725fbf28d78SMichael Dalton avg = &vi->rq[queue_index].mrg_avg_pkt_len; 1726fbf28d78SMichael Dalton return sprintf(buf, "%u\n", get_mergeable_buf_len(avg)); 1727fbf28d78SMichael Dalton } 1728fbf28d78SMichael Dalton 1729fbf28d78SMichael Dalton static struct rx_queue_attribute mergeable_rx_buffer_size_attribute = 1730fbf28d78SMichael Dalton __ATTR_RO(mergeable_rx_buffer_size); 1731fbf28d78SMichael Dalton 1732fbf28d78SMichael Dalton static struct attribute *virtio_net_mrg_rx_attrs[] = { 1733fbf28d78SMichael Dalton &mergeable_rx_buffer_size_attribute.attr, 1734fbf28d78SMichael Dalton NULL 1735fbf28d78SMichael Dalton }; 1736fbf28d78SMichael Dalton 1737fbf28d78SMichael Dalton static const struct attribute_group virtio_net_mrg_rx_group = { 1738fbf28d78SMichael Dalton .name = "virtio_net", 1739fbf28d78SMichael Dalton .attrs = virtio_net_mrg_rx_attrs 1740fbf28d78SMichael Dalton }; 1741fbf28d78SMichael Dalton #endif 1742fbf28d78SMichael Dalton 1743892d6eb1SJason Wang static bool virtnet_fail_on_feature(struct virtio_device *vdev, 1744892d6eb1SJason Wang unsigned int fbit, 1745892d6eb1SJason Wang const char *fname, const char *dname) 1746892d6eb1SJason Wang { 1747892d6eb1SJason Wang if (!virtio_has_feature(vdev, fbit)) 1748892d6eb1SJason Wang return false; 1749892d6eb1SJason Wang 1750892d6eb1SJason Wang dev_err(&vdev->dev, "device advertises feature %s but not %s", 1751892d6eb1SJason Wang fname, dname); 1752892d6eb1SJason Wang 1753892d6eb1SJason Wang return true; 1754892d6eb1SJason Wang } 1755892d6eb1SJason Wang 1756892d6eb1SJason Wang #define VIRTNET_FAIL_ON(vdev, fbit, dbit) \ 1757892d6eb1SJason Wang virtnet_fail_on_feature(vdev, fbit, #fbit, dbit) 1758892d6eb1SJason Wang 1759892d6eb1SJason Wang static bool virtnet_validate_features(struct virtio_device *vdev) 1760892d6eb1SJason Wang { 1761892d6eb1SJason Wang if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) && 1762892d6eb1SJason Wang (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX, 1763892d6eb1SJason Wang "VIRTIO_NET_F_CTRL_VQ") || 1764892d6eb1SJason Wang VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN, 1765892d6eb1SJason Wang "VIRTIO_NET_F_CTRL_VQ") || 1766892d6eb1SJason Wang VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE, 1767892d6eb1SJason Wang "VIRTIO_NET_F_CTRL_VQ") || 1768892d6eb1SJason Wang VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") || 1769892d6eb1SJason Wang VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR, 1770892d6eb1SJason Wang "VIRTIO_NET_F_CTRL_VQ"))) { 1771892d6eb1SJason Wang return false; 1772892d6eb1SJason Wang } 1773892d6eb1SJason Wang 1774892d6eb1SJason Wang return true; 1775892d6eb1SJason Wang } 1776892d6eb1SJason Wang 1777296f96fcSRusty Russell static int virtnet_probe(struct virtio_device *vdev) 1778296f96fcSRusty Russell { 1779986a4f4dSJason Wang int i, err; 1780296f96fcSRusty Russell struct net_device *dev; 1781296f96fcSRusty Russell struct virtnet_info *vi; 1782986a4f4dSJason Wang u16 max_queue_pairs; 1783986a4f4dSJason Wang 17846ba42248SMichael S. Tsirkin if (!vdev->config->get) { 17856ba42248SMichael S. Tsirkin dev_err(&vdev->dev, "%s failure: config access disabled\n", 17866ba42248SMichael S. Tsirkin __func__); 17876ba42248SMichael S. Tsirkin return -EINVAL; 17886ba42248SMichael S. Tsirkin } 17896ba42248SMichael S. Tsirkin 1790892d6eb1SJason Wang if (!virtnet_validate_features(vdev)) 1791892d6eb1SJason Wang return -EINVAL; 1792892d6eb1SJason Wang 1793986a4f4dSJason Wang /* Find if host supports multiqueue virtio_net device */ 1794855e0c52SRusty Russell err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ, 1795855e0c52SRusty Russell struct virtio_net_config, 1796855e0c52SRusty Russell max_virtqueue_pairs, &max_queue_pairs); 1797986a4f4dSJason Wang 1798986a4f4dSJason Wang /* We need at least 2 queue's */ 1799986a4f4dSJason Wang if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN || 1800986a4f4dSJason Wang max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX || 1801986a4f4dSJason Wang !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) 1802986a4f4dSJason Wang max_queue_pairs = 1; 1803296f96fcSRusty Russell 1804296f96fcSRusty Russell /* Allocate ourselves a network device with room for our info */ 1805986a4f4dSJason Wang dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs); 1806296f96fcSRusty Russell if (!dev) 1807296f96fcSRusty Russell return -ENOMEM; 1808296f96fcSRusty Russell 1809296f96fcSRusty Russell /* Set up network device as normal. */ 1810f2f2c8b4SJiri Pirko dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE; 181176288b4eSStephen Hemminger dev->netdev_ops = &virtnet_netdev; 1812296f96fcSRusty Russell dev->features = NETIF_F_HIGHDMA; 18133fa2a1dfSstephen hemminger 18147ad24ea4SWilfried Klaebe dev->ethtool_ops = &virtnet_ethtool_ops; 1815296f96fcSRusty Russell SET_NETDEV_DEV(dev, &vdev->dev); 1816296f96fcSRusty Russell 1817296f96fcSRusty Russell /* Do we support "hardware" checksums? */ 181898e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) { 1819296f96fcSRusty Russell /* This opens up the world of extra features. */ 182048900cb6SJason Wang dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG; 182198e778c9SMichał Mirosław if (csum) 182248900cb6SJason Wang dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG; 182398e778c9SMichał Mirosław 182498e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) { 1825e3e3c423SVlad Yasevich dev->hw_features |= NETIF_F_TSO | NETIF_F_UFO 182634a48579SRusty Russell | NETIF_F_TSO_ECN | NETIF_F_TSO6; 182734a48579SRusty Russell } 18285539ae96SRusty Russell /* Individual feature bits: what can host handle? */ 182998e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4)) 183098e778c9SMichał Mirosław dev->hw_features |= NETIF_F_TSO; 183198e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6)) 183298e778c9SMichał Mirosław dev->hw_features |= NETIF_F_TSO6; 183398e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN)) 183498e778c9SMichał Mirosław dev->hw_features |= NETIF_F_TSO_ECN; 1835e3e3c423SVlad Yasevich if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO)) 1836e3e3c423SVlad Yasevich dev->hw_features |= NETIF_F_UFO; 183798e778c9SMichał Mirosław 183841f2f127SJason Wang dev->features |= NETIF_F_GSO_ROBUST; 183941f2f127SJason Wang 184098e778c9SMichał Mirosław if (gso) 1841e3e3c423SVlad Yasevich dev->features |= dev->hw_features & (NETIF_F_ALL_TSO|NETIF_F_UFO); 184298e778c9SMichał Mirosław /* (!csum && gso) case will be fixed by register_netdev() */ 1843296f96fcSRusty Russell } 18444f49129bSThomas Huth if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM)) 18454f49129bSThomas Huth dev->features |= NETIF_F_RXCSUM; 1846296f96fcSRusty Russell 18474fda8302SJason Wang dev->vlan_features = dev->features; 18484fda8302SJason Wang 1849296f96fcSRusty Russell /* Configuration may specify what MAC to use. Otherwise random. */ 1850855e0c52SRusty Russell if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) 1851855e0c52SRusty Russell virtio_cread_bytes(vdev, 1852a586d4f6SRusty Russell offsetof(struct virtio_net_config, mac), 1853855e0c52SRusty Russell dev->dev_addr, dev->addr_len); 1854855e0c52SRusty Russell else 1855f2cedb63SDanny Kukawka eth_hw_addr_random(dev); 1856296f96fcSRusty Russell 1857296f96fcSRusty Russell /* Set up our device-specific information */ 1858296f96fcSRusty Russell vi = netdev_priv(dev); 1859296f96fcSRusty Russell vi->dev = dev; 1860296f96fcSRusty Russell vi->vdev = vdev; 1861d9d5dcc8SChristian Borntraeger vdev->priv = vi; 18623fa2a1dfSstephen hemminger vi->stats = alloc_percpu(struct virtnet_stats); 18633fa2a1dfSstephen hemminger err = -ENOMEM; 18643fa2a1dfSstephen hemminger if (vi->stats == NULL) 18653fa2a1dfSstephen hemminger goto free; 18663fa2a1dfSstephen hemminger 1867827da44cSJohn Stultz for_each_possible_cpu(i) { 1868827da44cSJohn Stultz struct virtnet_stats *virtnet_stats; 1869827da44cSJohn Stultz virtnet_stats = per_cpu_ptr(vi->stats, i); 1870827da44cSJohn Stultz u64_stats_init(&virtnet_stats->tx_syncp); 1871827da44cSJohn Stultz u64_stats_init(&virtnet_stats->rx_syncp); 1872827da44cSJohn Stultz } 1873827da44cSJohn Stultz 1874586d17c5SJason Wang INIT_WORK(&vi->config_work, virtnet_config_changed_work); 1875296f96fcSRusty Russell 187697402b96SHerbert Xu /* If we can receive ANY GSO packets, we must allocate large ones. */ 18778e95a202SJoe Perches if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) || 18788e95a202SJoe Perches virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) || 1879e3e3c423SVlad Yasevich virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN) || 1880e3e3c423SVlad Yasevich virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UFO)) 188197402b96SHerbert Xu vi->big_packets = true; 188297402b96SHerbert Xu 18833f2c31d9SMark McLoughlin if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF)) 18843f2c31d9SMark McLoughlin vi->mergeable_rx_bufs = true; 18853f2c31d9SMark McLoughlin 1886d04302b3SMichael S. Tsirkin if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) || 1887d04302b3SMichael S. Tsirkin virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) 1888012873d0SMichael S. Tsirkin vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf); 1889012873d0SMichael S. Tsirkin else 1890012873d0SMichael S. Tsirkin vi->hdr_len = sizeof(struct virtio_net_hdr); 1891012873d0SMichael S. Tsirkin 189275993300SMichael S. Tsirkin if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) || 189375993300SMichael S. Tsirkin virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) 1894e7428e95SMichael S. Tsirkin vi->any_header_sg = true; 1895e7428e95SMichael S. Tsirkin 1896986a4f4dSJason Wang if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) 1897986a4f4dSJason Wang vi->has_cvq = true; 1898986a4f4dSJason Wang 1899012873d0SMichael S. Tsirkin if (vi->any_header_sg) 1900012873d0SMichael S. Tsirkin dev->needed_headroom = vi->hdr_len; 19016ebbc1a6SZhangjie \(HZ\) 1902986a4f4dSJason Wang /* Use single tx/rx queue pair as default */ 1903986a4f4dSJason Wang vi->curr_queue_pairs = 1; 1904986a4f4dSJason Wang vi->max_queue_pairs = max_queue_pairs; 1905986a4f4dSJason Wang 1906986a4f4dSJason Wang /* Allocate/initialize the rx/tx queues, and invoke find_vqs */ 19073f9c10b0SAmit Shah err = init_vqs(vi); 1908d2a7dddaSMichael S. Tsirkin if (err) 19099bb8ca86SJason Wang goto free_stats; 1910d2a7dddaSMichael S. Tsirkin 1911fbf28d78SMichael Dalton #ifdef CONFIG_SYSFS 1912fbf28d78SMichael Dalton if (vi->mergeable_rx_bufs) 1913fbf28d78SMichael Dalton dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group; 1914fbf28d78SMichael Dalton #endif 19150f13b66bSZhi Yong Wu netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs); 19160f13b66bSZhi Yong Wu netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs); 1917986a4f4dSJason Wang 191816032be5SNikolay Aleksandrov virtnet_init_settings(dev); 191916032be5SNikolay Aleksandrov 1920296f96fcSRusty Russell err = register_netdev(dev); 1921296f96fcSRusty Russell if (err) { 1922296f96fcSRusty Russell pr_debug("virtio_net: registering device failed\n"); 1923d2a7dddaSMichael S. Tsirkin goto free_vqs; 1924296f96fcSRusty Russell } 1925b3369c1fSRusty Russell 19264baf1e33SMichael S. Tsirkin virtio_device_ready(vdev); 19274baf1e33SMichael S. Tsirkin 1928b3369c1fSRusty Russell /* Last of all, set up some receive buffers. */ 192955257d72SSasha Levin for (i = 0; i < vi->curr_queue_pairs; i++) { 1930946fa564SMichael S. Tsirkin try_fill_recv(vi, &vi->rq[i], GFP_KERNEL); 1931b3369c1fSRusty Russell 1932b3369c1fSRusty Russell /* If we didn't even get one input buffer, we're useless. */ 1933be121f46SJason Wang if (vi->rq[i].vq->num_free == 1934be121f46SJason Wang virtqueue_get_vring_size(vi->rq[i].vq)) { 1935986a4f4dSJason Wang free_unused_bufs(vi); 1936b3369c1fSRusty Russell err = -ENOMEM; 1937986a4f4dSJason Wang goto free_recv_bufs; 1938986a4f4dSJason Wang } 1939b3369c1fSRusty Russell } 1940b3369c1fSRusty Russell 19418de4b2f3SWanlong Gao vi->nb.notifier_call = &virtnet_cpu_callback; 19428de4b2f3SWanlong Gao err = register_hotcpu_notifier(&vi->nb); 19438de4b2f3SWanlong Gao if (err) { 19448de4b2f3SWanlong Gao pr_debug("virtio_net: registering cpu notifier failed\n"); 19458de4b2f3SWanlong Gao goto free_recv_bufs; 19468de4b2f3SWanlong Gao } 19478de4b2f3SWanlong Gao 1948167c25e4SJason Wang /* Assume link up if device can't report link status, 1949167c25e4SJason Wang otherwise get link status from config. */ 1950167c25e4SJason Wang if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) { 1951167c25e4SJason Wang netif_carrier_off(dev); 19523b07e9caSTejun Heo schedule_work(&vi->config_work); 1953167c25e4SJason Wang } else { 1954167c25e4SJason Wang vi->status = VIRTIO_NET_S_LINK_UP; 19554783256eSPantelis Koukousoulas netif_carrier_on(dev); 1956167c25e4SJason Wang } 19579f4d26d0SMark McLoughlin 1958986a4f4dSJason Wang pr_debug("virtnet: registered device %s with %d RX and TX vq's\n", 1959986a4f4dSJason Wang dev->name, max_queue_pairs); 1960986a4f4dSJason Wang 1961296f96fcSRusty Russell return 0; 1962296f96fcSRusty Russell 1963986a4f4dSJason Wang free_recv_bufs: 196402465555SMichael S. Tsirkin vi->vdev->config->reset(vdev); 196502465555SMichael S. Tsirkin 1966986a4f4dSJason Wang free_receive_bufs(vi); 1967b3369c1fSRusty Russell unregister_netdev(dev); 1968d2a7dddaSMichael S. Tsirkin free_vqs: 1969986a4f4dSJason Wang cancel_delayed_work_sync(&vi->refill); 1970fb51879dSMichael Dalton free_receive_page_frags(vi); 1971e9d7417bSJason Wang virtnet_del_vqs(vi); 19723fa2a1dfSstephen hemminger free_stats: 19733fa2a1dfSstephen hemminger free_percpu(vi->stats); 1974296f96fcSRusty Russell free: 1975296f96fcSRusty Russell free_netdev(dev); 1976296f96fcSRusty Russell return err; 1977296f96fcSRusty Russell } 1978296f96fcSRusty Russell 197904486ed0SAmit Shah static void remove_vq_common(struct virtnet_info *vi) 1980296f96fcSRusty Russell { 198104486ed0SAmit Shah vi->vdev->config->reset(vi->vdev); 1982830a8a97SShirley Ma 1983830a8a97SShirley Ma /* Free unused buffers in both send and recv, if any. */ 19849ab86bbcSShirley Ma free_unused_bufs(vi); 1985fb6813f4SRusty Russell 1986986a4f4dSJason Wang free_receive_bufs(vi); 1987d2a7dddaSMichael S. Tsirkin 1988fb51879dSMichael Dalton free_receive_page_frags(vi); 1989fb51879dSMichael Dalton 1990986a4f4dSJason Wang virtnet_del_vqs(vi); 199104486ed0SAmit Shah } 199204486ed0SAmit Shah 19938cc085d6SBill Pemberton static void virtnet_remove(struct virtio_device *vdev) 199404486ed0SAmit Shah { 199504486ed0SAmit Shah struct virtnet_info *vi = vdev->priv; 199604486ed0SAmit Shah 19978de4b2f3SWanlong Gao unregister_hotcpu_notifier(&vi->nb); 19988de4b2f3SWanlong Gao 1999102a2786SMichael S. Tsirkin /* Make sure no work handler is accessing the device. */ 2000102a2786SMichael S. Tsirkin flush_work(&vi->config_work); 2001586d17c5SJason Wang 200204486ed0SAmit Shah unregister_netdev(vi->dev); 200304486ed0SAmit Shah 200404486ed0SAmit Shah remove_vq_common(vi); 2005fb6813f4SRusty Russell 20062e66f55bSKrishna Kumar free_percpu(vi->stats); 200774b2553fSRusty Russell free_netdev(vi->dev); 2008296f96fcSRusty Russell } 2009296f96fcSRusty Russell 201089107000SAaron Lu #ifdef CONFIG_PM_SLEEP 20110741bcb5SAmit Shah static int virtnet_freeze(struct virtio_device *vdev) 20120741bcb5SAmit Shah { 20130741bcb5SAmit Shah struct virtnet_info *vi = vdev->priv; 2014986a4f4dSJason Wang int i; 20150741bcb5SAmit Shah 2016ec9debbdSJason Wang unregister_hotcpu_notifier(&vi->nb); 2017ec9debbdSJason Wang 2018102a2786SMichael S. Tsirkin /* Make sure no work handler is accessing the device */ 2019102a2786SMichael S. Tsirkin flush_work(&vi->config_work); 2020586d17c5SJason Wang 20210741bcb5SAmit Shah netif_device_detach(vi->dev); 20220741bcb5SAmit Shah cancel_delayed_work_sync(&vi->refill); 20230741bcb5SAmit Shah 202491815639SJason Wang if (netif_running(vi->dev)) { 2025ab3971b1SJason Wang for (i = 0; i < vi->max_queue_pairs; i++) 2026986a4f4dSJason Wang napi_disable(&vi->rq[i].napi); 202791815639SJason Wang } 20280741bcb5SAmit Shah 20290741bcb5SAmit Shah remove_vq_common(vi); 20300741bcb5SAmit Shah 20310741bcb5SAmit Shah return 0; 20320741bcb5SAmit Shah } 20330741bcb5SAmit Shah 20340741bcb5SAmit Shah static int virtnet_restore(struct virtio_device *vdev) 20350741bcb5SAmit Shah { 20360741bcb5SAmit Shah struct virtnet_info *vi = vdev->priv; 2037986a4f4dSJason Wang int err, i; 20380741bcb5SAmit Shah 20390741bcb5SAmit Shah err = init_vqs(vi); 20400741bcb5SAmit Shah if (err) 20410741bcb5SAmit Shah return err; 20420741bcb5SAmit Shah 2043e53fbd11SMichael S. Tsirkin virtio_device_ready(vdev); 2044e53fbd11SMichael S. Tsirkin 20456cd4ce00SJason Wang if (netif_running(vi->dev)) { 204655257d72SSasha Levin for (i = 0; i < vi->curr_queue_pairs; i++) 2047946fa564SMichael S. Tsirkin if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL)) 20483b07e9caSTejun Heo schedule_delayed_work(&vi->refill, 0); 20490741bcb5SAmit Shah 20506cd4ce00SJason Wang for (i = 0; i < vi->max_queue_pairs; i++) 20516cd4ce00SJason Wang virtnet_napi_enable(&vi->rq[i]); 20526cd4ce00SJason Wang } 20536cd4ce00SJason Wang 20546cd4ce00SJason Wang netif_device_attach(vi->dev); 20556cd4ce00SJason Wang 205635ed159bSJason Wang rtnl_lock(); 2057986a4f4dSJason Wang virtnet_set_queues(vi, vi->curr_queue_pairs); 205835ed159bSJason Wang rtnl_unlock(); 2059986a4f4dSJason Wang 2060ec9debbdSJason Wang err = register_hotcpu_notifier(&vi->nb); 2061ec9debbdSJason Wang if (err) 2062ec9debbdSJason Wang return err; 2063ec9debbdSJason Wang 20640741bcb5SAmit Shah return 0; 20650741bcb5SAmit Shah } 20660741bcb5SAmit Shah #endif 20670741bcb5SAmit Shah 2068296f96fcSRusty Russell static struct virtio_device_id id_table[] = { 2069296f96fcSRusty Russell { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID }, 2070296f96fcSRusty Russell { 0 }, 2071296f96fcSRusty Russell }; 2072296f96fcSRusty Russell 2073c45a6816SRusty Russell static unsigned int features[] = { 20745e4fe5c4SMark McLoughlin VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, 20755e4fe5c4SMark McLoughlin VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC, 2076e3e3c423SVlad Yasevich VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, 207797402b96SHerbert Xu VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, 2078e3e3c423SVlad Yasevich VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, 20792a41f71dSAlex Williamson VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, 20800bde9569SAlex Williamson VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, 2081986a4f4dSJason Wang VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, 20827e58d5aeSAmos Kong VIRTIO_NET_F_CTRL_MAC_ADDR, 2083e7428e95SMichael S. Tsirkin VIRTIO_F_ANY_LAYOUT, 2084c45a6816SRusty Russell }; 2085c45a6816SRusty Russell 208622402529SUwe Kleine-König static struct virtio_driver virtio_net_driver = { 2087c45a6816SRusty Russell .feature_table = features, 2088c45a6816SRusty Russell .feature_table_size = ARRAY_SIZE(features), 2089296f96fcSRusty Russell .driver.name = KBUILD_MODNAME, 2090296f96fcSRusty Russell .driver.owner = THIS_MODULE, 2091296f96fcSRusty Russell .id_table = id_table, 2092296f96fcSRusty Russell .probe = virtnet_probe, 20938cc085d6SBill Pemberton .remove = virtnet_remove, 20949f4d26d0SMark McLoughlin .config_changed = virtnet_config_changed, 209589107000SAaron Lu #ifdef CONFIG_PM_SLEEP 20960741bcb5SAmit Shah .freeze = virtnet_freeze, 20970741bcb5SAmit Shah .restore = virtnet_restore, 20980741bcb5SAmit Shah #endif 2099296f96fcSRusty Russell }; 2100296f96fcSRusty Russell 2101b2a17029SRusty Russell module_virtio_driver(virtio_net_driver); 2102296f96fcSRusty Russell 2103296f96fcSRusty Russell MODULE_DEVICE_TABLE(virtio, id_table); 2104296f96fcSRusty Russell MODULE_DESCRIPTION("Virtio network driver"); 2105296f96fcSRusty Russell MODULE_LICENSE("GPL"); 2106