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