xref: /openbmc/linux/drivers/net/virtio_net.c (revision 6c0cd7c000dc0851035c5003bf9d47733d0b257b)
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 
27*6c0cd7c0SDor Laor static int napi_weight = 128;
28*6c0cd7c0SDor Laor module_param(napi_weight, int, 0444);
29*6c0cd7c0SDor 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 
44296f96fcSRusty Russell 	/* Number of input buffers, and max we've ever had. */
45296f96fcSRusty Russell 	unsigned int num, max;
46296f96fcSRusty Russell 
47296f96fcSRusty Russell 	/* Receive & send queues. */
48296f96fcSRusty Russell 	struct sk_buff_head recv;
49296f96fcSRusty Russell 	struct sk_buff_head send;
50296f96fcSRusty Russell };
51296f96fcSRusty Russell 
52296f96fcSRusty Russell static inline struct virtio_net_hdr *skb_vnet_hdr(struct sk_buff *skb)
53296f96fcSRusty Russell {
54296f96fcSRusty Russell 	return (struct virtio_net_hdr *)skb->cb;
55296f96fcSRusty Russell }
56296f96fcSRusty Russell 
57296f96fcSRusty Russell static inline void vnet_hdr_to_sg(struct scatterlist *sg, struct sk_buff *skb)
58296f96fcSRusty Russell {
59296f96fcSRusty Russell 	sg_init_one(sg, skb_vnet_hdr(skb), sizeof(struct virtio_net_hdr));
60296f96fcSRusty Russell }
61296f96fcSRusty Russell 
622cb9c6baSRusty Russell static void skb_xmit_done(struct virtqueue *svq)
63296f96fcSRusty Russell {
642cb9c6baSRusty Russell 	struct virtnet_info *vi = svq->vdev->priv;
65296f96fcSRusty Russell 
662cb9c6baSRusty Russell 	/* Suppress further interrupts. */
672cb9c6baSRusty Russell 	svq->vq_ops->disable_cb(svq);
682cb9c6baSRusty Russell 	/* We were waiting for more output buffers. */
69296f96fcSRusty Russell 	netif_wake_queue(vi->dev);
70296f96fcSRusty Russell }
71296f96fcSRusty Russell 
72296f96fcSRusty Russell static void receive_skb(struct net_device *dev, struct sk_buff *skb,
73296f96fcSRusty Russell 			unsigned len)
74296f96fcSRusty Russell {
75296f96fcSRusty Russell 	struct virtio_net_hdr *hdr = skb_vnet_hdr(skb);
76296f96fcSRusty Russell 
77296f96fcSRusty Russell 	if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) {
78296f96fcSRusty Russell 		pr_debug("%s: short packet %i\n", dev->name, len);
79296f96fcSRusty Russell 		dev->stats.rx_length_errors++;
80296f96fcSRusty Russell 		goto drop;
81296f96fcSRusty Russell 	}
82296f96fcSRusty Russell 	len -= sizeof(struct virtio_net_hdr);
83296f96fcSRusty Russell 	BUG_ON(len > MAX_PACKET_LEN);
84296f96fcSRusty Russell 
85296f96fcSRusty Russell 	skb_trim(skb, len);
86296f96fcSRusty Russell 	skb->protocol = eth_type_trans(skb, dev);
87296f96fcSRusty Russell 	pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
88296f96fcSRusty Russell 		 ntohs(skb->protocol), skb->len, skb->pkt_type);
89296f96fcSRusty Russell 	dev->stats.rx_bytes += skb->len;
90296f96fcSRusty Russell 	dev->stats.rx_packets++;
91296f96fcSRusty Russell 
92296f96fcSRusty Russell 	if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
93296f96fcSRusty Russell 		pr_debug("Needs csum!\n");
94f35d9d8aSRusty Russell 		if (!skb_partial_csum_set(skb,hdr->csum_start,hdr->csum_offset))
95296f96fcSRusty Russell 			goto frame_err;
96296f96fcSRusty Russell 	}
97296f96fcSRusty Russell 
98296f96fcSRusty Russell 	if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
99296f96fcSRusty Russell 		pr_debug("GSO!\n");
10034a48579SRusty Russell 		switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
101296f96fcSRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV4:
102296f96fcSRusty Russell 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
103296f96fcSRusty Russell 			break;
104296f96fcSRusty Russell 		case VIRTIO_NET_HDR_GSO_UDP:
105296f96fcSRusty Russell 			skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
106296f96fcSRusty Russell 			break;
107296f96fcSRusty Russell 		case VIRTIO_NET_HDR_GSO_TCPV6:
108296f96fcSRusty Russell 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
109296f96fcSRusty Russell 			break;
110296f96fcSRusty Russell 		default:
111296f96fcSRusty Russell 			if (net_ratelimit())
112296f96fcSRusty Russell 				printk(KERN_WARNING "%s: bad gso type %u.\n",
113296f96fcSRusty Russell 				       dev->name, hdr->gso_type);
114296f96fcSRusty Russell 			goto frame_err;
115296f96fcSRusty Russell 		}
116296f96fcSRusty Russell 
11734a48579SRusty Russell 		if (hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN)
11834a48579SRusty Russell 			skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
11934a48579SRusty Russell 
120296f96fcSRusty Russell 		skb_shinfo(skb)->gso_size = hdr->gso_size;
121296f96fcSRusty Russell 		if (skb_shinfo(skb)->gso_size == 0) {
122296f96fcSRusty Russell 			if (net_ratelimit())
123296f96fcSRusty Russell 				printk(KERN_WARNING "%s: zero gso size.\n",
124296f96fcSRusty Russell 				       dev->name);
125296f96fcSRusty Russell 			goto frame_err;
126296f96fcSRusty Russell 		}
127296f96fcSRusty Russell 
128296f96fcSRusty Russell 		/* Header must be checked, and gso_segs computed. */
129296f96fcSRusty Russell 		skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
130296f96fcSRusty Russell 		skb_shinfo(skb)->gso_segs = 0;
131296f96fcSRusty Russell 	}
132296f96fcSRusty Russell 
133296f96fcSRusty Russell 	netif_receive_skb(skb);
134296f96fcSRusty Russell 	return;
135296f96fcSRusty Russell 
136296f96fcSRusty Russell frame_err:
137296f96fcSRusty Russell 	dev->stats.rx_frame_errors++;
138296f96fcSRusty Russell drop:
139296f96fcSRusty Russell 	dev_kfree_skb(skb);
140296f96fcSRusty Russell }
141296f96fcSRusty Russell 
142296f96fcSRusty Russell static void try_fill_recv(struct virtnet_info *vi)
143296f96fcSRusty Russell {
144296f96fcSRusty Russell 	struct sk_buff *skb;
145296f96fcSRusty Russell 	struct scatterlist sg[1+MAX_SKB_FRAGS];
146296f96fcSRusty Russell 	int num, err;
147296f96fcSRusty Russell 
1484d125de3SRusty Russell 	sg_init_table(sg, 1+MAX_SKB_FRAGS);
149296f96fcSRusty Russell 	for (;;) {
150296f96fcSRusty Russell 		skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN);
151296f96fcSRusty Russell 		if (unlikely(!skb))
152296f96fcSRusty Russell 			break;
153296f96fcSRusty Russell 
154296f96fcSRusty Russell 		skb_put(skb, MAX_PACKET_LEN);
155296f96fcSRusty Russell 		vnet_hdr_to_sg(sg, skb);
156296f96fcSRusty Russell 		num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
157296f96fcSRusty Russell 		skb_queue_head(&vi->recv, skb);
158296f96fcSRusty Russell 
159296f96fcSRusty Russell 		err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, num, skb);
160296f96fcSRusty Russell 		if (err) {
161296f96fcSRusty Russell 			skb_unlink(skb, &vi->recv);
162296f96fcSRusty Russell 			kfree_skb(skb);
163296f96fcSRusty Russell 			break;
164296f96fcSRusty Russell 		}
165296f96fcSRusty Russell 		vi->num++;
166296f96fcSRusty Russell 	}
167296f96fcSRusty Russell 	if (unlikely(vi->num > vi->max))
168296f96fcSRusty Russell 		vi->max = vi->num;
169296f96fcSRusty Russell 	vi->rvq->vq_ops->kick(vi->rvq);
170296f96fcSRusty Russell }
171296f96fcSRusty Russell 
17218445c4dSRusty Russell static void skb_recv_done(struct virtqueue *rvq)
173296f96fcSRusty Russell {
174296f96fcSRusty Russell 	struct virtnet_info *vi = rvq->vdev->priv;
17518445c4dSRusty Russell 	/* Schedule NAPI, Suppress further interrupts if successful. */
17618445c4dSRusty Russell 	if (netif_rx_schedule_prep(vi->dev, &vi->napi)) {
17718445c4dSRusty Russell 		rvq->vq_ops->disable_cb(rvq);
17818445c4dSRusty Russell 		__netif_rx_schedule(vi->dev, &vi->napi);
17918445c4dSRusty Russell 	}
180296f96fcSRusty Russell }
181296f96fcSRusty Russell 
182296f96fcSRusty Russell static int virtnet_poll(struct napi_struct *napi, int budget)
183296f96fcSRusty Russell {
184296f96fcSRusty Russell 	struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi);
185296f96fcSRusty Russell 	struct sk_buff *skb = NULL;
186296f96fcSRusty Russell 	unsigned int len, received = 0;
187296f96fcSRusty Russell 
188296f96fcSRusty Russell again:
189296f96fcSRusty Russell 	while (received < budget &&
190296f96fcSRusty Russell 	       (skb = vi->rvq->vq_ops->get_buf(vi->rvq, &len)) != NULL) {
191296f96fcSRusty Russell 		__skb_unlink(skb, &vi->recv);
192296f96fcSRusty Russell 		receive_skb(vi->dev, skb, len);
193296f96fcSRusty Russell 		vi->num--;
194296f96fcSRusty Russell 		received++;
195296f96fcSRusty Russell 	}
196296f96fcSRusty Russell 
197296f96fcSRusty Russell 	/* FIXME: If we oom and completely run out of inbufs, we need
198296f96fcSRusty Russell 	 * to start a timer trying to fill more. */
199296f96fcSRusty Russell 	if (vi->num < vi->max / 2)
200296f96fcSRusty Russell 		try_fill_recv(vi);
201296f96fcSRusty Russell 
2028329d98eSRusty Russell 	/* Out of packets? */
2038329d98eSRusty Russell 	if (received < budget) {
204296f96fcSRusty Russell 		netif_rx_complete(vi->dev, napi);
20518445c4dSRusty Russell 		if (unlikely(!vi->rvq->vq_ops->enable_cb(vi->rvq))
206296f96fcSRusty Russell 		    && netif_rx_reschedule(vi->dev, napi))
207296f96fcSRusty Russell 			goto again;
208296f96fcSRusty Russell 	}
209296f96fcSRusty Russell 
210296f96fcSRusty Russell 	return received;
211296f96fcSRusty Russell }
212296f96fcSRusty Russell 
213296f96fcSRusty Russell static void free_old_xmit_skbs(struct virtnet_info *vi)
214296f96fcSRusty Russell {
215296f96fcSRusty Russell 	struct sk_buff *skb;
216296f96fcSRusty Russell 	unsigned int len;
217296f96fcSRusty Russell 
218296f96fcSRusty Russell 	while ((skb = vi->svq->vq_ops->get_buf(vi->svq, &len)) != NULL) {
219296f96fcSRusty Russell 		pr_debug("Sent skb %p\n", skb);
220296f96fcSRusty Russell 		__skb_unlink(skb, &vi->send);
221296f96fcSRusty Russell 		vi->dev->stats.tx_bytes += len;
222296f96fcSRusty Russell 		vi->dev->stats.tx_packets++;
223296f96fcSRusty Russell 		kfree_skb(skb);
224296f96fcSRusty Russell 	}
225296f96fcSRusty Russell }
226296f96fcSRusty Russell 
227296f96fcSRusty Russell static int start_xmit(struct sk_buff *skb, struct net_device *dev)
228296f96fcSRusty Russell {
229296f96fcSRusty Russell 	struct virtnet_info *vi = netdev_priv(dev);
230296f96fcSRusty Russell 	int num, err;
231296f96fcSRusty Russell 	struct scatterlist sg[1+MAX_SKB_FRAGS];
232296f96fcSRusty Russell 	struct virtio_net_hdr *hdr;
233296f96fcSRusty Russell 	const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
234296f96fcSRusty Russell 	DECLARE_MAC_BUF(mac);
235296f96fcSRusty Russell 
2364d125de3SRusty Russell 	sg_init_table(sg, 1+MAX_SKB_FRAGS);
2374d125de3SRusty Russell 
238296f96fcSRusty Russell 	pr_debug("%s: xmit %p %s\n", dev->name, skb, print_mac(mac, dest));
239296f96fcSRusty Russell 
240296f96fcSRusty Russell 	/* Encode metadata header at front. */
241296f96fcSRusty Russell 	hdr = skb_vnet_hdr(skb);
242296f96fcSRusty Russell 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
243296f96fcSRusty Russell 		hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
244296f96fcSRusty Russell 		hdr->csum_start = skb->csum_start - skb_headroom(skb);
245296f96fcSRusty Russell 		hdr->csum_offset = skb->csum_offset;
246296f96fcSRusty Russell 	} else {
247296f96fcSRusty Russell 		hdr->flags = 0;
248296f96fcSRusty Russell 		hdr->csum_offset = hdr->csum_start = 0;
249296f96fcSRusty Russell 	}
250296f96fcSRusty Russell 
251296f96fcSRusty Russell 	if (skb_is_gso(skb)) {
25250c8ea80SRusty Russell 		hdr->hdr_len = skb_transport_header(skb) - skb->data;
253296f96fcSRusty Russell 		hdr->gso_size = skb_shinfo(skb)->gso_size;
25434a48579SRusty Russell 		if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
255296f96fcSRusty Russell 			hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
256296f96fcSRusty Russell 		else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
257296f96fcSRusty Russell 			hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
258296f96fcSRusty Russell 		else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
259296f96fcSRusty Russell 			hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
260296f96fcSRusty Russell 		else
261296f96fcSRusty Russell 			BUG();
26234a48579SRusty Russell 		if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
26334a48579SRusty Russell 			hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
264296f96fcSRusty Russell 	} else {
265296f96fcSRusty Russell 		hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
26650c8ea80SRusty Russell 		hdr->gso_size = hdr->hdr_len = 0;
267296f96fcSRusty Russell 	}
268296f96fcSRusty Russell 
269296f96fcSRusty Russell 	vnet_hdr_to_sg(sg, skb);
270296f96fcSRusty Russell 	num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
271296f96fcSRusty Russell 	__skb_queue_head(&vi->send, skb);
2722cb9c6baSRusty Russell 
2732cb9c6baSRusty Russell again:
2742cb9c6baSRusty Russell 	/* Free up any pending old buffers before queueing new ones. */
2752cb9c6baSRusty Russell 	free_old_xmit_skbs(vi);
276296f96fcSRusty Russell 	err = vi->svq->vq_ops->add_buf(vi->svq, sg, num, 0, skb);
277296f96fcSRusty Russell 	if (err) {
278296f96fcSRusty Russell 		pr_debug("%s: virtio not prepared to send\n", dev->name);
279296f96fcSRusty Russell 		netif_stop_queue(dev);
2802cb9c6baSRusty Russell 
2812cb9c6baSRusty Russell 		/* Activate callback for using skbs: if this fails it
2822cb9c6baSRusty Russell 		 * means some were used in the meantime. */
2832cb9c6baSRusty Russell 		if (unlikely(!vi->svq->vq_ops->enable_cb(vi->svq))) {
2842cb9c6baSRusty Russell 			printk("Unlikely: restart svq failed\n");
2852cb9c6baSRusty Russell 			netif_start_queue(dev);
2862cb9c6baSRusty Russell 			goto again;
2872cb9c6baSRusty Russell 		}
2882cb9c6baSRusty Russell 		__skb_unlink(skb, &vi->send);
2892cb9c6baSRusty Russell 
290296f96fcSRusty Russell 		return NETDEV_TX_BUSY;
291296f96fcSRusty Russell 	}
292296f96fcSRusty Russell 	vi->svq->vq_ops->kick(vi->svq);
293296f96fcSRusty Russell 
294296f96fcSRusty Russell 	return 0;
295296f96fcSRusty Russell }
296296f96fcSRusty Russell 
297296f96fcSRusty Russell static int virtnet_open(struct net_device *dev)
298296f96fcSRusty Russell {
299296f96fcSRusty Russell 	struct virtnet_info *vi = netdev_priv(dev);
300296f96fcSRusty Russell 
301296f96fcSRusty Russell 	napi_enable(&vi->napi);
302a48bd8f6SRusty Russell 
303a48bd8f6SRusty Russell 	/* If all buffers were filled by other side before we napi_enabled, we
304a48bd8f6SRusty Russell 	 * won't get another interrupt, so process any outstanding packets
305a48bd8f6SRusty Russell 	 * now.  virtnet_poll wants re-enable the queue, so we disable here. */
306a48bd8f6SRusty Russell 	vi->rvq->vq_ops->disable_cb(vi->rvq);
307a48bd8f6SRusty Russell 	netif_rx_schedule(vi->dev, &vi->napi);
308a48bd8f6SRusty Russell 
309296f96fcSRusty Russell 	return 0;
310296f96fcSRusty Russell }
311296f96fcSRusty Russell 
312296f96fcSRusty Russell static int virtnet_close(struct net_device *dev)
313296f96fcSRusty Russell {
314296f96fcSRusty Russell 	struct virtnet_info *vi = netdev_priv(dev);
315296f96fcSRusty Russell 
316296f96fcSRusty Russell 	napi_disable(&vi->napi);
317296f96fcSRusty Russell 
318296f96fcSRusty Russell 	return 0;
319296f96fcSRusty Russell }
320296f96fcSRusty Russell 
321296f96fcSRusty Russell static int virtnet_probe(struct virtio_device *vdev)
322296f96fcSRusty Russell {
323296f96fcSRusty Russell 	int err;
324296f96fcSRusty Russell 	struct net_device *dev;
325296f96fcSRusty Russell 	struct virtnet_info *vi;
326296f96fcSRusty Russell 
327296f96fcSRusty Russell 	/* Allocate ourselves a network device with room for our info */
328296f96fcSRusty Russell 	dev = alloc_etherdev(sizeof(struct virtnet_info));
329296f96fcSRusty Russell 	if (!dev)
330296f96fcSRusty Russell 		return -ENOMEM;
331296f96fcSRusty Russell 
332296f96fcSRusty Russell 	/* Set up network device as normal. */
333296f96fcSRusty Russell 	dev->open = virtnet_open;
334296f96fcSRusty Russell 	dev->stop = virtnet_close;
335296f96fcSRusty Russell 	dev->hard_start_xmit = start_xmit;
336296f96fcSRusty Russell 	dev->features = NETIF_F_HIGHDMA;
337296f96fcSRusty Russell 	SET_NETDEV_DEV(dev, &vdev->dev);
338296f96fcSRusty Russell 
339296f96fcSRusty Russell 	/* Do we support "hardware" checksums? */
34034a48579SRusty Russell 	if (csum && vdev->config->feature(vdev, VIRTIO_NET_F_CSUM)) {
341296f96fcSRusty Russell 		/* This opens up the world of extra features. */
342296f96fcSRusty Russell 		dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
34334a48579SRusty Russell 		if (gso && vdev->config->feature(vdev, VIRTIO_NET_F_GSO)) {
34434a48579SRusty Russell 			dev->features |= NETIF_F_TSO | NETIF_F_UFO
34534a48579SRusty Russell 				| NETIF_F_TSO_ECN | NETIF_F_TSO6;
34634a48579SRusty Russell 		}
347296f96fcSRusty Russell 	}
348296f96fcSRusty Russell 
349296f96fcSRusty Russell 	/* Configuration may specify what MAC to use.  Otherwise random. */
350a586d4f6SRusty Russell 	if (vdev->config->feature(vdev, VIRTIO_NET_F_MAC)) {
351a586d4f6SRusty Russell 		vdev->config->get(vdev,
352a586d4f6SRusty Russell 				  offsetof(struct virtio_net_config, mac),
353a586d4f6SRusty Russell 				  dev->dev_addr, dev->addr_len);
354296f96fcSRusty Russell 	} else
355296f96fcSRusty Russell 		random_ether_addr(dev->dev_addr);
356296f96fcSRusty Russell 
357296f96fcSRusty Russell 	/* Set up our device-specific information */
358296f96fcSRusty Russell 	vi = netdev_priv(dev);
359*6c0cd7c0SDor Laor 	netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight);
360296f96fcSRusty Russell 	vi->dev = dev;
361296f96fcSRusty Russell 	vi->vdev = vdev;
362296f96fcSRusty Russell 
363296f96fcSRusty Russell 	/* We expect two virtqueues, receive then send. */
364a586d4f6SRusty Russell 	vi->rvq = vdev->config->find_vq(vdev, 0, skb_recv_done);
365296f96fcSRusty Russell 	if (IS_ERR(vi->rvq)) {
366296f96fcSRusty Russell 		err = PTR_ERR(vi->rvq);
367296f96fcSRusty Russell 		goto free;
368296f96fcSRusty Russell 	}
369296f96fcSRusty Russell 
370a586d4f6SRusty Russell 	vi->svq = vdev->config->find_vq(vdev, 1, skb_xmit_done);
371296f96fcSRusty Russell 	if (IS_ERR(vi->svq)) {
372296f96fcSRusty Russell 		err = PTR_ERR(vi->svq);
373296f96fcSRusty Russell 		goto free_recv;
374296f96fcSRusty Russell 	}
375296f96fcSRusty Russell 
376296f96fcSRusty Russell 	/* Initialize our empty receive and send queues. */
377296f96fcSRusty Russell 	skb_queue_head_init(&vi->recv);
378296f96fcSRusty Russell 	skb_queue_head_init(&vi->send);
379296f96fcSRusty Russell 
380296f96fcSRusty Russell 	err = register_netdev(dev);
381296f96fcSRusty Russell 	if (err) {
382296f96fcSRusty Russell 		pr_debug("virtio_net: registering device failed\n");
383296f96fcSRusty Russell 		goto free_send;
384296f96fcSRusty Russell 	}
385b3369c1fSRusty Russell 
386b3369c1fSRusty Russell 	/* Last of all, set up some receive buffers. */
387b3369c1fSRusty Russell 	try_fill_recv(vi);
388b3369c1fSRusty Russell 
389b3369c1fSRusty Russell 	/* If we didn't even get one input buffer, we're useless. */
390b3369c1fSRusty Russell 	if (vi->num == 0) {
391b3369c1fSRusty Russell 		err = -ENOMEM;
392b3369c1fSRusty Russell 		goto unregister;
393b3369c1fSRusty Russell 	}
394b3369c1fSRusty Russell 
395296f96fcSRusty Russell 	pr_debug("virtnet: registered device %s\n", dev->name);
396296f96fcSRusty Russell 	vdev->priv = vi;
397296f96fcSRusty Russell 	return 0;
398296f96fcSRusty Russell 
399b3369c1fSRusty Russell unregister:
400b3369c1fSRusty Russell 	unregister_netdev(dev);
401296f96fcSRusty Russell free_send:
402296f96fcSRusty Russell 	vdev->config->del_vq(vi->svq);
403296f96fcSRusty Russell free_recv:
404296f96fcSRusty Russell 	vdev->config->del_vq(vi->rvq);
405296f96fcSRusty Russell free:
406296f96fcSRusty Russell 	free_netdev(dev);
407296f96fcSRusty Russell 	return err;
408296f96fcSRusty Russell }
409296f96fcSRusty Russell 
410296f96fcSRusty Russell static void virtnet_remove(struct virtio_device *vdev)
411296f96fcSRusty Russell {
41274b2553fSRusty Russell 	struct virtnet_info *vi = vdev->priv;
413b3369c1fSRusty Russell 	struct sk_buff *skb;
414b3369c1fSRusty Russell 
4156e5aa7efSRusty Russell 	/* Stop all the virtqueues. */
4166e5aa7efSRusty Russell 	vdev->config->reset(vdev);
4176e5aa7efSRusty Russell 
418b3369c1fSRusty Russell 	/* Free our skbs in send and recv queues, if any. */
419b3369c1fSRusty Russell 	while ((skb = __skb_dequeue(&vi->recv)) != NULL) {
420b3369c1fSRusty Russell 		kfree_skb(skb);
421b3369c1fSRusty Russell 		vi->num--;
422b3369c1fSRusty Russell 	}
423b3369c1fSRusty Russell 	while ((skb = __skb_dequeue(&vi->send)) != NULL)
424b3369c1fSRusty Russell 		kfree_skb(skb);
425b3369c1fSRusty Russell 
426b3369c1fSRusty Russell 	BUG_ON(vi->num != 0);
42774b2553fSRusty Russell 
42874b2553fSRusty Russell 	vdev->config->del_vq(vi->svq);
42974b2553fSRusty Russell 	vdev->config->del_vq(vi->rvq);
43074b2553fSRusty Russell 	unregister_netdev(vi->dev);
43174b2553fSRusty Russell 	free_netdev(vi->dev);
432296f96fcSRusty Russell }
433296f96fcSRusty Russell 
434296f96fcSRusty Russell static struct virtio_device_id id_table[] = {
435296f96fcSRusty Russell 	{ VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
436296f96fcSRusty Russell 	{ 0 },
437296f96fcSRusty Russell };
438296f96fcSRusty Russell 
439296f96fcSRusty Russell static struct virtio_driver virtio_net = {
440296f96fcSRusty Russell 	.driver.name =	KBUILD_MODNAME,
441296f96fcSRusty Russell 	.driver.owner =	THIS_MODULE,
442296f96fcSRusty Russell 	.id_table =	id_table,
443296f96fcSRusty Russell 	.probe =	virtnet_probe,
444296f96fcSRusty Russell 	.remove =	__devexit_p(virtnet_remove),
445296f96fcSRusty Russell };
446296f96fcSRusty Russell 
447296f96fcSRusty Russell static int __init init(void)
448296f96fcSRusty Russell {
449296f96fcSRusty Russell 	return register_virtio_driver(&virtio_net);
450296f96fcSRusty Russell }
451296f96fcSRusty Russell 
452296f96fcSRusty Russell static void __exit fini(void)
453296f96fcSRusty Russell {
454296f96fcSRusty Russell 	unregister_virtio_driver(&virtio_net);
455296f96fcSRusty Russell }
456296f96fcSRusty Russell module_init(init);
457296f96fcSRusty Russell module_exit(fini);
458296f96fcSRusty Russell 
459296f96fcSRusty Russell MODULE_DEVICE_TABLE(virtio, id_table);
460296f96fcSRusty Russell MODULE_DESCRIPTION("Virtio network driver");
461296f96fcSRusty Russell MODULE_LICENSE("GPL");
462