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