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