blob: 68f908a57ac326bb21bdcfafebd7cbc154a4b467 [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>
27
Dor Laor6c0cd7c2007-12-16 15:19:43 +020028static int napi_weight = 128;
29module_param(napi_weight, int, 0444);
30
Rusty Russell34a48572008-02-04 23:50:02 -050031static int csum = 1, gso = 1;
32module_param(csum, bool, 0444);
33module_param(gso, bool, 0444);
34
Rusty Russell296f96f2007-10-22 11:03:37 +100035/* FIXME: MTU in config. */
36#define MAX_PACKET_LEN (ETH_HLEN+ETH_DATA_LEN)
37
38struct virtnet_info
39{
40 struct virtio_device *vdev;
41 struct virtqueue *rvq, *svq;
42 struct net_device *dev;
43 struct napi_struct napi;
44
Rusty Russell99ffc692008-05-02 21:50:46 -050045 /* The skb we couldn't send because buffers were full. */
46 struct sk_buff *last_xmit_skb;
47
Rusty Russell363f1512008-06-08 20:51:55 +100048 /* If we need to free in a timer, this is it. */
Mark McLoughlin14c998f2008-06-08 20:50:56 +100049 struct timer_list xmit_free_timer;
50
Rusty Russell296f96f2007-10-22 11:03:37 +100051 /* Number of input buffers, and max we've ever had. */
52 unsigned int num, max;
53
Rusty Russell11a3a152008-05-26 17:48:13 +100054 /* For cleaning up after transmission. */
55 struct tasklet_struct tasklet;
Rusty Russell363f1512008-06-08 20:51:55 +100056 bool free_in_tasklet;
Rusty Russell11a3a152008-05-26 17:48:13 +100057
Herbert Xu97402b92008-04-18 11:24:27 +080058 /* I like... big packets and I cannot lie! */
59 bool big_packets;
60
Rusty Russell296f96f2007-10-22 11:03:37 +100061 /* Receive & send queues. */
62 struct sk_buff_head recv;
63 struct sk_buff_head send;
Rusty Russellfb6813f2008-07-25 12:06:01 -050064
65 /* Chain pages by the private ptr. */
66 struct page *pages;
Rusty Russell296f96f2007-10-22 11:03:37 +100067};
68
69static inline struct virtio_net_hdr *skb_vnet_hdr(struct sk_buff *skb)
70{
71 return (struct virtio_net_hdr *)skb->cb;
72}
73
74static inline void vnet_hdr_to_sg(struct scatterlist *sg, struct sk_buff *skb)
75{
76 sg_init_one(sg, skb_vnet_hdr(skb), sizeof(struct virtio_net_hdr));
77}
78
Rusty Russellfb6813f2008-07-25 12:06:01 -050079static void give_a_page(struct virtnet_info *vi, struct page *page)
80{
81 page->private = (unsigned long)vi->pages;
82 vi->pages = page;
83}
84
85static struct page *get_a_page(struct virtnet_info *vi, gfp_t gfp_mask)
86{
87 struct page *p = vi->pages;
88
89 if (p)
90 vi->pages = (struct page *)p->private;
91 else
92 p = alloc_page(gfp_mask);
93 return p;
94}
95
Rusty Russell2cb9c6b2008-02-04 23:50:07 -050096static void skb_xmit_done(struct virtqueue *svq)
Rusty Russell296f96f2007-10-22 11:03:37 +100097{
Rusty Russell2cb9c6b2008-02-04 23:50:07 -050098 struct virtnet_info *vi = svq->vdev->priv;
Rusty Russell296f96f2007-10-22 11:03:37 +100099
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500100 /* Suppress further interrupts. */
101 svq->vq_ops->disable_cb(svq);
Rusty Russell11a3a152008-05-26 17:48:13 +1000102
Rusty Russell363f1512008-06-08 20:51:55 +1000103 /* We were probably waiting for more output buffers. */
Rusty Russell296f96f2007-10-22 11:03:37 +1000104 netif_wake_queue(vi->dev);
Rusty Russell11a3a152008-05-26 17:48:13 +1000105
106 /* Make sure we re-xmit last_xmit_skb: if there are no more packets
107 * queued, start_xmit won't be called. */
108 tasklet_schedule(&vi->tasklet);
Rusty Russell296f96f2007-10-22 11:03:37 +1000109}
110
111static void receive_skb(struct net_device *dev, struct sk_buff *skb,
112 unsigned len)
113{
114 struct virtio_net_hdr *hdr = skb_vnet_hdr(skb);
Herbert Xu97402b92008-04-18 11:24:27 +0800115 int err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000116
117 if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) {
118 pr_debug("%s: short packet %i\n", dev->name, len);
119 dev->stats.rx_length_errors++;
120 goto drop;
121 }
122 len -= sizeof(struct virtio_net_hdr);
Rusty Russell296f96f2007-10-22 11:03:37 +1000123
Rusty Russellfb6813f2008-07-25 12:06:01 -0500124 if (len <= MAX_PACKET_LEN) {
125 unsigned int i;
126
127 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
128 give_a_page(dev->priv, skb_shinfo(skb)->frags[i].page);
129 skb->data_len = 0;
130 skb_shinfo(skb)->nr_frags = 0;
131 }
132
Herbert Xu97402b92008-04-18 11:24:27 +0800133 err = pskb_trim(skb, len);
134 if (err) {
135 pr_debug("%s: pskb_trim failed %i %d\n", dev->name, len, err);
136 dev->stats.rx_dropped++;
137 goto drop;
138 }
139 skb->truesize += skb->data_len;
Rusty Russell296f96f2007-10-22 11:03:37 +1000140 dev->stats.rx_bytes += skb->len;
141 dev->stats.rx_packets++;
142
143 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
144 pr_debug("Needs csum!\n");
Rusty Russellf35d9d82008-02-04 23:49:54 -0500145 if (!skb_partial_csum_set(skb,hdr->csum_start,hdr->csum_offset))
Rusty Russell296f96f2007-10-22 11:03:37 +1000146 goto frame_err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000147 }
148
Mark McLoughlin23cde762008-06-08 20:49:00 +1000149 skb->protocol = eth_type_trans(skb, dev);
150 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
151 ntohs(skb->protocol), skb->len, skb->pkt_type);
152
Rusty Russell296f96f2007-10-22 11:03:37 +1000153 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
154 pr_debug("GSO!\n");
Rusty Russell34a48572008-02-04 23:50:02 -0500155 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000156 case VIRTIO_NET_HDR_GSO_TCPV4:
157 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
158 break;
Rusty Russell296f96f2007-10-22 11:03:37 +1000159 case VIRTIO_NET_HDR_GSO_UDP:
160 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
161 break;
162 case VIRTIO_NET_HDR_GSO_TCPV6:
163 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
164 break;
165 default:
166 if (net_ratelimit())
167 printk(KERN_WARNING "%s: bad gso type %u.\n",
168 dev->name, hdr->gso_type);
169 goto frame_err;
170 }
171
Rusty Russell34a48572008-02-04 23:50:02 -0500172 if (hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN)
173 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
174
Rusty Russell296f96f2007-10-22 11:03:37 +1000175 skb_shinfo(skb)->gso_size = hdr->gso_size;
176 if (skb_shinfo(skb)->gso_size == 0) {
177 if (net_ratelimit())
178 printk(KERN_WARNING "%s: zero gso size.\n",
179 dev->name);
180 goto frame_err;
181 }
182
183 /* Header must be checked, and gso_segs computed. */
184 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
185 skb_shinfo(skb)->gso_segs = 0;
186 }
187
188 netif_receive_skb(skb);
189 return;
190
191frame_err:
192 dev->stats.rx_frame_errors++;
193drop:
194 dev_kfree_skb(skb);
195}
196
197static void try_fill_recv(struct virtnet_info *vi)
198{
199 struct sk_buff *skb;
Rusty Russell05271682008-05-02 21:50:45 -0500200 struct scatterlist sg[2+MAX_SKB_FRAGS];
Herbert Xu97402b92008-04-18 11:24:27 +0800201 int num, err, i;
Rusty Russell296f96f2007-10-22 11:03:37 +1000202
Rusty Russell05271682008-05-02 21:50:45 -0500203 sg_init_table(sg, 2+MAX_SKB_FRAGS);
Rusty Russell296f96f2007-10-22 11:03:37 +1000204 for (;;) {
205 skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN);
206 if (unlikely(!skb))
207 break;
208
209 skb_put(skb, MAX_PACKET_LEN);
210 vnet_hdr_to_sg(sg, skb);
Herbert Xu97402b92008-04-18 11:24:27 +0800211
212 if (vi->big_packets) {
213 for (i = 0; i < MAX_SKB_FRAGS; i++) {
214 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
Rusty Russellfb6813f2008-07-25 12:06:01 -0500215 f->page = get_a_page(vi, GFP_ATOMIC);
Herbert Xu97402b92008-04-18 11:24:27 +0800216 if (!f->page)
217 break;
218
219 f->page_offset = 0;
220 f->size = PAGE_SIZE;
221
222 skb->data_len += PAGE_SIZE;
223 skb->len += PAGE_SIZE;
224
225 skb_shinfo(skb)->nr_frags++;
226 }
227 }
228
Rusty Russell296f96f2007-10-22 11:03:37 +1000229 num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
230 skb_queue_head(&vi->recv, skb);
231
232 err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, num, skb);
233 if (err) {
234 skb_unlink(skb, &vi->recv);
235 kfree_skb(skb);
236 break;
237 }
238 vi->num++;
239 }
240 if (unlikely(vi->num > vi->max))
241 vi->max = vi->num;
242 vi->rvq->vq_ops->kick(vi->rvq);
243}
244
Rusty Russell18445c42008-02-04 23:49:57 -0500245static void skb_recv_done(struct virtqueue *rvq)
Rusty Russell296f96f2007-10-22 11:03:37 +1000246{
247 struct virtnet_info *vi = rvq->vdev->priv;
Rusty Russell18445c42008-02-04 23:49:57 -0500248 /* Schedule NAPI, Suppress further interrupts if successful. */
249 if (netif_rx_schedule_prep(vi->dev, &vi->napi)) {
250 rvq->vq_ops->disable_cb(rvq);
251 __netif_rx_schedule(vi->dev, &vi->napi);
252 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000253}
254
255static int virtnet_poll(struct napi_struct *napi, int budget)
256{
257 struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi);
258 struct sk_buff *skb = NULL;
259 unsigned int len, received = 0;
260
261again:
262 while (received < budget &&
263 (skb = vi->rvq->vq_ops->get_buf(vi->rvq, &len)) != NULL) {
264 __skb_unlink(skb, &vi->recv);
265 receive_skb(vi->dev, skb, len);
266 vi->num--;
267 received++;
268 }
269
270 /* FIXME: If we oom and completely run out of inbufs, we need
271 * to start a timer trying to fill more. */
272 if (vi->num < vi->max / 2)
273 try_fill_recv(vi);
274
Rusty Russell8329d982007-11-19 11:20:43 -0500275 /* Out of packets? */
276 if (received < budget) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000277 netif_rx_complete(vi->dev, napi);
Rusty Russell18445c42008-02-04 23:49:57 -0500278 if (unlikely(!vi->rvq->vq_ops->enable_cb(vi->rvq))
Christian Borntraeger4265f162008-03-14 14:17:05 +0100279 && napi_schedule_prep(napi)) {
280 vi->rvq->vq_ops->disable_cb(vi->rvq);
281 __netif_rx_schedule(vi->dev, napi);
Rusty Russell296f96f2007-10-22 11:03:37 +1000282 goto again;
Christian Borntraeger4265f162008-03-14 14:17:05 +0100283 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000284 }
285
286 return received;
287}
288
289static void free_old_xmit_skbs(struct virtnet_info *vi)
290{
291 struct sk_buff *skb;
292 unsigned int len;
293
294 while ((skb = vi->svq->vq_ops->get_buf(vi->svq, &len)) != NULL) {
295 pr_debug("Sent skb %p\n", skb);
296 __skb_unlink(skb, &vi->send);
Rusty Russell655aa312008-05-02 21:50:43 -0500297 vi->dev->stats.tx_bytes += skb->len;
Rusty Russell296f96f2007-10-22 11:03:37 +1000298 vi->dev->stats.tx_packets++;
299 kfree_skb(skb);
300 }
301}
302
Rusty Russell363f1512008-06-08 20:51:55 +1000303/* If the virtio transport doesn't always notify us when all in-flight packets
304 * are consumed, we fall back to using this function on a timer to free them. */
Mark McLoughlin14c998f2008-06-08 20:50:56 +1000305static void xmit_free(unsigned long data)
306{
307 struct virtnet_info *vi = (void *)data;
308
309 netif_tx_lock(vi->dev);
310
311 free_old_xmit_skbs(vi);
312
313 if (!skb_queue_empty(&vi->send))
314 mod_timer(&vi->xmit_free_timer, jiffies + (HZ/10));
315
316 netif_tx_unlock(vi->dev);
317}
318
Rusty Russell99ffc692008-05-02 21:50:46 -0500319static int xmit_skb(struct virtnet_info *vi, struct sk_buff *skb)
Rusty Russell296f96f2007-10-22 11:03:37 +1000320{
Mark McLoughlin14c998f2008-06-08 20:50:56 +1000321 int num, err;
Rusty Russell05271682008-05-02 21:50:45 -0500322 struct scatterlist sg[2+MAX_SKB_FRAGS];
Rusty Russell296f96f2007-10-22 11:03:37 +1000323 struct virtio_net_hdr *hdr;
324 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
Rusty Russell296f96f2007-10-22 11:03:37 +1000325
Rusty Russell05271682008-05-02 21:50:45 -0500326 sg_init_table(sg, 2+MAX_SKB_FRAGS);
Rusty Russell4d125de2007-11-07 16:34:49 +1100327
Johannes Berge1749612008-10-27 15:59:26 -0700328 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
Rusty Russell296f96f2007-10-22 11:03:37 +1000329
Rusty Russell296f96f2007-10-22 11:03:37 +1000330 /* Encode metadata header at front. */
331 hdr = skb_vnet_hdr(skb);
332 if (skb->ip_summed == CHECKSUM_PARTIAL) {
333 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
334 hdr->csum_start = skb->csum_start - skb_headroom(skb);
335 hdr->csum_offset = skb->csum_offset;
336 } else {
337 hdr->flags = 0;
338 hdr->csum_offset = hdr->csum_start = 0;
339 }
340
341 if (skb_is_gso(skb)) {
Rusty Russell50c8ea82008-02-04 23:50:01 -0500342 hdr->hdr_len = skb_transport_header(skb) - skb->data;
Rusty Russell296f96f2007-10-22 11:03:37 +1000343 hdr->gso_size = skb_shinfo(skb)->gso_size;
Rusty Russell34a48572008-02-04 23:50:02 -0500344 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
Rusty Russell296f96f2007-10-22 11:03:37 +1000345 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
346 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
347 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
348 else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
349 hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
350 else
351 BUG();
Rusty Russell34a48572008-02-04 23:50:02 -0500352 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
353 hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
Rusty Russell296f96f2007-10-22 11:03:37 +1000354 } else {
355 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
Rusty Russell50c8ea82008-02-04 23:50:01 -0500356 hdr->gso_size = hdr->hdr_len = 0;
Rusty Russell296f96f2007-10-22 11:03:37 +1000357 }
358
359 vnet_hdr_to_sg(sg, skb);
360 num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
Rusty Russell99ffc692008-05-02 21:50:46 -0500361
Mark McLoughlin14c998f2008-06-08 20:50:56 +1000362 err = vi->svq->vq_ops->add_buf(vi->svq, sg, num, 0, skb);
Rusty Russell363f1512008-06-08 20:51:55 +1000363 if (!err && !vi->free_in_tasklet)
Mark McLoughlin14c998f2008-06-08 20:50:56 +1000364 mod_timer(&vi->xmit_free_timer, jiffies + (HZ/10));
365
366 return err;
Rusty Russell99ffc692008-05-02 21:50:46 -0500367}
368
Rusty Russell11a3a152008-05-26 17:48:13 +1000369static void xmit_tasklet(unsigned long data)
370{
371 struct virtnet_info *vi = (void *)data;
372
373 netif_tx_lock_bh(vi->dev);
374 if (vi->last_xmit_skb && xmit_skb(vi, vi->last_xmit_skb) == 0) {
375 vi->svq->vq_ops->kick(vi->svq);
376 vi->last_xmit_skb = NULL;
377 }
Rusty Russell363f1512008-06-08 20:51:55 +1000378 if (vi->free_in_tasklet)
379 free_old_xmit_skbs(vi);
Rusty Russell11a3a152008-05-26 17:48:13 +1000380 netif_tx_unlock_bh(vi->dev);
381}
382
Rusty Russell99ffc692008-05-02 21:50:46 -0500383static int start_xmit(struct sk_buff *skb, struct net_device *dev)
384{
385 struct virtnet_info *vi = netdev_priv(dev);
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500386
387again:
388 /* Free up any pending old buffers before queueing new ones. */
389 free_old_xmit_skbs(vi);
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500390
Rusty Russell99ffc692008-05-02 21:50:46 -0500391 /* If we has a buffer left over from last time, send it now. */
Mark McLoughlin9953ca62008-05-27 12:06:26 +0100392 if (unlikely(vi->last_xmit_skb) &&
393 xmit_skb(vi, vi->last_xmit_skb) != 0)
394 goto stop_queue;
395
396 vi->last_xmit_skb = NULL;
Rusty Russell296f96f2007-10-22 11:03:37 +1000397
Rusty Russell99ffc692008-05-02 21:50:46 -0500398 /* Put new one in send queue and do transmit */
Rusty Russell7eb2e252008-05-26 17:42:42 +1000399 if (likely(skb)) {
400 __skb_queue_head(&vi->send, skb);
401 if (xmit_skb(vi, skb) != 0) {
402 vi->last_xmit_skb = skb;
403 skb = NULL;
404 goto stop_queue;
405 }
Rusty Russell99ffc692008-05-02 21:50:46 -0500406 }
407done:
408 vi->svq->vq_ops->kick(vi->svq);
409 return NETDEV_TX_OK;
410
411stop_queue:
412 pr_debug("%s: virtio not prepared to send\n", dev->name);
413 netif_stop_queue(dev);
414
415 /* Activate callback for using skbs: if this returns false it
416 * means some were used in the meantime. */
417 if (unlikely(!vi->svq->vq_ops->enable_cb(vi->svq))) {
418 vi->svq->vq_ops->disable_cb(vi->svq);
419 netif_start_queue(dev);
420 goto again;
421 }
Mark McLoughlin9953ca62008-05-27 12:06:26 +0100422 if (skb) {
423 /* Drop this skb: we only queue one. */
424 vi->dev->stats.tx_dropped++;
425 kfree_skb(skb);
426 }
Rusty Russell99ffc692008-05-02 21:50:46 -0500427 goto done;
Rusty Russell296f96f2007-10-22 11:03:37 +1000428}
429
Amit Shahda74e892008-02-29 16:24:50 +0530430#ifdef CONFIG_NET_POLL_CONTROLLER
431static void virtnet_netpoll(struct net_device *dev)
432{
433 struct virtnet_info *vi = netdev_priv(dev);
434
435 napi_schedule(&vi->napi);
436}
437#endif
438
Rusty Russell296f96f2007-10-22 11:03:37 +1000439static int virtnet_open(struct net_device *dev)
440{
441 struct virtnet_info *vi = netdev_priv(dev);
442
Rusty Russell296f96f2007-10-22 11:03:37 +1000443 napi_enable(&vi->napi);
Rusty Russella48bd8f2008-02-04 23:50:07 -0500444
445 /* If all buffers were filled by other side before we napi_enabled, we
446 * won't get another interrupt, so process any outstanding packets
Christian Borntraeger370076d2008-02-06 08:50:11 +0100447 * now. virtnet_poll wants re-enable the queue, so we disable here.
448 * We synchronize against interrupts via NAPI_STATE_SCHED */
449 if (netif_rx_schedule_prep(dev, &vi->napi)) {
450 vi->rvq->vq_ops->disable_cb(vi->rvq);
451 __netif_rx_schedule(dev, &vi->napi);
452 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000453 return 0;
454}
455
456static int virtnet_close(struct net_device *dev)
457{
458 struct virtnet_info *vi = netdev_priv(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +1000459
460 napi_disable(&vi->napi);
461
Rusty Russell296f96f2007-10-22 11:03:37 +1000462 return 0;
463}
464
Herbert Xua9ea3fc2008-04-18 11:21:42 +0800465static int virtnet_set_tx_csum(struct net_device *dev, u32 data)
466{
467 struct virtnet_info *vi = netdev_priv(dev);
468 struct virtio_device *vdev = vi->vdev;
469
470 if (data && !virtio_has_feature(vdev, VIRTIO_NET_F_CSUM))
471 return -ENOSYS;
472
473 return ethtool_op_set_tx_hw_csum(dev, data);
474}
475
476static struct ethtool_ops virtnet_ethtool_ops = {
477 .set_tx_csum = virtnet_set_tx_csum,
478 .set_sg = ethtool_op_set_sg,
479};
480
Rusty Russell296f96f2007-10-22 11:03:37 +1000481static int virtnet_probe(struct virtio_device *vdev)
482{
483 int err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000484 struct net_device *dev;
485 struct virtnet_info *vi;
Rusty Russell296f96f2007-10-22 11:03:37 +1000486
487 /* Allocate ourselves a network device with room for our info */
488 dev = alloc_etherdev(sizeof(struct virtnet_info));
489 if (!dev)
490 return -ENOMEM;
491
492 /* Set up network device as normal. */
Rusty Russell296f96f2007-10-22 11:03:37 +1000493 dev->open = virtnet_open;
494 dev->stop = virtnet_close;
495 dev->hard_start_xmit = start_xmit;
496 dev->features = NETIF_F_HIGHDMA;
Amit Shahda74e892008-02-29 16:24:50 +0530497#ifdef CONFIG_NET_POLL_CONTROLLER
498 dev->poll_controller = virtnet_netpoll;
499#endif
Herbert Xua9ea3fc2008-04-18 11:21:42 +0800500 SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops);
Rusty Russell296f96f2007-10-22 11:03:37 +1000501 SET_NETDEV_DEV(dev, &vdev->dev);
502
503 /* Do we support "hardware" checksums? */
Rusty Russellc45a6812008-05-02 21:50:50 -0500504 if (csum && virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000505 /* This opens up the world of extra features. */
506 dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
Rusty Russellc45a6812008-05-02 21:50:50 -0500507 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
Rusty Russell34a48572008-02-04 23:50:02 -0500508 dev->features |= NETIF_F_TSO | NETIF_F_UFO
509 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
510 }
Rusty Russell5539ae962008-05-02 21:50:46 -0500511 /* Individual feature bits: what can host handle? */
Rusty Russellc45a6812008-05-02 21:50:50 -0500512 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
Rusty Russell5539ae962008-05-02 21:50:46 -0500513 dev->features |= NETIF_F_TSO;
Rusty Russellc45a6812008-05-02 21:50:50 -0500514 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
Rusty Russell5539ae962008-05-02 21:50:46 -0500515 dev->features |= NETIF_F_TSO6;
Rusty Russellc45a6812008-05-02 21:50:50 -0500516 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
Rusty Russell5539ae962008-05-02 21:50:46 -0500517 dev->features |= NETIF_F_TSO_ECN;
Rusty Russellc45a6812008-05-02 21:50:50 -0500518 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
Rusty Russell5539ae962008-05-02 21:50:46 -0500519 dev->features |= NETIF_F_UFO;
Rusty Russell296f96f2007-10-22 11:03:37 +1000520 }
521
522 /* Configuration may specify what MAC to use. Otherwise random. */
Rusty Russellc45a6812008-05-02 21:50:50 -0500523 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
Rusty Russella586d4f2008-02-04 23:49:56 -0500524 vdev->config->get(vdev,
525 offsetof(struct virtio_net_config, mac),
526 dev->dev_addr, dev->addr_len);
Rusty Russell296f96f2007-10-22 11:03:37 +1000527 } else
528 random_ether_addr(dev->dev_addr);
529
530 /* Set up our device-specific information */
531 vi = netdev_priv(dev);
Dor Laor6c0cd7c2007-12-16 15:19:43 +0200532 netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight);
Rusty Russell296f96f2007-10-22 11:03:37 +1000533 vi->dev = dev;
534 vi->vdev = vdev;
Christian Borntraegerd9d5dcc2008-02-18 10:02:51 +0100535 vdev->priv = vi;
Rusty Russellfb6813f2008-07-25 12:06:01 -0500536 vi->pages = NULL;
Rusty Russell296f96f2007-10-22 11:03:37 +1000537
Rusty Russell363f1512008-06-08 20:51:55 +1000538 /* If they give us a callback when all buffers are done, we don't need
539 * the timer. */
540 vi->free_in_tasklet = virtio_has_feature(vdev,VIRTIO_F_NOTIFY_ON_EMPTY);
541
Herbert Xu97402b92008-04-18 11:24:27 +0800542 /* If we can receive ANY GSO packets, we must allocate large ones. */
543 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4)
544 || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6)
545 || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN))
546 vi->big_packets = true;
547
Rusty Russell296f96f2007-10-22 11:03:37 +1000548 /* We expect two virtqueues, receive then send. */
Rusty Russella586d4f2008-02-04 23:49:56 -0500549 vi->rvq = vdev->config->find_vq(vdev, 0, skb_recv_done);
Rusty Russell296f96f2007-10-22 11:03:37 +1000550 if (IS_ERR(vi->rvq)) {
551 err = PTR_ERR(vi->rvq);
552 goto free;
553 }
554
Rusty Russella586d4f2008-02-04 23:49:56 -0500555 vi->svq = vdev->config->find_vq(vdev, 1, skb_xmit_done);
Rusty Russell296f96f2007-10-22 11:03:37 +1000556 if (IS_ERR(vi->svq)) {
557 err = PTR_ERR(vi->svq);
558 goto free_recv;
559 }
560
561 /* Initialize our empty receive and send queues. */
562 skb_queue_head_init(&vi->recv);
563 skb_queue_head_init(&vi->send);
564
Rusty Russell11a3a152008-05-26 17:48:13 +1000565 tasklet_init(&vi->tasklet, xmit_tasklet, (unsigned long)vi);
566
Rusty Russell363f1512008-06-08 20:51:55 +1000567 if (!vi->free_in_tasklet)
568 setup_timer(&vi->xmit_free_timer, xmit_free, (unsigned long)vi);
Mark McLoughlin14c998f2008-06-08 20:50:56 +1000569
Rusty Russell296f96f2007-10-22 11:03:37 +1000570 err = register_netdev(dev);
571 if (err) {
572 pr_debug("virtio_net: registering device failed\n");
573 goto free_send;
574 }
Rusty Russellb3369c12008-02-04 23:50:02 -0500575
576 /* Last of all, set up some receive buffers. */
577 try_fill_recv(vi);
578
579 /* If we didn't even get one input buffer, we're useless. */
580 if (vi->num == 0) {
581 err = -ENOMEM;
582 goto unregister;
583 }
584
Rusty Russell296f96f2007-10-22 11:03:37 +1000585 pr_debug("virtnet: registered device %s\n", dev->name);
Rusty Russell296f96f2007-10-22 11:03:37 +1000586 return 0;
587
Rusty Russellb3369c12008-02-04 23:50:02 -0500588unregister:
589 unregister_netdev(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +1000590free_send:
591 vdev->config->del_vq(vi->svq);
592free_recv:
593 vdev->config->del_vq(vi->rvq);
594free:
595 free_netdev(dev);
596 return err;
597}
598
599static void virtnet_remove(struct virtio_device *vdev)
600{
Rusty Russell74b25532007-11-19 11:20:42 -0500601 struct virtnet_info *vi = vdev->priv;
Rusty Russellb3369c12008-02-04 23:50:02 -0500602 struct sk_buff *skb;
603
Rusty Russell6e5aa7e2008-02-04 23:50:03 -0500604 /* Stop all the virtqueues. */
605 vdev->config->reset(vdev);
606
Rusty Russell363f1512008-06-08 20:51:55 +1000607 if (!vi->free_in_tasklet)
608 del_timer_sync(&vi->xmit_free_timer);
Mark McLoughlin14c998f2008-06-08 20:50:56 +1000609
Rusty Russellb3369c12008-02-04 23:50:02 -0500610 /* Free our skbs in send and recv queues, if any. */
Rusty Russellb3369c12008-02-04 23:50:02 -0500611 while ((skb = __skb_dequeue(&vi->recv)) != NULL) {
612 kfree_skb(skb);
613 vi->num--;
614 }
Wang Chen288369c2008-05-22 18:07:43 +0800615 __skb_queue_purge(&vi->send);
Rusty Russellb3369c12008-02-04 23:50:02 -0500616
617 BUG_ON(vi->num != 0);
Rusty Russell74b25532007-11-19 11:20:42 -0500618
619 vdev->config->del_vq(vi->svq);
620 vdev->config->del_vq(vi->rvq);
621 unregister_netdev(vi->dev);
Rusty Russellfb6813f2008-07-25 12:06:01 -0500622
623 while (vi->pages)
624 __free_pages(get_a_page(vi, GFP_KERNEL), 0);
625
Rusty Russell74b25532007-11-19 11:20:42 -0500626 free_netdev(vi->dev);
Rusty Russell296f96f2007-10-22 11:03:37 +1000627}
628
629static struct virtio_device_id id_table[] = {
630 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
631 { 0 },
632};
633
Rusty Russellc45a6812008-05-02 21:50:50 -0500634static unsigned int features[] = {
Mark McLoughlin5e4fe5c2008-07-08 17:10:42 +1000635 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM,
636 VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
Rusty Russellc45a6812008-05-02 21:50:50 -0500637 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
Herbert Xu97402b92008-04-18 11:24:27 +0800638 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
639 VIRTIO_NET_F_GUEST_ECN, /* We don't yet handle UFO input. */
640 VIRTIO_F_NOTIFY_ON_EMPTY,
Rusty Russellc45a6812008-05-02 21:50:50 -0500641};
642
Rusty Russell296f96f2007-10-22 11:03:37 +1000643static struct virtio_driver virtio_net = {
Rusty Russellc45a6812008-05-02 21:50:50 -0500644 .feature_table = features,
645 .feature_table_size = ARRAY_SIZE(features),
Rusty Russell296f96f2007-10-22 11:03:37 +1000646 .driver.name = KBUILD_MODNAME,
647 .driver.owner = THIS_MODULE,
648 .id_table = id_table,
649 .probe = virtnet_probe,
650 .remove = __devexit_p(virtnet_remove),
651};
652
653static int __init init(void)
654{
655 return register_virtio_driver(&virtio_net);
656}
657
658static void __exit fini(void)
659{
660 unregister_virtio_driver(&virtio_net);
661}
662module_init(init);
663module_exit(fini);
664
665MODULE_DEVICE_TABLE(virtio, id_table);
666MODULE_DESCRIPTION("Virtio network driver");
667MODULE_LICENSE("GPL");