blob: 2f9de2e9f38e93464676fae4c12ac8f81dca2ad5 [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);
stephen hemminger7ce10122017-03-09 14:58:29 -0800209 unsigned int num_tx_queues = ndev->real_num_tx_queues;
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 hemminger7ce10122017-03-09 14:58:29 -0800213 if (q_idx < 0 || skb->ooo_okay || q_idx >= num_tx_queues) {
stephen hemmingerd8e18ee2017-01-24 13:06:05 -0800214 u16 hash = __skb_tx_hash(ndev, skb, VRSS_SEND_TAB_SIZE);
215 int new_idx;
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700216
stephen hemminger7ce10122017-03-09 14:58:29 -0800217 new_idx = net_device_ctx->tx_send_table[hash] % num_tx_queues;
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700218
stephen hemmingerd8e18ee2017-01-24 13:06:05 -0800219 if (q_idx != new_idx && sk &&
220 sk_fullsock(sk) && rcu_access_pointer(sk->sk_dst_cache))
221 sk_tx_queue_set(sk, new_idx);
222
223 q_idx = new_idx;
224 }
225
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700226 return q_idx;
227}
228
KY Srinivasan54a73572014-03-08 19:23:13 -0800229static u32 fill_pg_buf(struct page *page, u32 offset, u32 len,
230 struct hv_page_buffer *pb)
231{
232 int j = 0;
233
234 /* Deal with compund pages by ignoring unused part
235 * of the page.
236 */
237 page += (offset >> PAGE_SHIFT);
238 offset &= ~PAGE_MASK;
239
240 while (len > 0) {
241 unsigned long bytes;
242
243 bytes = PAGE_SIZE - offset;
244 if (bytes > len)
245 bytes = len;
246 pb[j].pfn = page_to_pfn(page);
247 pb[j].offset = offset;
248 pb[j].len = bytes;
249
250 offset += bytes;
251 len -= bytes;
252
253 if (offset == PAGE_SIZE && len) {
254 page++;
255 offset = 0;
256 j++;
257 }
258 }
259
260 return j + 1;
261}
262
KY Srinivasan8a002512014-03-08 19:23:14 -0800263static u32 init_page_array(void *hdr, u32 len, struct sk_buff *skb,
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800264 struct hv_netvsc_packet *packet,
265 struct hv_page_buffer **page_buf)
KY Srinivasan54a73572014-03-08 19:23:13 -0800266{
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800267 struct hv_page_buffer *pb = *page_buf;
KY Srinivasan54a73572014-03-08 19:23:13 -0800268 u32 slots_used = 0;
269 char *data = skb->data;
270 int frags = skb_shinfo(skb)->nr_frags;
271 int i;
272
273 /* The packet is laid out thus:
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700274 * 1. hdr: RNDIS header and PPI
KY Srinivasan54a73572014-03-08 19:23:13 -0800275 * 2. skb linear data
276 * 3. skb fragment data
277 */
278 if (hdr != NULL)
279 slots_used += fill_pg_buf(virt_to_page(hdr),
280 offset_in_page(hdr),
281 len, &pb[slots_used]);
282
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700283 packet->rmsg_size = len;
284 packet->rmsg_pgcnt = slots_used;
285
KY Srinivasan54a73572014-03-08 19:23:13 -0800286 slots_used += fill_pg_buf(virt_to_page(data),
287 offset_in_page(data),
288 skb_headlen(skb), &pb[slots_used]);
289
290 for (i = 0; i < frags; i++) {
291 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
292
293 slots_used += fill_pg_buf(skb_frag_page(frag),
294 frag->page_offset,
295 skb_frag_size(frag), &pb[slots_used]);
296 }
KY Srinivasan8a002512014-03-08 19:23:14 -0800297 return slots_used;
KY Srinivasan54a73572014-03-08 19:23:13 -0800298}
299
300static int count_skb_frag_slots(struct sk_buff *skb)
301{
302 int i, frags = skb_shinfo(skb)->nr_frags;
303 int pages = 0;
304
305 for (i = 0; i < frags; i++) {
306 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
307 unsigned long size = skb_frag_size(frag);
308 unsigned long offset = frag->page_offset;
309
310 /* Skip unused frames from start of page */
311 offset &= ~PAGE_MASK;
312 pages += PFN_UP(offset + size);
313 }
314 return pages;
315}
316
317static int netvsc_get_slots(struct sk_buff *skb)
318{
319 char *data = skb->data;
320 unsigned int offset = offset_in_page(data);
321 unsigned int len = skb_headlen(skb);
322 int slots;
323 int frag_slots;
324
325 slots = DIV_ROUND_UP(offset + len, PAGE_SIZE);
326 frag_slots = count_skb_frag_slots(skb);
327 return slots + frag_slots;
328}
329
stephen hemminger23312a32017-01-24 13:05:59 -0800330static u32 net_checksum_info(struct sk_buff *skb)
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800331{
stephen hemminger23312a32017-01-24 13:05:59 -0800332 if (skb->protocol == htons(ETH_P_IP)) {
333 struct iphdr *ip = ip_hdr(skb);
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800334
stephen hemminger23312a32017-01-24 13:05:59 -0800335 if (ip->protocol == IPPROTO_TCP)
336 return TRANSPORT_INFO_IPV4_TCP;
337 else if (ip->protocol == IPPROTO_UDP)
338 return TRANSPORT_INFO_IPV4_UDP;
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800339 } else {
stephen hemminger23312a32017-01-24 13:05:59 -0800340 struct ipv6hdr *ip6 = ipv6_hdr(skb);
341
342 if (ip6->nexthdr == IPPROTO_TCP)
343 return TRANSPORT_INFO_IPV6_TCP;
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800344 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
stephen hemminger23312a32017-01-24 13:05:59 -0800345 return TRANSPORT_INFO_IPV6_UDP;
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800346 }
347
stephen hemminger23312a32017-01-24 13:05:59 -0800348 return TRANSPORT_INFO_NOT_IP;
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800349}
350
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700351static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
Hank Janssenfceaf242009-07-13 15:34:54 -0700352{
Hank Janssenfceaf242009-07-13 15:34:54 -0700353 struct net_device_context *net_device_ctx = netdev_priv(net);
Vitaly Kuznetsov981a1bd2015-04-08 17:54:05 +0200354 struct hv_netvsc_packet *packet = NULL;
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700355 int ret;
KY Srinivasan8a002512014-03-08 19:23:14 -0800356 unsigned int num_data_pgs;
357 struct rndis_message *rndis_msg;
358 struct rndis_packet *rndis_pkt;
359 u32 rndis_msg_size;
KY Srinivasan8a002512014-03-08 19:23:14 -0800360 struct rndis_per_packet_info *ppi;
Haiyang Zhang307f0992014-05-21 12:55:39 -0700361 u32 hash;
KY Srinivasanb08cc792015-03-29 21:08:42 -0700362 struct hv_page_buffer page_buf[MAX_PAGE_BUFFER_COUNT];
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800363 struct hv_page_buffer *pb = page_buf;
Hank Janssenfceaf242009-07-13 15:34:54 -0700364
KY Srinivasan54a73572014-03-08 19:23:13 -0800365 /* We will atmost need two pages to describe the rndis
366 * header. We can only transmit MAX_PAGE_BUFFER_COUNT number
Vitaly Kuznetsove88f7e02015-04-08 17:54:06 +0200367 * of pages in a single packet. If skb is scattered around
368 * more pages we try linearizing it.
KY Srinivasan54a73572014-03-08 19:23:13 -0800369 */
Vitaly Kuznetsove88f7e02015-04-08 17:54:06 +0200370
KY Srinivasan8a002512014-03-08 19:23:14 -0800371 num_data_pgs = netvsc_get_slots(skb) + 2;
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700372
373 if (unlikely(num_data_pgs > MAX_PAGE_BUFFER_COUNT)) {
Stephen Hemminger4323b472016-08-23 12:17:57 -0700374 ++net_device_ctx->eth_stats.tx_scattered;
375
376 if (skb_linearize(skb))
377 goto no_memory;
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700378
379 num_data_pgs = netvsc_get_slots(skb) + 2;
380 if (num_data_pgs > MAX_PAGE_BUFFER_COUNT) {
Stephen Hemminger4323b472016-08-23 12:17:57 -0700381 ++net_device_ctx->eth_stats.tx_too_big;
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700382 goto drop;
383 }
KY Srinivasan54a73572014-03-08 19:23:13 -0800384 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700385
KY Srinivasanc0eb4542015-12-01 16:43:10 -0800386 /*
387 * Place the rndis header in the skb head room and
388 * the skb->cb will be used for hv_netvsc_packet
389 * structure.
390 */
391 ret = skb_cow_head(skb, RNDIS_AND_PPI_SIZE);
Stephen Hemminger4323b472016-08-23 12:17:57 -0700392 if (ret)
393 goto no_memory;
394
KY Srinivasanc0eb4542015-12-01 16:43:10 -0800395 /* Use the skb control buffer for building up the packet */
396 BUILD_BUG_ON(sizeof(struct hv_netvsc_packet) >
397 FIELD_SIZEOF(struct sk_buff, cb));
398 packet = (struct hv_netvsc_packet *)skb->cb;
Hank Janssenfceaf242009-07-13 15:34:54 -0700399
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700400 packet->q_idx = skb_get_queue_mapping(skb);
401
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800402 packet->total_data_buflen = skb->len;
stephen hemminger793e3952017-01-24 13:06:12 -0800403 packet->total_bytes = skb->len;
404 packet->total_packets = 1;
Hank Janssenfceaf242009-07-13 15:34:54 -0700405
KY Srinivasanc0eb4542015-12-01 16:43:10 -0800406 rndis_msg = (struct rndis_message *)skb->head;
KY Srinivasanb08cc792015-03-29 21:08:42 -0700407
KY Srinivasan24476762015-12-01 16:43:06 -0800408 memset(rndis_msg, 0, RNDIS_AND_PPI_SIZE);
Hank Janssenfceaf242009-07-13 15:34:54 -0700409
KY Srinivasan8a002512014-03-08 19:23:14 -0800410 /* Add the rndis header */
KY Srinivasan8a002512014-03-08 19:23:14 -0800411 rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET;
412 rndis_msg->msg_len = packet->total_data_buflen;
413 rndis_pkt = &rndis_msg->msg.pkt;
414 rndis_pkt->data_offset = sizeof(struct rndis_packet);
415 rndis_pkt->data_len = packet->total_data_buflen;
416 rndis_pkt->per_pkt_info_offset = sizeof(struct rndis_packet);
417
418 rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet);
419
Haiyang Zhang307f0992014-05-21 12:55:39 -0700420 hash = skb_get_hash_raw(skb);
421 if (hash != 0 && net->real_num_tx_queues > 1) {
422 rndis_msg_size += NDIS_HASH_PPI_SIZE;
423 ppi = init_ppi_data(rndis_msg, NDIS_HASH_PPI_SIZE,
424 NBL_HASH_VALUE);
425 *(u32 *)((void *)ppi + ppi->ppi_offset) = hash;
426 }
427
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700428 if (skb_vlan_tag_present(skb)) {
KY Srinivasan8a002512014-03-08 19:23:14 -0800429 struct ndis_pkt_8021q_info *vlan;
430
431 rndis_msg_size += NDIS_VLAN_PPI_SIZE;
432 ppi = init_ppi_data(rndis_msg, NDIS_VLAN_PPI_SIZE,
433 IEEE_8021Q_INFO);
434 vlan = (struct ndis_pkt_8021q_info *)((void *)ppi +
435 ppi->ppi_offset);
KY Srinivasan760d1e32015-12-01 16:43:19 -0800436 vlan->vlanid = skb->vlan_tci & VLAN_VID_MASK;
437 vlan->pri = (skb->vlan_tci & VLAN_PRIO_MASK) >>
KY Srinivasan8a002512014-03-08 19:23:14 -0800438 VLAN_PRIO_SHIFT;
439 }
440
stephen hemminger23312a32017-01-24 13:05:59 -0800441 if (skb_is_gso(skb)) {
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700442 struct ndis_tcp_lso_info *lso_info;
443
444 rndis_msg_size += NDIS_LSO_PPI_SIZE;
445 ppi = init_ppi_data(rndis_msg, NDIS_LSO_PPI_SIZE,
446 TCP_LARGESEND_PKTINFO);
447
448 lso_info = (struct ndis_tcp_lso_info *)((void *)ppi +
449 ppi->ppi_offset);
450
451 lso_info->lso_v2_transmit.type = NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
stephen hemminger23312a32017-01-24 13:05:59 -0800452 if (skb->protocol == htons(ETH_P_IP)) {
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700453 lso_info->lso_v2_transmit.ip_version =
454 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
455 ip_hdr(skb)->tot_len = 0;
456 ip_hdr(skb)->check = 0;
457 tcp_hdr(skb)->check =
458 ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
459 ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
460 } else {
461 lso_info->lso_v2_transmit.ip_version =
462 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
463 ipv6_hdr(skb)->payload_len = 0;
464 tcp_hdr(skb)->check =
465 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
466 &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
467 }
stephen hemminger23312a32017-01-24 13:05:59 -0800468 lso_info->lso_v2_transmit.tcp_header_offset = skb_transport_offset(skb);
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700469 lso_info->lso_v2_transmit.mss = skb_shinfo(skb)->gso_size;
stephen hemmingerad19bc82016-10-11 14:03:07 -0700470 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
stephen hemminger23312a32017-01-24 13:05:59 -0800471 if (net_checksum_info(skb) & net_device_ctx->tx_checksum_mask) {
472 struct ndis_tcp_ip_checksum_info *csum_info;
473
stephen hemmingerad19bc82016-10-11 14:03:07 -0700474 rndis_msg_size += NDIS_CSUM_PPI_SIZE;
475 ppi = init_ppi_data(rndis_msg, NDIS_CSUM_PPI_SIZE,
476 TCPIP_CHKSUM_PKTINFO);
477
478 csum_info = (struct ndis_tcp_ip_checksum_info *)((void *)ppi +
479 ppi->ppi_offset);
480
stephen hemminger23312a32017-01-24 13:05:59 -0800481 csum_info->transmit.tcp_header_offset = skb_transport_offset(skb);
482
483 if (skb->protocol == htons(ETH_P_IP)) {
stephen hemmingerad19bc82016-10-11 14:03:07 -0700484 csum_info->transmit.is_ipv4 = 1;
stephen hemminger23312a32017-01-24 13:05:59 -0800485
486 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
487 csum_info->transmit.tcp_checksum = 1;
488 else
489 csum_info->transmit.udp_checksum = 1;
490 } else {
stephen hemmingerad19bc82016-10-11 14:03:07 -0700491 csum_info->transmit.is_ipv6 = 1;
492
stephen hemminger23312a32017-01-24 13:05:59 -0800493 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
494 csum_info->transmit.tcp_checksum = 1;
495 else
496 csum_info->transmit.udp_checksum = 1;
497 }
stephen hemmingerad19bc82016-10-11 14:03:07 -0700498 } else {
stephen hemminger23312a32017-01-24 13:05:59 -0800499 /* Can't do offload of this type of checksum */
stephen hemmingerad19bc82016-10-11 14:03:07 -0700500 if (skb_checksum_help(skb))
501 goto drop;
502 }
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700503 }
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800504
KY Srinivasan8a002512014-03-08 19:23:14 -0800505 /* Start filling in the page buffers with the rndis hdr */
506 rndis_msg->msg_len += rndis_msg_size;
Haiyang Zhang942396b2014-10-22 13:47:18 -0700507 packet->total_data_buflen = rndis_msg->msg_len;
KY Srinivasan8a002512014-03-08 19:23:14 -0800508 packet->page_buf_cnt = init_page_array(rndis_msg, rndis_msg_size,
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800509 skb, packet, &pb);
KY Srinivasan8a002512014-03-08 19:23:14 -0800510
sixiao@microsoft.com76d13b52016-02-17 16:43:59 -0800511 /* timestamp packet in software */
512 skb_tx_timestamp(skb);
KY Srinivasan3a3d9a02015-12-01 16:43:14 -0800513 ret = netvsc_send(net_device_ctx->device_ctx, packet,
514 rndis_msg, &pb, skb);
stephen hemminger793e3952017-01-24 13:06:12 -0800515 if (likely(ret == 0))
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700516 return NETDEV_TX_OK;
Stephen Hemminger4323b472016-08-23 12:17:57 -0700517
518 if (ret == -EAGAIN) {
519 ++net_device_ctx->eth_stats.tx_busy;
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700520 return NETDEV_TX_BUSY;
Stephen Hemminger4323b472016-08-23 12:17:57 -0700521 }
522
523 if (ret == -ENOSPC)
524 ++net_device_ctx->eth_stats.tx_no_space;
Hank Janssenfceaf242009-07-13 15:34:54 -0700525
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700526drop:
527 dev_kfree_skb_any(skb);
528 net->stats.tx_dropped++;
529
530 return NETDEV_TX_OK;
Stephen Hemminger4323b472016-08-23 12:17:57 -0700531
532no_memory:
533 ++net_device_ctx->eth_stats.tx_no_memory;
534 goto drop;
Hank Janssenfceaf242009-07-13 15:34:54 -0700535}
Hank Janssen3e189512010-03-04 22:11:00 +0000536/*
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700537 * netvsc_linkstatus_callback - Link up/down notification
538 */
K. Y. Srinivasan90ef1172011-05-12 19:34:50 -0700539void netvsc_linkstatus_callback(struct hv_device *device_obj,
Haiyang Zhang3a494e72014-06-19 18:34:36 -0700540 struct rndis_message *resp)
Hank Janssenfceaf242009-07-13 15:34:54 -0700541{
Haiyang Zhang3a494e72014-06-19 18:34:36 -0700542 struct rndis_indicate_status *indicate = &resp->msg.indicate_status;
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -0700543 struct net_device *net;
Haiyang Zhangc996edc2011-04-06 15:18:00 -0700544 struct net_device_context *ndev_ctx;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +0100545 struct netvsc_reconfig *event;
546 unsigned long flags;
547
Haiyang Zhang7f5d5af2016-08-04 10:42:15 -0700548 net = hv_get_drvdata(device_obj);
549
550 if (!net)
551 return;
552
553 ndev_ctx = netdev_priv(net);
554
555 /* Update the physical link speed when changing to another vSwitch */
556 if (indicate->status == RNDIS_STATUS_LINK_SPEED_CHANGE) {
557 u32 speed;
558
559 speed = *(u32 *)((void *)indicate + indicate->
560 status_buf_offset) / 10000;
561 ndev_ctx->speed = speed;
562 return;
563 }
564
565 /* Handle these link change statuses below */
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +0100566 if (indicate->status != RNDIS_STATUS_NETWORK_CHANGE &&
567 indicate->status != RNDIS_STATUS_MEDIA_CONNECT &&
568 indicate->status != RNDIS_STATUS_MEDIA_DISCONNECT)
569 return;
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -0700570
Haiyang Zhang7f5d5af2016-08-04 10:42:15 -0700571 if (net->reg_state != NETREG_REGISTERED)
Hank Janssenfceaf242009-07-13 15:34:54 -0700572 return;
Hank Janssenfceaf242009-07-13 15:34:54 -0700573
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +0100574 event = kzalloc(sizeof(*event), GFP_ATOMIC);
575 if (!event)
576 return;
577 event->event = indicate->status;
578
579 spin_lock_irqsave(&ndev_ctx->lock, flags);
580 list_add_tail(&event->list, &ndev_ctx->reconfig_events);
581 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
582
583 schedule_delayed_work(&ndev_ctx->dwork, 0);
Hank Janssenfceaf242009-07-13 15:34:54 -0700584}
585
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700586static struct sk_buff *netvsc_alloc_recv_skb(struct net_device *net,
stephen hemmingere91e7dd2017-02-27 10:26:51 -0800587 struct napi_struct *napi,
stephen hemmingerdc54a082017-01-24 13:06:08 -0800588 const struct ndis_tcp_ip_checksum_info *csum_info,
589 const struct ndis_pkt_8021q_info *vlan,
590 void *data, u32 buflen)
Hank Janssenfceaf242009-07-13 15:34:54 -0700591{
Hank Janssenfceaf242009-07-13 15:34:54 -0700592 struct sk_buff *skb;
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -0700593
stephen hemmingere91e7dd2017-02-27 10:26:51 -0800594 skb = napi_alloc_skb(napi, buflen);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700595 if (!skb)
596 return skb;
Hank Janssenfceaf242009-07-13 15:34:54 -0700597
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700598 /*
599 * Copy to skb. This copy is needed here since the memory pointed by
600 * hv_netvsc_packet cannot be deallocated
601 */
stephen hemmingerdc54a082017-01-24 13:06:08 -0800602 memcpy(skb_put(skb, buflen), data, buflen);
Hank Janssenfceaf242009-07-13 15:34:54 -0700603
604 skb->protocol = eth_type_trans(skb, net);
Stephen Hemmingere52fed72016-10-23 21:32:47 -0700605
606 /* skb is already created with CHECKSUM_NONE */
607 skb_checksum_none_assert(skb);
608
609 /*
610 * In Linux, the IP checksum is always checked.
611 * Do L4 checksum offload if enabled and present.
612 */
613 if (csum_info && (net->features & NETIF_F_RXCSUM)) {
614 if (csum_info->receive.tcp_checksum_succeeded ||
615 csum_info->receive.udp_checksum_succeeded)
KY Srinivasane3d605e2014-03-08 19:23:16 -0800616 skb->ip_summed = CHECKSUM_UNNECESSARY;
KY Srinivasane3d605e2014-03-08 19:23:16 -0800617 }
618
stephen hemmingerdc54a082017-01-24 13:06:08 -0800619 if (vlan) {
620 u16 vlan_tci = vlan->vlanid | (vlan->pri << VLAN_PRIO_SHIFT);
621
Haiyang Zhang93725cb2013-06-17 15:36:49 -0700622 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
KY Srinivasan760d1e32015-12-01 16:43:19 -0800623 vlan_tci);
stephen hemmingerdc54a082017-01-24 13:06:08 -0800624 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700625
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700626 return skb;
627}
628
629/*
630 * netvsc_recv_callback - Callback when we receive a packet from the
631 * "wire" on the specified device.
632 */
stephen hemmingerdc54a082017-01-24 13:06:08 -0800633int netvsc_recv_callback(struct net_device *net,
634 struct vmbus_channel *channel,
635 void *data, u32 len,
636 const struct ndis_tcp_ip_checksum_info *csum_info,
637 const struct ndis_pkt_8021q_info *vlan)
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700638{
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200639 struct net_device_context *net_device_ctx = netdev_priv(net);
Simon Xiao6c80f3f2017-01-24 13:06:13 -0800640 struct netvsc_device *net_device = net_device_ctx->nvdev;
stephen hemminger742fe542017-02-27 10:26:50 -0800641 u16 q_idx = channel->offermsg.offer.sub_channel_index;
642 struct netvsc_channel *nvchan = &net_device->chan_table[q_idx];
Stephen Hemmingerf207c102016-09-22 16:56:33 -0700643 struct net_device *vf_netdev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700644 struct sk_buff *skb;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700645 struct netvsc_stats *rx_stats;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700646
Stephen Hemminger9cbcc422016-09-22 16:56:34 -0700647 if (net->reg_state != NETREG_REGISTERED)
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700648 return NVSP_STAT_FAIL;
649
Stephen Hemminger9cbcc422016-09-22 16:56:34 -0700650 /*
651 * If necessary, inject this packet into the VF interface.
652 * On Hyper-V, multicast and brodcast packets are only delivered
653 * to the synthetic interface (after subjecting these to
654 * policy filters on the host). Deliver these via the VF
655 * interface in the guest.
656 */
stephen hemminger0719e722017-01-11 09:16:32 -0800657 rcu_read_lock();
Stephen Hemmingerf207c102016-09-22 16:56:33 -0700658 vf_netdev = rcu_dereference(net_device_ctx->vf_netdev);
Stephen Hemminger9cbcc422016-09-22 16:56:34 -0700659 if (vf_netdev && (vf_netdev->flags & IFF_UP))
660 net = vf_netdev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700661
662 /* Allocate a skb - TODO direct I/O to pages? */
stephen hemmingere91e7dd2017-02-27 10:26:51 -0800663 skb = netvsc_alloc_recv_skb(net, &nvchan->napi,
664 csum_info, vlan, data, len);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700665 if (unlikely(!skb)) {
666 ++net->stats.rx_dropped;
stephen hemminger0719e722017-01-11 09:16:32 -0800667 rcu_read_unlock();
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700668 return NVSP_STAT_FAIL;
669 }
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700670
Stephen Hemminger9cbcc422016-09-22 16:56:34 -0700671 if (net != vf_netdev)
Simon Xiao6c80f3f2017-01-24 13:06:13 -0800672 skb_record_rx_queue(skb, q_idx);
Stephen Hemminger9cbcc422016-09-22 16:56:34 -0700673
674 /*
675 * Even if injecting the packet, record the statistics
676 * on the synthetic device because modifying the VF device
677 * statistics will not work correctly.
678 */
stephen hemminger742fe542017-02-27 10:26:50 -0800679 rx_stats = &nvchan->rx_stats;
sixiao@microsoft.com4b02b582015-05-15 02:33:03 -0700680 u64_stats_update_begin(&rx_stats->syncp);
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700681 rx_stats->packets++;
stephen hemmingerdc54a082017-01-24 13:06:08 -0800682 rx_stats->bytes += len;
Stephen Hemmingerf7ad75b2016-09-22 16:56:35 -0700683
684 if (skb->pkt_type == PACKET_BROADCAST)
685 ++rx_stats->broadcast;
686 else if (skb->pkt_type == PACKET_MULTICAST)
687 ++rx_stats->multicast;
sixiao@microsoft.com4b02b582015-05-15 02:33:03 -0700688 u64_stats_update_end(&rx_stats->syncp);
Stephen Hemminger9495c282010-03-09 17:42:17 -0800689
stephen hemminger742fe542017-02-27 10:26:50 -0800690 napi_gro_receive(&nvchan->napi, skb);
stephen hemminger0719e722017-01-11 09:16:32 -0800691 rcu_read_unlock();
Hank Janssenfceaf242009-07-13 15:34:54 -0700692
Hank Janssenfceaf242009-07-13 15:34:54 -0700693 return 0;
694}
695
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -0700696static void netvsc_get_drvinfo(struct net_device *net,
697 struct ethtool_drvinfo *info)
698{
Jiri Pirko7826d432013-01-06 00:44:26 +0000699 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
Jiri Pirko7826d432013-01-06 00:44:26 +0000700 strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -0700701}
702
Andrew Schwartzmeyer59995372015-02-26 16:27:14 -0800703static void netvsc_get_channels(struct net_device *net,
704 struct ethtool_channels *channel)
705{
706 struct net_device_context *net_device_ctx = netdev_priv(net);
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200707 struct netvsc_device *nvdev = net_device_ctx->nvdev;
Andrew Schwartzmeyer59995372015-02-26 16:27:14 -0800708
709 if (nvdev) {
710 channel->max_combined = nvdev->max_chn;
711 channel->combined_count = nvdev->num_chn;
712 }
713}
714
stephen hemminger2b018882017-01-24 13:06:03 -0800715static int netvsc_set_queues(struct net_device *net, struct hv_device *dev,
716 u32 num_chn)
717{
718 struct netvsc_device_info device_info;
719 int ret;
720
721 memset(&device_info, 0, sizeof(device_info));
722 device_info.num_chn = num_chn;
723 device_info.ring_size = ring_size;
724 device_info.max_num_vrss_chns = num_chn;
725
726 ret = rndis_filter_device_add(dev, &device_info);
727 if (ret)
728 return ret;
729
730 ret = netif_set_real_num_tx_queues(net, num_chn);
731 if (ret)
732 return ret;
733
734 ret = netif_set_real_num_rx_queues(net, num_chn);
735
736 return ret;
737}
738
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700739static int netvsc_set_channels(struct net_device *net,
740 struct ethtool_channels *channels)
741{
742 struct net_device_context *net_device_ctx = netdev_priv(net);
743 struct hv_device *dev = net_device_ctx->device_ctx;
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200744 struct netvsc_device *nvdev = net_device_ctx->nvdev;
stephen hemminger2b018882017-01-24 13:06:03 -0800745 unsigned int count = channels->combined_count;
stephen hemminger163891d2017-03-22 14:50:58 -0700746 bool was_running;
stephen hemminger2b018882017-01-24 13:06:03 -0800747 int ret;
748
749 /* We do not support separate count for rx, tx, or other */
750 if (count == 0 ||
751 channels->rx_count || channels->tx_count || channels->other_count)
752 return -EINVAL;
753
754 if (count > net->num_tx_queues || count > net->num_rx_queues)
755 return -EINVAL;
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700756
Vitaly Kuznetsov6da72252016-05-13 13:55:24 +0200757 if (net_device_ctx->start_remove || !nvdev || nvdev->destroy)
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700758 return -ENODEV;
759
stephen hemminger2b018882017-01-24 13:06:03 -0800760 if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5)
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700761 return -EINVAL;
762
stephen hemminger2b018882017-01-24 13:06:03 -0800763 if (count > nvdev->max_chn)
764 return -EINVAL;
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700765
stephen hemminger163891d2017-03-22 14:50:58 -0700766 was_running = netif_running(net);
767 if (was_running) {
768 ret = netvsc_close(net);
769 if (ret)
770 return ret;
771 }
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700772
Vitaly Kuznetsovf580aec2016-05-13 13:55:20 +0200773 net_device_ctx->start_remove = true;
stephen hemminger2289f0a2017-01-24 13:06:10 -0800774 rndis_filter_device_remove(dev, nvdev);
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700775
stephen hemminger2b018882017-01-24 13:06:03 -0800776 ret = netvsc_set_queues(net, dev, count);
777 if (ret == 0)
778 nvdev->num_chn = count;
779 else
780 netvsc_set_queues(net, dev, nvdev->num_chn);
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700781
Vitaly Kuznetsovf580aec2016-05-13 13:55:20 +0200782 net_device_ctx->start_remove = false;
stephen hemminger2b018882017-01-24 13:06:03 -0800783
stephen hemminger163891d2017-03-22 14:50:58 -0700784 if (was_running)
785 ret = netvsc_open(net);
786
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +0200787 /* We may have missed link change notifications */
788 schedule_delayed_work(&net_device_ctx->dwork, 0);
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700789
790 return ret;
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700791}
792
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100793static bool
794netvsc_validate_ethtool_ss_cmd(const struct ethtool_link_ksettings *cmd)
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800795{
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100796 struct ethtool_link_ksettings diff1 = *cmd;
797 struct ethtool_link_ksettings diff2 = {};
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800798
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100799 diff1.base.speed = 0;
800 diff1.base.duplex = 0;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800801 /* advertising and cmd are usually set */
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100802 ethtool_link_ksettings_zero_link_mode(&diff1, advertising);
803 diff1.base.cmd = 0;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800804 /* We set port to PORT_OTHER */
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100805 diff2.base.port = PORT_OTHER;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800806
807 return !memcmp(&diff1, &diff2, sizeof(diff1));
808}
809
810static void netvsc_init_settings(struct net_device *dev)
811{
812 struct net_device_context *ndc = netdev_priv(dev);
813
814 ndc->speed = SPEED_UNKNOWN;
815 ndc->duplex = DUPLEX_UNKNOWN;
816}
817
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100818static int netvsc_get_link_ksettings(struct net_device *dev,
819 struct ethtool_link_ksettings *cmd)
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800820{
821 struct net_device_context *ndc = netdev_priv(dev);
822
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100823 cmd->base.speed = ndc->speed;
824 cmd->base.duplex = ndc->duplex;
825 cmd->base.port = PORT_OTHER;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800826
827 return 0;
828}
829
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100830static int netvsc_set_link_ksettings(struct net_device *dev,
831 const struct ethtool_link_ksettings *cmd)
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800832{
833 struct net_device_context *ndc = netdev_priv(dev);
834 u32 speed;
835
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100836 speed = cmd->base.speed;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800837 if (!ethtool_validate_speed(speed) ||
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100838 !ethtool_validate_duplex(cmd->base.duplex) ||
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800839 !netvsc_validate_ethtool_ss_cmd(cmd))
840 return -EINVAL;
841
842 ndc->speed = speed;
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100843 ndc->duplex = cmd->base.duplex;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800844
845 return 0;
846}
847
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800848static int netvsc_change_mtu(struct net_device *ndev, int mtu)
849{
850 struct net_device_context *ndevctx = netdev_priv(ndev);
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200851 struct netvsc_device *nvdev = ndevctx->nvdev;
852 struct hv_device *hdev = ndevctx->device_ctx;
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800853 struct netvsc_device_info device_info;
stephen hemminger163891d2017-03-22 14:50:58 -0700854 bool was_running;
stephen hemminger2b018882017-01-24 13:06:03 -0800855 int ret;
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800856
Vitaly Kuznetsov6da72252016-05-13 13:55:24 +0200857 if (ndevctx->start_remove || !nvdev || nvdev->destroy)
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800858 return -ENODEV;
859
stephen hemminger163891d2017-03-22 14:50:58 -0700860 was_running = netif_running(ndev);
861 if (was_running) {
862 ret = netvsc_close(ndev);
863 if (ret)
864 return ret;
865 }
Haiyang Zhang2de85302015-07-13 13:09:16 -0700866
Andrew Schwartzmeyer8ebdcc52015-08-11 17:14:31 -0700867 memset(&device_info, 0, sizeof(device_info));
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800868 device_info.ring_size = ring_size;
stephen hemminger2b018882017-01-24 13:06:03 -0800869 device_info.num_chn = nvdev->num_chn;
870 device_info.max_num_vrss_chns = nvdev->num_chn;
Dexuan Cui152669b2017-03-02 13:00:53 +0000871
872 ndevctx->start_remove = true;
873 rndis_filter_device_remove(hdev, nvdev);
874
875 /* 'nvdev' has been freed in rndis_filter_device_remove() ->
876 * netvsc_device_remove () -> free_netvsc_device().
877 * We mustn't access it before it's re-created in
878 * rndis_filter_device_add() -> netvsc_device_add().
879 */
880
881 ndev->mtu = mtu;
882
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800883 rndis_filter_device_add(hdev, &device_info);
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800884
Vitaly Kuznetsovf580aec2016-05-13 13:55:20 +0200885 ndevctx->start_remove = false;
Haiyang Zhang2de85302015-07-13 13:09:16 -0700886
stephen hemminger163891d2017-03-22 14:50:58 -0700887 if (was_running)
888 ret = netvsc_open(ndev);
889
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +0200890 /* We may have missed link change notifications */
891 schedule_delayed_work(&ndevctx->dwork, 0);
892
Haiyang Zhang2de85302015-07-13 13:09:16 -0700893 return ret;
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800894}
895
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800896static void netvsc_get_stats64(struct net_device *net,
897 struct rtnl_link_stats64 *t)
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700898{
899 struct net_device_context *ndev_ctx = netdev_priv(net);
Simon Xiao6c80f3f2017-01-24 13:06:13 -0800900 struct netvsc_device *nvdev = ndev_ctx->nvdev;
901 int i;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700902
Simon Xiao6c80f3f2017-01-24 13:06:13 -0800903 if (!nvdev)
904 return;
905
906 for (i = 0; i < nvdev->num_chn; i++) {
907 const struct netvsc_channel *nvchan = &nvdev->chan_table[i];
908 const struct netvsc_stats *stats;
909 u64 packets, bytes, multicast;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700910 unsigned int start;
911
Simon Xiao6c80f3f2017-01-24 13:06:13 -0800912 stats = &nvchan->tx_stats;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700913 do {
Simon Xiao6c80f3f2017-01-24 13:06:13 -0800914 start = u64_stats_fetch_begin_irq(&stats->syncp);
915 packets = stats->packets;
916 bytes = stats->bytes;
917 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700918
Simon Xiao6c80f3f2017-01-24 13:06:13 -0800919 t->tx_bytes += bytes;
920 t->tx_packets += packets;
921
922 stats = &nvchan->rx_stats;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700923 do {
Simon Xiao6c80f3f2017-01-24 13:06:13 -0800924 start = u64_stats_fetch_begin_irq(&stats->syncp);
925 packets = stats->packets;
926 bytes = stats->bytes;
927 multicast = stats->multicast + stats->broadcast;
928 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700929
Simon Xiao6c80f3f2017-01-24 13:06:13 -0800930 t->rx_bytes += bytes;
931 t->rx_packets += packets;
932 t->multicast += multicast;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700933 }
934
935 t->tx_dropped = net->stats.tx_dropped;
Simon Xiaob5124722017-02-17 11:36:20 -0800936 t->tx_errors = net->stats.tx_errors;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700937
938 t->rx_dropped = net->stats.rx_dropped;
939 t->rx_errors = net->stats.rx_errors;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700940}
Haiyang Zhang1ce09e82012-07-10 07:19:22 +0000941
942static int netvsc_set_mac_addr(struct net_device *ndev, void *p)
943{
Haiyang Zhang1ce09e82012-07-10 07:19:22 +0000944 struct sockaddr *addr = p;
Jianjun Kong9a4c8312013-01-18 16:52:09 +0000945 char save_adr[ETH_ALEN];
Haiyang Zhang1ce09e82012-07-10 07:19:22 +0000946 unsigned char save_aatype;
947 int err;
948
949 memcpy(save_adr, ndev->dev_addr, ETH_ALEN);
950 save_aatype = ndev->addr_assign_type;
951
952 err = eth_mac_addr(ndev, p);
953 if (err != 0)
954 return err;
955
Vitaly Kuznetsove834da9a2016-06-03 17:51:01 +0200956 err = rndis_filter_set_device_mac(ndev, addr->sa_data);
Haiyang Zhang1ce09e82012-07-10 07:19:22 +0000957 if (err != 0) {
958 /* roll back to saved MAC */
959 memcpy(ndev->dev_addr, save_adr, ETH_ALEN);
960 ndev->addr_assign_type = save_aatype;
961 }
962
963 return err;
964}
965
Stephen Hemminger4323b472016-08-23 12:17:57 -0700966static const struct {
967 char name[ETH_GSTRING_LEN];
968 u16 offset;
969} netvsc_stats[] = {
970 { "tx_scattered", offsetof(struct netvsc_ethtool_stats, tx_scattered) },
971 { "tx_no_memory", offsetof(struct netvsc_ethtool_stats, tx_no_memory) },
972 { "tx_no_space", offsetof(struct netvsc_ethtool_stats, tx_no_space) },
973 { "tx_too_big", offsetof(struct netvsc_ethtool_stats, tx_too_big) },
974 { "tx_busy", offsetof(struct netvsc_ethtool_stats, tx_busy) },
975};
976
Simon Xiao6c80f3f2017-01-24 13:06:13 -0800977#define NETVSC_GLOBAL_STATS_LEN ARRAY_SIZE(netvsc_stats)
978
979/* 4 statistics per queue (rx/tx packets/bytes) */
980#define NETVSC_QUEUE_STATS_LEN(dev) ((dev)->num_chn * 4)
981
Stephen Hemminger4323b472016-08-23 12:17:57 -0700982static int netvsc_get_sset_count(struct net_device *dev, int string_set)
983{
Simon Xiao6c80f3f2017-01-24 13:06:13 -0800984 struct net_device_context *ndc = netdev_priv(dev);
985 struct netvsc_device *nvdev = ndc->nvdev;
986
Stephen Hemminger4323b472016-08-23 12:17:57 -0700987 switch (string_set) {
988 case ETH_SS_STATS:
Simon Xiao6c80f3f2017-01-24 13:06:13 -0800989 return NETVSC_GLOBAL_STATS_LEN + NETVSC_QUEUE_STATS_LEN(nvdev);
Stephen Hemminger4323b472016-08-23 12:17:57 -0700990 default:
991 return -EINVAL;
992 }
993}
994
995static void netvsc_get_ethtool_stats(struct net_device *dev,
996 struct ethtool_stats *stats, u64 *data)
997{
998 struct net_device_context *ndc = netdev_priv(dev);
Simon Xiao6c80f3f2017-01-24 13:06:13 -0800999 struct netvsc_device *nvdev = ndc->nvdev;
Stephen Hemminger4323b472016-08-23 12:17:57 -07001000 const void *nds = &ndc->eth_stats;
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001001 const struct netvsc_stats *qstats;
1002 unsigned int start;
1003 u64 packets, bytes;
1004 int i, j;
Stephen Hemminger4323b472016-08-23 12:17:57 -07001005
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001006 for (i = 0; i < NETVSC_GLOBAL_STATS_LEN; i++)
Stephen Hemminger4323b472016-08-23 12:17:57 -07001007 data[i] = *(unsigned long *)(nds + netvsc_stats[i].offset);
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001008
1009 for (j = 0; j < nvdev->num_chn; j++) {
1010 qstats = &nvdev->chan_table[j].tx_stats;
1011
1012 do {
1013 start = u64_stats_fetch_begin_irq(&qstats->syncp);
1014 packets = qstats->packets;
1015 bytes = qstats->bytes;
1016 } while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
1017 data[i++] = packets;
1018 data[i++] = bytes;
1019
1020 qstats = &nvdev->chan_table[j].rx_stats;
1021 do {
1022 start = u64_stats_fetch_begin_irq(&qstats->syncp);
1023 packets = qstats->packets;
1024 bytes = qstats->bytes;
1025 } while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
1026 data[i++] = packets;
1027 data[i++] = bytes;
1028 }
Stephen Hemminger4323b472016-08-23 12:17:57 -07001029}
1030
1031static void netvsc_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1032{
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001033 struct net_device_context *ndc = netdev_priv(dev);
1034 struct netvsc_device *nvdev = ndc->nvdev;
1035 u8 *p = data;
Stephen Hemminger4323b472016-08-23 12:17:57 -07001036 int i;
1037
1038 switch (stringset) {
1039 case ETH_SS_STATS:
1040 for (i = 0; i < ARRAY_SIZE(netvsc_stats); i++)
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001041 memcpy(p + i * ETH_GSTRING_LEN,
Stephen Hemminger4323b472016-08-23 12:17:57 -07001042 netvsc_stats[i].name, ETH_GSTRING_LEN);
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001043
1044 p += i * ETH_GSTRING_LEN;
1045 for (i = 0; i < nvdev->num_chn; i++) {
1046 sprintf(p, "tx_queue_%u_packets", i);
1047 p += ETH_GSTRING_LEN;
1048 sprintf(p, "tx_queue_%u_bytes", i);
1049 p += ETH_GSTRING_LEN;
1050 sprintf(p, "rx_queue_%u_packets", i);
1051 p += ETH_GSTRING_LEN;
1052 sprintf(p, "rx_queue_%u_bytes", i);
1053 p += ETH_GSTRING_LEN;
1054 }
1055
Stephen Hemminger4323b472016-08-23 12:17:57 -07001056 break;
1057 }
1058}
1059
stephen hemmingerb448f4e2017-01-24 13:06:00 -08001060static int
stephen hemmingerb5a5dc82017-01-24 13:06:01 -08001061netvsc_get_rss_hash_opts(struct netvsc_device *nvdev,
1062 struct ethtool_rxnfc *info)
1063{
1064 info->data = RXH_IP_SRC | RXH_IP_DST;
1065
1066 switch (info->flow_type) {
1067 case TCP_V4_FLOW:
1068 case TCP_V6_FLOW:
1069 info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
1070 /* fallthrough */
1071 case UDP_V4_FLOW:
1072 case UDP_V6_FLOW:
1073 case IPV4_FLOW:
1074 case IPV6_FLOW:
1075 break;
1076 default:
1077 info->data = 0;
1078 break;
1079 }
1080
1081 return 0;
1082}
1083
1084static int
stephen hemmingerb448f4e2017-01-24 13:06:00 -08001085netvsc_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
1086 u32 *rules)
1087{
1088 struct net_device_context *ndc = netdev_priv(dev);
1089 struct netvsc_device *nvdev = ndc->nvdev;
1090
1091 switch (info->cmd) {
1092 case ETHTOOL_GRXRINGS:
1093 info->data = nvdev->num_chn;
1094 return 0;
stephen hemmingerb5a5dc82017-01-24 13:06:01 -08001095
1096 case ETHTOOL_GRXFH:
1097 return netvsc_get_rss_hash_opts(nvdev, info);
stephen hemmingerb448f4e2017-01-24 13:06:00 -08001098 }
1099 return -EOPNOTSUPP;
1100}
1101
Richard Weinberger316158f2014-07-09 16:23:59 +02001102#ifdef CONFIG_NET_POLL_CONTROLLER
1103static void netvsc_poll_controller(struct net_device *net)
1104{
1105 /* As netvsc_start_xmit() works synchronous we don't have to
1106 * trigger anything here.
1107 */
1108}
1109#endif
Haiyang Zhang1ce09e82012-07-10 07:19:22 +00001110
stephen hemminger962f3fe2017-01-24 13:06:02 -08001111static u32 netvsc_get_rxfh_key_size(struct net_device *dev)
1112{
1113 return NETVSC_HASH_KEYLEN;
1114}
1115
1116static u32 netvsc_rss_indir_size(struct net_device *dev)
1117{
stephen hemmingerff4a4412017-01-24 13:06:04 -08001118 return ITAB_NUM;
stephen hemminger962f3fe2017-01-24 13:06:02 -08001119}
1120
1121static int netvsc_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
1122 u8 *hfunc)
1123{
1124 struct net_device_context *ndc = netdev_priv(dev);
1125 struct netvsc_device *ndev = ndc->nvdev;
1126 struct rndis_device *rndis_dev = ndev->extension;
stephen hemmingerff4a4412017-01-24 13:06:04 -08001127 int i;
stephen hemminger962f3fe2017-01-24 13:06:02 -08001128
1129 if (hfunc)
1130 *hfunc = ETH_RSS_HASH_TOP; /* Toeplitz */
1131
stephen hemmingerff4a4412017-01-24 13:06:04 -08001132 if (indir) {
1133 for (i = 0; i < ITAB_NUM; i++)
1134 indir[i] = rndis_dev->ind_table[i];
1135 }
1136
stephen hemminger962f3fe2017-01-24 13:06:02 -08001137 if (key)
1138 memcpy(key, rndis_dev->rss_key, NETVSC_HASH_KEYLEN);
1139
1140 return 0;
1141}
1142
1143static int netvsc_set_rxfh(struct net_device *dev, const u32 *indir,
1144 const u8 *key, const u8 hfunc)
1145{
1146 struct net_device_context *ndc = netdev_priv(dev);
1147 struct netvsc_device *ndev = ndc->nvdev;
1148 struct rndis_device *rndis_dev = ndev->extension;
stephen hemmingerff4a4412017-01-24 13:06:04 -08001149 int i;
stephen hemminger962f3fe2017-01-24 13:06:02 -08001150
1151 if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
1152 return -EOPNOTSUPP;
1153
stephen hemmingerff4a4412017-01-24 13:06:04 -08001154 if (indir) {
1155 for (i = 0; i < ITAB_NUM; i++)
1156 if (indir[i] >= dev->num_rx_queues)
1157 return -EINVAL;
1158
1159 for (i = 0; i < ITAB_NUM; i++)
1160 rndis_dev->ind_table[i] = indir[i];
1161 }
1162
1163 if (!key) {
1164 if (!indir)
1165 return 0;
1166
1167 key = rndis_dev->rss_key;
1168 }
stephen hemminger962f3fe2017-01-24 13:06:02 -08001169
1170 return rndis_filter_set_rss_param(rndis_dev, key, ndev->num_chn);
1171}
1172
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -07001173static const struct ethtool_ops ethtool_ops = {
1174 .get_drvinfo = netvsc_get_drvinfo,
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -07001175 .get_link = ethtool_op_get_link,
Stephen Hemminger4323b472016-08-23 12:17:57 -07001176 .get_ethtool_stats = netvsc_get_ethtool_stats,
1177 .get_sset_count = netvsc_get_sset_count,
1178 .get_strings = netvsc_get_strings,
Andrew Schwartzmeyer59995372015-02-26 16:27:14 -08001179 .get_channels = netvsc_get_channels,
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -07001180 .set_channels = netvsc_set_channels,
sixiao@microsoft.com76d13b52016-02-17 16:43:59 -08001181 .get_ts_info = ethtool_op_get_ts_info,
stephen hemmingerb448f4e2017-01-24 13:06:00 -08001182 .get_rxnfc = netvsc_get_rxnfc,
stephen hemminger962f3fe2017-01-24 13:06:02 -08001183 .get_rxfh_key_size = netvsc_get_rxfh_key_size,
1184 .get_rxfh_indir_size = netvsc_rss_indir_size,
1185 .get_rxfh = netvsc_get_rxfh,
1186 .set_rxfh = netvsc_set_rxfh,
Philippe Reynes5e8456f2017-03-08 23:41:04 +01001187 .get_link_ksettings = netvsc_get_link_ksettings,
1188 .set_link_ksettings = netvsc_set_link_ksettings,
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -07001189};
1190
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001191static const struct net_device_ops device_ops = {
1192 .ndo_open = netvsc_open,
1193 .ndo_stop = netvsc_close,
1194 .ndo_start_xmit = netvsc_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001195 .ndo_set_rx_mode = netvsc_set_multicast_list,
Haiyang Zhang4d447c92011-12-15 13:45:17 -08001196 .ndo_change_mtu = netvsc_change_mtu,
Haiyang Zhangb681b582010-08-03 19:15:31 +00001197 .ndo_validate_addr = eth_validate_addr,
Haiyang Zhang1ce09e82012-07-10 07:19:22 +00001198 .ndo_set_mac_address = netvsc_set_mac_addr,
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001199 .ndo_select_queue = netvsc_select_queue,
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001200 .ndo_get_stats64 = netvsc_get_stats64,
Richard Weinberger316158f2014-07-09 16:23:59 +02001201#ifdef CONFIG_NET_POLL_CONTROLLER
1202 .ndo_poll_controller = netvsc_poll_controller,
1203#endif
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001204};
1205
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001206/*
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001207 * Handle link status changes. For RNDIS_STATUS_NETWORK_CHANGE emulate link
1208 * down/up sequence. In case of RNDIS_STATUS_MEDIA_CONNECT when carrier is
1209 * present send GARP packet to network peers with netif_notify_peers().
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001210 */
Haiyang Zhang891de742014-02-12 16:54:27 -08001211static void netvsc_link_change(struct work_struct *w)
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001212{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001213 struct net_device_context *ndev_ctx =
1214 container_of(w, struct net_device_context, dwork.work);
1215 struct hv_device *device_obj = ndev_ctx->device_ctx;
1216 struct net_device *net = hv_get_drvdata(device_obj);
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -07001217 struct netvsc_device *net_device;
Haiyang Zhang891de742014-02-12 16:54:27 -08001218 struct rndis_device *rdev;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001219 struct netvsc_reconfig *event = NULL;
1220 bool notify = false, reschedule = false;
1221 unsigned long flags, next_reconfig, delay;
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001222
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001223 rtnl_lock();
1224 if (ndev_ctx->start_remove)
1225 goto out_unlock;
1226
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +02001227 net_device = ndev_ctx->nvdev;
Haiyang Zhang891de742014-02-12 16:54:27 -08001228 rdev = net_device->extension;
Haiyang Zhang891de742014-02-12 16:54:27 -08001229
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001230 next_reconfig = ndev_ctx->last_reconfig + LINKCHANGE_INT;
1231 if (time_is_after_jiffies(next_reconfig)) {
1232 /* link_watch only sends one notification with current state
1233 * per second, avoid doing reconfig more frequently. Handle
1234 * wrap around.
1235 */
1236 delay = next_reconfig - jiffies;
1237 delay = delay < LINKCHANGE_INT ? delay : LINKCHANGE_INT;
1238 schedule_delayed_work(&ndev_ctx->dwork, delay);
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001239 goto out_unlock;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001240 }
1241 ndev_ctx->last_reconfig = jiffies;
1242
1243 spin_lock_irqsave(&ndev_ctx->lock, flags);
1244 if (!list_empty(&ndev_ctx->reconfig_events)) {
1245 event = list_first_entry(&ndev_ctx->reconfig_events,
1246 struct netvsc_reconfig, list);
1247 list_del(&event->list);
1248 reschedule = !list_empty(&ndev_ctx->reconfig_events);
1249 }
1250 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1251
1252 if (!event)
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001253 goto out_unlock;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001254
1255 switch (event->event) {
1256 /* Only the following events are possible due to the check in
1257 * netvsc_linkstatus_callback()
1258 */
1259 case RNDIS_STATUS_MEDIA_CONNECT:
1260 if (rdev->link_state) {
1261 rdev->link_state = false;
1262 netif_carrier_on(net);
1263 netif_tx_wake_all_queues(net);
1264 } else {
1265 notify = true;
Haiyang Zhang3a494e72014-06-19 18:34:36 -07001266 }
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001267 kfree(event);
1268 break;
1269 case RNDIS_STATUS_MEDIA_DISCONNECT:
1270 if (!rdev->link_state) {
1271 rdev->link_state = true;
1272 netif_carrier_off(net);
1273 netif_tx_stop_all_queues(net);
1274 }
1275 kfree(event);
1276 break;
1277 case RNDIS_STATUS_NETWORK_CHANGE:
1278 /* Only makes sense if carrier is present */
1279 if (!rdev->link_state) {
1280 rdev->link_state = true;
1281 netif_carrier_off(net);
1282 netif_tx_stop_all_queues(net);
1283 event->event = RNDIS_STATUS_MEDIA_CONNECT;
1284 spin_lock_irqsave(&ndev_ctx->lock, flags);
Haiyang Zhang15cfd402016-04-21 16:13:01 -07001285 list_add(&event->list, &ndev_ctx->reconfig_events);
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001286 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1287 reschedule = true;
1288 }
1289 break;
Haiyang Zhang891de742014-02-12 16:54:27 -08001290 }
1291
1292 rtnl_unlock();
1293
1294 if (notify)
1295 netdev_notify_peers(net);
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001296
1297 /* link_watch only sends one notification with current state per
1298 * second, handle next reconfig event in 2 seconds.
1299 */
1300 if (reschedule)
1301 schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001302
1303 return;
1304
1305out_unlock:
1306 rtnl_unlock();
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001307}
1308
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001309static struct net_device *get_netvsc_bymac(const u8 *mac)
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001310{
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001311 struct net_device *dev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001312
Stephen Hemminger8737caa2016-08-23 12:17:44 -07001313 ASSERT_RTNL();
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001314
1315 for_each_netdev(&init_net, dev) {
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001316 if (dev->netdev_ops != &device_ops)
1317 continue; /* not a netvsc device */
1318
1319 if (ether_addr_equal(mac, dev->perm_addr))
1320 return dev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001321 }
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001322
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001323 return NULL;
1324}
1325
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001326static struct net_device *get_netvsc_byref(struct net_device *vf_netdev)
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001327{
1328 struct net_device *dev;
1329
1330 ASSERT_RTNL();
1331
1332 for_each_netdev(&init_net, dev) {
1333 struct net_device_context *net_device_ctx;
1334
1335 if (dev->netdev_ops != &device_ops)
1336 continue; /* not a netvsc device */
1337
1338 net_device_ctx = netdev_priv(dev);
1339 if (net_device_ctx->nvdev == NULL)
1340 continue; /* device is removed */
1341
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001342 if (rtnl_dereference(net_device_ctx->vf_netdev) == vf_netdev)
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001343 return dev; /* a match */
1344 }
1345
1346 return NULL;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001347}
1348
1349static int netvsc_register_vf(struct net_device *vf_netdev)
1350{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001351 struct net_device *ndev;
1352 struct net_device_context *net_device_ctx;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001353 struct netvsc_device *netvsc_dev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001354
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001355 if (vf_netdev->addr_len != ETH_ALEN)
1356 return NOTIFY_DONE;
1357
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001358 /*
1359 * We will use the MAC address to locate the synthetic interface to
1360 * associate with the VF interface. If we don't find a matching
1361 * synthetic interface, move on.
1362 */
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001363 ndev = get_netvsc_bymac(vf_netdev->perm_addr);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001364 if (!ndev)
1365 return NOTIFY_DONE;
1366
1367 net_device_ctx = netdev_priv(ndev);
1368 netvsc_dev = net_device_ctx->nvdev;
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001369 if (!netvsc_dev || rtnl_dereference(net_device_ctx->vf_netdev))
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001370 return NOTIFY_DONE;
1371
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001372 netdev_info(ndev, "VF registering: %s\n", vf_netdev->name);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001373 /*
1374 * Take a reference on the module.
1375 */
1376 try_module_get(THIS_MODULE);
Stephen Hemminger07d0f002016-09-22 16:56:30 -07001377
1378 dev_hold(vf_netdev);
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001379 rcu_assign_pointer(net_device_ctx->vf_netdev, vf_netdev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001380 return NOTIFY_OK;
1381}
1382
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001383static int netvsc_vf_up(struct net_device *vf_netdev)
1384{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001385 struct net_device *ndev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001386 struct netvsc_device *netvsc_dev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001387 struct net_device_context *net_device_ctx;
1388
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001389 ndev = get_netvsc_byref(vf_netdev);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001390 if (!ndev)
1391 return NOTIFY_DONE;
1392
1393 net_device_ctx = netdev_priv(ndev);
1394 netvsc_dev = net_device_ctx->nvdev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001395
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001396 netdev_info(ndev, "VF up: %s\n", vf_netdev->name);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001397
1398 /*
1399 * Open the device before switching data path.
1400 */
Vitaly Kuznetsov2f5fa6c2016-06-03 17:51:00 +02001401 rndis_filter_open(netvsc_dev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001402
1403 /*
1404 * notify the host to switch the data path.
1405 */
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001406 netvsc_switch_datapath(ndev, true);
1407 netdev_info(ndev, "Data path switched to VF: %s\n", vf_netdev->name);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001408
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001409 netif_carrier_off(ndev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001410
Vitaly Kuznetsovd0722182016-08-15 17:48:40 +02001411 /* Now notify peers through VF device. */
1412 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, vf_netdev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001413
1414 return NOTIFY_OK;
1415}
1416
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001417static int netvsc_vf_down(struct net_device *vf_netdev)
1418{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001419 struct net_device *ndev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001420 struct netvsc_device *netvsc_dev;
1421 struct net_device_context *net_device_ctx;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001422
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001423 ndev = get_netvsc_byref(vf_netdev);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001424 if (!ndev)
1425 return NOTIFY_DONE;
1426
1427 net_device_ctx = netdev_priv(ndev);
1428 netvsc_dev = net_device_ctx->nvdev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001429
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001430 netdev_info(ndev, "VF down: %s\n", vf_netdev->name);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001431 netvsc_switch_datapath(ndev, false);
1432 netdev_info(ndev, "Data path switched from VF: %s\n", vf_netdev->name);
Vitaly Kuznetsov2f5fa6c2016-06-03 17:51:00 +02001433 rndis_filter_close(netvsc_dev);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001434 netif_carrier_on(ndev);
Vitaly Kuznetsovd0722182016-08-15 17:48:40 +02001435
1436 /* Now notify peers through netvsc device. */
1437 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, ndev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001438
1439 return NOTIFY_OK;
1440}
1441
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001442static int netvsc_unregister_vf(struct net_device *vf_netdev)
1443{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001444 struct net_device *ndev;
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001445 struct net_device_context *net_device_ctx;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001446
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001447 ndev = get_netvsc_byref(vf_netdev);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001448 if (!ndev)
1449 return NOTIFY_DONE;
1450
1451 net_device_ctx = netdev_priv(ndev);
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001452
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001453 netdev_info(ndev, "VF unregistering: %s\n", vf_netdev->name);
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001454
1455 RCU_INIT_POINTER(net_device_ctx->vf_netdev, NULL);
Stephen Hemminger07d0f002016-09-22 16:56:30 -07001456 dev_put(vf_netdev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001457 module_put(THIS_MODULE);
1458 return NOTIFY_OK;
1459}
1460
K. Y. Srinivasan84946892011-09-13 10:59:38 -07001461static int netvsc_probe(struct hv_device *dev,
1462 const struct hv_vmbus_device_id *dev_id)
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001463{
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001464 struct net_device *net = NULL;
1465 struct net_device_context *net_device_ctx;
1466 struct netvsc_device_info device_info;
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001467 struct netvsc_device *nvdev;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001468 int ret;
1469
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001470 net = alloc_etherdev_mq(sizeof(struct net_device_context),
stephen hemminger2b018882017-01-24 13:06:03 -08001471 VRSS_CHANNEL_MAX);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001472 if (!net)
K. Y. Srinivasan51a805d2011-08-25 09:49:11 -07001473 return -ENOMEM;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001474
Haiyang Zhang1b07da52014-03-04 14:11:06 -08001475 netif_carrier_off(net);
1476
Haiyang Zhangb37879e2016-08-04 10:42:14 -07001477 netvsc_init_settings(net);
1478
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001479 net_device_ctx = netdev_priv(net);
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -07001480 net_device_ctx->device_ctx = dev;
Simon Xiao3f300ff2015-04-28 01:05:17 -07001481 net_device_ctx->msg_enable = netif_msg_init(debug, default_msg);
1482 if (netif_msg_probe(net_device_ctx))
1483 netdev_dbg(net, "netvsc msg_enable: %d\n",
1484 net_device_ctx->msg_enable);
1485
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -07001486 hv_set_drvdata(dev, net);
Vitaly Kuznetsovf580aec2016-05-13 13:55:20 +02001487
1488 net_device_ctx->start_remove = false;
1489
Haiyang Zhang891de742014-02-12 16:54:27 -08001490 INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change);
Wenqi Ma792df872012-04-19 00:39:37 +00001491 INIT_WORK(&net_device_ctx->work, do_set_multicast);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001492
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001493 spin_lock_init(&net_device_ctx->lock);
1494 INIT_LIST_HEAD(&net_device_ctx->reconfig_events);
1495
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001496 net->netdev_ops = &device_ops;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001497 net->ethtool_ops = &ethtool_ops;
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -07001498 SET_NETDEV_DEV(net, &dev->device);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001499
Vitaly Kuznetsov14a03cf2016-02-05 17:29:08 +01001500 /* We always need headroom for rndis header */
1501 net->needed_headroom = RNDIS_AND_PPI_SIZE;
1502
Haiyang Zhang692e0842011-09-01 12:19:43 -07001503 /* Notify the netvsc driver of the new device */
Andrew Schwartzmeyer8ebdcc52015-08-11 17:14:31 -07001504 memset(&device_info, 0, sizeof(device_info));
Haiyang Zhang692e0842011-09-01 12:19:43 -07001505 device_info.ring_size = ring_size;
stephen hemminger3071ada2017-03-22 14:50:59 -07001506 device_info.num_chn = VRSS_CHANNEL_DEFAULT;
Haiyang Zhang692e0842011-09-01 12:19:43 -07001507 ret = rndis_filter_device_add(dev, &device_info);
1508 if (ret != 0) {
1509 netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001510 free_netdev(net);
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -07001511 hv_set_drvdata(dev, NULL);
Haiyang Zhang692e0842011-09-01 12:19:43 -07001512 return ret;
1513 }
1514 memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
1515
stephen hemminger23312a32017-01-24 13:05:59 -08001516 /* hw_features computed in rndis_filter_device_add */
1517 net->features = net->hw_features |
1518 NETIF_F_HIGHDMA | NETIF_F_SG |
1519 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
1520 net->vlan_features = net->features;
1521
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +02001522 nvdev = net_device_ctx->nvdev;
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001523 netif_set_real_num_tx_queues(net, nvdev->num_chn);
1524 netif_set_real_num_rx_queues(net, nvdev->num_chn);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001525
Jarod Wilsond0c2c992016-10-20 13:55:21 -04001526 /* MTU range: 68 - 1500 or 65521 */
1527 net->min_mtu = NETVSC_MTU_MIN;
1528 if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
1529 net->max_mtu = NETVSC_MTU - ETH_HLEN;
1530 else
1531 net->max_mtu = ETH_DATA_LEN;
1532
Haiyang Zhanga68f9612013-12-20 16:52:31 -08001533 ret = register_netdev(net);
1534 if (ret != 0) {
1535 pr_err("Unable to register netdev.\n");
stephen hemminger2289f0a2017-01-24 13:06:10 -08001536 rndis_filter_device_remove(dev, nvdev);
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001537 free_netdev(net);
Haiyang Zhanga68f9612013-12-20 16:52:31 -08001538 }
1539
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001540 return ret;
1541}
1542
K. Y. Srinivasan415b0232011-04-29 13:45:12 -07001543static int netvsc_remove(struct hv_device *dev)
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001544{
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -07001545 struct net_device *net;
Haiyang Zhang122a5f62011-05-27 06:21:55 -07001546 struct net_device_context *ndev_ctx;
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -07001547
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +02001548 net = hv_get_drvdata(dev);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001549
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001550 if (net == NULL) {
K. Y. Srinivasan415b0232011-04-29 13:45:12 -07001551 dev_err(&dev->device, "No net device to remove\n");
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001552 return 0;
1553 }
1554
Haiyang Zhang122a5f62011-05-27 06:21:55 -07001555 ndev_ctx = netdev_priv(net);
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +02001556
Vitaly Kuznetsov6da72252016-05-13 13:55:24 +02001557 /* Avoid racing with netvsc_change_mtu()/netvsc_set_channels()
1558 * removing the device.
1559 */
1560 rtnl_lock();
Vitaly Kuznetsovf580aec2016-05-13 13:55:20 +02001561 ndev_ctx->start_remove = true;
Vitaly Kuznetsov6da72252016-05-13 13:55:24 +02001562 rtnl_unlock();
Vitaly Kuznetsovf580aec2016-05-13 13:55:20 +02001563
Haiyang Zhang122a5f62011-05-27 06:21:55 -07001564 cancel_delayed_work_sync(&ndev_ctx->dwork);
Wenqi Ma792df872012-04-19 00:39:37 +00001565 cancel_work_sync(&ndev_ctx->work);
Haiyang Zhang122a5f62011-05-27 06:21:55 -07001566
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001567 /* Stop outbound asap */
Haiyang Zhang0a282532012-02-02 07:17:59 +00001568 netif_tx_disable(net);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001569
1570 unregister_netdev(net);
1571
1572 /*
1573 * Call to the vsc driver to let it know that the device is being
1574 * removed
1575 */
stephen hemminger2289f0a2017-01-24 13:06:10 -08001576 rndis_filter_device_remove(dev, ndev_ctx->nvdev);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001577
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +02001578 hv_set_drvdata(dev, NULL);
1579
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001580 free_netdev(net);
Haiyang Zhangdf06bcf2011-05-23 09:03:47 -07001581 return 0;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001582}
1583
K. Y. Srinivasan345c4cc2011-08-25 09:48:34 -07001584static const struct hv_vmbus_device_id id_table[] = {
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07001585 /* Network guid */
K. Y. Srinivasan8f505942013-01-23 17:42:42 -08001586 { HV_NIC_GUID, },
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07001587 { },
K. Y. Srinivasan345c4cc2011-08-25 09:48:34 -07001588};
1589
1590MODULE_DEVICE_TABLE(vmbus, id_table);
1591
K. Y. Srinivasanf1542a62011-05-10 07:55:16 -07001592/* The one and only one */
K. Y. Srinivasanfde0ef92011-05-12 19:35:08 -07001593static struct hv_driver netvsc_drv = {
Haiyang Zhangd31b20f2012-03-07 10:02:00 +00001594 .name = KBUILD_MODNAME,
K. Y. Srinivasan345c4cc2011-08-25 09:48:34 -07001595 .id_table = id_table,
K. Y. Srinivasanfde0ef92011-05-12 19:35:08 -07001596 .probe = netvsc_probe,
1597 .remove = netvsc_remove,
K. Y. Srinivasand4890972011-05-10 07:55:17 -07001598};
K. Y. Srinivasanf1542a62011-05-10 07:55:16 -07001599
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001600/*
1601 * On Hyper-V, every VF interface is matched with a corresponding
1602 * synthetic interface. The synthetic interface is presented first
1603 * to the guest. When the corresponding VF instance is registered,
1604 * we will take care of switching the data path.
1605 */
1606static int netvsc_netdev_event(struct notifier_block *this,
1607 unsigned long event, void *ptr)
1608{
1609 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
1610
Stephen Hemmingeree837a12016-09-22 16:56:31 -07001611 /* Skip our own events */
1612 if (event_dev->netdev_ops == &device_ops)
1613 return NOTIFY_DONE;
1614
1615 /* Avoid non-Ethernet type devices */
1616 if (event_dev->type != ARPHRD_ETHER)
1617 return NOTIFY_DONE;
1618
Vitaly Kuznetsov0dbff142016-08-15 17:48:43 +02001619 /* Avoid Vlan dev with same MAC registering as VF */
Parav Panditd0d7b102017-02-04 11:00:49 -06001620 if (is_vlan_dev(event_dev))
Vitaly Kuznetsov0dbff142016-08-15 17:48:43 +02001621 return NOTIFY_DONE;
1622
1623 /* Avoid Bonding master dev with same MAC registering as VF */
Stephen Hemmingeree837a12016-09-22 16:56:31 -07001624 if ((event_dev->priv_flags & IFF_BONDING) &&
1625 (event_dev->flags & IFF_MASTER))
Haiyang Zhangcb2911f2016-06-02 12:02:04 -07001626 return NOTIFY_DONE;
1627
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001628 switch (event) {
1629 case NETDEV_REGISTER:
1630 return netvsc_register_vf(event_dev);
1631 case NETDEV_UNREGISTER:
1632 return netvsc_unregister_vf(event_dev);
1633 case NETDEV_UP:
1634 return netvsc_vf_up(event_dev);
1635 case NETDEV_DOWN:
1636 return netvsc_vf_down(event_dev);
1637 default:
1638 return NOTIFY_DONE;
1639 }
1640}
1641
1642static struct notifier_block netvsc_netdev_notifier = {
1643 .notifier_call = netvsc_netdev_event,
1644};
1645
K. Y. Srinivasana9869c92011-05-12 19:35:17 -07001646static void __exit netvsc_drv_exit(void)
Hank Janssenfceaf242009-07-13 15:34:54 -07001647{
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001648 unregister_netdevice_notifier(&netvsc_netdev_notifier);
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001649 vmbus_driver_unregister(&netvsc_drv);
Hank Janssenfceaf242009-07-13 15:34:54 -07001650}
1651
K. Y. Srinivasan1fde28c2011-05-12 19:35:16 -07001652static int __init netvsc_drv_init(void)
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001653{
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001654 int ret;
1655
Haiyang Zhangfa85a6c2012-07-25 08:08:41 +00001656 if (ring_size < RING_SIZE_MIN) {
1657 ring_size = RING_SIZE_MIN;
1658 pr_info("Increased ring_size to %d (min allowed)\n",
1659 ring_size);
1660 }
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001661 ret = vmbus_driver_register(&netvsc_drv);
1662
1663 if (ret)
1664 return ret;
1665
1666 register_netdevice_notifier(&netvsc_netdev_notifier);
1667 return 0;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001668}
1669
Hank Janssen26c14cc2010-02-11 23:02:42 +00001670MODULE_LICENSE("GPL");
Stephen Hemminger7880fc52010-05-04 09:58:52 -07001671MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
Hank Janssenfceaf242009-07-13 15:34:54 -07001672
K. Y. Srinivasan1fde28c2011-05-12 19:35:16 -07001673module_init(netvsc_drv_init);
K. Y. Srinivasana9869c92011-05-12 19:35:17 -07001674module_exit(netvsc_drv_exit);