blob: 02a71021565e65b4298e53c475ee078daea0875b [file] [log] [blame]
Rusty Russell48925e32009-09-24 09:59:20 -06001/* A network driver using virtio.
Rusty Russell296f96f2007-10-22 11:03:37 +10002 *
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>
Herbert Xua9ea3fc2008-04-18 11:21:42 +080022#include <linux/ethtool.h>
Rusty Russell296f96f2007-10-22 11:03:37 +100023#include <linux/module.h>
24#include <linux/virtio.h>
25#include <linux/virtio_net.h>
26#include <linux/scatterlist.h>
Alex Williamsone918085a2009-01-25 18:06:26 -080027#include <linux/if_vlan.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Rusty Russell296f96f2007-10-22 11:03:37 +100029
Dor Laor6c0cd7c2007-12-16 15:19:43 +020030static int napi_weight = 128;
31module_param(napi_weight, int, 0444);
32
Rusty Russelleb939922011-12-19 14:08:01 +000033static bool csum = true, gso = true;
Rusty Russell34a48572008-02-04 23:50:02 -050034module_param(csum, bool, 0444);
35module_param(gso, bool, 0444);
36
Rusty Russell296f96f2007-10-22 11:03:37 +100037/* FIXME: MTU in config. */
Alex Williamsone918085a2009-01-25 18:06:26 -080038#define MAX_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -080039#define GOOD_COPY_LEN 128
Rusty Russell296f96f2007-10-22 11:03:37 +100040
Alex Williamsonf565a7c2009-02-04 09:02:45 +000041#define VIRTNET_SEND_COMMAND_SG_MAX 2
Rick Jones66846042011-11-14 14:17:08 +000042#define VIRTNET_DRIVER_VERSION "1.0.0"
Alex Williamson2a41f712009-02-04 09:02:34 +000043
stephen hemminger3fa2a1d2011-06-15 06:36:29 +000044struct virtnet_stats {
Eric Dumazet83a27052012-06-05 22:35:24 +000045 struct u64_stats_sync tx_syncp;
46 struct u64_stats_sync rx_syncp;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +000047 u64 tx_bytes;
48 u64 tx_packets;
49
50 u64 rx_bytes;
51 u64 rx_packets;
52};
53
Jason Wange9d74172012-12-07 07:04:55 +000054/* Internal representation of a send virtqueue */
55struct send_queue {
56 /* Virtqueue associated with this send _queue */
57 struct virtqueue *vq;
58
59 /* TX: fragments + linear part + virtio header */
60 struct scatterlist sg[MAX_SKB_FRAGS + 2];
61};
62
63/* Internal representation of a receive virtqueue */
64struct receive_queue {
65 /* Virtqueue associated with this receive_queue */
66 struct virtqueue *vq;
67
Rusty Russell296f96f2007-10-22 11:03:37 +100068 struct napi_struct napi;
69
70 /* Number of input buffers, and max we've ever had. */
71 unsigned int num, max;
72
Jason Wange9d74172012-12-07 07:04:55 +000073 /* Chain pages by the private ptr. */
74 struct page *pages;
75
76 /* RX: fragments + linear part + virtio header */
77 struct scatterlist sg[MAX_SKB_FRAGS + 2];
78};
79
80struct virtnet_info {
81 struct virtio_device *vdev;
82 struct virtqueue *cvq;
83 struct net_device *dev;
84 struct send_queue sq;
85 struct receive_queue rq;
86 unsigned int status;
87
Herbert Xu97402b92008-04-18 11:24:27 +080088 /* I like... big packets and I cannot lie! */
89 bool big_packets;
90
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -080091 /* Host will merge rx buffers for big packets (shake it! shake it!) */
92 bool mergeable_rx_bufs;
93
Jason Wang586d17c2012-04-11 20:43:52 +000094 /* enable config space updates */
95 bool config_enable;
96
stephen hemminger3fa2a1d2011-06-15 06:36:29 +000097 /* Active statistics */
98 struct virtnet_stats __percpu *stats;
99
Rusty Russell3161e452009-08-26 12:22:32 -0700100 /* Work struct for refilling if we run low on memory. */
101 struct delayed_work refill;
102
Jason Wang586d17c2012-04-11 20:43:52 +0000103 /* Work struct for config space updates */
104 struct work_struct config_work;
105
106 /* Lock for config space updates */
107 struct mutex config_lock;
Rusty Russell296f96f2007-10-22 11:03:37 +1000108};
109
Rusty Russellb3f24692009-09-24 09:59:19 -0600110struct skb_vnet_hdr {
111 union {
112 struct virtio_net_hdr hdr;
113 struct virtio_net_hdr_mrg_rxbuf mhdr;
114 };
Rusty Russell48925e32009-09-24 09:59:20 -0600115 unsigned int num_sg;
Rusty Russellb3f24692009-09-24 09:59:19 -0600116};
117
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000118struct padded_vnet_hdr {
119 struct virtio_net_hdr hdr;
120 /*
121 * virtio_net_hdr should be in a separated sg buffer because of a
122 * QEMU bug, and data sg buffer shares same page with this header sg.
123 * This padding makes next sg 16 byte aligned after virtio_net_hdr.
124 */
125 char padding[6];
126};
127
Rusty Russellb3f24692009-09-24 09:59:19 -0600128static inline struct skb_vnet_hdr *skb_vnet_hdr(struct sk_buff *skb)
Rusty Russell296f96f2007-10-22 11:03:37 +1000129{
Rusty Russellb3f24692009-09-24 09:59:19 -0600130 return (struct skb_vnet_hdr *)skb->cb;
Rusty Russell296f96f2007-10-22 11:03:37 +1000131}
132
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000133/*
134 * private is used to chain pages for big packets, put the whole
135 * most recent used list in the beginning for reuse
136 */
Jason Wange9d74172012-12-07 07:04:55 +0000137static void give_pages(struct receive_queue *rq, struct page *page)
Rusty Russellfb6813f2008-07-25 12:06:01 -0500138{
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000139 struct page *end;
140
Jason Wange9d74172012-12-07 07:04:55 +0000141 /* Find end of list, sew whole thing into vi->rq.pages. */
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000142 for (end = page; end->private; end = (struct page *)end->private);
Jason Wange9d74172012-12-07 07:04:55 +0000143 end->private = (unsigned long)rq->pages;
144 rq->pages = page;
Rusty Russellfb6813f2008-07-25 12:06:01 -0500145}
146
Jason Wange9d74172012-12-07 07:04:55 +0000147static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
Rusty Russellfb6813f2008-07-25 12:06:01 -0500148{
Jason Wange9d74172012-12-07 07:04:55 +0000149 struct page *p = rq->pages;
Rusty Russellfb6813f2008-07-25 12:06:01 -0500150
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000151 if (p) {
Jason Wange9d74172012-12-07 07:04:55 +0000152 rq->pages = (struct page *)p->private;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000153 /* clear private here, it is used to chain pages */
154 p->private = 0;
155 } else
Rusty Russellfb6813f2008-07-25 12:06:01 -0500156 p = alloc_page(gfp_mask);
157 return p;
158}
159
Jason Wange9d74172012-12-07 07:04:55 +0000160static void skb_xmit_done(struct virtqueue *vq)
Rusty Russell296f96f2007-10-22 11:03:37 +1000161{
Jason Wange9d74172012-12-07 07:04:55 +0000162 struct virtnet_info *vi = vq->vdev->priv;
Rusty Russell296f96f2007-10-22 11:03:37 +1000163
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500164 /* Suppress further interrupts. */
Jason Wange9d74172012-12-07 07:04:55 +0000165 virtqueue_disable_cb(vq);
Rusty Russell11a3a152008-05-26 17:48:13 +1000166
Rusty Russell363f1512008-06-08 20:51:55 +1000167 /* We were probably waiting for more output buffers. */
Rusty Russell296f96f2007-10-22 11:03:37 +1000168 netif_wake_queue(vi->dev);
Rusty Russell296f96f2007-10-22 11:03:37 +1000169}
170
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000171static void set_skb_frag(struct sk_buff *skb, struct page *page,
172 unsigned int offset, unsigned int *len)
173{
Krishna Kumar8a59a7b2011-10-19 22:17:27 +0000174 int size = min((unsigned)PAGE_SIZE - offset, *len);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000175 int i = skb_shinfo(skb)->nr_frags;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000176
Krishna Kumar8a59a7b2011-10-19 22:17:27 +0000177 __skb_fill_page_desc(skb, i, page, offset, size);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000178
Krishna Kumar8a59a7b2011-10-19 22:17:27 +0000179 skb->data_len += size;
180 skb->len += size;
Eric Dumazet4b727362011-10-19 23:14:46 +0000181 skb->truesize += PAGE_SIZE;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000182 skb_shinfo(skb)->nr_frags++;
Krishna Kumar8a59a7b2011-10-19 22:17:27 +0000183 *len -= size;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000184}
185
Mike Waychison34646452012-01-04 12:52:32 +0000186/* Called from bottom half context */
Jason Wange9d74172012-12-07 07:04:55 +0000187static struct sk_buff *page_to_skb(struct receive_queue *rq,
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000188 struct page *page, unsigned int len)
189{
Jason Wange9d74172012-12-07 07:04:55 +0000190 struct virtnet_info *vi = rq->vq->vdev->priv;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000191 struct sk_buff *skb;
192 struct skb_vnet_hdr *hdr;
193 unsigned int copy, hdr_len, offset;
194 char *p;
195
196 p = page_address(page);
197
198 /* copy small packet so we can reuse these pages for small data */
199 skb = netdev_alloc_skb_ip_align(vi->dev, GOOD_COPY_LEN);
200 if (unlikely(!skb))
201 return NULL;
202
203 hdr = skb_vnet_hdr(skb);
204
205 if (vi->mergeable_rx_bufs) {
206 hdr_len = sizeof hdr->mhdr;
207 offset = hdr_len;
208 } else {
209 hdr_len = sizeof hdr->hdr;
210 offset = sizeof(struct padded_vnet_hdr);
211 }
212
213 memcpy(hdr, p, hdr_len);
214
215 len -= hdr_len;
216 p += offset;
217
218 copy = len;
219 if (copy > skb_tailroom(skb))
220 copy = skb_tailroom(skb);
221 memcpy(skb_put(skb, copy), p, copy);
222
223 len -= copy;
224 offset += copy;
225
Sasha Levine878d782011-09-28 04:40:54 +0000226 /*
227 * Verify that we can indeed put this data into a skb.
228 * This is here to handle cases when the device erroneously
229 * tries to receive more than is possible. This is usually
230 * the case of a broken device.
231 */
232 if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
Amerigo Wangbe443892012-11-08 17:47:28 +0000233 net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
Sasha Levine878d782011-09-28 04:40:54 +0000234 dev_kfree_skb(skb);
235 return NULL;
236 }
237
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000238 while (len) {
239 set_skb_frag(skb, page, offset, &len);
240 page = (struct page *)page->private;
241 offset = 0;
242 }
243
244 if (page)
Jason Wange9d74172012-12-07 07:04:55 +0000245 give_pages(rq, page);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000246
247 return skb;
248}
249
Jason Wange9d74172012-12-07 07:04:55 +0000250static int receive_mergeable(struct receive_queue *rq, struct sk_buff *skb)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000251{
252 struct skb_vnet_hdr *hdr = skb_vnet_hdr(skb);
253 struct page *page;
254 int num_buf, i, len;
255
256 num_buf = hdr->mhdr.num_buffers;
257 while (--num_buf) {
258 i = skb_shinfo(skb)->nr_frags;
259 if (i >= MAX_SKB_FRAGS) {
260 pr_debug("%s: packet too long\n", skb->dev->name);
261 skb->dev->stats.rx_length_errors++;
262 return -EINVAL;
263 }
Jason Wange9d74172012-12-07 07:04:55 +0000264 page = virtqueue_get_buf(rq->vq, &len);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000265 if (!page) {
266 pr_debug("%s: rx error: %d buffers missing\n",
267 skb->dev->name, hdr->mhdr.num_buffers);
268 skb->dev->stats.rx_length_errors++;
269 return -EINVAL;
270 }
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000271
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000272 if (len > PAGE_SIZE)
273 len = PAGE_SIZE;
274
275 set_skb_frag(skb, page, 0, &len);
276
Jason Wange9d74172012-12-07 07:04:55 +0000277 --rq->num;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000278 }
279 return 0;
280}
281
Jason Wange9d74172012-12-07 07:04:55 +0000282static void receive_buf(struct receive_queue *rq, void *buf, unsigned int len)
Rusty Russell296f96f2007-10-22 11:03:37 +1000283{
Jason Wange9d74172012-12-07 07:04:55 +0000284 struct virtnet_info *vi = rq->vq->vdev->priv;
285 struct net_device *dev = vi->dev;
Eric Dumazet58472a72012-02-13 06:53:41 +0000286 struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000287 struct sk_buff *skb;
288 struct page *page;
289 struct skb_vnet_hdr *hdr;
Rusty Russell296f96f2007-10-22 11:03:37 +1000290
291 if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) {
292 pr_debug("%s: short packet %i\n", dev->name, len);
293 dev->stats.rx_length_errors++;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000294 if (vi->mergeable_rx_bufs || vi->big_packets)
Jason Wange9d74172012-12-07 07:04:55 +0000295 give_pages(rq, buf);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000296 else
297 dev_kfree_skb(buf);
298 return;
Rusty Russell296f96f2007-10-22 11:03:37 +1000299 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000300
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000301 if (!vi->mergeable_rx_bufs && !vi->big_packets) {
302 skb = buf;
303 len -= sizeof(struct virtio_net_hdr);
304 skb_trim(skb, len);
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800305 } else {
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000306 page = buf;
Jason Wange9d74172012-12-07 07:04:55 +0000307 skb = page_to_skb(rq, page, len);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000308 if (unlikely(!skb)) {
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800309 dev->stats.rx_dropped++;
Jason Wange9d74172012-12-07 07:04:55 +0000310 give_pages(rq, page);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000311 return;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800312 }
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000313 if (vi->mergeable_rx_bufs)
Jason Wange9d74172012-12-07 07:04:55 +0000314 if (receive_mergeable(rq, skb)) {
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000315 dev_kfree_skb(skb);
316 return;
317 }
Herbert Xu97402b92008-04-18 11:24:27 +0800318 }
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800319
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000320 hdr = skb_vnet_hdr(skb);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000321
Eric Dumazet83a27052012-06-05 22:35:24 +0000322 u64_stats_update_begin(&stats->rx_syncp);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000323 stats->rx_bytes += skb->len;
324 stats->rx_packets++;
Eric Dumazet83a27052012-06-05 22:35:24 +0000325 u64_stats_update_end(&stats->rx_syncp);
Rusty Russell296f96f2007-10-22 11:03:37 +1000326
Rusty Russellb3f24692009-09-24 09:59:19 -0600327 if (hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000328 pr_debug("Needs csum!\n");
Rusty Russellb3f24692009-09-24 09:59:19 -0600329 if (!skb_partial_csum_set(skb,
330 hdr->hdr.csum_start,
331 hdr->hdr.csum_offset))
Rusty Russell296f96f2007-10-22 11:03:37 +1000332 goto frame_err;
Jason Wang10a8d942011-06-10 00:56:17 +0000333 } else if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID) {
334 skb->ip_summed = CHECKSUM_UNNECESSARY;
Rusty Russell296f96f2007-10-22 11:03:37 +1000335 }
336
Mark McLoughlin23cde762008-06-08 20:49:00 +1000337 skb->protocol = eth_type_trans(skb, dev);
338 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
339 ntohs(skb->protocol), skb->len, skb->pkt_type);
340
Rusty Russellb3f24692009-09-24 09:59:19 -0600341 if (hdr->hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000342 pr_debug("GSO!\n");
Rusty Russellb3f24692009-09-24 09:59:19 -0600343 switch (hdr->hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000344 case VIRTIO_NET_HDR_GSO_TCPV4:
345 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
346 break;
Rusty Russell296f96f2007-10-22 11:03:37 +1000347 case VIRTIO_NET_HDR_GSO_UDP:
348 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
349 break;
350 case VIRTIO_NET_HDR_GSO_TCPV6:
351 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
352 break;
353 default:
Amerigo Wangbe443892012-11-08 17:47:28 +0000354 net_warn_ratelimited("%s: bad gso type %u.\n",
355 dev->name, hdr->hdr.gso_type);
Rusty Russell296f96f2007-10-22 11:03:37 +1000356 goto frame_err;
357 }
358
Rusty Russellb3f24692009-09-24 09:59:19 -0600359 if (hdr->hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN)
Rusty Russell34a48572008-02-04 23:50:02 -0500360 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
361
Rusty Russellb3f24692009-09-24 09:59:19 -0600362 skb_shinfo(skb)->gso_size = hdr->hdr.gso_size;
Rusty Russell296f96f2007-10-22 11:03:37 +1000363 if (skb_shinfo(skb)->gso_size == 0) {
Amerigo Wangbe443892012-11-08 17:47:28 +0000364 net_warn_ratelimited("%s: zero gso size.\n", dev->name);
Rusty Russell296f96f2007-10-22 11:03:37 +1000365 goto frame_err;
366 }
367
368 /* Header must be checked, and gso_segs computed. */
369 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
370 skb_shinfo(skb)->gso_segs = 0;
371 }
372
373 netif_receive_skb(skb);
374 return;
375
376frame_err:
377 dev->stats.rx_frame_errors++;
Rusty Russell296f96f2007-10-22 11:03:37 +1000378 dev_kfree_skb(skb);
379}
380
Jason Wange9d74172012-12-07 07:04:55 +0000381static int add_recvbuf_small(struct receive_queue *rq, gfp_t gfp)
Rusty Russell296f96f2007-10-22 11:03:37 +1000382{
Jason Wange9d74172012-12-07 07:04:55 +0000383 struct virtnet_info *vi = rq->vq->vdev->priv;
Rusty Russell296f96f2007-10-22 11:03:37 +1000384 struct sk_buff *skb;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000385 struct skb_vnet_hdr *hdr;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000386 int err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000387
Mike Waychison34646452012-01-04 12:52:32 +0000388 skb = __netdev_alloc_skb_ip_align(vi->dev, MAX_PACKET_LEN, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000389 if (unlikely(!skb))
390 return -ENOMEM;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800391
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000392 skb_put(skb, MAX_PACKET_LEN);
393
394 hdr = skb_vnet_hdr(skb);
Jason Wange9d74172012-12-07 07:04:55 +0000395 sg_set_buf(rq->sg, &hdr->hdr, sizeof hdr->hdr);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000396
Jason Wange9d74172012-12-07 07:04:55 +0000397 skb_to_sgvec(skb, rq->sg + 1, 0, skb->len);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000398
Jason Wange9d74172012-12-07 07:04:55 +0000399 err = virtqueue_add_buf(rq->vq, rq->sg, 0, 2, skb, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000400 if (err < 0)
401 dev_kfree_skb(skb);
402
403 return err;
404}
405
Jason Wange9d74172012-12-07 07:04:55 +0000406static int add_recvbuf_big(struct receive_queue *rq, gfp_t gfp)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000407{
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000408 struct page *first, *list = NULL;
409 char *p;
410 int i, err, offset;
411
Jason Wange9d74172012-12-07 07:04:55 +0000412 /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000413 for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
Jason Wange9d74172012-12-07 07:04:55 +0000414 first = get_a_page(rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000415 if (!first) {
416 if (list)
Jason Wange9d74172012-12-07 07:04:55 +0000417 give_pages(rq, list);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000418 return -ENOMEM;
Rusty Russell3161e452009-08-26 12:22:32 -0700419 }
Jason Wange9d74172012-12-07 07:04:55 +0000420 sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
Rusty Russell296f96f2007-10-22 11:03:37 +1000421
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000422 /* chain new page in list head to match sg */
423 first->private = (unsigned long)list;
424 list = first;
425 }
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800426
Jason Wange9d74172012-12-07 07:04:55 +0000427 first = get_a_page(rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000428 if (!first) {
Jason Wange9d74172012-12-07 07:04:55 +0000429 give_pages(rq, list);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000430 return -ENOMEM;
431 }
432 p = page_address(first);
Herbert Xu97402b92008-04-18 11:24:27 +0800433
Jason Wange9d74172012-12-07 07:04:55 +0000434 /* rq->sg[0], rq->sg[1] share the same page */
435 /* a separated rq->sg[0] for virtio_net_hdr only due to QEMU bug */
436 sg_set_buf(&rq->sg[0], p, sizeof(struct virtio_net_hdr));
Herbert Xu97402b92008-04-18 11:24:27 +0800437
Jason Wange9d74172012-12-07 07:04:55 +0000438 /* rq->sg[1] for data packet, from offset */
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000439 offset = sizeof(struct padded_vnet_hdr);
Jason Wange9d74172012-12-07 07:04:55 +0000440 sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
Herbert Xu97402b92008-04-18 11:24:27 +0800441
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000442 /* chain first in list head */
443 first->private = (unsigned long)list;
Jason Wange9d74172012-12-07 07:04:55 +0000444 err = virtqueue_add_buf(rq->vq, rq->sg, 0, MAX_SKB_FRAGS + 2,
Rusty Russellf96fde42012-01-12 15:44:42 +1030445 first, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000446 if (err < 0)
Jason Wange9d74172012-12-07 07:04:55 +0000447 give_pages(rq, first);
Herbert Xu97402b92008-04-18 11:24:27 +0800448
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000449 return err;
450}
Herbert Xu97402b92008-04-18 11:24:27 +0800451
Jason Wange9d74172012-12-07 07:04:55 +0000452static int add_recvbuf_mergeable(struct receive_queue *rq, gfp_t gfp)
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000453{
454 struct page *page;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000455 int err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000456
Jason Wange9d74172012-12-07 07:04:55 +0000457 page = get_a_page(rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000458 if (!page)
459 return -ENOMEM;
460
Jason Wange9d74172012-12-07 07:04:55 +0000461 sg_init_one(rq->sg, page_address(page), PAGE_SIZE);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000462
Jason Wange9d74172012-12-07 07:04:55 +0000463 err = virtqueue_add_buf(rq->vq, rq->sg, 0, 1, page, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000464 if (err < 0)
Jason Wange9d74172012-12-07 07:04:55 +0000465 give_pages(rq, page);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000466
467 return err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000468}
469
Rusty Russellb2baed62011-12-29 00:42:38 +0000470/*
471 * Returns false if we couldn't fill entirely (OOM).
472 *
473 * Normally run in the receive path, but can also be run from ndo_open
474 * before we're receiving packets, or from refill_work which is
475 * careful to disable receiving (using napi_disable).
476 */
Jason Wange9d74172012-12-07 07:04:55 +0000477static bool try_fill_recv(struct receive_queue *rq, gfp_t gfp)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800478{
Jason Wange9d74172012-12-07 07:04:55 +0000479 struct virtnet_info *vi = rq->vq->vdev->priv;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800480 int err;
Michael S. Tsirkin1788f4952010-07-02 16:32:55 +0000481 bool oom;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800482
Amit Shah0aea51c2009-08-26 14:58:28 +0530483 do {
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000484 if (vi->mergeable_rx_bufs)
Jason Wange9d74172012-12-07 07:04:55 +0000485 err = add_recvbuf_mergeable(rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000486 else if (vi->big_packets)
Jason Wange9d74172012-12-07 07:04:55 +0000487 err = add_recvbuf_big(rq, gfp);
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000488 else
Jason Wange9d74172012-12-07 07:04:55 +0000489 err = add_recvbuf_small(rq, gfp);
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800490
Michael S. Tsirkin1788f4952010-07-02 16:32:55 +0000491 oom = err == -ENOMEM;
492 if (err < 0)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800493 break;
Jason Wange9d74172012-12-07 07:04:55 +0000494 ++rq->num;
Amit Shah0aea51c2009-08-26 14:58:28 +0530495 } while (err > 0);
Jason Wange9d74172012-12-07 07:04:55 +0000496 if (unlikely(rq->num > rq->max))
497 rq->max = rq->num;
498 virtqueue_kick(rq->vq);
Rusty Russell3161e452009-08-26 12:22:32 -0700499 return !oom;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800500}
501
Rusty Russell18445c42008-02-04 23:49:57 -0500502static void skb_recv_done(struct virtqueue *rvq)
Rusty Russell296f96f2007-10-22 11:03:37 +1000503{
504 struct virtnet_info *vi = rvq->vdev->priv;
Jason Wange9d74172012-12-07 07:04:55 +0000505 struct receive_queue *rq = &vi->rq;
506
Rusty Russell18445c42008-02-04 23:49:57 -0500507 /* Schedule NAPI, Suppress further interrupts if successful. */
Jason Wange9d74172012-12-07 07:04:55 +0000508 if (napi_schedule_prep(&rq->napi)) {
Michael S. Tsirkin1915a7122010-04-12 16:19:04 +0300509 virtqueue_disable_cb(rvq);
Jason Wange9d74172012-12-07 07:04:55 +0000510 __napi_schedule(&rq->napi);
Rusty Russell18445c42008-02-04 23:49:57 -0500511 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000512}
513
Jason Wange9d74172012-12-07 07:04:55 +0000514static void virtnet_napi_enable(struct receive_queue *rq)
Bruce Rogers3e9d08e2011-02-10 11:03:31 -0800515{
Jason Wange9d74172012-12-07 07:04:55 +0000516 napi_enable(&rq->napi);
Bruce Rogers3e9d08e2011-02-10 11:03:31 -0800517
518 /* If all buffers were filled by other side before we napi_enabled, we
519 * won't get another interrupt, so process any outstanding packets
520 * now. virtnet_poll wants re-enable the queue, so we disable here.
521 * We synchronize against interrupts via NAPI_STATE_SCHED */
Jason Wange9d74172012-12-07 07:04:55 +0000522 if (napi_schedule_prep(&rq->napi)) {
523 virtqueue_disable_cb(rq->vq);
Michael S. Tsirkinec13ee82012-05-16 10:57:12 +0300524 local_bh_disable();
Jason Wange9d74172012-12-07 07:04:55 +0000525 __napi_schedule(&rq->napi);
Michael S. Tsirkinec13ee82012-05-16 10:57:12 +0300526 local_bh_enable();
Bruce Rogers3e9d08e2011-02-10 11:03:31 -0800527 }
528}
529
Rusty Russell3161e452009-08-26 12:22:32 -0700530static void refill_work(struct work_struct *work)
531{
Jason Wange9d74172012-12-07 07:04:55 +0000532 struct virtnet_info *vi =
533 container_of(work, struct virtnet_info, refill.work);
Rusty Russell3161e452009-08-26 12:22:32 -0700534 bool still_empty;
535
Jason Wange9d74172012-12-07 07:04:55 +0000536 napi_disable(&vi->rq.napi);
537 still_empty = !try_fill_recv(&vi->rq, GFP_KERNEL);
538 virtnet_napi_enable(&vi->rq);
Rusty Russell3161e452009-08-26 12:22:32 -0700539
540 /* In theory, this can happen: if we don't get any buffers in
541 * we will *never* try to fill again. */
542 if (still_empty)
Tejun Heo3b07e9c2012-08-20 14:51:24 -0700543 schedule_delayed_work(&vi->refill, HZ/2);
Rusty Russell3161e452009-08-26 12:22:32 -0700544}
545
Rusty Russell296f96f2007-10-22 11:03:37 +1000546static int virtnet_poll(struct napi_struct *napi, int budget)
547{
Jason Wange9d74172012-12-07 07:04:55 +0000548 struct receive_queue *rq =
549 container_of(napi, struct receive_queue, napi);
550 struct virtnet_info *vi = rq->vq->vdev->priv;
Shirley Ma9ab86bb2010-01-29 03:20:04 +0000551 void *buf;
Rusty Russell296f96f2007-10-22 11:03:37 +1000552 unsigned int len, received = 0;
553
554again:
555 while (received < budget &&
Jason Wange9d74172012-12-07 07:04:55 +0000556 (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
557 receive_buf(rq, buf, len);
558 --rq->num;
Rusty Russell296f96f2007-10-22 11:03:37 +1000559 received++;
560 }
561
Jason Wange9d74172012-12-07 07:04:55 +0000562 if (rq->num < rq->max / 2) {
563 if (!try_fill_recv(rq, GFP_ATOMIC))
Tejun Heo3b07e9c2012-08-20 14:51:24 -0700564 schedule_delayed_work(&vi->refill, 0);
Rusty Russell3161e452009-08-26 12:22:32 -0700565 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000566
Rusty Russell8329d982007-11-19 11:20:43 -0500567 /* Out of packets? */
568 if (received < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -0800569 napi_complete(napi);
Jason Wange9d74172012-12-07 07:04:55 +0000570 if (unlikely(!virtqueue_enable_cb(rq->vq)) &&
Joe Perches8e95a202009-12-03 07:58:21 +0000571 napi_schedule_prep(napi)) {
Jason Wange9d74172012-12-07 07:04:55 +0000572 virtqueue_disable_cb(rq->vq);
Ben Hutchings288379f2009-01-19 16:43:59 -0800573 __napi_schedule(napi);
Rusty Russell296f96f2007-10-22 11:03:37 +1000574 goto again;
Christian Borntraeger4265f162008-03-14 14:17:05 +0100575 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000576 }
577
578 return received;
579}
580
Jason Wange9d74172012-12-07 07:04:55 +0000581static unsigned int free_old_xmit_skbs(struct send_queue *sq)
Rusty Russell296f96f2007-10-22 11:03:37 +1000582{
583 struct sk_buff *skb;
Rusty Russell48925e32009-09-24 09:59:20 -0600584 unsigned int len, tot_sgs = 0;
Jason Wange9d74172012-12-07 07:04:55 +0000585 struct virtnet_info *vi = sq->vq->vdev->priv;
Eric Dumazet58472a72012-02-13 06:53:41 +0000586 struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
Rusty Russell296f96f2007-10-22 11:03:37 +1000587
Jason Wange9d74172012-12-07 07:04:55 +0000588 while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000589 pr_debug("Sent skb %p\n", skb);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000590
Eric Dumazet83a27052012-06-05 22:35:24 +0000591 u64_stats_update_begin(&stats->tx_syncp);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000592 stats->tx_bytes += skb->len;
593 stats->tx_packets++;
Eric Dumazet83a27052012-06-05 22:35:24 +0000594 u64_stats_update_end(&stats->tx_syncp);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000595
Rusty Russell48925e32009-09-24 09:59:20 -0600596 tot_sgs += skb_vnet_hdr(skb)->num_sg;
Eric Dumazeted79bab2009-10-14 14:36:43 +0000597 dev_kfree_skb_any(skb);
Rusty Russell296f96f2007-10-22 11:03:37 +1000598 }
Rusty Russell48925e32009-09-24 09:59:20 -0600599 return tot_sgs;
Rusty Russell296f96f2007-10-22 11:03:37 +1000600}
601
Jason Wange9d74172012-12-07 07:04:55 +0000602static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
Rusty Russell296f96f2007-10-22 11:03:37 +1000603{
Rusty Russellb3f24692009-09-24 09:59:19 -0600604 struct skb_vnet_hdr *hdr = skb_vnet_hdr(skb);
Rusty Russell296f96f2007-10-22 11:03:37 +1000605 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
Jason Wange9d74172012-12-07 07:04:55 +0000606 struct virtnet_info *vi = sq->vq->vdev->priv;
Rusty Russell296f96f2007-10-22 11:03:37 +1000607
Johannes Berge1749612008-10-27 15:59:26 -0700608 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
Rusty Russell296f96f2007-10-22 11:03:37 +1000609
Rusty Russell296f96f2007-10-22 11:03:37 +1000610 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Rusty Russellb3f24692009-09-24 09:59:19 -0600611 hdr->hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
Michał Mirosław55508d62010-12-14 15:24:08 +0000612 hdr->hdr.csum_start = skb_checksum_start_offset(skb);
Rusty Russellb3f24692009-09-24 09:59:19 -0600613 hdr->hdr.csum_offset = skb->csum_offset;
Rusty Russell296f96f2007-10-22 11:03:37 +1000614 } else {
Rusty Russellb3f24692009-09-24 09:59:19 -0600615 hdr->hdr.flags = 0;
616 hdr->hdr.csum_offset = hdr->hdr.csum_start = 0;
Rusty Russell296f96f2007-10-22 11:03:37 +1000617 }
618
619 if (skb_is_gso(skb)) {
Rusty Russellb3f24692009-09-24 09:59:19 -0600620 hdr->hdr.hdr_len = skb_headlen(skb);
621 hdr->hdr.gso_size = skb_shinfo(skb)->gso_size;
Rusty Russell34a48572008-02-04 23:50:02 -0500622 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
Rusty Russellb3f24692009-09-24 09:59:19 -0600623 hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
Rusty Russell296f96f2007-10-22 11:03:37 +1000624 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
Rusty Russellb3f24692009-09-24 09:59:19 -0600625 hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
Rusty Russell296f96f2007-10-22 11:03:37 +1000626 else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
Rusty Russellb3f24692009-09-24 09:59:19 -0600627 hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP;
Rusty Russell296f96f2007-10-22 11:03:37 +1000628 else
629 BUG();
Rusty Russell34a48572008-02-04 23:50:02 -0500630 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
Rusty Russellb3f24692009-09-24 09:59:19 -0600631 hdr->hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
Rusty Russell296f96f2007-10-22 11:03:37 +1000632 } else {
Rusty Russellb3f24692009-09-24 09:59:19 -0600633 hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
634 hdr->hdr.gso_size = hdr->hdr.hdr_len = 0;
Rusty Russell296f96f2007-10-22 11:03:37 +1000635 }
636
Rusty Russellb3f24692009-09-24 09:59:19 -0600637 hdr->mhdr.num_buffers = 0;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800638
639 /* Encode metadata header at front. */
640 if (vi->mergeable_rx_bufs)
Jason Wange9d74172012-12-07 07:04:55 +0000641 sg_set_buf(sq->sg, &hdr->mhdr, sizeof hdr->mhdr);
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800642 else
Jason Wange9d74172012-12-07 07:04:55 +0000643 sg_set_buf(sq->sg, &hdr->hdr, sizeof hdr->hdr);
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800644
Jason Wange9d74172012-12-07 07:04:55 +0000645 hdr->num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len) + 1;
646 return virtqueue_add_buf(sq->vq, sq->sg, hdr->num_sg,
Rusty Russellf96fde42012-01-12 15:44:42 +1030647 0, skb, GFP_ATOMIC);
Rusty Russell11a3a152008-05-26 17:48:13 +1000648}
649
Stephen Hemminger424efe92009-08-31 19:50:51 +0000650static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
Rusty Russell99ffc692008-05-02 21:50:46 -0500651{
652 struct virtnet_info *vi = netdev_priv(dev);
Jason Wange9d74172012-12-07 07:04:55 +0000653 struct send_queue *sq = &vi->sq;
Rusty Russell48925e32009-09-24 09:59:20 -0600654 int capacity;
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500655
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500656 /* Free up any pending old buffers before queueing new ones. */
Jason Wange9d74172012-12-07 07:04:55 +0000657 free_old_xmit_skbs(sq);
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500658
Michael S. Tsirkin03f191b2009-10-28 04:03:38 -0700659 /* Try to transmit */
Jason Wange9d74172012-12-07 07:04:55 +0000660 capacity = xmit_skb(sq, skb);
Rusty Russell48925e32009-09-24 09:59:20 -0600661
662 /* This can happen with OOM and indirect buffers. */
663 if (unlikely(capacity < 0)) {
Rick Jones2e57b792012-03-27 07:28:09 +0000664 if (likely(capacity == -ENOMEM)) {
Torsten Kaiser31304162012-04-09 05:14:15 +0000665 if (net_ratelimit())
Rusty Russell58eba972010-07-02 16:34:01 +0000666 dev_warn(&dev->dev,
667 "TX queue failure: out of memory\n");
Torsten Kaiser31304162012-04-09 05:14:15 +0000668 } else {
Rick Jones2e57b792012-03-27 07:28:09 +0000669 dev->stats.tx_fifo_errors++;
670 if (net_ratelimit())
Rusty Russell58eba972010-07-02 16:34:01 +0000671 dev_warn(&dev->dev,
672 "Unexpected TX queue failure: %d\n",
673 capacity);
Rusty Russell48925e32009-09-24 09:59:20 -0600674 }
Rusty Russell58eba972010-07-02 16:34:01 +0000675 dev->stats.tx_dropped++;
676 kfree_skb(skb);
677 return NETDEV_TX_OK;
Rusty Russell99ffc692008-05-02 21:50:46 -0500678 }
Jason Wange9d74172012-12-07 07:04:55 +0000679 virtqueue_kick(sq->vq);
Michael S. Tsirkin03f191b2009-10-28 04:03:38 -0700680
Rusty Russell48925e32009-09-24 09:59:20 -0600681 /* Don't wait up for transmitted skbs to be freed. */
682 skb_orphan(skb);
683 nf_reset(skb);
Rusty Russell99ffc692008-05-02 21:50:46 -0500684
Rusty Russell48925e32009-09-24 09:59:20 -0600685 /* Apparently nice girls don't return TX_BUSY; stop the queue
686 * before it gets out of hand. Naturally, this wastes entries. */
687 if (capacity < 2+MAX_SKB_FRAGS) {
688 netif_stop_queue(dev);
Jason Wange9d74172012-12-07 07:04:55 +0000689 if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
Rusty Russell48925e32009-09-24 09:59:20 -0600690 /* More just got used, free them then recheck. */
Jason Wange9d74172012-12-07 07:04:55 +0000691 capacity += free_old_xmit_skbs(sq);
Rusty Russell48925e32009-09-24 09:59:20 -0600692 if (capacity >= 2+MAX_SKB_FRAGS) {
693 netif_start_queue(dev);
Jason Wange9d74172012-12-07 07:04:55 +0000694 virtqueue_disable_cb(sq->vq);
Rusty Russell48925e32009-09-24 09:59:20 -0600695 }
696 }
Rusty Russell99ffc692008-05-02 21:50:46 -0500697 }
Rusty Russell48925e32009-09-24 09:59:20 -0600698
699 return NETDEV_TX_OK;
Rusty Russell296f96f2007-10-22 11:03:37 +1000700}
701
Alex Williamson9c46f6d2009-02-04 16:36:34 -0800702static int virtnet_set_mac_address(struct net_device *dev, void *p)
703{
704 struct virtnet_info *vi = netdev_priv(dev);
705 struct virtio_device *vdev = vi->vdev;
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +0000706 int ret;
Alex Williamson9c46f6d2009-02-04 16:36:34 -0800707
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +0000708 ret = eth_mac_addr(dev, p);
709 if (ret)
710 return ret;
Alex Williamson9c46f6d2009-02-04 16:36:34 -0800711
Alex Williamson62994b22009-04-04 16:40:19 -0700712 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
713 vdev->config->set(vdev, offsetof(struct virtio_net_config, mac),
714 dev->dev_addr, dev->addr_len);
Alex Williamson9c46f6d2009-02-04 16:36:34 -0800715
716 return 0;
717}
718
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000719static struct rtnl_link_stats64 *virtnet_stats(struct net_device *dev,
720 struct rtnl_link_stats64 *tot)
721{
722 struct virtnet_info *vi = netdev_priv(dev);
723 int cpu;
724 unsigned int start;
725
726 for_each_possible_cpu(cpu) {
Eric Dumazet58472a72012-02-13 06:53:41 +0000727 struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000728 u64 tpackets, tbytes, rpackets, rbytes;
729
730 do {
Kevin Groenevelde3906482012-07-21 06:30:50 +0000731 start = u64_stats_fetch_begin_bh(&stats->tx_syncp);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000732 tpackets = stats->tx_packets;
733 tbytes = stats->tx_bytes;
Kevin Groenevelde3906482012-07-21 06:30:50 +0000734 } while (u64_stats_fetch_retry_bh(&stats->tx_syncp, start));
Eric Dumazet83a27052012-06-05 22:35:24 +0000735
736 do {
Kevin Groenevelde3906482012-07-21 06:30:50 +0000737 start = u64_stats_fetch_begin_bh(&stats->rx_syncp);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000738 rpackets = stats->rx_packets;
739 rbytes = stats->rx_bytes;
Kevin Groenevelde3906482012-07-21 06:30:50 +0000740 } while (u64_stats_fetch_retry_bh(&stats->rx_syncp, start));
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000741
742 tot->rx_packets += rpackets;
743 tot->tx_packets += tpackets;
744 tot->rx_bytes += rbytes;
745 tot->tx_bytes += tbytes;
746 }
747
748 tot->tx_dropped = dev->stats.tx_dropped;
Rick Jones021ac8d2011-11-21 09:28:17 +0000749 tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000750 tot->rx_dropped = dev->stats.rx_dropped;
751 tot->rx_length_errors = dev->stats.rx_length_errors;
752 tot->rx_frame_errors = dev->stats.rx_frame_errors;
753
754 return tot;
755}
756
Amit Shahda74e892008-02-29 16:24:50 +0530757#ifdef CONFIG_NET_POLL_CONTROLLER
758static void virtnet_netpoll(struct net_device *dev)
759{
760 struct virtnet_info *vi = netdev_priv(dev);
761
Jason Wange9d74172012-12-07 07:04:55 +0000762 napi_schedule(&vi->rq.napi);
Amit Shahda74e892008-02-29 16:24:50 +0530763}
764#endif
765
Rusty Russell296f96f2007-10-22 11:03:37 +1000766static int virtnet_open(struct net_device *dev)
767{
768 struct virtnet_info *vi = netdev_priv(dev);
769
Rusty Russellb2baed62011-12-29 00:42:38 +0000770 /* Make sure we have some buffers: if oom use wq. */
Jason Wange9d74172012-12-07 07:04:55 +0000771 if (!try_fill_recv(&vi->rq, GFP_KERNEL))
Tejun Heo3b07e9c2012-08-20 14:51:24 -0700772 schedule_delayed_work(&vi->refill, 0);
Rusty Russellb2baed62011-12-29 00:42:38 +0000773
Jason Wange9d74172012-12-07 07:04:55 +0000774 virtnet_napi_enable(&vi->rq);
Rusty Russell296f96f2007-10-22 11:03:37 +1000775 return 0;
776}
777
Alex Williamson2a41f712009-02-04 09:02:34 +0000778/*
779 * Send command via the control virtqueue and check status. Commands
780 * supported by the hypervisor, as indicated by feature bits, should
781 * never fail unless improperly formated.
782 */
783static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
784 struct scatterlist *data, int out, int in)
785{
Alex Williamson23e258e2009-05-01 17:27:56 +0000786 struct scatterlist *s, sg[VIRTNET_SEND_COMMAND_SG_MAX + 2];
Alex Williamson2a41f712009-02-04 09:02:34 +0000787 struct virtio_net_ctrl_hdr ctrl;
788 virtio_net_ctrl_ack status = ~0;
789 unsigned int tmp;
Alex Williamson23e258e2009-05-01 17:27:56 +0000790 int i;
Alex Williamson2a41f712009-02-04 09:02:34 +0000791
Alexander Beregalov0ee904c2009-04-11 14:50:23 +0000792 /* Caller should know better */
793 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) ||
794 (out + in > VIRTNET_SEND_COMMAND_SG_MAX));
Alex Williamson2a41f712009-02-04 09:02:34 +0000795
796 out++; /* Add header */
797 in++; /* Add return status */
798
799 ctrl.class = class;
800 ctrl.cmd = cmd;
801
802 sg_init_table(sg, out + in);
803
804 sg_set_buf(&sg[0], &ctrl, sizeof(ctrl));
Alex Williamson23e258e2009-05-01 17:27:56 +0000805 for_each_sg(data, s, out + in - 2, i)
806 sg_set_buf(&sg[i + 1], sg_virt(s), s->length);
Alex Williamson2a41f712009-02-04 09:02:34 +0000807 sg_set_buf(&sg[out + in - 1], &status, sizeof(status));
808
Rusty Russellf96fde42012-01-12 15:44:42 +1030809 BUG_ON(virtqueue_add_buf(vi->cvq, sg, out, in, vi, GFP_ATOMIC) < 0);
Alex Williamson2a41f712009-02-04 09:02:34 +0000810
Michael S. Tsirkin1915a7122010-04-12 16:19:04 +0300811 virtqueue_kick(vi->cvq);
Alex Williamson2a41f712009-02-04 09:02:34 +0000812
813 /*
814 * Spin for a response, the kick causes an ioport write, trapping
815 * into the hypervisor, so the request should be handled immediately.
816 */
Michael S. Tsirkin1915a7122010-04-12 16:19:04 +0300817 while (!virtqueue_get_buf(vi->cvq, &tmp))
Alex Williamson2a41f712009-02-04 09:02:34 +0000818 cpu_relax();
819
820 return status == VIRTIO_NET_OK;
821}
822
Jason Wang586d17c2012-04-11 20:43:52 +0000823static void virtnet_ack_link_announce(struct virtnet_info *vi)
824{
825 rtnl_lock();
826 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
827 VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL,
828 0, 0))
829 dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
830 rtnl_unlock();
831}
832
Rusty Russell296f96f2007-10-22 11:03:37 +1000833static int virtnet_close(struct net_device *dev)
834{
835 struct virtnet_info *vi = netdev_priv(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +1000836
Rusty Russellb2baed62011-12-29 00:42:38 +0000837 /* Make sure refill_work doesn't re-enable napi! */
838 cancel_delayed_work_sync(&vi->refill);
Jason Wange9d74172012-12-07 07:04:55 +0000839 napi_disable(&vi->rq.napi);
Rusty Russell296f96f2007-10-22 11:03:37 +1000840
Rusty Russell296f96f2007-10-22 11:03:37 +1000841 return 0;
842}
843
Alex Williamson2af76982009-02-04 09:02:40 +0000844static void virtnet_set_rx_mode(struct net_device *dev)
845{
846 struct virtnet_info *vi = netdev_priv(dev);
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000847 struct scatterlist sg[2];
Alex Williamson2af76982009-02-04 09:02:40 +0000848 u8 promisc, allmulti;
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000849 struct virtio_net_ctrl_mac *mac_data;
Jiri Pirkoccffad252009-05-22 23:22:17 +0000850 struct netdev_hw_addr *ha;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -0800851 int uc_count;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000852 int mc_count;
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000853 void *buf;
854 int i;
Alex Williamson2af76982009-02-04 09:02:40 +0000855
856 /* We can't dynamicaly set ndo_set_rx_mode, so return gracefully */
857 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
858 return;
859
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000860 promisc = ((dev->flags & IFF_PROMISC) != 0);
861 allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
Alex Williamson2af76982009-02-04 09:02:40 +0000862
Alex Williamson23e258e2009-05-01 17:27:56 +0000863 sg_init_one(sg, &promisc, sizeof(promisc));
Alex Williamson2af76982009-02-04 09:02:40 +0000864
865 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
866 VIRTIO_NET_CTRL_RX_PROMISC,
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000867 sg, 1, 0))
Alex Williamson2af76982009-02-04 09:02:40 +0000868 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
869 promisc ? "en" : "dis");
870
Alex Williamson23e258e2009-05-01 17:27:56 +0000871 sg_init_one(sg, &allmulti, sizeof(allmulti));
Alex Williamson2af76982009-02-04 09:02:40 +0000872
873 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
874 VIRTIO_NET_CTRL_RX_ALLMULTI,
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000875 sg, 1, 0))
Alex Williamson2af76982009-02-04 09:02:40 +0000876 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
877 allmulti ? "en" : "dis");
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000878
Jiri Pirko32e7bfc2010-01-25 13:36:10 -0800879 uc_count = netdev_uc_count(dev);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000880 mc_count = netdev_mc_count(dev);
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000881 /* MAC filter - use one buffer for both lists */
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000882 buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
883 (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
884 mac_data = buf;
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000885 if (!buf) {
886 dev_warn(&dev->dev, "No memory for MAC address buffer\n");
887 return;
888 }
889
Alex Williamson23e258e2009-05-01 17:27:56 +0000890 sg_init_table(sg, 2);
891
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000892 /* Store the unicast list and count in the front of the buffer */
Jiri Pirko32e7bfc2010-01-25 13:36:10 -0800893 mac_data->entries = uc_count;
Jiri Pirkoccffad252009-05-22 23:22:17 +0000894 i = 0;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -0800895 netdev_for_each_uc_addr(ha, dev)
Jiri Pirkoccffad252009-05-22 23:22:17 +0000896 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000897
898 sg_set_buf(&sg[0], mac_data,
Jiri Pirko32e7bfc2010-01-25 13:36:10 -0800899 sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000900
901 /* multicast list and count fill the end */
Jiri Pirko32e7bfc2010-01-25 13:36:10 -0800902 mac_data = (void *)&mac_data->macs[uc_count][0];
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000903
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000904 mac_data->entries = mc_count;
Jiri Pirko567ec872010-02-23 23:17:07 +0000905 i = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000906 netdev_for_each_mc_addr(ha, dev)
907 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000908
909 sg_set_buf(&sg[1], mac_data,
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000910 sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000911
912 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
913 VIRTIO_NET_CTRL_MAC_TABLE_SET,
914 sg, 2, 0))
915 dev_warn(&dev->dev, "Failed to set MAC fitler table.\n");
916
917 kfree(buf);
Alex Williamson2af76982009-02-04 09:02:40 +0000918}
919
Jiri Pirko8e586132011-12-08 19:52:37 -0500920static int virtnet_vlan_rx_add_vid(struct net_device *dev, u16 vid)
Alex Williamson0bde95692009-02-04 09:02:50 +0000921{
922 struct virtnet_info *vi = netdev_priv(dev);
923 struct scatterlist sg;
924
Alex Williamson23e258e2009-05-01 17:27:56 +0000925 sg_init_one(&sg, &vid, sizeof(vid));
Alex Williamson0bde95692009-02-04 09:02:50 +0000926
927 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
928 VIRTIO_NET_CTRL_VLAN_ADD, &sg, 1, 0))
929 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
Jiri Pirko8e586132011-12-08 19:52:37 -0500930 return 0;
Alex Williamson0bde95692009-02-04 09:02:50 +0000931}
932
Jiri Pirko8e586132011-12-08 19:52:37 -0500933static int virtnet_vlan_rx_kill_vid(struct net_device *dev, u16 vid)
Alex Williamson0bde95692009-02-04 09:02:50 +0000934{
935 struct virtnet_info *vi = netdev_priv(dev);
936 struct scatterlist sg;
937
Alex Williamson23e258e2009-05-01 17:27:56 +0000938 sg_init_one(&sg, &vid, sizeof(vid));
Alex Williamson0bde95692009-02-04 09:02:50 +0000939
940 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
941 VIRTIO_NET_CTRL_VLAN_DEL, &sg, 1, 0))
942 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
Jiri Pirko8e586132011-12-08 19:52:37 -0500943 return 0;
Alex Williamson0bde95692009-02-04 09:02:50 +0000944}
945
Rick Jones8f9f4662011-10-19 08:10:59 +0000946static void virtnet_get_ringparam(struct net_device *dev,
947 struct ethtool_ringparam *ring)
948{
949 struct virtnet_info *vi = netdev_priv(dev);
950
Jason Wange9d74172012-12-07 07:04:55 +0000951 ring->rx_max_pending = virtqueue_get_vring_size(vi->rq.vq);
952 ring->tx_max_pending = virtqueue_get_vring_size(vi->sq.vq);
Rick Jones8f9f4662011-10-19 08:10:59 +0000953 ring->rx_pending = ring->rx_max_pending;
954 ring->tx_pending = ring->tx_max_pending;
Rick Jones8f9f4662011-10-19 08:10:59 +0000955}
956
Rick Jones66846042011-11-14 14:17:08 +0000957
958static void virtnet_get_drvinfo(struct net_device *dev,
959 struct ethtool_drvinfo *info)
960{
961 struct virtnet_info *vi = netdev_priv(dev);
962 struct virtio_device *vdev = vi->vdev;
963
964 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
965 strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
966 strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
967
968}
969
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700970static const struct ethtool_ops virtnet_ethtool_ops = {
Rick Jones66846042011-11-14 14:17:08 +0000971 .get_drvinfo = virtnet_get_drvinfo,
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -0800972 .get_link = ethtool_op_get_link,
Rick Jones8f9f4662011-10-19 08:10:59 +0000973 .get_ringparam = virtnet_get_ringparam,
Herbert Xua9ea3fc2008-04-18 11:21:42 +0800974};
975
Mark McLoughlin39da5812008-11-26 13:58:11 +0000976#define MIN_MTU 68
977#define MAX_MTU 65535
978
979static int virtnet_change_mtu(struct net_device *dev, int new_mtu)
980{
981 if (new_mtu < MIN_MTU || new_mtu > MAX_MTU)
982 return -EINVAL;
983 dev->mtu = new_mtu;
984 return 0;
985}
986
Stephen Hemminger76288b42009-01-06 10:44:22 -0800987static const struct net_device_ops virtnet_netdev = {
988 .ndo_open = virtnet_open,
989 .ndo_stop = virtnet_close,
990 .ndo_start_xmit = start_xmit,
991 .ndo_validate_addr = eth_validate_addr,
Alex Williamson9c46f6d2009-02-04 16:36:34 -0800992 .ndo_set_mac_address = virtnet_set_mac_address,
Alex Williamson2af76982009-02-04 09:02:40 +0000993 .ndo_set_rx_mode = virtnet_set_rx_mode,
Stephen Hemminger76288b42009-01-06 10:44:22 -0800994 .ndo_change_mtu = virtnet_change_mtu,
stephen hemminger3fa2a1d2011-06-15 06:36:29 +0000995 .ndo_get_stats64 = virtnet_stats,
Alex Williamson1824a982009-05-01 17:31:10 +0000996 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
997 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
Stephen Hemminger76288b42009-01-06 10:44:22 -0800998#ifdef CONFIG_NET_POLL_CONTROLLER
999 .ndo_poll_controller = virtnet_netpoll,
1000#endif
1001};
1002
Jason Wang586d17c2012-04-11 20:43:52 +00001003static void virtnet_config_changed_work(struct work_struct *work)
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001004{
Jason Wang586d17c2012-04-11 20:43:52 +00001005 struct virtnet_info *vi =
1006 container_of(work, struct virtnet_info, config_work);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001007 u16 v;
1008
Jason Wang586d17c2012-04-11 20:43:52 +00001009 mutex_lock(&vi->config_lock);
1010 if (!vi->config_enable)
1011 goto done;
1012
Sasha Levin77dd7692011-08-14 17:52:33 +03001013 if (virtio_config_val(vi->vdev, VIRTIO_NET_F_STATUS,
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001014 offsetof(struct virtio_net_config, status),
Sasha Levin77dd7692011-08-14 17:52:33 +03001015 &v) < 0)
Jason Wang586d17c2012-04-11 20:43:52 +00001016 goto done;
1017
1018 if (v & VIRTIO_NET_S_ANNOUNCE) {
Amerigo Wangee89bab2012-08-09 22:14:56 +00001019 netdev_notify_peers(vi->dev);
Jason Wang586d17c2012-04-11 20:43:52 +00001020 virtnet_ack_link_announce(vi);
1021 }
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001022
1023 /* Ignore unknown (future) status bits */
1024 v &= VIRTIO_NET_S_LINK_UP;
1025
1026 if (vi->status == v)
Jason Wang586d17c2012-04-11 20:43:52 +00001027 goto done;
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001028
1029 vi->status = v;
1030
1031 if (vi->status & VIRTIO_NET_S_LINK_UP) {
1032 netif_carrier_on(vi->dev);
1033 netif_wake_queue(vi->dev);
1034 } else {
1035 netif_carrier_off(vi->dev);
1036 netif_stop_queue(vi->dev);
1037 }
Jason Wang586d17c2012-04-11 20:43:52 +00001038done:
1039 mutex_unlock(&vi->config_lock);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001040}
1041
1042static void virtnet_config_changed(struct virtio_device *vdev)
1043{
1044 struct virtnet_info *vi = vdev->priv;
1045
Tejun Heo3b07e9c2012-08-20 14:51:24 -07001046 schedule_work(&vi->config_work);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001047}
1048
Jason Wange9d74172012-12-07 07:04:55 +00001049static void virtnet_del_vqs(struct virtnet_info *vi)
1050{
1051 struct virtio_device *vdev = vi->vdev;
1052
1053 vdev->config->del_vqs(vdev);
1054}
1055
Amit Shah3f9c10b2011-12-22 16:58:31 +05301056static int init_vqs(struct virtnet_info *vi)
1057{
1058 struct virtqueue *vqs[3];
1059 vq_callback_t *callbacks[] = { skb_recv_done, skb_xmit_done, NULL};
1060 const char *names[] = { "input", "output", "control" };
1061 int nvqs, err;
1062
1063 /* We expect two virtqueues, receive then send,
1064 * and optionally control. */
1065 nvqs = virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) ? 3 : 2;
1066
1067 err = vi->vdev->config->find_vqs(vi->vdev, nvqs, vqs, callbacks, names);
1068 if (err)
1069 return err;
1070
Jason Wange9d74172012-12-07 07:04:55 +00001071 vi->rq.vq = vqs[0];
1072 vi->sq.vq = vqs[1];
Amit Shah3f9c10b2011-12-22 16:58:31 +05301073
1074 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)) {
1075 vi->cvq = vqs[2];
1076
1077 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
1078 vi->dev->features |= NETIF_F_HW_VLAN_FILTER;
1079 }
1080 return 0;
1081}
1082
Rusty Russell296f96f2007-10-22 11:03:37 +10001083static int virtnet_probe(struct virtio_device *vdev)
1084{
1085 int err;
Rusty Russell296f96f2007-10-22 11:03:37 +10001086 struct net_device *dev;
1087 struct virtnet_info *vi;
Rusty Russell296f96f2007-10-22 11:03:37 +10001088
1089 /* Allocate ourselves a network device with room for our info */
1090 dev = alloc_etherdev(sizeof(struct virtnet_info));
1091 if (!dev)
1092 return -ENOMEM;
1093
1094 /* Set up network device as normal. */
Jiri Pirkof2f2c8b2012-06-29 05:10:06 +00001095 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
Stephen Hemminger76288b42009-01-06 10:44:22 -08001096 dev->netdev_ops = &virtnet_netdev;
Rusty Russell296f96f2007-10-22 11:03:37 +10001097 dev->features = NETIF_F_HIGHDMA;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001098
Herbert Xua9ea3fc2008-04-18 11:21:42 +08001099 SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops);
Rusty Russell296f96f2007-10-22 11:03:37 +10001100 SET_NETDEV_DEV(dev, &vdev->dev);
1101
1102 /* Do we support "hardware" checksums? */
Michał Mirosław98e778c2011-03-31 01:01:35 +00001103 if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
Rusty Russell296f96f2007-10-22 11:03:37 +10001104 /* This opens up the world of extra features. */
Michał Mirosław98e778c2011-03-31 01:01:35 +00001105 dev->hw_features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
1106 if (csum)
1107 dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
1108
1109 if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
1110 dev->hw_features |= NETIF_F_TSO | NETIF_F_UFO
Rusty Russell34a48572008-02-04 23:50:02 -05001111 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
1112 }
Rusty Russell5539ae92008-05-02 21:50:46 -05001113 /* Individual feature bits: what can host handle? */
Michał Mirosław98e778c2011-03-31 01:01:35 +00001114 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
1115 dev->hw_features |= NETIF_F_TSO;
1116 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
1117 dev->hw_features |= NETIF_F_TSO6;
1118 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
1119 dev->hw_features |= NETIF_F_TSO_ECN;
1120 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
1121 dev->hw_features |= NETIF_F_UFO;
1122
1123 if (gso)
1124 dev->features |= dev->hw_features & (NETIF_F_ALL_TSO|NETIF_F_UFO);
1125 /* (!csum && gso) case will be fixed by register_netdev() */
Rusty Russell296f96f2007-10-22 11:03:37 +10001126 }
1127
1128 /* Configuration may specify what MAC to use. Otherwise random. */
Sasha Levin77dd7692011-08-14 17:52:33 +03001129 if (virtio_config_val_len(vdev, VIRTIO_NET_F_MAC,
Rusty Russella586d4f2008-02-04 23:49:56 -05001130 offsetof(struct virtio_net_config, mac),
Sasha Levin77dd7692011-08-14 17:52:33 +03001131 dev->dev_addr, dev->addr_len) < 0)
Danny Kukawkaf2cedb62012-02-15 06:45:39 +00001132 eth_hw_addr_random(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10001133
1134 /* Set up our device-specific information */
1135 vi = netdev_priv(dev);
Jason Wange9d74172012-12-07 07:04:55 +00001136 netif_napi_add(dev, &vi->rq.napi, virtnet_poll, napi_weight);
Rusty Russell296f96f2007-10-22 11:03:37 +10001137 vi->dev = dev;
1138 vi->vdev = vdev;
Christian Borntraegerd9d5dcc2008-02-18 10:02:51 +01001139 vdev->priv = vi;
Jason Wange9d74172012-12-07 07:04:55 +00001140 vi->rq.pages = NULL;
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001141 vi->stats = alloc_percpu(struct virtnet_stats);
1142 err = -ENOMEM;
1143 if (vi->stats == NULL)
1144 goto free;
1145
Rusty Russell3161e452009-08-26 12:22:32 -07001146 INIT_DELAYED_WORK(&vi->refill, refill_work);
Jason Wang586d17c2012-04-11 20:43:52 +00001147 mutex_init(&vi->config_lock);
1148 vi->config_enable = true;
1149 INIT_WORK(&vi->config_work, virtnet_config_changed_work);
Jason Wange9d74172012-12-07 07:04:55 +00001150 sg_init_table(vi->rq.sg, ARRAY_SIZE(vi->rq.sg));
1151 sg_init_table(vi->sq.sg, ARRAY_SIZE(vi->sq.sg));
Rusty Russell296f96f2007-10-22 11:03:37 +10001152
Herbert Xu97402b92008-04-18 11:24:27 +08001153 /* If we can receive ANY GSO packets, we must allocate large ones. */
Joe Perches8e95a202009-12-03 07:58:21 +00001154 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
1155 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
1156 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN))
Herbert Xu97402b92008-04-18 11:24:27 +08001157 vi->big_packets = true;
1158
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -08001159 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
1160 vi->mergeable_rx_bufs = true;
1161
Amit Shah3f9c10b2011-12-22 16:58:31 +05301162 err = init_vqs(vi);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06001163 if (err)
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001164 goto free_stats;
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06001165
Rusty Russell296f96f2007-10-22 11:03:37 +10001166 err = register_netdev(dev);
1167 if (err) {
1168 pr_debug("virtio_net: registering device failed\n");
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06001169 goto free_vqs;
Rusty Russell296f96f2007-10-22 11:03:37 +10001170 }
Rusty Russellb3369c12008-02-04 23:50:02 -05001171
1172 /* Last of all, set up some receive buffers. */
Jason Wange9d74172012-12-07 07:04:55 +00001173 try_fill_recv(&vi->rq, GFP_KERNEL);
Rusty Russellb3369c12008-02-04 23:50:02 -05001174
1175 /* If we didn't even get one input buffer, we're useless. */
Jason Wange9d74172012-12-07 07:04:55 +00001176 if (vi->rq.num == 0) {
Rusty Russellb3369c12008-02-04 23:50:02 -05001177 err = -ENOMEM;
1178 goto unregister;
1179 }
1180
Jason Wang167c25e2010-11-10 14:45:41 +00001181 /* Assume link up if device can't report link status,
1182 otherwise get link status from config. */
1183 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
1184 netif_carrier_off(dev);
Tejun Heo3b07e9c2012-08-20 14:51:24 -07001185 schedule_work(&vi->config_work);
Jason Wang167c25e2010-11-10 14:45:41 +00001186 } else {
1187 vi->status = VIRTIO_NET_S_LINK_UP;
1188 netif_carrier_on(dev);
1189 }
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001190
Rusty Russell296f96f2007-10-22 11:03:37 +10001191 pr_debug("virtnet: registered device %s\n", dev->name);
Rusty Russell296f96f2007-10-22 11:03:37 +10001192 return 0;
1193
Rusty Russellb3369c12008-02-04 23:50:02 -05001194unregister:
1195 unregister_netdev(dev);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06001196free_vqs:
Jason Wange9d74172012-12-07 07:04:55 +00001197 virtnet_del_vqs(vi);
stephen hemminger3fa2a1d2011-06-15 06:36:29 +00001198free_stats:
1199 free_percpu(vi->stats);
Rusty Russell296f96f2007-10-22 11:03:37 +10001200free:
1201 free_netdev(dev);
1202 return err;
1203}
1204
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001205static void free_unused_bufs(struct virtnet_info *vi)
1206{
1207 void *buf;
1208 while (1) {
Jason Wange9d74172012-12-07 07:04:55 +00001209 buf = virtqueue_detach_unused_buf(vi->sq.vq);
Shirley Ma830a8a92010-02-08 14:14:42 +00001210 if (!buf)
1211 break;
1212 dev_kfree_skb(buf);
1213 }
1214 while (1) {
Jason Wange9d74172012-12-07 07:04:55 +00001215 buf = virtqueue_detach_unused_buf(vi->rq.vq);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001216 if (!buf)
1217 break;
1218 if (vi->mergeable_rx_bufs || vi->big_packets)
Jason Wange9d74172012-12-07 07:04:55 +00001219 give_pages(&vi->rq, buf);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001220 else
1221 dev_kfree_skb(buf);
Jason Wange9d74172012-12-07 07:04:55 +00001222 --vi->rq.num;
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001223 }
Jason Wange9d74172012-12-07 07:04:55 +00001224 BUG_ON(vi->rq.num != 0);
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001225}
1226
Amit Shah04486ed2011-12-22 16:58:32 +05301227static void remove_vq_common(struct virtnet_info *vi)
Rusty Russell296f96f2007-10-22 11:03:37 +10001228{
Amit Shah04486ed2011-12-22 16:58:32 +05301229 vi->vdev->config->reset(vi->vdev);
Shirley Ma830a8a92010-02-08 14:14:42 +00001230
1231 /* Free unused buffers in both send and recv, if any. */
Shirley Ma9ab86bb2010-01-29 03:20:04 +00001232 free_unused_bufs(vi);
Rusty Russellfb6813f2008-07-25 12:06:01 -05001233
Jason Wange9d74172012-12-07 07:04:55 +00001234 virtnet_del_vqs(vi);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -06001235
Jason Wange9d74172012-12-07 07:04:55 +00001236 while (vi->rq.pages)
1237 __free_pages(get_a_page(&vi->rq, GFP_KERNEL), 0);
Amit Shah04486ed2011-12-22 16:58:32 +05301238}
1239
Bill Pemberton8cc085d2012-12-03 09:24:15 -05001240static void virtnet_remove(struct virtio_device *vdev)
Amit Shah04486ed2011-12-22 16:58:32 +05301241{
1242 struct virtnet_info *vi = vdev->priv;
1243
Jason Wang586d17c2012-04-11 20:43:52 +00001244 /* Prevent config work handler from accessing the device. */
1245 mutex_lock(&vi->config_lock);
1246 vi->config_enable = false;
1247 mutex_unlock(&vi->config_lock);
1248
Amit Shah04486ed2011-12-22 16:58:32 +05301249 unregister_netdev(vi->dev);
1250
1251 remove_vq_common(vi);
Rusty Russellfb6813f2008-07-25 12:06:01 -05001252
Jason Wang586d17c2012-04-11 20:43:52 +00001253 flush_work(&vi->config_work);
1254
Krishna Kumar2e66f552011-07-20 03:56:02 +00001255 free_percpu(vi->stats);
Rusty Russell74b25532007-11-19 11:20:42 -05001256 free_netdev(vi->dev);
Rusty Russell296f96f2007-10-22 11:03:37 +10001257}
1258
Amit Shah0741bcb2011-12-22 16:58:33 +05301259#ifdef CONFIG_PM
1260static int virtnet_freeze(struct virtio_device *vdev)
1261{
1262 struct virtnet_info *vi = vdev->priv;
1263
Jason Wang586d17c2012-04-11 20:43:52 +00001264 /* Prevent config work handler from accessing the device */
1265 mutex_lock(&vi->config_lock);
1266 vi->config_enable = false;
1267 mutex_unlock(&vi->config_lock);
1268
Amit Shah0741bcb2011-12-22 16:58:33 +05301269 netif_device_detach(vi->dev);
1270 cancel_delayed_work_sync(&vi->refill);
1271
1272 if (netif_running(vi->dev))
Jason Wange9d74172012-12-07 07:04:55 +00001273 napi_disable(&vi->rq.napi);
Amit Shah0741bcb2011-12-22 16:58:33 +05301274
1275 remove_vq_common(vi);
1276
Jason Wang586d17c2012-04-11 20:43:52 +00001277 flush_work(&vi->config_work);
1278
Amit Shah0741bcb2011-12-22 16:58:33 +05301279 return 0;
1280}
1281
1282static int virtnet_restore(struct virtio_device *vdev)
1283{
1284 struct virtnet_info *vi = vdev->priv;
1285 int err;
1286
1287 err = init_vqs(vi);
1288 if (err)
1289 return err;
1290
1291 if (netif_running(vi->dev))
Jason Wange9d74172012-12-07 07:04:55 +00001292 virtnet_napi_enable(&vi->rq);
Amit Shah0741bcb2011-12-22 16:58:33 +05301293
1294 netif_device_attach(vi->dev);
1295
Jason Wange9d74172012-12-07 07:04:55 +00001296 if (!try_fill_recv(&vi->rq, GFP_KERNEL))
Tejun Heo3b07e9c2012-08-20 14:51:24 -07001297 schedule_delayed_work(&vi->refill, 0);
Amit Shah0741bcb2011-12-22 16:58:33 +05301298
Jason Wang586d17c2012-04-11 20:43:52 +00001299 mutex_lock(&vi->config_lock);
1300 vi->config_enable = true;
1301 mutex_unlock(&vi->config_lock);
1302
Amit Shah0741bcb2011-12-22 16:58:33 +05301303 return 0;
1304}
1305#endif
1306
Rusty Russell296f96f2007-10-22 11:03:37 +10001307static struct virtio_device_id id_table[] = {
1308 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
1309 { 0 },
1310};
1311
Rusty Russellc45a6812008-05-02 21:50:50 -05001312static unsigned int features[] = {
Mark McLoughlin5e4fe5c2008-07-08 17:10:42 +10001313 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM,
1314 VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
Rusty Russellc45a6812008-05-02 21:50:50 -05001315 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
Herbert Xu97402b92008-04-18 11:24:27 +08001316 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
Sridhar Samudrala5c516752009-07-14 14:21:02 +00001317 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO,
Alex Williamson2a41f712009-02-04 09:02:34 +00001318 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ,
Alex Williamson0bde95692009-02-04 09:02:50 +00001319 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN,
Jason Wang586d17c2012-04-11 20:43:52 +00001320 VIRTIO_NET_F_GUEST_ANNOUNCE,
Rusty Russellc45a6812008-05-02 21:50:50 -05001321};
1322
Uwe Kleine-König22402522009-11-05 01:32:44 -08001323static struct virtio_driver virtio_net_driver = {
Rusty Russellc45a6812008-05-02 21:50:50 -05001324 .feature_table = features,
1325 .feature_table_size = ARRAY_SIZE(features),
Rusty Russell296f96f2007-10-22 11:03:37 +10001326 .driver.name = KBUILD_MODNAME,
1327 .driver.owner = THIS_MODULE,
1328 .id_table = id_table,
1329 .probe = virtnet_probe,
Bill Pemberton8cc085d2012-12-03 09:24:15 -05001330 .remove = virtnet_remove,
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001331 .config_changed = virtnet_config_changed,
Amit Shah0741bcb2011-12-22 16:58:33 +05301332#ifdef CONFIG_PM
1333 .freeze = virtnet_freeze,
1334 .restore = virtnet_restore,
1335#endif
Rusty Russell296f96f2007-10-22 11:03:37 +10001336};
1337
1338static int __init init(void)
1339{
Uwe Kleine-König22402522009-11-05 01:32:44 -08001340 return register_virtio_driver(&virtio_net_driver);
Rusty Russell296f96f2007-10-22 11:03:37 +10001341}
1342
1343static void __exit fini(void)
1344{
Uwe Kleine-König22402522009-11-05 01:32:44 -08001345 unregister_virtio_driver(&virtio_net_driver);
Rusty Russell296f96f2007-10-22 11:03:37 +10001346}
1347module_init(init);
1348module_exit(fini);
1349
1350MODULE_DEVICE_TABLE(virtio, id_table);
1351MODULE_DESCRIPTION("Virtio network driver");
1352MODULE_LICENSE("GPL");