blob: 3bbf0735243381a29fbf6ef98827708823103031 [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;
360 bool isvlan;
Vitaly Kuznetsove88f7e02015-04-08 17:54:06 +0200361 bool linear = false;
KY Srinivasan8a002512014-03-08 19:23:14 -0800362 struct rndis_per_packet_info *ppi;
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800363 struct ndis_tcp_ip_checksum_info *csum_info;
KY Srinivasan77bf5482014-03-08 19:23:18 -0800364 struct ndis_tcp_lso_info *lso_info;
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800365 int hdr_offset;
366 u32 net_trans_info;
Haiyang Zhang307f0992014-05-21 12:55:39 -0700367 u32 hash;
Vitaly Kuznetsove88f7e02015-04-08 17:54:06 +0200368 u32 skb_length;
KY Srinivasanb08cc792015-03-29 21:08:42 -0700369 struct hv_page_buffer page_buf[MAX_PAGE_BUFFER_COUNT];
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800370 struct hv_page_buffer *pb = page_buf;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700371 struct netvsc_stats *tx_stats = this_cpu_ptr(net_device_ctx->tx_stats);
Hank Janssenfceaf242009-07-13 15:34:54 -0700372
KY Srinivasan54a73572014-03-08 19:23:13 -0800373 /* We will atmost need two pages to describe the rndis
374 * header. We can only transmit MAX_PAGE_BUFFER_COUNT number
Vitaly Kuznetsove88f7e02015-04-08 17:54:06 +0200375 * of pages in a single packet. If skb is scattered around
376 * more pages we try linearizing it.
KY Srinivasan54a73572014-03-08 19:23:13 -0800377 */
Vitaly Kuznetsove88f7e02015-04-08 17:54:06 +0200378
379check_size:
380 skb_length = skb->len;
KY Srinivasan8a002512014-03-08 19:23:14 -0800381 num_data_pgs = netvsc_get_slots(skb) + 2;
Vitaly Kuznetsove88f7e02015-04-08 17:54:06 +0200382 if (num_data_pgs > MAX_PAGE_BUFFER_COUNT && linear) {
383 net_alert_ratelimited("packet too big: %u pages (%u bytes)\n",
384 num_data_pgs, skb->len);
Vitaly Kuznetsov981a1bd2015-04-08 17:54:05 +0200385 ret = -EFAULT;
386 goto drop;
Vitaly Kuznetsove88f7e02015-04-08 17:54:06 +0200387 } else if (num_data_pgs > MAX_PAGE_BUFFER_COUNT) {
388 if (skb_linearize(skb)) {
389 net_alert_ratelimited("failed to linearize skb\n");
390 ret = -ENOMEM;
391 goto drop;
392 }
393 linear = true;
394 goto check_size;
KY Srinivasan54a73572014-03-08 19:23:13 -0800395 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700396
KY Srinivasanc0eb4542015-12-01 16:43:10 -0800397 /*
398 * Place the rndis header in the skb head room and
399 * the skb->cb will be used for hv_netvsc_packet
400 * structure.
401 */
402 ret = skb_cow_head(skb, RNDIS_AND_PPI_SIZE);
KY Srinivasanb56fc3c2015-04-28 17:59:48 -0700403 if (ret) {
404 netdev_err(net, "unable to alloc hv_netvsc_packet\n");
405 ret = -ENOMEM;
406 goto drop;
Hank Janssenfceaf242009-07-13 15:34:54 -0700407 }
KY Srinivasanc0eb4542015-12-01 16:43:10 -0800408 /* Use the skb control buffer for building up the packet */
409 BUILD_BUG_ON(sizeof(struct hv_netvsc_packet) >
410 FIELD_SIZEOF(struct sk_buff, cb));
411 packet = (struct hv_netvsc_packet *)skb->cb;
Hank Janssenfceaf242009-07-13 15:34:54 -0700412
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700413 packet->q_idx = skb_get_queue_mapping(skb);
414
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800415 packet->total_data_buflen = skb->len;
Hank Janssenfceaf242009-07-13 15:34:54 -0700416
KY Srinivasanc0eb4542015-12-01 16:43:10 -0800417 rndis_msg = (struct rndis_message *)skb->head;
KY Srinivasanb08cc792015-03-29 21:08:42 -0700418
KY Srinivasan24476762015-12-01 16:43:06 -0800419 memset(rndis_msg, 0, RNDIS_AND_PPI_SIZE);
Hank Janssenfceaf242009-07-13 15:34:54 -0700420
KY Srinivasan760d1e32015-12-01 16:43:19 -0800421 isvlan = skb->vlan_tci & VLAN_TAG_PRESENT;
KY Srinivasan8a002512014-03-08 19:23:14 -0800422
423 /* Add the rndis header */
KY Srinivasan8a002512014-03-08 19:23:14 -0800424 rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET;
425 rndis_msg->msg_len = packet->total_data_buflen;
426 rndis_pkt = &rndis_msg->msg.pkt;
427 rndis_pkt->data_offset = sizeof(struct rndis_packet);
428 rndis_pkt->data_len = packet->total_data_buflen;
429 rndis_pkt->per_pkt_info_offset = sizeof(struct rndis_packet);
430
431 rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet);
432
Haiyang Zhang307f0992014-05-21 12:55:39 -0700433 hash = skb_get_hash_raw(skb);
434 if (hash != 0 && net->real_num_tx_queues > 1) {
435 rndis_msg_size += NDIS_HASH_PPI_SIZE;
436 ppi = init_ppi_data(rndis_msg, NDIS_HASH_PPI_SIZE,
437 NBL_HASH_VALUE);
438 *(u32 *)((void *)ppi + ppi->ppi_offset) = hash;
439 }
440
KY Srinivasan8a002512014-03-08 19:23:14 -0800441 if (isvlan) {
442 struct ndis_pkt_8021q_info *vlan;
443
444 rndis_msg_size += NDIS_VLAN_PPI_SIZE;
445 ppi = init_ppi_data(rndis_msg, NDIS_VLAN_PPI_SIZE,
446 IEEE_8021Q_INFO);
447 vlan = (struct ndis_pkt_8021q_info *)((void *)ppi +
448 ppi->ppi_offset);
KY Srinivasan760d1e32015-12-01 16:43:19 -0800449 vlan->vlanid = skb->vlan_tci & VLAN_VID_MASK;
450 vlan->pri = (skb->vlan_tci & VLAN_PRIO_MASK) >>
KY Srinivasan8a002512014-03-08 19:23:14 -0800451 VLAN_PRIO_SHIFT;
452 }
453
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800454 net_trans_info = get_net_transport_info(skb, &hdr_offset);
455 if (net_trans_info == TRANSPORT_INFO_NOT_IP)
456 goto do_send;
457
458 /*
459 * Setup the sendside checksum offload only if this is not a
460 * GSO packet.
461 */
462 if (skb_is_gso(skb))
KY Srinivasan77bf5482014-03-08 19:23:18 -0800463 goto do_lso;
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800464
KY Srinivasan22041fb2014-04-30 11:58:25 -0700465 if ((skb->ip_summed == CHECKSUM_NONE) ||
466 (skb->ip_summed == CHECKSUM_UNNECESSARY))
467 goto do_send;
468
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800469 rndis_msg_size += NDIS_CSUM_PPI_SIZE;
470 ppi = init_ppi_data(rndis_msg, NDIS_CSUM_PPI_SIZE,
471 TCPIP_CHKSUM_PKTINFO);
472
473 csum_info = (struct ndis_tcp_ip_checksum_info *)((void *)ppi +
474 ppi->ppi_offset);
475
476 if (net_trans_info & (INFO_IPV4 << 16))
477 csum_info->transmit.is_ipv4 = 1;
478 else
479 csum_info->transmit.is_ipv6 = 1;
480
481 if (net_trans_info & INFO_TCP) {
482 csum_info->transmit.tcp_checksum = 1;
483 csum_info->transmit.tcp_header_offset = hdr_offset;
484 } else if (net_trans_info & INFO_UDP) {
KY Srinivasanaf9893a2014-04-09 15:00:47 -0700485 /* UDP checksum offload is not supported on ws2008r2.
486 * Furthermore, on ws2012 and ws2012r2, there are some
487 * issues with udp checksum offload from Linux guests.
488 * (these are host issues).
489 * For now compute the checksum here.
490 */
491 struct udphdr *uh;
492 u16 udp_len;
493
494 ret = skb_cow_head(skb, 0);
495 if (ret)
496 goto drop;
497
498 uh = udp_hdr(skb);
499 udp_len = ntohs(uh->len);
500 uh->check = 0;
501 uh->check = csum_tcpudp_magic(ip_hdr(skb)->saddr,
502 ip_hdr(skb)->daddr,
503 udp_len, IPPROTO_UDP,
504 csum_partial(uh, udp_len, 0));
505 if (uh->check == 0)
506 uh->check = CSUM_MANGLED_0;
507
508 csum_info->transmit.udp_checksum = 0;
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800509 }
KY Srinivasan77bf5482014-03-08 19:23:18 -0800510 goto do_send;
511
512do_lso:
513 rndis_msg_size += NDIS_LSO_PPI_SIZE;
514 ppi = init_ppi_data(rndis_msg, NDIS_LSO_PPI_SIZE,
515 TCP_LARGESEND_PKTINFO);
516
517 lso_info = (struct ndis_tcp_lso_info *)((void *)ppi +
518 ppi->ppi_offset);
519
520 lso_info->lso_v2_transmit.type = NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
521 if (net_trans_info & (INFO_IPV4 << 16)) {
522 lso_info->lso_v2_transmit.ip_version =
523 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
524 ip_hdr(skb)->tot_len = 0;
525 ip_hdr(skb)->check = 0;
526 tcp_hdr(skb)->check =
527 ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
528 ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
529 } else {
530 lso_info->lso_v2_transmit.ip_version =
531 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
532 ipv6_hdr(skb)->payload_len = 0;
533 tcp_hdr(skb)->check =
534 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
535 &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
536 }
537 lso_info->lso_v2_transmit.tcp_header_offset = hdr_offset;
538 lso_info->lso_v2_transmit.mss = skb_shinfo(skb)->gso_size;
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800539
540do_send:
KY Srinivasan8a002512014-03-08 19:23:14 -0800541 /* Start filling in the page buffers with the rndis hdr */
542 rndis_msg->msg_len += rndis_msg_size;
Haiyang Zhang942396b2014-10-22 13:47:18 -0700543 packet->total_data_buflen = rndis_msg->msg_len;
KY Srinivasan8a002512014-03-08 19:23:14 -0800544 packet->page_buf_cnt = init_page_array(rndis_msg, rndis_msg_size,
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800545 skb, packet, &pb);
KY Srinivasan8a002512014-03-08 19:23:14 -0800546
sixiao@microsoft.com76d13b52016-02-17 16:43:59 -0800547 /* timestamp packet in software */
548 skb_tx_timestamp(skb);
KY Srinivasan3a3d9a02015-12-01 16:43:14 -0800549 ret = netvsc_send(net_device_ctx->device_ctx, packet,
550 rndis_msg, &pb, skb);
KY Srinivasan8a002512014-03-08 19:23:14 -0800551
KY Srinivasanaf9893a2014-04-09 15:00:47 -0700552drop:
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700553 if (ret == 0) {
sixiao@microsoft.com4b02b582015-05-15 02:33:03 -0700554 u64_stats_update_begin(&tx_stats->syncp);
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700555 tx_stats->packets++;
556 tx_stats->bytes += skb_length;
sixiao@microsoft.com4b02b582015-05-15 02:33:03 -0700557 u64_stats_update_end(&tx_stats->syncp);
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700558 } else {
Haiyang Zhang33be96e2012-03-27 13:20:45 +0000559 if (ret != -EAGAIN) {
560 dev_kfree_skb_any(skb);
561 net->stats.tx_dropped++;
562 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700563 }
564
Haiyang Zhang33be96e2012-03-27 13:20:45 +0000565 return (ret == -EAGAIN) ? NETDEV_TX_BUSY : NETDEV_TX_OK;
Hank Janssenfceaf242009-07-13 15:34:54 -0700566}
567
Hank Janssen3e189512010-03-04 22:11:00 +0000568/*
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700569 * netvsc_linkstatus_callback - Link up/down notification
570 */
K. Y. Srinivasan90ef1172011-05-12 19:34:50 -0700571void netvsc_linkstatus_callback(struct hv_device *device_obj,
Haiyang Zhang3a494e72014-06-19 18:34:36 -0700572 struct rndis_message *resp)
Hank Janssenfceaf242009-07-13 15:34:54 -0700573{
Haiyang Zhang3a494e72014-06-19 18:34:36 -0700574 struct rndis_indicate_status *indicate = &resp->msg.indicate_status;
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -0700575 struct net_device *net;
Haiyang Zhangc996edc2011-04-06 15:18:00 -0700576 struct net_device_context *ndev_ctx;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +0100577 struct netvsc_reconfig *event;
578 unsigned long flags;
579
Haiyang Zhang7f5d5af2016-08-04 10:42:15 -0700580 net = hv_get_drvdata(device_obj);
581
582 if (!net)
583 return;
584
585 ndev_ctx = netdev_priv(net);
586
587 /* Update the physical link speed when changing to another vSwitch */
588 if (indicate->status == RNDIS_STATUS_LINK_SPEED_CHANGE) {
589 u32 speed;
590
591 speed = *(u32 *)((void *)indicate + indicate->
592 status_buf_offset) / 10000;
593 ndev_ctx->speed = speed;
594 return;
595 }
596
597 /* Handle these link change statuses below */
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +0100598 if (indicate->status != RNDIS_STATUS_NETWORK_CHANGE &&
599 indicate->status != RNDIS_STATUS_MEDIA_CONNECT &&
600 indicate->status != RNDIS_STATUS_MEDIA_DISCONNECT)
601 return;
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -0700602
Haiyang Zhang7f5d5af2016-08-04 10:42:15 -0700603 if (net->reg_state != NETREG_REGISTERED)
Hank Janssenfceaf242009-07-13 15:34:54 -0700604 return;
Hank Janssenfceaf242009-07-13 15:34:54 -0700605
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +0100606 event = kzalloc(sizeof(*event), GFP_ATOMIC);
607 if (!event)
608 return;
609 event->event = indicate->status;
610
611 spin_lock_irqsave(&ndev_ctx->lock, flags);
612 list_add_tail(&event->list, &ndev_ctx->reconfig_events);
613 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
614
615 schedule_delayed_work(&ndev_ctx->dwork, 0);
Hank Janssenfceaf242009-07-13 15:34:54 -0700616}
617
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700618static struct sk_buff *netvsc_alloc_recv_skb(struct net_device *net,
KY Srinivasane3d605e2014-03-08 19:23:16 -0800619 struct hv_netvsc_packet *packet,
KY Srinivasan25b85ee2015-12-01 16:43:05 -0800620 struct ndis_tcp_ip_checksum_info *csum_info,
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700621 void *data, u16 vlan_tci)
Hank Janssenfceaf242009-07-13 15:34:54 -0700622{
Hank Janssenfceaf242009-07-13 15:34:54 -0700623 struct sk_buff *skb;
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -0700624
Haiyang Zhang72a2f5b2010-12-10 12:03:58 -0800625 skb = netdev_alloc_skb_ip_align(net, packet->total_data_buflen);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700626 if (!skb)
627 return skb;
Hank Janssenfceaf242009-07-13 15:34:54 -0700628
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700629 /*
630 * Copy to skb. This copy is needed here since the memory pointed by
631 * hv_netvsc_packet cannot be deallocated
632 */
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700633 memcpy(skb_put(skb, packet->total_data_buflen), data,
634 packet->total_data_buflen);
Hank Janssenfceaf242009-07-13 15:34:54 -0700635
636 skb->protocol = eth_type_trans(skb, net);
KY Srinivasane3d605e2014-03-08 19:23:16 -0800637 if (csum_info) {
638 /* We only look at the IP checksum here.
639 * Should we be dropping the packet if checksum
640 * failed? How do we deal with other checksums - TCP/UDP?
641 */
642 if (csum_info->receive.ip_checksum_succeeded)
643 skb->ip_summed = CHECKSUM_UNNECESSARY;
644 else
645 skb->ip_summed = CHECKSUM_NONE;
646 }
647
KY Srinivasan760d1e32015-12-01 16:43:19 -0800648 if (vlan_tci & VLAN_TAG_PRESENT)
Haiyang Zhang93725cb2013-06-17 15:36:49 -0700649 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
KY Srinivasan760d1e32015-12-01 16:43:19 -0800650 vlan_tci);
Hank Janssenfceaf242009-07-13 15:34:54 -0700651
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700652 return skb;
653}
654
655/*
656 * netvsc_recv_callback - Callback when we receive a packet from the
657 * "wire" on the specified device.
658 */
659int netvsc_recv_callback(struct hv_device *device_obj,
660 struct hv_netvsc_packet *packet,
661 void **data,
662 struct ndis_tcp_ip_checksum_info *csum_info,
663 struct vmbus_channel *channel,
664 u16 vlan_tci)
665{
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200666 struct net_device *net = hv_get_drvdata(device_obj);
667 struct net_device_context *net_device_ctx = netdev_priv(net);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700668 struct sk_buff *skb;
669 struct sk_buff *vf_skb;
670 struct netvsc_stats *rx_stats;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700671 u32 bytes_recvd = packet->total_data_buflen;
672 int ret = 0;
673
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700674 if (!net || net->reg_state != NETREG_REGISTERED)
675 return NVSP_STAT_FAIL;
676
Vitaly Kuznetsovf9a7da92016-08-15 17:48:39 +0200677 if (READ_ONCE(net_device_ctx->vf_inject)) {
678 atomic_inc(&net_device_ctx->vf_use_cnt);
679 if (!READ_ONCE(net_device_ctx->vf_inject)) {
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700680 /*
681 * We raced; just move on.
682 */
Vitaly Kuznetsovf9a7da92016-08-15 17:48:39 +0200683 atomic_dec(&net_device_ctx->vf_use_cnt);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700684 goto vf_injection_done;
685 }
686
687 /*
688 * Inject this packet into the VF inerface.
689 * On Hyper-V, multicast and brodcast packets
690 * are only delivered on the synthetic interface
691 * (after subjecting these to policy filters on
692 * the host). Deliver these via the VF interface
693 * in the guest.
694 */
Vitaly Kuznetsovf9a7da92016-08-15 17:48:39 +0200695 vf_skb = netvsc_alloc_recv_skb(net_device_ctx->vf_netdev,
696 packet, csum_info, *data,
697 vlan_tci);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700698 if (vf_skb != NULL) {
Vitaly Kuznetsovf9a7da92016-08-15 17:48:39 +0200699 ++net_device_ctx->vf_netdev->stats.rx_packets;
700 net_device_ctx->vf_netdev->stats.rx_bytes +=
701 bytes_recvd;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700702 netif_receive_skb(vf_skb);
703 } else {
704 ++net->stats.rx_dropped;
705 ret = NVSP_STAT_FAIL;
706 }
Vitaly Kuznetsovf9a7da92016-08-15 17:48:39 +0200707 atomic_dec(&net_device_ctx->vf_use_cnt);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700708 return ret;
709 }
710
711vf_injection_done:
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700712 rx_stats = this_cpu_ptr(net_device_ctx->rx_stats);
713
714 /* Allocate a skb - TODO direct I/O to pages? */
715 skb = netvsc_alloc_recv_skb(net, packet, csum_info, *data, vlan_tci);
716 if (unlikely(!skb)) {
717 ++net->stats.rx_dropped;
718 return NVSP_STAT_FAIL;
719 }
KY Srinivasan25b85ee2015-12-01 16:43:05 -0800720 skb_record_rx_queue(skb, channel->
Haiyang Zhange565e802014-05-08 15:14:10 -0700721 offermsg.offer.sub_channel_index);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700722
sixiao@microsoft.com4b02b582015-05-15 02:33:03 -0700723 u64_stats_update_begin(&rx_stats->syncp);
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700724 rx_stats->packets++;
725 rx_stats->bytes += packet->total_data_buflen;
sixiao@microsoft.com4b02b582015-05-15 02:33:03 -0700726 u64_stats_update_end(&rx_stats->syncp);
Stephen Hemminger9495c282010-03-09 17:42:17 -0800727
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700728 /*
729 * Pass the skb back up. Network stack will deallocate the skb when it
Stephen Hemminger9495c282010-03-09 17:42:17 -0800730 * is done.
731 * TODO - use NAPI?
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700732 */
Stephen Hemminger9495c282010-03-09 17:42:17 -0800733 netif_rx(skb);
Hank Janssenfceaf242009-07-13 15:34:54 -0700734
Hank Janssenfceaf242009-07-13 15:34:54 -0700735 return 0;
736}
737
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -0700738static void netvsc_get_drvinfo(struct net_device *net,
739 struct ethtool_drvinfo *info)
740{
Jiri Pirko7826d432013-01-06 00:44:26 +0000741 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
Jiri Pirko7826d432013-01-06 00:44:26 +0000742 strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -0700743}
744
Andrew Schwartzmeyer59995372015-02-26 16:27:14 -0800745static void netvsc_get_channels(struct net_device *net,
746 struct ethtool_channels *channel)
747{
748 struct net_device_context *net_device_ctx = netdev_priv(net);
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200749 struct netvsc_device *nvdev = net_device_ctx->nvdev;
Andrew Schwartzmeyer59995372015-02-26 16:27:14 -0800750
751 if (nvdev) {
752 channel->max_combined = nvdev->max_chn;
753 channel->combined_count = nvdev->num_chn;
754 }
755}
756
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700757static int netvsc_set_channels(struct net_device *net,
758 struct ethtool_channels *channels)
759{
760 struct net_device_context *net_device_ctx = netdev_priv(net);
761 struct hv_device *dev = net_device_ctx->device_ctx;
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200762 struct netvsc_device *nvdev = net_device_ctx->nvdev;
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700763 struct netvsc_device_info device_info;
Andrew Schwartzmeyer954591b2015-08-18 20:06:32 -0700764 u32 num_chn;
765 u32 max_chn;
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700766 int ret = 0;
767 bool recovering = false;
768
Vitaly Kuznetsov6da72252016-05-13 13:55:24 +0200769 if (net_device_ctx->start_remove || !nvdev || nvdev->destroy)
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700770 return -ENODEV;
771
Andrew Schwartzmeyer954591b2015-08-18 20:06:32 -0700772 num_chn = nvdev->num_chn;
773 max_chn = min_t(u32, nvdev->max_chn, num_online_cpus());
774
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700775 if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5) {
776 pr_info("vRSS unsupported before NVSP Version 5\n");
777 return -EINVAL;
778 }
779
780 /* We do not support rx, tx, or other */
781 if (!channels ||
782 channels->rx_count ||
783 channels->tx_count ||
784 channels->other_count ||
785 (channels->combined_count < 1))
786 return -EINVAL;
787
788 if (channels->combined_count > max_chn) {
789 pr_info("combined channels too high, using %d\n", max_chn);
790 channels->combined_count = max_chn;
791 }
792
793 ret = netvsc_close(net);
794 if (ret)
795 goto out;
796
797 do_set:
Vitaly Kuznetsovf580aec2016-05-13 13:55:20 +0200798 net_device_ctx->start_remove = true;
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700799 rndis_filter_device_remove(dev);
800
801 nvdev->num_chn = channels->combined_count;
802
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700803 memset(&device_info, 0, sizeof(device_info));
804 device_info.num_chn = nvdev->num_chn; /* passed to RNDIS */
805 device_info.ring_size = ring_size;
806 device_info.max_num_vrss_chns = max_num_vrss_chns;
807
808 ret = rndis_filter_device_add(dev, &device_info);
809 if (ret) {
810 if (recovering) {
811 netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
812 return ret;
813 }
814 goto recover;
815 }
816
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200817 nvdev = net_device_ctx->nvdev;
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700818
819 ret = netif_set_real_num_tx_queues(net, nvdev->num_chn);
820 if (ret) {
821 if (recovering) {
822 netdev_err(net, "could not set tx queue count (ret %d)\n", ret);
823 return ret;
824 }
825 goto recover;
826 }
827
828 ret = netif_set_real_num_rx_queues(net, nvdev->num_chn);
829 if (ret) {
830 if (recovering) {
831 netdev_err(net, "could not set rx queue count (ret %d)\n", ret);
832 return ret;
833 }
834 goto recover;
835 }
836
837 out:
838 netvsc_open(net);
Vitaly Kuznetsovf580aec2016-05-13 13:55:20 +0200839 net_device_ctx->start_remove = false;
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +0200840 /* We may have missed link change notifications */
841 schedule_delayed_work(&net_device_ctx->dwork, 0);
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700842
843 return ret;
844
845 recover:
846 /* If the above failed, we attempt to recover through the same
847 * process but with the original number of channels.
848 */
849 netdev_err(net, "could not set channels, recovering\n");
850 recovering = true;
851 channels->combined_count = num_chn;
852 goto do_set;
853}
854
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800855static bool netvsc_validate_ethtool_ss_cmd(const struct ethtool_cmd *cmd)
856{
857 struct ethtool_cmd diff1 = *cmd;
858 struct ethtool_cmd diff2 = {};
859
860 ethtool_cmd_speed_set(&diff1, 0);
861 diff1.duplex = 0;
862 /* advertising and cmd are usually set */
863 diff1.advertising = 0;
864 diff1.cmd = 0;
865 /* We set port to PORT_OTHER */
866 diff2.port = PORT_OTHER;
867
868 return !memcmp(&diff1, &diff2, sizeof(diff1));
869}
870
871static void netvsc_init_settings(struct net_device *dev)
872{
873 struct net_device_context *ndc = netdev_priv(dev);
874
875 ndc->speed = SPEED_UNKNOWN;
876 ndc->duplex = DUPLEX_UNKNOWN;
877}
878
879static int netvsc_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
880{
881 struct net_device_context *ndc = netdev_priv(dev);
882
883 ethtool_cmd_speed_set(cmd, ndc->speed);
884 cmd->duplex = ndc->duplex;
885 cmd->port = PORT_OTHER;
886
887 return 0;
888}
889
890static int netvsc_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
891{
892 struct net_device_context *ndc = netdev_priv(dev);
893 u32 speed;
894
895 speed = ethtool_cmd_speed(cmd);
896 if (!ethtool_validate_speed(speed) ||
897 !ethtool_validate_duplex(cmd->duplex) ||
898 !netvsc_validate_ethtool_ss_cmd(cmd))
899 return -EINVAL;
900
901 ndc->speed = speed;
902 ndc->duplex = cmd->duplex;
903
904 return 0;
905}
906
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800907static int netvsc_change_mtu(struct net_device *ndev, int mtu)
908{
909 struct net_device_context *ndevctx = netdev_priv(ndev);
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200910 struct netvsc_device *nvdev = ndevctx->nvdev;
911 struct hv_device *hdev = ndevctx->device_ctx;
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800912 struct netvsc_device_info device_info;
913 int limit = ETH_DATA_LEN;
Haiyang Zhangd212b462016-03-23 09:43:09 -0700914 u32 num_chn;
Haiyang Zhang2de85302015-07-13 13:09:16 -0700915 int ret = 0;
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800916
Vitaly Kuznetsov6da72252016-05-13 13:55:24 +0200917 if (ndevctx->start_remove || !nvdev || nvdev->destroy)
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800918 return -ENODEV;
919
Haiyang Zhanga1eabb02014-02-19 15:49:45 -0800920 if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
Haiyang Zhang4d3c9d32014-11-12 14:07:44 -0800921 limit = NETVSC_MTU - ETH_HLEN;
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800922
Haiyang Zhangf9cbce32015-07-06 14:11:37 -0700923 if (mtu < NETVSC_MTU_MIN || mtu > limit)
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800924 return -EINVAL;
925
Haiyang Zhang2de85302015-07-13 13:09:16 -0700926 ret = netvsc_close(ndev);
927 if (ret)
928 goto out;
929
Haiyang Zhangd212b462016-03-23 09:43:09 -0700930 num_chn = nvdev->num_chn;
931
Vitaly Kuznetsovf580aec2016-05-13 13:55:20 +0200932 ndevctx->start_remove = true;
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800933 rndis_filter_device_remove(hdev);
934
935 ndev->mtu = mtu;
936
Andrew Schwartzmeyer8ebdcc52015-08-11 17:14:31 -0700937 memset(&device_info, 0, sizeof(device_info));
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800938 device_info.ring_size = ring_size;
Haiyang Zhangd212b462016-03-23 09:43:09 -0700939 device_info.num_chn = num_chn;
KY Srinivasane01ec212015-05-27 13:16:57 -0700940 device_info.max_num_vrss_chns = max_num_vrss_chns;
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800941 rndis_filter_device_add(hdev, &device_info);
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800942
Haiyang Zhang2de85302015-07-13 13:09:16 -0700943out:
944 netvsc_open(ndev);
Vitaly Kuznetsovf580aec2016-05-13 13:55:20 +0200945 ndevctx->start_remove = false;
Haiyang Zhang2de85302015-07-13 13:09:16 -0700946
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +0200947 /* We may have missed link change notifications */
948 schedule_delayed_work(&ndevctx->dwork, 0);
949
Haiyang Zhang2de85302015-07-13 13:09:16 -0700950 return ret;
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800951}
952
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700953static struct rtnl_link_stats64 *netvsc_get_stats64(struct net_device *net,
954 struct rtnl_link_stats64 *t)
955{
956 struct net_device_context *ndev_ctx = netdev_priv(net);
957 int cpu;
958
959 for_each_possible_cpu(cpu) {
960 struct netvsc_stats *tx_stats = per_cpu_ptr(ndev_ctx->tx_stats,
961 cpu);
962 struct netvsc_stats *rx_stats = per_cpu_ptr(ndev_ctx->rx_stats,
963 cpu);
964 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
965 unsigned int start;
966
967 do {
sixiao@microsoft.com4b02b582015-05-15 02:33:03 -0700968 start = u64_stats_fetch_begin_irq(&tx_stats->syncp);
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700969 tx_packets = tx_stats->packets;
970 tx_bytes = tx_stats->bytes;
sixiao@microsoft.com4b02b582015-05-15 02:33:03 -0700971 } while (u64_stats_fetch_retry_irq(&tx_stats->syncp, start));
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700972
973 do {
sixiao@microsoft.com4b02b582015-05-15 02:33:03 -0700974 start = u64_stats_fetch_begin_irq(&rx_stats->syncp);
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700975 rx_packets = rx_stats->packets;
976 rx_bytes = rx_stats->bytes;
sixiao@microsoft.com4b02b582015-05-15 02:33:03 -0700977 } while (u64_stats_fetch_retry_irq(&rx_stats->syncp, start));
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700978
979 t->tx_bytes += tx_bytes;
980 t->tx_packets += tx_packets;
981 t->rx_bytes += rx_bytes;
982 t->rx_packets += rx_packets;
983 }
984
985 t->tx_dropped = net->stats.tx_dropped;
986 t->tx_errors = net->stats.tx_dropped;
987
988 t->rx_dropped = net->stats.rx_dropped;
989 t->rx_errors = net->stats.rx_errors;
990
991 return t;
992}
Haiyang Zhang1ce09e82012-07-10 07:19:22 +0000993
994static int netvsc_set_mac_addr(struct net_device *ndev, void *p)
995{
Haiyang Zhang1ce09e82012-07-10 07:19:22 +0000996 struct sockaddr *addr = p;
Jianjun Kong9a4c8312013-01-18 16:52:09 +0000997 char save_adr[ETH_ALEN];
Haiyang Zhang1ce09e82012-07-10 07:19:22 +0000998 unsigned char save_aatype;
999 int err;
1000
1001 memcpy(save_adr, ndev->dev_addr, ETH_ALEN);
1002 save_aatype = ndev->addr_assign_type;
1003
1004 err = eth_mac_addr(ndev, p);
1005 if (err != 0)
1006 return err;
1007
Vitaly Kuznetsove834da9a2016-06-03 17:51:01 +02001008 err = rndis_filter_set_device_mac(ndev, addr->sa_data);
Haiyang Zhang1ce09e82012-07-10 07:19:22 +00001009 if (err != 0) {
1010 /* roll back to saved MAC */
1011 memcpy(ndev->dev_addr, save_adr, ETH_ALEN);
1012 ndev->addr_assign_type = save_aatype;
1013 }
1014
1015 return err;
1016}
1017
Richard Weinberger316158f2014-07-09 16:23:59 +02001018#ifdef CONFIG_NET_POLL_CONTROLLER
1019static void netvsc_poll_controller(struct net_device *net)
1020{
1021 /* As netvsc_start_xmit() works synchronous we don't have to
1022 * trigger anything here.
1023 */
1024}
1025#endif
Haiyang Zhang1ce09e82012-07-10 07:19:22 +00001026
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -07001027static const struct ethtool_ops ethtool_ops = {
1028 .get_drvinfo = netvsc_get_drvinfo,
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -07001029 .get_link = ethtool_op_get_link,
Andrew Schwartzmeyer59995372015-02-26 16:27:14 -08001030 .get_channels = netvsc_get_channels,
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -07001031 .set_channels = netvsc_set_channels,
sixiao@microsoft.com76d13b52016-02-17 16:43:59 -08001032 .get_ts_info = ethtool_op_get_ts_info,
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -08001033 .get_settings = netvsc_get_settings,
1034 .set_settings = netvsc_set_settings,
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -07001035};
1036
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001037static const struct net_device_ops device_ops = {
1038 .ndo_open = netvsc_open,
1039 .ndo_stop = netvsc_close,
1040 .ndo_start_xmit = netvsc_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001041 .ndo_set_rx_mode = netvsc_set_multicast_list,
Haiyang Zhang4d447c92011-12-15 13:45:17 -08001042 .ndo_change_mtu = netvsc_change_mtu,
Haiyang Zhangb681b582010-08-03 19:15:31 +00001043 .ndo_validate_addr = eth_validate_addr,
Haiyang Zhang1ce09e82012-07-10 07:19:22 +00001044 .ndo_set_mac_address = netvsc_set_mac_addr,
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001045 .ndo_select_queue = netvsc_select_queue,
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001046 .ndo_get_stats64 = netvsc_get_stats64,
Richard Weinberger316158f2014-07-09 16:23:59 +02001047#ifdef CONFIG_NET_POLL_CONTROLLER
1048 .ndo_poll_controller = netvsc_poll_controller,
1049#endif
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001050};
1051
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001052/*
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001053 * Handle link status changes. For RNDIS_STATUS_NETWORK_CHANGE emulate link
1054 * down/up sequence. In case of RNDIS_STATUS_MEDIA_CONNECT when carrier is
1055 * present send GARP packet to network peers with netif_notify_peers().
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001056 */
Haiyang Zhang891de742014-02-12 16:54:27 -08001057static void netvsc_link_change(struct work_struct *w)
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001058{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001059 struct net_device_context *ndev_ctx =
1060 container_of(w, struct net_device_context, dwork.work);
1061 struct hv_device *device_obj = ndev_ctx->device_ctx;
1062 struct net_device *net = hv_get_drvdata(device_obj);
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -07001063 struct netvsc_device *net_device;
Haiyang Zhang891de742014-02-12 16:54:27 -08001064 struct rndis_device *rdev;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001065 struct netvsc_reconfig *event = NULL;
1066 bool notify = false, reschedule = false;
1067 unsigned long flags, next_reconfig, delay;
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001068
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001069 rtnl_lock();
1070 if (ndev_ctx->start_remove)
1071 goto out_unlock;
1072
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +02001073 net_device = ndev_ctx->nvdev;
Haiyang Zhang891de742014-02-12 16:54:27 -08001074 rdev = net_device->extension;
Haiyang Zhang891de742014-02-12 16:54:27 -08001075
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001076 next_reconfig = ndev_ctx->last_reconfig + LINKCHANGE_INT;
1077 if (time_is_after_jiffies(next_reconfig)) {
1078 /* link_watch only sends one notification with current state
1079 * per second, avoid doing reconfig more frequently. Handle
1080 * wrap around.
1081 */
1082 delay = next_reconfig - jiffies;
1083 delay = delay < LINKCHANGE_INT ? delay : LINKCHANGE_INT;
1084 schedule_delayed_work(&ndev_ctx->dwork, delay);
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001085 goto out_unlock;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001086 }
1087 ndev_ctx->last_reconfig = jiffies;
1088
1089 spin_lock_irqsave(&ndev_ctx->lock, flags);
1090 if (!list_empty(&ndev_ctx->reconfig_events)) {
1091 event = list_first_entry(&ndev_ctx->reconfig_events,
1092 struct netvsc_reconfig, list);
1093 list_del(&event->list);
1094 reschedule = !list_empty(&ndev_ctx->reconfig_events);
1095 }
1096 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1097
1098 if (!event)
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001099 goto out_unlock;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001100
1101 switch (event->event) {
1102 /* Only the following events are possible due to the check in
1103 * netvsc_linkstatus_callback()
1104 */
1105 case RNDIS_STATUS_MEDIA_CONNECT:
1106 if (rdev->link_state) {
1107 rdev->link_state = false;
1108 netif_carrier_on(net);
1109 netif_tx_wake_all_queues(net);
1110 } else {
1111 notify = true;
Haiyang Zhang3a494e72014-06-19 18:34:36 -07001112 }
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001113 kfree(event);
1114 break;
1115 case RNDIS_STATUS_MEDIA_DISCONNECT:
1116 if (!rdev->link_state) {
1117 rdev->link_state = true;
1118 netif_carrier_off(net);
1119 netif_tx_stop_all_queues(net);
1120 }
1121 kfree(event);
1122 break;
1123 case RNDIS_STATUS_NETWORK_CHANGE:
1124 /* Only makes sense if carrier is present */
1125 if (!rdev->link_state) {
1126 rdev->link_state = true;
1127 netif_carrier_off(net);
1128 netif_tx_stop_all_queues(net);
1129 event->event = RNDIS_STATUS_MEDIA_CONNECT;
1130 spin_lock_irqsave(&ndev_ctx->lock, flags);
Haiyang Zhang15cfd402016-04-21 16:13:01 -07001131 list_add(&event->list, &ndev_ctx->reconfig_events);
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001132 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1133 reschedule = true;
1134 }
1135 break;
Haiyang Zhang891de742014-02-12 16:54:27 -08001136 }
1137
1138 rtnl_unlock();
1139
1140 if (notify)
1141 netdev_notify_peers(net);
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001142
1143 /* link_watch only sends one notification with current state per
1144 * second, handle next reconfig event in 2 seconds.
1145 */
1146 if (reschedule)
1147 schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001148
1149 return;
1150
1151out_unlock:
1152 rtnl_unlock();
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001153}
1154
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001155static void netvsc_free_netdev(struct net_device *netdev)
1156{
1157 struct net_device_context *net_device_ctx = netdev_priv(netdev);
1158
1159 free_percpu(net_device_ctx->tx_stats);
1160 free_percpu(net_device_ctx->rx_stats);
1161 free_netdev(netdev);
1162}
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001163
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001164static struct net_device *get_netvsc_net_device(char *mac)
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001165{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001166 struct net_device *dev, *found = NULL;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001167
Stephen Hemminger8737caa2016-08-23 12:17:44 -07001168 ASSERT_RTNL();
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001169
1170 for_each_netdev(&init_net, dev) {
1171 if (memcmp(dev->dev_addr, mac, ETH_ALEN) == 0) {
1172 if (dev->netdev_ops != &device_ops)
1173 continue;
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001174 found = dev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001175 break;
1176 }
1177 }
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001178
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001179 return found;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001180}
1181
1182static int netvsc_register_vf(struct net_device *vf_netdev)
1183{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001184 struct net_device *ndev;
1185 struct net_device_context *net_device_ctx;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001186 struct netvsc_device *netvsc_dev;
1187 const struct ethtool_ops *eth_ops = vf_netdev->ethtool_ops;
1188
1189 if (eth_ops == NULL || eth_ops == &ethtool_ops)
1190 return NOTIFY_DONE;
1191
1192 /*
1193 * We will use the MAC address to locate the synthetic interface to
1194 * associate with the VF interface. If we don't find a matching
1195 * synthetic interface, move on.
1196 */
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001197 ndev = get_netvsc_net_device(vf_netdev->dev_addr);
1198 if (!ndev)
1199 return NOTIFY_DONE;
1200
1201 net_device_ctx = netdev_priv(ndev);
1202 netvsc_dev = net_device_ctx->nvdev;
Vitaly Kuznetsov0f20d792016-08-15 17:48:42 +02001203 if (!netvsc_dev || net_device_ctx->vf_netdev)
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001204 return NOTIFY_DONE;
1205
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001206 netdev_info(ndev, "VF registering: %s\n", vf_netdev->name);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001207 /*
1208 * Take a reference on the module.
1209 */
1210 try_module_get(THIS_MODULE);
Vitaly Kuznetsovf9a7da92016-08-15 17:48:39 +02001211 net_device_ctx->vf_netdev = vf_netdev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001212 return NOTIFY_OK;
1213}
1214
Vitaly Kuznetsov57c1826b992016-08-15 17:48:41 +02001215static void netvsc_inject_enable(struct net_device_context *net_device_ctx)
1216{
1217 net_device_ctx->vf_inject = true;
1218}
1219
1220static void netvsc_inject_disable(struct net_device_context *net_device_ctx)
1221{
1222 net_device_ctx->vf_inject = false;
1223
1224 /* Wait for currently active users to drain out. */
1225 while (atomic_read(&net_device_ctx->vf_use_cnt) != 0)
1226 udelay(50);
1227}
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001228
1229static int netvsc_vf_up(struct net_device *vf_netdev)
1230{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001231 struct net_device *ndev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001232 struct netvsc_device *netvsc_dev;
1233 const struct ethtool_ops *eth_ops = vf_netdev->ethtool_ops;
1234 struct net_device_context *net_device_ctx;
1235
1236 if (eth_ops == &ethtool_ops)
1237 return NOTIFY_DONE;
1238
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001239 ndev = get_netvsc_net_device(vf_netdev->dev_addr);
1240 if (!ndev)
1241 return NOTIFY_DONE;
1242
1243 net_device_ctx = netdev_priv(ndev);
1244 netvsc_dev = net_device_ctx->nvdev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001245
Vitaly Kuznetsovf9a7da92016-08-15 17:48:39 +02001246 if (!netvsc_dev || !net_device_ctx->vf_netdev)
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001247 return NOTIFY_DONE;
1248
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001249 netdev_info(ndev, "VF up: %s\n", vf_netdev->name);
Vitaly Kuznetsov57c1826b992016-08-15 17:48:41 +02001250 netvsc_inject_enable(net_device_ctx);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001251
1252 /*
1253 * Open the device before switching data path.
1254 */
Vitaly Kuznetsov2f5fa6c2016-06-03 17:51:00 +02001255 rndis_filter_open(netvsc_dev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001256
1257 /*
1258 * notify the host to switch the data path.
1259 */
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001260 netvsc_switch_datapath(ndev, true);
1261 netdev_info(ndev, "Data path switched to VF: %s\n", vf_netdev->name);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001262
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001263 netif_carrier_off(ndev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001264
Vitaly Kuznetsovd0722182016-08-15 17:48:40 +02001265 /* Now notify peers through VF device. */
1266 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, vf_netdev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001267
1268 return NOTIFY_OK;
1269}
1270
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001271static int netvsc_vf_down(struct net_device *vf_netdev)
1272{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001273 struct net_device *ndev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001274 struct netvsc_device *netvsc_dev;
1275 struct net_device_context *net_device_ctx;
1276 const struct ethtool_ops *eth_ops = vf_netdev->ethtool_ops;
1277
1278 if (eth_ops == &ethtool_ops)
1279 return NOTIFY_DONE;
1280
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001281 ndev = get_netvsc_net_device(vf_netdev->dev_addr);
1282 if (!ndev)
1283 return NOTIFY_DONE;
1284
1285 net_device_ctx = netdev_priv(ndev);
1286 netvsc_dev = net_device_ctx->nvdev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001287
Vitaly Kuznetsovf9a7da92016-08-15 17:48:39 +02001288 if (!netvsc_dev || !net_device_ctx->vf_netdev)
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001289 return NOTIFY_DONE;
1290
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001291 netdev_info(ndev, "VF down: %s\n", vf_netdev->name);
Vitaly Kuznetsov57c1826b992016-08-15 17:48:41 +02001292 netvsc_inject_disable(net_device_ctx);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001293 netvsc_switch_datapath(ndev, false);
1294 netdev_info(ndev, "Data path switched from VF: %s\n", vf_netdev->name);
Vitaly Kuznetsov2f5fa6c2016-06-03 17:51:00 +02001295 rndis_filter_close(netvsc_dev);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001296 netif_carrier_on(ndev);
Vitaly Kuznetsovd0722182016-08-15 17:48:40 +02001297
1298 /* Now notify peers through netvsc device. */
1299 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, ndev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001300
1301 return NOTIFY_OK;
1302}
1303
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001304static int netvsc_unregister_vf(struct net_device *vf_netdev)
1305{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001306 struct net_device *ndev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001307 struct netvsc_device *netvsc_dev;
1308 const struct ethtool_ops *eth_ops = vf_netdev->ethtool_ops;
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001309 struct net_device_context *net_device_ctx;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001310
1311 if (eth_ops == &ethtool_ops)
1312 return NOTIFY_DONE;
1313
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001314 ndev = get_netvsc_net_device(vf_netdev->dev_addr);
1315 if (!ndev)
1316 return NOTIFY_DONE;
1317
1318 net_device_ctx = netdev_priv(ndev);
1319 netvsc_dev = net_device_ctx->nvdev;
Vitaly Kuznetsov0f20d792016-08-15 17:48:42 +02001320 if (!netvsc_dev || !net_device_ctx->vf_netdev)
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001321 return NOTIFY_DONE;
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001322 netdev_info(ndev, "VF unregistering: %s\n", vf_netdev->name);
Vitaly Kuznetsov57c1826b992016-08-15 17:48:41 +02001323 netvsc_inject_disable(net_device_ctx);
Vitaly Kuznetsovf9a7da92016-08-15 17:48:39 +02001324 net_device_ctx->vf_netdev = NULL;
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. Srinivasan2ddd5e5f2011-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
Vitaly Kuznetsovf9a7da92016-08-15 17:48:39 +02001376 atomic_set(&net_device_ctx->vf_use_cnt, 0);
1377 net_device_ctx->vf_netdev = NULL;
1378 net_device_ctx->vf_inject = false;
1379
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001380 net->netdev_ops = &device_ops;
1381
sixiao@microsoft.coma0606792016-02-04 15:49:34 -08001382 net->hw_features = NETVSC_HW_FEATURES;
1383 net->features = NETVSC_HW_FEATURES | NETIF_F_HW_VLAN_CTAG_TX;
Stephen Hemminger60487182010-05-04 09:58:55 -07001384
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001385 net->ethtool_ops = &ethtool_ops;
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -07001386 SET_NETDEV_DEV(net, &dev->device);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001387
Vitaly Kuznetsov14a03cf2016-02-05 17:29:08 +01001388 /* We always need headroom for rndis header */
1389 net->needed_headroom = RNDIS_AND_PPI_SIZE;
1390
Haiyang Zhang692e0842011-09-01 12:19:43 -07001391 /* Notify the netvsc driver of the new device */
Andrew Schwartzmeyer8ebdcc52015-08-11 17:14:31 -07001392 memset(&device_info, 0, sizeof(device_info));
Haiyang Zhang692e0842011-09-01 12:19:43 -07001393 device_info.ring_size = ring_size;
KY Srinivasane01ec212015-05-27 13:16:57 -07001394 device_info.max_num_vrss_chns = max_num_vrss_chns;
Haiyang Zhang692e0842011-09-01 12:19:43 -07001395 ret = rndis_filter_device_add(dev, &device_info);
1396 if (ret != 0) {
1397 netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001398 netvsc_free_netdev(net);
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -07001399 hv_set_drvdata(dev, NULL);
Haiyang Zhang692e0842011-09-01 12:19:43 -07001400 return ret;
1401 }
1402 memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
1403
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +02001404 nvdev = net_device_ctx->nvdev;
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001405 netif_set_real_num_tx_queues(net, nvdev->num_chn);
1406 netif_set_real_num_rx_queues(net, nvdev->num_chn);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001407
Haiyang Zhanga68f9612013-12-20 16:52:31 -08001408 ret = register_netdev(net);
1409 if (ret != 0) {
1410 pr_err("Unable to register netdev.\n");
1411 rndis_filter_device_remove(dev);
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001412 netvsc_free_netdev(net);
Haiyang Zhanga68f9612013-12-20 16:52:31 -08001413 }
1414
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001415 return ret;
1416}
1417
K. Y. Srinivasan415b0232011-04-29 13:45:12 -07001418static int netvsc_remove(struct hv_device *dev)
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001419{
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -07001420 struct net_device *net;
Haiyang Zhang122a5f62011-05-27 06:21:55 -07001421 struct net_device_context *ndev_ctx;
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -07001422 struct netvsc_device *net_device;
1423
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +02001424 net = hv_get_drvdata(dev);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001425
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001426 if (net == NULL) {
K. Y. Srinivasan415b0232011-04-29 13:45:12 -07001427 dev_err(&dev->device, "No net device to remove\n");
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001428 return 0;
1429 }
1430
Haiyang Zhang122a5f62011-05-27 06:21:55 -07001431 ndev_ctx = netdev_priv(net);
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +02001432 net_device = ndev_ctx->nvdev;
1433
Vitaly Kuznetsov6da72252016-05-13 13:55:24 +02001434 /* Avoid racing with netvsc_change_mtu()/netvsc_set_channels()
1435 * removing the device.
1436 */
1437 rtnl_lock();
Vitaly Kuznetsovf580aec2016-05-13 13:55:20 +02001438 ndev_ctx->start_remove = true;
Vitaly Kuznetsov6da72252016-05-13 13:55:24 +02001439 rtnl_unlock();
Vitaly Kuznetsovf580aec2016-05-13 13:55:20 +02001440
Haiyang Zhang122a5f62011-05-27 06:21:55 -07001441 cancel_delayed_work_sync(&ndev_ctx->dwork);
Wenqi Ma792df872012-04-19 00:39:37 +00001442 cancel_work_sync(&ndev_ctx->work);
Haiyang Zhang122a5f62011-05-27 06:21:55 -07001443
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001444 /* Stop outbound asap */
Haiyang Zhang0a282532012-02-02 07:17:59 +00001445 netif_tx_disable(net);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001446
1447 unregister_netdev(net);
1448
1449 /*
1450 * Call to the vsc driver to let it know that the device is being
1451 * removed
1452 */
Haiyang Zhangdf06bcf2011-05-23 09:03:47 -07001453 rndis_filter_device_remove(dev);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001454
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +02001455 hv_set_drvdata(dev, NULL);
1456
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001457 netvsc_free_netdev(net);
Haiyang Zhangdf06bcf2011-05-23 09:03:47 -07001458 return 0;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001459}
1460
K. Y. Srinivasan345c4cc2011-08-25 09:48:34 -07001461static const struct hv_vmbus_device_id id_table[] = {
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07001462 /* Network guid */
K. Y. Srinivasan8f505942013-01-23 17:42:42 -08001463 { HV_NIC_GUID, },
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07001464 { },
K. Y. Srinivasan345c4cc2011-08-25 09:48:34 -07001465};
1466
1467MODULE_DEVICE_TABLE(vmbus, id_table);
1468
K. Y. Srinivasanf1542a62011-05-10 07:55:16 -07001469/* The one and only one */
K. Y. Srinivasanfde0ef92011-05-12 19:35:08 -07001470static struct hv_driver netvsc_drv = {
Haiyang Zhangd31b20f2012-03-07 10:02:00 +00001471 .name = KBUILD_MODNAME,
K. Y. Srinivasan345c4cc2011-08-25 09:48:34 -07001472 .id_table = id_table,
K. Y. Srinivasanfde0ef92011-05-12 19:35:08 -07001473 .probe = netvsc_probe,
1474 .remove = netvsc_remove,
K. Y. Srinivasand4890972011-05-10 07:55:17 -07001475};
K. Y. Srinivasanf1542a62011-05-10 07:55:16 -07001476
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001477/*
1478 * On Hyper-V, every VF interface is matched with a corresponding
1479 * synthetic interface. The synthetic interface is presented first
1480 * to the guest. When the corresponding VF instance is registered,
1481 * we will take care of switching the data path.
1482 */
1483static int netvsc_netdev_event(struct notifier_block *this,
1484 unsigned long event, void *ptr)
1485{
1486 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
1487
Vitaly Kuznetsov0dbff142016-08-15 17:48:43 +02001488 /* Avoid Vlan dev with same MAC registering as VF */
1489 if (event_dev->priv_flags & IFF_802_1Q_VLAN)
1490 return NOTIFY_DONE;
1491
1492 /* Avoid Bonding master dev with same MAC registering as VF */
1493 if (event_dev->priv_flags & IFF_BONDING &&
1494 event_dev->flags & IFF_MASTER)
Haiyang Zhangcb2911f2016-06-02 12:02:04 -07001495 return NOTIFY_DONE;
1496
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001497 switch (event) {
1498 case NETDEV_REGISTER:
1499 return netvsc_register_vf(event_dev);
1500 case NETDEV_UNREGISTER:
1501 return netvsc_unregister_vf(event_dev);
1502 case NETDEV_UP:
1503 return netvsc_vf_up(event_dev);
1504 case NETDEV_DOWN:
1505 return netvsc_vf_down(event_dev);
1506 default:
1507 return NOTIFY_DONE;
1508 }
1509}
1510
1511static struct notifier_block netvsc_netdev_notifier = {
1512 .notifier_call = netvsc_netdev_event,
1513};
1514
K. Y. Srinivasana9869c92011-05-12 19:35:17 -07001515static void __exit netvsc_drv_exit(void)
Hank Janssenfceaf242009-07-13 15:34:54 -07001516{
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001517 unregister_netdevice_notifier(&netvsc_netdev_notifier);
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001518 vmbus_driver_unregister(&netvsc_drv);
Hank Janssenfceaf242009-07-13 15:34:54 -07001519}
1520
K. Y. Srinivasan1fde28c2011-05-12 19:35:16 -07001521static int __init netvsc_drv_init(void)
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001522{
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001523 int ret;
1524
Haiyang Zhangfa85a6c2012-07-25 08:08:41 +00001525 if (ring_size < RING_SIZE_MIN) {
1526 ring_size = RING_SIZE_MIN;
1527 pr_info("Increased ring_size to %d (min allowed)\n",
1528 ring_size);
1529 }
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001530 ret = vmbus_driver_register(&netvsc_drv);
1531
1532 if (ret)
1533 return ret;
1534
1535 register_netdevice_notifier(&netvsc_netdev_notifier);
1536 return 0;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001537}
1538
Hank Janssen26c14cc2010-02-11 23:02:42 +00001539MODULE_LICENSE("GPL");
Stephen Hemminger7880fc52010-05-04 09:58:52 -07001540MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
Hank Janssenfceaf242009-07-13 15:34:54 -07001541
K. Y. Srinivasan1fde28c2011-05-12 19:35:16 -07001542module_init(netvsc_drv_init);
K. Y. Srinivasana9869c92011-05-12 19:35:17 -07001543module_exit(netvsc_drv_exit);