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