blob: 0886b8a2d92dc5dda7e75dfb70f41286df5c6140 [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;
64};
65
66static inline struct virtio_net_hdr *skb_vnet_hdr(struct sk_buff *skb)
67{
68 return (struct virtio_net_hdr *)skb->cb;
69}
70
71static inline void vnet_hdr_to_sg(struct scatterlist *sg, struct sk_buff *skb)
72{
73 sg_init_one(sg, skb_vnet_hdr(skb), sizeof(struct virtio_net_hdr));
74}
75
Rusty Russell2cb9c6b2008-02-04 23:50:07 -050076static void skb_xmit_done(struct virtqueue *svq)
Rusty Russell296f96f2007-10-22 11:03:37 +100077{
Rusty Russell2cb9c6b2008-02-04 23:50:07 -050078 struct virtnet_info *vi = svq->vdev->priv;
Rusty Russell296f96f2007-10-22 11:03:37 +100079
Rusty Russell2cb9c6b2008-02-04 23:50:07 -050080 /* Suppress further interrupts. */
81 svq->vq_ops->disable_cb(svq);
Rusty Russell11a3a152008-05-26 17:48:13 +100082
Rusty Russell363f1512008-06-08 20:51:55 +100083 /* We were probably waiting for more output buffers. */
Rusty Russell296f96f2007-10-22 11:03:37 +100084 netif_wake_queue(vi->dev);
Rusty Russell11a3a152008-05-26 17:48:13 +100085
86 /* Make sure we re-xmit last_xmit_skb: if there are no more packets
87 * queued, start_xmit won't be called. */
88 tasklet_schedule(&vi->tasklet);
Rusty Russell296f96f2007-10-22 11:03:37 +100089}
90
91static void receive_skb(struct net_device *dev, struct sk_buff *skb,
92 unsigned len)
93{
94 struct virtio_net_hdr *hdr = skb_vnet_hdr(skb);
Herbert Xu97402b92008-04-18 11:24:27 +080095 int err;
Rusty Russell296f96f2007-10-22 11:03:37 +100096
97 if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) {
98 pr_debug("%s: short packet %i\n", dev->name, len);
99 dev->stats.rx_length_errors++;
100 goto drop;
101 }
102 len -= sizeof(struct virtio_net_hdr);
Rusty Russell296f96f2007-10-22 11:03:37 +1000103
Herbert Xu97402b92008-04-18 11:24:27 +0800104 err = pskb_trim(skb, len);
105 if (err) {
106 pr_debug("%s: pskb_trim failed %i %d\n", dev->name, len, err);
107 dev->stats.rx_dropped++;
108 goto drop;
109 }
110 skb->truesize += skb->data_len;
Rusty Russell296f96f2007-10-22 11:03:37 +1000111 dev->stats.rx_bytes += skb->len;
112 dev->stats.rx_packets++;
113
114 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
115 pr_debug("Needs csum!\n");
Rusty Russellf35d9d82008-02-04 23:49:54 -0500116 if (!skb_partial_csum_set(skb,hdr->csum_start,hdr->csum_offset))
Rusty Russell296f96f2007-10-22 11:03:37 +1000117 goto frame_err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000118 }
119
Mark McLoughlin23cde762008-06-08 20:49:00 +1000120 skb->protocol = eth_type_trans(skb, dev);
121 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
122 ntohs(skb->protocol), skb->len, skb->pkt_type);
123
Rusty Russell296f96f2007-10-22 11:03:37 +1000124 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
125 pr_debug("GSO!\n");
Rusty Russell34a48572008-02-04 23:50:02 -0500126 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000127 case VIRTIO_NET_HDR_GSO_TCPV4:
128 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
129 break;
Rusty Russell296f96f2007-10-22 11:03:37 +1000130 case VIRTIO_NET_HDR_GSO_UDP:
131 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
132 break;
133 case VIRTIO_NET_HDR_GSO_TCPV6:
134 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
135 break;
136 default:
137 if (net_ratelimit())
138 printk(KERN_WARNING "%s: bad gso type %u.\n",
139 dev->name, hdr->gso_type);
140 goto frame_err;
141 }
142
Rusty Russell34a48572008-02-04 23:50:02 -0500143 if (hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN)
144 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
145
Rusty Russell296f96f2007-10-22 11:03:37 +1000146 skb_shinfo(skb)->gso_size = hdr->gso_size;
147 if (skb_shinfo(skb)->gso_size == 0) {
148 if (net_ratelimit())
149 printk(KERN_WARNING "%s: zero gso size.\n",
150 dev->name);
151 goto frame_err;
152 }
153
154 /* Header must be checked, and gso_segs computed. */
155 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
156 skb_shinfo(skb)->gso_segs = 0;
157 }
158
159 netif_receive_skb(skb);
160 return;
161
162frame_err:
163 dev->stats.rx_frame_errors++;
164drop:
165 dev_kfree_skb(skb);
166}
167
168static void try_fill_recv(struct virtnet_info *vi)
169{
170 struct sk_buff *skb;
Rusty Russell05271682008-05-02 21:50:45 -0500171 struct scatterlist sg[2+MAX_SKB_FRAGS];
Herbert Xu97402b92008-04-18 11:24:27 +0800172 int num, err, i;
Rusty Russell296f96f2007-10-22 11:03:37 +1000173
Rusty Russell05271682008-05-02 21:50:45 -0500174 sg_init_table(sg, 2+MAX_SKB_FRAGS);
Rusty Russell296f96f2007-10-22 11:03:37 +1000175 for (;;) {
176 skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN);
177 if (unlikely(!skb))
178 break;
179
180 skb_put(skb, MAX_PACKET_LEN);
181 vnet_hdr_to_sg(sg, skb);
Herbert Xu97402b92008-04-18 11:24:27 +0800182
183 if (vi->big_packets) {
184 for (i = 0; i < MAX_SKB_FRAGS; i++) {
185 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
186 f->page = alloc_page(GFP_ATOMIC);
187 if (!f->page)
188 break;
189
190 f->page_offset = 0;
191 f->size = PAGE_SIZE;
192
193 skb->data_len += PAGE_SIZE;
194 skb->len += PAGE_SIZE;
195
196 skb_shinfo(skb)->nr_frags++;
197 }
198 }
199
Rusty Russell296f96f2007-10-22 11:03:37 +1000200 num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
201 skb_queue_head(&vi->recv, skb);
202
203 err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, num, skb);
204 if (err) {
205 skb_unlink(skb, &vi->recv);
206 kfree_skb(skb);
207 break;
208 }
209 vi->num++;
210 }
211 if (unlikely(vi->num > vi->max))
212 vi->max = vi->num;
213 vi->rvq->vq_ops->kick(vi->rvq);
214}
215
Rusty Russell18445c42008-02-04 23:49:57 -0500216static void skb_recv_done(struct virtqueue *rvq)
Rusty Russell296f96f2007-10-22 11:03:37 +1000217{
218 struct virtnet_info *vi = rvq->vdev->priv;
Rusty Russell18445c42008-02-04 23:49:57 -0500219 /* Schedule NAPI, Suppress further interrupts if successful. */
220 if (netif_rx_schedule_prep(vi->dev, &vi->napi)) {
221 rvq->vq_ops->disable_cb(rvq);
222 __netif_rx_schedule(vi->dev, &vi->napi);
223 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000224}
225
226static int virtnet_poll(struct napi_struct *napi, int budget)
227{
228 struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi);
229 struct sk_buff *skb = NULL;
230 unsigned int len, received = 0;
231
232again:
233 while (received < budget &&
234 (skb = vi->rvq->vq_ops->get_buf(vi->rvq, &len)) != NULL) {
235 __skb_unlink(skb, &vi->recv);
236 receive_skb(vi->dev, skb, len);
237 vi->num--;
238 received++;
239 }
240
241 /* FIXME: If we oom and completely run out of inbufs, we need
242 * to start a timer trying to fill more. */
243 if (vi->num < vi->max / 2)
244 try_fill_recv(vi);
245
Rusty Russell8329d982007-11-19 11:20:43 -0500246 /* Out of packets? */
247 if (received < budget) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000248 netif_rx_complete(vi->dev, napi);
Rusty Russell18445c42008-02-04 23:49:57 -0500249 if (unlikely(!vi->rvq->vq_ops->enable_cb(vi->rvq))
Christian Borntraeger4265f162008-03-14 14:17:05 +0100250 && napi_schedule_prep(napi)) {
251 vi->rvq->vq_ops->disable_cb(vi->rvq);
252 __netif_rx_schedule(vi->dev, napi);
Rusty Russell296f96f2007-10-22 11:03:37 +1000253 goto again;
Christian Borntraeger4265f162008-03-14 14:17:05 +0100254 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000255 }
256
257 return received;
258}
259
260static void free_old_xmit_skbs(struct virtnet_info *vi)
261{
262 struct sk_buff *skb;
263 unsigned int len;
264
265 while ((skb = vi->svq->vq_ops->get_buf(vi->svq, &len)) != NULL) {
266 pr_debug("Sent skb %p\n", skb);
267 __skb_unlink(skb, &vi->send);
Rusty Russell655aa312008-05-02 21:50:43 -0500268 vi->dev->stats.tx_bytes += skb->len;
Rusty Russell296f96f2007-10-22 11:03:37 +1000269 vi->dev->stats.tx_packets++;
270 kfree_skb(skb);
271 }
272}
273
Rusty Russell363f1512008-06-08 20:51:55 +1000274/* If the virtio transport doesn't always notify us when all in-flight packets
275 * are consumed, we fall back to using this function on a timer to free them. */
Mark McLoughlin14c998f2008-06-08 20:50:56 +1000276static void xmit_free(unsigned long data)
277{
278 struct virtnet_info *vi = (void *)data;
279
280 netif_tx_lock(vi->dev);
281
282 free_old_xmit_skbs(vi);
283
284 if (!skb_queue_empty(&vi->send))
285 mod_timer(&vi->xmit_free_timer, jiffies + (HZ/10));
286
287 netif_tx_unlock(vi->dev);
288}
289
Rusty Russell99ffc692008-05-02 21:50:46 -0500290static int xmit_skb(struct virtnet_info *vi, struct sk_buff *skb)
Rusty Russell296f96f2007-10-22 11:03:37 +1000291{
Mark McLoughlin14c998f2008-06-08 20:50:56 +1000292 int num, err;
Rusty Russell05271682008-05-02 21:50:45 -0500293 struct scatterlist sg[2+MAX_SKB_FRAGS];
Rusty Russell296f96f2007-10-22 11:03:37 +1000294 struct virtio_net_hdr *hdr;
295 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
Rusty Russell296f96f2007-10-22 11:03:37 +1000296
Rusty Russell05271682008-05-02 21:50:45 -0500297 sg_init_table(sg, 2+MAX_SKB_FRAGS);
Rusty Russell4d125de2007-11-07 16:34:49 +1100298
Rusty Russell99ffc692008-05-02 21:50:46 -0500299 pr_debug("%s: xmit %p " MAC_FMT "\n", vi->dev->name, skb,
David S. Miller21f644f2008-04-08 16:50:44 -0700300 dest[0], dest[1], dest[2],
301 dest[3], dest[4], dest[5]);
Rusty Russell296f96f2007-10-22 11:03:37 +1000302
Rusty Russell296f96f2007-10-22 11:03:37 +1000303 /* Encode metadata header at front. */
304 hdr = skb_vnet_hdr(skb);
305 if (skb->ip_summed == CHECKSUM_PARTIAL) {
306 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
307 hdr->csum_start = skb->csum_start - skb_headroom(skb);
308 hdr->csum_offset = skb->csum_offset;
309 } else {
310 hdr->flags = 0;
311 hdr->csum_offset = hdr->csum_start = 0;
312 }
313
314 if (skb_is_gso(skb)) {
Rusty Russell50c8ea82008-02-04 23:50:01 -0500315 hdr->hdr_len = skb_transport_header(skb) - skb->data;
Rusty Russell296f96f2007-10-22 11:03:37 +1000316 hdr->gso_size = skb_shinfo(skb)->gso_size;
Rusty Russell34a48572008-02-04 23:50:02 -0500317 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
Rusty Russell296f96f2007-10-22 11:03:37 +1000318 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
319 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
320 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
321 else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
322 hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
323 else
324 BUG();
Rusty Russell34a48572008-02-04 23:50:02 -0500325 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
326 hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
Rusty Russell296f96f2007-10-22 11:03:37 +1000327 } else {
328 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
Rusty Russell50c8ea82008-02-04 23:50:01 -0500329 hdr->gso_size = hdr->hdr_len = 0;
Rusty Russell296f96f2007-10-22 11:03:37 +1000330 }
331
332 vnet_hdr_to_sg(sg, skb);
333 num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
Rusty Russell99ffc692008-05-02 21:50:46 -0500334
Mark McLoughlin14c998f2008-06-08 20:50:56 +1000335 err = vi->svq->vq_ops->add_buf(vi->svq, sg, num, 0, skb);
Rusty Russell363f1512008-06-08 20:51:55 +1000336 if (!err && !vi->free_in_tasklet)
Mark McLoughlin14c998f2008-06-08 20:50:56 +1000337 mod_timer(&vi->xmit_free_timer, jiffies + (HZ/10));
338
339 return err;
Rusty Russell99ffc692008-05-02 21:50:46 -0500340}
341
Rusty Russell11a3a152008-05-26 17:48:13 +1000342static void xmit_tasklet(unsigned long data)
343{
344 struct virtnet_info *vi = (void *)data;
345
346 netif_tx_lock_bh(vi->dev);
347 if (vi->last_xmit_skb && xmit_skb(vi, vi->last_xmit_skb) == 0) {
348 vi->svq->vq_ops->kick(vi->svq);
349 vi->last_xmit_skb = NULL;
350 }
Rusty Russell363f1512008-06-08 20:51:55 +1000351 if (vi->free_in_tasklet)
352 free_old_xmit_skbs(vi);
Rusty Russell11a3a152008-05-26 17:48:13 +1000353 netif_tx_unlock_bh(vi->dev);
354}
355
Rusty Russell99ffc692008-05-02 21:50:46 -0500356static int start_xmit(struct sk_buff *skb, struct net_device *dev)
357{
358 struct virtnet_info *vi = netdev_priv(dev);
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500359
360again:
361 /* Free up any pending old buffers before queueing new ones. */
362 free_old_xmit_skbs(vi);
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500363
Rusty Russell99ffc692008-05-02 21:50:46 -0500364 /* If we has a buffer left over from last time, send it now. */
Mark McLoughlin9953ca62008-05-27 12:06:26 +0100365 if (unlikely(vi->last_xmit_skb) &&
366 xmit_skb(vi, vi->last_xmit_skb) != 0)
367 goto stop_queue;
368
369 vi->last_xmit_skb = NULL;
Rusty Russell296f96f2007-10-22 11:03:37 +1000370
Rusty Russell99ffc692008-05-02 21:50:46 -0500371 /* Put new one in send queue and do transmit */
Rusty Russell7eb2e252008-05-26 17:42:42 +1000372 if (likely(skb)) {
373 __skb_queue_head(&vi->send, skb);
374 if (xmit_skb(vi, skb) != 0) {
375 vi->last_xmit_skb = skb;
376 skb = NULL;
377 goto stop_queue;
378 }
Rusty Russell99ffc692008-05-02 21:50:46 -0500379 }
380done:
381 vi->svq->vq_ops->kick(vi->svq);
382 return NETDEV_TX_OK;
383
384stop_queue:
385 pr_debug("%s: virtio not prepared to send\n", dev->name);
386 netif_stop_queue(dev);
387
388 /* Activate callback for using skbs: if this returns false it
389 * means some were used in the meantime. */
390 if (unlikely(!vi->svq->vq_ops->enable_cb(vi->svq))) {
391 vi->svq->vq_ops->disable_cb(vi->svq);
392 netif_start_queue(dev);
393 goto again;
394 }
Mark McLoughlin9953ca62008-05-27 12:06:26 +0100395 if (skb) {
396 /* Drop this skb: we only queue one. */
397 vi->dev->stats.tx_dropped++;
398 kfree_skb(skb);
399 }
Rusty Russell99ffc692008-05-02 21:50:46 -0500400 goto done;
Rusty Russell296f96f2007-10-22 11:03:37 +1000401}
402
Amit Shahda74e892008-02-29 16:24:50 +0530403#ifdef CONFIG_NET_POLL_CONTROLLER
404static void virtnet_netpoll(struct net_device *dev)
405{
406 struct virtnet_info *vi = netdev_priv(dev);
407
408 napi_schedule(&vi->napi);
409}
410#endif
411
Rusty Russell296f96f2007-10-22 11:03:37 +1000412static int virtnet_open(struct net_device *dev)
413{
414 struct virtnet_info *vi = netdev_priv(dev);
415
Rusty Russell296f96f2007-10-22 11:03:37 +1000416 napi_enable(&vi->napi);
Rusty Russella48bd8f2008-02-04 23:50:07 -0500417
418 /* If all buffers were filled by other side before we napi_enabled, we
419 * won't get another interrupt, so process any outstanding packets
Christian Borntraeger370076d2008-02-06 08:50:11 +0100420 * now. virtnet_poll wants re-enable the queue, so we disable here.
421 * We synchronize against interrupts via NAPI_STATE_SCHED */
422 if (netif_rx_schedule_prep(dev, &vi->napi)) {
423 vi->rvq->vq_ops->disable_cb(vi->rvq);
424 __netif_rx_schedule(dev, &vi->napi);
425 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000426 return 0;
427}
428
429static int virtnet_close(struct net_device *dev)
430{
431 struct virtnet_info *vi = netdev_priv(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +1000432
433 napi_disable(&vi->napi);
434
Rusty Russell296f96f2007-10-22 11:03:37 +1000435 return 0;
436}
437
Herbert Xua9ea3fc2008-04-18 11:21:42 +0800438static int virtnet_set_tx_csum(struct net_device *dev, u32 data)
439{
440 struct virtnet_info *vi = netdev_priv(dev);
441 struct virtio_device *vdev = vi->vdev;
442
443 if (data && !virtio_has_feature(vdev, VIRTIO_NET_F_CSUM))
444 return -ENOSYS;
445
446 return ethtool_op_set_tx_hw_csum(dev, data);
447}
448
449static struct ethtool_ops virtnet_ethtool_ops = {
450 .set_tx_csum = virtnet_set_tx_csum,
451 .set_sg = ethtool_op_set_sg,
452};
453
Rusty Russell296f96f2007-10-22 11:03:37 +1000454static int virtnet_probe(struct virtio_device *vdev)
455{
456 int err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000457 struct net_device *dev;
458 struct virtnet_info *vi;
Rusty Russell296f96f2007-10-22 11:03:37 +1000459
460 /* Allocate ourselves a network device with room for our info */
461 dev = alloc_etherdev(sizeof(struct virtnet_info));
462 if (!dev)
463 return -ENOMEM;
464
465 /* Set up network device as normal. */
Rusty Russell296f96f2007-10-22 11:03:37 +1000466 dev->open = virtnet_open;
467 dev->stop = virtnet_close;
468 dev->hard_start_xmit = start_xmit;
469 dev->features = NETIF_F_HIGHDMA;
Amit Shahda74e892008-02-29 16:24:50 +0530470#ifdef CONFIG_NET_POLL_CONTROLLER
471 dev->poll_controller = virtnet_netpoll;
472#endif
Herbert Xua9ea3fc2008-04-18 11:21:42 +0800473 SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops);
Rusty Russell296f96f2007-10-22 11:03:37 +1000474 SET_NETDEV_DEV(dev, &vdev->dev);
475
476 /* Do we support "hardware" checksums? */
Rusty Russellc45a6812008-05-02 21:50:50 -0500477 if (csum && virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000478 /* This opens up the world of extra features. */
479 dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
Rusty Russellc45a6812008-05-02 21:50:50 -0500480 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
Rusty Russell34a48572008-02-04 23:50:02 -0500481 dev->features |= NETIF_F_TSO | NETIF_F_UFO
482 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
483 }
Rusty Russell5539ae92008-05-02 21:50:46 -0500484 /* Individual feature bits: what can host handle? */
Rusty Russellc45a6812008-05-02 21:50:50 -0500485 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
Rusty Russell5539ae92008-05-02 21:50:46 -0500486 dev->features |= NETIF_F_TSO;
Rusty Russellc45a6812008-05-02 21:50:50 -0500487 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
Rusty Russell5539ae92008-05-02 21:50:46 -0500488 dev->features |= NETIF_F_TSO6;
Rusty Russellc45a6812008-05-02 21:50:50 -0500489 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
Rusty Russell5539ae92008-05-02 21:50:46 -0500490 dev->features |= NETIF_F_TSO_ECN;
Rusty Russellc45a6812008-05-02 21:50:50 -0500491 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
Rusty Russell5539ae92008-05-02 21:50:46 -0500492 dev->features |= NETIF_F_UFO;
Rusty Russell296f96f2007-10-22 11:03:37 +1000493 }
494
495 /* Configuration may specify what MAC to use. Otherwise random. */
Rusty Russellc45a6812008-05-02 21:50:50 -0500496 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
Rusty Russella586d4f2008-02-04 23:49:56 -0500497 vdev->config->get(vdev,
498 offsetof(struct virtio_net_config, mac),
499 dev->dev_addr, dev->addr_len);
Rusty Russell296f96f2007-10-22 11:03:37 +1000500 } else
501 random_ether_addr(dev->dev_addr);
502
503 /* Set up our device-specific information */
504 vi = netdev_priv(dev);
Dor Laor6c0cd7c2007-12-16 15:19:43 +0200505 netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight);
Rusty Russell296f96f2007-10-22 11:03:37 +1000506 vi->dev = dev;
507 vi->vdev = vdev;
Christian Borntraegerd9d5dcc2008-02-18 10:02:51 +0100508 vdev->priv = vi;
Rusty Russell296f96f2007-10-22 11:03:37 +1000509
Rusty Russell363f1512008-06-08 20:51:55 +1000510 /* If they give us a callback when all buffers are done, we don't need
511 * the timer. */
512 vi->free_in_tasklet = virtio_has_feature(vdev,VIRTIO_F_NOTIFY_ON_EMPTY);
513
Herbert Xu97402b92008-04-18 11:24:27 +0800514 /* If we can receive ANY GSO packets, we must allocate large ones. */
515 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4)
516 || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6)
517 || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN))
518 vi->big_packets = true;
519
Rusty Russell296f96f2007-10-22 11:03:37 +1000520 /* We expect two virtqueues, receive then send. */
Rusty Russella586d4f2008-02-04 23:49:56 -0500521 vi->rvq = vdev->config->find_vq(vdev, 0, skb_recv_done);
Rusty Russell296f96f2007-10-22 11:03:37 +1000522 if (IS_ERR(vi->rvq)) {
523 err = PTR_ERR(vi->rvq);
524 goto free;
525 }
526
Rusty Russella586d4f2008-02-04 23:49:56 -0500527 vi->svq = vdev->config->find_vq(vdev, 1, skb_xmit_done);
Rusty Russell296f96f2007-10-22 11:03:37 +1000528 if (IS_ERR(vi->svq)) {
529 err = PTR_ERR(vi->svq);
530 goto free_recv;
531 }
532
533 /* Initialize our empty receive and send queues. */
534 skb_queue_head_init(&vi->recv);
535 skb_queue_head_init(&vi->send);
536
Rusty Russell11a3a152008-05-26 17:48:13 +1000537 tasklet_init(&vi->tasklet, xmit_tasklet, (unsigned long)vi);
538
Rusty Russell363f1512008-06-08 20:51:55 +1000539 if (!vi->free_in_tasklet)
540 setup_timer(&vi->xmit_free_timer, xmit_free, (unsigned long)vi);
Mark McLoughlin14c998f2008-06-08 20:50:56 +1000541
Rusty Russell296f96f2007-10-22 11:03:37 +1000542 err = register_netdev(dev);
543 if (err) {
544 pr_debug("virtio_net: registering device failed\n");
545 goto free_send;
546 }
Rusty Russellb3369c12008-02-04 23:50:02 -0500547
548 /* Last of all, set up some receive buffers. */
549 try_fill_recv(vi);
550
551 /* If we didn't even get one input buffer, we're useless. */
552 if (vi->num == 0) {
553 err = -ENOMEM;
554 goto unregister;
555 }
556
Rusty Russell296f96f2007-10-22 11:03:37 +1000557 pr_debug("virtnet: registered device %s\n", dev->name);
Rusty Russell296f96f2007-10-22 11:03:37 +1000558 return 0;
559
Rusty Russellb3369c12008-02-04 23:50:02 -0500560unregister:
561 unregister_netdev(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +1000562free_send:
563 vdev->config->del_vq(vi->svq);
564free_recv:
565 vdev->config->del_vq(vi->rvq);
566free:
567 free_netdev(dev);
568 return err;
569}
570
571static void virtnet_remove(struct virtio_device *vdev)
572{
Rusty Russell74b25532007-11-19 11:20:42 -0500573 struct virtnet_info *vi = vdev->priv;
Rusty Russellb3369c12008-02-04 23:50:02 -0500574 struct sk_buff *skb;
575
Rusty Russell6e5aa7e2008-02-04 23:50:03 -0500576 /* Stop all the virtqueues. */
577 vdev->config->reset(vdev);
578
Rusty Russell363f1512008-06-08 20:51:55 +1000579 if (!vi->free_in_tasklet)
580 del_timer_sync(&vi->xmit_free_timer);
Mark McLoughlin14c998f2008-06-08 20:50:56 +1000581
Rusty Russellb3369c12008-02-04 23:50:02 -0500582 /* Free our skbs in send and recv queues, if any. */
Rusty Russellb3369c12008-02-04 23:50:02 -0500583 while ((skb = __skb_dequeue(&vi->recv)) != NULL) {
584 kfree_skb(skb);
585 vi->num--;
586 }
Wang Chen288369c2008-05-22 18:07:43 +0800587 __skb_queue_purge(&vi->send);
Rusty Russellb3369c12008-02-04 23:50:02 -0500588
589 BUG_ON(vi->num != 0);
Rusty Russell74b25532007-11-19 11:20:42 -0500590
591 vdev->config->del_vq(vi->svq);
592 vdev->config->del_vq(vi->rvq);
593 unregister_netdev(vi->dev);
594 free_netdev(vi->dev);
Rusty Russell296f96f2007-10-22 11:03:37 +1000595}
596
597static struct virtio_device_id id_table[] = {
598 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
599 { 0 },
600};
601
Rusty Russellc45a6812008-05-02 21:50:50 -0500602static unsigned int features[] = {
Mark McLoughlin5e4fe5c2008-07-08 17:10:42 +1000603 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM,
604 VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
Rusty Russellc45a6812008-05-02 21:50:50 -0500605 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
Herbert Xu97402b92008-04-18 11:24:27 +0800606 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
607 VIRTIO_NET_F_GUEST_ECN, /* We don't yet handle UFO input. */
608 VIRTIO_F_NOTIFY_ON_EMPTY,
Rusty Russellc45a6812008-05-02 21:50:50 -0500609};
610
Rusty Russell296f96f2007-10-22 11:03:37 +1000611static struct virtio_driver virtio_net = {
Rusty Russellc45a6812008-05-02 21:50:50 -0500612 .feature_table = features,
613 .feature_table_size = ARRAY_SIZE(features),
Rusty Russell296f96f2007-10-22 11:03:37 +1000614 .driver.name = KBUILD_MODNAME,
615 .driver.owner = THIS_MODULE,
616 .id_table = id_table,
617 .probe = virtnet_probe,
618 .remove = __devexit_p(virtnet_remove),
619};
620
621static int __init init(void)
622{
623 return register_virtio_driver(&virtio_net);
624}
625
626static void __exit fini(void)
627{
628 unregister_virtio_driver(&virtio_net);
629}
630module_init(init);
631module_exit(fini);
632
633MODULE_DEVICE_TABLE(virtio, id_table);
634MODULE_DESCRIPTION("Virtio network driver");
635MODULE_LICENSE("GPL");