blob: 1238600d717e34ebe99ae47d88aa929fb4313319 [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>
Michael Kelley8eb1b3c2017-05-30 11:36:56 -070040#include <net/checksum.h>
41#include <net/ip6_checksum.h>
K. Y. Srinivasan3f335ea2011-05-12 19:34:15 -070042
K. Y. Srinivasan5ca72522011-05-12 19:34:37 -070043#include "hyperv_net.h"
Hank Janssenfceaf242009-07-13 15:34:54 -070044
Haiyang Zhangfa85a6c2012-07-25 08:08:41 +000045#define RING_SIZE_MIN 64
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +010046#define LINKCHANGE_INT (2 * HZ)
stephen hemmingera50af862016-12-06 13:43:54 -080047
Hank Janssen99c8da02010-10-12 10:45:23 -070048static int ring_size = 128;
Stephen Hemminger450d7a42010-05-04 09:58:53 -070049module_param(ring_size, int, S_IRUGO);
50MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)");
Hank Janssenfceaf242009-07-13 15:34:54 -070051
Simon Xiao3f300ff2015-04-28 01:05:17 -070052static const u32 default_msg = NETIF_MSG_DRV | NETIF_MSG_PROBE |
53 NETIF_MSG_LINK | NETIF_MSG_IFUP |
54 NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR |
55 NETIF_MSG_TX_ERR;
56
57static int debug = -1;
58module_param(debug, int, S_IRUGO);
59MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
60
Greg Kroah-Hartman4e9bfef2009-07-15 12:45:20 -070061static void netvsc_set_multicast_list(struct net_device *net)
Hank Janssenfceaf242009-07-13 15:34:54 -070062{
Wenqi Ma792df872012-04-19 00:39:37 +000063 struct net_device_context *net_device_ctx = netdev_priv(net);
stephen hemminger4f19c0d2017-06-07 15:53:49 -070064 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
Haiyang Zhangd426b2e2011-11-30 07:19:08 -080065
stephen hemminger4f19c0d2017-06-07 15:53:49 -070066 rndis_filter_update(nvdev);
Hank Janssenfceaf242009-07-13 15:34:54 -070067}
68
Hank Janssenfceaf242009-07-13 15:34:54 -070069static int netvsc_open(struct net_device *net)
70{
Haiyang Zhang53fa1a62017-06-21 16:40:47 -070071 struct net_device_context *ndev_ctx = netdev_priv(net);
stephen hemminger79e8cbe2017-07-19 11:53:13 -070072 struct netvsc_device *nvdev = rtnl_dereference(ndev_ctx->nvdev);
Haiyang Zhang891de742014-02-12 16:54:27 -080073 struct rndis_device *rdev;
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -070074 int ret = 0;
Hank Janssenfceaf242009-07-13 15:34:54 -070075
Haiyang Zhang891de742014-02-12 16:54:27 -080076 netif_carrier_off(net);
77
Haiyang Zhangd515d0f2011-09-28 13:24:15 -070078 /* Open up the device */
Vitaly Kuznetsov2f5fa6c2016-06-03 17:51:00 +020079 ret = rndis_filter_open(nvdev);
Haiyang Zhangd515d0f2011-09-28 13:24:15 -070080 if (ret != 0) {
81 netdev_err(net, "unable to open device (ret %d).\n", ret);
82 return ret;
Hank Janssenfceaf242009-07-13 15:34:54 -070083 }
84
Haiyang Zhang2de85302015-07-13 13:09:16 -070085 netif_tx_wake_all_queues(net);
Haiyang Zhangd515d0f2011-09-28 13:24:15 -070086
Haiyang Zhang891de742014-02-12 16:54:27 -080087 rdev = nvdev->extension;
Haiyang Zhang53fa1a62017-06-21 16:40:47 -070088 if (!rdev->link_state && !ndev_ctx->datapath)
Haiyang Zhang891de742014-02-12 16:54:27 -080089 netif_carrier_on(net);
90
Hank Janssenfceaf242009-07-13 15:34:54 -070091 return ret;
92}
93
Hank Janssenfceaf242009-07-13 15:34:54 -070094static int netvsc_close(struct net_device *net)
95{
Hank Janssenfceaf242009-07-13 15:34:54 -070096 struct net_device_context *net_device_ctx = netdev_priv(net);
stephen hemminger545a8e72017-03-22 14:51:00 -070097 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -070098 int ret;
stephen hemminger40975962017-06-08 16:21:19 -070099 u32 aread, i, msec = 10, retry = 0, retry_max = 20;
Haiyang Zhang2de85302015-07-13 13:09:16 -0700100 struct vmbus_channel *chn;
Hank Janssenfceaf242009-07-13 15:34:54 -0700101
Haiyang Zhang0a282532012-02-02 07:17:59 +0000102 netif_tx_disable(net);
Hank Janssenfceaf242009-07-13 15:34:54 -0700103
Vitaly Kuznetsov2f5fa6c2016-06-03 17:51:00 +0200104 ret = rndis_filter_close(nvdev);
Haiyang Zhang2de85302015-07-13 13:09:16 -0700105 if (ret != 0) {
Hank Jansseneb335bc2011-03-29 13:58:48 -0700106 netdev_err(net, "unable to close device (ret %d).\n", ret);
Haiyang Zhang2de85302015-07-13 13:09:16 -0700107 return ret;
108 }
109
110 /* Ensure pending bytes in ring are read */
111 while (true) {
112 aread = 0;
113 for (i = 0; i < nvdev->num_chn; i++) {
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800114 chn = nvdev->chan_table[i].channel;
Haiyang Zhang2de85302015-07-13 13:09:16 -0700115 if (!chn)
116 continue;
117
stephen hemminger40975962017-06-08 16:21:19 -0700118 aread = hv_get_bytes_to_read(&chn->inbound);
Haiyang Zhang2de85302015-07-13 13:09:16 -0700119 if (aread)
120 break;
121
stephen hemminger40975962017-06-08 16:21:19 -0700122 aread = hv_get_bytes_to_read(&chn->outbound);
Haiyang Zhang2de85302015-07-13 13:09:16 -0700123 if (aread)
124 break;
125 }
126
127 retry++;
128 if (retry > retry_max || aread == 0)
129 break;
130
131 msleep(msec);
132
133 if (msec < 1000)
134 msec *= 2;
135 }
136
137 if (aread) {
138 netdev_err(net, "Ring buffer not empty after closing rndis\n");
139 ret = -ETIMEDOUT;
140 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700141
Hank Janssenfceaf242009-07-13 15:34:54 -0700142 return ret;
143}
144
KY Srinivasan8a002512014-03-08 19:23:14 -0800145static void *init_ppi_data(struct rndis_message *msg, u32 ppi_size,
146 int pkt_type)
147{
148 struct rndis_packet *rndis_pkt;
149 struct rndis_per_packet_info *ppi;
150
151 rndis_pkt = &msg->msg.pkt;
152 rndis_pkt->data_offset += ppi_size;
153
154 ppi = (struct rndis_per_packet_info *)((void *)rndis_pkt +
155 rndis_pkt->per_pkt_info_offset + rndis_pkt->per_pkt_info_len);
156
157 ppi->size = ppi_size;
158 ppi->type = pkt_type;
159 ppi->ppi_offset = sizeof(struct rndis_per_packet_info);
160
161 rndis_pkt->per_pkt_info_len += ppi_size;
162
163 return ppi;
164}
165
Haiyang Zhangf72860a2017-04-12 11:45:18 -0700166/* Azure hosts don't support non-TCP port numbers in hashing yet. We compute
167 * hash for non-TCP traffic with only IP numbers.
168 */
169static inline u32 netvsc_get_hash(struct sk_buff *skb, struct sock *sk)
170{
171 struct flow_keys flow;
172 u32 hash;
173 static u32 hashrnd __read_mostly;
174
175 net_get_random_once(&hashrnd, sizeof(hashrnd));
176
177 if (!skb_flow_dissect_flow_keys(skb, &flow, 0))
178 return 0;
179
180 if (flow.basic.ip_proto == IPPROTO_TCP) {
181 return skb_get_hash(skb);
182 } else {
183 if (flow.basic.n_proto == htons(ETH_P_IP))
184 hash = jhash2((u32 *)&flow.addrs.v4addrs, 2, hashrnd);
185 else if (flow.basic.n_proto == htons(ETH_P_IPV6))
186 hash = jhash2((u32 *)&flow.addrs.v6addrs, 8, hashrnd);
187 else
188 hash = 0;
189
190 skb_set_hash(skb, hash, PKT_HASH_TYPE_L3);
191 }
192
193 return hash;
194}
195
Haiyang Zhang8db91f62017-04-12 11:35:05 -0700196static inline int netvsc_get_tx_queue(struct net_device *ndev,
197 struct sk_buff *skb, int old_idx)
198{
199 const struct net_device_context *ndc = netdev_priv(ndev);
200 struct sock *sk = skb->sk;
201 int q_idx;
202
Haiyang Zhangf72860a2017-04-12 11:45:18 -0700203 q_idx = ndc->tx_send_table[netvsc_get_hash(skb, sk) &
Haiyang Zhang8db91f62017-04-12 11:35:05 -0700204 (VRSS_SEND_TAB_SIZE - 1)];
205
206 /* If queue index changed record the new value */
207 if (q_idx != old_idx &&
208 sk && sk_fullsock(sk) && rcu_access_pointer(sk->sk_dst_cache))
209 sk_tx_queue_set(sk, q_idx);
210
211 return q_idx;
212}
213
stephen hemmingerd8e18ee2017-01-24 13:06:05 -0800214/*
215 * Select queue for transmit.
216 *
217 * If a valid queue has already been assigned, then use that.
218 * Otherwise compute tx queue based on hash and the send table.
219 *
220 * This is basically similar to default (__netdev_pick_tx) with the added step
221 * of using the host send_table when no other queue has been assigned.
222 *
223 * TODO support XPS - but get_xps_queue not exported
224 */
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700225static u16 netvsc_select_queue(struct net_device *ndev, struct sk_buff *skb,
226 void *accel_priv, select_queue_fallback_t fallback)
227{
stephen hemminger7ce10122017-03-09 14:58:29 -0800228 unsigned int num_tx_queues = ndev->real_num_tx_queues;
Haiyang Zhang8db91f62017-04-12 11:35:05 -0700229 int q_idx = sk_tx_queue_get(skb->sk);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700230
Haiyang Zhang8db91f62017-04-12 11:35:05 -0700231 if (q_idx < 0 || skb->ooo_okay) {
232 /* If forwarding a packet, we use the recorded queue when
233 * available for better cache locality.
234 */
235 if (skb_rx_queue_recorded(skb))
236 q_idx = skb_get_rx_queue(skb);
237 else
238 q_idx = netvsc_get_tx_queue(ndev, skb, q_idx);
stephen hemmingerd8e18ee2017-01-24 13:06:05 -0800239 }
240
Haiyang Zhang8db91f62017-04-12 11:35:05 -0700241 while (unlikely(q_idx >= num_tx_queues))
242 q_idx -= num_tx_queues;
243
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700244 return q_idx;
245}
246
KY Srinivasan54a73572014-03-08 19:23:13 -0800247static u32 fill_pg_buf(struct page *page, u32 offset, u32 len,
248 struct hv_page_buffer *pb)
249{
250 int j = 0;
251
252 /* Deal with compund pages by ignoring unused part
253 * of the page.
254 */
255 page += (offset >> PAGE_SHIFT);
256 offset &= ~PAGE_MASK;
257
258 while (len > 0) {
259 unsigned long bytes;
260
261 bytes = PAGE_SIZE - offset;
262 if (bytes > len)
263 bytes = len;
264 pb[j].pfn = page_to_pfn(page);
265 pb[j].offset = offset;
266 pb[j].len = bytes;
267
268 offset += bytes;
269 len -= bytes;
270
271 if (offset == PAGE_SIZE && len) {
272 page++;
273 offset = 0;
274 j++;
275 }
276 }
277
278 return j + 1;
279}
280
KY Srinivasan8a002512014-03-08 19:23:14 -0800281static u32 init_page_array(void *hdr, u32 len, struct sk_buff *skb,
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800282 struct hv_netvsc_packet *packet,
283 struct hv_page_buffer **page_buf)
KY Srinivasan54a73572014-03-08 19:23:13 -0800284{
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800285 struct hv_page_buffer *pb = *page_buf;
KY Srinivasan54a73572014-03-08 19:23:13 -0800286 u32 slots_used = 0;
287 char *data = skb->data;
288 int frags = skb_shinfo(skb)->nr_frags;
289 int i;
290
291 /* The packet is laid out thus:
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700292 * 1. hdr: RNDIS header and PPI
KY Srinivasan54a73572014-03-08 19:23:13 -0800293 * 2. skb linear data
294 * 3. skb fragment data
295 */
296 if (hdr != NULL)
297 slots_used += fill_pg_buf(virt_to_page(hdr),
298 offset_in_page(hdr),
299 len, &pb[slots_used]);
300
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700301 packet->rmsg_size = len;
302 packet->rmsg_pgcnt = slots_used;
303
KY Srinivasan54a73572014-03-08 19:23:13 -0800304 slots_used += fill_pg_buf(virt_to_page(data),
305 offset_in_page(data),
306 skb_headlen(skb), &pb[slots_used]);
307
308 for (i = 0; i < frags; i++) {
309 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
310
311 slots_used += fill_pg_buf(skb_frag_page(frag),
312 frag->page_offset,
313 skb_frag_size(frag), &pb[slots_used]);
314 }
KY Srinivasan8a002512014-03-08 19:23:14 -0800315 return slots_used;
KY Srinivasan54a73572014-03-08 19:23:13 -0800316}
317
stephen hemminger60b86662017-06-08 16:21:18 -0700318/* Estimate number of page buffers neede to transmit
319 * Need at most 2 for RNDIS header plus skb body and fragments.
320 */
321static unsigned int netvsc_get_slots(const struct sk_buff *skb)
KY Srinivasan54a73572014-03-08 19:23:13 -0800322{
stephen hemminger60b86662017-06-08 16:21:18 -0700323 return PFN_UP(offset_in_page(skb->data) + skb_headlen(skb))
324 + skb_shinfo(skb)->nr_frags
325 + 2;
KY Srinivasan54a73572014-03-08 19:23:13 -0800326}
327
stephen hemminger23312a32017-01-24 13:05:59 -0800328static u32 net_checksum_info(struct sk_buff *skb)
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800329{
stephen hemminger23312a32017-01-24 13:05:59 -0800330 if (skb->protocol == htons(ETH_P_IP)) {
331 struct iphdr *ip = ip_hdr(skb);
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800332
stephen hemminger23312a32017-01-24 13:05:59 -0800333 if (ip->protocol == IPPROTO_TCP)
334 return TRANSPORT_INFO_IPV4_TCP;
335 else if (ip->protocol == IPPROTO_UDP)
336 return TRANSPORT_INFO_IPV4_UDP;
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800337 } else {
stephen hemminger23312a32017-01-24 13:05:59 -0800338 struct ipv6hdr *ip6 = ipv6_hdr(skb);
339
340 if (ip6->nexthdr == IPPROTO_TCP)
341 return TRANSPORT_INFO_IPV6_TCP;
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800342 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
stephen hemminger23312a32017-01-24 13:05:59 -0800343 return TRANSPORT_INFO_IPV6_UDP;
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800344 }
345
stephen hemminger23312a32017-01-24 13:05:59 -0800346 return TRANSPORT_INFO_NOT_IP;
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800347}
348
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700349static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
Hank Janssenfceaf242009-07-13 15:34:54 -0700350{
Hank Janssenfceaf242009-07-13 15:34:54 -0700351 struct net_device_context *net_device_ctx = netdev_priv(net);
Vitaly Kuznetsov981a1bd2015-04-08 17:54:05 +0200352 struct hv_netvsc_packet *packet = NULL;
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700353 int ret;
KY Srinivasan8a002512014-03-08 19:23:14 -0800354 unsigned int num_data_pgs;
355 struct rndis_message *rndis_msg;
356 struct rndis_packet *rndis_pkt;
357 u32 rndis_msg_size;
KY Srinivasan8a002512014-03-08 19:23:14 -0800358 struct rndis_per_packet_info *ppi;
Haiyang Zhang307f0992014-05-21 12:55:39 -0700359 u32 hash;
KY Srinivasanb08cc792015-03-29 21:08:42 -0700360 struct hv_page_buffer page_buf[MAX_PAGE_BUFFER_COUNT];
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800361 struct hv_page_buffer *pb = page_buf;
Hank Janssenfceaf242009-07-13 15:34:54 -0700362
stephen hemminger60b86662017-06-08 16:21:18 -0700363 /* We can only transmit MAX_PAGE_BUFFER_COUNT number
Vitaly Kuznetsove88f7e02015-04-08 17:54:06 +0200364 * of pages in a single packet. If skb is scattered around
365 * more pages we try linearizing it.
KY Srinivasan54a73572014-03-08 19:23:13 -0800366 */
stephen hemminger60b86662017-06-08 16:21:18 -0700367 num_data_pgs = netvsc_get_slots(skb);
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700368 if (unlikely(num_data_pgs > MAX_PAGE_BUFFER_COUNT)) {
Stephen Hemminger4323b472016-08-23 12:17:57 -0700369 ++net_device_ctx->eth_stats.tx_scattered;
370
371 if (skb_linearize(skb))
372 goto no_memory;
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700373
stephen hemminger60b86662017-06-08 16:21:18 -0700374 num_data_pgs = netvsc_get_slots(skb);
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700375 if (num_data_pgs > MAX_PAGE_BUFFER_COUNT) {
Stephen Hemminger4323b472016-08-23 12:17:57 -0700376 ++net_device_ctx->eth_stats.tx_too_big;
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700377 goto drop;
378 }
KY Srinivasan54a73572014-03-08 19:23:13 -0800379 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700380
KY Srinivasanc0eb4542015-12-01 16:43:10 -0800381 /*
382 * Place the rndis header in the skb head room and
383 * the skb->cb will be used for hv_netvsc_packet
384 * structure.
385 */
386 ret = skb_cow_head(skb, RNDIS_AND_PPI_SIZE);
Stephen Hemminger4323b472016-08-23 12:17:57 -0700387 if (ret)
388 goto no_memory;
389
KY Srinivasanc0eb4542015-12-01 16:43:10 -0800390 /* Use the skb control buffer for building up the packet */
391 BUILD_BUG_ON(sizeof(struct hv_netvsc_packet) >
392 FIELD_SIZEOF(struct sk_buff, cb));
393 packet = (struct hv_netvsc_packet *)skb->cb;
Hank Janssenfceaf242009-07-13 15:34:54 -0700394
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700395 packet->q_idx = skb_get_queue_mapping(skb);
396
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800397 packet->total_data_buflen = skb->len;
stephen hemminger793e3952017-01-24 13:06:12 -0800398 packet->total_bytes = skb->len;
399 packet->total_packets = 1;
Hank Janssenfceaf242009-07-13 15:34:54 -0700400
KY Srinivasanc0eb4542015-12-01 16:43:10 -0800401 rndis_msg = (struct rndis_message *)skb->head;
KY Srinivasanb08cc792015-03-29 21:08:42 -0700402
KY Srinivasan24476762015-12-01 16:43:06 -0800403 memset(rndis_msg, 0, RNDIS_AND_PPI_SIZE);
Hank Janssenfceaf242009-07-13 15:34:54 -0700404
KY Srinivasan8a002512014-03-08 19:23:14 -0800405 /* Add the rndis header */
KY Srinivasan8a002512014-03-08 19:23:14 -0800406 rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET;
407 rndis_msg->msg_len = packet->total_data_buflen;
408 rndis_pkt = &rndis_msg->msg.pkt;
409 rndis_pkt->data_offset = sizeof(struct rndis_packet);
410 rndis_pkt->data_len = packet->total_data_buflen;
411 rndis_pkt->per_pkt_info_offset = sizeof(struct rndis_packet);
412
413 rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet);
414
Haiyang Zhang307f0992014-05-21 12:55:39 -0700415 hash = skb_get_hash_raw(skb);
416 if (hash != 0 && net->real_num_tx_queues > 1) {
417 rndis_msg_size += NDIS_HASH_PPI_SIZE;
418 ppi = init_ppi_data(rndis_msg, NDIS_HASH_PPI_SIZE,
419 NBL_HASH_VALUE);
420 *(u32 *)((void *)ppi + ppi->ppi_offset) = hash;
421 }
422
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700423 if (skb_vlan_tag_present(skb)) {
KY Srinivasan8a002512014-03-08 19:23:14 -0800424 struct ndis_pkt_8021q_info *vlan;
425
426 rndis_msg_size += NDIS_VLAN_PPI_SIZE;
427 ppi = init_ppi_data(rndis_msg, NDIS_VLAN_PPI_SIZE,
428 IEEE_8021Q_INFO);
429 vlan = (struct ndis_pkt_8021q_info *)((void *)ppi +
430 ppi->ppi_offset);
KY Srinivasan760d1e32015-12-01 16:43:19 -0800431 vlan->vlanid = skb->vlan_tci & VLAN_VID_MASK;
432 vlan->pri = (skb->vlan_tci & VLAN_PRIO_MASK) >>
KY Srinivasan8a002512014-03-08 19:23:14 -0800433 VLAN_PRIO_SHIFT;
434 }
435
stephen hemminger23312a32017-01-24 13:05:59 -0800436 if (skb_is_gso(skb)) {
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700437 struct ndis_tcp_lso_info *lso_info;
438
439 rndis_msg_size += NDIS_LSO_PPI_SIZE;
440 ppi = init_ppi_data(rndis_msg, NDIS_LSO_PPI_SIZE,
441 TCP_LARGESEND_PKTINFO);
442
443 lso_info = (struct ndis_tcp_lso_info *)((void *)ppi +
444 ppi->ppi_offset);
445
446 lso_info->lso_v2_transmit.type = NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
stephen hemminger23312a32017-01-24 13:05:59 -0800447 if (skb->protocol == htons(ETH_P_IP)) {
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700448 lso_info->lso_v2_transmit.ip_version =
449 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
450 ip_hdr(skb)->tot_len = 0;
451 ip_hdr(skb)->check = 0;
452 tcp_hdr(skb)->check =
453 ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
454 ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
455 } else {
456 lso_info->lso_v2_transmit.ip_version =
457 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
458 ipv6_hdr(skb)->payload_len = 0;
459 tcp_hdr(skb)->check =
460 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
461 &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
462 }
stephen hemminger23312a32017-01-24 13:05:59 -0800463 lso_info->lso_v2_transmit.tcp_header_offset = skb_transport_offset(skb);
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700464 lso_info->lso_v2_transmit.mss = skb_shinfo(skb)->gso_size;
stephen hemmingerad19bc82016-10-11 14:03:07 -0700465 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
stephen hemminger23312a32017-01-24 13:05:59 -0800466 if (net_checksum_info(skb) & net_device_ctx->tx_checksum_mask) {
467 struct ndis_tcp_ip_checksum_info *csum_info;
468
stephen hemmingerad19bc82016-10-11 14:03:07 -0700469 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
stephen hemminger23312a32017-01-24 13:05:59 -0800476 csum_info->transmit.tcp_header_offset = skb_transport_offset(skb);
477
478 if (skb->protocol == htons(ETH_P_IP)) {
stephen hemmingerad19bc82016-10-11 14:03:07 -0700479 csum_info->transmit.is_ipv4 = 1;
stephen hemminger23312a32017-01-24 13:05:59 -0800480
481 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
482 csum_info->transmit.tcp_checksum = 1;
483 else
484 csum_info->transmit.udp_checksum = 1;
485 } else {
stephen hemmingerad19bc82016-10-11 14:03:07 -0700486 csum_info->transmit.is_ipv6 = 1;
487
stephen hemminger23312a32017-01-24 13:05:59 -0800488 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
489 csum_info->transmit.tcp_checksum = 1;
490 else
491 csum_info->transmit.udp_checksum = 1;
492 }
stephen hemmingerad19bc82016-10-11 14:03:07 -0700493 } else {
stephen hemminger23312a32017-01-24 13:05:59 -0800494 /* Can't do offload of this type of checksum */
stephen hemmingerad19bc82016-10-11 14:03:07 -0700495 if (skb_checksum_help(skb))
496 goto drop;
497 }
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700498 }
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800499
KY Srinivasan8a002512014-03-08 19:23:14 -0800500 /* Start filling in the page buffers with the rndis hdr */
501 rndis_msg->msg_len += rndis_msg_size;
Haiyang Zhang942396b2014-10-22 13:47:18 -0700502 packet->total_data_buflen = rndis_msg->msg_len;
KY Srinivasan8a002512014-03-08 19:23:14 -0800503 packet->page_buf_cnt = init_page_array(rndis_msg, rndis_msg_size,
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800504 skb, packet, &pb);
KY Srinivasan8a002512014-03-08 19:23:14 -0800505
sixiao@microsoft.com76d13b52016-02-17 16:43:59 -0800506 /* timestamp packet in software */
507 skb_tx_timestamp(skb);
stephen hemminger2a926f72017-07-19 11:53:17 -0700508
509 ret = netvsc_send(net_device_ctx, packet, rndis_msg, &pb, skb);
stephen hemminger793e3952017-01-24 13:06:12 -0800510 if (likely(ret == 0))
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700511 return NETDEV_TX_OK;
Stephen Hemminger4323b472016-08-23 12:17:57 -0700512
513 if (ret == -EAGAIN) {
514 ++net_device_ctx->eth_stats.tx_busy;
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700515 return NETDEV_TX_BUSY;
Stephen Hemminger4323b472016-08-23 12:17:57 -0700516 }
517
518 if (ret == -ENOSPC)
519 ++net_device_ctx->eth_stats.tx_no_space;
Hank Janssenfceaf242009-07-13 15:34:54 -0700520
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700521drop:
522 dev_kfree_skb_any(skb);
523 net->stats.tx_dropped++;
524
525 return NETDEV_TX_OK;
Stephen Hemminger4323b472016-08-23 12:17:57 -0700526
527no_memory:
528 ++net_device_ctx->eth_stats.tx_no_memory;
529 goto drop;
Hank Janssenfceaf242009-07-13 15:34:54 -0700530}
Hank Janssen3e189512010-03-04 22:11:00 +0000531/*
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700532 * netvsc_linkstatus_callback - Link up/down notification
533 */
K. Y. Srinivasan90ef1172011-05-12 19:34:50 -0700534void netvsc_linkstatus_callback(struct hv_device *device_obj,
Haiyang Zhang3a494e72014-06-19 18:34:36 -0700535 struct rndis_message *resp)
Hank Janssenfceaf242009-07-13 15:34:54 -0700536{
Haiyang Zhang3a494e72014-06-19 18:34:36 -0700537 struct rndis_indicate_status *indicate = &resp->msg.indicate_status;
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -0700538 struct net_device *net;
Haiyang Zhangc996edc2011-04-06 15:18:00 -0700539 struct net_device_context *ndev_ctx;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +0100540 struct netvsc_reconfig *event;
541 unsigned long flags;
542
Haiyang Zhang7f5d5af2016-08-04 10:42:15 -0700543 net = hv_get_drvdata(device_obj);
544
545 if (!net)
546 return;
547
548 ndev_ctx = netdev_priv(net);
549
550 /* Update the physical link speed when changing to another vSwitch */
551 if (indicate->status == RNDIS_STATUS_LINK_SPEED_CHANGE) {
552 u32 speed;
553
554 speed = *(u32 *)((void *)indicate + indicate->
555 status_buf_offset) / 10000;
556 ndev_ctx->speed = speed;
557 return;
558 }
559
560 /* Handle these link change statuses below */
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +0100561 if (indicate->status != RNDIS_STATUS_NETWORK_CHANGE &&
562 indicate->status != RNDIS_STATUS_MEDIA_CONNECT &&
563 indicate->status != RNDIS_STATUS_MEDIA_DISCONNECT)
564 return;
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -0700565
Haiyang Zhang7f5d5af2016-08-04 10:42:15 -0700566 if (net->reg_state != NETREG_REGISTERED)
Hank Janssenfceaf242009-07-13 15:34:54 -0700567 return;
Hank Janssenfceaf242009-07-13 15:34:54 -0700568
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +0100569 event = kzalloc(sizeof(*event), GFP_ATOMIC);
570 if (!event)
571 return;
572 event->event = indicate->status;
573
574 spin_lock_irqsave(&ndev_ctx->lock, flags);
575 list_add_tail(&event->list, &ndev_ctx->reconfig_events);
576 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
577
578 schedule_delayed_work(&ndev_ctx->dwork, 0);
Hank Janssenfceaf242009-07-13 15:34:54 -0700579}
580
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700581static struct sk_buff *netvsc_alloc_recv_skb(struct net_device *net,
stephen hemmingere91e7dd2017-02-27 10:26:51 -0800582 struct napi_struct *napi,
stephen hemmingerdc54a082017-01-24 13:06:08 -0800583 const struct ndis_tcp_ip_checksum_info *csum_info,
584 const struct ndis_pkt_8021q_info *vlan,
585 void *data, u32 buflen)
Hank Janssenfceaf242009-07-13 15:34:54 -0700586{
Hank Janssenfceaf242009-07-13 15:34:54 -0700587 struct sk_buff *skb;
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -0700588
stephen hemmingere91e7dd2017-02-27 10:26:51 -0800589 skb = napi_alloc_skb(napi, buflen);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700590 if (!skb)
591 return skb;
Hank Janssenfceaf242009-07-13 15:34:54 -0700592
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700593 /*
594 * Copy to skb. This copy is needed here since the memory pointed by
595 * hv_netvsc_packet cannot be deallocated
596 */
Johannes Berg59ae1d12017-06-16 14:29:20 +0200597 skb_put_data(skb, data, buflen);
Hank Janssenfceaf242009-07-13 15:34:54 -0700598
599 skb->protocol = eth_type_trans(skb, net);
Stephen Hemmingere52fed72016-10-23 21:32:47 -0700600
601 /* skb is already created with CHECKSUM_NONE */
602 skb_checksum_none_assert(skb);
603
604 /*
605 * In Linux, the IP checksum is always checked.
606 * Do L4 checksum offload if enabled and present.
607 */
608 if (csum_info && (net->features & NETIF_F_RXCSUM)) {
609 if (csum_info->receive.tcp_checksum_succeeded ||
610 csum_info->receive.udp_checksum_succeeded)
KY Srinivasane3d605e2014-03-08 19:23:16 -0800611 skb->ip_summed = CHECKSUM_UNNECESSARY;
KY Srinivasane3d605e2014-03-08 19:23:16 -0800612 }
613
stephen hemmingerdc54a082017-01-24 13:06:08 -0800614 if (vlan) {
615 u16 vlan_tci = vlan->vlanid | (vlan->pri << VLAN_PRIO_SHIFT);
616
Haiyang Zhang93725cb2013-06-17 15:36:49 -0700617 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
KY Srinivasan760d1e32015-12-01 16:43:19 -0800618 vlan_tci);
stephen hemmingerdc54a082017-01-24 13:06:08 -0800619 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700620
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700621 return skb;
622}
623
624/*
625 * netvsc_recv_callback - Callback when we receive a packet from the
626 * "wire" on the specified device.
627 */
stephen hemmingerdc54a082017-01-24 13:06:08 -0800628int netvsc_recv_callback(struct net_device *net,
629 struct vmbus_channel *channel,
630 void *data, u32 len,
631 const struct ndis_tcp_ip_checksum_info *csum_info,
632 const struct ndis_pkt_8021q_info *vlan)
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700633{
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200634 struct net_device_context *net_device_ctx = netdev_priv(net);
stephen hemminger545a8e72017-03-22 14:51:00 -0700635 struct netvsc_device *net_device;
stephen hemminger742fe542017-02-27 10:26:50 -0800636 u16 q_idx = channel->offermsg.offer.sub_channel_index;
stephen hemminger545a8e72017-03-22 14:51:00 -0700637 struct netvsc_channel *nvchan;
Stephen Hemmingerf207c102016-09-22 16:56:33 -0700638 struct net_device *vf_netdev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700639 struct sk_buff *skb;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700640 struct netvsc_stats *rx_stats;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700641
Stephen Hemminger9cbcc422016-09-22 16:56:34 -0700642 if (net->reg_state != NETREG_REGISTERED)
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700643 return NVSP_STAT_FAIL;
644
Stephen Hemminger9cbcc422016-09-22 16:56:34 -0700645 /*
646 * If necessary, inject this packet into the VF interface.
647 * On Hyper-V, multicast and brodcast packets are only delivered
648 * to the synthetic interface (after subjecting these to
649 * policy filters on the host). Deliver these via the VF
650 * interface in the guest.
651 */
stephen hemminger0719e722017-01-11 09:16:32 -0800652 rcu_read_lock();
stephen hemminger545a8e72017-03-22 14:51:00 -0700653 net_device = rcu_dereference(net_device_ctx->nvdev);
654 if (unlikely(!net_device))
655 goto drop;
656
657 nvchan = &net_device->chan_table[q_idx];
Stephen Hemmingerf207c102016-09-22 16:56:33 -0700658 vf_netdev = rcu_dereference(net_device_ctx->vf_netdev);
Stephen Hemminger9cbcc422016-09-22 16:56:34 -0700659 if (vf_netdev && (vf_netdev->flags & IFF_UP))
660 net = vf_netdev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700661
662 /* Allocate a skb - TODO direct I/O to pages? */
stephen hemmingere91e7dd2017-02-27 10:26:51 -0800663 skb = netvsc_alloc_recv_skb(net, &nvchan->napi,
664 csum_info, vlan, data, len);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700665 if (unlikely(!skb)) {
stephen hemminger545a8e72017-03-22 14:51:00 -0700666drop:
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700667 ++net->stats.rx_dropped;
stephen hemminger0719e722017-01-11 09:16:32 -0800668 rcu_read_unlock();
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700669 return NVSP_STAT_FAIL;
670 }
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700671
Stephen Hemminger9cbcc422016-09-22 16:56:34 -0700672 if (net != vf_netdev)
Simon Xiao6c80f3f2017-01-24 13:06:13 -0800673 skb_record_rx_queue(skb, q_idx);
Stephen Hemminger9cbcc422016-09-22 16:56:34 -0700674
675 /*
676 * Even if injecting the packet, record the statistics
677 * on the synthetic device because modifying the VF device
678 * statistics will not work correctly.
679 */
stephen hemminger742fe542017-02-27 10:26:50 -0800680 rx_stats = &nvchan->rx_stats;
sixiao@microsoft.com4b02b582015-05-15 02:33:03 -0700681 u64_stats_update_begin(&rx_stats->syncp);
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700682 rx_stats->packets++;
stephen hemmingerdc54a082017-01-24 13:06:08 -0800683 rx_stats->bytes += len;
Stephen Hemmingerf7ad75b2016-09-22 16:56:35 -0700684
685 if (skb->pkt_type == PACKET_BROADCAST)
686 ++rx_stats->broadcast;
687 else if (skb->pkt_type == PACKET_MULTICAST)
688 ++rx_stats->multicast;
sixiao@microsoft.com4b02b582015-05-15 02:33:03 -0700689 u64_stats_update_end(&rx_stats->syncp);
Stephen Hemminger9495c282010-03-09 17:42:17 -0800690
stephen hemminger742fe542017-02-27 10:26:50 -0800691 napi_gro_receive(&nvchan->napi, skb);
stephen hemminger0719e722017-01-11 09:16:32 -0800692 rcu_read_unlock();
Hank Janssenfceaf242009-07-13 15:34:54 -0700693
Hank Janssenfceaf242009-07-13 15:34:54 -0700694 return 0;
695}
696
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -0700697static void netvsc_get_drvinfo(struct net_device *net,
698 struct ethtool_drvinfo *info)
699{
Jiri Pirko7826d432013-01-06 00:44:26 +0000700 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
Jiri Pirko7826d432013-01-06 00:44:26 +0000701 strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -0700702}
703
Andrew Schwartzmeyer59995372015-02-26 16:27:14 -0800704static void netvsc_get_channels(struct net_device *net,
705 struct ethtool_channels *channel)
706{
707 struct net_device_context *net_device_ctx = netdev_priv(net);
stephen hemminger545a8e72017-03-22 14:51:00 -0700708 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
Andrew Schwartzmeyer59995372015-02-26 16:27:14 -0800709
710 if (nvdev) {
711 channel->max_combined = nvdev->max_chn;
712 channel->combined_count = nvdev->num_chn;
713 }
714}
715
stephen hemminger2b018882017-01-24 13:06:03 -0800716static int netvsc_set_queues(struct net_device *net, struct hv_device *dev,
717 u32 num_chn)
718{
719 struct netvsc_device_info device_info;
stephen hemminger9749fed2017-07-19 11:53:16 -0700720 struct netvsc_device *net_device;
stephen hemminger2b018882017-01-24 13:06:03 -0800721 int ret;
722
723 memset(&device_info, 0, sizeof(device_info));
724 device_info.num_chn = num_chn;
725 device_info.ring_size = ring_size;
726 device_info.max_num_vrss_chns = num_chn;
727
stephen hemminger2b018882017-01-24 13:06:03 -0800728 ret = netif_set_real_num_tx_queues(net, num_chn);
729 if (ret)
730 return ret;
731
732 ret = netif_set_real_num_rx_queues(net, num_chn);
stephen hemmingera5e1ec382017-07-19 11:53:14 -0700733 if (ret)
734 return ret;
stephen hemminger2b018882017-01-24 13:06:03 -0800735
stephen hemminger9749fed2017-07-19 11:53:16 -0700736 net_device = rndis_filter_device_add(dev, &device_info);
737 return IS_ERR(net_device) ? PTR_ERR(net_device) : 0;
stephen hemminger2b018882017-01-24 13:06:03 -0800738}
739
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700740static int netvsc_set_channels(struct net_device *net,
741 struct ethtool_channels *channels)
742{
743 struct net_device_context *net_device_ctx = netdev_priv(net);
744 struct hv_device *dev = net_device_ctx->device_ctx;
stephen hemminger545a8e72017-03-22 14:51:00 -0700745 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
stephen hemminger2b018882017-01-24 13:06:03 -0800746 unsigned int count = channels->combined_count;
stephen hemmingerea383bf2017-07-19 11:53:15 -0700747 bool was_opened;
stephen hemminger2b018882017-01-24 13:06:03 -0800748 int ret;
749
750 /* We do not support separate count for rx, tx, or other */
751 if (count == 0 ||
752 channels->rx_count || channels->tx_count || channels->other_count)
753 return -EINVAL;
754
Arnd Bergmannb92b7d32017-06-22 00:16:37 +0200755 if (count > net->num_tx_queues || count > VRSS_CHANNEL_MAX)
stephen hemminger2b018882017-01-24 13:06:03 -0800756 return -EINVAL;
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700757
stephen hemmingera0be4502017-03-22 14:51:01 -0700758 if (!nvdev || nvdev->destroy)
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700759 return -ENODEV;
760
stephen hemminger2b018882017-01-24 13:06:03 -0800761 if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5)
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700762 return -EINVAL;
763
stephen hemminger2b018882017-01-24 13:06:03 -0800764 if (count > nvdev->max_chn)
765 return -EINVAL;
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700766
stephen hemmingerea383bf2017-07-19 11:53:15 -0700767 was_opened = rndis_filter_opened(nvdev);
768 if (was_opened)
769 rndis_filter_close(nvdev);
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700770
stephen hemminger2289f0a2017-01-24 13:06:10 -0800771 rndis_filter_device_remove(dev, nvdev);
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700772
stephen hemminger2b018882017-01-24 13:06:03 -0800773 ret = netvsc_set_queues(net, dev, count);
774 if (ret == 0)
775 nvdev->num_chn = count;
776 else
777 netvsc_set_queues(net, dev, nvdev->num_chn);
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700778
stephen hemmingerea383bf2017-07-19 11:53:15 -0700779 nvdev = rtnl_dereference(net_device_ctx->nvdev);
780 if (was_opened)
781 rndis_filter_open(nvdev);
stephen hemminger163891d2017-03-22 14:50:58 -0700782
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +0200783 /* We may have missed link change notifications */
stephen hemminger1b019942017-07-19 11:53:12 -0700784 net_device_ctx->last_reconfig = 0;
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +0200785 schedule_delayed_work(&net_device_ctx->dwork, 0);
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700786
787 return ret;
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700788}
789
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100790static bool
791netvsc_validate_ethtool_ss_cmd(const struct ethtool_link_ksettings *cmd)
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800792{
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100793 struct ethtool_link_ksettings diff1 = *cmd;
794 struct ethtool_link_ksettings diff2 = {};
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800795
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100796 diff1.base.speed = 0;
797 diff1.base.duplex = 0;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800798 /* advertising and cmd are usually set */
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100799 ethtool_link_ksettings_zero_link_mode(&diff1, advertising);
800 diff1.base.cmd = 0;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800801 /* We set port to PORT_OTHER */
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100802 diff2.base.port = PORT_OTHER;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800803
804 return !memcmp(&diff1, &diff2, sizeof(diff1));
805}
806
807static void netvsc_init_settings(struct net_device *dev)
808{
809 struct net_device_context *ndc = netdev_priv(dev);
810
811 ndc->speed = SPEED_UNKNOWN;
Simon Xiaof3c9d40e2017-04-14 14:42:58 -0700812 ndc->duplex = DUPLEX_FULL;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800813}
814
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100815static int netvsc_get_link_ksettings(struct net_device *dev,
816 struct ethtool_link_ksettings *cmd)
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800817{
818 struct net_device_context *ndc = netdev_priv(dev);
819
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100820 cmd->base.speed = ndc->speed;
821 cmd->base.duplex = ndc->duplex;
822 cmd->base.port = PORT_OTHER;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800823
824 return 0;
825}
826
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100827static int netvsc_set_link_ksettings(struct net_device *dev,
828 const struct ethtool_link_ksettings *cmd)
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800829{
830 struct net_device_context *ndc = netdev_priv(dev);
831 u32 speed;
832
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100833 speed = cmd->base.speed;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800834 if (!ethtool_validate_speed(speed) ||
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100835 !ethtool_validate_duplex(cmd->base.duplex) ||
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800836 !netvsc_validate_ethtool_ss_cmd(cmd))
837 return -EINVAL;
838
839 ndc->speed = speed;
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100840 ndc->duplex = cmd->base.duplex;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800841
842 return 0;
843}
844
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800845static int netvsc_change_mtu(struct net_device *ndev, int mtu)
846{
847 struct net_device_context *ndevctx = netdev_priv(ndev);
stephen hemminger545a8e72017-03-22 14:51:00 -0700848 struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200849 struct hv_device *hdev = ndevctx->device_ctx;
stephen hemminger9749fed2017-07-19 11:53:16 -0700850 int orig_mtu = ndev->mtu;
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800851 struct netvsc_device_info device_info;
stephen hemmingerea383bf2017-07-19 11:53:15 -0700852 bool was_opened;
stephen hemminger9749fed2017-07-19 11:53:16 -0700853 int ret = 0;
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800854
stephen hemmingera0be4502017-03-22 14:51:01 -0700855 if (!nvdev || nvdev->destroy)
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800856 return -ENODEV;
857
stephen hemmingerea383bf2017-07-19 11:53:15 -0700858 netif_device_detach(ndev);
859 was_opened = rndis_filter_opened(nvdev);
860 if (was_opened)
861 rndis_filter_close(nvdev);
Haiyang Zhang2de85302015-07-13 13:09:16 -0700862
Andrew Schwartzmeyer8ebdcc52015-08-11 17:14:31 -0700863 memset(&device_info, 0, sizeof(device_info));
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800864 device_info.ring_size = ring_size;
stephen hemminger2b018882017-01-24 13:06:03 -0800865 device_info.num_chn = nvdev->num_chn;
866 device_info.max_num_vrss_chns = nvdev->num_chn;
Dexuan Cui152669b2017-03-02 13:00:53 +0000867
Dexuan Cui152669b2017-03-02 13:00:53 +0000868 rndis_filter_device_remove(hdev, nvdev);
869
Dexuan Cui152669b2017-03-02 13:00:53 +0000870 ndev->mtu = mtu;
871
stephen hemminger9749fed2017-07-19 11:53:16 -0700872 nvdev = rndis_filter_device_add(hdev, &device_info);
873 if (IS_ERR(nvdev)) {
874 ret = PTR_ERR(nvdev);
875
876 /* Attempt rollback to original MTU */
877 ndev->mtu = orig_mtu;
878 rndis_filter_device_add(hdev, &device_info);
879 }
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800880
stephen hemmingerea383bf2017-07-19 11:53:15 -0700881 if (was_opened)
882 rndis_filter_open(nvdev);
883
884 netif_device_attach(ndev);
stephen hemminger163891d2017-03-22 14:50:58 -0700885
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +0200886 /* We may have missed link change notifications */
887 schedule_delayed_work(&ndevctx->dwork, 0);
888
stephen hemminger9749fed2017-07-19 11:53:16 -0700889 return ret;
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800890}
891
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800892static void netvsc_get_stats64(struct net_device *net,
893 struct rtnl_link_stats64 *t)
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700894{
895 struct net_device_context *ndev_ctx = netdev_priv(net);
stephen hemminger776e7262017-04-14 14:42:57 -0700896 struct netvsc_device *nvdev = rcu_dereference_rtnl(ndev_ctx->nvdev);
Simon Xiao6c80f3f2017-01-24 13:06:13 -0800897 int i;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700898
Simon Xiao6c80f3f2017-01-24 13:06:13 -0800899 if (!nvdev)
900 return;
901
902 for (i = 0; i < nvdev->num_chn; i++) {
903 const struct netvsc_channel *nvchan = &nvdev->chan_table[i];
904 const struct netvsc_stats *stats;
905 u64 packets, bytes, multicast;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700906 unsigned int start;
907
Simon Xiao6c80f3f2017-01-24 13:06:13 -0800908 stats = &nvchan->tx_stats;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700909 do {
Simon Xiao6c80f3f2017-01-24 13:06:13 -0800910 start = u64_stats_fetch_begin_irq(&stats->syncp);
911 packets = stats->packets;
912 bytes = stats->bytes;
913 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700914
Simon Xiao6c80f3f2017-01-24 13:06:13 -0800915 t->tx_bytes += bytes;
916 t->tx_packets += packets;
917
918 stats = &nvchan->rx_stats;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700919 do {
Simon Xiao6c80f3f2017-01-24 13:06:13 -0800920 start = u64_stats_fetch_begin_irq(&stats->syncp);
921 packets = stats->packets;
922 bytes = stats->bytes;
923 multicast = stats->multicast + stats->broadcast;
924 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700925
Simon Xiao6c80f3f2017-01-24 13:06:13 -0800926 t->rx_bytes += bytes;
927 t->rx_packets += packets;
928 t->multicast += multicast;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700929 }
930
931 t->tx_dropped = net->stats.tx_dropped;
Simon Xiaob5124722017-02-17 11:36:20 -0800932 t->tx_errors = net->stats.tx_errors;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700933
934 t->rx_dropped = net->stats.rx_dropped;
935 t->rx_errors = net->stats.rx_errors;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700936}
Haiyang Zhang1ce09e82012-07-10 07:19:22 +0000937
938static int netvsc_set_mac_addr(struct net_device *ndev, void *p)
939{
Haiyang Zhang1ce09e82012-07-10 07:19:22 +0000940 struct sockaddr *addr = p;
Jianjun Kong9a4c8312013-01-18 16:52:09 +0000941 char save_adr[ETH_ALEN];
Haiyang Zhang1ce09e82012-07-10 07:19:22 +0000942 unsigned char save_aatype;
943 int err;
944
945 memcpy(save_adr, ndev->dev_addr, ETH_ALEN);
946 save_aatype = ndev->addr_assign_type;
947
948 err = eth_mac_addr(ndev, p);
949 if (err != 0)
950 return err;
951
Vitaly Kuznetsove834da9a2016-06-03 17:51:01 +0200952 err = rndis_filter_set_device_mac(ndev, addr->sa_data);
Haiyang Zhang1ce09e82012-07-10 07:19:22 +0000953 if (err != 0) {
954 /* roll back to saved MAC */
955 memcpy(ndev->dev_addr, save_adr, ETH_ALEN);
956 ndev->addr_assign_type = save_aatype;
957 }
958
959 return err;
960}
961
Stephen Hemminger4323b472016-08-23 12:17:57 -0700962static const struct {
963 char name[ETH_GSTRING_LEN];
964 u16 offset;
965} netvsc_stats[] = {
966 { "tx_scattered", offsetof(struct netvsc_ethtool_stats, tx_scattered) },
967 { "tx_no_memory", offsetof(struct netvsc_ethtool_stats, tx_no_memory) },
968 { "tx_no_space", offsetof(struct netvsc_ethtool_stats, tx_no_space) },
969 { "tx_too_big", offsetof(struct netvsc_ethtool_stats, tx_too_big) },
970 { "tx_busy", offsetof(struct netvsc_ethtool_stats, tx_busy) },
971};
972
Simon Xiao6c80f3f2017-01-24 13:06:13 -0800973#define NETVSC_GLOBAL_STATS_LEN ARRAY_SIZE(netvsc_stats)
974
975/* 4 statistics per queue (rx/tx packets/bytes) */
976#define NETVSC_QUEUE_STATS_LEN(dev) ((dev)->num_chn * 4)
977
Stephen Hemminger4323b472016-08-23 12:17:57 -0700978static int netvsc_get_sset_count(struct net_device *dev, int string_set)
979{
Simon Xiao6c80f3f2017-01-24 13:06:13 -0800980 struct net_device_context *ndc = netdev_priv(dev);
stephen hemmingerfbd4c7e2017-06-07 15:53:47 -0700981 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
stephen hemminger545a8e72017-03-22 14:51:00 -0700982
983 if (!nvdev)
984 return -ENODEV;
Simon Xiao6c80f3f2017-01-24 13:06:13 -0800985
Stephen Hemminger4323b472016-08-23 12:17:57 -0700986 switch (string_set) {
987 case ETH_SS_STATS:
Simon Xiao6c80f3f2017-01-24 13:06:13 -0800988 return NETVSC_GLOBAL_STATS_LEN + NETVSC_QUEUE_STATS_LEN(nvdev);
Stephen Hemminger4323b472016-08-23 12:17:57 -0700989 default:
990 return -EINVAL;
991 }
992}
993
994static void netvsc_get_ethtool_stats(struct net_device *dev,
995 struct ethtool_stats *stats, u64 *data)
996{
997 struct net_device_context *ndc = netdev_priv(dev);
stephen hemminger545a8e72017-03-22 14:51:00 -0700998 struct netvsc_device *nvdev = rcu_dereference(ndc->nvdev);
Stephen Hemminger4323b472016-08-23 12:17:57 -0700999 const void *nds = &ndc->eth_stats;
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001000 const struct netvsc_stats *qstats;
1001 unsigned int start;
1002 u64 packets, bytes;
1003 int i, j;
Stephen Hemminger4323b472016-08-23 12:17:57 -07001004
stephen hemminger545a8e72017-03-22 14:51:00 -07001005 if (!nvdev)
1006 return;
1007
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001008 for (i = 0; i < NETVSC_GLOBAL_STATS_LEN; i++)
Stephen Hemminger4323b472016-08-23 12:17:57 -07001009 data[i] = *(unsigned long *)(nds + netvsc_stats[i].offset);
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001010
1011 for (j = 0; j < nvdev->num_chn; j++) {
1012 qstats = &nvdev->chan_table[j].tx_stats;
1013
1014 do {
1015 start = u64_stats_fetch_begin_irq(&qstats->syncp);
1016 packets = qstats->packets;
1017 bytes = qstats->bytes;
1018 } while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
1019 data[i++] = packets;
1020 data[i++] = bytes;
1021
1022 qstats = &nvdev->chan_table[j].rx_stats;
1023 do {
1024 start = u64_stats_fetch_begin_irq(&qstats->syncp);
1025 packets = qstats->packets;
1026 bytes = qstats->bytes;
1027 } while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
1028 data[i++] = packets;
1029 data[i++] = bytes;
1030 }
Stephen Hemminger4323b472016-08-23 12:17:57 -07001031}
1032
1033static void netvsc_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1034{
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001035 struct net_device_context *ndc = netdev_priv(dev);
stephen hemminger545a8e72017-03-22 14:51:00 -07001036 struct netvsc_device *nvdev = rcu_dereference(ndc->nvdev);
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001037 u8 *p = data;
Stephen Hemminger4323b472016-08-23 12:17:57 -07001038 int i;
1039
stephen hemminger545a8e72017-03-22 14:51:00 -07001040 if (!nvdev)
1041 return;
1042
Stephen Hemminger4323b472016-08-23 12:17:57 -07001043 switch (stringset) {
1044 case ETH_SS_STATS:
1045 for (i = 0; i < ARRAY_SIZE(netvsc_stats); i++)
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001046 memcpy(p + i * ETH_GSTRING_LEN,
Stephen Hemminger4323b472016-08-23 12:17:57 -07001047 netvsc_stats[i].name, ETH_GSTRING_LEN);
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001048
1049 p += i * ETH_GSTRING_LEN;
1050 for (i = 0; i < nvdev->num_chn; i++) {
1051 sprintf(p, "tx_queue_%u_packets", i);
1052 p += ETH_GSTRING_LEN;
1053 sprintf(p, "tx_queue_%u_bytes", i);
1054 p += ETH_GSTRING_LEN;
1055 sprintf(p, "rx_queue_%u_packets", i);
1056 p += ETH_GSTRING_LEN;
1057 sprintf(p, "rx_queue_%u_bytes", i);
1058 p += ETH_GSTRING_LEN;
1059 }
1060
Stephen Hemminger4323b472016-08-23 12:17:57 -07001061 break;
1062 }
1063}
1064
stephen hemmingerb448f4e2017-01-24 13:06:00 -08001065static int
stephen hemmingerb5a5dc82017-01-24 13:06:01 -08001066netvsc_get_rss_hash_opts(struct netvsc_device *nvdev,
1067 struct ethtool_rxnfc *info)
1068{
1069 info->data = RXH_IP_SRC | RXH_IP_DST;
1070
1071 switch (info->flow_type) {
1072 case TCP_V4_FLOW:
1073 case TCP_V6_FLOW:
1074 info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
1075 /* fallthrough */
1076 case UDP_V4_FLOW:
1077 case UDP_V6_FLOW:
1078 case IPV4_FLOW:
1079 case IPV6_FLOW:
1080 break;
1081 default:
1082 info->data = 0;
1083 break;
1084 }
1085
1086 return 0;
1087}
1088
1089static int
stephen hemmingerb448f4e2017-01-24 13:06:00 -08001090netvsc_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
1091 u32 *rules)
1092{
1093 struct net_device_context *ndc = netdev_priv(dev);
stephen hemminger545a8e72017-03-22 14:51:00 -07001094 struct netvsc_device *nvdev = rcu_dereference(ndc->nvdev);
1095
1096 if (!nvdev)
1097 return -ENODEV;
stephen hemmingerb448f4e2017-01-24 13:06:00 -08001098
1099 switch (info->cmd) {
1100 case ETHTOOL_GRXRINGS:
1101 info->data = nvdev->num_chn;
1102 return 0;
stephen hemmingerb5a5dc82017-01-24 13:06:01 -08001103
1104 case ETHTOOL_GRXFH:
1105 return netvsc_get_rss_hash_opts(nvdev, info);
stephen hemmingerb448f4e2017-01-24 13:06:00 -08001106 }
1107 return -EOPNOTSUPP;
1108}
1109
Richard Weinberger316158f2014-07-09 16:23:59 +02001110#ifdef CONFIG_NET_POLL_CONTROLLER
stephen hemmingera5ecd432017-06-07 15:53:48 -07001111static void netvsc_poll_controller(struct net_device *dev)
Richard Weinberger316158f2014-07-09 16:23:59 +02001112{
stephen hemmingera5ecd432017-06-07 15:53:48 -07001113 struct net_device_context *ndc = netdev_priv(dev);
1114 struct netvsc_device *ndev;
1115 int i;
1116
1117 rcu_read_lock();
1118 ndev = rcu_dereference(ndc->nvdev);
1119 if (ndev) {
1120 for (i = 0; i < ndev->num_chn; i++) {
1121 struct netvsc_channel *nvchan = &ndev->chan_table[i];
1122
1123 napi_schedule(&nvchan->napi);
1124 }
1125 }
1126 rcu_read_unlock();
Richard Weinberger316158f2014-07-09 16:23:59 +02001127}
1128#endif
Haiyang Zhang1ce09e82012-07-10 07:19:22 +00001129
stephen hemminger962f3fe2017-01-24 13:06:02 -08001130static u32 netvsc_get_rxfh_key_size(struct net_device *dev)
1131{
1132 return NETVSC_HASH_KEYLEN;
1133}
1134
1135static u32 netvsc_rss_indir_size(struct net_device *dev)
1136{
stephen hemmingerff4a4412017-01-24 13:06:04 -08001137 return ITAB_NUM;
stephen hemminger962f3fe2017-01-24 13:06:02 -08001138}
1139
1140static int netvsc_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
1141 u8 *hfunc)
1142{
1143 struct net_device_context *ndc = netdev_priv(dev);
stephen hemminger545a8e72017-03-22 14:51:00 -07001144 struct netvsc_device *ndev = rcu_dereference(ndc->nvdev);
Colin Ian Kingeb996ed2017-03-25 14:26:39 +00001145 struct rndis_device *rndis_dev;
stephen hemmingerff4a4412017-01-24 13:06:04 -08001146 int i;
stephen hemminger962f3fe2017-01-24 13:06:02 -08001147
stephen hemminger545a8e72017-03-22 14:51:00 -07001148 if (!ndev)
1149 return -ENODEV;
1150
stephen hemminger962f3fe2017-01-24 13:06:02 -08001151 if (hfunc)
1152 *hfunc = ETH_RSS_HASH_TOP; /* Toeplitz */
1153
Colin Ian Kingeb996ed2017-03-25 14:26:39 +00001154 rndis_dev = ndev->extension;
stephen hemmingerff4a4412017-01-24 13:06:04 -08001155 if (indir) {
1156 for (i = 0; i < ITAB_NUM; i++)
1157 indir[i] = rndis_dev->ind_table[i];
1158 }
1159
stephen hemminger962f3fe2017-01-24 13:06:02 -08001160 if (key)
1161 memcpy(key, rndis_dev->rss_key, NETVSC_HASH_KEYLEN);
1162
1163 return 0;
1164}
1165
1166static int netvsc_set_rxfh(struct net_device *dev, const u32 *indir,
1167 const u8 *key, const u8 hfunc)
1168{
1169 struct net_device_context *ndc = netdev_priv(dev);
stephen hemminger545a8e72017-03-22 14:51:00 -07001170 struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev);
Colin Ian Kingeb996ed2017-03-25 14:26:39 +00001171 struct rndis_device *rndis_dev;
stephen hemmingerff4a4412017-01-24 13:06:04 -08001172 int i;
stephen hemminger962f3fe2017-01-24 13:06:02 -08001173
stephen hemminger545a8e72017-03-22 14:51:00 -07001174 if (!ndev)
1175 return -ENODEV;
1176
stephen hemminger962f3fe2017-01-24 13:06:02 -08001177 if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
1178 return -EOPNOTSUPP;
1179
Colin Ian Kingeb996ed2017-03-25 14:26:39 +00001180 rndis_dev = ndev->extension;
stephen hemmingerff4a4412017-01-24 13:06:04 -08001181 if (indir) {
1182 for (i = 0; i < ITAB_NUM; i++)
Arnd Bergmannb92b7d32017-06-22 00:16:37 +02001183 if (indir[i] >= VRSS_CHANNEL_MAX)
stephen hemmingerff4a4412017-01-24 13:06:04 -08001184 return -EINVAL;
1185
1186 for (i = 0; i < ITAB_NUM; i++)
1187 rndis_dev->ind_table[i] = indir[i];
1188 }
1189
1190 if (!key) {
1191 if (!indir)
1192 return 0;
1193
1194 key = rndis_dev->rss_key;
1195 }
stephen hemminger962f3fe2017-01-24 13:06:02 -08001196
1197 return rndis_filter_set_rss_param(rndis_dev, key, ndev->num_chn);
1198}
1199
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -07001200static const struct ethtool_ops ethtool_ops = {
1201 .get_drvinfo = netvsc_get_drvinfo,
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -07001202 .get_link = ethtool_op_get_link,
Stephen Hemminger4323b472016-08-23 12:17:57 -07001203 .get_ethtool_stats = netvsc_get_ethtool_stats,
1204 .get_sset_count = netvsc_get_sset_count,
1205 .get_strings = netvsc_get_strings,
Andrew Schwartzmeyer59995372015-02-26 16:27:14 -08001206 .get_channels = netvsc_get_channels,
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -07001207 .set_channels = netvsc_set_channels,
sixiao@microsoft.com76d13b52016-02-17 16:43:59 -08001208 .get_ts_info = ethtool_op_get_ts_info,
stephen hemmingerb448f4e2017-01-24 13:06:00 -08001209 .get_rxnfc = netvsc_get_rxnfc,
stephen hemminger962f3fe2017-01-24 13:06:02 -08001210 .get_rxfh_key_size = netvsc_get_rxfh_key_size,
1211 .get_rxfh_indir_size = netvsc_rss_indir_size,
1212 .get_rxfh = netvsc_get_rxfh,
1213 .set_rxfh = netvsc_set_rxfh,
Philippe Reynes5e8456f2017-03-08 23:41:04 +01001214 .get_link_ksettings = netvsc_get_link_ksettings,
1215 .set_link_ksettings = netvsc_set_link_ksettings,
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -07001216};
1217
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001218static const struct net_device_ops device_ops = {
1219 .ndo_open = netvsc_open,
1220 .ndo_stop = netvsc_close,
1221 .ndo_start_xmit = netvsc_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001222 .ndo_set_rx_mode = netvsc_set_multicast_list,
Haiyang Zhang4d447c92011-12-15 13:45:17 -08001223 .ndo_change_mtu = netvsc_change_mtu,
Haiyang Zhangb681b582010-08-03 19:15:31 +00001224 .ndo_validate_addr = eth_validate_addr,
Haiyang Zhang1ce09e82012-07-10 07:19:22 +00001225 .ndo_set_mac_address = netvsc_set_mac_addr,
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001226 .ndo_select_queue = netvsc_select_queue,
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001227 .ndo_get_stats64 = netvsc_get_stats64,
Richard Weinberger316158f2014-07-09 16:23:59 +02001228#ifdef CONFIG_NET_POLL_CONTROLLER
1229 .ndo_poll_controller = netvsc_poll_controller,
1230#endif
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001231};
1232
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001233/*
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001234 * Handle link status changes. For RNDIS_STATUS_NETWORK_CHANGE emulate link
1235 * down/up sequence. In case of RNDIS_STATUS_MEDIA_CONNECT when carrier is
1236 * present send GARP packet to network peers with netif_notify_peers().
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001237 */
Haiyang Zhang891de742014-02-12 16:54:27 -08001238static void netvsc_link_change(struct work_struct *w)
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001239{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001240 struct net_device_context *ndev_ctx =
1241 container_of(w, struct net_device_context, dwork.work);
1242 struct hv_device *device_obj = ndev_ctx->device_ctx;
1243 struct net_device *net = hv_get_drvdata(device_obj);
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -07001244 struct netvsc_device *net_device;
Haiyang Zhang891de742014-02-12 16:54:27 -08001245 struct rndis_device *rdev;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001246 struct netvsc_reconfig *event = NULL;
1247 bool notify = false, reschedule = false;
1248 unsigned long flags, next_reconfig, delay;
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001249
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001250 rtnl_lock();
stephen hemmingera0be4502017-03-22 14:51:01 -07001251 net_device = rtnl_dereference(ndev_ctx->nvdev);
1252 if (!net_device)
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001253 goto out_unlock;
1254
Haiyang Zhang891de742014-02-12 16:54:27 -08001255 rdev = net_device->extension;
Haiyang Zhang891de742014-02-12 16:54:27 -08001256
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001257 next_reconfig = ndev_ctx->last_reconfig + LINKCHANGE_INT;
1258 if (time_is_after_jiffies(next_reconfig)) {
1259 /* link_watch only sends one notification with current state
1260 * per second, avoid doing reconfig more frequently. Handle
1261 * wrap around.
1262 */
1263 delay = next_reconfig - jiffies;
1264 delay = delay < LINKCHANGE_INT ? delay : LINKCHANGE_INT;
1265 schedule_delayed_work(&ndev_ctx->dwork, delay);
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001266 goto out_unlock;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001267 }
1268 ndev_ctx->last_reconfig = jiffies;
1269
1270 spin_lock_irqsave(&ndev_ctx->lock, flags);
1271 if (!list_empty(&ndev_ctx->reconfig_events)) {
1272 event = list_first_entry(&ndev_ctx->reconfig_events,
1273 struct netvsc_reconfig, list);
1274 list_del(&event->list);
1275 reschedule = !list_empty(&ndev_ctx->reconfig_events);
1276 }
1277 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1278
1279 if (!event)
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001280 goto out_unlock;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001281
1282 switch (event->event) {
1283 /* Only the following events are possible due to the check in
1284 * netvsc_linkstatus_callback()
1285 */
1286 case RNDIS_STATUS_MEDIA_CONNECT:
1287 if (rdev->link_state) {
1288 rdev->link_state = false;
Haiyang Zhang53fa1a62017-06-21 16:40:47 -07001289 if (!ndev_ctx->datapath)
1290 netif_carrier_on(net);
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001291 netif_tx_wake_all_queues(net);
1292 } else {
1293 notify = true;
Haiyang Zhang3a494e72014-06-19 18:34:36 -07001294 }
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001295 kfree(event);
1296 break;
1297 case RNDIS_STATUS_MEDIA_DISCONNECT:
1298 if (!rdev->link_state) {
1299 rdev->link_state = true;
1300 netif_carrier_off(net);
1301 netif_tx_stop_all_queues(net);
1302 }
1303 kfree(event);
1304 break;
1305 case RNDIS_STATUS_NETWORK_CHANGE:
1306 /* Only makes sense if carrier is present */
1307 if (!rdev->link_state) {
1308 rdev->link_state = true;
1309 netif_carrier_off(net);
1310 netif_tx_stop_all_queues(net);
1311 event->event = RNDIS_STATUS_MEDIA_CONNECT;
1312 spin_lock_irqsave(&ndev_ctx->lock, flags);
Haiyang Zhang15cfd402016-04-21 16:13:01 -07001313 list_add(&event->list, &ndev_ctx->reconfig_events);
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001314 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1315 reschedule = true;
1316 }
1317 break;
Haiyang Zhang891de742014-02-12 16:54:27 -08001318 }
1319
1320 rtnl_unlock();
1321
1322 if (notify)
1323 netdev_notify_peers(net);
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001324
1325 /* link_watch only sends one notification with current state per
1326 * second, handle next reconfig event in 2 seconds.
1327 */
1328 if (reschedule)
1329 schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001330
1331 return;
1332
1333out_unlock:
1334 rtnl_unlock();
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001335}
1336
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001337static struct net_device *get_netvsc_bymac(const u8 *mac)
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001338{
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001339 struct net_device *dev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001340
Stephen Hemminger8737caa2016-08-23 12:17:44 -07001341 ASSERT_RTNL();
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001342
1343 for_each_netdev(&init_net, dev) {
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001344 if (dev->netdev_ops != &device_ops)
1345 continue; /* not a netvsc device */
1346
1347 if (ether_addr_equal(mac, dev->perm_addr))
1348 return dev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001349 }
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001350
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001351 return NULL;
1352}
1353
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001354static struct net_device *get_netvsc_byref(struct net_device *vf_netdev)
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001355{
1356 struct net_device *dev;
1357
1358 ASSERT_RTNL();
1359
1360 for_each_netdev(&init_net, dev) {
1361 struct net_device_context *net_device_ctx;
1362
1363 if (dev->netdev_ops != &device_ops)
1364 continue; /* not a netvsc device */
1365
1366 net_device_ctx = netdev_priv(dev);
stephen hemminger79e8cbe2017-07-19 11:53:13 -07001367 if (!rtnl_dereference(net_device_ctx->nvdev))
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001368 continue; /* device is removed */
1369
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001370 if (rtnl_dereference(net_device_ctx->vf_netdev) == vf_netdev)
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001371 return dev; /* a match */
1372 }
1373
1374 return NULL;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001375}
1376
1377static int netvsc_register_vf(struct net_device *vf_netdev)
1378{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001379 struct net_device *ndev;
1380 struct net_device_context *net_device_ctx;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001381 struct netvsc_device *netvsc_dev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001382
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001383 if (vf_netdev->addr_len != ETH_ALEN)
1384 return NOTIFY_DONE;
1385
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001386 /*
1387 * We will use the MAC address to locate the synthetic interface to
1388 * associate with the VF interface. If we don't find a matching
1389 * synthetic interface, move on.
1390 */
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001391 ndev = get_netvsc_bymac(vf_netdev->perm_addr);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001392 if (!ndev)
1393 return NOTIFY_DONE;
1394
1395 net_device_ctx = netdev_priv(ndev);
stephen hemminger545a8e72017-03-22 14:51:00 -07001396 netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001397 if (!netvsc_dev || rtnl_dereference(net_device_ctx->vf_netdev))
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001398 return NOTIFY_DONE;
1399
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001400 netdev_info(ndev, "VF registering: %s\n", vf_netdev->name);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001401 /*
1402 * Take a reference on the module.
1403 */
1404 try_module_get(THIS_MODULE);
Stephen Hemminger07d0f002016-09-22 16:56:30 -07001405
1406 dev_hold(vf_netdev);
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001407 rcu_assign_pointer(net_device_ctx->vf_netdev, vf_netdev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001408 return NOTIFY_OK;
1409}
1410
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001411static int netvsc_vf_up(struct net_device *vf_netdev)
1412{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001413 struct net_device *ndev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001414 struct netvsc_device *netvsc_dev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001415 struct net_device_context *net_device_ctx;
1416
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001417 ndev = get_netvsc_byref(vf_netdev);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001418 if (!ndev)
1419 return NOTIFY_DONE;
1420
1421 net_device_ctx = netdev_priv(ndev);
stephen hemminger545a8e72017-03-22 14:51:00 -07001422 netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001423
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001424 netdev_info(ndev, "VF up: %s\n", vf_netdev->name);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001425
1426 /*
1427 * Open the device before switching data path.
1428 */
Vitaly Kuznetsov2f5fa6c2016-06-03 17:51:00 +02001429 rndis_filter_open(netvsc_dev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001430
1431 /*
1432 * notify the host to switch the data path.
1433 */
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001434 netvsc_switch_datapath(ndev, true);
1435 netdev_info(ndev, "Data path switched to VF: %s\n", vf_netdev->name);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001436
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001437 netif_carrier_off(ndev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001438
Vitaly Kuznetsovd0722182016-08-15 17:48:40 +02001439 /* Now notify peers through VF device. */
1440 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, vf_netdev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001441
1442 return NOTIFY_OK;
1443}
1444
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001445static int netvsc_vf_down(struct net_device *vf_netdev)
1446{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001447 struct net_device *ndev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001448 struct netvsc_device *netvsc_dev;
1449 struct net_device_context *net_device_ctx;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001450
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001451 ndev = get_netvsc_byref(vf_netdev);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001452 if (!ndev)
1453 return NOTIFY_DONE;
1454
1455 net_device_ctx = netdev_priv(ndev);
stephen hemminger545a8e72017-03-22 14:51:00 -07001456 netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001457
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001458 netdev_info(ndev, "VF down: %s\n", vf_netdev->name);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001459 netvsc_switch_datapath(ndev, false);
1460 netdev_info(ndev, "Data path switched from VF: %s\n", vf_netdev->name);
Vitaly Kuznetsov2f5fa6c2016-06-03 17:51:00 +02001461 rndis_filter_close(netvsc_dev);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001462 netif_carrier_on(ndev);
Vitaly Kuznetsovd0722182016-08-15 17:48:40 +02001463
1464 /* Now notify peers through netvsc device. */
1465 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, ndev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001466
1467 return NOTIFY_OK;
1468}
1469
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001470static int netvsc_unregister_vf(struct net_device *vf_netdev)
1471{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001472 struct net_device *ndev;
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001473 struct net_device_context *net_device_ctx;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001474
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001475 ndev = get_netvsc_byref(vf_netdev);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001476 if (!ndev)
1477 return NOTIFY_DONE;
1478
1479 net_device_ctx = netdev_priv(ndev);
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001480
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001481 netdev_info(ndev, "VF unregistering: %s\n", vf_netdev->name);
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001482
1483 RCU_INIT_POINTER(net_device_ctx->vf_netdev, NULL);
Stephen Hemminger07d0f002016-09-22 16:56:30 -07001484 dev_put(vf_netdev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001485 module_put(THIS_MODULE);
1486 return NOTIFY_OK;
1487}
1488
K. Y. Srinivasan84946892011-09-13 10:59:38 -07001489static int netvsc_probe(struct hv_device *dev,
1490 const struct hv_vmbus_device_id *dev_id)
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001491{
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001492 struct net_device *net = NULL;
1493 struct net_device_context *net_device_ctx;
1494 struct netvsc_device_info device_info;
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001495 struct netvsc_device *nvdev;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001496 int ret;
1497
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001498 net = alloc_etherdev_mq(sizeof(struct net_device_context),
stephen hemminger2b018882017-01-24 13:06:03 -08001499 VRSS_CHANNEL_MAX);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001500 if (!net)
K. Y. Srinivasan51a805d2011-08-25 09:49:11 -07001501 return -ENOMEM;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001502
Haiyang Zhang1b07da52014-03-04 14:11:06 -08001503 netif_carrier_off(net);
1504
Haiyang Zhangb37879e2016-08-04 10:42:14 -07001505 netvsc_init_settings(net);
1506
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001507 net_device_ctx = netdev_priv(net);
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -07001508 net_device_ctx->device_ctx = dev;
Simon Xiao3f300ff2015-04-28 01:05:17 -07001509 net_device_ctx->msg_enable = netif_msg_init(debug, default_msg);
1510 if (netif_msg_probe(net_device_ctx))
1511 netdev_dbg(net, "netvsc msg_enable: %d\n",
1512 net_device_ctx->msg_enable);
1513
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -07001514 hv_set_drvdata(dev, net);
Vitaly Kuznetsovf580aec2016-05-13 13:55:20 +02001515
Haiyang Zhang891de742014-02-12 16:54:27 -08001516 INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001517
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001518 spin_lock_init(&net_device_ctx->lock);
1519 INIT_LIST_HEAD(&net_device_ctx->reconfig_events);
1520
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001521 net->netdev_ops = &device_ops;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001522 net->ethtool_ops = &ethtool_ops;
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -07001523 SET_NETDEV_DEV(net, &dev->device);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001524
Vitaly Kuznetsov14a03cf2016-02-05 17:29:08 +01001525 /* We always need headroom for rndis header */
1526 net->needed_headroom = RNDIS_AND_PPI_SIZE;
1527
Haiyang Zhang692e0842011-09-01 12:19:43 -07001528 /* Notify the netvsc driver of the new device */
Andrew Schwartzmeyer8ebdcc52015-08-11 17:14:31 -07001529 memset(&device_info, 0, sizeof(device_info));
Haiyang Zhang692e0842011-09-01 12:19:43 -07001530 device_info.ring_size = ring_size;
stephen hemminger3071ada2017-03-22 14:50:59 -07001531 device_info.num_chn = VRSS_CHANNEL_DEFAULT;
stephen hemminger9749fed2017-07-19 11:53:16 -07001532
1533 nvdev = rndis_filter_device_add(dev, &device_info);
1534 if (IS_ERR(nvdev)) {
1535 ret = PTR_ERR(nvdev);
Haiyang Zhang692e0842011-09-01 12:19:43 -07001536 netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001537 free_netdev(net);
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -07001538 hv_set_drvdata(dev, NULL);
Haiyang Zhang692e0842011-09-01 12:19:43 -07001539 return ret;
1540 }
1541 memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
1542
stephen hemminger23312a32017-01-24 13:05:59 -08001543 /* hw_features computed in rndis_filter_device_add */
1544 net->features = net->hw_features |
1545 NETIF_F_HIGHDMA | NETIF_F_SG |
1546 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
1547 net->vlan_features = net->features;
1548
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001549 netif_set_real_num_tx_queues(net, nvdev->num_chn);
1550 netif_set_real_num_rx_queues(net, nvdev->num_chn);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001551
stephen hemminger9749fed2017-07-19 11:53:16 -07001552 netdev_lockdep_set_classes(net);
1553
Jarod Wilsond0c2c992016-10-20 13:55:21 -04001554 /* MTU range: 68 - 1500 or 65521 */
1555 net->min_mtu = NETVSC_MTU_MIN;
1556 if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
1557 net->max_mtu = NETVSC_MTU - ETH_HLEN;
1558 else
1559 net->max_mtu = ETH_DATA_LEN;
1560
Haiyang Zhanga68f9612013-12-20 16:52:31 -08001561 ret = register_netdev(net);
1562 if (ret != 0) {
1563 pr_err("Unable to register netdev.\n");
stephen hemminger2289f0a2017-01-24 13:06:10 -08001564 rndis_filter_device_remove(dev, nvdev);
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001565 free_netdev(net);
Haiyang Zhanga68f9612013-12-20 16:52:31 -08001566 }
1567
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001568 return ret;
1569}
1570
K. Y. Srinivasan415b0232011-04-29 13:45:12 -07001571static int netvsc_remove(struct hv_device *dev)
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001572{
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -07001573 struct net_device *net;
Haiyang Zhang122a5f62011-05-27 06:21:55 -07001574 struct net_device_context *ndev_ctx;
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -07001575
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +02001576 net = hv_get_drvdata(dev);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001577
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001578 if (net == NULL) {
K. Y. Srinivasan415b0232011-04-29 13:45:12 -07001579 dev_err(&dev->device, "No net device to remove\n");
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001580 return 0;
1581 }
1582
Haiyang Zhang122a5f62011-05-27 06:21:55 -07001583 ndev_ctx = netdev_priv(net);
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +02001584
stephen hemmingera0be4502017-03-22 14:51:01 -07001585 netif_device_detach(net);
Vitaly Kuznetsovf580aec2016-05-13 13:55:20 +02001586
Haiyang Zhang122a5f62011-05-27 06:21:55 -07001587 cancel_delayed_work_sync(&ndev_ctx->dwork);
1588
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001589 /*
1590 * Call to the vsc driver to let it know that the device is being
stephen hemmingera0be4502017-03-22 14:51:01 -07001591 * removed. Also blocks mtu and channel changes.
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001592 */
stephen hemmingera0be4502017-03-22 14:51:01 -07001593 rtnl_lock();
stephen hemminger79e8cbe2017-07-19 11:53:13 -07001594 rndis_filter_device_remove(dev,
1595 rtnl_dereference(ndev_ctx->nvdev));
stephen hemmingera0be4502017-03-22 14:51:01 -07001596 rtnl_unlock();
1597
1598 unregister_netdev(net);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001599
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +02001600 hv_set_drvdata(dev, NULL);
1601
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001602 free_netdev(net);
Haiyang Zhangdf06bcf2011-05-23 09:03:47 -07001603 return 0;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001604}
1605
K. Y. Srinivasan345c4cc2011-08-25 09:48:34 -07001606static const struct hv_vmbus_device_id id_table[] = {
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07001607 /* Network guid */
K. Y. Srinivasan8f505942013-01-23 17:42:42 -08001608 { HV_NIC_GUID, },
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07001609 { },
K. Y. Srinivasan345c4cc2011-08-25 09:48:34 -07001610};
1611
1612MODULE_DEVICE_TABLE(vmbus, id_table);
1613
K. Y. Srinivasanf1542a62011-05-10 07:55:16 -07001614/* The one and only one */
K. Y. Srinivasanfde0ef92011-05-12 19:35:08 -07001615static struct hv_driver netvsc_drv = {
Haiyang Zhangd31b20f2012-03-07 10:02:00 +00001616 .name = KBUILD_MODNAME,
K. Y. Srinivasan345c4cc2011-08-25 09:48:34 -07001617 .id_table = id_table,
K. Y. Srinivasanfde0ef92011-05-12 19:35:08 -07001618 .probe = netvsc_probe,
1619 .remove = netvsc_remove,
K. Y. Srinivasand4890972011-05-10 07:55:17 -07001620};
K. Y. Srinivasanf1542a62011-05-10 07:55:16 -07001621
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001622/*
1623 * On Hyper-V, every VF interface is matched with a corresponding
1624 * synthetic interface. The synthetic interface is presented first
1625 * to the guest. When the corresponding VF instance is registered,
1626 * we will take care of switching the data path.
1627 */
1628static int netvsc_netdev_event(struct notifier_block *this,
1629 unsigned long event, void *ptr)
1630{
1631 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
1632
Stephen Hemmingeree837a12016-09-22 16:56:31 -07001633 /* Skip our own events */
1634 if (event_dev->netdev_ops == &device_ops)
1635 return NOTIFY_DONE;
1636
1637 /* Avoid non-Ethernet type devices */
1638 if (event_dev->type != ARPHRD_ETHER)
1639 return NOTIFY_DONE;
1640
Vitaly Kuznetsov0dbff142016-08-15 17:48:43 +02001641 /* Avoid Vlan dev with same MAC registering as VF */
Parav Panditd0d7b102017-02-04 11:00:49 -06001642 if (is_vlan_dev(event_dev))
Vitaly Kuznetsov0dbff142016-08-15 17:48:43 +02001643 return NOTIFY_DONE;
1644
1645 /* Avoid Bonding master dev with same MAC registering as VF */
Stephen Hemmingeree837a12016-09-22 16:56:31 -07001646 if ((event_dev->priv_flags & IFF_BONDING) &&
1647 (event_dev->flags & IFF_MASTER))
Haiyang Zhangcb2911f2016-06-02 12:02:04 -07001648 return NOTIFY_DONE;
1649
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001650 switch (event) {
1651 case NETDEV_REGISTER:
1652 return netvsc_register_vf(event_dev);
1653 case NETDEV_UNREGISTER:
1654 return netvsc_unregister_vf(event_dev);
1655 case NETDEV_UP:
1656 return netvsc_vf_up(event_dev);
1657 case NETDEV_DOWN:
1658 return netvsc_vf_down(event_dev);
1659 default:
1660 return NOTIFY_DONE;
1661 }
1662}
1663
1664static struct notifier_block netvsc_netdev_notifier = {
1665 .notifier_call = netvsc_netdev_event,
1666};
1667
K. Y. Srinivasana9869c92011-05-12 19:35:17 -07001668static void __exit netvsc_drv_exit(void)
Hank Janssenfceaf242009-07-13 15:34:54 -07001669{
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001670 unregister_netdevice_notifier(&netvsc_netdev_notifier);
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001671 vmbus_driver_unregister(&netvsc_drv);
Hank Janssenfceaf242009-07-13 15:34:54 -07001672}
1673
K. Y. Srinivasan1fde28c2011-05-12 19:35:16 -07001674static int __init netvsc_drv_init(void)
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001675{
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001676 int ret;
1677
Haiyang Zhangfa85a6c2012-07-25 08:08:41 +00001678 if (ring_size < RING_SIZE_MIN) {
1679 ring_size = RING_SIZE_MIN;
1680 pr_info("Increased ring_size to %d (min allowed)\n",
1681 ring_size);
1682 }
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001683 ret = vmbus_driver_register(&netvsc_drv);
1684
1685 if (ret)
1686 return ret;
1687
1688 register_netdevice_notifier(&netvsc_netdev_notifier);
1689 return 0;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001690}
1691
Hank Janssen26c14cc2010-02-11 23:02:42 +00001692MODULE_LICENSE("GPL");
Stephen Hemminger7880fc52010-05-04 09:58:52 -07001693MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
Hank Janssenfceaf242009-07-13 15:34:54 -07001694
K. Y. Srinivasan1fde28c2011-05-12 19:35:16 -07001695module_init(netvsc_drv_init);
K. Y. Srinivasana9869c92011-05-12 19:35:17 -07001696module_exit(netvsc_drv_exit);