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