xref: /openbmc/linux/drivers/net/virtio_net.c (revision 3fa2a1df909482cc234524906e4bd30dee3514df)
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 
3334a48579SRusty Russell static int csum = 1, gso = 1;
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
422a41f71dSAlex Williamson 
43*3fa2a1dfSstephen hemminger struct virtnet_stats {
44*3fa2a1dfSstephen hemminger 	struct u64_stats_sync syncp;
45*3fa2a1dfSstephen hemminger 	u64 tx_bytes;
46*3fa2a1dfSstephen hemminger 	u64 tx_packets;
47*3fa2a1dfSstephen hemminger 
48*3fa2a1dfSstephen hemminger 	u64 rx_bytes;
49*3fa2a1dfSstephen hemminger 	u64 rx_packets;
50*3fa2a1dfSstephen hemminger };
51*3fa2a1dfSstephen hemminger 
525e01d2f9SMichael S. Tsirkin struct virtnet_info {
53296f96fcSRusty Russell 	struct virtio_device *vdev;
542a41f71dSAlex Williamson 	struct virtqueue *rvq, *svq, *cvq;
55296f96fcSRusty Russell 	struct net_device *dev;
56296f96fcSRusty Russell 	struct napi_struct napi;
579f4d26d0SMark McLoughlin 	unsigned int status;
58296f96fcSRusty Russell 
59296f96fcSRusty Russell 	/* Number of input buffers, and max we've ever had. */
60296f96fcSRusty Russell 	unsigned int num, max;
61296f96fcSRusty Russell 
6297402b96SHerbert Xu 	/* I like... big packets and I cannot lie! */
6397402b96SHerbert Xu 	bool big_packets;
6497402b96SHerbert Xu 
653f2c31d9SMark McLoughlin 	/* Host will merge rx buffers for big packets (shake it! shake it!) */
663f2c31d9SMark McLoughlin 	bool mergeable_rx_bufs;
673f2c31d9SMark McLoughlin 
68*3fa2a1dfSstephen hemminger 	/* Active statistics */
69*3fa2a1dfSstephen hemminger 	struct virtnet_stats __percpu *stats;
70*3fa2a1dfSstephen hemminger 
713161e453SRusty Russell 	/* Work struct for refilling if we run low on memory. */
723161e453SRusty Russell 	struct delayed_work refill;
733161e453SRusty Russell 
74fb6813f4SRusty Russell 	/* Chain pages by the private ptr. */
75fb6813f4SRusty Russell 	struct page *pages;
765e01d2f9SMichael S. Tsirkin 
775e01d2f9SMichael S. Tsirkin 	/* fragments + linear part + virtio header */
785e01d2f9SMichael S. Tsirkin 	struct scatterlist rx_sg[MAX_SKB_FRAGS + 2];
795e01d2f9SMichael S. Tsirkin 	struct scatterlist tx_sg[MAX_SKB_FRAGS + 2];
80296f96fcSRusty Russell };
81296f96fcSRusty Russell 
82b3f24698SRusty Russell struct skb_vnet_hdr {
83b3f24698SRusty Russell 	union {
84b3f24698SRusty Russell 		struct virtio_net_hdr hdr;
85b3f24698SRusty Russell 		struct virtio_net_hdr_mrg_rxbuf mhdr;
86b3f24698SRusty Russell 	};
8748925e37SRusty Russell 	unsigned int num_sg;
88b3f24698SRusty Russell };
89b3f24698SRusty Russell 
909ab86bbcSShirley Ma struct padded_vnet_hdr {
919ab86bbcSShirley Ma 	struct virtio_net_hdr hdr;
929ab86bbcSShirley Ma 	/*
939ab86bbcSShirley Ma 	 * virtio_net_hdr should be in a separated sg buffer because of a
949ab86bbcSShirley Ma 	 * QEMU bug, and data sg buffer shares same page with this header sg.
959ab86bbcSShirley Ma 	 * This padding makes next sg 16 byte aligned after virtio_net_hdr.
969ab86bbcSShirley Ma 	 */
979ab86bbcSShirley Ma 	char padding[6];
989ab86bbcSShirley Ma };
999ab86bbcSShirley Ma 
100b3f24698SRusty Russell static inline struct skb_vnet_hdr *skb_vnet_hdr(struct sk_buff *skb)
101296f96fcSRusty Russell {
102b3f24698SRusty Russell 	return (struct skb_vnet_hdr *)skb->cb;
103296f96fcSRusty Russell }
104296f96fcSRusty Russell 
1059ab86bbcSShirley Ma /*
1069ab86bbcSShirley Ma  * private is used to chain pages for big packets, put the whole
1079ab86bbcSShirley Ma  * most recent used list in the beginning for reuse
1089ab86bbcSShirley Ma  */
1099ab86bbcSShirley Ma static void give_pages(struct virtnet_info *vi, struct page *page)
110fb6813f4SRusty Russell {
1119ab86bbcSShirley Ma 	struct page *end;
1129ab86bbcSShirley Ma 
1139ab86bbcSShirley Ma 	/* Find end of list, sew whole thing into vi->pages. */
1149ab86bbcSShirley Ma 	for (end = page; end->private; end = (struct page *)end->private);
1159ab86bbcSShirley Ma 	end->private = (unsigned long)vi->pages;
116fb6813f4SRusty Russell 	vi->pages = page;
117fb6813f4SRusty Russell }
118fb6813f4SRusty Russell 
119fb6813f4SRusty Russell static struct page *get_a_page(struct virtnet_info *vi, gfp_t gfp_mask)
120fb6813f4SRusty Russell {
121fb6813f4SRusty Russell 	struct page *p = vi->pages;
122fb6813f4SRusty Russell 
1239ab86bbcSShirley Ma 	if (p) {
124fb6813f4SRusty Russell 		vi->pages = (struct page *)p->private;
1259ab86bbcSShirley Ma 		/* clear private here, it is used to chain pages */
1269ab86bbcSShirley Ma 		p->private = 0;
1279ab86bbcSShirley Ma 	} else
128fb6813f4SRusty Russell 		p = alloc_page(gfp_mask);
129fb6813f4SRusty Russell 	return p;
130fb6813f4SRusty Russell }
131fb6813f4SRusty Russell 
1322cb9c6baSRusty Russell static void skb_xmit_done(struct virtqueue *svq)
133296f96fcSRusty Russell {
1342cb9c6baSRusty Russell 	struct virtnet_info *vi = svq->vdev->priv;
135296f96fcSRusty Russell 
1362cb9c6baSRusty Russell 	/* Suppress further interrupts. */
1371915a712SMichael S. Tsirkin 	virtqueue_disable_cb(svq);
13811a3a154SRusty Russell 
139363f1514SRusty Russell 	/* We were probably waiting for more output buffers. */
140296f96fcSRusty Russell 	netif_wake_queue(vi->dev);
141296f96fcSRusty Russell }
142296f96fcSRusty Russell 
1439ab86bbcSShirley Ma static void set_skb_frag(struct sk_buff *skb, struct page *page,
1449ab86bbcSShirley Ma 			 unsigned int offset, unsigned int *len)
145296f96fcSRusty Russell {
1469ab86bbcSShirley Ma 	int i = skb_shinfo(skb)->nr_frags;
1479ab86bbcSShirley Ma 	skb_frag_t *f;
148296f96fcSRusty Russell 
1499ab86bbcSShirley Ma 	f = &skb_shinfo(skb)->frags[i];
1509ab86bbcSShirley Ma 	f->size = min((unsigned)PAGE_SIZE - offset, *len);
1519ab86bbcSShirley Ma 	f->page_offset = offset;
1529ab86bbcSShirley Ma 	f->page = page;
1539ab86bbcSShirley Ma 
1549ab86bbcSShirley Ma 	skb->data_len += f->size;
1559ab86bbcSShirley Ma 	skb->len += f->size;
1569ab86bbcSShirley Ma 	skb_shinfo(skb)->nr_frags++;
1579ab86bbcSShirley Ma 	*len -= f->size;
158296f96fcSRusty Russell }
1593f2c31d9SMark McLoughlin 
1609ab86bbcSShirley Ma static struct sk_buff *page_to_skb(struct virtnet_info *vi,
1619ab86bbcSShirley Ma 				   struct page *page, unsigned int len)
1629ab86bbcSShirley Ma {
1639ab86bbcSShirley Ma 	struct sk_buff *skb;
1649ab86bbcSShirley Ma 	struct skb_vnet_hdr *hdr;
1659ab86bbcSShirley Ma 	unsigned int copy, hdr_len, offset;
1669ab86bbcSShirley Ma 	char *p;
1679ab86bbcSShirley Ma 
1689ab86bbcSShirley Ma 	p = page_address(page);
1699ab86bbcSShirley Ma 
1709ab86bbcSShirley Ma 	/* copy small packet so we can reuse these pages for small data */
1719ab86bbcSShirley Ma 	skb = netdev_alloc_skb_ip_align(vi->dev, GOOD_COPY_LEN);
1729ab86bbcSShirley Ma 	if (unlikely(!skb))
1739ab86bbcSShirley Ma 		return NULL;
1749ab86bbcSShirley Ma 
1759ab86bbcSShirley Ma 	hdr = skb_vnet_hdr(skb);
1769ab86bbcSShirley Ma 
1773f2c31d9SMark McLoughlin 	if (vi->mergeable_rx_bufs) {
1789ab86bbcSShirley Ma 		hdr_len = sizeof hdr->mhdr;
1799ab86bbcSShirley Ma 		offset = hdr_len;
1809ab86bbcSShirley Ma 	} else {
1819ab86bbcSShirley Ma 		hdr_len = sizeof hdr->hdr;
1829ab86bbcSShirley Ma 		offset = sizeof(struct padded_vnet_hdr);
1839ab86bbcSShirley Ma 	}
1843f2c31d9SMark McLoughlin 
1859ab86bbcSShirley Ma 	memcpy(hdr, p, hdr_len);
1863f2c31d9SMark McLoughlin 
1879ab86bbcSShirley Ma 	len -= hdr_len;
1889ab86bbcSShirley Ma 	p += offset;
1893f2c31d9SMark McLoughlin 
1903f2c31d9SMark McLoughlin 	copy = len;
1913f2c31d9SMark McLoughlin 	if (copy > skb_tailroom(skb))
1923f2c31d9SMark McLoughlin 		copy = skb_tailroom(skb);
1933f2c31d9SMark McLoughlin 	memcpy(skb_put(skb, copy), p, copy);
1943f2c31d9SMark McLoughlin 
1953f2c31d9SMark McLoughlin 	len -= copy;
1969ab86bbcSShirley Ma 	offset += copy;
1973f2c31d9SMark McLoughlin 
1989ab86bbcSShirley Ma 	while (len) {
1999ab86bbcSShirley Ma 		set_skb_frag(skb, page, offset, &len);
2009ab86bbcSShirley Ma 		page = (struct page *)page->private;
2019ab86bbcSShirley Ma 		offset = 0;
2023f2c31d9SMark McLoughlin 	}
2033f2c31d9SMark McLoughlin 
2049ab86bbcSShirley Ma 	if (page)
2059ab86bbcSShirley Ma 		give_pages(vi, page);
2063f2c31d9SMark McLoughlin 
2079ab86bbcSShirley Ma 	return skb;
2089ab86bbcSShirley Ma }
2099ab86bbcSShirley Ma 
2109ab86bbcSShirley Ma static int receive_mergeable(struct virtnet_info *vi, struct sk_buff *skb)
2119ab86bbcSShirley Ma {
2129ab86bbcSShirley Ma 	struct skb_vnet_hdr *hdr = skb_vnet_hdr(skb);
2139ab86bbcSShirley Ma 	struct page *page;
2149ab86bbcSShirley Ma 	int num_buf, i, len;
2159ab86bbcSShirley Ma 
2169ab86bbcSShirley Ma 	num_buf = hdr->mhdr.num_buffers;
2179ab86bbcSShirley Ma 	while (--num_buf) {
2183f2c31d9SMark McLoughlin 		i = skb_shinfo(skb)->nr_frags;
2193f2c31d9SMark McLoughlin 		if (i >= MAX_SKB_FRAGS) {
2209ab86bbcSShirley Ma 			pr_debug("%s: packet too long\n", skb->dev->name);
2219ab86bbcSShirley Ma 			skb->dev->stats.rx_length_errors++;
2229ab86bbcSShirley Ma 			return -EINVAL;
2233f2c31d9SMark McLoughlin 		}
2241915a712SMichael S. Tsirkin 		page = virtqueue_get_buf(vi->rvq, &len);
2259ab86bbcSShirley Ma 		if (!page) {
2263f2c31d9SMark McLoughlin 			pr_debug("%s: rx error: %d buffers missing\n",
2279ab86bbcSShirley Ma 				 skb->dev->name, hdr->mhdr.num_buffers);
2289ab86bbcSShirley Ma 			skb->dev->stats.rx_length_errors++;
2299ab86bbcSShirley Ma 			return -EINVAL;
2303f2c31d9SMark McLoughlin 		}
231*3fa2a1dfSstephen hemminger 
2323f2c31d9SMark McLoughlin 		if (len > PAGE_SIZE)
2333f2c31d9SMark McLoughlin 			len = PAGE_SIZE;
2343f2c31d9SMark McLoughlin 
2359ab86bbcSShirley Ma 		set_skb_frag(skb, page, 0, &len);
2369ab86bbcSShirley Ma 
2379ab86bbcSShirley Ma 		--vi->num;
2383f2c31d9SMark McLoughlin 	}
2399ab86bbcSShirley Ma 	return 0;
2409ab86bbcSShirley Ma }
2419ab86bbcSShirley Ma 
2429ab86bbcSShirley Ma static void receive_buf(struct net_device *dev, void *buf, unsigned int len)
2439ab86bbcSShirley Ma {
2449ab86bbcSShirley Ma 	struct virtnet_info *vi = netdev_priv(dev);
245*3fa2a1dfSstephen hemminger 	struct virtnet_stats __percpu *stats = this_cpu_ptr(vi->stats);
2469ab86bbcSShirley Ma 	struct sk_buff *skb;
2479ab86bbcSShirley Ma 	struct page *page;
2489ab86bbcSShirley Ma 	struct skb_vnet_hdr *hdr;
2499ab86bbcSShirley Ma 
2509ab86bbcSShirley Ma 	if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) {
2519ab86bbcSShirley Ma 		pr_debug("%s: short packet %i\n", dev->name, len);
2529ab86bbcSShirley Ma 		dev->stats.rx_length_errors++;
2539ab86bbcSShirley Ma 		if (vi->mergeable_rx_bufs || vi->big_packets)
2549ab86bbcSShirley Ma 			give_pages(vi, buf);
2559ab86bbcSShirley Ma 		else
2569ab86bbcSShirley Ma 			dev_kfree_skb(buf);
2579ab86bbcSShirley Ma 		return;
2589ab86bbcSShirley Ma 	}
2599ab86bbcSShirley Ma 
2609ab86bbcSShirley Ma 	if (!vi->mergeable_rx_bufs && !vi->big_packets) {
2619ab86bbcSShirley Ma 		skb = buf;
2629ab86bbcSShirley Ma 		len -= sizeof(struct virtio_net_hdr);
2639ab86bbcSShirley Ma 		skb_trim(skb, len);
2643f2c31d9SMark McLoughlin 	} else {
2659ab86bbcSShirley Ma 		page = buf;
2669ab86bbcSShirley Ma 		skb = page_to_skb(vi, page, len);
2679ab86bbcSShirley Ma 		if (unlikely(!skb)) {
26897402b96SHerbert Xu 			dev->stats.rx_dropped++;
2699ab86bbcSShirley Ma 			give_pages(vi, page);
2709ab86bbcSShirley Ma 			return;
2719ab86bbcSShirley Ma 		}
2729ab86bbcSShirley Ma 		if (vi->mergeable_rx_bufs)
2739ab86bbcSShirley Ma 			if (receive_mergeable(vi, skb)) {
2749ab86bbcSShirley Ma 				dev_kfree_skb(skb);
2759ab86bbcSShirley Ma 				return;
27697402b96SHerbert Xu 			}
2773f2c31d9SMark McLoughlin 	}
2783f2c31d9SMark McLoughlin 
2799ab86bbcSShirley Ma 	hdr = skb_vnet_hdr(skb);
28097402b96SHerbert Xu 	skb->truesize += skb->data_len;
281*3fa2a1dfSstephen hemminger 
282*3fa2a1dfSstephen hemminger 	u64_stats_update_begin(&stats->syncp);
283*3fa2a1dfSstephen hemminger 	stats->rx_bytes += skb->len;
284*3fa2a1dfSstephen hemminger 	stats->rx_packets++;
285*3fa2a1dfSstephen hemminger 	u64_stats_update_end(&stats->syncp);
286296f96fcSRusty Russell 
287b3f24698SRusty Russell 	if (hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
288296f96fcSRusty Russell 		pr_debug("Needs csum!\n");
289b3f24698SRusty Russell 		if (!skb_partial_csum_set(skb,
290b3f24698SRusty Russell 					  hdr->hdr.csum_start,
291b3f24698SRusty Russell 					  hdr->hdr.csum_offset))
292296f96fcSRusty Russell 			goto frame_err;
29310a8d94aSJason Wang 	} else if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID) {
29410a8d94aSJason Wang 		skb->ip_summed = CHECKSUM_UNNECESSARY;
295296f96fcSRusty Russell 	}
296296f96fcSRusty Russell 
29723cde76dSMark McLoughlin 	skb->protocol = eth_type_trans(skb, dev);
29823cde76dSMark McLoughlin 	pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
29923cde76dSMark McLoughlin 		 ntohs(skb->protocol), skb->len, skb->pkt_type);
30023cde76dSMark McLoughlin 
301b3f24698SRusty Russell 	if (hdr->hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
302296f96fcSRusty Russell 		pr_debug("GSO!\n");
303b3f24698SRusty Russell 		switch (hdr->hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
304296f96fcSRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV4:
305296f96fcSRusty Russell 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
306296f96fcSRusty Russell 			break;
307296f96fcSRusty Russell 		case VIRTIO_NET_HDR_GSO_UDP:
308296f96fcSRusty Russell 			skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
309296f96fcSRusty Russell 			break;
310296f96fcSRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV6:
311296f96fcSRusty Russell 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
312296f96fcSRusty Russell 			break;
313296f96fcSRusty Russell 		default:
314296f96fcSRusty Russell 			if (net_ratelimit())
315296f96fcSRusty Russell 				printk(KERN_WARNING "%s: bad gso type %u.\n",
316b3f24698SRusty Russell 				       dev->name, hdr->hdr.gso_type);
317296f96fcSRusty Russell 			goto frame_err;
318296f96fcSRusty Russell 		}
319296f96fcSRusty Russell 
320b3f24698SRusty Russell 		if (hdr->hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN)
32134a48579SRusty Russell 			skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
32234a48579SRusty Russell 
323b3f24698SRusty Russell 		skb_shinfo(skb)->gso_size = hdr->hdr.gso_size;
324296f96fcSRusty Russell 		if (skb_shinfo(skb)->gso_size == 0) {
325296f96fcSRusty Russell 			if (net_ratelimit())
326296f96fcSRusty Russell 				printk(KERN_WARNING "%s: zero gso size.\n",
327296f96fcSRusty Russell 				       dev->name);
328296f96fcSRusty Russell 			goto frame_err;
329296f96fcSRusty Russell 		}
330296f96fcSRusty Russell 
331296f96fcSRusty Russell 		/* Header must be checked, and gso_segs computed. */
332296f96fcSRusty Russell 		skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
333296f96fcSRusty Russell 		skb_shinfo(skb)->gso_segs = 0;
334296f96fcSRusty Russell 	}
335296f96fcSRusty Russell 
336296f96fcSRusty Russell 	netif_receive_skb(skb);
337296f96fcSRusty Russell 	return;
338296f96fcSRusty Russell 
339296f96fcSRusty Russell frame_err:
340296f96fcSRusty Russell 	dev->stats.rx_frame_errors++;
341296f96fcSRusty Russell 	dev_kfree_skb(skb);
342296f96fcSRusty Russell }
343296f96fcSRusty Russell 
3449ab86bbcSShirley Ma static int add_recvbuf_small(struct virtnet_info *vi, gfp_t gfp)
345296f96fcSRusty Russell {
346296f96fcSRusty Russell 	struct sk_buff *skb;
347b3f24698SRusty Russell 	struct skb_vnet_hdr *hdr;
3489ab86bbcSShirley Ma 	int err;
3493f2c31d9SMark McLoughlin 
35089d71a66SEric Dumazet 	skb = netdev_alloc_skb_ip_align(vi->dev, MAX_PACKET_LEN);
3519ab86bbcSShirley Ma 	if (unlikely(!skb))
3529ab86bbcSShirley Ma 		return -ENOMEM;
353296f96fcSRusty Russell 
354296f96fcSRusty Russell 	skb_put(skb, MAX_PACKET_LEN);
3553f2c31d9SMark McLoughlin 
3563f2c31d9SMark McLoughlin 	hdr = skb_vnet_hdr(skb);
3575e01d2f9SMichael S. Tsirkin 	sg_set_buf(vi->rx_sg, &hdr->hdr, sizeof hdr->hdr);
35897402b96SHerbert Xu 
3595e01d2f9SMichael S. Tsirkin 	skb_to_sgvec(skb, vi->rx_sg + 1, 0, skb->len);
36097402b96SHerbert Xu 
361aa989f5eSMichael S. Tsirkin 	err = virtqueue_add_buf_gfp(vi->rvq, vi->rx_sg, 0, 2, skb, gfp);
3629ab86bbcSShirley Ma 	if (err < 0)
3639ab86bbcSShirley Ma 		dev_kfree_skb(skb);
36497402b96SHerbert Xu 
3659ab86bbcSShirley Ma 	return err;
36697402b96SHerbert Xu }
36797402b96SHerbert Xu 
3689ab86bbcSShirley Ma static int add_recvbuf_big(struct virtnet_info *vi, gfp_t gfp)
3699ab86bbcSShirley Ma {
3709ab86bbcSShirley Ma 	struct page *first, *list = NULL;
3719ab86bbcSShirley Ma 	char *p;
3729ab86bbcSShirley Ma 	int i, err, offset;
373296f96fcSRusty Russell 
3745e01d2f9SMichael S. Tsirkin 	/* page in vi->rx_sg[MAX_SKB_FRAGS + 1] is list tail */
3759ab86bbcSShirley Ma 	for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
3769ab86bbcSShirley Ma 		first = get_a_page(vi, gfp);
3779ab86bbcSShirley Ma 		if (!first) {
3789ab86bbcSShirley Ma 			if (list)
3799ab86bbcSShirley Ma 				give_pages(vi, list);
3809ab86bbcSShirley Ma 			return -ENOMEM;
381296f96fcSRusty Russell 		}
3825e01d2f9SMichael S. Tsirkin 		sg_set_buf(&vi->rx_sg[i], page_address(first), PAGE_SIZE);
3839ab86bbcSShirley Ma 
3849ab86bbcSShirley Ma 		/* chain new page in list head to match sg */
3859ab86bbcSShirley Ma 		first->private = (unsigned long)list;
3869ab86bbcSShirley Ma 		list = first;
3879ab86bbcSShirley Ma 	}
3889ab86bbcSShirley Ma 
3899ab86bbcSShirley Ma 	first = get_a_page(vi, gfp);
3909ab86bbcSShirley Ma 	if (!first) {
3919ab86bbcSShirley Ma 		give_pages(vi, list);
3929ab86bbcSShirley Ma 		return -ENOMEM;
3939ab86bbcSShirley Ma 	}
3949ab86bbcSShirley Ma 	p = page_address(first);
3959ab86bbcSShirley Ma 
3965e01d2f9SMichael S. Tsirkin 	/* vi->rx_sg[0], vi->rx_sg[1] share the same page */
3975e01d2f9SMichael S. Tsirkin 	/* a separated vi->rx_sg[0] for virtio_net_hdr only due to QEMU bug */
3985e01d2f9SMichael S. Tsirkin 	sg_set_buf(&vi->rx_sg[0], p, sizeof(struct virtio_net_hdr));
3999ab86bbcSShirley Ma 
4005e01d2f9SMichael S. Tsirkin 	/* vi->rx_sg[1] for data packet, from offset */
4019ab86bbcSShirley Ma 	offset = sizeof(struct padded_vnet_hdr);
4025e01d2f9SMichael S. Tsirkin 	sg_set_buf(&vi->rx_sg[1], p + offset, PAGE_SIZE - offset);
4039ab86bbcSShirley Ma 
4049ab86bbcSShirley Ma 	/* chain first in list head */
4059ab86bbcSShirley Ma 	first->private = (unsigned long)list;
406aa989f5eSMichael S. Tsirkin 	err = virtqueue_add_buf_gfp(vi->rvq, vi->rx_sg, 0, MAX_SKB_FRAGS + 2,
407aa989f5eSMichael S. Tsirkin 				    first, gfp);
4089ab86bbcSShirley Ma 	if (err < 0)
4099ab86bbcSShirley Ma 		give_pages(vi, first);
4109ab86bbcSShirley Ma 
4119ab86bbcSShirley Ma 	return err;
4129ab86bbcSShirley Ma }
4139ab86bbcSShirley Ma 
4149ab86bbcSShirley Ma static int add_recvbuf_mergeable(struct virtnet_info *vi, gfp_t gfp)
4159ab86bbcSShirley Ma {
4169ab86bbcSShirley Ma 	struct page *page;
4179ab86bbcSShirley Ma 	int err;
4189ab86bbcSShirley Ma 
4199ab86bbcSShirley Ma 	page = get_a_page(vi, gfp);
4209ab86bbcSShirley Ma 	if (!page)
4219ab86bbcSShirley Ma 		return -ENOMEM;
4229ab86bbcSShirley Ma 
4235e01d2f9SMichael S. Tsirkin 	sg_init_one(vi->rx_sg, page_address(page), PAGE_SIZE);
4249ab86bbcSShirley Ma 
425aa989f5eSMichael S. Tsirkin 	err = virtqueue_add_buf_gfp(vi->rvq, vi->rx_sg, 0, 1, page, gfp);
4269ab86bbcSShirley Ma 	if (err < 0)
4279ab86bbcSShirley Ma 		give_pages(vi, page);
4289ab86bbcSShirley Ma 
4299ab86bbcSShirley Ma 	return err;
430296f96fcSRusty Russell }
431296f96fcSRusty Russell 
4323161e453SRusty Russell /* Returns false if we couldn't fill entirely (OOM). */
4333161e453SRusty Russell static bool try_fill_recv(struct virtnet_info *vi, gfp_t gfp)
4343f2c31d9SMark McLoughlin {
4353f2c31d9SMark McLoughlin 	int err;
4361788f495SMichael S. Tsirkin 	bool oom;
4373f2c31d9SMark McLoughlin 
4380aea51c3SAmit Shah 	do {
4399ab86bbcSShirley Ma 		if (vi->mergeable_rx_bufs)
4409ab86bbcSShirley Ma 			err = add_recvbuf_mergeable(vi, gfp);
4419ab86bbcSShirley Ma 		else if (vi->big_packets)
4429ab86bbcSShirley Ma 			err = add_recvbuf_big(vi, gfp);
4439ab86bbcSShirley Ma 		else
4449ab86bbcSShirley Ma 			err = add_recvbuf_small(vi, gfp);
4453f2c31d9SMark McLoughlin 
4461788f495SMichael S. Tsirkin 		oom = err == -ENOMEM;
4471788f495SMichael S. Tsirkin 		if (err < 0)
4483f2c31d9SMark McLoughlin 			break;
4499ab86bbcSShirley Ma 		++vi->num;
4500aea51c3SAmit Shah 	} while (err > 0);
4513f2c31d9SMark McLoughlin 	if (unlikely(vi->num > vi->max))
4523f2c31d9SMark McLoughlin 		vi->max = vi->num;
4531915a712SMichael S. Tsirkin 	virtqueue_kick(vi->rvq);
4543161e453SRusty Russell 	return !oom;
4553f2c31d9SMark McLoughlin }
4563f2c31d9SMark McLoughlin 
45718445c4dSRusty Russell static void skb_recv_done(struct virtqueue *rvq)
458296f96fcSRusty Russell {
459296f96fcSRusty Russell 	struct virtnet_info *vi = rvq->vdev->priv;
46018445c4dSRusty Russell 	/* Schedule NAPI, Suppress further interrupts if successful. */
461288379f0SBen Hutchings 	if (napi_schedule_prep(&vi->napi)) {
4621915a712SMichael S. Tsirkin 		virtqueue_disable_cb(rvq);
463288379f0SBen Hutchings 		__napi_schedule(&vi->napi);
46418445c4dSRusty Russell 	}
465296f96fcSRusty Russell }
466296f96fcSRusty Russell 
4673e9d08ecSBruce Rogers static void virtnet_napi_enable(struct virtnet_info *vi)
4683e9d08ecSBruce Rogers {
4693e9d08ecSBruce Rogers 	napi_enable(&vi->napi);
4703e9d08ecSBruce Rogers 
4713e9d08ecSBruce Rogers 	/* If all buffers were filled by other side before we napi_enabled, we
4723e9d08ecSBruce Rogers 	 * won't get another interrupt, so process any outstanding packets
4733e9d08ecSBruce Rogers 	 * now.  virtnet_poll wants re-enable the queue, so we disable here.
4743e9d08ecSBruce Rogers 	 * We synchronize against interrupts via NAPI_STATE_SCHED */
4753e9d08ecSBruce Rogers 	if (napi_schedule_prep(&vi->napi)) {
4763e9d08ecSBruce Rogers 		virtqueue_disable_cb(vi->rvq);
4773e9d08ecSBruce Rogers 		__napi_schedule(&vi->napi);
4783e9d08ecSBruce Rogers 	}
4793e9d08ecSBruce Rogers }
4803e9d08ecSBruce Rogers 
4813161e453SRusty Russell static void refill_work(struct work_struct *work)
4823161e453SRusty Russell {
4833161e453SRusty Russell 	struct virtnet_info *vi;
4843161e453SRusty Russell 	bool still_empty;
4853161e453SRusty Russell 
4863161e453SRusty Russell 	vi = container_of(work, struct virtnet_info, refill.work);
4873161e453SRusty Russell 	napi_disable(&vi->napi);
48839d32157SHerbert Xu 	still_empty = !try_fill_recv(vi, GFP_KERNEL);
4893e9d08ecSBruce Rogers 	virtnet_napi_enable(vi);
4903161e453SRusty Russell 
4913161e453SRusty Russell 	/* In theory, this can happen: if we don't get any buffers in
4923161e453SRusty Russell 	 * we will *never* try to fill again. */
4933161e453SRusty Russell 	if (still_empty)
4943161e453SRusty Russell 		schedule_delayed_work(&vi->refill, HZ/2);
4953161e453SRusty Russell }
4963161e453SRusty Russell 
497296f96fcSRusty Russell static int virtnet_poll(struct napi_struct *napi, int budget)
498296f96fcSRusty Russell {
499296f96fcSRusty Russell 	struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi);
5009ab86bbcSShirley Ma 	void *buf;
501296f96fcSRusty Russell 	unsigned int len, received = 0;
502296f96fcSRusty Russell 
503296f96fcSRusty Russell again:
504296f96fcSRusty Russell 	while (received < budget &&
5051915a712SMichael S. Tsirkin 	       (buf = virtqueue_get_buf(vi->rvq, &len)) != NULL) {
5069ab86bbcSShirley Ma 		receive_buf(vi->dev, buf, len);
5079ab86bbcSShirley Ma 		--vi->num;
508296f96fcSRusty Russell 		received++;
509296f96fcSRusty Russell 	}
510296f96fcSRusty Russell 
5113161e453SRusty Russell 	if (vi->num < vi->max / 2) {
5123161e453SRusty Russell 		if (!try_fill_recv(vi, GFP_ATOMIC))
5133161e453SRusty Russell 			schedule_delayed_work(&vi->refill, 0);
5143161e453SRusty Russell 	}
515296f96fcSRusty Russell 
5168329d98eSRusty Russell 	/* Out of packets? */
5178329d98eSRusty Russell 	if (received < budget) {
518288379f0SBen Hutchings 		napi_complete(napi);
5191915a712SMichael S. Tsirkin 		if (unlikely(!virtqueue_enable_cb(vi->rvq)) &&
5208e95a202SJoe Perches 		    napi_schedule_prep(napi)) {
5211915a712SMichael S. Tsirkin 			virtqueue_disable_cb(vi->rvq);
522288379f0SBen Hutchings 			__napi_schedule(napi);
523296f96fcSRusty Russell 			goto again;
524296f96fcSRusty Russell 		}
5254265f161SChristian Borntraeger 	}
526296f96fcSRusty Russell 
527296f96fcSRusty Russell 	return received;
528296f96fcSRusty Russell }
529296f96fcSRusty Russell 
53048925e37SRusty Russell static unsigned int free_old_xmit_skbs(struct virtnet_info *vi)
531296f96fcSRusty Russell {
532296f96fcSRusty Russell 	struct sk_buff *skb;
53348925e37SRusty Russell 	unsigned int len, tot_sgs = 0;
534*3fa2a1dfSstephen hemminger 	struct virtnet_stats __percpu *stats = this_cpu_ptr(vi->stats);
535296f96fcSRusty Russell 
5361915a712SMichael S. Tsirkin 	while ((skb = virtqueue_get_buf(vi->svq, &len)) != NULL) {
537296f96fcSRusty Russell 		pr_debug("Sent skb %p\n", skb);
538*3fa2a1dfSstephen hemminger 
539*3fa2a1dfSstephen hemminger 		u64_stats_update_begin(&stats->syncp);
540*3fa2a1dfSstephen hemminger 		stats->tx_bytes += skb->len;
541*3fa2a1dfSstephen hemminger 		stats->tx_packets++;
542*3fa2a1dfSstephen hemminger 		u64_stats_update_end(&stats->syncp);
543*3fa2a1dfSstephen hemminger 
54448925e37SRusty Russell 		tot_sgs += skb_vnet_hdr(skb)->num_sg;
545ed79bab8SEric Dumazet 		dev_kfree_skb_any(skb);
546296f96fcSRusty Russell 	}
54748925e37SRusty Russell 	return tot_sgs;
548296f96fcSRusty Russell }
549296f96fcSRusty Russell 
55099ffc696SRusty Russell static int xmit_skb(struct virtnet_info *vi, struct sk_buff *skb)
551296f96fcSRusty Russell {
552b3f24698SRusty Russell 	struct skb_vnet_hdr *hdr = skb_vnet_hdr(skb);
553296f96fcSRusty Russell 	const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
554296f96fcSRusty Russell 
555e174961cSJohannes Berg 	pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
556296f96fcSRusty Russell 
557296f96fcSRusty Russell 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
558b3f24698SRusty Russell 		hdr->hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
55955508d60SMichał Mirosław 		hdr->hdr.csum_start = skb_checksum_start_offset(skb);
560b3f24698SRusty Russell 		hdr->hdr.csum_offset = skb->csum_offset;
561296f96fcSRusty Russell 	} else {
562b3f24698SRusty Russell 		hdr->hdr.flags = 0;
563b3f24698SRusty Russell 		hdr->hdr.csum_offset = hdr->hdr.csum_start = 0;
564296f96fcSRusty Russell 	}
565296f96fcSRusty Russell 
566296f96fcSRusty Russell 	if (skb_is_gso(skb)) {
567b3f24698SRusty Russell 		hdr->hdr.hdr_len = skb_headlen(skb);
568b3f24698SRusty Russell 		hdr->hdr.gso_size = skb_shinfo(skb)->gso_size;
56934a48579SRusty Russell 		if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
570b3f24698SRusty Russell 			hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
571296f96fcSRusty Russell 		else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
572b3f24698SRusty Russell 			hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
573296f96fcSRusty Russell 		else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
574b3f24698SRusty Russell 			hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP;
575296f96fcSRusty Russell 		else
576296f96fcSRusty Russell 			BUG();
57734a48579SRusty Russell 		if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
578b3f24698SRusty Russell 			hdr->hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
579296f96fcSRusty Russell 	} else {
580b3f24698SRusty Russell 		hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
581b3f24698SRusty Russell 		hdr->hdr.gso_size = hdr->hdr.hdr_len = 0;
582296f96fcSRusty Russell 	}
583296f96fcSRusty Russell 
584b3f24698SRusty Russell 	hdr->mhdr.num_buffers = 0;
5853f2c31d9SMark McLoughlin 
5863f2c31d9SMark McLoughlin 	/* Encode metadata header at front. */
5873f2c31d9SMark McLoughlin 	if (vi->mergeable_rx_bufs)
5885e01d2f9SMichael S. Tsirkin 		sg_set_buf(vi->tx_sg, &hdr->mhdr, sizeof hdr->mhdr);
5893f2c31d9SMark McLoughlin 	else
5905e01d2f9SMichael S. Tsirkin 		sg_set_buf(vi->tx_sg, &hdr->hdr, sizeof hdr->hdr);
5913f2c31d9SMark McLoughlin 
5925e01d2f9SMichael S. Tsirkin 	hdr->num_sg = skb_to_sgvec(skb, vi->tx_sg + 1, 0, skb->len) + 1;
5931756ac3dSLinus Torvalds 	return virtqueue_add_buf(vi->svq, vi->tx_sg, hdr->num_sg,
5945e01d2f9SMichael S. Tsirkin 					0, skb);
59511a3a154SRusty Russell }
59611a3a154SRusty Russell 
597424efe9cSStephen Hemminger static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
59899ffc696SRusty Russell {
59999ffc696SRusty Russell 	struct virtnet_info *vi = netdev_priv(dev);
60048925e37SRusty Russell 	int capacity;
6012cb9c6baSRusty Russell 
6022cb9c6baSRusty Russell 	/* Free up any pending old buffers before queueing new ones. */
6032cb9c6baSRusty Russell 	free_old_xmit_skbs(vi);
60499ffc696SRusty Russell 
60503f191baSMichael S. Tsirkin 	/* Try to transmit */
60648925e37SRusty Russell 	capacity = xmit_skb(vi, skb);
60799ffc696SRusty Russell 
60848925e37SRusty Russell 	/* This can happen with OOM and indirect buffers. */
60948925e37SRusty Russell 	if (unlikely(capacity < 0)) {
61058eba97dSRusty Russell 		if (net_ratelimit()) {
61158eba97dSRusty Russell 			if (likely(capacity == -ENOMEM)) {
61258eba97dSRusty Russell 				dev_warn(&dev->dev,
61358eba97dSRusty Russell 					 "TX queue failure: out of memory\n");
61458eba97dSRusty Russell 			} else {
61558eba97dSRusty Russell 				dev->stats.tx_fifo_errors++;
61658eba97dSRusty Russell 				dev_warn(&dev->dev,
61758eba97dSRusty Russell 					 "Unexpected TX queue failure: %d\n",
61858eba97dSRusty Russell 					 capacity);
6192cb9c6baSRusty Russell 			}
62058eba97dSRusty Russell 		}
62158eba97dSRusty Russell 		dev->stats.tx_dropped++;
62258eba97dSRusty Russell 		kfree_skb(skb);
62358eba97dSRusty Russell 		return NETDEV_TX_OK;
624296f96fcSRusty Russell 	}
6251915a712SMichael S. Tsirkin 	virtqueue_kick(vi->svq);
62603f191baSMichael S. Tsirkin 
62748925e37SRusty Russell 	/* Don't wait up for transmitted skbs to be freed. */
62848925e37SRusty Russell 	skb_orphan(skb);
62948925e37SRusty Russell 	nf_reset(skb);
63048925e37SRusty Russell 
63148925e37SRusty Russell 	/* Apparently nice girls don't return TX_BUSY; stop the queue
63248925e37SRusty Russell 	 * before it gets out of hand.  Naturally, this wastes entries. */
63348925e37SRusty Russell 	if (capacity < 2+MAX_SKB_FRAGS) {
63448925e37SRusty Russell 		netif_stop_queue(dev);
6357a66f784SMichael S. Tsirkin 		if (unlikely(!virtqueue_enable_cb_delayed(vi->svq))) {
63648925e37SRusty Russell 			/* More just got used, free them then recheck. */
63748925e37SRusty Russell 			capacity += free_old_xmit_skbs(vi);
63848925e37SRusty Russell 			if (capacity >= 2+MAX_SKB_FRAGS) {
63948925e37SRusty Russell 				netif_start_queue(dev);
6401915a712SMichael S. Tsirkin 				virtqueue_disable_cb(vi->svq);
64148925e37SRusty Russell 			}
64248925e37SRusty Russell 		}
64348925e37SRusty Russell 	}
64448925e37SRusty Russell 
64548925e37SRusty Russell 	return NETDEV_TX_OK;
64648925e37SRusty Russell }
64748925e37SRusty Russell 
6489c46f6d4SAlex Williamson static int virtnet_set_mac_address(struct net_device *dev, void *p)
6499c46f6d4SAlex Williamson {
6509c46f6d4SAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
6519c46f6d4SAlex Williamson 	struct virtio_device *vdev = vi->vdev;
6529c46f6d4SAlex Williamson 	int ret;
6539c46f6d4SAlex Williamson 
6549c46f6d4SAlex Williamson 	ret = eth_mac_addr(dev, p);
6559c46f6d4SAlex Williamson 	if (ret)
6569c46f6d4SAlex Williamson 		return ret;
6579c46f6d4SAlex Williamson 
65862994b2dSAlex Williamson 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
6599c46f6d4SAlex Williamson 		vdev->config->set(vdev, offsetof(struct virtio_net_config, mac),
6609c46f6d4SAlex Williamson 		                  dev->dev_addr, dev->addr_len);
6619c46f6d4SAlex Williamson 
6629c46f6d4SAlex Williamson 	return 0;
6639c46f6d4SAlex Williamson }
6649c46f6d4SAlex Williamson 
665*3fa2a1dfSstephen hemminger static struct rtnl_link_stats64 *virtnet_stats(struct net_device *dev,
666*3fa2a1dfSstephen hemminger 					       struct rtnl_link_stats64 *tot)
667*3fa2a1dfSstephen hemminger {
668*3fa2a1dfSstephen hemminger 	struct virtnet_info *vi = netdev_priv(dev);
669*3fa2a1dfSstephen hemminger 	int cpu;
670*3fa2a1dfSstephen hemminger 	unsigned int start;
671*3fa2a1dfSstephen hemminger 
672*3fa2a1dfSstephen hemminger 	for_each_possible_cpu(cpu) {
673*3fa2a1dfSstephen hemminger 		struct virtnet_stats __percpu *stats
674*3fa2a1dfSstephen hemminger 			= per_cpu_ptr(vi->stats, cpu);
675*3fa2a1dfSstephen hemminger 		u64 tpackets, tbytes, rpackets, rbytes;
676*3fa2a1dfSstephen hemminger 
677*3fa2a1dfSstephen hemminger 		do {
678*3fa2a1dfSstephen hemminger 			start = u64_stats_fetch_begin(&stats->syncp);
679*3fa2a1dfSstephen hemminger 			tpackets = stats->tx_packets;
680*3fa2a1dfSstephen hemminger 			tbytes   = stats->tx_bytes;
681*3fa2a1dfSstephen hemminger 			rpackets = stats->rx_packets;
682*3fa2a1dfSstephen hemminger 			rbytes   = stats->rx_bytes;
683*3fa2a1dfSstephen hemminger 		} while (u64_stats_fetch_retry(&stats->syncp, start));
684*3fa2a1dfSstephen hemminger 
685*3fa2a1dfSstephen hemminger 		tot->rx_packets += rpackets;
686*3fa2a1dfSstephen hemminger 		tot->tx_packets += tpackets;
687*3fa2a1dfSstephen hemminger 		tot->rx_bytes   += rbytes;
688*3fa2a1dfSstephen hemminger 		tot->tx_bytes   += tbytes;
689*3fa2a1dfSstephen hemminger 	}
690*3fa2a1dfSstephen hemminger 
691*3fa2a1dfSstephen hemminger 	tot->tx_dropped = dev->stats.tx_dropped;
692*3fa2a1dfSstephen hemminger 	tot->rx_dropped = dev->stats.rx_dropped;
693*3fa2a1dfSstephen hemminger 	tot->rx_length_errors = dev->stats.rx_length_errors;
694*3fa2a1dfSstephen hemminger 	tot->rx_frame_errors = dev->stats.rx_frame_errors;
695*3fa2a1dfSstephen hemminger 
696*3fa2a1dfSstephen hemminger 	return tot;
697*3fa2a1dfSstephen hemminger }
698*3fa2a1dfSstephen hemminger 
699da74e89dSAmit Shah #ifdef CONFIG_NET_POLL_CONTROLLER
700da74e89dSAmit Shah static void virtnet_netpoll(struct net_device *dev)
701da74e89dSAmit Shah {
702da74e89dSAmit Shah 	struct virtnet_info *vi = netdev_priv(dev);
703da74e89dSAmit Shah 
704da74e89dSAmit Shah 	napi_schedule(&vi->napi);
705da74e89dSAmit Shah }
706da74e89dSAmit Shah #endif
707da74e89dSAmit Shah 
708*3fa2a1dfSstephen hemminger static void virtnet_free(struct net_device *dev)
709*3fa2a1dfSstephen hemminger {
710*3fa2a1dfSstephen hemminger 	struct virtnet_info *vi = netdev_priv(dev);
711*3fa2a1dfSstephen hemminger 
712*3fa2a1dfSstephen hemminger 	free_percpu(vi->stats);
713*3fa2a1dfSstephen hemminger 	free_netdev(dev);
714*3fa2a1dfSstephen hemminger }
715*3fa2a1dfSstephen hemminger 
716296f96fcSRusty Russell static int virtnet_open(struct net_device *dev)
717296f96fcSRusty Russell {
718296f96fcSRusty Russell 	struct virtnet_info *vi = netdev_priv(dev);
719296f96fcSRusty Russell 
7203e9d08ecSBruce Rogers 	virtnet_napi_enable(vi);
721296f96fcSRusty Russell 	return 0;
722296f96fcSRusty Russell }
723296f96fcSRusty Russell 
7242a41f71dSAlex Williamson /*
7252a41f71dSAlex Williamson  * Send command via the control virtqueue and check status.  Commands
7262a41f71dSAlex Williamson  * supported by the hypervisor, as indicated by feature bits, should
7272a41f71dSAlex Williamson  * never fail unless improperly formated.
7282a41f71dSAlex Williamson  */
7292a41f71dSAlex Williamson static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
7302a41f71dSAlex Williamson 				 struct scatterlist *data, int out, int in)
7312a41f71dSAlex Williamson {
73223e258e1SAlex Williamson 	struct scatterlist *s, sg[VIRTNET_SEND_COMMAND_SG_MAX + 2];
7332a41f71dSAlex Williamson 	struct virtio_net_ctrl_hdr ctrl;
7342a41f71dSAlex Williamson 	virtio_net_ctrl_ack status = ~0;
7352a41f71dSAlex Williamson 	unsigned int tmp;
73623e258e1SAlex Williamson 	int i;
7372a41f71dSAlex Williamson 
7380ee904c3SAlexander Beregalov 	/* Caller should know better */
7390ee904c3SAlexander Beregalov 	BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) ||
7400ee904c3SAlexander Beregalov 		(out + in > VIRTNET_SEND_COMMAND_SG_MAX));
7412a41f71dSAlex Williamson 
7422a41f71dSAlex Williamson 	out++; /* Add header */
7432a41f71dSAlex Williamson 	in++; /* Add return status */
7442a41f71dSAlex Williamson 
7452a41f71dSAlex Williamson 	ctrl.class = class;
7462a41f71dSAlex Williamson 	ctrl.cmd = cmd;
7472a41f71dSAlex Williamson 
7482a41f71dSAlex Williamson 	sg_init_table(sg, out + in);
7492a41f71dSAlex Williamson 
7502a41f71dSAlex Williamson 	sg_set_buf(&sg[0], &ctrl, sizeof(ctrl));
75123e258e1SAlex Williamson 	for_each_sg(data, s, out + in - 2, i)
75223e258e1SAlex Williamson 		sg_set_buf(&sg[i + 1], sg_virt(s), s->length);
7532a41f71dSAlex Williamson 	sg_set_buf(&sg[out + in - 1], &status, sizeof(status));
7542a41f71dSAlex Williamson 
7551915a712SMichael S. Tsirkin 	BUG_ON(virtqueue_add_buf(vi->cvq, sg, out, in, vi) < 0);
7562a41f71dSAlex Williamson 
7571915a712SMichael S. Tsirkin 	virtqueue_kick(vi->cvq);
7582a41f71dSAlex Williamson 
7592a41f71dSAlex Williamson 	/*
7602a41f71dSAlex Williamson 	 * Spin for a response, the kick causes an ioport write, trapping
7612a41f71dSAlex Williamson 	 * into the hypervisor, so the request should be handled immediately.
7622a41f71dSAlex Williamson 	 */
7631915a712SMichael S. Tsirkin 	while (!virtqueue_get_buf(vi->cvq, &tmp))
7642a41f71dSAlex Williamson 		cpu_relax();
7652a41f71dSAlex Williamson 
7662a41f71dSAlex Williamson 	return status == VIRTIO_NET_OK;
7672a41f71dSAlex Williamson }
7682a41f71dSAlex Williamson 
769296f96fcSRusty Russell static int virtnet_close(struct net_device *dev)
770296f96fcSRusty Russell {
771296f96fcSRusty Russell 	struct virtnet_info *vi = netdev_priv(dev);
772296f96fcSRusty Russell 
773296f96fcSRusty Russell 	napi_disable(&vi->napi);
774296f96fcSRusty Russell 
775296f96fcSRusty Russell 	return 0;
776296f96fcSRusty Russell }
777296f96fcSRusty Russell 
7782af7698eSAlex Williamson static void virtnet_set_rx_mode(struct net_device *dev)
7792af7698eSAlex Williamson {
7802af7698eSAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
781f565a7c2SAlex Williamson 	struct scatterlist sg[2];
7822af7698eSAlex Williamson 	u8 promisc, allmulti;
783f565a7c2SAlex Williamson 	struct virtio_net_ctrl_mac *mac_data;
784ccffad25SJiri Pirko 	struct netdev_hw_addr *ha;
78532e7bfc4SJiri Pirko 	int uc_count;
7864cd24eafSJiri Pirko 	int mc_count;
787f565a7c2SAlex Williamson 	void *buf;
788f565a7c2SAlex Williamson 	int i;
7892af7698eSAlex Williamson 
7902af7698eSAlex Williamson 	/* We can't dynamicaly set ndo_set_rx_mode, so return gracefully */
7912af7698eSAlex Williamson 	if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
7922af7698eSAlex Williamson 		return;
7932af7698eSAlex Williamson 
794f565a7c2SAlex Williamson 	promisc = ((dev->flags & IFF_PROMISC) != 0);
795f565a7c2SAlex Williamson 	allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
7962af7698eSAlex Williamson 
79723e258e1SAlex Williamson 	sg_init_one(sg, &promisc, sizeof(promisc));
7982af7698eSAlex Williamson 
7992af7698eSAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
8002af7698eSAlex Williamson 				  VIRTIO_NET_CTRL_RX_PROMISC,
801f565a7c2SAlex Williamson 				  sg, 1, 0))
8022af7698eSAlex Williamson 		dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
8032af7698eSAlex Williamson 			 promisc ? "en" : "dis");
8042af7698eSAlex Williamson 
80523e258e1SAlex Williamson 	sg_init_one(sg, &allmulti, sizeof(allmulti));
8062af7698eSAlex Williamson 
8072af7698eSAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
8082af7698eSAlex Williamson 				  VIRTIO_NET_CTRL_RX_ALLMULTI,
809f565a7c2SAlex Williamson 				  sg, 1, 0))
8102af7698eSAlex Williamson 		dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
8112af7698eSAlex Williamson 			 allmulti ? "en" : "dis");
812f565a7c2SAlex Williamson 
81332e7bfc4SJiri Pirko 	uc_count = netdev_uc_count(dev);
8144cd24eafSJiri Pirko 	mc_count = netdev_mc_count(dev);
815f565a7c2SAlex Williamson 	/* MAC filter - use one buffer for both lists */
8164cd24eafSJiri Pirko 	buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
817f565a7c2SAlex Williamson 		      (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
8184cd24eafSJiri Pirko 	mac_data = buf;
819f565a7c2SAlex Williamson 	if (!buf) {
820f565a7c2SAlex Williamson 		dev_warn(&dev->dev, "No memory for MAC address buffer\n");
821f565a7c2SAlex Williamson 		return;
822f565a7c2SAlex Williamson 	}
823f565a7c2SAlex Williamson 
82423e258e1SAlex Williamson 	sg_init_table(sg, 2);
82523e258e1SAlex Williamson 
826f565a7c2SAlex Williamson 	/* Store the unicast list and count in the front of the buffer */
82732e7bfc4SJiri Pirko 	mac_data->entries = uc_count;
828ccffad25SJiri Pirko 	i = 0;
82932e7bfc4SJiri Pirko 	netdev_for_each_uc_addr(ha, dev)
830ccffad25SJiri Pirko 		memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
831f565a7c2SAlex Williamson 
832f565a7c2SAlex Williamson 	sg_set_buf(&sg[0], mac_data,
83332e7bfc4SJiri Pirko 		   sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
834f565a7c2SAlex Williamson 
835f565a7c2SAlex Williamson 	/* multicast list and count fill the end */
83632e7bfc4SJiri Pirko 	mac_data = (void *)&mac_data->macs[uc_count][0];
837f565a7c2SAlex Williamson 
8384cd24eafSJiri Pirko 	mac_data->entries = mc_count;
839567ec874SJiri Pirko 	i = 0;
84022bedad3SJiri Pirko 	netdev_for_each_mc_addr(ha, dev)
84122bedad3SJiri Pirko 		memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
842f565a7c2SAlex Williamson 
843f565a7c2SAlex Williamson 	sg_set_buf(&sg[1], mac_data,
8444cd24eafSJiri Pirko 		   sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
845f565a7c2SAlex Williamson 
846f565a7c2SAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
847f565a7c2SAlex Williamson 				  VIRTIO_NET_CTRL_MAC_TABLE_SET,
848f565a7c2SAlex Williamson 				  sg, 2, 0))
849f565a7c2SAlex Williamson 		dev_warn(&dev->dev, "Failed to set MAC fitler table.\n");
850f565a7c2SAlex Williamson 
851f565a7c2SAlex Williamson 	kfree(buf);
8522af7698eSAlex Williamson }
8532af7698eSAlex Williamson 
8541824a989SAlex Williamson static void virtnet_vlan_rx_add_vid(struct net_device *dev, u16 vid)
8550bde9569SAlex Williamson {
8560bde9569SAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
8570bde9569SAlex Williamson 	struct scatterlist sg;
8580bde9569SAlex Williamson 
85923e258e1SAlex Williamson 	sg_init_one(&sg, &vid, sizeof(vid));
8600bde9569SAlex Williamson 
8610bde9569SAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
8620bde9569SAlex Williamson 				  VIRTIO_NET_CTRL_VLAN_ADD, &sg, 1, 0))
8630bde9569SAlex Williamson 		dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
8640bde9569SAlex Williamson }
8650bde9569SAlex Williamson 
8661824a989SAlex Williamson static void virtnet_vlan_rx_kill_vid(struct net_device *dev, u16 vid)
8670bde9569SAlex Williamson {
8680bde9569SAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
8690bde9569SAlex Williamson 	struct scatterlist sg;
8700bde9569SAlex Williamson 
87123e258e1SAlex Williamson 	sg_init_one(&sg, &vid, sizeof(vid));
8720bde9569SAlex Williamson 
8730bde9569SAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
8740bde9569SAlex Williamson 				  VIRTIO_NET_CTRL_VLAN_DEL, &sg, 1, 0))
8750bde9569SAlex Williamson 		dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
8760bde9569SAlex Williamson }
8770bde9569SAlex Williamson 
8780fc0b732SStephen Hemminger static const struct ethtool_ops virtnet_ethtool_ops = {
8799f4d26d0SMark McLoughlin 	.get_link = ethtool_op_get_link,
880a9ea3fc6SHerbert Xu };
881a9ea3fc6SHerbert Xu 
88239da5814SMark McLoughlin #define MIN_MTU 68
88339da5814SMark McLoughlin #define MAX_MTU 65535
88439da5814SMark McLoughlin 
88539da5814SMark McLoughlin static int virtnet_change_mtu(struct net_device *dev, int new_mtu)
88639da5814SMark McLoughlin {
88739da5814SMark McLoughlin 	if (new_mtu < MIN_MTU || new_mtu > MAX_MTU)
88839da5814SMark McLoughlin 		return -EINVAL;
88939da5814SMark McLoughlin 	dev->mtu = new_mtu;
89039da5814SMark McLoughlin 	return 0;
89139da5814SMark McLoughlin }
89239da5814SMark McLoughlin 
89376288b4eSStephen Hemminger static const struct net_device_ops virtnet_netdev = {
89476288b4eSStephen Hemminger 	.ndo_open            = virtnet_open,
89576288b4eSStephen Hemminger 	.ndo_stop   	     = virtnet_close,
89676288b4eSStephen Hemminger 	.ndo_start_xmit      = start_xmit,
89776288b4eSStephen Hemminger 	.ndo_validate_addr   = eth_validate_addr,
8989c46f6d4SAlex Williamson 	.ndo_set_mac_address = virtnet_set_mac_address,
8992af7698eSAlex Williamson 	.ndo_set_rx_mode     = virtnet_set_rx_mode,
90076288b4eSStephen Hemminger 	.ndo_change_mtu	     = virtnet_change_mtu,
901*3fa2a1dfSstephen hemminger 	.ndo_get_stats64     = virtnet_stats,
9021824a989SAlex Williamson 	.ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
9031824a989SAlex Williamson 	.ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
90476288b4eSStephen Hemminger #ifdef CONFIG_NET_POLL_CONTROLLER
90576288b4eSStephen Hemminger 	.ndo_poll_controller = virtnet_netpoll,
90676288b4eSStephen Hemminger #endif
90776288b4eSStephen Hemminger };
90876288b4eSStephen Hemminger 
9099f4d26d0SMark McLoughlin static void virtnet_update_status(struct virtnet_info *vi)
9109f4d26d0SMark McLoughlin {
9119f4d26d0SMark McLoughlin 	u16 v;
9129f4d26d0SMark McLoughlin 
9139f4d26d0SMark McLoughlin 	if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS))
9149f4d26d0SMark McLoughlin 		return;
9159f4d26d0SMark McLoughlin 
9169f4d26d0SMark McLoughlin 	vi->vdev->config->get(vi->vdev,
9179f4d26d0SMark McLoughlin 			      offsetof(struct virtio_net_config, status),
9189f4d26d0SMark McLoughlin 			      &v, sizeof(v));
9199f4d26d0SMark McLoughlin 
9209f4d26d0SMark McLoughlin 	/* Ignore unknown (future) status bits */
9219f4d26d0SMark McLoughlin 	v &= VIRTIO_NET_S_LINK_UP;
9229f4d26d0SMark McLoughlin 
9239f4d26d0SMark McLoughlin 	if (vi->status == v)
9249f4d26d0SMark McLoughlin 		return;
9259f4d26d0SMark McLoughlin 
9269f4d26d0SMark McLoughlin 	vi->status = v;
9279f4d26d0SMark McLoughlin 
9289f4d26d0SMark McLoughlin 	if (vi->status & VIRTIO_NET_S_LINK_UP) {
9299f4d26d0SMark McLoughlin 		netif_carrier_on(vi->dev);
9309f4d26d0SMark McLoughlin 		netif_wake_queue(vi->dev);
9319f4d26d0SMark McLoughlin 	} else {
9329f4d26d0SMark McLoughlin 		netif_carrier_off(vi->dev);
9339f4d26d0SMark McLoughlin 		netif_stop_queue(vi->dev);
9349f4d26d0SMark McLoughlin 	}
9359f4d26d0SMark McLoughlin }
9369f4d26d0SMark McLoughlin 
9379f4d26d0SMark McLoughlin static void virtnet_config_changed(struct virtio_device *vdev)
9389f4d26d0SMark McLoughlin {
9399f4d26d0SMark McLoughlin 	struct virtnet_info *vi = vdev->priv;
9409f4d26d0SMark McLoughlin 
9419f4d26d0SMark McLoughlin 	virtnet_update_status(vi);
9429f4d26d0SMark McLoughlin }
9439f4d26d0SMark McLoughlin 
944296f96fcSRusty Russell static int virtnet_probe(struct virtio_device *vdev)
945296f96fcSRusty Russell {
946296f96fcSRusty Russell 	int err;
947296f96fcSRusty Russell 	struct net_device *dev;
948296f96fcSRusty Russell 	struct virtnet_info *vi;
949d2a7dddaSMichael S. Tsirkin 	struct virtqueue *vqs[3];
950d2a7dddaSMichael S. Tsirkin 	vq_callback_t *callbacks[] = { skb_recv_done, skb_xmit_done, NULL};
951d2a7dddaSMichael S. Tsirkin 	const char *names[] = { "input", "output", "control" };
952d2a7dddaSMichael S. Tsirkin 	int nvqs;
953296f96fcSRusty Russell 
954296f96fcSRusty Russell 	/* Allocate ourselves a network device with room for our info */
955296f96fcSRusty Russell 	dev = alloc_etherdev(sizeof(struct virtnet_info));
956296f96fcSRusty Russell 	if (!dev)
957296f96fcSRusty Russell 		return -ENOMEM;
958296f96fcSRusty Russell 
959296f96fcSRusty Russell 	/* Set up network device as normal. */
96076288b4eSStephen Hemminger 	dev->netdev_ops = &virtnet_netdev;
961296f96fcSRusty Russell 	dev->features = NETIF_F_HIGHDMA;
962*3fa2a1dfSstephen hemminger 	dev->destructor = virtnet_free;
963*3fa2a1dfSstephen hemminger 
964a9ea3fc6SHerbert Xu 	SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops);
965296f96fcSRusty Russell 	SET_NETDEV_DEV(dev, &vdev->dev);
966296f96fcSRusty Russell 
967296f96fcSRusty Russell 	/* Do we support "hardware" checksums? */
96898e778c9SMichał Mirosław 	if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
969296f96fcSRusty Russell 		/* This opens up the world of extra features. */
97098e778c9SMichał Mirosław 		dev->hw_features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
97198e778c9SMichał Mirosław 		if (csum)
972296f96fcSRusty Russell 			dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
97398e778c9SMichał Mirosław 
97498e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
97598e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_TSO | NETIF_F_UFO
97634a48579SRusty Russell 				| NETIF_F_TSO_ECN | NETIF_F_TSO6;
97734a48579SRusty Russell 		}
9785539ae96SRusty Russell 		/* Individual feature bits: what can host handle? */
97998e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
98098e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_TSO;
98198e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
98298e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_TSO6;
98398e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
98498e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_TSO_ECN;
98598e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
98698e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_UFO;
98798e778c9SMichał Mirosław 
98898e778c9SMichał Mirosław 		if (gso)
98998e778c9SMichał Mirosław 			dev->features |= dev->hw_features & (NETIF_F_ALL_TSO|NETIF_F_UFO);
99098e778c9SMichał Mirosław 		/* (!csum && gso) case will be fixed by register_netdev() */
991296f96fcSRusty Russell 	}
992296f96fcSRusty Russell 
993296f96fcSRusty Russell 	/* Configuration may specify what MAC to use.  Otherwise random. */
994c45a6816SRusty Russell 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
995a586d4f6SRusty Russell 		vdev->config->get(vdev,
996a586d4f6SRusty Russell 				  offsetof(struct virtio_net_config, mac),
997a586d4f6SRusty Russell 				  dev->dev_addr, dev->addr_len);
99862994b2dSAlex Williamson 	} else
999296f96fcSRusty Russell 		random_ether_addr(dev->dev_addr);
1000296f96fcSRusty Russell 
1001296f96fcSRusty Russell 	/* Set up our device-specific information */
1002296f96fcSRusty Russell 	vi = netdev_priv(dev);
10036c0cd7c0SDor Laor 	netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight);
1004296f96fcSRusty Russell 	vi->dev = dev;
1005296f96fcSRusty Russell 	vi->vdev = vdev;
1006d9d5dcc8SChristian Borntraeger 	vdev->priv = vi;
1007fb6813f4SRusty Russell 	vi->pages = NULL;
1008*3fa2a1dfSstephen hemminger 	vi->stats = alloc_percpu(struct virtnet_stats);
1009*3fa2a1dfSstephen hemminger 	err = -ENOMEM;
1010*3fa2a1dfSstephen hemminger 	if (vi->stats == NULL)
1011*3fa2a1dfSstephen hemminger 		goto free;
1012*3fa2a1dfSstephen hemminger 
10133161e453SRusty Russell 	INIT_DELAYED_WORK(&vi->refill, refill_work);
10145e01d2f9SMichael S. Tsirkin 	sg_init_table(vi->rx_sg, ARRAY_SIZE(vi->rx_sg));
10155e01d2f9SMichael S. Tsirkin 	sg_init_table(vi->tx_sg, ARRAY_SIZE(vi->tx_sg));
1016296f96fcSRusty Russell 
101797402b96SHerbert Xu 	/* If we can receive ANY GSO packets, we must allocate large ones. */
10188e95a202SJoe Perches 	if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
10198e95a202SJoe Perches 	    virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
10208e95a202SJoe Perches 	    virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN))
102197402b96SHerbert Xu 		vi->big_packets = true;
102297402b96SHerbert Xu 
10233f2c31d9SMark McLoughlin 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
10243f2c31d9SMark McLoughlin 		vi->mergeable_rx_bufs = true;
10253f2c31d9SMark McLoughlin 
1026d2a7dddaSMichael S. Tsirkin 	/* We expect two virtqueues, receive then send,
1027d2a7dddaSMichael S. Tsirkin 	 * and optionally control. */
1028d2a7dddaSMichael S. Tsirkin 	nvqs = virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) ? 3 : 2;
1029296f96fcSRusty Russell 
1030d2a7dddaSMichael S. Tsirkin 	err = vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names);
1031d2a7dddaSMichael S. Tsirkin 	if (err)
1032*3fa2a1dfSstephen hemminger 		goto free_stats;
1033d2a7dddaSMichael S. Tsirkin 
1034d2a7dddaSMichael S. Tsirkin 	vi->rvq = vqs[0];
1035d2a7dddaSMichael S. Tsirkin 	vi->svq = vqs[1];
1036296f96fcSRusty Russell 
10372a41f71dSAlex Williamson 	if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)) {
1038d2a7dddaSMichael S. Tsirkin 		vi->cvq = vqs[2];
10390bde9569SAlex Williamson 
10400bde9569SAlex Williamson 		if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
10410bde9569SAlex Williamson 			dev->features |= NETIF_F_HW_VLAN_FILTER;
10422a41f71dSAlex Williamson 	}
10432a41f71dSAlex Williamson 
1044296f96fcSRusty Russell 	err = register_netdev(dev);
1045296f96fcSRusty Russell 	if (err) {
1046296f96fcSRusty Russell 		pr_debug("virtio_net: registering device failed\n");
1047d2a7dddaSMichael S. Tsirkin 		goto free_vqs;
1048296f96fcSRusty Russell 	}
1049b3369c1fSRusty Russell 
1050b3369c1fSRusty Russell 	/* Last of all, set up some receive buffers. */
10513161e453SRusty Russell 	try_fill_recv(vi, GFP_KERNEL);
1052b3369c1fSRusty Russell 
1053b3369c1fSRusty Russell 	/* If we didn't even get one input buffer, we're useless. */
1054b3369c1fSRusty Russell 	if (vi->num == 0) {
1055b3369c1fSRusty Russell 		err = -ENOMEM;
1056b3369c1fSRusty Russell 		goto unregister;
1057b3369c1fSRusty Russell 	}
1058b3369c1fSRusty Russell 
1059167c25e4SJason Wang 	/* Assume link up if device can't report link status,
1060167c25e4SJason Wang 	   otherwise get link status from config. */
1061167c25e4SJason Wang 	if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
1062167c25e4SJason Wang 		netif_carrier_off(dev);
10639f4d26d0SMark McLoughlin 		virtnet_update_status(vi);
1064167c25e4SJason Wang 	} else {
1065167c25e4SJason Wang 		vi->status = VIRTIO_NET_S_LINK_UP;
10664783256eSPantelis Koukousoulas 		netif_carrier_on(dev);
1067167c25e4SJason Wang 	}
10689f4d26d0SMark McLoughlin 
1069296f96fcSRusty Russell 	pr_debug("virtnet: registered device %s\n", dev->name);
1070296f96fcSRusty Russell 	return 0;
1071296f96fcSRusty Russell 
1072b3369c1fSRusty Russell unregister:
1073b3369c1fSRusty Russell 	unregister_netdev(dev);
10743161e453SRusty Russell 	cancel_delayed_work_sync(&vi->refill);
1075d2a7dddaSMichael S. Tsirkin free_vqs:
1076d2a7dddaSMichael S. Tsirkin 	vdev->config->del_vqs(vdev);
1077*3fa2a1dfSstephen hemminger free_stats:
1078*3fa2a1dfSstephen hemminger 	free_percpu(vi->stats);
1079296f96fcSRusty Russell free:
1080296f96fcSRusty Russell 	free_netdev(dev);
1081296f96fcSRusty Russell 	return err;
1082296f96fcSRusty Russell }
1083296f96fcSRusty Russell 
10849ab86bbcSShirley Ma static void free_unused_bufs(struct virtnet_info *vi)
10859ab86bbcSShirley Ma {
10869ab86bbcSShirley Ma 	void *buf;
10879ab86bbcSShirley Ma 	while (1) {
10881915a712SMichael S. Tsirkin 		buf = virtqueue_detach_unused_buf(vi->svq);
1089830a8a97SShirley Ma 		if (!buf)
1090830a8a97SShirley Ma 			break;
1091830a8a97SShirley Ma 		dev_kfree_skb(buf);
1092830a8a97SShirley Ma 	}
1093830a8a97SShirley Ma 	while (1) {
10941915a712SMichael S. Tsirkin 		buf = virtqueue_detach_unused_buf(vi->rvq);
10959ab86bbcSShirley Ma 		if (!buf)
10969ab86bbcSShirley Ma 			break;
10979ab86bbcSShirley Ma 		if (vi->mergeable_rx_bufs || vi->big_packets)
10989ab86bbcSShirley Ma 			give_pages(vi, buf);
10999ab86bbcSShirley Ma 		else
11009ab86bbcSShirley Ma 			dev_kfree_skb(buf);
11019ab86bbcSShirley Ma 		--vi->num;
11029ab86bbcSShirley Ma 	}
11039ab86bbcSShirley Ma 	BUG_ON(vi->num != 0);
11049ab86bbcSShirley Ma }
11059ab86bbcSShirley Ma 
11063d1285beSUwe Kleine-König static void __devexit virtnet_remove(struct virtio_device *vdev)
1107296f96fcSRusty Russell {
110874b2553fSRusty Russell 	struct virtnet_info *vi = vdev->priv;
1109b3369c1fSRusty Russell 
11106e5aa7efSRusty Russell 	/* Stop all the virtqueues. */
11116e5aa7efSRusty Russell 	vdev->config->reset(vdev);
11126e5aa7efSRusty Russell 
1113b3369c1fSRusty Russell 
111474b2553fSRusty Russell 	unregister_netdev(vi->dev);
11153161e453SRusty Russell 	cancel_delayed_work_sync(&vi->refill);
1116830a8a97SShirley Ma 
1117830a8a97SShirley Ma 	/* Free unused buffers in both send and recv, if any. */
11189ab86bbcSShirley Ma 	free_unused_bufs(vi);
1119fb6813f4SRusty Russell 
1120d2a7dddaSMichael S. Tsirkin 	vdev->config->del_vqs(vi->vdev);
1121d2a7dddaSMichael S. Tsirkin 
1122fb6813f4SRusty Russell 	while (vi->pages)
1123fb6813f4SRusty Russell 		__free_pages(get_a_page(vi, GFP_KERNEL), 0);
1124fb6813f4SRusty Russell 
112574b2553fSRusty Russell 	free_netdev(vi->dev);
1126296f96fcSRusty Russell }
1127296f96fcSRusty Russell 
1128296f96fcSRusty Russell static struct virtio_device_id id_table[] = {
1129296f96fcSRusty Russell 	{ VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
1130296f96fcSRusty Russell 	{ 0 },
1131296f96fcSRusty Russell };
1132296f96fcSRusty Russell 
1133c45a6816SRusty Russell static unsigned int features[] = {
11345e4fe5c4SMark McLoughlin 	VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM,
11355e4fe5c4SMark McLoughlin 	VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
1136c45a6816SRusty Russell 	VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
113797402b96SHerbert Xu 	VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
11385c516751SSridhar Samudrala 	VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO,
11392a41f71dSAlex Williamson 	VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ,
11400bde9569SAlex Williamson 	VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN,
1141c45a6816SRusty Russell };
1142c45a6816SRusty Russell 
114322402529SUwe Kleine-König static struct virtio_driver virtio_net_driver = {
1144c45a6816SRusty Russell 	.feature_table = features,
1145c45a6816SRusty Russell 	.feature_table_size = ARRAY_SIZE(features),
1146296f96fcSRusty Russell 	.driver.name =	KBUILD_MODNAME,
1147296f96fcSRusty Russell 	.driver.owner =	THIS_MODULE,
1148296f96fcSRusty Russell 	.id_table =	id_table,
1149296f96fcSRusty Russell 	.probe =	virtnet_probe,
1150296f96fcSRusty Russell 	.remove =	__devexit_p(virtnet_remove),
11519f4d26d0SMark McLoughlin 	.config_changed = virtnet_config_changed,
1152296f96fcSRusty Russell };
1153296f96fcSRusty Russell 
1154296f96fcSRusty Russell static int __init init(void)
1155296f96fcSRusty Russell {
115622402529SUwe Kleine-König 	return register_virtio_driver(&virtio_net_driver);
1157296f96fcSRusty Russell }
1158296f96fcSRusty Russell 
1159296f96fcSRusty Russell static void __exit fini(void)
1160296f96fcSRusty Russell {
116122402529SUwe Kleine-König 	unregister_virtio_driver(&virtio_net_driver);
1162296f96fcSRusty Russell }
1163296f96fcSRusty Russell module_init(init);
1164296f96fcSRusty Russell module_exit(fini);
1165296f96fcSRusty Russell 
1166296f96fcSRusty Russell MODULE_DEVICE_TABLE(virtio, id_table);
1167296f96fcSRusty Russell MODULE_DESCRIPTION("Virtio network driver");
1168296f96fcSRusty Russell MODULE_LICENSE("GPL");
1169