blob: 262486ce8e2a79ed0fc1e3f74a895374a8de933d [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>
37
Hank Janssenfceaf242009-07-13 15:34:54 -070038#include <net/arp.h>
39#include <net/route.h>
40#include <net/sock.h>
41#include <net/pkt_sched.h>
Michael Kelley8eb1b3c2017-05-30 11:36:56 -070042#include <net/checksum.h>
43#include <net/ip6_checksum.h>
K. Y. Srinivasan3f335ea2011-05-12 19:34:15 -070044
K. Y. Srinivasan5ca72522011-05-12 19:34:37 -070045#include "hyperv_net.h"
Hank Janssenfceaf242009-07-13 15:34:54 -070046
Haiyang Zhangfa85a6c2012-07-25 08:08:41 +000047#define RING_SIZE_MIN 64
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +010048#define LINKCHANGE_INT (2 * HZ)
stephen hemmingera50af862016-12-06 13:43:54 -080049
Hank Janssen99c8da02010-10-12 10:45:23 -070050static int ring_size = 128;
Stephen Hemminger450d7a42010-05-04 09:58:53 -070051module_param(ring_size, int, S_IRUGO);
52MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)");
Hank Janssenfceaf242009-07-13 15:34:54 -070053
Simon Xiao3f300ff2015-04-28 01:05:17 -070054static const u32 default_msg = NETIF_MSG_DRV | NETIF_MSG_PROBE |
55 NETIF_MSG_LINK | NETIF_MSG_IFUP |
56 NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR |
57 NETIF_MSG_TX_ERR;
58
59static int debug = -1;
60module_param(debug, int, S_IRUGO);
61MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
62
Greg Kroah-Hartman4e9bfef2009-07-15 12:45:20 -070063static void netvsc_set_multicast_list(struct net_device *net)
Hank Janssenfceaf242009-07-13 15:34:54 -070064{
Wenqi Ma792df872012-04-19 00:39:37 +000065 struct net_device_context *net_device_ctx = netdev_priv(net);
stephen hemminger4f19c0d2017-06-07 15:53:49 -070066 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
Haiyang Zhangd426b2e2011-11-30 07:19:08 -080067
stephen hemminger4f19c0d2017-06-07 15:53:49 -070068 rndis_filter_update(nvdev);
Hank Janssenfceaf242009-07-13 15:34:54 -070069}
70
Hank Janssenfceaf242009-07-13 15:34:54 -070071static int netvsc_open(struct net_device *net)
72{
Haiyang Zhang53fa1a62017-06-21 16:40:47 -070073 struct net_device_context *ndev_ctx = netdev_priv(net);
stephen hemminger79e8cbe2017-07-19 11:53:13 -070074 struct netvsc_device *nvdev = rtnl_dereference(ndev_ctx->nvdev);
Haiyang Zhang891de742014-02-12 16:54:27 -080075 struct rndis_device *rdev;
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -070076 int ret = 0;
Hank Janssenfceaf242009-07-13 15:34:54 -070077
Haiyang Zhang891de742014-02-12 16:54:27 -080078 netif_carrier_off(net);
79
Haiyang Zhangd515d0f2011-09-28 13:24:15 -070080 /* Open up the device */
Vitaly Kuznetsov2f5fa6c2016-06-03 17:51:00 +020081 ret = rndis_filter_open(nvdev);
Haiyang Zhangd515d0f2011-09-28 13:24:15 -070082 if (ret != 0) {
83 netdev_err(net, "unable to open device (ret %d).\n", ret);
84 return ret;
Hank Janssenfceaf242009-07-13 15:34:54 -070085 }
86
Haiyang Zhang2de85302015-07-13 13:09:16 -070087 netif_tx_wake_all_queues(net);
Haiyang Zhangd515d0f2011-09-28 13:24:15 -070088
Haiyang Zhang891de742014-02-12 16:54:27 -080089 rdev = nvdev->extension;
Haiyang Zhang53fa1a62017-06-21 16:40:47 -070090 if (!rdev->link_state && !ndev_ctx->datapath)
Haiyang Zhang891de742014-02-12 16:54:27 -080091 netif_carrier_on(net);
92
Hank Janssenfceaf242009-07-13 15:34:54 -070093 return ret;
94}
95
Hank Janssenfceaf242009-07-13 15:34:54 -070096static int netvsc_close(struct net_device *net)
97{
Hank Janssenfceaf242009-07-13 15:34:54 -070098 struct net_device_context *net_device_ctx = netdev_priv(net);
stephen hemminger545a8e72017-03-22 14:51:00 -070099 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700100 int ret;
stephen hemminger40975962017-06-08 16:21:19 -0700101 u32 aread, i, msec = 10, retry = 0, retry_max = 20;
Haiyang Zhang2de85302015-07-13 13:09:16 -0700102 struct vmbus_channel *chn;
Hank Janssenfceaf242009-07-13 15:34:54 -0700103
Haiyang Zhang0a282532012-02-02 07:17:59 +0000104 netif_tx_disable(net);
Hank Janssenfceaf242009-07-13 15:34:54 -0700105
Vitaly Kuznetsov2f5fa6c2016-06-03 17:51:00 +0200106 ret = rndis_filter_close(nvdev);
Haiyang Zhang2de85302015-07-13 13:09:16 -0700107 if (ret != 0) {
Hank Jansseneb335bc2011-03-29 13:58:48 -0700108 netdev_err(net, "unable to close device (ret %d).\n", ret);
Haiyang Zhang2de85302015-07-13 13:09:16 -0700109 return ret;
110 }
111
112 /* Ensure pending bytes in ring are read */
113 while (true) {
114 aread = 0;
115 for (i = 0; i < nvdev->num_chn; i++) {
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800116 chn = nvdev->chan_table[i].channel;
Haiyang Zhang2de85302015-07-13 13:09:16 -0700117 if (!chn)
118 continue;
119
stephen hemminger40975962017-06-08 16:21:19 -0700120 aread = hv_get_bytes_to_read(&chn->inbound);
Haiyang Zhang2de85302015-07-13 13:09:16 -0700121 if (aread)
122 break;
123
stephen hemminger40975962017-06-08 16:21:19 -0700124 aread = hv_get_bytes_to_read(&chn->outbound);
Haiyang Zhang2de85302015-07-13 13:09:16 -0700125 if (aread)
126 break;
127 }
128
129 retry++;
130 if (retry > retry_max || aread == 0)
131 break;
132
133 msleep(msec);
134
135 if (msec < 1000)
136 msec *= 2;
137 }
138
139 if (aread) {
140 netdev_err(net, "Ring buffer not empty after closing rndis\n");
141 ret = -ETIMEDOUT;
142 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700143
Hank Janssenfceaf242009-07-13 15:34:54 -0700144 return ret;
145}
146
KY Srinivasan8a002512014-03-08 19:23:14 -0800147static void *init_ppi_data(struct rndis_message *msg, u32 ppi_size,
148 int pkt_type)
149{
150 struct rndis_packet *rndis_pkt;
151 struct rndis_per_packet_info *ppi;
152
153 rndis_pkt = &msg->msg.pkt;
154 rndis_pkt->data_offset += ppi_size;
155
156 ppi = (struct rndis_per_packet_info *)((void *)rndis_pkt +
157 rndis_pkt->per_pkt_info_offset + rndis_pkt->per_pkt_info_len);
158
159 ppi->size = ppi_size;
160 ppi->type = pkt_type;
161 ppi->ppi_offset = sizeof(struct rndis_per_packet_info);
162
163 rndis_pkt->per_pkt_info_len += ppi_size;
164
165 return ppi;
166}
167
Haiyang Zhangf72860a2017-04-12 11:45:18 -0700168/* Azure hosts don't support non-TCP port numbers in hashing yet. We compute
169 * hash for non-TCP traffic with only IP numbers.
170 */
171static inline u32 netvsc_get_hash(struct sk_buff *skb, struct sock *sk)
172{
173 struct flow_keys flow;
174 u32 hash;
175 static u32 hashrnd __read_mostly;
176
177 net_get_random_once(&hashrnd, sizeof(hashrnd));
178
179 if (!skb_flow_dissect_flow_keys(skb, &flow, 0))
180 return 0;
181
182 if (flow.basic.ip_proto == IPPROTO_TCP) {
183 return skb_get_hash(skb);
184 } else {
185 if (flow.basic.n_proto == htons(ETH_P_IP))
186 hash = jhash2((u32 *)&flow.addrs.v4addrs, 2, hashrnd);
187 else if (flow.basic.n_proto == htons(ETH_P_IPV6))
188 hash = jhash2((u32 *)&flow.addrs.v6addrs, 8, hashrnd);
189 else
190 hash = 0;
191
192 skb_set_hash(skb, hash, PKT_HASH_TYPE_L3);
193 }
194
195 return hash;
196}
197
Haiyang Zhang8db91f62017-04-12 11:35:05 -0700198static inline int netvsc_get_tx_queue(struct net_device *ndev,
199 struct sk_buff *skb, int old_idx)
200{
201 const struct net_device_context *ndc = netdev_priv(ndev);
202 struct sock *sk = skb->sk;
203 int q_idx;
204
Haiyang Zhangf72860a2017-04-12 11:45:18 -0700205 q_idx = ndc->tx_send_table[netvsc_get_hash(skb, sk) &
Haiyang Zhang8db91f62017-04-12 11:35:05 -0700206 (VRSS_SEND_TAB_SIZE - 1)];
207
208 /* If queue index changed record the new value */
209 if (q_idx != old_idx &&
210 sk && sk_fullsock(sk) && rcu_access_pointer(sk->sk_dst_cache))
211 sk_tx_queue_set(sk, q_idx);
212
213 return q_idx;
214}
215
stephen hemmingerd8e18ee2017-01-24 13:06:05 -0800216/*
217 * Select queue for transmit.
218 *
219 * If a valid queue has already been assigned, then use that.
220 * Otherwise compute tx queue based on hash and the send table.
221 *
222 * This is basically similar to default (__netdev_pick_tx) with the added step
223 * of using the host send_table when no other queue has been assigned.
224 *
225 * TODO support XPS - but get_xps_queue not exported
226 */
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700227static u16 netvsc_select_queue(struct net_device *ndev, struct sk_buff *skb,
228 void *accel_priv, select_queue_fallback_t fallback)
229{
stephen hemminger7ce10122017-03-09 14:58:29 -0800230 unsigned int num_tx_queues = ndev->real_num_tx_queues;
Haiyang Zhang8db91f62017-04-12 11:35:05 -0700231 int q_idx = sk_tx_queue_get(skb->sk);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700232
Haiyang Zhang8db91f62017-04-12 11:35:05 -0700233 if (q_idx < 0 || skb->ooo_okay) {
234 /* If forwarding a packet, we use the recorded queue when
235 * available for better cache locality.
236 */
237 if (skb_rx_queue_recorded(skb))
238 q_idx = skb_get_rx_queue(skb);
239 else
240 q_idx = netvsc_get_tx_queue(ndev, skb, q_idx);
stephen hemmingerd8e18ee2017-01-24 13:06:05 -0800241 }
242
Haiyang Zhang8db91f62017-04-12 11:35:05 -0700243 while (unlikely(q_idx >= num_tx_queues))
244 q_idx -= num_tx_queues;
245
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700246 return q_idx;
247}
248
KY Srinivasan54a73572014-03-08 19:23:13 -0800249static u32 fill_pg_buf(struct page *page, u32 offset, u32 len,
250 struct hv_page_buffer *pb)
251{
252 int j = 0;
253
254 /* Deal with compund pages by ignoring unused part
255 * of the page.
256 */
257 page += (offset >> PAGE_SHIFT);
258 offset &= ~PAGE_MASK;
259
260 while (len > 0) {
261 unsigned long bytes;
262
263 bytes = PAGE_SIZE - offset;
264 if (bytes > len)
265 bytes = len;
266 pb[j].pfn = page_to_pfn(page);
267 pb[j].offset = offset;
268 pb[j].len = bytes;
269
270 offset += bytes;
271 len -= bytes;
272
273 if (offset == PAGE_SIZE && len) {
274 page++;
275 offset = 0;
276 j++;
277 }
278 }
279
280 return j + 1;
281}
282
KY Srinivasan8a002512014-03-08 19:23:14 -0800283static u32 init_page_array(void *hdr, u32 len, struct sk_buff *skb,
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800284 struct hv_netvsc_packet *packet,
285 struct hv_page_buffer **page_buf)
KY Srinivasan54a73572014-03-08 19:23:13 -0800286{
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800287 struct hv_page_buffer *pb = *page_buf;
KY Srinivasan54a73572014-03-08 19:23:13 -0800288 u32 slots_used = 0;
289 char *data = skb->data;
290 int frags = skb_shinfo(skb)->nr_frags;
291 int i;
292
293 /* The packet is laid out thus:
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700294 * 1. hdr: RNDIS header and PPI
KY Srinivasan54a73572014-03-08 19:23:13 -0800295 * 2. skb linear data
296 * 3. skb fragment data
297 */
298 if (hdr != NULL)
299 slots_used += fill_pg_buf(virt_to_page(hdr),
300 offset_in_page(hdr),
301 len, &pb[slots_used]);
302
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700303 packet->rmsg_size = len;
304 packet->rmsg_pgcnt = slots_used;
305
KY Srinivasan54a73572014-03-08 19:23:13 -0800306 slots_used += fill_pg_buf(virt_to_page(data),
307 offset_in_page(data),
308 skb_headlen(skb), &pb[slots_used]);
309
310 for (i = 0; i < frags; i++) {
311 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
312
313 slots_used += fill_pg_buf(skb_frag_page(frag),
314 frag->page_offset,
315 skb_frag_size(frag), &pb[slots_used]);
316 }
KY Srinivasan8a002512014-03-08 19:23:14 -0800317 return slots_used;
KY Srinivasan54a73572014-03-08 19:23:13 -0800318}
319
stephen hemminger60b86662017-06-08 16:21:18 -0700320/* Estimate number of page buffers neede to transmit
321 * Need at most 2 for RNDIS header plus skb body and fragments.
322 */
323static unsigned int netvsc_get_slots(const struct sk_buff *skb)
KY Srinivasan54a73572014-03-08 19:23:13 -0800324{
stephen hemminger60b86662017-06-08 16:21:18 -0700325 return PFN_UP(offset_in_page(skb->data) + skb_headlen(skb))
326 + skb_shinfo(skb)->nr_frags
327 + 2;
KY Srinivasan54a73572014-03-08 19:23:13 -0800328}
329
stephen hemminger23312a32017-01-24 13:05:59 -0800330static u32 net_checksum_info(struct sk_buff *skb)
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800331{
stephen hemminger23312a32017-01-24 13:05:59 -0800332 if (skb->protocol == htons(ETH_P_IP)) {
333 struct iphdr *ip = ip_hdr(skb);
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800334
stephen hemminger23312a32017-01-24 13:05:59 -0800335 if (ip->protocol == IPPROTO_TCP)
336 return TRANSPORT_INFO_IPV4_TCP;
337 else if (ip->protocol == IPPROTO_UDP)
338 return TRANSPORT_INFO_IPV4_UDP;
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800339 } else {
stephen hemminger23312a32017-01-24 13:05:59 -0800340 struct ipv6hdr *ip6 = ipv6_hdr(skb);
341
342 if (ip6->nexthdr == IPPROTO_TCP)
343 return TRANSPORT_INFO_IPV6_TCP;
Mohammed Gamal37b9dfa2017-07-24 10:57:26 -0700344 else if (ip6->nexthdr == IPPROTO_UDP)
stephen hemminger23312a32017-01-24 13:05:59 -0800345 return TRANSPORT_INFO_IPV6_UDP;
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800346 }
347
stephen hemminger23312a32017-01-24 13:05:59 -0800348 return TRANSPORT_INFO_NOT_IP;
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800349}
350
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700351static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
Hank Janssenfceaf242009-07-13 15:34:54 -0700352{
Hank Janssenfceaf242009-07-13 15:34:54 -0700353 struct net_device_context *net_device_ctx = netdev_priv(net);
Vitaly Kuznetsov981a1bd2015-04-08 17:54:05 +0200354 struct hv_netvsc_packet *packet = NULL;
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700355 int ret;
KY Srinivasan8a002512014-03-08 19:23:14 -0800356 unsigned int num_data_pgs;
357 struct rndis_message *rndis_msg;
358 struct rndis_packet *rndis_pkt;
359 u32 rndis_msg_size;
KY Srinivasan8a002512014-03-08 19:23:14 -0800360 struct rndis_per_packet_info *ppi;
Haiyang Zhang307f0992014-05-21 12:55:39 -0700361 u32 hash;
KY Srinivasanb08cc792015-03-29 21:08:42 -0700362 struct hv_page_buffer page_buf[MAX_PAGE_BUFFER_COUNT];
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800363 struct hv_page_buffer *pb = page_buf;
Hank Janssenfceaf242009-07-13 15:34:54 -0700364
stephen hemminger60b86662017-06-08 16:21:18 -0700365 /* We can only transmit MAX_PAGE_BUFFER_COUNT number
Vitaly Kuznetsove88f7e02015-04-08 17:54:06 +0200366 * of pages in a single packet. If skb is scattered around
367 * more pages we try linearizing it.
KY Srinivasan54a73572014-03-08 19:23:13 -0800368 */
stephen hemminger60b86662017-06-08 16:21:18 -0700369 num_data_pgs = netvsc_get_slots(skb);
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700370 if (unlikely(num_data_pgs > MAX_PAGE_BUFFER_COUNT)) {
Stephen Hemminger4323b472016-08-23 12:17:57 -0700371 ++net_device_ctx->eth_stats.tx_scattered;
372
373 if (skb_linearize(skb))
374 goto no_memory;
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700375
stephen hemminger60b86662017-06-08 16:21:18 -0700376 num_data_pgs = netvsc_get_slots(skb);
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700377 if (num_data_pgs > MAX_PAGE_BUFFER_COUNT) {
Stephen Hemminger4323b472016-08-23 12:17:57 -0700378 ++net_device_ctx->eth_stats.tx_too_big;
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700379 goto drop;
380 }
KY Srinivasan54a73572014-03-08 19:23:13 -0800381 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700382
KY Srinivasanc0eb4542015-12-01 16:43:10 -0800383 /*
384 * Place the rndis header in the skb head room and
385 * the skb->cb will be used for hv_netvsc_packet
386 * structure.
387 */
388 ret = skb_cow_head(skb, RNDIS_AND_PPI_SIZE);
Stephen Hemminger4323b472016-08-23 12:17:57 -0700389 if (ret)
390 goto no_memory;
391
KY Srinivasanc0eb4542015-12-01 16:43:10 -0800392 /* Use the skb control buffer for building up the packet */
393 BUILD_BUG_ON(sizeof(struct hv_netvsc_packet) >
394 FIELD_SIZEOF(struct sk_buff, cb));
395 packet = (struct hv_netvsc_packet *)skb->cb;
Hank Janssenfceaf242009-07-13 15:34:54 -0700396
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700397 packet->q_idx = skb_get_queue_mapping(skb);
398
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800399 packet->total_data_buflen = skb->len;
stephen hemminger793e3952017-01-24 13:06:12 -0800400 packet->total_bytes = skb->len;
401 packet->total_packets = 1;
Hank Janssenfceaf242009-07-13 15:34:54 -0700402
KY Srinivasanc0eb4542015-12-01 16:43:10 -0800403 rndis_msg = (struct rndis_message *)skb->head;
KY Srinivasanb08cc792015-03-29 21:08:42 -0700404
KY Srinivasan24476762015-12-01 16:43:06 -0800405 memset(rndis_msg, 0, RNDIS_AND_PPI_SIZE);
Hank Janssenfceaf242009-07-13 15:34:54 -0700406
KY Srinivasan8a002512014-03-08 19:23:14 -0800407 /* Add the rndis header */
KY Srinivasan8a002512014-03-08 19:23:14 -0800408 rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET;
409 rndis_msg->msg_len = packet->total_data_buflen;
410 rndis_pkt = &rndis_msg->msg.pkt;
411 rndis_pkt->data_offset = sizeof(struct rndis_packet);
412 rndis_pkt->data_len = packet->total_data_buflen;
413 rndis_pkt->per_pkt_info_offset = sizeof(struct rndis_packet);
414
415 rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet);
416
Haiyang Zhang307f0992014-05-21 12:55:39 -0700417 hash = skb_get_hash_raw(skb);
418 if (hash != 0 && net->real_num_tx_queues > 1) {
419 rndis_msg_size += NDIS_HASH_PPI_SIZE;
420 ppi = init_ppi_data(rndis_msg, NDIS_HASH_PPI_SIZE,
421 NBL_HASH_VALUE);
422 *(u32 *)((void *)ppi + ppi->ppi_offset) = hash;
423 }
424
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700425 if (skb_vlan_tag_present(skb)) {
KY Srinivasan8a002512014-03-08 19:23:14 -0800426 struct ndis_pkt_8021q_info *vlan;
427
428 rndis_msg_size += NDIS_VLAN_PPI_SIZE;
429 ppi = init_ppi_data(rndis_msg, NDIS_VLAN_PPI_SIZE,
430 IEEE_8021Q_INFO);
431 vlan = (struct ndis_pkt_8021q_info *)((void *)ppi +
432 ppi->ppi_offset);
KY Srinivasan760d1e32015-12-01 16:43:19 -0800433 vlan->vlanid = skb->vlan_tci & VLAN_VID_MASK;
434 vlan->pri = (skb->vlan_tci & VLAN_PRIO_MASK) >>
KY Srinivasan8a002512014-03-08 19:23:14 -0800435 VLAN_PRIO_SHIFT;
436 }
437
stephen hemminger23312a32017-01-24 13:05:59 -0800438 if (skb_is_gso(skb)) {
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700439 struct ndis_tcp_lso_info *lso_info;
440
441 rndis_msg_size += NDIS_LSO_PPI_SIZE;
442 ppi = init_ppi_data(rndis_msg, NDIS_LSO_PPI_SIZE,
443 TCP_LARGESEND_PKTINFO);
444
445 lso_info = (struct ndis_tcp_lso_info *)((void *)ppi +
446 ppi->ppi_offset);
447
448 lso_info->lso_v2_transmit.type = NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
stephen hemminger23312a32017-01-24 13:05:59 -0800449 if (skb->protocol == htons(ETH_P_IP)) {
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700450 lso_info->lso_v2_transmit.ip_version =
451 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
452 ip_hdr(skb)->tot_len = 0;
453 ip_hdr(skb)->check = 0;
454 tcp_hdr(skb)->check =
455 ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
456 ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
457 } else {
458 lso_info->lso_v2_transmit.ip_version =
459 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
460 ipv6_hdr(skb)->payload_len = 0;
461 tcp_hdr(skb)->check =
462 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
463 &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
464 }
stephen hemminger23312a32017-01-24 13:05:59 -0800465 lso_info->lso_v2_transmit.tcp_header_offset = skb_transport_offset(skb);
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700466 lso_info->lso_v2_transmit.mss = skb_shinfo(skb)->gso_size;
stephen hemmingerad19bc82016-10-11 14:03:07 -0700467 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
stephen hemminger23312a32017-01-24 13:05:59 -0800468 if (net_checksum_info(skb) & net_device_ctx->tx_checksum_mask) {
469 struct ndis_tcp_ip_checksum_info *csum_info;
470
stephen hemmingerad19bc82016-10-11 14:03:07 -0700471 rndis_msg_size += NDIS_CSUM_PPI_SIZE;
472 ppi = init_ppi_data(rndis_msg, NDIS_CSUM_PPI_SIZE,
473 TCPIP_CHKSUM_PKTINFO);
474
475 csum_info = (struct ndis_tcp_ip_checksum_info *)((void *)ppi +
476 ppi->ppi_offset);
477
stephen hemminger23312a32017-01-24 13:05:59 -0800478 csum_info->transmit.tcp_header_offset = skb_transport_offset(skb);
479
480 if (skb->protocol == htons(ETH_P_IP)) {
stephen hemmingerad19bc82016-10-11 14:03:07 -0700481 csum_info->transmit.is_ipv4 = 1;
stephen hemminger23312a32017-01-24 13:05:59 -0800482
483 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
484 csum_info->transmit.tcp_checksum = 1;
485 else
486 csum_info->transmit.udp_checksum = 1;
487 } else {
stephen hemmingerad19bc82016-10-11 14:03:07 -0700488 csum_info->transmit.is_ipv6 = 1;
489
stephen hemminger23312a32017-01-24 13:05:59 -0800490 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
491 csum_info->transmit.tcp_checksum = 1;
492 else
493 csum_info->transmit.udp_checksum = 1;
494 }
stephen hemmingerad19bc82016-10-11 14:03:07 -0700495 } else {
stephen hemminger23312a32017-01-24 13:05:59 -0800496 /* Can't do offload of this type of checksum */
stephen hemmingerad19bc82016-10-11 14:03:07 -0700497 if (skb_checksum_help(skb))
498 goto drop;
499 }
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700500 }
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800501
KY Srinivasan8a002512014-03-08 19:23:14 -0800502 /* Start filling in the page buffers with the rndis hdr */
503 rndis_msg->msg_len += rndis_msg_size;
Haiyang Zhang942396b2014-10-22 13:47:18 -0700504 packet->total_data_buflen = rndis_msg->msg_len;
KY Srinivasan8a002512014-03-08 19:23:14 -0800505 packet->page_buf_cnt = init_page_array(rndis_msg, rndis_msg_size,
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800506 skb, packet, &pb);
KY Srinivasan8a002512014-03-08 19:23:14 -0800507
sixiao@microsoft.com76d13b52016-02-17 16:43:59 -0800508 /* timestamp packet in software */
509 skb_tx_timestamp(skb);
stephen hemminger2a926f72017-07-19 11:53:17 -0700510
511 ret = netvsc_send(net_device_ctx, packet, rndis_msg, &pb, skb);
stephen hemminger793e3952017-01-24 13:06:12 -0800512 if (likely(ret == 0))
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700513 return NETDEV_TX_OK;
Stephen Hemminger4323b472016-08-23 12:17:57 -0700514
515 if (ret == -EAGAIN) {
516 ++net_device_ctx->eth_stats.tx_busy;
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700517 return NETDEV_TX_BUSY;
Stephen Hemminger4323b472016-08-23 12:17:57 -0700518 }
519
520 if (ret == -ENOSPC)
521 ++net_device_ctx->eth_stats.tx_no_space;
Hank Janssenfceaf242009-07-13 15:34:54 -0700522
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700523drop:
524 dev_kfree_skb_any(skb);
525 net->stats.tx_dropped++;
526
527 return NETDEV_TX_OK;
Stephen Hemminger4323b472016-08-23 12:17:57 -0700528
529no_memory:
530 ++net_device_ctx->eth_stats.tx_no_memory;
531 goto drop;
Hank Janssenfceaf242009-07-13 15:34:54 -0700532}
Hank Janssen3e189512010-03-04 22:11:00 +0000533/*
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700534 * netvsc_linkstatus_callback - Link up/down notification
535 */
K. Y. Srinivasan90ef1172011-05-12 19:34:50 -0700536void netvsc_linkstatus_callback(struct hv_device *device_obj,
Haiyang Zhang3a494e72014-06-19 18:34:36 -0700537 struct rndis_message *resp)
Hank Janssenfceaf242009-07-13 15:34:54 -0700538{
Haiyang Zhang3a494e72014-06-19 18:34:36 -0700539 struct rndis_indicate_status *indicate = &resp->msg.indicate_status;
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -0700540 struct net_device *net;
Haiyang Zhangc996edc2011-04-06 15:18:00 -0700541 struct net_device_context *ndev_ctx;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +0100542 struct netvsc_reconfig *event;
543 unsigned long flags;
544
Haiyang Zhang7f5d5af2016-08-04 10:42:15 -0700545 net = hv_get_drvdata(device_obj);
546
547 if (!net)
548 return;
549
550 ndev_ctx = netdev_priv(net);
551
552 /* Update the physical link speed when changing to another vSwitch */
553 if (indicate->status == RNDIS_STATUS_LINK_SPEED_CHANGE) {
554 u32 speed;
555
556 speed = *(u32 *)((void *)indicate + indicate->
557 status_buf_offset) / 10000;
558 ndev_ctx->speed = speed;
559 return;
560 }
561
562 /* Handle these link change statuses below */
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +0100563 if (indicate->status != RNDIS_STATUS_NETWORK_CHANGE &&
564 indicate->status != RNDIS_STATUS_MEDIA_CONNECT &&
565 indicate->status != RNDIS_STATUS_MEDIA_DISCONNECT)
566 return;
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -0700567
Haiyang Zhang7f5d5af2016-08-04 10:42:15 -0700568 if (net->reg_state != NETREG_REGISTERED)
Hank Janssenfceaf242009-07-13 15:34:54 -0700569 return;
Hank Janssenfceaf242009-07-13 15:34:54 -0700570
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +0100571 event = kzalloc(sizeof(*event), GFP_ATOMIC);
572 if (!event)
573 return;
574 event->event = indicate->status;
575
576 spin_lock_irqsave(&ndev_ctx->lock, flags);
577 list_add_tail(&event->list, &ndev_ctx->reconfig_events);
578 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
579
580 schedule_delayed_work(&ndev_ctx->dwork, 0);
Hank Janssenfceaf242009-07-13 15:34:54 -0700581}
582
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700583static struct sk_buff *netvsc_alloc_recv_skb(struct net_device *net,
stephen hemmingere91e7dd2017-02-27 10:26:51 -0800584 struct napi_struct *napi,
stephen hemmingerdc54a082017-01-24 13:06:08 -0800585 const struct ndis_tcp_ip_checksum_info *csum_info,
586 const struct ndis_pkt_8021q_info *vlan,
587 void *data, u32 buflen)
Hank Janssenfceaf242009-07-13 15:34:54 -0700588{
Hank Janssenfceaf242009-07-13 15:34:54 -0700589 struct sk_buff *skb;
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -0700590
stephen hemmingere91e7dd2017-02-27 10:26:51 -0800591 skb = napi_alloc_skb(napi, buflen);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700592 if (!skb)
593 return skb;
Hank Janssenfceaf242009-07-13 15:34:54 -0700594
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700595 /*
596 * Copy to skb. This copy is needed here since the memory pointed by
597 * hv_netvsc_packet cannot be deallocated
598 */
Johannes Berg59ae1d12017-06-16 14:29:20 +0200599 skb_put_data(skb, data, buflen);
Hank Janssenfceaf242009-07-13 15:34:54 -0700600
601 skb->protocol = eth_type_trans(skb, net);
Stephen Hemmingere52fed72016-10-23 21:32:47 -0700602
603 /* skb is already created with CHECKSUM_NONE */
604 skb_checksum_none_assert(skb);
605
606 /*
607 * In Linux, the IP checksum is always checked.
608 * Do L4 checksum offload if enabled and present.
609 */
610 if (csum_info && (net->features & NETIF_F_RXCSUM)) {
611 if (csum_info->receive.tcp_checksum_succeeded ||
612 csum_info->receive.udp_checksum_succeeded)
KY Srinivasane3d605e2014-03-08 19:23:16 -0800613 skb->ip_summed = CHECKSUM_UNNECESSARY;
KY Srinivasane3d605e2014-03-08 19:23:16 -0800614 }
615
stephen hemmingerdc54a082017-01-24 13:06:08 -0800616 if (vlan) {
617 u16 vlan_tci = vlan->vlanid | (vlan->pri << VLAN_PRIO_SHIFT);
618
Haiyang Zhang93725cb2013-06-17 15:36:49 -0700619 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
KY Srinivasan760d1e32015-12-01 16:43:19 -0800620 vlan_tci);
stephen hemmingerdc54a082017-01-24 13:06:08 -0800621 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700622
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700623 return skb;
624}
625
626/*
627 * netvsc_recv_callback - Callback when we receive a packet from the
628 * "wire" on the specified device.
629 */
stephen hemmingerdc54a082017-01-24 13:06:08 -0800630int netvsc_recv_callback(struct net_device *net,
631 struct vmbus_channel *channel,
632 void *data, u32 len,
633 const struct ndis_tcp_ip_checksum_info *csum_info,
634 const struct ndis_pkt_8021q_info *vlan)
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700635{
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200636 struct net_device_context *net_device_ctx = netdev_priv(net);
stephen hemminger545a8e72017-03-22 14:51:00 -0700637 struct netvsc_device *net_device;
stephen hemminger742fe542017-02-27 10:26:50 -0800638 u16 q_idx = channel->offermsg.offer.sub_channel_index;
stephen hemminger545a8e72017-03-22 14:51:00 -0700639 struct netvsc_channel *nvchan;
Stephen Hemmingerf207c102016-09-22 16:56:33 -0700640 struct net_device *vf_netdev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700641 struct sk_buff *skb;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700642 struct netvsc_stats *rx_stats;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700643
Stephen Hemminger9cbcc422016-09-22 16:56:34 -0700644 if (net->reg_state != NETREG_REGISTERED)
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700645 return NVSP_STAT_FAIL;
646
Stephen Hemminger9cbcc422016-09-22 16:56:34 -0700647 /*
648 * If necessary, inject this packet into the VF interface.
649 * On Hyper-V, multicast and brodcast packets are only delivered
650 * to the synthetic interface (after subjecting these to
651 * policy filters on the host). Deliver these via the VF
652 * interface in the guest.
653 */
stephen hemminger0719e722017-01-11 09:16:32 -0800654 rcu_read_lock();
stephen hemminger545a8e72017-03-22 14:51:00 -0700655 net_device = rcu_dereference(net_device_ctx->nvdev);
656 if (unlikely(!net_device))
657 goto drop;
658
659 nvchan = &net_device->chan_table[q_idx];
Stephen Hemmingerf207c102016-09-22 16:56:33 -0700660 vf_netdev = rcu_dereference(net_device_ctx->vf_netdev);
Stephen Hemminger9cbcc422016-09-22 16:56:34 -0700661 if (vf_netdev && (vf_netdev->flags & IFF_UP))
662 net = vf_netdev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700663
664 /* Allocate a skb - TODO direct I/O to pages? */
stephen hemmingere91e7dd2017-02-27 10:26:51 -0800665 skb = netvsc_alloc_recv_skb(net, &nvchan->napi,
666 csum_info, vlan, data, len);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700667 if (unlikely(!skb)) {
stephen hemminger545a8e72017-03-22 14:51:00 -0700668drop:
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700669 ++net->stats.rx_dropped;
stephen hemminger0719e722017-01-11 09:16:32 -0800670 rcu_read_unlock();
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700671 return NVSP_STAT_FAIL;
672 }
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700673
Stephen Hemminger9cbcc422016-09-22 16:56:34 -0700674 if (net != vf_netdev)
Simon Xiao6c80f3f2017-01-24 13:06:13 -0800675 skb_record_rx_queue(skb, q_idx);
Stephen Hemminger9cbcc422016-09-22 16:56:34 -0700676
677 /*
678 * Even if injecting the packet, record the statistics
679 * on the synthetic device because modifying the VF device
680 * statistics will not work correctly.
681 */
stephen hemminger742fe542017-02-27 10:26:50 -0800682 rx_stats = &nvchan->rx_stats;
sixiao@microsoft.com4b02b582015-05-15 02:33:03 -0700683 u64_stats_update_begin(&rx_stats->syncp);
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700684 rx_stats->packets++;
stephen hemmingerdc54a082017-01-24 13:06:08 -0800685 rx_stats->bytes += len;
Stephen Hemmingerf7ad75b2016-09-22 16:56:35 -0700686
687 if (skb->pkt_type == PACKET_BROADCAST)
688 ++rx_stats->broadcast;
689 else if (skb->pkt_type == PACKET_MULTICAST)
690 ++rx_stats->multicast;
sixiao@microsoft.com4b02b582015-05-15 02:33:03 -0700691 u64_stats_update_end(&rx_stats->syncp);
Stephen Hemminger9495c282010-03-09 17:42:17 -0800692
stephen hemminger742fe542017-02-27 10:26:50 -0800693 napi_gro_receive(&nvchan->napi, skb);
stephen hemminger0719e722017-01-11 09:16:32 -0800694 rcu_read_unlock();
Hank Janssenfceaf242009-07-13 15:34:54 -0700695
Hank Janssenfceaf242009-07-13 15:34:54 -0700696 return 0;
697}
698
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -0700699static void netvsc_get_drvinfo(struct net_device *net,
700 struct ethtool_drvinfo *info)
701{
Jiri Pirko7826d432013-01-06 00:44:26 +0000702 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
Jiri Pirko7826d432013-01-06 00:44:26 +0000703 strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -0700704}
705
Andrew Schwartzmeyer59995372015-02-26 16:27:14 -0800706static void netvsc_get_channels(struct net_device *net,
707 struct ethtool_channels *channel)
708{
709 struct net_device_context *net_device_ctx = netdev_priv(net);
stephen hemminger545a8e72017-03-22 14:51:00 -0700710 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
Andrew Schwartzmeyer59995372015-02-26 16:27:14 -0800711
712 if (nvdev) {
713 channel->max_combined = nvdev->max_chn;
714 channel->combined_count = nvdev->num_chn;
715 }
716}
717
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700718static int netvsc_set_channels(struct net_device *net,
719 struct ethtool_channels *channels)
720{
721 struct net_device_context *net_device_ctx = netdev_priv(net);
722 struct hv_device *dev = net_device_ctx->device_ctx;
stephen hemminger545a8e72017-03-22 14:51:00 -0700723 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
stephen hemminger7ca45932017-07-24 10:57:28 -0700724 unsigned int orig, count = channels->combined_count;
725 struct netvsc_device_info device_info;
stephen hemmingerea383bf2017-07-19 11:53:15 -0700726 bool was_opened;
stephen hemminger7ca45932017-07-24 10:57:28 -0700727 int ret = 0;
stephen hemminger2b018882017-01-24 13:06:03 -0800728
729 /* We do not support separate count for rx, tx, or other */
730 if (count == 0 ||
731 channels->rx_count || channels->tx_count || channels->other_count)
732 return -EINVAL;
733
Arnd Bergmannb92b7d32017-06-22 00:16:37 +0200734 if (count > net->num_tx_queues || count > VRSS_CHANNEL_MAX)
stephen hemminger2b018882017-01-24 13:06:03 -0800735 return -EINVAL;
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700736
stephen hemmingera0be4502017-03-22 14:51:01 -0700737 if (!nvdev || nvdev->destroy)
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700738 return -ENODEV;
739
stephen hemminger2b018882017-01-24 13:06:03 -0800740 if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5)
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700741 return -EINVAL;
742
stephen hemminger2b018882017-01-24 13:06:03 -0800743 if (count > nvdev->max_chn)
744 return -EINVAL;
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700745
stephen hemminger7ca45932017-07-24 10:57:28 -0700746 orig = nvdev->num_chn;
stephen hemmingerea383bf2017-07-19 11:53:15 -0700747 was_opened = rndis_filter_opened(nvdev);
748 if (was_opened)
749 rndis_filter_close(nvdev);
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700750
stephen hemminger2289f0a2017-01-24 13:06:10 -0800751 rndis_filter_device_remove(dev, nvdev);
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700752
stephen hemminger7ca45932017-07-24 10:57:28 -0700753 memset(&device_info, 0, sizeof(device_info));
754 device_info.num_chn = count;
755 device_info.ring_size = ring_size;
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700756
stephen hemminger7ca45932017-07-24 10:57:28 -0700757 nvdev = rndis_filter_device_add(dev, &device_info);
758 if (!IS_ERR(nvdev)) {
759 netif_set_real_num_tx_queues(net, nvdev->num_chn);
760 netif_set_real_num_rx_queues(net, nvdev->num_chn);
761 ret = PTR_ERR(nvdev);
762 } else {
763 device_info.num_chn = orig;
stephen hemminger7ca45932017-07-24 10:57:28 -0700764 rndis_filter_device_add(dev, &device_info);
765 }
766
stephen hemmingerea383bf2017-07-19 11:53:15 -0700767 if (was_opened)
768 rndis_filter_open(nvdev);
stephen hemminger163891d2017-03-22 14:50:58 -0700769
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +0200770 /* We may have missed link change notifications */
stephen hemminger1b019942017-07-19 11:53:12 -0700771 net_device_ctx->last_reconfig = 0;
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +0200772 schedule_delayed_work(&net_device_ctx->dwork, 0);
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700773
774 return ret;
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700775}
776
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100777static bool
778netvsc_validate_ethtool_ss_cmd(const struct ethtool_link_ksettings *cmd)
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800779{
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100780 struct ethtool_link_ksettings diff1 = *cmd;
781 struct ethtool_link_ksettings diff2 = {};
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800782
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100783 diff1.base.speed = 0;
784 diff1.base.duplex = 0;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800785 /* advertising and cmd are usually set */
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100786 ethtool_link_ksettings_zero_link_mode(&diff1, advertising);
787 diff1.base.cmd = 0;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800788 /* We set port to PORT_OTHER */
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100789 diff2.base.port = PORT_OTHER;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800790
791 return !memcmp(&diff1, &diff2, sizeof(diff1));
792}
793
794static void netvsc_init_settings(struct net_device *dev)
795{
796 struct net_device_context *ndc = netdev_priv(dev);
797
798 ndc->speed = SPEED_UNKNOWN;
Simon Xiaof3c9d40e2017-04-14 14:42:58 -0700799 ndc->duplex = DUPLEX_FULL;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800800}
801
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100802static int netvsc_get_link_ksettings(struct net_device *dev,
803 struct ethtool_link_ksettings *cmd)
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800804{
805 struct net_device_context *ndc = netdev_priv(dev);
806
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100807 cmd->base.speed = ndc->speed;
808 cmd->base.duplex = ndc->duplex;
809 cmd->base.port = PORT_OTHER;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800810
811 return 0;
812}
813
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100814static int netvsc_set_link_ksettings(struct net_device *dev,
815 const struct ethtool_link_ksettings *cmd)
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800816{
817 struct net_device_context *ndc = netdev_priv(dev);
818 u32 speed;
819
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100820 speed = cmd->base.speed;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800821 if (!ethtool_validate_speed(speed) ||
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100822 !ethtool_validate_duplex(cmd->base.duplex) ||
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800823 !netvsc_validate_ethtool_ss_cmd(cmd))
824 return -EINVAL;
825
826 ndc->speed = speed;
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100827 ndc->duplex = cmd->base.duplex;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800828
829 return 0;
830}
831
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800832static int netvsc_change_mtu(struct net_device *ndev, int mtu)
833{
834 struct net_device_context *ndevctx = netdev_priv(ndev);
stephen hemminger545a8e72017-03-22 14:51:00 -0700835 struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200836 struct hv_device *hdev = ndevctx->device_ctx;
stephen hemminger9749fed2017-07-19 11:53:16 -0700837 int orig_mtu = ndev->mtu;
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800838 struct netvsc_device_info device_info;
stephen hemmingerea383bf2017-07-19 11:53:15 -0700839 bool was_opened;
stephen hemminger9749fed2017-07-19 11:53:16 -0700840 int ret = 0;
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800841
stephen hemmingera0be4502017-03-22 14:51:01 -0700842 if (!nvdev || nvdev->destroy)
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800843 return -ENODEV;
844
stephen hemmingerea383bf2017-07-19 11:53:15 -0700845 netif_device_detach(ndev);
846 was_opened = rndis_filter_opened(nvdev);
847 if (was_opened)
848 rndis_filter_close(nvdev);
Haiyang Zhang2de85302015-07-13 13:09:16 -0700849
Andrew Schwartzmeyer8ebdcc52015-08-11 17:14:31 -0700850 memset(&device_info, 0, sizeof(device_info));
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800851 device_info.ring_size = ring_size;
stephen hemminger2b018882017-01-24 13:06:03 -0800852 device_info.num_chn = nvdev->num_chn;
Dexuan Cui152669b2017-03-02 13:00:53 +0000853
Dexuan Cui152669b2017-03-02 13:00:53 +0000854 rndis_filter_device_remove(hdev, nvdev);
855
Dexuan Cui152669b2017-03-02 13:00:53 +0000856 ndev->mtu = mtu;
857
stephen hemminger9749fed2017-07-19 11:53:16 -0700858 nvdev = rndis_filter_device_add(hdev, &device_info);
859 if (IS_ERR(nvdev)) {
860 ret = PTR_ERR(nvdev);
861
862 /* Attempt rollback to original MTU */
863 ndev->mtu = orig_mtu;
864 rndis_filter_device_add(hdev, &device_info);
865 }
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800866
stephen hemmingerea383bf2017-07-19 11:53:15 -0700867 if (was_opened)
868 rndis_filter_open(nvdev);
869
870 netif_device_attach(ndev);
stephen hemminger163891d2017-03-22 14:50:58 -0700871
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +0200872 /* We may have missed link change notifications */
873 schedule_delayed_work(&ndevctx->dwork, 0);
874
stephen hemminger9749fed2017-07-19 11:53:16 -0700875 return ret;
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800876}
877
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800878static void netvsc_get_stats64(struct net_device *net,
879 struct rtnl_link_stats64 *t)
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700880{
881 struct net_device_context *ndev_ctx = netdev_priv(net);
stephen hemminger776e7262017-04-14 14:42:57 -0700882 struct netvsc_device *nvdev = rcu_dereference_rtnl(ndev_ctx->nvdev);
Simon Xiao6c80f3f2017-01-24 13:06:13 -0800883 int i;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700884
Simon Xiao6c80f3f2017-01-24 13:06:13 -0800885 if (!nvdev)
886 return;
887
888 for (i = 0; i < nvdev->num_chn; i++) {
889 const struct netvsc_channel *nvchan = &nvdev->chan_table[i];
890 const struct netvsc_stats *stats;
891 u64 packets, bytes, multicast;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700892 unsigned int start;
893
Simon Xiao6c80f3f2017-01-24 13:06:13 -0800894 stats = &nvchan->tx_stats;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700895 do {
Simon Xiao6c80f3f2017-01-24 13:06:13 -0800896 start = u64_stats_fetch_begin_irq(&stats->syncp);
897 packets = stats->packets;
898 bytes = stats->bytes;
899 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700900
Simon Xiao6c80f3f2017-01-24 13:06:13 -0800901 t->tx_bytes += bytes;
902 t->tx_packets += packets;
903
904 stats = &nvchan->rx_stats;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700905 do {
Simon Xiao6c80f3f2017-01-24 13:06:13 -0800906 start = u64_stats_fetch_begin_irq(&stats->syncp);
907 packets = stats->packets;
908 bytes = stats->bytes;
909 multicast = stats->multicast + stats->broadcast;
910 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700911
Simon Xiao6c80f3f2017-01-24 13:06:13 -0800912 t->rx_bytes += bytes;
913 t->rx_packets += packets;
914 t->multicast += multicast;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700915 }
916
917 t->tx_dropped = net->stats.tx_dropped;
Simon Xiaob5124722017-02-17 11:36:20 -0800918 t->tx_errors = net->stats.tx_errors;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700919
920 t->rx_dropped = net->stats.rx_dropped;
921 t->rx_errors = net->stats.rx_errors;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700922}
Haiyang Zhang1ce09e82012-07-10 07:19:22 +0000923
924static int netvsc_set_mac_addr(struct net_device *ndev, void *p)
925{
Haiyang Zhang1ce09e82012-07-10 07:19:22 +0000926 struct sockaddr *addr = p;
Jianjun Kong9a4c8312013-01-18 16:52:09 +0000927 char save_adr[ETH_ALEN];
Haiyang Zhang1ce09e82012-07-10 07:19:22 +0000928 unsigned char save_aatype;
929 int err;
930
931 memcpy(save_adr, ndev->dev_addr, ETH_ALEN);
932 save_aatype = ndev->addr_assign_type;
933
934 err = eth_mac_addr(ndev, p);
935 if (err != 0)
936 return err;
937
Vitaly Kuznetsove834da9a2016-06-03 17:51:01 +0200938 err = rndis_filter_set_device_mac(ndev, addr->sa_data);
Haiyang Zhang1ce09e82012-07-10 07:19:22 +0000939 if (err != 0) {
940 /* roll back to saved MAC */
941 memcpy(ndev->dev_addr, save_adr, ETH_ALEN);
942 ndev->addr_assign_type = save_aatype;
943 }
944
945 return err;
946}
947
Stephen Hemminger4323b472016-08-23 12:17:57 -0700948static const struct {
949 char name[ETH_GSTRING_LEN];
950 u16 offset;
951} netvsc_stats[] = {
952 { "tx_scattered", offsetof(struct netvsc_ethtool_stats, tx_scattered) },
953 { "tx_no_memory", offsetof(struct netvsc_ethtool_stats, tx_no_memory) },
954 { "tx_no_space", offsetof(struct netvsc_ethtool_stats, tx_no_space) },
955 { "tx_too_big", offsetof(struct netvsc_ethtool_stats, tx_too_big) },
956 { "tx_busy", offsetof(struct netvsc_ethtool_stats, tx_busy) },
957};
958
Simon Xiao6c80f3f2017-01-24 13:06:13 -0800959#define NETVSC_GLOBAL_STATS_LEN ARRAY_SIZE(netvsc_stats)
960
961/* 4 statistics per queue (rx/tx packets/bytes) */
962#define NETVSC_QUEUE_STATS_LEN(dev) ((dev)->num_chn * 4)
963
Stephen Hemminger4323b472016-08-23 12:17:57 -0700964static int netvsc_get_sset_count(struct net_device *dev, int string_set)
965{
Simon Xiao6c80f3f2017-01-24 13:06:13 -0800966 struct net_device_context *ndc = netdev_priv(dev);
stephen hemmingerfbd4c7e2017-06-07 15:53:47 -0700967 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
stephen hemminger545a8e72017-03-22 14:51:00 -0700968
969 if (!nvdev)
970 return -ENODEV;
Simon Xiao6c80f3f2017-01-24 13:06:13 -0800971
Stephen Hemminger4323b472016-08-23 12:17:57 -0700972 switch (string_set) {
973 case ETH_SS_STATS:
Simon Xiao6c80f3f2017-01-24 13:06:13 -0800974 return NETVSC_GLOBAL_STATS_LEN + NETVSC_QUEUE_STATS_LEN(nvdev);
Stephen Hemminger4323b472016-08-23 12:17:57 -0700975 default:
976 return -EINVAL;
977 }
978}
979
980static void netvsc_get_ethtool_stats(struct net_device *dev,
981 struct ethtool_stats *stats, u64 *data)
982{
983 struct net_device_context *ndc = netdev_priv(dev);
stephen hemminger545a8e72017-03-22 14:51:00 -0700984 struct netvsc_device *nvdev = rcu_dereference(ndc->nvdev);
Stephen Hemminger4323b472016-08-23 12:17:57 -0700985 const void *nds = &ndc->eth_stats;
Simon Xiao6c80f3f2017-01-24 13:06:13 -0800986 const struct netvsc_stats *qstats;
987 unsigned int start;
988 u64 packets, bytes;
989 int i, j;
Stephen Hemminger4323b472016-08-23 12:17:57 -0700990
stephen hemminger545a8e72017-03-22 14:51:00 -0700991 if (!nvdev)
992 return;
993
Simon Xiao6c80f3f2017-01-24 13:06:13 -0800994 for (i = 0; i < NETVSC_GLOBAL_STATS_LEN; i++)
Stephen Hemminger4323b472016-08-23 12:17:57 -0700995 data[i] = *(unsigned long *)(nds + netvsc_stats[i].offset);
Simon Xiao6c80f3f2017-01-24 13:06:13 -0800996
997 for (j = 0; j < nvdev->num_chn; j++) {
998 qstats = &nvdev->chan_table[j].tx_stats;
999
1000 do {
1001 start = u64_stats_fetch_begin_irq(&qstats->syncp);
1002 packets = qstats->packets;
1003 bytes = qstats->bytes;
1004 } while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
1005 data[i++] = packets;
1006 data[i++] = bytes;
1007
1008 qstats = &nvdev->chan_table[j].rx_stats;
1009 do {
1010 start = u64_stats_fetch_begin_irq(&qstats->syncp);
1011 packets = qstats->packets;
1012 bytes = qstats->bytes;
1013 } while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
1014 data[i++] = packets;
1015 data[i++] = bytes;
1016 }
Stephen Hemminger4323b472016-08-23 12:17:57 -07001017}
1018
1019static void netvsc_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1020{
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001021 struct net_device_context *ndc = netdev_priv(dev);
stephen hemminger545a8e72017-03-22 14:51:00 -07001022 struct netvsc_device *nvdev = rcu_dereference(ndc->nvdev);
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001023 u8 *p = data;
Stephen Hemminger4323b472016-08-23 12:17:57 -07001024 int i;
1025
stephen hemminger545a8e72017-03-22 14:51:00 -07001026 if (!nvdev)
1027 return;
1028
Stephen Hemminger4323b472016-08-23 12:17:57 -07001029 switch (stringset) {
1030 case ETH_SS_STATS:
1031 for (i = 0; i < ARRAY_SIZE(netvsc_stats); i++)
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001032 memcpy(p + i * ETH_GSTRING_LEN,
Stephen Hemminger4323b472016-08-23 12:17:57 -07001033 netvsc_stats[i].name, ETH_GSTRING_LEN);
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001034
1035 p += i * ETH_GSTRING_LEN;
1036 for (i = 0; i < nvdev->num_chn; i++) {
1037 sprintf(p, "tx_queue_%u_packets", i);
1038 p += ETH_GSTRING_LEN;
1039 sprintf(p, "tx_queue_%u_bytes", i);
1040 p += ETH_GSTRING_LEN;
1041 sprintf(p, "rx_queue_%u_packets", i);
1042 p += ETH_GSTRING_LEN;
1043 sprintf(p, "rx_queue_%u_bytes", i);
1044 p += ETH_GSTRING_LEN;
1045 }
1046
Stephen Hemminger4323b472016-08-23 12:17:57 -07001047 break;
1048 }
1049}
1050
stephen hemmingerb448f4e2017-01-24 13:06:00 -08001051static int
stephen hemmingerb5a5dc82017-01-24 13:06:01 -08001052netvsc_get_rss_hash_opts(struct netvsc_device *nvdev,
1053 struct ethtool_rxnfc *info)
1054{
1055 info->data = RXH_IP_SRC | RXH_IP_DST;
1056
1057 switch (info->flow_type) {
1058 case TCP_V4_FLOW:
1059 case TCP_V6_FLOW:
1060 info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
1061 /* fallthrough */
1062 case UDP_V4_FLOW:
1063 case UDP_V6_FLOW:
1064 case IPV4_FLOW:
1065 case IPV6_FLOW:
1066 break;
1067 default:
1068 info->data = 0;
1069 break;
1070 }
1071
1072 return 0;
1073}
1074
1075static int
stephen hemmingerb448f4e2017-01-24 13:06:00 -08001076netvsc_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
1077 u32 *rules)
1078{
1079 struct net_device_context *ndc = netdev_priv(dev);
stephen hemminger545a8e72017-03-22 14:51:00 -07001080 struct netvsc_device *nvdev = rcu_dereference(ndc->nvdev);
1081
1082 if (!nvdev)
1083 return -ENODEV;
stephen hemmingerb448f4e2017-01-24 13:06:00 -08001084
1085 switch (info->cmd) {
1086 case ETHTOOL_GRXRINGS:
1087 info->data = nvdev->num_chn;
1088 return 0;
stephen hemmingerb5a5dc82017-01-24 13:06:01 -08001089
1090 case ETHTOOL_GRXFH:
1091 return netvsc_get_rss_hash_opts(nvdev, info);
stephen hemmingerb448f4e2017-01-24 13:06:00 -08001092 }
1093 return -EOPNOTSUPP;
1094}
1095
Richard Weinberger316158f2014-07-09 16:23:59 +02001096#ifdef CONFIG_NET_POLL_CONTROLLER
stephen hemmingera5ecd432017-06-07 15:53:48 -07001097static void netvsc_poll_controller(struct net_device *dev)
Richard Weinberger316158f2014-07-09 16:23:59 +02001098{
stephen hemmingera5ecd432017-06-07 15:53:48 -07001099 struct net_device_context *ndc = netdev_priv(dev);
1100 struct netvsc_device *ndev;
1101 int i;
1102
1103 rcu_read_lock();
1104 ndev = rcu_dereference(ndc->nvdev);
1105 if (ndev) {
1106 for (i = 0; i < ndev->num_chn; i++) {
1107 struct netvsc_channel *nvchan = &ndev->chan_table[i];
1108
1109 napi_schedule(&nvchan->napi);
1110 }
1111 }
1112 rcu_read_unlock();
Richard Weinberger316158f2014-07-09 16:23:59 +02001113}
1114#endif
Haiyang Zhang1ce09e82012-07-10 07:19:22 +00001115
stephen hemminger962f3fe2017-01-24 13:06:02 -08001116static u32 netvsc_get_rxfh_key_size(struct net_device *dev)
1117{
1118 return NETVSC_HASH_KEYLEN;
1119}
1120
1121static u32 netvsc_rss_indir_size(struct net_device *dev)
1122{
stephen hemmingerff4a4412017-01-24 13:06:04 -08001123 return ITAB_NUM;
stephen hemminger962f3fe2017-01-24 13:06:02 -08001124}
1125
1126static int netvsc_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
1127 u8 *hfunc)
1128{
1129 struct net_device_context *ndc = netdev_priv(dev);
stephen hemminger545a8e72017-03-22 14:51:00 -07001130 struct netvsc_device *ndev = rcu_dereference(ndc->nvdev);
Colin Ian Kingeb996ed2017-03-25 14:26:39 +00001131 struct rndis_device *rndis_dev;
stephen hemmingerff4a4412017-01-24 13:06:04 -08001132 int i;
stephen hemminger962f3fe2017-01-24 13:06:02 -08001133
stephen hemminger545a8e72017-03-22 14:51:00 -07001134 if (!ndev)
1135 return -ENODEV;
1136
stephen hemminger962f3fe2017-01-24 13:06:02 -08001137 if (hfunc)
1138 *hfunc = ETH_RSS_HASH_TOP; /* Toeplitz */
1139
Colin Ian Kingeb996ed2017-03-25 14:26:39 +00001140 rndis_dev = ndev->extension;
stephen hemmingerff4a4412017-01-24 13:06:04 -08001141 if (indir) {
1142 for (i = 0; i < ITAB_NUM; i++)
1143 indir[i] = rndis_dev->ind_table[i];
1144 }
1145
stephen hemminger962f3fe2017-01-24 13:06:02 -08001146 if (key)
1147 memcpy(key, rndis_dev->rss_key, NETVSC_HASH_KEYLEN);
1148
1149 return 0;
1150}
1151
1152static int netvsc_set_rxfh(struct net_device *dev, const u32 *indir,
1153 const u8 *key, const u8 hfunc)
1154{
1155 struct net_device_context *ndc = netdev_priv(dev);
stephen hemminger545a8e72017-03-22 14:51:00 -07001156 struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev);
Colin Ian Kingeb996ed2017-03-25 14:26:39 +00001157 struct rndis_device *rndis_dev;
stephen hemmingerff4a4412017-01-24 13:06:04 -08001158 int i;
stephen hemminger962f3fe2017-01-24 13:06:02 -08001159
stephen hemminger545a8e72017-03-22 14:51:00 -07001160 if (!ndev)
1161 return -ENODEV;
1162
stephen hemminger962f3fe2017-01-24 13:06:02 -08001163 if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
1164 return -EOPNOTSUPP;
1165
Colin Ian Kingeb996ed2017-03-25 14:26:39 +00001166 rndis_dev = ndev->extension;
stephen hemmingerff4a4412017-01-24 13:06:04 -08001167 if (indir) {
1168 for (i = 0; i < ITAB_NUM; i++)
Arnd Bergmannb92b7d32017-06-22 00:16:37 +02001169 if (indir[i] >= VRSS_CHANNEL_MAX)
stephen hemmingerff4a4412017-01-24 13:06:04 -08001170 return -EINVAL;
1171
1172 for (i = 0; i < ITAB_NUM; i++)
1173 rndis_dev->ind_table[i] = indir[i];
1174 }
1175
1176 if (!key) {
1177 if (!indir)
1178 return 0;
1179
1180 key = rndis_dev->rss_key;
1181 }
stephen hemminger962f3fe2017-01-24 13:06:02 -08001182
1183 return rndis_filter_set_rss_param(rndis_dev, key, ndev->num_chn);
1184}
1185
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -07001186static const struct ethtool_ops ethtool_ops = {
1187 .get_drvinfo = netvsc_get_drvinfo,
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -07001188 .get_link = ethtool_op_get_link,
Stephen Hemminger4323b472016-08-23 12:17:57 -07001189 .get_ethtool_stats = netvsc_get_ethtool_stats,
1190 .get_sset_count = netvsc_get_sset_count,
1191 .get_strings = netvsc_get_strings,
Andrew Schwartzmeyer59995372015-02-26 16:27:14 -08001192 .get_channels = netvsc_get_channels,
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -07001193 .set_channels = netvsc_set_channels,
sixiao@microsoft.com76d13b52016-02-17 16:43:59 -08001194 .get_ts_info = ethtool_op_get_ts_info,
stephen hemmingerb448f4e2017-01-24 13:06:00 -08001195 .get_rxnfc = netvsc_get_rxnfc,
stephen hemminger962f3fe2017-01-24 13:06:02 -08001196 .get_rxfh_key_size = netvsc_get_rxfh_key_size,
1197 .get_rxfh_indir_size = netvsc_rss_indir_size,
1198 .get_rxfh = netvsc_get_rxfh,
1199 .set_rxfh = netvsc_set_rxfh,
Philippe Reynes5e8456f2017-03-08 23:41:04 +01001200 .get_link_ksettings = netvsc_get_link_ksettings,
1201 .set_link_ksettings = netvsc_set_link_ksettings,
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -07001202};
1203
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001204static const struct net_device_ops device_ops = {
1205 .ndo_open = netvsc_open,
1206 .ndo_stop = netvsc_close,
1207 .ndo_start_xmit = netvsc_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001208 .ndo_set_rx_mode = netvsc_set_multicast_list,
Haiyang Zhang4d447c92011-12-15 13:45:17 -08001209 .ndo_change_mtu = netvsc_change_mtu,
Haiyang Zhangb681b582010-08-03 19:15:31 +00001210 .ndo_validate_addr = eth_validate_addr,
Haiyang Zhang1ce09e82012-07-10 07:19:22 +00001211 .ndo_set_mac_address = netvsc_set_mac_addr,
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001212 .ndo_select_queue = netvsc_select_queue,
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001213 .ndo_get_stats64 = netvsc_get_stats64,
Richard Weinberger316158f2014-07-09 16:23:59 +02001214#ifdef CONFIG_NET_POLL_CONTROLLER
1215 .ndo_poll_controller = netvsc_poll_controller,
1216#endif
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001217};
1218
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001219/*
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001220 * Handle link status changes. For RNDIS_STATUS_NETWORK_CHANGE emulate link
1221 * down/up sequence. In case of RNDIS_STATUS_MEDIA_CONNECT when carrier is
1222 * present send GARP packet to network peers with netif_notify_peers().
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001223 */
Haiyang Zhang891de742014-02-12 16:54:27 -08001224static void netvsc_link_change(struct work_struct *w)
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001225{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001226 struct net_device_context *ndev_ctx =
1227 container_of(w, struct net_device_context, dwork.work);
1228 struct hv_device *device_obj = ndev_ctx->device_ctx;
1229 struct net_device *net = hv_get_drvdata(device_obj);
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -07001230 struct netvsc_device *net_device;
Haiyang Zhang891de742014-02-12 16:54:27 -08001231 struct rndis_device *rdev;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001232 struct netvsc_reconfig *event = NULL;
1233 bool notify = false, reschedule = false;
1234 unsigned long flags, next_reconfig, delay;
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001235
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001236 rtnl_lock();
stephen hemmingera0be4502017-03-22 14:51:01 -07001237 net_device = rtnl_dereference(ndev_ctx->nvdev);
1238 if (!net_device)
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001239 goto out_unlock;
1240
Haiyang Zhang891de742014-02-12 16:54:27 -08001241 rdev = net_device->extension;
Haiyang Zhang891de742014-02-12 16:54:27 -08001242
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001243 next_reconfig = ndev_ctx->last_reconfig + LINKCHANGE_INT;
1244 if (time_is_after_jiffies(next_reconfig)) {
1245 /* link_watch only sends one notification with current state
1246 * per second, avoid doing reconfig more frequently. Handle
1247 * wrap around.
1248 */
1249 delay = next_reconfig - jiffies;
1250 delay = delay < LINKCHANGE_INT ? delay : LINKCHANGE_INT;
1251 schedule_delayed_work(&ndev_ctx->dwork, delay);
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001252 goto out_unlock;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001253 }
1254 ndev_ctx->last_reconfig = jiffies;
1255
1256 spin_lock_irqsave(&ndev_ctx->lock, flags);
1257 if (!list_empty(&ndev_ctx->reconfig_events)) {
1258 event = list_first_entry(&ndev_ctx->reconfig_events,
1259 struct netvsc_reconfig, list);
1260 list_del(&event->list);
1261 reschedule = !list_empty(&ndev_ctx->reconfig_events);
1262 }
1263 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1264
1265 if (!event)
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001266 goto out_unlock;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001267
1268 switch (event->event) {
1269 /* Only the following events are possible due to the check in
1270 * netvsc_linkstatus_callback()
1271 */
1272 case RNDIS_STATUS_MEDIA_CONNECT:
1273 if (rdev->link_state) {
1274 rdev->link_state = false;
Haiyang Zhang53fa1a62017-06-21 16:40:47 -07001275 if (!ndev_ctx->datapath)
1276 netif_carrier_on(net);
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001277 netif_tx_wake_all_queues(net);
1278 } else {
1279 notify = true;
Haiyang Zhang3a494e72014-06-19 18:34:36 -07001280 }
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001281 kfree(event);
1282 break;
1283 case RNDIS_STATUS_MEDIA_DISCONNECT:
1284 if (!rdev->link_state) {
1285 rdev->link_state = true;
1286 netif_carrier_off(net);
1287 netif_tx_stop_all_queues(net);
1288 }
1289 kfree(event);
1290 break;
1291 case RNDIS_STATUS_NETWORK_CHANGE:
1292 /* Only makes sense if carrier is present */
1293 if (!rdev->link_state) {
1294 rdev->link_state = true;
1295 netif_carrier_off(net);
1296 netif_tx_stop_all_queues(net);
1297 event->event = RNDIS_STATUS_MEDIA_CONNECT;
1298 spin_lock_irqsave(&ndev_ctx->lock, flags);
Haiyang Zhang15cfd402016-04-21 16:13:01 -07001299 list_add(&event->list, &ndev_ctx->reconfig_events);
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001300 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1301 reschedule = true;
1302 }
1303 break;
Haiyang Zhang891de742014-02-12 16:54:27 -08001304 }
1305
1306 rtnl_unlock();
1307
1308 if (notify)
1309 netdev_notify_peers(net);
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001310
1311 /* link_watch only sends one notification with current state per
1312 * second, handle next reconfig event in 2 seconds.
1313 */
1314 if (reschedule)
1315 schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001316
1317 return;
1318
1319out_unlock:
1320 rtnl_unlock();
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001321}
1322
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001323static struct net_device *get_netvsc_bymac(const u8 *mac)
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001324{
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001325 struct net_device *dev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001326
Stephen Hemminger8737caa2016-08-23 12:17:44 -07001327 ASSERT_RTNL();
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001328
1329 for_each_netdev(&init_net, dev) {
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001330 if (dev->netdev_ops != &device_ops)
1331 continue; /* not a netvsc device */
1332
1333 if (ether_addr_equal(mac, dev->perm_addr))
1334 return dev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001335 }
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001336
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001337 return NULL;
1338}
1339
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001340static struct net_device *get_netvsc_byref(struct net_device *vf_netdev)
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001341{
1342 struct net_device *dev;
1343
1344 ASSERT_RTNL();
1345
1346 for_each_netdev(&init_net, dev) {
1347 struct net_device_context *net_device_ctx;
1348
1349 if (dev->netdev_ops != &device_ops)
1350 continue; /* not a netvsc device */
1351
1352 net_device_ctx = netdev_priv(dev);
stephen hemminger79e8cbe2017-07-19 11:53:13 -07001353 if (!rtnl_dereference(net_device_ctx->nvdev))
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001354 continue; /* device is removed */
1355
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001356 if (rtnl_dereference(net_device_ctx->vf_netdev) == vf_netdev)
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001357 return dev; /* a match */
1358 }
1359
1360 return NULL;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001361}
1362
1363static int netvsc_register_vf(struct net_device *vf_netdev)
1364{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001365 struct net_device *ndev;
1366 struct net_device_context *net_device_ctx;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001367 struct netvsc_device *netvsc_dev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001368
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001369 if (vf_netdev->addr_len != ETH_ALEN)
1370 return NOTIFY_DONE;
1371
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001372 /*
1373 * We will use the MAC address to locate the synthetic interface to
1374 * associate with the VF interface. If we don't find a matching
1375 * synthetic interface, move on.
1376 */
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001377 ndev = get_netvsc_bymac(vf_netdev->perm_addr);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001378 if (!ndev)
1379 return NOTIFY_DONE;
1380
1381 net_device_ctx = netdev_priv(ndev);
stephen hemminger545a8e72017-03-22 14:51:00 -07001382 netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001383 if (!netvsc_dev || rtnl_dereference(net_device_ctx->vf_netdev))
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001384 return NOTIFY_DONE;
1385
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001386 netdev_info(ndev, "VF registering: %s\n", vf_netdev->name);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001387 /*
1388 * Take a reference on the module.
1389 */
1390 try_module_get(THIS_MODULE);
Stephen Hemminger07d0f002016-09-22 16:56:30 -07001391
1392 dev_hold(vf_netdev);
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001393 rcu_assign_pointer(net_device_ctx->vf_netdev, vf_netdev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001394 return NOTIFY_OK;
1395}
1396
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001397static int netvsc_vf_up(struct net_device *vf_netdev)
1398{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001399 struct net_device *ndev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001400 struct netvsc_device *netvsc_dev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001401 struct net_device_context *net_device_ctx;
1402
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001403 ndev = get_netvsc_byref(vf_netdev);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001404 if (!ndev)
1405 return NOTIFY_DONE;
1406
1407 net_device_ctx = netdev_priv(ndev);
stephen hemminger545a8e72017-03-22 14:51:00 -07001408 netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001409
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001410 netdev_info(ndev, "VF up: %s\n", vf_netdev->name);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001411
1412 /*
1413 * Open the device before switching data path.
1414 */
Vitaly Kuznetsov2f5fa6c2016-06-03 17:51:00 +02001415 rndis_filter_open(netvsc_dev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001416
1417 /*
1418 * notify the host to switch the data path.
1419 */
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001420 netvsc_switch_datapath(ndev, true);
1421 netdev_info(ndev, "Data path switched to VF: %s\n", vf_netdev->name);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001422
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001423 netif_carrier_off(ndev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001424
Vitaly Kuznetsovd0722182016-08-15 17:48:40 +02001425 /* Now notify peers through VF device. */
1426 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, vf_netdev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001427
1428 return NOTIFY_OK;
1429}
1430
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001431static int netvsc_vf_down(struct net_device *vf_netdev)
1432{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001433 struct net_device *ndev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001434 struct netvsc_device *netvsc_dev;
1435 struct net_device_context *net_device_ctx;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001436
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001437 ndev = get_netvsc_byref(vf_netdev);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001438 if (!ndev)
1439 return NOTIFY_DONE;
1440
1441 net_device_ctx = netdev_priv(ndev);
stephen hemminger545a8e72017-03-22 14:51:00 -07001442 netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001443
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001444 netdev_info(ndev, "VF down: %s\n", vf_netdev->name);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001445 netvsc_switch_datapath(ndev, false);
1446 netdev_info(ndev, "Data path switched from VF: %s\n", vf_netdev->name);
Vitaly Kuznetsov2f5fa6c2016-06-03 17:51:00 +02001447 rndis_filter_close(netvsc_dev);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001448 netif_carrier_on(ndev);
Vitaly Kuznetsovd0722182016-08-15 17:48:40 +02001449
1450 /* Now notify peers through netvsc device. */
1451 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, ndev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001452
1453 return NOTIFY_OK;
1454}
1455
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001456static int netvsc_unregister_vf(struct net_device *vf_netdev)
1457{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001458 struct net_device *ndev;
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001459 struct net_device_context *net_device_ctx;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001460
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001461 ndev = get_netvsc_byref(vf_netdev);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001462 if (!ndev)
1463 return NOTIFY_DONE;
1464
1465 net_device_ctx = netdev_priv(ndev);
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001466
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001467 netdev_info(ndev, "VF unregistering: %s\n", vf_netdev->name);
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001468
1469 RCU_INIT_POINTER(net_device_ctx->vf_netdev, NULL);
Stephen Hemminger07d0f002016-09-22 16:56:30 -07001470 dev_put(vf_netdev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001471 module_put(THIS_MODULE);
1472 return NOTIFY_OK;
1473}
1474
K. Y. Srinivasan84946892011-09-13 10:59:38 -07001475static int netvsc_probe(struct hv_device *dev,
1476 const struct hv_vmbus_device_id *dev_id)
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001477{
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001478 struct net_device *net = NULL;
1479 struct net_device_context *net_device_ctx;
1480 struct netvsc_device_info device_info;
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001481 struct netvsc_device *nvdev;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001482 int ret;
1483
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001484 net = alloc_etherdev_mq(sizeof(struct net_device_context),
stephen hemminger2b018882017-01-24 13:06:03 -08001485 VRSS_CHANNEL_MAX);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001486 if (!net)
K. Y. Srinivasan51a805d2011-08-25 09:49:11 -07001487 return -ENOMEM;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001488
Haiyang Zhang1b07da52014-03-04 14:11:06 -08001489 netif_carrier_off(net);
1490
Haiyang Zhangb37879e2016-08-04 10:42:14 -07001491 netvsc_init_settings(net);
1492
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001493 net_device_ctx = netdev_priv(net);
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -07001494 net_device_ctx->device_ctx = dev;
Simon Xiao3f300ff2015-04-28 01:05:17 -07001495 net_device_ctx->msg_enable = netif_msg_init(debug, default_msg);
1496 if (netif_msg_probe(net_device_ctx))
1497 netdev_dbg(net, "netvsc msg_enable: %d\n",
1498 net_device_ctx->msg_enable);
1499
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -07001500 hv_set_drvdata(dev, net);
Vitaly Kuznetsovf580aec2016-05-13 13:55:20 +02001501
Haiyang Zhang891de742014-02-12 16:54:27 -08001502 INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001503
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001504 spin_lock_init(&net_device_ctx->lock);
1505 INIT_LIST_HEAD(&net_device_ctx->reconfig_events);
1506
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001507 net->netdev_ops = &device_ops;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001508 net->ethtool_ops = &ethtool_ops;
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -07001509 SET_NETDEV_DEV(net, &dev->device);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001510
Vitaly Kuznetsov14a03cf2016-02-05 17:29:08 +01001511 /* We always need headroom for rndis header */
1512 net->needed_headroom = RNDIS_AND_PPI_SIZE;
1513
Haiyang Zhang692e0842011-09-01 12:19:43 -07001514 /* Notify the netvsc driver of the new device */
Andrew Schwartzmeyer8ebdcc52015-08-11 17:14:31 -07001515 memset(&device_info, 0, sizeof(device_info));
Haiyang Zhang692e0842011-09-01 12:19:43 -07001516 device_info.ring_size = ring_size;
stephen hemminger3071ada2017-03-22 14:50:59 -07001517 device_info.num_chn = VRSS_CHANNEL_DEFAULT;
stephen hemminger9749fed2017-07-19 11:53:16 -07001518
1519 nvdev = rndis_filter_device_add(dev, &device_info);
1520 if (IS_ERR(nvdev)) {
1521 ret = PTR_ERR(nvdev);
Haiyang Zhang692e0842011-09-01 12:19:43 -07001522 netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001523 free_netdev(net);
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -07001524 hv_set_drvdata(dev, NULL);
Haiyang Zhang692e0842011-09-01 12:19:43 -07001525 return ret;
1526 }
1527 memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
1528
stephen hemminger23312a32017-01-24 13:05:59 -08001529 /* hw_features computed in rndis_filter_device_add */
1530 net->features = net->hw_features |
1531 NETIF_F_HIGHDMA | NETIF_F_SG |
1532 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
1533 net->vlan_features = net->features;
1534
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001535 netif_set_real_num_tx_queues(net, nvdev->num_chn);
1536 netif_set_real_num_rx_queues(net, nvdev->num_chn);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001537
stephen hemminger9749fed2017-07-19 11:53:16 -07001538 netdev_lockdep_set_classes(net);
1539
Jarod Wilsond0c2c992016-10-20 13:55:21 -04001540 /* MTU range: 68 - 1500 or 65521 */
1541 net->min_mtu = NETVSC_MTU_MIN;
1542 if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
1543 net->max_mtu = NETVSC_MTU - ETH_HLEN;
1544 else
1545 net->max_mtu = ETH_DATA_LEN;
1546
Haiyang Zhanga68f9612013-12-20 16:52:31 -08001547 ret = register_netdev(net);
1548 if (ret != 0) {
1549 pr_err("Unable to register netdev.\n");
stephen hemminger2289f0a2017-01-24 13:06:10 -08001550 rndis_filter_device_remove(dev, nvdev);
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001551 free_netdev(net);
Haiyang Zhanga68f9612013-12-20 16:52:31 -08001552 }
1553
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001554 return ret;
1555}
1556
K. Y. Srinivasan415b0232011-04-29 13:45:12 -07001557static int netvsc_remove(struct hv_device *dev)
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001558{
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -07001559 struct net_device *net;
Haiyang Zhang122a5f62011-05-27 06:21:55 -07001560 struct net_device_context *ndev_ctx;
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -07001561
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +02001562 net = hv_get_drvdata(dev);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001563
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001564 if (net == NULL) {
K. Y. Srinivasan415b0232011-04-29 13:45:12 -07001565 dev_err(&dev->device, "No net device to remove\n");
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001566 return 0;
1567 }
1568
Haiyang Zhang122a5f62011-05-27 06:21:55 -07001569 ndev_ctx = netdev_priv(net);
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +02001570
stephen hemmingera0be4502017-03-22 14:51:01 -07001571 netif_device_detach(net);
Vitaly Kuznetsovf580aec2016-05-13 13:55:20 +02001572
Haiyang Zhang122a5f62011-05-27 06:21:55 -07001573 cancel_delayed_work_sync(&ndev_ctx->dwork);
1574
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001575 /*
1576 * Call to the vsc driver to let it know that the device is being
stephen hemmingera0be4502017-03-22 14:51:01 -07001577 * removed. Also blocks mtu and channel changes.
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001578 */
stephen hemmingera0be4502017-03-22 14:51:01 -07001579 rtnl_lock();
stephen hemminger79e8cbe2017-07-19 11:53:13 -07001580 rndis_filter_device_remove(dev,
1581 rtnl_dereference(ndev_ctx->nvdev));
stephen hemmingera0be4502017-03-22 14:51:01 -07001582 rtnl_unlock();
1583
1584 unregister_netdev(net);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001585
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +02001586 hv_set_drvdata(dev, NULL);
1587
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001588 free_netdev(net);
Haiyang Zhangdf06bcf2011-05-23 09:03:47 -07001589 return 0;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001590}
1591
K. Y. Srinivasan345c4cc2011-08-25 09:48:34 -07001592static const struct hv_vmbus_device_id id_table[] = {
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07001593 /* Network guid */
K. Y. Srinivasan8f505942013-01-23 17:42:42 -08001594 { HV_NIC_GUID, },
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07001595 { },
K. Y. Srinivasan345c4cc2011-08-25 09:48:34 -07001596};
1597
1598MODULE_DEVICE_TABLE(vmbus, id_table);
1599
K. Y. Srinivasanf1542a62011-05-10 07:55:16 -07001600/* The one and only one */
K. Y. Srinivasanfde0ef92011-05-12 19:35:08 -07001601static struct hv_driver netvsc_drv = {
Haiyang Zhangd31b20f2012-03-07 10:02:00 +00001602 .name = KBUILD_MODNAME,
K. Y. Srinivasan345c4cc2011-08-25 09:48:34 -07001603 .id_table = id_table,
K. Y. Srinivasanfde0ef92011-05-12 19:35:08 -07001604 .probe = netvsc_probe,
1605 .remove = netvsc_remove,
K. Y. Srinivasand4890972011-05-10 07:55:17 -07001606};
K. Y. Srinivasanf1542a62011-05-10 07:55:16 -07001607
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001608/*
1609 * On Hyper-V, every VF interface is matched with a corresponding
1610 * synthetic interface. The synthetic interface is presented first
1611 * to the guest. When the corresponding VF instance is registered,
1612 * we will take care of switching the data path.
1613 */
1614static int netvsc_netdev_event(struct notifier_block *this,
1615 unsigned long event, void *ptr)
1616{
1617 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
1618
Stephen Hemmingeree837a12016-09-22 16:56:31 -07001619 /* Skip our own events */
1620 if (event_dev->netdev_ops == &device_ops)
1621 return NOTIFY_DONE;
1622
1623 /* Avoid non-Ethernet type devices */
1624 if (event_dev->type != ARPHRD_ETHER)
1625 return NOTIFY_DONE;
1626
Vitaly Kuznetsov0dbff142016-08-15 17:48:43 +02001627 /* Avoid Vlan dev with same MAC registering as VF */
Parav Panditd0d7b102017-02-04 11:00:49 -06001628 if (is_vlan_dev(event_dev))
Vitaly Kuznetsov0dbff142016-08-15 17:48:43 +02001629 return NOTIFY_DONE;
1630
1631 /* Avoid Bonding master dev with same MAC registering as VF */
Stephen Hemmingeree837a12016-09-22 16:56:31 -07001632 if ((event_dev->priv_flags & IFF_BONDING) &&
1633 (event_dev->flags & IFF_MASTER))
Haiyang Zhangcb2911f2016-06-02 12:02:04 -07001634 return NOTIFY_DONE;
1635
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001636 switch (event) {
1637 case NETDEV_REGISTER:
1638 return netvsc_register_vf(event_dev);
1639 case NETDEV_UNREGISTER:
1640 return netvsc_unregister_vf(event_dev);
1641 case NETDEV_UP:
1642 return netvsc_vf_up(event_dev);
1643 case NETDEV_DOWN:
1644 return netvsc_vf_down(event_dev);
1645 default:
1646 return NOTIFY_DONE;
1647 }
1648}
1649
1650static struct notifier_block netvsc_netdev_notifier = {
1651 .notifier_call = netvsc_netdev_event,
1652};
1653
K. Y. Srinivasana9869c92011-05-12 19:35:17 -07001654static void __exit netvsc_drv_exit(void)
Hank Janssenfceaf242009-07-13 15:34:54 -07001655{
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001656 unregister_netdevice_notifier(&netvsc_netdev_notifier);
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001657 vmbus_driver_unregister(&netvsc_drv);
Hank Janssenfceaf242009-07-13 15:34:54 -07001658}
1659
K. Y. Srinivasan1fde28c2011-05-12 19:35:16 -07001660static int __init netvsc_drv_init(void)
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001661{
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001662 int ret;
1663
Haiyang Zhangfa85a6c2012-07-25 08:08:41 +00001664 if (ring_size < RING_SIZE_MIN) {
1665 ring_size = RING_SIZE_MIN;
1666 pr_info("Increased ring_size to %d (min allowed)\n",
1667 ring_size);
1668 }
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001669 ret = vmbus_driver_register(&netvsc_drv);
1670
1671 if (ret)
1672 return ret;
1673
1674 register_netdevice_notifier(&netvsc_netdev_notifier);
1675 return 0;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001676}
1677
Hank Janssen26c14cc2010-02-11 23:02:42 +00001678MODULE_LICENSE("GPL");
Stephen Hemminger7880fc52010-05-04 09:58:52 -07001679MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
Hank Janssenfceaf242009-07-13 15:34:54 -07001680
K. Y. Srinivasan1fde28c2011-05-12 19:35:16 -07001681module_init(netvsc_drv_init);
K. Y. Srinivasana9869c92011-05-12 19:35:17 -07001682module_exit(netvsc_drv_exit);