xref: /openbmc/linux/drivers/net/virtio_net.c (revision 10a8d94a95742bb15b4e617ee9884bb4381362be)
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 
435e01d2f9SMichael S. Tsirkin struct virtnet_info {
44296f96fcSRusty Russell 	struct virtio_device *vdev;
452a41f71dSAlex Williamson 	struct virtqueue *rvq, *svq, *cvq;
46296f96fcSRusty Russell 	struct net_device *dev;
47296f96fcSRusty Russell 	struct napi_struct napi;
489f4d26d0SMark McLoughlin 	unsigned int status;
49296f96fcSRusty Russell 
50296f96fcSRusty Russell 	/* Number of input buffers, and max we've ever had. */
51296f96fcSRusty Russell 	unsigned int num, max;
52296f96fcSRusty Russell 
5397402b96SHerbert Xu 	/* I like... big packets and I cannot lie! */
5497402b96SHerbert Xu 	bool big_packets;
5597402b96SHerbert Xu 
563f2c31d9SMark McLoughlin 	/* Host will merge rx buffers for big packets (shake it! shake it!) */
573f2c31d9SMark McLoughlin 	bool mergeable_rx_bufs;
583f2c31d9SMark McLoughlin 
593161e453SRusty Russell 	/* Work struct for refilling if we run low on memory. */
603161e453SRusty Russell 	struct delayed_work refill;
613161e453SRusty Russell 
62fb6813f4SRusty Russell 	/* Chain pages by the private ptr. */
63fb6813f4SRusty Russell 	struct page *pages;
645e01d2f9SMichael S. Tsirkin 
655e01d2f9SMichael S. Tsirkin 	/* fragments + linear part + virtio header */
665e01d2f9SMichael S. Tsirkin 	struct scatterlist rx_sg[MAX_SKB_FRAGS + 2];
675e01d2f9SMichael S. Tsirkin 	struct scatterlist tx_sg[MAX_SKB_FRAGS + 2];
68296f96fcSRusty Russell };
69296f96fcSRusty Russell 
70b3f24698SRusty Russell struct skb_vnet_hdr {
71b3f24698SRusty Russell 	union {
72b3f24698SRusty Russell 		struct virtio_net_hdr hdr;
73b3f24698SRusty Russell 		struct virtio_net_hdr_mrg_rxbuf mhdr;
74b3f24698SRusty Russell 	};
7548925e37SRusty Russell 	unsigned int num_sg;
76b3f24698SRusty Russell };
77b3f24698SRusty Russell 
789ab86bbcSShirley Ma struct padded_vnet_hdr {
799ab86bbcSShirley Ma 	struct virtio_net_hdr hdr;
809ab86bbcSShirley Ma 	/*
819ab86bbcSShirley Ma 	 * virtio_net_hdr should be in a separated sg buffer because of a
829ab86bbcSShirley Ma 	 * QEMU bug, and data sg buffer shares same page with this header sg.
839ab86bbcSShirley Ma 	 * This padding makes next sg 16 byte aligned after virtio_net_hdr.
849ab86bbcSShirley Ma 	 */
859ab86bbcSShirley Ma 	char padding[6];
869ab86bbcSShirley Ma };
879ab86bbcSShirley Ma 
88b3f24698SRusty Russell static inline struct skb_vnet_hdr *skb_vnet_hdr(struct sk_buff *skb)
89296f96fcSRusty Russell {
90b3f24698SRusty Russell 	return (struct skb_vnet_hdr *)skb->cb;
91296f96fcSRusty Russell }
92296f96fcSRusty Russell 
939ab86bbcSShirley Ma /*
949ab86bbcSShirley Ma  * private is used to chain pages for big packets, put the whole
959ab86bbcSShirley Ma  * most recent used list in the beginning for reuse
969ab86bbcSShirley Ma  */
979ab86bbcSShirley Ma static void give_pages(struct virtnet_info *vi, struct page *page)
98fb6813f4SRusty Russell {
999ab86bbcSShirley Ma 	struct page *end;
1009ab86bbcSShirley Ma 
1019ab86bbcSShirley Ma 	/* Find end of list, sew whole thing into vi->pages. */
1029ab86bbcSShirley Ma 	for (end = page; end->private; end = (struct page *)end->private);
1039ab86bbcSShirley Ma 	end->private = (unsigned long)vi->pages;
104fb6813f4SRusty Russell 	vi->pages = page;
105fb6813f4SRusty Russell }
106fb6813f4SRusty Russell 
107fb6813f4SRusty Russell static struct page *get_a_page(struct virtnet_info *vi, gfp_t gfp_mask)
108fb6813f4SRusty Russell {
109fb6813f4SRusty Russell 	struct page *p = vi->pages;
110fb6813f4SRusty Russell 
1119ab86bbcSShirley Ma 	if (p) {
112fb6813f4SRusty Russell 		vi->pages = (struct page *)p->private;
1139ab86bbcSShirley Ma 		/* clear private here, it is used to chain pages */
1149ab86bbcSShirley Ma 		p->private = 0;
1159ab86bbcSShirley Ma 	} else
116fb6813f4SRusty Russell 		p = alloc_page(gfp_mask);
117fb6813f4SRusty Russell 	return p;
118fb6813f4SRusty Russell }
119fb6813f4SRusty Russell 
1202cb9c6baSRusty Russell static void skb_xmit_done(struct virtqueue *svq)
121296f96fcSRusty Russell {
1222cb9c6baSRusty Russell 	struct virtnet_info *vi = svq->vdev->priv;
123296f96fcSRusty Russell 
1242cb9c6baSRusty Russell 	/* Suppress further interrupts. */
1251915a712SMichael S. Tsirkin 	virtqueue_disable_cb(svq);
12611a3a154SRusty Russell 
127363f1514SRusty Russell 	/* We were probably waiting for more output buffers. */
128296f96fcSRusty Russell 	netif_wake_queue(vi->dev);
129296f96fcSRusty Russell }
130296f96fcSRusty Russell 
1319ab86bbcSShirley Ma static void set_skb_frag(struct sk_buff *skb, struct page *page,
1329ab86bbcSShirley Ma 			 unsigned int offset, unsigned int *len)
133296f96fcSRusty Russell {
1349ab86bbcSShirley Ma 	int i = skb_shinfo(skb)->nr_frags;
1359ab86bbcSShirley Ma 	skb_frag_t *f;
136296f96fcSRusty Russell 
1379ab86bbcSShirley Ma 	f = &skb_shinfo(skb)->frags[i];
1389ab86bbcSShirley Ma 	f->size = min((unsigned)PAGE_SIZE - offset, *len);
1399ab86bbcSShirley Ma 	f->page_offset = offset;
1409ab86bbcSShirley Ma 	f->page = page;
1419ab86bbcSShirley Ma 
1429ab86bbcSShirley Ma 	skb->data_len += f->size;
1439ab86bbcSShirley Ma 	skb->len += f->size;
1449ab86bbcSShirley Ma 	skb_shinfo(skb)->nr_frags++;
1459ab86bbcSShirley Ma 	*len -= f->size;
146296f96fcSRusty Russell }
1473f2c31d9SMark McLoughlin 
1489ab86bbcSShirley Ma static struct sk_buff *page_to_skb(struct virtnet_info *vi,
1499ab86bbcSShirley Ma 				   struct page *page, unsigned int len)
1509ab86bbcSShirley Ma {
1519ab86bbcSShirley Ma 	struct sk_buff *skb;
1529ab86bbcSShirley Ma 	struct skb_vnet_hdr *hdr;
1539ab86bbcSShirley Ma 	unsigned int copy, hdr_len, offset;
1549ab86bbcSShirley Ma 	char *p;
1559ab86bbcSShirley Ma 
1569ab86bbcSShirley Ma 	p = page_address(page);
1579ab86bbcSShirley Ma 
1589ab86bbcSShirley Ma 	/* copy small packet so we can reuse these pages for small data */
1599ab86bbcSShirley Ma 	skb = netdev_alloc_skb_ip_align(vi->dev, GOOD_COPY_LEN);
1609ab86bbcSShirley Ma 	if (unlikely(!skb))
1619ab86bbcSShirley Ma 		return NULL;
1629ab86bbcSShirley Ma 
1639ab86bbcSShirley Ma 	hdr = skb_vnet_hdr(skb);
1649ab86bbcSShirley Ma 
1653f2c31d9SMark McLoughlin 	if (vi->mergeable_rx_bufs) {
1669ab86bbcSShirley Ma 		hdr_len = sizeof hdr->mhdr;
1679ab86bbcSShirley Ma 		offset = hdr_len;
1689ab86bbcSShirley Ma 	} else {
1699ab86bbcSShirley Ma 		hdr_len = sizeof hdr->hdr;
1709ab86bbcSShirley Ma 		offset = sizeof(struct padded_vnet_hdr);
1719ab86bbcSShirley Ma 	}
1723f2c31d9SMark McLoughlin 
1739ab86bbcSShirley Ma 	memcpy(hdr, p, hdr_len);
1743f2c31d9SMark McLoughlin 
1759ab86bbcSShirley Ma 	len -= hdr_len;
1769ab86bbcSShirley Ma 	p += offset;
1773f2c31d9SMark McLoughlin 
1783f2c31d9SMark McLoughlin 	copy = len;
1793f2c31d9SMark McLoughlin 	if (copy > skb_tailroom(skb))
1803f2c31d9SMark McLoughlin 		copy = skb_tailroom(skb);
1813f2c31d9SMark McLoughlin 	memcpy(skb_put(skb, copy), p, copy);
1823f2c31d9SMark McLoughlin 
1833f2c31d9SMark McLoughlin 	len -= copy;
1849ab86bbcSShirley Ma 	offset += copy;
1853f2c31d9SMark McLoughlin 
1869ab86bbcSShirley Ma 	while (len) {
1879ab86bbcSShirley Ma 		set_skb_frag(skb, page, offset, &len);
1889ab86bbcSShirley Ma 		page = (struct page *)page->private;
1899ab86bbcSShirley Ma 		offset = 0;
1903f2c31d9SMark McLoughlin 	}
1913f2c31d9SMark McLoughlin 
1929ab86bbcSShirley Ma 	if (page)
1939ab86bbcSShirley Ma 		give_pages(vi, page);
1943f2c31d9SMark McLoughlin 
1959ab86bbcSShirley Ma 	return skb;
1969ab86bbcSShirley Ma }
1979ab86bbcSShirley Ma 
1989ab86bbcSShirley Ma static int receive_mergeable(struct virtnet_info *vi, struct sk_buff *skb)
1999ab86bbcSShirley Ma {
2009ab86bbcSShirley Ma 	struct skb_vnet_hdr *hdr = skb_vnet_hdr(skb);
2019ab86bbcSShirley Ma 	struct page *page;
2029ab86bbcSShirley Ma 	int num_buf, i, len;
2039ab86bbcSShirley Ma 
2049ab86bbcSShirley Ma 	num_buf = hdr->mhdr.num_buffers;
2059ab86bbcSShirley Ma 	while (--num_buf) {
2063f2c31d9SMark McLoughlin 		i = skb_shinfo(skb)->nr_frags;
2073f2c31d9SMark McLoughlin 		if (i >= MAX_SKB_FRAGS) {
2089ab86bbcSShirley Ma 			pr_debug("%s: packet too long\n", skb->dev->name);
2099ab86bbcSShirley Ma 			skb->dev->stats.rx_length_errors++;
2109ab86bbcSShirley Ma 			return -EINVAL;
2113f2c31d9SMark McLoughlin 		}
2123f2c31d9SMark McLoughlin 
2131915a712SMichael S. Tsirkin 		page = virtqueue_get_buf(vi->rvq, &len);
2149ab86bbcSShirley Ma 		if (!page) {
2153f2c31d9SMark McLoughlin 			pr_debug("%s: rx error: %d buffers missing\n",
2169ab86bbcSShirley Ma 				 skb->dev->name, hdr->mhdr.num_buffers);
2179ab86bbcSShirley Ma 			skb->dev->stats.rx_length_errors++;
2189ab86bbcSShirley Ma 			return -EINVAL;
2193f2c31d9SMark McLoughlin 		}
2203f2c31d9SMark McLoughlin 		if (len > PAGE_SIZE)
2213f2c31d9SMark McLoughlin 			len = PAGE_SIZE;
2223f2c31d9SMark McLoughlin 
2239ab86bbcSShirley Ma 		set_skb_frag(skb, page, 0, &len);
2249ab86bbcSShirley Ma 
2259ab86bbcSShirley Ma 		--vi->num;
2263f2c31d9SMark McLoughlin 	}
2279ab86bbcSShirley Ma 	return 0;
2289ab86bbcSShirley Ma }
2299ab86bbcSShirley Ma 
2309ab86bbcSShirley Ma static void receive_buf(struct net_device *dev, void *buf, unsigned int len)
2319ab86bbcSShirley Ma {
2329ab86bbcSShirley Ma 	struct virtnet_info *vi = netdev_priv(dev);
2339ab86bbcSShirley Ma 	struct sk_buff *skb;
2349ab86bbcSShirley Ma 	struct page *page;
2359ab86bbcSShirley Ma 	struct skb_vnet_hdr *hdr;
2369ab86bbcSShirley Ma 
2379ab86bbcSShirley Ma 	if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) {
2389ab86bbcSShirley Ma 		pr_debug("%s: short packet %i\n", dev->name, len);
2399ab86bbcSShirley Ma 		dev->stats.rx_length_errors++;
2409ab86bbcSShirley Ma 		if (vi->mergeable_rx_bufs || vi->big_packets)
2419ab86bbcSShirley Ma 			give_pages(vi, buf);
2429ab86bbcSShirley Ma 		else
2439ab86bbcSShirley Ma 			dev_kfree_skb(buf);
2449ab86bbcSShirley Ma 		return;
2459ab86bbcSShirley Ma 	}
2469ab86bbcSShirley Ma 
2479ab86bbcSShirley Ma 	if (!vi->mergeable_rx_bufs && !vi->big_packets) {
2489ab86bbcSShirley Ma 		skb = buf;
2499ab86bbcSShirley Ma 		len -= sizeof(struct virtio_net_hdr);
2509ab86bbcSShirley Ma 		skb_trim(skb, len);
2513f2c31d9SMark McLoughlin 	} else {
2529ab86bbcSShirley Ma 		page = buf;
2539ab86bbcSShirley Ma 		skb = page_to_skb(vi, page, len);
2549ab86bbcSShirley Ma 		if (unlikely(!skb)) {
25597402b96SHerbert Xu 			dev->stats.rx_dropped++;
2569ab86bbcSShirley Ma 			give_pages(vi, page);
2579ab86bbcSShirley Ma 			return;
2589ab86bbcSShirley Ma 		}
2599ab86bbcSShirley Ma 		if (vi->mergeable_rx_bufs)
2609ab86bbcSShirley Ma 			if (receive_mergeable(vi, skb)) {
2619ab86bbcSShirley Ma 				dev_kfree_skb(skb);
2629ab86bbcSShirley Ma 				return;
26397402b96SHerbert Xu 			}
2643f2c31d9SMark McLoughlin 	}
2653f2c31d9SMark McLoughlin 
2669ab86bbcSShirley Ma 	hdr = skb_vnet_hdr(skb);
26797402b96SHerbert Xu 	skb->truesize += skb->data_len;
268296f96fcSRusty Russell 	dev->stats.rx_bytes += skb->len;
269296f96fcSRusty Russell 	dev->stats.rx_packets++;
270296f96fcSRusty Russell 
271b3f24698SRusty Russell 	if (hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
272296f96fcSRusty Russell 		pr_debug("Needs csum!\n");
273b3f24698SRusty Russell 		if (!skb_partial_csum_set(skb,
274b3f24698SRusty Russell 					  hdr->hdr.csum_start,
275b3f24698SRusty Russell 					  hdr->hdr.csum_offset))
276296f96fcSRusty Russell 			goto frame_err;
277*10a8d94aSJason Wang 	} else if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID) {
278*10a8d94aSJason Wang 		skb->ip_summed = CHECKSUM_UNNECESSARY;
279296f96fcSRusty Russell 	}
280296f96fcSRusty Russell 
28123cde76dSMark McLoughlin 	skb->protocol = eth_type_trans(skb, dev);
28223cde76dSMark McLoughlin 	pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
28323cde76dSMark McLoughlin 		 ntohs(skb->protocol), skb->len, skb->pkt_type);
28423cde76dSMark McLoughlin 
285b3f24698SRusty Russell 	if (hdr->hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
286296f96fcSRusty Russell 		pr_debug("GSO!\n");
287b3f24698SRusty Russell 		switch (hdr->hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
288296f96fcSRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV4:
289296f96fcSRusty Russell 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
290296f96fcSRusty Russell 			break;
291296f96fcSRusty Russell 		case VIRTIO_NET_HDR_GSO_UDP:
292296f96fcSRusty Russell 			skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
293296f96fcSRusty Russell 			break;
294296f96fcSRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV6:
295296f96fcSRusty Russell 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
296296f96fcSRusty Russell 			break;
297296f96fcSRusty Russell 		default:
298296f96fcSRusty Russell 			if (net_ratelimit())
299296f96fcSRusty Russell 				printk(KERN_WARNING "%s: bad gso type %u.\n",
300b3f24698SRusty Russell 				       dev->name, hdr->hdr.gso_type);
301296f96fcSRusty Russell 			goto frame_err;
302296f96fcSRusty Russell 		}
303296f96fcSRusty Russell 
304b3f24698SRusty Russell 		if (hdr->hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN)
30534a48579SRusty Russell 			skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
30634a48579SRusty Russell 
307b3f24698SRusty Russell 		skb_shinfo(skb)->gso_size = hdr->hdr.gso_size;
308296f96fcSRusty Russell 		if (skb_shinfo(skb)->gso_size == 0) {
309296f96fcSRusty Russell 			if (net_ratelimit())
310296f96fcSRusty Russell 				printk(KERN_WARNING "%s: zero gso size.\n",
311296f96fcSRusty Russell 				       dev->name);
312296f96fcSRusty Russell 			goto frame_err;
313296f96fcSRusty Russell 		}
314296f96fcSRusty Russell 
315296f96fcSRusty Russell 		/* Header must be checked, and gso_segs computed. */
316296f96fcSRusty Russell 		skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
317296f96fcSRusty Russell 		skb_shinfo(skb)->gso_segs = 0;
318296f96fcSRusty Russell 	}
319296f96fcSRusty Russell 
320296f96fcSRusty Russell 	netif_receive_skb(skb);
321296f96fcSRusty Russell 	return;
322296f96fcSRusty Russell 
323296f96fcSRusty Russell frame_err:
324296f96fcSRusty Russell 	dev->stats.rx_frame_errors++;
325296f96fcSRusty Russell 	dev_kfree_skb(skb);
326296f96fcSRusty Russell }
327296f96fcSRusty Russell 
3289ab86bbcSShirley Ma static int add_recvbuf_small(struct virtnet_info *vi, gfp_t gfp)
329296f96fcSRusty Russell {
330296f96fcSRusty Russell 	struct sk_buff *skb;
331b3f24698SRusty Russell 	struct skb_vnet_hdr *hdr;
3329ab86bbcSShirley Ma 	int err;
3333f2c31d9SMark McLoughlin 
33489d71a66SEric Dumazet 	skb = netdev_alloc_skb_ip_align(vi->dev, MAX_PACKET_LEN);
3359ab86bbcSShirley Ma 	if (unlikely(!skb))
3369ab86bbcSShirley Ma 		return -ENOMEM;
337296f96fcSRusty Russell 
338296f96fcSRusty Russell 	skb_put(skb, MAX_PACKET_LEN);
3393f2c31d9SMark McLoughlin 
3403f2c31d9SMark McLoughlin 	hdr = skb_vnet_hdr(skb);
3415e01d2f9SMichael S. Tsirkin 	sg_set_buf(vi->rx_sg, &hdr->hdr, sizeof hdr->hdr);
34297402b96SHerbert Xu 
3435e01d2f9SMichael S. Tsirkin 	skb_to_sgvec(skb, vi->rx_sg + 1, 0, skb->len);
34497402b96SHerbert Xu 
345aa989f5eSMichael S. Tsirkin 	err = virtqueue_add_buf_gfp(vi->rvq, vi->rx_sg, 0, 2, skb, gfp);
3469ab86bbcSShirley Ma 	if (err < 0)
3479ab86bbcSShirley Ma 		dev_kfree_skb(skb);
34897402b96SHerbert Xu 
3499ab86bbcSShirley Ma 	return err;
35097402b96SHerbert Xu }
35197402b96SHerbert Xu 
3529ab86bbcSShirley Ma static int add_recvbuf_big(struct virtnet_info *vi, gfp_t gfp)
3539ab86bbcSShirley Ma {
3549ab86bbcSShirley Ma 	struct page *first, *list = NULL;
3559ab86bbcSShirley Ma 	char *p;
3569ab86bbcSShirley Ma 	int i, err, offset;
357296f96fcSRusty Russell 
3585e01d2f9SMichael S. Tsirkin 	/* page in vi->rx_sg[MAX_SKB_FRAGS + 1] is list tail */
3599ab86bbcSShirley Ma 	for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
3609ab86bbcSShirley Ma 		first = get_a_page(vi, gfp);
3619ab86bbcSShirley Ma 		if (!first) {
3629ab86bbcSShirley Ma 			if (list)
3639ab86bbcSShirley Ma 				give_pages(vi, list);
3649ab86bbcSShirley Ma 			return -ENOMEM;
365296f96fcSRusty Russell 		}
3665e01d2f9SMichael S. Tsirkin 		sg_set_buf(&vi->rx_sg[i], page_address(first), PAGE_SIZE);
3679ab86bbcSShirley Ma 
3689ab86bbcSShirley Ma 		/* chain new page in list head to match sg */
3699ab86bbcSShirley Ma 		first->private = (unsigned long)list;
3709ab86bbcSShirley Ma 		list = first;
3719ab86bbcSShirley Ma 	}
3729ab86bbcSShirley Ma 
3739ab86bbcSShirley Ma 	first = get_a_page(vi, gfp);
3749ab86bbcSShirley Ma 	if (!first) {
3759ab86bbcSShirley Ma 		give_pages(vi, list);
3769ab86bbcSShirley Ma 		return -ENOMEM;
3779ab86bbcSShirley Ma 	}
3789ab86bbcSShirley Ma 	p = page_address(first);
3799ab86bbcSShirley Ma 
3805e01d2f9SMichael S. Tsirkin 	/* vi->rx_sg[0], vi->rx_sg[1] share the same page */
3815e01d2f9SMichael S. Tsirkin 	/* a separated vi->rx_sg[0] for virtio_net_hdr only due to QEMU bug */
3825e01d2f9SMichael S. Tsirkin 	sg_set_buf(&vi->rx_sg[0], p, sizeof(struct virtio_net_hdr));
3839ab86bbcSShirley Ma 
3845e01d2f9SMichael S. Tsirkin 	/* vi->rx_sg[1] for data packet, from offset */
3859ab86bbcSShirley Ma 	offset = sizeof(struct padded_vnet_hdr);
3865e01d2f9SMichael S. Tsirkin 	sg_set_buf(&vi->rx_sg[1], p + offset, PAGE_SIZE - offset);
3879ab86bbcSShirley Ma 
3889ab86bbcSShirley Ma 	/* chain first in list head */
3899ab86bbcSShirley Ma 	first->private = (unsigned long)list;
390aa989f5eSMichael S. Tsirkin 	err = virtqueue_add_buf_gfp(vi->rvq, vi->rx_sg, 0, MAX_SKB_FRAGS + 2,
391aa989f5eSMichael S. Tsirkin 				    first, gfp);
3929ab86bbcSShirley Ma 	if (err < 0)
3939ab86bbcSShirley Ma 		give_pages(vi, first);
3949ab86bbcSShirley Ma 
3959ab86bbcSShirley Ma 	return err;
3969ab86bbcSShirley Ma }
3979ab86bbcSShirley Ma 
3989ab86bbcSShirley Ma static int add_recvbuf_mergeable(struct virtnet_info *vi, gfp_t gfp)
3999ab86bbcSShirley Ma {
4009ab86bbcSShirley Ma 	struct page *page;
4019ab86bbcSShirley Ma 	int err;
4029ab86bbcSShirley Ma 
4039ab86bbcSShirley Ma 	page = get_a_page(vi, gfp);
4049ab86bbcSShirley Ma 	if (!page)
4059ab86bbcSShirley Ma 		return -ENOMEM;
4069ab86bbcSShirley Ma 
4075e01d2f9SMichael S. Tsirkin 	sg_init_one(vi->rx_sg, page_address(page), PAGE_SIZE);
4089ab86bbcSShirley Ma 
409aa989f5eSMichael S. Tsirkin 	err = virtqueue_add_buf_gfp(vi->rvq, vi->rx_sg, 0, 1, page, gfp);
4109ab86bbcSShirley Ma 	if (err < 0)
4119ab86bbcSShirley Ma 		give_pages(vi, page);
4129ab86bbcSShirley Ma 
4139ab86bbcSShirley Ma 	return err;
414296f96fcSRusty Russell }
415296f96fcSRusty Russell 
4163161e453SRusty Russell /* Returns false if we couldn't fill entirely (OOM). */
4173161e453SRusty Russell static bool try_fill_recv(struct virtnet_info *vi, gfp_t gfp)
4183f2c31d9SMark McLoughlin {
4193f2c31d9SMark McLoughlin 	int err;
4201788f495SMichael S. Tsirkin 	bool oom;
4213f2c31d9SMark McLoughlin 
4220aea51c3SAmit Shah 	do {
4239ab86bbcSShirley Ma 		if (vi->mergeable_rx_bufs)
4249ab86bbcSShirley Ma 			err = add_recvbuf_mergeable(vi, gfp);
4259ab86bbcSShirley Ma 		else if (vi->big_packets)
4269ab86bbcSShirley Ma 			err = add_recvbuf_big(vi, gfp);
4279ab86bbcSShirley Ma 		else
4289ab86bbcSShirley Ma 			err = add_recvbuf_small(vi, gfp);
4293f2c31d9SMark McLoughlin 
4301788f495SMichael S. Tsirkin 		oom = err == -ENOMEM;
4311788f495SMichael S. Tsirkin 		if (err < 0)
4323f2c31d9SMark McLoughlin 			break;
4339ab86bbcSShirley Ma 		++vi->num;
4340aea51c3SAmit Shah 	} while (err > 0);
4353f2c31d9SMark McLoughlin 	if (unlikely(vi->num > vi->max))
4363f2c31d9SMark McLoughlin 		vi->max = vi->num;
4371915a712SMichael S. Tsirkin 	virtqueue_kick(vi->rvq);
4383161e453SRusty Russell 	return !oom;
4393f2c31d9SMark McLoughlin }
4403f2c31d9SMark McLoughlin 
44118445c4dSRusty Russell static void skb_recv_done(struct virtqueue *rvq)
442296f96fcSRusty Russell {
443296f96fcSRusty Russell 	struct virtnet_info *vi = rvq->vdev->priv;
44418445c4dSRusty Russell 	/* Schedule NAPI, Suppress further interrupts if successful. */
445288379f0SBen Hutchings 	if (napi_schedule_prep(&vi->napi)) {
4461915a712SMichael S. Tsirkin 		virtqueue_disable_cb(rvq);
447288379f0SBen Hutchings 		__napi_schedule(&vi->napi);
44818445c4dSRusty Russell 	}
449296f96fcSRusty Russell }
450296f96fcSRusty Russell 
4513e9d08ecSBruce Rogers static void virtnet_napi_enable(struct virtnet_info *vi)
4523e9d08ecSBruce Rogers {
4533e9d08ecSBruce Rogers 	napi_enable(&vi->napi);
4543e9d08ecSBruce Rogers 
4553e9d08ecSBruce Rogers 	/* If all buffers were filled by other side before we napi_enabled, we
4563e9d08ecSBruce Rogers 	 * won't get another interrupt, so process any outstanding packets
4573e9d08ecSBruce Rogers 	 * now.  virtnet_poll wants re-enable the queue, so we disable here.
4583e9d08ecSBruce Rogers 	 * We synchronize against interrupts via NAPI_STATE_SCHED */
4593e9d08ecSBruce Rogers 	if (napi_schedule_prep(&vi->napi)) {
4603e9d08ecSBruce Rogers 		virtqueue_disable_cb(vi->rvq);
4613e9d08ecSBruce Rogers 		__napi_schedule(&vi->napi);
4623e9d08ecSBruce Rogers 	}
4633e9d08ecSBruce Rogers }
4643e9d08ecSBruce Rogers 
4653161e453SRusty Russell static void refill_work(struct work_struct *work)
4663161e453SRusty Russell {
4673161e453SRusty Russell 	struct virtnet_info *vi;
4683161e453SRusty Russell 	bool still_empty;
4693161e453SRusty Russell 
4703161e453SRusty Russell 	vi = container_of(work, struct virtnet_info, refill.work);
4713161e453SRusty Russell 	napi_disable(&vi->napi);
47239d32157SHerbert Xu 	still_empty = !try_fill_recv(vi, GFP_KERNEL);
4733e9d08ecSBruce Rogers 	virtnet_napi_enable(vi);
4743161e453SRusty Russell 
4753161e453SRusty Russell 	/* In theory, this can happen: if we don't get any buffers in
4763161e453SRusty Russell 	 * we will *never* try to fill again. */
4773161e453SRusty Russell 	if (still_empty)
4783161e453SRusty Russell 		schedule_delayed_work(&vi->refill, HZ/2);
4793161e453SRusty Russell }
4803161e453SRusty Russell 
481296f96fcSRusty Russell static int virtnet_poll(struct napi_struct *napi, int budget)
482296f96fcSRusty Russell {
483296f96fcSRusty Russell 	struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi);
4849ab86bbcSShirley Ma 	void *buf;
485296f96fcSRusty Russell 	unsigned int len, received = 0;
486296f96fcSRusty Russell 
487296f96fcSRusty Russell again:
488296f96fcSRusty Russell 	while (received < budget &&
4891915a712SMichael S. Tsirkin 	       (buf = virtqueue_get_buf(vi->rvq, &len)) != NULL) {
4909ab86bbcSShirley Ma 		receive_buf(vi->dev, buf, len);
4919ab86bbcSShirley Ma 		--vi->num;
492296f96fcSRusty Russell 		received++;
493296f96fcSRusty Russell 	}
494296f96fcSRusty Russell 
4953161e453SRusty Russell 	if (vi->num < vi->max / 2) {
4963161e453SRusty Russell 		if (!try_fill_recv(vi, GFP_ATOMIC))
4973161e453SRusty Russell 			schedule_delayed_work(&vi->refill, 0);
4983161e453SRusty Russell 	}
499296f96fcSRusty Russell 
5008329d98eSRusty Russell 	/* Out of packets? */
5018329d98eSRusty Russell 	if (received < budget) {
502288379f0SBen Hutchings 		napi_complete(napi);
5031915a712SMichael S. Tsirkin 		if (unlikely(!virtqueue_enable_cb(vi->rvq)) &&
5048e95a202SJoe Perches 		    napi_schedule_prep(napi)) {
5051915a712SMichael S. Tsirkin 			virtqueue_disable_cb(vi->rvq);
506288379f0SBen Hutchings 			__napi_schedule(napi);
507296f96fcSRusty Russell 			goto again;
508296f96fcSRusty Russell 		}
5094265f161SChristian Borntraeger 	}
510296f96fcSRusty Russell 
511296f96fcSRusty Russell 	return received;
512296f96fcSRusty Russell }
513296f96fcSRusty Russell 
51448925e37SRusty Russell static unsigned int free_old_xmit_skbs(struct virtnet_info *vi)
515296f96fcSRusty Russell {
516296f96fcSRusty Russell 	struct sk_buff *skb;
51748925e37SRusty Russell 	unsigned int len, tot_sgs = 0;
518296f96fcSRusty Russell 
5191915a712SMichael S. Tsirkin 	while ((skb = virtqueue_get_buf(vi->svq, &len)) != NULL) {
520296f96fcSRusty Russell 		pr_debug("Sent skb %p\n", skb);
521655aa31fSRusty Russell 		vi->dev->stats.tx_bytes += skb->len;
522296f96fcSRusty Russell 		vi->dev->stats.tx_packets++;
52348925e37SRusty Russell 		tot_sgs += skb_vnet_hdr(skb)->num_sg;
524ed79bab8SEric Dumazet 		dev_kfree_skb_any(skb);
525296f96fcSRusty Russell 	}
52648925e37SRusty Russell 	return tot_sgs;
527296f96fcSRusty Russell }
528296f96fcSRusty Russell 
52999ffc696SRusty Russell static int xmit_skb(struct virtnet_info *vi, struct sk_buff *skb)
530296f96fcSRusty Russell {
531b3f24698SRusty Russell 	struct skb_vnet_hdr *hdr = skb_vnet_hdr(skb);
532296f96fcSRusty Russell 	const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
533296f96fcSRusty Russell 
534e174961cSJohannes Berg 	pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
535296f96fcSRusty Russell 
536296f96fcSRusty Russell 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
537b3f24698SRusty Russell 		hdr->hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
53855508d60SMichał Mirosław 		hdr->hdr.csum_start = skb_checksum_start_offset(skb);
539b3f24698SRusty Russell 		hdr->hdr.csum_offset = skb->csum_offset;
540296f96fcSRusty Russell 	} else {
541b3f24698SRusty Russell 		hdr->hdr.flags = 0;
542b3f24698SRusty Russell 		hdr->hdr.csum_offset = hdr->hdr.csum_start = 0;
543296f96fcSRusty Russell 	}
544296f96fcSRusty Russell 
545296f96fcSRusty Russell 	if (skb_is_gso(skb)) {
546b3f24698SRusty Russell 		hdr->hdr.hdr_len = skb_headlen(skb);
547b3f24698SRusty Russell 		hdr->hdr.gso_size = skb_shinfo(skb)->gso_size;
54834a48579SRusty Russell 		if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
549b3f24698SRusty Russell 			hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
550296f96fcSRusty Russell 		else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
551b3f24698SRusty Russell 			hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
552296f96fcSRusty Russell 		else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
553b3f24698SRusty Russell 			hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP;
554296f96fcSRusty Russell 		else
555296f96fcSRusty Russell 			BUG();
55634a48579SRusty Russell 		if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
557b3f24698SRusty Russell 			hdr->hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
558296f96fcSRusty Russell 	} else {
559b3f24698SRusty Russell 		hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
560b3f24698SRusty Russell 		hdr->hdr.gso_size = hdr->hdr.hdr_len = 0;
561296f96fcSRusty Russell 	}
562296f96fcSRusty Russell 
563b3f24698SRusty Russell 	hdr->mhdr.num_buffers = 0;
5643f2c31d9SMark McLoughlin 
5653f2c31d9SMark McLoughlin 	/* Encode metadata header at front. */
5663f2c31d9SMark McLoughlin 	if (vi->mergeable_rx_bufs)
5675e01d2f9SMichael S. Tsirkin 		sg_set_buf(vi->tx_sg, &hdr->mhdr, sizeof hdr->mhdr);
5683f2c31d9SMark McLoughlin 	else
5695e01d2f9SMichael S. Tsirkin 		sg_set_buf(vi->tx_sg, &hdr->hdr, sizeof hdr->hdr);
5703f2c31d9SMark McLoughlin 
5715e01d2f9SMichael S. Tsirkin 	hdr->num_sg = skb_to_sgvec(skb, vi->tx_sg + 1, 0, skb->len) + 1;
5721756ac3dSLinus Torvalds 	return virtqueue_add_buf(vi->svq, vi->tx_sg, hdr->num_sg,
5735e01d2f9SMichael S. Tsirkin 					0, skb);
57411a3a154SRusty Russell }
57511a3a154SRusty Russell 
576424efe9cSStephen Hemminger static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
57799ffc696SRusty Russell {
57899ffc696SRusty Russell 	struct virtnet_info *vi = netdev_priv(dev);
57948925e37SRusty Russell 	int capacity;
5802cb9c6baSRusty Russell 
5812cb9c6baSRusty Russell 	/* Free up any pending old buffers before queueing new ones. */
5822cb9c6baSRusty Russell 	free_old_xmit_skbs(vi);
58399ffc696SRusty Russell 
58403f191baSMichael S. Tsirkin 	/* Try to transmit */
58548925e37SRusty Russell 	capacity = xmit_skb(vi, skb);
58699ffc696SRusty Russell 
58748925e37SRusty Russell 	/* This can happen with OOM and indirect buffers. */
58848925e37SRusty Russell 	if (unlikely(capacity < 0)) {
58958eba97dSRusty Russell 		if (net_ratelimit()) {
59058eba97dSRusty Russell 			if (likely(capacity == -ENOMEM)) {
59158eba97dSRusty Russell 				dev_warn(&dev->dev,
59258eba97dSRusty Russell 					 "TX queue failure: out of memory\n");
59358eba97dSRusty Russell 			} else {
59458eba97dSRusty Russell 				dev->stats.tx_fifo_errors++;
59558eba97dSRusty Russell 				dev_warn(&dev->dev,
59658eba97dSRusty Russell 					 "Unexpected TX queue failure: %d\n",
59758eba97dSRusty Russell 					 capacity);
5982cb9c6baSRusty Russell 			}
59958eba97dSRusty Russell 		}
60058eba97dSRusty Russell 		dev->stats.tx_dropped++;
60158eba97dSRusty Russell 		kfree_skb(skb);
60258eba97dSRusty Russell 		return NETDEV_TX_OK;
603296f96fcSRusty Russell 	}
6041915a712SMichael S. Tsirkin 	virtqueue_kick(vi->svq);
60503f191baSMichael S. Tsirkin 
60648925e37SRusty Russell 	/* Don't wait up for transmitted skbs to be freed. */
60748925e37SRusty Russell 	skb_orphan(skb);
60848925e37SRusty Russell 	nf_reset(skb);
60948925e37SRusty Russell 
61048925e37SRusty Russell 	/* Apparently nice girls don't return TX_BUSY; stop the queue
61148925e37SRusty Russell 	 * before it gets out of hand.  Naturally, this wastes entries. */
61248925e37SRusty Russell 	if (capacity < 2+MAX_SKB_FRAGS) {
61348925e37SRusty Russell 		netif_stop_queue(dev);
6147a66f784SMichael S. Tsirkin 		if (unlikely(!virtqueue_enable_cb_delayed(vi->svq))) {
61548925e37SRusty Russell 			/* More just got used, free them then recheck. */
61648925e37SRusty Russell 			capacity += free_old_xmit_skbs(vi);
61748925e37SRusty Russell 			if (capacity >= 2+MAX_SKB_FRAGS) {
61848925e37SRusty Russell 				netif_start_queue(dev);
6191915a712SMichael S. Tsirkin 				virtqueue_disable_cb(vi->svq);
62048925e37SRusty Russell 			}
62148925e37SRusty Russell 		}
62248925e37SRusty Russell 	}
62348925e37SRusty Russell 
62448925e37SRusty Russell 	return NETDEV_TX_OK;
62548925e37SRusty Russell }
62648925e37SRusty Russell 
6279c46f6d4SAlex Williamson static int virtnet_set_mac_address(struct net_device *dev, void *p)
6289c46f6d4SAlex Williamson {
6299c46f6d4SAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
6309c46f6d4SAlex Williamson 	struct virtio_device *vdev = vi->vdev;
6319c46f6d4SAlex Williamson 	int ret;
6329c46f6d4SAlex Williamson 
6339c46f6d4SAlex Williamson 	ret = eth_mac_addr(dev, p);
6349c46f6d4SAlex Williamson 	if (ret)
6359c46f6d4SAlex Williamson 		return ret;
6369c46f6d4SAlex Williamson 
63762994b2dSAlex Williamson 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
6389c46f6d4SAlex Williamson 		vdev->config->set(vdev, offsetof(struct virtio_net_config, mac),
6399c46f6d4SAlex Williamson 		                  dev->dev_addr, dev->addr_len);
6409c46f6d4SAlex Williamson 
6419c46f6d4SAlex Williamson 	return 0;
6429c46f6d4SAlex Williamson }
6439c46f6d4SAlex Williamson 
644da74e89dSAmit Shah #ifdef CONFIG_NET_POLL_CONTROLLER
645da74e89dSAmit Shah static void virtnet_netpoll(struct net_device *dev)
646da74e89dSAmit Shah {
647da74e89dSAmit Shah 	struct virtnet_info *vi = netdev_priv(dev);
648da74e89dSAmit Shah 
649da74e89dSAmit Shah 	napi_schedule(&vi->napi);
650da74e89dSAmit Shah }
651da74e89dSAmit Shah #endif
652da74e89dSAmit Shah 
653296f96fcSRusty Russell static int virtnet_open(struct net_device *dev)
654296f96fcSRusty Russell {
655296f96fcSRusty Russell 	struct virtnet_info *vi = netdev_priv(dev);
656296f96fcSRusty Russell 
6573e9d08ecSBruce Rogers 	virtnet_napi_enable(vi);
658296f96fcSRusty Russell 	return 0;
659296f96fcSRusty Russell }
660296f96fcSRusty Russell 
6612a41f71dSAlex Williamson /*
6622a41f71dSAlex Williamson  * Send command via the control virtqueue and check status.  Commands
6632a41f71dSAlex Williamson  * supported by the hypervisor, as indicated by feature bits, should
6642a41f71dSAlex Williamson  * never fail unless improperly formated.
6652a41f71dSAlex Williamson  */
6662a41f71dSAlex Williamson static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
6672a41f71dSAlex Williamson 				 struct scatterlist *data, int out, int in)
6682a41f71dSAlex Williamson {
66923e258e1SAlex Williamson 	struct scatterlist *s, sg[VIRTNET_SEND_COMMAND_SG_MAX + 2];
6702a41f71dSAlex Williamson 	struct virtio_net_ctrl_hdr ctrl;
6712a41f71dSAlex Williamson 	virtio_net_ctrl_ack status = ~0;
6722a41f71dSAlex Williamson 	unsigned int tmp;
67323e258e1SAlex Williamson 	int i;
6742a41f71dSAlex Williamson 
6750ee904c3SAlexander Beregalov 	/* Caller should know better */
6760ee904c3SAlexander Beregalov 	BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) ||
6770ee904c3SAlexander Beregalov 		(out + in > VIRTNET_SEND_COMMAND_SG_MAX));
6782a41f71dSAlex Williamson 
6792a41f71dSAlex Williamson 	out++; /* Add header */
6802a41f71dSAlex Williamson 	in++; /* Add return status */
6812a41f71dSAlex Williamson 
6822a41f71dSAlex Williamson 	ctrl.class = class;
6832a41f71dSAlex Williamson 	ctrl.cmd = cmd;
6842a41f71dSAlex Williamson 
6852a41f71dSAlex Williamson 	sg_init_table(sg, out + in);
6862a41f71dSAlex Williamson 
6872a41f71dSAlex Williamson 	sg_set_buf(&sg[0], &ctrl, sizeof(ctrl));
68823e258e1SAlex Williamson 	for_each_sg(data, s, out + in - 2, i)
68923e258e1SAlex Williamson 		sg_set_buf(&sg[i + 1], sg_virt(s), s->length);
6902a41f71dSAlex Williamson 	sg_set_buf(&sg[out + in - 1], &status, sizeof(status));
6912a41f71dSAlex Williamson 
6921915a712SMichael S. Tsirkin 	BUG_ON(virtqueue_add_buf(vi->cvq, sg, out, in, vi) < 0);
6932a41f71dSAlex Williamson 
6941915a712SMichael S. Tsirkin 	virtqueue_kick(vi->cvq);
6952a41f71dSAlex Williamson 
6962a41f71dSAlex Williamson 	/*
6972a41f71dSAlex Williamson 	 * Spin for a response, the kick causes an ioport write, trapping
6982a41f71dSAlex Williamson 	 * into the hypervisor, so the request should be handled immediately.
6992a41f71dSAlex Williamson 	 */
7001915a712SMichael S. Tsirkin 	while (!virtqueue_get_buf(vi->cvq, &tmp))
7012a41f71dSAlex Williamson 		cpu_relax();
7022a41f71dSAlex Williamson 
7032a41f71dSAlex Williamson 	return status == VIRTIO_NET_OK;
7042a41f71dSAlex Williamson }
7052a41f71dSAlex Williamson 
706296f96fcSRusty Russell static int virtnet_close(struct net_device *dev)
707296f96fcSRusty Russell {
708296f96fcSRusty Russell 	struct virtnet_info *vi = netdev_priv(dev);
709296f96fcSRusty Russell 
710296f96fcSRusty Russell 	napi_disable(&vi->napi);
711296f96fcSRusty Russell 
712296f96fcSRusty Russell 	return 0;
713296f96fcSRusty Russell }
714296f96fcSRusty Russell 
7152af7698eSAlex Williamson static void virtnet_set_rx_mode(struct net_device *dev)
7162af7698eSAlex Williamson {
7172af7698eSAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
718f565a7c2SAlex Williamson 	struct scatterlist sg[2];
7192af7698eSAlex Williamson 	u8 promisc, allmulti;
720f565a7c2SAlex Williamson 	struct virtio_net_ctrl_mac *mac_data;
721ccffad25SJiri Pirko 	struct netdev_hw_addr *ha;
72232e7bfc4SJiri Pirko 	int uc_count;
7234cd24eafSJiri Pirko 	int mc_count;
724f565a7c2SAlex Williamson 	void *buf;
725f565a7c2SAlex Williamson 	int i;
7262af7698eSAlex Williamson 
7272af7698eSAlex Williamson 	/* We can't dynamicaly set ndo_set_rx_mode, so return gracefully */
7282af7698eSAlex Williamson 	if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
7292af7698eSAlex Williamson 		return;
7302af7698eSAlex Williamson 
731f565a7c2SAlex Williamson 	promisc = ((dev->flags & IFF_PROMISC) != 0);
732f565a7c2SAlex Williamson 	allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
7332af7698eSAlex Williamson 
73423e258e1SAlex Williamson 	sg_init_one(sg, &promisc, sizeof(promisc));
7352af7698eSAlex Williamson 
7362af7698eSAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
7372af7698eSAlex Williamson 				  VIRTIO_NET_CTRL_RX_PROMISC,
738f565a7c2SAlex Williamson 				  sg, 1, 0))
7392af7698eSAlex Williamson 		dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
7402af7698eSAlex Williamson 			 promisc ? "en" : "dis");
7412af7698eSAlex Williamson 
74223e258e1SAlex Williamson 	sg_init_one(sg, &allmulti, sizeof(allmulti));
7432af7698eSAlex Williamson 
7442af7698eSAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
7452af7698eSAlex Williamson 				  VIRTIO_NET_CTRL_RX_ALLMULTI,
746f565a7c2SAlex Williamson 				  sg, 1, 0))
7472af7698eSAlex Williamson 		dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
7482af7698eSAlex Williamson 			 allmulti ? "en" : "dis");
749f565a7c2SAlex Williamson 
75032e7bfc4SJiri Pirko 	uc_count = netdev_uc_count(dev);
7514cd24eafSJiri Pirko 	mc_count = netdev_mc_count(dev);
752f565a7c2SAlex Williamson 	/* MAC filter - use one buffer for both lists */
7534cd24eafSJiri Pirko 	buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
754f565a7c2SAlex Williamson 		      (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
7554cd24eafSJiri Pirko 	mac_data = buf;
756f565a7c2SAlex Williamson 	if (!buf) {
757f565a7c2SAlex Williamson 		dev_warn(&dev->dev, "No memory for MAC address buffer\n");
758f565a7c2SAlex Williamson 		return;
759f565a7c2SAlex Williamson 	}
760f565a7c2SAlex Williamson 
76123e258e1SAlex Williamson 	sg_init_table(sg, 2);
76223e258e1SAlex Williamson 
763f565a7c2SAlex Williamson 	/* Store the unicast list and count in the front of the buffer */
76432e7bfc4SJiri Pirko 	mac_data->entries = uc_count;
765ccffad25SJiri Pirko 	i = 0;
76632e7bfc4SJiri Pirko 	netdev_for_each_uc_addr(ha, dev)
767ccffad25SJiri Pirko 		memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
768f565a7c2SAlex Williamson 
769f565a7c2SAlex Williamson 	sg_set_buf(&sg[0], mac_data,
77032e7bfc4SJiri Pirko 		   sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
771f565a7c2SAlex Williamson 
772f565a7c2SAlex Williamson 	/* multicast list and count fill the end */
77332e7bfc4SJiri Pirko 	mac_data = (void *)&mac_data->macs[uc_count][0];
774f565a7c2SAlex Williamson 
7754cd24eafSJiri Pirko 	mac_data->entries = mc_count;
776567ec874SJiri Pirko 	i = 0;
77722bedad3SJiri Pirko 	netdev_for_each_mc_addr(ha, dev)
77822bedad3SJiri Pirko 		memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
779f565a7c2SAlex Williamson 
780f565a7c2SAlex Williamson 	sg_set_buf(&sg[1], mac_data,
7814cd24eafSJiri Pirko 		   sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
782f565a7c2SAlex Williamson 
783f565a7c2SAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
784f565a7c2SAlex Williamson 				  VIRTIO_NET_CTRL_MAC_TABLE_SET,
785f565a7c2SAlex Williamson 				  sg, 2, 0))
786f565a7c2SAlex Williamson 		dev_warn(&dev->dev, "Failed to set MAC fitler table.\n");
787f565a7c2SAlex Williamson 
788f565a7c2SAlex Williamson 	kfree(buf);
7892af7698eSAlex Williamson }
7902af7698eSAlex Williamson 
7911824a989SAlex Williamson static void virtnet_vlan_rx_add_vid(struct net_device *dev, u16 vid)
7920bde9569SAlex Williamson {
7930bde9569SAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
7940bde9569SAlex Williamson 	struct scatterlist sg;
7950bde9569SAlex Williamson 
79623e258e1SAlex Williamson 	sg_init_one(&sg, &vid, sizeof(vid));
7970bde9569SAlex Williamson 
7980bde9569SAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
7990bde9569SAlex Williamson 				  VIRTIO_NET_CTRL_VLAN_ADD, &sg, 1, 0))
8000bde9569SAlex Williamson 		dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
8010bde9569SAlex Williamson }
8020bde9569SAlex Williamson 
8031824a989SAlex Williamson static void virtnet_vlan_rx_kill_vid(struct net_device *dev, u16 vid)
8040bde9569SAlex Williamson {
8050bde9569SAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
8060bde9569SAlex Williamson 	struct scatterlist sg;
8070bde9569SAlex Williamson 
80823e258e1SAlex Williamson 	sg_init_one(&sg, &vid, sizeof(vid));
8090bde9569SAlex Williamson 
8100bde9569SAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
8110bde9569SAlex Williamson 				  VIRTIO_NET_CTRL_VLAN_DEL, &sg, 1, 0))
8120bde9569SAlex Williamson 		dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
8130bde9569SAlex Williamson }
8140bde9569SAlex Williamson 
8150fc0b732SStephen Hemminger static const struct ethtool_ops virtnet_ethtool_ops = {
8169f4d26d0SMark McLoughlin 	.get_link = ethtool_op_get_link,
817a9ea3fc6SHerbert Xu };
818a9ea3fc6SHerbert Xu 
81939da5814SMark McLoughlin #define MIN_MTU 68
82039da5814SMark McLoughlin #define MAX_MTU 65535
82139da5814SMark McLoughlin 
82239da5814SMark McLoughlin static int virtnet_change_mtu(struct net_device *dev, int new_mtu)
82339da5814SMark McLoughlin {
82439da5814SMark McLoughlin 	if (new_mtu < MIN_MTU || new_mtu > MAX_MTU)
82539da5814SMark McLoughlin 		return -EINVAL;
82639da5814SMark McLoughlin 	dev->mtu = new_mtu;
82739da5814SMark McLoughlin 	return 0;
82839da5814SMark McLoughlin }
82939da5814SMark McLoughlin 
83076288b4eSStephen Hemminger static const struct net_device_ops virtnet_netdev = {
83176288b4eSStephen Hemminger 	.ndo_open            = virtnet_open,
83276288b4eSStephen Hemminger 	.ndo_stop   	     = virtnet_close,
83376288b4eSStephen Hemminger 	.ndo_start_xmit      = start_xmit,
83476288b4eSStephen Hemminger 	.ndo_validate_addr   = eth_validate_addr,
8359c46f6d4SAlex Williamson 	.ndo_set_mac_address = virtnet_set_mac_address,
8362af7698eSAlex Williamson 	.ndo_set_rx_mode     = virtnet_set_rx_mode,
83776288b4eSStephen Hemminger 	.ndo_change_mtu	     = virtnet_change_mtu,
8381824a989SAlex Williamson 	.ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
8391824a989SAlex Williamson 	.ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
84076288b4eSStephen Hemminger #ifdef CONFIG_NET_POLL_CONTROLLER
84176288b4eSStephen Hemminger 	.ndo_poll_controller = virtnet_netpoll,
84276288b4eSStephen Hemminger #endif
84376288b4eSStephen Hemminger };
84476288b4eSStephen Hemminger 
8459f4d26d0SMark McLoughlin static void virtnet_update_status(struct virtnet_info *vi)
8469f4d26d0SMark McLoughlin {
8479f4d26d0SMark McLoughlin 	u16 v;
8489f4d26d0SMark McLoughlin 
8499f4d26d0SMark McLoughlin 	if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS))
8509f4d26d0SMark McLoughlin 		return;
8519f4d26d0SMark McLoughlin 
8529f4d26d0SMark McLoughlin 	vi->vdev->config->get(vi->vdev,
8539f4d26d0SMark McLoughlin 			      offsetof(struct virtio_net_config, status),
8549f4d26d0SMark McLoughlin 			      &v, sizeof(v));
8559f4d26d0SMark McLoughlin 
8569f4d26d0SMark McLoughlin 	/* Ignore unknown (future) status bits */
8579f4d26d0SMark McLoughlin 	v &= VIRTIO_NET_S_LINK_UP;
8589f4d26d0SMark McLoughlin 
8599f4d26d0SMark McLoughlin 	if (vi->status == v)
8609f4d26d0SMark McLoughlin 		return;
8619f4d26d0SMark McLoughlin 
8629f4d26d0SMark McLoughlin 	vi->status = v;
8639f4d26d0SMark McLoughlin 
8649f4d26d0SMark McLoughlin 	if (vi->status & VIRTIO_NET_S_LINK_UP) {
8659f4d26d0SMark McLoughlin 		netif_carrier_on(vi->dev);
8669f4d26d0SMark McLoughlin 		netif_wake_queue(vi->dev);
8679f4d26d0SMark McLoughlin 	} else {
8689f4d26d0SMark McLoughlin 		netif_carrier_off(vi->dev);
8699f4d26d0SMark McLoughlin 		netif_stop_queue(vi->dev);
8709f4d26d0SMark McLoughlin 	}
8719f4d26d0SMark McLoughlin }
8729f4d26d0SMark McLoughlin 
8739f4d26d0SMark McLoughlin static void virtnet_config_changed(struct virtio_device *vdev)
8749f4d26d0SMark McLoughlin {
8759f4d26d0SMark McLoughlin 	struct virtnet_info *vi = vdev->priv;
8769f4d26d0SMark McLoughlin 
8779f4d26d0SMark McLoughlin 	virtnet_update_status(vi);
8789f4d26d0SMark McLoughlin }
8799f4d26d0SMark McLoughlin 
880296f96fcSRusty Russell static int virtnet_probe(struct virtio_device *vdev)
881296f96fcSRusty Russell {
882296f96fcSRusty Russell 	int err;
883296f96fcSRusty Russell 	struct net_device *dev;
884296f96fcSRusty Russell 	struct virtnet_info *vi;
885d2a7dddaSMichael S. Tsirkin 	struct virtqueue *vqs[3];
886d2a7dddaSMichael S. Tsirkin 	vq_callback_t *callbacks[] = { skb_recv_done, skb_xmit_done, NULL};
887d2a7dddaSMichael S. Tsirkin 	const char *names[] = { "input", "output", "control" };
888d2a7dddaSMichael S. Tsirkin 	int nvqs;
889296f96fcSRusty Russell 
890296f96fcSRusty Russell 	/* Allocate ourselves a network device with room for our info */
891296f96fcSRusty Russell 	dev = alloc_etherdev(sizeof(struct virtnet_info));
892296f96fcSRusty Russell 	if (!dev)
893296f96fcSRusty Russell 		return -ENOMEM;
894296f96fcSRusty Russell 
895296f96fcSRusty Russell 	/* Set up network device as normal. */
89676288b4eSStephen Hemminger 	dev->netdev_ops = &virtnet_netdev;
897296f96fcSRusty Russell 	dev->features = NETIF_F_HIGHDMA;
898a9ea3fc6SHerbert Xu 	SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops);
899296f96fcSRusty Russell 	SET_NETDEV_DEV(dev, &vdev->dev);
900296f96fcSRusty Russell 
901296f96fcSRusty Russell 	/* Do we support "hardware" checksums? */
90298e778c9SMichał Mirosław 	if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
903296f96fcSRusty Russell 		/* This opens up the world of extra features. */
90498e778c9SMichał Mirosław 		dev->hw_features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
90598e778c9SMichał Mirosław 		if (csum)
906296f96fcSRusty Russell 			dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
90798e778c9SMichał Mirosław 
90898e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
90998e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_TSO | NETIF_F_UFO
91034a48579SRusty Russell 				| NETIF_F_TSO_ECN | NETIF_F_TSO6;
91134a48579SRusty Russell 		}
9125539ae96SRusty Russell 		/* Individual feature bits: what can host handle? */
91398e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
91498e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_TSO;
91598e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
91698e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_TSO6;
91798e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
91898e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_TSO_ECN;
91998e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
92098e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_UFO;
92198e778c9SMichał Mirosław 
92298e778c9SMichał Mirosław 		if (gso)
92398e778c9SMichał Mirosław 			dev->features |= dev->hw_features & (NETIF_F_ALL_TSO|NETIF_F_UFO);
92498e778c9SMichał Mirosław 		/* (!csum && gso) case will be fixed by register_netdev() */
925296f96fcSRusty Russell 	}
926296f96fcSRusty Russell 
927296f96fcSRusty Russell 	/* Configuration may specify what MAC to use.  Otherwise random. */
928c45a6816SRusty Russell 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
929a586d4f6SRusty Russell 		vdev->config->get(vdev,
930a586d4f6SRusty Russell 				  offsetof(struct virtio_net_config, mac),
931a586d4f6SRusty Russell 				  dev->dev_addr, dev->addr_len);
93262994b2dSAlex Williamson 	} else
933296f96fcSRusty Russell 		random_ether_addr(dev->dev_addr);
934296f96fcSRusty Russell 
935296f96fcSRusty Russell 	/* Set up our device-specific information */
936296f96fcSRusty Russell 	vi = netdev_priv(dev);
9376c0cd7c0SDor Laor 	netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight);
938296f96fcSRusty Russell 	vi->dev = dev;
939296f96fcSRusty Russell 	vi->vdev = vdev;
940d9d5dcc8SChristian Borntraeger 	vdev->priv = vi;
941fb6813f4SRusty Russell 	vi->pages = NULL;
9423161e453SRusty Russell 	INIT_DELAYED_WORK(&vi->refill, refill_work);
9435e01d2f9SMichael S. Tsirkin 	sg_init_table(vi->rx_sg, ARRAY_SIZE(vi->rx_sg));
9445e01d2f9SMichael S. Tsirkin 	sg_init_table(vi->tx_sg, ARRAY_SIZE(vi->tx_sg));
945296f96fcSRusty Russell 
94697402b96SHerbert Xu 	/* If we can receive ANY GSO packets, we must allocate large ones. */
9478e95a202SJoe Perches 	if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
9488e95a202SJoe Perches 	    virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
9498e95a202SJoe Perches 	    virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN))
95097402b96SHerbert Xu 		vi->big_packets = true;
95197402b96SHerbert Xu 
9523f2c31d9SMark McLoughlin 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
9533f2c31d9SMark McLoughlin 		vi->mergeable_rx_bufs = true;
9543f2c31d9SMark McLoughlin 
955d2a7dddaSMichael S. Tsirkin 	/* We expect two virtqueues, receive then send,
956d2a7dddaSMichael S. Tsirkin 	 * and optionally control. */
957d2a7dddaSMichael S. Tsirkin 	nvqs = virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) ? 3 : 2;
958296f96fcSRusty Russell 
959d2a7dddaSMichael S. Tsirkin 	err = vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names);
960d2a7dddaSMichael S. Tsirkin 	if (err)
961d2a7dddaSMichael S. Tsirkin 		goto free;
962d2a7dddaSMichael S. Tsirkin 
963d2a7dddaSMichael S. Tsirkin 	vi->rvq = vqs[0];
964d2a7dddaSMichael S. Tsirkin 	vi->svq = vqs[1];
965296f96fcSRusty Russell 
9662a41f71dSAlex Williamson 	if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)) {
967d2a7dddaSMichael S. Tsirkin 		vi->cvq = vqs[2];
9680bde9569SAlex Williamson 
9690bde9569SAlex Williamson 		if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
9700bde9569SAlex Williamson 			dev->features |= NETIF_F_HW_VLAN_FILTER;
9712a41f71dSAlex Williamson 	}
9722a41f71dSAlex Williamson 
973296f96fcSRusty Russell 	err = register_netdev(dev);
974296f96fcSRusty Russell 	if (err) {
975296f96fcSRusty Russell 		pr_debug("virtio_net: registering device failed\n");
976d2a7dddaSMichael S. Tsirkin 		goto free_vqs;
977296f96fcSRusty Russell 	}
978b3369c1fSRusty Russell 
979b3369c1fSRusty Russell 	/* Last of all, set up some receive buffers. */
9803161e453SRusty Russell 	try_fill_recv(vi, GFP_KERNEL);
981b3369c1fSRusty Russell 
982b3369c1fSRusty Russell 	/* If we didn't even get one input buffer, we're useless. */
983b3369c1fSRusty Russell 	if (vi->num == 0) {
984b3369c1fSRusty Russell 		err = -ENOMEM;
985b3369c1fSRusty Russell 		goto unregister;
986b3369c1fSRusty Russell 	}
987b3369c1fSRusty Russell 
988167c25e4SJason Wang 	/* Assume link up if device can't report link status,
989167c25e4SJason Wang 	   otherwise get link status from config. */
990167c25e4SJason Wang 	if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
991167c25e4SJason Wang 		netif_carrier_off(dev);
9929f4d26d0SMark McLoughlin 		virtnet_update_status(vi);
993167c25e4SJason Wang 	} else {
994167c25e4SJason Wang 		vi->status = VIRTIO_NET_S_LINK_UP;
9954783256eSPantelis Koukousoulas 		netif_carrier_on(dev);
996167c25e4SJason Wang 	}
9979f4d26d0SMark McLoughlin 
998296f96fcSRusty Russell 	pr_debug("virtnet: registered device %s\n", dev->name);
999296f96fcSRusty Russell 	return 0;
1000296f96fcSRusty Russell 
1001b3369c1fSRusty Russell unregister:
1002b3369c1fSRusty Russell 	unregister_netdev(dev);
10033161e453SRusty Russell 	cancel_delayed_work_sync(&vi->refill);
1004d2a7dddaSMichael S. Tsirkin free_vqs:
1005d2a7dddaSMichael S. Tsirkin 	vdev->config->del_vqs(vdev);
1006296f96fcSRusty Russell free:
1007296f96fcSRusty Russell 	free_netdev(dev);
1008296f96fcSRusty Russell 	return err;
1009296f96fcSRusty Russell }
1010296f96fcSRusty Russell 
10119ab86bbcSShirley Ma static void free_unused_bufs(struct virtnet_info *vi)
10129ab86bbcSShirley Ma {
10139ab86bbcSShirley Ma 	void *buf;
10149ab86bbcSShirley Ma 	while (1) {
10151915a712SMichael S. Tsirkin 		buf = virtqueue_detach_unused_buf(vi->svq);
1016830a8a97SShirley Ma 		if (!buf)
1017830a8a97SShirley Ma 			break;
1018830a8a97SShirley Ma 		dev_kfree_skb(buf);
1019830a8a97SShirley Ma 	}
1020830a8a97SShirley Ma 	while (1) {
10211915a712SMichael S. Tsirkin 		buf = virtqueue_detach_unused_buf(vi->rvq);
10229ab86bbcSShirley Ma 		if (!buf)
10239ab86bbcSShirley Ma 			break;
10249ab86bbcSShirley Ma 		if (vi->mergeable_rx_bufs || vi->big_packets)
10259ab86bbcSShirley Ma 			give_pages(vi, buf);
10269ab86bbcSShirley Ma 		else
10279ab86bbcSShirley Ma 			dev_kfree_skb(buf);
10289ab86bbcSShirley Ma 		--vi->num;
10299ab86bbcSShirley Ma 	}
10309ab86bbcSShirley Ma 	BUG_ON(vi->num != 0);
10319ab86bbcSShirley Ma }
10329ab86bbcSShirley Ma 
10333d1285beSUwe Kleine-König static void __devexit virtnet_remove(struct virtio_device *vdev)
1034296f96fcSRusty Russell {
103574b2553fSRusty Russell 	struct virtnet_info *vi = vdev->priv;
1036b3369c1fSRusty Russell 
10376e5aa7efSRusty Russell 	/* Stop all the virtqueues. */
10386e5aa7efSRusty Russell 	vdev->config->reset(vdev);
10396e5aa7efSRusty Russell 
1040b3369c1fSRusty Russell 
104174b2553fSRusty Russell 	unregister_netdev(vi->dev);
10423161e453SRusty Russell 	cancel_delayed_work_sync(&vi->refill);
1043830a8a97SShirley Ma 
1044830a8a97SShirley Ma 	/* Free unused buffers in both send and recv, if any. */
10459ab86bbcSShirley Ma 	free_unused_bufs(vi);
1046fb6813f4SRusty Russell 
1047d2a7dddaSMichael S. Tsirkin 	vdev->config->del_vqs(vi->vdev);
1048d2a7dddaSMichael S. Tsirkin 
1049fb6813f4SRusty Russell 	while (vi->pages)
1050fb6813f4SRusty Russell 		__free_pages(get_a_page(vi, GFP_KERNEL), 0);
1051fb6813f4SRusty Russell 
105274b2553fSRusty Russell 	free_netdev(vi->dev);
1053296f96fcSRusty Russell }
1054296f96fcSRusty Russell 
1055296f96fcSRusty Russell static struct virtio_device_id id_table[] = {
1056296f96fcSRusty Russell 	{ VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
1057296f96fcSRusty Russell 	{ 0 },
1058296f96fcSRusty Russell };
1059296f96fcSRusty Russell 
1060c45a6816SRusty Russell static unsigned int features[] = {
10615e4fe5c4SMark McLoughlin 	VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM,
10625e4fe5c4SMark McLoughlin 	VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
1063c45a6816SRusty Russell 	VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
106497402b96SHerbert Xu 	VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
10655c516751SSridhar Samudrala 	VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO,
10662a41f71dSAlex Williamson 	VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ,
10670bde9569SAlex Williamson 	VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN,
1068c45a6816SRusty Russell };
1069c45a6816SRusty Russell 
107022402529SUwe Kleine-König static struct virtio_driver virtio_net_driver = {
1071c45a6816SRusty Russell 	.feature_table = features,
1072c45a6816SRusty Russell 	.feature_table_size = ARRAY_SIZE(features),
1073296f96fcSRusty Russell 	.driver.name =	KBUILD_MODNAME,
1074296f96fcSRusty Russell 	.driver.owner =	THIS_MODULE,
1075296f96fcSRusty Russell 	.id_table =	id_table,
1076296f96fcSRusty Russell 	.probe =	virtnet_probe,
1077296f96fcSRusty Russell 	.remove =	__devexit_p(virtnet_remove),
10789f4d26d0SMark McLoughlin 	.config_changed = virtnet_config_changed,
1079296f96fcSRusty Russell };
1080296f96fcSRusty Russell 
1081296f96fcSRusty Russell static int __init init(void)
1082296f96fcSRusty Russell {
108322402529SUwe Kleine-König 	return register_virtio_driver(&virtio_net_driver);
1084296f96fcSRusty Russell }
1085296f96fcSRusty Russell 
1086296f96fcSRusty Russell static void __exit fini(void)
1087296f96fcSRusty Russell {
108822402529SUwe Kleine-König 	unregister_virtio_driver(&virtio_net_driver);
1089296f96fcSRusty Russell }
1090296f96fcSRusty Russell module_init(init);
1091296f96fcSRusty Russell module_exit(fini);
1092296f96fcSRusty Russell 
1093296f96fcSRusty Russell MODULE_DEVICE_TABLE(virtio, id_table);
1094296f96fcSRusty Russell MODULE_DESCRIPTION("Virtio network driver");
1095296f96fcSRusty Russell MODULE_LICENSE("GPL");
1096