net: handle error more gracefully in socketpair()
[deliverable/linux.git] / drivers / net / virtio_net.c
CommitLineData
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
adf8d3ff 16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
296f96fc
RR
17 */
18//#define DEBUG
19#include <linux/netdevice.h>
20#include <linux/etherdevice.h>
a9ea3fc6 21#include <linux/ethtool.h>
296f96fc
RR
22#include <linux/module.h>
23#include <linux/virtio.h>
24#include <linux/virtio_net.h>
25#include <linux/scatterlist.h>
e918085a 26#include <linux/if_vlan.h>
5a0e3ad6 27#include <linux/slab.h>
8de4b2f3 28#include <linux/cpu.h>
296f96fc 29
d34710e3 30static int napi_weight = NAPI_POLL_WEIGHT;
6c0cd7c0
DL
31module_param(napi_weight, int, 0444);
32
eb939922 33static bool csum = true, gso = true;
34a48579
RR
34module_param(csum, bool, 0444);
35module_param(gso, bool, 0444);
36
296f96fc 37/* FIXME: MTU in config. */
5061de36
MD
38#define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
39#define MERGE_BUFFER_LEN (ALIGN(GOOD_PACKET_LEN + \
40 sizeof(struct virtio_net_hdr_mrg_rxbuf), \
41 L1_CACHE_BYTES))
3f2c31d9 42#define GOOD_COPY_LEN 128
296f96fc 43
66846048 44#define VIRTNET_DRIVER_VERSION "1.0.0"
2a41f71d 45
3fa2a1df 46struct virtnet_stats {
83a27052
ED
47 struct u64_stats_sync tx_syncp;
48 struct u64_stats_sync rx_syncp;
3fa2a1df 49 u64 tx_bytes;
50 u64 tx_packets;
51
52 u64 rx_bytes;
53 u64 rx_packets;
54};
55
e9d7417b
JW
56/* Internal representation of a send virtqueue */
57struct send_queue {
58 /* Virtqueue associated with this send _queue */
59 struct virtqueue *vq;
60
61 /* TX: fragments + linear part + virtio header */
62 struct scatterlist sg[MAX_SKB_FRAGS + 2];
986a4f4d
JW
63
64 /* Name of the send queue: output.$index */
65 char name[40];
e9d7417b
JW
66};
67
68/* Internal representation of a receive virtqueue */
69struct receive_queue {
70 /* Virtqueue associated with this receive_queue */
71 struct virtqueue *vq;
72
296f96fc
RR
73 struct napi_struct napi;
74
75 /* Number of input buffers, and max we've ever had. */
76 unsigned int num, max;
77
e9d7417b
JW
78 /* Chain pages by the private ptr. */
79 struct page *pages;
80
81 /* RX: fragments + linear part + virtio header */
82 struct scatterlist sg[MAX_SKB_FRAGS + 2];
986a4f4d
JW
83
84 /* Name of this receive queue: input.$index */
85 char name[40];
e9d7417b
JW
86};
87
88struct virtnet_info {
89 struct virtio_device *vdev;
90 struct virtqueue *cvq;
91 struct net_device *dev;
986a4f4d
JW
92 struct send_queue *sq;
93 struct receive_queue *rq;
e9d7417b
JW
94 unsigned int status;
95
986a4f4d
JW
96 /* Max # of queue pairs supported by the device */
97 u16 max_queue_pairs;
98
99 /* # of queue pairs currently used by the driver */
100 u16 curr_queue_pairs;
101
97402b96
HX
102 /* I like... big packets and I cannot lie! */
103 bool big_packets;
104
3f2c31d9
MM
105 /* Host will merge rx buffers for big packets (shake it! shake it!) */
106 bool mergeable_rx_bufs;
107
986a4f4d
JW
108 /* Has control virtqueue */
109 bool has_cvq;
110
e7428e95
MT
111 /* Host can handle any s/g split between our header and packet data */
112 bool any_header_sg;
113
586d17c5
JW
114 /* enable config space updates */
115 bool config_enable;
116
3fa2a1df 117 /* Active statistics */
118 struct virtnet_stats __percpu *stats;
119
3161e453
RR
120 /* Work struct for refilling if we run low on memory. */
121 struct delayed_work refill;
122
586d17c5
JW
123 /* Work struct for config space updates */
124 struct work_struct config_work;
125
126 /* Lock for config space updates */
127 struct mutex config_lock;
986a4f4d 128
2613af0e
MD
129 /* Page_frag for GFP_KERNEL packet buffer allocation when we run
130 * low on memory.
131 */
132 struct page_frag alloc_frag;
133
986a4f4d
JW
134 /* Does the affinity hint is set for virtqueues? */
135 bool affinity_hint_set;
47be2479 136
8de4b2f3
WG
137 /* CPU hot plug notifier */
138 struct notifier_block nb;
296f96fc
RR
139};
140
b3f24698
RR
141struct skb_vnet_hdr {
142 union {
143 struct virtio_net_hdr hdr;
144 struct virtio_net_hdr_mrg_rxbuf mhdr;
145 };
146};
147
9ab86bbc
SM
148struct padded_vnet_hdr {
149 struct virtio_net_hdr hdr;
150 /*
151 * virtio_net_hdr should be in a separated sg buffer because of a
152 * QEMU bug, and data sg buffer shares same page with this header sg.
153 * This padding makes next sg 16 byte aligned after virtio_net_hdr.
154 */
155 char padding[6];
156};
157
986a4f4d
JW
158/* Converting between virtqueue no. and kernel tx/rx queue no.
159 * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
160 */
161static int vq2txq(struct virtqueue *vq)
162{
9d0ca6ed 163 return (vq->index - 1) / 2;
986a4f4d
JW
164}
165
166static int txq2vq(int txq)
167{
168 return txq * 2 + 1;
169}
170
171static int vq2rxq(struct virtqueue *vq)
172{
9d0ca6ed 173 return vq->index / 2;
986a4f4d
JW
174}
175
176static int rxq2vq(int rxq)
177{
178 return rxq * 2;
179}
180
b3f24698 181static inline struct skb_vnet_hdr *skb_vnet_hdr(struct sk_buff *skb)
296f96fc 182{
b3f24698 183 return (struct skb_vnet_hdr *)skb->cb;
296f96fc
RR
184}
185
9ab86bbc
SM
186/*
187 * private is used to chain pages for big packets, put the whole
188 * most recent used list in the beginning for reuse
189 */
e9d7417b 190static void give_pages(struct receive_queue *rq, struct page *page)
0a888fd1 191{
9ab86bbc 192 struct page *end;
0a888fd1 193
e9d7417b 194 /* Find end of list, sew whole thing into vi->rq.pages. */
9ab86bbc 195 for (end = page; end->private; end = (struct page *)end->private);
e9d7417b
JW
196 end->private = (unsigned long)rq->pages;
197 rq->pages = page;
0a888fd1
MM
198}
199
e9d7417b 200static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
fb6813f4 201{
e9d7417b 202 struct page *p = rq->pages;
fb6813f4 203
9ab86bbc 204 if (p) {
e9d7417b 205 rq->pages = (struct page *)p->private;
9ab86bbc
SM
206 /* clear private here, it is used to chain pages */
207 p->private = 0;
208 } else
fb6813f4
RR
209 p = alloc_page(gfp_mask);
210 return p;
211}
212
e9d7417b 213static void skb_xmit_done(struct virtqueue *vq)
296f96fc 214{
e9d7417b 215 struct virtnet_info *vi = vq->vdev->priv;
296f96fc 216
2cb9c6ba 217 /* Suppress further interrupts. */
e9d7417b 218 virtqueue_disable_cb(vq);
11a3a154 219
363f1514 220 /* We were probably waiting for more output buffers. */
986a4f4d 221 netif_wake_subqueue(vi->dev, vq2txq(vq));
296f96fc
RR
222}
223
3464645a 224/* Called from bottom half context */
e9d7417b 225static struct sk_buff *page_to_skb(struct receive_queue *rq,
2613af0e
MD
226 struct page *page, unsigned int offset,
227 unsigned int len, unsigned int truesize)
9ab86bbc 228{
e9d7417b 229 struct virtnet_info *vi = rq->vq->vdev->priv;
9ab86bbc
SM
230 struct sk_buff *skb;
231 struct skb_vnet_hdr *hdr;
2613af0e 232 unsigned int copy, hdr_len, hdr_padded_len;
9ab86bbc 233 char *p;
fb6813f4 234
2613af0e 235 p = page_address(page) + offset;
3f2c31d9 236
9ab86bbc
SM
237 /* copy small packet so we can reuse these pages for small data */
238 skb = netdev_alloc_skb_ip_align(vi->dev, GOOD_COPY_LEN);
239 if (unlikely(!skb))
240 return NULL;
3f2c31d9 241
9ab86bbc 242 hdr = skb_vnet_hdr(skb);
3f2c31d9 243
9ab86bbc
SM
244 if (vi->mergeable_rx_bufs) {
245 hdr_len = sizeof hdr->mhdr;
2613af0e 246 hdr_padded_len = sizeof hdr->mhdr;
9ab86bbc
SM
247 } else {
248 hdr_len = sizeof hdr->hdr;
2613af0e 249 hdr_padded_len = sizeof(struct padded_vnet_hdr);
9ab86bbc 250 }
3f2c31d9 251
9ab86bbc 252 memcpy(hdr, p, hdr_len);
3f2c31d9 253
9ab86bbc 254 len -= hdr_len;
2613af0e
MD
255 offset += hdr_padded_len;
256 p += hdr_padded_len;
3f2c31d9 257
9ab86bbc
SM
258 copy = len;
259 if (copy > skb_tailroom(skb))
260 copy = skb_tailroom(skb);
261 memcpy(skb_put(skb, copy), p, copy);
3f2c31d9 262
9ab86bbc
SM
263 len -= copy;
264 offset += copy;
3f2c31d9 265
2613af0e
MD
266 if (vi->mergeable_rx_bufs) {
267 if (len)
268 skb_add_rx_frag(skb, 0, page, offset, len, truesize);
269 else
270 put_page(page);
271 return skb;
272 }
273
e878d78b
SL
274 /*
275 * Verify that we can indeed put this data into a skb.
276 * This is here to handle cases when the device erroneously
277 * tries to receive more than is possible. This is usually
278 * the case of a broken device.
279 */
280 if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
be443899 281 net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
e878d78b
SL
282 dev_kfree_skb(skb);
283 return NULL;
284 }
2613af0e 285 BUG_ON(offset >= PAGE_SIZE);
9ab86bbc 286 while (len) {
2613af0e
MD
287 unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
288 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
289 frag_size, truesize);
290 len -= frag_size;
9ab86bbc
SM
291 page = (struct page *)page->private;
292 offset = 0;
293 }
3f2c31d9 294
9ab86bbc 295 if (page)
e9d7417b 296 give_pages(rq, page);
3f2c31d9 297
9ab86bbc
SM
298 return skb;
299}
3f2c31d9 300
f121159d
MT
301static struct sk_buff *receive_small(void *buf, unsigned int len)
302{
303 struct sk_buff * skb = buf;
304
305 len -= sizeof(struct virtio_net_hdr);
306 skb_trim(skb, len);
307
308 return skb;
309}
310
311static struct sk_buff *receive_big(struct net_device *dev,
312 struct receive_queue *rq,
313 void *buf,
314 unsigned int len)
315{
316 struct page *page = buf;
317 struct sk_buff *skb = page_to_skb(rq, page, 0, len, PAGE_SIZE);
318
319 if (unlikely(!skb))
320 goto err;
321
322 return skb;
323
324err:
325 dev->stats.rx_dropped++;
326 give_pages(rq, page);
327 return NULL;
328}
329
8fc3b9e9
MT
330static struct sk_buff *receive_mergeable(struct net_device *dev,
331 struct receive_queue *rq,
332 void *buf,
333 unsigned int len)
9ab86bbc 334{
8fc3b9e9
MT
335 struct skb_vnet_hdr *hdr = buf;
336 int num_buf = hdr->mhdr.num_buffers;
337 struct page *page = virt_to_head_page(buf);
338 int offset = buf - page_address(page);
339 struct sk_buff *head_skb = page_to_skb(rq, page, offset, len,
340 MERGE_BUFFER_LEN);
2613af0e 341 struct sk_buff *curr_skb = head_skb;
9ab86bbc 342
8fc3b9e9
MT
343 if (unlikely(!curr_skb))
344 goto err_skb;
345
9ab86bbc 346 while (--num_buf) {
8fc3b9e9
MT
347 int num_skb_frags;
348
2613af0e
MD
349 buf = virtqueue_get_buf(rq->vq, &len);
350 if (unlikely(!buf)) {
8fc3b9e9
MT
351 pr_debug("%s: rx error: %d buffers out of %d missing\n",
352 dev->name, num_buf, hdr->mhdr.num_buffers);
353 dev->stats.rx_length_errors++;
354 goto err_buf;
3f2c31d9 355 }
5061de36 356 if (unlikely(len > MERGE_BUFFER_LEN)) {
2613af0e 357 pr_debug("%s: rx error: merge buffer too long\n",
8fc3b9e9 358 dev->name);
5061de36 359 len = MERGE_BUFFER_LEN;
2613af0e 360 }
8fc3b9e9
MT
361
362 page = virt_to_head_page(buf);
363 --rq->num;
364
365 num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
2613af0e
MD
366 if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
367 struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
8fc3b9e9
MT
368
369 if (unlikely(!nskb))
370 goto err_skb;
2613af0e
MD
371 if (curr_skb == head_skb)
372 skb_shinfo(curr_skb)->frag_list = nskb;
373 else
374 curr_skb->next = nskb;
375 curr_skb = nskb;
376 head_skb->truesize += nskb->truesize;
377 num_skb_frags = 0;
378 }
379 if (curr_skb != head_skb) {
380 head_skb->data_len += len;
381 head_skb->len += len;
5061de36 382 head_skb->truesize += MERGE_BUFFER_LEN;
2613af0e 383 }
8fc3b9e9 384 offset = buf - page_address(page);
ba275241
JW
385 if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
386 put_page(page);
387 skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
5061de36 388 len, MERGE_BUFFER_LEN);
ba275241
JW
389 } else {
390 skb_add_rx_frag(curr_skb, num_skb_frags, page,
5061de36 391 offset, len, MERGE_BUFFER_LEN);
ba275241 392 }
8fc3b9e9
MT
393 }
394
395 return head_skb;
396
397err_skb:
398 put_page(page);
399 while (--num_buf) {
400 buf = virtqueue_get_buf(rq->vq, &len);
401 if (unlikely(!buf)) {
402 pr_debug("%s: rx error: %d buffers missing\n",
403 dev->name, num_buf);
404 dev->stats.rx_length_errors++;
405 break;
406 }
407 page = virt_to_head_page(buf);
408 put_page(page);
e9d7417b 409 --rq->num;
9ab86bbc 410 }
8fc3b9e9
MT
411err_buf:
412 dev->stats.rx_dropped++;
413 dev_kfree_skb(head_skb);
414 return NULL;
9ab86bbc
SM
415}
416
e9d7417b 417static void receive_buf(struct receive_queue *rq, void *buf, unsigned int len)
9ab86bbc 418{
e9d7417b
JW
419 struct virtnet_info *vi = rq->vq->vdev->priv;
420 struct net_device *dev = vi->dev;
58472a76 421 struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
9ab86bbc 422 struct sk_buff *skb;
9ab86bbc 423 struct skb_vnet_hdr *hdr;
3f2c31d9 424
9ab86bbc
SM
425 if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) {
426 pr_debug("%s: short packet %i\n", dev->name, len);
427 dev->stats.rx_length_errors++;
98bfd23c 428 if (vi->mergeable_rx_bufs)
2613af0e 429 put_page(virt_to_head_page(buf));
98bfd23c
MD
430 else if (vi->big_packets)
431 give_pages(rq, buf);
9ab86bbc
SM
432 else
433 dev_kfree_skb(buf);
434 return;
435 }
3f2c31d9 436
f121159d 437 if (vi->mergeable_rx_bufs)
8fc3b9e9 438 skb = receive_mergeable(dev, rq, buf, len);
f121159d
MT
439 else if (vi->big_packets)
440 skb = receive_big(dev, rq, buf, len);
441 else
442 skb = receive_small(buf, len);
443
444 if (unlikely(!skb))
445 return;
3f2c31d9 446
9ab86bbc 447 hdr = skb_vnet_hdr(skb);
3fa2a1df 448
83a27052 449 u64_stats_update_begin(&stats->rx_syncp);
3fa2a1df 450 stats->rx_bytes += skb->len;
451 stats->rx_packets++;
83a27052 452 u64_stats_update_end(&stats->rx_syncp);
296f96fc 453
b3f24698 454 if (hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
296f96fc 455 pr_debug("Needs csum!\n");
b3f24698
RR
456 if (!skb_partial_csum_set(skb,
457 hdr->hdr.csum_start,
458 hdr->hdr.csum_offset))
296f96fc 459 goto frame_err;
10a8d94a
JW
460 } else if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID) {
461 skb->ip_summed = CHECKSUM_UNNECESSARY;
296f96fc
RR
462 }
463
23cde76d
MM
464 skb->protocol = eth_type_trans(skb, dev);
465 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
466 ntohs(skb->protocol), skb->len, skb->pkt_type);
467
b3f24698 468 if (hdr->hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
296f96fc 469 pr_debug("GSO!\n");
b3f24698 470 switch (hdr->hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
296f96fc 471 case VIRTIO_NET_HDR_GSO_TCPV4:
c9af6db4 472 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
296f96fc 473 break;
296f96fc 474 case VIRTIO_NET_HDR_GSO_UDP:
c9af6db4 475 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
296f96fc
RR
476 break;
477 case VIRTIO_NET_HDR_GSO_TCPV6:
c9af6db4 478 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
296f96fc
RR
479 break;
480 default:
be443899
AW
481 net_warn_ratelimited("%s: bad gso type %u.\n",
482 dev->name, hdr->hdr.gso_type);
296f96fc
RR
483 goto frame_err;
484 }
485
b3f24698 486 if (hdr->hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN)
c9af6db4 487 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
34a48579 488
b3f24698 489 skb_shinfo(skb)->gso_size = hdr->hdr.gso_size;
296f96fc 490 if (skb_shinfo(skb)->gso_size == 0) {
be443899 491 net_warn_ratelimited("%s: zero gso size.\n", dev->name);
296f96fc
RR
492 goto frame_err;
493 }
494
495 /* Header must be checked, and gso_segs computed. */
496 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
497 skb_shinfo(skb)->gso_segs = 0;
498 }
499
500 netif_receive_skb(skb);
501 return;
502
503frame_err:
504 dev->stats.rx_frame_errors++;
296f96fc
RR
505 dev_kfree_skb(skb);
506}
507
e9d7417b 508static int add_recvbuf_small(struct receive_queue *rq, gfp_t gfp)
296f96fc 509{
e9d7417b 510 struct virtnet_info *vi = rq->vq->vdev->priv;
296f96fc 511 struct sk_buff *skb;
9ab86bbc 512 struct skb_vnet_hdr *hdr;
9ab86bbc 513 int err;
3f2c31d9 514
5061de36 515 skb = __netdev_alloc_skb_ip_align(vi->dev, GOOD_PACKET_LEN, gfp);
9ab86bbc
SM
516 if (unlikely(!skb))
517 return -ENOMEM;
296f96fc 518
5061de36 519 skb_put(skb, GOOD_PACKET_LEN);
3f2c31d9 520
9ab86bbc 521 hdr = skb_vnet_hdr(skb);
e9d7417b 522 sg_set_buf(rq->sg, &hdr->hdr, sizeof hdr->hdr);
97402b96 523
e9d7417b 524 skb_to_sgvec(skb, rq->sg + 1, 0, skb->len);
97402b96 525
9dc7b9e4 526 err = virtqueue_add_inbuf(rq->vq, rq->sg, 2, skb, gfp);
9ab86bbc
SM
527 if (err < 0)
528 dev_kfree_skb(skb);
97402b96 529
9ab86bbc
SM
530 return err;
531}
97402b96 532
e9d7417b 533static int add_recvbuf_big(struct receive_queue *rq, gfp_t gfp)
9ab86bbc 534{
9ab86bbc
SM
535 struct page *first, *list = NULL;
536 char *p;
537 int i, err, offset;
538
e9d7417b 539 /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
9ab86bbc 540 for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
e9d7417b 541 first = get_a_page(rq, gfp);
9ab86bbc
SM
542 if (!first) {
543 if (list)
e9d7417b 544 give_pages(rq, list);
9ab86bbc 545 return -ENOMEM;
97402b96 546 }
e9d7417b 547 sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
97402b96 548
9ab86bbc
SM
549 /* chain new page in list head to match sg */
550 first->private = (unsigned long)list;
551 list = first;
552 }
296f96fc 553
e9d7417b 554 first = get_a_page(rq, gfp);
9ab86bbc 555 if (!first) {
e9d7417b 556 give_pages(rq, list);
9ab86bbc
SM
557 return -ENOMEM;
558 }
559 p = page_address(first);
560
e9d7417b
JW
561 /* rq->sg[0], rq->sg[1] share the same page */
562 /* a separated rq->sg[0] for virtio_net_hdr only due to QEMU bug */
563 sg_set_buf(&rq->sg[0], p, sizeof(struct virtio_net_hdr));
9ab86bbc 564
e9d7417b 565 /* rq->sg[1] for data packet, from offset */
9ab86bbc 566 offset = sizeof(struct padded_vnet_hdr);
e9d7417b 567 sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
9ab86bbc
SM
568
569 /* chain first in list head */
570 first->private = (unsigned long)list;
9dc7b9e4
RR
571 err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2,
572 first, gfp);
9ab86bbc 573 if (err < 0)
e9d7417b 574 give_pages(rq, first);
9ab86bbc
SM
575
576 return err;
296f96fc
RR
577}
578
e9d7417b 579static int add_recvbuf_mergeable(struct receive_queue *rq, gfp_t gfp)
3f2c31d9 580{
2613af0e
MD
581 struct virtnet_info *vi = rq->vq->vdev->priv;
582 char *buf = NULL;
3f2c31d9 583 int err;
3f2c31d9 584
2613af0e 585 if (gfp & __GFP_WAIT) {
5061de36 586 if (skb_page_frag_refill(MERGE_BUFFER_LEN, &vi->alloc_frag,
2613af0e
MD
587 gfp)) {
588 buf = (char *)page_address(vi->alloc_frag.page) +
589 vi->alloc_frag.offset;
590 get_page(vi->alloc_frag.page);
5061de36 591 vi->alloc_frag.offset += MERGE_BUFFER_LEN;
2613af0e
MD
592 }
593 } else {
5061de36 594 buf = netdev_alloc_frag(MERGE_BUFFER_LEN);
2613af0e
MD
595 }
596 if (!buf)
9ab86bbc 597 return -ENOMEM;
3f2c31d9 598
5061de36 599 sg_init_one(rq->sg, buf, MERGE_BUFFER_LEN);
2613af0e 600 err = virtqueue_add_inbuf(rq->vq, rq->sg, 1, buf, gfp);
9ab86bbc 601 if (err < 0)
2613af0e 602 put_page(virt_to_head_page(buf));
3f2c31d9 603
9ab86bbc
SM
604 return err;
605}
3f2c31d9 606
b2baed69
RR
607/*
608 * Returns false if we couldn't fill entirely (OOM).
609 *
610 * Normally run in the receive path, but can also be run from ndo_open
611 * before we're receiving packets, or from refill_work which is
612 * careful to disable receiving (using napi_disable).
613 */
e9d7417b 614static bool try_fill_recv(struct receive_queue *rq, gfp_t gfp)
9ab86bbc 615{
e9d7417b 616 struct virtnet_info *vi = rq->vq->vdev->priv;
9ab86bbc 617 int err;
1788f495 618 bool oom;
3f2c31d9 619
9ab86bbc
SM
620 do {
621 if (vi->mergeable_rx_bufs)
e9d7417b 622 err = add_recvbuf_mergeable(rq, gfp);
9ab86bbc 623 else if (vi->big_packets)
e9d7417b 624 err = add_recvbuf_big(rq, gfp);
9ab86bbc 625 else
e9d7417b 626 err = add_recvbuf_small(rq, gfp);
3f2c31d9 627
1788f495 628 oom = err == -ENOMEM;
9ed4cb07 629 if (err)
3f2c31d9 630 break;
e9d7417b 631 ++rq->num;
b7dfde95 632 } while (rq->vq->num_free);
e9d7417b
JW
633 if (unlikely(rq->num > rq->max))
634 rq->max = rq->num;
67975901
HG
635 if (unlikely(!virtqueue_kick(rq->vq)))
636 return false;
3161e453 637 return !oom;
3f2c31d9
MM
638}
639
18445c4d 640static void skb_recv_done(struct virtqueue *rvq)
296f96fc
RR
641{
642 struct virtnet_info *vi = rvq->vdev->priv;
986a4f4d 643 struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
e9d7417b 644
18445c4d 645 /* Schedule NAPI, Suppress further interrupts if successful. */
e9d7417b 646 if (napi_schedule_prep(&rq->napi)) {
1915a712 647 virtqueue_disable_cb(rvq);
e9d7417b 648 __napi_schedule(&rq->napi);
18445c4d 649 }
296f96fc
RR
650}
651
e9d7417b 652static void virtnet_napi_enable(struct receive_queue *rq)
3e9d08ec 653{
e9d7417b 654 napi_enable(&rq->napi);
3e9d08ec
BR
655
656 /* If all buffers were filled by other side before we napi_enabled, we
657 * won't get another interrupt, so process any outstanding packets
658 * now. virtnet_poll wants re-enable the queue, so we disable here.
659 * We synchronize against interrupts via NAPI_STATE_SCHED */
e9d7417b
JW
660 if (napi_schedule_prep(&rq->napi)) {
661 virtqueue_disable_cb(rq->vq);
ec13ee80 662 local_bh_disable();
e9d7417b 663 __napi_schedule(&rq->napi);
ec13ee80 664 local_bh_enable();
3e9d08ec
BR
665 }
666}
667
3161e453
RR
668static void refill_work(struct work_struct *work)
669{
e9d7417b
JW
670 struct virtnet_info *vi =
671 container_of(work, struct virtnet_info, refill.work);
3161e453 672 bool still_empty;
986a4f4d
JW
673 int i;
674
55257d72 675 for (i = 0; i < vi->curr_queue_pairs; i++) {
986a4f4d 676 struct receive_queue *rq = &vi->rq[i];
3161e453 677
986a4f4d
JW
678 napi_disable(&rq->napi);
679 still_empty = !try_fill_recv(rq, GFP_KERNEL);
680 virtnet_napi_enable(rq);
3161e453 681
986a4f4d
JW
682 /* In theory, this can happen: if we don't get any buffers in
683 * we will *never* try to fill again.
684 */
685 if (still_empty)
686 schedule_delayed_work(&vi->refill, HZ/2);
687 }
3161e453
RR
688}
689
296f96fc
RR
690static int virtnet_poll(struct napi_struct *napi, int budget)
691{
e9d7417b
JW
692 struct receive_queue *rq =
693 container_of(napi, struct receive_queue, napi);
694 struct virtnet_info *vi = rq->vq->vdev->priv;
9ab86bbc 695 void *buf;
cbdadbbf 696 unsigned int r, len, received = 0;
296f96fc
RR
697
698again:
699 while (received < budget &&
e9d7417b
JW
700 (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
701 receive_buf(rq, buf, len);
702 --rq->num;
296f96fc
RR
703 received++;
704 }
705
e9d7417b
JW
706 if (rq->num < rq->max / 2) {
707 if (!try_fill_recv(rq, GFP_ATOMIC))
3b07e9ca 708 schedule_delayed_work(&vi->refill, 0);
3161e453 709 }
296f96fc 710
8329d98e
RR
711 /* Out of packets? */
712 if (received < budget) {
cbdadbbf 713 r = virtqueue_enable_cb_prepare(rq->vq);
288379f0 714 napi_complete(napi);
cbdadbbf 715 if (unlikely(virtqueue_poll(rq->vq, r)) &&
8e95a202 716 napi_schedule_prep(napi)) {
e9d7417b 717 virtqueue_disable_cb(rq->vq);
288379f0 718 __napi_schedule(napi);
296f96fc 719 goto again;
4265f161 720 }
296f96fc
RR
721 }
722
723 return received;
724}
725
986a4f4d
JW
726static int virtnet_open(struct net_device *dev)
727{
728 struct virtnet_info *vi = netdev_priv(dev);
729 int i;
730
e4166625
JW
731 for (i = 0; i < vi->max_queue_pairs; i++) {
732 if (i < vi->curr_queue_pairs)
733 /* Make sure we have some buffers: if oom use wq. */
734 if (!try_fill_recv(&vi->rq[i], GFP_KERNEL))
735 schedule_delayed_work(&vi->refill, 0);
986a4f4d
JW
736 virtnet_napi_enable(&vi->rq[i]);
737 }
738
739 return 0;
740}
741
b7dfde95 742static void free_old_xmit_skbs(struct send_queue *sq)
296f96fc
RR
743{
744 struct sk_buff *skb;
6ee57bcc 745 unsigned int len;
e9d7417b 746 struct virtnet_info *vi = sq->vq->vdev->priv;
58472a76 747 struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
296f96fc 748
e9d7417b 749 while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) {
296f96fc 750 pr_debug("Sent skb %p\n", skb);
3fa2a1df 751
83a27052 752 u64_stats_update_begin(&stats->tx_syncp);
3fa2a1df 753 stats->tx_bytes += skb->len;
754 stats->tx_packets++;
83a27052 755 u64_stats_update_end(&stats->tx_syncp);
3fa2a1df 756
ed79bab8 757 dev_kfree_skb_any(skb);
296f96fc
RR
758 }
759}
760
e9d7417b 761static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
296f96fc 762{
e7428e95 763 struct skb_vnet_hdr *hdr;
296f96fc 764 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
e9d7417b 765 struct virtnet_info *vi = sq->vq->vdev->priv;
7bedc7dc 766 unsigned num_sg;
e7428e95
MT
767 unsigned hdr_len;
768 bool can_push;
296f96fc 769
e174961c 770 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
e7428e95
MT
771 if (vi->mergeable_rx_bufs)
772 hdr_len = sizeof hdr->mhdr;
773 else
774 hdr_len = sizeof hdr->hdr;
775
776 can_push = vi->any_header_sg &&
777 !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
778 !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
779 /* Even if we can, don't push here yet as this would skew
780 * csum_start offset below. */
781 if (can_push)
782 hdr = (struct skb_vnet_hdr *)(skb->data - hdr_len);
783 else
784 hdr = skb_vnet_hdr(skb);
296f96fc 785
296f96fc 786 if (skb->ip_summed == CHECKSUM_PARTIAL) {
b3f24698 787 hdr->hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
55508d60 788 hdr->hdr.csum_start = skb_checksum_start_offset(skb);
b3f24698 789 hdr->hdr.csum_offset = skb->csum_offset;
296f96fc 790 } else {
b3f24698
RR
791 hdr->hdr.flags = 0;
792 hdr->hdr.csum_offset = hdr->hdr.csum_start = 0;
296f96fc
RR
793 }
794
795 if (skb_is_gso(skb)) {
b3f24698
RR
796 hdr->hdr.hdr_len = skb_headlen(skb);
797 hdr->hdr.gso_size = skb_shinfo(skb)->gso_size;
34a48579 798 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
b3f24698 799 hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
296f96fc 800 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
b3f24698 801 hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
296f96fc 802 else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
b3f24698 803 hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP;
296f96fc
RR
804 else
805 BUG();
34a48579 806 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
b3f24698 807 hdr->hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
296f96fc 808 } else {
b3f24698
RR
809 hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
810 hdr->hdr.gso_size = hdr->hdr.hdr_len = 0;
296f96fc
RR
811 }
812
3f2c31d9 813 if (vi->mergeable_rx_bufs)
e7428e95 814 hdr->mhdr.num_buffers = 0;
3f2c31d9 815
e7428e95
MT
816 if (can_push) {
817 __skb_push(skb, hdr_len);
818 num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
819 /* Pull header back to avoid skew in tx bytes calculations. */
820 __skb_pull(skb, hdr_len);
821 } else {
822 sg_set_buf(sq->sg, hdr, hdr_len);
823 num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len) + 1;
824 }
9dc7b9e4 825 return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
11a3a154
RR
826}
827
424efe9c 828static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
99ffc696
RR
829{
830 struct virtnet_info *vi = netdev_priv(dev);
986a4f4d
JW
831 int qnum = skb_get_queue_mapping(skb);
832 struct send_queue *sq = &vi->sq[qnum];
9ed4cb07 833 int err;
2cb9c6ba 834
2cb9c6ba 835 /* Free up any pending old buffers before queueing new ones. */
e9d7417b 836 free_old_xmit_skbs(sq);
99ffc696 837
03f191ba 838 /* Try to transmit */
b7dfde95 839 err = xmit_skb(sq, skb);
48925e37 840
9ed4cb07 841 /* This should not happen! */
67975901 842 if (unlikely(err) || unlikely(!virtqueue_kick(sq->vq))) {
9ed4cb07
RR
843 dev->stats.tx_fifo_errors++;
844 if (net_ratelimit())
845 dev_warn(&dev->dev,
b7dfde95 846 "Unexpected TXQ (%d) queue failure: %d\n", qnum, err);
58eba97d
RR
847 dev->stats.tx_dropped++;
848 kfree_skb(skb);
849 return NETDEV_TX_OK;
296f96fc 850 }
03f191ba 851
48925e37
RR
852 /* Don't wait up for transmitted skbs to be freed. */
853 skb_orphan(skb);
854 nf_reset(skb);
855
856 /* Apparently nice girls don't return TX_BUSY; stop the queue
857 * before it gets out of hand. Naturally, this wastes entries. */
b7dfde95 858 if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
986a4f4d 859 netif_stop_subqueue(dev, qnum);
e9d7417b 860 if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
48925e37 861 /* More just got used, free them then recheck. */
b7dfde95
LT
862 free_old_xmit_skbs(sq);
863 if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
986a4f4d 864 netif_start_subqueue(dev, qnum);
e9d7417b 865 virtqueue_disable_cb(sq->vq);
48925e37
RR
866 }
867 }
99ffc696 868 }
48925e37
RR
869
870 return NETDEV_TX_OK;
296f96fc
RR
871}
872
40cbfc37
AK
873/*
874 * Send command via the control virtqueue and check status. Commands
875 * supported by the hypervisor, as indicated by feature bits, should
876 * never fail unless improperly formated.
877 */
878static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
f7bc9594
RR
879 struct scatterlist *out,
880 struct scatterlist *in)
40cbfc37 881{
f7bc9594 882 struct scatterlist *sgs[4], hdr, stat;
40cbfc37
AK
883 struct virtio_net_ctrl_hdr ctrl;
884 virtio_net_ctrl_ack status = ~0;
f7bc9594 885 unsigned out_num = 0, in_num = 0, tmp;
40cbfc37
AK
886
887 /* Caller should know better */
f7bc9594 888 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
40cbfc37
AK
889
890 ctrl.class = class;
891 ctrl.cmd = cmd;
f7bc9594
RR
892 /* Add header */
893 sg_init_one(&hdr, &ctrl, sizeof(ctrl));
894 sgs[out_num++] = &hdr;
40cbfc37 895
f7bc9594
RR
896 if (out)
897 sgs[out_num++] = out;
898 if (in)
899 sgs[out_num + in_num++] = in;
40cbfc37 900
f7bc9594
RR
901 /* Add return status. */
902 sg_init_one(&stat, &status, sizeof(status));
903 sgs[out_num + in_num++] = &stat;
40cbfc37 904
f7bc9594
RR
905 BUG_ON(out_num + in_num > ARRAY_SIZE(sgs));
906 BUG_ON(virtqueue_add_sgs(vi->cvq, sgs, out_num, in_num, vi, GFP_ATOMIC)
907 < 0);
40cbfc37 908
67975901
HG
909 if (unlikely(!virtqueue_kick(vi->cvq)))
910 return status == VIRTIO_NET_OK;
40cbfc37
AK
911
912 /* Spin for a response, the kick causes an ioport write, trapping
913 * into the hypervisor, so the request should be handled immediately.
914 */
047b9b94
HG
915 while (!virtqueue_get_buf(vi->cvq, &tmp) &&
916 !virtqueue_is_broken(vi->cvq))
40cbfc37
AK
917 cpu_relax();
918
919 return status == VIRTIO_NET_OK;
920}
921
9c46f6d4
AW
922static int virtnet_set_mac_address(struct net_device *dev, void *p)
923{
924 struct virtnet_info *vi = netdev_priv(dev);
925 struct virtio_device *vdev = vi->vdev;
f2f2c8b4 926 int ret;
7e58d5ae
AK
927 struct sockaddr *addr = p;
928 struct scatterlist sg;
9c46f6d4 929
7e58d5ae 930 ret = eth_prepare_mac_addr_change(dev, p);
f2f2c8b4
JP
931 if (ret)
932 return ret;
9c46f6d4 933
7e58d5ae
AK
934 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
935 sg_init_one(&sg, addr->sa_data, dev->addr_len);
936 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
937 VIRTIO_NET_CTRL_MAC_ADDR_SET,
f7bc9594 938 &sg, NULL)) {
7e58d5ae
AK
939 dev_warn(&vdev->dev,
940 "Failed to set mac address by vq command.\n");
941 return -EINVAL;
942 }
943 } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
855e0c52
RR
944 unsigned int i;
945
946 /* Naturally, this has an atomicity problem. */
947 for (i = 0; i < dev->addr_len; i++)
948 virtio_cwrite8(vdev,
949 offsetof(struct virtio_net_config, mac) +
950 i, addr->sa_data[i]);
7e58d5ae
AK
951 }
952
953 eth_commit_mac_addr_change(dev, p);
9c46f6d4
AW
954
955 return 0;
956}
957
3fa2a1df 958static struct rtnl_link_stats64 *virtnet_stats(struct net_device *dev,
959 struct rtnl_link_stats64 *tot)
960{
961 struct virtnet_info *vi = netdev_priv(dev);
962 int cpu;
963 unsigned int start;
964
965 for_each_possible_cpu(cpu) {
58472a76 966 struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu);
3fa2a1df 967 u64 tpackets, tbytes, rpackets, rbytes;
968
969 do {
e3906486 970 start = u64_stats_fetch_begin_bh(&stats->tx_syncp);
3fa2a1df 971 tpackets = stats->tx_packets;
972 tbytes = stats->tx_bytes;
e3906486 973 } while (u64_stats_fetch_retry_bh(&stats->tx_syncp, start));
83a27052
ED
974
975 do {
e3906486 976 start = u64_stats_fetch_begin_bh(&stats->rx_syncp);
3fa2a1df 977 rpackets = stats->rx_packets;
978 rbytes = stats->rx_bytes;
e3906486 979 } while (u64_stats_fetch_retry_bh(&stats->rx_syncp, start));
3fa2a1df 980
981 tot->rx_packets += rpackets;
982 tot->tx_packets += tpackets;
983 tot->rx_bytes += rbytes;
984 tot->tx_bytes += tbytes;
985 }
986
987 tot->tx_dropped = dev->stats.tx_dropped;
021ac8d3 988 tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
3fa2a1df 989 tot->rx_dropped = dev->stats.rx_dropped;
990 tot->rx_length_errors = dev->stats.rx_length_errors;
991 tot->rx_frame_errors = dev->stats.rx_frame_errors;
992
993 return tot;
994}
995
da74e89d
AS
996#ifdef CONFIG_NET_POLL_CONTROLLER
997static void virtnet_netpoll(struct net_device *dev)
998{
999 struct virtnet_info *vi = netdev_priv(dev);
986a4f4d 1000 int i;
da74e89d 1001
986a4f4d
JW
1002 for (i = 0; i < vi->curr_queue_pairs; i++)
1003 napi_schedule(&vi->rq[i].napi);
da74e89d
AS
1004}
1005#endif
1006
586d17c5
JW
1007static void virtnet_ack_link_announce(struct virtnet_info *vi)
1008{
1009 rtnl_lock();
1010 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
f7bc9594 1011 VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL, NULL))
586d17c5
JW
1012 dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
1013 rtnl_unlock();
1014}
1015
986a4f4d
JW
1016static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
1017{
1018 struct scatterlist sg;
1019 struct virtio_net_ctrl_mq s;
1020 struct net_device *dev = vi->dev;
1021
1022 if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
1023 return 0;
1024
1025 s.virtqueue_pairs = queue_pairs;
1026 sg_init_one(&sg, &s, sizeof(s));
1027
1028 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
f7bc9594 1029 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg, NULL)) {
986a4f4d
JW
1030 dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
1031 queue_pairs);
1032 return -EINVAL;
55257d72 1033 } else {
986a4f4d 1034 vi->curr_queue_pairs = queue_pairs;
35ed159b
JW
1035 /* virtnet_open() will refill when device is going to up. */
1036 if (dev->flags & IFF_UP)
1037 schedule_delayed_work(&vi->refill, 0);
55257d72 1038 }
986a4f4d
JW
1039
1040 return 0;
1041}
1042
296f96fc
RR
1043static int virtnet_close(struct net_device *dev)
1044{
1045 struct virtnet_info *vi = netdev_priv(dev);
986a4f4d 1046 int i;
296f96fc 1047
b2baed69
RR
1048 /* Make sure refill_work doesn't re-enable napi! */
1049 cancel_delayed_work_sync(&vi->refill);
986a4f4d
JW
1050
1051 for (i = 0; i < vi->max_queue_pairs; i++)
1052 napi_disable(&vi->rq[i].napi);
296f96fc 1053
296f96fc
RR
1054 return 0;
1055}
1056
2af7698e
AW
1057static void virtnet_set_rx_mode(struct net_device *dev)
1058{
1059 struct virtnet_info *vi = netdev_priv(dev);
f565a7c2 1060 struct scatterlist sg[2];
2af7698e 1061 u8 promisc, allmulti;
f565a7c2 1062 struct virtio_net_ctrl_mac *mac_data;
ccffad25 1063 struct netdev_hw_addr *ha;
32e7bfc4 1064 int uc_count;
4cd24eaf 1065 int mc_count;
f565a7c2
AW
1066 void *buf;
1067 int i;
2af7698e
AW
1068
1069 /* We can't dynamicaly set ndo_set_rx_mode, so return gracefully */
1070 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
1071 return;
1072
f565a7c2
AW
1073 promisc = ((dev->flags & IFF_PROMISC) != 0);
1074 allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
2af7698e 1075
23e258e1 1076 sg_init_one(sg, &promisc, sizeof(promisc));
2af7698e
AW
1077
1078 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
1079 VIRTIO_NET_CTRL_RX_PROMISC,
f7bc9594 1080 sg, NULL))
2af7698e
AW
1081 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
1082 promisc ? "en" : "dis");
1083
23e258e1 1084 sg_init_one(sg, &allmulti, sizeof(allmulti));
2af7698e
AW
1085
1086 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
1087 VIRTIO_NET_CTRL_RX_ALLMULTI,
f7bc9594 1088 sg, NULL))
2af7698e
AW
1089 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
1090 allmulti ? "en" : "dis");
f565a7c2 1091
32e7bfc4 1092 uc_count = netdev_uc_count(dev);
4cd24eaf 1093 mc_count = netdev_mc_count(dev);
f565a7c2 1094 /* MAC filter - use one buffer for both lists */
4cd24eaf
JP
1095 buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
1096 (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
1097 mac_data = buf;
e68ed8f0 1098 if (!buf)
f565a7c2 1099 return;
f565a7c2 1100
23e258e1
AW
1101 sg_init_table(sg, 2);
1102
f565a7c2 1103 /* Store the unicast list and count in the front of the buffer */
32e7bfc4 1104 mac_data->entries = uc_count;
ccffad25 1105 i = 0;
32e7bfc4 1106 netdev_for_each_uc_addr(ha, dev)
ccffad25 1107 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
f565a7c2
AW
1108
1109 sg_set_buf(&sg[0], mac_data,
32e7bfc4 1110 sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
f565a7c2
AW
1111
1112 /* multicast list and count fill the end */
32e7bfc4 1113 mac_data = (void *)&mac_data->macs[uc_count][0];
f565a7c2 1114
4cd24eaf 1115 mac_data->entries = mc_count;
567ec874 1116 i = 0;
22bedad3
JP
1117 netdev_for_each_mc_addr(ha, dev)
1118 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
f565a7c2
AW
1119
1120 sg_set_buf(&sg[1], mac_data,
4cd24eaf 1121 sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
f565a7c2
AW
1122
1123 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
1124 VIRTIO_NET_CTRL_MAC_TABLE_SET,
f7bc9594 1125 sg, NULL))
99e872ae 1126 dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
f565a7c2
AW
1127
1128 kfree(buf);
2af7698e
AW
1129}
1130
80d5c368
PM
1131static int virtnet_vlan_rx_add_vid(struct net_device *dev,
1132 __be16 proto, u16 vid)
0bde9569
AW
1133{
1134 struct virtnet_info *vi = netdev_priv(dev);
1135 struct scatterlist sg;
1136
23e258e1 1137 sg_init_one(&sg, &vid, sizeof(vid));
0bde9569
AW
1138
1139 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
f7bc9594 1140 VIRTIO_NET_CTRL_VLAN_ADD, &sg, NULL))
0bde9569 1141 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
8e586137 1142 return 0;
0bde9569
AW
1143}
1144
80d5c368
PM
1145static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
1146 __be16 proto, u16 vid)
0bde9569
AW
1147{
1148 struct virtnet_info *vi = netdev_priv(dev);
1149 struct scatterlist sg;
1150
23e258e1 1151 sg_init_one(&sg, &vid, sizeof(vid));
0bde9569
AW
1152
1153 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
f7bc9594 1154 VIRTIO_NET_CTRL_VLAN_DEL, &sg, NULL))
0bde9569 1155 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
8e586137 1156 return 0;
0bde9569
AW
1157}
1158
8898c21c 1159static void virtnet_clean_affinity(struct virtnet_info *vi, long hcpu)
986a4f4d
JW
1160{
1161 int i;
1162
8898c21c
WG
1163 if (vi->affinity_hint_set) {
1164 for (i = 0; i < vi->max_queue_pairs; i++) {
47be2479
WG
1165 virtqueue_set_affinity(vi->rq[i].vq, -1);
1166 virtqueue_set_affinity(vi->sq[i].vq, -1);
1167 }
1168
8898c21c
WG
1169 vi->affinity_hint_set = false;
1170 }
8898c21c 1171}
47be2479 1172
8898c21c
WG
1173static void virtnet_set_affinity(struct virtnet_info *vi)
1174{
1175 int i;
1176 int cpu;
986a4f4d
JW
1177
1178 /* In multiqueue mode, when the number of cpu is equal to the number of
1179 * queue pairs, we let the queue pairs to be private to one cpu by
1180 * setting the affinity hint to eliminate the contention.
1181 */
8898c21c
WG
1182 if (vi->curr_queue_pairs == 1 ||
1183 vi->max_queue_pairs != num_online_cpus()) {
1184 virtnet_clean_affinity(vi, -1);
1185 return;
986a4f4d
JW
1186 }
1187
8898c21c
WG
1188 i = 0;
1189 for_each_online_cpu(cpu) {
986a4f4d
JW
1190 virtqueue_set_affinity(vi->rq[i].vq, cpu);
1191 virtqueue_set_affinity(vi->sq[i].vq, cpu);
9bb8ca86 1192 netif_set_xps_queue(vi->dev, cpumask_of(cpu), i);
8898c21c 1193 i++;
986a4f4d
JW
1194 }
1195
8898c21c 1196 vi->affinity_hint_set = true;
986a4f4d
JW
1197}
1198
8de4b2f3
WG
1199static int virtnet_cpu_callback(struct notifier_block *nfb,
1200 unsigned long action, void *hcpu)
1201{
1202 struct virtnet_info *vi = container_of(nfb, struct virtnet_info, nb);
1203
1204 switch(action & ~CPU_TASKS_FROZEN) {
1205 case CPU_ONLINE:
1206 case CPU_DOWN_FAILED:
1207 case CPU_DEAD:
1208 virtnet_set_affinity(vi);
1209 break;
1210 case CPU_DOWN_PREPARE:
1211 virtnet_clean_affinity(vi, (long)hcpu);
1212 break;
1213 default:
1214 break;
1215 }
3ab098df 1216
8de4b2f3 1217 return NOTIFY_OK;
986a4f4d
JW
1218}
1219
8f9f4668
RJ
1220static void virtnet_get_ringparam(struct net_device *dev,
1221 struct ethtool_ringparam *ring)
1222{
1223 struct virtnet_info *vi = netdev_priv(dev);
1224
986a4f4d
JW
1225 ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq);
1226 ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq);
8f9f4668
RJ
1227 ring->rx_pending = ring->rx_max_pending;
1228 ring->tx_pending = ring->tx_max_pending;
8f9f4668
RJ
1229}
1230
66846048
RJ
1231
1232static void virtnet_get_drvinfo(struct net_device *dev,
1233 struct ethtool_drvinfo *info)
1234{
1235 struct virtnet_info *vi = netdev_priv(dev);
1236 struct virtio_device *vdev = vi->vdev;
1237
1238 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
1239 strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
1240 strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
1241
1242}
1243
d73bcd2c
JW
1244/* TODO: Eliminate OOO packets during switching */
1245static int virtnet_set_channels(struct net_device *dev,
1246 struct ethtool_channels *channels)
1247{
1248 struct virtnet_info *vi = netdev_priv(dev);
1249 u16 queue_pairs = channels->combined_count;
1250 int err;
1251
1252 /* We don't support separate rx/tx channels.
1253 * We don't allow setting 'other' channels.
1254 */
1255 if (channels->rx_count || channels->tx_count || channels->other_count)
1256 return -EINVAL;
1257
1258 if (queue_pairs > vi->max_queue_pairs)
1259 return -EINVAL;
1260
47be2479 1261 get_online_cpus();
d73bcd2c
JW
1262 err = virtnet_set_queues(vi, queue_pairs);
1263 if (!err) {
1264 netif_set_real_num_tx_queues(dev, queue_pairs);
1265 netif_set_real_num_rx_queues(dev, queue_pairs);
1266
8898c21c 1267 virtnet_set_affinity(vi);
d73bcd2c 1268 }
47be2479 1269 put_online_cpus();
d73bcd2c
JW
1270
1271 return err;
1272}
1273
1274static void virtnet_get_channels(struct net_device *dev,
1275 struct ethtool_channels *channels)
1276{
1277 struct virtnet_info *vi = netdev_priv(dev);
1278
1279 channels->combined_count = vi->curr_queue_pairs;
1280 channels->max_combined = vi->max_queue_pairs;
1281 channels->max_other = 0;
1282 channels->rx_count = 0;
1283 channels->tx_count = 0;
1284 channels->other_count = 0;
1285}
1286
0fc0b732 1287static const struct ethtool_ops virtnet_ethtool_ops = {
66846048 1288 .get_drvinfo = virtnet_get_drvinfo,
9f4d26d0 1289 .get_link = ethtool_op_get_link,
8f9f4668 1290 .get_ringparam = virtnet_get_ringparam,
d73bcd2c
JW
1291 .set_channels = virtnet_set_channels,
1292 .get_channels = virtnet_get_channels,
a9ea3fc6
HX
1293};
1294
39da5814
MM
1295#define MIN_MTU 68
1296#define MAX_MTU 65535
1297
1298static int virtnet_change_mtu(struct net_device *dev, int new_mtu)
1299{
1300 if (new_mtu < MIN_MTU || new_mtu > MAX_MTU)
1301 return -EINVAL;
1302 dev->mtu = new_mtu;
1303 return 0;
1304}
1305
76288b4e
SH
1306static const struct net_device_ops virtnet_netdev = {
1307 .ndo_open = virtnet_open,
1308 .ndo_stop = virtnet_close,
1309 .ndo_start_xmit = start_xmit,
1310 .ndo_validate_addr = eth_validate_addr,
9c46f6d4 1311 .ndo_set_mac_address = virtnet_set_mac_address,
2af7698e 1312 .ndo_set_rx_mode = virtnet_set_rx_mode,
76288b4e 1313 .ndo_change_mtu = virtnet_change_mtu,
3fa2a1df 1314 .ndo_get_stats64 = virtnet_stats,
1824a989
AW
1315 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
1316 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
76288b4e
SH
1317#ifdef CONFIG_NET_POLL_CONTROLLER
1318 .ndo_poll_controller = virtnet_netpoll,
1319#endif
1320};
1321
586d17c5 1322static void virtnet_config_changed_work(struct work_struct *work)
9f4d26d0 1323{
586d17c5
JW
1324 struct virtnet_info *vi =
1325 container_of(work, struct virtnet_info, config_work);
9f4d26d0
MM
1326 u16 v;
1327
586d17c5
JW
1328 mutex_lock(&vi->config_lock);
1329 if (!vi->config_enable)
1330 goto done;
1331
855e0c52
RR
1332 if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
1333 struct virtio_net_config, status, &v) < 0)
586d17c5
JW
1334 goto done;
1335
1336 if (v & VIRTIO_NET_S_ANNOUNCE) {
ee89bab1 1337 netdev_notify_peers(vi->dev);
586d17c5
JW
1338 virtnet_ack_link_announce(vi);
1339 }
9f4d26d0
MM
1340
1341 /* Ignore unknown (future) status bits */
1342 v &= VIRTIO_NET_S_LINK_UP;
1343
1344 if (vi->status == v)
586d17c5 1345 goto done;
9f4d26d0
MM
1346
1347 vi->status = v;
1348
1349 if (vi->status & VIRTIO_NET_S_LINK_UP) {
1350 netif_carrier_on(vi->dev);
986a4f4d 1351 netif_tx_wake_all_queues(vi->dev);
9f4d26d0
MM
1352 } else {
1353 netif_carrier_off(vi->dev);
986a4f4d 1354 netif_tx_stop_all_queues(vi->dev);
9f4d26d0 1355 }
586d17c5
JW
1356done:
1357 mutex_unlock(&vi->config_lock);
9f4d26d0
MM
1358}
1359
1360static void virtnet_config_changed(struct virtio_device *vdev)
1361{
1362 struct virtnet_info *vi = vdev->priv;
1363
3b07e9ca 1364 schedule_work(&vi->config_work);
9f4d26d0
MM
1365}
1366
986a4f4d
JW
1367static void virtnet_free_queues(struct virtnet_info *vi)
1368{
d4fb84ee
AV
1369 int i;
1370
1371 for (i = 0; i < vi->max_queue_pairs; i++)
1372 netif_napi_del(&vi->rq[i].napi);
1373
986a4f4d
JW
1374 kfree(vi->rq);
1375 kfree(vi->sq);
1376}
1377
1378static void free_receive_bufs(struct virtnet_info *vi)
1379{
1380 int i;
1381
1382 for (i = 0; i < vi->max_queue_pairs; i++) {
1383 while (vi->rq[i].pages)
1384 __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
1385 }
1386}
1387
1388static void free_unused_bufs(struct virtnet_info *vi)
1389{
1390 void *buf;
1391 int i;
1392
1393 for (i = 0; i < vi->max_queue_pairs; i++) {
1394 struct virtqueue *vq = vi->sq[i].vq;
1395 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL)
1396 dev_kfree_skb(buf);
1397 }
1398
1399 for (i = 0; i < vi->max_queue_pairs; i++) {
1400 struct virtqueue *vq = vi->rq[i].vq;
1401
1402 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
fa9fac17 1403 if (vi->mergeable_rx_bufs)
2613af0e 1404 put_page(virt_to_head_page(buf));
fa9fac17
AV
1405 else if (vi->big_packets)
1406 give_pages(&vi->rq[i], buf);
986a4f4d
JW
1407 else
1408 dev_kfree_skb(buf);
1409 --vi->rq[i].num;
1410 }
1411 BUG_ON(vi->rq[i].num != 0);
1412 }
1413}
1414
e9d7417b
JW
1415static void virtnet_del_vqs(struct virtnet_info *vi)
1416{
1417 struct virtio_device *vdev = vi->vdev;
1418
8898c21c 1419 virtnet_clean_affinity(vi, -1);
986a4f4d 1420
e9d7417b 1421 vdev->config->del_vqs(vdev);
986a4f4d
JW
1422
1423 virtnet_free_queues(vi);
e9d7417b
JW
1424}
1425
986a4f4d 1426static int virtnet_find_vqs(struct virtnet_info *vi)
3f9c10b0 1427{
986a4f4d
JW
1428 vq_callback_t **callbacks;
1429 struct virtqueue **vqs;
1430 int ret = -ENOMEM;
1431 int i, total_vqs;
1432 const char **names;
1433
1434 /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
1435 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
1436 * possible control vq.
1437 */
1438 total_vqs = vi->max_queue_pairs * 2 +
1439 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
1440
1441 /* Allocate space for find_vqs parameters */
1442 vqs = kzalloc(total_vqs * sizeof(*vqs), GFP_KERNEL);
1443 if (!vqs)
1444 goto err_vq;
1445 callbacks = kmalloc(total_vqs * sizeof(*callbacks), GFP_KERNEL);
1446 if (!callbacks)
1447 goto err_callback;
1448 names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL);
1449 if (!names)
1450 goto err_names;
1451
1452 /* Parameters for control virtqueue, if any */
1453 if (vi->has_cvq) {
1454 callbacks[total_vqs - 1] = NULL;
1455 names[total_vqs - 1] = "control";
1456 }
3f9c10b0 1457
986a4f4d
JW
1458 /* Allocate/initialize parameters for send/receive virtqueues */
1459 for (i = 0; i < vi->max_queue_pairs; i++) {
1460 callbacks[rxq2vq(i)] = skb_recv_done;
1461 callbacks[txq2vq(i)] = skb_xmit_done;
1462 sprintf(vi->rq[i].name, "input.%d", i);
1463 sprintf(vi->sq[i].name, "output.%d", i);
1464 names[rxq2vq(i)] = vi->rq[i].name;
1465 names[txq2vq(i)] = vi->sq[i].name;
1466 }
3f9c10b0 1467
986a4f4d
JW
1468 ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks,
1469 names);
1470 if (ret)
1471 goto err_find;
3f9c10b0 1472
986a4f4d
JW
1473 if (vi->has_cvq) {
1474 vi->cvq = vqs[total_vqs - 1];
3f9c10b0 1475 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
f646968f 1476 vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
3f9c10b0 1477 }
986a4f4d
JW
1478
1479 for (i = 0; i < vi->max_queue_pairs; i++) {
1480 vi->rq[i].vq = vqs[rxq2vq(i)];
1481 vi->sq[i].vq = vqs[txq2vq(i)];
1482 }
1483
1484 kfree(names);
1485 kfree(callbacks);
1486 kfree(vqs);
1487
3f9c10b0 1488 return 0;
986a4f4d
JW
1489
1490err_find:
1491 kfree(names);
1492err_names:
1493 kfree(callbacks);
1494err_callback:
1495 kfree(vqs);
1496err_vq:
1497 return ret;
1498}
1499
1500static int virtnet_alloc_queues(struct virtnet_info *vi)
1501{
1502 int i;
1503
1504 vi->sq = kzalloc(sizeof(*vi->sq) * vi->max_queue_pairs, GFP_KERNEL);
1505 if (!vi->sq)
1506 goto err_sq;
1507 vi->rq = kzalloc(sizeof(*vi->rq) * vi->max_queue_pairs, GFP_KERNEL);
008d4278 1508 if (!vi->rq)
986a4f4d
JW
1509 goto err_rq;
1510
1511 INIT_DELAYED_WORK(&vi->refill, refill_work);
1512 for (i = 0; i < vi->max_queue_pairs; i++) {
1513 vi->rq[i].pages = NULL;
1514 netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll,
1515 napi_weight);
1516
1517 sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
1518 sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
1519 }
1520
1521 return 0;
1522
1523err_rq:
1524 kfree(vi->sq);
1525err_sq:
1526 return -ENOMEM;
1527}
1528
1529static int init_vqs(struct virtnet_info *vi)
1530{
1531 int ret;
1532
1533 /* Allocate send & receive queues */
1534 ret = virtnet_alloc_queues(vi);
1535 if (ret)
1536 goto err;
1537
1538 ret = virtnet_find_vqs(vi);
1539 if (ret)
1540 goto err_free;
1541
47be2479 1542 get_online_cpus();
8898c21c 1543 virtnet_set_affinity(vi);
47be2479
WG
1544 put_online_cpus();
1545
986a4f4d
JW
1546 return 0;
1547
1548err_free:
1549 virtnet_free_queues(vi);
1550err:
1551 return ret;
3f9c10b0
AS
1552}
1553
296f96fc
RR
1554static int virtnet_probe(struct virtio_device *vdev)
1555{
986a4f4d 1556 int i, err;
296f96fc
RR
1557 struct net_device *dev;
1558 struct virtnet_info *vi;
986a4f4d
JW
1559 u16 max_queue_pairs;
1560
1561 /* Find if host supports multiqueue virtio_net device */
855e0c52
RR
1562 err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ,
1563 struct virtio_net_config,
1564 max_virtqueue_pairs, &max_queue_pairs);
986a4f4d
JW
1565
1566 /* We need at least 2 queue's */
1567 if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
1568 max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
1569 !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
1570 max_queue_pairs = 1;
296f96fc
RR
1571
1572 /* Allocate ourselves a network device with room for our info */
986a4f4d 1573 dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
296f96fc
RR
1574 if (!dev)
1575 return -ENOMEM;
1576
1577 /* Set up network device as normal. */
f2f2c8b4 1578 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
76288b4e 1579 dev->netdev_ops = &virtnet_netdev;
296f96fc 1580 dev->features = NETIF_F_HIGHDMA;
3fa2a1df 1581
a9ea3fc6 1582 SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops);
296f96fc
RR
1583 SET_NETDEV_DEV(dev, &vdev->dev);
1584
1585 /* Do we support "hardware" checksums? */
98e778c9 1586 if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
296f96fc 1587 /* This opens up the world of extra features. */
98e778c9
MM
1588 dev->hw_features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
1589 if (csum)
1590 dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
1591
1592 if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
1593 dev->hw_features |= NETIF_F_TSO | NETIF_F_UFO
34a48579
RR
1594 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
1595 }
5539ae96 1596 /* Individual feature bits: what can host handle? */
98e778c9
MM
1597 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
1598 dev->hw_features |= NETIF_F_TSO;
1599 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
1600 dev->hw_features |= NETIF_F_TSO6;
1601 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
1602 dev->hw_features |= NETIF_F_TSO_ECN;
1603 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
1604 dev->hw_features |= NETIF_F_UFO;
1605
1606 if (gso)
1607 dev->features |= dev->hw_features & (NETIF_F_ALL_TSO|NETIF_F_UFO);
1608 /* (!csum && gso) case will be fixed by register_netdev() */
296f96fc 1609 }
4f49129b
TH
1610 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
1611 dev->features |= NETIF_F_RXCSUM;
296f96fc 1612
4fda8302
JW
1613 dev->vlan_features = dev->features;
1614
296f96fc 1615 /* Configuration may specify what MAC to use. Otherwise random. */
855e0c52
RR
1616 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
1617 virtio_cread_bytes(vdev,
1618 offsetof(struct virtio_net_config, mac),
1619 dev->dev_addr, dev->addr_len);
1620 else
f2cedb63 1621 eth_hw_addr_random(dev);
296f96fc
RR
1622
1623 /* Set up our device-specific information */
1624 vi = netdev_priv(dev);
296f96fc
RR
1625 vi->dev = dev;
1626 vi->vdev = vdev;
d9d5dcc8 1627 vdev->priv = vi;
3fa2a1df 1628 vi->stats = alloc_percpu(struct virtnet_stats);
1629 err = -ENOMEM;
1630 if (vi->stats == NULL)
1631 goto free;
1632
827da44c
JS
1633 for_each_possible_cpu(i) {
1634 struct virtnet_stats *virtnet_stats;
1635 virtnet_stats = per_cpu_ptr(vi->stats, i);
1636 u64_stats_init(&virtnet_stats->tx_syncp);
1637 u64_stats_init(&virtnet_stats->rx_syncp);
1638 }
1639
586d17c5
JW
1640 mutex_init(&vi->config_lock);
1641 vi->config_enable = true;
1642 INIT_WORK(&vi->config_work, virtnet_config_changed_work);
296f96fc 1643
97402b96 1644 /* If we can receive ANY GSO packets, we must allocate large ones. */
8e95a202
JP
1645 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
1646 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
1647 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN))
97402b96
HX
1648 vi->big_packets = true;
1649
3f2c31d9
MM
1650 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
1651 vi->mergeable_rx_bufs = true;
1652
e7428e95
MT
1653 if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT))
1654 vi->any_header_sg = true;
1655
986a4f4d
JW
1656 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
1657 vi->has_cvq = true;
1658
1659 /* Use single tx/rx queue pair as default */
1660 vi->curr_queue_pairs = 1;
1661 vi->max_queue_pairs = max_queue_pairs;
1662
1663 /* Allocate/initialize the rx/tx queues, and invoke find_vqs */
3f9c10b0 1664 err = init_vqs(vi);
d2a7ddda 1665 if (err)
9bb8ca86 1666 goto free_stats;
296f96fc 1667
0f13b66b
ZYW
1668 netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
1669 netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
986a4f4d 1670
296f96fc
RR
1671 err = register_netdev(dev);
1672 if (err) {
1673 pr_debug("virtio_net: registering device failed\n");
d2a7ddda 1674 goto free_vqs;
296f96fc 1675 }
b3369c1f
RR
1676
1677 /* Last of all, set up some receive buffers. */
55257d72 1678 for (i = 0; i < vi->curr_queue_pairs; i++) {
986a4f4d
JW
1679 try_fill_recv(&vi->rq[i], GFP_KERNEL);
1680
1681 /* If we didn't even get one input buffer, we're useless. */
1682 if (vi->rq[i].num == 0) {
1683 free_unused_bufs(vi);
1684 err = -ENOMEM;
1685 goto free_recv_bufs;
1686 }
b3369c1f
RR
1687 }
1688
8de4b2f3
WG
1689 vi->nb.notifier_call = &virtnet_cpu_callback;
1690 err = register_hotcpu_notifier(&vi->nb);
1691 if (err) {
1692 pr_debug("virtio_net: registering cpu notifier failed\n");
1693 goto free_recv_bufs;
1694 }
1695
167c25e4
JW
1696 /* Assume link up if device can't report link status,
1697 otherwise get link status from config. */
1698 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
1699 netif_carrier_off(dev);
3b07e9ca 1700 schedule_work(&vi->config_work);
167c25e4
JW
1701 } else {
1702 vi->status = VIRTIO_NET_S_LINK_UP;
1703 netif_carrier_on(dev);
1704 }
9f4d26d0 1705
986a4f4d
JW
1706 pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
1707 dev->name, max_queue_pairs);
1708
296f96fc
RR
1709 return 0;
1710
986a4f4d
JW
1711free_recv_bufs:
1712 free_receive_bufs(vi);
b3369c1f 1713 unregister_netdev(dev);
d2a7ddda 1714free_vqs:
986a4f4d 1715 cancel_delayed_work_sync(&vi->refill);
e9d7417b 1716 virtnet_del_vqs(vi);
2613af0e
MD
1717 if (vi->alloc_frag.page)
1718 put_page(vi->alloc_frag.page);
3fa2a1df 1719free_stats:
1720 free_percpu(vi->stats);
296f96fc
RR
1721free:
1722 free_netdev(dev);
1723 return err;
1724}
1725
04486ed0 1726static void remove_vq_common(struct virtnet_info *vi)
296f96fc 1727{
04486ed0 1728 vi->vdev->config->reset(vi->vdev);
830a8a97
SM
1729
1730 /* Free unused buffers in both send and recv, if any. */
9ab86bbc 1731 free_unused_bufs(vi);
fb6813f4 1732
986a4f4d 1733 free_receive_bufs(vi);
d2a7ddda 1734
986a4f4d 1735 virtnet_del_vqs(vi);
04486ed0
AS
1736}
1737
8cc085d6 1738static void virtnet_remove(struct virtio_device *vdev)
04486ed0
AS
1739{
1740 struct virtnet_info *vi = vdev->priv;
1741
8de4b2f3
WG
1742 unregister_hotcpu_notifier(&vi->nb);
1743
586d17c5
JW
1744 /* Prevent config work handler from accessing the device. */
1745 mutex_lock(&vi->config_lock);
1746 vi->config_enable = false;
1747 mutex_unlock(&vi->config_lock);
1748
04486ed0
AS
1749 unregister_netdev(vi->dev);
1750
1751 remove_vq_common(vi);
2613af0e
MD
1752 if (vi->alloc_frag.page)
1753 put_page(vi->alloc_frag.page);
fb6813f4 1754
586d17c5
JW
1755 flush_work(&vi->config_work);
1756
2e66f55b 1757 free_percpu(vi->stats);
74b2553f 1758 free_netdev(vi->dev);
296f96fc
RR
1759}
1760
89107000 1761#ifdef CONFIG_PM_SLEEP
0741bcb5
AS
1762static int virtnet_freeze(struct virtio_device *vdev)
1763{
1764 struct virtnet_info *vi = vdev->priv;
986a4f4d 1765 int i;
0741bcb5 1766
ec9debbd
JW
1767 unregister_hotcpu_notifier(&vi->nb);
1768
586d17c5
JW
1769 /* Prevent config work handler from accessing the device */
1770 mutex_lock(&vi->config_lock);
1771 vi->config_enable = false;
1772 mutex_unlock(&vi->config_lock);
1773
0741bcb5
AS
1774 netif_device_detach(vi->dev);
1775 cancel_delayed_work_sync(&vi->refill);
1776
1777 if (netif_running(vi->dev))
986a4f4d
JW
1778 for (i = 0; i < vi->max_queue_pairs; i++) {
1779 napi_disable(&vi->rq[i].napi);
1780 netif_napi_del(&vi->rq[i].napi);
1781 }
0741bcb5
AS
1782
1783 remove_vq_common(vi);
1784
586d17c5
JW
1785 flush_work(&vi->config_work);
1786
0741bcb5
AS
1787 return 0;
1788}
1789
1790static int virtnet_restore(struct virtio_device *vdev)
1791{
1792 struct virtnet_info *vi = vdev->priv;
986a4f4d 1793 int err, i;
0741bcb5
AS
1794
1795 err = init_vqs(vi);
1796 if (err)
1797 return err;
1798
1799 if (netif_running(vi->dev))
986a4f4d
JW
1800 for (i = 0; i < vi->max_queue_pairs; i++)
1801 virtnet_napi_enable(&vi->rq[i]);
0741bcb5
AS
1802
1803 netif_device_attach(vi->dev);
1804
55257d72 1805 for (i = 0; i < vi->curr_queue_pairs; i++)
986a4f4d
JW
1806 if (!try_fill_recv(&vi->rq[i], GFP_KERNEL))
1807 schedule_delayed_work(&vi->refill, 0);
0741bcb5 1808
586d17c5
JW
1809 mutex_lock(&vi->config_lock);
1810 vi->config_enable = true;
1811 mutex_unlock(&vi->config_lock);
1812
35ed159b 1813 rtnl_lock();
986a4f4d 1814 virtnet_set_queues(vi, vi->curr_queue_pairs);
35ed159b 1815 rtnl_unlock();
986a4f4d 1816
ec9debbd
JW
1817 err = register_hotcpu_notifier(&vi->nb);
1818 if (err)
1819 return err;
1820
0741bcb5
AS
1821 return 0;
1822}
1823#endif
1824
296f96fc
RR
1825static struct virtio_device_id id_table[] = {
1826 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
1827 { 0 },
1828};
1829
c45a6816 1830static unsigned int features[] = {
5e4fe5c4
MM
1831 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM,
1832 VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
c45a6816 1833 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
97402b96 1834 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
5c516751 1835 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO,
2a41f71d 1836 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ,
0bde9569 1837 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN,
986a4f4d 1838 VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ,
7e58d5ae 1839 VIRTIO_NET_F_CTRL_MAC_ADDR,
e7428e95 1840 VIRTIO_F_ANY_LAYOUT,
c45a6816
RR
1841};
1842
22402529 1843static struct virtio_driver virtio_net_driver = {
c45a6816
RR
1844 .feature_table = features,
1845 .feature_table_size = ARRAY_SIZE(features),
296f96fc
RR
1846 .driver.name = KBUILD_MODNAME,
1847 .driver.owner = THIS_MODULE,
1848 .id_table = id_table,
1849 .probe = virtnet_probe,
8cc085d6 1850 .remove = virtnet_remove,
9f4d26d0 1851 .config_changed = virtnet_config_changed,
89107000 1852#ifdef CONFIG_PM_SLEEP
0741bcb5
AS
1853 .freeze = virtnet_freeze,
1854 .restore = virtnet_restore,
1855#endif
296f96fc
RR
1856};
1857
b2a17029 1858module_virtio_driver(virtio_net_driver);
296f96fc
RR
1859
1860MODULE_DEVICE_TABLE(virtio, id_table);
1861MODULE_DESCRIPTION("Virtio network driver");
1862MODULE_LICENSE("GPL");
This page took 0.801942 seconds and 5 git commands to generate.