xref: /openbmc/linux/drivers/net/virtio_net.c (revision 97402b96f87c6e32f75f1bffdd91a5ee144b679d)
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>
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>
27296f96fcSRusty Russell 
286c0cd7c0SDor Laor static int napi_weight = 128;
296c0cd7c0SDor Laor module_param(napi_weight, int, 0444);
306c0cd7c0SDor Laor 
3134a48579SRusty Russell static int csum = 1, gso = 1;
3234a48579SRusty Russell module_param(csum, bool, 0444);
3334a48579SRusty Russell module_param(gso, bool, 0444);
3434a48579SRusty Russell 
35296f96fcSRusty Russell /* FIXME: MTU in config. */
36296f96fcSRusty Russell #define MAX_PACKET_LEN (ETH_HLEN+ETH_DATA_LEN)
37296f96fcSRusty Russell 
38296f96fcSRusty Russell struct virtnet_info
39296f96fcSRusty Russell {
40296f96fcSRusty Russell 	struct virtio_device *vdev;
41296f96fcSRusty Russell 	struct virtqueue *rvq, *svq;
42296f96fcSRusty Russell 	struct net_device *dev;
43296f96fcSRusty Russell 	struct napi_struct napi;
44296f96fcSRusty Russell 
4599ffc696SRusty Russell 	/* The skb we couldn't send because buffers were full. */
4699ffc696SRusty Russell 	struct sk_buff *last_xmit_skb;
4799ffc696SRusty Russell 
48363f1514SRusty Russell 	/* If we need to free in a timer, this is it. */
4914c998f0SMark McLoughlin 	struct timer_list xmit_free_timer;
5014c998f0SMark McLoughlin 
51296f96fcSRusty Russell 	/* Number of input buffers, and max we've ever had. */
52296f96fcSRusty Russell 	unsigned int num, max;
53296f96fcSRusty Russell 
5411a3a154SRusty Russell 	/* For cleaning up after transmission. */
5511a3a154SRusty Russell 	struct tasklet_struct tasklet;
56363f1514SRusty Russell 	bool free_in_tasklet;
5711a3a154SRusty Russell 
58*97402b96SHerbert Xu 	/* I like... big packets and I cannot lie! */
59*97402b96SHerbert Xu 	bool big_packets;
60*97402b96SHerbert Xu 
61296f96fcSRusty Russell 	/* Receive & send queues. */
62296f96fcSRusty Russell 	struct sk_buff_head recv;
63296f96fcSRusty Russell 	struct sk_buff_head send;
64296f96fcSRusty Russell };
65296f96fcSRusty Russell 
66296f96fcSRusty Russell static inline struct virtio_net_hdr *skb_vnet_hdr(struct sk_buff *skb)
67296f96fcSRusty Russell {
68296f96fcSRusty Russell 	return (struct virtio_net_hdr *)skb->cb;
69296f96fcSRusty Russell }
70296f96fcSRusty Russell 
71296f96fcSRusty Russell static inline void vnet_hdr_to_sg(struct scatterlist *sg, struct sk_buff *skb)
72296f96fcSRusty Russell {
73296f96fcSRusty Russell 	sg_init_one(sg, skb_vnet_hdr(skb), sizeof(struct virtio_net_hdr));
74296f96fcSRusty Russell }
75296f96fcSRusty Russell 
762cb9c6baSRusty Russell static void skb_xmit_done(struct virtqueue *svq)
77296f96fcSRusty Russell {
782cb9c6baSRusty Russell 	struct virtnet_info *vi = svq->vdev->priv;
79296f96fcSRusty Russell 
802cb9c6baSRusty Russell 	/* Suppress further interrupts. */
812cb9c6baSRusty Russell 	svq->vq_ops->disable_cb(svq);
8211a3a154SRusty Russell 
83363f1514SRusty Russell 	/* We were probably waiting for more output buffers. */
84296f96fcSRusty Russell 	netif_wake_queue(vi->dev);
8511a3a154SRusty Russell 
8611a3a154SRusty Russell 	/* Make sure we re-xmit last_xmit_skb: if there are no more packets
8711a3a154SRusty Russell 	 * queued, start_xmit won't be called. */
8811a3a154SRusty Russell 	tasklet_schedule(&vi->tasklet);
89296f96fcSRusty Russell }
90296f96fcSRusty Russell 
91296f96fcSRusty Russell static void receive_skb(struct net_device *dev, struct sk_buff *skb,
92296f96fcSRusty Russell 			unsigned len)
93296f96fcSRusty Russell {
94296f96fcSRusty Russell 	struct virtio_net_hdr *hdr = skb_vnet_hdr(skb);
95*97402b96SHerbert Xu 	int err;
96296f96fcSRusty Russell 
97296f96fcSRusty Russell 	if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) {
98296f96fcSRusty Russell 		pr_debug("%s: short packet %i\n", dev->name, len);
99296f96fcSRusty Russell 		dev->stats.rx_length_errors++;
100296f96fcSRusty Russell 		goto drop;
101296f96fcSRusty Russell 	}
102296f96fcSRusty Russell 	len -= sizeof(struct virtio_net_hdr);
103296f96fcSRusty Russell 
104*97402b96SHerbert Xu 	err = pskb_trim(skb, len);
105*97402b96SHerbert Xu 	if (err) {
106*97402b96SHerbert Xu 		pr_debug("%s: pskb_trim failed %i %d\n", dev->name, len, err);
107*97402b96SHerbert Xu 		dev->stats.rx_dropped++;
108*97402b96SHerbert Xu 		goto drop;
109*97402b96SHerbert Xu 	}
110*97402b96SHerbert Xu 	skb->truesize += skb->data_len;
111296f96fcSRusty Russell 	dev->stats.rx_bytes += skb->len;
112296f96fcSRusty Russell 	dev->stats.rx_packets++;
113296f96fcSRusty Russell 
114296f96fcSRusty Russell 	if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
115296f96fcSRusty Russell 		pr_debug("Needs csum!\n");
116f35d9d8aSRusty Russell 		if (!skb_partial_csum_set(skb,hdr->csum_start,hdr->csum_offset))
117296f96fcSRusty Russell 			goto frame_err;
118296f96fcSRusty Russell 	}
119296f96fcSRusty Russell 
12023cde76dSMark McLoughlin 	skb->protocol = eth_type_trans(skb, dev);
12123cde76dSMark McLoughlin 	pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
12223cde76dSMark McLoughlin 		 ntohs(skb->protocol), skb->len, skb->pkt_type);
12323cde76dSMark McLoughlin 
124296f96fcSRusty Russell 	if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
125296f96fcSRusty Russell 		pr_debug("GSO!\n");
12634a48579SRusty Russell 		switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
127296f96fcSRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV4:
128296f96fcSRusty Russell 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
129296f96fcSRusty Russell 			break;
130296f96fcSRusty Russell 		case VIRTIO_NET_HDR_GSO_UDP:
131296f96fcSRusty Russell 			skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
132296f96fcSRusty Russell 			break;
133296f96fcSRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV6:
134296f96fcSRusty Russell 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
135296f96fcSRusty Russell 			break;
136296f96fcSRusty Russell 		default:
137296f96fcSRusty Russell 			if (net_ratelimit())
138296f96fcSRusty Russell 				printk(KERN_WARNING "%s: bad gso type %u.\n",
139296f96fcSRusty Russell 				       dev->name, hdr->gso_type);
140296f96fcSRusty Russell 			goto frame_err;
141296f96fcSRusty Russell 		}
142296f96fcSRusty Russell 
14334a48579SRusty Russell 		if (hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN)
14434a48579SRusty Russell 			skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
14534a48579SRusty Russell 
146296f96fcSRusty Russell 		skb_shinfo(skb)->gso_size = hdr->gso_size;
147296f96fcSRusty Russell 		if (skb_shinfo(skb)->gso_size == 0) {
148296f96fcSRusty Russell 			if (net_ratelimit())
149296f96fcSRusty Russell 				printk(KERN_WARNING "%s: zero gso size.\n",
150296f96fcSRusty Russell 				       dev->name);
151296f96fcSRusty Russell 			goto frame_err;
152296f96fcSRusty Russell 		}
153296f96fcSRusty Russell 
154296f96fcSRusty Russell 		/* Header must be checked, and gso_segs computed. */
155296f96fcSRusty Russell 		skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
156296f96fcSRusty Russell 		skb_shinfo(skb)->gso_segs = 0;
157296f96fcSRusty Russell 	}
158296f96fcSRusty Russell 
159296f96fcSRusty Russell 	netif_receive_skb(skb);
160296f96fcSRusty Russell 	return;
161296f96fcSRusty Russell 
162296f96fcSRusty Russell frame_err:
163296f96fcSRusty Russell 	dev->stats.rx_frame_errors++;
164296f96fcSRusty Russell drop:
165296f96fcSRusty Russell 	dev_kfree_skb(skb);
166296f96fcSRusty Russell }
167296f96fcSRusty Russell 
168296f96fcSRusty Russell static void try_fill_recv(struct virtnet_info *vi)
169296f96fcSRusty Russell {
170296f96fcSRusty Russell 	struct sk_buff *skb;
17105271685SRusty Russell 	struct scatterlist sg[2+MAX_SKB_FRAGS];
172*97402b96SHerbert Xu 	int num, err, i;
173296f96fcSRusty Russell 
17405271685SRusty Russell 	sg_init_table(sg, 2+MAX_SKB_FRAGS);
175296f96fcSRusty Russell 	for (;;) {
176296f96fcSRusty Russell 		skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN);
177296f96fcSRusty Russell 		if (unlikely(!skb))
178296f96fcSRusty Russell 			break;
179296f96fcSRusty Russell 
180296f96fcSRusty Russell 		skb_put(skb, MAX_PACKET_LEN);
181296f96fcSRusty Russell 		vnet_hdr_to_sg(sg, skb);
182*97402b96SHerbert Xu 
183*97402b96SHerbert Xu 		if (vi->big_packets) {
184*97402b96SHerbert Xu 			for (i = 0; i < MAX_SKB_FRAGS; i++) {
185*97402b96SHerbert Xu 				skb_frag_t *f = &skb_shinfo(skb)->frags[i];
186*97402b96SHerbert Xu 				f->page = alloc_page(GFP_ATOMIC);
187*97402b96SHerbert Xu 				if (!f->page)
188*97402b96SHerbert Xu 					break;
189*97402b96SHerbert Xu 
190*97402b96SHerbert Xu 				f->page_offset = 0;
191*97402b96SHerbert Xu 				f->size = PAGE_SIZE;
192*97402b96SHerbert Xu 
193*97402b96SHerbert Xu 				skb->data_len += PAGE_SIZE;
194*97402b96SHerbert Xu 				skb->len += PAGE_SIZE;
195*97402b96SHerbert Xu 
196*97402b96SHerbert Xu 				skb_shinfo(skb)->nr_frags++;
197*97402b96SHerbert Xu 			}
198*97402b96SHerbert Xu 		}
199*97402b96SHerbert Xu 
200296f96fcSRusty Russell 		num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
201296f96fcSRusty Russell 		skb_queue_head(&vi->recv, skb);
202296f96fcSRusty Russell 
203296f96fcSRusty Russell 		err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, num, skb);
204296f96fcSRusty Russell 		if (err) {
205296f96fcSRusty Russell 			skb_unlink(skb, &vi->recv);
206296f96fcSRusty Russell 			kfree_skb(skb);
207296f96fcSRusty Russell 			break;
208296f96fcSRusty Russell 		}
209296f96fcSRusty Russell 		vi->num++;
210296f96fcSRusty Russell 	}
211296f96fcSRusty Russell 	if (unlikely(vi->num > vi->max))
212296f96fcSRusty Russell 		vi->max = vi->num;
213296f96fcSRusty Russell 	vi->rvq->vq_ops->kick(vi->rvq);
214296f96fcSRusty Russell }
215296f96fcSRusty Russell 
21618445c4dSRusty Russell static void skb_recv_done(struct virtqueue *rvq)
217296f96fcSRusty Russell {
218296f96fcSRusty Russell 	struct virtnet_info *vi = rvq->vdev->priv;
21918445c4dSRusty Russell 	/* Schedule NAPI, Suppress further interrupts if successful. */
22018445c4dSRusty Russell 	if (netif_rx_schedule_prep(vi->dev, &vi->napi)) {
22118445c4dSRusty Russell 		rvq->vq_ops->disable_cb(rvq);
22218445c4dSRusty Russell 		__netif_rx_schedule(vi->dev, &vi->napi);
22318445c4dSRusty Russell 	}
224296f96fcSRusty Russell }
225296f96fcSRusty Russell 
226296f96fcSRusty Russell static int virtnet_poll(struct napi_struct *napi, int budget)
227296f96fcSRusty Russell {
228296f96fcSRusty Russell 	struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi);
229296f96fcSRusty Russell 	struct sk_buff *skb = NULL;
230296f96fcSRusty Russell 	unsigned int len, received = 0;
231296f96fcSRusty Russell 
232296f96fcSRusty Russell again:
233296f96fcSRusty Russell 	while (received < budget &&
234296f96fcSRusty Russell 	       (skb = vi->rvq->vq_ops->get_buf(vi->rvq, &len)) != NULL) {
235296f96fcSRusty Russell 		__skb_unlink(skb, &vi->recv);
236296f96fcSRusty Russell 		receive_skb(vi->dev, skb, len);
237296f96fcSRusty Russell 		vi->num--;
238296f96fcSRusty Russell 		received++;
239296f96fcSRusty Russell 	}
240296f96fcSRusty Russell 
241296f96fcSRusty Russell 	/* FIXME: If we oom and completely run out of inbufs, we need
242296f96fcSRusty Russell 	 * to start a timer trying to fill more. */
243296f96fcSRusty Russell 	if (vi->num < vi->max / 2)
244296f96fcSRusty Russell 		try_fill_recv(vi);
245296f96fcSRusty Russell 
2468329d98eSRusty Russell 	/* Out of packets? */
2478329d98eSRusty Russell 	if (received < budget) {
248296f96fcSRusty Russell 		netif_rx_complete(vi->dev, napi);
24918445c4dSRusty Russell 		if (unlikely(!vi->rvq->vq_ops->enable_cb(vi->rvq))
2504265f161SChristian Borntraeger 		    && napi_schedule_prep(napi)) {
2514265f161SChristian Borntraeger 			vi->rvq->vq_ops->disable_cb(vi->rvq);
2524265f161SChristian Borntraeger 			__netif_rx_schedule(vi->dev, napi);
253296f96fcSRusty Russell 			goto again;
254296f96fcSRusty Russell 		}
2554265f161SChristian Borntraeger 	}
256296f96fcSRusty Russell 
257296f96fcSRusty Russell 	return received;
258296f96fcSRusty Russell }
259296f96fcSRusty Russell 
260296f96fcSRusty Russell static void free_old_xmit_skbs(struct virtnet_info *vi)
261296f96fcSRusty Russell {
262296f96fcSRusty Russell 	struct sk_buff *skb;
263296f96fcSRusty Russell 	unsigned int len;
264296f96fcSRusty Russell 
265296f96fcSRusty Russell 	while ((skb = vi->svq->vq_ops->get_buf(vi->svq, &len)) != NULL) {
266296f96fcSRusty Russell 		pr_debug("Sent skb %p\n", skb);
267296f96fcSRusty Russell 		__skb_unlink(skb, &vi->send);
268655aa31fSRusty Russell 		vi->dev->stats.tx_bytes += skb->len;
269296f96fcSRusty Russell 		vi->dev->stats.tx_packets++;
270296f96fcSRusty Russell 		kfree_skb(skb);
271296f96fcSRusty Russell 	}
272296f96fcSRusty Russell }
273296f96fcSRusty Russell 
274363f1514SRusty Russell /* If the virtio transport doesn't always notify us when all in-flight packets
275363f1514SRusty Russell  * are consumed, we fall back to using this function on a timer to free them. */
27614c998f0SMark McLoughlin static void xmit_free(unsigned long data)
27714c998f0SMark McLoughlin {
27814c998f0SMark McLoughlin 	struct virtnet_info *vi = (void *)data;
27914c998f0SMark McLoughlin 
28014c998f0SMark McLoughlin 	netif_tx_lock(vi->dev);
28114c998f0SMark McLoughlin 
28214c998f0SMark McLoughlin 	free_old_xmit_skbs(vi);
28314c998f0SMark McLoughlin 
28414c998f0SMark McLoughlin 	if (!skb_queue_empty(&vi->send))
28514c998f0SMark McLoughlin 		mod_timer(&vi->xmit_free_timer, jiffies + (HZ/10));
28614c998f0SMark McLoughlin 
28714c998f0SMark McLoughlin 	netif_tx_unlock(vi->dev);
28814c998f0SMark McLoughlin }
28914c998f0SMark McLoughlin 
29099ffc696SRusty Russell static int xmit_skb(struct virtnet_info *vi, struct sk_buff *skb)
291296f96fcSRusty Russell {
29214c998f0SMark McLoughlin 	int num, err;
29305271685SRusty Russell 	struct scatterlist sg[2+MAX_SKB_FRAGS];
294296f96fcSRusty Russell 	struct virtio_net_hdr *hdr;
295296f96fcSRusty Russell 	const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
296296f96fcSRusty Russell 
29705271685SRusty Russell 	sg_init_table(sg, 2+MAX_SKB_FRAGS);
2984d125de3SRusty Russell 
29999ffc696SRusty Russell 	pr_debug("%s: xmit %p " MAC_FMT "\n", vi->dev->name, skb,
30021f644f3SDavid S. Miller 		 dest[0], dest[1], dest[2],
30121f644f3SDavid S. Miller 		 dest[3], dest[4], dest[5]);
302296f96fcSRusty Russell 
303296f96fcSRusty Russell 	/* Encode metadata header at front. */
304296f96fcSRusty Russell 	hdr = skb_vnet_hdr(skb);
305296f96fcSRusty Russell 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
306296f96fcSRusty Russell 		hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
307296f96fcSRusty Russell 		hdr->csum_start = skb->csum_start - skb_headroom(skb);
308296f96fcSRusty Russell 		hdr->csum_offset = skb->csum_offset;
309296f96fcSRusty Russell 	} else {
310296f96fcSRusty Russell 		hdr->flags = 0;
311296f96fcSRusty Russell 		hdr->csum_offset = hdr->csum_start = 0;
312296f96fcSRusty Russell 	}
313296f96fcSRusty Russell 
314296f96fcSRusty Russell 	if (skb_is_gso(skb)) {
31550c8ea80SRusty Russell 		hdr->hdr_len = skb_transport_header(skb) - skb->data;
316296f96fcSRusty Russell 		hdr->gso_size = skb_shinfo(skb)->gso_size;
31734a48579SRusty Russell 		if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
318296f96fcSRusty Russell 			hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
319296f96fcSRusty Russell 		else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
320296f96fcSRusty Russell 			hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
321296f96fcSRusty Russell 		else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
322296f96fcSRusty Russell 			hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
323296f96fcSRusty Russell 		else
324296f96fcSRusty Russell 			BUG();
32534a48579SRusty Russell 		if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
32634a48579SRusty Russell 			hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
327296f96fcSRusty Russell 	} else {
328296f96fcSRusty Russell 		hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
32950c8ea80SRusty Russell 		hdr->gso_size = hdr->hdr_len = 0;
330296f96fcSRusty Russell 	}
331296f96fcSRusty Russell 
332296f96fcSRusty Russell 	vnet_hdr_to_sg(sg, skb);
333296f96fcSRusty Russell 	num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
33499ffc696SRusty Russell 
33514c998f0SMark McLoughlin 	err = vi->svq->vq_ops->add_buf(vi->svq, sg, num, 0, skb);
336363f1514SRusty Russell 	if (!err && !vi->free_in_tasklet)
33714c998f0SMark McLoughlin 		mod_timer(&vi->xmit_free_timer, jiffies + (HZ/10));
33814c998f0SMark McLoughlin 
33914c998f0SMark McLoughlin 	return err;
34099ffc696SRusty Russell }
34199ffc696SRusty Russell 
34211a3a154SRusty Russell static void xmit_tasklet(unsigned long data)
34311a3a154SRusty Russell {
34411a3a154SRusty Russell 	struct virtnet_info *vi = (void *)data;
34511a3a154SRusty Russell 
34611a3a154SRusty Russell 	netif_tx_lock_bh(vi->dev);
34711a3a154SRusty Russell 	if (vi->last_xmit_skb && xmit_skb(vi, vi->last_xmit_skb) == 0) {
34811a3a154SRusty Russell 		vi->svq->vq_ops->kick(vi->svq);
34911a3a154SRusty Russell 		vi->last_xmit_skb = NULL;
35011a3a154SRusty Russell 	}
351363f1514SRusty Russell 	if (vi->free_in_tasklet)
352363f1514SRusty Russell 		free_old_xmit_skbs(vi);
35311a3a154SRusty Russell 	netif_tx_unlock_bh(vi->dev);
35411a3a154SRusty Russell }
35511a3a154SRusty Russell 
35699ffc696SRusty Russell static int start_xmit(struct sk_buff *skb, struct net_device *dev)
35799ffc696SRusty Russell {
35899ffc696SRusty Russell 	struct virtnet_info *vi = netdev_priv(dev);
3592cb9c6baSRusty Russell 
3602cb9c6baSRusty Russell again:
3612cb9c6baSRusty Russell 	/* Free up any pending old buffers before queueing new ones. */
3622cb9c6baSRusty Russell 	free_old_xmit_skbs(vi);
36399ffc696SRusty Russell 
36499ffc696SRusty Russell 	/* If we has a buffer left over from last time, send it now. */
3659953ca6cSMark McLoughlin 	if (unlikely(vi->last_xmit_skb) &&
3669953ca6cSMark McLoughlin 	    xmit_skb(vi, vi->last_xmit_skb) != 0)
36799ffc696SRusty Russell 		goto stop_queue;
3689953ca6cSMark McLoughlin 
36999ffc696SRusty Russell 	vi->last_xmit_skb = NULL;
37099ffc696SRusty Russell 
37199ffc696SRusty Russell 	/* Put new one in send queue and do transmit */
3727eb2e251SRusty Russell 	if (likely(skb)) {
37399ffc696SRusty Russell 		__skb_queue_head(&vi->send, skb);
37499ffc696SRusty Russell 		if (xmit_skb(vi, skb) != 0) {
37599ffc696SRusty Russell 			vi->last_xmit_skb = skb;
3767eb2e251SRusty Russell 			skb = NULL;
37799ffc696SRusty Russell 			goto stop_queue;
37899ffc696SRusty Russell 		}
3797eb2e251SRusty Russell 	}
38099ffc696SRusty Russell done:
38199ffc696SRusty Russell 	vi->svq->vq_ops->kick(vi->svq);
38299ffc696SRusty Russell 	return NETDEV_TX_OK;
38399ffc696SRusty Russell 
38499ffc696SRusty Russell stop_queue:
385296f96fcSRusty Russell 	pr_debug("%s: virtio not prepared to send\n", dev->name);
386296f96fcSRusty Russell 	netif_stop_queue(dev);
3872cb9c6baSRusty Russell 
3884265f161SChristian Borntraeger 	/* Activate callback for using skbs: if this returns false it
3892cb9c6baSRusty Russell 	 * means some were used in the meantime. */
3902cb9c6baSRusty Russell 	if (unlikely(!vi->svq->vq_ops->enable_cb(vi->svq))) {
3914265f161SChristian Borntraeger 		vi->svq->vq_ops->disable_cb(vi->svq);
3922cb9c6baSRusty Russell 		netif_start_queue(dev);
3932cb9c6baSRusty Russell 		goto again;
3942cb9c6baSRusty Russell 	}
3959953ca6cSMark McLoughlin 	if (skb) {
3969953ca6cSMark McLoughlin 		/* Drop this skb: we only queue one. */
3979953ca6cSMark McLoughlin 		vi->dev->stats.tx_dropped++;
3989953ca6cSMark McLoughlin 		kfree_skb(skb);
3999953ca6cSMark McLoughlin 	}
40099ffc696SRusty Russell 	goto done;
401296f96fcSRusty Russell }
402296f96fcSRusty Russell 
403da74e89dSAmit Shah #ifdef CONFIG_NET_POLL_CONTROLLER
404da74e89dSAmit Shah static void virtnet_netpoll(struct net_device *dev)
405da74e89dSAmit Shah {
406da74e89dSAmit Shah 	struct virtnet_info *vi = netdev_priv(dev);
407da74e89dSAmit Shah 
408da74e89dSAmit Shah 	napi_schedule(&vi->napi);
409da74e89dSAmit Shah }
410da74e89dSAmit Shah #endif
411da74e89dSAmit Shah 
412296f96fcSRusty Russell static int virtnet_open(struct net_device *dev)
413296f96fcSRusty Russell {
414296f96fcSRusty Russell 	struct virtnet_info *vi = netdev_priv(dev);
415296f96fcSRusty Russell 
416296f96fcSRusty Russell 	napi_enable(&vi->napi);
417a48bd8f6SRusty Russell 
418a48bd8f6SRusty Russell 	/* If all buffers were filled by other side before we napi_enabled, we
419a48bd8f6SRusty Russell 	 * won't get another interrupt, so process any outstanding packets
420370076d9SChristian Borntraeger 	 * now.  virtnet_poll wants re-enable the queue, so we disable here.
421370076d9SChristian Borntraeger 	 * We synchronize against interrupts via NAPI_STATE_SCHED */
422370076d9SChristian Borntraeger 	if (netif_rx_schedule_prep(dev, &vi->napi)) {
423a48bd8f6SRusty Russell 		vi->rvq->vq_ops->disable_cb(vi->rvq);
424370076d9SChristian Borntraeger 		__netif_rx_schedule(dev, &vi->napi);
425370076d9SChristian Borntraeger 	}
426296f96fcSRusty Russell 	return 0;
427296f96fcSRusty Russell }
428296f96fcSRusty Russell 
429296f96fcSRusty Russell static int virtnet_close(struct net_device *dev)
430296f96fcSRusty Russell {
431296f96fcSRusty Russell 	struct virtnet_info *vi = netdev_priv(dev);
432296f96fcSRusty Russell 
433296f96fcSRusty Russell 	napi_disable(&vi->napi);
434296f96fcSRusty Russell 
435296f96fcSRusty Russell 	return 0;
436296f96fcSRusty Russell }
437296f96fcSRusty Russell 
438a9ea3fc6SHerbert Xu static int virtnet_set_tx_csum(struct net_device *dev, u32 data)
439a9ea3fc6SHerbert Xu {
440a9ea3fc6SHerbert Xu 	struct virtnet_info *vi = netdev_priv(dev);
441a9ea3fc6SHerbert Xu 	struct virtio_device *vdev = vi->vdev;
442a9ea3fc6SHerbert Xu 
443a9ea3fc6SHerbert Xu 	if (data && !virtio_has_feature(vdev, VIRTIO_NET_F_CSUM))
444a9ea3fc6SHerbert Xu 		return -ENOSYS;
445a9ea3fc6SHerbert Xu 
446a9ea3fc6SHerbert Xu 	return ethtool_op_set_tx_hw_csum(dev, data);
447a9ea3fc6SHerbert Xu }
448a9ea3fc6SHerbert Xu 
449a9ea3fc6SHerbert Xu static struct ethtool_ops virtnet_ethtool_ops = {
450a9ea3fc6SHerbert Xu 	.set_tx_csum = virtnet_set_tx_csum,
451a9ea3fc6SHerbert Xu 	.set_sg = ethtool_op_set_sg,
452a9ea3fc6SHerbert Xu };
453a9ea3fc6SHerbert Xu 
454296f96fcSRusty Russell static int virtnet_probe(struct virtio_device *vdev)
455296f96fcSRusty Russell {
456296f96fcSRusty Russell 	int err;
457296f96fcSRusty Russell 	struct net_device *dev;
458296f96fcSRusty Russell 	struct virtnet_info *vi;
459296f96fcSRusty Russell 
460296f96fcSRusty Russell 	/* Allocate ourselves a network device with room for our info */
461296f96fcSRusty Russell 	dev = alloc_etherdev(sizeof(struct virtnet_info));
462296f96fcSRusty Russell 	if (!dev)
463296f96fcSRusty Russell 		return -ENOMEM;
464296f96fcSRusty Russell 
465296f96fcSRusty Russell 	/* Set up network device as normal. */
466296f96fcSRusty Russell 	dev->open = virtnet_open;
467296f96fcSRusty Russell 	dev->stop = virtnet_close;
468296f96fcSRusty Russell 	dev->hard_start_xmit = start_xmit;
469296f96fcSRusty Russell 	dev->features = NETIF_F_HIGHDMA;
470da74e89dSAmit Shah #ifdef CONFIG_NET_POLL_CONTROLLER
471da74e89dSAmit Shah 	dev->poll_controller = virtnet_netpoll;
472da74e89dSAmit Shah #endif
473a9ea3fc6SHerbert Xu 	SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops);
474296f96fcSRusty Russell 	SET_NETDEV_DEV(dev, &vdev->dev);
475296f96fcSRusty Russell 
476296f96fcSRusty Russell 	/* Do we support "hardware" checksums? */
477c45a6816SRusty Russell 	if (csum && virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
478296f96fcSRusty Russell 		/* This opens up the world of extra features. */
479296f96fcSRusty Russell 		dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
480c45a6816SRusty Russell 		if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
48134a48579SRusty Russell 			dev->features |= NETIF_F_TSO | NETIF_F_UFO
48234a48579SRusty Russell 				| NETIF_F_TSO_ECN | NETIF_F_TSO6;
48334a48579SRusty Russell 		}
4845539ae96SRusty Russell 		/* Individual feature bits: what can host handle? */
485c45a6816SRusty Russell 		if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
4865539ae96SRusty Russell 			dev->features |= NETIF_F_TSO;
487c45a6816SRusty Russell 		if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
4885539ae96SRusty Russell 			dev->features |= NETIF_F_TSO6;
489c45a6816SRusty Russell 		if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
4905539ae96SRusty Russell 			dev->features |= NETIF_F_TSO_ECN;
491c45a6816SRusty Russell 		if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
4925539ae96SRusty Russell 			dev->features |= NETIF_F_UFO;
493296f96fcSRusty Russell 	}
494296f96fcSRusty Russell 
495296f96fcSRusty Russell 	/* Configuration may specify what MAC to use.  Otherwise random. */
496c45a6816SRusty Russell 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
497a586d4f6SRusty Russell 		vdev->config->get(vdev,
498a586d4f6SRusty Russell 				  offsetof(struct virtio_net_config, mac),
499a586d4f6SRusty Russell 				  dev->dev_addr, dev->addr_len);
500296f96fcSRusty Russell 	} else
501296f96fcSRusty Russell 		random_ether_addr(dev->dev_addr);
502296f96fcSRusty Russell 
503296f96fcSRusty Russell 	/* Set up our device-specific information */
504296f96fcSRusty Russell 	vi = netdev_priv(dev);
5056c0cd7c0SDor Laor 	netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight);
506296f96fcSRusty Russell 	vi->dev = dev;
507296f96fcSRusty Russell 	vi->vdev = vdev;
508d9d5dcc8SChristian Borntraeger 	vdev->priv = vi;
509296f96fcSRusty Russell 
510363f1514SRusty Russell 	/* If they give us a callback when all buffers are done, we don't need
511363f1514SRusty Russell 	 * the timer. */
512363f1514SRusty Russell 	vi->free_in_tasklet = virtio_has_feature(vdev,VIRTIO_F_NOTIFY_ON_EMPTY);
513363f1514SRusty Russell 
514*97402b96SHerbert Xu 	/* If we can receive ANY GSO packets, we must allocate large ones. */
515*97402b96SHerbert Xu 	if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4)
516*97402b96SHerbert Xu 	    || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6)
517*97402b96SHerbert Xu 	    || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN))
518*97402b96SHerbert Xu 		vi->big_packets = true;
519*97402b96SHerbert Xu 
520296f96fcSRusty Russell 	/* We expect two virtqueues, receive then send. */
521a586d4f6SRusty Russell 	vi->rvq = vdev->config->find_vq(vdev, 0, skb_recv_done);
522296f96fcSRusty Russell 	if (IS_ERR(vi->rvq)) {
523296f96fcSRusty Russell 		err = PTR_ERR(vi->rvq);
524296f96fcSRusty Russell 		goto free;
525296f96fcSRusty Russell 	}
526296f96fcSRusty Russell 
527a586d4f6SRusty Russell 	vi->svq = vdev->config->find_vq(vdev, 1, skb_xmit_done);
528296f96fcSRusty Russell 	if (IS_ERR(vi->svq)) {
529296f96fcSRusty Russell 		err = PTR_ERR(vi->svq);
530296f96fcSRusty Russell 		goto free_recv;
531296f96fcSRusty Russell 	}
532296f96fcSRusty Russell 
533296f96fcSRusty Russell 	/* Initialize our empty receive and send queues. */
534296f96fcSRusty Russell 	skb_queue_head_init(&vi->recv);
535296f96fcSRusty Russell 	skb_queue_head_init(&vi->send);
536296f96fcSRusty Russell 
53711a3a154SRusty Russell 	tasklet_init(&vi->tasklet, xmit_tasklet, (unsigned long)vi);
53811a3a154SRusty Russell 
539363f1514SRusty Russell 	if (!vi->free_in_tasklet)
54014c998f0SMark McLoughlin 		setup_timer(&vi->xmit_free_timer, xmit_free, (unsigned long)vi);
54114c998f0SMark McLoughlin 
542296f96fcSRusty Russell 	err = register_netdev(dev);
543296f96fcSRusty Russell 	if (err) {
544296f96fcSRusty Russell 		pr_debug("virtio_net: registering device failed\n");
545296f96fcSRusty Russell 		goto free_send;
546296f96fcSRusty Russell 	}
547b3369c1fSRusty Russell 
548b3369c1fSRusty Russell 	/* Last of all, set up some receive buffers. */
549b3369c1fSRusty Russell 	try_fill_recv(vi);
550b3369c1fSRusty Russell 
551b3369c1fSRusty Russell 	/* If we didn't even get one input buffer, we're useless. */
552b3369c1fSRusty Russell 	if (vi->num == 0) {
553b3369c1fSRusty Russell 		err = -ENOMEM;
554b3369c1fSRusty Russell 		goto unregister;
555b3369c1fSRusty Russell 	}
556b3369c1fSRusty Russell 
557296f96fcSRusty Russell 	pr_debug("virtnet: registered device %s\n", dev->name);
558296f96fcSRusty Russell 	return 0;
559296f96fcSRusty Russell 
560b3369c1fSRusty Russell unregister:
561b3369c1fSRusty Russell 	unregister_netdev(dev);
562296f96fcSRusty Russell free_send:
563296f96fcSRusty Russell 	vdev->config->del_vq(vi->svq);
564296f96fcSRusty Russell free_recv:
565296f96fcSRusty Russell 	vdev->config->del_vq(vi->rvq);
566296f96fcSRusty Russell free:
567296f96fcSRusty Russell 	free_netdev(dev);
568296f96fcSRusty Russell 	return err;
569296f96fcSRusty Russell }
570296f96fcSRusty Russell 
571296f96fcSRusty Russell static void virtnet_remove(struct virtio_device *vdev)
572296f96fcSRusty Russell {
57374b2553fSRusty Russell 	struct virtnet_info *vi = vdev->priv;
574b3369c1fSRusty Russell 	struct sk_buff *skb;
575b3369c1fSRusty Russell 
5766e5aa7efSRusty Russell 	/* Stop all the virtqueues. */
5776e5aa7efSRusty Russell 	vdev->config->reset(vdev);
5786e5aa7efSRusty Russell 
579363f1514SRusty Russell 	if (!vi->free_in_tasklet)
58014c998f0SMark McLoughlin 		del_timer_sync(&vi->xmit_free_timer);
58114c998f0SMark McLoughlin 
582b3369c1fSRusty Russell 	/* Free our skbs in send and recv queues, if any. */
583b3369c1fSRusty Russell 	while ((skb = __skb_dequeue(&vi->recv)) != NULL) {
584b3369c1fSRusty Russell 		kfree_skb(skb);
585b3369c1fSRusty Russell 		vi->num--;
586b3369c1fSRusty Russell 	}
587288369ccSWang Chen 	__skb_queue_purge(&vi->send);
588b3369c1fSRusty Russell 
589b3369c1fSRusty Russell 	BUG_ON(vi->num != 0);
59074b2553fSRusty Russell 
59174b2553fSRusty Russell 	vdev->config->del_vq(vi->svq);
59274b2553fSRusty Russell 	vdev->config->del_vq(vi->rvq);
59374b2553fSRusty Russell 	unregister_netdev(vi->dev);
59474b2553fSRusty Russell 	free_netdev(vi->dev);
595296f96fcSRusty Russell }
596296f96fcSRusty Russell 
597296f96fcSRusty Russell static struct virtio_device_id id_table[] = {
598296f96fcSRusty Russell 	{ VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
599296f96fcSRusty Russell 	{ 0 },
600296f96fcSRusty Russell };
601296f96fcSRusty Russell 
602c45a6816SRusty Russell static unsigned int features[] = {
6035e4fe5c4SMark McLoughlin 	VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM,
6045e4fe5c4SMark McLoughlin 	VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
605c45a6816SRusty Russell 	VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
606*97402b96SHerbert Xu 	VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
607*97402b96SHerbert Xu 	VIRTIO_NET_F_GUEST_ECN, /* We don't yet handle UFO input. */
608*97402b96SHerbert Xu 	VIRTIO_F_NOTIFY_ON_EMPTY,
609c45a6816SRusty Russell };
610c45a6816SRusty Russell 
611296f96fcSRusty Russell static struct virtio_driver virtio_net = {
612c45a6816SRusty Russell 	.feature_table = features,
613c45a6816SRusty Russell 	.feature_table_size = ARRAY_SIZE(features),
614296f96fcSRusty Russell 	.driver.name =	KBUILD_MODNAME,
615296f96fcSRusty Russell 	.driver.owner =	THIS_MODULE,
616296f96fcSRusty Russell 	.id_table =	id_table,
617296f96fcSRusty Russell 	.probe =	virtnet_probe,
618296f96fcSRusty Russell 	.remove =	__devexit_p(virtnet_remove),
619296f96fcSRusty Russell };
620296f96fcSRusty Russell 
621296f96fcSRusty Russell static int __init init(void)
622296f96fcSRusty Russell {
623296f96fcSRusty Russell 	return register_virtio_driver(&virtio_net);
624296f96fcSRusty Russell }
625296f96fcSRusty Russell 
626296f96fcSRusty Russell static void __exit fini(void)
627296f96fcSRusty Russell {
628296f96fcSRusty Russell 	unregister_virtio_driver(&virtio_net);
629296f96fcSRusty Russell }
630296f96fcSRusty Russell module_init(init);
631296f96fcSRusty Russell module_exit(fini);
632296f96fcSRusty Russell 
633296f96fcSRusty Russell MODULE_DEVICE_TABLE(virtio, id_table);
634296f96fcSRusty Russell MODULE_DESCRIPTION("Virtio network driver");
635296f96fcSRusty Russell MODULE_LICENSE("GPL");
636