virtio_net: hook up the set-tso ethtool op
[linux-block.git] / drivers / net / virtio_net.c
CommitLineData
296f96fc
RR
1/* A simple network driver using virtio.
2 *
3 * Copyright 2007 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19//#define DEBUG
20#include <linux/netdevice.h>
21#include <linux/etherdevice.h>
a9ea3fc6 22#include <linux/ethtool.h>
296f96fc
RR
23#include <linux/module.h>
24#include <linux/virtio.h>
25#include <linux/virtio_net.h>
26#include <linux/scatterlist.h>
27
6c0cd7c0
DL
28static int napi_weight = 128;
29module_param(napi_weight, int, 0444);
30
34a48579
RR
31static int csum = 1, gso = 1;
32module_param(csum, bool, 0444);
33module_param(gso, bool, 0444);
34
296f96fc
RR
35/* FIXME: MTU in config. */
36#define MAX_PACKET_LEN (ETH_HLEN+ETH_DATA_LEN)
37
38struct virtnet_info
39{
40 struct virtio_device *vdev;
41 struct virtqueue *rvq, *svq;
42 struct net_device *dev;
43 struct napi_struct napi;
44
99ffc696
RR
45 /* The skb we couldn't send because buffers were full. */
46 struct sk_buff *last_xmit_skb;
47
363f1514 48 /* If we need to free in a timer, this is it. */
14c998f0
MM
49 struct timer_list xmit_free_timer;
50
296f96fc
RR
51 /* Number of input buffers, and max we've ever had. */
52 unsigned int num, max;
53
11a3a154
RR
54 /* For cleaning up after transmission. */
55 struct tasklet_struct tasklet;
363f1514 56 bool free_in_tasklet;
11a3a154 57
97402b96
HX
58 /* I like... big packets and I cannot lie! */
59 bool big_packets;
60
296f96fc
RR
61 /* Receive & send queues. */
62 struct sk_buff_head recv;
63 struct sk_buff_head send;
fb6813f4
RR
64
65 /* Chain pages by the private ptr. */
66 struct page *pages;
296f96fc
RR
67};
68
69static inline struct virtio_net_hdr *skb_vnet_hdr(struct sk_buff *skb)
70{
71 return (struct virtio_net_hdr *)skb->cb;
72}
73
74static inline void vnet_hdr_to_sg(struct scatterlist *sg, struct sk_buff *skb)
75{
76 sg_init_one(sg, skb_vnet_hdr(skb), sizeof(struct virtio_net_hdr));
77}
78
fb6813f4
RR
79static void give_a_page(struct virtnet_info *vi, struct page *page)
80{
81 page->private = (unsigned long)vi->pages;
82 vi->pages = page;
83}
84
0a888fd1
MM
85static void trim_pages(struct virtnet_info *vi, struct sk_buff *skb)
86{
87 unsigned int i;
88
89 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
90 give_a_page(vi, skb_shinfo(skb)->frags[i].page);
91 skb_shinfo(skb)->nr_frags = 0;
92 skb->data_len = 0;
93}
94
fb6813f4
RR
95static struct page *get_a_page(struct virtnet_info *vi, gfp_t gfp_mask)
96{
97 struct page *p = vi->pages;
98
99 if (p)
100 vi->pages = (struct page *)p->private;
101 else
102 p = alloc_page(gfp_mask);
103 return p;
104}
105
2cb9c6ba 106static void skb_xmit_done(struct virtqueue *svq)
296f96fc 107{
2cb9c6ba 108 struct virtnet_info *vi = svq->vdev->priv;
296f96fc 109
2cb9c6ba
RR
110 /* Suppress further interrupts. */
111 svq->vq_ops->disable_cb(svq);
11a3a154 112
363f1514 113 /* We were probably waiting for more output buffers. */
296f96fc 114 netif_wake_queue(vi->dev);
11a3a154
RR
115
116 /* Make sure we re-xmit last_xmit_skb: if there are no more packets
117 * queued, start_xmit won't be called. */
118 tasklet_schedule(&vi->tasklet);
296f96fc
RR
119}
120
121static void receive_skb(struct net_device *dev, struct sk_buff *skb,
122 unsigned len)
123{
124 struct virtio_net_hdr *hdr = skb_vnet_hdr(skb);
97402b96 125 int err;
296f96fc
RR
126
127 if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) {
128 pr_debug("%s: short packet %i\n", dev->name, len);
129 dev->stats.rx_length_errors++;
130 goto drop;
131 }
132 len -= sizeof(struct virtio_net_hdr);
23cde76d 133
0a888fd1
MM
134 if (len <= MAX_PACKET_LEN)
135 trim_pages(netdev_priv(dev), skb);
fb6813f4 136
97402b96
HX
137 err = pskb_trim(skb, len);
138 if (err) {
139 pr_debug("%s: pskb_trim failed %i %d\n", dev->name, len, err);
140 dev->stats.rx_dropped++;
141 goto drop;
142 }
143 skb->truesize += skb->data_len;
296f96fc
RR
144 dev->stats.rx_bytes += skb->len;
145 dev->stats.rx_packets++;
146
147 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
148 pr_debug("Needs csum!\n");
f35d9d8a 149 if (!skb_partial_csum_set(skb,hdr->csum_start,hdr->csum_offset))
296f96fc 150 goto frame_err;
296f96fc
RR
151 }
152
23cde76d
MM
153 skb->protocol = eth_type_trans(skb, dev);
154 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
155 ntohs(skb->protocol), skb->len, skb->pkt_type);
156
296f96fc
RR
157 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
158 pr_debug("GSO!\n");
34a48579 159 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
296f96fc
RR
160 case VIRTIO_NET_HDR_GSO_TCPV4:
161 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
162 break;
296f96fc
RR
163 case VIRTIO_NET_HDR_GSO_UDP:
164 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
165 break;
166 case VIRTIO_NET_HDR_GSO_TCPV6:
167 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
168 break;
169 default:
170 if (net_ratelimit())
171 printk(KERN_WARNING "%s: bad gso type %u.\n",
172 dev->name, hdr->gso_type);
173 goto frame_err;
174 }
175
34a48579
RR
176 if (hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN)
177 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
178
296f96fc
RR
179 skb_shinfo(skb)->gso_size = hdr->gso_size;
180 if (skb_shinfo(skb)->gso_size == 0) {
181 if (net_ratelimit())
182 printk(KERN_WARNING "%s: zero gso size.\n",
183 dev->name);
184 goto frame_err;
185 }
186
187 /* Header must be checked, and gso_segs computed. */
188 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
189 skb_shinfo(skb)->gso_segs = 0;
190 }
191
192 netif_receive_skb(skb);
193 return;
194
195frame_err:
196 dev->stats.rx_frame_errors++;
197drop:
198 dev_kfree_skb(skb);
199}
200
201static void try_fill_recv(struct virtnet_info *vi)
202{
203 struct sk_buff *skb;
05271685 204 struct scatterlist sg[2+MAX_SKB_FRAGS];
97402b96 205 int num, err, i;
296f96fc 206
05271685 207 sg_init_table(sg, 2+MAX_SKB_FRAGS);
296f96fc
RR
208 for (;;) {
209 skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN);
210 if (unlikely(!skb))
211 break;
212
213 skb_put(skb, MAX_PACKET_LEN);
214 vnet_hdr_to_sg(sg, skb);
97402b96
HX
215
216 if (vi->big_packets) {
217 for (i = 0; i < MAX_SKB_FRAGS; i++) {
218 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
fb6813f4 219 f->page = get_a_page(vi, GFP_ATOMIC);
97402b96
HX
220 if (!f->page)
221 break;
222
223 f->page_offset = 0;
224 f->size = PAGE_SIZE;
225
226 skb->data_len += PAGE_SIZE;
227 skb->len += PAGE_SIZE;
228
229 skb_shinfo(skb)->nr_frags++;
230 }
231 }
232
296f96fc
RR
233 num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
234 skb_queue_head(&vi->recv, skb);
235
236 err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, num, skb);
237 if (err) {
238 skb_unlink(skb, &vi->recv);
0a888fd1 239 trim_pages(vi, skb);
296f96fc
RR
240 kfree_skb(skb);
241 break;
242 }
243 vi->num++;
244 }
245 if (unlikely(vi->num > vi->max))
246 vi->max = vi->num;
247 vi->rvq->vq_ops->kick(vi->rvq);
248}
249
18445c4d 250static void skb_recv_done(struct virtqueue *rvq)
296f96fc
RR
251{
252 struct virtnet_info *vi = rvq->vdev->priv;
18445c4d
RR
253 /* Schedule NAPI, Suppress further interrupts if successful. */
254 if (netif_rx_schedule_prep(vi->dev, &vi->napi)) {
255 rvq->vq_ops->disable_cb(rvq);
256 __netif_rx_schedule(vi->dev, &vi->napi);
257 }
296f96fc
RR
258}
259
260static int virtnet_poll(struct napi_struct *napi, int budget)
261{
262 struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi);
263 struct sk_buff *skb = NULL;
264 unsigned int len, received = 0;
265
266again:
267 while (received < budget &&
268 (skb = vi->rvq->vq_ops->get_buf(vi->rvq, &len)) != NULL) {
269 __skb_unlink(skb, &vi->recv);
270 receive_skb(vi->dev, skb, len);
271 vi->num--;
272 received++;
273 }
274
275 /* FIXME: If we oom and completely run out of inbufs, we need
276 * to start a timer trying to fill more. */
277 if (vi->num < vi->max / 2)
278 try_fill_recv(vi);
279
8329d98e
RR
280 /* Out of packets? */
281 if (received < budget) {
296f96fc 282 netif_rx_complete(vi->dev, napi);
18445c4d 283 if (unlikely(!vi->rvq->vq_ops->enable_cb(vi->rvq))
4265f161
CB
284 && napi_schedule_prep(napi)) {
285 vi->rvq->vq_ops->disable_cb(vi->rvq);
286 __netif_rx_schedule(vi->dev, napi);
296f96fc 287 goto again;
4265f161 288 }
296f96fc
RR
289 }
290
291 return received;
292}
293
294static void free_old_xmit_skbs(struct virtnet_info *vi)
295{
296 struct sk_buff *skb;
297 unsigned int len;
298
299 while ((skb = vi->svq->vq_ops->get_buf(vi->svq, &len)) != NULL) {
300 pr_debug("Sent skb %p\n", skb);
301 __skb_unlink(skb, &vi->send);
655aa31f 302 vi->dev->stats.tx_bytes += skb->len;
296f96fc
RR
303 vi->dev->stats.tx_packets++;
304 kfree_skb(skb);
305 }
306}
307
363f1514
RR
308/* If the virtio transport doesn't always notify us when all in-flight packets
309 * are consumed, we fall back to using this function on a timer to free them. */
14c998f0
MM
310static void xmit_free(unsigned long data)
311{
312 struct virtnet_info *vi = (void *)data;
313
314 netif_tx_lock(vi->dev);
315
316 free_old_xmit_skbs(vi);
317
318 if (!skb_queue_empty(&vi->send))
319 mod_timer(&vi->xmit_free_timer, jiffies + (HZ/10));
320
321 netif_tx_unlock(vi->dev);
322}
323
99ffc696 324static int xmit_skb(struct virtnet_info *vi, struct sk_buff *skb)
296f96fc 325{
14c998f0 326 int num, err;
05271685 327 struct scatterlist sg[2+MAX_SKB_FRAGS];
296f96fc
RR
328 struct virtio_net_hdr *hdr;
329 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
296f96fc 330
05271685 331 sg_init_table(sg, 2+MAX_SKB_FRAGS);
4d125de3 332
e174961c 333 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
296f96fc 334
296f96fc
RR
335 /* Encode metadata header at front. */
336 hdr = skb_vnet_hdr(skb);
337 if (skb->ip_summed == CHECKSUM_PARTIAL) {
338 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
339 hdr->csum_start = skb->csum_start - skb_headroom(skb);
340 hdr->csum_offset = skb->csum_offset;
341 } else {
342 hdr->flags = 0;
343 hdr->csum_offset = hdr->csum_start = 0;
344 }
345
346 if (skb_is_gso(skb)) {
50c8ea80 347 hdr->hdr_len = skb_transport_header(skb) - skb->data;
296f96fc 348 hdr->gso_size = skb_shinfo(skb)->gso_size;
34a48579 349 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
296f96fc
RR
350 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
351 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
352 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
353 else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
354 hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
355 else
356 BUG();
34a48579
RR
357 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
358 hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
296f96fc
RR
359 } else {
360 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
50c8ea80 361 hdr->gso_size = hdr->hdr_len = 0;
296f96fc
RR
362 }
363
364 vnet_hdr_to_sg(sg, skb);
365 num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
99ffc696 366
14c998f0 367 err = vi->svq->vq_ops->add_buf(vi->svq, sg, num, 0, skb);
363f1514 368 if (!err && !vi->free_in_tasklet)
14c998f0
MM
369 mod_timer(&vi->xmit_free_timer, jiffies + (HZ/10));
370
371 return err;
99ffc696
RR
372}
373
11a3a154
RR
374static void xmit_tasklet(unsigned long data)
375{
376 struct virtnet_info *vi = (void *)data;
377
378 netif_tx_lock_bh(vi->dev);
379 if (vi->last_xmit_skb && xmit_skb(vi, vi->last_xmit_skb) == 0) {
380 vi->svq->vq_ops->kick(vi->svq);
381 vi->last_xmit_skb = NULL;
382 }
363f1514
RR
383 if (vi->free_in_tasklet)
384 free_old_xmit_skbs(vi);
11a3a154
RR
385 netif_tx_unlock_bh(vi->dev);
386}
387
99ffc696
RR
388static int start_xmit(struct sk_buff *skb, struct net_device *dev)
389{
390 struct virtnet_info *vi = netdev_priv(dev);
2cb9c6ba
RR
391
392again:
393 /* Free up any pending old buffers before queueing new ones. */
394 free_old_xmit_skbs(vi);
99ffc696
RR
395
396 /* If we has a buffer left over from last time, send it now. */
9953ca6c
MM
397 if (unlikely(vi->last_xmit_skb) &&
398 xmit_skb(vi, vi->last_xmit_skb) != 0)
399 goto stop_queue;
400
401 vi->last_xmit_skb = NULL;
2cb9c6ba 402
99ffc696 403 /* Put new one in send queue and do transmit */
7eb2e251
RR
404 if (likely(skb)) {
405 __skb_queue_head(&vi->send, skb);
406 if (xmit_skb(vi, skb) != 0) {
407 vi->last_xmit_skb = skb;
408 skb = NULL;
409 goto stop_queue;
410 }
296f96fc 411 }
99ffc696 412done:
296f96fc 413 vi->svq->vq_ops->kick(vi->svq);
99ffc696
RR
414 return NETDEV_TX_OK;
415
416stop_queue:
417 pr_debug("%s: virtio not prepared to send\n", dev->name);
418 netif_stop_queue(dev);
419
420 /* Activate callback for using skbs: if this returns false it
421 * means some were used in the meantime. */
422 if (unlikely(!vi->svq->vq_ops->enable_cb(vi->svq))) {
423 vi->svq->vq_ops->disable_cb(vi->svq);
424 netif_start_queue(dev);
425 goto again;
426 }
9953ca6c
MM
427 if (skb) {
428 /* Drop this skb: we only queue one. */
429 vi->dev->stats.tx_dropped++;
430 kfree_skb(skb);
431 }
99ffc696 432 goto done;
296f96fc
RR
433}
434
da74e89d
AS
435#ifdef CONFIG_NET_POLL_CONTROLLER
436static void virtnet_netpoll(struct net_device *dev)
437{
438 struct virtnet_info *vi = netdev_priv(dev);
439
440 napi_schedule(&vi->napi);
441}
442#endif
443
296f96fc
RR
444static int virtnet_open(struct net_device *dev)
445{
446 struct virtnet_info *vi = netdev_priv(dev);
447
296f96fc 448 napi_enable(&vi->napi);
a48bd8f6
RR
449
450 /* If all buffers were filled by other side before we napi_enabled, we
451 * won't get another interrupt, so process any outstanding packets
370076d9
CB
452 * now. virtnet_poll wants re-enable the queue, so we disable here.
453 * We synchronize against interrupts via NAPI_STATE_SCHED */
454 if (netif_rx_schedule_prep(dev, &vi->napi)) {
455 vi->rvq->vq_ops->disable_cb(vi->rvq);
456 __netif_rx_schedule(dev, &vi->napi);
457 }
296f96fc
RR
458 return 0;
459}
460
461static int virtnet_close(struct net_device *dev)
462{
463 struct virtnet_info *vi = netdev_priv(dev);
296f96fc
RR
464
465 napi_disable(&vi->napi);
466
296f96fc
RR
467 return 0;
468}
469
a9ea3fc6
HX
470static int virtnet_set_tx_csum(struct net_device *dev, u32 data)
471{
472 struct virtnet_info *vi = netdev_priv(dev);
473 struct virtio_device *vdev = vi->vdev;
474
475 if (data && !virtio_has_feature(vdev, VIRTIO_NET_F_CSUM))
476 return -ENOSYS;
477
478 return ethtool_op_set_tx_hw_csum(dev, data);
479}
480
481static struct ethtool_ops virtnet_ethtool_ops = {
482 .set_tx_csum = virtnet_set_tx_csum,
483 .set_sg = ethtool_op_set_sg,
0276b497 484 .set_tso = ethtool_op_set_tso,
a9ea3fc6
HX
485};
486
296f96fc
RR
487static int virtnet_probe(struct virtio_device *vdev)
488{
489 int err;
296f96fc
RR
490 struct net_device *dev;
491 struct virtnet_info *vi;
296f96fc
RR
492
493 /* Allocate ourselves a network device with room for our info */
494 dev = alloc_etherdev(sizeof(struct virtnet_info));
495 if (!dev)
496 return -ENOMEM;
497
498 /* Set up network device as normal. */
296f96fc
RR
499 dev->open = virtnet_open;
500 dev->stop = virtnet_close;
501 dev->hard_start_xmit = start_xmit;
502 dev->features = NETIF_F_HIGHDMA;
da74e89d
AS
503#ifdef CONFIG_NET_POLL_CONTROLLER
504 dev->poll_controller = virtnet_netpoll;
505#endif
a9ea3fc6 506 SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops);
296f96fc
RR
507 SET_NETDEV_DEV(dev, &vdev->dev);
508
509 /* Do we support "hardware" checksums? */
c45a6816 510 if (csum && virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
296f96fc
RR
511 /* This opens up the world of extra features. */
512 dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
c45a6816 513 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
34a48579
RR
514 dev->features |= NETIF_F_TSO | NETIF_F_UFO
515 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
516 }
5539ae96 517 /* Individual feature bits: what can host handle? */
c45a6816 518 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
5539ae96 519 dev->features |= NETIF_F_TSO;
c45a6816 520 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
5539ae96 521 dev->features |= NETIF_F_TSO6;
c45a6816 522 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
5539ae96 523 dev->features |= NETIF_F_TSO_ECN;
c45a6816 524 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
5539ae96 525 dev->features |= NETIF_F_UFO;
296f96fc
RR
526 }
527
528 /* Configuration may specify what MAC to use. Otherwise random. */
c45a6816 529 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
a586d4f6
RR
530 vdev->config->get(vdev,
531 offsetof(struct virtio_net_config, mac),
532 dev->dev_addr, dev->addr_len);
296f96fc
RR
533 } else
534 random_ether_addr(dev->dev_addr);
535
536 /* Set up our device-specific information */
537 vi = netdev_priv(dev);
6c0cd7c0 538 netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight);
296f96fc
RR
539 vi->dev = dev;
540 vi->vdev = vdev;
d9d5dcc8 541 vdev->priv = vi;
fb6813f4 542 vi->pages = NULL;
296f96fc 543
363f1514
RR
544 /* If they give us a callback when all buffers are done, we don't need
545 * the timer. */
546 vi->free_in_tasklet = virtio_has_feature(vdev,VIRTIO_F_NOTIFY_ON_EMPTY);
547
97402b96
HX
548 /* If we can receive ANY GSO packets, we must allocate large ones. */
549 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4)
550 || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6)
551 || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN))
552 vi->big_packets = true;
553
296f96fc 554 /* We expect two virtqueues, receive then send. */
a586d4f6 555 vi->rvq = vdev->config->find_vq(vdev, 0, skb_recv_done);
296f96fc
RR
556 if (IS_ERR(vi->rvq)) {
557 err = PTR_ERR(vi->rvq);
558 goto free;
559 }
560
a586d4f6 561 vi->svq = vdev->config->find_vq(vdev, 1, skb_xmit_done);
296f96fc
RR
562 if (IS_ERR(vi->svq)) {
563 err = PTR_ERR(vi->svq);
564 goto free_recv;
565 }
566
567 /* Initialize our empty receive and send queues. */
568 skb_queue_head_init(&vi->recv);
569 skb_queue_head_init(&vi->send);
570
11a3a154
RR
571 tasklet_init(&vi->tasklet, xmit_tasklet, (unsigned long)vi);
572
363f1514
RR
573 if (!vi->free_in_tasklet)
574 setup_timer(&vi->xmit_free_timer, xmit_free, (unsigned long)vi);
14c998f0 575
296f96fc
RR
576 err = register_netdev(dev);
577 if (err) {
578 pr_debug("virtio_net: registering device failed\n");
579 goto free_send;
580 }
b3369c1f
RR
581
582 /* Last of all, set up some receive buffers. */
583 try_fill_recv(vi);
584
585 /* If we didn't even get one input buffer, we're useless. */
586 if (vi->num == 0) {
587 err = -ENOMEM;
588 goto unregister;
589 }
590
296f96fc 591 pr_debug("virtnet: registered device %s\n", dev->name);
296f96fc
RR
592 return 0;
593
b3369c1f
RR
594unregister:
595 unregister_netdev(dev);
296f96fc
RR
596free_send:
597 vdev->config->del_vq(vi->svq);
598free_recv:
599 vdev->config->del_vq(vi->rvq);
600free:
601 free_netdev(dev);
602 return err;
603}
604
605static void virtnet_remove(struct virtio_device *vdev)
606{
74b2553f 607 struct virtnet_info *vi = vdev->priv;
b3369c1f
RR
608 struct sk_buff *skb;
609
6e5aa7ef
RR
610 /* Stop all the virtqueues. */
611 vdev->config->reset(vdev);
612
363f1514
RR
613 if (!vi->free_in_tasklet)
614 del_timer_sync(&vi->xmit_free_timer);
14c998f0 615
b3369c1f 616 /* Free our skbs in send and recv queues, if any. */
b3369c1f
RR
617 while ((skb = __skb_dequeue(&vi->recv)) != NULL) {
618 kfree_skb(skb);
619 vi->num--;
620 }
288369cc 621 __skb_queue_purge(&vi->send);
b3369c1f
RR
622
623 BUG_ON(vi->num != 0);
74b2553f
RR
624
625 vdev->config->del_vq(vi->svq);
626 vdev->config->del_vq(vi->rvq);
627 unregister_netdev(vi->dev);
fb6813f4
RR
628
629 while (vi->pages)
630 __free_pages(get_a_page(vi, GFP_KERNEL), 0);
631
74b2553f 632 free_netdev(vi->dev);
296f96fc
RR
633}
634
635static struct virtio_device_id id_table[] = {
636 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
637 { 0 },
638};
639
c45a6816 640static unsigned int features[] = {
5e4fe5c4
MM
641 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM,
642 VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
c45a6816 643 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
97402b96
HX
644 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
645 VIRTIO_NET_F_GUEST_ECN, /* We don't yet handle UFO input. */
646 VIRTIO_F_NOTIFY_ON_EMPTY,
c45a6816
RR
647};
648
296f96fc 649static struct virtio_driver virtio_net = {
c45a6816
RR
650 .feature_table = features,
651 .feature_table_size = ARRAY_SIZE(features),
296f96fc
RR
652 .driver.name = KBUILD_MODNAME,
653 .driver.owner = THIS_MODULE,
654 .id_table = id_table,
655 .probe = virtnet_probe,
656 .remove = __devexit_p(virtnet_remove),
657};
658
659static int __init init(void)
660{
661 return register_virtio_driver(&virtio_net);
662}
663
664static void __exit fini(void)
665{
666 unregister_virtio_driver(&virtio_net);
667}
668module_init(init);
669module_exit(fini);
670
671MODULE_DEVICE_TABLE(virtio, id_table);
672MODULE_DESCRIPTION("Virtio network driver");
673MODULE_LICENSE("GPL");