blob: a33a1c92d489c5e5f48b168d148b63ba73a3097e [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
Hank Janssenfceaf242009-07-13 15:34:54 -070043
Haiyang Zhangfa85a6c2012-07-25 08:08:41 +000044#define RING_SIZE_MIN 64
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +010045#define LINKCHANGE_INT (2 * HZ)
sixiao@microsoft.coma0606792016-02-04 15:49:34 -080046#define NETVSC_HW_FEATURES (NETIF_F_RXCSUM | \
47 NETIF_F_SG | \
48 NETIF_F_TSO | \
49 NETIF_F_TSO6 | \
50 NETIF_F_HW_CSUM)
Hank Janssen99c8da02010-10-12 10:45:23 -070051static int ring_size = 128;
Stephen Hemminger450d7a42010-05-04 09:58:53 -070052module_param(ring_size, int, S_IRUGO);
53MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)");
Hank Janssenfceaf242009-07-13 15:34:54 -070054
KY Srinivasane01ec212015-05-27 13:16:57 -070055static int max_num_vrss_chns = 8;
56
Simon Xiao3f300ff2015-04-28 01:05:17 -070057static const u32 default_msg = NETIF_MSG_DRV | NETIF_MSG_PROBE |
58 NETIF_MSG_LINK | NETIF_MSG_IFUP |
59 NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR |
60 NETIF_MSG_TX_ERR;
61
62static int debug = -1;
63module_param(debug, int, S_IRUGO);
64MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
65
Haiyang Zhangd426b2e2011-11-30 07:19:08 -080066static void do_set_multicast(struct work_struct *w)
67{
Wenqi Ma792df872012-04-19 00:39:37 +000068 struct net_device_context *ndevctx =
69 container_of(w, struct net_device_context, work);
Haiyang Zhangd426b2e2011-11-30 07:19:08 -080070 struct netvsc_device *nvdev;
71 struct rndis_device *rdev;
72
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +020073 nvdev = ndevctx->nvdev;
Wenqi Ma792df872012-04-19 00:39:37 +000074 if (nvdev == NULL || nvdev->ndev == NULL)
75 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
Wenqi Ma792df872012-04-19 00:39:37 +000081 if (nvdev->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{
Hank Janssenfceaf242009-07-13 15:34:54 -0700100 struct net_device_context *net_device_ctx = netdev_priv(net);
K. Y. Srinivasan6bad88d2011-03-07 13:35:48 -0800101 struct hv_device *device_obj = net_device_ctx->device_ctx;
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200102 struct netvsc_device *nvdev = net_device_ctx->nvdev;
Haiyang Zhang891de742014-02-12 16:54:27 -0800103 struct rndis_device *rdev;
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700104 int ret = 0;
Hank Janssenfceaf242009-07-13 15:34:54 -0700105
Haiyang Zhang891de742014-02-12 16:54:27 -0800106 netif_carrier_off(net);
107
Haiyang Zhangd515d0f2011-09-28 13:24:15 -0700108 /* Open up the device */
109 ret = rndis_filter_open(device_obj);
110 if (ret != 0) {
111 netdev_err(net, "unable to open device (ret %d).\n", ret);
112 return ret;
Hank Janssenfceaf242009-07-13 15:34:54 -0700113 }
114
Haiyang Zhang2de85302015-07-13 13:09:16 -0700115 netif_tx_wake_all_queues(net);
Haiyang Zhangd515d0f2011-09-28 13:24:15 -0700116
Haiyang Zhang891de742014-02-12 16:54:27 -0800117 rdev = nvdev->extension;
118 if (!rdev->link_state)
119 netif_carrier_on(net);
120
Hank Janssenfceaf242009-07-13 15:34:54 -0700121 return ret;
122}
123
Hank Janssenfceaf242009-07-13 15:34:54 -0700124static int netvsc_close(struct net_device *net)
125{
Hank Janssenfceaf242009-07-13 15:34:54 -0700126 struct net_device_context *net_device_ctx = netdev_priv(net);
K. Y. Srinivasan6bad88d2011-03-07 13:35:48 -0800127 struct hv_device *device_obj = net_device_ctx->device_ctx;
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200128 struct netvsc_device *nvdev = net_device_ctx->nvdev;
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700129 int ret;
Haiyang Zhang2de85302015-07-13 13:09:16 -0700130 u32 aread, awrite, i, msec = 10, retry = 0, retry_max = 20;
131 struct vmbus_channel *chn;
Hank Janssenfceaf242009-07-13 15:34:54 -0700132
Haiyang Zhang0a282532012-02-02 07:17:59 +0000133 netif_tx_disable(net);
Hank Janssenfceaf242009-07-13 15:34:54 -0700134
Wenqi Ma792df872012-04-19 00:39:37 +0000135 /* Make sure netvsc_set_multicast_list doesn't re-enable filter! */
136 cancel_work_sync(&net_device_ctx->work);
Haiyang Zhang9c26aa02010-12-10 12:03:57 -0800137 ret = rndis_filter_close(device_obj);
Haiyang Zhang2de85302015-07-13 13:09:16 -0700138 if (ret != 0) {
Hank Jansseneb335bc2011-03-29 13:58:48 -0700139 netdev_err(net, "unable to close device (ret %d).\n", ret);
Haiyang Zhang2de85302015-07-13 13:09:16 -0700140 return ret;
141 }
142
143 /* Ensure pending bytes in ring are read */
144 while (true) {
145 aread = 0;
146 for (i = 0; i < nvdev->num_chn; i++) {
147 chn = nvdev->chn_table[i];
148 if (!chn)
149 continue;
150
151 hv_get_ringbuffer_availbytes(&chn->inbound, &aread,
152 &awrite);
153
154 if (aread)
155 break;
156
157 hv_get_ringbuffer_availbytes(&chn->outbound, &aread,
158 &awrite);
159
160 if (aread)
161 break;
162 }
163
164 retry++;
165 if (retry > retry_max || aread == 0)
166 break;
167
168 msleep(msec);
169
170 if (msec < 1000)
171 msec *= 2;
172 }
173
174 if (aread) {
175 netdev_err(net, "Ring buffer not empty after closing rndis\n");
176 ret = -ETIMEDOUT;
177 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700178
Hank Janssenfceaf242009-07-13 15:34:54 -0700179 return ret;
180}
181
KY Srinivasan8a002512014-03-08 19:23:14 -0800182static void *init_ppi_data(struct rndis_message *msg, u32 ppi_size,
183 int pkt_type)
184{
185 struct rndis_packet *rndis_pkt;
186 struct rndis_per_packet_info *ppi;
187
188 rndis_pkt = &msg->msg.pkt;
189 rndis_pkt->data_offset += ppi_size;
190
191 ppi = (struct rndis_per_packet_info *)((void *)rndis_pkt +
192 rndis_pkt->per_pkt_info_offset + rndis_pkt->per_pkt_info_len);
193
194 ppi->size = ppi_size;
195 ppi->type = pkt_type;
196 ppi->ppi_offset = sizeof(struct rndis_per_packet_info);
197
198 rndis_pkt->per_pkt_info_len += ppi_size;
199
200 return ppi;
201}
202
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700203static u16 netvsc_select_queue(struct net_device *ndev, struct sk_buff *skb,
204 void *accel_priv, select_queue_fallback_t fallback)
205{
206 struct net_device_context *net_device_ctx = netdev_priv(ndev);
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200207 struct netvsc_device *nvsc_dev = net_device_ctx->nvdev;
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700208 u32 hash;
209 u16 q_idx = 0;
210
211 if (nvsc_dev == NULL || ndev->real_num_tx_queues <= 1)
212 return 0;
213
Vitaly Kuznetsov757647e2016-01-25 16:00:41 +0100214 hash = skb_get_hash(skb);
215 q_idx = nvsc_dev->send_table[hash % VRSS_SEND_TAB_SIZE] %
216 ndev->real_num_tx_queues;
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700217
Vitaly Kuznetsov8b9fbe12015-12-01 16:43:11 -0800218 if (!nvsc_dev->chn_table[q_idx])
219 q_idx = 0;
220
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700221 return q_idx;
222}
223
KY Srinivasan54a73572014-03-08 19:23:13 -0800224static u32 fill_pg_buf(struct page *page, u32 offset, u32 len,
225 struct hv_page_buffer *pb)
226{
227 int j = 0;
228
229 /* Deal with compund pages by ignoring unused part
230 * of the page.
231 */
232 page += (offset >> PAGE_SHIFT);
233 offset &= ~PAGE_MASK;
234
235 while (len > 0) {
236 unsigned long bytes;
237
238 bytes = PAGE_SIZE - offset;
239 if (bytes > len)
240 bytes = len;
241 pb[j].pfn = page_to_pfn(page);
242 pb[j].offset = offset;
243 pb[j].len = bytes;
244
245 offset += bytes;
246 len -= bytes;
247
248 if (offset == PAGE_SIZE && len) {
249 page++;
250 offset = 0;
251 j++;
252 }
253 }
254
255 return j + 1;
256}
257
KY Srinivasan8a002512014-03-08 19:23:14 -0800258static u32 init_page_array(void *hdr, u32 len, struct sk_buff *skb,
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800259 struct hv_netvsc_packet *packet,
260 struct hv_page_buffer **page_buf)
KY Srinivasan54a73572014-03-08 19:23:13 -0800261{
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800262 struct hv_page_buffer *pb = *page_buf;
KY Srinivasan54a73572014-03-08 19:23:13 -0800263 u32 slots_used = 0;
264 char *data = skb->data;
265 int frags = skb_shinfo(skb)->nr_frags;
266 int i;
267
268 /* The packet is laid out thus:
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700269 * 1. hdr: RNDIS header and PPI
KY Srinivasan54a73572014-03-08 19:23:13 -0800270 * 2. skb linear data
271 * 3. skb fragment data
272 */
273 if (hdr != NULL)
274 slots_used += fill_pg_buf(virt_to_page(hdr),
275 offset_in_page(hdr),
276 len, &pb[slots_used]);
277
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700278 packet->rmsg_size = len;
279 packet->rmsg_pgcnt = slots_used;
280
KY Srinivasan54a73572014-03-08 19:23:13 -0800281 slots_used += fill_pg_buf(virt_to_page(data),
282 offset_in_page(data),
283 skb_headlen(skb), &pb[slots_used]);
284
285 for (i = 0; i < frags; i++) {
286 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
287
288 slots_used += fill_pg_buf(skb_frag_page(frag),
289 frag->page_offset,
290 skb_frag_size(frag), &pb[slots_used]);
291 }
KY Srinivasan8a002512014-03-08 19:23:14 -0800292 return slots_used;
KY Srinivasan54a73572014-03-08 19:23:13 -0800293}
294
295static int count_skb_frag_slots(struct sk_buff *skb)
296{
297 int i, frags = skb_shinfo(skb)->nr_frags;
298 int pages = 0;
299
300 for (i = 0; i < frags; i++) {
301 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
302 unsigned long size = skb_frag_size(frag);
303 unsigned long offset = frag->page_offset;
304
305 /* Skip unused frames from start of page */
306 offset &= ~PAGE_MASK;
307 pages += PFN_UP(offset + size);
308 }
309 return pages;
310}
311
312static int netvsc_get_slots(struct sk_buff *skb)
313{
314 char *data = skb->data;
315 unsigned int offset = offset_in_page(data);
316 unsigned int len = skb_headlen(skb);
317 int slots;
318 int frag_slots;
319
320 slots = DIV_ROUND_UP(offset + len, PAGE_SIZE);
321 frag_slots = count_skb_frag_slots(skb);
322 return slots + frag_slots;
323}
324
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800325static u32 get_net_transport_info(struct sk_buff *skb, u32 *trans_off)
326{
327 u32 ret_val = TRANSPORT_INFO_NOT_IP;
328
329 if ((eth_hdr(skb)->h_proto != htons(ETH_P_IP)) &&
330 (eth_hdr(skb)->h_proto != htons(ETH_P_IPV6))) {
331 goto not_ip;
332 }
333
334 *trans_off = skb_transport_offset(skb);
335
336 if ((eth_hdr(skb)->h_proto == htons(ETH_P_IP))) {
337 struct iphdr *iphdr = ip_hdr(skb);
338
339 if (iphdr->protocol == IPPROTO_TCP)
340 ret_val = TRANSPORT_INFO_IPV4_TCP;
341 else if (iphdr->protocol == IPPROTO_UDP)
342 ret_val = TRANSPORT_INFO_IPV4_UDP;
343 } else {
344 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
345 ret_val = TRANSPORT_INFO_IPV6_TCP;
346 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
347 ret_val = TRANSPORT_INFO_IPV6_UDP;
348 }
349
350not_ip:
351 return ret_val;
352}
353
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700354static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
Hank Janssenfceaf242009-07-13 15:34:54 -0700355{
Hank Janssenfceaf242009-07-13 15:34:54 -0700356 struct net_device_context *net_device_ctx = netdev_priv(net);
Vitaly Kuznetsov981a1bd2015-04-08 17:54:05 +0200357 struct hv_netvsc_packet *packet = NULL;
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700358 int ret;
KY Srinivasan8a002512014-03-08 19:23:14 -0800359 unsigned int num_data_pgs;
360 struct rndis_message *rndis_msg;
361 struct rndis_packet *rndis_pkt;
362 u32 rndis_msg_size;
363 bool isvlan;
Vitaly Kuznetsove88f7e02015-04-08 17:54:06 +0200364 bool linear = false;
KY Srinivasan8a002512014-03-08 19:23:14 -0800365 struct rndis_per_packet_info *ppi;
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800366 struct ndis_tcp_ip_checksum_info *csum_info;
KY Srinivasan77bf5482014-03-08 19:23:18 -0800367 struct ndis_tcp_lso_info *lso_info;
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800368 int hdr_offset;
369 u32 net_trans_info;
Haiyang Zhang307f0992014-05-21 12:55:39 -0700370 u32 hash;
Vitaly Kuznetsove88f7e02015-04-08 17:54:06 +0200371 u32 skb_length;
KY Srinivasanb08cc792015-03-29 21:08:42 -0700372 struct hv_page_buffer page_buf[MAX_PAGE_BUFFER_COUNT];
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800373 struct hv_page_buffer *pb = page_buf;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700374 struct netvsc_stats *tx_stats = this_cpu_ptr(net_device_ctx->tx_stats);
Hank Janssenfceaf242009-07-13 15:34:54 -0700375
KY Srinivasan54a73572014-03-08 19:23:13 -0800376 /* We will atmost need two pages to describe the rndis
377 * header. We can only transmit MAX_PAGE_BUFFER_COUNT number
Vitaly Kuznetsove88f7e02015-04-08 17:54:06 +0200378 * of pages in a single packet. If skb is scattered around
379 * more pages we try linearizing it.
KY Srinivasan54a73572014-03-08 19:23:13 -0800380 */
Vitaly Kuznetsove88f7e02015-04-08 17:54:06 +0200381
382check_size:
383 skb_length = skb->len;
KY Srinivasan8a002512014-03-08 19:23:14 -0800384 num_data_pgs = netvsc_get_slots(skb) + 2;
Vitaly Kuznetsove88f7e02015-04-08 17:54:06 +0200385 if (num_data_pgs > MAX_PAGE_BUFFER_COUNT && linear) {
386 net_alert_ratelimited("packet too big: %u pages (%u bytes)\n",
387 num_data_pgs, skb->len);
Vitaly Kuznetsov981a1bd2015-04-08 17:54:05 +0200388 ret = -EFAULT;
389 goto drop;
Vitaly Kuznetsove88f7e02015-04-08 17:54:06 +0200390 } else if (num_data_pgs > MAX_PAGE_BUFFER_COUNT) {
391 if (skb_linearize(skb)) {
392 net_alert_ratelimited("failed to linearize skb\n");
393 ret = -ENOMEM;
394 goto drop;
395 }
396 linear = true;
397 goto check_size;
KY Srinivasan54a73572014-03-08 19:23:13 -0800398 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700399
KY Srinivasanc0eb4542015-12-01 16:43:10 -0800400 /*
401 * Place the rndis header in the skb head room and
402 * the skb->cb will be used for hv_netvsc_packet
403 * structure.
404 */
405 ret = skb_cow_head(skb, RNDIS_AND_PPI_SIZE);
KY Srinivasanb56fc3c2015-04-28 17:59:48 -0700406 if (ret) {
407 netdev_err(net, "unable to alloc hv_netvsc_packet\n");
408 ret = -ENOMEM;
409 goto drop;
Hank Janssenfceaf242009-07-13 15:34:54 -0700410 }
KY Srinivasanc0eb4542015-12-01 16:43:10 -0800411 /* Use the skb control buffer for building up the packet */
412 BUILD_BUG_ON(sizeof(struct hv_netvsc_packet) >
413 FIELD_SIZEOF(struct sk_buff, cb));
414 packet = (struct hv_netvsc_packet *)skb->cb;
Hank Janssenfceaf242009-07-13 15:34:54 -0700415
Haiyang Zhang1f5f3a72012-03-12 10:20:50 +0000416
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700417 packet->q_idx = skb_get_queue_mapping(skb);
418
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800419 packet->total_data_buflen = skb->len;
Hank Janssenfceaf242009-07-13 15:34:54 -0700420
KY Srinivasanc0eb4542015-12-01 16:43:10 -0800421 rndis_msg = (struct rndis_message *)skb->head;
KY Srinivasanb08cc792015-03-29 21:08:42 -0700422
KY Srinivasan24476762015-12-01 16:43:06 -0800423 memset(rndis_msg, 0, RNDIS_AND_PPI_SIZE);
Hank Janssenfceaf242009-07-13 15:34:54 -0700424
KY Srinivasan760d1e32015-12-01 16:43:19 -0800425 isvlan = skb->vlan_tci & VLAN_TAG_PRESENT;
KY Srinivasan8a002512014-03-08 19:23:14 -0800426
427 /* Add the rndis header */
KY Srinivasan8a002512014-03-08 19:23:14 -0800428 rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET;
429 rndis_msg->msg_len = packet->total_data_buflen;
430 rndis_pkt = &rndis_msg->msg.pkt;
431 rndis_pkt->data_offset = sizeof(struct rndis_packet);
432 rndis_pkt->data_len = packet->total_data_buflen;
433 rndis_pkt->per_pkt_info_offset = sizeof(struct rndis_packet);
434
435 rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet);
436
Haiyang Zhang307f0992014-05-21 12:55:39 -0700437 hash = skb_get_hash_raw(skb);
438 if (hash != 0 && net->real_num_tx_queues > 1) {
439 rndis_msg_size += NDIS_HASH_PPI_SIZE;
440 ppi = init_ppi_data(rndis_msg, NDIS_HASH_PPI_SIZE,
441 NBL_HASH_VALUE);
442 *(u32 *)((void *)ppi + ppi->ppi_offset) = hash;
443 }
444
KY Srinivasan8a002512014-03-08 19:23:14 -0800445 if (isvlan) {
446 struct ndis_pkt_8021q_info *vlan;
447
448 rndis_msg_size += NDIS_VLAN_PPI_SIZE;
449 ppi = init_ppi_data(rndis_msg, NDIS_VLAN_PPI_SIZE,
450 IEEE_8021Q_INFO);
451 vlan = (struct ndis_pkt_8021q_info *)((void *)ppi +
452 ppi->ppi_offset);
KY Srinivasan760d1e32015-12-01 16:43:19 -0800453 vlan->vlanid = skb->vlan_tci & VLAN_VID_MASK;
454 vlan->pri = (skb->vlan_tci & VLAN_PRIO_MASK) >>
KY Srinivasan8a002512014-03-08 19:23:14 -0800455 VLAN_PRIO_SHIFT;
456 }
457
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800458 net_trans_info = get_net_transport_info(skb, &hdr_offset);
459 if (net_trans_info == TRANSPORT_INFO_NOT_IP)
460 goto do_send;
461
462 /*
463 * Setup the sendside checksum offload only if this is not a
464 * GSO packet.
465 */
466 if (skb_is_gso(skb))
KY Srinivasan77bf5482014-03-08 19:23:18 -0800467 goto do_lso;
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800468
KY Srinivasan22041fb2014-04-30 11:58:25 -0700469 if ((skb->ip_summed == CHECKSUM_NONE) ||
470 (skb->ip_summed == CHECKSUM_UNNECESSARY))
471 goto do_send;
472
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800473 rndis_msg_size += NDIS_CSUM_PPI_SIZE;
474 ppi = init_ppi_data(rndis_msg, NDIS_CSUM_PPI_SIZE,
475 TCPIP_CHKSUM_PKTINFO);
476
477 csum_info = (struct ndis_tcp_ip_checksum_info *)((void *)ppi +
478 ppi->ppi_offset);
479
480 if (net_trans_info & (INFO_IPV4 << 16))
481 csum_info->transmit.is_ipv4 = 1;
482 else
483 csum_info->transmit.is_ipv6 = 1;
484
485 if (net_trans_info & INFO_TCP) {
486 csum_info->transmit.tcp_checksum = 1;
487 csum_info->transmit.tcp_header_offset = hdr_offset;
488 } else if (net_trans_info & INFO_UDP) {
KY Srinivasanaf9893a2014-04-09 15:00:47 -0700489 /* UDP checksum offload is not supported on ws2008r2.
490 * Furthermore, on ws2012 and ws2012r2, there are some
491 * issues with udp checksum offload from Linux guests.
492 * (these are host issues).
493 * For now compute the checksum here.
494 */
495 struct udphdr *uh;
496 u16 udp_len;
497
498 ret = skb_cow_head(skb, 0);
499 if (ret)
500 goto drop;
501
502 uh = udp_hdr(skb);
503 udp_len = ntohs(uh->len);
504 uh->check = 0;
505 uh->check = csum_tcpudp_magic(ip_hdr(skb)->saddr,
506 ip_hdr(skb)->daddr,
507 udp_len, IPPROTO_UDP,
508 csum_partial(uh, udp_len, 0));
509 if (uh->check == 0)
510 uh->check = CSUM_MANGLED_0;
511
512 csum_info->transmit.udp_checksum = 0;
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800513 }
KY Srinivasan77bf5482014-03-08 19:23:18 -0800514 goto do_send;
515
516do_lso:
517 rndis_msg_size += NDIS_LSO_PPI_SIZE;
518 ppi = init_ppi_data(rndis_msg, NDIS_LSO_PPI_SIZE,
519 TCP_LARGESEND_PKTINFO);
520
521 lso_info = (struct ndis_tcp_lso_info *)((void *)ppi +
522 ppi->ppi_offset);
523
524 lso_info->lso_v2_transmit.type = NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
525 if (net_trans_info & (INFO_IPV4 << 16)) {
526 lso_info->lso_v2_transmit.ip_version =
527 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
528 ip_hdr(skb)->tot_len = 0;
529 ip_hdr(skb)->check = 0;
530 tcp_hdr(skb)->check =
531 ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
532 ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
533 } else {
534 lso_info->lso_v2_transmit.ip_version =
535 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
536 ipv6_hdr(skb)->payload_len = 0;
537 tcp_hdr(skb)->check =
538 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
539 &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
540 }
541 lso_info->lso_v2_transmit.tcp_header_offset = hdr_offset;
542 lso_info->lso_v2_transmit.mss = skb_shinfo(skb)->gso_size;
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800543
544do_send:
KY Srinivasan8a002512014-03-08 19:23:14 -0800545 /* Start filling in the page buffers with the rndis hdr */
546 rndis_msg->msg_len += rndis_msg_size;
Haiyang Zhang942396b2014-10-22 13:47:18 -0700547 packet->total_data_buflen = rndis_msg->msg_len;
KY Srinivasan8a002512014-03-08 19:23:14 -0800548 packet->page_buf_cnt = init_page_array(rndis_msg, rndis_msg_size,
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800549 skb, packet, &pb);
KY Srinivasan8a002512014-03-08 19:23:14 -0800550
sixiao@microsoft.com76d13b52016-02-17 16:43:59 -0800551 /* timestamp packet in software */
552 skb_tx_timestamp(skb);
KY Srinivasan3a3d9a02015-12-01 16:43:14 -0800553 ret = netvsc_send(net_device_ctx->device_ctx, packet,
554 rndis_msg, &pb, skb);
KY Srinivasan8a002512014-03-08 19:23:14 -0800555
KY Srinivasanaf9893a2014-04-09 15:00:47 -0700556drop:
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700557 if (ret == 0) {
sixiao@microsoft.com4b02b582015-05-15 02:33:03 -0700558 u64_stats_update_begin(&tx_stats->syncp);
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700559 tx_stats->packets++;
560 tx_stats->bytes += skb_length;
sixiao@microsoft.com4b02b582015-05-15 02:33:03 -0700561 u64_stats_update_end(&tx_stats->syncp);
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700562 } else {
Haiyang Zhang33be96e2012-03-27 13:20:45 +0000563 if (ret != -EAGAIN) {
564 dev_kfree_skb_any(skb);
565 net->stats.tx_dropped++;
566 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700567 }
568
Haiyang Zhang33be96e2012-03-27 13:20:45 +0000569 return (ret == -EAGAIN) ? NETDEV_TX_BUSY : NETDEV_TX_OK;
Hank Janssenfceaf242009-07-13 15:34:54 -0700570}
571
Hank Janssen3e189512010-03-04 22:11:00 +0000572/*
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700573 * netvsc_linkstatus_callback - Link up/down notification
574 */
K. Y. Srinivasan90ef1172011-05-12 19:34:50 -0700575void netvsc_linkstatus_callback(struct hv_device *device_obj,
Haiyang Zhang3a494e72014-06-19 18:34:36 -0700576 struct rndis_message *resp)
Hank Janssenfceaf242009-07-13 15:34:54 -0700577{
Haiyang Zhang3a494e72014-06-19 18:34:36 -0700578 struct rndis_indicate_status *indicate = &resp->msg.indicate_status;
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -0700579 struct net_device *net;
Haiyang Zhangc996edc2011-04-06 15:18:00 -0700580 struct net_device_context *ndev_ctx;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +0100581 struct netvsc_reconfig *event;
582 unsigned long flags;
583
584 /* Handle link change statuses only */
585 if (indicate->status != RNDIS_STATUS_NETWORK_CHANGE &&
586 indicate->status != RNDIS_STATUS_MEDIA_CONNECT &&
587 indicate->status != RNDIS_STATUS_MEDIA_DISCONNECT)
588 return;
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -0700589
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200590 net = hv_get_drvdata(device_obj);
Hank Janssenfceaf242009-07-13 15:34:54 -0700591
Haiyang Zhang891de742014-02-12 16:54:27 -0800592 if (!net || net->reg_state != NETREG_REGISTERED)
Hank Janssenfceaf242009-07-13 15:34:54 -0700593 return;
Hank Janssenfceaf242009-07-13 15:34:54 -0700594
Haiyang Zhang891de742014-02-12 16:54:27 -0800595 ndev_ctx = netdev_priv(net);
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +0100596
597 event = kzalloc(sizeof(*event), GFP_ATOMIC);
598 if (!event)
599 return;
600 event->event = indicate->status;
601
602 spin_lock_irqsave(&ndev_ctx->lock, flags);
603 list_add_tail(&event->list, &ndev_ctx->reconfig_events);
604 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
605
606 schedule_delayed_work(&ndev_ctx->dwork, 0);
Hank Janssenfceaf242009-07-13 15:34:54 -0700607}
608
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700609
610static struct sk_buff *netvsc_alloc_recv_skb(struct net_device *net,
KY Srinivasane3d605e2014-03-08 19:23:16 -0800611 struct hv_netvsc_packet *packet,
KY Srinivasan25b85ee2015-12-01 16:43:05 -0800612 struct ndis_tcp_ip_checksum_info *csum_info,
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700613 void *data, u16 vlan_tci)
Hank Janssenfceaf242009-07-13 15:34:54 -0700614{
Hank Janssenfceaf242009-07-13 15:34:54 -0700615 struct sk_buff *skb;
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -0700616
Haiyang Zhang72a2f5b2010-12-10 12:03:58 -0800617 skb = netdev_alloc_skb_ip_align(net, packet->total_data_buflen);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700618 if (!skb)
619 return skb;
Hank Janssenfceaf242009-07-13 15:34:54 -0700620
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700621 /*
622 * Copy to skb. This copy is needed here since the memory pointed by
623 * hv_netvsc_packet cannot be deallocated
624 */
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700625 memcpy(skb_put(skb, packet->total_data_buflen), data,
626 packet->total_data_buflen);
Hank Janssenfceaf242009-07-13 15:34:54 -0700627
628 skb->protocol = eth_type_trans(skb, net);
KY Srinivasane3d605e2014-03-08 19:23:16 -0800629 if (csum_info) {
630 /* We only look at the IP checksum here.
631 * Should we be dropping the packet if checksum
632 * failed? How do we deal with other checksums - TCP/UDP?
633 */
634 if (csum_info->receive.ip_checksum_succeeded)
635 skb->ip_summed = CHECKSUM_UNNECESSARY;
636 else
637 skb->ip_summed = CHECKSUM_NONE;
638 }
639
KY Srinivasan760d1e32015-12-01 16:43:19 -0800640 if (vlan_tci & VLAN_TAG_PRESENT)
Haiyang Zhang93725cb2013-06-17 15:36:49 -0700641 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
KY Srinivasan760d1e32015-12-01 16:43:19 -0800642 vlan_tci);
Hank Janssenfceaf242009-07-13 15:34:54 -0700643
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700644 return skb;
645}
646
647/*
648 * netvsc_recv_callback - Callback when we receive a packet from the
649 * "wire" on the specified device.
650 */
651int netvsc_recv_callback(struct hv_device *device_obj,
652 struct hv_netvsc_packet *packet,
653 void **data,
654 struct ndis_tcp_ip_checksum_info *csum_info,
655 struct vmbus_channel *channel,
656 u16 vlan_tci)
657{
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200658 struct net_device *net = hv_get_drvdata(device_obj);
659 struct net_device_context *net_device_ctx = netdev_priv(net);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700660 struct sk_buff *skb;
661 struct sk_buff *vf_skb;
662 struct netvsc_stats *rx_stats;
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200663 struct netvsc_device *netvsc_dev = net_device_ctx->nvdev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700664 u32 bytes_recvd = packet->total_data_buflen;
665 int ret = 0;
666
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700667 if (!net || net->reg_state != NETREG_REGISTERED)
668 return NVSP_STAT_FAIL;
669
670 if (READ_ONCE(netvsc_dev->vf_inject)) {
671 atomic_inc(&netvsc_dev->vf_use_cnt);
672 if (!READ_ONCE(netvsc_dev->vf_inject)) {
673 /*
674 * We raced; just move on.
675 */
676 atomic_dec(&netvsc_dev->vf_use_cnt);
677 goto vf_injection_done;
678 }
679
680 /*
681 * Inject this packet into the VF inerface.
682 * On Hyper-V, multicast and brodcast packets
683 * are only delivered on the synthetic interface
684 * (after subjecting these to policy filters on
685 * the host). Deliver these via the VF interface
686 * in the guest.
687 */
688 vf_skb = netvsc_alloc_recv_skb(netvsc_dev->vf_netdev, packet,
689 csum_info, *data, vlan_tci);
690 if (vf_skb != NULL) {
691 ++netvsc_dev->vf_netdev->stats.rx_packets;
692 netvsc_dev->vf_netdev->stats.rx_bytes += bytes_recvd;
693 netif_receive_skb(vf_skb);
694 } else {
695 ++net->stats.rx_dropped;
696 ret = NVSP_STAT_FAIL;
697 }
698 atomic_dec(&netvsc_dev->vf_use_cnt);
699 return ret;
700 }
701
702vf_injection_done:
703 net_device_ctx = netdev_priv(net);
704 rx_stats = this_cpu_ptr(net_device_ctx->rx_stats);
705
706 /* Allocate a skb - TODO direct I/O to pages? */
707 skb = netvsc_alloc_recv_skb(net, packet, csum_info, *data, vlan_tci);
708 if (unlikely(!skb)) {
709 ++net->stats.rx_dropped;
710 return NVSP_STAT_FAIL;
711 }
KY Srinivasan25b85ee2015-12-01 16:43:05 -0800712 skb_record_rx_queue(skb, channel->
Haiyang Zhange565e802014-05-08 15:14:10 -0700713 offermsg.offer.sub_channel_index);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700714
sixiao@microsoft.com4b02b582015-05-15 02:33:03 -0700715 u64_stats_update_begin(&rx_stats->syncp);
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700716 rx_stats->packets++;
717 rx_stats->bytes += packet->total_data_buflen;
sixiao@microsoft.com4b02b582015-05-15 02:33:03 -0700718 u64_stats_update_end(&rx_stats->syncp);
Stephen Hemminger9495c282010-03-09 17:42:17 -0800719
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700720 /*
721 * Pass the skb back up. Network stack will deallocate the skb when it
Stephen Hemminger9495c282010-03-09 17:42:17 -0800722 * is done.
723 * TODO - use NAPI?
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700724 */
Stephen Hemminger9495c282010-03-09 17:42:17 -0800725 netif_rx(skb);
Hank Janssenfceaf242009-07-13 15:34:54 -0700726
Hank Janssenfceaf242009-07-13 15:34:54 -0700727 return 0;
728}
729
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -0700730static void netvsc_get_drvinfo(struct net_device *net,
731 struct ethtool_drvinfo *info)
732{
Jiri Pirko7826d432013-01-06 00:44:26 +0000733 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
Jiri Pirko7826d432013-01-06 00:44:26 +0000734 strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -0700735}
736
Andrew Schwartzmeyer59995372015-02-26 16:27:14 -0800737static void netvsc_get_channels(struct net_device *net,
738 struct ethtool_channels *channel)
739{
740 struct net_device_context *net_device_ctx = netdev_priv(net);
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200741 struct netvsc_device *nvdev = net_device_ctx->nvdev;
Andrew Schwartzmeyer59995372015-02-26 16:27:14 -0800742
743 if (nvdev) {
744 channel->max_combined = nvdev->max_chn;
745 channel->combined_count = nvdev->num_chn;
746 }
747}
748
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700749static int netvsc_set_channels(struct net_device *net,
750 struct ethtool_channels *channels)
751{
752 struct net_device_context *net_device_ctx = netdev_priv(net);
753 struct hv_device *dev = net_device_ctx->device_ctx;
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200754 struct netvsc_device *nvdev = net_device_ctx->nvdev;
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700755 struct netvsc_device_info device_info;
Andrew Schwartzmeyer954591b2015-08-18 20:06:32 -0700756 u32 num_chn;
757 u32 max_chn;
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700758 int ret = 0;
759 bool recovering = false;
760
761 if (!nvdev || nvdev->destroy)
762 return -ENODEV;
763
Andrew Schwartzmeyer954591b2015-08-18 20:06:32 -0700764 num_chn = nvdev->num_chn;
765 max_chn = min_t(u32, nvdev->max_chn, num_online_cpus());
766
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700767 if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5) {
768 pr_info("vRSS unsupported before NVSP Version 5\n");
769 return -EINVAL;
770 }
771
772 /* We do not support rx, tx, or other */
773 if (!channels ||
774 channels->rx_count ||
775 channels->tx_count ||
776 channels->other_count ||
777 (channels->combined_count < 1))
778 return -EINVAL;
779
780 if (channels->combined_count > max_chn) {
781 pr_info("combined channels too high, using %d\n", max_chn);
782 channels->combined_count = max_chn;
783 }
784
785 ret = netvsc_close(net);
786 if (ret)
787 goto out;
788
789 do_set:
Vitaly Kuznetsovf580aec2016-05-13 13:55:20 +0200790 net_device_ctx->start_remove = true;
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700791 rndis_filter_device_remove(dev);
792
793 nvdev->num_chn = channels->combined_count;
794
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700795 memset(&device_info, 0, sizeof(device_info));
796 device_info.num_chn = nvdev->num_chn; /* passed to RNDIS */
797 device_info.ring_size = ring_size;
798 device_info.max_num_vrss_chns = max_num_vrss_chns;
799
800 ret = rndis_filter_device_add(dev, &device_info);
801 if (ret) {
802 if (recovering) {
803 netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
804 return ret;
805 }
806 goto recover;
807 }
808
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200809 nvdev = net_device_ctx->nvdev;
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700810
811 ret = netif_set_real_num_tx_queues(net, nvdev->num_chn);
812 if (ret) {
813 if (recovering) {
814 netdev_err(net, "could not set tx queue count (ret %d)\n", ret);
815 return ret;
816 }
817 goto recover;
818 }
819
820 ret = netif_set_real_num_rx_queues(net, nvdev->num_chn);
821 if (ret) {
822 if (recovering) {
823 netdev_err(net, "could not set rx queue count (ret %d)\n", ret);
824 return ret;
825 }
826 goto recover;
827 }
828
829 out:
830 netvsc_open(net);
Vitaly Kuznetsovf580aec2016-05-13 13:55:20 +0200831 net_device_ctx->start_remove = false;
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +0200832 /* We may have missed link change notifications */
833 schedule_delayed_work(&net_device_ctx->dwork, 0);
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700834
835 return ret;
836
837 recover:
838 /* If the above failed, we attempt to recover through the same
839 * process but with the original number of channels.
840 */
841 netdev_err(net, "could not set channels, recovering\n");
842 recovering = true;
843 channels->combined_count = num_chn;
844 goto do_set;
845}
846
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800847static bool netvsc_validate_ethtool_ss_cmd(const struct ethtool_cmd *cmd)
848{
849 struct ethtool_cmd diff1 = *cmd;
850 struct ethtool_cmd diff2 = {};
851
852 ethtool_cmd_speed_set(&diff1, 0);
853 diff1.duplex = 0;
854 /* advertising and cmd are usually set */
855 diff1.advertising = 0;
856 diff1.cmd = 0;
857 /* We set port to PORT_OTHER */
858 diff2.port = PORT_OTHER;
859
860 return !memcmp(&diff1, &diff2, sizeof(diff1));
861}
862
863static void netvsc_init_settings(struct net_device *dev)
864{
865 struct net_device_context *ndc = netdev_priv(dev);
866
867 ndc->speed = SPEED_UNKNOWN;
868 ndc->duplex = DUPLEX_UNKNOWN;
869}
870
871static int netvsc_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
872{
873 struct net_device_context *ndc = netdev_priv(dev);
874
875 ethtool_cmd_speed_set(cmd, ndc->speed);
876 cmd->duplex = ndc->duplex;
877 cmd->port = PORT_OTHER;
878
879 return 0;
880}
881
882static int netvsc_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
883{
884 struct net_device_context *ndc = netdev_priv(dev);
885 u32 speed;
886
887 speed = ethtool_cmd_speed(cmd);
888 if (!ethtool_validate_speed(speed) ||
889 !ethtool_validate_duplex(cmd->duplex) ||
890 !netvsc_validate_ethtool_ss_cmd(cmd))
891 return -EINVAL;
892
893 ndc->speed = speed;
894 ndc->duplex = cmd->duplex;
895
896 return 0;
897}
898
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800899static int netvsc_change_mtu(struct net_device *ndev, int mtu)
900{
901 struct net_device_context *ndevctx = netdev_priv(ndev);
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200902 struct netvsc_device *nvdev = ndevctx->nvdev;
903 struct hv_device *hdev = ndevctx->device_ctx;
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800904 struct netvsc_device_info device_info;
905 int limit = ETH_DATA_LEN;
Haiyang Zhangd212b462016-03-23 09:43:09 -0700906 u32 num_chn;
Haiyang Zhang2de85302015-07-13 13:09:16 -0700907 int ret = 0;
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800908
909 if (nvdev == NULL || nvdev->destroy)
910 return -ENODEV;
911
Haiyang Zhanga1eabb02014-02-19 15:49:45 -0800912 if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
Haiyang Zhang4d3c9d32014-11-12 14:07:44 -0800913 limit = NETVSC_MTU - ETH_HLEN;
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800914
Haiyang Zhangf9cbce32015-07-06 14:11:37 -0700915 if (mtu < NETVSC_MTU_MIN || mtu > limit)
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800916 return -EINVAL;
917
Haiyang Zhang2de85302015-07-13 13:09:16 -0700918 ret = netvsc_close(ndev);
919 if (ret)
920 goto out;
921
Haiyang Zhangd212b462016-03-23 09:43:09 -0700922 num_chn = nvdev->num_chn;
923
Vitaly Kuznetsovf580aec2016-05-13 13:55:20 +0200924 ndevctx->start_remove = true;
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800925 rndis_filter_device_remove(hdev);
926
927 ndev->mtu = mtu;
928
Andrew Schwartzmeyer8ebdcc52015-08-11 17:14:31 -0700929 memset(&device_info, 0, sizeof(device_info));
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800930 device_info.ring_size = ring_size;
Haiyang Zhangd212b462016-03-23 09:43:09 -0700931 device_info.num_chn = num_chn;
KY Srinivasane01ec212015-05-27 13:16:57 -0700932 device_info.max_num_vrss_chns = max_num_vrss_chns;
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800933 rndis_filter_device_add(hdev, &device_info);
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800934
Haiyang Zhang2de85302015-07-13 13:09:16 -0700935out:
936 netvsc_open(ndev);
Vitaly Kuznetsovf580aec2016-05-13 13:55:20 +0200937 ndevctx->start_remove = false;
Haiyang Zhang2de85302015-07-13 13:09:16 -0700938
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +0200939 /* We may have missed link change notifications */
940 schedule_delayed_work(&ndevctx->dwork, 0);
941
Haiyang Zhang2de85302015-07-13 13:09:16 -0700942 return ret;
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800943}
944
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700945static struct rtnl_link_stats64 *netvsc_get_stats64(struct net_device *net,
946 struct rtnl_link_stats64 *t)
947{
948 struct net_device_context *ndev_ctx = netdev_priv(net);
949 int cpu;
950
951 for_each_possible_cpu(cpu) {
952 struct netvsc_stats *tx_stats = per_cpu_ptr(ndev_ctx->tx_stats,
953 cpu);
954 struct netvsc_stats *rx_stats = per_cpu_ptr(ndev_ctx->rx_stats,
955 cpu);
956 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
957 unsigned int start;
958
959 do {
sixiao@microsoft.com4b02b582015-05-15 02:33:03 -0700960 start = u64_stats_fetch_begin_irq(&tx_stats->syncp);
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700961 tx_packets = tx_stats->packets;
962 tx_bytes = tx_stats->bytes;
sixiao@microsoft.com4b02b582015-05-15 02:33:03 -0700963 } while (u64_stats_fetch_retry_irq(&tx_stats->syncp, start));
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700964
965 do {
sixiao@microsoft.com4b02b582015-05-15 02:33:03 -0700966 start = u64_stats_fetch_begin_irq(&rx_stats->syncp);
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700967 rx_packets = rx_stats->packets;
968 rx_bytes = rx_stats->bytes;
sixiao@microsoft.com4b02b582015-05-15 02:33:03 -0700969 } while (u64_stats_fetch_retry_irq(&rx_stats->syncp, start));
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700970
971 t->tx_bytes += tx_bytes;
972 t->tx_packets += tx_packets;
973 t->rx_bytes += rx_bytes;
974 t->rx_packets += rx_packets;
975 }
976
977 t->tx_dropped = net->stats.tx_dropped;
978 t->tx_errors = net->stats.tx_dropped;
979
980 t->rx_dropped = net->stats.rx_dropped;
981 t->rx_errors = net->stats.rx_errors;
982
983 return t;
984}
Haiyang Zhang1ce09e82012-07-10 07:19:22 +0000985
986static int netvsc_set_mac_addr(struct net_device *ndev, void *p)
987{
988 struct net_device_context *ndevctx = netdev_priv(ndev);
989 struct hv_device *hdev = ndevctx->device_ctx;
990 struct sockaddr *addr = p;
Jianjun Kong9a4c8312013-01-18 16:52:09 +0000991 char save_adr[ETH_ALEN];
Haiyang Zhang1ce09e82012-07-10 07:19:22 +0000992 unsigned char save_aatype;
993 int err;
994
995 memcpy(save_adr, ndev->dev_addr, ETH_ALEN);
996 save_aatype = ndev->addr_assign_type;
997
998 err = eth_mac_addr(ndev, p);
999 if (err != 0)
1000 return err;
1001
1002 err = rndis_filter_set_device_mac(hdev, addr->sa_data);
1003 if (err != 0) {
1004 /* roll back to saved MAC */
1005 memcpy(ndev->dev_addr, save_adr, ETH_ALEN);
1006 ndev->addr_assign_type = save_aatype;
1007 }
1008
1009 return err;
1010}
1011
Richard Weinberger316158f2014-07-09 16:23:59 +02001012#ifdef CONFIG_NET_POLL_CONTROLLER
1013static void netvsc_poll_controller(struct net_device *net)
1014{
1015 /* As netvsc_start_xmit() works synchronous we don't have to
1016 * trigger anything here.
1017 */
1018}
1019#endif
Haiyang Zhang1ce09e82012-07-10 07:19:22 +00001020
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -07001021static const struct ethtool_ops ethtool_ops = {
1022 .get_drvinfo = netvsc_get_drvinfo,
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -07001023 .get_link = ethtool_op_get_link,
Andrew Schwartzmeyer59995372015-02-26 16:27:14 -08001024 .get_channels = netvsc_get_channels,
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -07001025 .set_channels = netvsc_set_channels,
sixiao@microsoft.com76d13b52016-02-17 16:43:59 -08001026 .get_ts_info = ethtool_op_get_ts_info,
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -08001027 .get_settings = netvsc_get_settings,
1028 .set_settings = netvsc_set_settings,
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -07001029};
1030
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001031static const struct net_device_ops device_ops = {
1032 .ndo_open = netvsc_open,
1033 .ndo_stop = netvsc_close,
1034 .ndo_start_xmit = netvsc_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001035 .ndo_set_rx_mode = netvsc_set_multicast_list,
Haiyang Zhang4d447c92011-12-15 13:45:17 -08001036 .ndo_change_mtu = netvsc_change_mtu,
Haiyang Zhangb681b582010-08-03 19:15:31 +00001037 .ndo_validate_addr = eth_validate_addr,
Haiyang Zhang1ce09e82012-07-10 07:19:22 +00001038 .ndo_set_mac_address = netvsc_set_mac_addr,
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001039 .ndo_select_queue = netvsc_select_queue,
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001040 .ndo_get_stats64 = netvsc_get_stats64,
Richard Weinberger316158f2014-07-09 16:23:59 +02001041#ifdef CONFIG_NET_POLL_CONTROLLER
1042 .ndo_poll_controller = netvsc_poll_controller,
1043#endif
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001044};
1045
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001046/*
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001047 * Handle link status changes. For RNDIS_STATUS_NETWORK_CHANGE emulate link
1048 * down/up sequence. In case of RNDIS_STATUS_MEDIA_CONNECT when carrier is
1049 * present send GARP packet to network peers with netif_notify_peers().
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001050 */
Haiyang Zhang891de742014-02-12 16:54:27 -08001051static void netvsc_link_change(struct work_struct *w)
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001052{
1053 struct net_device_context *ndev_ctx;
1054 struct net_device *net;
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -07001055 struct netvsc_device *net_device;
Haiyang Zhang891de742014-02-12 16:54:27 -08001056 struct rndis_device *rdev;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001057 struct netvsc_reconfig *event = NULL;
1058 bool notify = false, reschedule = false;
1059 unsigned long flags, next_reconfig, delay;
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001060
Haiyang Zhang122a5f62011-05-27 06:21:55 -07001061 ndev_ctx = container_of(w, struct net_device_context, dwork.work);
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001062
1063 rtnl_lock();
1064 if (ndev_ctx->start_remove)
1065 goto out_unlock;
1066
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +02001067 net_device = ndev_ctx->nvdev;
Haiyang Zhang891de742014-02-12 16:54:27 -08001068 rdev = net_device->extension;
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -07001069 net = net_device->ndev;
Haiyang Zhang891de742014-02-12 16:54:27 -08001070
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001071 next_reconfig = ndev_ctx->last_reconfig + LINKCHANGE_INT;
1072 if (time_is_after_jiffies(next_reconfig)) {
1073 /* link_watch only sends one notification with current state
1074 * per second, avoid doing reconfig more frequently. Handle
1075 * wrap around.
1076 */
1077 delay = next_reconfig - jiffies;
1078 delay = delay < LINKCHANGE_INT ? delay : LINKCHANGE_INT;
1079 schedule_delayed_work(&ndev_ctx->dwork, delay);
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001080 goto out_unlock;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001081 }
1082 ndev_ctx->last_reconfig = jiffies;
1083
1084 spin_lock_irqsave(&ndev_ctx->lock, flags);
1085 if (!list_empty(&ndev_ctx->reconfig_events)) {
1086 event = list_first_entry(&ndev_ctx->reconfig_events,
1087 struct netvsc_reconfig, list);
1088 list_del(&event->list);
1089 reschedule = !list_empty(&ndev_ctx->reconfig_events);
1090 }
1091 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1092
1093 if (!event)
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001094 goto out_unlock;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001095
1096 switch (event->event) {
1097 /* Only the following events are possible due to the check in
1098 * netvsc_linkstatus_callback()
1099 */
1100 case RNDIS_STATUS_MEDIA_CONNECT:
1101 if (rdev->link_state) {
1102 rdev->link_state = false;
1103 netif_carrier_on(net);
1104 netif_tx_wake_all_queues(net);
1105 } else {
1106 notify = true;
Haiyang Zhang3a494e72014-06-19 18:34:36 -07001107 }
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001108 kfree(event);
1109 break;
1110 case RNDIS_STATUS_MEDIA_DISCONNECT:
1111 if (!rdev->link_state) {
1112 rdev->link_state = true;
1113 netif_carrier_off(net);
1114 netif_tx_stop_all_queues(net);
1115 }
1116 kfree(event);
1117 break;
1118 case RNDIS_STATUS_NETWORK_CHANGE:
1119 /* Only makes sense if carrier is present */
1120 if (!rdev->link_state) {
1121 rdev->link_state = true;
1122 netif_carrier_off(net);
1123 netif_tx_stop_all_queues(net);
1124 event->event = RNDIS_STATUS_MEDIA_CONNECT;
1125 spin_lock_irqsave(&ndev_ctx->lock, flags);
Haiyang Zhang15cfd402016-04-21 16:13:01 -07001126 list_add(&event->list, &ndev_ctx->reconfig_events);
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001127 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1128 reschedule = true;
1129 }
1130 break;
Haiyang Zhang891de742014-02-12 16:54:27 -08001131 }
1132
1133 rtnl_unlock();
1134
1135 if (notify)
1136 netdev_notify_peers(net);
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001137
1138 /* link_watch only sends one notification with current state per
1139 * second, handle next reconfig event in 2 seconds.
1140 */
1141 if (reschedule)
1142 schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001143
1144 return;
1145
1146out_unlock:
1147 rtnl_unlock();
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001148}
1149
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001150static void netvsc_free_netdev(struct net_device *netdev)
1151{
1152 struct net_device_context *net_device_ctx = netdev_priv(netdev);
1153
1154 free_percpu(net_device_ctx->tx_stats);
1155 free_percpu(net_device_ctx->rx_stats);
1156 free_netdev(netdev);
1157}
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001158
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001159static void netvsc_notify_peers(struct work_struct *wrk)
1160{
1161 struct garp_wrk *gwrk;
1162
1163 gwrk = container_of(wrk, struct garp_wrk, dwrk);
1164
1165 netdev_notify_peers(gwrk->netdev);
1166
1167 atomic_dec(&gwrk->netvsc_dev->vf_use_cnt);
1168}
1169
1170static struct netvsc_device *get_netvsc_device(char *mac)
1171{
1172 struct net_device *dev;
1173 struct net_device_context *netvsc_ctx = NULL;
1174 int rtnl_locked;
1175
1176 rtnl_locked = rtnl_trylock();
1177
1178 for_each_netdev(&init_net, dev) {
1179 if (memcmp(dev->dev_addr, mac, ETH_ALEN) == 0) {
1180 if (dev->netdev_ops != &device_ops)
1181 continue;
1182 netvsc_ctx = netdev_priv(dev);
1183 break;
1184 }
1185 }
1186 if (rtnl_locked)
1187 rtnl_unlock();
1188
1189 if (netvsc_ctx == NULL)
1190 return NULL;
1191
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +02001192 return netvsc_ctx->nvdev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001193}
1194
1195static int netvsc_register_vf(struct net_device *vf_netdev)
1196{
1197 struct netvsc_device *netvsc_dev;
1198 const struct ethtool_ops *eth_ops = vf_netdev->ethtool_ops;
1199
1200 if (eth_ops == NULL || eth_ops == &ethtool_ops)
1201 return NOTIFY_DONE;
1202
1203 /*
1204 * We will use the MAC address to locate the synthetic interface to
1205 * associate with the VF interface. If we don't find a matching
1206 * synthetic interface, move on.
1207 */
1208 netvsc_dev = get_netvsc_device(vf_netdev->dev_addr);
1209 if (netvsc_dev == NULL)
1210 return NOTIFY_DONE;
1211
1212 netdev_info(netvsc_dev->ndev, "VF registering: %s\n", vf_netdev->name);
1213 /*
1214 * Take a reference on the module.
1215 */
1216 try_module_get(THIS_MODULE);
1217 netvsc_dev->vf_netdev = vf_netdev;
1218 return NOTIFY_OK;
1219}
1220
1221
1222static int netvsc_vf_up(struct net_device *vf_netdev)
1223{
1224 struct netvsc_device *netvsc_dev;
1225 const struct ethtool_ops *eth_ops = vf_netdev->ethtool_ops;
1226 struct net_device_context *net_device_ctx;
1227
1228 if (eth_ops == &ethtool_ops)
1229 return NOTIFY_DONE;
1230
1231 netvsc_dev = get_netvsc_device(vf_netdev->dev_addr);
1232
1233 if ((netvsc_dev == NULL) || (netvsc_dev->vf_netdev == NULL))
1234 return NOTIFY_DONE;
1235
1236 netdev_info(netvsc_dev->ndev, "VF up: %s\n", vf_netdev->name);
1237 net_device_ctx = netdev_priv(netvsc_dev->ndev);
1238 netvsc_dev->vf_inject = true;
1239
1240 /*
1241 * Open the device before switching data path.
1242 */
1243 rndis_filter_open(net_device_ctx->device_ctx);
1244
1245 /*
1246 * notify the host to switch the data path.
1247 */
1248 netvsc_switch_datapath(netvsc_dev, true);
1249 netdev_info(netvsc_dev->ndev, "Data path switched to VF: %s\n",
1250 vf_netdev->name);
1251
1252 netif_carrier_off(netvsc_dev->ndev);
1253
1254 /*
1255 * Now notify peers. We are scheduling work to
1256 * notify peers; take a reference to prevent
1257 * the VF interface from vanishing.
1258 */
1259 atomic_inc(&netvsc_dev->vf_use_cnt);
1260 net_device_ctx->gwrk.netdev = vf_netdev;
1261 net_device_ctx->gwrk.netvsc_dev = netvsc_dev;
1262 schedule_work(&net_device_ctx->gwrk.dwrk);
1263
1264 return NOTIFY_OK;
1265}
1266
1267
1268static int netvsc_vf_down(struct net_device *vf_netdev)
1269{
1270 struct netvsc_device *netvsc_dev;
1271 struct net_device_context *net_device_ctx;
1272 const struct ethtool_ops *eth_ops = vf_netdev->ethtool_ops;
1273
1274 if (eth_ops == &ethtool_ops)
1275 return NOTIFY_DONE;
1276
1277 netvsc_dev = get_netvsc_device(vf_netdev->dev_addr);
1278
1279 if ((netvsc_dev == NULL) || (netvsc_dev->vf_netdev == NULL))
1280 return NOTIFY_DONE;
1281
1282 netdev_info(netvsc_dev->ndev, "VF down: %s\n", vf_netdev->name);
1283 net_device_ctx = netdev_priv(netvsc_dev->ndev);
1284 netvsc_dev->vf_inject = false;
1285 /*
1286 * Wait for currently active users to
1287 * drain out.
1288 */
1289
1290 while (atomic_read(&netvsc_dev->vf_use_cnt) != 0)
1291 udelay(50);
1292 netvsc_switch_datapath(netvsc_dev, false);
1293 netdev_info(netvsc_dev->ndev, "Data path switched from VF: %s\n",
1294 vf_netdev->name);
1295 rndis_filter_close(net_device_ctx->device_ctx);
1296 netif_carrier_on(netvsc_dev->ndev);
1297 /*
1298 * Notify peers.
1299 */
1300 atomic_inc(&netvsc_dev->vf_use_cnt);
1301 net_device_ctx->gwrk.netdev = netvsc_dev->ndev;
1302 net_device_ctx->gwrk.netvsc_dev = netvsc_dev;
1303 schedule_work(&net_device_ctx->gwrk.dwrk);
1304
1305 return NOTIFY_OK;
1306}
1307
1308
1309static int netvsc_unregister_vf(struct net_device *vf_netdev)
1310{
1311 struct netvsc_device *netvsc_dev;
1312 const struct ethtool_ops *eth_ops = vf_netdev->ethtool_ops;
1313
1314 if (eth_ops == &ethtool_ops)
1315 return NOTIFY_DONE;
1316
1317 netvsc_dev = get_netvsc_device(vf_netdev->dev_addr);
1318 if (netvsc_dev == NULL)
1319 return NOTIFY_DONE;
1320 netdev_info(netvsc_dev->ndev, "VF unregistering: %s\n",
1321 vf_netdev->name);
1322
1323 netvsc_dev->vf_netdev = NULL;
1324 module_put(THIS_MODULE);
1325 return NOTIFY_OK;
1326}
1327
K. Y. Srinivasan84946892011-09-13 10:59:38 -07001328static int netvsc_probe(struct hv_device *dev,
1329 const struct hv_vmbus_device_id *dev_id)
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001330{
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001331 struct net_device *net = NULL;
1332 struct net_device_context *net_device_ctx;
1333 struct netvsc_device_info device_info;
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001334 struct netvsc_device *nvdev;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001335 int ret;
1336
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001337 net = alloc_etherdev_mq(sizeof(struct net_device_context),
1338 num_online_cpus());
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001339 if (!net)
K. Y. Srinivasan51a805d2011-08-25 09:49:11 -07001340 return -ENOMEM;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001341
Haiyang Zhang1b07da52014-03-04 14:11:06 -08001342 netif_carrier_off(net);
1343
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001344 net_device_ctx = netdev_priv(net);
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -07001345 net_device_ctx->device_ctx = dev;
Simon Xiao3f300ff2015-04-28 01:05:17 -07001346 net_device_ctx->msg_enable = netif_msg_init(debug, default_msg);
1347 if (netif_msg_probe(net_device_ctx))
1348 netdev_dbg(net, "netvsc msg_enable: %d\n",
1349 net_device_ctx->msg_enable);
1350
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001351 net_device_ctx->tx_stats = netdev_alloc_pcpu_stats(struct netvsc_stats);
1352 if (!net_device_ctx->tx_stats) {
1353 free_netdev(net);
1354 return -ENOMEM;
1355 }
1356 net_device_ctx->rx_stats = netdev_alloc_pcpu_stats(struct netvsc_stats);
1357 if (!net_device_ctx->rx_stats) {
1358 free_percpu(net_device_ctx->tx_stats);
1359 free_netdev(net);
1360 return -ENOMEM;
1361 }
1362
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -07001363 hv_set_drvdata(dev, net);
Vitaly Kuznetsovf580aec2016-05-13 13:55:20 +02001364
1365 net_device_ctx->start_remove = false;
1366
Haiyang Zhang891de742014-02-12 16:54:27 -08001367 INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change);
Wenqi Ma792df872012-04-19 00:39:37 +00001368 INIT_WORK(&net_device_ctx->work, do_set_multicast);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001369 INIT_WORK(&net_device_ctx->gwrk.dwrk, netvsc_notify_peers);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001370
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001371 spin_lock_init(&net_device_ctx->lock);
1372 INIT_LIST_HEAD(&net_device_ctx->reconfig_events);
1373
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001374 net->netdev_ops = &device_ops;
1375
sixiao@microsoft.coma0606792016-02-04 15:49:34 -08001376 net->hw_features = NETVSC_HW_FEATURES;
1377 net->features = NETVSC_HW_FEATURES | NETIF_F_HW_VLAN_CTAG_TX;
Stephen Hemminger60487182010-05-04 09:58:55 -07001378
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001379 net->ethtool_ops = &ethtool_ops;
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -07001380 SET_NETDEV_DEV(net, &dev->device);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001381
Vitaly Kuznetsov14a03cf2016-02-05 17:29:08 +01001382 /* We always need headroom for rndis header */
1383 net->needed_headroom = RNDIS_AND_PPI_SIZE;
1384
Haiyang Zhang692e0842011-09-01 12:19:43 -07001385 /* Notify the netvsc driver of the new device */
Andrew Schwartzmeyer8ebdcc52015-08-11 17:14:31 -07001386 memset(&device_info, 0, sizeof(device_info));
Haiyang Zhang692e0842011-09-01 12:19:43 -07001387 device_info.ring_size = ring_size;
KY Srinivasane01ec212015-05-27 13:16:57 -07001388 device_info.max_num_vrss_chns = max_num_vrss_chns;
Haiyang Zhang692e0842011-09-01 12:19:43 -07001389 ret = rndis_filter_device_add(dev, &device_info);
1390 if (ret != 0) {
1391 netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001392 netvsc_free_netdev(net);
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -07001393 hv_set_drvdata(dev, NULL);
Haiyang Zhang692e0842011-09-01 12:19:43 -07001394 return ret;
1395 }
1396 memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
1397
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +02001398 nvdev = net_device_ctx->nvdev;
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001399 netif_set_real_num_tx_queues(net, nvdev->num_chn);
1400 netif_set_real_num_rx_queues(net, nvdev->num_chn);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001401
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -08001402 netvsc_init_settings(net);
1403
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 Zhang4d447c92011-12-15 13:45:17 -08001427
Haiyang Zhang122a5f62011-05-27 06:21:55 -07001428 ndev_ctx = netdev_priv(net);
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +02001429 net_device = ndev_ctx->nvdev;
1430
Vitaly Kuznetsovf580aec2016-05-13 13:55:20 +02001431 ndev_ctx->start_remove = true;
1432
Haiyang Zhang122a5f62011-05-27 06:21:55 -07001433 cancel_delayed_work_sync(&ndev_ctx->dwork);
Wenqi Ma792df872012-04-19 00:39:37 +00001434 cancel_work_sync(&ndev_ctx->work);
Haiyang Zhang122a5f62011-05-27 06:21:55 -07001435
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001436 /* Stop outbound asap */
Haiyang Zhang0a282532012-02-02 07:17:59 +00001437 netif_tx_disable(net);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001438
1439 unregister_netdev(net);
1440
1441 /*
1442 * Call to the vsc driver to let it know that the device is being
1443 * removed
1444 */
Haiyang Zhangdf06bcf2011-05-23 09:03:47 -07001445 rndis_filter_device_remove(dev);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001446
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +02001447 hv_set_drvdata(dev, NULL);
1448
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001449 netvsc_free_netdev(net);
Haiyang Zhangdf06bcf2011-05-23 09:03:47 -07001450 return 0;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001451}
1452
K. Y. Srinivasan345c4cc2011-08-25 09:48:34 -07001453static const struct hv_vmbus_device_id id_table[] = {
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07001454 /* Network guid */
K. Y. Srinivasan8f505942013-01-23 17:42:42 -08001455 { HV_NIC_GUID, },
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07001456 { },
K. Y. Srinivasan345c4cc2011-08-25 09:48:34 -07001457};
1458
1459MODULE_DEVICE_TABLE(vmbus, id_table);
1460
K. Y. Srinivasanf1542a62011-05-10 07:55:16 -07001461/* The one and only one */
K. Y. Srinivasanfde0ef92011-05-12 19:35:08 -07001462static struct hv_driver netvsc_drv = {
Haiyang Zhangd31b20f2012-03-07 10:02:00 +00001463 .name = KBUILD_MODNAME,
K. Y. Srinivasan345c4cc2011-08-25 09:48:34 -07001464 .id_table = id_table,
K. Y. Srinivasanfde0ef92011-05-12 19:35:08 -07001465 .probe = netvsc_probe,
1466 .remove = netvsc_remove,
K. Y. Srinivasand4890972011-05-10 07:55:17 -07001467};
K. Y. Srinivasanf1542a62011-05-10 07:55:16 -07001468
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001469
1470/*
1471 * On Hyper-V, every VF interface is matched with a corresponding
1472 * synthetic interface. The synthetic interface is presented first
1473 * to the guest. When the corresponding VF instance is registered,
1474 * we will take care of switching the data path.
1475 */
1476static int netvsc_netdev_event(struct notifier_block *this,
1477 unsigned long event, void *ptr)
1478{
1479 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
1480
1481 switch (event) {
1482 case NETDEV_REGISTER:
1483 return netvsc_register_vf(event_dev);
1484 case NETDEV_UNREGISTER:
1485 return netvsc_unregister_vf(event_dev);
1486 case NETDEV_UP:
1487 return netvsc_vf_up(event_dev);
1488 case NETDEV_DOWN:
1489 return netvsc_vf_down(event_dev);
1490 default:
1491 return NOTIFY_DONE;
1492 }
1493}
1494
1495static struct notifier_block netvsc_netdev_notifier = {
1496 .notifier_call = netvsc_netdev_event,
1497};
1498
K. Y. Srinivasana9869c92011-05-12 19:35:17 -07001499static void __exit netvsc_drv_exit(void)
Hank Janssenfceaf242009-07-13 15:34:54 -07001500{
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001501 unregister_netdevice_notifier(&netvsc_netdev_notifier);
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001502 vmbus_driver_unregister(&netvsc_drv);
Hank Janssenfceaf242009-07-13 15:34:54 -07001503}
1504
K. Y. Srinivasan1fde28c2011-05-12 19:35:16 -07001505static int __init netvsc_drv_init(void)
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001506{
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001507 int ret;
1508
Haiyang Zhangfa85a6c2012-07-25 08:08:41 +00001509 if (ring_size < RING_SIZE_MIN) {
1510 ring_size = RING_SIZE_MIN;
1511 pr_info("Increased ring_size to %d (min allowed)\n",
1512 ring_size);
1513 }
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001514 ret = vmbus_driver_register(&netvsc_drv);
1515
1516 if (ret)
1517 return ret;
1518
1519 register_netdevice_notifier(&netvsc_netdev_notifier);
1520 return 0;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001521}
1522
Hank Janssen26c14cc2010-02-11 23:02:42 +00001523MODULE_LICENSE("GPL");
Stephen Hemminger7880fc54c2010-05-04 09:58:52 -07001524MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
Hank Janssenfceaf242009-07-13 15:34:54 -07001525
K. Y. Srinivasan1fde28c2011-05-12 19:35:16 -07001526module_init(netvsc_drv_init);
K. Y. Srinivasana9869c92011-05-12 19:35:17 -07001527module_exit(netvsc_drv_exit);