Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[safe/jmp/linux-2.6] / drivers / net / virtio_net.c
1 /* A 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_ids.h>
26 #include <linux/virtio_net.h>
27 #include <linux/scatterlist.h>
28 #include <linux/if_vlan.h>
29
30 static int napi_weight = 128;
31 module_param(napi_weight, int, 0444);
32
33 static int csum = 1, gso = 1;
34 module_param(csum, bool, 0444);
35 module_param(gso, bool, 0444);
36
37 /* FIXME: MTU in config. */
38 #define MAX_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
39 #define GOOD_COPY_LEN   128
40
41 #define VIRTNET_SEND_COMMAND_SG_MAX    2
42
43 struct virtnet_info
44 {
45         struct virtio_device *vdev;
46         struct virtqueue *rvq, *svq, *cvq;
47         struct net_device *dev;
48         struct napi_struct napi;
49         unsigned int status;
50
51         /* Number of input buffers, and max we've ever had. */
52         unsigned int num, max;
53
54         /* I like... big packets and I cannot lie! */
55         bool big_packets;
56
57         /* Host will merge rx buffers for big packets (shake it! shake it!) */
58         bool mergeable_rx_bufs;
59
60         /* Receive & send queues. */
61         struct sk_buff_head recv;
62         struct sk_buff_head send;
63
64         /* Work struct for refilling if we run low on memory. */
65         struct delayed_work refill;
66
67         /* Chain pages by the private ptr. */
68         struct page *pages;
69 };
70
71 struct skb_vnet_hdr {
72         union {
73                 struct virtio_net_hdr hdr;
74                 struct virtio_net_hdr_mrg_rxbuf mhdr;
75         };
76         unsigned int num_sg;
77 };
78
79 static inline struct skb_vnet_hdr *skb_vnet_hdr(struct sk_buff *skb)
80 {
81         return (struct skb_vnet_hdr *)skb->cb;
82 }
83
84 static void give_a_page(struct virtnet_info *vi, struct page *page)
85 {
86         page->private = (unsigned long)vi->pages;
87         vi->pages = page;
88 }
89
90 static void trim_pages(struct virtnet_info *vi, struct sk_buff *skb)
91 {
92         unsigned int i;
93
94         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
95                 give_a_page(vi, skb_shinfo(skb)->frags[i].page);
96         skb_shinfo(skb)->nr_frags = 0;
97         skb->data_len = 0;
98 }
99
100 static struct page *get_a_page(struct virtnet_info *vi, gfp_t gfp_mask)
101 {
102         struct page *p = vi->pages;
103
104         if (p)
105                 vi->pages = (struct page *)p->private;
106         else
107                 p = alloc_page(gfp_mask);
108         return p;
109 }
110
111 static void skb_xmit_done(struct virtqueue *svq)
112 {
113         struct virtnet_info *vi = svq->vdev->priv;
114
115         /* Suppress further interrupts. */
116         svq->vq_ops->disable_cb(svq);
117
118         /* We were probably waiting for more output buffers. */
119         netif_wake_queue(vi->dev);
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 skb_vnet_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                 unsigned int copy;
138                 char *p = page_address(skb_shinfo(skb)->frags[0].page);
139
140                 if (len > PAGE_SIZE)
141                         len = PAGE_SIZE;
142                 len -= sizeof(struct virtio_net_hdr_mrg_rxbuf);
143
144                 memcpy(&hdr->mhdr, p, sizeof(hdr->mhdr));
145                 p += sizeof(hdr->mhdr);
146
147                 copy = len;
148                 if (copy > skb_tailroom(skb))
149                         copy = skb_tailroom(skb);
150
151                 memcpy(skb_put(skb, copy), p, copy);
152
153                 len -= copy;
154
155                 if (!len) {
156                         give_a_page(vi, skb_shinfo(skb)->frags[0].page);
157                         skb_shinfo(skb)->nr_frags--;
158                 } else {
159                         skb_shinfo(skb)->frags[0].page_offset +=
160                                 sizeof(hdr->mhdr) + copy;
161                         skb_shinfo(skb)->frags[0].size = len;
162                         skb->data_len += len;
163                         skb->len += len;
164                 }
165
166                 while (--hdr->mhdr.num_buffers) {
167                         struct sk_buff *nskb;
168
169                         i = skb_shinfo(skb)->nr_frags;
170                         if (i >= MAX_SKB_FRAGS) {
171                                 pr_debug("%s: packet too long %d\n", dev->name,
172                                          len);
173                                 dev->stats.rx_length_errors++;
174                                 goto drop;
175                         }
176
177                         nskb = vi->rvq->vq_ops->get_buf(vi->rvq, &len);
178                         if (!nskb) {
179                                 pr_debug("%s: rx error: %d buffers missing\n",
180                                          dev->name, hdr->mhdr.num_buffers);
181                                 dev->stats.rx_length_errors++;
182                                 goto drop;
183                         }
184
185                         __skb_unlink(nskb, &vi->recv);
186                         vi->num--;
187
188                         skb_shinfo(skb)->frags[i] = skb_shinfo(nskb)->frags[0];
189                         skb_shinfo(nskb)->nr_frags = 0;
190                         kfree_skb(nskb);
191
192                         if (len > PAGE_SIZE)
193                                 len = PAGE_SIZE;
194
195                         skb_shinfo(skb)->frags[i].size = len;
196                         skb_shinfo(skb)->nr_frags++;
197                         skb->data_len += len;
198                         skb->len += len;
199                 }
200         } else {
201                 len -= sizeof(hdr->hdr);
202
203                 if (len <= MAX_PACKET_LEN)
204                         trim_pages(vi, skb);
205
206                 err = pskb_trim(skb, len);
207                 if (err) {
208                         pr_debug("%s: pskb_trim failed %i %d\n", dev->name,
209                                  len, err);
210                         dev->stats.rx_dropped++;
211                         goto drop;
212                 }
213         }
214
215         skb->truesize += skb->data_len;
216         dev->stats.rx_bytes += skb->len;
217         dev->stats.rx_packets++;
218
219         if (hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
220                 pr_debug("Needs csum!\n");
221                 if (!skb_partial_csum_set(skb,
222                                           hdr->hdr.csum_start,
223                                           hdr->hdr.csum_offset))
224                         goto frame_err;
225         }
226
227         skb->protocol = eth_type_trans(skb, dev);
228         pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
229                  ntohs(skb->protocol), skb->len, skb->pkt_type);
230
231         if (hdr->hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
232                 pr_debug("GSO!\n");
233                 switch (hdr->hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
234                 case VIRTIO_NET_HDR_GSO_TCPV4:
235                         skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
236                         break;
237                 case VIRTIO_NET_HDR_GSO_UDP:
238                         skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
239                         break;
240                 case VIRTIO_NET_HDR_GSO_TCPV6:
241                         skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
242                         break;
243                 default:
244                         if (net_ratelimit())
245                                 printk(KERN_WARNING "%s: bad gso type %u.\n",
246                                        dev->name, hdr->hdr.gso_type);
247                         goto frame_err;
248                 }
249
250                 if (hdr->hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN)
251                         skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
252
253                 skb_shinfo(skb)->gso_size = hdr->hdr.gso_size;
254                 if (skb_shinfo(skb)->gso_size == 0) {
255                         if (net_ratelimit())
256                                 printk(KERN_WARNING "%s: zero gso size.\n",
257                                        dev->name);
258                         goto frame_err;
259                 }
260
261                 /* Header must be checked, and gso_segs computed. */
262                 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
263                 skb_shinfo(skb)->gso_segs = 0;
264         }
265
266         netif_receive_skb(skb);
267         return;
268
269 frame_err:
270         dev->stats.rx_frame_errors++;
271 drop:
272         dev_kfree_skb(skb);
273 }
274
275 static bool try_fill_recv_maxbufs(struct virtnet_info *vi, gfp_t gfp)
276 {
277         struct sk_buff *skb;
278         struct scatterlist sg[2+MAX_SKB_FRAGS];
279         int num, err, i;
280         bool oom = false;
281
282         sg_init_table(sg, 2+MAX_SKB_FRAGS);
283         do {
284                 struct skb_vnet_hdr *hdr;
285
286                 skb = netdev_alloc_skb_ip_align(vi->dev, MAX_PACKET_LEN);
287                 if (unlikely(!skb)) {
288                         oom = true;
289                         break;
290                 }
291
292                 skb_put(skb, MAX_PACKET_LEN);
293
294                 hdr = skb_vnet_hdr(skb);
295                 sg_set_buf(sg, &hdr->hdr, sizeof(hdr->hdr));
296
297                 if (vi->big_packets) {
298                         for (i = 0; i < MAX_SKB_FRAGS; i++) {
299                                 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
300                                 f->page = get_a_page(vi, gfp);
301                                 if (!f->page)
302                                         break;
303
304                                 f->page_offset = 0;
305                                 f->size = PAGE_SIZE;
306
307                                 skb->data_len += PAGE_SIZE;
308                                 skb->len += PAGE_SIZE;
309
310                                 skb_shinfo(skb)->nr_frags++;
311                         }
312                 }
313
314                 num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
315                 skb_queue_head(&vi->recv, skb);
316
317                 err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, num, skb);
318                 if (err < 0) {
319                         skb_unlink(skb, &vi->recv);
320                         trim_pages(vi, skb);
321                         kfree_skb(skb);
322                         break;
323                 }
324                 vi->num++;
325         } while (err >= num);
326         if (unlikely(vi->num > vi->max))
327                 vi->max = vi->num;
328         vi->rvq->vq_ops->kick(vi->rvq);
329         return !oom;
330 }
331
332 /* Returns false if we couldn't fill entirely (OOM). */
333 static bool try_fill_recv(struct virtnet_info *vi, gfp_t gfp)
334 {
335         struct sk_buff *skb;
336         struct scatterlist sg[1];
337         int err;
338         bool oom = false;
339
340         if (!vi->mergeable_rx_bufs)
341                 return try_fill_recv_maxbufs(vi, gfp);
342
343         do {
344                 skb_frag_t *f;
345
346                 skb = netdev_alloc_skb_ip_align(vi->dev, GOOD_COPY_LEN);
347                 if (unlikely(!skb)) {
348                         oom = true;
349                         break;
350                 }
351
352                 f = &skb_shinfo(skb)->frags[0];
353                 f->page = get_a_page(vi, gfp);
354                 if (!f->page) {
355                         oom = true;
356                         kfree_skb(skb);
357                         break;
358                 }
359
360                 f->page_offset = 0;
361                 f->size = PAGE_SIZE;
362
363                 skb_shinfo(skb)->nr_frags++;
364
365                 sg_init_one(sg, page_address(f->page), PAGE_SIZE);
366                 skb_queue_head(&vi->recv, skb);
367
368                 err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, 1, skb);
369                 if (err < 0) {
370                         skb_unlink(skb, &vi->recv);
371                         kfree_skb(skb);
372                         break;
373                 }
374                 vi->num++;
375         } while (err > 0);
376         if (unlikely(vi->num > vi->max))
377                 vi->max = vi->num;
378         vi->rvq->vq_ops->kick(vi->rvq);
379         return !oom;
380 }
381
382 static void skb_recv_done(struct virtqueue *rvq)
383 {
384         struct virtnet_info *vi = rvq->vdev->priv;
385         /* Schedule NAPI, Suppress further interrupts if successful. */
386         if (napi_schedule_prep(&vi->napi)) {
387                 rvq->vq_ops->disable_cb(rvq);
388                 __napi_schedule(&vi->napi);
389         }
390 }
391
392 static void refill_work(struct work_struct *work)
393 {
394         struct virtnet_info *vi;
395         bool still_empty;
396
397         vi = container_of(work, struct virtnet_info, refill.work);
398         napi_disable(&vi->napi);
399         try_fill_recv(vi, GFP_KERNEL);
400         still_empty = (vi->num == 0);
401         napi_enable(&vi->napi);
402
403         /* In theory, this can happen: if we don't get any buffers in
404          * we will *never* try to fill again. */
405         if (still_empty)
406                 schedule_delayed_work(&vi->refill, HZ/2);
407 }
408
409 static int virtnet_poll(struct napi_struct *napi, int budget)
410 {
411         struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi);
412         struct sk_buff *skb = NULL;
413         unsigned int len, received = 0;
414
415 again:
416         while (received < budget &&
417                (skb = vi->rvq->vq_ops->get_buf(vi->rvq, &len)) != NULL) {
418                 __skb_unlink(skb, &vi->recv);
419                 receive_skb(vi->dev, skb, len);
420                 vi->num--;
421                 received++;
422         }
423
424         if (vi->num < vi->max / 2) {
425                 if (!try_fill_recv(vi, GFP_ATOMIC))
426                         schedule_delayed_work(&vi->refill, 0);
427         }
428
429         /* Out of packets? */
430         if (received < budget) {
431                 napi_complete(napi);
432                 if (unlikely(!vi->rvq->vq_ops->enable_cb(vi->rvq))
433                     && napi_schedule_prep(napi)) {
434                         vi->rvq->vq_ops->disable_cb(vi->rvq);
435                         __napi_schedule(napi);
436                         goto again;
437                 }
438         }
439
440         return received;
441 }
442
443 static unsigned int free_old_xmit_skbs(struct virtnet_info *vi)
444 {
445         struct sk_buff *skb;
446         unsigned int len, tot_sgs = 0;
447
448         while ((skb = vi->svq->vq_ops->get_buf(vi->svq, &len)) != NULL) {
449                 pr_debug("Sent skb %p\n", skb);
450                 __skb_unlink(skb, &vi->send);
451                 vi->dev->stats.tx_bytes += skb->len;
452                 vi->dev->stats.tx_packets++;
453                 tot_sgs += skb_vnet_hdr(skb)->num_sg;
454                 dev_kfree_skb_any(skb);
455         }
456         return tot_sgs;
457 }
458
459 static int xmit_skb(struct virtnet_info *vi, struct sk_buff *skb)
460 {
461         struct scatterlist sg[2+MAX_SKB_FRAGS];
462         struct skb_vnet_hdr *hdr = skb_vnet_hdr(skb);
463         const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
464
465         sg_init_table(sg, 2+MAX_SKB_FRAGS);
466
467         pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
468
469         if (skb->ip_summed == CHECKSUM_PARTIAL) {
470                 hdr->hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
471                 hdr->hdr.csum_start = skb->csum_start - skb_headroom(skb);
472                 hdr->hdr.csum_offset = skb->csum_offset;
473         } else {
474                 hdr->hdr.flags = 0;
475                 hdr->hdr.csum_offset = hdr->hdr.csum_start = 0;
476         }
477
478         if (skb_is_gso(skb)) {
479                 hdr->hdr.hdr_len = skb_headlen(skb);
480                 hdr->hdr.gso_size = skb_shinfo(skb)->gso_size;
481                 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
482                         hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
483                 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
484                         hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
485                 else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
486                         hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP;
487                 else
488                         BUG();
489                 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
490                         hdr->hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
491         } else {
492                 hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
493                 hdr->hdr.gso_size = hdr->hdr.hdr_len = 0;
494         }
495
496         hdr->mhdr.num_buffers = 0;
497
498         /* Encode metadata header at front. */
499         if (vi->mergeable_rx_bufs)
500                 sg_set_buf(sg, &hdr->mhdr, sizeof(hdr->mhdr));
501         else
502                 sg_set_buf(sg, &hdr->hdr, sizeof(hdr->hdr));
503
504         hdr->num_sg = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
505         return vi->svq->vq_ops->add_buf(vi->svq, sg, hdr->num_sg, 0, skb);
506 }
507
508 static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
509 {
510         struct virtnet_info *vi = netdev_priv(dev);
511         int capacity;
512
513 again:
514         /* Free up any pending old buffers before queueing new ones. */
515         free_old_xmit_skbs(vi);
516
517         /* Put new one in send queue and do transmit */
518         __skb_queue_head(&vi->send, skb);
519         capacity = xmit_skb(vi, skb);
520
521         /* This can happen with OOM and indirect buffers. */
522         if (unlikely(capacity < 0)) {
523                 netif_stop_queue(dev);
524                 dev_warn(&dev->dev, "Unexpected full queue\n");
525                 if (unlikely(!vi->svq->vq_ops->enable_cb(vi->svq))) {
526                         vi->svq->vq_ops->disable_cb(vi->svq);
527                         netif_start_queue(dev);
528                         goto again;
529                 }
530                 return NETDEV_TX_BUSY;
531         }
532
533         vi->svq->vq_ops->kick(vi->svq);
534         /* Don't wait up for transmitted skbs to be freed. */
535         skb_orphan(skb);
536         nf_reset(skb);
537
538         /* Apparently nice girls don't return TX_BUSY; stop the queue
539          * before it gets out of hand.  Naturally, this wastes entries. */
540         if (capacity < 2+MAX_SKB_FRAGS) {
541                 netif_stop_queue(dev);
542                 if (unlikely(!vi->svq->vq_ops->enable_cb(vi->svq))) {
543                         /* More just got used, free them then recheck. */
544                         capacity += free_old_xmit_skbs(vi);
545                         if (capacity >= 2+MAX_SKB_FRAGS) {
546                                 netif_start_queue(dev);
547                                 vi->svq->vq_ops->disable_cb(vi->svq);
548                         }
549                 }
550         }
551
552         return NETDEV_TX_OK;
553 }
554
555 static int virtnet_set_mac_address(struct net_device *dev, void *p)
556 {
557         struct virtnet_info *vi = netdev_priv(dev);
558         struct virtio_device *vdev = vi->vdev;
559         int ret;
560
561         ret = eth_mac_addr(dev, p);
562         if (ret)
563                 return ret;
564
565         if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
566                 vdev->config->set(vdev, offsetof(struct virtio_net_config, mac),
567                                   dev->dev_addr, dev->addr_len);
568
569         return 0;
570 }
571
572 #ifdef CONFIG_NET_POLL_CONTROLLER
573 static void virtnet_netpoll(struct net_device *dev)
574 {
575         struct virtnet_info *vi = netdev_priv(dev);
576
577         napi_schedule(&vi->napi);
578 }
579 #endif
580
581 static int virtnet_open(struct net_device *dev)
582 {
583         struct virtnet_info *vi = netdev_priv(dev);
584
585         napi_enable(&vi->napi);
586
587         /* If all buffers were filled by other side before we napi_enabled, we
588          * won't get another interrupt, so process any outstanding packets
589          * now.  virtnet_poll wants re-enable the queue, so we disable here.
590          * We synchronize against interrupts via NAPI_STATE_SCHED */
591         if (napi_schedule_prep(&vi->napi)) {
592                 vi->rvq->vq_ops->disable_cb(vi->rvq);
593                 __napi_schedule(&vi->napi);
594         }
595         return 0;
596 }
597
598 /*
599  * Send command via the control virtqueue and check status.  Commands
600  * supported by the hypervisor, as indicated by feature bits, should
601  * never fail unless improperly formated.
602  */
603 static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
604                                  struct scatterlist *data, int out, int in)
605 {
606         struct scatterlist *s, sg[VIRTNET_SEND_COMMAND_SG_MAX + 2];
607         struct virtio_net_ctrl_hdr ctrl;
608         virtio_net_ctrl_ack status = ~0;
609         unsigned int tmp;
610         int i;
611
612         /* Caller should know better */
613         BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) ||
614                 (out + in > VIRTNET_SEND_COMMAND_SG_MAX));
615
616         out++; /* Add header */
617         in++; /* Add return status */
618
619         ctrl.class = class;
620         ctrl.cmd = cmd;
621
622         sg_init_table(sg, out + in);
623
624         sg_set_buf(&sg[0], &ctrl, sizeof(ctrl));
625         for_each_sg(data, s, out + in - 2, i)
626                 sg_set_buf(&sg[i + 1], sg_virt(s), s->length);
627         sg_set_buf(&sg[out + in - 1], &status, sizeof(status));
628
629         BUG_ON(vi->cvq->vq_ops->add_buf(vi->cvq, sg, out, in, vi) < 0);
630
631         vi->cvq->vq_ops->kick(vi->cvq);
632
633         /*
634          * Spin for a response, the kick causes an ioport write, trapping
635          * into the hypervisor, so the request should be handled immediately.
636          */
637         while (!vi->cvq->vq_ops->get_buf(vi->cvq, &tmp))
638                 cpu_relax();
639
640         return status == VIRTIO_NET_OK;
641 }
642
643 static int virtnet_close(struct net_device *dev)
644 {
645         struct virtnet_info *vi = netdev_priv(dev);
646
647         napi_disable(&vi->napi);
648
649         return 0;
650 }
651
652 static int virtnet_set_tx_csum(struct net_device *dev, u32 data)
653 {
654         struct virtnet_info *vi = netdev_priv(dev);
655         struct virtio_device *vdev = vi->vdev;
656
657         if (data && !virtio_has_feature(vdev, VIRTIO_NET_F_CSUM))
658                 return -ENOSYS;
659
660         return ethtool_op_set_tx_hw_csum(dev, data);
661 }
662
663 static void virtnet_set_rx_mode(struct net_device *dev)
664 {
665         struct virtnet_info *vi = netdev_priv(dev);
666         struct scatterlist sg[2];
667         u8 promisc, allmulti;
668         struct virtio_net_ctrl_mac *mac_data;
669         struct dev_addr_list *addr;
670         struct netdev_hw_addr *ha;
671         void *buf;
672         int i;
673
674         /* We can't dynamicaly set ndo_set_rx_mode, so return gracefully */
675         if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
676                 return;
677
678         promisc = ((dev->flags & IFF_PROMISC) != 0);
679         allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
680
681         sg_init_one(sg, &promisc, sizeof(promisc));
682
683         if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
684                                   VIRTIO_NET_CTRL_RX_PROMISC,
685                                   sg, 1, 0))
686                 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
687                          promisc ? "en" : "dis");
688
689         sg_init_one(sg, &allmulti, sizeof(allmulti));
690
691         if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
692                                   VIRTIO_NET_CTRL_RX_ALLMULTI,
693                                   sg, 1, 0))
694                 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
695                          allmulti ? "en" : "dis");
696
697         /* MAC filter - use one buffer for both lists */
698         mac_data = buf = kzalloc(((dev->uc.count + dev->mc_count) * ETH_ALEN) +
699                                  (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
700         if (!buf) {
701                 dev_warn(&dev->dev, "No memory for MAC address buffer\n");
702                 return;
703         }
704
705         sg_init_table(sg, 2);
706
707         /* Store the unicast list and count in the front of the buffer */
708         mac_data->entries = dev->uc.count;
709         i = 0;
710         list_for_each_entry(ha, &dev->uc.list, list)
711                 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
712
713         sg_set_buf(&sg[0], mac_data,
714                    sizeof(mac_data->entries) + (dev->uc.count * ETH_ALEN));
715
716         /* multicast list and count fill the end */
717         mac_data = (void *)&mac_data->macs[dev->uc.count][0];
718
719         mac_data->entries = dev->mc_count;
720         addr = dev->mc_list;
721         for (i = 0; i < dev->mc_count; i++, addr = addr->next)
722                 memcpy(&mac_data->macs[i][0], addr->da_addr, ETH_ALEN);
723
724         sg_set_buf(&sg[1], mac_data,
725                    sizeof(mac_data->entries) + (dev->mc_count * ETH_ALEN));
726
727         if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
728                                   VIRTIO_NET_CTRL_MAC_TABLE_SET,
729                                   sg, 2, 0))
730                 dev_warn(&dev->dev, "Failed to set MAC fitler table.\n");
731
732         kfree(buf);
733 }
734
735 static void virtnet_vlan_rx_add_vid(struct net_device *dev, u16 vid)
736 {
737         struct virtnet_info *vi = netdev_priv(dev);
738         struct scatterlist sg;
739
740         sg_init_one(&sg, &vid, sizeof(vid));
741
742         if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
743                                   VIRTIO_NET_CTRL_VLAN_ADD, &sg, 1, 0))
744                 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
745 }
746
747 static void virtnet_vlan_rx_kill_vid(struct net_device *dev, u16 vid)
748 {
749         struct virtnet_info *vi = netdev_priv(dev);
750         struct scatterlist sg;
751
752         sg_init_one(&sg, &vid, sizeof(vid));
753
754         if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
755                                   VIRTIO_NET_CTRL_VLAN_DEL, &sg, 1, 0))
756                 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
757 }
758
759 static const struct ethtool_ops virtnet_ethtool_ops = {
760         .set_tx_csum = virtnet_set_tx_csum,
761         .set_sg = ethtool_op_set_sg,
762         .set_tso = ethtool_op_set_tso,
763         .set_ufo = ethtool_op_set_ufo,
764         .get_link = ethtool_op_get_link,
765 };
766
767 #define MIN_MTU 68
768 #define MAX_MTU 65535
769
770 static int virtnet_change_mtu(struct net_device *dev, int new_mtu)
771 {
772         if (new_mtu < MIN_MTU || new_mtu > MAX_MTU)
773                 return -EINVAL;
774         dev->mtu = new_mtu;
775         return 0;
776 }
777
778 static const struct net_device_ops virtnet_netdev = {
779         .ndo_open            = virtnet_open,
780         .ndo_stop            = virtnet_close,
781         .ndo_start_xmit      = start_xmit,
782         .ndo_validate_addr   = eth_validate_addr,
783         .ndo_set_mac_address = virtnet_set_mac_address,
784         .ndo_set_rx_mode     = virtnet_set_rx_mode,
785         .ndo_change_mtu      = virtnet_change_mtu,
786         .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
787         .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
788 #ifdef CONFIG_NET_POLL_CONTROLLER
789         .ndo_poll_controller = virtnet_netpoll,
790 #endif
791 };
792
793 static void virtnet_update_status(struct virtnet_info *vi)
794 {
795         u16 v;
796
797         if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS))
798                 return;
799
800         vi->vdev->config->get(vi->vdev,
801                               offsetof(struct virtio_net_config, status),
802                               &v, sizeof(v));
803
804         /* Ignore unknown (future) status bits */
805         v &= VIRTIO_NET_S_LINK_UP;
806
807         if (vi->status == v)
808                 return;
809
810         vi->status = v;
811
812         if (vi->status & VIRTIO_NET_S_LINK_UP) {
813                 netif_carrier_on(vi->dev);
814                 netif_wake_queue(vi->dev);
815         } else {
816                 netif_carrier_off(vi->dev);
817                 netif_stop_queue(vi->dev);
818         }
819 }
820
821 static void virtnet_config_changed(struct virtio_device *vdev)
822 {
823         struct virtnet_info *vi = vdev->priv;
824
825         virtnet_update_status(vi);
826 }
827
828 static int virtnet_probe(struct virtio_device *vdev)
829 {
830         int err;
831         struct net_device *dev;
832         struct virtnet_info *vi;
833         struct virtqueue *vqs[3];
834         vq_callback_t *callbacks[] = { skb_recv_done, skb_xmit_done, NULL};
835         const char *names[] = { "input", "output", "control" };
836         int nvqs;
837
838         /* Allocate ourselves a network device with room for our info */
839         dev = alloc_etherdev(sizeof(struct virtnet_info));
840         if (!dev)
841                 return -ENOMEM;
842
843         /* Set up network device as normal. */
844         dev->netdev_ops = &virtnet_netdev;
845         dev->features = NETIF_F_HIGHDMA;
846         SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops);
847         SET_NETDEV_DEV(dev, &vdev->dev);
848
849         /* Do we support "hardware" checksums? */
850         if (csum && virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
851                 /* This opens up the world of extra features. */
852                 dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
853                 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
854                         dev->features |= NETIF_F_TSO | NETIF_F_UFO
855                                 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
856                 }
857                 /* Individual feature bits: what can host handle? */
858                 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
859                         dev->features |= NETIF_F_TSO;
860                 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
861                         dev->features |= NETIF_F_TSO6;
862                 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
863                         dev->features |= NETIF_F_TSO_ECN;
864                 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
865                         dev->features |= NETIF_F_UFO;
866         }
867
868         /* Configuration may specify what MAC to use.  Otherwise random. */
869         if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
870                 vdev->config->get(vdev,
871                                   offsetof(struct virtio_net_config, mac),
872                                   dev->dev_addr, dev->addr_len);
873         } else
874                 random_ether_addr(dev->dev_addr);
875
876         /* Set up our device-specific information */
877         vi = netdev_priv(dev);
878         netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight);
879         vi->dev = dev;
880         vi->vdev = vdev;
881         vdev->priv = vi;
882         vi->pages = NULL;
883         INIT_DELAYED_WORK(&vi->refill, refill_work);
884
885         /* If we can receive ANY GSO packets, we must allocate large ones. */
886         if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4)
887             || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6)
888             || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN))
889                 vi->big_packets = true;
890
891         if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
892                 vi->mergeable_rx_bufs = true;
893
894         /* We expect two virtqueues, receive then send,
895          * and optionally control. */
896         nvqs = virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) ? 3 : 2;
897
898         err = vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names);
899         if (err)
900                 goto free;
901
902         vi->rvq = vqs[0];
903         vi->svq = vqs[1];
904
905         if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)) {
906                 vi->cvq = vqs[2];
907
908                 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
909                         dev->features |= NETIF_F_HW_VLAN_FILTER;
910         }
911
912         /* Initialize our empty receive and send queues. */
913         skb_queue_head_init(&vi->recv);
914         skb_queue_head_init(&vi->send);
915
916         err = register_netdev(dev);
917         if (err) {
918                 pr_debug("virtio_net: registering device failed\n");
919                 goto free_vqs;
920         }
921
922         /* Last of all, set up some receive buffers. */
923         try_fill_recv(vi, GFP_KERNEL);
924
925         /* If we didn't even get one input buffer, we're useless. */
926         if (vi->num == 0) {
927                 err = -ENOMEM;
928                 goto unregister;
929         }
930
931         vi->status = VIRTIO_NET_S_LINK_UP;
932         virtnet_update_status(vi);
933         netif_carrier_on(dev);
934
935         pr_debug("virtnet: registered device %s\n", dev->name);
936         return 0;
937
938 unregister:
939         unregister_netdev(dev);
940         cancel_delayed_work_sync(&vi->refill);
941 free_vqs:
942         vdev->config->del_vqs(vdev);
943 free:
944         free_netdev(dev);
945         return err;
946 }
947
948 static void __devexit virtnet_remove(struct virtio_device *vdev)
949 {
950         struct virtnet_info *vi = vdev->priv;
951         struct sk_buff *skb;
952
953         /* Stop all the virtqueues. */
954         vdev->config->reset(vdev);
955
956         /* Free our skbs in send and recv queues, if any. */
957         while ((skb = __skb_dequeue(&vi->recv)) != NULL) {
958                 kfree_skb(skb);
959                 vi->num--;
960         }
961         __skb_queue_purge(&vi->send);
962
963         BUG_ON(vi->num != 0);
964
965         unregister_netdev(vi->dev);
966         cancel_delayed_work_sync(&vi->refill);
967
968         vdev->config->del_vqs(vi->vdev);
969
970         while (vi->pages)
971                 __free_pages(get_a_page(vi, GFP_KERNEL), 0);
972
973         free_netdev(vi->dev);
974 }
975
976 static struct virtio_device_id id_table[] = {
977         { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
978         { 0 },
979 };
980
981 static unsigned int features[] = {
982         VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM,
983         VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
984         VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
985         VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
986         VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO,
987         VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ,
988         VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN,
989 };
990
991 static struct virtio_driver virtio_net = {
992         .feature_table = features,
993         .feature_table_size = ARRAY_SIZE(features),
994         .driver.name =  KBUILD_MODNAME,
995         .driver.owner = THIS_MODULE,
996         .id_table =     id_table,
997         .probe =        virtnet_probe,
998         .remove =       __devexit_p(virtnet_remove),
999         .config_changed = virtnet_config_changed,
1000 };
1001
1002 static int __init init(void)
1003 {
1004         return register_virtio_driver(&virtio_net);
1005 }
1006
1007 static void __exit fini(void)
1008 {
1009         unregister_virtio_driver(&virtio_net);
1010 }
1011 module_init(init);
1012 module_exit(fini);
1013
1014 MODULE_DEVICE_TABLE(virtio, id_table);
1015 MODULE_DESCRIPTION("Virtio network driver");
1016 MODULE_LICENSE("GPL");