blob: ce37a7e9541c5024d90a274e4f346f00f03699fa [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
Rusty Russell296f96f2007-10-22 11:03:37 +100058 /* Receive & send queues. */
59 struct sk_buff_head recv;
60 struct sk_buff_head send;
61};
62
63static inline struct virtio_net_hdr *skb_vnet_hdr(struct sk_buff *skb)
64{
65 return (struct virtio_net_hdr *)skb->cb;
66}
67
68static inline void vnet_hdr_to_sg(struct scatterlist *sg, struct sk_buff *skb)
69{
70 sg_init_one(sg, skb_vnet_hdr(skb), sizeof(struct virtio_net_hdr));
71}
72
Rusty Russell2cb9c6b2008-02-04 23:50:07 -050073static void skb_xmit_done(struct virtqueue *svq)
Rusty Russell296f96f2007-10-22 11:03:37 +100074{
Rusty Russell2cb9c6b2008-02-04 23:50:07 -050075 struct virtnet_info *vi = svq->vdev->priv;
Rusty Russell296f96f2007-10-22 11:03:37 +100076
Rusty Russell2cb9c6b2008-02-04 23:50:07 -050077 /* Suppress further interrupts. */
78 svq->vq_ops->disable_cb(svq);
Rusty Russell11a3a152008-05-26 17:48:13 +100079
Rusty Russell363f1512008-06-08 20:51:55 +100080 /* We were probably waiting for more output buffers. */
Rusty Russell296f96f2007-10-22 11:03:37 +100081 netif_wake_queue(vi->dev);
Rusty Russell11a3a152008-05-26 17:48:13 +100082
83 /* Make sure we re-xmit last_xmit_skb: if there are no more packets
84 * queued, start_xmit won't be called. */
85 tasklet_schedule(&vi->tasklet);
Rusty Russell296f96f2007-10-22 11:03:37 +100086}
87
88static void receive_skb(struct net_device *dev, struct sk_buff *skb,
89 unsigned len)
90{
91 struct virtio_net_hdr *hdr = skb_vnet_hdr(skb);
92
93 if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) {
94 pr_debug("%s: short packet %i\n", dev->name, len);
95 dev->stats.rx_length_errors++;
96 goto drop;
97 }
98 len -= sizeof(struct virtio_net_hdr);
99 BUG_ON(len > MAX_PACKET_LEN);
100
101 skb_trim(skb, len);
Mark McLoughlin23cde762008-06-08 20:49:00 +1000102
Rusty Russell296f96f2007-10-22 11:03:37 +1000103 dev->stats.rx_bytes += skb->len;
104 dev->stats.rx_packets++;
105
106 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
107 pr_debug("Needs csum!\n");
Rusty Russellf35d9d82008-02-04 23:49:54 -0500108 if (!skb_partial_csum_set(skb,hdr->csum_start,hdr->csum_offset))
Rusty Russell296f96f2007-10-22 11:03:37 +1000109 goto frame_err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000110 }
111
Mark McLoughlin23cde762008-06-08 20:49:00 +1000112 skb->protocol = eth_type_trans(skb, dev);
113 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
114 ntohs(skb->protocol), skb->len, skb->pkt_type);
115
Rusty Russell296f96f2007-10-22 11:03:37 +1000116 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
117 pr_debug("GSO!\n");
Rusty Russell34a48572008-02-04 23:50:02 -0500118 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000119 case VIRTIO_NET_HDR_GSO_TCPV4:
120 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
121 break;
Rusty Russell296f96f2007-10-22 11:03:37 +1000122 case VIRTIO_NET_HDR_GSO_UDP:
123 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
124 break;
125 case VIRTIO_NET_HDR_GSO_TCPV6:
126 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
127 break;
128 default:
129 if (net_ratelimit())
130 printk(KERN_WARNING "%s: bad gso type %u.\n",
131 dev->name, hdr->gso_type);
132 goto frame_err;
133 }
134
Rusty Russell34a48572008-02-04 23:50:02 -0500135 if (hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN)
136 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
137
Rusty Russell296f96f2007-10-22 11:03:37 +1000138 skb_shinfo(skb)->gso_size = hdr->gso_size;
139 if (skb_shinfo(skb)->gso_size == 0) {
140 if (net_ratelimit())
141 printk(KERN_WARNING "%s: zero gso size.\n",
142 dev->name);
143 goto frame_err;
144 }
145
146 /* Header must be checked, and gso_segs computed. */
147 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
148 skb_shinfo(skb)->gso_segs = 0;
149 }
150
151 netif_receive_skb(skb);
152 return;
153
154frame_err:
155 dev->stats.rx_frame_errors++;
156drop:
157 dev_kfree_skb(skb);
158}
159
160static void try_fill_recv(struct virtnet_info *vi)
161{
162 struct sk_buff *skb;
Rusty Russell05271682008-05-02 21:50:45 -0500163 struct scatterlist sg[2+MAX_SKB_FRAGS];
Rusty Russell296f96f2007-10-22 11:03:37 +1000164 int num, err;
165
Rusty Russell05271682008-05-02 21:50:45 -0500166 sg_init_table(sg, 2+MAX_SKB_FRAGS);
Rusty Russell296f96f2007-10-22 11:03:37 +1000167 for (;;) {
168 skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN);
169 if (unlikely(!skb))
170 break;
171
172 skb_put(skb, MAX_PACKET_LEN);
173 vnet_hdr_to_sg(sg, skb);
174 num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
175 skb_queue_head(&vi->recv, skb);
176
177 err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, num, skb);
178 if (err) {
179 skb_unlink(skb, &vi->recv);
180 kfree_skb(skb);
181 break;
182 }
183 vi->num++;
184 }
185 if (unlikely(vi->num > vi->max))
186 vi->max = vi->num;
187 vi->rvq->vq_ops->kick(vi->rvq);
188}
189
Rusty Russell18445c42008-02-04 23:49:57 -0500190static void skb_recv_done(struct virtqueue *rvq)
Rusty Russell296f96f2007-10-22 11:03:37 +1000191{
192 struct virtnet_info *vi = rvq->vdev->priv;
Rusty Russell18445c42008-02-04 23:49:57 -0500193 /* Schedule NAPI, Suppress further interrupts if successful. */
194 if (netif_rx_schedule_prep(vi->dev, &vi->napi)) {
195 rvq->vq_ops->disable_cb(rvq);
196 __netif_rx_schedule(vi->dev, &vi->napi);
197 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000198}
199
200static int virtnet_poll(struct napi_struct *napi, int budget)
201{
202 struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi);
203 struct sk_buff *skb = NULL;
204 unsigned int len, received = 0;
205
206again:
207 while (received < budget &&
208 (skb = vi->rvq->vq_ops->get_buf(vi->rvq, &len)) != NULL) {
209 __skb_unlink(skb, &vi->recv);
210 receive_skb(vi->dev, skb, len);
211 vi->num--;
212 received++;
213 }
214
215 /* FIXME: If we oom and completely run out of inbufs, we need
216 * to start a timer trying to fill more. */
217 if (vi->num < vi->max / 2)
218 try_fill_recv(vi);
219
Rusty Russell8329d982007-11-19 11:20:43 -0500220 /* Out of packets? */
221 if (received < budget) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000222 netif_rx_complete(vi->dev, napi);
Rusty Russell18445c42008-02-04 23:49:57 -0500223 if (unlikely(!vi->rvq->vq_ops->enable_cb(vi->rvq))
Christian Borntraeger4265f162008-03-14 14:17:05 +0100224 && napi_schedule_prep(napi)) {
225 vi->rvq->vq_ops->disable_cb(vi->rvq);
226 __netif_rx_schedule(vi->dev, napi);
Rusty Russell296f96f2007-10-22 11:03:37 +1000227 goto again;
Christian Borntraeger4265f162008-03-14 14:17:05 +0100228 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000229 }
230
231 return received;
232}
233
234static void free_old_xmit_skbs(struct virtnet_info *vi)
235{
236 struct sk_buff *skb;
237 unsigned int len;
238
239 while ((skb = vi->svq->vq_ops->get_buf(vi->svq, &len)) != NULL) {
240 pr_debug("Sent skb %p\n", skb);
241 __skb_unlink(skb, &vi->send);
Rusty Russell655aa312008-05-02 21:50:43 -0500242 vi->dev->stats.tx_bytes += skb->len;
Rusty Russell296f96f2007-10-22 11:03:37 +1000243 vi->dev->stats.tx_packets++;
244 kfree_skb(skb);
245 }
246}
247
Rusty Russell363f1512008-06-08 20:51:55 +1000248/* If the virtio transport doesn't always notify us when all in-flight packets
249 * are consumed, we fall back to using this function on a timer to free them. */
Mark McLoughlin14c998f2008-06-08 20:50:56 +1000250static void xmit_free(unsigned long data)
251{
252 struct virtnet_info *vi = (void *)data;
253
254 netif_tx_lock(vi->dev);
255
256 free_old_xmit_skbs(vi);
257
258 if (!skb_queue_empty(&vi->send))
259 mod_timer(&vi->xmit_free_timer, jiffies + (HZ/10));
260
261 netif_tx_unlock(vi->dev);
262}
263
Rusty Russell99ffc692008-05-02 21:50:46 -0500264static int xmit_skb(struct virtnet_info *vi, struct sk_buff *skb)
Rusty Russell296f96f2007-10-22 11:03:37 +1000265{
Mark McLoughlin14c998f2008-06-08 20:50:56 +1000266 int num, err;
Rusty Russell05271682008-05-02 21:50:45 -0500267 struct scatterlist sg[2+MAX_SKB_FRAGS];
Rusty Russell296f96f2007-10-22 11:03:37 +1000268 struct virtio_net_hdr *hdr;
269 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
Rusty Russell296f96f2007-10-22 11:03:37 +1000270
Rusty Russell05271682008-05-02 21:50:45 -0500271 sg_init_table(sg, 2+MAX_SKB_FRAGS);
Rusty Russell4d125de2007-11-07 16:34:49 +1100272
Rusty Russell99ffc692008-05-02 21:50:46 -0500273 pr_debug("%s: xmit %p " MAC_FMT "\n", vi->dev->name, skb,
David S. Miller21f644f2008-04-08 16:50:44 -0700274 dest[0], dest[1], dest[2],
275 dest[3], dest[4], dest[5]);
Rusty Russell296f96f2007-10-22 11:03:37 +1000276
Rusty Russell296f96f2007-10-22 11:03:37 +1000277 /* Encode metadata header at front. */
278 hdr = skb_vnet_hdr(skb);
279 if (skb->ip_summed == CHECKSUM_PARTIAL) {
280 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
281 hdr->csum_start = skb->csum_start - skb_headroom(skb);
282 hdr->csum_offset = skb->csum_offset;
283 } else {
284 hdr->flags = 0;
285 hdr->csum_offset = hdr->csum_start = 0;
286 }
287
288 if (skb_is_gso(skb)) {
Rusty Russell50c8ea82008-02-04 23:50:01 -0500289 hdr->hdr_len = skb_transport_header(skb) - skb->data;
Rusty Russell296f96f2007-10-22 11:03:37 +1000290 hdr->gso_size = skb_shinfo(skb)->gso_size;
Rusty Russell34a48572008-02-04 23:50:02 -0500291 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
Rusty Russell296f96f2007-10-22 11:03:37 +1000292 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
293 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
294 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
295 else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
296 hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
297 else
298 BUG();
Rusty Russell34a48572008-02-04 23:50:02 -0500299 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
300 hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
Rusty Russell296f96f2007-10-22 11:03:37 +1000301 } else {
302 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
Rusty Russell50c8ea82008-02-04 23:50:01 -0500303 hdr->gso_size = hdr->hdr_len = 0;
Rusty Russell296f96f2007-10-22 11:03:37 +1000304 }
305
306 vnet_hdr_to_sg(sg, skb);
307 num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
Rusty Russell99ffc692008-05-02 21:50:46 -0500308
Mark McLoughlin14c998f2008-06-08 20:50:56 +1000309 err = vi->svq->vq_ops->add_buf(vi->svq, sg, num, 0, skb);
Rusty Russell363f1512008-06-08 20:51:55 +1000310 if (!err && !vi->free_in_tasklet)
Mark McLoughlin14c998f2008-06-08 20:50:56 +1000311 mod_timer(&vi->xmit_free_timer, jiffies + (HZ/10));
312
313 return err;
Rusty Russell99ffc692008-05-02 21:50:46 -0500314}
315
Rusty Russell11a3a152008-05-26 17:48:13 +1000316static void xmit_tasklet(unsigned long data)
317{
318 struct virtnet_info *vi = (void *)data;
319
320 netif_tx_lock_bh(vi->dev);
321 if (vi->last_xmit_skb && xmit_skb(vi, vi->last_xmit_skb) == 0) {
322 vi->svq->vq_ops->kick(vi->svq);
323 vi->last_xmit_skb = NULL;
324 }
Rusty Russell363f1512008-06-08 20:51:55 +1000325 if (vi->free_in_tasklet)
326 free_old_xmit_skbs(vi);
Rusty Russell11a3a152008-05-26 17:48:13 +1000327 netif_tx_unlock_bh(vi->dev);
328}
329
Rusty Russell99ffc692008-05-02 21:50:46 -0500330static int start_xmit(struct sk_buff *skb, struct net_device *dev)
331{
332 struct virtnet_info *vi = netdev_priv(dev);
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500333
334again:
335 /* Free up any pending old buffers before queueing new ones. */
336 free_old_xmit_skbs(vi);
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500337
Rusty Russell99ffc692008-05-02 21:50:46 -0500338 /* If we has a buffer left over from last time, send it now. */
Mark McLoughlin9953ca62008-05-27 12:06:26 +0100339 if (unlikely(vi->last_xmit_skb) &&
340 xmit_skb(vi, vi->last_xmit_skb) != 0)
341 goto stop_queue;
342
343 vi->last_xmit_skb = NULL;
Rusty Russell296f96f2007-10-22 11:03:37 +1000344
Rusty Russell99ffc692008-05-02 21:50:46 -0500345 /* Put new one in send queue and do transmit */
Rusty Russell7eb2e252008-05-26 17:42:42 +1000346 if (likely(skb)) {
347 __skb_queue_head(&vi->send, skb);
348 if (xmit_skb(vi, skb) != 0) {
349 vi->last_xmit_skb = skb;
350 skb = NULL;
351 goto stop_queue;
352 }
Rusty Russell99ffc692008-05-02 21:50:46 -0500353 }
354done:
355 vi->svq->vq_ops->kick(vi->svq);
356 return NETDEV_TX_OK;
357
358stop_queue:
359 pr_debug("%s: virtio not prepared to send\n", dev->name);
360 netif_stop_queue(dev);
361
362 /* Activate callback for using skbs: if this returns false it
363 * means some were used in the meantime. */
364 if (unlikely(!vi->svq->vq_ops->enable_cb(vi->svq))) {
365 vi->svq->vq_ops->disable_cb(vi->svq);
366 netif_start_queue(dev);
367 goto again;
368 }
Mark McLoughlin9953ca62008-05-27 12:06:26 +0100369 if (skb) {
370 /* Drop this skb: we only queue one. */
371 vi->dev->stats.tx_dropped++;
372 kfree_skb(skb);
373 }
Rusty Russell99ffc692008-05-02 21:50:46 -0500374 goto done;
Rusty Russell296f96f2007-10-22 11:03:37 +1000375}
376
Amit Shahda74e892008-02-29 16:24:50 +0530377#ifdef CONFIG_NET_POLL_CONTROLLER
378static void virtnet_netpoll(struct net_device *dev)
379{
380 struct virtnet_info *vi = netdev_priv(dev);
381
382 napi_schedule(&vi->napi);
383}
384#endif
385
Rusty Russell296f96f2007-10-22 11:03:37 +1000386static int virtnet_open(struct net_device *dev)
387{
388 struct virtnet_info *vi = netdev_priv(dev);
389
Rusty Russell296f96f2007-10-22 11:03:37 +1000390 napi_enable(&vi->napi);
Rusty Russella48bd8f2008-02-04 23:50:07 -0500391
392 /* If all buffers were filled by other side before we napi_enabled, we
393 * won't get another interrupt, so process any outstanding packets
Christian Borntraeger370076d2008-02-06 08:50:11 +0100394 * now. virtnet_poll wants re-enable the queue, so we disable here.
395 * We synchronize against interrupts via NAPI_STATE_SCHED */
396 if (netif_rx_schedule_prep(dev, &vi->napi)) {
397 vi->rvq->vq_ops->disable_cb(vi->rvq);
398 __netif_rx_schedule(dev, &vi->napi);
399 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000400 return 0;
401}
402
403static int virtnet_close(struct net_device *dev)
404{
405 struct virtnet_info *vi = netdev_priv(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +1000406
407 napi_disable(&vi->napi);
408
Rusty Russell296f96f2007-10-22 11:03:37 +1000409 return 0;
410}
411
Herbert Xua9ea3fc2008-04-18 11:21:42 +0800412static int virtnet_set_tx_csum(struct net_device *dev, u32 data)
413{
414 struct virtnet_info *vi = netdev_priv(dev);
415 struct virtio_device *vdev = vi->vdev;
416
417 if (data && !virtio_has_feature(vdev, VIRTIO_NET_F_CSUM))
418 return -ENOSYS;
419
420 return ethtool_op_set_tx_hw_csum(dev, data);
421}
422
423static struct ethtool_ops virtnet_ethtool_ops = {
424 .set_tx_csum = virtnet_set_tx_csum,
425 .set_sg = ethtool_op_set_sg,
426};
427
Rusty Russell296f96f2007-10-22 11:03:37 +1000428static int virtnet_probe(struct virtio_device *vdev)
429{
430 int err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000431 struct net_device *dev;
432 struct virtnet_info *vi;
Rusty Russell296f96f2007-10-22 11:03:37 +1000433
434 /* Allocate ourselves a network device with room for our info */
435 dev = alloc_etherdev(sizeof(struct virtnet_info));
436 if (!dev)
437 return -ENOMEM;
438
439 /* Set up network device as normal. */
Rusty Russell296f96f2007-10-22 11:03:37 +1000440 dev->open = virtnet_open;
441 dev->stop = virtnet_close;
442 dev->hard_start_xmit = start_xmit;
443 dev->features = NETIF_F_HIGHDMA;
Amit Shahda74e892008-02-29 16:24:50 +0530444#ifdef CONFIG_NET_POLL_CONTROLLER
445 dev->poll_controller = virtnet_netpoll;
446#endif
Herbert Xua9ea3fc2008-04-18 11:21:42 +0800447 SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops);
Rusty Russell296f96f2007-10-22 11:03:37 +1000448 SET_NETDEV_DEV(dev, &vdev->dev);
449
450 /* Do we support "hardware" checksums? */
Rusty Russellc45a6812008-05-02 21:50:50 -0500451 if (csum && virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000452 /* This opens up the world of extra features. */
453 dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
Rusty Russellc45a6812008-05-02 21:50:50 -0500454 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
Rusty Russell34a48572008-02-04 23:50:02 -0500455 dev->features |= NETIF_F_TSO | NETIF_F_UFO
456 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
457 }
Rusty Russell5539ae92008-05-02 21:50:46 -0500458 /* Individual feature bits: what can host handle? */
Rusty Russellc45a6812008-05-02 21:50:50 -0500459 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
Rusty Russell5539ae92008-05-02 21:50:46 -0500460 dev->features |= NETIF_F_TSO;
Rusty Russellc45a6812008-05-02 21:50:50 -0500461 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
Rusty Russell5539ae92008-05-02 21:50:46 -0500462 dev->features |= NETIF_F_TSO6;
Rusty Russellc45a6812008-05-02 21:50:50 -0500463 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
Rusty Russell5539ae92008-05-02 21:50:46 -0500464 dev->features |= NETIF_F_TSO_ECN;
Rusty Russellc45a6812008-05-02 21:50:50 -0500465 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
Rusty Russell5539ae92008-05-02 21:50:46 -0500466 dev->features |= NETIF_F_UFO;
Rusty Russell296f96f2007-10-22 11:03:37 +1000467 }
468
469 /* Configuration may specify what MAC to use. Otherwise random. */
Rusty Russellc45a6812008-05-02 21:50:50 -0500470 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
Rusty Russella586d4f2008-02-04 23:49:56 -0500471 vdev->config->get(vdev,
472 offsetof(struct virtio_net_config, mac),
473 dev->dev_addr, dev->addr_len);
Rusty Russell296f96f2007-10-22 11:03:37 +1000474 } else
475 random_ether_addr(dev->dev_addr);
476
477 /* Set up our device-specific information */
478 vi = netdev_priv(dev);
Dor Laor6c0cd7c2007-12-16 15:19:43 +0200479 netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight);
Rusty Russell296f96f2007-10-22 11:03:37 +1000480 vi->dev = dev;
481 vi->vdev = vdev;
Christian Borntraegerd9d5dcc2008-02-18 10:02:51 +0100482 vdev->priv = vi;
Rusty Russell296f96f2007-10-22 11:03:37 +1000483
Rusty Russell363f1512008-06-08 20:51:55 +1000484 /* If they give us a callback when all buffers are done, we don't need
485 * the timer. */
486 vi->free_in_tasklet = virtio_has_feature(vdev,VIRTIO_F_NOTIFY_ON_EMPTY);
487
Rusty Russell296f96f2007-10-22 11:03:37 +1000488 /* We expect two virtqueues, receive then send. */
Rusty Russella586d4f2008-02-04 23:49:56 -0500489 vi->rvq = vdev->config->find_vq(vdev, 0, skb_recv_done);
Rusty Russell296f96f2007-10-22 11:03:37 +1000490 if (IS_ERR(vi->rvq)) {
491 err = PTR_ERR(vi->rvq);
492 goto free;
493 }
494
Rusty Russella586d4f2008-02-04 23:49:56 -0500495 vi->svq = vdev->config->find_vq(vdev, 1, skb_xmit_done);
Rusty Russell296f96f2007-10-22 11:03:37 +1000496 if (IS_ERR(vi->svq)) {
497 err = PTR_ERR(vi->svq);
498 goto free_recv;
499 }
500
501 /* Initialize our empty receive and send queues. */
502 skb_queue_head_init(&vi->recv);
503 skb_queue_head_init(&vi->send);
504
Rusty Russell11a3a152008-05-26 17:48:13 +1000505 tasklet_init(&vi->tasklet, xmit_tasklet, (unsigned long)vi);
506
Rusty Russell363f1512008-06-08 20:51:55 +1000507 if (!vi->free_in_tasklet)
508 setup_timer(&vi->xmit_free_timer, xmit_free, (unsigned long)vi);
Mark McLoughlin14c998f2008-06-08 20:50:56 +1000509
Rusty Russell296f96f2007-10-22 11:03:37 +1000510 err = register_netdev(dev);
511 if (err) {
512 pr_debug("virtio_net: registering device failed\n");
513 goto free_send;
514 }
Rusty Russellb3369c12008-02-04 23:50:02 -0500515
516 /* Last of all, set up some receive buffers. */
517 try_fill_recv(vi);
518
519 /* If we didn't even get one input buffer, we're useless. */
520 if (vi->num == 0) {
521 err = -ENOMEM;
522 goto unregister;
523 }
524
Rusty Russell296f96f2007-10-22 11:03:37 +1000525 pr_debug("virtnet: registered device %s\n", dev->name);
Rusty Russell296f96f2007-10-22 11:03:37 +1000526 return 0;
527
Rusty Russellb3369c12008-02-04 23:50:02 -0500528unregister:
529 unregister_netdev(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +1000530free_send:
531 vdev->config->del_vq(vi->svq);
532free_recv:
533 vdev->config->del_vq(vi->rvq);
534free:
535 free_netdev(dev);
536 return err;
537}
538
539static void virtnet_remove(struct virtio_device *vdev)
540{
Rusty Russell74b25532007-11-19 11:20:42 -0500541 struct virtnet_info *vi = vdev->priv;
Rusty Russellb3369c12008-02-04 23:50:02 -0500542 struct sk_buff *skb;
543
Rusty Russell6e5aa7e2008-02-04 23:50:03 -0500544 /* Stop all the virtqueues. */
545 vdev->config->reset(vdev);
546
Rusty Russell363f1512008-06-08 20:51:55 +1000547 if (!vi->free_in_tasklet)
548 del_timer_sync(&vi->xmit_free_timer);
Mark McLoughlin14c998f2008-06-08 20:50:56 +1000549
Rusty Russellb3369c12008-02-04 23:50:02 -0500550 /* Free our skbs in send and recv queues, if any. */
Rusty Russellb3369c12008-02-04 23:50:02 -0500551 while ((skb = __skb_dequeue(&vi->recv)) != NULL) {
552 kfree_skb(skb);
553 vi->num--;
554 }
Wang Chen288369c2008-05-22 18:07:43 +0800555 __skb_queue_purge(&vi->send);
Rusty Russellb3369c12008-02-04 23:50:02 -0500556
557 BUG_ON(vi->num != 0);
Rusty Russell74b25532007-11-19 11:20:42 -0500558
559 vdev->config->del_vq(vi->svq);
560 vdev->config->del_vq(vi->rvq);
561 unregister_netdev(vi->dev);
562 free_netdev(vi->dev);
Rusty Russell296f96f2007-10-22 11:03:37 +1000563}
564
565static struct virtio_device_id id_table[] = {
566 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
567 { 0 },
568};
569
Rusty Russellc45a6812008-05-02 21:50:50 -0500570static unsigned int features[] = {
Mark McLoughlin5e4fe5c2008-07-08 17:10:42 +1000571 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM,
572 VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
Rusty Russellc45a6812008-05-02 21:50:50 -0500573 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
Rusty Russell363f1512008-06-08 20:51:55 +1000574 VIRTIO_NET_F_HOST_ECN, VIRTIO_F_NOTIFY_ON_EMPTY,
Rusty Russellc45a6812008-05-02 21:50:50 -0500575};
576
Rusty Russell296f96f2007-10-22 11:03:37 +1000577static struct virtio_driver virtio_net = {
Rusty Russellc45a6812008-05-02 21:50:50 -0500578 .feature_table = features,
579 .feature_table_size = ARRAY_SIZE(features),
Rusty Russell296f96f2007-10-22 11:03:37 +1000580 .driver.name = KBUILD_MODNAME,
581 .driver.owner = THIS_MODULE,
582 .id_table = id_table,
583 .probe = virtnet_probe,
584 .remove = __devexit_p(virtnet_remove),
585};
586
587static int __init init(void)
588{
589 return register_virtio_driver(&virtio_net);
590}
591
592static void __exit fini(void)
593{
594 unregister_virtio_driver(&virtio_net);
595}
596module_init(init);
597module_exit(fini);
598
599MODULE_DEVICE_TABLE(virtio, id_table);
600MODULE_DESCRIPTION("Virtio network driver");
601MODULE_LICENSE("GPL");