]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/net/virtio_net.c
virtio_net: Add a MAC filter table
[net-next-2.6.git] / drivers / net / virtio_net.c
CommitLineData
296f96fc
RR
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>
a9ea3fc6 22#include <linux/ethtool.h>
296f96fc
RR
23#include <linux/module.h>
24#include <linux/virtio.h>
25#include <linux/virtio_net.h>
26#include <linux/scatterlist.h>
e918085a 27#include <linux/if_vlan.h>
296f96fc 28
6c0cd7c0
DL
29static int napi_weight = 128;
30module_param(napi_weight, int, 0444);
31
34a48579
RR
32static int csum = 1, gso = 1;
33module_param(csum, bool, 0444);
34module_param(gso, bool, 0444);
35
296f96fc 36/* FIXME: MTU in config. */
e918085a 37#define MAX_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
3f2c31d9 38#define GOOD_COPY_LEN 128
296f96fc 39
f565a7c2 40#define VIRTNET_SEND_COMMAND_SG_MAX 2
2a41f71d 41
296f96fc
RR
42struct virtnet_info
43{
44 struct virtio_device *vdev;
2a41f71d 45 struct virtqueue *rvq, *svq, *cvq;
296f96fc
RR
46 struct net_device *dev;
47 struct napi_struct napi;
9f4d26d0 48 unsigned int status;
296f96fc 49
99ffc696
RR
50 /* The skb we couldn't send because buffers were full. */
51 struct sk_buff *last_xmit_skb;
52
363f1514 53 /* If we need to free in a timer, this is it. */
14c998f0
MM
54 struct timer_list xmit_free_timer;
55
296f96fc
RR
56 /* Number of input buffers, and max we've ever had. */
57 unsigned int num, max;
58
11a3a154
RR
59 /* For cleaning up after transmission. */
60 struct tasklet_struct tasklet;
363f1514 61 bool free_in_tasklet;
11a3a154 62
97402b96
HX
63 /* I like... big packets and I cannot lie! */
64 bool big_packets;
65
3f2c31d9
MM
66 /* Host will merge rx buffers for big packets (shake it! shake it!) */
67 bool mergeable_rx_bufs;
68
296f96fc
RR
69 /* Receive & send queues. */
70 struct sk_buff_head recv;
71 struct sk_buff_head send;
fb6813f4
RR
72
73 /* Chain pages by the private ptr. */
74 struct page *pages;
296f96fc
RR
75};
76
3f2c31d9 77static inline void *skb_vnet_hdr(struct sk_buff *skb)
296f96fc
RR
78{
79 return (struct virtio_net_hdr *)skb->cb;
80}
81
fb6813f4
RR
82static 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
0a888fd1
MM
88static 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
fb6813f4
RR
98static 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
2cb9c6ba 109static void skb_xmit_done(struct virtqueue *svq)
296f96fc 110{
2cb9c6ba 111 struct virtnet_info *vi = svq->vdev->priv;
296f96fc 112
2cb9c6ba
RR
113 /* Suppress further interrupts. */
114 svq->vq_ops->disable_cb(svq);
11a3a154 115
363f1514 116 /* We were probably waiting for more output buffers. */
296f96fc 117 netif_wake_queue(vi->dev);
11a3a154
RR
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);
296f96fc
RR
122}
123
124static void receive_skb(struct net_device *dev, struct sk_buff *skb,
125 unsigned len)
126{
3f2c31d9 127 struct virtnet_info *vi = netdev_priv(dev);
296f96fc 128 struct virtio_net_hdr *hdr = skb_vnet_hdr(skb);
97402b96 129 int err;
3f2c31d9 130 int i;
296f96fc
RR
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 }
23cde76d 137
3f2c31d9
MM
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);
fb6813f4 142
3f2c31d9
MM
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 }
97402b96 216 }
3f2c31d9 217
97402b96 218 skb->truesize += skb->data_len;
296f96fc
RR
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");
f35d9d8a 224 if (!skb_partial_csum_set(skb,hdr->csum_start,hdr->csum_offset))
296f96fc 225 goto frame_err;
296f96fc
RR
226 }
227
23cde76d
MM
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
296f96fc
RR
232 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
233 pr_debug("GSO!\n");
34a48579 234 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
296f96fc
RR
235 case VIRTIO_NET_HDR_GSO_TCPV4:
236 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
237 break;
296f96fc
RR
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
34a48579
RR
251 if (hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN)
252 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
253
296f96fc
RR
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
270frame_err:
271 dev->stats.rx_frame_errors++;
272drop:
273 dev_kfree_skb(skb);
274}
275
3f2c31d9 276static void try_fill_recv_maxbufs(struct virtnet_info *vi)
296f96fc
RR
277{
278 struct sk_buff *skb;
05271685 279 struct scatterlist sg[2+MAX_SKB_FRAGS];
97402b96 280 int num, err, i;
296f96fc 281
05271685 282 sg_init_table(sg, 2+MAX_SKB_FRAGS);
296f96fc 283 for (;;) {
3f2c31d9
MM
284 struct virtio_net_hdr *hdr;
285
296f96fc
RR
286 skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN);
287 if (unlikely(!skb))
288 break;
289
290 skb_put(skb, MAX_PACKET_LEN);
3f2c31d9
MM
291
292 hdr = skb_vnet_hdr(skb);
8527bec5 293 sg_set_buf(sg, hdr, sizeof(*hdr));
97402b96
HX
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];
fb6813f4 298 f->page = get_a_page(vi, GFP_ATOMIC);
97402b96
HX
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
296f96fc
RR
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);
0a888fd1 318 trim_pages(vi, skb);
296f96fc
RR
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
3f2c31d9
MM
329static 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
18445c4d 377static void skb_recv_done(struct virtqueue *rvq)
296f96fc
RR
378{
379 struct virtnet_info *vi = rvq->vdev->priv;
18445c4d 380 /* Schedule NAPI, Suppress further interrupts if successful. */
288379f0 381 if (napi_schedule_prep(&vi->napi)) {
18445c4d 382 rvq->vq_ops->disable_cb(rvq);
288379f0 383 __napi_schedule(&vi->napi);
18445c4d 384 }
296f96fc
RR
385}
386
387static 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
393again:
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
8329d98e
RR
407 /* Out of packets? */
408 if (received < budget) {
288379f0 409 napi_complete(napi);
18445c4d 410 if (unlikely(!vi->rvq->vq_ops->enable_cb(vi->rvq))
4265f161
CB
411 && napi_schedule_prep(napi)) {
412 vi->rvq->vq_ops->disable_cb(vi->rvq);
288379f0 413 __napi_schedule(napi);
296f96fc 414 goto again;
4265f161 415 }
296f96fc
RR
416 }
417
418 return received;
419}
420
421static 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);
655aa31f 429 vi->dev->stats.tx_bytes += skb->len;
296f96fc
RR
430 vi->dev->stats.tx_packets++;
431 kfree_skb(skb);
432 }
433}
434
363f1514
RR
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. */
14c998f0
MM
437static 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
99ffc696 451static int xmit_skb(struct virtnet_info *vi, struct sk_buff *skb)
296f96fc 452{
14c998f0 453 int num, err;
05271685 454 struct scatterlist sg[2+MAX_SKB_FRAGS];
3f2c31d9
MM
455 struct virtio_net_hdr_mrg_rxbuf *mhdr = skb_vnet_hdr(skb);
456 struct virtio_net_hdr *hdr = skb_vnet_hdr(skb);
296f96fc 457 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
296f96fc 458
05271685 459 sg_init_table(sg, 2+MAX_SKB_FRAGS);
4d125de3 460
e174961c 461 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
296f96fc 462
296f96fc
RR
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)) {
50c8ea80 473 hdr->hdr_len = skb_transport_header(skb) - skb->data;
296f96fc 474 hdr->gso_size = skb_shinfo(skb)->gso_size;
34a48579 475 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
296f96fc
RR
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();
34a48579
RR
483 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
484 hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
296f96fc
RR
485 } else {
486 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
50c8ea80 487 hdr->gso_size = hdr->hdr_len = 0;
296f96fc
RR
488 }
489
3f2c31d9
MM
490 mhdr->num_buffers = 0;
491
492 /* Encode metadata header at front. */
493 if (vi->mergeable_rx_bufs)
8527bec5 494 sg_set_buf(sg, mhdr, sizeof(*mhdr));
3f2c31d9 495 else
8527bec5 496 sg_set_buf(sg, hdr, sizeof(*hdr));
3f2c31d9 497
296f96fc 498 num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
99ffc696 499
14c998f0 500 err = vi->svq->vq_ops->add_buf(vi->svq, sg, num, 0, skb);
363f1514 501 if (!err && !vi->free_in_tasklet)
14c998f0
MM
502 mod_timer(&vi->xmit_free_timer, jiffies + (HZ/10));
503
504 return err;
99ffc696
RR
505}
506
11a3a154
RR
507static 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 }
363f1514
RR
516 if (vi->free_in_tasklet)
517 free_old_xmit_skbs(vi);
11a3a154
RR
518 netif_tx_unlock_bh(vi->dev);
519}
520
99ffc696
RR
521static int start_xmit(struct sk_buff *skb, struct net_device *dev)
522{
523 struct virtnet_info *vi = netdev_priv(dev);
2cb9c6ba
RR
524
525again:
526 /* Free up any pending old buffers before queueing new ones. */
527 free_old_xmit_skbs(vi);
99ffc696
RR
528
529 /* If we has a buffer left over from last time, send it now. */
9953ca6c
MM
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;
2cb9c6ba 535
99ffc696 536 /* Put new one in send queue and do transmit */
7eb2e251
RR
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 }
296f96fc 544 }
99ffc696 545done:
296f96fc 546 vi->svq->vq_ops->kick(vi->svq);
99ffc696
RR
547 return NETDEV_TX_OK;
548
549stop_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 }
9953ca6c
MM
560 if (skb) {
561 /* Drop this skb: we only queue one. */
562 vi->dev->stats.tx_dropped++;
563 kfree_skb(skb);
564 }
99ffc696 565 goto done;
296f96fc
RR
566}
567
da74e89d
AS
568#ifdef CONFIG_NET_POLL_CONTROLLER
569static void virtnet_netpoll(struct net_device *dev)
570{
571 struct virtnet_info *vi = netdev_priv(dev);
572
573 napi_schedule(&vi->napi);
574}
575#endif
576
296f96fc
RR
577static int virtnet_open(struct net_device *dev)
578{
579 struct virtnet_info *vi = netdev_priv(dev);
580
296f96fc 581 napi_enable(&vi->napi);
a48bd8f6
RR
582
583 /* If all buffers were filled by other side before we napi_enabled, we
584 * won't get another interrupt, so process any outstanding packets
370076d9
CB
585 * now. virtnet_poll wants re-enable the queue, so we disable here.
586 * We synchronize against interrupts via NAPI_STATE_SCHED */
288379f0 587 if (napi_schedule_prep(&vi->napi)) {
370076d9 588 vi->rvq->vq_ops->disable_cb(vi->rvq);
288379f0 589 __napi_schedule(&vi->napi);
370076d9 590 }
296f96fc
RR
591 return 0;
592}
593
2a41f71d
AW
594/*
595 * Send command via the control virtqueue and check status. Commands
596 * supported by the hypervisor, as indicated by feature bits, should
597 * never fail unless improperly formated.
598 */
599static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
600 struct scatterlist *data, int out, int in)
601{
602 struct scatterlist sg[VIRTNET_SEND_COMMAND_SG_MAX + 2];
603 struct virtio_net_ctrl_hdr ctrl;
604 virtio_net_ctrl_ack status = ~0;
605 unsigned int tmp;
606
607 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)) {
608 BUG(); /* Caller should know better */
609 return false;
610 }
611
612 BUG_ON(out + in > VIRTNET_SEND_COMMAND_SG_MAX);
613
614 out++; /* Add header */
615 in++; /* Add return status */
616
617 ctrl.class = class;
618 ctrl.cmd = cmd;
619
620 sg_init_table(sg, out + in);
621
622 sg_set_buf(&sg[0], &ctrl, sizeof(ctrl));
623 memcpy(&sg[1], data, sizeof(struct scatterlist) * (out + in - 2));
624 sg_set_buf(&sg[out + in - 1], &status, sizeof(status));
625
626 if (vi->cvq->vq_ops->add_buf(vi->cvq, sg, out, in, vi) != 0)
627 BUG();
628
629 vi->cvq->vq_ops->kick(vi->cvq);
630
631 /*
632 * Spin for a response, the kick causes an ioport write, trapping
633 * into the hypervisor, so the request should be handled immediately.
634 */
635 while (!vi->cvq->vq_ops->get_buf(vi->cvq, &tmp))
636 cpu_relax();
637
638 return status == VIRTIO_NET_OK;
639}
640
296f96fc
RR
641static int virtnet_close(struct net_device *dev)
642{
643 struct virtnet_info *vi = netdev_priv(dev);
296f96fc
RR
644
645 napi_disable(&vi->napi);
646
296f96fc
RR
647 return 0;
648}
649
a9ea3fc6
HX
650static int virtnet_set_tx_csum(struct net_device *dev, u32 data)
651{
652 struct virtnet_info *vi = netdev_priv(dev);
653 struct virtio_device *vdev = vi->vdev;
654
655 if (data && !virtio_has_feature(vdev, VIRTIO_NET_F_CSUM))
656 return -ENOSYS;
657
658 return ethtool_op_set_tx_hw_csum(dev, data);
659}
660
2af7698e
AW
661static void virtnet_set_rx_mode(struct net_device *dev)
662{
663 struct virtnet_info *vi = netdev_priv(dev);
f565a7c2 664 struct scatterlist sg[2];
2af7698e 665 u8 promisc, allmulti;
f565a7c2
AW
666 struct virtio_net_ctrl_mac *mac_data;
667 struct dev_addr_list *addr;
668 void *buf;
669 int i;
2af7698e
AW
670
671 /* We can't dynamicaly set ndo_set_rx_mode, so return gracefully */
672 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
673 return;
674
f565a7c2
AW
675 promisc = ((dev->flags & IFF_PROMISC) != 0);
676 allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
2af7698e 677
f565a7c2 678 sg_set_buf(sg, &promisc, sizeof(promisc));
2af7698e
AW
679
680 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
681 VIRTIO_NET_CTRL_RX_PROMISC,
f565a7c2 682 sg, 1, 0))
2af7698e
AW
683 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
684 promisc ? "en" : "dis");
685
f565a7c2 686 sg_set_buf(sg, &allmulti, sizeof(allmulti));
2af7698e
AW
687
688 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
689 VIRTIO_NET_CTRL_RX_ALLMULTI,
f565a7c2 690 sg, 1, 0))
2af7698e
AW
691 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
692 allmulti ? "en" : "dis");
f565a7c2
AW
693
694 /* MAC filter - use one buffer for both lists */
695 mac_data = buf = kzalloc(((dev->uc_count + dev->mc_count) * ETH_ALEN) +
696 (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
697 if (!buf) {
698 dev_warn(&dev->dev, "No memory for MAC address buffer\n");
699 return;
700 }
701
702 /* Store the unicast list and count in the front of the buffer */
703 mac_data->entries = dev->uc_count;
704 addr = dev->uc_list;
705 for (i = 0; i < dev->uc_count; i++, addr = addr->next)
706 memcpy(&mac_data->macs[i][0], addr->da_addr, ETH_ALEN);
707
708 sg_set_buf(&sg[0], mac_data,
709 sizeof(mac_data->entries) + (dev->uc_count * ETH_ALEN));
710
711 /* multicast list and count fill the end */
712 mac_data = (void *)&mac_data->macs[dev->uc_count][0];
713
714 mac_data->entries = dev->mc_count;
715 addr = dev->mc_list;
716 for (i = 0; i < dev->mc_count; i++, addr = addr->next)
717 memcpy(&mac_data->macs[i][0], addr->da_addr, ETH_ALEN);
718
719 sg_set_buf(&sg[1], mac_data,
720 sizeof(mac_data->entries) + (dev->mc_count * ETH_ALEN));
721
722 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
723 VIRTIO_NET_CTRL_MAC_TABLE_SET,
724 sg, 2, 0))
725 dev_warn(&dev->dev, "Failed to set MAC fitler table.\n");
726
727 kfree(buf);
2af7698e
AW
728}
729
a9ea3fc6
HX
730static struct ethtool_ops virtnet_ethtool_ops = {
731 .set_tx_csum = virtnet_set_tx_csum,
732 .set_sg = ethtool_op_set_sg,
0276b497 733 .set_tso = ethtool_op_set_tso,
9f4d26d0 734 .get_link = ethtool_op_get_link,
a9ea3fc6
HX
735};
736
39da5814
MM
737#define MIN_MTU 68
738#define MAX_MTU 65535
739
740static int virtnet_change_mtu(struct net_device *dev, int new_mtu)
741{
742 if (new_mtu < MIN_MTU || new_mtu > MAX_MTU)
743 return -EINVAL;
744 dev->mtu = new_mtu;
745 return 0;
746}
747
76288b4e
SH
748static const struct net_device_ops virtnet_netdev = {
749 .ndo_open = virtnet_open,
750 .ndo_stop = virtnet_close,
751 .ndo_start_xmit = start_xmit,
752 .ndo_validate_addr = eth_validate_addr,
753 .ndo_set_mac_address = eth_mac_addr,
2af7698e 754 .ndo_set_rx_mode = virtnet_set_rx_mode,
76288b4e
SH
755 .ndo_change_mtu = virtnet_change_mtu,
756#ifdef CONFIG_NET_POLL_CONTROLLER
757 .ndo_poll_controller = virtnet_netpoll,
758#endif
759};
760
9f4d26d0
MM
761static void virtnet_update_status(struct virtnet_info *vi)
762{
763 u16 v;
764
765 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS))
766 return;
767
768 vi->vdev->config->get(vi->vdev,
769 offsetof(struct virtio_net_config, status),
770 &v, sizeof(v));
771
772 /* Ignore unknown (future) status bits */
773 v &= VIRTIO_NET_S_LINK_UP;
774
775 if (vi->status == v)
776 return;
777
778 vi->status = v;
779
780 if (vi->status & VIRTIO_NET_S_LINK_UP) {
781 netif_carrier_on(vi->dev);
782 netif_wake_queue(vi->dev);
783 } else {
784 netif_carrier_off(vi->dev);
785 netif_stop_queue(vi->dev);
786 }
787}
788
789static void virtnet_config_changed(struct virtio_device *vdev)
790{
791 struct virtnet_info *vi = vdev->priv;
792
793 virtnet_update_status(vi);
794}
795
296f96fc
RR
796static int virtnet_probe(struct virtio_device *vdev)
797{
798 int err;
296f96fc
RR
799 struct net_device *dev;
800 struct virtnet_info *vi;
296f96fc
RR
801
802 /* Allocate ourselves a network device with room for our info */
803 dev = alloc_etherdev(sizeof(struct virtnet_info));
804 if (!dev)
805 return -ENOMEM;
806
807 /* Set up network device as normal. */
76288b4e 808 dev->netdev_ops = &virtnet_netdev;
296f96fc 809 dev->features = NETIF_F_HIGHDMA;
a9ea3fc6 810 SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops);
296f96fc
RR
811 SET_NETDEV_DEV(dev, &vdev->dev);
812
813 /* Do we support "hardware" checksums? */
c45a6816 814 if (csum && virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
296f96fc
RR
815 /* This opens up the world of extra features. */
816 dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
c45a6816 817 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
34a48579
RR
818 dev->features |= NETIF_F_TSO | NETIF_F_UFO
819 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
820 }
5539ae96 821 /* Individual feature bits: what can host handle? */
c45a6816 822 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
5539ae96 823 dev->features |= NETIF_F_TSO;
c45a6816 824 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
5539ae96 825 dev->features |= NETIF_F_TSO6;
c45a6816 826 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
5539ae96 827 dev->features |= NETIF_F_TSO_ECN;
c45a6816 828 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
5539ae96 829 dev->features |= NETIF_F_UFO;
296f96fc
RR
830 }
831
832 /* Configuration may specify what MAC to use. Otherwise random. */
c45a6816 833 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
a586d4f6
RR
834 vdev->config->get(vdev,
835 offsetof(struct virtio_net_config, mac),
836 dev->dev_addr, dev->addr_len);
296f96fc
RR
837 } else
838 random_ether_addr(dev->dev_addr);
839
840 /* Set up our device-specific information */
841 vi = netdev_priv(dev);
6c0cd7c0 842 netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight);
296f96fc
RR
843 vi->dev = dev;
844 vi->vdev = vdev;
d9d5dcc8 845 vdev->priv = vi;
fb6813f4 846 vi->pages = NULL;
296f96fc 847
363f1514
RR
848 /* If they give us a callback when all buffers are done, we don't need
849 * the timer. */
850 vi->free_in_tasklet = virtio_has_feature(vdev,VIRTIO_F_NOTIFY_ON_EMPTY);
851
97402b96
HX
852 /* If we can receive ANY GSO packets, we must allocate large ones. */
853 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4)
854 || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6)
855 || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN))
856 vi->big_packets = true;
857
3f2c31d9
MM
858 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
859 vi->mergeable_rx_bufs = true;
860
296f96fc 861 /* We expect two virtqueues, receive then send. */
a586d4f6 862 vi->rvq = vdev->config->find_vq(vdev, 0, skb_recv_done);
296f96fc
RR
863 if (IS_ERR(vi->rvq)) {
864 err = PTR_ERR(vi->rvq);
865 goto free;
866 }
867
a586d4f6 868 vi->svq = vdev->config->find_vq(vdev, 1, skb_xmit_done);
296f96fc
RR
869 if (IS_ERR(vi->svq)) {
870 err = PTR_ERR(vi->svq);
871 goto free_recv;
872 }
873
2a41f71d
AW
874 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)) {
875 vi->cvq = vdev->config->find_vq(vdev, 2, NULL);
876 if (IS_ERR(vi->cvq)) {
877 err = PTR_ERR(vi->svq);
878 goto free_send;
879 }
880 }
881
296f96fc
RR
882 /* Initialize our empty receive and send queues. */
883 skb_queue_head_init(&vi->recv);
884 skb_queue_head_init(&vi->send);
885
11a3a154
RR
886 tasklet_init(&vi->tasklet, xmit_tasklet, (unsigned long)vi);
887
363f1514
RR
888 if (!vi->free_in_tasklet)
889 setup_timer(&vi->xmit_free_timer, xmit_free, (unsigned long)vi);
14c998f0 890
296f96fc
RR
891 err = register_netdev(dev);
892 if (err) {
893 pr_debug("virtio_net: registering device failed\n");
2a41f71d 894 goto free_ctrl;
296f96fc 895 }
b3369c1f
RR
896
897 /* Last of all, set up some receive buffers. */
898 try_fill_recv(vi);
899
900 /* If we didn't even get one input buffer, we're useless. */
901 if (vi->num == 0) {
902 err = -ENOMEM;
903 goto unregister;
904 }
905
9f4d26d0
MM
906 vi->status = VIRTIO_NET_S_LINK_UP;
907 virtnet_update_status(vi);
908
296f96fc 909 pr_debug("virtnet: registered device %s\n", dev->name);
296f96fc
RR
910 return 0;
911
b3369c1f
RR
912unregister:
913 unregister_netdev(dev);
2a41f71d
AW
914free_ctrl:
915 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ))
916 vdev->config->del_vq(vi->cvq);
296f96fc
RR
917free_send:
918 vdev->config->del_vq(vi->svq);
919free_recv:
920 vdev->config->del_vq(vi->rvq);
921free:
922 free_netdev(dev);
923 return err;
924}
925
926static void virtnet_remove(struct virtio_device *vdev)
927{
74b2553f 928 struct virtnet_info *vi = vdev->priv;
b3369c1f
RR
929 struct sk_buff *skb;
930
6e5aa7ef
RR
931 /* Stop all the virtqueues. */
932 vdev->config->reset(vdev);
933
363f1514
RR
934 if (!vi->free_in_tasklet)
935 del_timer_sync(&vi->xmit_free_timer);
14c998f0 936
b3369c1f 937 /* Free our skbs in send and recv queues, if any. */
b3369c1f
RR
938 while ((skb = __skb_dequeue(&vi->recv)) != NULL) {
939 kfree_skb(skb);
940 vi->num--;
941 }
288369cc 942 __skb_queue_purge(&vi->send);
b3369c1f
RR
943
944 BUG_ON(vi->num != 0);
74b2553f
RR
945
946 vdev->config->del_vq(vi->svq);
947 vdev->config->del_vq(vi->rvq);
2a41f71d
AW
948 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ))
949 vdev->config->del_vq(vi->cvq);
74b2553f 950 unregister_netdev(vi->dev);
fb6813f4
RR
951
952 while (vi->pages)
953 __free_pages(get_a_page(vi, GFP_KERNEL), 0);
954
74b2553f 955 free_netdev(vi->dev);
296f96fc
RR
956}
957
958static struct virtio_device_id id_table[] = {
959 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
960 { 0 },
961};
962
c45a6816 963static unsigned int features[] = {
5e4fe5c4
MM
964 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM,
965 VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
c45a6816 966 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
97402b96
HX
967 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
968 VIRTIO_NET_F_GUEST_ECN, /* We don't yet handle UFO input. */
2a41f71d 969 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ,
2af7698e 970 VIRTIO_NET_F_CTRL_RX,
97402b96 971 VIRTIO_F_NOTIFY_ON_EMPTY,
c45a6816
RR
972};
973
296f96fc 974static struct virtio_driver virtio_net = {
c45a6816
RR
975 .feature_table = features,
976 .feature_table_size = ARRAY_SIZE(features),
296f96fc
RR
977 .driver.name = KBUILD_MODNAME,
978 .driver.owner = THIS_MODULE,
979 .id_table = id_table,
980 .probe = virtnet_probe,
981 .remove = __devexit_p(virtnet_remove),
9f4d26d0 982 .config_changed = virtnet_config_changed,
296f96fc
RR
983};
984
985static int __init init(void)
986{
987 return register_virtio_driver(&virtio_net);
988}
989
990static void __exit fini(void)
991{
992 unregister_virtio_driver(&virtio_net);
993}
994module_init(init);
995module_exit(fini);
996
997MODULE_DEVICE_TABLE(virtio, id_table);
998MODULE_DESCRIPTION("Virtio network driver");
999MODULE_LICENSE("GPL");