xref: /openbmc/linux/drivers/net/virtio_net.c (revision 363f15149cfba67d29f1e6a6103dda079f27f3fa)
1296f96fcSRusty Russell /* A simple 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>
22296f96fcSRusty Russell #include <linux/module.h>
23296f96fcSRusty Russell #include <linux/virtio.h>
24296f96fcSRusty Russell #include <linux/virtio_net.h>
25296f96fcSRusty Russell #include <linux/scatterlist.h>
26296f96fcSRusty Russell 
276c0cd7c0SDor Laor static int napi_weight = 128;
286c0cd7c0SDor Laor module_param(napi_weight, int, 0444);
296c0cd7c0SDor Laor 
3034a48579SRusty Russell static int csum = 1, gso = 1;
3134a48579SRusty Russell module_param(csum, bool, 0444);
3234a48579SRusty Russell module_param(gso, bool, 0444);
3334a48579SRusty Russell 
34296f96fcSRusty Russell /* FIXME: MTU in config. */
35296f96fcSRusty Russell #define MAX_PACKET_LEN (ETH_HLEN+ETH_DATA_LEN)
36296f96fcSRusty Russell 
37296f96fcSRusty Russell struct virtnet_info
38296f96fcSRusty Russell {
39296f96fcSRusty Russell 	struct virtio_device *vdev;
40296f96fcSRusty Russell 	struct virtqueue *rvq, *svq;
41296f96fcSRusty Russell 	struct net_device *dev;
42296f96fcSRusty Russell 	struct napi_struct napi;
43296f96fcSRusty Russell 
4499ffc696SRusty Russell 	/* The skb we couldn't send because buffers were full. */
4599ffc696SRusty Russell 	struct sk_buff *last_xmit_skb;
4699ffc696SRusty Russell 
47*363f1514SRusty Russell 	/* If we need to free in a timer, this is it. */
4814c998f0SMark McLoughlin 	struct timer_list xmit_free_timer;
4914c998f0SMark McLoughlin 
50296f96fcSRusty Russell 	/* Number of input buffers, and max we've ever had. */
51296f96fcSRusty Russell 	unsigned int num, max;
52296f96fcSRusty Russell 
5311a3a154SRusty Russell 	/* For cleaning up after transmission. */
5411a3a154SRusty Russell 	struct tasklet_struct tasklet;
55*363f1514SRusty Russell 	bool free_in_tasklet;
5611a3a154SRusty Russell 
57296f96fcSRusty Russell 	/* Receive & send queues. */
58296f96fcSRusty Russell 	struct sk_buff_head recv;
59296f96fcSRusty Russell 	struct sk_buff_head send;
60296f96fcSRusty Russell };
61296f96fcSRusty Russell 
62296f96fcSRusty Russell static inline struct virtio_net_hdr *skb_vnet_hdr(struct sk_buff *skb)
63296f96fcSRusty Russell {
64296f96fcSRusty Russell 	return (struct virtio_net_hdr *)skb->cb;
65296f96fcSRusty Russell }
66296f96fcSRusty Russell 
67296f96fcSRusty Russell static inline void vnet_hdr_to_sg(struct scatterlist *sg, struct sk_buff *skb)
68296f96fcSRusty Russell {
69296f96fcSRusty Russell 	sg_init_one(sg, skb_vnet_hdr(skb), sizeof(struct virtio_net_hdr));
70296f96fcSRusty Russell }
71296f96fcSRusty Russell 
722cb9c6baSRusty Russell static void skb_xmit_done(struct virtqueue *svq)
73296f96fcSRusty Russell {
742cb9c6baSRusty Russell 	struct virtnet_info *vi = svq->vdev->priv;
75296f96fcSRusty Russell 
762cb9c6baSRusty Russell 	/* Suppress further interrupts. */
772cb9c6baSRusty Russell 	svq->vq_ops->disable_cb(svq);
7811a3a154SRusty Russell 
79*363f1514SRusty Russell 	/* We were probably waiting for more output buffers. */
80296f96fcSRusty Russell 	netif_wake_queue(vi->dev);
8111a3a154SRusty Russell 
8211a3a154SRusty Russell 	/* Make sure we re-xmit last_xmit_skb: if there are no more packets
8311a3a154SRusty Russell 	 * queued, start_xmit won't be called. */
8411a3a154SRusty Russell 	tasklet_schedule(&vi->tasklet);
85296f96fcSRusty Russell }
86296f96fcSRusty Russell 
87296f96fcSRusty Russell static void receive_skb(struct net_device *dev, struct sk_buff *skb,
88296f96fcSRusty Russell 			unsigned len)
89296f96fcSRusty Russell {
90296f96fcSRusty Russell 	struct virtio_net_hdr *hdr = skb_vnet_hdr(skb);
91296f96fcSRusty Russell 
92296f96fcSRusty Russell 	if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) {
93296f96fcSRusty Russell 		pr_debug("%s: short packet %i\n", dev->name, len);
94296f96fcSRusty Russell 		dev->stats.rx_length_errors++;
95296f96fcSRusty Russell 		goto drop;
96296f96fcSRusty Russell 	}
97296f96fcSRusty Russell 	len -= sizeof(struct virtio_net_hdr);
98296f96fcSRusty Russell 	BUG_ON(len > MAX_PACKET_LEN);
99296f96fcSRusty Russell 
100296f96fcSRusty Russell 	skb_trim(skb, len);
10123cde76dSMark McLoughlin 
102296f96fcSRusty Russell 	dev->stats.rx_bytes += skb->len;
103296f96fcSRusty Russell 	dev->stats.rx_packets++;
104296f96fcSRusty Russell 
105296f96fcSRusty Russell 	if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
106296f96fcSRusty Russell 		pr_debug("Needs csum!\n");
107f35d9d8aSRusty Russell 		if (!skb_partial_csum_set(skb,hdr->csum_start,hdr->csum_offset))
108296f96fcSRusty Russell 			goto frame_err;
109296f96fcSRusty Russell 	}
110296f96fcSRusty Russell 
11123cde76dSMark McLoughlin 	skb->protocol = eth_type_trans(skb, dev);
11223cde76dSMark McLoughlin 	pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
11323cde76dSMark McLoughlin 		 ntohs(skb->protocol), skb->len, skb->pkt_type);
11423cde76dSMark McLoughlin 
115296f96fcSRusty Russell 	if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
116296f96fcSRusty Russell 		pr_debug("GSO!\n");
11734a48579SRusty Russell 		switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
118296f96fcSRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV4:
119296f96fcSRusty Russell 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
120296f96fcSRusty Russell 			break;
121296f96fcSRusty Russell 		case VIRTIO_NET_HDR_GSO_UDP:
122296f96fcSRusty Russell 			skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
123296f96fcSRusty Russell 			break;
124296f96fcSRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV6:
125296f96fcSRusty Russell 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
126296f96fcSRusty Russell 			break;
127296f96fcSRusty Russell 		default:
128296f96fcSRusty Russell 			if (net_ratelimit())
129296f96fcSRusty Russell 				printk(KERN_WARNING "%s: bad gso type %u.\n",
130296f96fcSRusty Russell 				       dev->name, hdr->gso_type);
131296f96fcSRusty Russell 			goto frame_err;
132296f96fcSRusty Russell 		}
133296f96fcSRusty Russell 
13434a48579SRusty Russell 		if (hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN)
13534a48579SRusty Russell 			skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
13634a48579SRusty Russell 
137296f96fcSRusty Russell 		skb_shinfo(skb)->gso_size = hdr->gso_size;
138296f96fcSRusty Russell 		if (skb_shinfo(skb)->gso_size == 0) {
139296f96fcSRusty Russell 			if (net_ratelimit())
140296f96fcSRusty Russell 				printk(KERN_WARNING "%s: zero gso size.\n",
141296f96fcSRusty Russell 				       dev->name);
142296f96fcSRusty Russell 			goto frame_err;
143296f96fcSRusty Russell 		}
144296f96fcSRusty Russell 
145296f96fcSRusty Russell 		/* Header must be checked, and gso_segs computed. */
146296f96fcSRusty Russell 		skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
147296f96fcSRusty Russell 		skb_shinfo(skb)->gso_segs = 0;
148296f96fcSRusty Russell 	}
149296f96fcSRusty Russell 
150296f96fcSRusty Russell 	netif_receive_skb(skb);
151296f96fcSRusty Russell 	return;
152296f96fcSRusty Russell 
153296f96fcSRusty Russell frame_err:
154296f96fcSRusty Russell 	dev->stats.rx_frame_errors++;
155296f96fcSRusty Russell drop:
156296f96fcSRusty Russell 	dev_kfree_skb(skb);
157296f96fcSRusty Russell }
158296f96fcSRusty Russell 
159296f96fcSRusty Russell static void try_fill_recv(struct virtnet_info *vi)
160296f96fcSRusty Russell {
161296f96fcSRusty Russell 	struct sk_buff *skb;
16205271685SRusty Russell 	struct scatterlist sg[2+MAX_SKB_FRAGS];
163296f96fcSRusty Russell 	int num, err;
164296f96fcSRusty Russell 
16505271685SRusty Russell 	sg_init_table(sg, 2+MAX_SKB_FRAGS);
166296f96fcSRusty Russell 	for (;;) {
167296f96fcSRusty Russell 		skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN);
168296f96fcSRusty Russell 		if (unlikely(!skb))
169296f96fcSRusty Russell 			break;
170296f96fcSRusty Russell 
171296f96fcSRusty Russell 		skb_put(skb, MAX_PACKET_LEN);
172296f96fcSRusty Russell 		vnet_hdr_to_sg(sg, skb);
173296f96fcSRusty Russell 		num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
174296f96fcSRusty Russell 		skb_queue_head(&vi->recv, skb);
175296f96fcSRusty Russell 
176296f96fcSRusty Russell 		err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, num, skb);
177296f96fcSRusty Russell 		if (err) {
178296f96fcSRusty Russell 			skb_unlink(skb, &vi->recv);
179296f96fcSRusty Russell 			kfree_skb(skb);
180296f96fcSRusty Russell 			break;
181296f96fcSRusty Russell 		}
182296f96fcSRusty Russell 		vi->num++;
183296f96fcSRusty Russell 	}
184296f96fcSRusty Russell 	if (unlikely(vi->num > vi->max))
185296f96fcSRusty Russell 		vi->max = vi->num;
186296f96fcSRusty Russell 	vi->rvq->vq_ops->kick(vi->rvq);
187296f96fcSRusty Russell }
188296f96fcSRusty Russell 
18918445c4dSRusty Russell static void skb_recv_done(struct virtqueue *rvq)
190296f96fcSRusty Russell {
191296f96fcSRusty Russell 	struct virtnet_info *vi = rvq->vdev->priv;
19218445c4dSRusty Russell 	/* Schedule NAPI, Suppress further interrupts if successful. */
19318445c4dSRusty Russell 	if (netif_rx_schedule_prep(vi->dev, &vi->napi)) {
19418445c4dSRusty Russell 		rvq->vq_ops->disable_cb(rvq);
19518445c4dSRusty Russell 		__netif_rx_schedule(vi->dev, &vi->napi);
19618445c4dSRusty Russell 	}
197296f96fcSRusty Russell }
198296f96fcSRusty Russell 
199296f96fcSRusty Russell static int virtnet_poll(struct napi_struct *napi, int budget)
200296f96fcSRusty Russell {
201296f96fcSRusty Russell 	struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi);
202296f96fcSRusty Russell 	struct sk_buff *skb = NULL;
203296f96fcSRusty Russell 	unsigned int len, received = 0;
204296f96fcSRusty Russell 
205296f96fcSRusty Russell again:
206296f96fcSRusty Russell 	while (received < budget &&
207296f96fcSRusty Russell 	       (skb = vi->rvq->vq_ops->get_buf(vi->rvq, &len)) != NULL) {
208296f96fcSRusty Russell 		__skb_unlink(skb, &vi->recv);
209296f96fcSRusty Russell 		receive_skb(vi->dev, skb, len);
210296f96fcSRusty Russell 		vi->num--;
211296f96fcSRusty Russell 		received++;
212296f96fcSRusty Russell 	}
213296f96fcSRusty Russell 
214296f96fcSRusty Russell 	/* FIXME: If we oom and completely run out of inbufs, we need
215296f96fcSRusty Russell 	 * to start a timer trying to fill more. */
216296f96fcSRusty Russell 	if (vi->num < vi->max / 2)
217296f96fcSRusty Russell 		try_fill_recv(vi);
218296f96fcSRusty Russell 
2198329d98eSRusty Russell 	/* Out of packets? */
2208329d98eSRusty Russell 	if (received < budget) {
221296f96fcSRusty Russell 		netif_rx_complete(vi->dev, napi);
22218445c4dSRusty Russell 		if (unlikely(!vi->rvq->vq_ops->enable_cb(vi->rvq))
2234265f161SChristian Borntraeger 		    && napi_schedule_prep(napi)) {
2244265f161SChristian Borntraeger 			vi->rvq->vq_ops->disable_cb(vi->rvq);
2254265f161SChristian Borntraeger 			__netif_rx_schedule(vi->dev, napi);
226296f96fcSRusty Russell 			goto again;
227296f96fcSRusty Russell 		}
2284265f161SChristian Borntraeger 	}
229296f96fcSRusty Russell 
230296f96fcSRusty Russell 	return received;
231296f96fcSRusty Russell }
232296f96fcSRusty Russell 
233296f96fcSRusty Russell static void free_old_xmit_skbs(struct virtnet_info *vi)
234296f96fcSRusty Russell {
235296f96fcSRusty Russell 	struct sk_buff *skb;
236296f96fcSRusty Russell 	unsigned int len;
237296f96fcSRusty Russell 
238296f96fcSRusty Russell 	while ((skb = vi->svq->vq_ops->get_buf(vi->svq, &len)) != NULL) {
239296f96fcSRusty Russell 		pr_debug("Sent skb %p\n", skb);
240296f96fcSRusty Russell 		__skb_unlink(skb, &vi->send);
241655aa31fSRusty Russell 		vi->dev->stats.tx_bytes += skb->len;
242296f96fcSRusty Russell 		vi->dev->stats.tx_packets++;
243296f96fcSRusty Russell 		kfree_skb(skb);
244296f96fcSRusty Russell 	}
245296f96fcSRusty Russell }
246296f96fcSRusty Russell 
247*363f1514SRusty Russell /* If the virtio transport doesn't always notify us when all in-flight packets
248*363f1514SRusty Russell  * are consumed, we fall back to using this function on a timer to free them. */
24914c998f0SMark McLoughlin static void xmit_free(unsigned long data)
25014c998f0SMark McLoughlin {
25114c998f0SMark McLoughlin 	struct virtnet_info *vi = (void *)data;
25214c998f0SMark McLoughlin 
25314c998f0SMark McLoughlin 	netif_tx_lock(vi->dev);
25414c998f0SMark McLoughlin 
25514c998f0SMark McLoughlin 	free_old_xmit_skbs(vi);
25614c998f0SMark McLoughlin 
25714c998f0SMark McLoughlin 	if (!skb_queue_empty(&vi->send))
25814c998f0SMark McLoughlin 		mod_timer(&vi->xmit_free_timer, jiffies + (HZ/10));
25914c998f0SMark McLoughlin 
26014c998f0SMark McLoughlin 	netif_tx_unlock(vi->dev);
26114c998f0SMark McLoughlin }
26214c998f0SMark McLoughlin 
26399ffc696SRusty Russell static int xmit_skb(struct virtnet_info *vi, struct sk_buff *skb)
264296f96fcSRusty Russell {
26514c998f0SMark McLoughlin 	int num, err;
26605271685SRusty Russell 	struct scatterlist sg[2+MAX_SKB_FRAGS];
267296f96fcSRusty Russell 	struct virtio_net_hdr *hdr;
268296f96fcSRusty Russell 	const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
269296f96fcSRusty Russell 
27005271685SRusty Russell 	sg_init_table(sg, 2+MAX_SKB_FRAGS);
2714d125de3SRusty Russell 
27299ffc696SRusty Russell 	pr_debug("%s: xmit %p " MAC_FMT "\n", vi->dev->name, skb,
27321f644f3SDavid S. Miller 		 dest[0], dest[1], dest[2],
27421f644f3SDavid S. Miller 		 dest[3], dest[4], dest[5]);
275296f96fcSRusty Russell 
276296f96fcSRusty Russell 	/* Encode metadata header at front. */
277296f96fcSRusty Russell 	hdr = skb_vnet_hdr(skb);
278296f96fcSRusty Russell 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
279296f96fcSRusty Russell 		hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
280296f96fcSRusty Russell 		hdr->csum_start = skb->csum_start - skb_headroom(skb);
281296f96fcSRusty Russell 		hdr->csum_offset = skb->csum_offset;
282296f96fcSRusty Russell 	} else {
283296f96fcSRusty Russell 		hdr->flags = 0;
284296f96fcSRusty Russell 		hdr->csum_offset = hdr->csum_start = 0;
285296f96fcSRusty Russell 	}
286296f96fcSRusty Russell 
287296f96fcSRusty Russell 	if (skb_is_gso(skb)) {
28850c8ea80SRusty Russell 		hdr->hdr_len = skb_transport_header(skb) - skb->data;
289296f96fcSRusty Russell 		hdr->gso_size = skb_shinfo(skb)->gso_size;
29034a48579SRusty Russell 		if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
291296f96fcSRusty Russell 			hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
292296f96fcSRusty Russell 		else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
293296f96fcSRusty Russell 			hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
294296f96fcSRusty Russell 		else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
295296f96fcSRusty Russell 			hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
296296f96fcSRusty Russell 		else
297296f96fcSRusty Russell 			BUG();
29834a48579SRusty Russell 		if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
29934a48579SRusty Russell 			hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
300296f96fcSRusty Russell 	} else {
301296f96fcSRusty Russell 		hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
30250c8ea80SRusty Russell 		hdr->gso_size = hdr->hdr_len = 0;
303296f96fcSRusty Russell 	}
304296f96fcSRusty Russell 
305296f96fcSRusty Russell 	vnet_hdr_to_sg(sg, skb);
306296f96fcSRusty Russell 	num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
30799ffc696SRusty Russell 
30814c998f0SMark McLoughlin 	err = vi->svq->vq_ops->add_buf(vi->svq, sg, num, 0, skb);
309*363f1514SRusty Russell 	if (!err && !vi->free_in_tasklet)
31014c998f0SMark McLoughlin 		mod_timer(&vi->xmit_free_timer, jiffies + (HZ/10));
31114c998f0SMark McLoughlin 
31214c998f0SMark McLoughlin 	return err;
31399ffc696SRusty Russell }
31499ffc696SRusty Russell 
31511a3a154SRusty Russell static void xmit_tasklet(unsigned long data)
31611a3a154SRusty Russell {
31711a3a154SRusty Russell 	struct virtnet_info *vi = (void *)data;
31811a3a154SRusty Russell 
31911a3a154SRusty Russell 	netif_tx_lock_bh(vi->dev);
32011a3a154SRusty Russell 	if (vi->last_xmit_skb && xmit_skb(vi, vi->last_xmit_skb) == 0) {
32111a3a154SRusty Russell 		vi->svq->vq_ops->kick(vi->svq);
32211a3a154SRusty Russell 		vi->last_xmit_skb = NULL;
32311a3a154SRusty Russell 	}
324*363f1514SRusty Russell 	if (vi->free_in_tasklet)
325*363f1514SRusty Russell 		free_old_xmit_skbs(vi);
32611a3a154SRusty Russell 	netif_tx_unlock_bh(vi->dev);
32711a3a154SRusty Russell }
32811a3a154SRusty Russell 
32999ffc696SRusty Russell static int start_xmit(struct sk_buff *skb, struct net_device *dev)
33099ffc696SRusty Russell {
33199ffc696SRusty Russell 	struct virtnet_info *vi = netdev_priv(dev);
3322cb9c6baSRusty Russell 
3332cb9c6baSRusty Russell again:
3342cb9c6baSRusty Russell 	/* Free up any pending old buffers before queueing new ones. */
3352cb9c6baSRusty Russell 	free_old_xmit_skbs(vi);
33699ffc696SRusty Russell 
33799ffc696SRusty Russell 	/* If we has a buffer left over from last time, send it now. */
3387eb2e251SRusty Russell 	if (unlikely(vi->last_xmit_skb)) {
33999ffc696SRusty Russell 		if (xmit_skb(vi, vi->last_xmit_skb) != 0) {
34099ffc696SRusty Russell 			/* Drop this skb: we only queue one. */
34199ffc696SRusty Russell 			vi->dev->stats.tx_dropped++;
34299ffc696SRusty Russell 			kfree_skb(skb);
3437eb2e251SRusty Russell 			skb = NULL;
34499ffc696SRusty Russell 			goto stop_queue;
34599ffc696SRusty Russell 		}
34699ffc696SRusty Russell 		vi->last_xmit_skb = NULL;
34799ffc696SRusty Russell 	}
34899ffc696SRusty Russell 
34999ffc696SRusty Russell 	/* Put new one in send queue and do transmit */
3507eb2e251SRusty Russell 	if (likely(skb)) {
35199ffc696SRusty Russell 		__skb_queue_head(&vi->send, skb);
35299ffc696SRusty Russell 		if (xmit_skb(vi, skb) != 0) {
35399ffc696SRusty Russell 			vi->last_xmit_skb = skb;
3547eb2e251SRusty Russell 			skb = NULL;
35599ffc696SRusty Russell 			goto stop_queue;
35699ffc696SRusty Russell 		}
3577eb2e251SRusty Russell 	}
35899ffc696SRusty Russell done:
35999ffc696SRusty Russell 	vi->svq->vq_ops->kick(vi->svq);
36099ffc696SRusty Russell 	return NETDEV_TX_OK;
36199ffc696SRusty Russell 
36299ffc696SRusty Russell stop_queue:
363296f96fcSRusty Russell 	pr_debug("%s: virtio not prepared to send\n", dev->name);
364296f96fcSRusty Russell 	netif_stop_queue(dev);
3652cb9c6baSRusty Russell 
3664265f161SChristian Borntraeger 	/* Activate callback for using skbs: if this returns false it
3672cb9c6baSRusty Russell 	 * means some were used in the meantime. */
3682cb9c6baSRusty Russell 	if (unlikely(!vi->svq->vq_ops->enable_cb(vi->svq))) {
3694265f161SChristian Borntraeger 		vi->svq->vq_ops->disable_cb(vi->svq);
3702cb9c6baSRusty Russell 		netif_start_queue(dev);
3712cb9c6baSRusty Russell 		goto again;
3722cb9c6baSRusty Russell 	}
37399ffc696SRusty Russell 	goto done;
374296f96fcSRusty Russell }
375296f96fcSRusty Russell 
376da74e89dSAmit Shah #ifdef CONFIG_NET_POLL_CONTROLLER
377da74e89dSAmit Shah static void virtnet_netpoll(struct net_device *dev)
378da74e89dSAmit Shah {
379da74e89dSAmit Shah 	struct virtnet_info *vi = netdev_priv(dev);
380da74e89dSAmit Shah 
381da74e89dSAmit Shah 	napi_schedule(&vi->napi);
382da74e89dSAmit Shah }
383da74e89dSAmit Shah #endif
384da74e89dSAmit Shah 
385296f96fcSRusty Russell static int virtnet_open(struct net_device *dev)
386296f96fcSRusty Russell {
387296f96fcSRusty Russell 	struct virtnet_info *vi = netdev_priv(dev);
388296f96fcSRusty Russell 
389296f96fcSRusty Russell 	napi_enable(&vi->napi);
390a48bd8f6SRusty Russell 
391a48bd8f6SRusty Russell 	/* If all buffers were filled by other side before we napi_enabled, we
392a48bd8f6SRusty Russell 	 * won't get another interrupt, so process any outstanding packets
393370076d9SChristian Borntraeger 	 * now.  virtnet_poll wants re-enable the queue, so we disable here.
394370076d9SChristian Borntraeger 	 * We synchronize against interrupts via NAPI_STATE_SCHED */
395370076d9SChristian Borntraeger 	if (netif_rx_schedule_prep(dev, &vi->napi)) {
396a48bd8f6SRusty Russell 		vi->rvq->vq_ops->disable_cb(vi->rvq);
397370076d9SChristian Borntraeger 		__netif_rx_schedule(dev, &vi->napi);
398370076d9SChristian Borntraeger 	}
399296f96fcSRusty Russell 	return 0;
400296f96fcSRusty Russell }
401296f96fcSRusty Russell 
402296f96fcSRusty Russell static int virtnet_close(struct net_device *dev)
403296f96fcSRusty Russell {
404296f96fcSRusty Russell 	struct virtnet_info *vi = netdev_priv(dev);
405296f96fcSRusty Russell 
406296f96fcSRusty Russell 	napi_disable(&vi->napi);
407296f96fcSRusty Russell 
408296f96fcSRusty Russell 	return 0;
409296f96fcSRusty Russell }
410296f96fcSRusty Russell 
411296f96fcSRusty Russell static int virtnet_probe(struct virtio_device *vdev)
412296f96fcSRusty Russell {
413296f96fcSRusty Russell 	int err;
414296f96fcSRusty Russell 	struct net_device *dev;
415296f96fcSRusty Russell 	struct virtnet_info *vi;
416296f96fcSRusty Russell 
417296f96fcSRusty Russell 	/* Allocate ourselves a network device with room for our info */
418296f96fcSRusty Russell 	dev = alloc_etherdev(sizeof(struct virtnet_info));
419296f96fcSRusty Russell 	if (!dev)
420296f96fcSRusty Russell 		return -ENOMEM;
421296f96fcSRusty Russell 
422296f96fcSRusty Russell 	/* Set up network device as normal. */
423296f96fcSRusty Russell 	dev->open = virtnet_open;
424296f96fcSRusty Russell 	dev->stop = virtnet_close;
425296f96fcSRusty Russell 	dev->hard_start_xmit = start_xmit;
426296f96fcSRusty Russell 	dev->features = NETIF_F_HIGHDMA;
427da74e89dSAmit Shah #ifdef CONFIG_NET_POLL_CONTROLLER
428da74e89dSAmit Shah 	dev->poll_controller = virtnet_netpoll;
429da74e89dSAmit Shah #endif
430296f96fcSRusty Russell 	SET_NETDEV_DEV(dev, &vdev->dev);
431296f96fcSRusty Russell 
432296f96fcSRusty Russell 	/* Do we support "hardware" checksums? */
433c45a6816SRusty Russell 	if (csum && virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
434296f96fcSRusty Russell 		/* This opens up the world of extra features. */
435296f96fcSRusty Russell 		dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
436c45a6816SRusty Russell 		if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
43734a48579SRusty Russell 			dev->features |= NETIF_F_TSO | NETIF_F_UFO
43834a48579SRusty Russell 				| NETIF_F_TSO_ECN | NETIF_F_TSO6;
43934a48579SRusty Russell 		}
4405539ae96SRusty Russell 		/* Individual feature bits: what can host handle? */
441c45a6816SRusty Russell 		if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
4425539ae96SRusty Russell 			dev->features |= NETIF_F_TSO;
443c45a6816SRusty Russell 		if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
4445539ae96SRusty Russell 			dev->features |= NETIF_F_TSO6;
445c45a6816SRusty Russell 		if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
4465539ae96SRusty Russell 			dev->features |= NETIF_F_TSO_ECN;
447c45a6816SRusty Russell 		if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
4485539ae96SRusty Russell 			dev->features |= NETIF_F_UFO;
449296f96fcSRusty Russell 	}
450296f96fcSRusty Russell 
451296f96fcSRusty Russell 	/* Configuration may specify what MAC to use.  Otherwise random. */
452c45a6816SRusty Russell 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
453a586d4f6SRusty Russell 		vdev->config->get(vdev,
454a586d4f6SRusty Russell 				  offsetof(struct virtio_net_config, mac),
455a586d4f6SRusty Russell 				  dev->dev_addr, dev->addr_len);
456296f96fcSRusty Russell 	} else
457296f96fcSRusty Russell 		random_ether_addr(dev->dev_addr);
458296f96fcSRusty Russell 
459296f96fcSRusty Russell 	/* Set up our device-specific information */
460296f96fcSRusty Russell 	vi = netdev_priv(dev);
4616c0cd7c0SDor Laor 	netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight);
462296f96fcSRusty Russell 	vi->dev = dev;
463296f96fcSRusty Russell 	vi->vdev = vdev;
464d9d5dcc8SChristian Borntraeger 	vdev->priv = vi;
465296f96fcSRusty Russell 
466*363f1514SRusty Russell 	/* If they give us a callback when all buffers are done, we don't need
467*363f1514SRusty Russell 	 * the timer. */
468*363f1514SRusty Russell 	vi->free_in_tasklet = virtio_has_feature(vdev,VIRTIO_F_NOTIFY_ON_EMPTY);
469*363f1514SRusty Russell 
470296f96fcSRusty Russell 	/* We expect two virtqueues, receive then send. */
471a586d4f6SRusty Russell 	vi->rvq = vdev->config->find_vq(vdev, 0, skb_recv_done);
472296f96fcSRusty Russell 	if (IS_ERR(vi->rvq)) {
473296f96fcSRusty Russell 		err = PTR_ERR(vi->rvq);
474296f96fcSRusty Russell 		goto free;
475296f96fcSRusty Russell 	}
476296f96fcSRusty Russell 
477a586d4f6SRusty Russell 	vi->svq = vdev->config->find_vq(vdev, 1, skb_xmit_done);
478296f96fcSRusty Russell 	if (IS_ERR(vi->svq)) {
479296f96fcSRusty Russell 		err = PTR_ERR(vi->svq);
480296f96fcSRusty Russell 		goto free_recv;
481296f96fcSRusty Russell 	}
482296f96fcSRusty Russell 
483296f96fcSRusty Russell 	/* Initialize our empty receive and send queues. */
484296f96fcSRusty Russell 	skb_queue_head_init(&vi->recv);
485296f96fcSRusty Russell 	skb_queue_head_init(&vi->send);
486296f96fcSRusty Russell 
48711a3a154SRusty Russell 	tasklet_init(&vi->tasklet, xmit_tasklet, (unsigned long)vi);
48811a3a154SRusty Russell 
489*363f1514SRusty Russell 	if (!vi->free_in_tasklet)
49014c998f0SMark McLoughlin 		setup_timer(&vi->xmit_free_timer, xmit_free, (unsigned long)vi);
49114c998f0SMark McLoughlin 
492296f96fcSRusty Russell 	err = register_netdev(dev);
493296f96fcSRusty Russell 	if (err) {
494296f96fcSRusty Russell 		pr_debug("virtio_net: registering device failed\n");
495296f96fcSRusty Russell 		goto free_send;
496296f96fcSRusty Russell 	}
497b3369c1fSRusty Russell 
498b3369c1fSRusty Russell 	/* Last of all, set up some receive buffers. */
499b3369c1fSRusty Russell 	try_fill_recv(vi);
500b3369c1fSRusty Russell 
501b3369c1fSRusty Russell 	/* If we didn't even get one input buffer, we're useless. */
502b3369c1fSRusty Russell 	if (vi->num == 0) {
503b3369c1fSRusty Russell 		err = -ENOMEM;
504b3369c1fSRusty Russell 		goto unregister;
505b3369c1fSRusty Russell 	}
506b3369c1fSRusty Russell 
507296f96fcSRusty Russell 	pr_debug("virtnet: registered device %s\n", dev->name);
508296f96fcSRusty Russell 	return 0;
509296f96fcSRusty Russell 
510b3369c1fSRusty Russell unregister:
511b3369c1fSRusty Russell 	unregister_netdev(dev);
512296f96fcSRusty Russell free_send:
513296f96fcSRusty Russell 	vdev->config->del_vq(vi->svq);
514296f96fcSRusty Russell free_recv:
515296f96fcSRusty Russell 	vdev->config->del_vq(vi->rvq);
516296f96fcSRusty Russell free:
517296f96fcSRusty Russell 	free_netdev(dev);
518296f96fcSRusty Russell 	return err;
519296f96fcSRusty Russell }
520296f96fcSRusty Russell 
521296f96fcSRusty Russell static void virtnet_remove(struct virtio_device *vdev)
522296f96fcSRusty Russell {
52374b2553fSRusty Russell 	struct virtnet_info *vi = vdev->priv;
524b3369c1fSRusty Russell 	struct sk_buff *skb;
525b3369c1fSRusty Russell 
5266e5aa7efSRusty Russell 	/* Stop all the virtqueues. */
5276e5aa7efSRusty Russell 	vdev->config->reset(vdev);
5286e5aa7efSRusty Russell 
529*363f1514SRusty Russell 	if (!vi->free_in_tasklet)
53014c998f0SMark McLoughlin 		del_timer_sync(&vi->xmit_free_timer);
53114c998f0SMark McLoughlin 
532b3369c1fSRusty Russell 	/* Free our skbs in send and recv queues, if any. */
533b3369c1fSRusty Russell 	while ((skb = __skb_dequeue(&vi->recv)) != NULL) {
534b3369c1fSRusty Russell 		kfree_skb(skb);
535b3369c1fSRusty Russell 		vi->num--;
536b3369c1fSRusty Russell 	}
537288369ccSWang Chen 	__skb_queue_purge(&vi->send);
538b3369c1fSRusty Russell 
539b3369c1fSRusty Russell 	BUG_ON(vi->num != 0);
54074b2553fSRusty Russell 
54174b2553fSRusty Russell 	vdev->config->del_vq(vi->svq);
54274b2553fSRusty Russell 	vdev->config->del_vq(vi->rvq);
54374b2553fSRusty Russell 	unregister_netdev(vi->dev);
54474b2553fSRusty Russell 	free_netdev(vi->dev);
545296f96fcSRusty Russell }
546296f96fcSRusty Russell 
547296f96fcSRusty Russell static struct virtio_device_id id_table[] = {
548296f96fcSRusty Russell 	{ VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
549296f96fcSRusty Russell 	{ 0 },
550296f96fcSRusty Russell };
551296f96fcSRusty Russell 
552c45a6816SRusty Russell static unsigned int features[] = {
553c45a6816SRusty Russell 	VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
554c45a6816SRusty Russell 	VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
555*363f1514SRusty Russell 	VIRTIO_NET_F_HOST_ECN, VIRTIO_F_NOTIFY_ON_EMPTY,
556c45a6816SRusty Russell };
557c45a6816SRusty Russell 
558296f96fcSRusty Russell static struct virtio_driver virtio_net = {
559c45a6816SRusty Russell 	.feature_table = features,
560c45a6816SRusty Russell 	.feature_table_size = ARRAY_SIZE(features),
561296f96fcSRusty Russell 	.driver.name =	KBUILD_MODNAME,
562296f96fcSRusty Russell 	.driver.owner =	THIS_MODULE,
563296f96fcSRusty Russell 	.id_table =	id_table,
564296f96fcSRusty Russell 	.probe =	virtnet_probe,
565296f96fcSRusty Russell 	.remove =	__devexit_p(virtnet_remove),
566296f96fcSRusty Russell };
567296f96fcSRusty Russell 
568296f96fcSRusty Russell static int __init init(void)
569296f96fcSRusty Russell {
570296f96fcSRusty Russell 	return register_virtio_driver(&virtio_net);
571296f96fcSRusty Russell }
572296f96fcSRusty Russell 
573296f96fcSRusty Russell static void __exit fini(void)
574296f96fcSRusty Russell {
575296f96fcSRusty Russell 	unregister_virtio_driver(&virtio_net);
576296f96fcSRusty Russell }
577296f96fcSRusty Russell module_init(init);
578296f96fcSRusty Russell module_exit(fini);
579296f96fcSRusty Russell 
580296f96fcSRusty Russell MODULE_DEVICE_TABLE(virtio, id_table);
581296f96fcSRusty Russell MODULE_DESCRIPTION("Virtio network driver");
582296f96fcSRusty Russell MODULE_LICENSE("GPL");
583