blob: 9a3b85e55ccc4fa62c671b7f3dc7835e225e6e7a [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>
22#include <linux/module.h>
23#include <linux/virtio.h>
24#include <linux/virtio_net.h>
25#include <linux/scatterlist.h>
26
Dor Laor6c0cd7c2007-12-16 15:19:43 +020027static int napi_weight = 128;
28module_param(napi_weight, int, 0444);
29
Rusty Russell34a48572008-02-04 23:50:02 -050030static int csum = 1, gso = 1;
31module_param(csum, bool, 0444);
32module_param(gso, bool, 0444);
33
Rusty Russell296f96f2007-10-22 11:03:37 +100034/* FIXME: MTU in config. */
35#define MAX_PACKET_LEN (ETH_HLEN+ETH_DATA_LEN)
36
37struct virtnet_info
38{
39 struct virtio_device *vdev;
40 struct virtqueue *rvq, *svq;
41 struct net_device *dev;
42 struct napi_struct napi;
43
Rusty Russell99ffc692008-05-02 21:50:46 -050044 /* The skb we couldn't send because buffers were full. */
45 struct sk_buff *last_xmit_skb;
46
Rusty Russell296f96f2007-10-22 11:03:37 +100047 /* Number of input buffers, and max we've ever had. */
48 unsigned int num, max;
49
Rusty Russell11a3a152008-05-26 17:48:13 +100050 /* For cleaning up after transmission. */
51 struct tasklet_struct tasklet;
52
Rusty Russell296f96f2007-10-22 11:03:37 +100053 /* Receive & send queues. */
54 struct sk_buff_head recv;
55 struct sk_buff_head send;
56};
57
58static inline struct virtio_net_hdr *skb_vnet_hdr(struct sk_buff *skb)
59{
60 return (struct virtio_net_hdr *)skb->cb;
61}
62
63static inline void vnet_hdr_to_sg(struct scatterlist *sg, struct sk_buff *skb)
64{
65 sg_init_one(sg, skb_vnet_hdr(skb), sizeof(struct virtio_net_hdr));
66}
67
Rusty Russell2cb9c6b2008-02-04 23:50:07 -050068static void skb_xmit_done(struct virtqueue *svq)
Rusty Russell296f96f2007-10-22 11:03:37 +100069{
Rusty Russell2cb9c6b2008-02-04 23:50:07 -050070 struct virtnet_info *vi = svq->vdev->priv;
Rusty Russell296f96f2007-10-22 11:03:37 +100071
Rusty Russell2cb9c6b2008-02-04 23:50:07 -050072 /* Suppress further interrupts. */
73 svq->vq_ops->disable_cb(svq);
Rusty Russell11a3a152008-05-26 17:48:13 +100074
Rusty Russell2cb9c6b2008-02-04 23:50:07 -050075 /* We were waiting for more output buffers. */
Rusty Russell296f96f2007-10-22 11:03:37 +100076 netif_wake_queue(vi->dev);
Rusty Russell11a3a152008-05-26 17:48:13 +100077
78 /* Make sure we re-xmit last_xmit_skb: if there are no more packets
79 * queued, start_xmit won't be called. */
80 tasklet_schedule(&vi->tasklet);
Rusty Russell296f96f2007-10-22 11:03:37 +100081}
82
83static void receive_skb(struct net_device *dev, struct sk_buff *skb,
84 unsigned len)
85{
86 struct virtio_net_hdr *hdr = skb_vnet_hdr(skb);
87
88 if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) {
89 pr_debug("%s: short packet %i\n", dev->name, len);
90 dev->stats.rx_length_errors++;
91 goto drop;
92 }
93 len -= sizeof(struct virtio_net_hdr);
94 BUG_ON(len > MAX_PACKET_LEN);
95
96 skb_trim(skb, len);
Mark McLoughlin23cde762008-06-08 20:49:00 +100097
Rusty Russell296f96f2007-10-22 11:03:37 +100098 dev->stats.rx_bytes += skb->len;
99 dev->stats.rx_packets++;
100
101 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
102 pr_debug("Needs csum!\n");
Rusty Russellf35d9d82008-02-04 23:49:54 -0500103 if (!skb_partial_csum_set(skb,hdr->csum_start,hdr->csum_offset))
Rusty Russell296f96f2007-10-22 11:03:37 +1000104 goto frame_err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000105 }
106
Mark McLoughlin23cde762008-06-08 20:49:00 +1000107 skb->protocol = eth_type_trans(skb, dev);
108 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
109 ntohs(skb->protocol), skb->len, skb->pkt_type);
110
Rusty Russell296f96f2007-10-22 11:03:37 +1000111 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
112 pr_debug("GSO!\n");
Rusty Russell34a48572008-02-04 23:50:02 -0500113 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000114 case VIRTIO_NET_HDR_GSO_TCPV4:
115 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
116 break;
Rusty Russell296f96f2007-10-22 11:03:37 +1000117 case VIRTIO_NET_HDR_GSO_UDP:
118 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
119 break;
120 case VIRTIO_NET_HDR_GSO_TCPV6:
121 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
122 break;
123 default:
124 if (net_ratelimit())
125 printk(KERN_WARNING "%s: bad gso type %u.\n",
126 dev->name, hdr->gso_type);
127 goto frame_err;
128 }
129
Rusty Russell34a48572008-02-04 23:50:02 -0500130 if (hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN)
131 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
132
Rusty Russell296f96f2007-10-22 11:03:37 +1000133 skb_shinfo(skb)->gso_size = hdr->gso_size;
134 if (skb_shinfo(skb)->gso_size == 0) {
135 if (net_ratelimit())
136 printk(KERN_WARNING "%s: zero gso size.\n",
137 dev->name);
138 goto frame_err;
139 }
140
141 /* Header must be checked, and gso_segs computed. */
142 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
143 skb_shinfo(skb)->gso_segs = 0;
144 }
145
146 netif_receive_skb(skb);
147 return;
148
149frame_err:
150 dev->stats.rx_frame_errors++;
151drop:
152 dev_kfree_skb(skb);
153}
154
155static void try_fill_recv(struct virtnet_info *vi)
156{
157 struct sk_buff *skb;
Rusty Russell05271682008-05-02 21:50:45 -0500158 struct scatterlist sg[2+MAX_SKB_FRAGS];
Rusty Russell296f96f2007-10-22 11:03:37 +1000159 int num, err;
160
Rusty Russell05271682008-05-02 21:50:45 -0500161 sg_init_table(sg, 2+MAX_SKB_FRAGS);
Rusty Russell296f96f2007-10-22 11:03:37 +1000162 for (;;) {
163 skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN);
164 if (unlikely(!skb))
165 break;
166
167 skb_put(skb, MAX_PACKET_LEN);
168 vnet_hdr_to_sg(sg, skb);
169 num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
170 skb_queue_head(&vi->recv, skb);
171
172 err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, num, skb);
173 if (err) {
174 skb_unlink(skb, &vi->recv);
175 kfree_skb(skb);
176 break;
177 }
178 vi->num++;
179 }
180 if (unlikely(vi->num > vi->max))
181 vi->max = vi->num;
182 vi->rvq->vq_ops->kick(vi->rvq);
183}
184
Rusty Russell18445c42008-02-04 23:49:57 -0500185static void skb_recv_done(struct virtqueue *rvq)
Rusty Russell296f96f2007-10-22 11:03:37 +1000186{
187 struct virtnet_info *vi = rvq->vdev->priv;
Rusty Russell18445c42008-02-04 23:49:57 -0500188 /* Schedule NAPI, Suppress further interrupts if successful. */
189 if (netif_rx_schedule_prep(vi->dev, &vi->napi)) {
190 rvq->vq_ops->disable_cb(rvq);
191 __netif_rx_schedule(vi->dev, &vi->napi);
192 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000193}
194
195static int virtnet_poll(struct napi_struct *napi, int budget)
196{
197 struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi);
198 struct sk_buff *skb = NULL;
199 unsigned int len, received = 0;
200
201again:
202 while (received < budget &&
203 (skb = vi->rvq->vq_ops->get_buf(vi->rvq, &len)) != NULL) {
204 __skb_unlink(skb, &vi->recv);
205 receive_skb(vi->dev, skb, len);
206 vi->num--;
207 received++;
208 }
209
210 /* FIXME: If we oom and completely run out of inbufs, we need
211 * to start a timer trying to fill more. */
212 if (vi->num < vi->max / 2)
213 try_fill_recv(vi);
214
Rusty Russell8329d982007-11-19 11:20:43 -0500215 /* Out of packets? */
216 if (received < budget) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000217 netif_rx_complete(vi->dev, napi);
Rusty Russell18445c42008-02-04 23:49:57 -0500218 if (unlikely(!vi->rvq->vq_ops->enable_cb(vi->rvq))
Christian Borntraeger4265f162008-03-14 14:17:05 +0100219 && napi_schedule_prep(napi)) {
220 vi->rvq->vq_ops->disable_cb(vi->rvq);
221 __netif_rx_schedule(vi->dev, napi);
Rusty Russell296f96f2007-10-22 11:03:37 +1000222 goto again;
Christian Borntraeger4265f162008-03-14 14:17:05 +0100223 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000224 }
225
226 return received;
227}
228
229static void free_old_xmit_skbs(struct virtnet_info *vi)
230{
231 struct sk_buff *skb;
232 unsigned int len;
233
234 while ((skb = vi->svq->vq_ops->get_buf(vi->svq, &len)) != NULL) {
235 pr_debug("Sent skb %p\n", skb);
236 __skb_unlink(skb, &vi->send);
Rusty Russell655aa312008-05-02 21:50:43 -0500237 vi->dev->stats.tx_bytes += skb->len;
Rusty Russell296f96f2007-10-22 11:03:37 +1000238 vi->dev->stats.tx_packets++;
239 kfree_skb(skb);
240 }
241}
242
Rusty Russell99ffc692008-05-02 21:50:46 -0500243static int xmit_skb(struct virtnet_info *vi, struct sk_buff *skb)
Rusty Russell296f96f2007-10-22 11:03:37 +1000244{
Rusty Russell99ffc692008-05-02 21:50:46 -0500245 int num;
Rusty Russell05271682008-05-02 21:50:45 -0500246 struct scatterlist sg[2+MAX_SKB_FRAGS];
Rusty Russell296f96f2007-10-22 11:03:37 +1000247 struct virtio_net_hdr *hdr;
248 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
Rusty Russell296f96f2007-10-22 11:03:37 +1000249
Rusty Russell05271682008-05-02 21:50:45 -0500250 sg_init_table(sg, 2+MAX_SKB_FRAGS);
Rusty Russell4d125de2007-11-07 16:34:49 +1100251
Rusty Russell99ffc692008-05-02 21:50:46 -0500252 pr_debug("%s: xmit %p " MAC_FMT "\n", vi->dev->name, skb,
David S. Miller21f644f2008-04-08 16:50:44 -0700253 dest[0], dest[1], dest[2],
254 dest[3], dest[4], dest[5]);
Rusty Russell296f96f2007-10-22 11:03:37 +1000255
Rusty Russell296f96f2007-10-22 11:03:37 +1000256 /* Encode metadata header at front. */
257 hdr = skb_vnet_hdr(skb);
258 if (skb->ip_summed == CHECKSUM_PARTIAL) {
259 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
260 hdr->csum_start = skb->csum_start - skb_headroom(skb);
261 hdr->csum_offset = skb->csum_offset;
262 } else {
263 hdr->flags = 0;
264 hdr->csum_offset = hdr->csum_start = 0;
265 }
266
267 if (skb_is_gso(skb)) {
Rusty Russell50c8ea82008-02-04 23:50:01 -0500268 hdr->hdr_len = skb_transport_header(skb) - skb->data;
Rusty Russell296f96f2007-10-22 11:03:37 +1000269 hdr->gso_size = skb_shinfo(skb)->gso_size;
Rusty Russell34a48572008-02-04 23:50:02 -0500270 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
Rusty Russell296f96f2007-10-22 11:03:37 +1000271 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
272 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
273 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
274 else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
275 hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
276 else
277 BUG();
Rusty Russell34a48572008-02-04 23:50:02 -0500278 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
279 hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
Rusty Russell296f96f2007-10-22 11:03:37 +1000280 } else {
281 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
Rusty Russell50c8ea82008-02-04 23:50:01 -0500282 hdr->gso_size = hdr->hdr_len = 0;
Rusty Russell296f96f2007-10-22 11:03:37 +1000283 }
284
285 vnet_hdr_to_sg(sg, skb);
286 num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
Rusty Russell99ffc692008-05-02 21:50:46 -0500287
288 return vi->svq->vq_ops->add_buf(vi->svq, sg, num, 0, skb);
289}
290
Rusty Russell11a3a152008-05-26 17:48:13 +1000291static void xmit_tasklet(unsigned long data)
292{
293 struct virtnet_info *vi = (void *)data;
294
295 netif_tx_lock_bh(vi->dev);
296 if (vi->last_xmit_skb && xmit_skb(vi, vi->last_xmit_skb) == 0) {
297 vi->svq->vq_ops->kick(vi->svq);
298 vi->last_xmit_skb = NULL;
299 }
300 netif_tx_unlock_bh(vi->dev);
301}
302
Rusty Russell99ffc692008-05-02 21:50:46 -0500303static int start_xmit(struct sk_buff *skb, struct net_device *dev)
304{
305 struct virtnet_info *vi = netdev_priv(dev);
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500306
307again:
308 /* Free up any pending old buffers before queueing new ones. */
309 free_old_xmit_skbs(vi);
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500310
Rusty Russell99ffc692008-05-02 21:50:46 -0500311 /* If we has a buffer left over from last time, send it now. */
Rusty Russell7eb2e252008-05-26 17:42:42 +1000312 if (unlikely(vi->last_xmit_skb)) {
Rusty Russell99ffc692008-05-02 21:50:46 -0500313 if (xmit_skb(vi, vi->last_xmit_skb) != 0) {
314 /* Drop this skb: we only queue one. */
315 vi->dev->stats.tx_dropped++;
316 kfree_skb(skb);
Rusty Russell7eb2e252008-05-26 17:42:42 +1000317 skb = NULL;
Rusty Russell99ffc692008-05-02 21:50:46 -0500318 goto stop_queue;
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500319 }
Rusty Russell99ffc692008-05-02 21:50:46 -0500320 vi->last_xmit_skb = NULL;
Rusty Russell296f96f2007-10-22 11:03:37 +1000321 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000322
Rusty Russell99ffc692008-05-02 21:50:46 -0500323 /* Put new one in send queue and do transmit */
Rusty Russell7eb2e252008-05-26 17:42:42 +1000324 if (likely(skb)) {
325 __skb_queue_head(&vi->send, skb);
326 if (xmit_skb(vi, skb) != 0) {
327 vi->last_xmit_skb = skb;
328 skb = NULL;
329 goto stop_queue;
330 }
Rusty Russell99ffc692008-05-02 21:50:46 -0500331 }
332done:
333 vi->svq->vq_ops->kick(vi->svq);
334 return NETDEV_TX_OK;
335
336stop_queue:
337 pr_debug("%s: virtio not prepared to send\n", dev->name);
338 netif_stop_queue(dev);
339
340 /* Activate callback for using skbs: if this returns false it
341 * means some were used in the meantime. */
342 if (unlikely(!vi->svq->vq_ops->enable_cb(vi->svq))) {
343 vi->svq->vq_ops->disable_cb(vi->svq);
344 netif_start_queue(dev);
345 goto again;
346 }
347 goto done;
Rusty Russell296f96f2007-10-22 11:03:37 +1000348}
349
Amit Shahda74e892008-02-29 16:24:50 +0530350#ifdef CONFIG_NET_POLL_CONTROLLER
351static void virtnet_netpoll(struct net_device *dev)
352{
353 struct virtnet_info *vi = netdev_priv(dev);
354
355 napi_schedule(&vi->napi);
356}
357#endif
358
Rusty Russell296f96f2007-10-22 11:03:37 +1000359static int virtnet_open(struct net_device *dev)
360{
361 struct virtnet_info *vi = netdev_priv(dev);
362
Rusty Russell296f96f2007-10-22 11:03:37 +1000363 napi_enable(&vi->napi);
Rusty Russella48bd8f2008-02-04 23:50:07 -0500364
365 /* If all buffers were filled by other side before we napi_enabled, we
366 * won't get another interrupt, so process any outstanding packets
Christian Borntraeger370076d2008-02-06 08:50:11 +0100367 * now. virtnet_poll wants re-enable the queue, so we disable here.
368 * We synchronize against interrupts via NAPI_STATE_SCHED */
369 if (netif_rx_schedule_prep(dev, &vi->napi)) {
370 vi->rvq->vq_ops->disable_cb(vi->rvq);
371 __netif_rx_schedule(dev, &vi->napi);
372 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000373 return 0;
374}
375
376static int virtnet_close(struct net_device *dev)
377{
378 struct virtnet_info *vi = netdev_priv(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +1000379
380 napi_disable(&vi->napi);
381
Rusty Russell296f96f2007-10-22 11:03:37 +1000382 return 0;
383}
384
385static int virtnet_probe(struct virtio_device *vdev)
386{
387 int err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000388 struct net_device *dev;
389 struct virtnet_info *vi;
Rusty Russell296f96f2007-10-22 11:03:37 +1000390
391 /* Allocate ourselves a network device with room for our info */
392 dev = alloc_etherdev(sizeof(struct virtnet_info));
393 if (!dev)
394 return -ENOMEM;
395
396 /* Set up network device as normal. */
Rusty Russell296f96f2007-10-22 11:03:37 +1000397 dev->open = virtnet_open;
398 dev->stop = virtnet_close;
399 dev->hard_start_xmit = start_xmit;
400 dev->features = NETIF_F_HIGHDMA;
Amit Shahda74e892008-02-29 16:24:50 +0530401#ifdef CONFIG_NET_POLL_CONTROLLER
402 dev->poll_controller = virtnet_netpoll;
403#endif
Rusty Russell296f96f2007-10-22 11:03:37 +1000404 SET_NETDEV_DEV(dev, &vdev->dev);
405
406 /* Do we support "hardware" checksums? */
Rusty Russellc45a6812008-05-02 21:50:50 -0500407 if (csum && virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000408 /* This opens up the world of extra features. */
409 dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
Rusty Russellc45a6812008-05-02 21:50:50 -0500410 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
Rusty Russell34a48572008-02-04 23:50:02 -0500411 dev->features |= NETIF_F_TSO | NETIF_F_UFO
412 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
413 }
Rusty Russell5539ae962008-05-02 21:50:46 -0500414 /* Individual feature bits: what can host handle? */
Rusty Russellc45a6812008-05-02 21:50:50 -0500415 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
Rusty Russell5539ae962008-05-02 21:50:46 -0500416 dev->features |= NETIF_F_TSO;
Rusty Russellc45a6812008-05-02 21:50:50 -0500417 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
Rusty Russell5539ae962008-05-02 21:50:46 -0500418 dev->features |= NETIF_F_TSO6;
Rusty Russellc45a6812008-05-02 21:50:50 -0500419 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
Rusty Russell5539ae962008-05-02 21:50:46 -0500420 dev->features |= NETIF_F_TSO_ECN;
Rusty Russellc45a6812008-05-02 21:50:50 -0500421 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
Rusty Russell5539ae962008-05-02 21:50:46 -0500422 dev->features |= NETIF_F_UFO;
Rusty Russell296f96f2007-10-22 11:03:37 +1000423 }
424
425 /* Configuration may specify what MAC to use. Otherwise random. */
Rusty Russellc45a6812008-05-02 21:50:50 -0500426 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
Rusty Russella586d4f2008-02-04 23:49:56 -0500427 vdev->config->get(vdev,
428 offsetof(struct virtio_net_config, mac),
429 dev->dev_addr, dev->addr_len);
Rusty Russell296f96f2007-10-22 11:03:37 +1000430 } else
431 random_ether_addr(dev->dev_addr);
432
433 /* Set up our device-specific information */
434 vi = netdev_priv(dev);
Dor Laor6c0cd7c2007-12-16 15:19:43 +0200435 netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight);
Rusty Russell296f96f2007-10-22 11:03:37 +1000436 vi->dev = dev;
437 vi->vdev = vdev;
Christian Borntraegerd9d5dcc2008-02-18 10:02:51 +0100438 vdev->priv = vi;
Rusty Russell296f96f2007-10-22 11:03:37 +1000439
440 /* We expect two virtqueues, receive then send. */
Rusty Russella586d4f2008-02-04 23:49:56 -0500441 vi->rvq = vdev->config->find_vq(vdev, 0, skb_recv_done);
Rusty Russell296f96f2007-10-22 11:03:37 +1000442 if (IS_ERR(vi->rvq)) {
443 err = PTR_ERR(vi->rvq);
444 goto free;
445 }
446
Rusty Russella586d4f2008-02-04 23:49:56 -0500447 vi->svq = vdev->config->find_vq(vdev, 1, skb_xmit_done);
Rusty Russell296f96f2007-10-22 11:03:37 +1000448 if (IS_ERR(vi->svq)) {
449 err = PTR_ERR(vi->svq);
450 goto free_recv;
451 }
452
453 /* Initialize our empty receive and send queues. */
454 skb_queue_head_init(&vi->recv);
455 skb_queue_head_init(&vi->send);
456
Rusty Russell11a3a152008-05-26 17:48:13 +1000457 tasklet_init(&vi->tasklet, xmit_tasklet, (unsigned long)vi);
458
Rusty Russell296f96f2007-10-22 11:03:37 +1000459 err = register_netdev(dev);
460 if (err) {
461 pr_debug("virtio_net: registering device failed\n");
462 goto free_send;
463 }
Rusty Russellb3369c12008-02-04 23:50:02 -0500464
465 /* Last of all, set up some receive buffers. */
466 try_fill_recv(vi);
467
468 /* If we didn't even get one input buffer, we're useless. */
469 if (vi->num == 0) {
470 err = -ENOMEM;
471 goto unregister;
472 }
473
Rusty Russell296f96f2007-10-22 11:03:37 +1000474 pr_debug("virtnet: registered device %s\n", dev->name);
Rusty Russell296f96f2007-10-22 11:03:37 +1000475 return 0;
476
Rusty Russellb3369c12008-02-04 23:50:02 -0500477unregister:
478 unregister_netdev(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +1000479free_send:
480 vdev->config->del_vq(vi->svq);
481free_recv:
482 vdev->config->del_vq(vi->rvq);
483free:
484 free_netdev(dev);
485 return err;
486}
487
488static void virtnet_remove(struct virtio_device *vdev)
489{
Rusty Russell74b25532007-11-19 11:20:42 -0500490 struct virtnet_info *vi = vdev->priv;
Rusty Russellb3369c12008-02-04 23:50:02 -0500491 struct sk_buff *skb;
492
Rusty Russell6e5aa7e2008-02-04 23:50:03 -0500493 /* Stop all the virtqueues. */
494 vdev->config->reset(vdev);
495
Rusty Russellb3369c12008-02-04 23:50:02 -0500496 /* Free our skbs in send and recv queues, if any. */
Rusty Russellb3369c12008-02-04 23:50:02 -0500497 while ((skb = __skb_dequeue(&vi->recv)) != NULL) {
498 kfree_skb(skb);
499 vi->num--;
500 }
Wang Chen288369c2008-05-22 18:07:43 +0800501 __skb_queue_purge(&vi->send);
Rusty Russellb3369c12008-02-04 23:50:02 -0500502
503 BUG_ON(vi->num != 0);
Rusty Russell74b25532007-11-19 11:20:42 -0500504
505 vdev->config->del_vq(vi->svq);
506 vdev->config->del_vq(vi->rvq);
507 unregister_netdev(vi->dev);
508 free_netdev(vi->dev);
Rusty Russell296f96f2007-10-22 11:03:37 +1000509}
510
511static struct virtio_device_id id_table[] = {
512 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
513 { 0 },
514};
515
Rusty Russellc45a6812008-05-02 21:50:50 -0500516static unsigned int features[] = {
517 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
518 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
519 VIRTIO_NET_F_HOST_ECN,
520};
521
Rusty Russell296f96f2007-10-22 11:03:37 +1000522static struct virtio_driver virtio_net = {
Rusty Russellc45a6812008-05-02 21:50:50 -0500523 .feature_table = features,
524 .feature_table_size = ARRAY_SIZE(features),
Rusty Russell296f96f2007-10-22 11:03:37 +1000525 .driver.name = KBUILD_MODNAME,
526 .driver.owner = THIS_MODULE,
527 .id_table = id_table,
528 .probe = virtnet_probe,
529 .remove = __devexit_p(virtnet_remove),
530};
531
532static int __init init(void)
533{
534 return register_virtio_driver(&virtio_net);
535}
536
537static void __exit fini(void)
538{
539 unregister_virtio_driver(&virtio_net);
540}
541module_init(init);
542module_exit(fini);
543
544MODULE_DEVICE_TABLE(virtio, id_table);
545MODULE_DESCRIPTION("Virtio network driver");
546MODULE_LICENSE("GPL");