Commit | Line | Data |
---|---|---|
48925e37 | 1 | /* A network driver using virtio. |
296f96fc RR |
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 RR |
49 | |
50 | /* Number of input buffers, and max we've ever had. */ | |
51 | unsigned int num, max; | |
52 | ||
97402b96 HX |
53 | /* I like... big packets and I cannot lie! */ |
54 | bool big_packets; | |
55 | ||
3f2c31d9 MM |
56 | /* Host will merge rx buffers for big packets (shake it! shake it!) */ |
57 | bool mergeable_rx_bufs; | |
58 | ||
296f96fc RR |
59 | /* Receive & send queues. */ |
60 | struct sk_buff_head recv; | |
61 | struct sk_buff_head send; | |
fb6813f4 | 62 | |
3161e453 RR |
63 | /* Work struct for refilling if we run low on memory. */ |
64 | struct delayed_work refill; | |
65 | ||
fb6813f4 RR |
66 | /* Chain pages by the private ptr. */ |
67 | struct page *pages; | |
296f96fc RR |
68 | }; |
69 | ||
b3f24698 RR |
70 | struct skb_vnet_hdr { |
71 | union { | |
72 | struct virtio_net_hdr hdr; | |
73 | struct virtio_net_hdr_mrg_rxbuf mhdr; | |
74 | }; | |
48925e37 | 75 | unsigned int num_sg; |
b3f24698 RR |
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); |
0aea51c3 | 282 | do { |
b3f24698 | 283 | struct skb_vnet_hdr *hdr; |
3f2c31d9 | 284 | |
89d71a66 | 285 | skb = netdev_alloc_skb_ip_align(vi->dev, MAX_PACKET_LEN); |
3161e453 RR |
286 | if (unlikely(!skb)) { |
287 | oom = true; | |
296f96fc | 288 | break; |
3161e453 | 289 | } |
296f96fc RR |
290 | |
291 | skb_put(skb, MAX_PACKET_LEN); | |
3f2c31d9 MM |
292 | |
293 | hdr = skb_vnet_hdr(skb); | |
b3f24698 | 294 | sg_set_buf(sg, &hdr->hdr, sizeof(hdr->hdr)); |
97402b96 HX |
295 | |
296 | if (vi->big_packets) { | |
297 | for (i = 0; i < MAX_SKB_FRAGS; i++) { | |
298 | skb_frag_t *f = &skb_shinfo(skb)->frags[i]; | |
3161e453 | 299 | f->page = get_a_page(vi, gfp); |
97402b96 HX |
300 | if (!f->page) |
301 | break; | |
302 | ||
303 | f->page_offset = 0; | |
304 | f->size = PAGE_SIZE; | |
305 | ||
306 | skb->data_len += PAGE_SIZE; | |
307 | skb->len += PAGE_SIZE; | |
308 | ||
309 | skb_shinfo(skb)->nr_frags++; | |
310 | } | |
311 | } | |
312 | ||
296f96fc RR |
313 | num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1; |
314 | skb_queue_head(&vi->recv, skb); | |
315 | ||
316 | err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, num, skb); | |
3c1b27d5 | 317 | if (err < 0) { |
296f96fc | 318 | skb_unlink(skb, &vi->recv); |
0a888fd1 | 319 | trim_pages(vi, skb); |
296f96fc RR |
320 | kfree_skb(skb); |
321 | break; | |
322 | } | |
323 | vi->num++; | |
0aea51c3 | 324 | } while (err >= num); |
296f96fc RR |
325 | if (unlikely(vi->num > vi->max)) |
326 | vi->max = vi->num; | |
327 | vi->rvq->vq_ops->kick(vi->rvq); | |
3161e453 | 328 | return !oom; |
296f96fc RR |
329 | } |
330 | ||
3161e453 RR |
331 | /* Returns false if we couldn't fill entirely (OOM). */ |
332 | static bool try_fill_recv(struct virtnet_info *vi, gfp_t gfp) | |
3f2c31d9 MM |
333 | { |
334 | struct sk_buff *skb; | |
335 | struct scatterlist sg[1]; | |
336 | int err; | |
3161e453 | 337 | bool oom = false; |
3f2c31d9 | 338 | |
3161e453 RR |
339 | if (!vi->mergeable_rx_bufs) |
340 | return try_fill_recv_maxbufs(vi, gfp); | |
3f2c31d9 | 341 | |
0aea51c3 | 342 | do { |
3f2c31d9 MM |
343 | skb_frag_t *f; |
344 | ||
89d71a66 | 345 | skb = netdev_alloc_skb_ip_align(vi->dev, GOOD_COPY_LEN); |
3161e453 RR |
346 | if (unlikely(!skb)) { |
347 | oom = true; | |
3f2c31d9 | 348 | break; |
3161e453 | 349 | } |
3f2c31d9 | 350 | |
3f2c31d9 | 351 | f = &skb_shinfo(skb)->frags[0]; |
3161e453 | 352 | f->page = get_a_page(vi, gfp); |
3f2c31d9 | 353 | if (!f->page) { |
3161e453 | 354 | oom = true; |
3f2c31d9 MM |
355 | kfree_skb(skb); |
356 | break; | |
357 | } | |
358 | ||
359 | f->page_offset = 0; | |
360 | f->size = PAGE_SIZE; | |
361 | ||
362 | skb_shinfo(skb)->nr_frags++; | |
363 | ||
364 | sg_init_one(sg, page_address(f->page), PAGE_SIZE); | |
365 | skb_queue_head(&vi->recv, skb); | |
366 | ||
367 | err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, 1, skb); | |
3c1b27d5 | 368 | if (err < 0) { |
3f2c31d9 MM |
369 | skb_unlink(skb, &vi->recv); |
370 | kfree_skb(skb); | |
371 | break; | |
372 | } | |
373 | vi->num++; | |
0aea51c3 | 374 | } while (err > 0); |
3f2c31d9 MM |
375 | if (unlikely(vi->num > vi->max)) |
376 | vi->max = vi->num; | |
377 | vi->rvq->vq_ops->kick(vi->rvq); | |
3161e453 | 378 | return !oom; |
3f2c31d9 MM |
379 | } |
380 | ||
18445c4d | 381 | static void skb_recv_done(struct virtqueue *rvq) |
296f96fc RR |
382 | { |
383 | struct virtnet_info *vi = rvq->vdev->priv; | |
18445c4d | 384 | /* Schedule NAPI, Suppress further interrupts if successful. */ |
288379f0 | 385 | if (napi_schedule_prep(&vi->napi)) { |
18445c4d | 386 | rvq->vq_ops->disable_cb(rvq); |
288379f0 | 387 | __napi_schedule(&vi->napi); |
18445c4d | 388 | } |
296f96fc RR |
389 | } |
390 | ||
3161e453 RR |
391 | static void refill_work(struct work_struct *work) |
392 | { | |
393 | struct virtnet_info *vi; | |
394 | bool still_empty; | |
395 | ||
396 | vi = container_of(work, struct virtnet_info, refill.work); | |
397 | napi_disable(&vi->napi); | |
39d32157 | 398 | still_empty = !try_fill_recv(vi, GFP_KERNEL); |
3161e453 RR |
399 | napi_enable(&vi->napi); |
400 | ||
401 | /* In theory, this can happen: if we don't get any buffers in | |
402 | * we will *never* try to fill again. */ | |
403 | if (still_empty) | |
404 | schedule_delayed_work(&vi->refill, HZ/2); | |
405 | } | |
406 | ||
296f96fc RR |
407 | static int virtnet_poll(struct napi_struct *napi, int budget) |
408 | { | |
409 | struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi); | |
410 | struct sk_buff *skb = NULL; | |
411 | unsigned int len, received = 0; | |
412 | ||
413 | again: | |
414 | while (received < budget && | |
415 | (skb = vi->rvq->vq_ops->get_buf(vi->rvq, &len)) != NULL) { | |
416 | __skb_unlink(skb, &vi->recv); | |
417 | receive_skb(vi->dev, skb, len); | |
418 | vi->num--; | |
419 | received++; | |
420 | } | |
421 | ||
3161e453 RR |
422 | if (vi->num < vi->max / 2) { |
423 | if (!try_fill_recv(vi, GFP_ATOMIC)) | |
424 | schedule_delayed_work(&vi->refill, 0); | |
425 | } | |
296f96fc | 426 | |
8329d98e RR |
427 | /* Out of packets? */ |
428 | if (received < budget) { | |
288379f0 | 429 | napi_complete(napi); |
8e95a202 JP |
430 | if (unlikely(!vi->rvq->vq_ops->enable_cb(vi->rvq)) && |
431 | napi_schedule_prep(napi)) { | |
4265f161 | 432 | vi->rvq->vq_ops->disable_cb(vi->rvq); |
288379f0 | 433 | __napi_schedule(napi); |
296f96fc | 434 | goto again; |
4265f161 | 435 | } |
296f96fc RR |
436 | } |
437 | ||
438 | return received; | |
439 | } | |
440 | ||
48925e37 | 441 | static unsigned int free_old_xmit_skbs(struct virtnet_info *vi) |
296f96fc RR |
442 | { |
443 | struct sk_buff *skb; | |
48925e37 | 444 | unsigned int len, tot_sgs = 0; |
296f96fc RR |
445 | |
446 | while ((skb = vi->svq->vq_ops->get_buf(vi->svq, &len)) != NULL) { | |
447 | pr_debug("Sent skb %p\n", skb); | |
448 | __skb_unlink(skb, &vi->send); | |
655aa31f | 449 | vi->dev->stats.tx_bytes += skb->len; |
296f96fc | 450 | vi->dev->stats.tx_packets++; |
48925e37 | 451 | tot_sgs += skb_vnet_hdr(skb)->num_sg; |
ed79bab8 | 452 | dev_kfree_skb_any(skb); |
296f96fc | 453 | } |
48925e37 | 454 | return tot_sgs; |
296f96fc RR |
455 | } |
456 | ||
99ffc696 | 457 | static int xmit_skb(struct virtnet_info *vi, struct sk_buff *skb) |
296f96fc | 458 | { |
05271685 | 459 | struct scatterlist sg[2+MAX_SKB_FRAGS]; |
b3f24698 | 460 | struct skb_vnet_hdr *hdr = skb_vnet_hdr(skb); |
296f96fc | 461 | const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest; |
296f96fc | 462 | |
05271685 | 463 | sg_init_table(sg, 2+MAX_SKB_FRAGS); |
4d125de3 | 464 | |
e174961c | 465 | pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest); |
296f96fc | 466 | |
296f96fc | 467 | if (skb->ip_summed == CHECKSUM_PARTIAL) { |
b3f24698 RR |
468 | hdr->hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM; |
469 | hdr->hdr.csum_start = skb->csum_start - skb_headroom(skb); | |
470 | hdr->hdr.csum_offset = skb->csum_offset; | |
296f96fc | 471 | } else { |
b3f24698 RR |
472 | hdr->hdr.flags = 0; |
473 | hdr->hdr.csum_offset = hdr->hdr.csum_start = 0; | |
296f96fc RR |
474 | } |
475 | ||
476 | if (skb_is_gso(skb)) { | |
b3f24698 RR |
477 | hdr->hdr.hdr_len = skb_headlen(skb); |
478 | hdr->hdr.gso_size = skb_shinfo(skb)->gso_size; | |
34a48579 | 479 | if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) |
b3f24698 | 480 | hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4; |
296f96fc | 481 | else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) |
b3f24698 | 482 | hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6; |
296f96fc | 483 | else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP) |
b3f24698 | 484 | hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP; |
296f96fc RR |
485 | else |
486 | BUG(); | |
34a48579 | 487 | if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN) |
b3f24698 | 488 | hdr->hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN; |
296f96fc | 489 | } else { |
b3f24698 RR |
490 | hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE; |
491 | hdr->hdr.gso_size = hdr->hdr.hdr_len = 0; | |
296f96fc RR |
492 | } |
493 | ||
b3f24698 | 494 | hdr->mhdr.num_buffers = 0; |
3f2c31d9 MM |
495 | |
496 | /* Encode metadata header at front. */ | |
497 | if (vi->mergeable_rx_bufs) | |
b3f24698 | 498 | sg_set_buf(sg, &hdr->mhdr, sizeof(hdr->mhdr)); |
3f2c31d9 | 499 | else |
b3f24698 | 500 | sg_set_buf(sg, &hdr->hdr, sizeof(hdr->hdr)); |
3f2c31d9 | 501 | |
48925e37 RR |
502 | hdr->num_sg = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1; |
503 | return vi->svq->vq_ops->add_buf(vi->svq, sg, hdr->num_sg, 0, skb); | |
11a3a154 RR |
504 | } |
505 | ||
424efe9c | 506 | static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev) |
99ffc696 RR |
507 | { |
508 | struct virtnet_info *vi = netdev_priv(dev); | |
48925e37 | 509 | int capacity; |
2cb9c6ba RR |
510 | |
511 | again: | |
512 | /* Free up any pending old buffers before queueing new ones. */ | |
513 | free_old_xmit_skbs(vi); | |
99ffc696 | 514 | |
03f191ba | 515 | /* Try to transmit */ |
48925e37 RR |
516 | capacity = xmit_skb(vi, skb); |
517 | ||
518 | /* This can happen with OOM and indirect buffers. */ | |
519 | if (unlikely(capacity < 0)) { | |
520 | netif_stop_queue(dev); | |
521 | dev_warn(&dev->dev, "Unexpected full queue\n"); | |
522 | if (unlikely(!vi->svq->vq_ops->enable_cb(vi->svq))) { | |
523 | vi->svq->vq_ops->disable_cb(vi->svq); | |
524 | netif_start_queue(dev); | |
525 | goto again; | |
526 | } | |
527 | return NETDEV_TX_BUSY; | |
296f96fc | 528 | } |
48925e37 | 529 | vi->svq->vq_ops->kick(vi->svq); |
03f191ba MT |
530 | |
531 | /* | |
532 | * Put new one in send queue. You'd expect we'd need this before | |
533 | * xmit_skb calls add_buf(), since the callback can be triggered | |
534 | * immediately after that. But since the callback just triggers | |
535 | * another call back here, normal network xmit locking prevents the | |
536 | * race. | |
537 | */ | |
538 | __skb_queue_head(&vi->send, skb); | |
539 | ||
48925e37 RR |
540 | /* Don't wait up for transmitted skbs to be freed. */ |
541 | skb_orphan(skb); | |
542 | nf_reset(skb); | |
543 | ||
544 | /* Apparently nice girls don't return TX_BUSY; stop the queue | |
545 | * before it gets out of hand. Naturally, this wastes entries. */ | |
546 | if (capacity < 2+MAX_SKB_FRAGS) { | |
547 | netif_stop_queue(dev); | |
548 | if (unlikely(!vi->svq->vq_ops->enable_cb(vi->svq))) { | |
549 | /* More just got used, free them then recheck. */ | |
550 | capacity += free_old_xmit_skbs(vi); | |
551 | if (capacity >= 2+MAX_SKB_FRAGS) { | |
552 | netif_start_queue(dev); | |
553 | vi->svq->vq_ops->disable_cb(vi->svq); | |
554 | } | |
555 | } | |
99ffc696 | 556 | } |
48925e37 RR |
557 | |
558 | return NETDEV_TX_OK; | |
296f96fc RR |
559 | } |
560 | ||
9c46f6d4 AW |
561 | static int virtnet_set_mac_address(struct net_device *dev, void *p) |
562 | { | |
563 | struct virtnet_info *vi = netdev_priv(dev); | |
564 | struct virtio_device *vdev = vi->vdev; | |
565 | int ret; | |
566 | ||
567 | ret = eth_mac_addr(dev, p); | |
568 | if (ret) | |
569 | return ret; | |
570 | ||
62994b2d AW |
571 | if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) |
572 | vdev->config->set(vdev, offsetof(struct virtio_net_config, mac), | |
573 | dev->dev_addr, dev->addr_len); | |
9c46f6d4 AW |
574 | |
575 | return 0; | |
576 | } | |
577 | ||
da74e89d AS |
578 | #ifdef CONFIG_NET_POLL_CONTROLLER |
579 | static void virtnet_netpoll(struct net_device *dev) | |
580 | { | |
581 | struct virtnet_info *vi = netdev_priv(dev); | |
582 | ||
583 | napi_schedule(&vi->napi); | |
584 | } | |
585 | #endif | |
586 | ||
296f96fc RR |
587 | static int virtnet_open(struct net_device *dev) |
588 | { | |
589 | struct virtnet_info *vi = netdev_priv(dev); | |
590 | ||
296f96fc | 591 | napi_enable(&vi->napi); |
a48bd8f6 RR |
592 | |
593 | /* If all buffers were filled by other side before we napi_enabled, we | |
594 | * won't get another interrupt, so process any outstanding packets | |
370076d9 CB |
595 | * now. virtnet_poll wants re-enable the queue, so we disable here. |
596 | * We synchronize against interrupts via NAPI_STATE_SCHED */ | |
288379f0 | 597 | if (napi_schedule_prep(&vi->napi)) { |
370076d9 | 598 | vi->rvq->vq_ops->disable_cb(vi->rvq); |
288379f0 | 599 | __napi_schedule(&vi->napi); |
370076d9 | 600 | } |
296f96fc RR |
601 | return 0; |
602 | } | |
603 | ||
2a41f71d AW |
604 | /* |
605 | * Send command via the control virtqueue and check status. Commands | |
606 | * supported by the hypervisor, as indicated by feature bits, should | |
607 | * never fail unless improperly formated. | |
608 | */ | |
609 | static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd, | |
610 | struct scatterlist *data, int out, int in) | |
611 | { | |
23e258e1 | 612 | struct scatterlist *s, sg[VIRTNET_SEND_COMMAND_SG_MAX + 2]; |
2a41f71d AW |
613 | struct virtio_net_ctrl_hdr ctrl; |
614 | virtio_net_ctrl_ack status = ~0; | |
615 | unsigned int tmp; | |
23e258e1 | 616 | int i; |
2a41f71d | 617 | |
0ee904c3 AB |
618 | /* Caller should know better */ |
619 | BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) || | |
620 | (out + in > VIRTNET_SEND_COMMAND_SG_MAX)); | |
2a41f71d AW |
621 | |
622 | out++; /* Add header */ | |
623 | in++; /* Add return status */ | |
624 | ||
625 | ctrl.class = class; | |
626 | ctrl.cmd = cmd; | |
627 | ||
628 | sg_init_table(sg, out + in); | |
629 | ||
630 | sg_set_buf(&sg[0], &ctrl, sizeof(ctrl)); | |
23e258e1 AW |
631 | for_each_sg(data, s, out + in - 2, i) |
632 | sg_set_buf(&sg[i + 1], sg_virt(s), s->length); | |
2a41f71d AW |
633 | sg_set_buf(&sg[out + in - 1], &status, sizeof(status)); |
634 | ||
3c1b27d5 | 635 | BUG_ON(vi->cvq->vq_ops->add_buf(vi->cvq, sg, out, in, vi) < 0); |
2a41f71d AW |
636 | |
637 | vi->cvq->vq_ops->kick(vi->cvq); | |
638 | ||
639 | /* | |
640 | * Spin for a response, the kick causes an ioport write, trapping | |
641 | * into the hypervisor, so the request should be handled immediately. | |
642 | */ | |
643 | while (!vi->cvq->vq_ops->get_buf(vi->cvq, &tmp)) | |
644 | cpu_relax(); | |
645 | ||
646 | return status == VIRTIO_NET_OK; | |
647 | } | |
648 | ||
296f96fc RR |
649 | static int virtnet_close(struct net_device *dev) |
650 | { | |
651 | struct virtnet_info *vi = netdev_priv(dev); | |
296f96fc RR |
652 | |
653 | napi_disable(&vi->napi); | |
654 | ||
296f96fc RR |
655 | return 0; |
656 | } | |
657 | ||
a9ea3fc6 HX |
658 | static int virtnet_set_tx_csum(struct net_device *dev, u32 data) |
659 | { | |
660 | struct virtnet_info *vi = netdev_priv(dev); | |
661 | struct virtio_device *vdev = vi->vdev; | |
662 | ||
663 | if (data && !virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) | |
664 | return -ENOSYS; | |
665 | ||
666 | return ethtool_op_set_tx_hw_csum(dev, data); | |
667 | } | |
668 | ||
2af7698e AW |
669 | static void virtnet_set_rx_mode(struct net_device *dev) |
670 | { | |
671 | struct virtnet_info *vi = netdev_priv(dev); | |
f565a7c2 | 672 | struct scatterlist sg[2]; |
2af7698e | 673 | u8 promisc, allmulti; |
f565a7c2 AW |
674 | struct virtio_net_ctrl_mac *mac_data; |
675 | struct dev_addr_list *addr; | |
ccffad25 | 676 | struct netdev_hw_addr *ha; |
f565a7c2 AW |
677 | void *buf; |
678 | int i; | |
2af7698e AW |
679 | |
680 | /* We can't dynamicaly set ndo_set_rx_mode, so return gracefully */ | |
681 | if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX)) | |
682 | return; | |
683 | ||
f565a7c2 AW |
684 | promisc = ((dev->flags & IFF_PROMISC) != 0); |
685 | allmulti = ((dev->flags & IFF_ALLMULTI) != 0); | |
2af7698e | 686 | |
23e258e1 | 687 | sg_init_one(sg, &promisc, sizeof(promisc)); |
2af7698e AW |
688 | |
689 | if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, | |
690 | VIRTIO_NET_CTRL_RX_PROMISC, | |
f565a7c2 | 691 | sg, 1, 0)) |
2af7698e AW |
692 | dev_warn(&dev->dev, "Failed to %sable promisc mode.\n", |
693 | promisc ? "en" : "dis"); | |
694 | ||
23e258e1 | 695 | sg_init_one(sg, &allmulti, sizeof(allmulti)); |
2af7698e AW |
696 | |
697 | if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, | |
698 | VIRTIO_NET_CTRL_RX_ALLMULTI, | |
f565a7c2 | 699 | sg, 1, 0)) |
2af7698e AW |
700 | dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n", |
701 | allmulti ? "en" : "dis"); | |
f565a7c2 AW |
702 | |
703 | /* MAC filter - use one buffer for both lists */ | |
31278e71 | 704 | mac_data = buf = kzalloc(((dev->uc.count + dev->mc_count) * ETH_ALEN) + |
f565a7c2 AW |
705 | (2 * sizeof(mac_data->entries)), GFP_ATOMIC); |
706 | if (!buf) { | |
707 | dev_warn(&dev->dev, "No memory for MAC address buffer\n"); | |
708 | return; | |
709 | } | |
710 | ||
23e258e1 AW |
711 | sg_init_table(sg, 2); |
712 | ||
f565a7c2 | 713 | /* Store the unicast list and count in the front of the buffer */ |
31278e71 | 714 | mac_data->entries = dev->uc.count; |
ccffad25 | 715 | i = 0; |
31278e71 | 716 | list_for_each_entry(ha, &dev->uc.list, list) |
ccffad25 | 717 | memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN); |
f565a7c2 AW |
718 | |
719 | sg_set_buf(&sg[0], mac_data, | |
31278e71 | 720 | sizeof(mac_data->entries) + (dev->uc.count * ETH_ALEN)); |
f565a7c2 AW |
721 | |
722 | /* multicast list and count fill the end */ | |
31278e71 | 723 | mac_data = (void *)&mac_data->macs[dev->uc.count][0]; |
f565a7c2 AW |
724 | |
725 | mac_data->entries = dev->mc_count; | |
726 | addr = dev->mc_list; | |
727 | for (i = 0; i < dev->mc_count; i++, addr = addr->next) | |
728 | memcpy(&mac_data->macs[i][0], addr->da_addr, ETH_ALEN); | |
729 | ||
730 | sg_set_buf(&sg[1], mac_data, | |
731 | sizeof(mac_data->entries) + (dev->mc_count * ETH_ALEN)); | |
732 | ||
733 | if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, | |
734 | VIRTIO_NET_CTRL_MAC_TABLE_SET, | |
735 | sg, 2, 0)) | |
736 | dev_warn(&dev->dev, "Failed to set MAC fitler table.\n"); | |
737 | ||
738 | kfree(buf); | |
2af7698e AW |
739 | } |
740 | ||
1824a989 | 741 | static void virtnet_vlan_rx_add_vid(struct net_device *dev, u16 vid) |
0bde9569 AW |
742 | { |
743 | struct virtnet_info *vi = netdev_priv(dev); | |
744 | struct scatterlist sg; | |
745 | ||
23e258e1 | 746 | sg_init_one(&sg, &vid, sizeof(vid)); |
0bde9569 AW |
747 | |
748 | if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, | |
749 | VIRTIO_NET_CTRL_VLAN_ADD, &sg, 1, 0)) | |
750 | dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid); | |
751 | } | |
752 | ||
1824a989 | 753 | static void virtnet_vlan_rx_kill_vid(struct net_device *dev, u16 vid) |
0bde9569 AW |
754 | { |
755 | struct virtnet_info *vi = netdev_priv(dev); | |
756 | struct scatterlist sg; | |
757 | ||
23e258e1 | 758 | sg_init_one(&sg, &vid, sizeof(vid)); |
0bde9569 AW |
759 | |
760 | if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, | |
761 | VIRTIO_NET_CTRL_VLAN_DEL, &sg, 1, 0)) | |
762 | dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid); | |
763 | } | |
764 | ||
0fc0b732 | 765 | static const struct ethtool_ops virtnet_ethtool_ops = { |
a9ea3fc6 HX |
766 | .set_tx_csum = virtnet_set_tx_csum, |
767 | .set_sg = ethtool_op_set_sg, | |
0276b497 | 768 | .set_tso = ethtool_op_set_tso, |
5c516751 | 769 | .set_ufo = ethtool_op_set_ufo, |
9f4d26d0 | 770 | .get_link = ethtool_op_get_link, |
a9ea3fc6 HX |
771 | }; |
772 | ||
39da5814 MM |
773 | #define MIN_MTU 68 |
774 | #define MAX_MTU 65535 | |
775 | ||
776 | static int virtnet_change_mtu(struct net_device *dev, int new_mtu) | |
777 | { | |
778 | if (new_mtu < MIN_MTU || new_mtu > MAX_MTU) | |
779 | return -EINVAL; | |
780 | dev->mtu = new_mtu; | |
781 | return 0; | |
782 | } | |
783 | ||
76288b4e SH |
784 | static const struct net_device_ops virtnet_netdev = { |
785 | .ndo_open = virtnet_open, | |
786 | .ndo_stop = virtnet_close, | |
787 | .ndo_start_xmit = start_xmit, | |
788 | .ndo_validate_addr = eth_validate_addr, | |
9c46f6d4 | 789 | .ndo_set_mac_address = virtnet_set_mac_address, |
2af7698e | 790 | .ndo_set_rx_mode = virtnet_set_rx_mode, |
76288b4e | 791 | .ndo_change_mtu = virtnet_change_mtu, |
1824a989 AW |
792 | .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid, |
793 | .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid, | |
76288b4e SH |
794 | #ifdef CONFIG_NET_POLL_CONTROLLER |
795 | .ndo_poll_controller = virtnet_netpoll, | |
796 | #endif | |
797 | }; | |
798 | ||
9f4d26d0 MM |
799 | static void virtnet_update_status(struct virtnet_info *vi) |
800 | { | |
801 | u16 v; | |
802 | ||
803 | if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) | |
804 | return; | |
805 | ||
806 | vi->vdev->config->get(vi->vdev, | |
807 | offsetof(struct virtio_net_config, status), | |
808 | &v, sizeof(v)); | |
809 | ||
810 | /* Ignore unknown (future) status bits */ | |
811 | v &= VIRTIO_NET_S_LINK_UP; | |
812 | ||
813 | if (vi->status == v) | |
814 | return; | |
815 | ||
816 | vi->status = v; | |
817 | ||
818 | if (vi->status & VIRTIO_NET_S_LINK_UP) { | |
819 | netif_carrier_on(vi->dev); | |
820 | netif_wake_queue(vi->dev); | |
821 | } else { | |
822 | netif_carrier_off(vi->dev); | |
823 | netif_stop_queue(vi->dev); | |
824 | } | |
825 | } | |
826 | ||
827 | static void virtnet_config_changed(struct virtio_device *vdev) | |
828 | { | |
829 | struct virtnet_info *vi = vdev->priv; | |
830 | ||
831 | virtnet_update_status(vi); | |
832 | } | |
833 | ||
296f96fc RR |
834 | static int virtnet_probe(struct virtio_device *vdev) |
835 | { | |
836 | int err; | |
296f96fc RR |
837 | struct net_device *dev; |
838 | struct virtnet_info *vi; | |
d2a7ddda MT |
839 | struct virtqueue *vqs[3]; |
840 | vq_callback_t *callbacks[] = { skb_recv_done, skb_xmit_done, NULL}; | |
841 | const char *names[] = { "input", "output", "control" }; | |
842 | int nvqs; | |
296f96fc RR |
843 | |
844 | /* Allocate ourselves a network device with room for our info */ | |
845 | dev = alloc_etherdev(sizeof(struct virtnet_info)); | |
846 | if (!dev) | |
847 | return -ENOMEM; | |
848 | ||
849 | /* Set up network device as normal. */ | |
76288b4e | 850 | dev->netdev_ops = &virtnet_netdev; |
296f96fc | 851 | dev->features = NETIF_F_HIGHDMA; |
a9ea3fc6 | 852 | SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops); |
296f96fc RR |
853 | SET_NETDEV_DEV(dev, &vdev->dev); |
854 | ||
855 | /* Do we support "hardware" checksums? */ | |
c45a6816 | 856 | if (csum && virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) { |
296f96fc RR |
857 | /* This opens up the world of extra features. */ |
858 | dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST; | |
c45a6816 | 859 | if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) { |
34a48579 RR |
860 | dev->features |= NETIF_F_TSO | NETIF_F_UFO |
861 | | NETIF_F_TSO_ECN | NETIF_F_TSO6; | |
862 | } | |
5539ae96 | 863 | /* Individual feature bits: what can host handle? */ |
c45a6816 | 864 | if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4)) |
5539ae96 | 865 | dev->features |= NETIF_F_TSO; |
c45a6816 | 866 | if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6)) |
5539ae96 | 867 | dev->features |= NETIF_F_TSO6; |
c45a6816 | 868 | if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN)) |
5539ae96 | 869 | dev->features |= NETIF_F_TSO_ECN; |
c45a6816 | 870 | if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO)) |
5539ae96 | 871 | dev->features |= NETIF_F_UFO; |
296f96fc RR |
872 | } |
873 | ||
874 | /* Configuration may specify what MAC to use. Otherwise random. */ | |
c45a6816 | 875 | if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) { |
a586d4f6 RR |
876 | vdev->config->get(vdev, |
877 | offsetof(struct virtio_net_config, mac), | |
878 | dev->dev_addr, dev->addr_len); | |
62994b2d | 879 | } else |
296f96fc RR |
880 | random_ether_addr(dev->dev_addr); |
881 | ||
882 | /* Set up our device-specific information */ | |
883 | vi = netdev_priv(dev); | |
6c0cd7c0 | 884 | netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight); |
296f96fc RR |
885 | vi->dev = dev; |
886 | vi->vdev = vdev; | |
d9d5dcc8 | 887 | vdev->priv = vi; |
fb6813f4 | 888 | vi->pages = NULL; |
3161e453 | 889 | INIT_DELAYED_WORK(&vi->refill, refill_work); |
296f96fc | 890 | |
97402b96 | 891 | /* If we can receive ANY GSO packets, we must allocate large ones. */ |
8e95a202 JP |
892 | if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) || |
893 | virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) || | |
894 | virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN)) | |
97402b96 HX |
895 | vi->big_packets = true; |
896 | ||
3f2c31d9 MM |
897 | if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF)) |
898 | vi->mergeable_rx_bufs = true; | |
899 | ||
d2a7ddda MT |
900 | /* We expect two virtqueues, receive then send, |
901 | * and optionally control. */ | |
902 | nvqs = virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) ? 3 : 2; | |
903 | ||
904 | err = vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names); | |
905 | if (err) | |
296f96fc | 906 | goto free; |
296f96fc | 907 | |
d2a7ddda MT |
908 | vi->rvq = vqs[0]; |
909 | vi->svq = vqs[1]; | |
296f96fc | 910 | |
2a41f71d | 911 | if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)) { |
d2a7ddda | 912 | vi->cvq = vqs[2]; |
0bde9569 AW |
913 | |
914 | if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN)) | |
915 | dev->features |= NETIF_F_HW_VLAN_FILTER; | |
2a41f71d AW |
916 | } |
917 | ||
296f96fc RR |
918 | /* Initialize our empty receive and send queues. */ |
919 | skb_queue_head_init(&vi->recv); | |
920 | skb_queue_head_init(&vi->send); | |
921 | ||
922 | err = register_netdev(dev); | |
923 | if (err) { | |
924 | pr_debug("virtio_net: registering device failed\n"); | |
d2a7ddda | 925 | goto free_vqs; |
296f96fc | 926 | } |
b3369c1f RR |
927 | |
928 | /* Last of all, set up some receive buffers. */ | |
3161e453 | 929 | try_fill_recv(vi, GFP_KERNEL); |
b3369c1f RR |
930 | |
931 | /* If we didn't even get one input buffer, we're useless. */ | |
932 | if (vi->num == 0) { | |
933 | err = -ENOMEM; | |
934 | goto unregister; | |
935 | } | |
936 | ||
9f4d26d0 MM |
937 | vi->status = VIRTIO_NET_S_LINK_UP; |
938 | virtnet_update_status(vi); | |
4783256e | 939 | netif_carrier_on(dev); |
9f4d26d0 | 940 | |
296f96fc | 941 | pr_debug("virtnet: registered device %s\n", dev->name); |
296f96fc RR |
942 | return 0; |
943 | ||
b3369c1f RR |
944 | unregister: |
945 | unregister_netdev(dev); | |
3161e453 | 946 | cancel_delayed_work_sync(&vi->refill); |
d2a7ddda MT |
947 | free_vqs: |
948 | vdev->config->del_vqs(vdev); | |
296f96fc RR |
949 | free: |
950 | free_netdev(dev); | |
951 | return err; | |
952 | } | |
953 | ||
3d1285be | 954 | static void __devexit virtnet_remove(struct virtio_device *vdev) |
296f96fc | 955 | { |
74b2553f | 956 | struct virtnet_info *vi = vdev->priv; |
b3369c1f RR |
957 | struct sk_buff *skb; |
958 | ||
6e5aa7ef RR |
959 | /* Stop all the virtqueues. */ |
960 | vdev->config->reset(vdev); | |
961 | ||
b3369c1f | 962 | /* Free our skbs in send and recv queues, if any. */ |
b3369c1f RR |
963 | while ((skb = __skb_dequeue(&vi->recv)) != NULL) { |
964 | kfree_skb(skb); | |
965 | vi->num--; | |
966 | } | |
288369cc | 967 | __skb_queue_purge(&vi->send); |
b3369c1f RR |
968 | |
969 | BUG_ON(vi->num != 0); | |
74b2553f | 970 | |
74b2553f | 971 | unregister_netdev(vi->dev); |
3161e453 | 972 | cancel_delayed_work_sync(&vi->refill); |
fb6813f4 | 973 | |
d2a7ddda MT |
974 | vdev->config->del_vqs(vi->vdev); |
975 | ||
fb6813f4 RR |
976 | while (vi->pages) |
977 | __free_pages(get_a_page(vi, GFP_KERNEL), 0); | |
978 | ||
74b2553f | 979 | free_netdev(vi->dev); |
296f96fc RR |
980 | } |
981 | ||
982 | static struct virtio_device_id id_table[] = { | |
983 | { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID }, | |
984 | { 0 }, | |
985 | }; | |
986 | ||
c45a6816 | 987 | static unsigned int features[] = { |
5e4fe5c4 MM |
988 | VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, |
989 | VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC, | |
c45a6816 | 990 | VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, |
97402b96 | 991 | VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, |
5c516751 | 992 | VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, |
2a41f71d | 993 | VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, |
0bde9569 | 994 | VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, |
c45a6816 RR |
995 | }; |
996 | ||
22402529 | 997 | static struct virtio_driver virtio_net_driver = { |
c45a6816 RR |
998 | .feature_table = features, |
999 | .feature_table_size = ARRAY_SIZE(features), | |
296f96fc RR |
1000 | .driver.name = KBUILD_MODNAME, |
1001 | .driver.owner = THIS_MODULE, | |
1002 | .id_table = id_table, | |
1003 | .probe = virtnet_probe, | |
1004 | .remove = __devexit_p(virtnet_remove), | |
9f4d26d0 | 1005 | .config_changed = virtnet_config_changed, |
296f96fc RR |
1006 | }; |
1007 | ||
1008 | static int __init init(void) | |
1009 | { | |
22402529 | 1010 | return register_virtio_driver(&virtio_net_driver); |
296f96fc RR |
1011 | } |
1012 | ||
1013 | static void __exit fini(void) | |
1014 | { | |
22402529 | 1015 | unregister_virtio_driver(&virtio_net_driver); |
296f96fc RR |
1016 | } |
1017 | module_init(init); | |
1018 | module_exit(fini); | |
1019 | ||
1020 | MODULE_DEVICE_TABLE(virtio, id_table); | |
1021 | MODULE_DESCRIPTION("Virtio network driver"); | |
1022 | MODULE_LICENSE("GPL"); |