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 16296f96fcSRusty Russell * along with this program; if not, write to the Free Software 17296f96fcSRusty Russell * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18296f96fcSRusty Russell */ 19296f96fcSRusty Russell //#define DEBUG 20296f96fcSRusty Russell #include <linux/netdevice.h> 21296f96fcSRusty Russell #include <linux/etherdevice.h> 22a9ea3fc6SHerbert Xu #include <linux/ethtool.h> 23296f96fcSRusty Russell #include <linux/module.h> 24296f96fcSRusty Russell #include <linux/virtio.h> 25296f96fcSRusty Russell #include <linux/virtio_net.h> 26296f96fcSRusty Russell #include <linux/scatterlist.h> 27e918085aSAlex Williamson #include <linux/if_vlan.h> 285a0e3ad6STejun Heo #include <linux/slab.h> 29296f96fcSRusty Russell 306c0cd7c0SDor Laor static int napi_weight = 128; 316c0cd7c0SDor Laor module_param(napi_weight, int, 0444); 326c0cd7c0SDor Laor 33eb939922SRusty Russell static bool csum = true, gso = true; 3434a48579SRusty Russell module_param(csum, bool, 0444); 3534a48579SRusty Russell module_param(gso, bool, 0444); 3634a48579SRusty Russell 37296f96fcSRusty Russell /* FIXME: MTU in config. */ 38e918085aSAlex Williamson #define MAX_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN) 393f2c31d9SMark McLoughlin #define GOOD_COPY_LEN 128 40296f96fcSRusty Russell 41f565a7c2SAlex Williamson #define VIRTNET_SEND_COMMAND_SG_MAX 2 4266846048SRick Jones #define VIRTNET_DRIVER_VERSION "1.0.0" 432a41f71dSAlex Williamson 443fa2a1dfSstephen hemminger struct virtnet_stats { 4583a27052SEric Dumazet struct u64_stats_sync tx_syncp; 4683a27052SEric Dumazet struct u64_stats_sync rx_syncp; 473fa2a1dfSstephen hemminger u64 tx_bytes; 483fa2a1dfSstephen hemminger u64 tx_packets; 493fa2a1dfSstephen hemminger 503fa2a1dfSstephen hemminger u64 rx_bytes; 513fa2a1dfSstephen hemminger u64 rx_packets; 523fa2a1dfSstephen hemminger }; 533fa2a1dfSstephen hemminger 54e9d7417bSJason Wang /* Internal representation of a send virtqueue */ 55e9d7417bSJason Wang struct send_queue { 56e9d7417bSJason Wang /* Virtqueue associated with this send _queue */ 57e9d7417bSJason Wang struct virtqueue *vq; 58e9d7417bSJason Wang 59e9d7417bSJason Wang /* TX: fragments + linear part + virtio header */ 60e9d7417bSJason Wang struct scatterlist sg[MAX_SKB_FRAGS + 2]; 61986a4f4dSJason Wang 62986a4f4dSJason Wang /* Name of the send queue: output.$index */ 63986a4f4dSJason Wang char name[40]; 64e9d7417bSJason Wang }; 65e9d7417bSJason Wang 66e9d7417bSJason Wang /* Internal representation of a receive virtqueue */ 67e9d7417bSJason Wang struct receive_queue { 68e9d7417bSJason Wang /* Virtqueue associated with this receive_queue */ 69e9d7417bSJason Wang struct virtqueue *vq; 70e9d7417bSJason Wang 71296f96fcSRusty Russell struct napi_struct napi; 72296f96fcSRusty Russell 73296f96fcSRusty Russell /* Number of input buffers, and max we've ever had. */ 74296f96fcSRusty Russell unsigned int num, max; 75296f96fcSRusty Russell 76e9d7417bSJason Wang /* Chain pages by the private ptr. */ 77e9d7417bSJason Wang struct page *pages; 78e9d7417bSJason Wang 79e9d7417bSJason Wang /* RX: fragments + linear part + virtio header */ 80e9d7417bSJason Wang struct scatterlist sg[MAX_SKB_FRAGS + 2]; 81986a4f4dSJason Wang 82986a4f4dSJason Wang /* Name of this receive queue: input.$index */ 83986a4f4dSJason Wang char name[40]; 84e9d7417bSJason Wang }; 85e9d7417bSJason Wang 86e9d7417bSJason Wang struct virtnet_info { 87e9d7417bSJason Wang struct virtio_device *vdev; 88e9d7417bSJason Wang struct virtqueue *cvq; 89e9d7417bSJason Wang struct net_device *dev; 90986a4f4dSJason Wang struct send_queue *sq; 91986a4f4dSJason Wang struct receive_queue *rq; 92e9d7417bSJason Wang unsigned int status; 93e9d7417bSJason Wang 94986a4f4dSJason Wang /* Max # of queue pairs supported by the device */ 95986a4f4dSJason Wang u16 max_queue_pairs; 96986a4f4dSJason Wang 97986a4f4dSJason Wang /* # of queue pairs currently used by the driver */ 98986a4f4dSJason Wang u16 curr_queue_pairs; 99986a4f4dSJason Wang 10097402b96SHerbert Xu /* I like... big packets and I cannot lie! */ 10197402b96SHerbert Xu bool big_packets; 10297402b96SHerbert Xu 1033f2c31d9SMark McLoughlin /* Host will merge rx buffers for big packets (shake it! shake it!) */ 1043f2c31d9SMark McLoughlin bool mergeable_rx_bufs; 1053f2c31d9SMark McLoughlin 106986a4f4dSJason Wang /* Has control virtqueue */ 107986a4f4dSJason Wang bool has_cvq; 108986a4f4dSJason Wang 109586d17c5SJason Wang /* enable config space updates */ 110586d17c5SJason Wang bool config_enable; 111586d17c5SJason Wang 1123fa2a1dfSstephen hemminger /* Active statistics */ 1133fa2a1dfSstephen hemminger struct virtnet_stats __percpu *stats; 1143fa2a1dfSstephen hemminger 1153161e453SRusty Russell /* Work struct for refilling if we run low on memory. */ 1163161e453SRusty Russell struct delayed_work refill; 1173161e453SRusty Russell 118586d17c5SJason Wang /* Work struct for config space updates */ 119586d17c5SJason Wang struct work_struct config_work; 120586d17c5SJason Wang 121586d17c5SJason Wang /* Lock for config space updates */ 122586d17c5SJason Wang struct mutex config_lock; 123986a4f4dSJason Wang 124986a4f4dSJason Wang /* Does the affinity hint is set for virtqueues? */ 125986a4f4dSJason Wang bool affinity_hint_set; 126*47be2479SWanlong Gao 127*47be2479SWanlong Gao /* Per-cpu variable to show the mapping from CPU to virtqueue */ 128*47be2479SWanlong Gao int __percpu *vq_index; 129296f96fcSRusty Russell }; 130296f96fcSRusty Russell 131b3f24698SRusty Russell struct skb_vnet_hdr { 132b3f24698SRusty Russell union { 133b3f24698SRusty Russell struct virtio_net_hdr hdr; 134b3f24698SRusty Russell struct virtio_net_hdr_mrg_rxbuf mhdr; 135b3f24698SRusty Russell }; 136b3f24698SRusty Russell }; 137b3f24698SRusty Russell 1389ab86bbcSShirley Ma struct padded_vnet_hdr { 1399ab86bbcSShirley Ma struct virtio_net_hdr hdr; 1409ab86bbcSShirley Ma /* 1419ab86bbcSShirley Ma * virtio_net_hdr should be in a separated sg buffer because of a 1429ab86bbcSShirley Ma * QEMU bug, and data sg buffer shares same page with this header sg. 1439ab86bbcSShirley Ma * This padding makes next sg 16 byte aligned after virtio_net_hdr. 1449ab86bbcSShirley Ma */ 1459ab86bbcSShirley Ma char padding[6]; 1469ab86bbcSShirley Ma }; 1479ab86bbcSShirley Ma 148986a4f4dSJason Wang /* Converting between virtqueue no. and kernel tx/rx queue no. 149986a4f4dSJason Wang * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq 150986a4f4dSJason Wang */ 151986a4f4dSJason Wang static int vq2txq(struct virtqueue *vq) 152986a4f4dSJason Wang { 153986a4f4dSJason Wang return (virtqueue_get_queue_index(vq) - 1) / 2; 154986a4f4dSJason Wang } 155986a4f4dSJason Wang 156986a4f4dSJason Wang static int txq2vq(int txq) 157986a4f4dSJason Wang { 158986a4f4dSJason Wang return txq * 2 + 1; 159986a4f4dSJason Wang } 160986a4f4dSJason Wang 161986a4f4dSJason Wang static int vq2rxq(struct virtqueue *vq) 162986a4f4dSJason Wang { 163986a4f4dSJason Wang return virtqueue_get_queue_index(vq) / 2; 164986a4f4dSJason Wang } 165986a4f4dSJason Wang 166986a4f4dSJason Wang static int rxq2vq(int rxq) 167986a4f4dSJason Wang { 168986a4f4dSJason Wang return rxq * 2; 169986a4f4dSJason Wang } 170986a4f4dSJason Wang 171b3f24698SRusty Russell static inline struct skb_vnet_hdr *skb_vnet_hdr(struct sk_buff *skb) 172296f96fcSRusty Russell { 173b3f24698SRusty Russell return (struct skb_vnet_hdr *)skb->cb; 174296f96fcSRusty Russell } 175296f96fcSRusty Russell 1769ab86bbcSShirley Ma /* 1779ab86bbcSShirley Ma * private is used to chain pages for big packets, put the whole 1789ab86bbcSShirley Ma * most recent used list in the beginning for reuse 1799ab86bbcSShirley Ma */ 180e9d7417bSJason Wang static void give_pages(struct receive_queue *rq, struct page *page) 181fb6813f4SRusty Russell { 1829ab86bbcSShirley Ma struct page *end; 1839ab86bbcSShirley Ma 184e9d7417bSJason Wang /* Find end of list, sew whole thing into vi->rq.pages. */ 1859ab86bbcSShirley Ma for (end = page; end->private; end = (struct page *)end->private); 186e9d7417bSJason Wang end->private = (unsigned long)rq->pages; 187e9d7417bSJason Wang rq->pages = page; 188fb6813f4SRusty Russell } 189fb6813f4SRusty Russell 190e9d7417bSJason Wang static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask) 191fb6813f4SRusty Russell { 192e9d7417bSJason Wang struct page *p = rq->pages; 193fb6813f4SRusty Russell 1949ab86bbcSShirley Ma if (p) { 195e9d7417bSJason Wang rq->pages = (struct page *)p->private; 1969ab86bbcSShirley Ma /* clear private here, it is used to chain pages */ 1979ab86bbcSShirley Ma p->private = 0; 1989ab86bbcSShirley Ma } else 199fb6813f4SRusty Russell p = alloc_page(gfp_mask); 200fb6813f4SRusty Russell return p; 201fb6813f4SRusty Russell } 202fb6813f4SRusty Russell 203e9d7417bSJason Wang static void skb_xmit_done(struct virtqueue *vq) 204296f96fcSRusty Russell { 205e9d7417bSJason Wang struct virtnet_info *vi = vq->vdev->priv; 206296f96fcSRusty Russell 2072cb9c6baSRusty Russell /* Suppress further interrupts. */ 208e9d7417bSJason Wang virtqueue_disable_cb(vq); 20911a3a154SRusty Russell 210363f1514SRusty Russell /* We were probably waiting for more output buffers. */ 211986a4f4dSJason Wang netif_wake_subqueue(vi->dev, vq2txq(vq)); 212296f96fcSRusty Russell } 213296f96fcSRusty Russell 2149ab86bbcSShirley Ma static void set_skb_frag(struct sk_buff *skb, struct page *page, 2159ab86bbcSShirley Ma unsigned int offset, unsigned int *len) 216296f96fcSRusty Russell { 2178a59a7b9SKrishna Kumar int size = min((unsigned)PAGE_SIZE - offset, *len); 2189ab86bbcSShirley Ma int i = skb_shinfo(skb)->nr_frags; 219296f96fcSRusty Russell 2208a59a7b9SKrishna Kumar __skb_fill_page_desc(skb, i, page, offset, size); 2219ab86bbcSShirley Ma 2228a59a7b9SKrishna Kumar skb->data_len += size; 2238a59a7b9SKrishna Kumar skb->len += size; 2244b727361SEric Dumazet skb->truesize += PAGE_SIZE; 2259ab86bbcSShirley Ma skb_shinfo(skb)->nr_frags++; 2268a59a7b9SKrishna Kumar *len -= size; 227296f96fcSRusty Russell } 2283f2c31d9SMark McLoughlin 2293464645aSMike Waychison /* Called from bottom half context */ 230e9d7417bSJason Wang static struct sk_buff *page_to_skb(struct receive_queue *rq, 2319ab86bbcSShirley Ma struct page *page, unsigned int len) 2329ab86bbcSShirley Ma { 233e9d7417bSJason Wang struct virtnet_info *vi = rq->vq->vdev->priv; 2349ab86bbcSShirley Ma struct sk_buff *skb; 2359ab86bbcSShirley Ma struct skb_vnet_hdr *hdr; 2369ab86bbcSShirley Ma unsigned int copy, hdr_len, offset; 2379ab86bbcSShirley Ma char *p; 2389ab86bbcSShirley Ma 2399ab86bbcSShirley Ma p = page_address(page); 2409ab86bbcSShirley Ma 2419ab86bbcSShirley Ma /* copy small packet so we can reuse these pages for small data */ 2429ab86bbcSShirley Ma skb = netdev_alloc_skb_ip_align(vi->dev, GOOD_COPY_LEN); 2439ab86bbcSShirley Ma if (unlikely(!skb)) 2449ab86bbcSShirley Ma return NULL; 2459ab86bbcSShirley Ma 2469ab86bbcSShirley Ma hdr = skb_vnet_hdr(skb); 2479ab86bbcSShirley Ma 2483f2c31d9SMark McLoughlin if (vi->mergeable_rx_bufs) { 2499ab86bbcSShirley Ma hdr_len = sizeof hdr->mhdr; 2509ab86bbcSShirley Ma offset = hdr_len; 2519ab86bbcSShirley Ma } else { 2529ab86bbcSShirley Ma hdr_len = sizeof hdr->hdr; 2539ab86bbcSShirley Ma offset = sizeof(struct padded_vnet_hdr); 2549ab86bbcSShirley Ma } 2553f2c31d9SMark McLoughlin 2569ab86bbcSShirley Ma memcpy(hdr, p, hdr_len); 2573f2c31d9SMark McLoughlin 2589ab86bbcSShirley Ma len -= hdr_len; 2599ab86bbcSShirley Ma p += offset; 2603f2c31d9SMark McLoughlin 2613f2c31d9SMark McLoughlin copy = len; 2623f2c31d9SMark McLoughlin if (copy > skb_tailroom(skb)) 2633f2c31d9SMark McLoughlin copy = skb_tailroom(skb); 2643f2c31d9SMark McLoughlin memcpy(skb_put(skb, copy), p, copy); 2653f2c31d9SMark McLoughlin 2663f2c31d9SMark McLoughlin len -= copy; 2679ab86bbcSShirley Ma offset += copy; 2683f2c31d9SMark McLoughlin 269e878d78bSSasha Levin /* 270e878d78bSSasha Levin * Verify that we can indeed put this data into a skb. 271e878d78bSSasha Levin * This is here to handle cases when the device erroneously 272e878d78bSSasha Levin * tries to receive more than is possible. This is usually 273e878d78bSSasha Levin * the case of a broken device. 274e878d78bSSasha Levin */ 275e878d78bSSasha Levin if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) { 276be443899SAmerigo Wang net_dbg_ratelimited("%s: too much data\n", skb->dev->name); 277e878d78bSSasha Levin dev_kfree_skb(skb); 278e878d78bSSasha Levin return NULL; 279e878d78bSSasha Levin } 280e878d78bSSasha Levin 2819ab86bbcSShirley Ma while (len) { 2829ab86bbcSShirley Ma set_skb_frag(skb, page, offset, &len); 2839ab86bbcSShirley Ma page = (struct page *)page->private; 2849ab86bbcSShirley Ma offset = 0; 2853f2c31d9SMark McLoughlin } 2863f2c31d9SMark McLoughlin 2879ab86bbcSShirley Ma if (page) 288e9d7417bSJason Wang give_pages(rq, page); 2893f2c31d9SMark McLoughlin 2909ab86bbcSShirley Ma return skb; 2919ab86bbcSShirley Ma } 2929ab86bbcSShirley Ma 293e9d7417bSJason Wang static int receive_mergeable(struct receive_queue *rq, struct sk_buff *skb) 2949ab86bbcSShirley Ma { 2959ab86bbcSShirley Ma struct skb_vnet_hdr *hdr = skb_vnet_hdr(skb); 2969ab86bbcSShirley Ma struct page *page; 2979ab86bbcSShirley Ma int num_buf, i, len; 2989ab86bbcSShirley Ma 2999ab86bbcSShirley Ma num_buf = hdr->mhdr.num_buffers; 3009ab86bbcSShirley Ma while (--num_buf) { 3013f2c31d9SMark McLoughlin i = skb_shinfo(skb)->nr_frags; 3023f2c31d9SMark McLoughlin if (i >= MAX_SKB_FRAGS) { 3039ab86bbcSShirley Ma pr_debug("%s: packet too long\n", skb->dev->name); 3049ab86bbcSShirley Ma skb->dev->stats.rx_length_errors++; 3059ab86bbcSShirley Ma return -EINVAL; 3063f2c31d9SMark McLoughlin } 307e9d7417bSJason Wang page = virtqueue_get_buf(rq->vq, &len); 3089ab86bbcSShirley Ma if (!page) { 3093f2c31d9SMark McLoughlin pr_debug("%s: rx error: %d buffers missing\n", 3109ab86bbcSShirley Ma skb->dev->name, hdr->mhdr.num_buffers); 3119ab86bbcSShirley Ma skb->dev->stats.rx_length_errors++; 3129ab86bbcSShirley Ma return -EINVAL; 3133f2c31d9SMark McLoughlin } 3143fa2a1dfSstephen hemminger 3153f2c31d9SMark McLoughlin if (len > PAGE_SIZE) 3163f2c31d9SMark McLoughlin len = PAGE_SIZE; 3173f2c31d9SMark McLoughlin 3189ab86bbcSShirley Ma set_skb_frag(skb, page, 0, &len); 3199ab86bbcSShirley Ma 320e9d7417bSJason Wang --rq->num; 3213f2c31d9SMark McLoughlin } 3229ab86bbcSShirley Ma return 0; 3239ab86bbcSShirley Ma } 3249ab86bbcSShirley Ma 325e9d7417bSJason Wang static void receive_buf(struct receive_queue *rq, void *buf, unsigned int len) 3269ab86bbcSShirley Ma { 327e9d7417bSJason Wang struct virtnet_info *vi = rq->vq->vdev->priv; 328e9d7417bSJason Wang struct net_device *dev = vi->dev; 32958472a76SEric Dumazet struct virtnet_stats *stats = this_cpu_ptr(vi->stats); 3309ab86bbcSShirley Ma struct sk_buff *skb; 3319ab86bbcSShirley Ma struct page *page; 3329ab86bbcSShirley Ma struct skb_vnet_hdr *hdr; 3339ab86bbcSShirley Ma 3349ab86bbcSShirley Ma if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) { 3359ab86bbcSShirley Ma pr_debug("%s: short packet %i\n", dev->name, len); 3369ab86bbcSShirley Ma dev->stats.rx_length_errors++; 3379ab86bbcSShirley Ma if (vi->mergeable_rx_bufs || vi->big_packets) 338e9d7417bSJason Wang give_pages(rq, buf); 3399ab86bbcSShirley Ma else 3409ab86bbcSShirley Ma dev_kfree_skb(buf); 3419ab86bbcSShirley Ma return; 3429ab86bbcSShirley Ma } 3439ab86bbcSShirley Ma 3449ab86bbcSShirley Ma if (!vi->mergeable_rx_bufs && !vi->big_packets) { 3459ab86bbcSShirley Ma skb = buf; 3469ab86bbcSShirley Ma len -= sizeof(struct virtio_net_hdr); 3479ab86bbcSShirley Ma skb_trim(skb, len); 3483f2c31d9SMark McLoughlin } else { 3499ab86bbcSShirley Ma page = buf; 350e9d7417bSJason Wang skb = page_to_skb(rq, page, len); 3519ab86bbcSShirley Ma if (unlikely(!skb)) { 35297402b96SHerbert Xu dev->stats.rx_dropped++; 353e9d7417bSJason Wang give_pages(rq, page); 3549ab86bbcSShirley Ma return; 3559ab86bbcSShirley Ma } 3569ab86bbcSShirley Ma if (vi->mergeable_rx_bufs) 357e9d7417bSJason Wang if (receive_mergeable(rq, skb)) { 3589ab86bbcSShirley Ma dev_kfree_skb(skb); 3599ab86bbcSShirley Ma return; 36097402b96SHerbert Xu } 3613f2c31d9SMark McLoughlin } 3623f2c31d9SMark McLoughlin 3639ab86bbcSShirley Ma hdr = skb_vnet_hdr(skb); 3643fa2a1dfSstephen hemminger 36583a27052SEric Dumazet u64_stats_update_begin(&stats->rx_syncp); 3663fa2a1dfSstephen hemminger stats->rx_bytes += skb->len; 3673fa2a1dfSstephen hemminger stats->rx_packets++; 36883a27052SEric Dumazet u64_stats_update_end(&stats->rx_syncp); 369296f96fcSRusty Russell 370b3f24698SRusty Russell if (hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) { 371296f96fcSRusty Russell pr_debug("Needs csum!\n"); 372b3f24698SRusty Russell if (!skb_partial_csum_set(skb, 373b3f24698SRusty Russell hdr->hdr.csum_start, 374b3f24698SRusty Russell hdr->hdr.csum_offset)) 375296f96fcSRusty Russell goto frame_err; 37610a8d94aSJason Wang } else if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID) { 37710a8d94aSJason Wang skb->ip_summed = CHECKSUM_UNNECESSARY; 378296f96fcSRusty Russell } 379296f96fcSRusty Russell 38023cde76dSMark McLoughlin skb->protocol = eth_type_trans(skb, dev); 38123cde76dSMark McLoughlin pr_debug("Receiving skb proto 0x%04x len %i type %i\n", 38223cde76dSMark McLoughlin ntohs(skb->protocol), skb->len, skb->pkt_type); 38323cde76dSMark McLoughlin 384b3f24698SRusty Russell if (hdr->hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) { 385296f96fcSRusty Russell pr_debug("GSO!\n"); 386b3f24698SRusty Russell switch (hdr->hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) { 387296f96fcSRusty Russell case VIRTIO_NET_HDR_GSO_TCPV4: 388296f96fcSRusty Russell skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4; 389296f96fcSRusty Russell break; 390296f96fcSRusty Russell case VIRTIO_NET_HDR_GSO_UDP: 391296f96fcSRusty Russell skb_shinfo(skb)->gso_type = SKB_GSO_UDP; 392296f96fcSRusty Russell break; 393296f96fcSRusty Russell case VIRTIO_NET_HDR_GSO_TCPV6: 394296f96fcSRusty Russell skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6; 395296f96fcSRusty Russell break; 396296f96fcSRusty Russell default: 397be443899SAmerigo Wang net_warn_ratelimited("%s: bad gso type %u.\n", 398b3f24698SRusty Russell dev->name, hdr->hdr.gso_type); 399296f96fcSRusty Russell goto frame_err; 400296f96fcSRusty Russell } 401296f96fcSRusty Russell 402b3f24698SRusty Russell if (hdr->hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN) 40334a48579SRusty Russell skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN; 40434a48579SRusty Russell 405b3f24698SRusty Russell skb_shinfo(skb)->gso_size = hdr->hdr.gso_size; 406296f96fcSRusty Russell if (skb_shinfo(skb)->gso_size == 0) { 407be443899SAmerigo Wang net_warn_ratelimited("%s: zero gso size.\n", dev->name); 408296f96fcSRusty Russell goto frame_err; 409296f96fcSRusty Russell } 410296f96fcSRusty Russell 411296f96fcSRusty Russell /* Header must be checked, and gso_segs computed. */ 412296f96fcSRusty Russell skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY; 413296f96fcSRusty Russell skb_shinfo(skb)->gso_segs = 0; 414296f96fcSRusty Russell } 415296f96fcSRusty Russell 416296f96fcSRusty Russell netif_receive_skb(skb); 417296f96fcSRusty Russell return; 418296f96fcSRusty Russell 419296f96fcSRusty Russell frame_err: 420296f96fcSRusty Russell dev->stats.rx_frame_errors++; 421296f96fcSRusty Russell dev_kfree_skb(skb); 422296f96fcSRusty Russell } 423296f96fcSRusty Russell 424e9d7417bSJason Wang static int add_recvbuf_small(struct receive_queue *rq, gfp_t gfp) 425296f96fcSRusty Russell { 426e9d7417bSJason Wang struct virtnet_info *vi = rq->vq->vdev->priv; 427296f96fcSRusty Russell struct sk_buff *skb; 428b3f24698SRusty Russell struct skb_vnet_hdr *hdr; 4299ab86bbcSShirley Ma int err; 4303f2c31d9SMark McLoughlin 4313464645aSMike Waychison skb = __netdev_alloc_skb_ip_align(vi->dev, MAX_PACKET_LEN, gfp); 4329ab86bbcSShirley Ma if (unlikely(!skb)) 4339ab86bbcSShirley Ma return -ENOMEM; 434296f96fcSRusty Russell 435296f96fcSRusty Russell skb_put(skb, MAX_PACKET_LEN); 4363f2c31d9SMark McLoughlin 4373f2c31d9SMark McLoughlin hdr = skb_vnet_hdr(skb); 438e9d7417bSJason Wang sg_set_buf(rq->sg, &hdr->hdr, sizeof hdr->hdr); 43997402b96SHerbert Xu 440e9d7417bSJason Wang skb_to_sgvec(skb, rq->sg + 1, 0, skb->len); 44197402b96SHerbert Xu 442e9d7417bSJason Wang err = virtqueue_add_buf(rq->vq, rq->sg, 0, 2, skb, gfp); 4439ab86bbcSShirley Ma if (err < 0) 4449ab86bbcSShirley Ma dev_kfree_skb(skb); 44597402b96SHerbert Xu 4469ab86bbcSShirley Ma return err; 44797402b96SHerbert Xu } 44897402b96SHerbert Xu 449e9d7417bSJason Wang static int add_recvbuf_big(struct receive_queue *rq, gfp_t gfp) 4509ab86bbcSShirley Ma { 4519ab86bbcSShirley Ma struct page *first, *list = NULL; 4529ab86bbcSShirley Ma char *p; 4539ab86bbcSShirley Ma int i, err, offset; 454296f96fcSRusty Russell 455e9d7417bSJason Wang /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */ 4569ab86bbcSShirley Ma for (i = MAX_SKB_FRAGS + 1; i > 1; --i) { 457e9d7417bSJason Wang first = get_a_page(rq, gfp); 4589ab86bbcSShirley Ma if (!first) { 4599ab86bbcSShirley Ma if (list) 460e9d7417bSJason Wang give_pages(rq, list); 4619ab86bbcSShirley Ma return -ENOMEM; 462296f96fcSRusty Russell } 463e9d7417bSJason Wang sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE); 4649ab86bbcSShirley Ma 4659ab86bbcSShirley Ma /* chain new page in list head to match sg */ 4669ab86bbcSShirley Ma first->private = (unsigned long)list; 4679ab86bbcSShirley Ma list = first; 4689ab86bbcSShirley Ma } 4699ab86bbcSShirley Ma 470e9d7417bSJason Wang first = get_a_page(rq, gfp); 4719ab86bbcSShirley Ma if (!first) { 472e9d7417bSJason Wang give_pages(rq, list); 4739ab86bbcSShirley Ma return -ENOMEM; 4749ab86bbcSShirley Ma } 4759ab86bbcSShirley Ma p = page_address(first); 4769ab86bbcSShirley Ma 477e9d7417bSJason Wang /* rq->sg[0], rq->sg[1] share the same page */ 478e9d7417bSJason Wang /* a separated rq->sg[0] for virtio_net_hdr only due to QEMU bug */ 479e9d7417bSJason Wang sg_set_buf(&rq->sg[0], p, sizeof(struct virtio_net_hdr)); 4809ab86bbcSShirley Ma 481e9d7417bSJason Wang /* rq->sg[1] for data packet, from offset */ 4829ab86bbcSShirley Ma offset = sizeof(struct padded_vnet_hdr); 483e9d7417bSJason Wang sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset); 4849ab86bbcSShirley Ma 4859ab86bbcSShirley Ma /* chain first in list head */ 4869ab86bbcSShirley Ma first->private = (unsigned long)list; 487e9d7417bSJason Wang err = virtqueue_add_buf(rq->vq, rq->sg, 0, MAX_SKB_FRAGS + 2, 488aa989f5eSMichael S. Tsirkin first, gfp); 4899ab86bbcSShirley Ma if (err < 0) 490e9d7417bSJason Wang give_pages(rq, first); 4919ab86bbcSShirley Ma 4929ab86bbcSShirley Ma return err; 4939ab86bbcSShirley Ma } 4949ab86bbcSShirley Ma 495e9d7417bSJason Wang static int add_recvbuf_mergeable(struct receive_queue *rq, gfp_t gfp) 4969ab86bbcSShirley Ma { 4979ab86bbcSShirley Ma struct page *page; 4989ab86bbcSShirley Ma int err; 4999ab86bbcSShirley Ma 500e9d7417bSJason Wang page = get_a_page(rq, gfp); 5019ab86bbcSShirley Ma if (!page) 5029ab86bbcSShirley Ma return -ENOMEM; 5039ab86bbcSShirley Ma 504e9d7417bSJason Wang sg_init_one(rq->sg, page_address(page), PAGE_SIZE); 5059ab86bbcSShirley Ma 506e9d7417bSJason Wang err = virtqueue_add_buf(rq->vq, rq->sg, 0, 1, page, gfp); 5079ab86bbcSShirley Ma if (err < 0) 508e9d7417bSJason Wang give_pages(rq, page); 5099ab86bbcSShirley Ma 5109ab86bbcSShirley Ma return err; 511296f96fcSRusty Russell } 512296f96fcSRusty Russell 513b2baed69SRusty Russell /* 514b2baed69SRusty Russell * Returns false if we couldn't fill entirely (OOM). 515b2baed69SRusty Russell * 516b2baed69SRusty Russell * Normally run in the receive path, but can also be run from ndo_open 517b2baed69SRusty Russell * before we're receiving packets, or from refill_work which is 518b2baed69SRusty Russell * careful to disable receiving (using napi_disable). 519b2baed69SRusty Russell */ 520e9d7417bSJason Wang static bool try_fill_recv(struct receive_queue *rq, gfp_t gfp) 5213f2c31d9SMark McLoughlin { 522e9d7417bSJason Wang struct virtnet_info *vi = rq->vq->vdev->priv; 5233f2c31d9SMark McLoughlin int err; 5241788f495SMichael S. Tsirkin bool oom; 5253f2c31d9SMark McLoughlin 5260aea51c3SAmit Shah do { 5279ab86bbcSShirley Ma if (vi->mergeable_rx_bufs) 528e9d7417bSJason Wang err = add_recvbuf_mergeable(rq, gfp); 5299ab86bbcSShirley Ma else if (vi->big_packets) 530e9d7417bSJason Wang err = add_recvbuf_big(rq, gfp); 5319ab86bbcSShirley Ma else 532e9d7417bSJason Wang err = add_recvbuf_small(rq, gfp); 5333f2c31d9SMark McLoughlin 5341788f495SMichael S. Tsirkin oom = err == -ENOMEM; 5359ed4cb07SRusty Russell if (err) 5363f2c31d9SMark McLoughlin break; 537e9d7417bSJason Wang ++rq->num; 538b7dfde95SLinus Torvalds } while (rq->vq->num_free); 539e9d7417bSJason Wang if (unlikely(rq->num > rq->max)) 540e9d7417bSJason Wang rq->max = rq->num; 541e9d7417bSJason Wang virtqueue_kick(rq->vq); 5423161e453SRusty Russell return !oom; 5433f2c31d9SMark McLoughlin } 5443f2c31d9SMark McLoughlin 54518445c4dSRusty Russell static void skb_recv_done(struct virtqueue *rvq) 546296f96fcSRusty Russell { 547296f96fcSRusty Russell struct virtnet_info *vi = rvq->vdev->priv; 548986a4f4dSJason Wang struct receive_queue *rq = &vi->rq[vq2rxq(rvq)]; 549e9d7417bSJason Wang 55018445c4dSRusty Russell /* Schedule NAPI, Suppress further interrupts if successful. */ 551e9d7417bSJason Wang if (napi_schedule_prep(&rq->napi)) { 5521915a712SMichael S. Tsirkin virtqueue_disable_cb(rvq); 553e9d7417bSJason Wang __napi_schedule(&rq->napi); 55418445c4dSRusty Russell } 555296f96fcSRusty Russell } 556296f96fcSRusty Russell 557e9d7417bSJason Wang static void virtnet_napi_enable(struct receive_queue *rq) 5583e9d08ecSBruce Rogers { 559e9d7417bSJason Wang napi_enable(&rq->napi); 5603e9d08ecSBruce Rogers 5613e9d08ecSBruce Rogers /* If all buffers were filled by other side before we napi_enabled, we 5623e9d08ecSBruce Rogers * won't get another interrupt, so process any outstanding packets 5633e9d08ecSBruce Rogers * now. virtnet_poll wants re-enable the queue, so we disable here. 5643e9d08ecSBruce Rogers * We synchronize against interrupts via NAPI_STATE_SCHED */ 565e9d7417bSJason Wang if (napi_schedule_prep(&rq->napi)) { 566e9d7417bSJason Wang virtqueue_disable_cb(rq->vq); 567ec13ee80SMichael S. Tsirkin local_bh_disable(); 568e9d7417bSJason Wang __napi_schedule(&rq->napi); 569ec13ee80SMichael S. Tsirkin local_bh_enable(); 5703e9d08ecSBruce Rogers } 5713e9d08ecSBruce Rogers } 5723e9d08ecSBruce Rogers 5733161e453SRusty Russell static void refill_work(struct work_struct *work) 5743161e453SRusty Russell { 575e9d7417bSJason Wang struct virtnet_info *vi = 576e9d7417bSJason Wang container_of(work, struct virtnet_info, refill.work); 5773161e453SRusty Russell bool still_empty; 578986a4f4dSJason Wang int i; 5793161e453SRusty Russell 580986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 581986a4f4dSJason Wang struct receive_queue *rq = &vi->rq[i]; 582986a4f4dSJason Wang 583986a4f4dSJason Wang napi_disable(&rq->napi); 584986a4f4dSJason Wang still_empty = !try_fill_recv(rq, GFP_KERNEL); 585986a4f4dSJason Wang virtnet_napi_enable(rq); 5863161e453SRusty Russell 5873161e453SRusty Russell /* In theory, this can happen: if we don't get any buffers in 588986a4f4dSJason Wang * we will *never* try to fill again. 589986a4f4dSJason Wang */ 5903161e453SRusty Russell if (still_empty) 5913b07e9caSTejun Heo schedule_delayed_work(&vi->refill, HZ/2); 5923161e453SRusty Russell } 593986a4f4dSJason Wang } 5943161e453SRusty Russell 595296f96fcSRusty Russell static int virtnet_poll(struct napi_struct *napi, int budget) 596296f96fcSRusty Russell { 597e9d7417bSJason Wang struct receive_queue *rq = 598e9d7417bSJason Wang container_of(napi, struct receive_queue, napi); 599e9d7417bSJason Wang struct virtnet_info *vi = rq->vq->vdev->priv; 6009ab86bbcSShirley Ma void *buf; 601296f96fcSRusty Russell unsigned int len, received = 0; 602296f96fcSRusty Russell 603296f96fcSRusty Russell again: 604296f96fcSRusty Russell while (received < budget && 605e9d7417bSJason Wang (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) { 606e9d7417bSJason Wang receive_buf(rq, buf, len); 607e9d7417bSJason Wang --rq->num; 608296f96fcSRusty Russell received++; 609296f96fcSRusty Russell } 610296f96fcSRusty Russell 611e9d7417bSJason Wang if (rq->num < rq->max / 2) { 612e9d7417bSJason Wang if (!try_fill_recv(rq, GFP_ATOMIC)) 6133b07e9caSTejun Heo schedule_delayed_work(&vi->refill, 0); 6143161e453SRusty Russell } 615296f96fcSRusty Russell 6168329d98eSRusty Russell /* Out of packets? */ 6178329d98eSRusty Russell if (received < budget) { 618288379f0SBen Hutchings napi_complete(napi); 619e9d7417bSJason Wang if (unlikely(!virtqueue_enable_cb(rq->vq)) && 6208e95a202SJoe Perches napi_schedule_prep(napi)) { 621e9d7417bSJason Wang virtqueue_disable_cb(rq->vq); 622288379f0SBen Hutchings __napi_schedule(napi); 623296f96fcSRusty Russell goto again; 624296f96fcSRusty Russell } 6254265f161SChristian Borntraeger } 626296f96fcSRusty Russell 627296f96fcSRusty Russell return received; 628296f96fcSRusty Russell } 629296f96fcSRusty Russell 630986a4f4dSJason Wang static int virtnet_open(struct net_device *dev) 631986a4f4dSJason Wang { 632986a4f4dSJason Wang struct virtnet_info *vi = netdev_priv(dev); 633986a4f4dSJason Wang int i; 634986a4f4dSJason Wang 635986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 636986a4f4dSJason Wang /* Make sure we have some buffers: if oom use wq. */ 637986a4f4dSJason Wang if (!try_fill_recv(&vi->rq[i], GFP_KERNEL)) 638986a4f4dSJason Wang schedule_delayed_work(&vi->refill, 0); 639986a4f4dSJason Wang virtnet_napi_enable(&vi->rq[i]); 640986a4f4dSJason Wang } 641986a4f4dSJason Wang 642986a4f4dSJason Wang return 0; 643986a4f4dSJason Wang } 644986a4f4dSJason Wang 645b7dfde95SLinus Torvalds static void free_old_xmit_skbs(struct send_queue *sq) 646296f96fcSRusty Russell { 647296f96fcSRusty Russell struct sk_buff *skb; 6486ee57bccSMichael S. Tsirkin unsigned int len; 649e9d7417bSJason Wang struct virtnet_info *vi = sq->vq->vdev->priv; 65058472a76SEric Dumazet struct virtnet_stats *stats = this_cpu_ptr(vi->stats); 651296f96fcSRusty Russell 652e9d7417bSJason Wang while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) { 653296f96fcSRusty Russell pr_debug("Sent skb %p\n", skb); 6543fa2a1dfSstephen hemminger 65583a27052SEric Dumazet u64_stats_update_begin(&stats->tx_syncp); 6563fa2a1dfSstephen hemminger stats->tx_bytes += skb->len; 6573fa2a1dfSstephen hemminger stats->tx_packets++; 65883a27052SEric Dumazet u64_stats_update_end(&stats->tx_syncp); 6593fa2a1dfSstephen hemminger 660ed79bab8SEric Dumazet dev_kfree_skb_any(skb); 661296f96fcSRusty Russell } 662296f96fcSRusty Russell } 663296f96fcSRusty Russell 664e9d7417bSJason Wang static int xmit_skb(struct send_queue *sq, struct sk_buff *skb) 665296f96fcSRusty Russell { 666b3f24698SRusty Russell struct skb_vnet_hdr *hdr = skb_vnet_hdr(skb); 667296f96fcSRusty Russell const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest; 668e9d7417bSJason Wang struct virtnet_info *vi = sq->vq->vdev->priv; 6697bedc7dcSMichael S. Tsirkin unsigned num_sg; 670296f96fcSRusty Russell 671e174961cSJohannes Berg pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest); 672296f96fcSRusty Russell 673296f96fcSRusty Russell if (skb->ip_summed == CHECKSUM_PARTIAL) { 674b3f24698SRusty Russell hdr->hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM; 67555508d60SMichał Mirosław hdr->hdr.csum_start = skb_checksum_start_offset(skb); 676b3f24698SRusty Russell hdr->hdr.csum_offset = skb->csum_offset; 677296f96fcSRusty Russell } else { 678b3f24698SRusty Russell hdr->hdr.flags = 0; 679b3f24698SRusty Russell hdr->hdr.csum_offset = hdr->hdr.csum_start = 0; 680296f96fcSRusty Russell } 681296f96fcSRusty Russell 682296f96fcSRusty Russell if (skb_is_gso(skb)) { 683b3f24698SRusty Russell hdr->hdr.hdr_len = skb_headlen(skb); 684b3f24698SRusty Russell hdr->hdr.gso_size = skb_shinfo(skb)->gso_size; 68534a48579SRusty Russell if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) 686b3f24698SRusty Russell hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4; 687296f96fcSRusty Russell else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) 688b3f24698SRusty Russell hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6; 689296f96fcSRusty Russell else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP) 690b3f24698SRusty Russell hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP; 691296f96fcSRusty Russell else 692296f96fcSRusty Russell BUG(); 69334a48579SRusty Russell if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN) 694b3f24698SRusty Russell hdr->hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN; 695296f96fcSRusty Russell } else { 696b3f24698SRusty Russell hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE; 697b3f24698SRusty Russell hdr->hdr.gso_size = hdr->hdr.hdr_len = 0; 698296f96fcSRusty Russell } 699296f96fcSRusty Russell 700b3f24698SRusty Russell hdr->mhdr.num_buffers = 0; 7013f2c31d9SMark McLoughlin 7023f2c31d9SMark McLoughlin /* Encode metadata header at front. */ 7033f2c31d9SMark McLoughlin if (vi->mergeable_rx_bufs) 704e9d7417bSJason Wang sg_set_buf(sq->sg, &hdr->mhdr, sizeof hdr->mhdr); 7053f2c31d9SMark McLoughlin else 706e9d7417bSJason Wang sg_set_buf(sq->sg, &hdr->hdr, sizeof hdr->hdr); 7073f2c31d9SMark McLoughlin 708b7dfde95SLinus Torvalds num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len) + 1; 709b7dfde95SLinus Torvalds return virtqueue_add_buf(sq->vq, sq->sg, num_sg, 710f96fde41SRusty Russell 0, skb, GFP_ATOMIC); 71111a3a154SRusty Russell } 71211a3a154SRusty Russell 713424efe9cSStephen Hemminger static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev) 71499ffc696SRusty Russell { 71599ffc696SRusty Russell struct virtnet_info *vi = netdev_priv(dev); 716986a4f4dSJason Wang int qnum = skb_get_queue_mapping(skb); 717986a4f4dSJason Wang struct send_queue *sq = &vi->sq[qnum]; 7189ed4cb07SRusty Russell int err; 7192cb9c6baSRusty Russell 7202cb9c6baSRusty Russell /* Free up any pending old buffers before queueing new ones. */ 721e9d7417bSJason Wang free_old_xmit_skbs(sq); 72299ffc696SRusty Russell 72303f191baSMichael S. Tsirkin /* Try to transmit */ 724b7dfde95SLinus Torvalds err = xmit_skb(sq, skb); 72599ffc696SRusty Russell 7269ed4cb07SRusty Russell /* This should not happen! */ 7270e3daa64SRusty Russell if (unlikely(err)) { 72858eba97dSRusty Russell dev->stats.tx_fifo_errors++; 7292e57b79cSRick Jones if (net_ratelimit()) 73058eba97dSRusty Russell dev_warn(&dev->dev, 731b7dfde95SLinus Torvalds "Unexpected TXQ (%d) queue failure: %d\n", qnum, err); 73258eba97dSRusty Russell dev->stats.tx_dropped++; 73358eba97dSRusty Russell kfree_skb(skb); 73458eba97dSRusty Russell return NETDEV_TX_OK; 735296f96fcSRusty Russell } 736e9d7417bSJason Wang virtqueue_kick(sq->vq); 73703f191baSMichael S. Tsirkin 73848925e37SRusty Russell /* Don't wait up for transmitted skbs to be freed. */ 73948925e37SRusty Russell skb_orphan(skb); 74048925e37SRusty Russell nf_reset(skb); 74148925e37SRusty Russell 74248925e37SRusty Russell /* Apparently nice girls don't return TX_BUSY; stop the queue 74348925e37SRusty Russell * before it gets out of hand. Naturally, this wastes entries. */ 744b7dfde95SLinus Torvalds if (sq->vq->num_free < 2+MAX_SKB_FRAGS) { 745986a4f4dSJason Wang netif_stop_subqueue(dev, qnum); 746e9d7417bSJason Wang if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) { 74748925e37SRusty Russell /* More just got used, free them then recheck. */ 748b7dfde95SLinus Torvalds free_old_xmit_skbs(sq); 749b7dfde95SLinus Torvalds if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) { 750986a4f4dSJason Wang netif_start_subqueue(dev, qnum); 751e9d7417bSJason Wang virtqueue_disable_cb(sq->vq); 75248925e37SRusty Russell } 75348925e37SRusty Russell } 75448925e37SRusty Russell } 75548925e37SRusty Russell 75648925e37SRusty Russell return NETDEV_TX_OK; 75748925e37SRusty Russell } 75848925e37SRusty Russell 7599c46f6d4SAlex Williamson static int virtnet_set_mac_address(struct net_device *dev, void *p) 7609c46f6d4SAlex Williamson { 7619c46f6d4SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 7629c46f6d4SAlex Williamson struct virtio_device *vdev = vi->vdev; 763f2f2c8b4SJiri Pirko int ret; 7649c46f6d4SAlex Williamson 765f2f2c8b4SJiri Pirko ret = eth_mac_addr(dev, p); 766f2f2c8b4SJiri Pirko if (ret) 767f2f2c8b4SJiri Pirko return ret; 7689c46f6d4SAlex Williamson 76962994b2dSAlex Williamson if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) 7709c46f6d4SAlex Williamson vdev->config->set(vdev, offsetof(struct virtio_net_config, mac), 7719c46f6d4SAlex Williamson dev->dev_addr, dev->addr_len); 7729c46f6d4SAlex Williamson 7739c46f6d4SAlex Williamson return 0; 7749c46f6d4SAlex Williamson } 7759c46f6d4SAlex Williamson 7763fa2a1dfSstephen hemminger static struct rtnl_link_stats64 *virtnet_stats(struct net_device *dev, 7773fa2a1dfSstephen hemminger struct rtnl_link_stats64 *tot) 7783fa2a1dfSstephen hemminger { 7793fa2a1dfSstephen hemminger struct virtnet_info *vi = netdev_priv(dev); 7803fa2a1dfSstephen hemminger int cpu; 7813fa2a1dfSstephen hemminger unsigned int start; 7823fa2a1dfSstephen hemminger 7833fa2a1dfSstephen hemminger for_each_possible_cpu(cpu) { 78458472a76SEric Dumazet struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu); 7853fa2a1dfSstephen hemminger u64 tpackets, tbytes, rpackets, rbytes; 7863fa2a1dfSstephen hemminger 7873fa2a1dfSstephen hemminger do { 788e3906486SKevin Groeneveld start = u64_stats_fetch_begin_bh(&stats->tx_syncp); 7893fa2a1dfSstephen hemminger tpackets = stats->tx_packets; 7903fa2a1dfSstephen hemminger tbytes = stats->tx_bytes; 791e3906486SKevin Groeneveld } while (u64_stats_fetch_retry_bh(&stats->tx_syncp, start)); 79283a27052SEric Dumazet 79383a27052SEric Dumazet do { 794e3906486SKevin Groeneveld start = u64_stats_fetch_begin_bh(&stats->rx_syncp); 7953fa2a1dfSstephen hemminger rpackets = stats->rx_packets; 7963fa2a1dfSstephen hemminger rbytes = stats->rx_bytes; 797e3906486SKevin Groeneveld } while (u64_stats_fetch_retry_bh(&stats->rx_syncp, start)); 7983fa2a1dfSstephen hemminger 7993fa2a1dfSstephen hemminger tot->rx_packets += rpackets; 8003fa2a1dfSstephen hemminger tot->tx_packets += tpackets; 8013fa2a1dfSstephen hemminger tot->rx_bytes += rbytes; 8023fa2a1dfSstephen hemminger tot->tx_bytes += tbytes; 8033fa2a1dfSstephen hemminger } 8043fa2a1dfSstephen hemminger 8053fa2a1dfSstephen hemminger tot->tx_dropped = dev->stats.tx_dropped; 806021ac8d3SRick Jones tot->tx_fifo_errors = dev->stats.tx_fifo_errors; 8073fa2a1dfSstephen hemminger tot->rx_dropped = dev->stats.rx_dropped; 8083fa2a1dfSstephen hemminger tot->rx_length_errors = dev->stats.rx_length_errors; 8093fa2a1dfSstephen hemminger tot->rx_frame_errors = dev->stats.rx_frame_errors; 8103fa2a1dfSstephen hemminger 8113fa2a1dfSstephen hemminger return tot; 8123fa2a1dfSstephen hemminger } 8133fa2a1dfSstephen hemminger 814da74e89dSAmit Shah #ifdef CONFIG_NET_POLL_CONTROLLER 815da74e89dSAmit Shah static void virtnet_netpoll(struct net_device *dev) 816da74e89dSAmit Shah { 817da74e89dSAmit Shah struct virtnet_info *vi = netdev_priv(dev); 818986a4f4dSJason Wang int i; 819da74e89dSAmit Shah 820986a4f4dSJason Wang for (i = 0; i < vi->curr_queue_pairs; i++) 821986a4f4dSJason Wang napi_schedule(&vi->rq[i].napi); 822da74e89dSAmit Shah } 823da74e89dSAmit Shah #endif 824da74e89dSAmit Shah 8252a41f71dSAlex Williamson /* 8262a41f71dSAlex Williamson * Send command via the control virtqueue and check status. Commands 8272a41f71dSAlex Williamson * supported by the hypervisor, as indicated by feature bits, should 8282a41f71dSAlex Williamson * never fail unless improperly formated. 8292a41f71dSAlex Williamson */ 8302a41f71dSAlex Williamson static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd, 8312a41f71dSAlex Williamson struct scatterlist *data, int out, int in) 8322a41f71dSAlex Williamson { 83323e258e1SAlex Williamson struct scatterlist *s, sg[VIRTNET_SEND_COMMAND_SG_MAX + 2]; 8342a41f71dSAlex Williamson struct virtio_net_ctrl_hdr ctrl; 8352a41f71dSAlex Williamson virtio_net_ctrl_ack status = ~0; 8362a41f71dSAlex Williamson unsigned int tmp; 83723e258e1SAlex Williamson int i; 8382a41f71dSAlex Williamson 8390ee904c3SAlexander Beregalov /* Caller should know better */ 8400ee904c3SAlexander Beregalov BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) || 8410ee904c3SAlexander Beregalov (out + in > VIRTNET_SEND_COMMAND_SG_MAX)); 8422a41f71dSAlex Williamson 8432a41f71dSAlex Williamson out++; /* Add header */ 8442a41f71dSAlex Williamson in++; /* Add return status */ 8452a41f71dSAlex Williamson 8462a41f71dSAlex Williamson ctrl.class = class; 8472a41f71dSAlex Williamson ctrl.cmd = cmd; 8482a41f71dSAlex Williamson 8492a41f71dSAlex Williamson sg_init_table(sg, out + in); 8502a41f71dSAlex Williamson 8512a41f71dSAlex Williamson sg_set_buf(&sg[0], &ctrl, sizeof(ctrl)); 85223e258e1SAlex Williamson for_each_sg(data, s, out + in - 2, i) 85323e258e1SAlex Williamson sg_set_buf(&sg[i + 1], sg_virt(s), s->length); 8542a41f71dSAlex Williamson sg_set_buf(&sg[out + in - 1], &status, sizeof(status)); 8552a41f71dSAlex Williamson 856f96fde41SRusty Russell BUG_ON(virtqueue_add_buf(vi->cvq, sg, out, in, vi, GFP_ATOMIC) < 0); 8572a41f71dSAlex Williamson 8581915a712SMichael S. Tsirkin virtqueue_kick(vi->cvq); 8592a41f71dSAlex Williamson 8602a41f71dSAlex Williamson /* 8612a41f71dSAlex Williamson * Spin for a response, the kick causes an ioport write, trapping 8622a41f71dSAlex Williamson * into the hypervisor, so the request should be handled immediately. 8632a41f71dSAlex Williamson */ 8641915a712SMichael S. Tsirkin while (!virtqueue_get_buf(vi->cvq, &tmp)) 8652a41f71dSAlex Williamson cpu_relax(); 8662a41f71dSAlex Williamson 8672a41f71dSAlex Williamson return status == VIRTIO_NET_OK; 8682a41f71dSAlex Williamson } 8692a41f71dSAlex Williamson 870586d17c5SJason Wang static void virtnet_ack_link_announce(struct virtnet_info *vi) 871586d17c5SJason Wang { 872586d17c5SJason Wang rtnl_lock(); 873586d17c5SJason Wang if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE, 874586d17c5SJason Wang VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL, 875586d17c5SJason Wang 0, 0)) 876586d17c5SJason Wang dev_warn(&vi->dev->dev, "Failed to ack link announce.\n"); 877586d17c5SJason Wang rtnl_unlock(); 878586d17c5SJason Wang } 879586d17c5SJason Wang 880986a4f4dSJason Wang static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs) 881986a4f4dSJason Wang { 882986a4f4dSJason Wang struct scatterlist sg; 883986a4f4dSJason Wang struct virtio_net_ctrl_mq s; 884986a4f4dSJason Wang struct net_device *dev = vi->dev; 885986a4f4dSJason Wang 886986a4f4dSJason Wang if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ)) 887986a4f4dSJason Wang return 0; 888986a4f4dSJason Wang 889986a4f4dSJason Wang s.virtqueue_pairs = queue_pairs; 890986a4f4dSJason Wang sg_init_one(&sg, &s, sizeof(s)); 891986a4f4dSJason Wang 892986a4f4dSJason Wang if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ, 893986a4f4dSJason Wang VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg, 1, 0)){ 894986a4f4dSJason Wang dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n", 895986a4f4dSJason Wang queue_pairs); 896986a4f4dSJason Wang return -EINVAL; 897986a4f4dSJason Wang } else 898986a4f4dSJason Wang vi->curr_queue_pairs = queue_pairs; 899986a4f4dSJason Wang 900986a4f4dSJason Wang return 0; 901986a4f4dSJason Wang } 902986a4f4dSJason Wang 903296f96fcSRusty Russell static int virtnet_close(struct net_device *dev) 904296f96fcSRusty Russell { 905296f96fcSRusty Russell struct virtnet_info *vi = netdev_priv(dev); 906986a4f4dSJason Wang int i; 907296f96fcSRusty Russell 908b2baed69SRusty Russell /* Make sure refill_work doesn't re-enable napi! */ 909b2baed69SRusty Russell cancel_delayed_work_sync(&vi->refill); 910986a4f4dSJason Wang 911986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) 912986a4f4dSJason Wang napi_disable(&vi->rq[i].napi); 913296f96fcSRusty Russell 914296f96fcSRusty Russell return 0; 915296f96fcSRusty Russell } 916296f96fcSRusty Russell 9172af7698eSAlex Williamson static void virtnet_set_rx_mode(struct net_device *dev) 9182af7698eSAlex Williamson { 9192af7698eSAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 920f565a7c2SAlex Williamson struct scatterlist sg[2]; 9212af7698eSAlex Williamson u8 promisc, allmulti; 922f565a7c2SAlex Williamson struct virtio_net_ctrl_mac *mac_data; 923ccffad25SJiri Pirko struct netdev_hw_addr *ha; 92432e7bfc4SJiri Pirko int uc_count; 9254cd24eafSJiri Pirko int mc_count; 926f565a7c2SAlex Williamson void *buf; 927f565a7c2SAlex Williamson int i; 9282af7698eSAlex Williamson 9292af7698eSAlex Williamson /* We can't dynamicaly set ndo_set_rx_mode, so return gracefully */ 9302af7698eSAlex Williamson if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX)) 9312af7698eSAlex Williamson return; 9322af7698eSAlex Williamson 933f565a7c2SAlex Williamson promisc = ((dev->flags & IFF_PROMISC) != 0); 934f565a7c2SAlex Williamson allmulti = ((dev->flags & IFF_ALLMULTI) != 0); 9352af7698eSAlex Williamson 93623e258e1SAlex Williamson sg_init_one(sg, &promisc, sizeof(promisc)); 9372af7698eSAlex Williamson 9382af7698eSAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, 9392af7698eSAlex Williamson VIRTIO_NET_CTRL_RX_PROMISC, 940f565a7c2SAlex Williamson sg, 1, 0)) 9412af7698eSAlex Williamson dev_warn(&dev->dev, "Failed to %sable promisc mode.\n", 9422af7698eSAlex Williamson promisc ? "en" : "dis"); 9432af7698eSAlex Williamson 94423e258e1SAlex Williamson sg_init_one(sg, &allmulti, sizeof(allmulti)); 9452af7698eSAlex Williamson 9462af7698eSAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, 9472af7698eSAlex Williamson VIRTIO_NET_CTRL_RX_ALLMULTI, 948f565a7c2SAlex Williamson sg, 1, 0)) 9492af7698eSAlex Williamson dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n", 9502af7698eSAlex Williamson allmulti ? "en" : "dis"); 951f565a7c2SAlex Williamson 95232e7bfc4SJiri Pirko uc_count = netdev_uc_count(dev); 9534cd24eafSJiri Pirko mc_count = netdev_mc_count(dev); 954f565a7c2SAlex Williamson /* MAC filter - use one buffer for both lists */ 9554cd24eafSJiri Pirko buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) + 956f565a7c2SAlex Williamson (2 * sizeof(mac_data->entries)), GFP_ATOMIC); 9574cd24eafSJiri Pirko mac_data = buf; 958f565a7c2SAlex Williamson if (!buf) { 959f565a7c2SAlex Williamson dev_warn(&dev->dev, "No memory for MAC address buffer\n"); 960f565a7c2SAlex Williamson return; 961f565a7c2SAlex Williamson } 962f565a7c2SAlex Williamson 96323e258e1SAlex Williamson sg_init_table(sg, 2); 96423e258e1SAlex Williamson 965f565a7c2SAlex Williamson /* Store the unicast list and count in the front of the buffer */ 96632e7bfc4SJiri Pirko mac_data->entries = uc_count; 967ccffad25SJiri Pirko i = 0; 96832e7bfc4SJiri Pirko netdev_for_each_uc_addr(ha, dev) 969ccffad25SJiri Pirko memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN); 970f565a7c2SAlex Williamson 971f565a7c2SAlex Williamson sg_set_buf(&sg[0], mac_data, 97232e7bfc4SJiri Pirko sizeof(mac_data->entries) + (uc_count * ETH_ALEN)); 973f565a7c2SAlex Williamson 974f565a7c2SAlex Williamson /* multicast list and count fill the end */ 97532e7bfc4SJiri Pirko mac_data = (void *)&mac_data->macs[uc_count][0]; 976f565a7c2SAlex Williamson 9774cd24eafSJiri Pirko mac_data->entries = mc_count; 978567ec874SJiri Pirko i = 0; 97922bedad3SJiri Pirko netdev_for_each_mc_addr(ha, dev) 98022bedad3SJiri Pirko memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN); 981f565a7c2SAlex Williamson 982f565a7c2SAlex Williamson sg_set_buf(&sg[1], mac_data, 9834cd24eafSJiri Pirko sizeof(mac_data->entries) + (mc_count * ETH_ALEN)); 984f565a7c2SAlex Williamson 985f565a7c2SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, 986f565a7c2SAlex Williamson VIRTIO_NET_CTRL_MAC_TABLE_SET, 987f565a7c2SAlex Williamson sg, 2, 0)) 988f565a7c2SAlex Williamson dev_warn(&dev->dev, "Failed to set MAC fitler table.\n"); 989f565a7c2SAlex Williamson 990f565a7c2SAlex Williamson kfree(buf); 9912af7698eSAlex Williamson } 9922af7698eSAlex Williamson 9938e586137SJiri Pirko static int virtnet_vlan_rx_add_vid(struct net_device *dev, u16 vid) 9940bde9569SAlex Williamson { 9950bde9569SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 9960bde9569SAlex Williamson struct scatterlist sg; 9970bde9569SAlex Williamson 99823e258e1SAlex Williamson sg_init_one(&sg, &vid, sizeof(vid)); 9990bde9569SAlex Williamson 10000bde9569SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, 10010bde9569SAlex Williamson VIRTIO_NET_CTRL_VLAN_ADD, &sg, 1, 0)) 10020bde9569SAlex Williamson dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid); 10038e586137SJiri Pirko return 0; 10040bde9569SAlex Williamson } 10050bde9569SAlex Williamson 10068e586137SJiri Pirko static int virtnet_vlan_rx_kill_vid(struct net_device *dev, u16 vid) 10070bde9569SAlex Williamson { 10080bde9569SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 10090bde9569SAlex Williamson struct scatterlist sg; 10100bde9569SAlex Williamson 101123e258e1SAlex Williamson sg_init_one(&sg, &vid, sizeof(vid)); 10120bde9569SAlex Williamson 10130bde9569SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, 10140bde9569SAlex Williamson VIRTIO_NET_CTRL_VLAN_DEL, &sg, 1, 0)) 10150bde9569SAlex Williamson dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid); 10168e586137SJiri Pirko return 0; 10170bde9569SAlex Williamson } 10180bde9569SAlex Williamson 1019986a4f4dSJason Wang static void virtnet_set_affinity(struct virtnet_info *vi, bool set) 1020986a4f4dSJason Wang { 1021986a4f4dSJason Wang int i; 1022*47be2479SWanlong Gao int cpu; 1023986a4f4dSJason Wang 1024986a4f4dSJason Wang /* In multiqueue mode, when the number of cpu is equal to the number of 1025986a4f4dSJason Wang * queue pairs, we let the queue pairs to be private to one cpu by 1026986a4f4dSJason Wang * setting the affinity hint to eliminate the contention. 1027986a4f4dSJason Wang */ 1028986a4f4dSJason Wang if ((vi->curr_queue_pairs == 1 || 1029986a4f4dSJason Wang vi->max_queue_pairs != num_online_cpus()) && set) { 1030*47be2479SWanlong Gao if (vi->affinity_hint_set) { 1031986a4f4dSJason Wang set = false; 1032*47be2479SWanlong Gao } else { 1033*47be2479SWanlong Gao i = 0; 1034*47be2479SWanlong Gao for_each_online_cpu(cpu) 1035*47be2479SWanlong Gao *per_cpu_ptr(vi->vq_index, cpu) = 1036*47be2479SWanlong Gao ++i % vi->curr_queue_pairs; 1037986a4f4dSJason Wang return; 1038986a4f4dSJason Wang } 1039986a4f4dSJason Wang } 1040986a4f4dSJason Wang 1041*47be2479SWanlong Gao if (set) { 1042*47be2479SWanlong Gao i = 0; 1043*47be2479SWanlong Gao for_each_online_cpu(cpu) { 1044*47be2479SWanlong Gao virtqueue_set_affinity(vi->rq[i].vq, cpu); 1045*47be2479SWanlong Gao virtqueue_set_affinity(vi->sq[i].vq, cpu); 1046*47be2479SWanlong Gao *per_cpu_ptr(vi->vq_index, cpu) = i; 1047*47be2479SWanlong Gao i++; 1048*47be2479SWanlong Gao } 1049*47be2479SWanlong Gao 1050986a4f4dSJason Wang vi->affinity_hint_set = true; 1051*47be2479SWanlong Gao } else { 1052*47be2479SWanlong Gao for(i = 0; i < vi->max_queue_pairs; i++) { 1053*47be2479SWanlong Gao virtqueue_set_affinity(vi->rq[i].vq, -1); 1054*47be2479SWanlong Gao virtqueue_set_affinity(vi->sq[i].vq, -1); 1055*47be2479SWanlong Gao } 1056*47be2479SWanlong Gao 1057*47be2479SWanlong Gao i = 0; 1058*47be2479SWanlong Gao for_each_online_cpu(cpu) 1059*47be2479SWanlong Gao *per_cpu_ptr(vi->vq_index, cpu) = 1060*47be2479SWanlong Gao ++i % vi->curr_queue_pairs; 1061*47be2479SWanlong Gao 1062986a4f4dSJason Wang vi->affinity_hint_set = false; 1063986a4f4dSJason Wang } 1064*47be2479SWanlong Gao } 1065986a4f4dSJason Wang 10668f9f4668SRick Jones static void virtnet_get_ringparam(struct net_device *dev, 10678f9f4668SRick Jones struct ethtool_ringparam *ring) 10688f9f4668SRick Jones { 10698f9f4668SRick Jones struct virtnet_info *vi = netdev_priv(dev); 10708f9f4668SRick Jones 1071986a4f4dSJason Wang ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq); 1072986a4f4dSJason Wang ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq); 10738f9f4668SRick Jones ring->rx_pending = ring->rx_max_pending; 10748f9f4668SRick Jones ring->tx_pending = ring->tx_max_pending; 10758f9f4668SRick Jones } 10768f9f4668SRick Jones 107766846048SRick Jones 107866846048SRick Jones static void virtnet_get_drvinfo(struct net_device *dev, 107966846048SRick Jones struct ethtool_drvinfo *info) 108066846048SRick Jones { 108166846048SRick Jones struct virtnet_info *vi = netdev_priv(dev); 108266846048SRick Jones struct virtio_device *vdev = vi->vdev; 108366846048SRick Jones 108466846048SRick Jones strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver)); 108566846048SRick Jones strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version)); 108666846048SRick Jones strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info)); 108766846048SRick Jones 108866846048SRick Jones } 108966846048SRick Jones 1090d73bcd2cSJason Wang /* TODO: Eliminate OOO packets during switching */ 1091d73bcd2cSJason Wang static int virtnet_set_channels(struct net_device *dev, 1092d73bcd2cSJason Wang struct ethtool_channels *channels) 1093d73bcd2cSJason Wang { 1094d73bcd2cSJason Wang struct virtnet_info *vi = netdev_priv(dev); 1095d73bcd2cSJason Wang u16 queue_pairs = channels->combined_count; 1096d73bcd2cSJason Wang int err; 1097d73bcd2cSJason Wang 1098d73bcd2cSJason Wang /* We don't support separate rx/tx channels. 1099d73bcd2cSJason Wang * We don't allow setting 'other' channels. 1100d73bcd2cSJason Wang */ 1101d73bcd2cSJason Wang if (channels->rx_count || channels->tx_count || channels->other_count) 1102d73bcd2cSJason Wang return -EINVAL; 1103d73bcd2cSJason Wang 1104d73bcd2cSJason Wang if (queue_pairs > vi->max_queue_pairs) 1105d73bcd2cSJason Wang return -EINVAL; 1106d73bcd2cSJason Wang 1107*47be2479SWanlong Gao get_online_cpus(); 1108d73bcd2cSJason Wang err = virtnet_set_queues(vi, queue_pairs); 1109d73bcd2cSJason Wang if (!err) { 1110d73bcd2cSJason Wang netif_set_real_num_tx_queues(dev, queue_pairs); 1111d73bcd2cSJason Wang netif_set_real_num_rx_queues(dev, queue_pairs); 1112d73bcd2cSJason Wang 1113d73bcd2cSJason Wang virtnet_set_affinity(vi, true); 1114d73bcd2cSJason Wang } 1115*47be2479SWanlong Gao put_online_cpus(); 1116d73bcd2cSJason Wang 1117d73bcd2cSJason Wang return err; 1118d73bcd2cSJason Wang } 1119d73bcd2cSJason Wang 1120d73bcd2cSJason Wang static void virtnet_get_channels(struct net_device *dev, 1121d73bcd2cSJason Wang struct ethtool_channels *channels) 1122d73bcd2cSJason Wang { 1123d73bcd2cSJason Wang struct virtnet_info *vi = netdev_priv(dev); 1124d73bcd2cSJason Wang 1125d73bcd2cSJason Wang channels->combined_count = vi->curr_queue_pairs; 1126d73bcd2cSJason Wang channels->max_combined = vi->max_queue_pairs; 1127d73bcd2cSJason Wang channels->max_other = 0; 1128d73bcd2cSJason Wang channels->rx_count = 0; 1129d73bcd2cSJason Wang channels->tx_count = 0; 1130d73bcd2cSJason Wang channels->other_count = 0; 1131d73bcd2cSJason Wang } 1132d73bcd2cSJason Wang 11330fc0b732SStephen Hemminger static const struct ethtool_ops virtnet_ethtool_ops = { 113466846048SRick Jones .get_drvinfo = virtnet_get_drvinfo, 11359f4d26d0SMark McLoughlin .get_link = ethtool_op_get_link, 11368f9f4668SRick Jones .get_ringparam = virtnet_get_ringparam, 1137d73bcd2cSJason Wang .set_channels = virtnet_set_channels, 1138d73bcd2cSJason Wang .get_channels = virtnet_get_channels, 1139a9ea3fc6SHerbert Xu }; 1140a9ea3fc6SHerbert Xu 114139da5814SMark McLoughlin #define MIN_MTU 68 114239da5814SMark McLoughlin #define MAX_MTU 65535 114339da5814SMark McLoughlin 114439da5814SMark McLoughlin static int virtnet_change_mtu(struct net_device *dev, int new_mtu) 114539da5814SMark McLoughlin { 114639da5814SMark McLoughlin if (new_mtu < MIN_MTU || new_mtu > MAX_MTU) 114739da5814SMark McLoughlin return -EINVAL; 114839da5814SMark McLoughlin dev->mtu = new_mtu; 114939da5814SMark McLoughlin return 0; 115039da5814SMark McLoughlin } 115139da5814SMark McLoughlin 1152986a4f4dSJason Wang /* To avoid contending a lock hold by a vcpu who would exit to host, select the 1153986a4f4dSJason Wang * txq based on the processor id. 1154986a4f4dSJason Wang */ 1155986a4f4dSJason Wang static u16 virtnet_select_queue(struct net_device *dev, struct sk_buff *skb) 1156986a4f4dSJason Wang { 1157*47be2479SWanlong Gao int txq; 1158*47be2479SWanlong Gao struct virtnet_info *vi = netdev_priv(dev); 1159*47be2479SWanlong Gao 1160*47be2479SWanlong Gao if (skb_rx_queue_recorded(skb)) { 1161*47be2479SWanlong Gao txq = skb_get_rx_queue(skb); 1162*47be2479SWanlong Gao } else { 1163*47be2479SWanlong Gao txq = *__this_cpu_ptr(vi->vq_index); 1164*47be2479SWanlong Gao if (txq == -1) 1165*47be2479SWanlong Gao txq = 0; 1166*47be2479SWanlong Gao } 1167986a4f4dSJason Wang 1168986a4f4dSJason Wang while (unlikely(txq >= dev->real_num_tx_queues)) 1169986a4f4dSJason Wang txq -= dev->real_num_tx_queues; 1170986a4f4dSJason Wang 1171986a4f4dSJason Wang return txq; 1172986a4f4dSJason Wang } 1173986a4f4dSJason Wang 117476288b4eSStephen Hemminger static const struct net_device_ops virtnet_netdev = { 117576288b4eSStephen Hemminger .ndo_open = virtnet_open, 117676288b4eSStephen Hemminger .ndo_stop = virtnet_close, 117776288b4eSStephen Hemminger .ndo_start_xmit = start_xmit, 117876288b4eSStephen Hemminger .ndo_validate_addr = eth_validate_addr, 11799c46f6d4SAlex Williamson .ndo_set_mac_address = virtnet_set_mac_address, 11802af7698eSAlex Williamson .ndo_set_rx_mode = virtnet_set_rx_mode, 118176288b4eSStephen Hemminger .ndo_change_mtu = virtnet_change_mtu, 11823fa2a1dfSstephen hemminger .ndo_get_stats64 = virtnet_stats, 11831824a989SAlex Williamson .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid, 11841824a989SAlex Williamson .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid, 1185986a4f4dSJason Wang .ndo_select_queue = virtnet_select_queue, 118676288b4eSStephen Hemminger #ifdef CONFIG_NET_POLL_CONTROLLER 118776288b4eSStephen Hemminger .ndo_poll_controller = virtnet_netpoll, 118876288b4eSStephen Hemminger #endif 118976288b4eSStephen Hemminger }; 119076288b4eSStephen Hemminger 1191586d17c5SJason Wang static void virtnet_config_changed_work(struct work_struct *work) 11929f4d26d0SMark McLoughlin { 1193586d17c5SJason Wang struct virtnet_info *vi = 1194586d17c5SJason Wang container_of(work, struct virtnet_info, config_work); 11959f4d26d0SMark McLoughlin u16 v; 11969f4d26d0SMark McLoughlin 1197586d17c5SJason Wang mutex_lock(&vi->config_lock); 1198586d17c5SJason Wang if (!vi->config_enable) 1199586d17c5SJason Wang goto done; 1200586d17c5SJason Wang 120177dd7693SSasha Levin if (virtio_config_val(vi->vdev, VIRTIO_NET_F_STATUS, 12029f4d26d0SMark McLoughlin offsetof(struct virtio_net_config, status), 120377dd7693SSasha Levin &v) < 0) 1204586d17c5SJason Wang goto done; 1205586d17c5SJason Wang 1206586d17c5SJason Wang if (v & VIRTIO_NET_S_ANNOUNCE) { 1207ee89bab1SAmerigo Wang netdev_notify_peers(vi->dev); 1208586d17c5SJason Wang virtnet_ack_link_announce(vi); 1209586d17c5SJason Wang } 12109f4d26d0SMark McLoughlin 12119f4d26d0SMark McLoughlin /* Ignore unknown (future) status bits */ 12129f4d26d0SMark McLoughlin v &= VIRTIO_NET_S_LINK_UP; 12139f4d26d0SMark McLoughlin 12149f4d26d0SMark McLoughlin if (vi->status == v) 1215586d17c5SJason Wang goto done; 12169f4d26d0SMark McLoughlin 12179f4d26d0SMark McLoughlin vi->status = v; 12189f4d26d0SMark McLoughlin 12199f4d26d0SMark McLoughlin if (vi->status & VIRTIO_NET_S_LINK_UP) { 12209f4d26d0SMark McLoughlin netif_carrier_on(vi->dev); 1221986a4f4dSJason Wang netif_tx_wake_all_queues(vi->dev); 12229f4d26d0SMark McLoughlin } else { 12239f4d26d0SMark McLoughlin netif_carrier_off(vi->dev); 1224986a4f4dSJason Wang netif_tx_stop_all_queues(vi->dev); 12259f4d26d0SMark McLoughlin } 1226586d17c5SJason Wang done: 1227586d17c5SJason Wang mutex_unlock(&vi->config_lock); 12289f4d26d0SMark McLoughlin } 12299f4d26d0SMark McLoughlin 12309f4d26d0SMark McLoughlin static void virtnet_config_changed(struct virtio_device *vdev) 12319f4d26d0SMark McLoughlin { 12329f4d26d0SMark McLoughlin struct virtnet_info *vi = vdev->priv; 12339f4d26d0SMark McLoughlin 12343b07e9caSTejun Heo schedule_work(&vi->config_work); 12359f4d26d0SMark McLoughlin } 12369f4d26d0SMark McLoughlin 1237986a4f4dSJason Wang static void virtnet_free_queues(struct virtnet_info *vi) 1238986a4f4dSJason Wang { 1239986a4f4dSJason Wang kfree(vi->rq); 1240986a4f4dSJason Wang kfree(vi->sq); 1241986a4f4dSJason Wang } 1242986a4f4dSJason Wang 1243986a4f4dSJason Wang static void free_receive_bufs(struct virtnet_info *vi) 1244986a4f4dSJason Wang { 1245986a4f4dSJason Wang int i; 1246986a4f4dSJason Wang 1247986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1248986a4f4dSJason Wang while (vi->rq[i].pages) 1249986a4f4dSJason Wang __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0); 1250986a4f4dSJason Wang } 1251986a4f4dSJason Wang } 1252986a4f4dSJason Wang 1253986a4f4dSJason Wang static void free_unused_bufs(struct virtnet_info *vi) 1254986a4f4dSJason Wang { 1255986a4f4dSJason Wang void *buf; 1256986a4f4dSJason Wang int i; 1257986a4f4dSJason Wang 1258986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1259986a4f4dSJason Wang struct virtqueue *vq = vi->sq[i].vq; 1260986a4f4dSJason Wang while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) 1261986a4f4dSJason Wang dev_kfree_skb(buf); 1262986a4f4dSJason Wang } 1263986a4f4dSJason Wang 1264986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1265986a4f4dSJason Wang struct virtqueue *vq = vi->rq[i].vq; 1266986a4f4dSJason Wang 1267986a4f4dSJason Wang while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) { 1268986a4f4dSJason Wang if (vi->mergeable_rx_bufs || vi->big_packets) 1269986a4f4dSJason Wang give_pages(&vi->rq[i], buf); 1270986a4f4dSJason Wang else 1271986a4f4dSJason Wang dev_kfree_skb(buf); 1272986a4f4dSJason Wang --vi->rq[i].num; 1273986a4f4dSJason Wang } 1274986a4f4dSJason Wang BUG_ON(vi->rq[i].num != 0); 1275986a4f4dSJason Wang } 1276986a4f4dSJason Wang } 1277986a4f4dSJason Wang 1278e9d7417bSJason Wang static void virtnet_del_vqs(struct virtnet_info *vi) 1279e9d7417bSJason Wang { 1280e9d7417bSJason Wang struct virtio_device *vdev = vi->vdev; 1281e9d7417bSJason Wang 1282986a4f4dSJason Wang virtnet_set_affinity(vi, false); 1283986a4f4dSJason Wang 1284e9d7417bSJason Wang vdev->config->del_vqs(vdev); 1285986a4f4dSJason Wang 1286986a4f4dSJason Wang virtnet_free_queues(vi); 1287986a4f4dSJason Wang } 1288986a4f4dSJason Wang 1289986a4f4dSJason Wang static int virtnet_find_vqs(struct virtnet_info *vi) 1290986a4f4dSJason Wang { 1291986a4f4dSJason Wang vq_callback_t **callbacks; 1292986a4f4dSJason Wang struct virtqueue **vqs; 1293986a4f4dSJason Wang int ret = -ENOMEM; 1294986a4f4dSJason Wang int i, total_vqs; 1295986a4f4dSJason Wang const char **names; 1296986a4f4dSJason Wang 1297986a4f4dSJason Wang /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by 1298986a4f4dSJason Wang * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by 1299986a4f4dSJason Wang * possible control vq. 1300986a4f4dSJason Wang */ 1301986a4f4dSJason Wang total_vqs = vi->max_queue_pairs * 2 + 1302986a4f4dSJason Wang virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ); 1303986a4f4dSJason Wang 1304986a4f4dSJason Wang /* Allocate space for find_vqs parameters */ 1305986a4f4dSJason Wang vqs = kzalloc(total_vqs * sizeof(*vqs), GFP_KERNEL); 1306986a4f4dSJason Wang if (!vqs) 1307986a4f4dSJason Wang goto err_vq; 1308986a4f4dSJason Wang callbacks = kmalloc(total_vqs * sizeof(*callbacks), GFP_KERNEL); 1309986a4f4dSJason Wang if (!callbacks) 1310986a4f4dSJason Wang goto err_callback; 1311986a4f4dSJason Wang names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL); 1312986a4f4dSJason Wang if (!names) 1313986a4f4dSJason Wang goto err_names; 1314986a4f4dSJason Wang 1315986a4f4dSJason Wang /* Parameters for control virtqueue, if any */ 1316986a4f4dSJason Wang if (vi->has_cvq) { 1317986a4f4dSJason Wang callbacks[total_vqs - 1] = NULL; 1318986a4f4dSJason Wang names[total_vqs - 1] = "control"; 1319986a4f4dSJason Wang } 1320986a4f4dSJason Wang 1321986a4f4dSJason Wang /* Allocate/initialize parameters for send/receive virtqueues */ 1322986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1323986a4f4dSJason Wang callbacks[rxq2vq(i)] = skb_recv_done; 1324986a4f4dSJason Wang callbacks[txq2vq(i)] = skb_xmit_done; 1325986a4f4dSJason Wang sprintf(vi->rq[i].name, "input.%d", i); 1326986a4f4dSJason Wang sprintf(vi->sq[i].name, "output.%d", i); 1327986a4f4dSJason Wang names[rxq2vq(i)] = vi->rq[i].name; 1328986a4f4dSJason Wang names[txq2vq(i)] = vi->sq[i].name; 1329986a4f4dSJason Wang } 1330986a4f4dSJason Wang 1331986a4f4dSJason Wang ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks, 1332986a4f4dSJason Wang names); 1333986a4f4dSJason Wang if (ret) 1334986a4f4dSJason Wang goto err_find; 1335986a4f4dSJason Wang 1336986a4f4dSJason Wang if (vi->has_cvq) { 1337986a4f4dSJason Wang vi->cvq = vqs[total_vqs - 1]; 1338986a4f4dSJason Wang if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN)) 1339986a4f4dSJason Wang vi->dev->features |= NETIF_F_HW_VLAN_FILTER; 1340986a4f4dSJason Wang } 1341986a4f4dSJason Wang 1342986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1343986a4f4dSJason Wang vi->rq[i].vq = vqs[rxq2vq(i)]; 1344986a4f4dSJason Wang vi->sq[i].vq = vqs[txq2vq(i)]; 1345986a4f4dSJason Wang } 1346986a4f4dSJason Wang 1347986a4f4dSJason Wang kfree(names); 1348986a4f4dSJason Wang kfree(callbacks); 1349986a4f4dSJason Wang kfree(vqs); 1350986a4f4dSJason Wang 1351986a4f4dSJason Wang return 0; 1352986a4f4dSJason Wang 1353986a4f4dSJason Wang err_find: 1354986a4f4dSJason Wang kfree(names); 1355986a4f4dSJason Wang err_names: 1356986a4f4dSJason Wang kfree(callbacks); 1357986a4f4dSJason Wang err_callback: 1358986a4f4dSJason Wang kfree(vqs); 1359986a4f4dSJason Wang err_vq: 1360986a4f4dSJason Wang return ret; 1361986a4f4dSJason Wang } 1362986a4f4dSJason Wang 1363986a4f4dSJason Wang static int virtnet_alloc_queues(struct virtnet_info *vi) 1364986a4f4dSJason Wang { 1365986a4f4dSJason Wang int i; 1366986a4f4dSJason Wang 1367986a4f4dSJason Wang vi->sq = kzalloc(sizeof(*vi->sq) * vi->max_queue_pairs, GFP_KERNEL); 1368986a4f4dSJason Wang if (!vi->sq) 1369986a4f4dSJason Wang goto err_sq; 1370986a4f4dSJason Wang vi->rq = kzalloc(sizeof(*vi->rq) * vi->max_queue_pairs, GFP_KERNEL); 1371008d4278SAmerigo Wang if (!vi->rq) 1372986a4f4dSJason Wang goto err_rq; 1373986a4f4dSJason Wang 1374986a4f4dSJason Wang INIT_DELAYED_WORK(&vi->refill, refill_work); 1375986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1376986a4f4dSJason Wang vi->rq[i].pages = NULL; 1377986a4f4dSJason Wang netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll, 1378986a4f4dSJason Wang napi_weight); 1379986a4f4dSJason Wang 1380986a4f4dSJason Wang sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg)); 1381986a4f4dSJason Wang sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg)); 1382986a4f4dSJason Wang } 1383986a4f4dSJason Wang 1384986a4f4dSJason Wang return 0; 1385986a4f4dSJason Wang 1386986a4f4dSJason Wang err_rq: 1387986a4f4dSJason Wang kfree(vi->sq); 1388986a4f4dSJason Wang err_sq: 1389986a4f4dSJason Wang return -ENOMEM; 1390e9d7417bSJason Wang } 1391e9d7417bSJason Wang 13923f9c10b0SAmit Shah static int init_vqs(struct virtnet_info *vi) 13933f9c10b0SAmit Shah { 1394986a4f4dSJason Wang int ret; 13953f9c10b0SAmit Shah 1396986a4f4dSJason Wang /* Allocate send & receive queues */ 1397986a4f4dSJason Wang ret = virtnet_alloc_queues(vi); 1398986a4f4dSJason Wang if (ret) 1399986a4f4dSJason Wang goto err; 14003f9c10b0SAmit Shah 1401986a4f4dSJason Wang ret = virtnet_find_vqs(vi); 1402986a4f4dSJason Wang if (ret) 1403986a4f4dSJason Wang goto err_free; 14043f9c10b0SAmit Shah 1405*47be2479SWanlong Gao get_online_cpus(); 1406986a4f4dSJason Wang virtnet_set_affinity(vi, true); 1407*47be2479SWanlong Gao put_online_cpus(); 1408*47be2479SWanlong Gao 14093f9c10b0SAmit Shah return 0; 1410986a4f4dSJason Wang 1411986a4f4dSJason Wang err_free: 1412986a4f4dSJason Wang virtnet_free_queues(vi); 1413986a4f4dSJason Wang err: 1414986a4f4dSJason Wang return ret; 14153f9c10b0SAmit Shah } 14163f9c10b0SAmit Shah 1417296f96fcSRusty Russell static int virtnet_probe(struct virtio_device *vdev) 1418296f96fcSRusty Russell { 1419986a4f4dSJason Wang int i, err; 1420296f96fcSRusty Russell struct net_device *dev; 1421296f96fcSRusty Russell struct virtnet_info *vi; 1422986a4f4dSJason Wang u16 max_queue_pairs; 1423986a4f4dSJason Wang 1424986a4f4dSJason Wang /* Find if host supports multiqueue virtio_net device */ 1425986a4f4dSJason Wang err = virtio_config_val(vdev, VIRTIO_NET_F_MQ, 1426986a4f4dSJason Wang offsetof(struct virtio_net_config, 1427986a4f4dSJason Wang max_virtqueue_pairs), &max_queue_pairs); 1428986a4f4dSJason Wang 1429986a4f4dSJason Wang /* We need at least 2 queue's */ 1430986a4f4dSJason Wang if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN || 1431986a4f4dSJason Wang max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX || 1432986a4f4dSJason Wang !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) 1433986a4f4dSJason Wang max_queue_pairs = 1; 1434296f96fcSRusty Russell 1435296f96fcSRusty Russell /* Allocate ourselves a network device with room for our info */ 1436986a4f4dSJason Wang dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs); 1437296f96fcSRusty Russell if (!dev) 1438296f96fcSRusty Russell return -ENOMEM; 1439296f96fcSRusty Russell 1440296f96fcSRusty Russell /* Set up network device as normal. */ 1441f2f2c8b4SJiri Pirko dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE; 144276288b4eSStephen Hemminger dev->netdev_ops = &virtnet_netdev; 1443296f96fcSRusty Russell dev->features = NETIF_F_HIGHDMA; 14443fa2a1dfSstephen hemminger 1445a9ea3fc6SHerbert Xu SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops); 1446296f96fcSRusty Russell SET_NETDEV_DEV(dev, &vdev->dev); 1447296f96fcSRusty Russell 1448296f96fcSRusty Russell /* Do we support "hardware" checksums? */ 144998e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) { 1450296f96fcSRusty Russell /* This opens up the world of extra features. */ 145198e778c9SMichał Mirosław dev->hw_features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST; 145298e778c9SMichał Mirosław if (csum) 1453296f96fcSRusty Russell dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST; 145498e778c9SMichał Mirosław 145598e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) { 145698e778c9SMichał Mirosław dev->hw_features |= NETIF_F_TSO | NETIF_F_UFO 145734a48579SRusty Russell | NETIF_F_TSO_ECN | NETIF_F_TSO6; 145834a48579SRusty Russell } 14595539ae96SRusty Russell /* Individual feature bits: what can host handle? */ 146098e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4)) 146198e778c9SMichał Mirosław dev->hw_features |= NETIF_F_TSO; 146298e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6)) 146398e778c9SMichał Mirosław dev->hw_features |= NETIF_F_TSO6; 146498e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN)) 146598e778c9SMichał Mirosław dev->hw_features |= NETIF_F_TSO_ECN; 146698e778c9SMichał Mirosław if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO)) 146798e778c9SMichał Mirosław dev->hw_features |= NETIF_F_UFO; 146898e778c9SMichał Mirosław 146998e778c9SMichał Mirosław if (gso) 147098e778c9SMichał Mirosław dev->features |= dev->hw_features & (NETIF_F_ALL_TSO|NETIF_F_UFO); 147198e778c9SMichał Mirosław /* (!csum && gso) case will be fixed by register_netdev() */ 1472296f96fcSRusty Russell } 1473296f96fcSRusty Russell 1474296f96fcSRusty Russell /* Configuration may specify what MAC to use. Otherwise random. */ 147577dd7693SSasha Levin if (virtio_config_val_len(vdev, VIRTIO_NET_F_MAC, 1476a586d4f6SRusty Russell offsetof(struct virtio_net_config, mac), 147777dd7693SSasha Levin dev->dev_addr, dev->addr_len) < 0) 1478f2cedb63SDanny Kukawka eth_hw_addr_random(dev); 1479296f96fcSRusty Russell 1480296f96fcSRusty Russell /* Set up our device-specific information */ 1481296f96fcSRusty Russell vi = netdev_priv(dev); 1482296f96fcSRusty Russell vi->dev = dev; 1483296f96fcSRusty Russell vi->vdev = vdev; 1484d9d5dcc8SChristian Borntraeger vdev->priv = vi; 14853fa2a1dfSstephen hemminger vi->stats = alloc_percpu(struct virtnet_stats); 14863fa2a1dfSstephen hemminger err = -ENOMEM; 14873fa2a1dfSstephen hemminger if (vi->stats == NULL) 14883fa2a1dfSstephen hemminger goto free; 14893fa2a1dfSstephen hemminger 1490*47be2479SWanlong Gao vi->vq_index = alloc_percpu(int); 1491*47be2479SWanlong Gao if (vi->vq_index == NULL) 1492*47be2479SWanlong Gao goto free_stats; 1493*47be2479SWanlong Gao 1494586d17c5SJason Wang mutex_init(&vi->config_lock); 1495586d17c5SJason Wang vi->config_enable = true; 1496586d17c5SJason Wang INIT_WORK(&vi->config_work, virtnet_config_changed_work); 1497296f96fcSRusty Russell 149897402b96SHerbert Xu /* If we can receive ANY GSO packets, we must allocate large ones. */ 14998e95a202SJoe Perches if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) || 15008e95a202SJoe Perches virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) || 15018e95a202SJoe Perches virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN)) 150297402b96SHerbert Xu vi->big_packets = true; 150397402b96SHerbert Xu 15043f2c31d9SMark McLoughlin if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF)) 15053f2c31d9SMark McLoughlin vi->mergeable_rx_bufs = true; 15063f2c31d9SMark McLoughlin 1507986a4f4dSJason Wang if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) 1508986a4f4dSJason Wang vi->has_cvq = true; 1509986a4f4dSJason Wang 1510986a4f4dSJason Wang /* Use single tx/rx queue pair as default */ 1511986a4f4dSJason Wang vi->curr_queue_pairs = 1; 1512986a4f4dSJason Wang vi->max_queue_pairs = max_queue_pairs; 1513986a4f4dSJason Wang 1514986a4f4dSJason Wang /* Allocate/initialize the rx/tx queues, and invoke find_vqs */ 15153f9c10b0SAmit Shah err = init_vqs(vi); 1516d2a7dddaSMichael S. Tsirkin if (err) 1517*47be2479SWanlong Gao goto free_index; 1518d2a7dddaSMichael S. Tsirkin 1519986a4f4dSJason Wang netif_set_real_num_tx_queues(dev, 1); 1520986a4f4dSJason Wang netif_set_real_num_rx_queues(dev, 1); 1521986a4f4dSJason Wang 1522296f96fcSRusty Russell err = register_netdev(dev); 1523296f96fcSRusty Russell if (err) { 1524296f96fcSRusty Russell pr_debug("virtio_net: registering device failed\n"); 1525d2a7dddaSMichael S. Tsirkin goto free_vqs; 1526296f96fcSRusty Russell } 1527b3369c1fSRusty Russell 1528b3369c1fSRusty Russell /* Last of all, set up some receive buffers. */ 1529986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1530986a4f4dSJason Wang try_fill_recv(&vi->rq[i], GFP_KERNEL); 1531b3369c1fSRusty Russell 1532b3369c1fSRusty Russell /* If we didn't even get one input buffer, we're useless. */ 1533986a4f4dSJason Wang if (vi->rq[i].num == 0) { 1534986a4f4dSJason Wang free_unused_bufs(vi); 1535b3369c1fSRusty Russell err = -ENOMEM; 1536986a4f4dSJason Wang goto free_recv_bufs; 1537986a4f4dSJason Wang } 1538b3369c1fSRusty Russell } 1539b3369c1fSRusty Russell 1540167c25e4SJason Wang /* Assume link up if device can't report link status, 1541167c25e4SJason Wang otherwise get link status from config. */ 1542167c25e4SJason Wang if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) { 1543167c25e4SJason Wang netif_carrier_off(dev); 15443b07e9caSTejun Heo schedule_work(&vi->config_work); 1545167c25e4SJason Wang } else { 1546167c25e4SJason Wang vi->status = VIRTIO_NET_S_LINK_UP; 15474783256eSPantelis Koukousoulas netif_carrier_on(dev); 1548167c25e4SJason Wang } 15499f4d26d0SMark McLoughlin 1550986a4f4dSJason Wang pr_debug("virtnet: registered device %s with %d RX and TX vq's\n", 1551986a4f4dSJason Wang dev->name, max_queue_pairs); 1552986a4f4dSJason Wang 1553296f96fcSRusty Russell return 0; 1554296f96fcSRusty Russell 1555986a4f4dSJason Wang free_recv_bufs: 1556986a4f4dSJason Wang free_receive_bufs(vi); 1557b3369c1fSRusty Russell unregister_netdev(dev); 1558d2a7dddaSMichael S. Tsirkin free_vqs: 1559986a4f4dSJason Wang cancel_delayed_work_sync(&vi->refill); 1560e9d7417bSJason Wang virtnet_del_vqs(vi); 1561*47be2479SWanlong Gao free_index: 1562*47be2479SWanlong Gao free_percpu(vi->vq_index); 15633fa2a1dfSstephen hemminger free_stats: 15643fa2a1dfSstephen hemminger free_percpu(vi->stats); 1565296f96fcSRusty Russell free: 1566296f96fcSRusty Russell free_netdev(dev); 1567296f96fcSRusty Russell return err; 1568296f96fcSRusty Russell } 1569296f96fcSRusty Russell 157004486ed0SAmit Shah static void remove_vq_common(struct virtnet_info *vi) 1571296f96fcSRusty Russell { 157204486ed0SAmit Shah vi->vdev->config->reset(vi->vdev); 1573830a8a97SShirley Ma 1574830a8a97SShirley Ma /* Free unused buffers in both send and recv, if any. */ 15759ab86bbcSShirley Ma free_unused_bufs(vi); 1576fb6813f4SRusty Russell 1577986a4f4dSJason Wang free_receive_bufs(vi); 1578d2a7dddaSMichael S. Tsirkin 1579986a4f4dSJason Wang virtnet_del_vqs(vi); 158004486ed0SAmit Shah } 158104486ed0SAmit Shah 15828cc085d6SBill Pemberton static void virtnet_remove(struct virtio_device *vdev) 158304486ed0SAmit Shah { 158404486ed0SAmit Shah struct virtnet_info *vi = vdev->priv; 158504486ed0SAmit Shah 1586586d17c5SJason Wang /* Prevent config work handler from accessing the device. */ 1587586d17c5SJason Wang mutex_lock(&vi->config_lock); 1588586d17c5SJason Wang vi->config_enable = false; 1589586d17c5SJason Wang mutex_unlock(&vi->config_lock); 1590586d17c5SJason Wang 159104486ed0SAmit Shah unregister_netdev(vi->dev); 159204486ed0SAmit Shah 159304486ed0SAmit Shah remove_vq_common(vi); 1594fb6813f4SRusty Russell 1595586d17c5SJason Wang flush_work(&vi->config_work); 1596586d17c5SJason Wang 1597*47be2479SWanlong Gao free_percpu(vi->vq_index); 15982e66f55bSKrishna Kumar free_percpu(vi->stats); 159974b2553fSRusty Russell free_netdev(vi->dev); 1600296f96fcSRusty Russell } 1601296f96fcSRusty Russell 16020741bcb5SAmit Shah #ifdef CONFIG_PM 16030741bcb5SAmit Shah static int virtnet_freeze(struct virtio_device *vdev) 16040741bcb5SAmit Shah { 16050741bcb5SAmit Shah struct virtnet_info *vi = vdev->priv; 1606986a4f4dSJason Wang int i; 16070741bcb5SAmit Shah 1608586d17c5SJason Wang /* Prevent config work handler from accessing the device */ 1609586d17c5SJason Wang mutex_lock(&vi->config_lock); 1610586d17c5SJason Wang vi->config_enable = false; 1611586d17c5SJason Wang mutex_unlock(&vi->config_lock); 1612586d17c5SJason Wang 16130741bcb5SAmit Shah netif_device_detach(vi->dev); 16140741bcb5SAmit Shah cancel_delayed_work_sync(&vi->refill); 16150741bcb5SAmit Shah 16160741bcb5SAmit Shah if (netif_running(vi->dev)) 1617986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) { 1618986a4f4dSJason Wang napi_disable(&vi->rq[i].napi); 1619986a4f4dSJason Wang netif_napi_del(&vi->rq[i].napi); 1620986a4f4dSJason Wang } 16210741bcb5SAmit Shah 16220741bcb5SAmit Shah remove_vq_common(vi); 16230741bcb5SAmit Shah 1624586d17c5SJason Wang flush_work(&vi->config_work); 1625586d17c5SJason Wang 16260741bcb5SAmit Shah return 0; 16270741bcb5SAmit Shah } 16280741bcb5SAmit Shah 16290741bcb5SAmit Shah static int virtnet_restore(struct virtio_device *vdev) 16300741bcb5SAmit Shah { 16310741bcb5SAmit Shah struct virtnet_info *vi = vdev->priv; 1632986a4f4dSJason Wang int err, i; 16330741bcb5SAmit Shah 16340741bcb5SAmit Shah err = init_vqs(vi); 16350741bcb5SAmit Shah if (err) 16360741bcb5SAmit Shah return err; 16370741bcb5SAmit Shah 16380741bcb5SAmit Shah if (netif_running(vi->dev)) 1639986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) 1640986a4f4dSJason Wang virtnet_napi_enable(&vi->rq[i]); 16410741bcb5SAmit Shah 16420741bcb5SAmit Shah netif_device_attach(vi->dev); 16430741bcb5SAmit Shah 1644986a4f4dSJason Wang for (i = 0; i < vi->max_queue_pairs; i++) 1645986a4f4dSJason Wang if (!try_fill_recv(&vi->rq[i], GFP_KERNEL)) 16463b07e9caSTejun Heo schedule_delayed_work(&vi->refill, 0); 16470741bcb5SAmit Shah 1648586d17c5SJason Wang mutex_lock(&vi->config_lock); 1649586d17c5SJason Wang vi->config_enable = true; 1650586d17c5SJason Wang mutex_unlock(&vi->config_lock); 1651586d17c5SJason Wang 1652986a4f4dSJason Wang virtnet_set_queues(vi, vi->curr_queue_pairs); 1653986a4f4dSJason Wang 16540741bcb5SAmit Shah return 0; 16550741bcb5SAmit Shah } 16560741bcb5SAmit Shah #endif 16570741bcb5SAmit Shah 1658296f96fcSRusty Russell static struct virtio_device_id id_table[] = { 1659296f96fcSRusty Russell { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID }, 1660296f96fcSRusty Russell { 0 }, 1661296f96fcSRusty Russell }; 1662296f96fcSRusty Russell 1663c45a6816SRusty Russell static unsigned int features[] = { 16645e4fe5c4SMark McLoughlin VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, 16655e4fe5c4SMark McLoughlin VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC, 1666c45a6816SRusty Russell VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, 166797402b96SHerbert Xu VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, 16685c516751SSridhar Samudrala VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, 16692a41f71dSAlex Williamson VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, 16700bde9569SAlex Williamson VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, 1671986a4f4dSJason Wang VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, 1672c45a6816SRusty Russell }; 1673c45a6816SRusty Russell 167422402529SUwe Kleine-König static struct virtio_driver virtio_net_driver = { 1675c45a6816SRusty Russell .feature_table = features, 1676c45a6816SRusty Russell .feature_table_size = ARRAY_SIZE(features), 1677296f96fcSRusty Russell .driver.name = KBUILD_MODNAME, 1678296f96fcSRusty Russell .driver.owner = THIS_MODULE, 1679296f96fcSRusty Russell .id_table = id_table, 1680296f96fcSRusty Russell .probe = virtnet_probe, 16818cc085d6SBill Pemberton .remove = virtnet_remove, 16829f4d26d0SMark McLoughlin .config_changed = virtnet_config_changed, 16830741bcb5SAmit Shah #ifdef CONFIG_PM 16840741bcb5SAmit Shah .freeze = virtnet_freeze, 16850741bcb5SAmit Shah .restore = virtnet_restore, 16860741bcb5SAmit Shah #endif 1687296f96fcSRusty Russell }; 1688296f96fcSRusty Russell 1689296f96fcSRusty Russell static int __init init(void) 1690296f96fcSRusty Russell { 169122402529SUwe Kleine-König return register_virtio_driver(&virtio_net_driver); 1692296f96fcSRusty Russell } 1693296f96fcSRusty Russell 1694296f96fcSRusty Russell static void __exit fini(void) 1695296f96fcSRusty Russell { 169622402529SUwe Kleine-König unregister_virtio_driver(&virtio_net_driver); 1697296f96fcSRusty Russell } 1698296f96fcSRusty Russell module_init(init); 1699296f96fcSRusty Russell module_exit(fini); 1700296f96fcSRusty Russell 1701296f96fcSRusty Russell MODULE_DEVICE_TABLE(virtio, id_table); 1702296f96fcSRusty Russell MODULE_DESCRIPTION("Virtio network driver"); 1703296f96fcSRusty Russell MODULE_LICENSE("GPL"); 1704