blob: 11755783c2f6057566d583fc0a5d89da42eb0d55 [file] [log] [blame]
Hank Janssenfceaf242009-07-13 15:34:54 -07001/*
Hank Janssenfceaf242009-07-13 15:34:54 -07002 * 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
Jeff Kirsheradf8d3f2013-12-06 06:28:47 -080014 * this program; if not, see <http://www.gnu.org/licenses/>.
Hank Janssenfceaf242009-07-13 15:34:54 -070015 *
16 * Authors:
Haiyang Zhangd0e94d12009-11-23 17:00:22 +000017 * Haiyang Zhang <haiyangz@microsoft.com>
Hank Janssenfceaf242009-07-13 15:34:54 -070018 * Hank Janssen <hjanssen@microsoft.com>
Hank Janssenfceaf242009-07-13 15:34:54 -070019 */
Hank Jansseneb335bc2011-03-29 13:58:48 -070020#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
Hank Janssenfceaf242009-07-13 15:34:54 -070022#include <linux/init.h>
K. Y. Srinivasan9079ce62011-06-16 13:16:37 -070023#include <linux/atomic.h>
Hank Janssenfceaf242009-07-13 15:34:54 -070024#include <linux/module.h>
25#include <linux/highmem.h>
26#include <linux/device.h>
Hank Janssenfceaf242009-07-13 15:34:54 -070027#include <linux/io.h>
Hank Janssenfceaf242009-07-13 15:34:54 -070028#include <linux/delay.h>
29#include <linux/netdevice.h>
30#include <linux/inetdevice.h>
31#include <linux/etherdevice.h>
32#include <linux/skbuff.h>
Haiyang Zhangc802db12013-05-28 06:15:56 +000033#include <linux/if_vlan.h>
Hank Janssenfceaf242009-07-13 15:34:54 -070034#include <linux/in.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Hank Janssenfceaf242009-07-13 15:34:54 -070036#include <net/arp.h>
37#include <net/route.h>
38#include <net/sock.h>
39#include <net/pkt_sched.h>
K. Y. Srinivasan3f335ea2011-05-12 19:34:15 -070040
K. Y. Srinivasan5ca72522011-05-12 19:34:37 -070041#include "hyperv_net.h"
Hank Janssenfceaf242009-07-13 15:34:54 -070042
Haiyang Zhangfa85a6c2012-07-25 08:08:41 +000043#define RING_SIZE_MIN 64
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +010044#define LINKCHANGE_INT (2 * HZ)
stephen hemmingera50af862016-12-06 13:43:54 -080045
Hank Janssen99c8da02010-10-12 10:45:23 -070046static int ring_size = 128;
Stephen Hemminger450d7a42010-05-04 09:58:53 -070047module_param(ring_size, int, S_IRUGO);
48MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)");
Hank Janssenfceaf242009-07-13 15:34:54 -070049
Simon Xiao3f300ff2015-04-28 01:05:17 -070050static 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
Haiyang Zhangd426b2e2011-11-30 07:19:08 -080059static void do_set_multicast(struct work_struct *w)
60{
Wenqi Ma792df872012-04-19 00:39:37 +000061 struct net_device_context *ndevctx =
62 container_of(w, struct net_device_context, work);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +020063 struct hv_device *device_obj = ndevctx->device_ctx;
64 struct net_device *ndev = hv_get_drvdata(device_obj);
65 struct netvsc_device *nvdev = ndevctx->nvdev;
Haiyang Zhangd426b2e2011-11-30 07:19:08 -080066 struct rndis_device *rdev;
67
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +020068 if (!nvdev)
Wenqi Ma792df872012-04-19 00:39:37 +000069 return;
Haiyang Zhangd426b2e2011-11-30 07:19:08 -080070
71 rdev = nvdev->extension;
72 if (rdev == NULL)
Wenqi Ma792df872012-04-19 00:39:37 +000073 return;
Haiyang Zhangd426b2e2011-11-30 07:19:08 -080074
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +020075 if (ndev->flags & IFF_PROMISC)
Haiyang Zhangd426b2e2011-11-30 07:19:08 -080076 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);
Haiyang Zhangd426b2e2011-11-30 07:19:08 -080083}
84
Greg Kroah-Hartman4e9bfef2009-07-15 12:45:20 -070085static void netvsc_set_multicast_list(struct net_device *net)
Hank Janssenfceaf242009-07-13 15:34:54 -070086{
Wenqi Ma792df872012-04-19 00:39:37 +000087 struct net_device_context *net_device_ctx = netdev_priv(net);
Haiyang Zhangd426b2e2011-11-30 07:19:08 -080088
Wenqi Ma792df872012-04-19 00:39:37 +000089 schedule_work(&net_device_ctx->work);
Hank Janssenfceaf242009-07-13 15:34:54 -070090}
91
Hank Janssenfceaf242009-07-13 15:34:54 -070092static int netvsc_open(struct net_device *net)
93{
Vitaly Kuznetsov2f5fa6c2016-06-03 17:51:00 +020094 struct netvsc_device *nvdev = net_device_to_netvsc_device(net);
Haiyang Zhang891de742014-02-12 16:54:27 -080095 struct rndis_device *rdev;
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -070096 int ret = 0;
Hank Janssenfceaf242009-07-13 15:34:54 -070097
Haiyang Zhang891de742014-02-12 16:54:27 -080098 netif_carrier_off(net);
99
Haiyang Zhangd515d0f2011-09-28 13:24:15 -0700100 /* Open up the device */
Vitaly Kuznetsov2f5fa6c2016-06-03 17:51:00 +0200101 ret = rndis_filter_open(nvdev);
Haiyang Zhangd515d0f2011-09-28 13:24:15 -0700102 if (ret != 0) {
103 netdev_err(net, "unable to open device (ret %d).\n", ret);
104 return ret;
Hank Janssenfceaf242009-07-13 15:34:54 -0700105 }
106
Haiyang Zhang2de85302015-07-13 13:09:16 -0700107 netif_tx_wake_all_queues(net);
Haiyang Zhangd515d0f2011-09-28 13:24:15 -0700108
Haiyang Zhang891de742014-02-12 16:54:27 -0800109 rdev = nvdev->extension;
110 if (!rdev->link_state)
111 netif_carrier_on(net);
112
Hank Janssenfceaf242009-07-13 15:34:54 -0700113 return ret;
114}
115
Hank Janssenfceaf242009-07-13 15:34:54 -0700116static int netvsc_close(struct net_device *net)
117{
Hank Janssenfceaf242009-07-13 15:34:54 -0700118 struct net_device_context *net_device_ctx = netdev_priv(net);
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200119 struct netvsc_device *nvdev = net_device_ctx->nvdev;
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700120 int ret;
Haiyang Zhang2de85302015-07-13 13:09:16 -0700121 u32 aread, awrite, i, msec = 10, retry = 0, retry_max = 20;
122 struct vmbus_channel *chn;
Hank Janssenfceaf242009-07-13 15:34:54 -0700123
Haiyang Zhang0a282532012-02-02 07:17:59 +0000124 netif_tx_disable(net);
Hank Janssenfceaf242009-07-13 15:34:54 -0700125
Wenqi Ma792df872012-04-19 00:39:37 +0000126 /* Make sure netvsc_set_multicast_list doesn't re-enable filter! */
127 cancel_work_sync(&net_device_ctx->work);
Vitaly Kuznetsov2f5fa6c2016-06-03 17:51:00 +0200128 ret = rndis_filter_close(nvdev);
Haiyang Zhang2de85302015-07-13 13:09:16 -0700129 if (ret != 0) {
Hank Jansseneb335bc2011-03-29 13:58:48 -0700130 netdev_err(net, "unable to close device (ret %d).\n", ret);
Haiyang Zhang2de85302015-07-13 13:09:16 -0700131 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++) {
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800138 chn = nvdev->chan_table[i].channel;
Haiyang Zhang2de85302015-07-13 13:09:16 -0700139 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 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700169
Hank Janssenfceaf242009-07-13 15:34:54 -0700170 return ret;
171}
172
KY Srinivasan8a002512014-03-08 19:23:14 -0800173static 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
stephen hemmingerd8e18ee2017-01-24 13:06:05 -0800194/*
195 * Select queue for transmit.
196 *
197 * If a valid queue has already been assigned, then use that.
198 * Otherwise compute tx queue based on hash and the send table.
199 *
200 * This is basically similar to default (__netdev_pick_tx) with the added step
201 * of using the host send_table when no other queue has been assigned.
202 *
203 * TODO support XPS - but get_xps_queue not exported
204 */
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700205static u16 netvsc_select_queue(struct net_device *ndev, struct sk_buff *skb,
206 void *accel_priv, select_queue_fallback_t fallback)
207{
208 struct net_device_context *net_device_ctx = netdev_priv(ndev);
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200209 struct netvsc_device *nvsc_dev = net_device_ctx->nvdev;
stephen hemmingerd8e18ee2017-01-24 13:06:05 -0800210 struct sock *sk = skb->sk;
211 int q_idx = sk_tx_queue_get(sk);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700212
stephen hemmingerd8e18ee2017-01-24 13:06:05 -0800213 if (q_idx < 0 || skb->ooo_okay ||
214 q_idx >= ndev->real_num_tx_queues) {
215 u16 hash = __skb_tx_hash(ndev, skb, VRSS_SEND_TAB_SIZE);
216 int new_idx;
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700217
stephen hemmingerd8e18ee2017-01-24 13:06:05 -0800218 new_idx = nvsc_dev->send_table[hash]
219 % nvsc_dev->num_chn;
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700220
stephen hemmingerd8e18ee2017-01-24 13:06:05 -0800221 if (q_idx != new_idx && sk &&
222 sk_fullsock(sk) && rcu_access_pointer(sk->sk_dst_cache))
223 sk_tx_queue_set(sk, new_idx);
224
225 q_idx = new_idx;
226 }
227
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800228 if (unlikely(!nvsc_dev->chan_table[q_idx].channel))
Vitaly Kuznetsov8b9fbe12015-12-01 16:43:11 -0800229 q_idx = 0;
230
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700231 return q_idx;
232}
233
KY Srinivasan54a73572014-03-08 19:23:13 -0800234static u32 fill_pg_buf(struct page *page, u32 offset, u32 len,
235 struct hv_page_buffer *pb)
236{
237 int j = 0;
238
239 /* Deal with compund pages by ignoring unused part
240 * of the page.
241 */
242 page += (offset >> PAGE_SHIFT);
243 offset &= ~PAGE_MASK;
244
245 while (len > 0) {
246 unsigned long bytes;
247
248 bytes = PAGE_SIZE - offset;
249 if (bytes > len)
250 bytes = len;
251 pb[j].pfn = page_to_pfn(page);
252 pb[j].offset = offset;
253 pb[j].len = bytes;
254
255 offset += bytes;
256 len -= bytes;
257
258 if (offset == PAGE_SIZE && len) {
259 page++;
260 offset = 0;
261 j++;
262 }
263 }
264
265 return j + 1;
266}
267
KY Srinivasan8a002512014-03-08 19:23:14 -0800268static u32 init_page_array(void *hdr, u32 len, struct sk_buff *skb,
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800269 struct hv_netvsc_packet *packet,
270 struct hv_page_buffer **page_buf)
KY Srinivasan54a73572014-03-08 19:23:13 -0800271{
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800272 struct hv_page_buffer *pb = *page_buf;
KY Srinivasan54a73572014-03-08 19:23:13 -0800273 u32 slots_used = 0;
274 char *data = skb->data;
275 int frags = skb_shinfo(skb)->nr_frags;
276 int i;
277
278 /* The packet is laid out thus:
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700279 * 1. hdr: RNDIS header and PPI
KY Srinivasan54a73572014-03-08 19:23:13 -0800280 * 2. skb linear data
281 * 3. skb fragment data
282 */
283 if (hdr != NULL)
284 slots_used += fill_pg_buf(virt_to_page(hdr),
285 offset_in_page(hdr),
286 len, &pb[slots_used]);
287
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700288 packet->rmsg_size = len;
289 packet->rmsg_pgcnt = slots_used;
290
KY Srinivasan54a73572014-03-08 19:23:13 -0800291 slots_used += fill_pg_buf(virt_to_page(data),
292 offset_in_page(data),
293 skb_headlen(skb), &pb[slots_used]);
294
295 for (i = 0; i < frags; i++) {
296 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
297
298 slots_used += fill_pg_buf(skb_frag_page(frag),
299 frag->page_offset,
300 skb_frag_size(frag), &pb[slots_used]);
301 }
KY Srinivasan8a002512014-03-08 19:23:14 -0800302 return slots_used;
KY Srinivasan54a73572014-03-08 19:23:13 -0800303}
304
305static int count_skb_frag_slots(struct sk_buff *skb)
306{
307 int i, frags = skb_shinfo(skb)->nr_frags;
308 int pages = 0;
309
310 for (i = 0; i < frags; i++) {
311 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
312 unsigned long size = skb_frag_size(frag);
313 unsigned long offset = frag->page_offset;
314
315 /* Skip unused frames from start of page */
316 offset &= ~PAGE_MASK;
317 pages += PFN_UP(offset + size);
318 }
319 return pages;
320}
321
322static int netvsc_get_slots(struct sk_buff *skb)
323{
324 char *data = skb->data;
325 unsigned int offset = offset_in_page(data);
326 unsigned int len = skb_headlen(skb);
327 int slots;
328 int frag_slots;
329
330 slots = DIV_ROUND_UP(offset + len, PAGE_SIZE);
331 frag_slots = count_skb_frag_slots(skb);
332 return slots + frag_slots;
333}
334
stephen hemminger23312a32017-01-24 13:05:59 -0800335static u32 net_checksum_info(struct sk_buff *skb)
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800336{
stephen hemminger23312a32017-01-24 13:05:59 -0800337 if (skb->protocol == htons(ETH_P_IP)) {
338 struct iphdr *ip = ip_hdr(skb);
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800339
stephen hemminger23312a32017-01-24 13:05:59 -0800340 if (ip->protocol == IPPROTO_TCP)
341 return TRANSPORT_INFO_IPV4_TCP;
342 else if (ip->protocol == IPPROTO_UDP)
343 return TRANSPORT_INFO_IPV4_UDP;
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800344 } else {
stephen hemminger23312a32017-01-24 13:05:59 -0800345 struct ipv6hdr *ip6 = ipv6_hdr(skb);
346
347 if (ip6->nexthdr == IPPROTO_TCP)
348 return TRANSPORT_INFO_IPV6_TCP;
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800349 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
stephen hemminger23312a32017-01-24 13:05:59 -0800350 return TRANSPORT_INFO_IPV6_UDP;
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800351 }
352
stephen hemminger23312a32017-01-24 13:05:59 -0800353 return TRANSPORT_INFO_NOT_IP;
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800354}
355
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700356static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
Hank Janssenfceaf242009-07-13 15:34:54 -0700357{
Hank Janssenfceaf242009-07-13 15:34:54 -0700358 struct net_device_context *net_device_ctx = netdev_priv(net);
Vitaly Kuznetsov981a1bd2015-04-08 17:54:05 +0200359 struct hv_netvsc_packet *packet = NULL;
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700360 int ret;
KY Srinivasan8a002512014-03-08 19:23:14 -0800361 unsigned int num_data_pgs;
362 struct rndis_message *rndis_msg;
363 struct rndis_packet *rndis_pkt;
364 u32 rndis_msg_size;
KY Srinivasan8a002512014-03-08 19:23:14 -0800365 struct rndis_per_packet_info *ppi;
Haiyang Zhang307f0992014-05-21 12:55:39 -0700366 u32 hash;
Vitaly Kuznetsove88f7e02015-04-08 17:54:06 +0200367 u32 skb_length;
KY Srinivasanb08cc792015-03-29 21:08:42 -0700368 struct hv_page_buffer page_buf[MAX_PAGE_BUFFER_COUNT];
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800369 struct hv_page_buffer *pb = page_buf;
Hank Janssenfceaf242009-07-13 15:34:54 -0700370
KY Srinivasan54a73572014-03-08 19:23:13 -0800371 /* We will atmost need two pages to describe the rndis
372 * header. We can only transmit MAX_PAGE_BUFFER_COUNT number
Vitaly Kuznetsove88f7e02015-04-08 17:54:06 +0200373 * of pages in a single packet. If skb is scattered around
374 * more pages we try linearizing it.
KY Srinivasan54a73572014-03-08 19:23:13 -0800375 */
Vitaly Kuznetsove88f7e02015-04-08 17:54:06 +0200376
Vitaly Kuznetsove88f7e02015-04-08 17:54:06 +0200377 skb_length = skb->len;
KY Srinivasan8a002512014-03-08 19:23:14 -0800378 num_data_pgs = netvsc_get_slots(skb) + 2;
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700379
380 if (unlikely(num_data_pgs > MAX_PAGE_BUFFER_COUNT)) {
Stephen Hemminger4323b472016-08-23 12:17:57 -0700381 ++net_device_ctx->eth_stats.tx_scattered;
382
383 if (skb_linearize(skb))
384 goto no_memory;
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700385
386 num_data_pgs = netvsc_get_slots(skb) + 2;
387 if (num_data_pgs > MAX_PAGE_BUFFER_COUNT) {
Stephen Hemminger4323b472016-08-23 12:17:57 -0700388 ++net_device_ctx->eth_stats.tx_too_big;
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700389 goto drop;
390 }
KY Srinivasan54a73572014-03-08 19:23:13 -0800391 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700392
KY Srinivasanc0eb4542015-12-01 16:43:10 -0800393 /*
394 * Place the rndis header in the skb head room and
395 * the skb->cb will be used for hv_netvsc_packet
396 * structure.
397 */
398 ret = skb_cow_head(skb, RNDIS_AND_PPI_SIZE);
Stephen Hemminger4323b472016-08-23 12:17:57 -0700399 if (ret)
400 goto no_memory;
401
KY Srinivasanc0eb4542015-12-01 16:43:10 -0800402 /* Use the skb control buffer for building up the packet */
403 BUILD_BUG_ON(sizeof(struct hv_netvsc_packet) >
404 FIELD_SIZEOF(struct sk_buff, cb));
405 packet = (struct hv_netvsc_packet *)skb->cb;
Hank Janssenfceaf242009-07-13 15:34:54 -0700406
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700407 packet->q_idx = skb_get_queue_mapping(skb);
408
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800409 packet->total_data_buflen = skb->len;
Hank Janssenfceaf242009-07-13 15:34:54 -0700410
KY Srinivasanc0eb4542015-12-01 16:43:10 -0800411 rndis_msg = (struct rndis_message *)skb->head;
KY Srinivasanb08cc792015-03-29 21:08:42 -0700412
KY Srinivasan24476762015-12-01 16:43:06 -0800413 memset(rndis_msg, 0, RNDIS_AND_PPI_SIZE);
Hank Janssenfceaf242009-07-13 15:34:54 -0700414
KY Srinivasan8a002512014-03-08 19:23:14 -0800415 /* Add the rndis header */
KY Srinivasan8a002512014-03-08 19:23:14 -0800416 rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET;
417 rndis_msg->msg_len = packet->total_data_buflen;
418 rndis_pkt = &rndis_msg->msg.pkt;
419 rndis_pkt->data_offset = sizeof(struct rndis_packet);
420 rndis_pkt->data_len = packet->total_data_buflen;
421 rndis_pkt->per_pkt_info_offset = sizeof(struct rndis_packet);
422
423 rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet);
424
Haiyang Zhang307f0992014-05-21 12:55:39 -0700425 hash = skb_get_hash_raw(skb);
426 if (hash != 0 && net->real_num_tx_queues > 1) {
427 rndis_msg_size += NDIS_HASH_PPI_SIZE;
428 ppi = init_ppi_data(rndis_msg, NDIS_HASH_PPI_SIZE,
429 NBL_HASH_VALUE);
430 *(u32 *)((void *)ppi + ppi->ppi_offset) = hash;
431 }
432
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700433 if (skb_vlan_tag_present(skb)) {
KY Srinivasan8a002512014-03-08 19:23:14 -0800434 struct ndis_pkt_8021q_info *vlan;
435
436 rndis_msg_size += NDIS_VLAN_PPI_SIZE;
437 ppi = init_ppi_data(rndis_msg, NDIS_VLAN_PPI_SIZE,
438 IEEE_8021Q_INFO);
439 vlan = (struct ndis_pkt_8021q_info *)((void *)ppi +
440 ppi->ppi_offset);
KY Srinivasan760d1e32015-12-01 16:43:19 -0800441 vlan->vlanid = skb->vlan_tci & VLAN_VID_MASK;
442 vlan->pri = (skb->vlan_tci & VLAN_PRIO_MASK) >>
KY Srinivasan8a002512014-03-08 19:23:14 -0800443 VLAN_PRIO_SHIFT;
444 }
445
stephen hemminger23312a32017-01-24 13:05:59 -0800446 if (skb_is_gso(skb)) {
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700447 struct ndis_tcp_lso_info *lso_info;
448
449 rndis_msg_size += NDIS_LSO_PPI_SIZE;
450 ppi = init_ppi_data(rndis_msg, NDIS_LSO_PPI_SIZE,
451 TCP_LARGESEND_PKTINFO);
452
453 lso_info = (struct ndis_tcp_lso_info *)((void *)ppi +
454 ppi->ppi_offset);
455
456 lso_info->lso_v2_transmit.type = NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
stephen hemminger23312a32017-01-24 13:05:59 -0800457 if (skb->protocol == htons(ETH_P_IP)) {
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700458 lso_info->lso_v2_transmit.ip_version =
459 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
460 ip_hdr(skb)->tot_len = 0;
461 ip_hdr(skb)->check = 0;
462 tcp_hdr(skb)->check =
463 ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
464 ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
465 } else {
466 lso_info->lso_v2_transmit.ip_version =
467 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
468 ipv6_hdr(skb)->payload_len = 0;
469 tcp_hdr(skb)->check =
470 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
471 &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
472 }
stephen hemminger23312a32017-01-24 13:05:59 -0800473 lso_info->lso_v2_transmit.tcp_header_offset = skb_transport_offset(skb);
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700474 lso_info->lso_v2_transmit.mss = skb_shinfo(skb)->gso_size;
stephen hemmingerad19bc82016-10-11 14:03:07 -0700475 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
stephen hemminger23312a32017-01-24 13:05:59 -0800476 if (net_checksum_info(skb) & net_device_ctx->tx_checksum_mask) {
477 struct ndis_tcp_ip_checksum_info *csum_info;
478
stephen hemmingerad19bc82016-10-11 14:03:07 -0700479 rndis_msg_size += NDIS_CSUM_PPI_SIZE;
480 ppi = init_ppi_data(rndis_msg, NDIS_CSUM_PPI_SIZE,
481 TCPIP_CHKSUM_PKTINFO);
482
483 csum_info = (struct ndis_tcp_ip_checksum_info *)((void *)ppi +
484 ppi->ppi_offset);
485
stephen hemminger23312a32017-01-24 13:05:59 -0800486 csum_info->transmit.tcp_header_offset = skb_transport_offset(skb);
487
488 if (skb->protocol == htons(ETH_P_IP)) {
stephen hemmingerad19bc82016-10-11 14:03:07 -0700489 csum_info->transmit.is_ipv4 = 1;
stephen hemminger23312a32017-01-24 13:05:59 -0800490
491 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
492 csum_info->transmit.tcp_checksum = 1;
493 else
494 csum_info->transmit.udp_checksum = 1;
495 } else {
stephen hemmingerad19bc82016-10-11 14:03:07 -0700496 csum_info->transmit.is_ipv6 = 1;
497
stephen hemminger23312a32017-01-24 13:05:59 -0800498 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
499 csum_info->transmit.tcp_checksum = 1;
500 else
501 csum_info->transmit.udp_checksum = 1;
502 }
stephen hemmingerad19bc82016-10-11 14:03:07 -0700503 } else {
stephen hemminger23312a32017-01-24 13:05:59 -0800504 /* Can't do offload of this type of checksum */
stephen hemmingerad19bc82016-10-11 14:03:07 -0700505 if (skb_checksum_help(skb))
506 goto drop;
507 }
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700508 }
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800509
KY Srinivasan8a002512014-03-08 19:23:14 -0800510 /* Start filling in the page buffers with the rndis hdr */
511 rndis_msg->msg_len += rndis_msg_size;
Haiyang Zhang942396b2014-10-22 13:47:18 -0700512 packet->total_data_buflen = rndis_msg->msg_len;
KY Srinivasan8a002512014-03-08 19:23:14 -0800513 packet->page_buf_cnt = init_page_array(rndis_msg, rndis_msg_size,
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800514 skb, packet, &pb);
KY Srinivasan8a002512014-03-08 19:23:14 -0800515
sixiao@microsoft.com76d13b52016-02-17 16:43:59 -0800516 /* timestamp packet in software */
517 skb_tx_timestamp(skb);
KY Srinivasan3a3d9a02015-12-01 16:43:14 -0800518 ret = netvsc_send(net_device_ctx->device_ctx, packet,
519 rndis_msg, &pb, skb);
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700520 if (likely(ret == 0)) {
Stephen Hemminger4323b472016-08-23 12:17:57 -0700521 struct netvsc_stats *tx_stats = this_cpu_ptr(net_device_ctx->tx_stats);
522
sixiao@microsoft.com4b02b582015-05-15 02:33:03 -0700523 u64_stats_update_begin(&tx_stats->syncp);
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700524 tx_stats->packets++;
525 tx_stats->bytes += skb_length;
sixiao@microsoft.com4b02b582015-05-15 02:33:03 -0700526 u64_stats_update_end(&tx_stats->syncp);
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700527 return NETDEV_TX_OK;
Hank Janssenfceaf242009-07-13 15:34:54 -0700528 }
Stephen Hemminger4323b472016-08-23 12:17:57 -0700529
530 if (ret == -EAGAIN) {
531 ++net_device_ctx->eth_stats.tx_busy;
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700532 return NETDEV_TX_BUSY;
Stephen Hemminger4323b472016-08-23 12:17:57 -0700533 }
534
535 if (ret == -ENOSPC)
536 ++net_device_ctx->eth_stats.tx_no_space;
Hank Janssenfceaf242009-07-13 15:34:54 -0700537
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700538drop:
539 dev_kfree_skb_any(skb);
540 net->stats.tx_dropped++;
541
542 return NETDEV_TX_OK;
Stephen Hemminger4323b472016-08-23 12:17:57 -0700543
544no_memory:
545 ++net_device_ctx->eth_stats.tx_no_memory;
546 goto drop;
Hank Janssenfceaf242009-07-13 15:34:54 -0700547}
Hank Janssen3e189512010-03-04 22:11:00 +0000548/*
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700549 * netvsc_linkstatus_callback - Link up/down notification
550 */
K. Y. Srinivasan90ef1172011-05-12 19:34:50 -0700551void netvsc_linkstatus_callback(struct hv_device *device_obj,
Haiyang Zhang3a494e72014-06-19 18:34:36 -0700552 struct rndis_message *resp)
Hank Janssenfceaf242009-07-13 15:34:54 -0700553{
Haiyang Zhang3a494e72014-06-19 18:34:36 -0700554 struct rndis_indicate_status *indicate = &resp->msg.indicate_status;
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -0700555 struct net_device *net;
Haiyang Zhangc996edc2011-04-06 15:18:00 -0700556 struct net_device_context *ndev_ctx;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +0100557 struct netvsc_reconfig *event;
558 unsigned long flags;
559
Haiyang Zhang7f5d5af2016-08-04 10:42:15 -0700560 net = hv_get_drvdata(device_obj);
561
562 if (!net)
563 return;
564
565 ndev_ctx = netdev_priv(net);
566
567 /* Update the physical link speed when changing to another vSwitch */
568 if (indicate->status == RNDIS_STATUS_LINK_SPEED_CHANGE) {
569 u32 speed;
570
571 speed = *(u32 *)((void *)indicate + indicate->
572 status_buf_offset) / 10000;
573 ndev_ctx->speed = speed;
574 return;
575 }
576
577 /* Handle these link change statuses below */
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +0100578 if (indicate->status != RNDIS_STATUS_NETWORK_CHANGE &&
579 indicate->status != RNDIS_STATUS_MEDIA_CONNECT &&
580 indicate->status != RNDIS_STATUS_MEDIA_DISCONNECT)
581 return;
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -0700582
Haiyang Zhang7f5d5af2016-08-04 10:42:15 -0700583 if (net->reg_state != NETREG_REGISTERED)
Hank Janssenfceaf242009-07-13 15:34:54 -0700584 return;
Hank Janssenfceaf242009-07-13 15:34:54 -0700585
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +0100586 event = kzalloc(sizeof(*event), GFP_ATOMIC);
587 if (!event)
588 return;
589 event->event = indicate->status;
590
591 spin_lock_irqsave(&ndev_ctx->lock, flags);
592 list_add_tail(&event->list, &ndev_ctx->reconfig_events);
593 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
594
595 schedule_delayed_work(&ndev_ctx->dwork, 0);
Hank Janssenfceaf242009-07-13 15:34:54 -0700596}
597
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700598static struct sk_buff *netvsc_alloc_recv_skb(struct net_device *net,
stephen hemmingerdc54a082017-01-24 13:06:08 -0800599 const struct ndis_tcp_ip_checksum_info *csum_info,
600 const struct ndis_pkt_8021q_info *vlan,
601 void *data, u32 buflen)
Hank Janssenfceaf242009-07-13 15:34:54 -0700602{
Hank Janssenfceaf242009-07-13 15:34:54 -0700603 struct sk_buff *skb;
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -0700604
stephen hemmingerdc54a082017-01-24 13:06:08 -0800605 skb = netdev_alloc_skb_ip_align(net, buflen);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700606 if (!skb)
607 return skb;
Hank Janssenfceaf242009-07-13 15:34:54 -0700608
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700609 /*
610 * Copy to skb. This copy is needed here since the memory pointed by
611 * hv_netvsc_packet cannot be deallocated
612 */
stephen hemmingerdc54a082017-01-24 13:06:08 -0800613 memcpy(skb_put(skb, buflen), data, buflen);
Hank Janssenfceaf242009-07-13 15:34:54 -0700614
615 skb->protocol = eth_type_trans(skb, net);
Stephen Hemmingere52fed72016-10-23 21:32:47 -0700616
617 /* skb is already created with CHECKSUM_NONE */
618 skb_checksum_none_assert(skb);
619
620 /*
621 * In Linux, the IP checksum is always checked.
622 * Do L4 checksum offload if enabled and present.
623 */
624 if (csum_info && (net->features & NETIF_F_RXCSUM)) {
625 if (csum_info->receive.tcp_checksum_succeeded ||
626 csum_info->receive.udp_checksum_succeeded)
KY Srinivasane3d605e2014-03-08 19:23:16 -0800627 skb->ip_summed = CHECKSUM_UNNECESSARY;
KY Srinivasane3d605e2014-03-08 19:23:16 -0800628 }
629
stephen hemmingerdc54a082017-01-24 13:06:08 -0800630 if (vlan) {
631 u16 vlan_tci = vlan->vlanid | (vlan->pri << VLAN_PRIO_SHIFT);
632
Haiyang Zhang93725cb2013-06-17 15:36:49 -0700633 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
KY Srinivasan760d1e32015-12-01 16:43:19 -0800634 vlan_tci);
stephen hemmingerdc54a082017-01-24 13:06:08 -0800635 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700636
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700637 return skb;
638}
639
640/*
641 * netvsc_recv_callback - Callback when we receive a packet from the
642 * "wire" on the specified device.
643 */
stephen hemmingerdc54a082017-01-24 13:06:08 -0800644int netvsc_recv_callback(struct net_device *net,
645 struct vmbus_channel *channel,
646 void *data, u32 len,
647 const struct ndis_tcp_ip_checksum_info *csum_info,
648 const struct ndis_pkt_8021q_info *vlan)
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700649{
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200650 struct net_device_context *net_device_ctx = netdev_priv(net);
Stephen Hemmingerf207c102016-09-22 16:56:33 -0700651 struct net_device *vf_netdev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700652 struct sk_buff *skb;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700653 struct netvsc_stats *rx_stats;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700654
Stephen Hemminger9cbcc422016-09-22 16:56:34 -0700655 if (net->reg_state != NETREG_REGISTERED)
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700656 return NVSP_STAT_FAIL;
657
Stephen Hemminger9cbcc422016-09-22 16:56:34 -0700658 /*
659 * If necessary, inject this packet into the VF interface.
660 * On Hyper-V, multicast and brodcast packets are only delivered
661 * to the synthetic interface (after subjecting these to
662 * policy filters on the host). Deliver these via the VF
663 * interface in the guest.
664 */
stephen hemminger0719e722017-01-11 09:16:32 -0800665 rcu_read_lock();
Stephen Hemmingerf207c102016-09-22 16:56:33 -0700666 vf_netdev = rcu_dereference(net_device_ctx->vf_netdev);
Stephen Hemminger9cbcc422016-09-22 16:56:34 -0700667 if (vf_netdev && (vf_netdev->flags & IFF_UP))
668 net = vf_netdev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700669
670 /* Allocate a skb - TODO direct I/O to pages? */
stephen hemmingerdc54a082017-01-24 13:06:08 -0800671 skb = netvsc_alloc_recv_skb(net, csum_info, vlan, data, len);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700672 if (unlikely(!skb)) {
673 ++net->stats.rx_dropped;
stephen hemminger0719e722017-01-11 09:16:32 -0800674 rcu_read_unlock();
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700675 return NVSP_STAT_FAIL;
676 }
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700677
Stephen Hemminger9cbcc422016-09-22 16:56:34 -0700678 if (net != vf_netdev)
679 skb_record_rx_queue(skb,
680 channel->offermsg.offer.sub_channel_index);
681
682 /*
683 * Even if injecting the packet, record the statistics
684 * on the synthetic device because modifying the VF device
685 * statistics will not work correctly.
686 */
687 rx_stats = this_cpu_ptr(net_device_ctx->rx_stats);
sixiao@microsoft.com4b02b582015-05-15 02:33:03 -0700688 u64_stats_update_begin(&rx_stats->syncp);
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700689 rx_stats->packets++;
stephen hemmingerdc54a082017-01-24 13:06:08 -0800690 rx_stats->bytes += len;
Stephen Hemmingerf7ad75b2016-09-22 16:56:35 -0700691
692 if (skb->pkt_type == PACKET_BROADCAST)
693 ++rx_stats->broadcast;
694 else if (skb->pkt_type == PACKET_MULTICAST)
695 ++rx_stats->multicast;
sixiao@microsoft.com4b02b582015-05-15 02:33:03 -0700696 u64_stats_update_end(&rx_stats->syncp);
Stephen Hemminger9495c282010-03-09 17:42:17 -0800697
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700698 /*
699 * Pass the skb back up. Network stack will deallocate the skb when it
Stephen Hemminger9495c282010-03-09 17:42:17 -0800700 * is done.
701 * TODO - use NAPI?
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700702 */
Stephen Hemminger9495c282010-03-09 17:42:17 -0800703 netif_rx(skb);
stephen hemminger0719e722017-01-11 09:16:32 -0800704 rcu_read_unlock();
Hank Janssenfceaf242009-07-13 15:34:54 -0700705
Hank Janssenfceaf242009-07-13 15:34:54 -0700706 return 0;
707}
708
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -0700709static void netvsc_get_drvinfo(struct net_device *net,
710 struct ethtool_drvinfo *info)
711{
Jiri Pirko7826d432013-01-06 00:44:26 +0000712 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
Jiri Pirko7826d432013-01-06 00:44:26 +0000713 strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -0700714}
715
Andrew Schwartzmeyer59995372015-02-26 16:27:14 -0800716static void netvsc_get_channels(struct net_device *net,
717 struct ethtool_channels *channel)
718{
719 struct net_device_context *net_device_ctx = netdev_priv(net);
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200720 struct netvsc_device *nvdev = net_device_ctx->nvdev;
Andrew Schwartzmeyer59995372015-02-26 16:27:14 -0800721
722 if (nvdev) {
723 channel->max_combined = nvdev->max_chn;
724 channel->combined_count = nvdev->num_chn;
725 }
726}
727
stephen hemminger2b018882017-01-24 13:06:03 -0800728static int netvsc_set_queues(struct net_device *net, struct hv_device *dev,
729 u32 num_chn)
730{
731 struct netvsc_device_info device_info;
732 int ret;
733
734 memset(&device_info, 0, sizeof(device_info));
735 device_info.num_chn = num_chn;
736 device_info.ring_size = ring_size;
737 device_info.max_num_vrss_chns = num_chn;
738
739 ret = rndis_filter_device_add(dev, &device_info);
740 if (ret)
741 return ret;
742
743 ret = netif_set_real_num_tx_queues(net, num_chn);
744 if (ret)
745 return ret;
746
747 ret = netif_set_real_num_rx_queues(net, num_chn);
748
749 return ret;
750}
751
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700752static int netvsc_set_channels(struct net_device *net,
753 struct ethtool_channels *channels)
754{
755 struct net_device_context *net_device_ctx = netdev_priv(net);
756 struct hv_device *dev = net_device_ctx->device_ctx;
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200757 struct netvsc_device *nvdev = net_device_ctx->nvdev;
stephen hemminger2b018882017-01-24 13:06:03 -0800758 unsigned int count = channels->combined_count;
759 int ret;
760
761 /* We do not support separate count for rx, tx, or other */
762 if (count == 0 ||
763 channels->rx_count || channels->tx_count || channels->other_count)
764 return -EINVAL;
765
766 if (count > net->num_tx_queues || count > net->num_rx_queues)
767 return -EINVAL;
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700768
Vitaly Kuznetsov6da72252016-05-13 13:55:24 +0200769 if (net_device_ctx->start_remove || !nvdev || nvdev->destroy)
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700770 return -ENODEV;
771
stephen hemminger2b018882017-01-24 13:06:03 -0800772 if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5)
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700773 return -EINVAL;
774
stephen hemminger2b018882017-01-24 13:06:03 -0800775 if (count > nvdev->max_chn)
776 return -EINVAL;
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700777
778 ret = netvsc_close(net);
779 if (ret)
stephen hemminger2b018882017-01-24 13:06:03 -0800780 return ret;
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700781
Vitaly Kuznetsovf580aec2016-05-13 13:55:20 +0200782 net_device_ctx->start_remove = true;
stephen hemminger2289f0a2017-01-24 13:06:10 -0800783 rndis_filter_device_remove(dev, nvdev);
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700784
stephen hemminger2b018882017-01-24 13:06:03 -0800785 ret = netvsc_set_queues(net, dev, count);
786 if (ret == 0)
787 nvdev->num_chn = count;
788 else
789 netvsc_set_queues(net, dev, nvdev->num_chn);
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700790
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700791 netvsc_open(net);
Vitaly Kuznetsovf580aec2016-05-13 13:55:20 +0200792 net_device_ctx->start_remove = false;
stephen hemminger2b018882017-01-24 13:06:03 -0800793
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +0200794 /* We may have missed link change notifications */
795 schedule_delayed_work(&net_device_ctx->dwork, 0);
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700796
797 return ret;
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700798}
799
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800800static bool netvsc_validate_ethtool_ss_cmd(const struct ethtool_cmd *cmd)
801{
802 struct ethtool_cmd diff1 = *cmd;
803 struct ethtool_cmd diff2 = {};
804
805 ethtool_cmd_speed_set(&diff1, 0);
806 diff1.duplex = 0;
807 /* advertising and cmd are usually set */
808 diff1.advertising = 0;
809 diff1.cmd = 0;
810 /* We set port to PORT_OTHER */
811 diff2.port = PORT_OTHER;
812
813 return !memcmp(&diff1, &diff2, sizeof(diff1));
814}
815
816static void netvsc_init_settings(struct net_device *dev)
817{
818 struct net_device_context *ndc = netdev_priv(dev);
819
820 ndc->speed = SPEED_UNKNOWN;
821 ndc->duplex = DUPLEX_UNKNOWN;
822}
823
824static int netvsc_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
825{
826 struct net_device_context *ndc = netdev_priv(dev);
827
828 ethtool_cmd_speed_set(cmd, ndc->speed);
829 cmd->duplex = ndc->duplex;
830 cmd->port = PORT_OTHER;
831
832 return 0;
833}
834
835static int netvsc_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
836{
837 struct net_device_context *ndc = netdev_priv(dev);
838 u32 speed;
839
840 speed = ethtool_cmd_speed(cmd);
841 if (!ethtool_validate_speed(speed) ||
842 !ethtool_validate_duplex(cmd->duplex) ||
843 !netvsc_validate_ethtool_ss_cmd(cmd))
844 return -EINVAL;
845
846 ndc->speed = speed;
847 ndc->duplex = cmd->duplex;
848
849 return 0;
850}
851
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800852static int netvsc_change_mtu(struct net_device *ndev, int mtu)
853{
854 struct net_device_context *ndevctx = netdev_priv(ndev);
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200855 struct netvsc_device *nvdev = ndevctx->nvdev;
856 struct hv_device *hdev = ndevctx->device_ctx;
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800857 struct netvsc_device_info device_info;
stephen hemminger2b018882017-01-24 13:06:03 -0800858 int ret;
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800859
Vitaly Kuznetsov6da72252016-05-13 13:55:24 +0200860 if (ndevctx->start_remove || !nvdev || nvdev->destroy)
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800861 return -ENODEV;
862
Haiyang Zhang2de85302015-07-13 13:09:16 -0700863 ret = netvsc_close(ndev);
864 if (ret)
865 goto out;
866
Vitaly Kuznetsovf580aec2016-05-13 13:55:20 +0200867 ndevctx->start_remove = true;
stephen hemminger2289f0a2017-01-24 13:06:10 -0800868 rndis_filter_device_remove(hdev, nvdev);
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800869
870 ndev->mtu = mtu;
871
Andrew Schwartzmeyer8ebdcc52015-08-11 17:14:31 -0700872 memset(&device_info, 0, sizeof(device_info));
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800873 device_info.ring_size = ring_size;
stephen hemminger2b018882017-01-24 13:06:03 -0800874 device_info.num_chn = nvdev->num_chn;
875 device_info.max_num_vrss_chns = nvdev->num_chn;
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800876 rndis_filter_device_add(hdev, &device_info);
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800877
Haiyang Zhang2de85302015-07-13 13:09:16 -0700878out:
879 netvsc_open(ndev);
Vitaly Kuznetsovf580aec2016-05-13 13:55:20 +0200880 ndevctx->start_remove = false;
Haiyang Zhang2de85302015-07-13 13:09:16 -0700881
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +0200882 /* We may have missed link change notifications */
883 schedule_delayed_work(&ndevctx->dwork, 0);
884
Haiyang Zhang2de85302015-07-13 13:09:16 -0700885 return ret;
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800886}
887
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800888static void netvsc_get_stats64(struct net_device *net,
889 struct rtnl_link_stats64 *t)
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700890{
891 struct net_device_context *ndev_ctx = netdev_priv(net);
892 int cpu;
893
894 for_each_possible_cpu(cpu) {
895 struct netvsc_stats *tx_stats = per_cpu_ptr(ndev_ctx->tx_stats,
896 cpu);
897 struct netvsc_stats *rx_stats = per_cpu_ptr(ndev_ctx->rx_stats,
898 cpu);
Stephen Hemmingerf7ad75b2016-09-22 16:56:35 -0700899 u64 tx_packets, tx_bytes, rx_packets, rx_bytes, rx_multicast;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700900 unsigned int start;
901
902 do {
sixiao@microsoft.com4b02b582015-05-15 02:33:03 -0700903 start = u64_stats_fetch_begin_irq(&tx_stats->syncp);
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700904 tx_packets = tx_stats->packets;
905 tx_bytes = tx_stats->bytes;
sixiao@microsoft.com4b02b582015-05-15 02:33:03 -0700906 } while (u64_stats_fetch_retry_irq(&tx_stats->syncp, start));
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700907
908 do {
sixiao@microsoft.com4b02b582015-05-15 02:33:03 -0700909 start = u64_stats_fetch_begin_irq(&rx_stats->syncp);
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700910 rx_packets = rx_stats->packets;
911 rx_bytes = rx_stats->bytes;
Stephen Hemmingerf7ad75b2016-09-22 16:56:35 -0700912 rx_multicast = rx_stats->multicast + rx_stats->broadcast;
sixiao@microsoft.com4b02b582015-05-15 02:33:03 -0700913 } while (u64_stats_fetch_retry_irq(&rx_stats->syncp, start));
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700914
915 t->tx_bytes += tx_bytes;
916 t->tx_packets += tx_packets;
917 t->rx_bytes += rx_bytes;
918 t->rx_packets += rx_packets;
Stephen Hemmingerf7ad75b2016-09-22 16:56:35 -0700919 t->multicast += rx_multicast;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700920 }
921
922 t->tx_dropped = net->stats.tx_dropped;
923 t->tx_errors = net->stats.tx_dropped;
924
925 t->rx_dropped = net->stats.rx_dropped;
926 t->rx_errors = net->stats.rx_errors;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700927}
Haiyang Zhang1ce09e82012-07-10 07:19:22 +0000928
929static int netvsc_set_mac_addr(struct net_device *ndev, void *p)
930{
Haiyang Zhang1ce09e82012-07-10 07:19:22 +0000931 struct sockaddr *addr = p;
Jianjun Kong9a4c8312013-01-18 16:52:09 +0000932 char save_adr[ETH_ALEN];
Haiyang Zhang1ce09e82012-07-10 07:19:22 +0000933 unsigned char save_aatype;
934 int err;
935
936 memcpy(save_adr, ndev->dev_addr, ETH_ALEN);
937 save_aatype = ndev->addr_assign_type;
938
939 err = eth_mac_addr(ndev, p);
940 if (err != 0)
941 return err;
942
Vitaly Kuznetsove834da9a2016-06-03 17:51:01 +0200943 err = rndis_filter_set_device_mac(ndev, addr->sa_data);
Haiyang Zhang1ce09e82012-07-10 07:19:22 +0000944 if (err != 0) {
945 /* roll back to saved MAC */
946 memcpy(ndev->dev_addr, save_adr, ETH_ALEN);
947 ndev->addr_assign_type = save_aatype;
948 }
949
950 return err;
951}
952
Stephen Hemminger4323b472016-08-23 12:17:57 -0700953static const struct {
954 char name[ETH_GSTRING_LEN];
955 u16 offset;
956} netvsc_stats[] = {
957 { "tx_scattered", offsetof(struct netvsc_ethtool_stats, tx_scattered) },
958 { "tx_no_memory", offsetof(struct netvsc_ethtool_stats, tx_no_memory) },
959 { "tx_no_space", offsetof(struct netvsc_ethtool_stats, tx_no_space) },
960 { "tx_too_big", offsetof(struct netvsc_ethtool_stats, tx_too_big) },
961 { "tx_busy", offsetof(struct netvsc_ethtool_stats, tx_busy) },
962};
963
964static int netvsc_get_sset_count(struct net_device *dev, int string_set)
965{
966 switch (string_set) {
967 case ETH_SS_STATS:
968 return ARRAY_SIZE(netvsc_stats);
969 default:
970 return -EINVAL;
971 }
972}
973
974static void netvsc_get_ethtool_stats(struct net_device *dev,
975 struct ethtool_stats *stats, u64 *data)
976{
977 struct net_device_context *ndc = netdev_priv(dev);
978 const void *nds = &ndc->eth_stats;
979 int i;
980
981 for (i = 0; i < ARRAY_SIZE(netvsc_stats); i++)
982 data[i] = *(unsigned long *)(nds + netvsc_stats[i].offset);
983}
984
985static void netvsc_get_strings(struct net_device *dev, u32 stringset, u8 *data)
986{
987 int i;
988
989 switch (stringset) {
990 case ETH_SS_STATS:
991 for (i = 0; i < ARRAY_SIZE(netvsc_stats); i++)
992 memcpy(data + i * ETH_GSTRING_LEN,
993 netvsc_stats[i].name, ETH_GSTRING_LEN);
994 break;
995 }
996}
997
stephen hemmingerb448f4e2017-01-24 13:06:00 -0800998static int
stephen hemmingerb5a5dc82017-01-24 13:06:01 -0800999netvsc_get_rss_hash_opts(struct netvsc_device *nvdev,
1000 struct ethtool_rxnfc *info)
1001{
1002 info->data = RXH_IP_SRC | RXH_IP_DST;
1003
1004 switch (info->flow_type) {
1005 case TCP_V4_FLOW:
1006 case TCP_V6_FLOW:
1007 info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
1008 /* fallthrough */
1009 case UDP_V4_FLOW:
1010 case UDP_V6_FLOW:
1011 case IPV4_FLOW:
1012 case IPV6_FLOW:
1013 break;
1014 default:
1015 info->data = 0;
1016 break;
1017 }
1018
1019 return 0;
1020}
1021
1022static int
stephen hemmingerb448f4e2017-01-24 13:06:00 -08001023netvsc_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
1024 u32 *rules)
1025{
1026 struct net_device_context *ndc = netdev_priv(dev);
1027 struct netvsc_device *nvdev = ndc->nvdev;
1028
1029 switch (info->cmd) {
1030 case ETHTOOL_GRXRINGS:
1031 info->data = nvdev->num_chn;
1032 return 0;
stephen hemmingerb5a5dc82017-01-24 13:06:01 -08001033
1034 case ETHTOOL_GRXFH:
1035 return netvsc_get_rss_hash_opts(nvdev, info);
stephen hemmingerb448f4e2017-01-24 13:06:00 -08001036 }
1037 return -EOPNOTSUPP;
1038}
1039
Richard Weinberger316158f2014-07-09 16:23:59 +02001040#ifdef CONFIG_NET_POLL_CONTROLLER
1041static void netvsc_poll_controller(struct net_device *net)
1042{
1043 /* As netvsc_start_xmit() works synchronous we don't have to
1044 * trigger anything here.
1045 */
1046}
1047#endif
Haiyang Zhang1ce09e82012-07-10 07:19:22 +00001048
stephen hemminger962f3fe2017-01-24 13:06:02 -08001049static u32 netvsc_get_rxfh_key_size(struct net_device *dev)
1050{
1051 return NETVSC_HASH_KEYLEN;
1052}
1053
1054static u32 netvsc_rss_indir_size(struct net_device *dev)
1055{
stephen hemmingerff4a4412017-01-24 13:06:04 -08001056 return ITAB_NUM;
stephen hemminger962f3fe2017-01-24 13:06:02 -08001057}
1058
1059static int netvsc_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
1060 u8 *hfunc)
1061{
1062 struct net_device_context *ndc = netdev_priv(dev);
1063 struct netvsc_device *ndev = ndc->nvdev;
1064 struct rndis_device *rndis_dev = ndev->extension;
stephen hemmingerff4a4412017-01-24 13:06:04 -08001065 int i;
stephen hemminger962f3fe2017-01-24 13:06:02 -08001066
1067 if (hfunc)
1068 *hfunc = ETH_RSS_HASH_TOP; /* Toeplitz */
1069
stephen hemmingerff4a4412017-01-24 13:06:04 -08001070 if (indir) {
1071 for (i = 0; i < ITAB_NUM; i++)
1072 indir[i] = rndis_dev->ind_table[i];
1073 }
1074
stephen hemminger962f3fe2017-01-24 13:06:02 -08001075 if (key)
1076 memcpy(key, rndis_dev->rss_key, NETVSC_HASH_KEYLEN);
1077
1078 return 0;
1079}
1080
1081static int netvsc_set_rxfh(struct net_device *dev, const u32 *indir,
1082 const u8 *key, const u8 hfunc)
1083{
1084 struct net_device_context *ndc = netdev_priv(dev);
1085 struct netvsc_device *ndev = ndc->nvdev;
1086 struct rndis_device *rndis_dev = ndev->extension;
stephen hemmingerff4a4412017-01-24 13:06:04 -08001087 int i;
stephen hemminger962f3fe2017-01-24 13:06:02 -08001088
1089 if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
1090 return -EOPNOTSUPP;
1091
stephen hemmingerff4a4412017-01-24 13:06:04 -08001092 if (indir) {
1093 for (i = 0; i < ITAB_NUM; i++)
1094 if (indir[i] >= dev->num_rx_queues)
1095 return -EINVAL;
1096
1097 for (i = 0; i < ITAB_NUM; i++)
1098 rndis_dev->ind_table[i] = indir[i];
1099 }
1100
1101 if (!key) {
1102 if (!indir)
1103 return 0;
1104
1105 key = rndis_dev->rss_key;
1106 }
stephen hemminger962f3fe2017-01-24 13:06:02 -08001107
1108 return rndis_filter_set_rss_param(rndis_dev, key, ndev->num_chn);
1109}
1110
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -07001111static const struct ethtool_ops ethtool_ops = {
1112 .get_drvinfo = netvsc_get_drvinfo,
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -07001113 .get_link = ethtool_op_get_link,
Stephen Hemminger4323b472016-08-23 12:17:57 -07001114 .get_ethtool_stats = netvsc_get_ethtool_stats,
1115 .get_sset_count = netvsc_get_sset_count,
1116 .get_strings = netvsc_get_strings,
Andrew Schwartzmeyer59995372015-02-26 16:27:14 -08001117 .get_channels = netvsc_get_channels,
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -07001118 .set_channels = netvsc_set_channels,
sixiao@microsoft.com76d13b52016-02-17 16:43:59 -08001119 .get_ts_info = ethtool_op_get_ts_info,
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -08001120 .get_settings = netvsc_get_settings,
1121 .set_settings = netvsc_set_settings,
stephen hemmingerb448f4e2017-01-24 13:06:00 -08001122 .get_rxnfc = netvsc_get_rxnfc,
stephen hemminger962f3fe2017-01-24 13:06:02 -08001123 .get_rxfh_key_size = netvsc_get_rxfh_key_size,
1124 .get_rxfh_indir_size = netvsc_rss_indir_size,
1125 .get_rxfh = netvsc_get_rxfh,
1126 .set_rxfh = netvsc_set_rxfh,
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -07001127};
1128
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001129static const struct net_device_ops device_ops = {
1130 .ndo_open = netvsc_open,
1131 .ndo_stop = netvsc_close,
1132 .ndo_start_xmit = netvsc_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001133 .ndo_set_rx_mode = netvsc_set_multicast_list,
Haiyang Zhang4d447c92011-12-15 13:45:17 -08001134 .ndo_change_mtu = netvsc_change_mtu,
Haiyang Zhangb681b582010-08-03 19:15:31 +00001135 .ndo_validate_addr = eth_validate_addr,
Haiyang Zhang1ce09e82012-07-10 07:19:22 +00001136 .ndo_set_mac_address = netvsc_set_mac_addr,
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001137 .ndo_select_queue = netvsc_select_queue,
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001138 .ndo_get_stats64 = netvsc_get_stats64,
Richard Weinberger316158f2014-07-09 16:23:59 +02001139#ifdef CONFIG_NET_POLL_CONTROLLER
1140 .ndo_poll_controller = netvsc_poll_controller,
1141#endif
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001142};
1143
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001144/*
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001145 * Handle link status changes. For RNDIS_STATUS_NETWORK_CHANGE emulate link
1146 * down/up sequence. In case of RNDIS_STATUS_MEDIA_CONNECT when carrier is
1147 * present send GARP packet to network peers with netif_notify_peers().
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001148 */
Haiyang Zhang891de742014-02-12 16:54:27 -08001149static void netvsc_link_change(struct work_struct *w)
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001150{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001151 struct net_device_context *ndev_ctx =
1152 container_of(w, struct net_device_context, dwork.work);
1153 struct hv_device *device_obj = ndev_ctx->device_ctx;
1154 struct net_device *net = hv_get_drvdata(device_obj);
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -07001155 struct netvsc_device *net_device;
Haiyang Zhang891de742014-02-12 16:54:27 -08001156 struct rndis_device *rdev;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001157 struct netvsc_reconfig *event = NULL;
1158 bool notify = false, reschedule = false;
1159 unsigned long flags, next_reconfig, delay;
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001160
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001161 rtnl_lock();
1162 if (ndev_ctx->start_remove)
1163 goto out_unlock;
1164
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +02001165 net_device = ndev_ctx->nvdev;
Haiyang Zhang891de742014-02-12 16:54:27 -08001166 rdev = net_device->extension;
Haiyang Zhang891de742014-02-12 16:54:27 -08001167
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001168 next_reconfig = ndev_ctx->last_reconfig + LINKCHANGE_INT;
1169 if (time_is_after_jiffies(next_reconfig)) {
1170 /* link_watch only sends one notification with current state
1171 * per second, avoid doing reconfig more frequently. Handle
1172 * wrap around.
1173 */
1174 delay = next_reconfig - jiffies;
1175 delay = delay < LINKCHANGE_INT ? delay : LINKCHANGE_INT;
1176 schedule_delayed_work(&ndev_ctx->dwork, delay);
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001177 goto out_unlock;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001178 }
1179 ndev_ctx->last_reconfig = jiffies;
1180
1181 spin_lock_irqsave(&ndev_ctx->lock, flags);
1182 if (!list_empty(&ndev_ctx->reconfig_events)) {
1183 event = list_first_entry(&ndev_ctx->reconfig_events,
1184 struct netvsc_reconfig, list);
1185 list_del(&event->list);
1186 reschedule = !list_empty(&ndev_ctx->reconfig_events);
1187 }
1188 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1189
1190 if (!event)
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001191 goto out_unlock;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001192
1193 switch (event->event) {
1194 /* Only the following events are possible due to the check in
1195 * netvsc_linkstatus_callback()
1196 */
1197 case RNDIS_STATUS_MEDIA_CONNECT:
1198 if (rdev->link_state) {
1199 rdev->link_state = false;
1200 netif_carrier_on(net);
1201 netif_tx_wake_all_queues(net);
1202 } else {
1203 notify = true;
Haiyang Zhang3a494e72014-06-19 18:34:36 -07001204 }
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001205 kfree(event);
1206 break;
1207 case RNDIS_STATUS_MEDIA_DISCONNECT:
1208 if (!rdev->link_state) {
1209 rdev->link_state = true;
1210 netif_carrier_off(net);
1211 netif_tx_stop_all_queues(net);
1212 }
1213 kfree(event);
1214 break;
1215 case RNDIS_STATUS_NETWORK_CHANGE:
1216 /* Only makes sense if carrier is present */
1217 if (!rdev->link_state) {
1218 rdev->link_state = true;
1219 netif_carrier_off(net);
1220 netif_tx_stop_all_queues(net);
1221 event->event = RNDIS_STATUS_MEDIA_CONNECT;
1222 spin_lock_irqsave(&ndev_ctx->lock, flags);
Haiyang Zhang15cfd402016-04-21 16:13:01 -07001223 list_add(&event->list, &ndev_ctx->reconfig_events);
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001224 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1225 reschedule = true;
1226 }
1227 break;
Haiyang Zhang891de742014-02-12 16:54:27 -08001228 }
1229
1230 rtnl_unlock();
1231
1232 if (notify)
1233 netdev_notify_peers(net);
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001234
1235 /* link_watch only sends one notification with current state per
1236 * second, handle next reconfig event in 2 seconds.
1237 */
1238 if (reschedule)
1239 schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001240
1241 return;
1242
1243out_unlock:
1244 rtnl_unlock();
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001245}
1246
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001247static void netvsc_free_netdev(struct net_device *netdev)
1248{
1249 struct net_device_context *net_device_ctx = netdev_priv(netdev);
1250
1251 free_percpu(net_device_ctx->tx_stats);
1252 free_percpu(net_device_ctx->rx_stats);
1253 free_netdev(netdev);
1254}
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001255
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001256static struct net_device *get_netvsc_bymac(const u8 *mac)
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001257{
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001258 struct net_device *dev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001259
Stephen Hemminger8737caa2016-08-23 12:17:44 -07001260 ASSERT_RTNL();
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001261
1262 for_each_netdev(&init_net, dev) {
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001263 if (dev->netdev_ops != &device_ops)
1264 continue; /* not a netvsc device */
1265
1266 if (ether_addr_equal(mac, dev->perm_addr))
1267 return dev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001268 }
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001269
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001270 return NULL;
1271}
1272
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001273static struct net_device *get_netvsc_byref(struct net_device *vf_netdev)
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001274{
1275 struct net_device *dev;
1276
1277 ASSERT_RTNL();
1278
1279 for_each_netdev(&init_net, dev) {
1280 struct net_device_context *net_device_ctx;
1281
1282 if (dev->netdev_ops != &device_ops)
1283 continue; /* not a netvsc device */
1284
1285 net_device_ctx = netdev_priv(dev);
1286 if (net_device_ctx->nvdev == NULL)
1287 continue; /* device is removed */
1288
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001289 if (rtnl_dereference(net_device_ctx->vf_netdev) == vf_netdev)
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001290 return dev; /* a match */
1291 }
1292
1293 return NULL;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001294}
1295
1296static int netvsc_register_vf(struct net_device *vf_netdev)
1297{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001298 struct net_device *ndev;
1299 struct net_device_context *net_device_ctx;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001300 struct netvsc_device *netvsc_dev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001301
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001302 if (vf_netdev->addr_len != ETH_ALEN)
1303 return NOTIFY_DONE;
1304
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001305 /*
1306 * We will use the MAC address to locate the synthetic interface to
1307 * associate with the VF interface. If we don't find a matching
1308 * synthetic interface, move on.
1309 */
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001310 ndev = get_netvsc_bymac(vf_netdev->perm_addr);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001311 if (!ndev)
1312 return NOTIFY_DONE;
1313
1314 net_device_ctx = netdev_priv(ndev);
1315 netvsc_dev = net_device_ctx->nvdev;
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001316 if (!netvsc_dev || rtnl_dereference(net_device_ctx->vf_netdev))
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001317 return NOTIFY_DONE;
1318
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001319 netdev_info(ndev, "VF registering: %s\n", vf_netdev->name);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001320 /*
1321 * Take a reference on the module.
1322 */
1323 try_module_get(THIS_MODULE);
Stephen Hemminger07d0f002016-09-22 16:56:30 -07001324
1325 dev_hold(vf_netdev);
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001326 rcu_assign_pointer(net_device_ctx->vf_netdev, vf_netdev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001327 return NOTIFY_OK;
1328}
1329
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001330static int netvsc_vf_up(struct net_device *vf_netdev)
1331{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001332 struct net_device *ndev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001333 struct netvsc_device *netvsc_dev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001334 struct net_device_context *net_device_ctx;
1335
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001336 ndev = get_netvsc_byref(vf_netdev);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001337 if (!ndev)
1338 return NOTIFY_DONE;
1339
1340 net_device_ctx = netdev_priv(ndev);
1341 netvsc_dev = net_device_ctx->nvdev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001342
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001343 netdev_info(ndev, "VF up: %s\n", vf_netdev->name);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001344
1345 /*
1346 * Open the device before switching data path.
1347 */
Vitaly Kuznetsov2f5fa6c2016-06-03 17:51:00 +02001348 rndis_filter_open(netvsc_dev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001349
1350 /*
1351 * notify the host to switch the data path.
1352 */
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001353 netvsc_switch_datapath(ndev, true);
1354 netdev_info(ndev, "Data path switched to VF: %s\n", vf_netdev->name);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001355
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001356 netif_carrier_off(ndev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001357
Vitaly Kuznetsovd0722182016-08-15 17:48:40 +02001358 /* Now notify peers through VF device. */
1359 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, vf_netdev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001360
1361 return NOTIFY_OK;
1362}
1363
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001364static int netvsc_vf_down(struct net_device *vf_netdev)
1365{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001366 struct net_device *ndev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001367 struct netvsc_device *netvsc_dev;
1368 struct net_device_context *net_device_ctx;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001369
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001370 ndev = get_netvsc_byref(vf_netdev);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001371 if (!ndev)
1372 return NOTIFY_DONE;
1373
1374 net_device_ctx = netdev_priv(ndev);
1375 netvsc_dev = net_device_ctx->nvdev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001376
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001377 netdev_info(ndev, "VF down: %s\n", vf_netdev->name);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001378 netvsc_switch_datapath(ndev, false);
1379 netdev_info(ndev, "Data path switched from VF: %s\n", vf_netdev->name);
Vitaly Kuznetsov2f5fa6c2016-06-03 17:51:00 +02001380 rndis_filter_close(netvsc_dev);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001381 netif_carrier_on(ndev);
Vitaly Kuznetsovd0722182016-08-15 17:48:40 +02001382
1383 /* Now notify peers through netvsc device. */
1384 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, ndev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001385
1386 return NOTIFY_OK;
1387}
1388
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001389static int netvsc_unregister_vf(struct net_device *vf_netdev)
1390{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001391 struct net_device *ndev;
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001392 struct net_device_context *net_device_ctx;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001393
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001394 ndev = get_netvsc_byref(vf_netdev);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001395 if (!ndev)
1396 return NOTIFY_DONE;
1397
1398 net_device_ctx = netdev_priv(ndev);
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001399
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001400 netdev_info(ndev, "VF unregistering: %s\n", vf_netdev->name);
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001401
1402 RCU_INIT_POINTER(net_device_ctx->vf_netdev, NULL);
Stephen Hemminger07d0f002016-09-22 16:56:30 -07001403 dev_put(vf_netdev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001404 module_put(THIS_MODULE);
1405 return NOTIFY_OK;
1406}
1407
K. Y. Srinivasan84946892011-09-13 10:59:38 -07001408static int netvsc_probe(struct hv_device *dev,
1409 const struct hv_vmbus_device_id *dev_id)
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001410{
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001411 struct net_device *net = NULL;
1412 struct net_device_context *net_device_ctx;
1413 struct netvsc_device_info device_info;
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001414 struct netvsc_device *nvdev;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001415 int ret;
1416
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001417 net = alloc_etherdev_mq(sizeof(struct net_device_context),
stephen hemminger2b018882017-01-24 13:06:03 -08001418 VRSS_CHANNEL_MAX);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001419 if (!net)
K. Y. Srinivasan51a805d2011-08-25 09:49:11 -07001420 return -ENOMEM;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001421
Haiyang Zhang1b07da52014-03-04 14:11:06 -08001422 netif_carrier_off(net);
1423
Haiyang Zhangb37879e2016-08-04 10:42:14 -07001424 netvsc_init_settings(net);
1425
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001426 net_device_ctx = netdev_priv(net);
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -07001427 net_device_ctx->device_ctx = dev;
Simon Xiao3f300ff2015-04-28 01:05:17 -07001428 net_device_ctx->msg_enable = netif_msg_init(debug, default_msg);
1429 if (netif_msg_probe(net_device_ctx))
1430 netdev_dbg(net, "netvsc msg_enable: %d\n",
1431 net_device_ctx->msg_enable);
1432
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001433 net_device_ctx->tx_stats = netdev_alloc_pcpu_stats(struct netvsc_stats);
1434 if (!net_device_ctx->tx_stats) {
1435 free_netdev(net);
1436 return -ENOMEM;
1437 }
1438 net_device_ctx->rx_stats = netdev_alloc_pcpu_stats(struct netvsc_stats);
1439 if (!net_device_ctx->rx_stats) {
1440 free_percpu(net_device_ctx->tx_stats);
1441 free_netdev(net);
1442 return -ENOMEM;
1443 }
1444
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -07001445 hv_set_drvdata(dev, net);
Vitaly Kuznetsovf580aec2016-05-13 13:55:20 +02001446
1447 net_device_ctx->start_remove = false;
1448
Haiyang Zhang891de742014-02-12 16:54:27 -08001449 INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change);
Wenqi Ma792df872012-04-19 00:39:37 +00001450 INIT_WORK(&net_device_ctx->work, do_set_multicast);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001451
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001452 spin_lock_init(&net_device_ctx->lock);
1453 INIT_LIST_HEAD(&net_device_ctx->reconfig_events);
1454
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001455 net->netdev_ops = &device_ops;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001456 net->ethtool_ops = &ethtool_ops;
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -07001457 SET_NETDEV_DEV(net, &dev->device);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001458
Vitaly Kuznetsov14a03cf2016-02-05 17:29:08 +01001459 /* We always need headroom for rndis header */
1460 net->needed_headroom = RNDIS_AND_PPI_SIZE;
1461
Haiyang Zhang692e0842011-09-01 12:19:43 -07001462 /* Notify the netvsc driver of the new device */
Andrew Schwartzmeyer8ebdcc52015-08-11 17:14:31 -07001463 memset(&device_info, 0, sizeof(device_info));
Haiyang Zhang692e0842011-09-01 12:19:43 -07001464 device_info.ring_size = ring_size;
stephen hemminger2b018882017-01-24 13:06:03 -08001465 device_info.max_num_vrss_chns = min_t(u32, VRSS_CHANNEL_DEFAULT,
1466 num_online_cpus());
Haiyang Zhang692e0842011-09-01 12:19:43 -07001467 ret = rndis_filter_device_add(dev, &device_info);
1468 if (ret != 0) {
1469 netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001470 netvsc_free_netdev(net);
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -07001471 hv_set_drvdata(dev, NULL);
Haiyang Zhang692e0842011-09-01 12:19:43 -07001472 return ret;
1473 }
1474 memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
1475
stephen hemminger23312a32017-01-24 13:05:59 -08001476 /* hw_features computed in rndis_filter_device_add */
1477 net->features = net->hw_features |
1478 NETIF_F_HIGHDMA | NETIF_F_SG |
1479 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
1480 net->vlan_features = net->features;
1481
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +02001482 nvdev = net_device_ctx->nvdev;
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001483 netif_set_real_num_tx_queues(net, nvdev->num_chn);
1484 netif_set_real_num_rx_queues(net, nvdev->num_chn);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001485
Jarod Wilsond0c2c992016-10-20 13:55:21 -04001486 /* MTU range: 68 - 1500 or 65521 */
1487 net->min_mtu = NETVSC_MTU_MIN;
1488 if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
1489 net->max_mtu = NETVSC_MTU - ETH_HLEN;
1490 else
1491 net->max_mtu = ETH_DATA_LEN;
1492
Haiyang Zhanga68f9612013-12-20 16:52:31 -08001493 ret = register_netdev(net);
1494 if (ret != 0) {
1495 pr_err("Unable to register netdev.\n");
stephen hemminger2289f0a2017-01-24 13:06:10 -08001496 rndis_filter_device_remove(dev, nvdev);
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001497 netvsc_free_netdev(net);
Haiyang Zhanga68f9612013-12-20 16:52:31 -08001498 }
1499
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001500 return ret;
1501}
1502
K. Y. Srinivasan415b0232011-04-29 13:45:12 -07001503static int netvsc_remove(struct hv_device *dev)
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001504{
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -07001505 struct net_device *net;
Haiyang Zhang122a5f62011-05-27 06:21:55 -07001506 struct net_device_context *ndev_ctx;
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -07001507
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +02001508 net = hv_get_drvdata(dev);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001509
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001510 if (net == NULL) {
K. Y. Srinivasan415b0232011-04-29 13:45:12 -07001511 dev_err(&dev->device, "No net device to remove\n");
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001512 return 0;
1513 }
1514
Haiyang Zhang122a5f62011-05-27 06:21:55 -07001515 ndev_ctx = netdev_priv(net);
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +02001516
Vitaly Kuznetsov6da72252016-05-13 13:55:24 +02001517 /* Avoid racing with netvsc_change_mtu()/netvsc_set_channels()
1518 * removing the device.
1519 */
1520 rtnl_lock();
Vitaly Kuznetsovf580aec2016-05-13 13:55:20 +02001521 ndev_ctx->start_remove = true;
Vitaly Kuznetsov6da72252016-05-13 13:55:24 +02001522 rtnl_unlock();
Vitaly Kuznetsovf580aec2016-05-13 13:55:20 +02001523
Haiyang Zhang122a5f62011-05-27 06:21:55 -07001524 cancel_delayed_work_sync(&ndev_ctx->dwork);
Wenqi Ma792df872012-04-19 00:39:37 +00001525 cancel_work_sync(&ndev_ctx->work);
Haiyang Zhang122a5f62011-05-27 06:21:55 -07001526
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001527 /* Stop outbound asap */
Haiyang Zhang0a282532012-02-02 07:17:59 +00001528 netif_tx_disable(net);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001529
1530 unregister_netdev(net);
1531
1532 /*
1533 * Call to the vsc driver to let it know that the device is being
1534 * removed
1535 */
stephen hemminger2289f0a2017-01-24 13:06:10 -08001536 rndis_filter_device_remove(dev, ndev_ctx->nvdev);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001537
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +02001538 hv_set_drvdata(dev, NULL);
1539
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001540 netvsc_free_netdev(net);
Haiyang Zhangdf06bcf2011-05-23 09:03:47 -07001541 return 0;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001542}
1543
K. Y. Srinivasan345c4cc2011-08-25 09:48:34 -07001544static const struct hv_vmbus_device_id id_table[] = {
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07001545 /* Network guid */
K. Y. Srinivasan8f505942013-01-23 17:42:42 -08001546 { HV_NIC_GUID, },
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07001547 { },
K. Y. Srinivasan345c4cc2011-08-25 09:48:34 -07001548};
1549
1550MODULE_DEVICE_TABLE(vmbus, id_table);
1551
K. Y. Srinivasanf1542a62011-05-10 07:55:16 -07001552/* The one and only one */
K. Y. Srinivasanfde0ef92011-05-12 19:35:08 -07001553static struct hv_driver netvsc_drv = {
Haiyang Zhangd31b20f2012-03-07 10:02:00 +00001554 .name = KBUILD_MODNAME,
K. Y. Srinivasan345c4cc2011-08-25 09:48:34 -07001555 .id_table = id_table,
K. Y. Srinivasanfde0ef92011-05-12 19:35:08 -07001556 .probe = netvsc_probe,
1557 .remove = netvsc_remove,
K. Y. Srinivasand4890972011-05-10 07:55:17 -07001558};
K. Y. Srinivasanf1542a62011-05-10 07:55:16 -07001559
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001560/*
1561 * On Hyper-V, every VF interface is matched with a corresponding
1562 * synthetic interface. The synthetic interface is presented first
1563 * to the guest. When the corresponding VF instance is registered,
1564 * we will take care of switching the data path.
1565 */
1566static int netvsc_netdev_event(struct notifier_block *this,
1567 unsigned long event, void *ptr)
1568{
1569 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
1570
Stephen Hemmingeree837a12016-09-22 16:56:31 -07001571 /* Skip our own events */
1572 if (event_dev->netdev_ops == &device_ops)
1573 return NOTIFY_DONE;
1574
1575 /* Avoid non-Ethernet type devices */
1576 if (event_dev->type != ARPHRD_ETHER)
1577 return NOTIFY_DONE;
1578
Vitaly Kuznetsov0dbff142016-08-15 17:48:43 +02001579 /* Avoid Vlan dev with same MAC registering as VF */
1580 if (event_dev->priv_flags & IFF_802_1Q_VLAN)
1581 return NOTIFY_DONE;
1582
1583 /* Avoid Bonding master dev with same MAC registering as VF */
Stephen Hemmingeree837a12016-09-22 16:56:31 -07001584 if ((event_dev->priv_flags & IFF_BONDING) &&
1585 (event_dev->flags & IFF_MASTER))
Haiyang Zhangcb2911f2016-06-02 12:02:04 -07001586 return NOTIFY_DONE;
1587
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001588 switch (event) {
1589 case NETDEV_REGISTER:
1590 return netvsc_register_vf(event_dev);
1591 case NETDEV_UNREGISTER:
1592 return netvsc_unregister_vf(event_dev);
1593 case NETDEV_UP:
1594 return netvsc_vf_up(event_dev);
1595 case NETDEV_DOWN:
1596 return netvsc_vf_down(event_dev);
1597 default:
1598 return NOTIFY_DONE;
1599 }
1600}
1601
1602static struct notifier_block netvsc_netdev_notifier = {
1603 .notifier_call = netvsc_netdev_event,
1604};
1605
K. Y. Srinivasana9869c92011-05-12 19:35:17 -07001606static void __exit netvsc_drv_exit(void)
Hank Janssenfceaf242009-07-13 15:34:54 -07001607{
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001608 unregister_netdevice_notifier(&netvsc_netdev_notifier);
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001609 vmbus_driver_unregister(&netvsc_drv);
Hank Janssenfceaf242009-07-13 15:34:54 -07001610}
1611
K. Y. Srinivasan1fde28c2011-05-12 19:35:16 -07001612static int __init netvsc_drv_init(void)
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001613{
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001614 int ret;
1615
Haiyang Zhangfa85a6c2012-07-25 08:08:41 +00001616 if (ring_size < RING_SIZE_MIN) {
1617 ring_size = RING_SIZE_MIN;
1618 pr_info("Increased ring_size to %d (min allowed)\n",
1619 ring_size);
1620 }
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001621 ret = vmbus_driver_register(&netvsc_drv);
1622
1623 if (ret)
1624 return ret;
1625
1626 register_netdevice_notifier(&netvsc_netdev_notifier);
1627 return 0;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001628}
1629
Hank Janssen26c14cc2010-02-11 23:02:42 +00001630MODULE_LICENSE("GPL");
Stephen Hemminger7880fc52010-05-04 09:58:52 -07001631MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
Hank Janssenfceaf242009-07-13 15:34:54 -07001632
K. Y. Srinivasan1fde28c2011-05-12 19:35:16 -07001633module_init(netvsc_drv_init);
K. Y. Srinivasana9869c92011-05-12 19:35:17 -07001634module_exit(netvsc_drv_exit);