blob: 3041e4eddb3b7363dd2c6f88c38f9d193defad13 [file] [log] [blame]
Rusty Russell296f96f2007-10-22 11:03:37 +10001/* 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>
Herbert Xua9ea3fc2008-04-18 11:21:42 +080022#include <linux/ethtool.h>
Rusty Russell296f96f2007-10-22 11:03:37 +100023#include <linux/module.h>
24#include <linux/virtio.h>
Fernando Luis Vazquez Cao3ca4f5c2009-07-31 15:25:56 +090025#include <linux/virtio_ids.h>
Rusty Russell296f96f2007-10-22 11:03:37 +100026#include <linux/virtio_net.h>
27#include <linux/scatterlist.h>
Alex Williamsone918085a2009-01-25 18:06:26 -080028#include <linux/if_vlan.h>
Rusty Russell296f96f2007-10-22 11:03:37 +100029
Dor Laor6c0cd7c2007-12-16 15:19:43 +020030static int napi_weight = 128;
31module_param(napi_weight, int, 0444);
32
Rusty Russell34a48572008-02-04 23:50:02 -050033static int csum = 1, gso = 1;
34module_param(csum, bool, 0444);
35module_param(gso, bool, 0444);
36
Rusty Russell296f96f2007-10-22 11:03:37 +100037/* FIXME: MTU in config. */
Alex Williamsone918085a2009-01-25 18:06:26 -080038#define MAX_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -080039#define GOOD_COPY_LEN 128
Rusty Russell296f96f2007-10-22 11:03:37 +100040
Alex Williamsonf565a7c2009-02-04 09:02:45 +000041#define VIRTNET_SEND_COMMAND_SG_MAX 2
Alex Williamson2a41f712009-02-04 09:02:34 +000042
Rusty Russell296f96f2007-10-22 11:03:37 +100043struct virtnet_info
44{
45 struct virtio_device *vdev;
Alex Williamson2a41f712009-02-04 09:02:34 +000046 struct virtqueue *rvq, *svq, *cvq;
Rusty Russell296f96f2007-10-22 11:03:37 +100047 struct net_device *dev;
48 struct napi_struct napi;
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -080049 unsigned int status;
Rusty Russell296f96f2007-10-22 11:03:37 +100050
51 /* Number of input buffers, and max we've ever had. */
52 unsigned int num, max;
53
Herbert Xu97402b92008-04-18 11:24:27 +080054 /* I like... big packets and I cannot lie! */
55 bool big_packets;
56
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -080057 /* Host will merge rx buffers for big packets (shake it! shake it!) */
58 bool mergeable_rx_bufs;
59
Rusty Russell296f96f2007-10-22 11:03:37 +100060 /* Receive & send queues. */
61 struct sk_buff_head recv;
62 struct sk_buff_head send;
Rusty Russellfb6813f2008-07-25 12:06:01 -050063
Rusty Russell3161e452009-08-26 12:22:32 -070064 /* Work struct for refilling if we run low on memory. */
65 struct delayed_work refill;
66
Rusty Russellfb6813f2008-07-25 12:06:01 -050067 /* Chain pages by the private ptr. */
68 struct page *pages;
Rusty Russell296f96f2007-10-22 11:03:37 +100069};
70
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -080071static inline void *skb_vnet_hdr(struct sk_buff *skb)
Rusty Russell296f96f2007-10-22 11:03:37 +100072{
73 return (struct virtio_net_hdr *)skb->cb;
74}
75
Rusty Russellfb6813f2008-07-25 12:06:01 -050076static void give_a_page(struct virtnet_info *vi, struct page *page)
77{
78 page->private = (unsigned long)vi->pages;
79 vi->pages = page;
80}
81
Mark McLoughlin0a888fd2008-11-16 22:39:18 -080082static void trim_pages(struct virtnet_info *vi, struct sk_buff *skb)
83{
84 unsigned int i;
85
86 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
87 give_a_page(vi, skb_shinfo(skb)->frags[i].page);
88 skb_shinfo(skb)->nr_frags = 0;
89 skb->data_len = 0;
90}
91
Rusty Russellfb6813f2008-07-25 12:06:01 -050092static struct page *get_a_page(struct virtnet_info *vi, gfp_t gfp_mask)
93{
94 struct page *p = vi->pages;
95
96 if (p)
97 vi->pages = (struct page *)p->private;
98 else
99 p = alloc_page(gfp_mask);
100 return p;
101}
102
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500103static void skb_xmit_done(struct virtqueue *svq)
Rusty Russell296f96f2007-10-22 11:03:37 +1000104{
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500105 struct virtnet_info *vi = svq->vdev->priv;
Rusty Russell296f96f2007-10-22 11:03:37 +1000106
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500107 /* Suppress further interrupts. */
108 svq->vq_ops->disable_cb(svq);
Rusty Russell11a3a152008-05-26 17:48:13 +1000109
Rusty Russell363f1512008-06-08 20:51:55 +1000110 /* We were probably waiting for more output buffers. */
Rusty Russell296f96f2007-10-22 11:03:37 +1000111 netif_wake_queue(vi->dev);
Rusty Russell296f96f2007-10-22 11:03:37 +1000112}
113
114static void receive_skb(struct net_device *dev, struct sk_buff *skb,
115 unsigned len)
116{
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800117 struct virtnet_info *vi = netdev_priv(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +1000118 struct virtio_net_hdr *hdr = skb_vnet_hdr(skb);
Herbert Xu97402b92008-04-18 11:24:27 +0800119 int err;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800120 int i;
Rusty Russell296f96f2007-10-22 11:03:37 +1000121
122 if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) {
123 pr_debug("%s: short packet %i\n", dev->name, len);
124 dev->stats.rx_length_errors++;
125 goto drop;
126 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000127
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800128 if (vi->mergeable_rx_bufs) {
129 struct virtio_net_hdr_mrg_rxbuf *mhdr = skb_vnet_hdr(skb);
130 unsigned int copy;
131 char *p = page_address(skb_shinfo(skb)->frags[0].page);
Rusty Russellfb6813f2008-07-25 12:06:01 -0500132
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800133 if (len > PAGE_SIZE)
134 len = PAGE_SIZE;
135 len -= sizeof(struct virtio_net_hdr_mrg_rxbuf);
136
137 memcpy(hdr, p, sizeof(*mhdr));
138 p += sizeof(*mhdr);
139
140 copy = len;
141 if (copy > skb_tailroom(skb))
142 copy = skb_tailroom(skb);
143
144 memcpy(skb_put(skb, copy), p, copy);
145
146 len -= copy;
147
148 if (!len) {
149 give_a_page(vi, skb_shinfo(skb)->frags[0].page);
150 skb_shinfo(skb)->nr_frags--;
151 } else {
152 skb_shinfo(skb)->frags[0].page_offset +=
153 sizeof(*mhdr) + copy;
154 skb_shinfo(skb)->frags[0].size = len;
155 skb->data_len += len;
156 skb->len += len;
157 }
158
159 while (--mhdr->num_buffers) {
160 struct sk_buff *nskb;
161
162 i = skb_shinfo(skb)->nr_frags;
163 if (i >= MAX_SKB_FRAGS) {
164 pr_debug("%s: packet too long %d\n", dev->name,
165 len);
166 dev->stats.rx_length_errors++;
167 goto drop;
168 }
169
170 nskb = vi->rvq->vq_ops->get_buf(vi->rvq, &len);
171 if (!nskb) {
172 pr_debug("%s: rx error: %d buffers missing\n",
173 dev->name, mhdr->num_buffers);
174 dev->stats.rx_length_errors++;
175 goto drop;
176 }
177
178 __skb_unlink(nskb, &vi->recv);
179 vi->num--;
180
181 skb_shinfo(skb)->frags[i] = skb_shinfo(nskb)->frags[0];
182 skb_shinfo(nskb)->nr_frags = 0;
183 kfree_skb(nskb);
184
185 if (len > PAGE_SIZE)
186 len = PAGE_SIZE;
187
188 skb_shinfo(skb)->frags[i].size = len;
189 skb_shinfo(skb)->nr_frags++;
190 skb->data_len += len;
191 skb->len += len;
192 }
193 } else {
194 len -= sizeof(struct virtio_net_hdr);
195
196 if (len <= MAX_PACKET_LEN)
197 trim_pages(vi, skb);
198
199 err = pskb_trim(skb, len);
200 if (err) {
201 pr_debug("%s: pskb_trim failed %i %d\n", dev->name,
202 len, err);
203 dev->stats.rx_dropped++;
204 goto drop;
205 }
Herbert Xu97402b92008-04-18 11:24:27 +0800206 }
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800207
Herbert Xu97402b92008-04-18 11:24:27 +0800208 skb->truesize += skb->data_len;
Rusty Russell296f96f2007-10-22 11:03:37 +1000209 dev->stats.rx_bytes += skb->len;
210 dev->stats.rx_packets++;
211
212 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
213 pr_debug("Needs csum!\n");
Rusty Russellf35d9d82008-02-04 23:49:54 -0500214 if (!skb_partial_csum_set(skb,hdr->csum_start,hdr->csum_offset))
Rusty Russell296f96f2007-10-22 11:03:37 +1000215 goto frame_err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000216 }
217
Mark McLoughlin23cde762008-06-08 20:49:00 +1000218 skb->protocol = eth_type_trans(skb, dev);
219 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
220 ntohs(skb->protocol), skb->len, skb->pkt_type);
221
Rusty Russell296f96f2007-10-22 11:03:37 +1000222 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
223 pr_debug("GSO!\n");
Rusty Russell34a48572008-02-04 23:50:02 -0500224 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000225 case VIRTIO_NET_HDR_GSO_TCPV4:
226 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
227 break;
Rusty Russell296f96f2007-10-22 11:03:37 +1000228 case VIRTIO_NET_HDR_GSO_UDP:
229 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
230 break;
231 case VIRTIO_NET_HDR_GSO_TCPV6:
232 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
233 break;
234 default:
235 if (net_ratelimit())
236 printk(KERN_WARNING "%s: bad gso type %u.\n",
237 dev->name, hdr->gso_type);
238 goto frame_err;
239 }
240
Rusty Russell34a48572008-02-04 23:50:02 -0500241 if (hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN)
242 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
243
Rusty Russell296f96f2007-10-22 11:03:37 +1000244 skb_shinfo(skb)->gso_size = hdr->gso_size;
245 if (skb_shinfo(skb)->gso_size == 0) {
246 if (net_ratelimit())
247 printk(KERN_WARNING "%s: zero gso size.\n",
248 dev->name);
249 goto frame_err;
250 }
251
252 /* Header must be checked, and gso_segs computed. */
253 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
254 skb_shinfo(skb)->gso_segs = 0;
255 }
256
257 netif_receive_skb(skb);
258 return;
259
260frame_err:
261 dev->stats.rx_frame_errors++;
262drop:
263 dev_kfree_skb(skb);
264}
265
Rusty Russell3161e452009-08-26 12:22:32 -0700266static bool try_fill_recv_maxbufs(struct virtnet_info *vi, gfp_t gfp)
Rusty Russell296f96f2007-10-22 11:03:37 +1000267{
268 struct sk_buff *skb;
Rusty Russell05271682008-05-02 21:50:45 -0500269 struct scatterlist sg[2+MAX_SKB_FRAGS];
Herbert Xu97402b92008-04-18 11:24:27 +0800270 int num, err, i;
Rusty Russell3161e452009-08-26 12:22:32 -0700271 bool oom = false;
Rusty Russell296f96f2007-10-22 11:03:37 +1000272
Rusty Russell05271682008-05-02 21:50:45 -0500273 sg_init_table(sg, 2+MAX_SKB_FRAGS);
Rusty Russell296f96f2007-10-22 11:03:37 +1000274 for (;;) {
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800275 struct virtio_net_hdr *hdr;
276
Herbert Xu8981f012009-06-11 20:55:17 -0700277 skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN + NET_IP_ALIGN);
Rusty Russell3161e452009-08-26 12:22:32 -0700278 if (unlikely(!skb)) {
279 oom = true;
Rusty Russell296f96f2007-10-22 11:03:37 +1000280 break;
Rusty Russell3161e452009-08-26 12:22:32 -0700281 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000282
Herbert Xu8981f012009-06-11 20:55:17 -0700283 skb_reserve(skb, NET_IP_ALIGN);
Rusty Russell296f96f2007-10-22 11:03:37 +1000284 skb_put(skb, MAX_PACKET_LEN);
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800285
286 hdr = skb_vnet_hdr(skb);
Ira W. Snyder8527bec2009-01-26 21:00:33 -0800287 sg_set_buf(sg, hdr, sizeof(*hdr));
Herbert Xu97402b92008-04-18 11:24:27 +0800288
289 if (vi->big_packets) {
290 for (i = 0; i < MAX_SKB_FRAGS; i++) {
291 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
Rusty Russell3161e452009-08-26 12:22:32 -0700292 f->page = get_a_page(vi, gfp);
Herbert Xu97402b92008-04-18 11:24:27 +0800293 if (!f->page)
294 break;
295
296 f->page_offset = 0;
297 f->size = PAGE_SIZE;
298
299 skb->data_len += PAGE_SIZE;
300 skb->len += PAGE_SIZE;
301
302 skb_shinfo(skb)->nr_frags++;
303 }
304 }
305
Rusty Russell296f96f2007-10-22 11:03:37 +1000306 num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
307 skb_queue_head(&vi->recv, skb);
308
309 err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, num, skb);
Rusty Russell3c1b27d2009-09-23 22:26:31 -0600310 if (err < 0) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000311 skb_unlink(skb, &vi->recv);
Mark McLoughlin0a888fd2008-11-16 22:39:18 -0800312 trim_pages(vi, skb);
Rusty Russell296f96f2007-10-22 11:03:37 +1000313 kfree_skb(skb);
314 break;
315 }
316 vi->num++;
317 }
318 if (unlikely(vi->num > vi->max))
319 vi->max = vi->num;
320 vi->rvq->vq_ops->kick(vi->rvq);
Rusty Russell3161e452009-08-26 12:22:32 -0700321 return !oom;
Rusty Russell296f96f2007-10-22 11:03:37 +1000322}
323
Rusty Russell3161e452009-08-26 12:22:32 -0700324/* Returns false if we couldn't fill entirely (OOM). */
325static bool try_fill_recv(struct virtnet_info *vi, gfp_t gfp)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800326{
327 struct sk_buff *skb;
328 struct scatterlist sg[1];
329 int err;
Rusty Russell3161e452009-08-26 12:22:32 -0700330 bool oom = false;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800331
Rusty Russell3161e452009-08-26 12:22:32 -0700332 if (!vi->mergeable_rx_bufs)
333 return try_fill_recv_maxbufs(vi, gfp);
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800334
335 for (;;) {
336 skb_frag_t *f;
337
338 skb = netdev_alloc_skb(vi->dev, GOOD_COPY_LEN + NET_IP_ALIGN);
Rusty Russell3161e452009-08-26 12:22:32 -0700339 if (unlikely(!skb)) {
340 oom = true;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800341 break;
Rusty Russell3161e452009-08-26 12:22:32 -0700342 }
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800343
344 skb_reserve(skb, NET_IP_ALIGN);
345
346 f = &skb_shinfo(skb)->frags[0];
Rusty Russell3161e452009-08-26 12:22:32 -0700347 f->page = get_a_page(vi, gfp);
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800348 if (!f->page) {
Rusty Russell3161e452009-08-26 12:22:32 -0700349 oom = true;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800350 kfree_skb(skb);
351 break;
352 }
353
354 f->page_offset = 0;
355 f->size = PAGE_SIZE;
356
357 skb_shinfo(skb)->nr_frags++;
358
359 sg_init_one(sg, page_address(f->page), PAGE_SIZE);
360 skb_queue_head(&vi->recv, skb);
361
362 err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, 1, skb);
Rusty Russell3c1b27d2009-09-23 22:26:31 -0600363 if (err < 0) {
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800364 skb_unlink(skb, &vi->recv);
365 kfree_skb(skb);
366 break;
367 }
368 vi->num++;
369 }
370 if (unlikely(vi->num > vi->max))
371 vi->max = vi->num;
372 vi->rvq->vq_ops->kick(vi->rvq);
Rusty Russell3161e452009-08-26 12:22:32 -0700373 return !oom;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800374}
375
Rusty Russell18445c42008-02-04 23:49:57 -0500376static void skb_recv_done(struct virtqueue *rvq)
Rusty Russell296f96f2007-10-22 11:03:37 +1000377{
378 struct virtnet_info *vi = rvq->vdev->priv;
Rusty Russell18445c42008-02-04 23:49:57 -0500379 /* Schedule NAPI, Suppress further interrupts if successful. */
Ben Hutchings288379f2009-01-19 16:43:59 -0800380 if (napi_schedule_prep(&vi->napi)) {
Rusty Russell18445c42008-02-04 23:49:57 -0500381 rvq->vq_ops->disable_cb(rvq);
Ben Hutchings288379f2009-01-19 16:43:59 -0800382 __napi_schedule(&vi->napi);
Rusty Russell18445c42008-02-04 23:49:57 -0500383 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000384}
385
Rusty Russell3161e452009-08-26 12:22:32 -0700386static void refill_work(struct work_struct *work)
387{
388 struct virtnet_info *vi;
389 bool still_empty;
390
391 vi = container_of(work, struct virtnet_info, refill.work);
392 napi_disable(&vi->napi);
393 try_fill_recv(vi, GFP_KERNEL);
394 still_empty = (vi->num == 0);
395 napi_enable(&vi->napi);
396
397 /* In theory, this can happen: if we don't get any buffers in
398 * we will *never* try to fill again. */
399 if (still_empty)
400 schedule_delayed_work(&vi->refill, HZ/2);
401}
402
Rusty Russell296f96f2007-10-22 11:03:37 +1000403static int virtnet_poll(struct napi_struct *napi, int budget)
404{
405 struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi);
406 struct sk_buff *skb = NULL;
407 unsigned int len, received = 0;
408
409again:
410 while (received < budget &&
411 (skb = vi->rvq->vq_ops->get_buf(vi->rvq, &len)) != NULL) {
412 __skb_unlink(skb, &vi->recv);
413 receive_skb(vi->dev, skb, len);
414 vi->num--;
415 received++;
416 }
417
Rusty Russell3161e452009-08-26 12:22:32 -0700418 if (vi->num < vi->max / 2) {
419 if (!try_fill_recv(vi, GFP_ATOMIC))
420 schedule_delayed_work(&vi->refill, 0);
421 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000422
Rusty Russell8329d982007-11-19 11:20:43 -0500423 /* Out of packets? */
424 if (received < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -0800425 napi_complete(napi);
Rusty Russell18445c42008-02-04 23:49:57 -0500426 if (unlikely(!vi->rvq->vq_ops->enable_cb(vi->rvq))
Christian Borntraeger4265f162008-03-14 14:17:05 +0100427 && napi_schedule_prep(napi)) {
428 vi->rvq->vq_ops->disable_cb(vi->rvq);
Ben Hutchings288379f2009-01-19 16:43:59 -0800429 __napi_schedule(napi);
Rusty Russell296f96f2007-10-22 11:03:37 +1000430 goto again;
Christian Borntraeger4265f162008-03-14 14:17:05 +0100431 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000432 }
433
434 return received;
435}
436
437static void free_old_xmit_skbs(struct virtnet_info *vi)
438{
439 struct sk_buff *skb;
440 unsigned int len;
441
442 while ((skb = vi->svq->vq_ops->get_buf(vi->svq, &len)) != NULL) {
443 pr_debug("Sent skb %p\n", skb);
444 __skb_unlink(skb, &vi->send);
Rusty Russell655aa312008-05-02 21:50:43 -0500445 vi->dev->stats.tx_bytes += skb->len;
Rusty Russell296f96f2007-10-22 11:03:37 +1000446 vi->dev->stats.tx_packets++;
447 kfree_skb(skb);
448 }
449}
450
Rusty Russell99ffc692008-05-02 21:50:46 -0500451static int xmit_skb(struct virtnet_info *vi, struct sk_buff *skb)
Rusty Russell296f96f2007-10-22 11:03:37 +1000452{
Rusty Russellb0c39dbd2009-09-24 09:59:19 -0600453 int num;
Rusty Russell05271682008-05-02 21:50:45 -0500454 struct scatterlist sg[2+MAX_SKB_FRAGS];
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800455 struct virtio_net_hdr_mrg_rxbuf *mhdr = skb_vnet_hdr(skb);
456 struct virtio_net_hdr *hdr = skb_vnet_hdr(skb);
Rusty Russell296f96f2007-10-22 11:03:37 +1000457 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
Rusty Russell296f96f2007-10-22 11:03:37 +1000458
Rusty Russell05271682008-05-02 21:50:45 -0500459 sg_init_table(sg, 2+MAX_SKB_FRAGS);
Rusty Russell4d125de2007-11-07 16:34:49 +1100460
Johannes Berge1749612008-10-27 15:59:26 -0700461 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
Rusty Russell296f96f2007-10-22 11:03:37 +1000462
Rusty Russell296f96f2007-10-22 11:03:37 +1000463 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)) {
Herbert Xub82f08e2009-06-04 00:59:18 +0000473 hdr->hdr_len = skb_headlen(skb);
Rusty Russell296f96f2007-10-22 11:03:37 +1000474 hdr->gso_size = skb_shinfo(skb)->gso_size;
Rusty Russell34a48572008-02-04 23:50:02 -0500475 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
Rusty Russell296f96f2007-10-22 11:03:37 +1000476 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();
Rusty Russell34a48572008-02-04 23:50:02 -0500483 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
484 hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
Rusty Russell296f96f2007-10-22 11:03:37 +1000485 } else {
486 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
Rusty Russell50c8ea82008-02-04 23:50:01 -0500487 hdr->gso_size = hdr->hdr_len = 0;
Rusty Russell296f96f2007-10-22 11:03:37 +1000488 }
489
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800490 mhdr->num_buffers = 0;
491
492 /* Encode metadata header at front. */
493 if (vi->mergeable_rx_bufs)
Ira W. Snyder8527bec2009-01-26 21:00:33 -0800494 sg_set_buf(sg, mhdr, sizeof(*mhdr));
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800495 else
Ira W. Snyder8527bec2009-01-26 21:00:33 -0800496 sg_set_buf(sg, hdr, sizeof(*hdr));
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800497
Rusty Russell296f96f2007-10-22 11:03:37 +1000498 num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
Rusty Russellb0c39dbd2009-09-24 09:59:19 -0600499 return vi->svq->vq_ops->add_buf(vi->svq, sg, num, 0, skb);
Rusty Russell11a3a152008-05-26 17:48:13 +1000500}
501
Stephen Hemminger424efe92009-08-31 19:50:51 +0000502static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
Rusty Russell99ffc692008-05-02 21:50:46 -0500503{
504 struct virtnet_info *vi = netdev_priv(dev);
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500505
506again:
507 /* Free up any pending old buffers before queueing new ones. */
508 free_old_xmit_skbs(vi);
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500509
Rusty Russell99ffc692008-05-02 21:50:46 -0500510 /* Put new one in send queue and do transmit */
Rusty Russell8958f572009-09-24 09:59:18 -0600511 __skb_queue_head(&vi->send, skb);
512 if (likely(xmit_skb(vi, skb) >= 0)) {
513 vi->svq->vq_ops->kick(vi->svq);
Rusty Russellb0c39dbd2009-09-24 09:59:19 -0600514 /* Don't wait up for transmitted skbs to be freed. */
515 skb_orphan(skb);
516 nf_reset(skb);
Rusty Russell8958f572009-09-24 09:59:18 -0600517 return NETDEV_TX_OK;
Rusty Russell99ffc692008-05-02 21:50:46 -0500518 }
Rusty Russell99ffc692008-05-02 21:50:46 -0500519
Rusty Russell8958f572009-09-24 09:59:18 -0600520 /* Ring too full for this packet, remove it from queue again. */
Rusty Russell99ffc692008-05-02 21:50:46 -0500521 pr_debug("%s: virtio not prepared to send\n", dev->name);
Rusty Russell8958f572009-09-24 09:59:18 -0600522 __skb_unlink(skb, &vi->send);
Rusty Russell99ffc692008-05-02 21:50:46 -0500523 netif_stop_queue(dev);
524
525 /* Activate callback for using skbs: if this returns false it
526 * means some were used in the meantime. */
527 if (unlikely(!vi->svq->vq_ops->enable_cb(vi->svq))) {
528 vi->svq->vq_ops->disable_cb(vi->svq);
529 netif_start_queue(dev);
530 goto again;
531 }
Rusty Russell8958f572009-09-24 09:59:18 -0600532 return NETDEV_TX_BUSY;
Rusty Russell296f96f2007-10-22 11:03:37 +1000533}
534
Alex Williamson9c46f6d2009-02-04 16:36:34 -0800535static int virtnet_set_mac_address(struct net_device *dev, void *p)
536{
537 struct virtnet_info *vi = netdev_priv(dev);
538 struct virtio_device *vdev = vi->vdev;
539 int ret;
540
541 ret = eth_mac_addr(dev, p);
542 if (ret)
543 return ret;
544
Alex Williamson62994b22009-04-04 16:40:19 -0700545 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
546 vdev->config->set(vdev, offsetof(struct virtio_net_config, mac),
547 dev->dev_addr, dev->addr_len);
Alex Williamson9c46f6d2009-02-04 16:36:34 -0800548
549 return 0;
550}
551
Amit Shahda74e892008-02-29 16:24:50 +0530552#ifdef CONFIG_NET_POLL_CONTROLLER
553static void virtnet_netpoll(struct net_device *dev)
554{
555 struct virtnet_info *vi = netdev_priv(dev);
556
557 napi_schedule(&vi->napi);
558}
559#endif
560
Rusty Russell296f96f2007-10-22 11:03:37 +1000561static int virtnet_open(struct net_device *dev)
562{
563 struct virtnet_info *vi = netdev_priv(dev);
564
Rusty Russell296f96f2007-10-22 11:03:37 +1000565 napi_enable(&vi->napi);
Rusty Russella48bd8f2008-02-04 23:50:07 -0500566
567 /* If all buffers were filled by other side before we napi_enabled, we
568 * won't get another interrupt, so process any outstanding packets
Christian Borntraeger370076d2008-02-06 08:50:11 +0100569 * now. virtnet_poll wants re-enable the queue, so we disable here.
570 * We synchronize against interrupts via NAPI_STATE_SCHED */
Ben Hutchings288379f2009-01-19 16:43:59 -0800571 if (napi_schedule_prep(&vi->napi)) {
Christian Borntraeger370076d2008-02-06 08:50:11 +0100572 vi->rvq->vq_ops->disable_cb(vi->rvq);
Ben Hutchings288379f2009-01-19 16:43:59 -0800573 __napi_schedule(&vi->napi);
Christian Borntraeger370076d2008-02-06 08:50:11 +0100574 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000575 return 0;
576}
577
Alex Williamson2a41f712009-02-04 09:02:34 +0000578/*
579 * Send command via the control virtqueue and check status. Commands
580 * supported by the hypervisor, as indicated by feature bits, should
581 * never fail unless improperly formated.
582 */
583static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
584 struct scatterlist *data, int out, int in)
585{
Alex Williamson23e258e2009-05-01 17:27:56 +0000586 struct scatterlist *s, sg[VIRTNET_SEND_COMMAND_SG_MAX + 2];
Alex Williamson2a41f712009-02-04 09:02:34 +0000587 struct virtio_net_ctrl_hdr ctrl;
588 virtio_net_ctrl_ack status = ~0;
589 unsigned int tmp;
Alex Williamson23e258e2009-05-01 17:27:56 +0000590 int i;
Alex Williamson2a41f712009-02-04 09:02:34 +0000591
Alexander Beregalov0ee904c2009-04-11 14:50:23 +0000592 /* Caller should know better */
593 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) ||
594 (out + in > VIRTNET_SEND_COMMAND_SG_MAX));
Alex Williamson2a41f712009-02-04 09:02:34 +0000595
596 out++; /* Add header */
597 in++; /* Add return status */
598
599 ctrl.class = class;
600 ctrl.cmd = cmd;
601
602 sg_init_table(sg, out + in);
603
604 sg_set_buf(&sg[0], &ctrl, sizeof(ctrl));
Alex Williamson23e258e2009-05-01 17:27:56 +0000605 for_each_sg(data, s, out + in - 2, i)
606 sg_set_buf(&sg[i + 1], sg_virt(s), s->length);
Alex Williamson2a41f712009-02-04 09:02:34 +0000607 sg_set_buf(&sg[out + in - 1], &status, sizeof(status));
608
Rusty Russell3c1b27d2009-09-23 22:26:31 -0600609 BUG_ON(vi->cvq->vq_ops->add_buf(vi->cvq, sg, out, in, vi) < 0);
Alex Williamson2a41f712009-02-04 09:02:34 +0000610
611 vi->cvq->vq_ops->kick(vi->cvq);
612
613 /*
614 * Spin for a response, the kick causes an ioport write, trapping
615 * into the hypervisor, so the request should be handled immediately.
616 */
617 while (!vi->cvq->vq_ops->get_buf(vi->cvq, &tmp))
618 cpu_relax();
619
620 return status == VIRTIO_NET_OK;
621}
622
Rusty Russell296f96f2007-10-22 11:03:37 +1000623static int virtnet_close(struct net_device *dev)
624{
625 struct virtnet_info *vi = netdev_priv(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +1000626
627 napi_disable(&vi->napi);
628
Rusty Russell296f96f2007-10-22 11:03:37 +1000629 return 0;
630}
631
Herbert Xua9ea3fc2008-04-18 11:21:42 +0800632static int virtnet_set_tx_csum(struct net_device *dev, u32 data)
633{
634 struct virtnet_info *vi = netdev_priv(dev);
635 struct virtio_device *vdev = vi->vdev;
636
637 if (data && !virtio_has_feature(vdev, VIRTIO_NET_F_CSUM))
638 return -ENOSYS;
639
640 return ethtool_op_set_tx_hw_csum(dev, data);
641}
642
Alex Williamson2af76982009-02-04 09:02:40 +0000643static void virtnet_set_rx_mode(struct net_device *dev)
644{
645 struct virtnet_info *vi = netdev_priv(dev);
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000646 struct scatterlist sg[2];
Alex Williamson2af76982009-02-04 09:02:40 +0000647 u8 promisc, allmulti;
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000648 struct virtio_net_ctrl_mac *mac_data;
649 struct dev_addr_list *addr;
Jiri Pirkoccffad252009-05-22 23:22:17 +0000650 struct netdev_hw_addr *ha;
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000651 void *buf;
652 int i;
Alex Williamson2af76982009-02-04 09:02:40 +0000653
654 /* We can't dynamicaly set ndo_set_rx_mode, so return gracefully */
655 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
656 return;
657
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000658 promisc = ((dev->flags & IFF_PROMISC) != 0);
659 allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
Alex Williamson2af76982009-02-04 09:02:40 +0000660
Alex Williamson23e258e2009-05-01 17:27:56 +0000661 sg_init_one(sg, &promisc, sizeof(promisc));
Alex Williamson2af76982009-02-04 09:02:40 +0000662
663 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
664 VIRTIO_NET_CTRL_RX_PROMISC,
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000665 sg, 1, 0))
Alex Williamson2af76982009-02-04 09:02:40 +0000666 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
667 promisc ? "en" : "dis");
668
Alex Williamson23e258e2009-05-01 17:27:56 +0000669 sg_init_one(sg, &allmulti, sizeof(allmulti));
Alex Williamson2af76982009-02-04 09:02:40 +0000670
671 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
672 VIRTIO_NET_CTRL_RX_ALLMULTI,
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000673 sg, 1, 0))
Alex Williamson2af76982009-02-04 09:02:40 +0000674 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
675 allmulti ? "en" : "dis");
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000676
677 /* MAC filter - use one buffer for both lists */
Jiri Pirko31278e72009-06-17 01:12:19 +0000678 mac_data = buf = kzalloc(((dev->uc.count + dev->mc_count) * ETH_ALEN) +
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000679 (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
680 if (!buf) {
681 dev_warn(&dev->dev, "No memory for MAC address buffer\n");
682 return;
683 }
684
Alex Williamson23e258e2009-05-01 17:27:56 +0000685 sg_init_table(sg, 2);
686
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000687 /* Store the unicast list and count in the front of the buffer */
Jiri Pirko31278e72009-06-17 01:12:19 +0000688 mac_data->entries = dev->uc.count;
Jiri Pirkoccffad252009-05-22 23:22:17 +0000689 i = 0;
Jiri Pirko31278e72009-06-17 01:12:19 +0000690 list_for_each_entry(ha, &dev->uc.list, list)
Jiri Pirkoccffad252009-05-22 23:22:17 +0000691 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000692
693 sg_set_buf(&sg[0], mac_data,
Jiri Pirko31278e72009-06-17 01:12:19 +0000694 sizeof(mac_data->entries) + (dev->uc.count * ETH_ALEN));
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000695
696 /* multicast list and count fill the end */
Jiri Pirko31278e72009-06-17 01:12:19 +0000697 mac_data = (void *)&mac_data->macs[dev->uc.count][0];
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000698
699 mac_data->entries = dev->mc_count;
700 addr = dev->mc_list;
701 for (i = 0; i < dev->mc_count; i++, addr = addr->next)
702 memcpy(&mac_data->macs[i][0], addr->da_addr, ETH_ALEN);
703
704 sg_set_buf(&sg[1], mac_data,
705 sizeof(mac_data->entries) + (dev->mc_count * ETH_ALEN));
706
707 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
708 VIRTIO_NET_CTRL_MAC_TABLE_SET,
709 sg, 2, 0))
710 dev_warn(&dev->dev, "Failed to set MAC fitler table.\n");
711
712 kfree(buf);
Alex Williamson2af76982009-02-04 09:02:40 +0000713}
714
Alex Williamson1824a982009-05-01 17:31:10 +0000715static void virtnet_vlan_rx_add_vid(struct net_device *dev, u16 vid)
Alex Williamson0bde95692009-02-04 09:02:50 +0000716{
717 struct virtnet_info *vi = netdev_priv(dev);
718 struct scatterlist sg;
719
Alex Williamson23e258e2009-05-01 17:27:56 +0000720 sg_init_one(&sg, &vid, sizeof(vid));
Alex Williamson0bde95692009-02-04 09:02:50 +0000721
722 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
723 VIRTIO_NET_CTRL_VLAN_ADD, &sg, 1, 0))
724 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
725}
726
Alex Williamson1824a982009-05-01 17:31:10 +0000727static void virtnet_vlan_rx_kill_vid(struct net_device *dev, u16 vid)
Alex Williamson0bde95692009-02-04 09:02:50 +0000728{
729 struct virtnet_info *vi = netdev_priv(dev);
730 struct scatterlist sg;
731
Alex Williamson23e258e2009-05-01 17:27:56 +0000732 sg_init_one(&sg, &vid, sizeof(vid));
Alex Williamson0bde95692009-02-04 09:02:50 +0000733
734 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
735 VIRTIO_NET_CTRL_VLAN_DEL, &sg, 1, 0))
736 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
737}
738
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700739static const struct ethtool_ops virtnet_ethtool_ops = {
Herbert Xua9ea3fc2008-04-18 11:21:42 +0800740 .set_tx_csum = virtnet_set_tx_csum,
741 .set_sg = ethtool_op_set_sg,
Mark McLoughlin0276b492008-11-16 22:40:36 -0800742 .set_tso = ethtool_op_set_tso,
Sridhar Samudrala5c516752009-07-14 14:21:02 +0000743 .set_ufo = ethtool_op_set_ufo,
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -0800744 .get_link = ethtool_op_get_link,
Herbert Xua9ea3fc2008-04-18 11:21:42 +0800745};
746
Mark McLoughlin39da5812008-11-26 13:58:11 +0000747#define MIN_MTU 68
748#define MAX_MTU 65535
749
750static int virtnet_change_mtu(struct net_device *dev, int new_mtu)
751{
752 if (new_mtu < MIN_MTU || new_mtu > MAX_MTU)
753 return -EINVAL;
754 dev->mtu = new_mtu;
755 return 0;
756}
757
Stephen Hemminger76288b42009-01-06 10:44:22 -0800758static const struct net_device_ops virtnet_netdev = {
759 .ndo_open = virtnet_open,
760 .ndo_stop = virtnet_close,
761 .ndo_start_xmit = start_xmit,
762 .ndo_validate_addr = eth_validate_addr,
Alex Williamson9c46f6d2009-02-04 16:36:34 -0800763 .ndo_set_mac_address = virtnet_set_mac_address,
Alex Williamson2af76982009-02-04 09:02:40 +0000764 .ndo_set_rx_mode = virtnet_set_rx_mode,
Stephen Hemminger76288b42009-01-06 10:44:22 -0800765 .ndo_change_mtu = virtnet_change_mtu,
Alex Williamson1824a982009-05-01 17:31:10 +0000766 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
767 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
Stephen Hemminger76288b42009-01-06 10:44:22 -0800768#ifdef CONFIG_NET_POLL_CONTROLLER
769 .ndo_poll_controller = virtnet_netpoll,
770#endif
771};
772
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -0800773static void virtnet_update_status(struct virtnet_info *vi)
774{
775 u16 v;
776
777 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS))
778 return;
779
780 vi->vdev->config->get(vi->vdev,
781 offsetof(struct virtio_net_config, status),
782 &v, sizeof(v));
783
784 /* Ignore unknown (future) status bits */
785 v &= VIRTIO_NET_S_LINK_UP;
786
787 if (vi->status == v)
788 return;
789
790 vi->status = v;
791
792 if (vi->status & VIRTIO_NET_S_LINK_UP) {
793 netif_carrier_on(vi->dev);
794 netif_wake_queue(vi->dev);
795 } else {
796 netif_carrier_off(vi->dev);
797 netif_stop_queue(vi->dev);
798 }
799}
800
801static void virtnet_config_changed(struct virtio_device *vdev)
802{
803 struct virtnet_info *vi = vdev->priv;
804
805 virtnet_update_status(vi);
806}
807
Rusty Russell296f96f2007-10-22 11:03:37 +1000808static int virtnet_probe(struct virtio_device *vdev)
809{
810 int err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000811 struct net_device *dev;
812 struct virtnet_info *vi;
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600813 struct virtqueue *vqs[3];
814 vq_callback_t *callbacks[] = { skb_recv_done, skb_xmit_done, NULL};
815 const char *names[] = { "input", "output", "control" };
816 int nvqs;
Rusty Russell296f96f2007-10-22 11:03:37 +1000817
818 /* Allocate ourselves a network device with room for our info */
819 dev = alloc_etherdev(sizeof(struct virtnet_info));
820 if (!dev)
821 return -ENOMEM;
822
823 /* Set up network device as normal. */
Stephen Hemminger76288b42009-01-06 10:44:22 -0800824 dev->netdev_ops = &virtnet_netdev;
Rusty Russell296f96f2007-10-22 11:03:37 +1000825 dev->features = NETIF_F_HIGHDMA;
Herbert Xua9ea3fc2008-04-18 11:21:42 +0800826 SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops);
Rusty Russell296f96f2007-10-22 11:03:37 +1000827 SET_NETDEV_DEV(dev, &vdev->dev);
828
829 /* Do we support "hardware" checksums? */
Rusty Russellc45a6812008-05-02 21:50:50 -0500830 if (csum && virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000831 /* This opens up the world of extra features. */
832 dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
Rusty Russellc45a6812008-05-02 21:50:50 -0500833 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
Rusty Russell34a48572008-02-04 23:50:02 -0500834 dev->features |= NETIF_F_TSO | NETIF_F_UFO
835 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
836 }
Rusty Russell5539ae92008-05-02 21:50:46 -0500837 /* Individual feature bits: what can host handle? */
Rusty Russellc45a6812008-05-02 21:50:50 -0500838 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
Rusty Russell5539ae92008-05-02 21:50:46 -0500839 dev->features |= NETIF_F_TSO;
Rusty Russellc45a6812008-05-02 21:50:50 -0500840 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
Rusty Russell5539ae92008-05-02 21:50:46 -0500841 dev->features |= NETIF_F_TSO6;
Rusty Russellc45a6812008-05-02 21:50:50 -0500842 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
Rusty Russell5539ae92008-05-02 21:50:46 -0500843 dev->features |= NETIF_F_TSO_ECN;
Rusty Russellc45a6812008-05-02 21:50:50 -0500844 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
Rusty Russell5539ae92008-05-02 21:50:46 -0500845 dev->features |= NETIF_F_UFO;
Rusty Russell296f96f2007-10-22 11:03:37 +1000846 }
847
848 /* Configuration may specify what MAC to use. Otherwise random. */
Rusty Russellc45a6812008-05-02 21:50:50 -0500849 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
Rusty Russella586d4f2008-02-04 23:49:56 -0500850 vdev->config->get(vdev,
851 offsetof(struct virtio_net_config, mac),
852 dev->dev_addr, dev->addr_len);
Alex Williamson62994b22009-04-04 16:40:19 -0700853 } else
Rusty Russell296f96f2007-10-22 11:03:37 +1000854 random_ether_addr(dev->dev_addr);
855
856 /* Set up our device-specific information */
857 vi = netdev_priv(dev);
Dor Laor6c0cd7c2007-12-16 15:19:43 +0200858 netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight);
Rusty Russell296f96f2007-10-22 11:03:37 +1000859 vi->dev = dev;
860 vi->vdev = vdev;
Christian Borntraegerd9d5dcc2008-02-18 10:02:51 +0100861 vdev->priv = vi;
Rusty Russellfb6813f2008-07-25 12:06:01 -0500862 vi->pages = NULL;
Rusty Russell3161e452009-08-26 12:22:32 -0700863 INIT_DELAYED_WORK(&vi->refill, refill_work);
Rusty Russell296f96f2007-10-22 11:03:37 +1000864
Herbert Xu97402b92008-04-18 11:24:27 +0800865 /* If we can receive ANY GSO packets, we must allocate large ones. */
866 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4)
867 || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6)
868 || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN))
869 vi->big_packets = true;
870
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800871 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
872 vi->mergeable_rx_bufs = true;
873
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600874 /* We expect two virtqueues, receive then send,
875 * and optionally control. */
876 nvqs = virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) ? 3 : 2;
Rusty Russell296f96f2007-10-22 11:03:37 +1000877
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600878 err = vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names);
879 if (err)
880 goto free;
881
882 vi->rvq = vqs[0];
883 vi->svq = vqs[1];
Rusty Russell296f96f2007-10-22 11:03:37 +1000884
Alex Williamson2a41f712009-02-04 09:02:34 +0000885 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)) {
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600886 vi->cvq = vqs[2];
Alex Williamson0bde95692009-02-04 09:02:50 +0000887
888 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
889 dev->features |= NETIF_F_HW_VLAN_FILTER;
Alex Williamson2a41f712009-02-04 09:02:34 +0000890 }
891
Rusty Russell296f96f2007-10-22 11:03:37 +1000892 /* Initialize our empty receive and send queues. */
893 skb_queue_head_init(&vi->recv);
894 skb_queue_head_init(&vi->send);
895
896 err = register_netdev(dev);
897 if (err) {
898 pr_debug("virtio_net: registering device failed\n");
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600899 goto free_vqs;
Rusty Russell296f96f2007-10-22 11:03:37 +1000900 }
Rusty Russellb3369c12008-02-04 23:50:02 -0500901
902 /* Last of all, set up some receive buffers. */
Rusty Russell3161e452009-08-26 12:22:32 -0700903 try_fill_recv(vi, GFP_KERNEL);
Rusty Russellb3369c12008-02-04 23:50:02 -0500904
905 /* If we didn't even get one input buffer, we're useless. */
906 if (vi->num == 0) {
907 err = -ENOMEM;
908 goto unregister;
909 }
910
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -0800911 vi->status = VIRTIO_NET_S_LINK_UP;
912 virtnet_update_status(vi);
Pantelis Koukousoulas47832562009-03-18 18:40:02 -0700913 netif_carrier_on(dev);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -0800914
Rusty Russell296f96f2007-10-22 11:03:37 +1000915 pr_debug("virtnet: registered device %s\n", dev->name);
Rusty Russell296f96f2007-10-22 11:03:37 +1000916 return 0;
917
Rusty Russellb3369c12008-02-04 23:50:02 -0500918unregister:
919 unregister_netdev(dev);
Rusty Russell3161e452009-08-26 12:22:32 -0700920 cancel_delayed_work_sync(&vi->refill);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600921free_vqs:
922 vdev->config->del_vqs(vdev);
Rusty Russell296f96f2007-10-22 11:03:37 +1000923free:
924 free_netdev(dev);
925 return err;
926}
927
928static void virtnet_remove(struct virtio_device *vdev)
929{
Rusty Russell74b25532007-11-19 11:20:42 -0500930 struct virtnet_info *vi = vdev->priv;
Rusty Russellb3369c12008-02-04 23:50:02 -0500931 struct sk_buff *skb;
932
Rusty Russell6e5aa7e2008-02-04 23:50:03 -0500933 /* Stop all the virtqueues. */
934 vdev->config->reset(vdev);
935
Rusty Russellb3369c12008-02-04 23:50:02 -0500936 /* Free our skbs in send and recv queues, if any. */
Rusty Russellb3369c12008-02-04 23:50:02 -0500937 while ((skb = __skb_dequeue(&vi->recv)) != NULL) {
938 kfree_skb(skb);
939 vi->num--;
940 }
Wang Chen288369c2008-05-22 18:07:43 +0800941 __skb_queue_purge(&vi->send);
Rusty Russellb3369c12008-02-04 23:50:02 -0500942
943 BUG_ON(vi->num != 0);
Rusty Russell74b25532007-11-19 11:20:42 -0500944
Rusty Russell74b25532007-11-19 11:20:42 -0500945 unregister_netdev(vi->dev);
Rusty Russell3161e452009-08-26 12:22:32 -0700946 cancel_delayed_work_sync(&vi->refill);
Rusty Russellfb6813f2008-07-25 12:06:01 -0500947
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600948 vdev->config->del_vqs(vi->vdev);
949
Rusty Russellfb6813f2008-07-25 12:06:01 -0500950 while (vi->pages)
951 __free_pages(get_a_page(vi, GFP_KERNEL), 0);
952
Rusty Russell74b25532007-11-19 11:20:42 -0500953 free_netdev(vi->dev);
Rusty Russell296f96f2007-10-22 11:03:37 +1000954}
955
956static struct virtio_device_id id_table[] = {
957 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
958 { 0 },
959};
960
Rusty Russellc45a6812008-05-02 21:50:50 -0500961static unsigned int features[] = {
Mark McLoughlin5e4fe5c2008-07-08 17:10:42 +1000962 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM,
963 VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
Rusty Russellc45a6812008-05-02 21:50:50 -0500964 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
Herbert Xu97402b92008-04-18 11:24:27 +0800965 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
Sridhar Samudrala5c516752009-07-14 14:21:02 +0000966 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO,
Alex Williamson2a41f712009-02-04 09:02:34 +0000967 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ,
Alex Williamson0bde95692009-02-04 09:02:50 +0000968 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN,
Rusty Russellc45a6812008-05-02 21:50:50 -0500969};
970
Rusty Russell296f96f2007-10-22 11:03:37 +1000971static struct virtio_driver virtio_net = {
Rusty Russellc45a6812008-05-02 21:50:50 -0500972 .feature_table = features,
973 .feature_table_size = ARRAY_SIZE(features),
Rusty Russell296f96f2007-10-22 11:03:37 +1000974 .driver.name = KBUILD_MODNAME,
975 .driver.owner = THIS_MODULE,
976 .id_table = id_table,
977 .probe = virtnet_probe,
978 .remove = __devexit_p(virtnet_remove),
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -0800979 .config_changed = virtnet_config_changed,
Rusty Russell296f96f2007-10-22 11:03:37 +1000980};
981
982static int __init init(void)
983{
984 return register_virtio_driver(&virtio_net);
985}
986
987static void __exit fini(void)
988{
989 unregister_virtio_driver(&virtio_net);
990}
991module_init(init);
992module_exit(fini);
993
994MODULE_DEVICE_TABLE(virtio, id_table);
995MODULE_DESCRIPTION("Virtio network driver");
996MODULE_LICENSE("GPL");