blob: 32266fb89c2022204c4b7fd396b4139c53f69f0d [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>
25#include <linux/virtio_net.h>
26#include <linux/scatterlist.h>
Alex Williamsone918085a2009-01-25 18:06:26 -080027#include <linux/if_vlan.h>
Rusty Russell296f96f2007-10-22 11:03:37 +100028
Dor Laor6c0cd7c2007-12-16 15:19:43 +020029static int napi_weight = 128;
30module_param(napi_weight, int, 0444);
31
Rusty Russell34a48572008-02-04 23:50:02 -050032static int csum = 1, gso = 1;
33module_param(csum, bool, 0444);
34module_param(gso, bool, 0444);
35
Rusty Russell296f96f2007-10-22 11:03:37 +100036/* FIXME: MTU in config. */
Alex Williamsone918085a2009-01-25 18:06:26 -080037#define MAX_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -080038#define GOOD_COPY_LEN 128
Rusty Russell296f96f2007-10-22 11:03:37 +100039
Alex Williamsonf565a7c2009-02-04 09:02:45 +000040#define VIRTNET_SEND_COMMAND_SG_MAX 2
Alex Williamson2a41f712009-02-04 09:02:34 +000041
Rusty Russell296f96f2007-10-22 11:03:37 +100042struct virtnet_info
43{
44 struct virtio_device *vdev;
Alex Williamson2a41f712009-02-04 09:02:34 +000045 struct virtqueue *rvq, *svq, *cvq;
Rusty Russell296f96f2007-10-22 11:03:37 +100046 struct net_device *dev;
47 struct napi_struct napi;
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -080048 unsigned int status;
Rusty Russell296f96f2007-10-22 11:03:37 +100049
Rusty Russell99ffc692008-05-02 21:50:46 -050050 /* The skb we couldn't send because buffers were full. */
51 struct sk_buff *last_xmit_skb;
52
Rusty Russell363f1512008-06-08 20:51:55 +100053 /* If we need to free in a timer, this is it. */
Mark McLoughlin14c998f2008-06-08 20:50:56 +100054 struct timer_list xmit_free_timer;
55
Rusty Russell296f96f2007-10-22 11:03:37 +100056 /* Number of input buffers, and max we've ever had. */
57 unsigned int num, max;
58
Rusty Russell11a3a152008-05-26 17:48:13 +100059 /* For cleaning up after transmission. */
60 struct tasklet_struct tasklet;
Rusty Russell363f1512008-06-08 20:51:55 +100061 bool free_in_tasklet;
Rusty Russell11a3a152008-05-26 17:48:13 +100062
Herbert Xu97402b92008-04-18 11:24:27 +080063 /* I like... big packets and I cannot lie! */
64 bool big_packets;
65
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -080066 /* Host will merge rx buffers for big packets (shake it! shake it!) */
67 bool mergeable_rx_bufs;
68
Rusty Russell296f96f2007-10-22 11:03:37 +100069 /* Receive & send queues. */
70 struct sk_buff_head recv;
71 struct sk_buff_head send;
Rusty Russellfb6813f2008-07-25 12:06:01 -050072
Rusty Russell3161e452009-08-26 12:22:32 -070073 /* Work struct for refilling if we run low on memory. */
74 struct delayed_work refill;
75
Rusty Russellfb6813f2008-07-25 12:06:01 -050076 /* Chain pages by the private ptr. */
77 struct page *pages;
Rusty Russell296f96f2007-10-22 11:03:37 +100078};
79
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -080080static inline void *skb_vnet_hdr(struct sk_buff *skb)
Rusty Russell296f96f2007-10-22 11:03:37 +100081{
82 return (struct virtio_net_hdr *)skb->cb;
83}
84
Rusty Russellfb6813f2008-07-25 12:06:01 -050085static void give_a_page(struct virtnet_info *vi, struct page *page)
86{
87 page->private = (unsigned long)vi->pages;
88 vi->pages = page;
89}
90
Mark McLoughlin0a888fd2008-11-16 22:39:18 -080091static void trim_pages(struct virtnet_info *vi, struct sk_buff *skb)
92{
93 unsigned int i;
94
95 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
96 give_a_page(vi, skb_shinfo(skb)->frags[i].page);
97 skb_shinfo(skb)->nr_frags = 0;
98 skb->data_len = 0;
99}
100
Rusty Russellfb6813f2008-07-25 12:06:01 -0500101static struct page *get_a_page(struct virtnet_info *vi, gfp_t gfp_mask)
102{
103 struct page *p = vi->pages;
104
105 if (p)
106 vi->pages = (struct page *)p->private;
107 else
108 p = alloc_page(gfp_mask);
109 return p;
110}
111
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500112static void skb_xmit_done(struct virtqueue *svq)
Rusty Russell296f96f2007-10-22 11:03:37 +1000113{
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500114 struct virtnet_info *vi = svq->vdev->priv;
Rusty Russell296f96f2007-10-22 11:03:37 +1000115
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500116 /* Suppress further interrupts. */
117 svq->vq_ops->disable_cb(svq);
Rusty Russell11a3a152008-05-26 17:48:13 +1000118
Rusty Russell363f1512008-06-08 20:51:55 +1000119 /* We were probably waiting for more output buffers. */
Rusty Russell296f96f2007-10-22 11:03:37 +1000120 netif_wake_queue(vi->dev);
Rusty Russell11a3a152008-05-26 17:48:13 +1000121
122 /* Make sure we re-xmit last_xmit_skb: if there are no more packets
123 * queued, start_xmit won't be called. */
124 tasklet_schedule(&vi->tasklet);
Rusty Russell296f96f2007-10-22 11:03:37 +1000125}
126
127static void receive_skb(struct net_device *dev, struct sk_buff *skb,
128 unsigned len)
129{
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800130 struct virtnet_info *vi = netdev_priv(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +1000131 struct virtio_net_hdr *hdr = skb_vnet_hdr(skb);
Herbert Xu97402b92008-04-18 11:24:27 +0800132 int err;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800133 int i;
Rusty Russell296f96f2007-10-22 11:03:37 +1000134
135 if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) {
136 pr_debug("%s: short packet %i\n", dev->name, len);
137 dev->stats.rx_length_errors++;
138 goto drop;
139 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000140
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800141 if (vi->mergeable_rx_bufs) {
142 struct virtio_net_hdr_mrg_rxbuf *mhdr = skb_vnet_hdr(skb);
143 unsigned int copy;
144 char *p = page_address(skb_shinfo(skb)->frags[0].page);
Rusty Russellfb6813f2008-07-25 12:06:01 -0500145
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800146 if (len > PAGE_SIZE)
147 len = PAGE_SIZE;
148 len -= sizeof(struct virtio_net_hdr_mrg_rxbuf);
149
150 memcpy(hdr, p, sizeof(*mhdr));
151 p += sizeof(*mhdr);
152
153 copy = len;
154 if (copy > skb_tailroom(skb))
155 copy = skb_tailroom(skb);
156
157 memcpy(skb_put(skb, copy), p, copy);
158
159 len -= copy;
160
161 if (!len) {
162 give_a_page(vi, skb_shinfo(skb)->frags[0].page);
163 skb_shinfo(skb)->nr_frags--;
164 } else {
165 skb_shinfo(skb)->frags[0].page_offset +=
166 sizeof(*mhdr) + copy;
167 skb_shinfo(skb)->frags[0].size = len;
168 skb->data_len += len;
169 skb->len += len;
170 }
171
172 while (--mhdr->num_buffers) {
173 struct sk_buff *nskb;
174
175 i = skb_shinfo(skb)->nr_frags;
176 if (i >= MAX_SKB_FRAGS) {
177 pr_debug("%s: packet too long %d\n", dev->name,
178 len);
179 dev->stats.rx_length_errors++;
180 goto drop;
181 }
182
183 nskb = vi->rvq->vq_ops->get_buf(vi->rvq, &len);
184 if (!nskb) {
185 pr_debug("%s: rx error: %d buffers missing\n",
186 dev->name, mhdr->num_buffers);
187 dev->stats.rx_length_errors++;
188 goto drop;
189 }
190
191 __skb_unlink(nskb, &vi->recv);
192 vi->num--;
193
194 skb_shinfo(skb)->frags[i] = skb_shinfo(nskb)->frags[0];
195 skb_shinfo(nskb)->nr_frags = 0;
196 kfree_skb(nskb);
197
198 if (len > PAGE_SIZE)
199 len = PAGE_SIZE;
200
201 skb_shinfo(skb)->frags[i].size = len;
202 skb_shinfo(skb)->nr_frags++;
203 skb->data_len += len;
204 skb->len += len;
205 }
206 } else {
207 len -= sizeof(struct virtio_net_hdr);
208
209 if (len <= MAX_PACKET_LEN)
210 trim_pages(vi, skb);
211
212 err = pskb_trim(skb, len);
213 if (err) {
214 pr_debug("%s: pskb_trim failed %i %d\n", dev->name,
215 len, err);
216 dev->stats.rx_dropped++;
217 goto drop;
218 }
Herbert Xu97402b92008-04-18 11:24:27 +0800219 }
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800220
Herbert Xu97402b92008-04-18 11:24:27 +0800221 skb->truesize += skb->data_len;
Rusty Russell296f96f2007-10-22 11:03:37 +1000222 dev->stats.rx_bytes += skb->len;
223 dev->stats.rx_packets++;
224
225 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
226 pr_debug("Needs csum!\n");
Rusty Russellf35d9d82008-02-04 23:49:54 -0500227 if (!skb_partial_csum_set(skb,hdr->csum_start,hdr->csum_offset))
Rusty Russell296f96f2007-10-22 11:03:37 +1000228 goto frame_err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000229 }
230
Mark McLoughlin23cde762008-06-08 20:49:00 +1000231 skb->protocol = eth_type_trans(skb, dev);
232 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
233 ntohs(skb->protocol), skb->len, skb->pkt_type);
234
Rusty Russell296f96f2007-10-22 11:03:37 +1000235 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
236 pr_debug("GSO!\n");
Rusty Russell34a48572008-02-04 23:50:02 -0500237 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000238 case VIRTIO_NET_HDR_GSO_TCPV4:
239 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
240 break;
Rusty Russell296f96f2007-10-22 11:03:37 +1000241 case VIRTIO_NET_HDR_GSO_UDP:
242 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
243 break;
244 case VIRTIO_NET_HDR_GSO_TCPV6:
245 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
246 break;
247 default:
248 if (net_ratelimit())
249 printk(KERN_WARNING "%s: bad gso type %u.\n",
250 dev->name, hdr->gso_type);
251 goto frame_err;
252 }
253
Rusty Russell34a48572008-02-04 23:50:02 -0500254 if (hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN)
255 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
256
Rusty Russell296f96f2007-10-22 11:03:37 +1000257 skb_shinfo(skb)->gso_size = hdr->gso_size;
258 if (skb_shinfo(skb)->gso_size == 0) {
259 if (net_ratelimit())
260 printk(KERN_WARNING "%s: zero gso size.\n",
261 dev->name);
262 goto frame_err;
263 }
264
265 /* Header must be checked, and gso_segs computed. */
266 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
267 skb_shinfo(skb)->gso_segs = 0;
268 }
269
270 netif_receive_skb(skb);
271 return;
272
273frame_err:
274 dev->stats.rx_frame_errors++;
275drop:
276 dev_kfree_skb(skb);
277}
278
Rusty Russell3161e452009-08-26 12:22:32 -0700279static bool try_fill_recv_maxbufs(struct virtnet_info *vi, gfp_t gfp)
Rusty Russell296f96f2007-10-22 11:03:37 +1000280{
281 struct sk_buff *skb;
Rusty Russell05271682008-05-02 21:50:45 -0500282 struct scatterlist sg[2+MAX_SKB_FRAGS];
Herbert Xu97402b92008-04-18 11:24:27 +0800283 int num, err, i;
Rusty Russell3161e452009-08-26 12:22:32 -0700284 bool oom = false;
Rusty Russell296f96f2007-10-22 11:03:37 +1000285
Rusty Russell05271682008-05-02 21:50:45 -0500286 sg_init_table(sg, 2+MAX_SKB_FRAGS);
Rusty Russell296f96f2007-10-22 11:03:37 +1000287 for (;;) {
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800288 struct virtio_net_hdr *hdr;
289
Herbert Xu8981f012009-06-11 20:55:17 -0700290 skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN + NET_IP_ALIGN);
Rusty Russell3161e452009-08-26 12:22:32 -0700291 if (unlikely(!skb)) {
292 oom = true;
Rusty Russell296f96f2007-10-22 11:03:37 +1000293 break;
Rusty Russell3161e452009-08-26 12:22:32 -0700294 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000295
Herbert Xu8981f012009-06-11 20:55:17 -0700296 skb_reserve(skb, NET_IP_ALIGN);
Rusty Russell296f96f2007-10-22 11:03:37 +1000297 skb_put(skb, MAX_PACKET_LEN);
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800298
299 hdr = skb_vnet_hdr(skb);
Ira W. Snyder8527bec2009-01-26 21:00:33 -0800300 sg_set_buf(sg, hdr, sizeof(*hdr));
Herbert Xu97402b92008-04-18 11:24:27 +0800301
302 if (vi->big_packets) {
303 for (i = 0; i < MAX_SKB_FRAGS; i++) {
304 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
Rusty Russell3161e452009-08-26 12:22:32 -0700305 f->page = get_a_page(vi, gfp);
Herbert Xu97402b92008-04-18 11:24:27 +0800306 if (!f->page)
307 break;
308
309 f->page_offset = 0;
310 f->size = PAGE_SIZE;
311
312 skb->data_len += PAGE_SIZE;
313 skb->len += PAGE_SIZE;
314
315 skb_shinfo(skb)->nr_frags++;
316 }
317 }
318
Rusty Russell296f96f2007-10-22 11:03:37 +1000319 num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
320 skb_queue_head(&vi->recv, skb);
321
322 err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, num, skb);
323 if (err) {
324 skb_unlink(skb, &vi->recv);
Mark McLoughlin0a888fd2008-11-16 22:39:18 -0800325 trim_pages(vi, skb);
Rusty Russell296f96f2007-10-22 11:03:37 +1000326 kfree_skb(skb);
327 break;
328 }
329 vi->num++;
330 }
331 if (unlikely(vi->num > vi->max))
332 vi->max = vi->num;
333 vi->rvq->vq_ops->kick(vi->rvq);
Rusty Russell3161e452009-08-26 12:22:32 -0700334 return !oom;
Rusty Russell296f96f2007-10-22 11:03:37 +1000335}
336
Rusty Russell3161e452009-08-26 12:22:32 -0700337/* Returns false if we couldn't fill entirely (OOM). */
338static bool try_fill_recv(struct virtnet_info *vi, gfp_t gfp)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800339{
340 struct sk_buff *skb;
341 struct scatterlist sg[1];
342 int err;
Rusty Russell3161e452009-08-26 12:22:32 -0700343 bool oom = false;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800344
Rusty Russell3161e452009-08-26 12:22:32 -0700345 if (!vi->mergeable_rx_bufs)
346 return try_fill_recv_maxbufs(vi, gfp);
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800347
348 for (;;) {
349 skb_frag_t *f;
350
351 skb = netdev_alloc_skb(vi->dev, GOOD_COPY_LEN + NET_IP_ALIGN);
Rusty Russell3161e452009-08-26 12:22:32 -0700352 if (unlikely(!skb)) {
353 oom = true;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800354 break;
Rusty Russell3161e452009-08-26 12:22:32 -0700355 }
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800356
357 skb_reserve(skb, NET_IP_ALIGN);
358
359 f = &skb_shinfo(skb)->frags[0];
Rusty Russell3161e452009-08-26 12:22:32 -0700360 f->page = get_a_page(vi, gfp);
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800361 if (!f->page) {
Rusty Russell3161e452009-08-26 12:22:32 -0700362 oom = true;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800363 kfree_skb(skb);
364 break;
365 }
366
367 f->page_offset = 0;
368 f->size = PAGE_SIZE;
369
370 skb_shinfo(skb)->nr_frags++;
371
372 sg_init_one(sg, page_address(f->page), PAGE_SIZE);
373 skb_queue_head(&vi->recv, skb);
374
375 err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, 1, skb);
376 if (err) {
377 skb_unlink(skb, &vi->recv);
378 kfree_skb(skb);
379 break;
380 }
381 vi->num++;
382 }
383 if (unlikely(vi->num > vi->max))
384 vi->max = vi->num;
385 vi->rvq->vq_ops->kick(vi->rvq);
Rusty Russell3161e452009-08-26 12:22:32 -0700386 return !oom;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800387}
388
Rusty Russell18445c42008-02-04 23:49:57 -0500389static void skb_recv_done(struct virtqueue *rvq)
Rusty Russell296f96f2007-10-22 11:03:37 +1000390{
391 struct virtnet_info *vi = rvq->vdev->priv;
Rusty Russell18445c42008-02-04 23:49:57 -0500392 /* Schedule NAPI, Suppress further interrupts if successful. */
Ben Hutchings288379f2009-01-19 16:43:59 -0800393 if (napi_schedule_prep(&vi->napi)) {
Rusty Russell18445c42008-02-04 23:49:57 -0500394 rvq->vq_ops->disable_cb(rvq);
Ben Hutchings288379f2009-01-19 16:43:59 -0800395 __napi_schedule(&vi->napi);
Rusty Russell18445c42008-02-04 23:49:57 -0500396 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000397}
398
Rusty Russell3161e452009-08-26 12:22:32 -0700399static void refill_work(struct work_struct *work)
400{
401 struct virtnet_info *vi;
402 bool still_empty;
403
404 vi = container_of(work, struct virtnet_info, refill.work);
405 napi_disable(&vi->napi);
406 try_fill_recv(vi, GFP_KERNEL);
407 still_empty = (vi->num == 0);
408 napi_enable(&vi->napi);
409
410 /* In theory, this can happen: if we don't get any buffers in
411 * we will *never* try to fill again. */
412 if (still_empty)
413 schedule_delayed_work(&vi->refill, HZ/2);
414}
415
Rusty Russell296f96f2007-10-22 11:03:37 +1000416static int virtnet_poll(struct napi_struct *napi, int budget)
417{
418 struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi);
419 struct sk_buff *skb = NULL;
420 unsigned int len, received = 0;
421
422again:
423 while (received < budget &&
424 (skb = vi->rvq->vq_ops->get_buf(vi->rvq, &len)) != NULL) {
425 __skb_unlink(skb, &vi->recv);
426 receive_skb(vi->dev, skb, len);
427 vi->num--;
428 received++;
429 }
430
Rusty Russell3161e452009-08-26 12:22:32 -0700431 if (vi->num < vi->max / 2) {
432 if (!try_fill_recv(vi, GFP_ATOMIC))
433 schedule_delayed_work(&vi->refill, 0);
434 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000435
Rusty Russell8329d982007-11-19 11:20:43 -0500436 /* Out of packets? */
437 if (received < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -0800438 napi_complete(napi);
Rusty Russell18445c42008-02-04 23:49:57 -0500439 if (unlikely(!vi->rvq->vq_ops->enable_cb(vi->rvq))
Christian Borntraeger4265f162008-03-14 14:17:05 +0100440 && napi_schedule_prep(napi)) {
441 vi->rvq->vq_ops->disable_cb(vi->rvq);
Ben Hutchings288379f2009-01-19 16:43:59 -0800442 __napi_schedule(napi);
Rusty Russell296f96f2007-10-22 11:03:37 +1000443 goto again;
Christian Borntraeger4265f162008-03-14 14:17:05 +0100444 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000445 }
446
447 return received;
448}
449
450static void free_old_xmit_skbs(struct virtnet_info *vi)
451{
452 struct sk_buff *skb;
453 unsigned int len;
454
455 while ((skb = vi->svq->vq_ops->get_buf(vi->svq, &len)) != NULL) {
456 pr_debug("Sent skb %p\n", skb);
457 __skb_unlink(skb, &vi->send);
Rusty Russell655aa312008-05-02 21:50:43 -0500458 vi->dev->stats.tx_bytes += skb->len;
Rusty Russell296f96f2007-10-22 11:03:37 +1000459 vi->dev->stats.tx_packets++;
460 kfree_skb(skb);
461 }
462}
463
Rusty Russell363f1512008-06-08 20:51:55 +1000464/* If the virtio transport doesn't always notify us when all in-flight packets
465 * are consumed, we fall back to using this function on a timer to free them. */
Mark McLoughlin14c998f2008-06-08 20:50:56 +1000466static void xmit_free(unsigned long data)
467{
468 struct virtnet_info *vi = (void *)data;
469
470 netif_tx_lock(vi->dev);
471
472 free_old_xmit_skbs(vi);
473
474 if (!skb_queue_empty(&vi->send))
475 mod_timer(&vi->xmit_free_timer, jiffies + (HZ/10));
476
477 netif_tx_unlock(vi->dev);
478}
479
Rusty Russell99ffc692008-05-02 21:50:46 -0500480static int xmit_skb(struct virtnet_info *vi, struct sk_buff *skb)
Rusty Russell296f96f2007-10-22 11:03:37 +1000481{
Mark McLoughlin14c998f2008-06-08 20:50:56 +1000482 int num, err;
Rusty Russell05271682008-05-02 21:50:45 -0500483 struct scatterlist sg[2+MAX_SKB_FRAGS];
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800484 struct virtio_net_hdr_mrg_rxbuf *mhdr = skb_vnet_hdr(skb);
485 struct virtio_net_hdr *hdr = skb_vnet_hdr(skb);
Rusty Russell296f96f2007-10-22 11:03:37 +1000486 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
Rusty Russell296f96f2007-10-22 11:03:37 +1000487
Rusty Russell05271682008-05-02 21:50:45 -0500488 sg_init_table(sg, 2+MAX_SKB_FRAGS);
Rusty Russell4d125de2007-11-07 16:34:49 +1100489
Johannes Berge1749612008-10-27 15:59:26 -0700490 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
Rusty Russell296f96f2007-10-22 11:03:37 +1000491
Rusty Russell296f96f2007-10-22 11:03:37 +1000492 if (skb->ip_summed == CHECKSUM_PARTIAL) {
493 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
494 hdr->csum_start = skb->csum_start - skb_headroom(skb);
495 hdr->csum_offset = skb->csum_offset;
496 } else {
497 hdr->flags = 0;
498 hdr->csum_offset = hdr->csum_start = 0;
499 }
500
501 if (skb_is_gso(skb)) {
Herbert Xub82f08e2009-06-04 00:59:18 +0000502 hdr->hdr_len = skb_headlen(skb);
Rusty Russell296f96f2007-10-22 11:03:37 +1000503 hdr->gso_size = skb_shinfo(skb)->gso_size;
Rusty Russell34a48572008-02-04 23:50:02 -0500504 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
Rusty Russell296f96f2007-10-22 11:03:37 +1000505 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
506 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
507 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
508 else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
509 hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
510 else
511 BUG();
Rusty Russell34a48572008-02-04 23:50:02 -0500512 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
513 hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
Rusty Russell296f96f2007-10-22 11:03:37 +1000514 } else {
515 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
Rusty Russell50c8ea82008-02-04 23:50:01 -0500516 hdr->gso_size = hdr->hdr_len = 0;
Rusty Russell296f96f2007-10-22 11:03:37 +1000517 }
518
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800519 mhdr->num_buffers = 0;
520
521 /* Encode metadata header at front. */
522 if (vi->mergeable_rx_bufs)
Ira W. Snyder8527bec2009-01-26 21:00:33 -0800523 sg_set_buf(sg, mhdr, sizeof(*mhdr));
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800524 else
Ira W. Snyder8527bec2009-01-26 21:00:33 -0800525 sg_set_buf(sg, hdr, sizeof(*hdr));
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800526
Rusty Russell296f96f2007-10-22 11:03:37 +1000527 num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
Rusty Russell99ffc692008-05-02 21:50:46 -0500528
Mark McLoughlin14c998f2008-06-08 20:50:56 +1000529 err = vi->svq->vq_ops->add_buf(vi->svq, sg, num, 0, skb);
Rusty Russell363f1512008-06-08 20:51:55 +1000530 if (!err && !vi->free_in_tasklet)
Mark McLoughlin14c998f2008-06-08 20:50:56 +1000531 mod_timer(&vi->xmit_free_timer, jiffies + (HZ/10));
532
533 return err;
Rusty Russell99ffc692008-05-02 21:50:46 -0500534}
535
Rusty Russell11a3a152008-05-26 17:48:13 +1000536static void xmit_tasklet(unsigned long data)
537{
538 struct virtnet_info *vi = (void *)data;
539
540 netif_tx_lock_bh(vi->dev);
541 if (vi->last_xmit_skb && xmit_skb(vi, vi->last_xmit_skb) == 0) {
542 vi->svq->vq_ops->kick(vi->svq);
543 vi->last_xmit_skb = NULL;
544 }
Rusty Russell363f1512008-06-08 20:51:55 +1000545 if (vi->free_in_tasklet)
546 free_old_xmit_skbs(vi);
Rusty Russell11a3a152008-05-26 17:48:13 +1000547 netif_tx_unlock_bh(vi->dev);
548}
549
Stephen Hemminger424efe92009-08-31 19:50:51 +0000550static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
Rusty Russell99ffc692008-05-02 21:50:46 -0500551{
552 struct virtnet_info *vi = netdev_priv(dev);
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500553
554again:
555 /* Free up any pending old buffers before queueing new ones. */
556 free_old_xmit_skbs(vi);
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500557
Rusty Russell99ffc692008-05-02 21:50:46 -0500558 /* If we has a buffer left over from last time, send it now. */
Mark McLoughlin9953ca62008-05-27 12:06:26 +0100559 if (unlikely(vi->last_xmit_skb) &&
560 xmit_skb(vi, vi->last_xmit_skb) != 0)
561 goto stop_queue;
562
563 vi->last_xmit_skb = NULL;
Rusty Russell296f96f2007-10-22 11:03:37 +1000564
Rusty Russell99ffc692008-05-02 21:50:46 -0500565 /* Put new one in send queue and do transmit */
Rusty Russell7eb2e252008-05-26 17:42:42 +1000566 if (likely(skb)) {
567 __skb_queue_head(&vi->send, skb);
568 if (xmit_skb(vi, skb) != 0) {
569 vi->last_xmit_skb = skb;
570 skb = NULL;
571 goto stop_queue;
572 }
Rusty Russell99ffc692008-05-02 21:50:46 -0500573 }
574done:
575 vi->svq->vq_ops->kick(vi->svq);
576 return NETDEV_TX_OK;
577
578stop_queue:
579 pr_debug("%s: virtio not prepared to send\n", dev->name);
580 netif_stop_queue(dev);
581
582 /* Activate callback for using skbs: if this returns false it
583 * means some were used in the meantime. */
584 if (unlikely(!vi->svq->vq_ops->enable_cb(vi->svq))) {
585 vi->svq->vq_ops->disable_cb(vi->svq);
586 netif_start_queue(dev);
587 goto again;
588 }
Mark McLoughlin9953ca62008-05-27 12:06:26 +0100589 if (skb) {
590 /* Drop this skb: we only queue one. */
591 vi->dev->stats.tx_dropped++;
592 kfree_skb(skb);
593 }
Rusty Russell99ffc692008-05-02 21:50:46 -0500594 goto done;
Rusty Russell296f96f2007-10-22 11:03:37 +1000595}
596
Alex Williamson9c46f6d2009-02-04 16:36:34 -0800597static int virtnet_set_mac_address(struct net_device *dev, void *p)
598{
599 struct virtnet_info *vi = netdev_priv(dev);
600 struct virtio_device *vdev = vi->vdev;
601 int ret;
602
603 ret = eth_mac_addr(dev, p);
604 if (ret)
605 return ret;
606
Alex Williamson62994b22009-04-04 16:40:19 -0700607 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
608 vdev->config->set(vdev, offsetof(struct virtio_net_config, mac),
609 dev->dev_addr, dev->addr_len);
Alex Williamson9c46f6d2009-02-04 16:36:34 -0800610
611 return 0;
612}
613
Amit Shahda74e892008-02-29 16:24:50 +0530614#ifdef CONFIG_NET_POLL_CONTROLLER
615static void virtnet_netpoll(struct net_device *dev)
616{
617 struct virtnet_info *vi = netdev_priv(dev);
618
619 napi_schedule(&vi->napi);
620}
621#endif
622
Rusty Russell296f96f2007-10-22 11:03:37 +1000623static int virtnet_open(struct net_device *dev)
624{
625 struct virtnet_info *vi = netdev_priv(dev);
626
Rusty Russell296f96f2007-10-22 11:03:37 +1000627 napi_enable(&vi->napi);
Rusty Russella48bd8f2008-02-04 23:50:07 -0500628
629 /* If all buffers were filled by other side before we napi_enabled, we
630 * won't get another interrupt, so process any outstanding packets
Christian Borntraeger370076d2008-02-06 08:50:11 +0100631 * now. virtnet_poll wants re-enable the queue, so we disable here.
632 * We synchronize against interrupts via NAPI_STATE_SCHED */
Ben Hutchings288379f2009-01-19 16:43:59 -0800633 if (napi_schedule_prep(&vi->napi)) {
Christian Borntraeger370076d2008-02-06 08:50:11 +0100634 vi->rvq->vq_ops->disable_cb(vi->rvq);
Ben Hutchings288379f2009-01-19 16:43:59 -0800635 __napi_schedule(&vi->napi);
Christian Borntraeger370076d2008-02-06 08:50:11 +0100636 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000637 return 0;
638}
639
Alex Williamson2a41f712009-02-04 09:02:34 +0000640/*
641 * Send command via the control virtqueue and check status. Commands
642 * supported by the hypervisor, as indicated by feature bits, should
643 * never fail unless improperly formated.
644 */
645static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
646 struct scatterlist *data, int out, int in)
647{
Alex Williamson23e258e2009-05-01 17:27:56 +0000648 struct scatterlist *s, sg[VIRTNET_SEND_COMMAND_SG_MAX + 2];
Alex Williamson2a41f712009-02-04 09:02:34 +0000649 struct virtio_net_ctrl_hdr ctrl;
650 virtio_net_ctrl_ack status = ~0;
651 unsigned int tmp;
Alex Williamson23e258e2009-05-01 17:27:56 +0000652 int i;
Alex Williamson2a41f712009-02-04 09:02:34 +0000653
Alexander Beregalov0ee904c2009-04-11 14:50:23 +0000654 /* Caller should know better */
655 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) ||
656 (out + in > VIRTNET_SEND_COMMAND_SG_MAX));
Alex Williamson2a41f712009-02-04 09:02:34 +0000657
658 out++; /* Add header */
659 in++; /* Add return status */
660
661 ctrl.class = class;
662 ctrl.cmd = cmd;
663
664 sg_init_table(sg, out + in);
665
666 sg_set_buf(&sg[0], &ctrl, sizeof(ctrl));
Alex Williamson23e258e2009-05-01 17:27:56 +0000667 for_each_sg(data, s, out + in - 2, i)
668 sg_set_buf(&sg[i + 1], sg_virt(s), s->length);
Alex Williamson2a41f712009-02-04 09:02:34 +0000669 sg_set_buf(&sg[out + in - 1], &status, sizeof(status));
670
Alexander Beregalov0ee904c2009-04-11 14:50:23 +0000671 BUG_ON(vi->cvq->vq_ops->add_buf(vi->cvq, sg, out, in, vi));
Alex Williamson2a41f712009-02-04 09:02:34 +0000672
673 vi->cvq->vq_ops->kick(vi->cvq);
674
675 /*
676 * Spin for a response, the kick causes an ioport write, trapping
677 * into the hypervisor, so the request should be handled immediately.
678 */
679 while (!vi->cvq->vq_ops->get_buf(vi->cvq, &tmp))
680 cpu_relax();
681
682 return status == VIRTIO_NET_OK;
683}
684
Rusty Russell296f96f2007-10-22 11:03:37 +1000685static int virtnet_close(struct net_device *dev)
686{
687 struct virtnet_info *vi = netdev_priv(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +1000688
689 napi_disable(&vi->napi);
690
Rusty Russell296f96f2007-10-22 11:03:37 +1000691 return 0;
692}
693
Herbert Xua9ea3fc2008-04-18 11:21:42 +0800694static int virtnet_set_tx_csum(struct net_device *dev, u32 data)
695{
696 struct virtnet_info *vi = netdev_priv(dev);
697 struct virtio_device *vdev = vi->vdev;
698
699 if (data && !virtio_has_feature(vdev, VIRTIO_NET_F_CSUM))
700 return -ENOSYS;
701
702 return ethtool_op_set_tx_hw_csum(dev, data);
703}
704
Alex Williamson2af76982009-02-04 09:02:40 +0000705static void virtnet_set_rx_mode(struct net_device *dev)
706{
707 struct virtnet_info *vi = netdev_priv(dev);
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000708 struct scatterlist sg[2];
Alex Williamson2af76982009-02-04 09:02:40 +0000709 u8 promisc, allmulti;
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000710 struct virtio_net_ctrl_mac *mac_data;
711 struct dev_addr_list *addr;
Jiri Pirkoccffad22009-05-22 23:22:17 +0000712 struct netdev_hw_addr *ha;
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000713 void *buf;
714 int i;
Alex Williamson2af76982009-02-04 09:02:40 +0000715
716 /* We can't dynamicaly set ndo_set_rx_mode, so return gracefully */
717 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
718 return;
719
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000720 promisc = ((dev->flags & IFF_PROMISC) != 0);
721 allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
Alex Williamson2af76982009-02-04 09:02:40 +0000722
Alex Williamson23e258e2009-05-01 17:27:56 +0000723 sg_init_one(sg, &promisc, sizeof(promisc));
Alex Williamson2af76982009-02-04 09:02:40 +0000724
725 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
726 VIRTIO_NET_CTRL_RX_PROMISC,
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000727 sg, 1, 0))
Alex Williamson2af76982009-02-04 09:02:40 +0000728 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
729 promisc ? "en" : "dis");
730
Alex Williamson23e258e2009-05-01 17:27:56 +0000731 sg_init_one(sg, &allmulti, sizeof(allmulti));
Alex Williamson2af76982009-02-04 09:02:40 +0000732
733 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
734 VIRTIO_NET_CTRL_RX_ALLMULTI,
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000735 sg, 1, 0))
Alex Williamson2af76982009-02-04 09:02:40 +0000736 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
737 allmulti ? "en" : "dis");
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000738
739 /* MAC filter - use one buffer for both lists */
Jiri Pirko31278e72009-06-17 01:12:19 +0000740 mac_data = buf = kzalloc(((dev->uc.count + dev->mc_count) * ETH_ALEN) +
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000741 (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
742 if (!buf) {
743 dev_warn(&dev->dev, "No memory for MAC address buffer\n");
744 return;
745 }
746
Alex Williamson23e258e2009-05-01 17:27:56 +0000747 sg_init_table(sg, 2);
748
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000749 /* Store the unicast list and count in the front of the buffer */
Jiri Pirko31278e72009-06-17 01:12:19 +0000750 mac_data->entries = dev->uc.count;
Jiri Pirkoccffad22009-05-22 23:22:17 +0000751 i = 0;
Jiri Pirko31278e72009-06-17 01:12:19 +0000752 list_for_each_entry(ha, &dev->uc.list, list)
Jiri Pirkoccffad22009-05-22 23:22:17 +0000753 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000754
755 sg_set_buf(&sg[0], mac_data,
Jiri Pirko31278e72009-06-17 01:12:19 +0000756 sizeof(mac_data->entries) + (dev->uc.count * ETH_ALEN));
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000757
758 /* multicast list and count fill the end */
Jiri Pirko31278e72009-06-17 01:12:19 +0000759 mac_data = (void *)&mac_data->macs[dev->uc.count][0];
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000760
761 mac_data->entries = dev->mc_count;
762 addr = dev->mc_list;
763 for (i = 0; i < dev->mc_count; i++, addr = addr->next)
764 memcpy(&mac_data->macs[i][0], addr->da_addr, ETH_ALEN);
765
766 sg_set_buf(&sg[1], mac_data,
767 sizeof(mac_data->entries) + (dev->mc_count * ETH_ALEN));
768
769 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
770 VIRTIO_NET_CTRL_MAC_TABLE_SET,
771 sg, 2, 0))
772 dev_warn(&dev->dev, "Failed to set MAC fitler table.\n");
773
774 kfree(buf);
Alex Williamson2af76982009-02-04 09:02:40 +0000775}
776
Alex Williamson1824a982009-05-01 17:31:10 +0000777static void virtnet_vlan_rx_add_vid(struct net_device *dev, u16 vid)
Alex Williamson0bde95692009-02-04 09:02:50 +0000778{
779 struct virtnet_info *vi = netdev_priv(dev);
780 struct scatterlist sg;
781
Alex Williamson23e258e2009-05-01 17:27:56 +0000782 sg_init_one(&sg, &vid, sizeof(vid));
Alex Williamson0bde95692009-02-04 09:02:50 +0000783
784 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
785 VIRTIO_NET_CTRL_VLAN_ADD, &sg, 1, 0))
786 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
787}
788
Alex Williamson1824a982009-05-01 17:31:10 +0000789static void virtnet_vlan_rx_kill_vid(struct net_device *dev, u16 vid)
Alex Williamson0bde95692009-02-04 09:02:50 +0000790{
791 struct virtnet_info *vi = netdev_priv(dev);
792 struct scatterlist sg;
793
Alex Williamson23e258e2009-05-01 17:27:56 +0000794 sg_init_one(&sg, &vid, sizeof(vid));
Alex Williamson0bde95692009-02-04 09:02:50 +0000795
796 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
797 VIRTIO_NET_CTRL_VLAN_DEL, &sg, 1, 0))
798 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
799}
800
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700801static const struct ethtool_ops virtnet_ethtool_ops = {
Herbert Xua9ea3fc2008-04-18 11:21:42 +0800802 .set_tx_csum = virtnet_set_tx_csum,
803 .set_sg = ethtool_op_set_sg,
Mark McLoughlin0276b492008-11-16 22:40:36 -0800804 .set_tso = ethtool_op_set_tso,
Sridhar Samudrala5c516752009-07-14 14:21:02 +0000805 .set_ufo = ethtool_op_set_ufo,
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -0800806 .get_link = ethtool_op_get_link,
Herbert Xua9ea3fc2008-04-18 11:21:42 +0800807};
808
Mark McLoughlin39da5812008-11-26 13:58:11 +0000809#define MIN_MTU 68
810#define MAX_MTU 65535
811
812static int virtnet_change_mtu(struct net_device *dev, int new_mtu)
813{
814 if (new_mtu < MIN_MTU || new_mtu > MAX_MTU)
815 return -EINVAL;
816 dev->mtu = new_mtu;
817 return 0;
818}
819
Stephen Hemminger76288b42009-01-06 10:44:22 -0800820static const struct net_device_ops virtnet_netdev = {
821 .ndo_open = virtnet_open,
822 .ndo_stop = virtnet_close,
823 .ndo_start_xmit = start_xmit,
824 .ndo_validate_addr = eth_validate_addr,
Alex Williamson9c46f6d2009-02-04 16:36:34 -0800825 .ndo_set_mac_address = virtnet_set_mac_address,
Alex Williamson2af76982009-02-04 09:02:40 +0000826 .ndo_set_rx_mode = virtnet_set_rx_mode,
Stephen Hemminger76288b42009-01-06 10:44:22 -0800827 .ndo_change_mtu = virtnet_change_mtu,
Alex Williamson1824a982009-05-01 17:31:10 +0000828 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
829 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
Stephen Hemminger76288b42009-01-06 10:44:22 -0800830#ifdef CONFIG_NET_POLL_CONTROLLER
831 .ndo_poll_controller = virtnet_netpoll,
832#endif
833};
834
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -0800835static void virtnet_update_status(struct virtnet_info *vi)
836{
837 u16 v;
838
839 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS))
840 return;
841
842 vi->vdev->config->get(vi->vdev,
843 offsetof(struct virtio_net_config, status),
844 &v, sizeof(v));
845
846 /* Ignore unknown (future) status bits */
847 v &= VIRTIO_NET_S_LINK_UP;
848
849 if (vi->status == v)
850 return;
851
852 vi->status = v;
853
854 if (vi->status & VIRTIO_NET_S_LINK_UP) {
855 netif_carrier_on(vi->dev);
856 netif_wake_queue(vi->dev);
857 } else {
858 netif_carrier_off(vi->dev);
859 netif_stop_queue(vi->dev);
860 }
861}
862
863static void virtnet_config_changed(struct virtio_device *vdev)
864{
865 struct virtnet_info *vi = vdev->priv;
866
867 virtnet_update_status(vi);
868}
869
Rusty Russell296f96f2007-10-22 11:03:37 +1000870static int virtnet_probe(struct virtio_device *vdev)
871{
872 int err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000873 struct net_device *dev;
874 struct virtnet_info *vi;
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600875 struct virtqueue *vqs[3];
876 vq_callback_t *callbacks[] = { skb_recv_done, skb_xmit_done, NULL};
877 const char *names[] = { "input", "output", "control" };
878 int nvqs;
Rusty Russell296f96f2007-10-22 11:03:37 +1000879
880 /* Allocate ourselves a network device with room for our info */
881 dev = alloc_etherdev(sizeof(struct virtnet_info));
882 if (!dev)
883 return -ENOMEM;
884
885 /* Set up network device as normal. */
Stephen Hemminger76288b42009-01-06 10:44:22 -0800886 dev->netdev_ops = &virtnet_netdev;
Rusty Russell296f96f2007-10-22 11:03:37 +1000887 dev->features = NETIF_F_HIGHDMA;
Herbert Xua9ea3fc2008-04-18 11:21:42 +0800888 SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops);
Rusty Russell296f96f2007-10-22 11:03:37 +1000889 SET_NETDEV_DEV(dev, &vdev->dev);
890
891 /* Do we support "hardware" checksums? */
Rusty Russellc45a6812008-05-02 21:50:50 -0500892 if (csum && virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000893 /* This opens up the world of extra features. */
894 dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
Rusty Russellc45a6812008-05-02 21:50:50 -0500895 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
Rusty Russell34a48572008-02-04 23:50:02 -0500896 dev->features |= NETIF_F_TSO | NETIF_F_UFO
897 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
898 }
Rusty Russell5539ae92008-05-02 21:50:46 -0500899 /* Individual feature bits: what can host handle? */
Rusty Russellc45a6812008-05-02 21:50:50 -0500900 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
Rusty Russell5539ae92008-05-02 21:50:46 -0500901 dev->features |= NETIF_F_TSO;
Rusty Russellc45a6812008-05-02 21:50:50 -0500902 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
Rusty Russell5539ae92008-05-02 21:50:46 -0500903 dev->features |= NETIF_F_TSO6;
Rusty Russellc45a6812008-05-02 21:50:50 -0500904 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
Rusty Russell5539ae92008-05-02 21:50:46 -0500905 dev->features |= NETIF_F_TSO_ECN;
Rusty Russellc45a6812008-05-02 21:50:50 -0500906 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
Rusty Russell5539ae92008-05-02 21:50:46 -0500907 dev->features |= NETIF_F_UFO;
Rusty Russell296f96f2007-10-22 11:03:37 +1000908 }
909
910 /* Configuration may specify what MAC to use. Otherwise random. */
Rusty Russellc45a6812008-05-02 21:50:50 -0500911 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
Rusty Russella586d4f2008-02-04 23:49:56 -0500912 vdev->config->get(vdev,
913 offsetof(struct virtio_net_config, mac),
914 dev->dev_addr, dev->addr_len);
Alex Williamson62994b22009-04-04 16:40:19 -0700915 } else
Rusty Russell296f96f2007-10-22 11:03:37 +1000916 random_ether_addr(dev->dev_addr);
917
918 /* Set up our device-specific information */
919 vi = netdev_priv(dev);
Dor Laor6c0cd7c2007-12-16 15:19:43 +0200920 netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight);
Rusty Russell296f96f2007-10-22 11:03:37 +1000921 vi->dev = dev;
922 vi->vdev = vdev;
Christian Borntraegerd9d5dcc2008-02-18 10:02:51 +0100923 vdev->priv = vi;
Rusty Russellfb6813f2008-07-25 12:06:01 -0500924 vi->pages = NULL;
Rusty Russell3161e452009-08-26 12:22:32 -0700925 INIT_DELAYED_WORK(&vi->refill, refill_work);
Rusty Russell296f96f2007-10-22 11:03:37 +1000926
Rusty Russell363f1512008-06-08 20:51:55 +1000927 /* If they give us a callback when all buffers are done, we don't need
928 * the timer. */
929 vi->free_in_tasklet = virtio_has_feature(vdev,VIRTIO_F_NOTIFY_ON_EMPTY);
930
Herbert Xu97402b92008-04-18 11:24:27 +0800931 /* If we can receive ANY GSO packets, we must allocate large ones. */
932 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4)
933 || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6)
934 || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN))
935 vi->big_packets = true;
936
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800937 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
938 vi->mergeable_rx_bufs = true;
939
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600940 /* We expect two virtqueues, receive then send,
941 * and optionally control. */
942 nvqs = virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) ? 3 : 2;
Rusty Russell296f96f2007-10-22 11:03:37 +1000943
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600944 err = vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names);
945 if (err)
946 goto free;
947
948 vi->rvq = vqs[0];
949 vi->svq = vqs[1];
Rusty Russell296f96f2007-10-22 11:03:37 +1000950
Alex Williamson2a41f712009-02-04 09:02:34 +0000951 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)) {
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600952 vi->cvq = vqs[2];
Alex Williamson0bde95692009-02-04 09:02:50 +0000953
954 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
955 dev->features |= NETIF_F_HW_VLAN_FILTER;
Alex Williamson2a41f712009-02-04 09:02:34 +0000956 }
957
Rusty Russell296f96f2007-10-22 11:03:37 +1000958 /* Initialize our empty receive and send queues. */
959 skb_queue_head_init(&vi->recv);
960 skb_queue_head_init(&vi->send);
961
Rusty Russell11a3a152008-05-26 17:48:13 +1000962 tasklet_init(&vi->tasklet, xmit_tasklet, (unsigned long)vi);
963
Rusty Russell363f1512008-06-08 20:51:55 +1000964 if (!vi->free_in_tasklet)
965 setup_timer(&vi->xmit_free_timer, xmit_free, (unsigned long)vi);
Mark McLoughlin14c998f2008-06-08 20:50:56 +1000966
Rusty Russell296f96f2007-10-22 11:03:37 +1000967 err = register_netdev(dev);
968 if (err) {
969 pr_debug("virtio_net: registering device failed\n");
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600970 goto free_vqs;
Rusty Russell296f96f2007-10-22 11:03:37 +1000971 }
Rusty Russellb3369c12008-02-04 23:50:02 -0500972
973 /* Last of all, set up some receive buffers. */
Rusty Russell3161e452009-08-26 12:22:32 -0700974 try_fill_recv(vi, GFP_KERNEL);
Rusty Russellb3369c12008-02-04 23:50:02 -0500975
976 /* If we didn't even get one input buffer, we're useless. */
977 if (vi->num == 0) {
978 err = -ENOMEM;
979 goto unregister;
980 }
981
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -0800982 vi->status = VIRTIO_NET_S_LINK_UP;
983 virtnet_update_status(vi);
Pantelis Koukousoulas47832562009-03-18 18:40:02 -0700984 netif_carrier_on(dev);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -0800985
Rusty Russell296f96f2007-10-22 11:03:37 +1000986 pr_debug("virtnet: registered device %s\n", dev->name);
Rusty Russell296f96f2007-10-22 11:03:37 +1000987 return 0;
988
Rusty Russellb3369c12008-02-04 23:50:02 -0500989unregister:
990 unregister_netdev(dev);
Rusty Russell3161e452009-08-26 12:22:32 -0700991 cancel_delayed_work_sync(&vi->refill);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600992free_vqs:
993 vdev->config->del_vqs(vdev);
Rusty Russell296f96f2007-10-22 11:03:37 +1000994free:
995 free_netdev(dev);
996 return err;
997}
998
999static void virtnet_remove(struct virtio_device *vdev)
1000{
Rusty Russell74b25532007-11-19 11:20:42 -05001001 struct virtnet_info *vi = vdev->priv;
Rusty Russellb3369c12008-02-04 23:50:02 -05001002 struct sk_buff *skb;
1003
Rusty Russell6e5aa7e2008-02-04 23:50:03 -05001004 /* Stop all the virtqueues. */
1005 vdev->config->reset(vdev);
1006
Rusty Russell363f1512008-06-08 20:51:55 +10001007 if (!vi->free_in_tasklet)
1008 del_timer_sync(&vi->xmit_free_timer);
Mark McLoughlin14c998f2008-06-08 20:50:56 +10001009
Rusty Russellb3369c12008-02-04 23:50:02 -05001010 /* Free our skbs in send and recv queues, if any. */
Rusty Russellb3369c12008-02-04 23:50:02 -05001011 while ((skb = __skb_dequeue(&vi->recv)) != NULL) {
1012 kfree_skb(skb);
1013 vi->num--;
1014 }
Wang Chen288369c2008-05-22 18:07:43 +08001015 __skb_queue_purge(&vi->send);
Rusty Russellb3369c12008-02-04 23:50:02 -05001016
1017 BUG_ON(vi->num != 0);
Rusty Russell74b25532007-11-19 11:20:42 -05001018
Rusty Russell74b25532007-11-19 11:20:42 -05001019 unregister_netdev(vi->dev);
Rusty Russell3161e452009-08-26 12:22:32 -07001020 cancel_delayed_work_sync(&vi->refill);
Rusty Russellfb6813f2008-07-25 12:06:01 -05001021
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06001022 vdev->config->del_vqs(vi->vdev);
1023
Rusty Russellfb6813f2008-07-25 12:06:01 -05001024 while (vi->pages)
1025 __free_pages(get_a_page(vi, GFP_KERNEL), 0);
1026
Rusty Russell74b25532007-11-19 11:20:42 -05001027 free_netdev(vi->dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10001028}
1029
1030static struct virtio_device_id id_table[] = {
1031 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
1032 { 0 },
1033};
1034
Rusty Russellc45a6812008-05-02 21:50:50 -05001035static unsigned int features[] = {
Mark McLoughlin5e4fe5c2008-07-08 17:10:42 +10001036 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM,
1037 VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
Rusty Russellc45a6812008-05-02 21:50:50 -05001038 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
Herbert Xu97402b92008-04-18 11:24:27 +08001039 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
Sridhar Samudrala5c516752009-07-14 14:21:02 +00001040 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO,
Alex Williamson2a41f712009-02-04 09:02:34 +00001041 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ,
Alex Williamson0bde95692009-02-04 09:02:50 +00001042 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN,
Herbert Xu97402b92008-04-18 11:24:27 +08001043 VIRTIO_F_NOTIFY_ON_EMPTY,
Rusty Russellc45a6812008-05-02 21:50:50 -05001044};
1045
Rusty Russell296f96f2007-10-22 11:03:37 +10001046static struct virtio_driver virtio_net = {
Rusty Russellc45a6812008-05-02 21:50:50 -05001047 .feature_table = features,
1048 .feature_table_size = ARRAY_SIZE(features),
Rusty Russell296f96f2007-10-22 11:03:37 +10001049 .driver.name = KBUILD_MODNAME,
1050 .driver.owner = THIS_MODULE,
1051 .id_table = id_table,
1052 .probe = virtnet_probe,
1053 .remove = __devexit_p(virtnet_remove),
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001054 .config_changed = virtnet_config_changed,
Rusty Russell296f96f2007-10-22 11:03:37 +10001055};
1056
1057static int __init init(void)
1058{
1059 return register_virtio_driver(&virtio_net);
1060}
1061
1062static void __exit fini(void)
1063{
1064 unregister_virtio_driver(&virtio_net);
1065}
1066module_init(init);
1067module_exit(fini);
1068
1069MODULE_DEVICE_TABLE(virtio, id_table);
1070MODULE_DESCRIPTION("Virtio network driver");
1071MODULE_LICENSE("GPL");