blob: f6382150b16a13e7071f4640c548b377023a02b3 [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)
sixiao@microsoft.coma0606792016-02-04 15:49:34 -080045#define NETVSC_HW_FEATURES (NETIF_F_RXCSUM | \
46 NETIF_F_SG | \
47 NETIF_F_TSO | \
48 NETIF_F_TSO6 | \
49 NETIF_F_HW_CSUM)
Hank Janssen99c8da02010-10-12 10:45:23 -070050static int ring_size = 128;
Stephen Hemminger450d7a42010-05-04 09:58:53 -070051module_param(ring_size, int, S_IRUGO);
52MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)");
Hank Janssenfceaf242009-07-13 15:34:54 -070053
KY Srinivasane01ec212015-05-27 13:16:57 -070054static int max_num_vrss_chns = 8;
55
Simon Xiao3f300ff2015-04-28 01:05:17 -070056static const u32 default_msg = NETIF_MSG_DRV | NETIF_MSG_PROBE |
57 NETIF_MSG_LINK | NETIF_MSG_IFUP |
58 NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR |
59 NETIF_MSG_TX_ERR;
60
61static int debug = -1;
62module_param(debug, int, S_IRUGO);
63MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
64
Haiyang Zhangd426b2e2011-11-30 07:19:08 -080065static void do_set_multicast(struct work_struct *w)
66{
Wenqi Ma792df872012-04-19 00:39:37 +000067 struct net_device_context *ndevctx =
68 container_of(w, struct net_device_context, work);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +020069 struct hv_device *device_obj = ndevctx->device_ctx;
70 struct net_device *ndev = hv_get_drvdata(device_obj);
71 struct netvsc_device *nvdev = ndevctx->nvdev;
Haiyang Zhangd426b2e2011-11-30 07:19:08 -080072 struct rndis_device *rdev;
73
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +020074 if (!nvdev)
Wenqi Ma792df872012-04-19 00:39:37 +000075 return;
Haiyang Zhangd426b2e2011-11-30 07:19:08 -080076
77 rdev = nvdev->extension;
78 if (rdev == NULL)
Wenqi Ma792df872012-04-19 00:39:37 +000079 return;
Haiyang Zhangd426b2e2011-11-30 07:19:08 -080080
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +020081 if (ndev->flags & IFF_PROMISC)
Haiyang Zhangd426b2e2011-11-30 07:19:08 -080082 rndis_filter_set_packet_filter(rdev,
83 NDIS_PACKET_TYPE_PROMISCUOUS);
84 else
85 rndis_filter_set_packet_filter(rdev,
86 NDIS_PACKET_TYPE_BROADCAST |
87 NDIS_PACKET_TYPE_ALL_MULTICAST |
88 NDIS_PACKET_TYPE_DIRECTED);
Haiyang Zhangd426b2e2011-11-30 07:19:08 -080089}
90
Greg Kroah-Hartman4e9bfef2009-07-15 12:45:20 -070091static void netvsc_set_multicast_list(struct net_device *net)
Hank Janssenfceaf242009-07-13 15:34:54 -070092{
Wenqi Ma792df872012-04-19 00:39:37 +000093 struct net_device_context *net_device_ctx = netdev_priv(net);
Haiyang Zhangd426b2e2011-11-30 07:19:08 -080094
Wenqi Ma792df872012-04-19 00:39:37 +000095 schedule_work(&net_device_ctx->work);
Hank Janssenfceaf242009-07-13 15:34:54 -070096}
97
Hank Janssenfceaf242009-07-13 15:34:54 -070098static int netvsc_open(struct net_device *net)
99{
Vitaly Kuznetsov2f5fa6c2016-06-03 17:51:00 +0200100 struct netvsc_device *nvdev = net_device_to_netvsc_device(net);
Haiyang Zhang891de742014-02-12 16:54:27 -0800101 struct rndis_device *rdev;
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700102 int ret = 0;
Hank Janssenfceaf242009-07-13 15:34:54 -0700103
Haiyang Zhang891de742014-02-12 16:54:27 -0800104 netif_carrier_off(net);
105
Haiyang Zhangd515d0f2011-09-28 13:24:15 -0700106 /* Open up the device */
Vitaly Kuznetsov2f5fa6c2016-06-03 17:51:00 +0200107 ret = rndis_filter_open(nvdev);
Haiyang Zhangd515d0f2011-09-28 13:24:15 -0700108 if (ret != 0) {
109 netdev_err(net, "unable to open device (ret %d).\n", ret);
110 return ret;
Hank Janssenfceaf242009-07-13 15:34:54 -0700111 }
112
Haiyang Zhang2de85302015-07-13 13:09:16 -0700113 netif_tx_wake_all_queues(net);
Haiyang Zhangd515d0f2011-09-28 13:24:15 -0700114
Haiyang Zhang891de742014-02-12 16:54:27 -0800115 rdev = nvdev->extension;
116 if (!rdev->link_state)
117 netif_carrier_on(net);
118
Hank Janssenfceaf242009-07-13 15:34:54 -0700119 return ret;
120}
121
Hank Janssenfceaf242009-07-13 15:34:54 -0700122static int netvsc_close(struct net_device *net)
123{
Hank Janssenfceaf242009-07-13 15:34:54 -0700124 struct net_device_context *net_device_ctx = netdev_priv(net);
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200125 struct netvsc_device *nvdev = net_device_ctx->nvdev;
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700126 int ret;
Haiyang Zhang2de85302015-07-13 13:09:16 -0700127 u32 aread, awrite, i, msec = 10, retry = 0, retry_max = 20;
128 struct vmbus_channel *chn;
Hank Janssenfceaf242009-07-13 15:34:54 -0700129
Haiyang Zhang0a282532012-02-02 07:17:59 +0000130 netif_tx_disable(net);
Hank Janssenfceaf242009-07-13 15:34:54 -0700131
Wenqi Ma792df872012-04-19 00:39:37 +0000132 /* Make sure netvsc_set_multicast_list doesn't re-enable filter! */
133 cancel_work_sync(&net_device_ctx->work);
Vitaly Kuznetsov2f5fa6c2016-06-03 17:51:00 +0200134 ret = rndis_filter_close(nvdev);
Haiyang Zhang2de85302015-07-13 13:09:16 -0700135 if (ret != 0) {
Hank Jansseneb335bc2011-03-29 13:58:48 -0700136 netdev_err(net, "unable to close device (ret %d).\n", ret);
Haiyang Zhang2de85302015-07-13 13:09:16 -0700137 return ret;
138 }
139
140 /* Ensure pending bytes in ring are read */
141 while (true) {
142 aread = 0;
143 for (i = 0; i < nvdev->num_chn; i++) {
144 chn = nvdev->chn_table[i];
145 if (!chn)
146 continue;
147
148 hv_get_ringbuffer_availbytes(&chn->inbound, &aread,
149 &awrite);
150
151 if (aread)
152 break;
153
154 hv_get_ringbuffer_availbytes(&chn->outbound, &aread,
155 &awrite);
156
157 if (aread)
158 break;
159 }
160
161 retry++;
162 if (retry > retry_max || aread == 0)
163 break;
164
165 msleep(msec);
166
167 if (msec < 1000)
168 msec *= 2;
169 }
170
171 if (aread) {
172 netdev_err(net, "Ring buffer not empty after closing rndis\n");
173 ret = -ETIMEDOUT;
174 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700175
Hank Janssenfceaf242009-07-13 15:34:54 -0700176 return ret;
177}
178
KY Srinivasan8a002512014-03-08 19:23:14 -0800179static void *init_ppi_data(struct rndis_message *msg, u32 ppi_size,
180 int pkt_type)
181{
182 struct rndis_packet *rndis_pkt;
183 struct rndis_per_packet_info *ppi;
184
185 rndis_pkt = &msg->msg.pkt;
186 rndis_pkt->data_offset += ppi_size;
187
188 ppi = (struct rndis_per_packet_info *)((void *)rndis_pkt +
189 rndis_pkt->per_pkt_info_offset + rndis_pkt->per_pkt_info_len);
190
191 ppi->size = ppi_size;
192 ppi->type = pkt_type;
193 ppi->ppi_offset = sizeof(struct rndis_per_packet_info);
194
195 rndis_pkt->per_pkt_info_len += ppi_size;
196
197 return ppi;
198}
199
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700200static u16 netvsc_select_queue(struct net_device *ndev, struct sk_buff *skb,
201 void *accel_priv, select_queue_fallback_t fallback)
202{
203 struct net_device_context *net_device_ctx = netdev_priv(ndev);
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200204 struct netvsc_device *nvsc_dev = net_device_ctx->nvdev;
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700205 u32 hash;
206 u16 q_idx = 0;
207
208 if (nvsc_dev == NULL || ndev->real_num_tx_queues <= 1)
209 return 0;
210
Vitaly Kuznetsov757647e2016-01-25 16:00:41 +0100211 hash = skb_get_hash(skb);
212 q_idx = nvsc_dev->send_table[hash % VRSS_SEND_TAB_SIZE] %
213 ndev->real_num_tx_queues;
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700214
Vitaly Kuznetsov8b9fbe12015-12-01 16:43:11 -0800215 if (!nvsc_dev->chn_table[q_idx])
216 q_idx = 0;
217
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700218 return q_idx;
219}
220
KY Srinivasan54a73572014-03-08 19:23:13 -0800221static u32 fill_pg_buf(struct page *page, u32 offset, u32 len,
222 struct hv_page_buffer *pb)
223{
224 int j = 0;
225
226 /* Deal with compund pages by ignoring unused part
227 * of the page.
228 */
229 page += (offset >> PAGE_SHIFT);
230 offset &= ~PAGE_MASK;
231
232 while (len > 0) {
233 unsigned long bytes;
234
235 bytes = PAGE_SIZE - offset;
236 if (bytes > len)
237 bytes = len;
238 pb[j].pfn = page_to_pfn(page);
239 pb[j].offset = offset;
240 pb[j].len = bytes;
241
242 offset += bytes;
243 len -= bytes;
244
245 if (offset == PAGE_SIZE && len) {
246 page++;
247 offset = 0;
248 j++;
249 }
250 }
251
252 return j + 1;
253}
254
KY Srinivasan8a002512014-03-08 19:23:14 -0800255static u32 init_page_array(void *hdr, u32 len, struct sk_buff *skb,
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800256 struct hv_netvsc_packet *packet,
257 struct hv_page_buffer **page_buf)
KY Srinivasan54a73572014-03-08 19:23:13 -0800258{
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800259 struct hv_page_buffer *pb = *page_buf;
KY Srinivasan54a73572014-03-08 19:23:13 -0800260 u32 slots_used = 0;
261 char *data = skb->data;
262 int frags = skb_shinfo(skb)->nr_frags;
263 int i;
264
265 /* The packet is laid out thus:
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700266 * 1. hdr: RNDIS header and PPI
KY Srinivasan54a73572014-03-08 19:23:13 -0800267 * 2. skb linear data
268 * 3. skb fragment data
269 */
270 if (hdr != NULL)
271 slots_used += fill_pg_buf(virt_to_page(hdr),
272 offset_in_page(hdr),
273 len, &pb[slots_used]);
274
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700275 packet->rmsg_size = len;
276 packet->rmsg_pgcnt = slots_used;
277
KY Srinivasan54a73572014-03-08 19:23:13 -0800278 slots_used += fill_pg_buf(virt_to_page(data),
279 offset_in_page(data),
280 skb_headlen(skb), &pb[slots_used]);
281
282 for (i = 0; i < frags; i++) {
283 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
284
285 slots_used += fill_pg_buf(skb_frag_page(frag),
286 frag->page_offset,
287 skb_frag_size(frag), &pb[slots_used]);
288 }
KY Srinivasan8a002512014-03-08 19:23:14 -0800289 return slots_used;
KY Srinivasan54a73572014-03-08 19:23:13 -0800290}
291
292static int count_skb_frag_slots(struct sk_buff *skb)
293{
294 int i, frags = skb_shinfo(skb)->nr_frags;
295 int pages = 0;
296
297 for (i = 0; i < frags; i++) {
298 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
299 unsigned long size = skb_frag_size(frag);
300 unsigned long offset = frag->page_offset;
301
302 /* Skip unused frames from start of page */
303 offset &= ~PAGE_MASK;
304 pages += PFN_UP(offset + size);
305 }
306 return pages;
307}
308
309static int netvsc_get_slots(struct sk_buff *skb)
310{
311 char *data = skb->data;
312 unsigned int offset = offset_in_page(data);
313 unsigned int len = skb_headlen(skb);
314 int slots;
315 int frag_slots;
316
317 slots = DIV_ROUND_UP(offset + len, PAGE_SIZE);
318 frag_slots = count_skb_frag_slots(skb);
319 return slots + frag_slots;
320}
321
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800322static u32 get_net_transport_info(struct sk_buff *skb, u32 *trans_off)
323{
324 u32 ret_val = TRANSPORT_INFO_NOT_IP;
325
326 if ((eth_hdr(skb)->h_proto != htons(ETH_P_IP)) &&
327 (eth_hdr(skb)->h_proto != htons(ETH_P_IPV6))) {
328 goto not_ip;
329 }
330
331 *trans_off = skb_transport_offset(skb);
332
333 if ((eth_hdr(skb)->h_proto == htons(ETH_P_IP))) {
334 struct iphdr *iphdr = ip_hdr(skb);
335
336 if (iphdr->protocol == IPPROTO_TCP)
337 ret_val = TRANSPORT_INFO_IPV4_TCP;
338 else if (iphdr->protocol == IPPROTO_UDP)
339 ret_val = TRANSPORT_INFO_IPV4_UDP;
340 } else {
341 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
342 ret_val = TRANSPORT_INFO_IPV6_TCP;
343 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
344 ret_val = TRANSPORT_INFO_IPV6_UDP;
345 }
346
347not_ip:
348 return ret_val;
349}
350
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;
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800361 struct ndis_tcp_ip_checksum_info *csum_info;
362 int hdr_offset;
363 u32 net_trans_info;
Haiyang Zhang307f0992014-05-21 12:55:39 -0700364 u32 hash;
Vitaly Kuznetsove88f7e02015-04-08 17:54:06 +0200365 u32 skb_length;
KY Srinivasanb08cc792015-03-29 21:08:42 -0700366 struct hv_page_buffer page_buf[MAX_PAGE_BUFFER_COUNT];
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800367 struct hv_page_buffer *pb = page_buf;
Hank Janssenfceaf242009-07-13 15:34:54 -0700368
KY Srinivasan54a73572014-03-08 19:23:13 -0800369 /* We will atmost need two pages to describe the rndis
370 * header. We can only transmit MAX_PAGE_BUFFER_COUNT number
Vitaly Kuznetsove88f7e02015-04-08 17:54:06 +0200371 * of pages in a single packet. If skb is scattered around
372 * more pages we try linearizing it.
KY Srinivasan54a73572014-03-08 19:23:13 -0800373 */
Vitaly Kuznetsove88f7e02015-04-08 17:54:06 +0200374
Vitaly Kuznetsove88f7e02015-04-08 17:54:06 +0200375 skb_length = skb->len;
KY Srinivasan8a002512014-03-08 19:23:14 -0800376 num_data_pgs = netvsc_get_slots(skb) + 2;
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700377
378 if (unlikely(num_data_pgs > MAX_PAGE_BUFFER_COUNT)) {
Stephen Hemminger4323b472016-08-23 12:17:57 -0700379 ++net_device_ctx->eth_stats.tx_scattered;
380
381 if (skb_linearize(skb))
382 goto no_memory;
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700383
384 num_data_pgs = netvsc_get_slots(skb) + 2;
385 if (num_data_pgs > MAX_PAGE_BUFFER_COUNT) {
Stephen Hemminger4323b472016-08-23 12:17:57 -0700386 ++net_device_ctx->eth_stats.tx_too_big;
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700387 goto drop;
388 }
KY Srinivasan54a73572014-03-08 19:23:13 -0800389 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700390
KY Srinivasanc0eb4542015-12-01 16:43:10 -0800391 /*
392 * Place the rndis header in the skb head room and
393 * the skb->cb will be used for hv_netvsc_packet
394 * structure.
395 */
396 ret = skb_cow_head(skb, RNDIS_AND_PPI_SIZE);
Stephen Hemminger4323b472016-08-23 12:17:57 -0700397 if (ret)
398 goto no_memory;
399
KY Srinivasanc0eb4542015-12-01 16:43:10 -0800400 /* Use the skb control buffer for building up the packet */
401 BUILD_BUG_ON(sizeof(struct hv_netvsc_packet) >
402 FIELD_SIZEOF(struct sk_buff, cb));
403 packet = (struct hv_netvsc_packet *)skb->cb;
Hank Janssenfceaf242009-07-13 15:34:54 -0700404
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700405 packet->q_idx = skb_get_queue_mapping(skb);
406
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800407 packet->total_data_buflen = skb->len;
Hank Janssenfceaf242009-07-13 15:34:54 -0700408
KY Srinivasanc0eb4542015-12-01 16:43:10 -0800409 rndis_msg = (struct rndis_message *)skb->head;
KY Srinivasanb08cc792015-03-29 21:08:42 -0700410
KY Srinivasan24476762015-12-01 16:43:06 -0800411 memset(rndis_msg, 0, RNDIS_AND_PPI_SIZE);
Hank Janssenfceaf242009-07-13 15:34:54 -0700412
KY Srinivasan8a002512014-03-08 19:23:14 -0800413 /* Add the rndis header */
KY Srinivasan8a002512014-03-08 19:23:14 -0800414 rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET;
415 rndis_msg->msg_len = packet->total_data_buflen;
416 rndis_pkt = &rndis_msg->msg.pkt;
417 rndis_pkt->data_offset = sizeof(struct rndis_packet);
418 rndis_pkt->data_len = packet->total_data_buflen;
419 rndis_pkt->per_pkt_info_offset = sizeof(struct rndis_packet);
420
421 rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet);
422
Haiyang Zhang307f0992014-05-21 12:55:39 -0700423 hash = skb_get_hash_raw(skb);
424 if (hash != 0 && net->real_num_tx_queues > 1) {
425 rndis_msg_size += NDIS_HASH_PPI_SIZE;
426 ppi = init_ppi_data(rndis_msg, NDIS_HASH_PPI_SIZE,
427 NBL_HASH_VALUE);
428 *(u32 *)((void *)ppi + ppi->ppi_offset) = hash;
429 }
430
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700431 if (skb_vlan_tag_present(skb)) {
KY Srinivasan8a002512014-03-08 19:23:14 -0800432 struct ndis_pkt_8021q_info *vlan;
433
434 rndis_msg_size += NDIS_VLAN_PPI_SIZE;
435 ppi = init_ppi_data(rndis_msg, NDIS_VLAN_PPI_SIZE,
436 IEEE_8021Q_INFO);
437 vlan = (struct ndis_pkt_8021q_info *)((void *)ppi +
438 ppi->ppi_offset);
KY Srinivasan760d1e32015-12-01 16:43:19 -0800439 vlan->vlanid = skb->vlan_tci & VLAN_VID_MASK;
440 vlan->pri = (skb->vlan_tci & VLAN_PRIO_MASK) >>
KY Srinivasan8a002512014-03-08 19:23:14 -0800441 VLAN_PRIO_SHIFT;
442 }
443
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800444 net_trans_info = get_net_transport_info(skb, &hdr_offset);
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800445
446 /*
447 * Setup the sendside checksum offload only if this is not a
448 * GSO packet.
449 */
Arnd Bergmann52ccd632016-10-18 00:16:09 +0200450 if ((net_trans_info & (INFO_TCP | INFO_UDP)) && skb_is_gso(skb)) {
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700451 struct ndis_tcp_lso_info *lso_info;
452
453 rndis_msg_size += NDIS_LSO_PPI_SIZE;
454 ppi = init_ppi_data(rndis_msg, NDIS_LSO_PPI_SIZE,
455 TCP_LARGESEND_PKTINFO);
456
457 lso_info = (struct ndis_tcp_lso_info *)((void *)ppi +
458 ppi->ppi_offset);
459
460 lso_info->lso_v2_transmit.type = NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
461 if (net_trans_info & (INFO_IPV4 << 16)) {
462 lso_info->lso_v2_transmit.ip_version =
463 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
464 ip_hdr(skb)->tot_len = 0;
465 ip_hdr(skb)->check = 0;
466 tcp_hdr(skb)->check =
467 ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
468 ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
469 } else {
470 lso_info->lso_v2_transmit.ip_version =
471 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
472 ipv6_hdr(skb)->payload_len = 0;
473 tcp_hdr(skb)->check =
474 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
475 &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
476 }
477 lso_info->lso_v2_transmit.tcp_header_offset = hdr_offset;
478 lso_info->lso_v2_transmit.mss = skb_shinfo(skb)->gso_size;
stephen hemmingerad19bc82016-10-11 14:03:07 -0700479 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
480 if (net_trans_info & INFO_TCP) {
481 rndis_msg_size += NDIS_CSUM_PPI_SIZE;
482 ppi = init_ppi_data(rndis_msg, NDIS_CSUM_PPI_SIZE,
483 TCPIP_CHKSUM_PKTINFO);
484
485 csum_info = (struct ndis_tcp_ip_checksum_info *)((void *)ppi +
486 ppi->ppi_offset);
487
488 if (net_trans_info & (INFO_IPV4 << 16))
489 csum_info->transmit.is_ipv4 = 1;
490 else
491 csum_info->transmit.is_ipv6 = 1;
492
493 csum_info->transmit.tcp_checksum = 1;
494 csum_info->transmit.tcp_header_offset = hdr_offset;
495 } else {
496 /* UDP checksum (and other) offload is not supported. */
497 if (skb_checksum_help(skb))
498 goto drop;
499 }
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700500 }
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800501
KY Srinivasan8a002512014-03-08 19:23:14 -0800502 /* Start filling in the page buffers with the rndis hdr */
503 rndis_msg->msg_len += rndis_msg_size;
Haiyang Zhang942396b2014-10-22 13:47:18 -0700504 packet->total_data_buflen = rndis_msg->msg_len;
KY Srinivasan8a002512014-03-08 19:23:14 -0800505 packet->page_buf_cnt = init_page_array(rndis_msg, rndis_msg_size,
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800506 skb, packet, &pb);
KY Srinivasan8a002512014-03-08 19:23:14 -0800507
sixiao@microsoft.com76d13b52016-02-17 16:43:59 -0800508 /* timestamp packet in software */
509 skb_tx_timestamp(skb);
KY Srinivasan3a3d9a02015-12-01 16:43:14 -0800510 ret = netvsc_send(net_device_ctx->device_ctx, packet,
511 rndis_msg, &pb, skb);
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700512 if (likely(ret == 0)) {
Stephen Hemminger4323b472016-08-23 12:17:57 -0700513 struct netvsc_stats *tx_stats = this_cpu_ptr(net_device_ctx->tx_stats);
514
sixiao@microsoft.com4b02b582015-05-15 02:33:03 -0700515 u64_stats_update_begin(&tx_stats->syncp);
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700516 tx_stats->packets++;
517 tx_stats->bytes += skb_length;
sixiao@microsoft.com4b02b582015-05-15 02:33:03 -0700518 u64_stats_update_end(&tx_stats->syncp);
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700519 return NETDEV_TX_OK;
Hank Janssenfceaf242009-07-13 15:34:54 -0700520 }
Stephen Hemminger4323b472016-08-23 12:17:57 -0700521
522 if (ret == -EAGAIN) {
523 ++net_device_ctx->eth_stats.tx_busy;
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700524 return NETDEV_TX_BUSY;
Stephen Hemminger4323b472016-08-23 12:17:57 -0700525 }
526
527 if (ret == -ENOSPC)
528 ++net_device_ctx->eth_stats.tx_no_space;
Hank Janssenfceaf242009-07-13 15:34:54 -0700529
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700530drop:
531 dev_kfree_skb_any(skb);
532 net->stats.tx_dropped++;
533
534 return NETDEV_TX_OK;
Stephen Hemminger4323b472016-08-23 12:17:57 -0700535
536no_memory:
537 ++net_device_ctx->eth_stats.tx_no_memory;
538 goto drop;
Hank Janssenfceaf242009-07-13 15:34:54 -0700539}
540
Hank Janssen3e189512010-03-04 22:11:00 +0000541/*
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700542 * netvsc_linkstatus_callback - Link up/down notification
543 */
K. Y. Srinivasan90ef1172011-05-12 19:34:50 -0700544void netvsc_linkstatus_callback(struct hv_device *device_obj,
Haiyang Zhang3a494e72014-06-19 18:34:36 -0700545 struct rndis_message *resp)
Hank Janssenfceaf242009-07-13 15:34:54 -0700546{
Haiyang Zhang3a494e72014-06-19 18:34:36 -0700547 struct rndis_indicate_status *indicate = &resp->msg.indicate_status;
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -0700548 struct net_device *net;
Haiyang Zhangc996edc2011-04-06 15:18:00 -0700549 struct net_device_context *ndev_ctx;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +0100550 struct netvsc_reconfig *event;
551 unsigned long flags;
552
Haiyang Zhang7f5d5af2016-08-04 10:42:15 -0700553 net = hv_get_drvdata(device_obj);
554
555 if (!net)
556 return;
557
558 ndev_ctx = netdev_priv(net);
559
560 /* Update the physical link speed when changing to another vSwitch */
561 if (indicate->status == RNDIS_STATUS_LINK_SPEED_CHANGE) {
562 u32 speed;
563
564 speed = *(u32 *)((void *)indicate + indicate->
565 status_buf_offset) / 10000;
566 ndev_ctx->speed = speed;
567 return;
568 }
569
570 /* Handle these link change statuses below */
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +0100571 if (indicate->status != RNDIS_STATUS_NETWORK_CHANGE &&
572 indicate->status != RNDIS_STATUS_MEDIA_CONNECT &&
573 indicate->status != RNDIS_STATUS_MEDIA_DISCONNECT)
574 return;
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -0700575
Haiyang Zhang7f5d5af2016-08-04 10:42:15 -0700576 if (net->reg_state != NETREG_REGISTERED)
Hank Janssenfceaf242009-07-13 15:34:54 -0700577 return;
Hank Janssenfceaf242009-07-13 15:34:54 -0700578
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +0100579 event = kzalloc(sizeof(*event), GFP_ATOMIC);
580 if (!event)
581 return;
582 event->event = indicate->status;
583
584 spin_lock_irqsave(&ndev_ctx->lock, flags);
585 list_add_tail(&event->list, &ndev_ctx->reconfig_events);
586 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
587
588 schedule_delayed_work(&ndev_ctx->dwork, 0);
Hank Janssenfceaf242009-07-13 15:34:54 -0700589}
590
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700591static struct sk_buff *netvsc_alloc_recv_skb(struct net_device *net,
KY Srinivasane3d605e2014-03-08 19:23:16 -0800592 struct hv_netvsc_packet *packet,
KY Srinivasan25b85ee2015-12-01 16:43:05 -0800593 struct ndis_tcp_ip_checksum_info *csum_info,
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700594 void *data, u16 vlan_tci)
Hank Janssenfceaf242009-07-13 15:34:54 -0700595{
Hank Janssenfceaf242009-07-13 15:34:54 -0700596 struct sk_buff *skb;
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -0700597
Haiyang Zhang72a2f5b2010-12-10 12:03:58 -0800598 skb = netdev_alloc_skb_ip_align(net, packet->total_data_buflen);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700599 if (!skb)
600 return skb;
Hank Janssenfceaf242009-07-13 15:34:54 -0700601
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700602 /*
603 * Copy to skb. This copy is needed here since the memory pointed by
604 * hv_netvsc_packet cannot be deallocated
605 */
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700606 memcpy(skb_put(skb, packet->total_data_buflen), data,
607 packet->total_data_buflen);
Hank Janssenfceaf242009-07-13 15:34:54 -0700608
609 skb->protocol = eth_type_trans(skb, net);
Stephen Hemmingere52fed72016-10-23 21:32:47 -0700610
611 /* skb is already created with CHECKSUM_NONE */
612 skb_checksum_none_assert(skb);
613
614 /*
615 * In Linux, the IP checksum is always checked.
616 * Do L4 checksum offload if enabled and present.
617 */
618 if (csum_info && (net->features & NETIF_F_RXCSUM)) {
619 if (csum_info->receive.tcp_checksum_succeeded ||
620 csum_info->receive.udp_checksum_succeeded)
KY Srinivasane3d605e2014-03-08 19:23:16 -0800621 skb->ip_summed = CHECKSUM_UNNECESSARY;
KY Srinivasane3d605e2014-03-08 19:23:16 -0800622 }
623
KY Srinivasan760d1e32015-12-01 16:43:19 -0800624 if (vlan_tci & VLAN_TAG_PRESENT)
Haiyang Zhang93725cb2013-06-17 15:36:49 -0700625 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
KY Srinivasan760d1e32015-12-01 16:43:19 -0800626 vlan_tci);
Hank Janssenfceaf242009-07-13 15:34:54 -0700627
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700628 return skb;
629}
630
631/*
632 * netvsc_recv_callback - Callback when we receive a packet from the
633 * "wire" on the specified device.
634 */
635int netvsc_recv_callback(struct hv_device *device_obj,
636 struct hv_netvsc_packet *packet,
637 void **data,
638 struct ndis_tcp_ip_checksum_info *csum_info,
639 struct vmbus_channel *channel,
640 u16 vlan_tci)
641{
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200642 struct net_device *net = hv_get_drvdata(device_obj);
643 struct net_device_context *net_device_ctx = netdev_priv(net);
Stephen Hemmingerf207c102016-09-22 16:56:33 -0700644 struct net_device *vf_netdev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700645 struct sk_buff *skb;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700646 struct netvsc_stats *rx_stats;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700647
Stephen Hemminger9cbcc422016-09-22 16:56:34 -0700648 if (net->reg_state != NETREG_REGISTERED)
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700649 return NVSP_STAT_FAIL;
650
Stephen Hemminger9cbcc422016-09-22 16:56:34 -0700651 /*
652 * If necessary, inject this packet into the VF interface.
653 * On Hyper-V, multicast and brodcast packets are only delivered
654 * to the synthetic interface (after subjecting these to
655 * policy filters on the host). Deliver these via the VF
656 * interface in the guest.
657 */
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? */
663 skb = netvsc_alloc_recv_skb(net, packet, csum_info, *data, vlan_tci);
664 if (unlikely(!skb)) {
665 ++net->stats.rx_dropped;
666 return NVSP_STAT_FAIL;
667 }
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700668
Stephen Hemminger9cbcc422016-09-22 16:56:34 -0700669 if (net != vf_netdev)
670 skb_record_rx_queue(skb,
671 channel->offermsg.offer.sub_channel_index);
672
673 /*
674 * Even if injecting the packet, record the statistics
675 * on the synthetic device because modifying the VF device
676 * statistics will not work correctly.
677 */
678 rx_stats = this_cpu_ptr(net_device_ctx->rx_stats);
sixiao@microsoft.com4b02b582015-05-15 02:33:03 -0700679 u64_stats_update_begin(&rx_stats->syncp);
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700680 rx_stats->packets++;
681 rx_stats->bytes += packet->total_data_buflen;
Stephen Hemmingerf7ad75b2016-09-22 16:56:35 -0700682
683 if (skb->pkt_type == PACKET_BROADCAST)
684 ++rx_stats->broadcast;
685 else if (skb->pkt_type == PACKET_MULTICAST)
686 ++rx_stats->multicast;
sixiao@microsoft.com4b02b582015-05-15 02:33:03 -0700687 u64_stats_update_end(&rx_stats->syncp);
Stephen Hemminger9495c282010-03-09 17:42:17 -0800688
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700689 /*
690 * Pass the skb back up. Network stack will deallocate the skb when it
Stephen Hemminger9495c282010-03-09 17:42:17 -0800691 * is done.
692 * TODO - use NAPI?
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700693 */
Stephen Hemminger9495c282010-03-09 17:42:17 -0800694 netif_rx(skb);
Hank Janssenfceaf242009-07-13 15:34:54 -0700695
Hank Janssenfceaf242009-07-13 15:34:54 -0700696 return 0;
697}
698
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -0700699static void netvsc_get_drvinfo(struct net_device *net,
700 struct ethtool_drvinfo *info)
701{
Jiri Pirko7826d432013-01-06 00:44:26 +0000702 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
Jiri Pirko7826d432013-01-06 00:44:26 +0000703 strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -0700704}
705
Andrew Schwartzmeyer59995372015-02-26 16:27:14 -0800706static void netvsc_get_channels(struct net_device *net,
707 struct ethtool_channels *channel)
708{
709 struct net_device_context *net_device_ctx = netdev_priv(net);
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200710 struct netvsc_device *nvdev = net_device_ctx->nvdev;
Andrew Schwartzmeyer59995372015-02-26 16:27:14 -0800711
712 if (nvdev) {
713 channel->max_combined = nvdev->max_chn;
714 channel->combined_count = nvdev->num_chn;
715 }
716}
717
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700718static int netvsc_set_channels(struct net_device *net,
719 struct ethtool_channels *channels)
720{
721 struct net_device_context *net_device_ctx = netdev_priv(net);
722 struct hv_device *dev = net_device_ctx->device_ctx;
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200723 struct netvsc_device *nvdev = net_device_ctx->nvdev;
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700724 struct netvsc_device_info device_info;
Andrew Schwartzmeyer954591b2015-08-18 20:06:32 -0700725 u32 num_chn;
726 u32 max_chn;
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700727 int ret = 0;
728 bool recovering = false;
729
Vitaly Kuznetsov6da72252016-05-13 13:55:24 +0200730 if (net_device_ctx->start_remove || !nvdev || nvdev->destroy)
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700731 return -ENODEV;
732
Andrew Schwartzmeyer954591b2015-08-18 20:06:32 -0700733 num_chn = nvdev->num_chn;
734 max_chn = min_t(u32, nvdev->max_chn, num_online_cpus());
735
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700736 if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5) {
737 pr_info("vRSS unsupported before NVSP Version 5\n");
738 return -EINVAL;
739 }
740
741 /* We do not support rx, tx, or other */
742 if (!channels ||
743 channels->rx_count ||
744 channels->tx_count ||
745 channels->other_count ||
746 (channels->combined_count < 1))
747 return -EINVAL;
748
749 if (channels->combined_count > max_chn) {
750 pr_info("combined channels too high, using %d\n", max_chn);
751 channels->combined_count = max_chn;
752 }
753
754 ret = netvsc_close(net);
755 if (ret)
756 goto out;
757
758 do_set:
Vitaly Kuznetsovf580aec2016-05-13 13:55:20 +0200759 net_device_ctx->start_remove = true;
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700760 rndis_filter_device_remove(dev);
761
762 nvdev->num_chn = channels->combined_count;
763
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700764 memset(&device_info, 0, sizeof(device_info));
765 device_info.num_chn = nvdev->num_chn; /* passed to RNDIS */
766 device_info.ring_size = ring_size;
767 device_info.max_num_vrss_chns = max_num_vrss_chns;
768
769 ret = rndis_filter_device_add(dev, &device_info);
770 if (ret) {
771 if (recovering) {
772 netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
773 return ret;
774 }
775 goto recover;
776 }
777
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200778 nvdev = net_device_ctx->nvdev;
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700779
780 ret = netif_set_real_num_tx_queues(net, nvdev->num_chn);
781 if (ret) {
782 if (recovering) {
783 netdev_err(net, "could not set tx queue count (ret %d)\n", ret);
784 return ret;
785 }
786 goto recover;
787 }
788
789 ret = netif_set_real_num_rx_queues(net, nvdev->num_chn);
790 if (ret) {
791 if (recovering) {
792 netdev_err(net, "could not set rx queue count (ret %d)\n", ret);
793 return ret;
794 }
795 goto recover;
796 }
797
798 out:
799 netvsc_open(net);
Vitaly Kuznetsovf580aec2016-05-13 13:55:20 +0200800 net_device_ctx->start_remove = false;
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +0200801 /* We may have missed link change notifications */
802 schedule_delayed_work(&net_device_ctx->dwork, 0);
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700803
804 return ret;
805
806 recover:
807 /* If the above failed, we attempt to recover through the same
808 * process but with the original number of channels.
809 */
810 netdev_err(net, "could not set channels, recovering\n");
811 recovering = true;
812 channels->combined_count = num_chn;
813 goto do_set;
814}
815
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800816static bool netvsc_validate_ethtool_ss_cmd(const struct ethtool_cmd *cmd)
817{
818 struct ethtool_cmd diff1 = *cmd;
819 struct ethtool_cmd diff2 = {};
820
821 ethtool_cmd_speed_set(&diff1, 0);
822 diff1.duplex = 0;
823 /* advertising and cmd are usually set */
824 diff1.advertising = 0;
825 diff1.cmd = 0;
826 /* We set port to PORT_OTHER */
827 diff2.port = PORT_OTHER;
828
829 return !memcmp(&diff1, &diff2, sizeof(diff1));
830}
831
832static void netvsc_init_settings(struct net_device *dev)
833{
834 struct net_device_context *ndc = netdev_priv(dev);
835
836 ndc->speed = SPEED_UNKNOWN;
837 ndc->duplex = DUPLEX_UNKNOWN;
838}
839
840static int netvsc_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
841{
842 struct net_device_context *ndc = netdev_priv(dev);
843
844 ethtool_cmd_speed_set(cmd, ndc->speed);
845 cmd->duplex = ndc->duplex;
846 cmd->port = PORT_OTHER;
847
848 return 0;
849}
850
851static int netvsc_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
852{
853 struct net_device_context *ndc = netdev_priv(dev);
854 u32 speed;
855
856 speed = ethtool_cmd_speed(cmd);
857 if (!ethtool_validate_speed(speed) ||
858 !ethtool_validate_duplex(cmd->duplex) ||
859 !netvsc_validate_ethtool_ss_cmd(cmd))
860 return -EINVAL;
861
862 ndc->speed = speed;
863 ndc->duplex = cmd->duplex;
864
865 return 0;
866}
867
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800868static int netvsc_change_mtu(struct net_device *ndev, int mtu)
869{
870 struct net_device_context *ndevctx = netdev_priv(ndev);
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200871 struct netvsc_device *nvdev = ndevctx->nvdev;
872 struct hv_device *hdev = ndevctx->device_ctx;
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800873 struct netvsc_device_info device_info;
874 int limit = ETH_DATA_LEN;
Haiyang Zhangd212b462016-03-23 09:43:09 -0700875 u32 num_chn;
Haiyang Zhang2de85302015-07-13 13:09:16 -0700876 int ret = 0;
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800877
Vitaly Kuznetsov6da72252016-05-13 13:55:24 +0200878 if (ndevctx->start_remove || !nvdev || nvdev->destroy)
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800879 return -ENODEV;
880
Haiyang Zhanga1eabb02014-02-19 15:49:45 -0800881 if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
Haiyang Zhang4d3c9d32014-11-12 14:07:44 -0800882 limit = NETVSC_MTU - ETH_HLEN;
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800883
Haiyang Zhangf9cbce32015-07-06 14:11:37 -0700884 if (mtu < NETVSC_MTU_MIN || mtu > limit)
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800885 return -EINVAL;
886
Haiyang Zhang2de85302015-07-13 13:09:16 -0700887 ret = netvsc_close(ndev);
888 if (ret)
889 goto out;
890
Haiyang Zhangd212b462016-03-23 09:43:09 -0700891 num_chn = nvdev->num_chn;
892
Vitaly Kuznetsovf580aec2016-05-13 13:55:20 +0200893 ndevctx->start_remove = true;
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800894 rndis_filter_device_remove(hdev);
895
896 ndev->mtu = mtu;
897
Andrew Schwartzmeyer8ebdcc52015-08-11 17:14:31 -0700898 memset(&device_info, 0, sizeof(device_info));
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800899 device_info.ring_size = ring_size;
Haiyang Zhangd212b462016-03-23 09:43:09 -0700900 device_info.num_chn = num_chn;
KY Srinivasane01ec212015-05-27 13:16:57 -0700901 device_info.max_num_vrss_chns = max_num_vrss_chns;
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800902 rndis_filter_device_add(hdev, &device_info);
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800903
Haiyang Zhang2de85302015-07-13 13:09:16 -0700904out:
905 netvsc_open(ndev);
Vitaly Kuznetsovf580aec2016-05-13 13:55:20 +0200906 ndevctx->start_remove = false;
Haiyang Zhang2de85302015-07-13 13:09:16 -0700907
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +0200908 /* We may have missed link change notifications */
909 schedule_delayed_work(&ndevctx->dwork, 0);
910
Haiyang Zhang2de85302015-07-13 13:09:16 -0700911 return ret;
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800912}
913
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700914static struct rtnl_link_stats64 *netvsc_get_stats64(struct net_device *net,
915 struct rtnl_link_stats64 *t)
916{
917 struct net_device_context *ndev_ctx = netdev_priv(net);
918 int cpu;
919
920 for_each_possible_cpu(cpu) {
921 struct netvsc_stats *tx_stats = per_cpu_ptr(ndev_ctx->tx_stats,
922 cpu);
923 struct netvsc_stats *rx_stats = per_cpu_ptr(ndev_ctx->rx_stats,
924 cpu);
Stephen Hemmingerf7ad75b2016-09-22 16:56:35 -0700925 u64 tx_packets, tx_bytes, rx_packets, rx_bytes, rx_multicast;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700926 unsigned int start;
927
928 do {
sixiao@microsoft.com4b02b582015-05-15 02:33:03 -0700929 start = u64_stats_fetch_begin_irq(&tx_stats->syncp);
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700930 tx_packets = tx_stats->packets;
931 tx_bytes = tx_stats->bytes;
sixiao@microsoft.com4b02b582015-05-15 02:33:03 -0700932 } while (u64_stats_fetch_retry_irq(&tx_stats->syncp, start));
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700933
934 do {
sixiao@microsoft.com4b02b582015-05-15 02:33:03 -0700935 start = u64_stats_fetch_begin_irq(&rx_stats->syncp);
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700936 rx_packets = rx_stats->packets;
937 rx_bytes = rx_stats->bytes;
Stephen Hemmingerf7ad75b2016-09-22 16:56:35 -0700938 rx_multicast = rx_stats->multicast + rx_stats->broadcast;
sixiao@microsoft.com4b02b582015-05-15 02:33:03 -0700939 } while (u64_stats_fetch_retry_irq(&rx_stats->syncp, start));
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700940
941 t->tx_bytes += tx_bytes;
942 t->tx_packets += tx_packets;
943 t->rx_bytes += rx_bytes;
944 t->rx_packets += rx_packets;
Stephen Hemmingerf7ad75b2016-09-22 16:56:35 -0700945 t->multicast += rx_multicast;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700946 }
947
948 t->tx_dropped = net->stats.tx_dropped;
949 t->tx_errors = net->stats.tx_dropped;
950
951 t->rx_dropped = net->stats.rx_dropped;
952 t->rx_errors = net->stats.rx_errors;
953
954 return t;
955}
Haiyang Zhang1ce09e82012-07-10 07:19:22 +0000956
957static int netvsc_set_mac_addr(struct net_device *ndev, void *p)
958{
Haiyang Zhang1ce09e82012-07-10 07:19:22 +0000959 struct sockaddr *addr = p;
Jianjun Kong9a4c8312013-01-18 16:52:09 +0000960 char save_adr[ETH_ALEN];
Haiyang Zhang1ce09e82012-07-10 07:19:22 +0000961 unsigned char save_aatype;
962 int err;
963
964 memcpy(save_adr, ndev->dev_addr, ETH_ALEN);
965 save_aatype = ndev->addr_assign_type;
966
967 err = eth_mac_addr(ndev, p);
968 if (err != 0)
969 return err;
970
Vitaly Kuznetsove834da9a2016-06-03 17:51:01 +0200971 err = rndis_filter_set_device_mac(ndev, addr->sa_data);
Haiyang Zhang1ce09e82012-07-10 07:19:22 +0000972 if (err != 0) {
973 /* roll back to saved MAC */
974 memcpy(ndev->dev_addr, save_adr, ETH_ALEN);
975 ndev->addr_assign_type = save_aatype;
976 }
977
978 return err;
979}
980
Stephen Hemminger4323b472016-08-23 12:17:57 -0700981static const struct {
982 char name[ETH_GSTRING_LEN];
983 u16 offset;
984} netvsc_stats[] = {
985 { "tx_scattered", offsetof(struct netvsc_ethtool_stats, tx_scattered) },
986 { "tx_no_memory", offsetof(struct netvsc_ethtool_stats, tx_no_memory) },
987 { "tx_no_space", offsetof(struct netvsc_ethtool_stats, tx_no_space) },
988 { "tx_too_big", offsetof(struct netvsc_ethtool_stats, tx_too_big) },
989 { "tx_busy", offsetof(struct netvsc_ethtool_stats, tx_busy) },
990};
991
992static int netvsc_get_sset_count(struct net_device *dev, int string_set)
993{
994 switch (string_set) {
995 case ETH_SS_STATS:
996 return ARRAY_SIZE(netvsc_stats);
997 default:
998 return -EINVAL;
999 }
1000}
1001
1002static void netvsc_get_ethtool_stats(struct net_device *dev,
1003 struct ethtool_stats *stats, u64 *data)
1004{
1005 struct net_device_context *ndc = netdev_priv(dev);
1006 const void *nds = &ndc->eth_stats;
1007 int i;
1008
1009 for (i = 0; i < ARRAY_SIZE(netvsc_stats); i++)
1010 data[i] = *(unsigned long *)(nds + netvsc_stats[i].offset);
1011}
1012
1013static void netvsc_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1014{
1015 int i;
1016
1017 switch (stringset) {
1018 case ETH_SS_STATS:
1019 for (i = 0; i < ARRAY_SIZE(netvsc_stats); i++)
1020 memcpy(data + i * ETH_GSTRING_LEN,
1021 netvsc_stats[i].name, ETH_GSTRING_LEN);
1022 break;
1023 }
1024}
1025
Richard Weinberger316158f2014-07-09 16:23:59 +02001026#ifdef CONFIG_NET_POLL_CONTROLLER
1027static void netvsc_poll_controller(struct net_device *net)
1028{
1029 /* As netvsc_start_xmit() works synchronous we don't have to
1030 * trigger anything here.
1031 */
1032}
1033#endif
Haiyang Zhang1ce09e82012-07-10 07:19:22 +00001034
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -07001035static const struct ethtool_ops ethtool_ops = {
1036 .get_drvinfo = netvsc_get_drvinfo,
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -07001037 .get_link = ethtool_op_get_link,
Stephen Hemminger4323b472016-08-23 12:17:57 -07001038 .get_ethtool_stats = netvsc_get_ethtool_stats,
1039 .get_sset_count = netvsc_get_sset_count,
1040 .get_strings = netvsc_get_strings,
Andrew Schwartzmeyer59995372015-02-26 16:27:14 -08001041 .get_channels = netvsc_get_channels,
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -07001042 .set_channels = netvsc_set_channels,
sixiao@microsoft.com76d13b52016-02-17 16:43:59 -08001043 .get_ts_info = ethtool_op_get_ts_info,
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -08001044 .get_settings = netvsc_get_settings,
1045 .set_settings = netvsc_set_settings,
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -07001046};
1047
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001048static const struct net_device_ops device_ops = {
1049 .ndo_open = netvsc_open,
1050 .ndo_stop = netvsc_close,
1051 .ndo_start_xmit = netvsc_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001052 .ndo_set_rx_mode = netvsc_set_multicast_list,
Haiyang Zhang4d447c92011-12-15 13:45:17 -08001053 .ndo_change_mtu = netvsc_change_mtu,
Haiyang Zhangb681b582010-08-03 19:15:31 +00001054 .ndo_validate_addr = eth_validate_addr,
Haiyang Zhang1ce09e82012-07-10 07:19:22 +00001055 .ndo_set_mac_address = netvsc_set_mac_addr,
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001056 .ndo_select_queue = netvsc_select_queue,
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001057 .ndo_get_stats64 = netvsc_get_stats64,
Richard Weinberger316158f2014-07-09 16:23:59 +02001058#ifdef CONFIG_NET_POLL_CONTROLLER
1059 .ndo_poll_controller = netvsc_poll_controller,
1060#endif
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001061};
1062
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001063/*
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001064 * Handle link status changes. For RNDIS_STATUS_NETWORK_CHANGE emulate link
1065 * down/up sequence. In case of RNDIS_STATUS_MEDIA_CONNECT when carrier is
1066 * present send GARP packet to network peers with netif_notify_peers().
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001067 */
Haiyang Zhang891de742014-02-12 16:54:27 -08001068static void netvsc_link_change(struct work_struct *w)
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001069{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001070 struct net_device_context *ndev_ctx =
1071 container_of(w, struct net_device_context, dwork.work);
1072 struct hv_device *device_obj = ndev_ctx->device_ctx;
1073 struct net_device *net = hv_get_drvdata(device_obj);
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -07001074 struct netvsc_device *net_device;
Haiyang Zhang891de742014-02-12 16:54:27 -08001075 struct rndis_device *rdev;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001076 struct netvsc_reconfig *event = NULL;
1077 bool notify = false, reschedule = false;
1078 unsigned long flags, next_reconfig, delay;
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001079
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001080 rtnl_lock();
1081 if (ndev_ctx->start_remove)
1082 goto out_unlock;
1083
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +02001084 net_device = ndev_ctx->nvdev;
Haiyang Zhang891de742014-02-12 16:54:27 -08001085 rdev = net_device->extension;
Haiyang Zhang891de742014-02-12 16:54:27 -08001086
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001087 next_reconfig = ndev_ctx->last_reconfig + LINKCHANGE_INT;
1088 if (time_is_after_jiffies(next_reconfig)) {
1089 /* link_watch only sends one notification with current state
1090 * per second, avoid doing reconfig more frequently. Handle
1091 * wrap around.
1092 */
1093 delay = next_reconfig - jiffies;
1094 delay = delay < LINKCHANGE_INT ? delay : LINKCHANGE_INT;
1095 schedule_delayed_work(&ndev_ctx->dwork, delay);
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001096 goto out_unlock;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001097 }
1098 ndev_ctx->last_reconfig = jiffies;
1099
1100 spin_lock_irqsave(&ndev_ctx->lock, flags);
1101 if (!list_empty(&ndev_ctx->reconfig_events)) {
1102 event = list_first_entry(&ndev_ctx->reconfig_events,
1103 struct netvsc_reconfig, list);
1104 list_del(&event->list);
1105 reschedule = !list_empty(&ndev_ctx->reconfig_events);
1106 }
1107 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1108
1109 if (!event)
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001110 goto out_unlock;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001111
1112 switch (event->event) {
1113 /* Only the following events are possible due to the check in
1114 * netvsc_linkstatus_callback()
1115 */
1116 case RNDIS_STATUS_MEDIA_CONNECT:
1117 if (rdev->link_state) {
1118 rdev->link_state = false;
1119 netif_carrier_on(net);
1120 netif_tx_wake_all_queues(net);
1121 } else {
1122 notify = true;
Haiyang Zhang3a494e72014-06-19 18:34:36 -07001123 }
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001124 kfree(event);
1125 break;
1126 case RNDIS_STATUS_MEDIA_DISCONNECT:
1127 if (!rdev->link_state) {
1128 rdev->link_state = true;
1129 netif_carrier_off(net);
1130 netif_tx_stop_all_queues(net);
1131 }
1132 kfree(event);
1133 break;
1134 case RNDIS_STATUS_NETWORK_CHANGE:
1135 /* Only makes sense if carrier is present */
1136 if (!rdev->link_state) {
1137 rdev->link_state = true;
1138 netif_carrier_off(net);
1139 netif_tx_stop_all_queues(net);
1140 event->event = RNDIS_STATUS_MEDIA_CONNECT;
1141 spin_lock_irqsave(&ndev_ctx->lock, flags);
Haiyang Zhang15cfd402016-04-21 16:13:01 -07001142 list_add(&event->list, &ndev_ctx->reconfig_events);
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001143 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1144 reschedule = true;
1145 }
1146 break;
Haiyang Zhang891de742014-02-12 16:54:27 -08001147 }
1148
1149 rtnl_unlock();
1150
1151 if (notify)
1152 netdev_notify_peers(net);
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001153
1154 /* link_watch only sends one notification with current state per
1155 * second, handle next reconfig event in 2 seconds.
1156 */
1157 if (reschedule)
1158 schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001159
1160 return;
1161
1162out_unlock:
1163 rtnl_unlock();
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001164}
1165
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001166static void netvsc_free_netdev(struct net_device *netdev)
1167{
1168 struct net_device_context *net_device_ctx = netdev_priv(netdev);
1169
1170 free_percpu(net_device_ctx->tx_stats);
1171 free_percpu(net_device_ctx->rx_stats);
1172 free_netdev(netdev);
1173}
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001174
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001175static struct net_device *get_netvsc_bymac(const u8 *mac)
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001176{
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001177 struct net_device *dev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001178
Stephen Hemminger8737caa2016-08-23 12:17:44 -07001179 ASSERT_RTNL();
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001180
1181 for_each_netdev(&init_net, dev) {
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001182 if (dev->netdev_ops != &device_ops)
1183 continue; /* not a netvsc device */
1184
1185 if (ether_addr_equal(mac, dev->perm_addr))
1186 return dev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001187 }
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001188
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001189 return NULL;
1190}
1191
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001192static struct net_device *get_netvsc_byref(struct net_device *vf_netdev)
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001193{
1194 struct net_device *dev;
1195
1196 ASSERT_RTNL();
1197
1198 for_each_netdev(&init_net, dev) {
1199 struct net_device_context *net_device_ctx;
1200
1201 if (dev->netdev_ops != &device_ops)
1202 continue; /* not a netvsc device */
1203
1204 net_device_ctx = netdev_priv(dev);
1205 if (net_device_ctx->nvdev == NULL)
1206 continue; /* device is removed */
1207
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001208 if (rtnl_dereference(net_device_ctx->vf_netdev) == vf_netdev)
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001209 return dev; /* a match */
1210 }
1211
1212 return NULL;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001213}
1214
1215static int netvsc_register_vf(struct net_device *vf_netdev)
1216{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001217 struct net_device *ndev;
1218 struct net_device_context *net_device_ctx;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001219 struct netvsc_device *netvsc_dev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001220
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001221 if (vf_netdev->addr_len != ETH_ALEN)
1222 return NOTIFY_DONE;
1223
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001224 /*
1225 * We will use the MAC address to locate the synthetic interface to
1226 * associate with the VF interface. If we don't find a matching
1227 * synthetic interface, move on.
1228 */
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001229 ndev = get_netvsc_bymac(vf_netdev->perm_addr);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001230 if (!ndev)
1231 return NOTIFY_DONE;
1232
1233 net_device_ctx = netdev_priv(ndev);
1234 netvsc_dev = net_device_ctx->nvdev;
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001235 if (!netvsc_dev || rtnl_dereference(net_device_ctx->vf_netdev))
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001236 return NOTIFY_DONE;
1237
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001238 netdev_info(ndev, "VF registering: %s\n", vf_netdev->name);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001239 /*
1240 * Take a reference on the module.
1241 */
1242 try_module_get(THIS_MODULE);
Stephen Hemminger07d0f002016-09-22 16:56:30 -07001243
1244 dev_hold(vf_netdev);
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001245 rcu_assign_pointer(net_device_ctx->vf_netdev, vf_netdev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001246 return NOTIFY_OK;
1247}
1248
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001249static int netvsc_vf_up(struct net_device *vf_netdev)
1250{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001251 struct net_device *ndev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001252 struct netvsc_device *netvsc_dev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001253 struct net_device_context *net_device_ctx;
1254
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001255 ndev = get_netvsc_byref(vf_netdev);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001256 if (!ndev)
1257 return NOTIFY_DONE;
1258
1259 net_device_ctx = netdev_priv(ndev);
1260 netvsc_dev = net_device_ctx->nvdev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001261
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001262 netdev_info(ndev, "VF up: %s\n", vf_netdev->name);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001263
1264 /*
1265 * Open the device before switching data path.
1266 */
Vitaly Kuznetsov2f5fa6c2016-06-03 17:51:00 +02001267 rndis_filter_open(netvsc_dev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001268
1269 /*
1270 * notify the host to switch the data path.
1271 */
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001272 netvsc_switch_datapath(ndev, true);
1273 netdev_info(ndev, "Data path switched to VF: %s\n", vf_netdev->name);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001274
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001275 netif_carrier_off(ndev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001276
Vitaly Kuznetsovd0722182016-08-15 17:48:40 +02001277 /* Now notify peers through VF device. */
1278 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, vf_netdev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001279
1280 return NOTIFY_OK;
1281}
1282
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001283static int netvsc_vf_down(struct net_device *vf_netdev)
1284{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001285 struct net_device *ndev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001286 struct netvsc_device *netvsc_dev;
1287 struct net_device_context *net_device_ctx;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001288
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001289 ndev = get_netvsc_byref(vf_netdev);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001290 if (!ndev)
1291 return NOTIFY_DONE;
1292
1293 net_device_ctx = netdev_priv(ndev);
1294 netvsc_dev = net_device_ctx->nvdev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001295
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001296 netdev_info(ndev, "VF down: %s\n", vf_netdev->name);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001297 netvsc_switch_datapath(ndev, false);
1298 netdev_info(ndev, "Data path switched from VF: %s\n", vf_netdev->name);
Vitaly Kuznetsov2f5fa6c2016-06-03 17:51:00 +02001299 rndis_filter_close(netvsc_dev);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001300 netif_carrier_on(ndev);
Vitaly Kuznetsovd0722182016-08-15 17:48:40 +02001301
1302 /* Now notify peers through netvsc device. */
1303 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, ndev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001304
1305 return NOTIFY_OK;
1306}
1307
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001308static int netvsc_unregister_vf(struct net_device *vf_netdev)
1309{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001310 struct net_device *ndev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001311 struct netvsc_device *netvsc_dev;
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001312 struct net_device_context *net_device_ctx;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001313
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001314 ndev = get_netvsc_byref(vf_netdev);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001315 if (!ndev)
1316 return NOTIFY_DONE;
1317
1318 net_device_ctx = netdev_priv(ndev);
1319 netvsc_dev = net_device_ctx->nvdev;
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001320
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001321 netdev_info(ndev, "VF unregistering: %s\n", vf_netdev->name);
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001322
1323 RCU_INIT_POINTER(net_device_ctx->vf_netdev, NULL);
Stephen Hemminger07d0f002016-09-22 16:56:30 -07001324 dev_put(vf_netdev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001325 module_put(THIS_MODULE);
1326 return NOTIFY_OK;
1327}
1328
K. Y. Srinivasan84946892011-09-13 10:59:38 -07001329static int netvsc_probe(struct hv_device *dev,
1330 const struct hv_vmbus_device_id *dev_id)
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001331{
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001332 struct net_device *net = NULL;
1333 struct net_device_context *net_device_ctx;
1334 struct netvsc_device_info device_info;
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001335 struct netvsc_device *nvdev;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001336 int ret;
1337
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001338 net = alloc_etherdev_mq(sizeof(struct net_device_context),
1339 num_online_cpus());
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001340 if (!net)
K. Y. Srinivasan51a805d2011-08-25 09:49:11 -07001341 return -ENOMEM;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001342
Haiyang Zhang1b07da52014-03-04 14:11:06 -08001343 netif_carrier_off(net);
1344
Haiyang Zhangb37879e2016-08-04 10:42:14 -07001345 netvsc_init_settings(net);
1346
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001347 net_device_ctx = netdev_priv(net);
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -07001348 net_device_ctx->device_ctx = dev;
Simon Xiao3f300ff2015-04-28 01:05:17 -07001349 net_device_ctx->msg_enable = netif_msg_init(debug, default_msg);
1350 if (netif_msg_probe(net_device_ctx))
1351 netdev_dbg(net, "netvsc msg_enable: %d\n",
1352 net_device_ctx->msg_enable);
1353
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001354 net_device_ctx->tx_stats = netdev_alloc_pcpu_stats(struct netvsc_stats);
1355 if (!net_device_ctx->tx_stats) {
1356 free_netdev(net);
1357 return -ENOMEM;
1358 }
1359 net_device_ctx->rx_stats = netdev_alloc_pcpu_stats(struct netvsc_stats);
1360 if (!net_device_ctx->rx_stats) {
1361 free_percpu(net_device_ctx->tx_stats);
1362 free_netdev(net);
1363 return -ENOMEM;
1364 }
1365
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -07001366 hv_set_drvdata(dev, net);
Vitaly Kuznetsovf580aec2016-05-13 13:55:20 +02001367
1368 net_device_ctx->start_remove = false;
1369
Haiyang Zhang891de742014-02-12 16:54:27 -08001370 INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change);
Wenqi Ma792df872012-04-19 00:39:37 +00001371 INIT_WORK(&net_device_ctx->work, do_set_multicast);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001372
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001373 spin_lock_init(&net_device_ctx->lock);
1374 INIT_LIST_HEAD(&net_device_ctx->reconfig_events);
1375
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001376 net->netdev_ops = &device_ops;
1377
sixiao@microsoft.coma0606792016-02-04 15:49:34 -08001378 net->hw_features = NETVSC_HW_FEATURES;
1379 net->features = NETVSC_HW_FEATURES | NETIF_F_HW_VLAN_CTAG_TX;
Stephen Hemminger60487182010-05-04 09:58:55 -07001380
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001381 net->ethtool_ops = &ethtool_ops;
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -07001382 SET_NETDEV_DEV(net, &dev->device);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001383
Vitaly Kuznetsov14a03cf2016-02-05 17:29:08 +01001384 /* We always need headroom for rndis header */
1385 net->needed_headroom = RNDIS_AND_PPI_SIZE;
1386
Haiyang Zhang692e0842011-09-01 12:19:43 -07001387 /* Notify the netvsc driver of the new device */
Andrew Schwartzmeyer8ebdcc52015-08-11 17:14:31 -07001388 memset(&device_info, 0, sizeof(device_info));
Haiyang Zhang692e0842011-09-01 12:19:43 -07001389 device_info.ring_size = ring_size;
KY Srinivasane01ec212015-05-27 13:16:57 -07001390 device_info.max_num_vrss_chns = max_num_vrss_chns;
Haiyang Zhang692e0842011-09-01 12:19:43 -07001391 ret = rndis_filter_device_add(dev, &device_info);
1392 if (ret != 0) {
1393 netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001394 netvsc_free_netdev(net);
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -07001395 hv_set_drvdata(dev, NULL);
Haiyang Zhang692e0842011-09-01 12:19:43 -07001396 return ret;
1397 }
1398 memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
1399
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +02001400 nvdev = net_device_ctx->nvdev;
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001401 netif_set_real_num_tx_queues(net, nvdev->num_chn);
1402 netif_set_real_num_rx_queues(net, nvdev->num_chn);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001403
Haiyang Zhanga68f9612013-12-20 16:52:31 -08001404 ret = register_netdev(net);
1405 if (ret != 0) {
1406 pr_err("Unable to register netdev.\n");
1407 rndis_filter_device_remove(dev);
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001408 netvsc_free_netdev(net);
Haiyang Zhanga68f9612013-12-20 16:52:31 -08001409 }
1410
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001411 return ret;
1412}
1413
K. Y. Srinivasan415b0232011-04-29 13:45:12 -07001414static int netvsc_remove(struct hv_device *dev)
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001415{
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -07001416 struct net_device *net;
Haiyang Zhang122a5f62011-05-27 06:21:55 -07001417 struct net_device_context *ndev_ctx;
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -07001418 struct netvsc_device *net_device;
1419
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +02001420 net = hv_get_drvdata(dev);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001421
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001422 if (net == NULL) {
K. Y. Srinivasan415b0232011-04-29 13:45:12 -07001423 dev_err(&dev->device, "No net device to remove\n");
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001424 return 0;
1425 }
1426
Haiyang Zhang122a5f62011-05-27 06:21:55 -07001427 ndev_ctx = netdev_priv(net);
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +02001428 net_device = ndev_ctx->nvdev;
1429
Vitaly Kuznetsov6da72252016-05-13 13:55:24 +02001430 /* Avoid racing with netvsc_change_mtu()/netvsc_set_channels()
1431 * removing the device.
1432 */
1433 rtnl_lock();
Vitaly Kuznetsovf580aec2016-05-13 13:55:20 +02001434 ndev_ctx->start_remove = true;
Vitaly Kuznetsov6da72252016-05-13 13:55:24 +02001435 rtnl_unlock();
Vitaly Kuznetsovf580aec2016-05-13 13:55:20 +02001436
Haiyang Zhang122a5f62011-05-27 06:21:55 -07001437 cancel_delayed_work_sync(&ndev_ctx->dwork);
Wenqi Ma792df872012-04-19 00:39:37 +00001438 cancel_work_sync(&ndev_ctx->work);
Haiyang Zhang122a5f62011-05-27 06:21:55 -07001439
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001440 /* Stop outbound asap */
Haiyang Zhang0a282532012-02-02 07:17:59 +00001441 netif_tx_disable(net);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001442
1443 unregister_netdev(net);
1444
1445 /*
1446 * Call to the vsc driver to let it know that the device is being
1447 * removed
1448 */
Haiyang Zhangdf06bcf2011-05-23 09:03:47 -07001449 rndis_filter_device_remove(dev);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001450
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +02001451 hv_set_drvdata(dev, NULL);
1452
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001453 netvsc_free_netdev(net);
Haiyang Zhangdf06bcf2011-05-23 09:03:47 -07001454 return 0;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001455}
1456
K. Y. Srinivasan345c4cc2011-08-25 09:48:34 -07001457static const struct hv_vmbus_device_id id_table[] = {
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07001458 /* Network guid */
K. Y. Srinivasan8f505942013-01-23 17:42:42 -08001459 { HV_NIC_GUID, },
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07001460 { },
K. Y. Srinivasan345c4cc2011-08-25 09:48:34 -07001461};
1462
1463MODULE_DEVICE_TABLE(vmbus, id_table);
1464
K. Y. Srinivasanf1542a62011-05-10 07:55:16 -07001465/* The one and only one */
K. Y. Srinivasanfde0ef92011-05-12 19:35:08 -07001466static struct hv_driver netvsc_drv = {
Haiyang Zhangd31b20f2012-03-07 10:02:00 +00001467 .name = KBUILD_MODNAME,
K. Y. Srinivasan345c4cc2011-08-25 09:48:34 -07001468 .id_table = id_table,
K. Y. Srinivasanfde0ef92011-05-12 19:35:08 -07001469 .probe = netvsc_probe,
1470 .remove = netvsc_remove,
K. Y. Srinivasand4890972011-05-10 07:55:17 -07001471};
K. Y. Srinivasanf1542a62011-05-10 07:55:16 -07001472
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001473/*
1474 * On Hyper-V, every VF interface is matched with a corresponding
1475 * synthetic interface. The synthetic interface is presented first
1476 * to the guest. When the corresponding VF instance is registered,
1477 * we will take care of switching the data path.
1478 */
1479static int netvsc_netdev_event(struct notifier_block *this,
1480 unsigned long event, void *ptr)
1481{
1482 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
1483
Stephen Hemmingeree837a12016-09-22 16:56:31 -07001484 /* Skip our own events */
1485 if (event_dev->netdev_ops == &device_ops)
1486 return NOTIFY_DONE;
1487
1488 /* Avoid non-Ethernet type devices */
1489 if (event_dev->type != ARPHRD_ETHER)
1490 return NOTIFY_DONE;
1491
Vitaly Kuznetsov0dbff142016-08-15 17:48:43 +02001492 /* Avoid Vlan dev with same MAC registering as VF */
1493 if (event_dev->priv_flags & IFF_802_1Q_VLAN)
1494 return NOTIFY_DONE;
1495
1496 /* Avoid Bonding master dev with same MAC registering as VF */
Stephen Hemmingeree837a12016-09-22 16:56:31 -07001497 if ((event_dev->priv_flags & IFF_BONDING) &&
1498 (event_dev->flags & IFF_MASTER))
Haiyang Zhangcb2911f2016-06-02 12:02:04 -07001499 return NOTIFY_DONE;
1500
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001501 switch (event) {
1502 case NETDEV_REGISTER:
1503 return netvsc_register_vf(event_dev);
1504 case NETDEV_UNREGISTER:
1505 return netvsc_unregister_vf(event_dev);
1506 case NETDEV_UP:
1507 return netvsc_vf_up(event_dev);
1508 case NETDEV_DOWN:
1509 return netvsc_vf_down(event_dev);
1510 default:
1511 return NOTIFY_DONE;
1512 }
1513}
1514
1515static struct notifier_block netvsc_netdev_notifier = {
1516 .notifier_call = netvsc_netdev_event,
1517};
1518
K. Y. Srinivasana9869c92011-05-12 19:35:17 -07001519static void __exit netvsc_drv_exit(void)
Hank Janssenfceaf242009-07-13 15:34:54 -07001520{
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001521 unregister_netdevice_notifier(&netvsc_netdev_notifier);
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001522 vmbus_driver_unregister(&netvsc_drv);
Hank Janssenfceaf242009-07-13 15:34:54 -07001523}
1524
K. Y. Srinivasan1fde28c2011-05-12 19:35:16 -07001525static int __init netvsc_drv_init(void)
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001526{
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001527 int ret;
1528
Haiyang Zhangfa85a6c2012-07-25 08:08:41 +00001529 if (ring_size < RING_SIZE_MIN) {
1530 ring_size = RING_SIZE_MIN;
1531 pr_info("Increased ring_size to %d (min allowed)\n",
1532 ring_size);
1533 }
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001534 ret = vmbus_driver_register(&netvsc_drv);
1535
1536 if (ret)
1537 return ret;
1538
1539 register_netdevice_notifier(&netvsc_netdev_notifier);
1540 return 0;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001541}
1542
Hank Janssen26c14cc2010-02-11 23:02:42 +00001543MODULE_LICENSE("GPL");
Stephen Hemminger7880fc54c2010-05-04 09:58:52 -07001544MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
Hank Janssenfceaf242009-07-13 15:34:54 -07001545
K. Y. Srinivasan1fde28c2011-05-12 19:35:16 -07001546module_init(netvsc_drv_init);
K. Y. Srinivasana9869c92011-05-12 19:35:17 -07001547module_exit(netvsc_drv_exit);