]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/net/hyperv/netvsc_drv.c
Merge tag 'shared-for-4.9-1' of git://git.kernel.org/pub/scm/linux/kernel/git/leon...
[mirror_ubuntu-bionic-kernel.git] / drivers / net / hyperv / netvsc_drv.c
CommitLineData
fceaf24a 1/*
fceaf24a
HJ
2 * Copyright (c) 2009, Microsoft Corporation.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
adf8d3ff 14 * this program; if not, see <http://www.gnu.org/licenses/>.
fceaf24a
HJ
15 *
16 * Authors:
d0e94d17 17 * Haiyang Zhang <haiyangz@microsoft.com>
fceaf24a 18 * Hank Janssen <hjanssen@microsoft.com>
fceaf24a 19 */
eb335bc4
HJ
20#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
fceaf24a 22#include <linux/init.h>
9079ce69 23#include <linux/atomic.h>
fceaf24a
HJ
24#include <linux/module.h>
25#include <linux/highmem.h>
26#include <linux/device.h>
fceaf24a 27#include <linux/io.h>
fceaf24a
HJ
28#include <linux/delay.h>
29#include <linux/netdevice.h>
30#include <linux/inetdevice.h>
31#include <linux/etherdevice.h>
32#include <linux/skbuff.h>
c802db11 33#include <linux/if_vlan.h>
fceaf24a 34#include <linux/in.h>
5a0e3ad6 35#include <linux/slab.h>
fceaf24a
HJ
36#include <net/arp.h>
37#include <net/route.h>
38#include <net/sock.h>
39#include <net/pkt_sched.h>
3f335ea2 40
5ca7252a 41#include "hyperv_net.h"
fceaf24a 42
fceaf24a 43
fa85a6c2 44#define RING_SIZE_MIN 64
27a70af3 45#define LINKCHANGE_INT (2 * HZ)
a060679c 46#define NETVSC_HW_FEATURES (NETIF_F_RXCSUM | \
47 NETIF_F_SG | \
48 NETIF_F_TSO | \
49 NETIF_F_TSO6 | \
50 NETIF_F_HW_CSUM)
99c8da0f 51static int ring_size = 128;
450d7a4b
SH
52module_param(ring_size, int, S_IRUGO);
53MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)");
fceaf24a 54
e01ec219
KS
55static int max_num_vrss_chns = 8;
56
3f300ff4
SX
57static const u32 default_msg = NETIF_MSG_DRV | NETIF_MSG_PROBE |
58 NETIF_MSG_LINK | NETIF_MSG_IFUP |
59 NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR |
60 NETIF_MSG_TX_ERR;
61
62static int debug = -1;
63module_param(debug, int, S_IRUGO);
64MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
65
d426b2e3
HZ
66static void do_set_multicast(struct work_struct *w)
67{
792df872
WM
68 struct net_device_context *ndevctx =
69 container_of(w, struct net_device_context, work);
0a1275ca
VK
70 struct hv_device *device_obj = ndevctx->device_ctx;
71 struct net_device *ndev = hv_get_drvdata(device_obj);
72 struct netvsc_device *nvdev = ndevctx->nvdev;
d426b2e3
HZ
73 struct rndis_device *rdev;
74
0a1275ca 75 if (!nvdev)
792df872 76 return;
d426b2e3
HZ
77
78 rdev = nvdev->extension;
79 if (rdev == NULL)
792df872 80 return;
d426b2e3 81
0a1275ca 82 if (ndev->flags & IFF_PROMISC)
d426b2e3
HZ
83 rndis_filter_set_packet_filter(rdev,
84 NDIS_PACKET_TYPE_PROMISCUOUS);
85 else
86 rndis_filter_set_packet_filter(rdev,
87 NDIS_PACKET_TYPE_BROADCAST |
88 NDIS_PACKET_TYPE_ALL_MULTICAST |
89 NDIS_PACKET_TYPE_DIRECTED);
d426b2e3
HZ
90}
91
4e9bfefa 92static void netvsc_set_multicast_list(struct net_device *net)
fceaf24a 93{
792df872 94 struct net_device_context *net_device_ctx = netdev_priv(net);
d426b2e3 95
792df872 96 schedule_work(&net_device_ctx->work);
fceaf24a
HJ
97}
98
fceaf24a
HJ
99static int netvsc_open(struct net_device *net)
100{
2f5fa6c8 101 struct netvsc_device *nvdev = net_device_to_netvsc_device(net);
891de74d 102 struct rndis_device *rdev;
02fafbc6 103 int ret = 0;
fceaf24a 104
891de74d
HZ
105 netif_carrier_off(net);
106
d515d0ff 107 /* Open up the device */
2f5fa6c8 108 ret = rndis_filter_open(nvdev);
d515d0ff
HZ
109 if (ret != 0) {
110 netdev_err(net, "unable to open device (ret %d).\n", ret);
111 return ret;
fceaf24a
HJ
112 }
113
2de8530b 114 netif_tx_wake_all_queues(net);
d515d0ff 115
891de74d
HZ
116 rdev = nvdev->extension;
117 if (!rdev->link_state)
118 netif_carrier_on(net);
119
fceaf24a
HJ
120 return ret;
121}
122
fceaf24a
HJ
123static int netvsc_close(struct net_device *net)
124{
fceaf24a 125 struct net_device_context *net_device_ctx = netdev_priv(net);
3d541ac5 126 struct netvsc_device *nvdev = net_device_ctx->nvdev;
02fafbc6 127 int ret;
2de8530b
HZ
128 u32 aread, awrite, i, msec = 10, retry = 0, retry_max = 20;
129 struct vmbus_channel *chn;
fceaf24a 130
0a282538 131 netif_tx_disable(net);
fceaf24a 132
792df872
WM
133 /* Make sure netvsc_set_multicast_list doesn't re-enable filter! */
134 cancel_work_sync(&net_device_ctx->work);
2f5fa6c8 135 ret = rndis_filter_close(nvdev);
2de8530b 136 if (ret != 0) {
eb335bc4 137 netdev_err(net, "unable to close device (ret %d).\n", ret);
2de8530b
HZ
138 return ret;
139 }
140
141 /* Ensure pending bytes in ring are read */
142 while (true) {
143 aread = 0;
144 for (i = 0; i < nvdev->num_chn; i++) {
145 chn = nvdev->chn_table[i];
146 if (!chn)
147 continue;
148
149 hv_get_ringbuffer_availbytes(&chn->inbound, &aread,
150 &awrite);
151
152 if (aread)
153 break;
154
155 hv_get_ringbuffer_availbytes(&chn->outbound, &aread,
156 &awrite);
157
158 if (aread)
159 break;
160 }
161
162 retry++;
163 if (retry > retry_max || aread == 0)
164 break;
165
166 msleep(msec);
167
168 if (msec < 1000)
169 msec *= 2;
170 }
171
172 if (aread) {
173 netdev_err(net, "Ring buffer not empty after closing rndis\n");
174 ret = -ETIMEDOUT;
175 }
fceaf24a 176
fceaf24a
HJ
177 return ret;
178}
179
8a00251a
KS
180static void *init_ppi_data(struct rndis_message *msg, u32 ppi_size,
181 int pkt_type)
182{
183 struct rndis_packet *rndis_pkt;
184 struct rndis_per_packet_info *ppi;
185
186 rndis_pkt = &msg->msg.pkt;
187 rndis_pkt->data_offset += ppi_size;
188
189 ppi = (struct rndis_per_packet_info *)((void *)rndis_pkt +
190 rndis_pkt->per_pkt_info_offset + rndis_pkt->per_pkt_info_len);
191
192 ppi->size = ppi_size;
193 ppi->type = pkt_type;
194 ppi->ppi_offset = sizeof(struct rndis_per_packet_info);
195
196 rndis_pkt->per_pkt_info_len += ppi_size;
197
198 return ppi;
199}
200
5b54dac8
HZ
201static u16 netvsc_select_queue(struct net_device *ndev, struct sk_buff *skb,
202 void *accel_priv, select_queue_fallback_t fallback)
203{
204 struct net_device_context *net_device_ctx = netdev_priv(ndev);
3d541ac5 205 struct netvsc_device *nvsc_dev = net_device_ctx->nvdev;
5b54dac8
HZ
206 u32 hash;
207 u16 q_idx = 0;
208
209 if (nvsc_dev == NULL || ndev->real_num_tx_queues <= 1)
210 return 0;
211
757647e1
VK
212 hash = skb_get_hash(skb);
213 q_idx = nvsc_dev->send_table[hash % VRSS_SEND_TAB_SIZE] %
214 ndev->real_num_tx_queues;
5b54dac8 215
8b9fbe1a
VK
216 if (!nvsc_dev->chn_table[q_idx])
217 q_idx = 0;
218
5b54dac8
HZ
219 return q_idx;
220}
221
54a7357f
KS
222static u32 fill_pg_buf(struct page *page, u32 offset, u32 len,
223 struct hv_page_buffer *pb)
224{
225 int j = 0;
226
227 /* Deal with compund pages by ignoring unused part
228 * of the page.
229 */
230 page += (offset >> PAGE_SHIFT);
231 offset &= ~PAGE_MASK;
232
233 while (len > 0) {
234 unsigned long bytes;
235
236 bytes = PAGE_SIZE - offset;
237 if (bytes > len)
238 bytes = len;
239 pb[j].pfn = page_to_pfn(page);
240 pb[j].offset = offset;
241 pb[j].len = bytes;
242
243 offset += bytes;
244 len -= bytes;
245
246 if (offset == PAGE_SIZE && len) {
247 page++;
248 offset = 0;
249 j++;
250 }
251 }
252
253 return j + 1;
254}
255
8a00251a 256static u32 init_page_array(void *hdr, u32 len, struct sk_buff *skb,
a9f2e2d6
KS
257 struct hv_netvsc_packet *packet,
258 struct hv_page_buffer **page_buf)
54a7357f 259{
a9f2e2d6 260 struct hv_page_buffer *pb = *page_buf;
54a7357f
KS
261 u32 slots_used = 0;
262 char *data = skb->data;
263 int frags = skb_shinfo(skb)->nr_frags;
264 int i;
265
266 /* The packet is laid out thus:
aa0a34be 267 * 1. hdr: RNDIS header and PPI
54a7357f
KS
268 * 2. skb linear data
269 * 3. skb fragment data
270 */
271 if (hdr != NULL)
272 slots_used += fill_pg_buf(virt_to_page(hdr),
273 offset_in_page(hdr),
274 len, &pb[slots_used]);
275
aa0a34be
HZ
276 packet->rmsg_size = len;
277 packet->rmsg_pgcnt = slots_used;
278
54a7357f
KS
279 slots_used += fill_pg_buf(virt_to_page(data),
280 offset_in_page(data),
281 skb_headlen(skb), &pb[slots_used]);
282
283 for (i = 0; i < frags; i++) {
284 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
285
286 slots_used += fill_pg_buf(skb_frag_page(frag),
287 frag->page_offset,
288 skb_frag_size(frag), &pb[slots_used]);
289 }
8a00251a 290 return slots_used;
54a7357f
KS
291}
292
293static int count_skb_frag_slots(struct sk_buff *skb)
294{
295 int i, frags = skb_shinfo(skb)->nr_frags;
296 int pages = 0;
297
298 for (i = 0; i < frags; i++) {
299 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
300 unsigned long size = skb_frag_size(frag);
301 unsigned long offset = frag->page_offset;
302
303 /* Skip unused frames from start of page */
304 offset &= ~PAGE_MASK;
305 pages += PFN_UP(offset + size);
306 }
307 return pages;
308}
309
310static int netvsc_get_slots(struct sk_buff *skb)
311{
312 char *data = skb->data;
313 unsigned int offset = offset_in_page(data);
314 unsigned int len = skb_headlen(skb);
315 int slots;
316 int frag_slots;
317
318 slots = DIV_ROUND_UP(offset + len, PAGE_SIZE);
319 frag_slots = count_skb_frag_slots(skb);
320 return slots + frag_slots;
321}
322
08cd04bf
KS
323static u32 get_net_transport_info(struct sk_buff *skb, u32 *trans_off)
324{
325 u32 ret_val = TRANSPORT_INFO_NOT_IP;
326
327 if ((eth_hdr(skb)->h_proto != htons(ETH_P_IP)) &&
328 (eth_hdr(skb)->h_proto != htons(ETH_P_IPV6))) {
329 goto not_ip;
330 }
331
332 *trans_off = skb_transport_offset(skb);
333
334 if ((eth_hdr(skb)->h_proto == htons(ETH_P_IP))) {
335 struct iphdr *iphdr = ip_hdr(skb);
336
337 if (iphdr->protocol == IPPROTO_TCP)
338 ret_val = TRANSPORT_INFO_IPV4_TCP;
339 else if (iphdr->protocol == IPPROTO_UDP)
340 ret_val = TRANSPORT_INFO_IPV4_UDP;
341 } else {
342 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
343 ret_val = TRANSPORT_INFO_IPV6_TCP;
344 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
345 ret_val = TRANSPORT_INFO_IPV6_UDP;
346 }
347
348not_ip:
349 return ret_val;
350}
351
02fafbc6 352static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
fceaf24a 353{
fceaf24a 354 struct net_device_context *net_device_ctx = netdev_priv(net);
981a1bd8 355 struct hv_netvsc_packet *packet = NULL;
02fafbc6 356 int ret;
8a00251a
KS
357 unsigned int num_data_pgs;
358 struct rndis_message *rndis_msg;
359 struct rndis_packet *rndis_pkt;
360 u32 rndis_msg_size;
361 bool isvlan;
e88f7e07 362 bool linear = false;
8a00251a 363 struct rndis_per_packet_info *ppi;
08cd04bf 364 struct ndis_tcp_ip_checksum_info *csum_info;
77bf5487 365 struct ndis_tcp_lso_info *lso_info;
08cd04bf
KS
366 int hdr_offset;
367 u32 net_trans_info;
307f0995 368 u32 hash;
e88f7e07 369 u32 skb_length;
b08cc791 370 struct hv_page_buffer page_buf[MAX_PAGE_BUFFER_COUNT];
a9f2e2d6 371 struct hv_page_buffer *pb = page_buf;
7eafd9b4 372 struct netvsc_stats *tx_stats = this_cpu_ptr(net_device_ctx->tx_stats);
fceaf24a 373
54a7357f
KS
374 /* We will atmost need two pages to describe the rndis
375 * header. We can only transmit MAX_PAGE_BUFFER_COUNT number
e88f7e07
VK
376 * of pages in a single packet. If skb is scattered around
377 * more pages we try linearizing it.
54a7357f 378 */
e88f7e07
VK
379
380check_size:
381 skb_length = skb->len;
8a00251a 382 num_data_pgs = netvsc_get_slots(skb) + 2;
e88f7e07
VK
383 if (num_data_pgs > MAX_PAGE_BUFFER_COUNT && linear) {
384 net_alert_ratelimited("packet too big: %u pages (%u bytes)\n",
385 num_data_pgs, skb->len);
981a1bd8
VK
386 ret = -EFAULT;
387 goto drop;
e88f7e07
VK
388 } else if (num_data_pgs > MAX_PAGE_BUFFER_COUNT) {
389 if (skb_linearize(skb)) {
390 net_alert_ratelimited("failed to linearize skb\n");
391 ret = -ENOMEM;
392 goto drop;
393 }
394 linear = true;
395 goto check_size;
54a7357f 396 }
fceaf24a 397
c0eb4540
KS
398 /*
399 * Place the rndis header in the skb head room and
400 * the skb->cb will be used for hv_netvsc_packet
401 * structure.
402 */
403 ret = skb_cow_head(skb, RNDIS_AND_PPI_SIZE);
b56fc3c5
KS
404 if (ret) {
405 netdev_err(net, "unable to alloc hv_netvsc_packet\n");
406 ret = -ENOMEM;
407 goto drop;
fceaf24a 408 }
c0eb4540
KS
409 /* Use the skb control buffer for building up the packet */
410 BUILD_BUG_ON(sizeof(struct hv_netvsc_packet) >
411 FIELD_SIZEOF(struct sk_buff, cb));
412 packet = (struct hv_netvsc_packet *)skb->cb;
fceaf24a 413
1f5f3a75 414
5b54dac8
HZ
415 packet->q_idx = skb_get_queue_mapping(skb);
416
4d447c9a 417 packet->total_data_buflen = skb->len;
fceaf24a 418
c0eb4540 419 rndis_msg = (struct rndis_message *)skb->head;
b08cc791 420
24476760 421 memset(rndis_msg, 0, RNDIS_AND_PPI_SIZE);
fceaf24a 422
760d1e36 423 isvlan = skb->vlan_tci & VLAN_TAG_PRESENT;
8a00251a
KS
424
425 /* Add the rndis header */
8a00251a
KS
426 rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET;
427 rndis_msg->msg_len = packet->total_data_buflen;
428 rndis_pkt = &rndis_msg->msg.pkt;
429 rndis_pkt->data_offset = sizeof(struct rndis_packet);
430 rndis_pkt->data_len = packet->total_data_buflen;
431 rndis_pkt->per_pkt_info_offset = sizeof(struct rndis_packet);
432
433 rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet);
434
307f0995
HZ
435 hash = skb_get_hash_raw(skb);
436 if (hash != 0 && net->real_num_tx_queues > 1) {
437 rndis_msg_size += NDIS_HASH_PPI_SIZE;
438 ppi = init_ppi_data(rndis_msg, NDIS_HASH_PPI_SIZE,
439 NBL_HASH_VALUE);
440 *(u32 *)((void *)ppi + ppi->ppi_offset) = hash;
441 }
442
8a00251a
KS
443 if (isvlan) {
444 struct ndis_pkt_8021q_info *vlan;
445
446 rndis_msg_size += NDIS_VLAN_PPI_SIZE;
447 ppi = init_ppi_data(rndis_msg, NDIS_VLAN_PPI_SIZE,
448 IEEE_8021Q_INFO);
449 vlan = (struct ndis_pkt_8021q_info *)((void *)ppi +
450 ppi->ppi_offset);
760d1e36
KS
451 vlan->vlanid = skb->vlan_tci & VLAN_VID_MASK;
452 vlan->pri = (skb->vlan_tci & VLAN_PRIO_MASK) >>
8a00251a
KS
453 VLAN_PRIO_SHIFT;
454 }
455
08cd04bf
KS
456 net_trans_info = get_net_transport_info(skb, &hdr_offset);
457 if (net_trans_info == TRANSPORT_INFO_NOT_IP)
458 goto do_send;
459
460 /*
461 * Setup the sendside checksum offload only if this is not a
462 * GSO packet.
463 */
464 if (skb_is_gso(skb))
77bf5487 465 goto do_lso;
08cd04bf 466
22041fb0
KS
467 if ((skb->ip_summed == CHECKSUM_NONE) ||
468 (skb->ip_summed == CHECKSUM_UNNECESSARY))
469 goto do_send;
470
08cd04bf
KS
471 rndis_msg_size += NDIS_CSUM_PPI_SIZE;
472 ppi = init_ppi_data(rndis_msg, NDIS_CSUM_PPI_SIZE,
473 TCPIP_CHKSUM_PKTINFO);
474
475 csum_info = (struct ndis_tcp_ip_checksum_info *)((void *)ppi +
476 ppi->ppi_offset);
477
478 if (net_trans_info & (INFO_IPV4 << 16))
479 csum_info->transmit.is_ipv4 = 1;
480 else
481 csum_info->transmit.is_ipv6 = 1;
482
483 if (net_trans_info & INFO_TCP) {
484 csum_info->transmit.tcp_checksum = 1;
485 csum_info->transmit.tcp_header_offset = hdr_offset;
486 } else if (net_trans_info & INFO_UDP) {
af9893a3
KS
487 /* UDP checksum offload is not supported on ws2008r2.
488 * Furthermore, on ws2012 and ws2012r2, there are some
489 * issues with udp checksum offload from Linux guests.
490 * (these are host issues).
491 * For now compute the checksum here.
492 */
493 struct udphdr *uh;
494 u16 udp_len;
495
496 ret = skb_cow_head(skb, 0);
497 if (ret)
498 goto drop;
499
500 uh = udp_hdr(skb);
501 udp_len = ntohs(uh->len);
502 uh->check = 0;
503 uh->check = csum_tcpudp_magic(ip_hdr(skb)->saddr,
504 ip_hdr(skb)->daddr,
505 udp_len, IPPROTO_UDP,
506 csum_partial(uh, udp_len, 0));
507 if (uh->check == 0)
508 uh->check = CSUM_MANGLED_0;
509
510 csum_info->transmit.udp_checksum = 0;
08cd04bf 511 }
77bf5487
KS
512 goto do_send;
513
514do_lso:
515 rndis_msg_size += NDIS_LSO_PPI_SIZE;
516 ppi = init_ppi_data(rndis_msg, NDIS_LSO_PPI_SIZE,
517 TCP_LARGESEND_PKTINFO);
518
519 lso_info = (struct ndis_tcp_lso_info *)((void *)ppi +
520 ppi->ppi_offset);
521
522 lso_info->lso_v2_transmit.type = NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
523 if (net_trans_info & (INFO_IPV4 << 16)) {
524 lso_info->lso_v2_transmit.ip_version =
525 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
526 ip_hdr(skb)->tot_len = 0;
527 ip_hdr(skb)->check = 0;
528 tcp_hdr(skb)->check =
529 ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
530 ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
531 } else {
532 lso_info->lso_v2_transmit.ip_version =
533 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
534 ipv6_hdr(skb)->payload_len = 0;
535 tcp_hdr(skb)->check =
536 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
537 &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
538 }
539 lso_info->lso_v2_transmit.tcp_header_offset = hdr_offset;
540 lso_info->lso_v2_transmit.mss = skb_shinfo(skb)->gso_size;
08cd04bf
KS
541
542do_send:
8a00251a
KS
543 /* Start filling in the page buffers with the rndis hdr */
544 rndis_msg->msg_len += rndis_msg_size;
942396b0 545 packet->total_data_buflen = rndis_msg->msg_len;
8a00251a 546 packet->page_buf_cnt = init_page_array(rndis_msg, rndis_msg_size,
a9f2e2d6 547 skb, packet, &pb);
8a00251a 548
76d13b56 549 /* timestamp packet in software */
550 skb_tx_timestamp(skb);
3a3d9a0a
KS
551 ret = netvsc_send(net_device_ctx->device_ctx, packet,
552 rndis_msg, &pb, skb);
8a00251a 553
af9893a3 554drop:
02fafbc6 555 if (ret == 0) {
4b02b58b 556 u64_stats_update_begin(&tx_stats->syncp);
7eafd9b4 557 tx_stats->packets++;
558 tx_stats->bytes += skb_length;
4b02b58b 559 u64_stats_update_end(&tx_stats->syncp);
b220f5f9 560 } else {
33be96e4
HZ
561 if (ret != -EAGAIN) {
562 dev_kfree_skb_any(skb);
563 net->stats.tx_dropped++;
564 }
fceaf24a
HJ
565 }
566
33be96e4 567 return (ret == -EAGAIN) ? NETDEV_TX_BUSY : NETDEV_TX_OK;
fceaf24a
HJ
568}
569
3e189519 570/*
02fafbc6
GKH
571 * netvsc_linkstatus_callback - Link up/down notification
572 */
90ef117a 573void netvsc_linkstatus_callback(struct hv_device *device_obj,
3a494e71 574 struct rndis_message *resp)
fceaf24a 575{
3a494e71 576 struct rndis_indicate_status *indicate = &resp->msg.indicate_status;
2ddd5e5f 577 struct net_device *net;
c996edcf 578 struct net_device_context *ndev_ctx;
27a70af3
VK
579 struct netvsc_reconfig *event;
580 unsigned long flags;
891de74d 581
7f5d5af0
HZ
582 net = hv_get_drvdata(device_obj);
583
584 if (!net)
585 return;
586
587 ndev_ctx = netdev_priv(net);
588
589 /* Update the physical link speed when changing to another vSwitch */
590 if (indicate->status == RNDIS_STATUS_LINK_SPEED_CHANGE) {
591 u32 speed;
592
593 speed = *(u32 *)((void *)indicate + indicate->
594 status_buf_offset) / 10000;
595 ndev_ctx->speed = speed;
596 return;
597 }
598
599 /* Handle these link change statuses below */
27a70af3
VK
600 if (indicate->status != RNDIS_STATUS_NETWORK_CHANGE &&
601 indicate->status != RNDIS_STATUS_MEDIA_CONNECT &&
602 indicate->status != RNDIS_STATUS_MEDIA_DISCONNECT)
3a494e71 603 return;
891de74d 604
7f5d5af0 605 if (net->reg_state != NETREG_REGISTERED)
fceaf24a 606 return;
fceaf24a 607
27a70af3
VK
608 event = kzalloc(sizeof(*event), GFP_ATOMIC);
609 if (!event)
610 return;
611 event->event = indicate->status;
612
613 spin_lock_irqsave(&ndev_ctx->lock, flags);
614 list_add_tail(&event->list, &ndev_ctx->reconfig_events);
615 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
616
617 schedule_delayed_work(&ndev_ctx->dwork, 0);
fceaf24a
HJ
618}
619
84bf9cef
KS
620
621static struct sk_buff *netvsc_alloc_recv_skb(struct net_device *net,
e3d605ed 622 struct hv_netvsc_packet *packet,
25b85ee8 623 struct ndis_tcp_ip_checksum_info *csum_info,
84bf9cef 624 void *data, u16 vlan_tci)
fceaf24a 625{
fceaf24a 626 struct sk_buff *skb;
fceaf24a 627
72a2f5bd 628 skb = netdev_alloc_skb_ip_align(net, packet->total_data_buflen);
84bf9cef
KS
629 if (!skb)
630 return skb;
fceaf24a 631
02fafbc6
GKH
632 /*
633 * Copy to skb. This copy is needed here since the memory pointed by
634 * hv_netvsc_packet cannot be deallocated
635 */
84bf9cef
KS
636 memcpy(skb_put(skb, packet->total_data_buflen), data,
637 packet->total_data_buflen);
fceaf24a
HJ
638
639 skb->protocol = eth_type_trans(skb, net);
e3d605ed
KS
640 if (csum_info) {
641 /* We only look at the IP checksum here.
642 * Should we be dropping the packet if checksum
643 * failed? How do we deal with other checksums - TCP/UDP?
644 */
645 if (csum_info->receive.ip_checksum_succeeded)
646 skb->ip_summed = CHECKSUM_UNNECESSARY;
647 else
648 skb->ip_summed = CHECKSUM_NONE;
649 }
650
760d1e36 651 if (vlan_tci & VLAN_TAG_PRESENT)
93725cbd 652 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
760d1e36 653 vlan_tci);
fceaf24a 654
84bf9cef
KS
655 return skb;
656}
657
658/*
659 * netvsc_recv_callback - Callback when we receive a packet from the
660 * "wire" on the specified device.
661 */
662int netvsc_recv_callback(struct hv_device *device_obj,
663 struct hv_netvsc_packet *packet,
664 void **data,
665 struct ndis_tcp_ip_checksum_info *csum_info,
666 struct vmbus_channel *channel,
667 u16 vlan_tci)
668{
3d541ac5
VK
669 struct net_device *net = hv_get_drvdata(device_obj);
670 struct net_device_context *net_device_ctx = netdev_priv(net);
84bf9cef
KS
671 struct sk_buff *skb;
672 struct sk_buff *vf_skb;
673 struct netvsc_stats *rx_stats;
84bf9cef
KS
674 u32 bytes_recvd = packet->total_data_buflen;
675 int ret = 0;
676
84bf9cef
KS
677 if (!net || net->reg_state != NETREG_REGISTERED)
678 return NVSP_STAT_FAIL;
679
f9a7da91
VK
680 if (READ_ONCE(net_device_ctx->vf_inject)) {
681 atomic_inc(&net_device_ctx->vf_use_cnt);
682 if (!READ_ONCE(net_device_ctx->vf_inject)) {
84bf9cef
KS
683 /*
684 * We raced; just move on.
685 */
f9a7da91 686 atomic_dec(&net_device_ctx->vf_use_cnt);
84bf9cef
KS
687 goto vf_injection_done;
688 }
689
690 /*
691 * Inject this packet into the VF inerface.
692 * On Hyper-V, multicast and brodcast packets
693 * are only delivered on the synthetic interface
694 * (after subjecting these to policy filters on
695 * the host). Deliver these via the VF interface
696 * in the guest.
697 */
f9a7da91
VK
698 vf_skb = netvsc_alloc_recv_skb(net_device_ctx->vf_netdev,
699 packet, csum_info, *data,
700 vlan_tci);
84bf9cef 701 if (vf_skb != NULL) {
f9a7da91
VK
702 ++net_device_ctx->vf_netdev->stats.rx_packets;
703 net_device_ctx->vf_netdev->stats.rx_bytes +=
704 bytes_recvd;
84bf9cef
KS
705 netif_receive_skb(vf_skb);
706 } else {
707 ++net->stats.rx_dropped;
708 ret = NVSP_STAT_FAIL;
709 }
f9a7da91 710 atomic_dec(&net_device_ctx->vf_use_cnt);
84bf9cef
KS
711 return ret;
712 }
713
714vf_injection_done:
84bf9cef
KS
715 rx_stats = this_cpu_ptr(net_device_ctx->rx_stats);
716
717 /* Allocate a skb - TODO direct I/O to pages? */
718 skb = netvsc_alloc_recv_skb(net, packet, csum_info, *data, vlan_tci);
719 if (unlikely(!skb)) {
720 ++net->stats.rx_dropped;
721 return NVSP_STAT_FAIL;
722 }
25b85ee8 723 skb_record_rx_queue(skb, channel->
e565e803 724 offermsg.offer.sub_channel_index);
5b54dac8 725
4b02b58b 726 u64_stats_update_begin(&rx_stats->syncp);
7eafd9b4 727 rx_stats->packets++;
728 rx_stats->bytes += packet->total_data_buflen;
4b02b58b 729 u64_stats_update_end(&rx_stats->syncp);
9495c282 730
02fafbc6
GKH
731 /*
732 * Pass the skb back up. Network stack will deallocate the skb when it
9495c282
SH
733 * is done.
734 * TODO - use NAPI?
02fafbc6 735 */
9495c282 736 netif_rx(skb);
fceaf24a 737
fceaf24a
HJ
738 return 0;
739}
740
f82f4ad7
SH
741static void netvsc_get_drvinfo(struct net_device *net,
742 struct ethtool_drvinfo *info)
743{
7826d43f 744 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
7826d43f 745 strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
f82f4ad7
SH
746}
747
59995370
AS
748static void netvsc_get_channels(struct net_device *net,
749 struct ethtool_channels *channel)
750{
751 struct net_device_context *net_device_ctx = netdev_priv(net);
3d541ac5 752 struct netvsc_device *nvdev = net_device_ctx->nvdev;
59995370
AS
753
754 if (nvdev) {
755 channel->max_combined = nvdev->max_chn;
756 channel->combined_count = nvdev->num_chn;
757 }
758}
759
b5960e6e
AS
760static int netvsc_set_channels(struct net_device *net,
761 struct ethtool_channels *channels)
762{
763 struct net_device_context *net_device_ctx = netdev_priv(net);
764 struct hv_device *dev = net_device_ctx->device_ctx;
3d541ac5 765 struct netvsc_device *nvdev = net_device_ctx->nvdev;
b5960e6e 766 struct netvsc_device_info device_info;
954591b9
AS
767 u32 num_chn;
768 u32 max_chn;
b5960e6e
AS
769 int ret = 0;
770 bool recovering = false;
771
6da7225f 772 if (net_device_ctx->start_remove || !nvdev || nvdev->destroy)
b5960e6e
AS
773 return -ENODEV;
774
954591b9
AS
775 num_chn = nvdev->num_chn;
776 max_chn = min_t(u32, nvdev->max_chn, num_online_cpus());
777
b5960e6e
AS
778 if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5) {
779 pr_info("vRSS unsupported before NVSP Version 5\n");
780 return -EINVAL;
781 }
782
783 /* We do not support rx, tx, or other */
784 if (!channels ||
785 channels->rx_count ||
786 channels->tx_count ||
787 channels->other_count ||
788 (channels->combined_count < 1))
789 return -EINVAL;
790
791 if (channels->combined_count > max_chn) {
792 pr_info("combined channels too high, using %d\n", max_chn);
793 channels->combined_count = max_chn;
794 }
795
796 ret = netvsc_close(net);
797 if (ret)
798 goto out;
799
800 do_set:
f580aec4 801 net_device_ctx->start_remove = true;
b5960e6e
AS
802 rndis_filter_device_remove(dev);
803
804 nvdev->num_chn = channels->combined_count;
805
b5960e6e
AS
806 memset(&device_info, 0, sizeof(device_info));
807 device_info.num_chn = nvdev->num_chn; /* passed to RNDIS */
808 device_info.ring_size = ring_size;
809 device_info.max_num_vrss_chns = max_num_vrss_chns;
810
811 ret = rndis_filter_device_add(dev, &device_info);
812 if (ret) {
813 if (recovering) {
814 netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
815 return ret;
816 }
817 goto recover;
818 }
819
3d541ac5 820 nvdev = net_device_ctx->nvdev;
b5960e6e
AS
821
822 ret = netif_set_real_num_tx_queues(net, nvdev->num_chn);
823 if (ret) {
824 if (recovering) {
825 netdev_err(net, "could not set tx queue count (ret %d)\n", ret);
826 return ret;
827 }
828 goto recover;
829 }
830
831 ret = netif_set_real_num_rx_queues(net, nvdev->num_chn);
832 if (ret) {
833 if (recovering) {
834 netdev_err(net, "could not set rx queue count (ret %d)\n", ret);
835 return ret;
836 }
837 goto recover;
838 }
839
840 out:
841 netvsc_open(net);
f580aec4 842 net_device_ctx->start_remove = false;
1bdcec8a
VK
843 /* We may have missed link change notifications */
844 schedule_delayed_work(&net_device_ctx->dwork, 0);
b5960e6e
AS
845
846 return ret;
847
848 recover:
849 /* If the above failed, we attempt to recover through the same
850 * process but with the original number of channels.
851 */
852 netdev_err(net, "could not set channels, recovering\n");
853 recovering = true;
854 channels->combined_count = num_chn;
855 goto do_set;
856}
857
49eb9389 858static bool netvsc_validate_ethtool_ss_cmd(const struct ethtool_cmd *cmd)
859{
860 struct ethtool_cmd diff1 = *cmd;
861 struct ethtool_cmd diff2 = {};
862
863 ethtool_cmd_speed_set(&diff1, 0);
864 diff1.duplex = 0;
865 /* advertising and cmd are usually set */
866 diff1.advertising = 0;
867 diff1.cmd = 0;
868 /* We set port to PORT_OTHER */
869 diff2.port = PORT_OTHER;
870
871 return !memcmp(&diff1, &diff2, sizeof(diff1));
872}
873
874static void netvsc_init_settings(struct net_device *dev)
875{
876 struct net_device_context *ndc = netdev_priv(dev);
877
878 ndc->speed = SPEED_UNKNOWN;
879 ndc->duplex = DUPLEX_UNKNOWN;
880}
881
882static int netvsc_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
883{
884 struct net_device_context *ndc = netdev_priv(dev);
885
886 ethtool_cmd_speed_set(cmd, ndc->speed);
887 cmd->duplex = ndc->duplex;
888 cmd->port = PORT_OTHER;
889
890 return 0;
891}
892
893static int netvsc_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
894{
895 struct net_device_context *ndc = netdev_priv(dev);
896 u32 speed;
897
898 speed = ethtool_cmd_speed(cmd);
899 if (!ethtool_validate_speed(speed) ||
900 !ethtool_validate_duplex(cmd->duplex) ||
901 !netvsc_validate_ethtool_ss_cmd(cmd))
902 return -EINVAL;
903
904 ndc->speed = speed;
905 ndc->duplex = cmd->duplex;
906
907 return 0;
908}
909
4d447c9a
HZ
910static int netvsc_change_mtu(struct net_device *ndev, int mtu)
911{
912 struct net_device_context *ndevctx = netdev_priv(ndev);
3d541ac5
VK
913 struct netvsc_device *nvdev = ndevctx->nvdev;
914 struct hv_device *hdev = ndevctx->device_ctx;
4d447c9a
HZ
915 struct netvsc_device_info device_info;
916 int limit = ETH_DATA_LEN;
d212b463 917 u32 num_chn;
2de8530b 918 int ret = 0;
4d447c9a 919
6da7225f 920 if (ndevctx->start_remove || !nvdev || nvdev->destroy)
4d447c9a
HZ
921 return -ENODEV;
922
a1eabb01 923 if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
4d3c9d37 924 limit = NETVSC_MTU - ETH_HLEN;
4d447c9a 925
f9cbce34 926 if (mtu < NETVSC_MTU_MIN || mtu > limit)
4d447c9a
HZ
927 return -EINVAL;
928
2de8530b
HZ
929 ret = netvsc_close(ndev);
930 if (ret)
931 goto out;
932
d212b463
HZ
933 num_chn = nvdev->num_chn;
934
f580aec4 935 ndevctx->start_remove = true;
4d447c9a
HZ
936 rndis_filter_device_remove(hdev);
937
938 ndev->mtu = mtu;
939
8ebdcc52 940 memset(&device_info, 0, sizeof(device_info));
4d447c9a 941 device_info.ring_size = ring_size;
d212b463 942 device_info.num_chn = num_chn;
e01ec219 943 device_info.max_num_vrss_chns = max_num_vrss_chns;
4d447c9a 944 rndis_filter_device_add(hdev, &device_info);
4d447c9a 945
2de8530b
HZ
946out:
947 netvsc_open(ndev);
f580aec4 948 ndevctx->start_remove = false;
2de8530b 949
1bdcec8a
VK
950 /* We may have missed link change notifications */
951 schedule_delayed_work(&ndevctx->dwork, 0);
952
2de8530b 953 return ret;
4d447c9a
HZ
954}
955
7eafd9b4 956static struct rtnl_link_stats64 *netvsc_get_stats64(struct net_device *net,
957 struct rtnl_link_stats64 *t)
958{
959 struct net_device_context *ndev_ctx = netdev_priv(net);
960 int cpu;
961
962 for_each_possible_cpu(cpu) {
963 struct netvsc_stats *tx_stats = per_cpu_ptr(ndev_ctx->tx_stats,
964 cpu);
965 struct netvsc_stats *rx_stats = per_cpu_ptr(ndev_ctx->rx_stats,
966 cpu);
967 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
968 unsigned int start;
969
970 do {
4b02b58b 971 start = u64_stats_fetch_begin_irq(&tx_stats->syncp);
7eafd9b4 972 tx_packets = tx_stats->packets;
973 tx_bytes = tx_stats->bytes;
4b02b58b 974 } while (u64_stats_fetch_retry_irq(&tx_stats->syncp, start));
7eafd9b4 975
976 do {
4b02b58b 977 start = u64_stats_fetch_begin_irq(&rx_stats->syncp);
7eafd9b4 978 rx_packets = rx_stats->packets;
979 rx_bytes = rx_stats->bytes;
4b02b58b 980 } while (u64_stats_fetch_retry_irq(&rx_stats->syncp, start));
7eafd9b4 981
982 t->tx_bytes += tx_bytes;
983 t->tx_packets += tx_packets;
984 t->rx_bytes += rx_bytes;
985 t->rx_packets += rx_packets;
986 }
987
988 t->tx_dropped = net->stats.tx_dropped;
989 t->tx_errors = net->stats.tx_dropped;
990
991 t->rx_dropped = net->stats.rx_dropped;
992 t->rx_errors = net->stats.rx_errors;
993
994 return t;
995}
1ce09e89
HZ
996
997static int netvsc_set_mac_addr(struct net_device *ndev, void *p)
998{
1ce09e89 999 struct sockaddr *addr = p;
9a4c831e 1000 char save_adr[ETH_ALEN];
1ce09e89
HZ
1001 unsigned char save_aatype;
1002 int err;
1003
1004 memcpy(save_adr, ndev->dev_addr, ETH_ALEN);
1005 save_aatype = ndev->addr_assign_type;
1006
1007 err = eth_mac_addr(ndev, p);
1008 if (err != 0)
1009 return err;
1010
e834da9a 1011 err = rndis_filter_set_device_mac(ndev, addr->sa_data);
1ce09e89
HZ
1012 if (err != 0) {
1013 /* roll back to saved MAC */
1014 memcpy(ndev->dev_addr, save_adr, ETH_ALEN);
1015 ndev->addr_assign_type = save_aatype;
1016 }
1017
1018 return err;
1019}
1020
316158fe
RW
1021#ifdef CONFIG_NET_POLL_CONTROLLER
1022static void netvsc_poll_controller(struct net_device *net)
1023{
1024 /* As netvsc_start_xmit() works synchronous we don't have to
1025 * trigger anything here.
1026 */
1027}
1028#endif
1ce09e89 1029
f82f4ad7
SH
1030static const struct ethtool_ops ethtool_ops = {
1031 .get_drvinfo = netvsc_get_drvinfo,
f82f4ad7 1032 .get_link = ethtool_op_get_link,
59995370 1033 .get_channels = netvsc_get_channels,
b5960e6e 1034 .set_channels = netvsc_set_channels,
76d13b56 1035 .get_ts_info = ethtool_op_get_ts_info,
49eb9389 1036 .get_settings = netvsc_get_settings,
1037 .set_settings = netvsc_set_settings,
f82f4ad7
SH
1038};
1039
df2fff28
GKH
1040static const struct net_device_ops device_ops = {
1041 .ndo_open = netvsc_open,
1042 .ndo_stop = netvsc_close,
1043 .ndo_start_xmit = netvsc_start_xmit,
afc4b13d 1044 .ndo_set_rx_mode = netvsc_set_multicast_list,
4d447c9a 1045 .ndo_change_mtu = netvsc_change_mtu,
b681b588 1046 .ndo_validate_addr = eth_validate_addr,
1ce09e89 1047 .ndo_set_mac_address = netvsc_set_mac_addr,
5b54dac8 1048 .ndo_select_queue = netvsc_select_queue,
7eafd9b4 1049 .ndo_get_stats64 = netvsc_get_stats64,
316158fe
RW
1050#ifdef CONFIG_NET_POLL_CONTROLLER
1051 .ndo_poll_controller = netvsc_poll_controller,
1052#endif
df2fff28
GKH
1053};
1054
c996edcf 1055/*
27a70af3
VK
1056 * Handle link status changes. For RNDIS_STATUS_NETWORK_CHANGE emulate link
1057 * down/up sequence. In case of RNDIS_STATUS_MEDIA_CONNECT when carrier is
1058 * present send GARP packet to network peers with netif_notify_peers().
c996edcf 1059 */
891de74d 1060static void netvsc_link_change(struct work_struct *w)
c996edcf 1061{
0a1275ca
VK
1062 struct net_device_context *ndev_ctx =
1063 container_of(w, struct net_device_context, dwork.work);
1064 struct hv_device *device_obj = ndev_ctx->device_ctx;
1065 struct net_device *net = hv_get_drvdata(device_obj);
2ddd5e5f 1066 struct netvsc_device *net_device;
891de74d 1067 struct rndis_device *rdev;
27a70af3
VK
1068 struct netvsc_reconfig *event = NULL;
1069 bool notify = false, reschedule = false;
1070 unsigned long flags, next_reconfig, delay;
c996edcf 1071
1bdcec8a
VK
1072 rtnl_lock();
1073 if (ndev_ctx->start_remove)
1074 goto out_unlock;
1075
3d541ac5 1076 net_device = ndev_ctx->nvdev;
891de74d 1077 rdev = net_device->extension;
891de74d 1078
27a70af3
VK
1079 next_reconfig = ndev_ctx->last_reconfig + LINKCHANGE_INT;
1080 if (time_is_after_jiffies(next_reconfig)) {
1081 /* link_watch only sends one notification with current state
1082 * per second, avoid doing reconfig more frequently. Handle
1083 * wrap around.
1084 */
1085 delay = next_reconfig - jiffies;
1086 delay = delay < LINKCHANGE_INT ? delay : LINKCHANGE_INT;
1087 schedule_delayed_work(&ndev_ctx->dwork, delay);
1bdcec8a 1088 goto out_unlock;
27a70af3
VK
1089 }
1090 ndev_ctx->last_reconfig = jiffies;
1091
1092 spin_lock_irqsave(&ndev_ctx->lock, flags);
1093 if (!list_empty(&ndev_ctx->reconfig_events)) {
1094 event = list_first_entry(&ndev_ctx->reconfig_events,
1095 struct netvsc_reconfig, list);
1096 list_del(&event->list);
1097 reschedule = !list_empty(&ndev_ctx->reconfig_events);
1098 }
1099 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1100
1101 if (!event)
1bdcec8a 1102 goto out_unlock;
27a70af3
VK
1103
1104 switch (event->event) {
1105 /* Only the following events are possible due to the check in
1106 * netvsc_linkstatus_callback()
1107 */
1108 case RNDIS_STATUS_MEDIA_CONNECT:
1109 if (rdev->link_state) {
1110 rdev->link_state = false;
1111 netif_carrier_on(net);
1112 netif_tx_wake_all_queues(net);
1113 } else {
1114 notify = true;
1115 }
1116 kfree(event);
1117 break;
1118 case RNDIS_STATUS_MEDIA_DISCONNECT:
1119 if (!rdev->link_state) {
1120 rdev->link_state = true;
1121 netif_carrier_off(net);
1122 netif_tx_stop_all_queues(net);
1123 }
1124 kfree(event);
1125 break;
1126 case RNDIS_STATUS_NETWORK_CHANGE:
1127 /* Only makes sense if carrier is present */
1128 if (!rdev->link_state) {
1129 rdev->link_state = true;
1130 netif_carrier_off(net);
1131 netif_tx_stop_all_queues(net);
1132 event->event = RNDIS_STATUS_MEDIA_CONNECT;
1133 spin_lock_irqsave(&ndev_ctx->lock, flags);
15cfd407 1134 list_add(&event->list, &ndev_ctx->reconfig_events);
27a70af3
VK
1135 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1136 reschedule = true;
3a494e71 1137 }
27a70af3 1138 break;
891de74d
HZ
1139 }
1140
1141 rtnl_unlock();
1142
1143 if (notify)
1144 netdev_notify_peers(net);
27a70af3
VK
1145
1146 /* link_watch only sends one notification with current state per
1147 * second, handle next reconfig event in 2 seconds.
1148 */
1149 if (reschedule)
1150 schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
1bdcec8a
VK
1151
1152 return;
1153
1154out_unlock:
1155 rtnl_unlock();
c996edcf
HZ
1156}
1157
7eafd9b4 1158static void netvsc_free_netdev(struct net_device *netdev)
1159{
1160 struct net_device_context *net_device_ctx = netdev_priv(netdev);
1161
1162 free_percpu(net_device_ctx->tx_stats);
1163 free_percpu(net_device_ctx->rx_stats);
1164 free_netdev(netdev);
1165}
c996edcf 1166
0a1275ca 1167static struct net_device *get_netvsc_net_device(char *mac)
84bf9cef 1168{
0a1275ca 1169 struct net_device *dev, *found = NULL;
84bf9cef
KS
1170 int rtnl_locked;
1171
1172 rtnl_locked = rtnl_trylock();
1173
1174 for_each_netdev(&init_net, dev) {
1175 if (memcmp(dev->dev_addr, mac, ETH_ALEN) == 0) {
1176 if (dev->netdev_ops != &device_ops)
1177 continue;
0a1275ca 1178 found = dev;
84bf9cef
KS
1179 break;
1180 }
1181 }
1182 if (rtnl_locked)
1183 rtnl_unlock();
1184
0a1275ca 1185 return found;
84bf9cef
KS
1186}
1187
1188static int netvsc_register_vf(struct net_device *vf_netdev)
1189{
0a1275ca
VK
1190 struct net_device *ndev;
1191 struct net_device_context *net_device_ctx;
84bf9cef
KS
1192 struct netvsc_device *netvsc_dev;
1193 const struct ethtool_ops *eth_ops = vf_netdev->ethtool_ops;
1194
1195 if (eth_ops == NULL || eth_ops == &ethtool_ops)
1196 return NOTIFY_DONE;
1197
1198 /*
1199 * We will use the MAC address to locate the synthetic interface to
1200 * associate with the VF interface. If we don't find a matching
1201 * synthetic interface, move on.
1202 */
0a1275ca
VK
1203 ndev = get_netvsc_net_device(vf_netdev->dev_addr);
1204 if (!ndev)
1205 return NOTIFY_DONE;
1206
1207 net_device_ctx = netdev_priv(ndev);
1208 netvsc_dev = net_device_ctx->nvdev;
0f20d795 1209 if (!netvsc_dev || net_device_ctx->vf_netdev)
84bf9cef
KS
1210 return NOTIFY_DONE;
1211
0a1275ca 1212 netdev_info(ndev, "VF registering: %s\n", vf_netdev->name);
84bf9cef
KS
1213 /*
1214 * Take a reference on the module.
1215 */
1216 try_module_get(THIS_MODULE);
f9a7da91 1217 net_device_ctx->vf_netdev = vf_netdev;
84bf9cef
KS
1218 return NOTIFY_OK;
1219}
1220
57c1826b
VK
1221static void netvsc_inject_enable(struct net_device_context *net_device_ctx)
1222{
1223 net_device_ctx->vf_inject = true;
1224}
1225
1226static void netvsc_inject_disable(struct net_device_context *net_device_ctx)
1227{
1228 net_device_ctx->vf_inject = false;
1229
1230 /* Wait for currently active users to drain out. */
1231 while (atomic_read(&net_device_ctx->vf_use_cnt) != 0)
1232 udelay(50);
1233}
84bf9cef
KS
1234
1235static int netvsc_vf_up(struct net_device *vf_netdev)
1236{
0a1275ca 1237 struct net_device *ndev;
84bf9cef
KS
1238 struct netvsc_device *netvsc_dev;
1239 const struct ethtool_ops *eth_ops = vf_netdev->ethtool_ops;
1240 struct net_device_context *net_device_ctx;
1241
1242 if (eth_ops == &ethtool_ops)
1243 return NOTIFY_DONE;
1244
0a1275ca
VK
1245 ndev = get_netvsc_net_device(vf_netdev->dev_addr);
1246 if (!ndev)
1247 return NOTIFY_DONE;
1248
1249 net_device_ctx = netdev_priv(ndev);
1250 netvsc_dev = net_device_ctx->nvdev;
84bf9cef 1251
f9a7da91 1252 if (!netvsc_dev || !net_device_ctx->vf_netdev)
84bf9cef
KS
1253 return NOTIFY_DONE;
1254
0a1275ca 1255 netdev_info(ndev, "VF up: %s\n", vf_netdev->name);
57c1826b 1256 netvsc_inject_enable(net_device_ctx);
84bf9cef
KS
1257
1258 /*
1259 * Open the device before switching data path.
1260 */
2f5fa6c8 1261 rndis_filter_open(netvsc_dev);
84bf9cef
KS
1262
1263 /*
1264 * notify the host to switch the data path.
1265 */
0a1275ca
VK
1266 netvsc_switch_datapath(ndev, true);
1267 netdev_info(ndev, "Data path switched to VF: %s\n", vf_netdev->name);
84bf9cef 1268
0a1275ca 1269 netif_carrier_off(ndev);
84bf9cef 1270
d072218f
VK
1271 /* Now notify peers through VF device. */
1272 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, vf_netdev);
84bf9cef
KS
1273
1274 return NOTIFY_OK;
1275}
1276
1277
1278static int netvsc_vf_down(struct net_device *vf_netdev)
1279{
0a1275ca 1280 struct net_device *ndev;
84bf9cef
KS
1281 struct netvsc_device *netvsc_dev;
1282 struct net_device_context *net_device_ctx;
1283 const struct ethtool_ops *eth_ops = vf_netdev->ethtool_ops;
1284
1285 if (eth_ops == &ethtool_ops)
1286 return NOTIFY_DONE;
1287
0a1275ca
VK
1288 ndev = get_netvsc_net_device(vf_netdev->dev_addr);
1289 if (!ndev)
1290 return NOTIFY_DONE;
1291
1292 net_device_ctx = netdev_priv(ndev);
1293 netvsc_dev = net_device_ctx->nvdev;
84bf9cef 1294
f9a7da91 1295 if (!netvsc_dev || !net_device_ctx->vf_netdev)
84bf9cef
KS
1296 return NOTIFY_DONE;
1297
0a1275ca 1298 netdev_info(ndev, "VF down: %s\n", vf_netdev->name);
57c1826b 1299 netvsc_inject_disable(net_device_ctx);
0a1275ca
VK
1300 netvsc_switch_datapath(ndev, false);
1301 netdev_info(ndev, "Data path switched from VF: %s\n", vf_netdev->name);
2f5fa6c8 1302 rndis_filter_close(netvsc_dev);
0a1275ca 1303 netif_carrier_on(ndev);
d072218f
VK
1304
1305 /* Now notify peers through netvsc device. */
1306 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, ndev);
84bf9cef
KS
1307
1308 return NOTIFY_OK;
1309}
1310
1311
1312static int netvsc_unregister_vf(struct net_device *vf_netdev)
1313{
0a1275ca 1314 struct net_device *ndev;
84bf9cef
KS
1315 struct netvsc_device *netvsc_dev;
1316 const struct ethtool_ops *eth_ops = vf_netdev->ethtool_ops;
0a1275ca 1317 struct net_device_context *net_device_ctx;
84bf9cef
KS
1318
1319 if (eth_ops == &ethtool_ops)
1320 return NOTIFY_DONE;
1321
0a1275ca
VK
1322 ndev = get_netvsc_net_device(vf_netdev->dev_addr);
1323 if (!ndev)
1324 return NOTIFY_DONE;
1325
1326 net_device_ctx = netdev_priv(ndev);
1327 netvsc_dev = net_device_ctx->nvdev;
0f20d795 1328 if (!netvsc_dev || !net_device_ctx->vf_netdev)
84bf9cef 1329 return NOTIFY_DONE;
0a1275ca 1330 netdev_info(ndev, "VF unregistering: %s\n", vf_netdev->name);
57c1826b 1331 netvsc_inject_disable(net_device_ctx);
f9a7da91 1332 net_device_ctx->vf_netdev = NULL;
84bf9cef
KS
1333 module_put(THIS_MODULE);
1334 return NOTIFY_OK;
1335}
1336
84946899
S
1337static int netvsc_probe(struct hv_device *dev,
1338 const struct hv_vmbus_device_id *dev_id)
df2fff28 1339{
df2fff28
GKH
1340 struct net_device *net = NULL;
1341 struct net_device_context *net_device_ctx;
1342 struct netvsc_device_info device_info;
5b54dac8 1343 struct netvsc_device *nvdev;
df2fff28
GKH
1344 int ret;
1345
5b54dac8
HZ
1346 net = alloc_etherdev_mq(sizeof(struct net_device_context),
1347 num_online_cpus());
df2fff28 1348 if (!net)
51a805d0 1349 return -ENOMEM;
df2fff28 1350
1b07da51
HZ
1351 netif_carrier_off(net);
1352
b37879e6
HZ
1353 netvsc_init_settings(net);
1354
df2fff28 1355 net_device_ctx = netdev_priv(net);
9efd21e1 1356 net_device_ctx->device_ctx = dev;
3f300ff4
SX
1357 net_device_ctx->msg_enable = netif_msg_init(debug, default_msg);
1358 if (netif_msg_probe(net_device_ctx))
1359 netdev_dbg(net, "netvsc msg_enable: %d\n",
1360 net_device_ctx->msg_enable);
1361
7eafd9b4 1362 net_device_ctx->tx_stats = netdev_alloc_pcpu_stats(struct netvsc_stats);
1363 if (!net_device_ctx->tx_stats) {
1364 free_netdev(net);
1365 return -ENOMEM;
1366 }
1367 net_device_ctx->rx_stats = netdev_alloc_pcpu_stats(struct netvsc_stats);
1368 if (!net_device_ctx->rx_stats) {
1369 free_percpu(net_device_ctx->tx_stats);
1370 free_netdev(net);
1371 return -ENOMEM;
1372 }
1373
2ddd5e5f 1374 hv_set_drvdata(dev, net);
f580aec4
VK
1375
1376 net_device_ctx->start_remove = false;
1377
891de74d 1378 INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change);
792df872 1379 INIT_WORK(&net_device_ctx->work, do_set_multicast);
df2fff28 1380
27a70af3
VK
1381 spin_lock_init(&net_device_ctx->lock);
1382 INIT_LIST_HEAD(&net_device_ctx->reconfig_events);
1383
f9a7da91
VK
1384 atomic_set(&net_device_ctx->vf_use_cnt, 0);
1385 net_device_ctx->vf_netdev = NULL;
1386 net_device_ctx->vf_inject = false;
1387
df2fff28
GKH
1388 net->netdev_ops = &device_ops;
1389
a060679c 1390 net->hw_features = NETVSC_HW_FEATURES;
1391 net->features = NETVSC_HW_FEATURES | NETIF_F_HW_VLAN_CTAG_TX;
6048718d 1392
7ad24ea4 1393 net->ethtool_ops = &ethtool_ops;
9efd21e1 1394 SET_NETDEV_DEV(net, &dev->device);
df2fff28 1395
14a03cf8
VK
1396 /* We always need headroom for rndis header */
1397 net->needed_headroom = RNDIS_AND_PPI_SIZE;
1398
692e084e 1399 /* Notify the netvsc driver of the new device */
8ebdcc52 1400 memset(&device_info, 0, sizeof(device_info));
692e084e 1401 device_info.ring_size = ring_size;
e01ec219 1402 device_info.max_num_vrss_chns = max_num_vrss_chns;
692e084e
HZ
1403 ret = rndis_filter_device_add(dev, &device_info);
1404 if (ret != 0) {
1405 netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
7eafd9b4 1406 netvsc_free_netdev(net);
2ddd5e5f 1407 hv_set_drvdata(dev, NULL);
692e084e 1408 return ret;
df2fff28 1409 }
692e084e
HZ
1410 memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
1411
3d541ac5 1412 nvdev = net_device_ctx->nvdev;
5b54dac8
HZ
1413 netif_set_real_num_tx_queues(net, nvdev->num_chn);
1414 netif_set_real_num_rx_queues(net, nvdev->num_chn);
5b54dac8 1415
a68f9614
HZ
1416 ret = register_netdev(net);
1417 if (ret != 0) {
1418 pr_err("Unable to register netdev.\n");
1419 rndis_filter_device_remove(dev);
7eafd9b4 1420 netvsc_free_netdev(net);
a68f9614
HZ
1421 }
1422
df2fff28
GKH
1423 return ret;
1424}
1425
415b023a 1426static int netvsc_remove(struct hv_device *dev)
df2fff28 1427{
2ddd5e5f 1428 struct net_device *net;
122a5f64 1429 struct net_device_context *ndev_ctx;
2ddd5e5f
S
1430 struct netvsc_device *net_device;
1431
3d541ac5 1432 net = hv_get_drvdata(dev);
df2fff28 1433
df2fff28 1434 if (net == NULL) {
415b023a 1435 dev_err(&dev->device, "No net device to remove\n");
df2fff28
GKH
1436 return 0;
1437 }
1438
4d447c9a 1439
122a5f64 1440 ndev_ctx = netdev_priv(net);
3d541ac5
VK
1441 net_device = ndev_ctx->nvdev;
1442
6da7225f
VK
1443 /* Avoid racing with netvsc_change_mtu()/netvsc_set_channels()
1444 * removing the device.
1445 */
1446 rtnl_lock();
f580aec4 1447 ndev_ctx->start_remove = true;
6da7225f 1448 rtnl_unlock();
f580aec4 1449
122a5f64 1450 cancel_delayed_work_sync(&ndev_ctx->dwork);
792df872 1451 cancel_work_sync(&ndev_ctx->work);
122a5f64 1452
df2fff28 1453 /* Stop outbound asap */
0a282538 1454 netif_tx_disable(net);
df2fff28
GKH
1455
1456 unregister_netdev(net);
1457
1458 /*
1459 * Call to the vsc driver to let it know that the device is being
1460 * removed
1461 */
df06bcff 1462 rndis_filter_device_remove(dev);
df2fff28 1463
3d541ac5
VK
1464 hv_set_drvdata(dev, NULL);
1465
7eafd9b4 1466 netvsc_free_netdev(net);
df06bcff 1467 return 0;
df2fff28
GKH
1468}
1469
345c4cc3 1470static const struct hv_vmbus_device_id id_table[] = {
c45cf2d4 1471 /* Network guid */
8f505944 1472 { HV_NIC_GUID, },
c45cf2d4 1473 { },
345c4cc3
S
1474};
1475
1476MODULE_DEVICE_TABLE(vmbus, id_table);
1477
f1542a66 1478/* The one and only one */
fde0ef9b 1479static struct hv_driver netvsc_drv = {
d31b20fc 1480 .name = KBUILD_MODNAME,
345c4cc3 1481 .id_table = id_table,
fde0ef9b
S
1482 .probe = netvsc_probe,
1483 .remove = netvsc_remove,
d4890970 1484};
f1542a66 1485
84bf9cef
KS
1486
1487/*
1488 * On Hyper-V, every VF interface is matched with a corresponding
1489 * synthetic interface. The synthetic interface is presented first
1490 * to the guest. When the corresponding VF instance is registered,
1491 * we will take care of switching the data path.
1492 */
1493static int netvsc_netdev_event(struct notifier_block *this,
1494 unsigned long event, void *ptr)
1495{
1496 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
1497
0dbff144
VK
1498 /* Avoid Vlan dev with same MAC registering as VF */
1499 if (event_dev->priv_flags & IFF_802_1Q_VLAN)
1500 return NOTIFY_DONE;
1501
1502 /* Avoid Bonding master dev with same MAC registering as VF */
1503 if (event_dev->priv_flags & IFF_BONDING &&
1504 event_dev->flags & IFF_MASTER)
cb2911fe
HZ
1505 return NOTIFY_DONE;
1506
84bf9cef
KS
1507 switch (event) {
1508 case NETDEV_REGISTER:
1509 return netvsc_register_vf(event_dev);
1510 case NETDEV_UNREGISTER:
1511 return netvsc_unregister_vf(event_dev);
1512 case NETDEV_UP:
1513 return netvsc_vf_up(event_dev);
1514 case NETDEV_DOWN:
1515 return netvsc_vf_down(event_dev);
1516 default:
1517 return NOTIFY_DONE;
1518 }
1519}
1520
1521static struct notifier_block netvsc_netdev_notifier = {
1522 .notifier_call = netvsc_netdev_event,
1523};
1524
a9869c94 1525static void __exit netvsc_drv_exit(void)
fceaf24a 1526{
84bf9cef 1527 unregister_netdevice_notifier(&netvsc_netdev_notifier);
768fa219 1528 vmbus_driver_unregister(&netvsc_drv);
fceaf24a
HJ
1529}
1530
1fde28cf 1531static int __init netvsc_drv_init(void)
df2fff28 1532{
84bf9cef
KS
1533 int ret;
1534
fa85a6c2
HZ
1535 if (ring_size < RING_SIZE_MIN) {
1536 ring_size = RING_SIZE_MIN;
1537 pr_info("Increased ring_size to %d (min allowed)\n",
1538 ring_size);
1539 }
84bf9cef
KS
1540 ret = vmbus_driver_register(&netvsc_drv);
1541
1542 if (ret)
1543 return ret;
1544
1545 register_netdevice_notifier(&netvsc_netdev_notifier);
1546 return 0;
df2fff28
GKH
1547}
1548
26c14cc1 1549MODULE_LICENSE("GPL");
7880fc54 1550MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
fceaf24a 1551
1fde28cf 1552module_init(netvsc_drv_init);
a9869c94 1553module_exit(netvsc_drv_exit);