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