blob: c0c4c9195a3f91041f44abc6a183272965867ff1 [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>
stephen hemminger27f5aa92017-07-24 10:57:29 -070036#include <linux/rtnetlink.h>
stephen hemminger0c195562017-08-01 19:58:53 -070037#include <linux/netpoll.h>
stephen hemminger27f5aa92017-07-24 10:57:29 -070038
Hank Janssenfceaf242009-07-13 15:34:54 -070039#include <net/arp.h>
40#include <net/route.h>
41#include <net/sock.h>
42#include <net/pkt_sched.h>
Michael Kelley8eb1b3c2017-05-30 11:36:56 -070043#include <net/checksum.h>
44#include <net/ip6_checksum.h>
K. Y. Srinivasan3f335ea2011-05-12 19:34:15 -070045
K. Y. Srinivasan5ca72522011-05-12 19:34:37 -070046#include "hyperv_net.h"
Hank Janssenfceaf242009-07-13 15:34:54 -070047
stephen hemminger8b532792017-08-09 17:46:11 -070048#define RING_SIZE_MIN 64
49#define NETVSC_MIN_TX_SECTIONS 10
50#define NETVSC_DEFAULT_TX 192 /* ~1M */
51#define NETVSC_MIN_RX_SECTIONS 10 /* ~64K */
52#define NETVSC_DEFAULT_RX 2048 /* ~4M */
53
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +010054#define LINKCHANGE_INT (2 * HZ)
stephen hemminger6123c662017-08-09 17:46:03 -070055#define VF_TAKEOVER_INT (HZ / 10)
stephen hemmingera50af862016-12-06 13:43:54 -080056
Hank Janssen99c8da02010-10-12 10:45:23 -070057static int ring_size = 128;
Stephen Hemminger450d7a42010-05-04 09:58:53 -070058module_param(ring_size, int, S_IRUGO);
59MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)");
Hank Janssenfceaf242009-07-13 15:34:54 -070060
Simon Xiao3f300ff2015-04-28 01:05:17 -070061static const u32 default_msg = NETIF_MSG_DRV | NETIF_MSG_PROBE |
62 NETIF_MSG_LINK | NETIF_MSG_IFUP |
63 NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR |
64 NETIF_MSG_TX_ERR;
65
66static int debug = -1;
67module_param(debug, int, S_IRUGO);
68MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
69
Greg Kroah-Hartman4e9bfef2009-07-15 12:45:20 -070070static void netvsc_set_multicast_list(struct net_device *net)
Hank Janssenfceaf242009-07-13 15:34:54 -070071{
Wenqi Ma792df872012-04-19 00:39:37 +000072 struct net_device_context *net_device_ctx = netdev_priv(net);
stephen hemminger4f19c0d2017-06-07 15:53:49 -070073 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
Haiyang Zhangd426b2e2011-11-30 07:19:08 -080074
stephen hemminger4f19c0d2017-06-07 15:53:49 -070075 rndis_filter_update(nvdev);
Hank Janssenfceaf242009-07-13 15:34:54 -070076}
77
Hank Janssenfceaf242009-07-13 15:34:54 -070078static int netvsc_open(struct net_device *net)
79{
Haiyang Zhang53fa1a62017-06-21 16:40:47 -070080 struct net_device_context *ndev_ctx = netdev_priv(net);
stephen hemminger0c195562017-08-01 19:58:53 -070081 struct net_device *vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
stephen hemminger79e8cbe2017-07-19 11:53:13 -070082 struct netvsc_device *nvdev = rtnl_dereference(ndev_ctx->nvdev);
Haiyang Zhang891de742014-02-12 16:54:27 -080083 struct rndis_device *rdev;
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -070084 int ret = 0;
Hank Janssenfceaf242009-07-13 15:34:54 -070085
Haiyang Zhang891de742014-02-12 16:54:27 -080086 netif_carrier_off(net);
87
Haiyang Zhangd515d0f2011-09-28 13:24:15 -070088 /* Open up the device */
Vitaly Kuznetsov2f5fa6c2016-06-03 17:51:00 +020089 ret = rndis_filter_open(nvdev);
Haiyang Zhangd515d0f2011-09-28 13:24:15 -070090 if (ret != 0) {
91 netdev_err(net, "unable to open device (ret %d).\n", ret);
92 return ret;
Hank Janssenfceaf242009-07-13 15:34:54 -070093 }
94
Haiyang Zhang2de85302015-07-13 13:09:16 -070095 netif_tx_wake_all_queues(net);
Haiyang Zhangd515d0f2011-09-28 13:24:15 -070096
Haiyang Zhang891de742014-02-12 16:54:27 -080097 rdev = nvdev->extension;
stephen hemminger0c195562017-08-01 19:58:53 -070098
99 if (!rdev->link_state)
Haiyang Zhang891de742014-02-12 16:54:27 -0800100 netif_carrier_on(net);
101
stephen hemminger0c195562017-08-01 19:58:53 -0700102 if (vf_netdev) {
103 /* Setting synthetic device up transparently sets
104 * slave as up. If open fails, then slave will be
105 * still be offline (and not used).
106 */
107 ret = dev_open(vf_netdev);
108 if (ret)
109 netdev_warn(net,
110 "unable to open slave: %s: %d\n",
111 vf_netdev->name, ret);
112 }
113 return 0;
Hank Janssenfceaf242009-07-13 15:34:54 -0700114}
115
Hank Janssenfceaf242009-07-13 15:34:54 -0700116static int netvsc_close(struct net_device *net)
117{
Hank Janssenfceaf242009-07-13 15:34:54 -0700118 struct net_device_context *net_device_ctx = netdev_priv(net);
stephen hemminger0c195562017-08-01 19:58:53 -0700119 struct net_device *vf_netdev
120 = rtnl_dereference(net_device_ctx->vf_netdev);
stephen hemminger545a8e72017-03-22 14:51:00 -0700121 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700122 int ret;
stephen hemminger40975962017-06-08 16:21:19 -0700123 u32 aread, i, msec = 10, retry = 0, retry_max = 20;
Haiyang Zhang2de85302015-07-13 13:09:16 -0700124 struct vmbus_channel *chn;
Hank Janssenfceaf242009-07-13 15:34:54 -0700125
Haiyang Zhang0a282532012-02-02 07:17:59 +0000126 netif_tx_disable(net);
Hank Janssenfceaf242009-07-13 15:34:54 -0700127
Vitaly Kuznetsov2f5fa6c2016-06-03 17:51:00 +0200128 ret = rndis_filter_close(nvdev);
Haiyang Zhang2de85302015-07-13 13:09:16 -0700129 if (ret != 0) {
Hank Jansseneb335bc2011-03-29 13:58:48 -0700130 netdev_err(net, "unable to close device (ret %d).\n", ret);
Haiyang Zhang2de85302015-07-13 13:09:16 -0700131 return ret;
132 }
133
134 /* Ensure pending bytes in ring are read */
135 while (true) {
136 aread = 0;
137 for (i = 0; i < nvdev->num_chn; i++) {
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800138 chn = nvdev->chan_table[i].channel;
Haiyang Zhang2de85302015-07-13 13:09:16 -0700139 if (!chn)
140 continue;
141
stephen hemminger40975962017-06-08 16:21:19 -0700142 aread = hv_get_bytes_to_read(&chn->inbound);
Haiyang Zhang2de85302015-07-13 13:09:16 -0700143 if (aread)
144 break;
145
stephen hemminger40975962017-06-08 16:21:19 -0700146 aread = hv_get_bytes_to_read(&chn->outbound);
Haiyang Zhang2de85302015-07-13 13:09:16 -0700147 if (aread)
148 break;
149 }
150
151 retry++;
152 if (retry > retry_max || aread == 0)
153 break;
154
155 msleep(msec);
156
157 if (msec < 1000)
158 msec *= 2;
159 }
160
161 if (aread) {
162 netdev_err(net, "Ring buffer not empty after closing rndis\n");
163 ret = -ETIMEDOUT;
164 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700165
stephen hemminger0c195562017-08-01 19:58:53 -0700166 if (vf_netdev)
167 dev_close(vf_netdev);
168
Hank Janssenfceaf242009-07-13 15:34:54 -0700169 return ret;
170}
171
KY Srinivasan8a002512014-03-08 19:23:14 -0800172static void *init_ppi_data(struct rndis_message *msg, u32 ppi_size,
stephen hemminger89bb42b2017-08-09 17:46:08 -0700173 int pkt_type)
KY Srinivasan8a002512014-03-08 19:23:14 -0800174{
175 struct rndis_packet *rndis_pkt;
176 struct rndis_per_packet_info *ppi;
177
178 rndis_pkt = &msg->msg.pkt;
179 rndis_pkt->data_offset += ppi_size;
180
181 ppi = (struct rndis_per_packet_info *)((void *)rndis_pkt +
182 rndis_pkt->per_pkt_info_offset + rndis_pkt->per_pkt_info_len);
183
184 ppi->size = ppi_size;
185 ppi->type = pkt_type;
186 ppi->ppi_offset = sizeof(struct rndis_per_packet_info);
187
188 rndis_pkt->per_pkt_info_len += ppi_size;
189
190 return ppi;
191}
192
Haiyang Zhang4823eb22017-08-21 19:22:39 -0700193/* Azure hosts don't support non-TCP port numbers in hashing for fragmented
194 * packets. We can use ethtool to change UDP hash level when necessary.
Haiyang Zhangf72860a2017-04-12 11:45:18 -0700195 */
Haiyang Zhang4823eb22017-08-21 19:22:39 -0700196static inline u32 netvsc_get_hash(
197 struct sk_buff *skb,
198 const struct net_device_context *ndc)
Haiyang Zhangf72860a2017-04-12 11:45:18 -0700199{
200 struct flow_keys flow;
201 u32 hash;
202 static u32 hashrnd __read_mostly;
203
204 net_get_random_once(&hashrnd, sizeof(hashrnd));
205
206 if (!skb_flow_dissect_flow_keys(skb, &flow, 0))
207 return 0;
208
Haiyang Zhang4823eb22017-08-21 19:22:39 -0700209 if (flow.basic.ip_proto == IPPROTO_TCP ||
210 (flow.basic.ip_proto == IPPROTO_UDP &&
211 ((flow.basic.n_proto == htons(ETH_P_IP) && ndc->udp4_l4_hash) ||
212 (flow.basic.n_proto == htons(ETH_P_IPV6) &&
213 ndc->udp6_l4_hash)))) {
Haiyang Zhangf72860a2017-04-12 11:45:18 -0700214 return skb_get_hash(skb);
215 } else {
216 if (flow.basic.n_proto == htons(ETH_P_IP))
217 hash = jhash2((u32 *)&flow.addrs.v4addrs, 2, hashrnd);
218 else if (flow.basic.n_proto == htons(ETH_P_IPV6))
219 hash = jhash2((u32 *)&flow.addrs.v6addrs, 8, hashrnd);
220 else
221 hash = 0;
222
223 skb_set_hash(skb, hash, PKT_HASH_TYPE_L3);
224 }
225
226 return hash;
227}
228
Haiyang Zhang8db91f62017-04-12 11:35:05 -0700229static inline int netvsc_get_tx_queue(struct net_device *ndev,
230 struct sk_buff *skb, int old_idx)
231{
232 const struct net_device_context *ndc = netdev_priv(ndev);
233 struct sock *sk = skb->sk;
234 int q_idx;
235
Haiyang Zhang4823eb22017-08-21 19:22:39 -0700236 q_idx = ndc->tx_send_table[netvsc_get_hash(skb, ndc) &
Haiyang Zhang8db91f62017-04-12 11:35:05 -0700237 (VRSS_SEND_TAB_SIZE - 1)];
238
239 /* If queue index changed record the new value */
240 if (q_idx != old_idx &&
241 sk && sk_fullsock(sk) && rcu_access_pointer(sk->sk_dst_cache))
242 sk_tx_queue_set(sk, q_idx);
243
244 return q_idx;
245}
246
stephen hemmingerd8e18ee2017-01-24 13:06:05 -0800247/*
248 * Select queue for transmit.
249 *
250 * If a valid queue has already been assigned, then use that.
251 * Otherwise compute tx queue based on hash and the send table.
252 *
253 * This is basically similar to default (__netdev_pick_tx) with the added step
254 * of using the host send_table when no other queue has been assigned.
255 *
256 * TODO support XPS - but get_xps_queue not exported
257 */
stephen hemminger0c195562017-08-01 19:58:53 -0700258static u16 netvsc_pick_tx(struct net_device *ndev, struct sk_buff *skb)
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700259{
Haiyang Zhang8db91f62017-04-12 11:35:05 -0700260 int q_idx = sk_tx_queue_get(skb->sk);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700261
stephen hemminger0c195562017-08-01 19:58:53 -0700262 if (q_idx < 0 || skb->ooo_okay || q_idx >= ndev->real_num_tx_queues) {
Haiyang Zhang8db91f62017-04-12 11:35:05 -0700263 /* If forwarding a packet, we use the recorded queue when
264 * available for better cache locality.
265 */
266 if (skb_rx_queue_recorded(skb))
267 q_idx = skb_get_rx_queue(skb);
268 else
269 q_idx = netvsc_get_tx_queue(ndev, skb, q_idx);
stephen hemmingerd8e18ee2017-01-24 13:06:05 -0800270 }
271
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700272 return q_idx;
273}
274
stephen hemminger0c195562017-08-01 19:58:53 -0700275static u16 netvsc_select_queue(struct net_device *ndev, struct sk_buff *skb,
276 void *accel_priv,
277 select_queue_fallback_t fallback)
278{
279 struct net_device_context *ndc = netdev_priv(ndev);
280 struct net_device *vf_netdev;
281 u16 txq;
282
283 rcu_read_lock();
284 vf_netdev = rcu_dereference(ndc->vf_netdev);
285 if (vf_netdev) {
286 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
287 qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping;
288 } else {
289 txq = netvsc_pick_tx(ndev, skb);
290 }
291 rcu_read_unlock();
292
293 while (unlikely(txq >= ndev->real_num_tx_queues))
294 txq -= ndev->real_num_tx_queues;
295
296 return txq;
297}
298
KY Srinivasan54a73572014-03-08 19:23:13 -0800299static u32 fill_pg_buf(struct page *page, u32 offset, u32 len,
stephen hemminger89bb42b2017-08-09 17:46:08 -0700300 struct hv_page_buffer *pb)
KY Srinivasan54a73572014-03-08 19:23:13 -0800301{
302 int j = 0;
303
304 /* Deal with compund pages by ignoring unused part
305 * of the page.
306 */
307 page += (offset >> PAGE_SHIFT);
308 offset &= ~PAGE_MASK;
309
310 while (len > 0) {
311 unsigned long bytes;
312
313 bytes = PAGE_SIZE - offset;
314 if (bytes > len)
315 bytes = len;
316 pb[j].pfn = page_to_pfn(page);
317 pb[j].offset = offset;
318 pb[j].len = bytes;
319
320 offset += bytes;
321 len -= bytes;
322
323 if (offset == PAGE_SIZE && len) {
324 page++;
325 offset = 0;
326 j++;
327 }
328 }
329
330 return j + 1;
331}
332
KY Srinivasan8a002512014-03-08 19:23:14 -0800333static u32 init_page_array(void *hdr, u32 len, struct sk_buff *skb,
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800334 struct hv_netvsc_packet *packet,
stephen hemminger02b6de02017-07-28 08:59:44 -0700335 struct hv_page_buffer *pb)
KY Srinivasan54a73572014-03-08 19:23:13 -0800336{
337 u32 slots_used = 0;
338 char *data = skb->data;
339 int frags = skb_shinfo(skb)->nr_frags;
340 int i;
341
342 /* The packet is laid out thus:
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700343 * 1. hdr: RNDIS header and PPI
KY Srinivasan54a73572014-03-08 19:23:13 -0800344 * 2. skb linear data
345 * 3. skb fragment data
346 */
stephen hemmingerea5a32c2017-08-09 17:46:10 -0700347 slots_used += fill_pg_buf(virt_to_page(hdr),
348 offset_in_page(hdr),
349 len, &pb[slots_used]);
KY Srinivasan54a73572014-03-08 19:23:13 -0800350
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700351 packet->rmsg_size = len;
352 packet->rmsg_pgcnt = slots_used;
353
KY Srinivasan54a73572014-03-08 19:23:13 -0800354 slots_used += fill_pg_buf(virt_to_page(data),
355 offset_in_page(data),
356 skb_headlen(skb), &pb[slots_used]);
357
358 for (i = 0; i < frags; i++) {
359 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
360
361 slots_used += fill_pg_buf(skb_frag_page(frag),
362 frag->page_offset,
363 skb_frag_size(frag), &pb[slots_used]);
364 }
KY Srinivasan8a002512014-03-08 19:23:14 -0800365 return slots_used;
KY Srinivasan54a73572014-03-08 19:23:13 -0800366}
367
stephen hemminger80d887d2017-07-24 21:03:19 -0700368static int count_skb_frag_slots(struct sk_buff *skb)
KY Srinivasan54a73572014-03-08 19:23:13 -0800369{
stephen hemminger80d887d2017-07-24 21:03:19 -0700370 int i, frags = skb_shinfo(skb)->nr_frags;
371 int pages = 0;
372
373 for (i = 0; i < frags; i++) {
374 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
375 unsigned long size = skb_frag_size(frag);
376 unsigned long offset = frag->page_offset;
377
378 /* Skip unused frames from start of page */
379 offset &= ~PAGE_MASK;
380 pages += PFN_UP(offset + size);
381 }
382 return pages;
383}
384
385static int netvsc_get_slots(struct sk_buff *skb)
386{
387 char *data = skb->data;
388 unsigned int offset = offset_in_page(data);
389 unsigned int len = skb_headlen(skb);
390 int slots;
391 int frag_slots;
392
393 slots = DIV_ROUND_UP(offset + len, PAGE_SIZE);
394 frag_slots = count_skb_frag_slots(skb);
395 return slots + frag_slots;
KY Srinivasan54a73572014-03-08 19:23:13 -0800396}
397
stephen hemminger23312a32017-01-24 13:05:59 -0800398static u32 net_checksum_info(struct sk_buff *skb)
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800399{
stephen hemminger23312a32017-01-24 13:05:59 -0800400 if (skb->protocol == htons(ETH_P_IP)) {
401 struct iphdr *ip = ip_hdr(skb);
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800402
stephen hemminger23312a32017-01-24 13:05:59 -0800403 if (ip->protocol == IPPROTO_TCP)
404 return TRANSPORT_INFO_IPV4_TCP;
405 else if (ip->protocol == IPPROTO_UDP)
406 return TRANSPORT_INFO_IPV4_UDP;
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800407 } else {
stephen hemminger23312a32017-01-24 13:05:59 -0800408 struct ipv6hdr *ip6 = ipv6_hdr(skb);
409
410 if (ip6->nexthdr == IPPROTO_TCP)
411 return TRANSPORT_INFO_IPV6_TCP;
Mohammed Gamal37b9dfa2017-07-24 10:57:26 -0700412 else if (ip6->nexthdr == IPPROTO_UDP)
stephen hemminger23312a32017-01-24 13:05:59 -0800413 return TRANSPORT_INFO_IPV6_UDP;
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800414 }
415
stephen hemminger23312a32017-01-24 13:05:59 -0800416 return TRANSPORT_INFO_NOT_IP;
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800417}
418
stephen hemminger0c195562017-08-01 19:58:53 -0700419/* Send skb on the slave VF device. */
420static int netvsc_vf_xmit(struct net_device *net, struct net_device *vf_netdev,
421 struct sk_buff *skb)
422{
423 struct net_device_context *ndev_ctx = netdev_priv(net);
424 unsigned int len = skb->len;
425 int rc;
426
427 skb->dev = vf_netdev;
428 skb->queue_mapping = qdisc_skb_cb(skb)->slave_dev_queue_mapping;
429
430 rc = dev_queue_xmit(skb);
431 if (likely(rc == NET_XMIT_SUCCESS || rc == NET_XMIT_CN)) {
432 struct netvsc_vf_pcpu_stats *pcpu_stats
433 = this_cpu_ptr(ndev_ctx->vf_stats);
434
435 u64_stats_update_begin(&pcpu_stats->syncp);
436 pcpu_stats->tx_packets++;
437 pcpu_stats->tx_bytes += len;
438 u64_stats_update_end(&pcpu_stats->syncp);
439 } else {
440 this_cpu_inc(ndev_ctx->vf_stats->tx_dropped);
441 }
442
443 return rc;
444}
445
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700446static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
Hank Janssenfceaf242009-07-13 15:34:54 -0700447{
Hank Janssenfceaf242009-07-13 15:34:54 -0700448 struct net_device_context *net_device_ctx = netdev_priv(net);
Vitaly Kuznetsov981a1bd2015-04-08 17:54:05 +0200449 struct hv_netvsc_packet *packet = NULL;
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700450 int ret;
KY Srinivasan8a002512014-03-08 19:23:14 -0800451 unsigned int num_data_pgs;
452 struct rndis_message *rndis_msg;
453 struct rndis_packet *rndis_pkt;
stephen hemminger0c195562017-08-01 19:58:53 -0700454 struct net_device *vf_netdev;
KY Srinivasan8a002512014-03-08 19:23:14 -0800455 u32 rndis_msg_size;
KY Srinivasan8a002512014-03-08 19:23:14 -0800456 struct rndis_per_packet_info *ppi;
Haiyang Zhang307f0992014-05-21 12:55:39 -0700457 u32 hash;
stephen hemminger02b6de02017-07-28 08:59:44 -0700458 struct hv_page_buffer pb[MAX_PAGE_BUFFER_COUNT];
Hank Janssenfceaf242009-07-13 15:34:54 -0700459
stephen hemminger0c195562017-08-01 19:58:53 -0700460 /* if VF is present and up then redirect packets
461 * already called with rcu_read_lock_bh
462 */
463 vf_netdev = rcu_dereference_bh(net_device_ctx->vf_netdev);
464 if (vf_netdev && netif_running(vf_netdev) &&
465 !netpoll_tx_running(net))
466 return netvsc_vf_xmit(net, vf_netdev, skb);
467
stephen hemminger80d887d2017-07-24 21:03:19 -0700468 /* We will atmost need two pages to describe the rndis
469 * header. We can only transmit MAX_PAGE_BUFFER_COUNT number
Vitaly Kuznetsove88f7e02015-04-08 17:54:06 +0200470 * of pages in a single packet. If skb is scattered around
471 * more pages we try linearizing it.
KY Srinivasan54a73572014-03-08 19:23:13 -0800472 */
stephen hemminger80d887d2017-07-24 21:03:19 -0700473
474 num_data_pgs = netvsc_get_slots(skb) + 2;
475
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700476 if (unlikely(num_data_pgs > MAX_PAGE_BUFFER_COUNT)) {
Stephen Hemminger4323b472016-08-23 12:17:57 -0700477 ++net_device_ctx->eth_stats.tx_scattered;
478
479 if (skb_linearize(skb))
480 goto no_memory;
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700481
stephen hemminger80d887d2017-07-24 21:03:19 -0700482 num_data_pgs = netvsc_get_slots(skb) + 2;
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700483 if (num_data_pgs > MAX_PAGE_BUFFER_COUNT) {
Stephen Hemminger4323b472016-08-23 12:17:57 -0700484 ++net_device_ctx->eth_stats.tx_too_big;
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700485 goto drop;
486 }
KY Srinivasan54a73572014-03-08 19:23:13 -0800487 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700488
KY Srinivasanc0eb4542015-12-01 16:43:10 -0800489 /*
490 * Place the rndis header in the skb head room and
491 * the skb->cb will be used for hv_netvsc_packet
492 * structure.
493 */
494 ret = skb_cow_head(skb, RNDIS_AND_PPI_SIZE);
Stephen Hemminger4323b472016-08-23 12:17:57 -0700495 if (ret)
496 goto no_memory;
497
KY Srinivasanc0eb4542015-12-01 16:43:10 -0800498 /* Use the skb control buffer for building up the packet */
499 BUILD_BUG_ON(sizeof(struct hv_netvsc_packet) >
500 FIELD_SIZEOF(struct sk_buff, cb));
501 packet = (struct hv_netvsc_packet *)skb->cb;
Hank Janssenfceaf242009-07-13 15:34:54 -0700502
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700503 packet->q_idx = skb_get_queue_mapping(skb);
504
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800505 packet->total_data_buflen = skb->len;
stephen hemminger793e3952017-01-24 13:06:12 -0800506 packet->total_bytes = skb->len;
507 packet->total_packets = 1;
Hank Janssenfceaf242009-07-13 15:34:54 -0700508
KY Srinivasanc0eb4542015-12-01 16:43:10 -0800509 rndis_msg = (struct rndis_message *)skb->head;
KY Srinivasanb08cc792015-03-29 21:08:42 -0700510
KY Srinivasan24476762015-12-01 16:43:06 -0800511 memset(rndis_msg, 0, RNDIS_AND_PPI_SIZE);
Hank Janssenfceaf242009-07-13 15:34:54 -0700512
KY Srinivasan8a002512014-03-08 19:23:14 -0800513 /* Add the rndis header */
KY Srinivasan8a002512014-03-08 19:23:14 -0800514 rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET;
515 rndis_msg->msg_len = packet->total_data_buflen;
516 rndis_pkt = &rndis_msg->msg.pkt;
517 rndis_pkt->data_offset = sizeof(struct rndis_packet);
518 rndis_pkt->data_len = packet->total_data_buflen;
519 rndis_pkt->per_pkt_info_offset = sizeof(struct rndis_packet);
520
521 rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet);
522
Haiyang Zhang307f0992014-05-21 12:55:39 -0700523 hash = skb_get_hash_raw(skb);
524 if (hash != 0 && net->real_num_tx_queues > 1) {
525 rndis_msg_size += NDIS_HASH_PPI_SIZE;
526 ppi = init_ppi_data(rndis_msg, NDIS_HASH_PPI_SIZE,
527 NBL_HASH_VALUE);
528 *(u32 *)((void *)ppi + ppi->ppi_offset) = hash;
529 }
530
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700531 if (skb_vlan_tag_present(skb)) {
KY Srinivasan8a002512014-03-08 19:23:14 -0800532 struct ndis_pkt_8021q_info *vlan;
533
534 rndis_msg_size += NDIS_VLAN_PPI_SIZE;
535 ppi = init_ppi_data(rndis_msg, NDIS_VLAN_PPI_SIZE,
stephen hemminger00f50242017-08-09 17:46:09 -0700536 IEEE_8021Q_INFO);
537
538 vlan = (void *)ppi + ppi->ppi_offset;
KY Srinivasan760d1e32015-12-01 16:43:19 -0800539 vlan->vlanid = skb->vlan_tci & VLAN_VID_MASK;
540 vlan->pri = (skb->vlan_tci & VLAN_PRIO_MASK) >>
KY Srinivasan8a002512014-03-08 19:23:14 -0800541 VLAN_PRIO_SHIFT;
542 }
543
stephen hemminger23312a32017-01-24 13:05:59 -0800544 if (skb_is_gso(skb)) {
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700545 struct ndis_tcp_lso_info *lso_info;
546
547 rndis_msg_size += NDIS_LSO_PPI_SIZE;
548 ppi = init_ppi_data(rndis_msg, NDIS_LSO_PPI_SIZE,
549 TCP_LARGESEND_PKTINFO);
550
stephen hemminger00f50242017-08-09 17:46:09 -0700551 lso_info = (void *)ppi + ppi->ppi_offset;
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700552
553 lso_info->lso_v2_transmit.type = NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
stephen hemminger23312a32017-01-24 13:05:59 -0800554 if (skb->protocol == htons(ETH_P_IP)) {
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700555 lso_info->lso_v2_transmit.ip_version =
556 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
557 ip_hdr(skb)->tot_len = 0;
558 ip_hdr(skb)->check = 0;
559 tcp_hdr(skb)->check =
560 ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
561 ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
562 } else {
563 lso_info->lso_v2_transmit.ip_version =
564 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
565 ipv6_hdr(skb)->payload_len = 0;
566 tcp_hdr(skb)->check =
567 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
568 &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
569 }
stephen hemminger23312a32017-01-24 13:05:59 -0800570 lso_info->lso_v2_transmit.tcp_header_offset = skb_transport_offset(skb);
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700571 lso_info->lso_v2_transmit.mss = skb_shinfo(skb)->gso_size;
stephen hemmingerad19bc82016-10-11 14:03:07 -0700572 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
stephen hemminger23312a32017-01-24 13:05:59 -0800573 if (net_checksum_info(skb) & net_device_ctx->tx_checksum_mask) {
574 struct ndis_tcp_ip_checksum_info *csum_info;
575
stephen hemmingerad19bc82016-10-11 14:03:07 -0700576 rndis_msg_size += NDIS_CSUM_PPI_SIZE;
577 ppi = init_ppi_data(rndis_msg, NDIS_CSUM_PPI_SIZE,
578 TCPIP_CHKSUM_PKTINFO);
579
580 csum_info = (struct ndis_tcp_ip_checksum_info *)((void *)ppi +
581 ppi->ppi_offset);
582
stephen hemminger23312a32017-01-24 13:05:59 -0800583 csum_info->transmit.tcp_header_offset = skb_transport_offset(skb);
584
585 if (skb->protocol == htons(ETH_P_IP)) {
stephen hemmingerad19bc82016-10-11 14:03:07 -0700586 csum_info->transmit.is_ipv4 = 1;
stephen hemminger23312a32017-01-24 13:05:59 -0800587
588 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
589 csum_info->transmit.tcp_checksum = 1;
590 else
591 csum_info->transmit.udp_checksum = 1;
592 } else {
stephen hemmingerad19bc82016-10-11 14:03:07 -0700593 csum_info->transmit.is_ipv6 = 1;
594
stephen hemminger23312a32017-01-24 13:05:59 -0800595 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
596 csum_info->transmit.tcp_checksum = 1;
597 else
598 csum_info->transmit.udp_checksum = 1;
599 }
stephen hemmingerad19bc82016-10-11 14:03:07 -0700600 } else {
stephen hemminger23312a32017-01-24 13:05:59 -0800601 /* Can't do offload of this type of checksum */
stephen hemmingerad19bc82016-10-11 14:03:07 -0700602 if (skb_checksum_help(skb))
603 goto drop;
604 }
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700605 }
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800606
KY Srinivasan8a002512014-03-08 19:23:14 -0800607 /* Start filling in the page buffers with the rndis hdr */
608 rndis_msg->msg_len += rndis_msg_size;
Haiyang Zhang942396b2014-10-22 13:47:18 -0700609 packet->total_data_buflen = rndis_msg->msg_len;
KY Srinivasan8a002512014-03-08 19:23:14 -0800610 packet->page_buf_cnt = init_page_array(rndis_msg, rndis_msg_size,
stephen hemminger02b6de02017-07-28 08:59:44 -0700611 skb, packet, pb);
KY Srinivasan8a002512014-03-08 19:23:14 -0800612
sixiao@microsoft.com76d13b52016-02-17 16:43:59 -0800613 /* timestamp packet in software */
614 skb_tx_timestamp(skb);
stephen hemminger2a926f72017-07-19 11:53:17 -0700615
stephen hemminger02b6de02017-07-28 08:59:44 -0700616 ret = netvsc_send(net_device_ctx, packet, rndis_msg, pb, skb);
stephen hemminger793e3952017-01-24 13:06:12 -0800617 if (likely(ret == 0))
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700618 return NETDEV_TX_OK;
Stephen Hemminger4323b472016-08-23 12:17:57 -0700619
620 if (ret == -EAGAIN) {
621 ++net_device_ctx->eth_stats.tx_busy;
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700622 return NETDEV_TX_BUSY;
Stephen Hemminger4323b472016-08-23 12:17:57 -0700623 }
624
625 if (ret == -ENOSPC)
626 ++net_device_ctx->eth_stats.tx_no_space;
Hank Janssenfceaf242009-07-13 15:34:54 -0700627
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700628drop:
629 dev_kfree_skb_any(skb);
630 net->stats.tx_dropped++;
631
632 return NETDEV_TX_OK;
Stephen Hemminger4323b472016-08-23 12:17:57 -0700633
634no_memory:
635 ++net_device_ctx->eth_stats.tx_no_memory;
636 goto drop;
Hank Janssenfceaf242009-07-13 15:34:54 -0700637}
stephen hemminger89bb42b2017-08-09 17:46:08 -0700638
Hank Janssen3e189512010-03-04 22:11:00 +0000639/*
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700640 * netvsc_linkstatus_callback - Link up/down notification
641 */
K. Y. Srinivasan90ef1172011-05-12 19:34:50 -0700642void netvsc_linkstatus_callback(struct hv_device *device_obj,
Haiyang Zhang3a494e72014-06-19 18:34:36 -0700643 struct rndis_message *resp)
Hank Janssenfceaf242009-07-13 15:34:54 -0700644{
Haiyang Zhang3a494e72014-06-19 18:34:36 -0700645 struct rndis_indicate_status *indicate = &resp->msg.indicate_status;
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -0700646 struct net_device *net;
Haiyang Zhangc996edc2011-04-06 15:18:00 -0700647 struct net_device_context *ndev_ctx;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +0100648 struct netvsc_reconfig *event;
649 unsigned long flags;
650
Haiyang Zhang7f5d5af2016-08-04 10:42:15 -0700651 net = hv_get_drvdata(device_obj);
652
653 if (!net)
654 return;
655
656 ndev_ctx = netdev_priv(net);
657
658 /* Update the physical link speed when changing to another vSwitch */
659 if (indicate->status == RNDIS_STATUS_LINK_SPEED_CHANGE) {
660 u32 speed;
661
stephen hemminger89bb42b2017-08-09 17:46:08 -0700662 speed = *(u32 *)((void *)indicate
663 + indicate->status_buf_offset) / 10000;
Haiyang Zhang7f5d5af2016-08-04 10:42:15 -0700664 ndev_ctx->speed = speed;
665 return;
666 }
667
668 /* Handle these link change statuses below */
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +0100669 if (indicate->status != RNDIS_STATUS_NETWORK_CHANGE &&
670 indicate->status != RNDIS_STATUS_MEDIA_CONNECT &&
671 indicate->status != RNDIS_STATUS_MEDIA_DISCONNECT)
672 return;
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -0700673
Haiyang Zhang7f5d5af2016-08-04 10:42:15 -0700674 if (net->reg_state != NETREG_REGISTERED)
Hank Janssenfceaf242009-07-13 15:34:54 -0700675 return;
Hank Janssenfceaf242009-07-13 15:34:54 -0700676
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +0100677 event = kzalloc(sizeof(*event), GFP_ATOMIC);
678 if (!event)
679 return;
680 event->event = indicate->status;
681
682 spin_lock_irqsave(&ndev_ctx->lock, flags);
683 list_add_tail(&event->list, &ndev_ctx->reconfig_events);
684 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
685
686 schedule_delayed_work(&ndev_ctx->dwork, 0);
Hank Janssenfceaf242009-07-13 15:34:54 -0700687}
688
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700689static struct sk_buff *netvsc_alloc_recv_skb(struct net_device *net,
stephen hemmingere91e7dd2017-02-27 10:26:51 -0800690 struct napi_struct *napi,
stephen hemmingerdc54a082017-01-24 13:06:08 -0800691 const struct ndis_tcp_ip_checksum_info *csum_info,
692 const struct ndis_pkt_8021q_info *vlan,
693 void *data, u32 buflen)
Hank Janssenfceaf242009-07-13 15:34:54 -0700694{
Hank Janssenfceaf242009-07-13 15:34:54 -0700695 struct sk_buff *skb;
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -0700696
stephen hemmingere91e7dd2017-02-27 10:26:51 -0800697 skb = napi_alloc_skb(napi, buflen);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700698 if (!skb)
699 return skb;
Hank Janssenfceaf242009-07-13 15:34:54 -0700700
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700701 /*
702 * Copy to skb. This copy is needed here since the memory pointed by
703 * hv_netvsc_packet cannot be deallocated
704 */
Johannes Berg59ae1d12017-06-16 14:29:20 +0200705 skb_put_data(skb, data, buflen);
Hank Janssenfceaf242009-07-13 15:34:54 -0700706
707 skb->protocol = eth_type_trans(skb, net);
Stephen Hemmingere52fed72016-10-23 21:32:47 -0700708
709 /* skb is already created with CHECKSUM_NONE */
710 skb_checksum_none_assert(skb);
711
712 /*
713 * In Linux, the IP checksum is always checked.
714 * Do L4 checksum offload if enabled and present.
715 */
716 if (csum_info && (net->features & NETIF_F_RXCSUM)) {
717 if (csum_info->receive.tcp_checksum_succeeded ||
718 csum_info->receive.udp_checksum_succeeded)
KY Srinivasane3d605e2014-03-08 19:23:16 -0800719 skb->ip_summed = CHECKSUM_UNNECESSARY;
KY Srinivasane3d605e2014-03-08 19:23:16 -0800720 }
721
stephen hemmingerdc54a082017-01-24 13:06:08 -0800722 if (vlan) {
723 u16 vlan_tci = vlan->vlanid | (vlan->pri << VLAN_PRIO_SHIFT);
724
Haiyang Zhang93725cb2013-06-17 15:36:49 -0700725 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
KY Srinivasan760d1e32015-12-01 16:43:19 -0800726 vlan_tci);
stephen hemmingerdc54a082017-01-24 13:06:08 -0800727 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700728
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700729 return skb;
730}
731
732/*
733 * netvsc_recv_callback - Callback when we receive a packet from the
734 * "wire" on the specified device.
735 */
stephen hemmingerdc54a082017-01-24 13:06:08 -0800736int netvsc_recv_callback(struct net_device *net,
737 struct vmbus_channel *channel,
738 void *data, u32 len,
739 const struct ndis_tcp_ip_checksum_info *csum_info,
740 const struct ndis_pkt_8021q_info *vlan)
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700741{
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200742 struct net_device_context *net_device_ctx = netdev_priv(net);
stephen hemminger545a8e72017-03-22 14:51:00 -0700743 struct netvsc_device *net_device;
stephen hemminger742fe542017-02-27 10:26:50 -0800744 u16 q_idx = channel->offermsg.offer.sub_channel_index;
stephen hemminger545a8e72017-03-22 14:51:00 -0700745 struct netvsc_channel *nvchan;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700746 struct sk_buff *skb;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700747 struct netvsc_stats *rx_stats;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700748
Stephen Hemminger9cbcc422016-09-22 16:56:34 -0700749 if (net->reg_state != NETREG_REGISTERED)
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700750 return NVSP_STAT_FAIL;
751
stephen hemminger0719e722017-01-11 09:16:32 -0800752 rcu_read_lock();
stephen hemminger545a8e72017-03-22 14:51:00 -0700753 net_device = rcu_dereference(net_device_ctx->nvdev);
754 if (unlikely(!net_device))
755 goto drop;
756
757 nvchan = &net_device->chan_table[q_idx];
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700758
759 /* Allocate a skb - TODO direct I/O to pages? */
stephen hemmingere91e7dd2017-02-27 10:26:51 -0800760 skb = netvsc_alloc_recv_skb(net, &nvchan->napi,
761 csum_info, vlan, data, len);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700762 if (unlikely(!skb)) {
stephen hemminger545a8e72017-03-22 14:51:00 -0700763drop:
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700764 ++net->stats.rx_dropped;
stephen hemminger0719e722017-01-11 09:16:32 -0800765 rcu_read_unlock();
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700766 return NVSP_STAT_FAIL;
767 }
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700768
stephen hemminger0c195562017-08-01 19:58:53 -0700769 skb_record_rx_queue(skb, q_idx);
Stephen Hemminger9cbcc422016-09-22 16:56:34 -0700770
771 /*
772 * Even if injecting the packet, record the statistics
773 * on the synthetic device because modifying the VF device
774 * statistics will not work correctly.
775 */
stephen hemminger742fe542017-02-27 10:26:50 -0800776 rx_stats = &nvchan->rx_stats;
sixiao@microsoft.com4b02b582015-05-15 02:33:03 -0700777 u64_stats_update_begin(&rx_stats->syncp);
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700778 rx_stats->packets++;
stephen hemmingerdc54a082017-01-24 13:06:08 -0800779 rx_stats->bytes += len;
Stephen Hemmingerf7ad75b2016-09-22 16:56:35 -0700780
781 if (skb->pkt_type == PACKET_BROADCAST)
782 ++rx_stats->broadcast;
783 else if (skb->pkt_type == PACKET_MULTICAST)
784 ++rx_stats->multicast;
sixiao@microsoft.com4b02b582015-05-15 02:33:03 -0700785 u64_stats_update_end(&rx_stats->syncp);
Stephen Hemminger9495c282010-03-09 17:42:17 -0800786
stephen hemminger742fe542017-02-27 10:26:50 -0800787 napi_gro_receive(&nvchan->napi, skb);
stephen hemminger0719e722017-01-11 09:16:32 -0800788 rcu_read_unlock();
Hank Janssenfceaf242009-07-13 15:34:54 -0700789
Hank Janssenfceaf242009-07-13 15:34:54 -0700790 return 0;
791}
792
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -0700793static void netvsc_get_drvinfo(struct net_device *net,
794 struct ethtool_drvinfo *info)
795{
Jiri Pirko7826d432013-01-06 00:44:26 +0000796 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
Jiri Pirko7826d432013-01-06 00:44:26 +0000797 strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -0700798}
799
Andrew Schwartzmeyer59995372015-02-26 16:27:14 -0800800static void netvsc_get_channels(struct net_device *net,
801 struct ethtool_channels *channel)
802{
803 struct net_device_context *net_device_ctx = netdev_priv(net);
stephen hemminger545a8e72017-03-22 14:51:00 -0700804 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
Andrew Schwartzmeyer59995372015-02-26 16:27:14 -0800805
806 if (nvdev) {
807 channel->max_combined = nvdev->max_chn;
808 channel->combined_count = nvdev->num_chn;
809 }
810}
811
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700812static int netvsc_set_channels(struct net_device *net,
813 struct ethtool_channels *channels)
814{
815 struct net_device_context *net_device_ctx = netdev_priv(net);
816 struct hv_device *dev = net_device_ctx->device_ctx;
stephen hemminger545a8e72017-03-22 14:51:00 -0700817 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
stephen hemminger7ca45932017-07-24 10:57:28 -0700818 unsigned int orig, count = channels->combined_count;
819 struct netvsc_device_info device_info;
stephen hemmingerea383bf2017-07-19 11:53:15 -0700820 bool was_opened;
stephen hemminger7ca45932017-07-24 10:57:28 -0700821 int ret = 0;
stephen hemminger2b018882017-01-24 13:06:03 -0800822
823 /* We do not support separate count for rx, tx, or other */
824 if (count == 0 ||
825 channels->rx_count || channels->tx_count || channels->other_count)
826 return -EINVAL;
827
Arnd Bergmannb92b7d32017-06-22 00:16:37 +0200828 if (count > net->num_tx_queues || count > VRSS_CHANNEL_MAX)
stephen hemminger2b018882017-01-24 13:06:03 -0800829 return -EINVAL;
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700830
stephen hemmingera0be4502017-03-22 14:51:01 -0700831 if (!nvdev || nvdev->destroy)
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700832 return -ENODEV;
833
stephen hemminger2b018882017-01-24 13:06:03 -0800834 if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5)
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700835 return -EINVAL;
836
stephen hemminger2b018882017-01-24 13:06:03 -0800837 if (count > nvdev->max_chn)
838 return -EINVAL;
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700839
stephen hemminger7ca45932017-07-24 10:57:28 -0700840 orig = nvdev->num_chn;
stephen hemmingerea383bf2017-07-19 11:53:15 -0700841 was_opened = rndis_filter_opened(nvdev);
842 if (was_opened)
843 rndis_filter_close(nvdev);
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700844
stephen hemminger7ca45932017-07-24 10:57:28 -0700845 memset(&device_info, 0, sizeof(device_info));
846 device_info.num_chn = count;
847 device_info.ring_size = ring_size;
stephen hemminger8b532792017-08-09 17:46:11 -0700848 device_info.send_sections = nvdev->send_section_cnt;
849 device_info.recv_sections = nvdev->recv_section_cnt;
850
851 rndis_filter_device_remove(dev, nvdev);
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700852
stephen hemminger7ca45932017-07-24 10:57:28 -0700853 nvdev = rndis_filter_device_add(dev, &device_info);
854 if (!IS_ERR(nvdev)) {
855 netif_set_real_num_tx_queues(net, nvdev->num_chn);
856 netif_set_real_num_rx_queues(net, nvdev->num_chn);
stephen hemminger7ca45932017-07-24 10:57:28 -0700857 } else {
stephen hemmingerd6aac1f2017-07-28 08:59:41 -0700858 ret = PTR_ERR(nvdev);
stephen hemminger7ca45932017-07-24 10:57:28 -0700859 device_info.num_chn = orig;
stephen hemminger68d715f2017-08-09 17:46:06 -0700860 nvdev = rndis_filter_device_add(dev, &device_info);
861
862 if (IS_ERR(nvdev)) {
863 netdev_err(net, "restoring channel setting failed: %ld\n",
864 PTR_ERR(nvdev));
865 return ret;
866 }
stephen hemminger7ca45932017-07-24 10:57:28 -0700867 }
868
stephen hemmingerea383bf2017-07-19 11:53:15 -0700869 if (was_opened)
870 rndis_filter_open(nvdev);
stephen hemminger163891d2017-03-22 14:50:58 -0700871
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +0200872 /* We may have missed link change notifications */
stephen hemminger1b019942017-07-19 11:53:12 -0700873 net_device_ctx->last_reconfig = 0;
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +0200874 schedule_delayed_work(&net_device_ctx->dwork, 0);
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700875
876 return ret;
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700877}
878
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100879static bool
880netvsc_validate_ethtool_ss_cmd(const struct ethtool_link_ksettings *cmd)
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800881{
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100882 struct ethtool_link_ksettings diff1 = *cmd;
883 struct ethtool_link_ksettings diff2 = {};
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800884
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100885 diff1.base.speed = 0;
886 diff1.base.duplex = 0;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800887 /* advertising and cmd are usually set */
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100888 ethtool_link_ksettings_zero_link_mode(&diff1, advertising);
889 diff1.base.cmd = 0;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800890 /* We set port to PORT_OTHER */
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100891 diff2.base.port = PORT_OTHER;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800892
893 return !memcmp(&diff1, &diff2, sizeof(diff1));
894}
895
896static void netvsc_init_settings(struct net_device *dev)
897{
898 struct net_device_context *ndc = netdev_priv(dev);
899
Haiyang Zhang4823eb22017-08-21 19:22:39 -0700900 ndc->udp4_l4_hash = true;
901 ndc->udp6_l4_hash = true;
902
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800903 ndc->speed = SPEED_UNKNOWN;
Simon Xiaof3c9d40e2017-04-14 14:42:58 -0700904 ndc->duplex = DUPLEX_FULL;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800905}
906
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100907static int netvsc_get_link_ksettings(struct net_device *dev,
908 struct ethtool_link_ksettings *cmd)
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800909{
910 struct net_device_context *ndc = netdev_priv(dev);
911
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100912 cmd->base.speed = ndc->speed;
913 cmd->base.duplex = ndc->duplex;
914 cmd->base.port = PORT_OTHER;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800915
916 return 0;
917}
918
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100919static int netvsc_set_link_ksettings(struct net_device *dev,
920 const struct ethtool_link_ksettings *cmd)
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800921{
922 struct net_device_context *ndc = netdev_priv(dev);
923 u32 speed;
924
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100925 speed = cmd->base.speed;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800926 if (!ethtool_validate_speed(speed) ||
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100927 !ethtool_validate_duplex(cmd->base.duplex) ||
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800928 !netvsc_validate_ethtool_ss_cmd(cmd))
929 return -EINVAL;
930
931 ndc->speed = speed;
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100932 ndc->duplex = cmd->base.duplex;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800933
934 return 0;
935}
936
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800937static int netvsc_change_mtu(struct net_device *ndev, int mtu)
938{
939 struct net_device_context *ndevctx = netdev_priv(ndev);
stephen hemminger0c195562017-08-01 19:58:53 -0700940 struct net_device *vf_netdev = rtnl_dereference(ndevctx->vf_netdev);
stephen hemminger545a8e72017-03-22 14:51:00 -0700941 struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200942 struct hv_device *hdev = ndevctx->device_ctx;
stephen hemminger9749fed2017-07-19 11:53:16 -0700943 int orig_mtu = ndev->mtu;
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800944 struct netvsc_device_info device_info;
stephen hemmingerea383bf2017-07-19 11:53:15 -0700945 bool was_opened;
stephen hemminger9749fed2017-07-19 11:53:16 -0700946 int ret = 0;
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800947
stephen hemmingera0be4502017-03-22 14:51:01 -0700948 if (!nvdev || nvdev->destroy)
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800949 return -ENODEV;
950
stephen hemminger0c195562017-08-01 19:58:53 -0700951 /* Change MTU of underlying VF netdev first. */
952 if (vf_netdev) {
953 ret = dev_set_mtu(vf_netdev, mtu);
954 if (ret)
955 return ret;
956 }
957
stephen hemmingerea383bf2017-07-19 11:53:15 -0700958 netif_device_detach(ndev);
959 was_opened = rndis_filter_opened(nvdev);
960 if (was_opened)
961 rndis_filter_close(nvdev);
Haiyang Zhang2de85302015-07-13 13:09:16 -0700962
Andrew Schwartzmeyer8ebdcc52015-08-11 17:14:31 -0700963 memset(&device_info, 0, sizeof(device_info));
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800964 device_info.ring_size = ring_size;
stephen hemminger2b018882017-01-24 13:06:03 -0800965 device_info.num_chn = nvdev->num_chn;
stephen hemminger8b532792017-08-09 17:46:11 -0700966 device_info.send_sections = nvdev->send_section_cnt;
967 device_info.recv_sections = nvdev->recv_section_cnt;
Dexuan Cui152669b2017-03-02 13:00:53 +0000968
Dexuan Cui152669b2017-03-02 13:00:53 +0000969 rndis_filter_device_remove(hdev, nvdev);
970
Dexuan Cui152669b2017-03-02 13:00:53 +0000971 ndev->mtu = mtu;
972
stephen hemminger9749fed2017-07-19 11:53:16 -0700973 nvdev = rndis_filter_device_add(hdev, &device_info);
974 if (IS_ERR(nvdev)) {
975 ret = PTR_ERR(nvdev);
976
977 /* Attempt rollback to original MTU */
978 ndev->mtu = orig_mtu;
stephen hemminger68d715f2017-08-09 17:46:06 -0700979 nvdev = rndis_filter_device_add(hdev, &device_info);
stephen hemminger0c195562017-08-01 19:58:53 -0700980
981 if (vf_netdev)
982 dev_set_mtu(vf_netdev, orig_mtu);
stephen hemminger68d715f2017-08-09 17:46:06 -0700983
984 if (IS_ERR(nvdev)) {
985 netdev_err(ndev, "restoring mtu failed: %ld\n",
986 PTR_ERR(nvdev));
987 return ret;
988 }
stephen hemminger9749fed2017-07-19 11:53:16 -0700989 }
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800990
stephen hemmingerea383bf2017-07-19 11:53:15 -0700991 if (was_opened)
992 rndis_filter_open(nvdev);
993
994 netif_device_attach(ndev);
stephen hemminger163891d2017-03-22 14:50:58 -0700995
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +0200996 /* We may have missed link change notifications */
997 schedule_delayed_work(&ndevctx->dwork, 0);
998
stephen hemminger9749fed2017-07-19 11:53:16 -0700999 return ret;
Haiyang Zhang4d447c92011-12-15 13:45:17 -08001000}
1001
stephen hemminger0c195562017-08-01 19:58:53 -07001002static void netvsc_get_vf_stats(struct net_device *net,
1003 struct netvsc_vf_pcpu_stats *tot)
1004{
1005 struct net_device_context *ndev_ctx = netdev_priv(net);
1006 int i;
1007
1008 memset(tot, 0, sizeof(*tot));
1009
1010 for_each_possible_cpu(i) {
1011 const struct netvsc_vf_pcpu_stats *stats
1012 = per_cpu_ptr(ndev_ctx->vf_stats, i);
1013 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
1014 unsigned int start;
1015
1016 do {
1017 start = u64_stats_fetch_begin_irq(&stats->syncp);
1018 rx_packets = stats->rx_packets;
1019 tx_packets = stats->tx_packets;
1020 rx_bytes = stats->rx_bytes;
1021 tx_bytes = stats->tx_bytes;
1022 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
1023
1024 tot->rx_packets += rx_packets;
1025 tot->tx_packets += tx_packets;
1026 tot->rx_bytes += rx_bytes;
1027 tot->tx_bytes += tx_bytes;
1028 tot->tx_dropped += stats->tx_dropped;
1029 }
1030}
1031
stephen hemmingerbc1f4472017-01-06 19:12:52 -08001032static void netvsc_get_stats64(struct net_device *net,
1033 struct rtnl_link_stats64 *t)
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001034{
1035 struct net_device_context *ndev_ctx = netdev_priv(net);
stephen hemminger776e7262017-04-14 14:42:57 -07001036 struct netvsc_device *nvdev = rcu_dereference_rtnl(ndev_ctx->nvdev);
stephen hemminger0c195562017-08-01 19:58:53 -07001037 struct netvsc_vf_pcpu_stats vf_tot;
stephen hemminger89bb42b2017-08-09 17:46:08 -07001038 int i;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001039
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001040 if (!nvdev)
1041 return;
1042
stephen hemminger0c195562017-08-01 19:58:53 -07001043 netdev_stats_to_stats64(t, &net->stats);
1044
1045 netvsc_get_vf_stats(net, &vf_tot);
1046 t->rx_packets += vf_tot.rx_packets;
1047 t->tx_packets += vf_tot.tx_packets;
1048 t->rx_bytes += vf_tot.rx_bytes;
1049 t->tx_bytes += vf_tot.tx_bytes;
1050 t->tx_dropped += vf_tot.tx_dropped;
1051
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001052 for (i = 0; i < nvdev->num_chn; i++) {
1053 const struct netvsc_channel *nvchan = &nvdev->chan_table[i];
1054 const struct netvsc_stats *stats;
1055 u64 packets, bytes, multicast;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001056 unsigned int start;
1057
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001058 stats = &nvchan->tx_stats;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001059 do {
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001060 start = u64_stats_fetch_begin_irq(&stats->syncp);
1061 packets = stats->packets;
1062 bytes = stats->bytes;
1063 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001064
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001065 t->tx_bytes += bytes;
1066 t->tx_packets += packets;
1067
1068 stats = &nvchan->rx_stats;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001069 do {
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001070 start = u64_stats_fetch_begin_irq(&stats->syncp);
1071 packets = stats->packets;
1072 bytes = stats->bytes;
1073 multicast = stats->multicast + stats->broadcast;
1074 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001075
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001076 t->rx_bytes += bytes;
1077 t->rx_packets += packets;
1078 t->multicast += multicast;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001079 }
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001080}
Haiyang Zhang1ce09e82012-07-10 07:19:22 +00001081
1082static int netvsc_set_mac_addr(struct net_device *ndev, void *p)
1083{
stephen hemminger867047c2017-07-28 08:59:42 -07001084 struct net_device_context *ndc = netdev_priv(ndev);
stephen hemminger16ba3262017-08-09 17:46:05 -07001085 struct net_device *vf_netdev = rtnl_dereference(ndc->vf_netdev);
stephen hemminger867047c2017-07-28 08:59:42 -07001086 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
Haiyang Zhang1ce09e82012-07-10 07:19:22 +00001087 struct sockaddr *addr = p;
Haiyang Zhang1ce09e82012-07-10 07:19:22 +00001088 int err;
1089
stephen hemminger16ba3262017-08-09 17:46:05 -07001090 err = eth_prepare_mac_addr_change(ndev, p);
1091 if (err)
Haiyang Zhang1ce09e82012-07-10 07:19:22 +00001092 return err;
1093
stephen hemminger867047c2017-07-28 08:59:42 -07001094 if (!nvdev)
1095 return -ENODEV;
1096
stephen hemminger16ba3262017-08-09 17:46:05 -07001097 if (vf_netdev) {
1098 err = dev_set_mac_address(vf_netdev, addr);
1099 if (err)
1100 return err;
1101 }
1102
stephen hemminger867047c2017-07-28 08:59:42 -07001103 err = rndis_filter_set_device_mac(nvdev, addr->sa_data);
stephen hemminger16ba3262017-08-09 17:46:05 -07001104 if (!err) {
1105 eth_commit_mac_addr_change(ndev, p);
1106 } else if (vf_netdev) {
1107 /* rollback change on VF */
1108 memcpy(addr->sa_data, ndev->dev_addr, ETH_ALEN);
1109 dev_set_mac_address(vf_netdev, addr);
Haiyang Zhang1ce09e82012-07-10 07:19:22 +00001110 }
1111
1112 return err;
1113}
1114
Stephen Hemminger4323b472016-08-23 12:17:57 -07001115static const struct {
1116 char name[ETH_GSTRING_LEN];
1117 u16 offset;
1118} netvsc_stats[] = {
1119 { "tx_scattered", offsetof(struct netvsc_ethtool_stats, tx_scattered) },
1120 { "tx_no_memory", offsetof(struct netvsc_ethtool_stats, tx_no_memory) },
1121 { "tx_no_space", offsetof(struct netvsc_ethtool_stats, tx_no_space) },
1122 { "tx_too_big", offsetof(struct netvsc_ethtool_stats, tx_too_big) },
1123 { "tx_busy", offsetof(struct netvsc_ethtool_stats, tx_busy) },
stephen hemmingercad5c192017-08-09 17:46:12 -07001124 { "tx_send_full", offsetof(struct netvsc_ethtool_stats, tx_send_full) },
1125 { "rx_comp_busy", offsetof(struct netvsc_ethtool_stats, rx_comp_busy) },
stephen hemminger0c195562017-08-01 19:58:53 -07001126}, vf_stats[] = {
1127 { "vf_rx_packets", offsetof(struct netvsc_vf_pcpu_stats, rx_packets) },
1128 { "vf_rx_bytes", offsetof(struct netvsc_vf_pcpu_stats, rx_bytes) },
1129 { "vf_tx_packets", offsetof(struct netvsc_vf_pcpu_stats, tx_packets) },
1130 { "vf_tx_bytes", offsetof(struct netvsc_vf_pcpu_stats, tx_bytes) },
1131 { "vf_tx_dropped", offsetof(struct netvsc_vf_pcpu_stats, tx_dropped) },
Stephen Hemminger4323b472016-08-23 12:17:57 -07001132};
1133
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001134#define NETVSC_GLOBAL_STATS_LEN ARRAY_SIZE(netvsc_stats)
stephen hemminger0c195562017-08-01 19:58:53 -07001135#define NETVSC_VF_STATS_LEN ARRAY_SIZE(vf_stats)
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001136
1137/* 4 statistics per queue (rx/tx packets/bytes) */
1138#define NETVSC_QUEUE_STATS_LEN(dev) ((dev)->num_chn * 4)
1139
Stephen Hemminger4323b472016-08-23 12:17:57 -07001140static int netvsc_get_sset_count(struct net_device *dev, int string_set)
1141{
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001142 struct net_device_context *ndc = netdev_priv(dev);
stephen hemmingerfbd4c7e2017-06-07 15:53:47 -07001143 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
stephen hemminger545a8e72017-03-22 14:51:00 -07001144
1145 if (!nvdev)
1146 return -ENODEV;
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001147
Stephen Hemminger4323b472016-08-23 12:17:57 -07001148 switch (string_set) {
1149 case ETH_SS_STATS:
stephen hemminger0c195562017-08-01 19:58:53 -07001150 return NETVSC_GLOBAL_STATS_LEN
1151 + NETVSC_VF_STATS_LEN
1152 + NETVSC_QUEUE_STATS_LEN(nvdev);
Stephen Hemminger4323b472016-08-23 12:17:57 -07001153 default:
1154 return -EINVAL;
1155 }
1156}
1157
1158static void netvsc_get_ethtool_stats(struct net_device *dev,
1159 struct ethtool_stats *stats, u64 *data)
1160{
1161 struct net_device_context *ndc = netdev_priv(dev);
stephen hemminger867047c2017-07-28 08:59:42 -07001162 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
Stephen Hemminger4323b472016-08-23 12:17:57 -07001163 const void *nds = &ndc->eth_stats;
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001164 const struct netvsc_stats *qstats;
stephen hemminger0c195562017-08-01 19:58:53 -07001165 struct netvsc_vf_pcpu_stats sum;
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001166 unsigned int start;
1167 u64 packets, bytes;
1168 int i, j;
Stephen Hemminger4323b472016-08-23 12:17:57 -07001169
stephen hemminger545a8e72017-03-22 14:51:00 -07001170 if (!nvdev)
1171 return;
1172
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001173 for (i = 0; i < NETVSC_GLOBAL_STATS_LEN; i++)
Stephen Hemminger4323b472016-08-23 12:17:57 -07001174 data[i] = *(unsigned long *)(nds + netvsc_stats[i].offset);
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001175
stephen hemminger0c195562017-08-01 19:58:53 -07001176 netvsc_get_vf_stats(dev, &sum);
1177 for (j = 0; j < NETVSC_VF_STATS_LEN; j++)
1178 data[i++] = *(u64 *)((void *)&sum + vf_stats[j].offset);
1179
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001180 for (j = 0; j < nvdev->num_chn; j++) {
1181 qstats = &nvdev->chan_table[j].tx_stats;
1182
1183 do {
1184 start = u64_stats_fetch_begin_irq(&qstats->syncp);
1185 packets = qstats->packets;
1186 bytes = qstats->bytes;
1187 } while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
1188 data[i++] = packets;
1189 data[i++] = bytes;
1190
1191 qstats = &nvdev->chan_table[j].rx_stats;
1192 do {
1193 start = u64_stats_fetch_begin_irq(&qstats->syncp);
1194 packets = qstats->packets;
1195 bytes = qstats->bytes;
1196 } while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
1197 data[i++] = packets;
1198 data[i++] = bytes;
1199 }
Stephen Hemminger4323b472016-08-23 12:17:57 -07001200}
1201
1202static void netvsc_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1203{
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001204 struct net_device_context *ndc = netdev_priv(dev);
stephen hemminger867047c2017-07-28 08:59:42 -07001205 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001206 u8 *p = data;
Stephen Hemminger4323b472016-08-23 12:17:57 -07001207 int i;
1208
stephen hemminger545a8e72017-03-22 14:51:00 -07001209 if (!nvdev)
1210 return;
1211
Stephen Hemminger4323b472016-08-23 12:17:57 -07001212 switch (stringset) {
1213 case ETH_SS_STATS:
stephen hemminger0c195562017-08-01 19:58:53 -07001214 for (i = 0; i < ARRAY_SIZE(netvsc_stats); i++) {
1215 memcpy(p, netvsc_stats[i].name, ETH_GSTRING_LEN);
1216 p += ETH_GSTRING_LEN;
1217 }
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001218
stephen hemminger0c195562017-08-01 19:58:53 -07001219 for (i = 0; i < ARRAY_SIZE(vf_stats); i++) {
1220 memcpy(p, vf_stats[i].name, ETH_GSTRING_LEN);
1221 p += ETH_GSTRING_LEN;
1222 }
1223
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001224 for (i = 0; i < nvdev->num_chn; i++) {
1225 sprintf(p, "tx_queue_%u_packets", i);
1226 p += ETH_GSTRING_LEN;
1227 sprintf(p, "tx_queue_%u_bytes", i);
1228 p += ETH_GSTRING_LEN;
1229 sprintf(p, "rx_queue_%u_packets", i);
1230 p += ETH_GSTRING_LEN;
1231 sprintf(p, "rx_queue_%u_bytes", i);
1232 p += ETH_GSTRING_LEN;
1233 }
1234
Stephen Hemminger4323b472016-08-23 12:17:57 -07001235 break;
1236 }
1237}
1238
stephen hemmingerb448f4e2017-01-24 13:06:00 -08001239static int
Haiyang Zhang4823eb22017-08-21 19:22:39 -07001240netvsc_get_rss_hash_opts(struct net_device_context *ndc,
1241 struct ethtool_rxnfc *info)
stephen hemmingerb5a5dc82017-01-24 13:06:01 -08001242{
1243 info->data = RXH_IP_SRC | RXH_IP_DST;
1244
1245 switch (info->flow_type) {
1246 case TCP_V4_FLOW:
1247 case TCP_V6_FLOW:
1248 info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
Haiyang Zhang4823eb22017-08-21 19:22:39 -07001249 break;
1250
stephen hemmingerb5a5dc82017-01-24 13:06:01 -08001251 case UDP_V4_FLOW:
Haiyang Zhang4823eb22017-08-21 19:22:39 -07001252 if (ndc->udp4_l4_hash)
1253 info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
1254
1255 break;
1256
stephen hemmingerb5a5dc82017-01-24 13:06:01 -08001257 case UDP_V6_FLOW:
Haiyang Zhang4823eb22017-08-21 19:22:39 -07001258 if (ndc->udp6_l4_hash)
1259 info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
1260
1261 break;
1262
stephen hemmingerb5a5dc82017-01-24 13:06:01 -08001263 case IPV4_FLOW:
1264 case IPV6_FLOW:
1265 break;
1266 default:
1267 info->data = 0;
1268 break;
1269 }
1270
1271 return 0;
1272}
1273
1274static int
stephen hemmingerb448f4e2017-01-24 13:06:00 -08001275netvsc_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
1276 u32 *rules)
1277{
1278 struct net_device_context *ndc = netdev_priv(dev);
stephen hemminger867047c2017-07-28 08:59:42 -07001279 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
stephen hemminger545a8e72017-03-22 14:51:00 -07001280
1281 if (!nvdev)
1282 return -ENODEV;
stephen hemmingerb448f4e2017-01-24 13:06:00 -08001283
1284 switch (info->cmd) {
1285 case ETHTOOL_GRXRINGS:
1286 info->data = nvdev->num_chn;
1287 return 0;
stephen hemmingerb5a5dc82017-01-24 13:06:01 -08001288
1289 case ETHTOOL_GRXFH:
Haiyang Zhang4823eb22017-08-21 19:22:39 -07001290 return netvsc_get_rss_hash_opts(ndc, info);
stephen hemmingerb448f4e2017-01-24 13:06:00 -08001291 }
1292 return -EOPNOTSUPP;
1293}
1294
Haiyang Zhang4823eb22017-08-21 19:22:39 -07001295static int netvsc_set_rss_hash_opts(struct net_device_context *ndc,
1296 struct ethtool_rxnfc *info)
1297{
1298 if (info->data == (RXH_IP_SRC | RXH_IP_DST |
1299 RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
1300 if (info->flow_type == UDP_V4_FLOW)
1301 ndc->udp4_l4_hash = true;
1302 else if (info->flow_type == UDP_V6_FLOW)
1303 ndc->udp6_l4_hash = true;
1304 else
1305 return -EOPNOTSUPP;
1306
1307 return 0;
1308 }
1309
1310 if (info->data == (RXH_IP_SRC | RXH_IP_DST)) {
1311 if (info->flow_type == UDP_V4_FLOW)
1312 ndc->udp4_l4_hash = false;
1313 else if (info->flow_type == UDP_V6_FLOW)
1314 ndc->udp6_l4_hash = false;
1315 else
1316 return -EOPNOTSUPP;
1317
1318 return 0;
1319 }
1320
1321 return -EOPNOTSUPP;
1322}
1323
1324static int
1325netvsc_set_rxnfc(struct net_device *ndev, struct ethtool_rxnfc *info)
1326{
1327 struct net_device_context *ndc = netdev_priv(ndev);
1328
1329 if (info->cmd == ETHTOOL_SRXFH)
1330 return netvsc_set_rss_hash_opts(ndc, info);
1331
1332 return -EOPNOTSUPP;
1333}
1334
Richard Weinberger316158f2014-07-09 16:23:59 +02001335#ifdef CONFIG_NET_POLL_CONTROLLER
stephen hemmingera5ecd432017-06-07 15:53:48 -07001336static void netvsc_poll_controller(struct net_device *dev)
Richard Weinberger316158f2014-07-09 16:23:59 +02001337{
stephen hemmingera5ecd432017-06-07 15:53:48 -07001338 struct net_device_context *ndc = netdev_priv(dev);
1339 struct netvsc_device *ndev;
1340 int i;
1341
1342 rcu_read_lock();
1343 ndev = rcu_dereference(ndc->nvdev);
1344 if (ndev) {
1345 for (i = 0; i < ndev->num_chn; i++) {
1346 struct netvsc_channel *nvchan = &ndev->chan_table[i];
1347
1348 napi_schedule(&nvchan->napi);
1349 }
1350 }
1351 rcu_read_unlock();
Richard Weinberger316158f2014-07-09 16:23:59 +02001352}
1353#endif
Haiyang Zhang1ce09e82012-07-10 07:19:22 +00001354
stephen hemminger962f3fe2017-01-24 13:06:02 -08001355static u32 netvsc_get_rxfh_key_size(struct net_device *dev)
1356{
1357 return NETVSC_HASH_KEYLEN;
1358}
1359
1360static u32 netvsc_rss_indir_size(struct net_device *dev)
1361{
stephen hemmingerff4a4412017-01-24 13:06:04 -08001362 return ITAB_NUM;
stephen hemminger962f3fe2017-01-24 13:06:02 -08001363}
1364
1365static int netvsc_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
1366 u8 *hfunc)
1367{
1368 struct net_device_context *ndc = netdev_priv(dev);
stephen hemminger867047c2017-07-28 08:59:42 -07001369 struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev);
Colin Ian Kingeb996ed2017-03-25 14:26:39 +00001370 struct rndis_device *rndis_dev;
stephen hemmingerff4a4412017-01-24 13:06:04 -08001371 int i;
stephen hemminger962f3fe2017-01-24 13:06:02 -08001372
stephen hemminger545a8e72017-03-22 14:51:00 -07001373 if (!ndev)
1374 return -ENODEV;
1375
stephen hemminger962f3fe2017-01-24 13:06:02 -08001376 if (hfunc)
1377 *hfunc = ETH_RSS_HASH_TOP; /* Toeplitz */
1378
Colin Ian Kingeb996ed2017-03-25 14:26:39 +00001379 rndis_dev = ndev->extension;
stephen hemmingerff4a4412017-01-24 13:06:04 -08001380 if (indir) {
1381 for (i = 0; i < ITAB_NUM; i++)
1382 indir[i] = rndis_dev->ind_table[i];
1383 }
1384
stephen hemminger962f3fe2017-01-24 13:06:02 -08001385 if (key)
1386 memcpy(key, rndis_dev->rss_key, NETVSC_HASH_KEYLEN);
1387
1388 return 0;
1389}
1390
1391static int netvsc_set_rxfh(struct net_device *dev, const u32 *indir,
1392 const u8 *key, const u8 hfunc)
1393{
1394 struct net_device_context *ndc = netdev_priv(dev);
stephen hemminger545a8e72017-03-22 14:51:00 -07001395 struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev);
Colin Ian Kingeb996ed2017-03-25 14:26:39 +00001396 struct rndis_device *rndis_dev;
stephen hemmingerff4a4412017-01-24 13:06:04 -08001397 int i;
stephen hemminger962f3fe2017-01-24 13:06:02 -08001398
stephen hemminger545a8e72017-03-22 14:51:00 -07001399 if (!ndev)
1400 return -ENODEV;
1401
stephen hemminger962f3fe2017-01-24 13:06:02 -08001402 if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
1403 return -EOPNOTSUPP;
1404
Colin Ian Kingeb996ed2017-03-25 14:26:39 +00001405 rndis_dev = ndev->extension;
stephen hemmingerff4a4412017-01-24 13:06:04 -08001406 if (indir) {
1407 for (i = 0; i < ITAB_NUM; i++)
Arnd Bergmannb92b7d32017-06-22 00:16:37 +02001408 if (indir[i] >= VRSS_CHANNEL_MAX)
stephen hemmingerff4a4412017-01-24 13:06:04 -08001409 return -EINVAL;
1410
1411 for (i = 0; i < ITAB_NUM; i++)
1412 rndis_dev->ind_table[i] = indir[i];
1413 }
1414
1415 if (!key) {
1416 if (!indir)
1417 return 0;
1418
1419 key = rndis_dev->rss_key;
1420 }
stephen hemminger962f3fe2017-01-24 13:06:02 -08001421
1422 return rndis_filter_set_rss_param(rndis_dev, key, ndev->num_chn);
1423}
1424
stephen hemminger8b532792017-08-09 17:46:11 -07001425/* Hyper-V RNDIS protocol does not have ring in the HW sense.
1426 * It does have pre-allocated receive area which is divided into sections.
1427 */
1428static void __netvsc_get_ringparam(struct netvsc_device *nvdev,
1429 struct ethtool_ringparam *ring)
1430{
1431 u32 max_buf_size;
1432
1433 ring->rx_pending = nvdev->recv_section_cnt;
1434 ring->tx_pending = nvdev->send_section_cnt;
1435
1436 if (nvdev->nvsp_version <= NVSP_PROTOCOL_VERSION_2)
1437 max_buf_size = NETVSC_RECEIVE_BUFFER_SIZE_LEGACY;
1438 else
1439 max_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
1440
1441 ring->rx_max_pending = max_buf_size / nvdev->recv_section_size;
1442 ring->tx_max_pending = NETVSC_SEND_BUFFER_SIZE
1443 / nvdev->send_section_size;
1444}
1445
1446static void netvsc_get_ringparam(struct net_device *ndev,
1447 struct ethtool_ringparam *ring)
1448{
1449 struct net_device_context *ndevctx = netdev_priv(ndev);
1450 struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
1451
1452 if (!nvdev)
1453 return;
1454
1455 __netvsc_get_ringparam(nvdev, ring);
1456}
1457
1458static int netvsc_set_ringparam(struct net_device *ndev,
1459 struct ethtool_ringparam *ring)
1460{
1461 struct net_device_context *ndevctx = netdev_priv(ndev);
1462 struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
1463 struct hv_device *hdev = ndevctx->device_ctx;
1464 struct netvsc_device_info device_info;
1465 struct ethtool_ringparam orig;
1466 u32 new_tx, new_rx;
1467 bool was_opened;
1468 int ret = 0;
1469
1470 if (!nvdev || nvdev->destroy)
1471 return -ENODEV;
1472
1473 memset(&orig, 0, sizeof(orig));
1474 __netvsc_get_ringparam(nvdev, &orig);
1475
1476 new_tx = clamp_t(u32, ring->tx_pending,
1477 NETVSC_MIN_TX_SECTIONS, orig.tx_max_pending);
1478 new_rx = clamp_t(u32, ring->rx_pending,
1479 NETVSC_MIN_RX_SECTIONS, orig.rx_max_pending);
1480
1481 if (new_tx == orig.tx_pending &&
1482 new_rx == orig.rx_pending)
1483 return 0; /* no change */
1484
1485 memset(&device_info, 0, sizeof(device_info));
1486 device_info.num_chn = nvdev->num_chn;
1487 device_info.ring_size = ring_size;
1488 device_info.send_sections = new_tx;
1489 device_info.recv_sections = new_rx;
1490
1491 netif_device_detach(ndev);
1492 was_opened = rndis_filter_opened(nvdev);
1493 if (was_opened)
1494 rndis_filter_close(nvdev);
1495
1496 rndis_filter_device_remove(hdev, nvdev);
1497
1498 nvdev = rndis_filter_device_add(hdev, &device_info);
1499 if (IS_ERR(nvdev)) {
1500 ret = PTR_ERR(nvdev);
1501
1502 device_info.send_sections = orig.tx_pending;
1503 device_info.recv_sections = orig.rx_pending;
1504 nvdev = rndis_filter_device_add(hdev, &device_info);
1505 if (IS_ERR(nvdev)) {
1506 netdev_err(ndev, "restoring ringparam failed: %ld\n",
1507 PTR_ERR(nvdev));
1508 return ret;
1509 }
1510 }
1511
1512 if (was_opened)
1513 rndis_filter_open(nvdev);
1514 netif_device_attach(ndev);
1515
1516 /* We may have missed link change notifications */
1517 ndevctx->last_reconfig = 0;
1518 schedule_delayed_work(&ndevctx->dwork, 0);
1519
1520 return ret;
1521}
1522
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -07001523static const struct ethtool_ops ethtool_ops = {
1524 .get_drvinfo = netvsc_get_drvinfo,
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -07001525 .get_link = ethtool_op_get_link,
Stephen Hemminger4323b472016-08-23 12:17:57 -07001526 .get_ethtool_stats = netvsc_get_ethtool_stats,
1527 .get_sset_count = netvsc_get_sset_count,
1528 .get_strings = netvsc_get_strings,
Andrew Schwartzmeyer59995372015-02-26 16:27:14 -08001529 .get_channels = netvsc_get_channels,
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -07001530 .set_channels = netvsc_set_channels,
sixiao@microsoft.com76d13b52016-02-17 16:43:59 -08001531 .get_ts_info = ethtool_op_get_ts_info,
stephen hemmingerb448f4e2017-01-24 13:06:00 -08001532 .get_rxnfc = netvsc_get_rxnfc,
Haiyang Zhang4823eb22017-08-21 19:22:39 -07001533 .set_rxnfc = netvsc_set_rxnfc,
stephen hemminger962f3fe2017-01-24 13:06:02 -08001534 .get_rxfh_key_size = netvsc_get_rxfh_key_size,
1535 .get_rxfh_indir_size = netvsc_rss_indir_size,
1536 .get_rxfh = netvsc_get_rxfh,
1537 .set_rxfh = netvsc_set_rxfh,
Philippe Reynes5e8456f2017-03-08 23:41:04 +01001538 .get_link_ksettings = netvsc_get_link_ksettings,
1539 .set_link_ksettings = netvsc_set_link_ksettings,
stephen hemminger8b532792017-08-09 17:46:11 -07001540 .get_ringparam = netvsc_get_ringparam,
1541 .set_ringparam = netvsc_set_ringparam,
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -07001542};
1543
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001544static const struct net_device_ops device_ops = {
1545 .ndo_open = netvsc_open,
1546 .ndo_stop = netvsc_close,
1547 .ndo_start_xmit = netvsc_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001548 .ndo_set_rx_mode = netvsc_set_multicast_list,
Haiyang Zhang4d447c92011-12-15 13:45:17 -08001549 .ndo_change_mtu = netvsc_change_mtu,
Haiyang Zhangb681b582010-08-03 19:15:31 +00001550 .ndo_validate_addr = eth_validate_addr,
Haiyang Zhang1ce09e82012-07-10 07:19:22 +00001551 .ndo_set_mac_address = netvsc_set_mac_addr,
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001552 .ndo_select_queue = netvsc_select_queue,
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001553 .ndo_get_stats64 = netvsc_get_stats64,
Richard Weinberger316158f2014-07-09 16:23:59 +02001554#ifdef CONFIG_NET_POLL_CONTROLLER
1555 .ndo_poll_controller = netvsc_poll_controller,
1556#endif
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001557};
1558
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001559/*
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001560 * Handle link status changes. For RNDIS_STATUS_NETWORK_CHANGE emulate link
1561 * down/up sequence. In case of RNDIS_STATUS_MEDIA_CONNECT when carrier is
1562 * present send GARP packet to network peers with netif_notify_peers().
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001563 */
Haiyang Zhang891de742014-02-12 16:54:27 -08001564static void netvsc_link_change(struct work_struct *w)
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001565{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001566 struct net_device_context *ndev_ctx =
1567 container_of(w, struct net_device_context, dwork.work);
1568 struct hv_device *device_obj = ndev_ctx->device_ctx;
1569 struct net_device *net = hv_get_drvdata(device_obj);
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -07001570 struct netvsc_device *net_device;
Haiyang Zhang891de742014-02-12 16:54:27 -08001571 struct rndis_device *rdev;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001572 struct netvsc_reconfig *event = NULL;
1573 bool notify = false, reschedule = false;
1574 unsigned long flags, next_reconfig, delay;
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001575
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001576 rtnl_lock();
stephen hemmingera0be4502017-03-22 14:51:01 -07001577 net_device = rtnl_dereference(ndev_ctx->nvdev);
1578 if (!net_device)
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001579 goto out_unlock;
1580
Haiyang Zhang891de742014-02-12 16:54:27 -08001581 rdev = net_device->extension;
Haiyang Zhang891de742014-02-12 16:54:27 -08001582
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001583 next_reconfig = ndev_ctx->last_reconfig + LINKCHANGE_INT;
1584 if (time_is_after_jiffies(next_reconfig)) {
1585 /* link_watch only sends one notification with current state
1586 * per second, avoid doing reconfig more frequently. Handle
1587 * wrap around.
1588 */
1589 delay = next_reconfig - jiffies;
1590 delay = delay < LINKCHANGE_INT ? delay : LINKCHANGE_INT;
1591 schedule_delayed_work(&ndev_ctx->dwork, delay);
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001592 goto out_unlock;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001593 }
1594 ndev_ctx->last_reconfig = jiffies;
1595
1596 spin_lock_irqsave(&ndev_ctx->lock, flags);
1597 if (!list_empty(&ndev_ctx->reconfig_events)) {
1598 event = list_first_entry(&ndev_ctx->reconfig_events,
1599 struct netvsc_reconfig, list);
1600 list_del(&event->list);
1601 reschedule = !list_empty(&ndev_ctx->reconfig_events);
1602 }
1603 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1604
1605 if (!event)
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001606 goto out_unlock;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001607
1608 switch (event->event) {
1609 /* Only the following events are possible due to the check in
1610 * netvsc_linkstatus_callback()
1611 */
1612 case RNDIS_STATUS_MEDIA_CONNECT:
1613 if (rdev->link_state) {
1614 rdev->link_state = false;
stephen hemminger0c195562017-08-01 19:58:53 -07001615 netif_carrier_on(net);
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001616 netif_tx_wake_all_queues(net);
1617 } else {
1618 notify = true;
Haiyang Zhang3a494e72014-06-19 18:34:36 -07001619 }
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001620 kfree(event);
1621 break;
1622 case RNDIS_STATUS_MEDIA_DISCONNECT:
1623 if (!rdev->link_state) {
1624 rdev->link_state = true;
1625 netif_carrier_off(net);
1626 netif_tx_stop_all_queues(net);
1627 }
1628 kfree(event);
1629 break;
1630 case RNDIS_STATUS_NETWORK_CHANGE:
1631 /* Only makes sense if carrier is present */
1632 if (!rdev->link_state) {
1633 rdev->link_state = true;
1634 netif_carrier_off(net);
1635 netif_tx_stop_all_queues(net);
1636 event->event = RNDIS_STATUS_MEDIA_CONNECT;
1637 spin_lock_irqsave(&ndev_ctx->lock, flags);
Haiyang Zhang15cfd402016-04-21 16:13:01 -07001638 list_add(&event->list, &ndev_ctx->reconfig_events);
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001639 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1640 reschedule = true;
1641 }
1642 break;
Haiyang Zhang891de742014-02-12 16:54:27 -08001643 }
1644
1645 rtnl_unlock();
1646
1647 if (notify)
1648 netdev_notify_peers(net);
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001649
1650 /* link_watch only sends one notification with current state per
1651 * second, handle next reconfig event in 2 seconds.
1652 */
1653 if (reschedule)
1654 schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001655
1656 return;
1657
1658out_unlock:
1659 rtnl_unlock();
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001660}
1661
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001662static struct net_device *get_netvsc_bymac(const u8 *mac)
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001663{
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001664 struct net_device *dev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001665
Stephen Hemminger8737caa2016-08-23 12:17:44 -07001666 ASSERT_RTNL();
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001667
1668 for_each_netdev(&init_net, dev) {
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001669 if (dev->netdev_ops != &device_ops)
1670 continue; /* not a netvsc device */
1671
1672 if (ether_addr_equal(mac, dev->perm_addr))
1673 return dev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001674 }
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001675
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001676 return NULL;
1677}
1678
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001679static struct net_device *get_netvsc_byref(struct net_device *vf_netdev)
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001680{
1681 struct net_device *dev;
1682
1683 ASSERT_RTNL();
1684
1685 for_each_netdev(&init_net, dev) {
1686 struct net_device_context *net_device_ctx;
1687
1688 if (dev->netdev_ops != &device_ops)
1689 continue; /* not a netvsc device */
1690
1691 net_device_ctx = netdev_priv(dev);
stephen hemminger79e8cbe2017-07-19 11:53:13 -07001692 if (!rtnl_dereference(net_device_ctx->nvdev))
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001693 continue; /* device is removed */
1694
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001695 if (rtnl_dereference(net_device_ctx->vf_netdev) == vf_netdev)
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001696 return dev; /* a match */
1697 }
1698
1699 return NULL;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001700}
1701
stephen hemminger0c195562017-08-01 19:58:53 -07001702/* Called when VF is injecting data into network stack.
1703 * Change the associated network device from VF to netvsc.
1704 * note: already called with rcu_read_lock
1705 */
1706static rx_handler_result_t netvsc_vf_handle_frame(struct sk_buff **pskb)
1707{
1708 struct sk_buff *skb = *pskb;
1709 struct net_device *ndev = rcu_dereference(skb->dev->rx_handler_data);
1710 struct net_device_context *ndev_ctx = netdev_priv(ndev);
1711 struct netvsc_vf_pcpu_stats *pcpu_stats
1712 = this_cpu_ptr(ndev_ctx->vf_stats);
1713
1714 skb->dev = ndev;
1715
1716 u64_stats_update_begin(&pcpu_stats->syncp);
1717 pcpu_stats->rx_packets++;
1718 pcpu_stats->rx_bytes += skb->len;
1719 u64_stats_update_end(&pcpu_stats->syncp);
1720
1721 return RX_HANDLER_ANOTHER;
1722}
1723
1724static int netvsc_vf_join(struct net_device *vf_netdev,
1725 struct net_device *ndev)
1726{
1727 struct net_device_context *ndev_ctx = netdev_priv(ndev);
1728 int ret;
1729
1730 ret = netdev_rx_handler_register(vf_netdev,
1731 netvsc_vf_handle_frame, ndev);
1732 if (ret != 0) {
1733 netdev_err(vf_netdev,
1734 "can not register netvsc VF receive handler (err = %d)\n",
1735 ret);
1736 goto rx_handler_failed;
1737 }
1738
1739 ret = netdev_upper_dev_link(vf_netdev, ndev);
1740 if (ret != 0) {
1741 netdev_err(vf_netdev,
1742 "can not set master device %s (err = %d)\n",
1743 ndev->name, ret);
1744 goto upper_link_failed;
1745 }
1746
1747 /* set slave flag before open to prevent IPv6 addrconf */
1748 vf_netdev->flags |= IFF_SLAVE;
1749
stephen hemminger6123c662017-08-09 17:46:03 -07001750 schedule_delayed_work(&ndev_ctx->vf_takeover, VF_TAKEOVER_INT);
1751
1752 call_netdevice_notifiers(NETDEV_JOIN, vf_netdev);
stephen hemminger0c195562017-08-01 19:58:53 -07001753
1754 netdev_info(vf_netdev, "joined to %s\n", ndev->name);
1755 return 0;
1756
1757upper_link_failed:
1758 netdev_rx_handler_unregister(vf_netdev);
1759rx_handler_failed:
1760 return ret;
1761}
1762
1763static void __netvsc_vf_setup(struct net_device *ndev,
1764 struct net_device *vf_netdev)
1765{
1766 int ret;
1767
stephen hemminger0c195562017-08-01 19:58:53 -07001768 /* Align MTU of VF with master */
1769 ret = dev_set_mtu(vf_netdev, ndev->mtu);
1770 if (ret)
1771 netdev_warn(vf_netdev,
1772 "unable to change mtu to %u\n", ndev->mtu);
1773
1774 if (netif_running(ndev)) {
1775 ret = dev_open(vf_netdev);
1776 if (ret)
1777 netdev_warn(vf_netdev,
1778 "unable to open: %d\n", ret);
1779 }
1780}
1781
1782/* Setup VF as slave of the synthetic device.
1783 * Runs in workqueue to avoid recursion in netlink callbacks.
1784 */
1785static void netvsc_vf_setup(struct work_struct *w)
1786{
1787 struct net_device_context *ndev_ctx
stephen hemminger6123c662017-08-09 17:46:03 -07001788 = container_of(w, struct net_device_context, vf_takeover.work);
stephen hemminger0c195562017-08-01 19:58:53 -07001789 struct net_device *ndev = hv_get_drvdata(ndev_ctx->device_ctx);
1790 struct net_device *vf_netdev;
1791
stephen hemmingerfb84af82017-08-04 12:14:00 -07001792 if (!rtnl_trylock()) {
stephen hemminger6123c662017-08-09 17:46:03 -07001793 schedule_delayed_work(&ndev_ctx->vf_takeover, 0);
stephen hemmingerfb84af82017-08-04 12:14:00 -07001794 return;
1795 }
1796
stephen hemminger0c195562017-08-01 19:58:53 -07001797 vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
1798 if (vf_netdev)
1799 __netvsc_vf_setup(ndev, vf_netdev);
1800
1801 rtnl_unlock();
1802}
1803
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001804static int netvsc_register_vf(struct net_device *vf_netdev)
1805{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001806 struct net_device *ndev;
1807 struct net_device_context *net_device_ctx;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001808 struct netvsc_device *netvsc_dev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001809
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001810 if (vf_netdev->addr_len != ETH_ALEN)
1811 return NOTIFY_DONE;
1812
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001813 /*
1814 * We will use the MAC address to locate the synthetic interface to
1815 * associate with the VF interface. If we don't find a matching
1816 * synthetic interface, move on.
1817 */
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001818 ndev = get_netvsc_bymac(vf_netdev->perm_addr);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001819 if (!ndev)
1820 return NOTIFY_DONE;
1821
1822 net_device_ctx = netdev_priv(ndev);
stephen hemminger545a8e72017-03-22 14:51:00 -07001823 netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001824 if (!netvsc_dev || rtnl_dereference(net_device_ctx->vf_netdev))
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001825 return NOTIFY_DONE;
1826
stephen hemminger0c195562017-08-01 19:58:53 -07001827 if (netvsc_vf_join(vf_netdev, ndev) != 0)
1828 return NOTIFY_DONE;
1829
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001830 netdev_info(ndev, "VF registering: %s\n", vf_netdev->name);
stephen hemminger0c195562017-08-01 19:58:53 -07001831
1832 /* Prevent this module from being unloaded while VF is registered */
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001833 try_module_get(THIS_MODULE);
Stephen Hemminger07d0f002016-09-22 16:56:30 -07001834
1835 dev_hold(vf_netdev);
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001836 rcu_assign_pointer(net_device_ctx->vf_netdev, vf_netdev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001837 return NOTIFY_OK;
1838}
1839
stephen hemminger7b83f522017-08-07 11:30:00 -07001840static int netvsc_vf_up(struct net_device *vf_netdev)
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001841{
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001842 struct net_device_context *net_device_ctx;
stephen hemminger7b83f522017-08-07 11:30:00 -07001843 struct netvsc_device *netvsc_dev;
stephen hemminger0c195562017-08-01 19:58:53 -07001844 struct net_device *ndev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001845
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001846 ndev = get_netvsc_byref(vf_netdev);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001847 if (!ndev)
1848 return NOTIFY_DONE;
1849
1850 net_device_ctx = netdev_priv(ndev);
stephen hemminger7b83f522017-08-07 11:30:00 -07001851 netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
1852 if (!netvsc_dev)
1853 return NOTIFY_DONE;
1854
1855 /* Bump refcount when datapath is acvive - Why? */
1856 rndis_filter_open(netvsc_dev);
1857
1858 /* notify the host to switch the data path. */
1859 netvsc_switch_datapath(ndev, true);
1860 netdev_info(ndev, "Data path switched to VF: %s\n", vf_netdev->name);
1861
1862 return NOTIFY_OK;
1863}
1864
1865static int netvsc_vf_down(struct net_device *vf_netdev)
1866{
1867 struct net_device_context *net_device_ctx;
1868 struct netvsc_device *netvsc_dev;
1869 struct net_device *ndev;
1870
1871 ndev = get_netvsc_byref(vf_netdev);
1872 if (!ndev)
1873 return NOTIFY_DONE;
1874
1875 net_device_ctx = netdev_priv(ndev);
1876 netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
1877 if (!netvsc_dev)
1878 return NOTIFY_DONE;
1879
1880 netvsc_switch_datapath(ndev, false);
1881 netdev_info(ndev, "Data path switched from VF: %s\n", vf_netdev->name);
1882 rndis_filter_close(netvsc_dev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001883
1884 return NOTIFY_OK;
1885}
1886
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001887static int netvsc_unregister_vf(struct net_device *vf_netdev)
1888{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001889 struct net_device *ndev;
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001890 struct net_device_context *net_device_ctx;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001891
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001892 ndev = get_netvsc_byref(vf_netdev);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001893 if (!ndev)
1894 return NOTIFY_DONE;
1895
1896 net_device_ctx = netdev_priv(ndev);
stephen hemminger6123c662017-08-09 17:46:03 -07001897 cancel_delayed_work_sync(&net_device_ctx->vf_takeover);
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001898
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001899 netdev_info(ndev, "VF unregistering: %s\n", vf_netdev->name);
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001900
stephen hemminger0c195562017-08-01 19:58:53 -07001901 netdev_upper_dev_unlink(vf_netdev, ndev);
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001902 RCU_INIT_POINTER(net_device_ctx->vf_netdev, NULL);
Stephen Hemminger07d0f002016-09-22 16:56:30 -07001903 dev_put(vf_netdev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001904 module_put(THIS_MODULE);
1905 return NOTIFY_OK;
1906}
1907
K. Y. Srinivasan84946892011-09-13 10:59:38 -07001908static int netvsc_probe(struct hv_device *dev,
1909 const struct hv_vmbus_device_id *dev_id)
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001910{
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001911 struct net_device *net = NULL;
1912 struct net_device_context *net_device_ctx;
1913 struct netvsc_device_info device_info;
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001914 struct netvsc_device *nvdev;
stephen hemminger0c195562017-08-01 19:58:53 -07001915 int ret = -ENOMEM;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001916
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001917 net = alloc_etherdev_mq(sizeof(struct net_device_context),
stephen hemminger2b018882017-01-24 13:06:03 -08001918 VRSS_CHANNEL_MAX);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001919 if (!net)
stephen hemminger0c195562017-08-01 19:58:53 -07001920 goto no_net;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001921
Haiyang Zhang1b07da52014-03-04 14:11:06 -08001922 netif_carrier_off(net);
1923
Haiyang Zhangb37879e2016-08-04 10:42:14 -07001924 netvsc_init_settings(net);
1925
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001926 net_device_ctx = netdev_priv(net);
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -07001927 net_device_ctx->device_ctx = dev;
Simon Xiao3f300ff2015-04-28 01:05:17 -07001928 net_device_ctx->msg_enable = netif_msg_init(debug, default_msg);
1929 if (netif_msg_probe(net_device_ctx))
1930 netdev_dbg(net, "netvsc msg_enable: %d\n",
1931 net_device_ctx->msg_enable);
1932
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -07001933 hv_set_drvdata(dev, net);
Vitaly Kuznetsovf580aec2016-05-13 13:55:20 +02001934
Haiyang Zhang891de742014-02-12 16:54:27 -08001935 INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001936
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001937 spin_lock_init(&net_device_ctx->lock);
1938 INIT_LIST_HEAD(&net_device_ctx->reconfig_events);
stephen hemminger6123c662017-08-09 17:46:03 -07001939 INIT_DELAYED_WORK(&net_device_ctx->vf_takeover, netvsc_vf_setup);
stephen hemminger0c195562017-08-01 19:58:53 -07001940
1941 net_device_ctx->vf_stats
1942 = netdev_alloc_pcpu_stats(struct netvsc_vf_pcpu_stats);
1943 if (!net_device_ctx->vf_stats)
1944 goto no_stats;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001945
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001946 net->netdev_ops = &device_ops;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001947 net->ethtool_ops = &ethtool_ops;
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -07001948 SET_NETDEV_DEV(net, &dev->device);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001949
Vitaly Kuznetsov14a03cf2016-02-05 17:29:08 +01001950 /* We always need headroom for rndis header */
1951 net->needed_headroom = RNDIS_AND_PPI_SIZE;
1952
Haiyang Zhang692e0842011-09-01 12:19:43 -07001953 /* Notify the netvsc driver of the new device */
Andrew Schwartzmeyer8ebdcc52015-08-11 17:14:31 -07001954 memset(&device_info, 0, sizeof(device_info));
Haiyang Zhang692e0842011-09-01 12:19:43 -07001955 device_info.ring_size = ring_size;
stephen hemminger3071ada2017-03-22 14:50:59 -07001956 device_info.num_chn = VRSS_CHANNEL_DEFAULT;
stephen hemminger8b532792017-08-09 17:46:11 -07001957 device_info.send_sections = NETVSC_DEFAULT_TX;
1958 device_info.recv_sections = NETVSC_DEFAULT_RX;
stephen hemminger9749fed2017-07-19 11:53:16 -07001959
1960 nvdev = rndis_filter_device_add(dev, &device_info);
1961 if (IS_ERR(nvdev)) {
1962 ret = PTR_ERR(nvdev);
Haiyang Zhang692e0842011-09-01 12:19:43 -07001963 netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
stephen hemminger0c195562017-08-01 19:58:53 -07001964 goto rndis_failed;
Haiyang Zhang692e0842011-09-01 12:19:43 -07001965 }
stephen hemminger0c195562017-08-01 19:58:53 -07001966
Haiyang Zhang692e0842011-09-01 12:19:43 -07001967 memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
1968
stephen hemminger23312a32017-01-24 13:05:59 -08001969 /* hw_features computed in rndis_filter_device_add */
1970 net->features = net->hw_features |
1971 NETIF_F_HIGHDMA | NETIF_F_SG |
1972 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
1973 net->vlan_features = net->features;
1974
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001975 netif_set_real_num_tx_queues(net, nvdev->num_chn);
1976 netif_set_real_num_rx_queues(net, nvdev->num_chn);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001977
stephen hemminger9749fed2017-07-19 11:53:16 -07001978 netdev_lockdep_set_classes(net);
1979
Jarod Wilsond0c2c992016-10-20 13:55:21 -04001980 /* MTU range: 68 - 1500 or 65521 */
1981 net->min_mtu = NETVSC_MTU_MIN;
1982 if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
1983 net->max_mtu = NETVSC_MTU - ETH_HLEN;
1984 else
1985 net->max_mtu = ETH_DATA_LEN;
1986
Haiyang Zhanga68f9612013-12-20 16:52:31 -08001987 ret = register_netdev(net);
1988 if (ret != 0) {
1989 pr_err("Unable to register netdev.\n");
stephen hemminger0c195562017-08-01 19:58:53 -07001990 goto register_failed;
Haiyang Zhanga68f9612013-12-20 16:52:31 -08001991 }
1992
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001993 return ret;
stephen hemminger0c195562017-08-01 19:58:53 -07001994
1995register_failed:
1996 rndis_filter_device_remove(dev, nvdev);
1997rndis_failed:
1998 free_percpu(net_device_ctx->vf_stats);
1999no_stats:
2000 hv_set_drvdata(dev, NULL);
2001 free_netdev(net);
2002no_net:
2003 return ret;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07002004}
2005
K. Y. Srinivasan415b0232011-04-29 13:45:12 -07002006static int netvsc_remove(struct hv_device *dev)
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07002007{
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -07002008 struct net_device *net;
Haiyang Zhang122a5f62011-05-27 06:21:55 -07002009 struct net_device_context *ndev_ctx;
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -07002010
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +02002011 net = hv_get_drvdata(dev);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07002012
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07002013 if (net == NULL) {
K. Y. Srinivasan415b0232011-04-29 13:45:12 -07002014 dev_err(&dev->device, "No net device to remove\n");
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07002015 return 0;
2016 }
2017
Haiyang Zhang122a5f62011-05-27 06:21:55 -07002018 ndev_ctx = netdev_priv(net);
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +02002019
stephen hemmingera0be4502017-03-22 14:51:01 -07002020 netif_device_detach(net);
Vitaly Kuznetsovf580aec2016-05-13 13:55:20 +02002021
Haiyang Zhang122a5f62011-05-27 06:21:55 -07002022 cancel_delayed_work_sync(&ndev_ctx->dwork);
2023
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07002024 /*
2025 * Call to the vsc driver to let it know that the device is being
stephen hemmingera0be4502017-03-22 14:51:01 -07002026 * removed. Also blocks mtu and channel changes.
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07002027 */
stephen hemmingera0be4502017-03-22 14:51:01 -07002028 rtnl_lock();
stephen hemminger79e8cbe2017-07-19 11:53:13 -07002029 rndis_filter_device_remove(dev,
2030 rtnl_dereference(ndev_ctx->nvdev));
stephen hemmingera0be4502017-03-22 14:51:01 -07002031 rtnl_unlock();
2032
2033 unregister_netdev(net);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07002034
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +02002035 hv_set_drvdata(dev, NULL);
2036
stephen hemminger0c195562017-08-01 19:58:53 -07002037 free_percpu(ndev_ctx->vf_stats);
Simon Xiao6c80f3f2017-01-24 13:06:13 -08002038 free_netdev(net);
Haiyang Zhangdf06bcf2011-05-23 09:03:47 -07002039 return 0;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07002040}
2041
K. Y. Srinivasan345c4cc2011-08-25 09:48:34 -07002042static const struct hv_vmbus_device_id id_table[] = {
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07002043 /* Network guid */
K. Y. Srinivasan8f505942013-01-23 17:42:42 -08002044 { HV_NIC_GUID, },
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07002045 { },
K. Y. Srinivasan345c4cc2011-08-25 09:48:34 -07002046};
2047
2048MODULE_DEVICE_TABLE(vmbus, id_table);
2049
K. Y. Srinivasanf1542a62011-05-10 07:55:16 -07002050/* The one and only one */
K. Y. Srinivasanfde0ef92011-05-12 19:35:08 -07002051static struct hv_driver netvsc_drv = {
Haiyang Zhangd31b20f2012-03-07 10:02:00 +00002052 .name = KBUILD_MODNAME,
K. Y. Srinivasan345c4cc2011-08-25 09:48:34 -07002053 .id_table = id_table,
K. Y. Srinivasanfde0ef92011-05-12 19:35:08 -07002054 .probe = netvsc_probe,
2055 .remove = netvsc_remove,
K. Y. Srinivasand4890972011-05-10 07:55:17 -07002056};
K. Y. Srinivasanf1542a62011-05-10 07:55:16 -07002057
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07002058/*
2059 * On Hyper-V, every VF interface is matched with a corresponding
2060 * synthetic interface. The synthetic interface is presented first
2061 * to the guest. When the corresponding VF instance is registered,
2062 * we will take care of switching the data path.
2063 */
2064static int netvsc_netdev_event(struct notifier_block *this,
2065 unsigned long event, void *ptr)
2066{
2067 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
2068
Stephen Hemmingeree837a12016-09-22 16:56:31 -07002069 /* Skip our own events */
2070 if (event_dev->netdev_ops == &device_ops)
2071 return NOTIFY_DONE;
2072
2073 /* Avoid non-Ethernet type devices */
2074 if (event_dev->type != ARPHRD_ETHER)
2075 return NOTIFY_DONE;
2076
Vitaly Kuznetsov0dbff142016-08-15 17:48:43 +02002077 /* Avoid Vlan dev with same MAC registering as VF */
Parav Panditd0d7b102017-02-04 11:00:49 -06002078 if (is_vlan_dev(event_dev))
Vitaly Kuznetsov0dbff142016-08-15 17:48:43 +02002079 return NOTIFY_DONE;
2080
2081 /* Avoid Bonding master dev with same MAC registering as VF */
Stephen Hemmingeree837a12016-09-22 16:56:31 -07002082 if ((event_dev->priv_flags & IFF_BONDING) &&
2083 (event_dev->flags & IFF_MASTER))
Haiyang Zhangcb2911f2016-06-02 12:02:04 -07002084 return NOTIFY_DONE;
2085
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07002086 switch (event) {
2087 case NETDEV_REGISTER:
2088 return netvsc_register_vf(event_dev);
2089 case NETDEV_UNREGISTER:
2090 return netvsc_unregister_vf(event_dev);
2091 case NETDEV_UP:
stephen hemminger7b83f522017-08-07 11:30:00 -07002092 return netvsc_vf_up(event_dev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07002093 case NETDEV_DOWN:
stephen hemminger7b83f522017-08-07 11:30:00 -07002094 return netvsc_vf_down(event_dev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07002095 default:
2096 return NOTIFY_DONE;
2097 }
2098}
2099
2100static struct notifier_block netvsc_netdev_notifier = {
2101 .notifier_call = netvsc_netdev_event,
2102};
2103
K. Y. Srinivasana9869c92011-05-12 19:35:17 -07002104static void __exit netvsc_drv_exit(void)
Hank Janssenfceaf242009-07-13 15:34:54 -07002105{
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07002106 unregister_netdevice_notifier(&netvsc_netdev_notifier);
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07002107 vmbus_driver_unregister(&netvsc_drv);
Hank Janssenfceaf242009-07-13 15:34:54 -07002108}
2109
K. Y. Srinivasan1fde28c2011-05-12 19:35:16 -07002110static int __init netvsc_drv_init(void)
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07002111{
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07002112 int ret;
2113
Haiyang Zhangfa85a6c2012-07-25 08:08:41 +00002114 if (ring_size < RING_SIZE_MIN) {
2115 ring_size = RING_SIZE_MIN;
2116 pr_info("Increased ring_size to %d (min allowed)\n",
2117 ring_size);
2118 }
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07002119 ret = vmbus_driver_register(&netvsc_drv);
2120
2121 if (ret)
2122 return ret;
2123
2124 register_netdevice_notifier(&netvsc_netdev_notifier);
2125 return 0;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07002126}
2127
Hank Janssen26c14cc2010-02-11 23:02:42 +00002128MODULE_LICENSE("GPL");
Stephen Hemminger7880fc52010-05-04 09:58:52 -07002129MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
Hank Janssenfceaf242009-07-13 15:34:54 -07002130
K. Y. Srinivasan1fde28c2011-05-12 19:35:16 -07002131module_init(netvsc_drv_init);
K. Y. Srinivasana9869c92011-05-12 19:35:17 -07002132module_exit(netvsc_drv_exit);