iwlwifi: allow user to set max rate
[linux-2.6] / drivers / net / virtio_net.c
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>
22 #include <linux/ethtool.h>
23 #include <linux/module.h>
24 #include <linux/virtio.h>
25 #include <linux/virtio_net.h>
26 #include <linux/scatterlist.h>
27 #include <linux/if_vlan.h>
28
29 static int napi_weight = 128;
30 module_param(napi_weight, int, 0444);
31
32 static int csum = 1, gso = 1;
33 module_param(csum, bool, 0444);
34 module_param(gso, bool, 0444);
35
36 /* FIXME: MTU in config. */
37 #define MAX_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
38 #define GOOD_COPY_LEN   128
39
40 struct virtnet_info
41 {
42         struct virtio_device *vdev;
43         struct virtqueue *rvq, *svq;
44         struct net_device *dev;
45         struct napi_struct napi;
46         unsigned int status;
47
48         /* The skb we couldn't send because buffers were full. */
49         struct sk_buff *last_xmit_skb;
50
51         /* If we need to free in a timer, this is it. */
52         struct timer_list xmit_free_timer;
53
54         /* Number of input buffers, and max we've ever had. */
55         unsigned int num, max;
56
57         /* For cleaning up after transmission. */
58         struct tasklet_struct tasklet;
59         bool free_in_tasklet;
60
61         /* I like... big packets and I cannot lie! */
62         bool big_packets;
63
64         /* Host will merge rx buffers for big packets (shake it! shake it!) */
65         bool mergeable_rx_bufs;
66
67         /* Receive & send queues. */
68         struct sk_buff_head recv;
69         struct sk_buff_head send;
70
71         /* Chain pages by the private ptr. */
72         struct page *pages;
73 };
74
75 static inline void *skb_vnet_hdr(struct sk_buff *skb)
76 {
77         return (struct virtio_net_hdr *)skb->cb;
78 }
79
80 static void give_a_page(struct virtnet_info *vi, struct page *page)
81 {
82         page->private = (unsigned long)vi->pages;
83         vi->pages = page;
84 }
85
86 static void trim_pages(struct virtnet_info *vi, struct sk_buff *skb)
87 {
88         unsigned int i;
89
90         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
91                 give_a_page(vi, skb_shinfo(skb)->frags[i].page);
92         skb_shinfo(skb)->nr_frags = 0;
93         skb->data_len = 0;
94 }
95
96 static struct page *get_a_page(struct virtnet_info *vi, gfp_t gfp_mask)
97 {
98         struct page *p = vi->pages;
99
100         if (p)
101                 vi->pages = (struct page *)p->private;
102         else
103                 p = alloc_page(gfp_mask);
104         return p;
105 }
106
107 static void skb_xmit_done(struct virtqueue *svq)
108 {
109         struct virtnet_info *vi = svq->vdev->priv;
110
111         /* Suppress further interrupts. */
112         svq->vq_ops->disable_cb(svq);
113
114         /* We were probably waiting for more output buffers. */
115         netif_wake_queue(vi->dev);
116
117         /* Make sure we re-xmit last_xmit_skb: if there are no more packets
118          * queued, start_xmit won't be called. */
119         tasklet_schedule(&vi->tasklet);
120 }
121
122 static void receive_skb(struct net_device *dev, struct sk_buff *skb,
123                         unsigned len)
124 {
125         struct virtnet_info *vi = netdev_priv(dev);
126         struct virtio_net_hdr *hdr = skb_vnet_hdr(skb);
127         int err;
128         int i;
129
130         if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) {
131                 pr_debug("%s: short packet %i\n", dev->name, len);
132                 dev->stats.rx_length_errors++;
133                 goto drop;
134         }
135
136         if (vi->mergeable_rx_bufs) {
137                 struct virtio_net_hdr_mrg_rxbuf *mhdr = skb_vnet_hdr(skb);
138                 unsigned int copy;
139                 char *p = page_address(skb_shinfo(skb)->frags[0].page);
140
141                 if (len > PAGE_SIZE)
142                         len = PAGE_SIZE;
143                 len -= sizeof(struct virtio_net_hdr_mrg_rxbuf);
144
145                 memcpy(hdr, p, sizeof(*mhdr));
146                 p += sizeof(*mhdr);
147
148                 copy = len;
149                 if (copy > skb_tailroom(skb))
150                         copy = skb_tailroom(skb);
151
152                 memcpy(skb_put(skb, copy), p, copy);
153
154                 len -= copy;
155
156                 if (!len) {
157                         give_a_page(vi, skb_shinfo(skb)->frags[0].page);
158                         skb_shinfo(skb)->nr_frags--;
159                 } else {
160                         skb_shinfo(skb)->frags[0].page_offset +=
161                                 sizeof(*mhdr) + copy;
162                         skb_shinfo(skb)->frags[0].size = len;
163                         skb->data_len += len;
164                         skb->len += len;
165                 }
166
167                 while (--mhdr->num_buffers) {
168                         struct sk_buff *nskb;
169
170                         i = skb_shinfo(skb)->nr_frags;
171                         if (i >= MAX_SKB_FRAGS) {
172                                 pr_debug("%s: packet too long %d\n", dev->name,
173                                          len);
174                                 dev->stats.rx_length_errors++;
175                                 goto drop;
176                         }
177
178                         nskb = vi->rvq->vq_ops->get_buf(vi->rvq, &len);
179                         if (!nskb) {
180                                 pr_debug("%s: rx error: %d buffers missing\n",
181                                          dev->name, mhdr->num_buffers);
182                                 dev->stats.rx_length_errors++;
183                                 goto drop;
184                         }
185
186                         __skb_unlink(nskb, &vi->recv);
187                         vi->num--;
188
189                         skb_shinfo(skb)->frags[i] = skb_shinfo(nskb)->frags[0];
190                         skb_shinfo(nskb)->nr_frags = 0;
191                         kfree_skb(nskb);
192
193                         if (len > PAGE_SIZE)
194                                 len = PAGE_SIZE;
195
196                         skb_shinfo(skb)->frags[i].size = len;
197                         skb_shinfo(skb)->nr_frags++;
198                         skb->data_len += len;
199                         skb->len += len;
200                 }
201         } else {
202                 len -= sizeof(struct virtio_net_hdr);
203
204                 if (len <= MAX_PACKET_LEN)
205                         trim_pages(vi, skb);
206
207                 err = pskb_trim(skb, len);
208                 if (err) {
209                         pr_debug("%s: pskb_trim failed %i %d\n", dev->name,
210                                  len, err);
211                         dev->stats.rx_dropped++;
212                         goto drop;
213                 }
214         }
215
216         skb->truesize += skb->data_len;
217         dev->stats.rx_bytes += skb->len;
218         dev->stats.rx_packets++;
219
220         if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
221                 pr_debug("Needs csum!\n");
222                 if (!skb_partial_csum_set(skb,hdr->csum_start,hdr->csum_offset))
223                         goto frame_err;
224         }
225
226         skb->protocol = eth_type_trans(skb, dev);
227         pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
228                  ntohs(skb->protocol), skb->len, skb->pkt_type);
229
230         if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
231                 pr_debug("GSO!\n");
232                 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
233                 case VIRTIO_NET_HDR_GSO_TCPV4:
234                         skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
235                         break;
236                 case VIRTIO_NET_HDR_GSO_UDP:
237                         skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
238                         break;
239                 case VIRTIO_NET_HDR_GSO_TCPV6:
240                         skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
241                         break;
242                 default:
243                         if (net_ratelimit())
244                                 printk(KERN_WARNING "%s: bad gso type %u.\n",
245                                        dev->name, hdr->gso_type);
246                         goto frame_err;
247                 }
248
249                 if (hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN)
250                         skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
251
252                 skb_shinfo(skb)->gso_size = hdr->gso_size;
253                 if (skb_shinfo(skb)->gso_size == 0) {
254                         if (net_ratelimit())
255                                 printk(KERN_WARNING "%s: zero gso size.\n",
256                                        dev->name);
257                         goto frame_err;
258                 }
259
260                 /* Header must be checked, and gso_segs computed. */
261                 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
262                 skb_shinfo(skb)->gso_segs = 0;
263         }
264
265         netif_receive_skb(skb);
266         return;
267
268 frame_err:
269         dev->stats.rx_frame_errors++;
270 drop:
271         dev_kfree_skb(skb);
272 }
273
274 static void try_fill_recv_maxbufs(struct virtnet_info *vi)
275 {
276         struct sk_buff *skb;
277         struct scatterlist sg[2+MAX_SKB_FRAGS];
278         int num, err, i;
279
280         sg_init_table(sg, 2+MAX_SKB_FRAGS);
281         for (;;) {
282                 struct virtio_net_hdr *hdr;
283
284                 skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN);
285                 if (unlikely(!skb))
286                         break;
287
288                 skb_put(skb, MAX_PACKET_LEN);
289
290                 hdr = skb_vnet_hdr(skb);
291                 sg_init_one(sg, hdr, sizeof(*hdr));
292
293                 if (vi->big_packets) {
294                         for (i = 0; i < MAX_SKB_FRAGS; i++) {
295                                 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
296                                 f->page = get_a_page(vi, GFP_ATOMIC);
297                                 if (!f->page)
298                                         break;
299
300                                 f->page_offset = 0;
301                                 f->size = PAGE_SIZE;
302
303                                 skb->data_len += PAGE_SIZE;
304                                 skb->len += PAGE_SIZE;
305
306                                 skb_shinfo(skb)->nr_frags++;
307                         }
308                 }
309
310                 num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
311                 skb_queue_head(&vi->recv, skb);
312
313                 err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, num, skb);
314                 if (err) {
315                         skb_unlink(skb, &vi->recv);
316                         trim_pages(vi, skb);
317                         kfree_skb(skb);
318                         break;
319                 }
320                 vi->num++;
321         }
322         if (unlikely(vi->num > vi->max))
323                 vi->max = vi->num;
324         vi->rvq->vq_ops->kick(vi->rvq);
325 }
326
327 static void try_fill_recv(struct virtnet_info *vi)
328 {
329         struct sk_buff *skb;
330         struct scatterlist sg[1];
331         int err;
332
333         if (!vi->mergeable_rx_bufs) {
334                 try_fill_recv_maxbufs(vi);
335                 return;
336         }
337
338         for (;;) {
339                 skb_frag_t *f;
340
341                 skb = netdev_alloc_skb(vi->dev, GOOD_COPY_LEN + NET_IP_ALIGN);
342                 if (unlikely(!skb))
343                         break;
344
345                 skb_reserve(skb, NET_IP_ALIGN);
346
347                 f = &skb_shinfo(skb)->frags[0];
348                 f->page = get_a_page(vi, GFP_ATOMIC);
349                 if (!f->page) {
350                         kfree_skb(skb);
351                         break;
352                 }
353
354                 f->page_offset = 0;
355                 f->size = PAGE_SIZE;
356
357                 skb_shinfo(skb)->nr_frags++;
358
359                 sg_init_one(sg, page_address(f->page), PAGE_SIZE);
360                 skb_queue_head(&vi->recv, skb);
361
362                 err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, 1, skb);
363                 if (err) {
364                         skb_unlink(skb, &vi->recv);
365                         kfree_skb(skb);
366                         break;
367                 }
368                 vi->num++;
369         }
370         if (unlikely(vi->num > vi->max))
371                 vi->max = vi->num;
372         vi->rvq->vq_ops->kick(vi->rvq);
373 }
374
375 static void skb_recv_done(struct virtqueue *rvq)
376 {
377         struct virtnet_info *vi = rvq->vdev->priv;
378         /* Schedule NAPI, Suppress further interrupts if successful. */
379         if (napi_schedule_prep(&vi->napi)) {
380                 rvq->vq_ops->disable_cb(rvq);
381                 __napi_schedule(&vi->napi);
382         }
383 }
384
385 static int virtnet_poll(struct napi_struct *napi, int budget)
386 {
387         struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi);
388         struct sk_buff *skb = NULL;
389         unsigned int len, received = 0;
390
391 again:
392         while (received < budget &&
393                (skb = vi->rvq->vq_ops->get_buf(vi->rvq, &len)) != NULL) {
394                 __skb_unlink(skb, &vi->recv);
395                 receive_skb(vi->dev, skb, len);
396                 vi->num--;
397                 received++;
398         }
399
400         /* FIXME: If we oom and completely run out of inbufs, we need
401          * to start a timer trying to fill more. */
402         if (vi->num < vi->max / 2)
403                 try_fill_recv(vi);
404
405         /* Out of packets? */
406         if (received < budget) {
407                 napi_complete(napi);
408                 if (unlikely(!vi->rvq->vq_ops->enable_cb(vi->rvq))
409                     && napi_schedule_prep(napi)) {
410                         vi->rvq->vq_ops->disable_cb(vi->rvq);
411                         __napi_schedule(napi);
412                         goto again;
413                 }
414         }
415
416         return received;
417 }
418
419 static void free_old_xmit_skbs(struct virtnet_info *vi)
420 {
421         struct sk_buff *skb;
422         unsigned int len;
423
424         while ((skb = vi->svq->vq_ops->get_buf(vi->svq, &len)) != NULL) {
425                 pr_debug("Sent skb %p\n", skb);
426                 __skb_unlink(skb, &vi->send);
427                 vi->dev->stats.tx_bytes += skb->len;
428                 vi->dev->stats.tx_packets++;
429                 kfree_skb(skb);
430         }
431 }
432
433 /* If the virtio transport doesn't always notify us when all in-flight packets
434  * are consumed, we fall back to using this function on a timer to free them. */
435 static void xmit_free(unsigned long data)
436 {
437         struct virtnet_info *vi = (void *)data;
438
439         netif_tx_lock(vi->dev);
440
441         free_old_xmit_skbs(vi);
442
443         if (!skb_queue_empty(&vi->send))
444                 mod_timer(&vi->xmit_free_timer, jiffies + (HZ/10));
445
446         netif_tx_unlock(vi->dev);
447 }
448
449 static int xmit_skb(struct virtnet_info *vi, struct sk_buff *skb)
450 {
451         int num, err;
452         struct scatterlist sg[2+MAX_SKB_FRAGS];
453         struct virtio_net_hdr_mrg_rxbuf *mhdr = skb_vnet_hdr(skb);
454         struct virtio_net_hdr *hdr = skb_vnet_hdr(skb);
455         const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
456
457         sg_init_table(sg, 2+MAX_SKB_FRAGS);
458
459         pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
460
461         if (skb->ip_summed == CHECKSUM_PARTIAL) {
462                 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
463                 hdr->csum_start = skb->csum_start - skb_headroom(skb);
464                 hdr->csum_offset = skb->csum_offset;
465         } else {
466                 hdr->flags = 0;
467                 hdr->csum_offset = hdr->csum_start = 0;
468         }
469
470         if (skb_is_gso(skb)) {
471                 hdr->hdr_len = skb_transport_header(skb) - skb->data;
472                 hdr->gso_size = skb_shinfo(skb)->gso_size;
473                 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
474                         hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
475                 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
476                         hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
477                 else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
478                         hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
479                 else
480                         BUG();
481                 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
482                         hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
483         } else {
484                 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
485                 hdr->gso_size = hdr->hdr_len = 0;
486         }
487
488         mhdr->num_buffers = 0;
489
490         /* Encode metadata header at front. */
491         if (vi->mergeable_rx_bufs)
492                 sg_init_one(sg, mhdr, sizeof(*mhdr));
493         else
494                 sg_init_one(sg, hdr, sizeof(*hdr));
495
496         num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
497
498         err = vi->svq->vq_ops->add_buf(vi->svq, sg, num, 0, skb);
499         if (!err && !vi->free_in_tasklet)
500                 mod_timer(&vi->xmit_free_timer, jiffies + (HZ/10));
501
502         return err;
503 }
504
505 static void xmit_tasklet(unsigned long data)
506 {
507         struct virtnet_info *vi = (void *)data;
508
509         netif_tx_lock_bh(vi->dev);
510         if (vi->last_xmit_skb && xmit_skb(vi, vi->last_xmit_skb) == 0) {
511                 vi->svq->vq_ops->kick(vi->svq);
512                 vi->last_xmit_skb = NULL;
513         }
514         if (vi->free_in_tasklet)
515                 free_old_xmit_skbs(vi);
516         netif_tx_unlock_bh(vi->dev);
517 }
518
519 static int start_xmit(struct sk_buff *skb, struct net_device *dev)
520 {
521         struct virtnet_info *vi = netdev_priv(dev);
522
523 again:
524         /* Free up any pending old buffers before queueing new ones. */
525         free_old_xmit_skbs(vi);
526
527         /* If we has a buffer left over from last time, send it now. */
528         if (unlikely(vi->last_xmit_skb) &&
529             xmit_skb(vi, vi->last_xmit_skb) != 0)
530                 goto stop_queue;
531
532         vi->last_xmit_skb = NULL;
533
534         /* Put new one in send queue and do transmit */
535         if (likely(skb)) {
536                 __skb_queue_head(&vi->send, skb);
537                 if (xmit_skb(vi, skb) != 0) {
538                         vi->last_xmit_skb = skb;
539                         skb = NULL;
540                         goto stop_queue;
541                 }
542         }
543 done:
544         vi->svq->vq_ops->kick(vi->svq);
545         return NETDEV_TX_OK;
546
547 stop_queue:
548         pr_debug("%s: virtio not prepared to send\n", dev->name);
549         netif_stop_queue(dev);
550
551         /* Activate callback for using skbs: if this returns false it
552          * means some were used in the meantime. */
553         if (unlikely(!vi->svq->vq_ops->enable_cb(vi->svq))) {
554                 vi->svq->vq_ops->disable_cb(vi->svq);
555                 netif_start_queue(dev);
556                 goto again;
557         }
558         if (skb) {
559                 /* Drop this skb: we only queue one. */
560                 vi->dev->stats.tx_dropped++;
561                 kfree_skb(skb);
562         }
563         goto done;
564 }
565
566 #ifdef CONFIG_NET_POLL_CONTROLLER
567 static void virtnet_netpoll(struct net_device *dev)
568 {
569         struct virtnet_info *vi = netdev_priv(dev);
570
571         napi_schedule(&vi->napi);
572 }
573 #endif
574
575 static int virtnet_open(struct net_device *dev)
576 {
577         struct virtnet_info *vi = netdev_priv(dev);
578
579         napi_enable(&vi->napi);
580
581         /* If all buffers were filled by other side before we napi_enabled, we
582          * won't get another interrupt, so process any outstanding packets
583          * now.  virtnet_poll wants re-enable the queue, so we disable here.
584          * We synchronize against interrupts via NAPI_STATE_SCHED */
585         if (napi_schedule_prep(&vi->napi)) {
586                 vi->rvq->vq_ops->disable_cb(vi->rvq);
587                 __napi_schedule(&vi->napi);
588         }
589         return 0;
590 }
591
592 static int virtnet_close(struct net_device *dev)
593 {
594         struct virtnet_info *vi = netdev_priv(dev);
595
596         napi_disable(&vi->napi);
597
598         return 0;
599 }
600
601 static int virtnet_set_tx_csum(struct net_device *dev, u32 data)
602 {
603         struct virtnet_info *vi = netdev_priv(dev);
604         struct virtio_device *vdev = vi->vdev;
605
606         if (data && !virtio_has_feature(vdev, VIRTIO_NET_F_CSUM))
607                 return -ENOSYS;
608
609         return ethtool_op_set_tx_hw_csum(dev, data);
610 }
611
612 static struct ethtool_ops virtnet_ethtool_ops = {
613         .set_tx_csum = virtnet_set_tx_csum,
614         .set_sg = ethtool_op_set_sg,
615         .set_tso = ethtool_op_set_tso,
616         .get_link = ethtool_op_get_link,
617 };
618
619 #define MIN_MTU 68
620 #define MAX_MTU 65535
621
622 static int virtnet_change_mtu(struct net_device *dev, int new_mtu)
623 {
624         if (new_mtu < MIN_MTU || new_mtu > MAX_MTU)
625                 return -EINVAL;
626         dev->mtu = new_mtu;
627         return 0;
628 }
629
630 static const struct net_device_ops virtnet_netdev = {
631         .ndo_open            = virtnet_open,
632         .ndo_stop            = virtnet_close,
633         .ndo_start_xmit      = start_xmit,
634         .ndo_validate_addr   = eth_validate_addr,
635         .ndo_set_mac_address = eth_mac_addr,
636         .ndo_change_mtu      = virtnet_change_mtu,
637 #ifdef CONFIG_NET_POLL_CONTROLLER
638         .ndo_poll_controller = virtnet_netpoll,
639 #endif
640 };
641
642 static void virtnet_update_status(struct virtnet_info *vi)
643 {
644         u16 v;
645
646         if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS))
647                 return;
648
649         vi->vdev->config->get(vi->vdev,
650                               offsetof(struct virtio_net_config, status),
651                               &v, sizeof(v));
652
653         /* Ignore unknown (future) status bits */
654         v &= VIRTIO_NET_S_LINK_UP;
655
656         if (vi->status == v)
657                 return;
658
659         vi->status = v;
660
661         if (vi->status & VIRTIO_NET_S_LINK_UP) {
662                 netif_carrier_on(vi->dev);
663                 netif_wake_queue(vi->dev);
664         } else {
665                 netif_carrier_off(vi->dev);
666                 netif_stop_queue(vi->dev);
667         }
668 }
669
670 static void virtnet_config_changed(struct virtio_device *vdev)
671 {
672         struct virtnet_info *vi = vdev->priv;
673
674         virtnet_update_status(vi);
675 }
676
677 static int virtnet_probe(struct virtio_device *vdev)
678 {
679         int err;
680         struct net_device *dev;
681         struct virtnet_info *vi;
682
683         /* Allocate ourselves a network device with room for our info */
684         dev = alloc_etherdev(sizeof(struct virtnet_info));
685         if (!dev)
686                 return -ENOMEM;
687
688         /* Set up network device as normal. */
689         dev->netdev_ops = &virtnet_netdev;
690         dev->features = NETIF_F_HIGHDMA;
691         SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops);
692         SET_NETDEV_DEV(dev, &vdev->dev);
693
694         /* Do we support "hardware" checksums? */
695         if (csum && virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
696                 /* This opens up the world of extra features. */
697                 dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
698                 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
699                         dev->features |= NETIF_F_TSO | NETIF_F_UFO
700                                 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
701                 }
702                 /* Individual feature bits: what can host handle? */
703                 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
704                         dev->features |= NETIF_F_TSO;
705                 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
706                         dev->features |= NETIF_F_TSO6;
707                 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
708                         dev->features |= NETIF_F_TSO_ECN;
709                 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
710                         dev->features |= NETIF_F_UFO;
711         }
712
713         /* Configuration may specify what MAC to use.  Otherwise random. */
714         if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
715                 vdev->config->get(vdev,
716                                   offsetof(struct virtio_net_config, mac),
717                                   dev->dev_addr, dev->addr_len);
718         } else
719                 random_ether_addr(dev->dev_addr);
720
721         /* Set up our device-specific information */
722         vi = netdev_priv(dev);
723         netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight);
724         vi->dev = dev;
725         vi->vdev = vdev;
726         vdev->priv = vi;
727         vi->pages = NULL;
728
729         /* If they give us a callback when all buffers are done, we don't need
730          * the timer. */
731         vi->free_in_tasklet = virtio_has_feature(vdev,VIRTIO_F_NOTIFY_ON_EMPTY);
732
733         /* If we can receive ANY GSO packets, we must allocate large ones. */
734         if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4)
735             || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6)
736             || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN))
737                 vi->big_packets = true;
738
739         if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
740                 vi->mergeable_rx_bufs = true;
741
742         /* We expect two virtqueues, receive then send. */
743         vi->rvq = vdev->config->find_vq(vdev, 0, skb_recv_done);
744         if (IS_ERR(vi->rvq)) {
745                 err = PTR_ERR(vi->rvq);
746                 goto free;
747         }
748
749         vi->svq = vdev->config->find_vq(vdev, 1, skb_xmit_done);
750         if (IS_ERR(vi->svq)) {
751                 err = PTR_ERR(vi->svq);
752                 goto free_recv;
753         }
754
755         /* Initialize our empty receive and send queues. */
756         skb_queue_head_init(&vi->recv);
757         skb_queue_head_init(&vi->send);
758
759         tasklet_init(&vi->tasklet, xmit_tasklet, (unsigned long)vi);
760
761         if (!vi->free_in_tasklet)
762                 setup_timer(&vi->xmit_free_timer, xmit_free, (unsigned long)vi);
763
764         err = register_netdev(dev);
765         if (err) {
766                 pr_debug("virtio_net: registering device failed\n");
767                 goto free_send;
768         }
769
770         /* Last of all, set up some receive buffers. */
771         try_fill_recv(vi);
772
773         /* If we didn't even get one input buffer, we're useless. */
774         if (vi->num == 0) {
775                 err = -ENOMEM;
776                 goto unregister;
777         }
778
779         vi->status = VIRTIO_NET_S_LINK_UP;
780         virtnet_update_status(vi);
781
782         pr_debug("virtnet: registered device %s\n", dev->name);
783         return 0;
784
785 unregister:
786         unregister_netdev(dev);
787 free_send:
788         vdev->config->del_vq(vi->svq);
789 free_recv:
790         vdev->config->del_vq(vi->rvq);
791 free:
792         free_netdev(dev);
793         return err;
794 }
795
796 static void virtnet_remove(struct virtio_device *vdev)
797 {
798         struct virtnet_info *vi = vdev->priv;
799         struct sk_buff *skb;
800
801         /* Stop all the virtqueues. */
802         vdev->config->reset(vdev);
803
804         if (!vi->free_in_tasklet)
805                 del_timer_sync(&vi->xmit_free_timer);
806
807         /* Free our skbs in send and recv queues, if any. */
808         while ((skb = __skb_dequeue(&vi->recv)) != NULL) {
809                 kfree_skb(skb);
810                 vi->num--;
811         }
812         __skb_queue_purge(&vi->send);
813
814         BUG_ON(vi->num != 0);
815
816         vdev->config->del_vq(vi->svq);
817         vdev->config->del_vq(vi->rvq);
818         unregister_netdev(vi->dev);
819
820         while (vi->pages)
821                 __free_pages(get_a_page(vi, GFP_KERNEL), 0);
822
823         free_netdev(vi->dev);
824 }
825
826 static struct virtio_device_id id_table[] = {
827         { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
828         { 0 },
829 };
830
831 static unsigned int features[] = {
832         VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM,
833         VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
834         VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
835         VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
836         VIRTIO_NET_F_GUEST_ECN, /* We don't yet handle UFO input. */
837         VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS,
838         VIRTIO_F_NOTIFY_ON_EMPTY,
839 };
840
841 static struct virtio_driver virtio_net = {
842         .feature_table = features,
843         .feature_table_size = ARRAY_SIZE(features),
844         .driver.name =  KBUILD_MODNAME,
845         .driver.owner = THIS_MODULE,
846         .id_table =     id_table,
847         .probe =        virtnet_probe,
848         .remove =       __devexit_p(virtnet_remove),
849         .config_changed = virtnet_config_changed,
850 };
851
852 static int __init init(void)
853 {
854         return register_virtio_driver(&virtio_net);
855 }
856
857 static void __exit fini(void)
858 {
859         unregister_virtio_driver(&virtio_net);
860 }
861 module_init(init);
862 module_exit(fini);
863
864 MODULE_DEVICE_TABLE(virtio, id_table);
865 MODULE_DESCRIPTION("Virtio network driver");
866 MODULE_LICENSE("GPL");