Commit | Line | Data |
---|---|---|
296f96fc RR |
1 | /* 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> | |
a9ea3fc6 | 22 | #include <linux/ethtool.h> |
296f96fc RR |
23 | #include <linux/module.h> |
24 | #include <linux/virtio.h> | |
25 | #include <linux/virtio_net.h> | |
26 | #include <linux/scatterlist.h> | |
e918085a | 27 | #include <linux/if_vlan.h> |
296f96fc | 28 | |
6c0cd7c0 DL |
29 | static int napi_weight = 128; |
30 | module_param(napi_weight, int, 0444); | |
31 | ||
34a48579 RR |
32 | static int csum = 1, gso = 1; |
33 | module_param(csum, bool, 0444); | |
34 | module_param(gso, bool, 0444); | |
35 | ||
296f96fc | 36 | /* FIXME: MTU in config. */ |
e918085a | 37 | #define MAX_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN) |
3f2c31d9 | 38 | #define GOOD_COPY_LEN 128 |
296f96fc | 39 | |
f565a7c2 | 40 | #define VIRTNET_SEND_COMMAND_SG_MAX 2 |
2a41f71d | 41 | |
296f96fc RR |
42 | struct virtnet_info |
43 | { | |
44 | struct virtio_device *vdev; | |
2a41f71d | 45 | struct virtqueue *rvq, *svq, *cvq; |
296f96fc RR |
46 | struct net_device *dev; |
47 | struct napi_struct napi; | |
9f4d26d0 | 48 | unsigned int status; |
296f96fc | 49 | |
99ffc696 RR |
50 | /* The skb we couldn't send because buffers were full. */ |
51 | struct sk_buff *last_xmit_skb; | |
52 | ||
363f1514 | 53 | /* If we need to free in a timer, this is it. */ |
14c998f0 MM |
54 | struct timer_list xmit_free_timer; |
55 | ||
296f96fc RR |
56 | /* Number of input buffers, and max we've ever had. */ |
57 | unsigned int num, max; | |
58 | ||
11a3a154 RR |
59 | /* For cleaning up after transmission. */ |
60 | struct tasklet_struct tasklet; | |
363f1514 | 61 | bool free_in_tasklet; |
11a3a154 | 62 | |
97402b96 HX |
63 | /* I like... big packets and I cannot lie! */ |
64 | bool big_packets; | |
65 | ||
3f2c31d9 MM |
66 | /* Host will merge rx buffers for big packets (shake it! shake it!) */ |
67 | bool mergeable_rx_bufs; | |
68 | ||
296f96fc RR |
69 | /* Receive & send queues. */ |
70 | struct sk_buff_head recv; | |
71 | struct sk_buff_head send; | |
fb6813f4 | 72 | |
3161e453 RR |
73 | /* Work struct for refilling if we run low on memory. */ |
74 | struct delayed_work refill; | |
75 | ||
fb6813f4 RR |
76 | /* Chain pages by the private ptr. */ |
77 | struct page *pages; | |
296f96fc RR |
78 | }; |
79 | ||
3f2c31d9 | 80 | static inline void *skb_vnet_hdr(struct sk_buff *skb) |
296f96fc RR |
81 | { |
82 | return (struct virtio_net_hdr *)skb->cb; | |
83 | } | |
84 | ||
fb6813f4 RR |
85 | static void give_a_page(struct virtnet_info *vi, struct page *page) |
86 | { | |
87 | page->private = (unsigned long)vi->pages; | |
88 | vi->pages = page; | |
89 | } | |
90 | ||
0a888fd1 MM |
91 | static void trim_pages(struct virtnet_info *vi, struct sk_buff *skb) |
92 | { | |
93 | unsigned int i; | |
94 | ||
95 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) | |
96 | give_a_page(vi, skb_shinfo(skb)->frags[i].page); | |
97 | skb_shinfo(skb)->nr_frags = 0; | |
98 | skb->data_len = 0; | |
99 | } | |
100 | ||
fb6813f4 RR |
101 | static struct page *get_a_page(struct virtnet_info *vi, gfp_t gfp_mask) |
102 | { | |
103 | struct page *p = vi->pages; | |
104 | ||
105 | if (p) | |
106 | vi->pages = (struct page *)p->private; | |
107 | else | |
108 | p = alloc_page(gfp_mask); | |
109 | return p; | |
110 | } | |
111 | ||
2cb9c6ba | 112 | static void skb_xmit_done(struct virtqueue *svq) |
296f96fc | 113 | { |
2cb9c6ba | 114 | struct virtnet_info *vi = svq->vdev->priv; |
296f96fc | 115 | |
2cb9c6ba RR |
116 | /* Suppress further interrupts. */ |
117 | svq->vq_ops->disable_cb(svq); | |
11a3a154 | 118 | |
363f1514 | 119 | /* We were probably waiting for more output buffers. */ |
296f96fc | 120 | netif_wake_queue(vi->dev); |
11a3a154 RR |
121 | |
122 | /* Make sure we re-xmit last_xmit_skb: if there are no more packets | |
123 | * queued, start_xmit won't be called. */ | |
124 | tasklet_schedule(&vi->tasklet); | |
296f96fc RR |
125 | } |
126 | ||
127 | static void receive_skb(struct net_device *dev, struct sk_buff *skb, | |
128 | unsigned len) | |
129 | { | |
3f2c31d9 | 130 | struct virtnet_info *vi = netdev_priv(dev); |
296f96fc | 131 | struct virtio_net_hdr *hdr = skb_vnet_hdr(skb); |
97402b96 | 132 | int err; |
3f2c31d9 | 133 | int i; |
296f96fc RR |
134 | |
135 | if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) { | |
136 | pr_debug("%s: short packet %i\n", dev->name, len); | |
137 | dev->stats.rx_length_errors++; | |
138 | goto drop; | |
139 | } | |
23cde76d | 140 | |
3f2c31d9 MM |
141 | if (vi->mergeable_rx_bufs) { |
142 | struct virtio_net_hdr_mrg_rxbuf *mhdr = skb_vnet_hdr(skb); | |
143 | unsigned int copy; | |
144 | char *p = page_address(skb_shinfo(skb)->frags[0].page); | |
fb6813f4 | 145 | |
3f2c31d9 MM |
146 | if (len > PAGE_SIZE) |
147 | len = PAGE_SIZE; | |
148 | len -= sizeof(struct virtio_net_hdr_mrg_rxbuf); | |
149 | ||
150 | memcpy(hdr, p, sizeof(*mhdr)); | |
151 | p += sizeof(*mhdr); | |
152 | ||
153 | copy = len; | |
154 | if (copy > skb_tailroom(skb)) | |
155 | copy = skb_tailroom(skb); | |
156 | ||
157 | memcpy(skb_put(skb, copy), p, copy); | |
158 | ||
159 | len -= copy; | |
160 | ||
161 | if (!len) { | |
162 | give_a_page(vi, skb_shinfo(skb)->frags[0].page); | |
163 | skb_shinfo(skb)->nr_frags--; | |
164 | } else { | |
165 | skb_shinfo(skb)->frags[0].page_offset += | |
166 | sizeof(*mhdr) + copy; | |
167 | skb_shinfo(skb)->frags[0].size = len; | |
168 | skb->data_len += len; | |
169 | skb->len += len; | |
170 | } | |
171 | ||
172 | while (--mhdr->num_buffers) { | |
173 | struct sk_buff *nskb; | |
174 | ||
175 | i = skb_shinfo(skb)->nr_frags; | |
176 | if (i >= MAX_SKB_FRAGS) { | |
177 | pr_debug("%s: packet too long %d\n", dev->name, | |
178 | len); | |
179 | dev->stats.rx_length_errors++; | |
180 | goto drop; | |
181 | } | |
182 | ||
183 | nskb = vi->rvq->vq_ops->get_buf(vi->rvq, &len); | |
184 | if (!nskb) { | |
185 | pr_debug("%s: rx error: %d buffers missing\n", | |
186 | dev->name, mhdr->num_buffers); | |
187 | dev->stats.rx_length_errors++; | |
188 | goto drop; | |
189 | } | |
190 | ||
191 | __skb_unlink(nskb, &vi->recv); | |
192 | vi->num--; | |
193 | ||
194 | skb_shinfo(skb)->frags[i] = skb_shinfo(nskb)->frags[0]; | |
195 | skb_shinfo(nskb)->nr_frags = 0; | |
196 | kfree_skb(nskb); | |
197 | ||
198 | if (len > PAGE_SIZE) | |
199 | len = PAGE_SIZE; | |
200 | ||
201 | skb_shinfo(skb)->frags[i].size = len; | |
202 | skb_shinfo(skb)->nr_frags++; | |
203 | skb->data_len += len; | |
204 | skb->len += len; | |
205 | } | |
206 | } else { | |
207 | len -= sizeof(struct virtio_net_hdr); | |
208 | ||
209 | if (len <= MAX_PACKET_LEN) | |
210 | trim_pages(vi, skb); | |
211 | ||
212 | err = pskb_trim(skb, len); | |
213 | if (err) { | |
214 | pr_debug("%s: pskb_trim failed %i %d\n", dev->name, | |
215 | len, err); | |
216 | dev->stats.rx_dropped++; | |
217 | goto drop; | |
218 | } | |
97402b96 | 219 | } |
3f2c31d9 | 220 | |
97402b96 | 221 | skb->truesize += skb->data_len; |
296f96fc RR |
222 | dev->stats.rx_bytes += skb->len; |
223 | dev->stats.rx_packets++; | |
224 | ||
225 | if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) { | |
226 | pr_debug("Needs csum!\n"); | |
f35d9d8a | 227 | if (!skb_partial_csum_set(skb,hdr->csum_start,hdr->csum_offset)) |
296f96fc | 228 | goto frame_err; |
296f96fc RR |
229 | } |
230 | ||
23cde76d MM |
231 | skb->protocol = eth_type_trans(skb, dev); |
232 | pr_debug("Receiving skb proto 0x%04x len %i type %i\n", | |
233 | ntohs(skb->protocol), skb->len, skb->pkt_type); | |
234 | ||
296f96fc RR |
235 | if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) { |
236 | pr_debug("GSO!\n"); | |
34a48579 | 237 | switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) { |
296f96fc RR |
238 | case VIRTIO_NET_HDR_GSO_TCPV4: |
239 | skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4; | |
240 | break; | |
296f96fc RR |
241 | case VIRTIO_NET_HDR_GSO_UDP: |
242 | skb_shinfo(skb)->gso_type = SKB_GSO_UDP; | |
243 | break; | |
244 | case VIRTIO_NET_HDR_GSO_TCPV6: | |
245 | skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6; | |
246 | break; | |
247 | default: | |
248 | if (net_ratelimit()) | |
249 | printk(KERN_WARNING "%s: bad gso type %u.\n", | |
250 | dev->name, hdr->gso_type); | |
251 | goto frame_err; | |
252 | } | |
253 | ||
34a48579 RR |
254 | if (hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN) |
255 | skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN; | |
256 | ||
296f96fc RR |
257 | skb_shinfo(skb)->gso_size = hdr->gso_size; |
258 | if (skb_shinfo(skb)->gso_size == 0) { | |
259 | if (net_ratelimit()) | |
260 | printk(KERN_WARNING "%s: zero gso size.\n", | |
261 | dev->name); | |
262 | goto frame_err; | |
263 | } | |
264 | ||
265 | /* Header must be checked, and gso_segs computed. */ | |
266 | skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY; | |
267 | skb_shinfo(skb)->gso_segs = 0; | |
268 | } | |
269 | ||
270 | netif_receive_skb(skb); | |
271 | return; | |
272 | ||
273 | frame_err: | |
274 | dev->stats.rx_frame_errors++; | |
275 | drop: | |
276 | dev_kfree_skb(skb); | |
277 | } | |
278 | ||
3161e453 | 279 | static bool try_fill_recv_maxbufs(struct virtnet_info *vi, gfp_t gfp) |
296f96fc RR |
280 | { |
281 | struct sk_buff *skb; | |
05271685 | 282 | struct scatterlist sg[2+MAX_SKB_FRAGS]; |
97402b96 | 283 | int num, err, i; |
3161e453 | 284 | bool oom = false; |
296f96fc | 285 | |
05271685 | 286 | sg_init_table(sg, 2+MAX_SKB_FRAGS); |
296f96fc | 287 | for (;;) { |
3f2c31d9 MM |
288 | struct virtio_net_hdr *hdr; |
289 | ||
8981f010 | 290 | skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN + NET_IP_ALIGN); |
3161e453 RR |
291 | if (unlikely(!skb)) { |
292 | oom = true; | |
296f96fc | 293 | break; |
3161e453 | 294 | } |
296f96fc | 295 | |
8981f010 | 296 | skb_reserve(skb, NET_IP_ALIGN); |
296f96fc | 297 | skb_put(skb, MAX_PACKET_LEN); |
3f2c31d9 MM |
298 | |
299 | hdr = skb_vnet_hdr(skb); | |
8527bec5 | 300 | sg_set_buf(sg, hdr, sizeof(*hdr)); |
97402b96 HX |
301 | |
302 | if (vi->big_packets) { | |
303 | for (i = 0; i < MAX_SKB_FRAGS; i++) { | |
304 | skb_frag_t *f = &skb_shinfo(skb)->frags[i]; | |
3161e453 | 305 | f->page = get_a_page(vi, gfp); |
97402b96 HX |
306 | if (!f->page) |
307 | break; | |
308 | ||
309 | f->page_offset = 0; | |
310 | f->size = PAGE_SIZE; | |
311 | ||
312 | skb->data_len += PAGE_SIZE; | |
313 | skb->len += PAGE_SIZE; | |
314 | ||
315 | skb_shinfo(skb)->nr_frags++; | |
316 | } | |
317 | } | |
318 | ||
296f96fc RR |
319 | num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1; |
320 | skb_queue_head(&vi->recv, skb); | |
321 | ||
322 | err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, num, skb); | |
323 | if (err) { | |
324 | skb_unlink(skb, &vi->recv); | |
0a888fd1 | 325 | trim_pages(vi, skb); |
296f96fc RR |
326 | kfree_skb(skb); |
327 | break; | |
328 | } | |
329 | vi->num++; | |
330 | } | |
331 | if (unlikely(vi->num > vi->max)) | |
332 | vi->max = vi->num; | |
333 | vi->rvq->vq_ops->kick(vi->rvq); | |
3161e453 | 334 | return !oom; |
296f96fc RR |
335 | } |
336 | ||
3161e453 RR |
337 | /* Returns false if we couldn't fill entirely (OOM). */ |
338 | static bool try_fill_recv(struct virtnet_info *vi, gfp_t gfp) | |
3f2c31d9 MM |
339 | { |
340 | struct sk_buff *skb; | |
341 | struct scatterlist sg[1]; | |
342 | int err; | |
3161e453 | 343 | bool oom = false; |
3f2c31d9 | 344 | |
3161e453 RR |
345 | if (!vi->mergeable_rx_bufs) |
346 | return try_fill_recv_maxbufs(vi, gfp); | |
3f2c31d9 MM |
347 | |
348 | for (;;) { | |
349 | skb_frag_t *f; | |
350 | ||
351 | skb = netdev_alloc_skb(vi->dev, GOOD_COPY_LEN + NET_IP_ALIGN); | |
3161e453 RR |
352 | if (unlikely(!skb)) { |
353 | oom = true; | |
3f2c31d9 | 354 | break; |
3161e453 | 355 | } |
3f2c31d9 MM |
356 | |
357 | skb_reserve(skb, NET_IP_ALIGN); | |
358 | ||
359 | f = &skb_shinfo(skb)->frags[0]; | |
3161e453 | 360 | f->page = get_a_page(vi, gfp); |
3f2c31d9 | 361 | if (!f->page) { |
3161e453 | 362 | oom = true; |
3f2c31d9 MM |
363 | kfree_skb(skb); |
364 | break; | |
365 | } | |
366 | ||
367 | f->page_offset = 0; | |
368 | f->size = PAGE_SIZE; | |
369 | ||
370 | skb_shinfo(skb)->nr_frags++; | |
371 | ||
372 | sg_init_one(sg, page_address(f->page), PAGE_SIZE); | |
373 | skb_queue_head(&vi->recv, skb); | |
374 | ||
375 | err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, 1, skb); | |
376 | if (err) { | |
377 | skb_unlink(skb, &vi->recv); | |
378 | kfree_skb(skb); | |
379 | break; | |
380 | } | |
381 | vi->num++; | |
382 | } | |
383 | if (unlikely(vi->num > vi->max)) | |
384 | vi->max = vi->num; | |
385 | vi->rvq->vq_ops->kick(vi->rvq); | |
3161e453 | 386 | return !oom; |
3f2c31d9 MM |
387 | } |
388 | ||
18445c4d | 389 | static void skb_recv_done(struct virtqueue *rvq) |
296f96fc RR |
390 | { |
391 | struct virtnet_info *vi = rvq->vdev->priv; | |
18445c4d | 392 | /* Schedule NAPI, Suppress further interrupts if successful. */ |
288379f0 | 393 | if (napi_schedule_prep(&vi->napi)) { |
18445c4d | 394 | rvq->vq_ops->disable_cb(rvq); |
288379f0 | 395 | __napi_schedule(&vi->napi); |
18445c4d | 396 | } |
296f96fc RR |
397 | } |
398 | ||
3161e453 RR |
399 | static void refill_work(struct work_struct *work) |
400 | { | |
401 | struct virtnet_info *vi; | |
402 | bool still_empty; | |
403 | ||
404 | vi = container_of(work, struct virtnet_info, refill.work); | |
405 | napi_disable(&vi->napi); | |
406 | try_fill_recv(vi, GFP_KERNEL); | |
407 | still_empty = (vi->num == 0); | |
408 | napi_enable(&vi->napi); | |
409 | ||
410 | /* In theory, this can happen: if we don't get any buffers in | |
411 | * we will *never* try to fill again. */ | |
412 | if (still_empty) | |
413 | schedule_delayed_work(&vi->refill, HZ/2); | |
414 | } | |
415 | ||
296f96fc RR |
416 | static int virtnet_poll(struct napi_struct *napi, int budget) |
417 | { | |
418 | struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi); | |
419 | struct sk_buff *skb = NULL; | |
420 | unsigned int len, received = 0; | |
421 | ||
422 | again: | |
423 | while (received < budget && | |
424 | (skb = vi->rvq->vq_ops->get_buf(vi->rvq, &len)) != NULL) { | |
425 | __skb_unlink(skb, &vi->recv); | |
426 | receive_skb(vi->dev, skb, len); | |
427 | vi->num--; | |
428 | received++; | |
429 | } | |
430 | ||
3161e453 RR |
431 | if (vi->num < vi->max / 2) { |
432 | if (!try_fill_recv(vi, GFP_ATOMIC)) | |
433 | schedule_delayed_work(&vi->refill, 0); | |
434 | } | |
296f96fc | 435 | |
8329d98e RR |
436 | /* Out of packets? */ |
437 | if (received < budget) { | |
288379f0 | 438 | napi_complete(napi); |
18445c4d | 439 | if (unlikely(!vi->rvq->vq_ops->enable_cb(vi->rvq)) |
4265f161 CB |
440 | && napi_schedule_prep(napi)) { |
441 | vi->rvq->vq_ops->disable_cb(vi->rvq); | |
288379f0 | 442 | __napi_schedule(napi); |
296f96fc | 443 | goto again; |
4265f161 | 444 | } |
296f96fc RR |
445 | } |
446 | ||
447 | return received; | |
448 | } | |
449 | ||
450 | static void free_old_xmit_skbs(struct virtnet_info *vi) | |
451 | { | |
452 | struct sk_buff *skb; | |
453 | unsigned int len; | |
454 | ||
455 | while ((skb = vi->svq->vq_ops->get_buf(vi->svq, &len)) != NULL) { | |
456 | pr_debug("Sent skb %p\n", skb); | |
457 | __skb_unlink(skb, &vi->send); | |
655aa31f | 458 | vi->dev->stats.tx_bytes += skb->len; |
296f96fc RR |
459 | vi->dev->stats.tx_packets++; |
460 | kfree_skb(skb); | |
461 | } | |
462 | } | |
463 | ||
363f1514 RR |
464 | /* If the virtio transport doesn't always notify us when all in-flight packets |
465 | * are consumed, we fall back to using this function on a timer to free them. */ | |
14c998f0 MM |
466 | static void xmit_free(unsigned long data) |
467 | { | |
468 | struct virtnet_info *vi = (void *)data; | |
469 | ||
470 | netif_tx_lock(vi->dev); | |
471 | ||
472 | free_old_xmit_skbs(vi); | |
473 | ||
474 | if (!skb_queue_empty(&vi->send)) | |
475 | mod_timer(&vi->xmit_free_timer, jiffies + (HZ/10)); | |
476 | ||
477 | netif_tx_unlock(vi->dev); | |
478 | } | |
479 | ||
99ffc696 | 480 | static int xmit_skb(struct virtnet_info *vi, struct sk_buff *skb) |
296f96fc | 481 | { |
14c998f0 | 482 | int num, err; |
05271685 | 483 | struct scatterlist sg[2+MAX_SKB_FRAGS]; |
3f2c31d9 MM |
484 | struct virtio_net_hdr_mrg_rxbuf *mhdr = skb_vnet_hdr(skb); |
485 | struct virtio_net_hdr *hdr = skb_vnet_hdr(skb); | |
296f96fc | 486 | const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest; |
296f96fc | 487 | |
05271685 | 488 | sg_init_table(sg, 2+MAX_SKB_FRAGS); |
4d125de3 | 489 | |
e174961c | 490 | pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest); |
296f96fc | 491 | |
296f96fc RR |
492 | if (skb->ip_summed == CHECKSUM_PARTIAL) { |
493 | hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM; | |
494 | hdr->csum_start = skb->csum_start - skb_headroom(skb); | |
495 | hdr->csum_offset = skb->csum_offset; | |
496 | } else { | |
497 | hdr->flags = 0; | |
498 | hdr->csum_offset = hdr->csum_start = 0; | |
499 | } | |
500 | ||
501 | if (skb_is_gso(skb)) { | |
b82f08ea | 502 | hdr->hdr_len = skb_headlen(skb); |
296f96fc | 503 | hdr->gso_size = skb_shinfo(skb)->gso_size; |
34a48579 | 504 | if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) |
296f96fc RR |
505 | hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4; |
506 | else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) | |
507 | hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6; | |
508 | else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP) | |
509 | hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP; | |
510 | else | |
511 | BUG(); | |
34a48579 RR |
512 | if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN) |
513 | hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN; | |
296f96fc RR |
514 | } else { |
515 | hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE; | |
50c8ea80 | 516 | hdr->gso_size = hdr->hdr_len = 0; |
296f96fc RR |
517 | } |
518 | ||
3f2c31d9 MM |
519 | mhdr->num_buffers = 0; |
520 | ||
521 | /* Encode metadata header at front. */ | |
522 | if (vi->mergeable_rx_bufs) | |
8527bec5 | 523 | sg_set_buf(sg, mhdr, sizeof(*mhdr)); |
3f2c31d9 | 524 | else |
8527bec5 | 525 | sg_set_buf(sg, hdr, sizeof(*hdr)); |
3f2c31d9 | 526 | |
296f96fc | 527 | num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1; |
99ffc696 | 528 | |
14c998f0 | 529 | err = vi->svq->vq_ops->add_buf(vi->svq, sg, num, 0, skb); |
363f1514 | 530 | if (!err && !vi->free_in_tasklet) |
14c998f0 MM |
531 | mod_timer(&vi->xmit_free_timer, jiffies + (HZ/10)); |
532 | ||
533 | return err; | |
99ffc696 RR |
534 | } |
535 | ||
11a3a154 RR |
536 | static void xmit_tasklet(unsigned long data) |
537 | { | |
538 | struct virtnet_info *vi = (void *)data; | |
539 | ||
540 | netif_tx_lock_bh(vi->dev); | |
541 | if (vi->last_xmit_skb && xmit_skb(vi, vi->last_xmit_skb) == 0) { | |
542 | vi->svq->vq_ops->kick(vi->svq); | |
543 | vi->last_xmit_skb = NULL; | |
544 | } | |
363f1514 RR |
545 | if (vi->free_in_tasklet) |
546 | free_old_xmit_skbs(vi); | |
11a3a154 RR |
547 | netif_tx_unlock_bh(vi->dev); |
548 | } | |
549 | ||
424efe9c | 550 | static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev) |
99ffc696 RR |
551 | { |
552 | struct virtnet_info *vi = netdev_priv(dev); | |
2cb9c6ba RR |
553 | |
554 | again: | |
555 | /* Free up any pending old buffers before queueing new ones. */ | |
556 | free_old_xmit_skbs(vi); | |
99ffc696 RR |
557 | |
558 | /* If we has a buffer left over from last time, send it now. */ | |
9953ca6c MM |
559 | if (unlikely(vi->last_xmit_skb) && |
560 | xmit_skb(vi, vi->last_xmit_skb) != 0) | |
561 | goto stop_queue; | |
562 | ||
563 | vi->last_xmit_skb = NULL; | |
2cb9c6ba | 564 | |
99ffc696 | 565 | /* Put new one in send queue and do transmit */ |
7eb2e251 RR |
566 | if (likely(skb)) { |
567 | __skb_queue_head(&vi->send, skb); | |
568 | if (xmit_skb(vi, skb) != 0) { | |
569 | vi->last_xmit_skb = skb; | |
570 | skb = NULL; | |
571 | goto stop_queue; | |
572 | } | |
296f96fc | 573 | } |
99ffc696 | 574 | done: |
296f96fc | 575 | vi->svq->vq_ops->kick(vi->svq); |
99ffc696 RR |
576 | return NETDEV_TX_OK; |
577 | ||
578 | stop_queue: | |
579 | pr_debug("%s: virtio not prepared to send\n", dev->name); | |
580 | netif_stop_queue(dev); | |
581 | ||
582 | /* Activate callback for using skbs: if this returns false it | |
583 | * means some were used in the meantime. */ | |
584 | if (unlikely(!vi->svq->vq_ops->enable_cb(vi->svq))) { | |
585 | vi->svq->vq_ops->disable_cb(vi->svq); | |
586 | netif_start_queue(dev); | |
587 | goto again; | |
588 | } | |
9953ca6c MM |
589 | if (skb) { |
590 | /* Drop this skb: we only queue one. */ | |
591 | vi->dev->stats.tx_dropped++; | |
592 | kfree_skb(skb); | |
593 | } | |
99ffc696 | 594 | goto done; |
296f96fc RR |
595 | } |
596 | ||
9c46f6d4 AW |
597 | static int virtnet_set_mac_address(struct net_device *dev, void *p) |
598 | { | |
599 | struct virtnet_info *vi = netdev_priv(dev); | |
600 | struct virtio_device *vdev = vi->vdev; | |
601 | int ret; | |
602 | ||
603 | ret = eth_mac_addr(dev, p); | |
604 | if (ret) | |
605 | return ret; | |
606 | ||
62994b2d AW |
607 | if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) |
608 | vdev->config->set(vdev, offsetof(struct virtio_net_config, mac), | |
609 | dev->dev_addr, dev->addr_len); | |
9c46f6d4 AW |
610 | |
611 | return 0; | |
612 | } | |
613 | ||
da74e89d AS |
614 | #ifdef CONFIG_NET_POLL_CONTROLLER |
615 | static void virtnet_netpoll(struct net_device *dev) | |
616 | { | |
617 | struct virtnet_info *vi = netdev_priv(dev); | |
618 | ||
619 | napi_schedule(&vi->napi); | |
620 | } | |
621 | #endif | |
622 | ||
296f96fc RR |
623 | static int virtnet_open(struct net_device *dev) |
624 | { | |
625 | struct virtnet_info *vi = netdev_priv(dev); | |
626 | ||
296f96fc | 627 | napi_enable(&vi->napi); |
a48bd8f6 RR |
628 | |
629 | /* If all buffers were filled by other side before we napi_enabled, we | |
630 | * won't get another interrupt, so process any outstanding packets | |
370076d9 CB |
631 | * now. virtnet_poll wants re-enable the queue, so we disable here. |
632 | * We synchronize against interrupts via NAPI_STATE_SCHED */ | |
288379f0 | 633 | if (napi_schedule_prep(&vi->napi)) { |
370076d9 | 634 | vi->rvq->vq_ops->disable_cb(vi->rvq); |
288379f0 | 635 | __napi_schedule(&vi->napi); |
370076d9 | 636 | } |
296f96fc RR |
637 | return 0; |
638 | } | |
639 | ||
2a41f71d AW |
640 | /* |
641 | * Send command via the control virtqueue and check status. Commands | |
642 | * supported by the hypervisor, as indicated by feature bits, should | |
643 | * never fail unless improperly formated. | |
644 | */ | |
645 | static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd, | |
646 | struct scatterlist *data, int out, int in) | |
647 | { | |
23e258e1 | 648 | struct scatterlist *s, sg[VIRTNET_SEND_COMMAND_SG_MAX + 2]; |
2a41f71d AW |
649 | struct virtio_net_ctrl_hdr ctrl; |
650 | virtio_net_ctrl_ack status = ~0; | |
651 | unsigned int tmp; | |
23e258e1 | 652 | int i; |
2a41f71d | 653 | |
0ee904c3 AB |
654 | /* Caller should know better */ |
655 | BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) || | |
656 | (out + in > VIRTNET_SEND_COMMAND_SG_MAX)); | |
2a41f71d AW |
657 | |
658 | out++; /* Add header */ | |
659 | in++; /* Add return status */ | |
660 | ||
661 | ctrl.class = class; | |
662 | ctrl.cmd = cmd; | |
663 | ||
664 | sg_init_table(sg, out + in); | |
665 | ||
666 | sg_set_buf(&sg[0], &ctrl, sizeof(ctrl)); | |
23e258e1 AW |
667 | for_each_sg(data, s, out + in - 2, i) |
668 | sg_set_buf(&sg[i + 1], sg_virt(s), s->length); | |
2a41f71d AW |
669 | sg_set_buf(&sg[out + in - 1], &status, sizeof(status)); |
670 | ||
0ee904c3 | 671 | BUG_ON(vi->cvq->vq_ops->add_buf(vi->cvq, sg, out, in, vi)); |
2a41f71d AW |
672 | |
673 | vi->cvq->vq_ops->kick(vi->cvq); | |
674 | ||
675 | /* | |
676 | * Spin for a response, the kick causes an ioport write, trapping | |
677 | * into the hypervisor, so the request should be handled immediately. | |
678 | */ | |
679 | while (!vi->cvq->vq_ops->get_buf(vi->cvq, &tmp)) | |
680 | cpu_relax(); | |
681 | ||
682 | return status == VIRTIO_NET_OK; | |
683 | } | |
684 | ||
296f96fc RR |
685 | static int virtnet_close(struct net_device *dev) |
686 | { | |
687 | struct virtnet_info *vi = netdev_priv(dev); | |
296f96fc RR |
688 | |
689 | napi_disable(&vi->napi); | |
690 | ||
296f96fc RR |
691 | return 0; |
692 | } | |
693 | ||
a9ea3fc6 HX |
694 | static int virtnet_set_tx_csum(struct net_device *dev, u32 data) |
695 | { | |
696 | struct virtnet_info *vi = netdev_priv(dev); | |
697 | struct virtio_device *vdev = vi->vdev; | |
698 | ||
699 | if (data && !virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) | |
700 | return -ENOSYS; | |
701 | ||
702 | return ethtool_op_set_tx_hw_csum(dev, data); | |
703 | } | |
704 | ||
2af7698e AW |
705 | static void virtnet_set_rx_mode(struct net_device *dev) |
706 | { | |
707 | struct virtnet_info *vi = netdev_priv(dev); | |
f565a7c2 | 708 | struct scatterlist sg[2]; |
2af7698e | 709 | u8 promisc, allmulti; |
f565a7c2 AW |
710 | struct virtio_net_ctrl_mac *mac_data; |
711 | struct dev_addr_list *addr; | |
ccffad25 | 712 | struct netdev_hw_addr *ha; |
f565a7c2 AW |
713 | void *buf; |
714 | int i; | |
2af7698e AW |
715 | |
716 | /* We can't dynamicaly set ndo_set_rx_mode, so return gracefully */ | |
717 | if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX)) | |
718 | return; | |
719 | ||
f565a7c2 AW |
720 | promisc = ((dev->flags & IFF_PROMISC) != 0); |
721 | allmulti = ((dev->flags & IFF_ALLMULTI) != 0); | |
2af7698e | 722 | |
23e258e1 | 723 | sg_init_one(sg, &promisc, sizeof(promisc)); |
2af7698e AW |
724 | |
725 | if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, | |
726 | VIRTIO_NET_CTRL_RX_PROMISC, | |
f565a7c2 | 727 | sg, 1, 0)) |
2af7698e AW |
728 | dev_warn(&dev->dev, "Failed to %sable promisc mode.\n", |
729 | promisc ? "en" : "dis"); | |
730 | ||
23e258e1 | 731 | sg_init_one(sg, &allmulti, sizeof(allmulti)); |
2af7698e AW |
732 | |
733 | if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, | |
734 | VIRTIO_NET_CTRL_RX_ALLMULTI, | |
f565a7c2 | 735 | sg, 1, 0)) |
2af7698e AW |
736 | dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n", |
737 | allmulti ? "en" : "dis"); | |
f565a7c2 AW |
738 | |
739 | /* MAC filter - use one buffer for both lists */ | |
31278e71 | 740 | mac_data = buf = kzalloc(((dev->uc.count + dev->mc_count) * ETH_ALEN) + |
f565a7c2 AW |
741 | (2 * sizeof(mac_data->entries)), GFP_ATOMIC); |
742 | if (!buf) { | |
743 | dev_warn(&dev->dev, "No memory for MAC address buffer\n"); | |
744 | return; | |
745 | } | |
746 | ||
23e258e1 AW |
747 | sg_init_table(sg, 2); |
748 | ||
f565a7c2 | 749 | /* Store the unicast list and count in the front of the buffer */ |
31278e71 | 750 | mac_data->entries = dev->uc.count; |
ccffad25 | 751 | i = 0; |
31278e71 | 752 | list_for_each_entry(ha, &dev->uc.list, list) |
ccffad25 | 753 | memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN); |
f565a7c2 AW |
754 | |
755 | sg_set_buf(&sg[0], mac_data, | |
31278e71 | 756 | sizeof(mac_data->entries) + (dev->uc.count * ETH_ALEN)); |
f565a7c2 AW |
757 | |
758 | /* multicast list and count fill the end */ | |
31278e71 | 759 | mac_data = (void *)&mac_data->macs[dev->uc.count][0]; |
f565a7c2 AW |
760 | |
761 | mac_data->entries = dev->mc_count; | |
762 | addr = dev->mc_list; | |
763 | for (i = 0; i < dev->mc_count; i++, addr = addr->next) | |
764 | memcpy(&mac_data->macs[i][0], addr->da_addr, ETH_ALEN); | |
765 | ||
766 | sg_set_buf(&sg[1], mac_data, | |
767 | sizeof(mac_data->entries) + (dev->mc_count * ETH_ALEN)); | |
768 | ||
769 | if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, | |
770 | VIRTIO_NET_CTRL_MAC_TABLE_SET, | |
771 | sg, 2, 0)) | |
772 | dev_warn(&dev->dev, "Failed to set MAC fitler table.\n"); | |
773 | ||
774 | kfree(buf); | |
2af7698e AW |
775 | } |
776 | ||
1824a989 | 777 | static void virtnet_vlan_rx_add_vid(struct net_device *dev, u16 vid) |
0bde9569 AW |
778 | { |
779 | struct virtnet_info *vi = netdev_priv(dev); | |
780 | struct scatterlist sg; | |
781 | ||
23e258e1 | 782 | sg_init_one(&sg, &vid, sizeof(vid)); |
0bde9569 AW |
783 | |
784 | if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, | |
785 | VIRTIO_NET_CTRL_VLAN_ADD, &sg, 1, 0)) | |
786 | dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid); | |
787 | } | |
788 | ||
1824a989 | 789 | static void virtnet_vlan_rx_kill_vid(struct net_device *dev, u16 vid) |
0bde9569 AW |
790 | { |
791 | struct virtnet_info *vi = netdev_priv(dev); | |
792 | struct scatterlist sg; | |
793 | ||
23e258e1 | 794 | sg_init_one(&sg, &vid, sizeof(vid)); |
0bde9569 AW |
795 | |
796 | if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, | |
797 | VIRTIO_NET_CTRL_VLAN_DEL, &sg, 1, 0)) | |
798 | dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid); | |
799 | } | |
800 | ||
a9ea3fc6 HX |
801 | static struct ethtool_ops virtnet_ethtool_ops = { |
802 | .set_tx_csum = virtnet_set_tx_csum, | |
803 | .set_sg = ethtool_op_set_sg, | |
0276b497 | 804 | .set_tso = ethtool_op_set_tso, |
5c516751 | 805 | .set_ufo = ethtool_op_set_ufo, |
9f4d26d0 | 806 | .get_link = ethtool_op_get_link, |
a9ea3fc6 HX |
807 | }; |
808 | ||
39da5814 MM |
809 | #define MIN_MTU 68 |
810 | #define MAX_MTU 65535 | |
811 | ||
812 | static int virtnet_change_mtu(struct net_device *dev, int new_mtu) | |
813 | { | |
814 | if (new_mtu < MIN_MTU || new_mtu > MAX_MTU) | |
815 | return -EINVAL; | |
816 | dev->mtu = new_mtu; | |
817 | return 0; | |
818 | } | |
819 | ||
76288b4e SH |
820 | static const struct net_device_ops virtnet_netdev = { |
821 | .ndo_open = virtnet_open, | |
822 | .ndo_stop = virtnet_close, | |
823 | .ndo_start_xmit = start_xmit, | |
824 | .ndo_validate_addr = eth_validate_addr, | |
9c46f6d4 | 825 | .ndo_set_mac_address = virtnet_set_mac_address, |
2af7698e | 826 | .ndo_set_rx_mode = virtnet_set_rx_mode, |
76288b4e | 827 | .ndo_change_mtu = virtnet_change_mtu, |
1824a989 AW |
828 | .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid, |
829 | .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid, | |
76288b4e SH |
830 | #ifdef CONFIG_NET_POLL_CONTROLLER |
831 | .ndo_poll_controller = virtnet_netpoll, | |
832 | #endif | |
833 | }; | |
834 | ||
9f4d26d0 MM |
835 | static void virtnet_update_status(struct virtnet_info *vi) |
836 | { | |
837 | u16 v; | |
838 | ||
839 | if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) | |
840 | return; | |
841 | ||
842 | vi->vdev->config->get(vi->vdev, | |
843 | offsetof(struct virtio_net_config, status), | |
844 | &v, sizeof(v)); | |
845 | ||
846 | /* Ignore unknown (future) status bits */ | |
847 | v &= VIRTIO_NET_S_LINK_UP; | |
848 | ||
849 | if (vi->status == v) | |
850 | return; | |
851 | ||
852 | vi->status = v; | |
853 | ||
854 | if (vi->status & VIRTIO_NET_S_LINK_UP) { | |
855 | netif_carrier_on(vi->dev); | |
856 | netif_wake_queue(vi->dev); | |
857 | } else { | |
858 | netif_carrier_off(vi->dev); | |
859 | netif_stop_queue(vi->dev); | |
860 | } | |
861 | } | |
862 | ||
863 | static void virtnet_config_changed(struct virtio_device *vdev) | |
864 | { | |
865 | struct virtnet_info *vi = vdev->priv; | |
866 | ||
867 | virtnet_update_status(vi); | |
868 | } | |
869 | ||
296f96fc RR |
870 | static int virtnet_probe(struct virtio_device *vdev) |
871 | { | |
872 | int err; | |
296f96fc RR |
873 | struct net_device *dev; |
874 | struct virtnet_info *vi; | |
d2a7ddda MT |
875 | struct virtqueue *vqs[3]; |
876 | vq_callback_t *callbacks[] = { skb_recv_done, skb_xmit_done, NULL}; | |
877 | const char *names[] = { "input", "output", "control" }; | |
878 | int nvqs; | |
296f96fc RR |
879 | |
880 | /* Allocate ourselves a network device with room for our info */ | |
881 | dev = alloc_etherdev(sizeof(struct virtnet_info)); | |
882 | if (!dev) | |
883 | return -ENOMEM; | |
884 | ||
885 | /* Set up network device as normal. */ | |
76288b4e | 886 | dev->netdev_ops = &virtnet_netdev; |
296f96fc | 887 | dev->features = NETIF_F_HIGHDMA; |
a9ea3fc6 | 888 | SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops); |
296f96fc RR |
889 | SET_NETDEV_DEV(dev, &vdev->dev); |
890 | ||
891 | /* Do we support "hardware" checksums? */ | |
c45a6816 | 892 | if (csum && virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) { |
296f96fc RR |
893 | /* This opens up the world of extra features. */ |
894 | dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST; | |
c45a6816 | 895 | if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) { |
34a48579 RR |
896 | dev->features |= NETIF_F_TSO | NETIF_F_UFO |
897 | | NETIF_F_TSO_ECN | NETIF_F_TSO6; | |
898 | } | |
5539ae96 | 899 | /* Individual feature bits: what can host handle? */ |
c45a6816 | 900 | if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4)) |
5539ae96 | 901 | dev->features |= NETIF_F_TSO; |
c45a6816 | 902 | if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6)) |
5539ae96 | 903 | dev->features |= NETIF_F_TSO6; |
c45a6816 | 904 | if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN)) |
5539ae96 | 905 | dev->features |= NETIF_F_TSO_ECN; |
c45a6816 | 906 | if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO)) |
5539ae96 | 907 | dev->features |= NETIF_F_UFO; |
296f96fc RR |
908 | } |
909 | ||
910 | /* Configuration may specify what MAC to use. Otherwise random. */ | |
c45a6816 | 911 | if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) { |
a586d4f6 RR |
912 | vdev->config->get(vdev, |
913 | offsetof(struct virtio_net_config, mac), | |
914 | dev->dev_addr, dev->addr_len); | |
62994b2d | 915 | } else |
296f96fc RR |
916 | random_ether_addr(dev->dev_addr); |
917 | ||
918 | /* Set up our device-specific information */ | |
919 | vi = netdev_priv(dev); | |
6c0cd7c0 | 920 | netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight); |
296f96fc RR |
921 | vi->dev = dev; |
922 | vi->vdev = vdev; | |
d9d5dcc8 | 923 | vdev->priv = vi; |
fb6813f4 | 924 | vi->pages = NULL; |
3161e453 | 925 | INIT_DELAYED_WORK(&vi->refill, refill_work); |
296f96fc | 926 | |
363f1514 RR |
927 | /* If they give us a callback when all buffers are done, we don't need |
928 | * the timer. */ | |
929 | vi->free_in_tasklet = virtio_has_feature(vdev,VIRTIO_F_NOTIFY_ON_EMPTY); | |
930 | ||
97402b96 HX |
931 | /* If we can receive ANY GSO packets, we must allocate large ones. */ |
932 | if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) | |
933 | || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) | |
934 | || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN)) | |
935 | vi->big_packets = true; | |
936 | ||
3f2c31d9 MM |
937 | if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF)) |
938 | vi->mergeable_rx_bufs = true; | |
939 | ||
d2a7ddda MT |
940 | /* We expect two virtqueues, receive then send, |
941 | * and optionally control. */ | |
942 | nvqs = virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) ? 3 : 2; | |
943 | ||
944 | err = vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names); | |
945 | if (err) | |
296f96fc | 946 | goto free; |
296f96fc | 947 | |
d2a7ddda MT |
948 | vi->rvq = vqs[0]; |
949 | vi->svq = vqs[1]; | |
296f96fc | 950 | |
2a41f71d | 951 | if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)) { |
d2a7ddda | 952 | vi->cvq = vqs[2]; |
0bde9569 AW |
953 | |
954 | if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN)) | |
955 | dev->features |= NETIF_F_HW_VLAN_FILTER; | |
2a41f71d AW |
956 | } |
957 | ||
296f96fc RR |
958 | /* Initialize our empty receive and send queues. */ |
959 | skb_queue_head_init(&vi->recv); | |
960 | skb_queue_head_init(&vi->send); | |
961 | ||
11a3a154 RR |
962 | tasklet_init(&vi->tasklet, xmit_tasklet, (unsigned long)vi); |
963 | ||
363f1514 RR |
964 | if (!vi->free_in_tasklet) |
965 | setup_timer(&vi->xmit_free_timer, xmit_free, (unsigned long)vi); | |
14c998f0 | 966 | |
296f96fc RR |
967 | err = register_netdev(dev); |
968 | if (err) { | |
969 | pr_debug("virtio_net: registering device failed\n"); | |
d2a7ddda | 970 | goto free_vqs; |
296f96fc | 971 | } |
b3369c1f RR |
972 | |
973 | /* Last of all, set up some receive buffers. */ | |
3161e453 | 974 | try_fill_recv(vi, GFP_KERNEL); |
b3369c1f RR |
975 | |
976 | /* If we didn't even get one input buffer, we're useless. */ | |
977 | if (vi->num == 0) { | |
978 | err = -ENOMEM; | |
979 | goto unregister; | |
980 | } | |
981 | ||
9f4d26d0 MM |
982 | vi->status = VIRTIO_NET_S_LINK_UP; |
983 | virtnet_update_status(vi); | |
4783256e | 984 | netif_carrier_on(dev); |
9f4d26d0 | 985 | |
296f96fc | 986 | pr_debug("virtnet: registered device %s\n", dev->name); |
296f96fc RR |
987 | return 0; |
988 | ||
b3369c1f RR |
989 | unregister: |
990 | unregister_netdev(dev); | |
3161e453 | 991 | cancel_delayed_work_sync(&vi->refill); |
d2a7ddda MT |
992 | free_vqs: |
993 | vdev->config->del_vqs(vdev); | |
296f96fc RR |
994 | free: |
995 | free_netdev(dev); | |
996 | return err; | |
997 | } | |
998 | ||
999 | static void virtnet_remove(struct virtio_device *vdev) | |
1000 | { | |
74b2553f | 1001 | struct virtnet_info *vi = vdev->priv; |
b3369c1f RR |
1002 | struct sk_buff *skb; |
1003 | ||
6e5aa7ef RR |
1004 | /* Stop all the virtqueues. */ |
1005 | vdev->config->reset(vdev); | |
1006 | ||
363f1514 RR |
1007 | if (!vi->free_in_tasklet) |
1008 | del_timer_sync(&vi->xmit_free_timer); | |
14c998f0 | 1009 | |
b3369c1f | 1010 | /* Free our skbs in send and recv queues, if any. */ |
b3369c1f RR |
1011 | while ((skb = __skb_dequeue(&vi->recv)) != NULL) { |
1012 | kfree_skb(skb); | |
1013 | vi->num--; | |
1014 | } | |
288369cc | 1015 | __skb_queue_purge(&vi->send); |
b3369c1f RR |
1016 | |
1017 | BUG_ON(vi->num != 0); | |
74b2553f | 1018 | |
74b2553f | 1019 | unregister_netdev(vi->dev); |
3161e453 | 1020 | cancel_delayed_work_sync(&vi->refill); |
fb6813f4 | 1021 | |
d2a7ddda MT |
1022 | vdev->config->del_vqs(vi->vdev); |
1023 | ||
fb6813f4 RR |
1024 | while (vi->pages) |
1025 | __free_pages(get_a_page(vi, GFP_KERNEL), 0); | |
1026 | ||
74b2553f | 1027 | free_netdev(vi->dev); |
296f96fc RR |
1028 | } |
1029 | ||
1030 | static struct virtio_device_id id_table[] = { | |
1031 | { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID }, | |
1032 | { 0 }, | |
1033 | }; | |
1034 | ||
c45a6816 | 1035 | static unsigned int features[] = { |
5e4fe5c4 MM |
1036 | VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, |
1037 | VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC, | |
c45a6816 | 1038 | VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, |
97402b96 | 1039 | VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, |
5c516751 | 1040 | VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, |
2a41f71d | 1041 | VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, |
0bde9569 | 1042 | VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, |
97402b96 | 1043 | VIRTIO_F_NOTIFY_ON_EMPTY, |
c45a6816 RR |
1044 | }; |
1045 | ||
296f96fc | 1046 | static struct virtio_driver virtio_net = { |
c45a6816 RR |
1047 | .feature_table = features, |
1048 | .feature_table_size = ARRAY_SIZE(features), | |
296f96fc RR |
1049 | .driver.name = KBUILD_MODNAME, |
1050 | .driver.owner = THIS_MODULE, | |
1051 | .id_table = id_table, | |
1052 | .probe = virtnet_probe, | |
1053 | .remove = __devexit_p(virtnet_remove), | |
9f4d26d0 | 1054 | .config_changed = virtnet_config_changed, |
296f96fc RR |
1055 | }; |
1056 | ||
1057 | static int __init init(void) | |
1058 | { | |
1059 | return register_virtio_driver(&virtio_net); | |
1060 | } | |
1061 | ||
1062 | static void __exit fini(void) | |
1063 | { | |
1064 | unregister_virtio_driver(&virtio_net); | |
1065 | } | |
1066 | module_init(init); | |
1067 | module_exit(fini); | |
1068 | ||
1069 | MODULE_DEVICE_TABLE(virtio, id_table); | |
1070 | MODULE_DESCRIPTION("Virtio network driver"); | |
1071 | MODULE_LICENSE("GPL"); |