]>
Commit | Line | Data |
---|---|---|
fceaf24a | 1 | /* |
fceaf24a HJ |
2 | * 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 | |
adf8d3ff | 14 | * this program; if not, see <http://www.gnu.org/licenses/>. |
fceaf24a HJ |
15 | * |
16 | * Authors: | |
d0e94d17 | 17 | * Haiyang Zhang <haiyangz@microsoft.com> |
fceaf24a | 18 | * Hank Janssen <hjanssen@microsoft.com> |
fceaf24a | 19 | */ |
eb335bc4 HJ |
20 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
21 | ||
fceaf24a | 22 | #include <linux/init.h> |
9079ce69 | 23 | #include <linux/atomic.h> |
fceaf24a HJ |
24 | #include <linux/module.h> |
25 | #include <linux/highmem.h> | |
26 | #include <linux/device.h> | |
fceaf24a | 27 | #include <linux/io.h> |
fceaf24a HJ |
28 | #include <linux/delay.h> |
29 | #include <linux/netdevice.h> | |
30 | #include <linux/inetdevice.h> | |
31 | #include <linux/etherdevice.h> | |
4d0ce115 | 32 | #include <linux/pci.h> |
fceaf24a | 33 | #include <linux/skbuff.h> |
c802db11 | 34 | #include <linux/if_vlan.h> |
fceaf24a | 35 | #include <linux/in.h> |
5a0e3ad6 | 36 | #include <linux/slab.h> |
27f5aa92 | 37 | #include <linux/rtnetlink.h> |
0c195567 | 38 | #include <linux/netpoll.h> |
27f5aa92 | 39 | |
fceaf24a HJ |
40 | #include <net/arp.h> |
41 | #include <net/route.h> | |
42 | #include <net/sock.h> | |
43 | #include <net/pkt_sched.h> | |
8eb1b3c3 MK |
44 | #include <net/checksum.h> |
45 | #include <net/ip6_checksum.h> | |
3f335ea2 | 46 | |
5ca7252a | 47 | #include "hyperv_net.h" |
fceaf24a | 48 | |
cef03981 SH |
49 | #define RING_SIZE_MIN 64 |
50 | #define RETRY_US_LO 5000 | |
51 | #define RETRY_US_HI 10000 | |
52 | #define RETRY_MAX 2000 /* >10 sec */ | |
8b532797 | 53 | |
27a70af3 | 54 | #define LINKCHANGE_INT (2 * HZ) |
6123c668 | 55 | #define VF_TAKEOVER_INT (HZ / 10) |
a50af86d | 56 | |
99c8da0f | 57 | static int ring_size = 128; |
450d7a4b SH |
58 | module_param(ring_size, int, S_IRUGO); |
59 | MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)"); | |
fceaf24a | 60 | |
3f300ff4 SX |
61 | static 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 | ||
66 | static int debug = -1; | |
67 | module_param(debug, int, S_IRUGO); | |
68 | MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)"); | |
69 | ||
ad375c00 SH |
70 | static LIST_HEAD(netvsc_dev_list); |
71 | ||
18cc16d1 | 72 | static void netvsc_change_rx_flags(struct net_device *net, int change) |
fceaf24a | 73 | { |
18cc16d1 SH |
74 | struct net_device_context *ndev_ctx = netdev_priv(net); |
75 | struct net_device *vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev); | |
76 | int inc; | |
77 | ||
78 | if (!vf_netdev) | |
79 | return; | |
80 | ||
81 | if (change & IFF_PROMISC) { | |
82 | inc = (net->flags & IFF_PROMISC) ? 1 : -1; | |
83 | dev_set_promiscuity(vf_netdev, inc); | |
84 | } | |
85 | ||
86 | if (change & IFF_ALLMULTI) { | |
87 | inc = (net->flags & IFF_ALLMULTI) ? 1 : -1; | |
88 | dev_set_allmulti(vf_netdev, inc); | |
89 | } | |
90 | } | |
91 | ||
92 | static void netvsc_set_rx_mode(struct net_device *net) | |
93 | { | |
94 | struct net_device_context *ndev_ctx = netdev_priv(net); | |
ce851c69 SH |
95 | struct net_device *vf_netdev; |
96 | struct netvsc_device *nvdev; | |
18cc16d1 | 97 | |
ce851c69 SH |
98 | rcu_read_lock(); |
99 | vf_netdev = rcu_dereference(ndev_ctx->vf_netdev); | |
18cc16d1 SH |
100 | if (vf_netdev) { |
101 | dev_uc_sync(vf_netdev, net); | |
102 | dev_mc_sync(vf_netdev, net); | |
103 | } | |
d426b2e3 | 104 | |
ce851c69 SH |
105 | nvdev = rcu_dereference(ndev_ctx->nvdev); |
106 | if (nvdev) | |
107 | rndis_filter_update(nvdev); | |
108 | rcu_read_unlock(); | |
fceaf24a HJ |
109 | } |
110 | ||
a1979526 HZ |
111 | static void netvsc_tx_enable(struct netvsc_device *nvscdev, |
112 | struct net_device *ndev) | |
113 | { | |
114 | nvscdev->tx_disable = false; | |
115 | virt_wmb(); /* ensure queue wake up mechanism is on */ | |
116 | ||
117 | netif_tx_wake_all_queues(ndev); | |
118 | } | |
119 | ||
fceaf24a HJ |
120 | static int netvsc_open(struct net_device *net) |
121 | { | |
53fa1a6f | 122 | struct net_device_context *ndev_ctx = netdev_priv(net); |
0c195567 | 123 | struct net_device *vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev); |
79e8cbe7 | 124 | struct netvsc_device *nvdev = rtnl_dereference(ndev_ctx->nvdev); |
891de74d | 125 | struct rndis_device *rdev; |
02fafbc6 | 126 | int ret = 0; |
fceaf24a | 127 | |
891de74d HZ |
128 | netif_carrier_off(net); |
129 | ||
d515d0ff | 130 | /* Open up the device */ |
2f5fa6c8 | 131 | ret = rndis_filter_open(nvdev); |
d515d0ff HZ |
132 | if (ret != 0) { |
133 | netdev_err(net, "unable to open device (ret %d).\n", ret); | |
134 | return ret; | |
fceaf24a HJ |
135 | } |
136 | ||
891de74d | 137 | rdev = nvdev->extension; |
dd6fd5a4 | 138 | if (!rdev->link_state) { |
891de74d | 139 | netif_carrier_on(net); |
a1979526 | 140 | netvsc_tx_enable(nvdev, net); |
dd6fd5a4 | 141 | } |
891de74d | 142 | |
0c195567 | 143 | if (vf_netdev) { |
144 | /* Setting synthetic device up transparently sets | |
145 | * slave as up. If open fails, then slave will be | |
146 | * still be offline (and not used). | |
147 | */ | |
148 | ret = dev_open(vf_netdev); | |
149 | if (ret) | |
150 | netdev_warn(net, | |
151 | "unable to open slave: %s: %d\n", | |
152 | vf_netdev->name, ret); | |
153 | } | |
154 | return 0; | |
fceaf24a HJ |
155 | } |
156 | ||
cef03981 | 157 | static int netvsc_wait_until_empty(struct netvsc_device *nvdev) |
fceaf24a | 158 | { |
cef03981 SH |
159 | unsigned int retry = 0; |
160 | int i; | |
2de8530b HZ |
161 | |
162 | /* Ensure pending bytes in ring are read */ | |
cef03981 SH |
163 | for (;;) { |
164 | u32 aread = 0; | |
165 | ||
2de8530b | 166 | for (i = 0; i < nvdev->num_chn; i++) { |
cef03981 SH |
167 | struct vmbus_channel *chn |
168 | = nvdev->chan_table[i].channel; | |
169 | ||
2de8530b HZ |
170 | if (!chn) |
171 | continue; | |
172 | ||
cef03981 SH |
173 | /* make sure receive not running now */ |
174 | napi_synchronize(&nvdev->chan_table[i].napi); | |
175 | ||
40975962 | 176 | aread = hv_get_bytes_to_read(&chn->inbound); |
2de8530b HZ |
177 | if (aread) |
178 | break; | |
179 | ||
40975962 | 180 | aread = hv_get_bytes_to_read(&chn->outbound); |
2de8530b HZ |
181 | if (aread) |
182 | break; | |
183 | } | |
184 | ||
cef03981 SH |
185 | if (aread == 0) |
186 | return 0; | |
2de8530b | 187 | |
cef03981 SH |
188 | if (++retry > RETRY_MAX) |
189 | return -ETIMEDOUT; | |
2de8530b | 190 | |
cef03981 | 191 | usleep_range(RETRY_US_LO, RETRY_US_HI); |
2de8530b | 192 | } |
cef03981 | 193 | } |
2de8530b | 194 | |
a1979526 HZ |
195 | static void netvsc_tx_disable(struct netvsc_device *nvscdev, |
196 | struct net_device *ndev) | |
197 | { | |
198 | if (nvscdev) { | |
199 | nvscdev->tx_disable = true; | |
200 | virt_wmb(); /* ensure txq will not wake up after stop */ | |
201 | } | |
202 | ||
203 | netif_tx_disable(ndev); | |
204 | } | |
205 | ||
cef03981 SH |
206 | static int netvsc_close(struct net_device *net) |
207 | { | |
208 | struct net_device_context *net_device_ctx = netdev_priv(net); | |
209 | struct net_device *vf_netdev | |
210 | = rtnl_dereference(net_device_ctx->vf_netdev); | |
211 | struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev); | |
212 | int ret; | |
213 | ||
a1979526 | 214 | netvsc_tx_disable(nvdev, net); |
cef03981 SH |
215 | |
216 | /* No need to close rndis filter if it is removed already */ | |
217 | if (!nvdev) | |
218 | return 0; | |
219 | ||
220 | ret = rndis_filter_close(nvdev); | |
221 | if (ret != 0) { | |
222 | netdev_err(net, "unable to close device (ret %d).\n", ret); | |
223 | return ret; | |
2de8530b | 224 | } |
fceaf24a | 225 | |
cef03981 SH |
226 | ret = netvsc_wait_until_empty(nvdev); |
227 | if (ret) | |
228 | netdev_err(net, "Ring buffer not empty after closing rndis\n"); | |
229 | ||
0c195567 | 230 | if (vf_netdev) |
231 | dev_close(vf_netdev); | |
232 | ||
fceaf24a HJ |
233 | return ret; |
234 | } | |
235 | ||
8a00251a | 236 | static void *init_ppi_data(struct rndis_message *msg, u32 ppi_size, |
89bb42b1 | 237 | int pkt_type) |
8a00251a KS |
238 | { |
239 | struct rndis_packet *rndis_pkt; | |
240 | struct rndis_per_packet_info *ppi; | |
241 | ||
242 | rndis_pkt = &msg->msg.pkt; | |
243 | rndis_pkt->data_offset += ppi_size; | |
244 | ||
245 | ppi = (struct rndis_per_packet_info *)((void *)rndis_pkt + | |
246 | rndis_pkt->per_pkt_info_offset + rndis_pkt->per_pkt_info_len); | |
247 | ||
248 | ppi->size = ppi_size; | |
249 | ppi->type = pkt_type; | |
250 | ppi->ppi_offset = sizeof(struct rndis_per_packet_info); | |
251 | ||
252 | rndis_pkt->per_pkt_info_len += ppi_size; | |
253 | ||
254 | return ppi; | |
255 | } | |
256 | ||
4823eb2f HZ |
257 | /* Azure hosts don't support non-TCP port numbers in hashing for fragmented |
258 | * packets. We can use ethtool to change UDP hash level when necessary. | |
f72860af | 259 | */ |
4823eb2f HZ |
260 | static inline u32 netvsc_get_hash( |
261 | struct sk_buff *skb, | |
262 | const struct net_device_context *ndc) | |
f72860af HZ |
263 | { |
264 | struct flow_keys flow; | |
486e3981 | 265 | u32 hash, pkt_proto = 0; |
f72860af HZ |
266 | static u32 hashrnd __read_mostly; |
267 | ||
268 | net_get_random_once(&hashrnd, sizeof(hashrnd)); | |
269 | ||
270 | if (!skb_flow_dissect_flow_keys(skb, &flow, 0)) | |
271 | return 0; | |
272 | ||
486e3981 HZ |
273 | switch (flow.basic.ip_proto) { |
274 | case IPPROTO_TCP: | |
275 | if (flow.basic.n_proto == htons(ETH_P_IP)) | |
276 | pkt_proto = HV_TCP4_L4HASH; | |
277 | else if (flow.basic.n_proto == htons(ETH_P_IPV6)) | |
278 | pkt_proto = HV_TCP6_L4HASH; | |
279 | ||
280 | break; | |
281 | ||
282 | case IPPROTO_UDP: | |
283 | if (flow.basic.n_proto == htons(ETH_P_IP)) | |
284 | pkt_proto = HV_UDP4_L4HASH; | |
285 | else if (flow.basic.n_proto == htons(ETH_P_IPV6)) | |
286 | pkt_proto = HV_UDP6_L4HASH; | |
287 | ||
288 | break; | |
289 | } | |
290 | ||
291 | if (pkt_proto & ndc->l4_hash) { | |
f72860af HZ |
292 | return skb_get_hash(skb); |
293 | } else { | |
294 | if (flow.basic.n_proto == htons(ETH_P_IP)) | |
295 | hash = jhash2((u32 *)&flow.addrs.v4addrs, 2, hashrnd); | |
296 | else if (flow.basic.n_proto == htons(ETH_P_IPV6)) | |
297 | hash = jhash2((u32 *)&flow.addrs.v6addrs, 8, hashrnd); | |
298 | else | |
299 | hash = 0; | |
300 | ||
301 | skb_set_hash(skb, hash, PKT_HASH_TYPE_L3); | |
302 | } | |
303 | ||
304 | return hash; | |
305 | } | |
306 | ||
8db91f6a HZ |
307 | static inline int netvsc_get_tx_queue(struct net_device *ndev, |
308 | struct sk_buff *skb, int old_idx) | |
309 | { | |
310 | const struct net_device_context *ndc = netdev_priv(ndev); | |
311 | struct sock *sk = skb->sk; | |
312 | int q_idx; | |
313 | ||
39e91cfb HZ |
314 | q_idx = ndc->tx_table[netvsc_get_hash(skb, ndc) & |
315 | (VRSS_SEND_TAB_SIZE - 1)]; | |
8db91f6a HZ |
316 | |
317 | /* If queue index changed record the new value */ | |
318 | if (q_idx != old_idx && | |
319 | sk && sk_fullsock(sk) && rcu_access_pointer(sk->sk_dst_cache)) | |
320 | sk_tx_queue_set(sk, q_idx); | |
321 | ||
322 | return q_idx; | |
323 | } | |
324 | ||
d8e18ee0 | 325 | /* |
326 | * Select queue for transmit. | |
327 | * | |
328 | * If a valid queue has already been assigned, then use that. | |
329 | * Otherwise compute tx queue based on hash and the send table. | |
330 | * | |
331 | * This is basically similar to default (__netdev_pick_tx) with the added step | |
332 | * of using the host send_table when no other queue has been assigned. | |
333 | * | |
334 | * TODO support XPS - but get_xps_queue not exported | |
335 | */ | |
0c195567 | 336 | static u16 netvsc_pick_tx(struct net_device *ndev, struct sk_buff *skb) |
5b54dac8 | 337 | { |
8db91f6a HZ |
338 | int q_idx = sk_tx_queue_get(skb->sk); |
339 | ||
0c195567 | 340 | if (q_idx < 0 || skb->ooo_okay || q_idx >= ndev->real_num_tx_queues) { |
8db91f6a HZ |
341 | /* If forwarding a packet, we use the recorded queue when |
342 | * available for better cache locality. | |
343 | */ | |
344 | if (skb_rx_queue_recorded(skb)) | |
345 | q_idx = skb_get_rx_queue(skb); | |
346 | else | |
347 | q_idx = netvsc_get_tx_queue(ndev, skb, q_idx); | |
d8e18ee0 | 348 | } |
5b54dac8 HZ |
349 | |
350 | return q_idx; | |
351 | } | |
352 | ||
0c195567 | 353 | static u16 netvsc_select_queue(struct net_device *ndev, struct sk_buff *skb, |
354 | void *accel_priv, | |
355 | select_queue_fallback_t fallback) | |
356 | { | |
357 | struct net_device_context *ndc = netdev_priv(ndev); | |
358 | struct net_device *vf_netdev; | |
359 | u16 txq; | |
360 | ||
361 | rcu_read_lock(); | |
362 | vf_netdev = rcu_dereference(ndc->vf_netdev); | |
363 | if (vf_netdev) { | |
b3c96b37 SH |
364 | const struct net_device_ops *vf_ops = vf_netdev->netdev_ops; |
365 | ||
366 | if (vf_ops->ndo_select_queue) | |
367 | txq = vf_ops->ndo_select_queue(vf_netdev, skb, | |
368 | accel_priv, fallback); | |
369 | else | |
370 | txq = fallback(vf_netdev, skb); | |
371 | ||
372 | /* Record the queue selected by VF so that it can be | |
373 | * used for common case where VF has more queues than | |
374 | * the synthetic device. | |
375 | */ | |
376 | qdisc_skb_cb(skb)->slave_dev_queue_mapping = txq; | |
0c195567 | 377 | } else { |
378 | txq = netvsc_pick_tx(ndev, skb); | |
379 | } | |
380 | rcu_read_unlock(); | |
381 | ||
382 | while (unlikely(txq >= ndev->real_num_tx_queues)) | |
383 | txq -= ndev->real_num_tx_queues; | |
384 | ||
385 | return txq; | |
386 | } | |
387 | ||
54a7357f | 388 | static u32 fill_pg_buf(struct page *page, u32 offset, u32 len, |
89bb42b1 | 389 | struct hv_page_buffer *pb) |
54a7357f KS |
390 | { |
391 | int j = 0; | |
392 | ||
393 | /* Deal with compund pages by ignoring unused part | |
394 | * of the page. | |
395 | */ | |
396 | page += (offset >> PAGE_SHIFT); | |
397 | offset &= ~PAGE_MASK; | |
398 | ||
399 | while (len > 0) { | |
400 | unsigned long bytes; | |
401 | ||
402 | bytes = PAGE_SIZE - offset; | |
403 | if (bytes > len) | |
404 | bytes = len; | |
405 | pb[j].pfn = page_to_pfn(page); | |
406 | pb[j].offset = offset; | |
407 | pb[j].len = bytes; | |
408 | ||
409 | offset += bytes; | |
410 | len -= bytes; | |
411 | ||
412 | if (offset == PAGE_SIZE && len) { | |
413 | page++; | |
414 | offset = 0; | |
415 | j++; | |
416 | } | |
417 | } | |
418 | ||
419 | return j + 1; | |
420 | } | |
421 | ||
8a00251a | 422 | static u32 init_page_array(void *hdr, u32 len, struct sk_buff *skb, |
a9f2e2d6 | 423 | struct hv_netvsc_packet *packet, |
02b6de01 | 424 | struct hv_page_buffer *pb) |
54a7357f KS |
425 | { |
426 | u32 slots_used = 0; | |
427 | char *data = skb->data; | |
428 | int frags = skb_shinfo(skb)->nr_frags; | |
429 | int i; | |
430 | ||
431 | /* The packet is laid out thus: | |
aa0a34be | 432 | * 1. hdr: RNDIS header and PPI |
54a7357f KS |
433 | * 2. skb linear data |
434 | * 3. skb fragment data | |
435 | */ | |
ea5a32c0 | 436 | slots_used += fill_pg_buf(virt_to_page(hdr), |
437 | offset_in_page(hdr), | |
438 | len, &pb[slots_used]); | |
54a7357f | 439 | |
aa0a34be HZ |
440 | packet->rmsg_size = len; |
441 | packet->rmsg_pgcnt = slots_used; | |
442 | ||
54a7357f KS |
443 | slots_used += fill_pg_buf(virt_to_page(data), |
444 | offset_in_page(data), | |
445 | skb_headlen(skb), &pb[slots_used]); | |
446 | ||
447 | for (i = 0; i < frags; i++) { | |
448 | skb_frag_t *frag = skb_shinfo(skb)->frags + i; | |
449 | ||
450 | slots_used += fill_pg_buf(skb_frag_page(frag), | |
451 | frag->page_offset, | |
452 | skb_frag_size(frag), &pb[slots_used]); | |
453 | } | |
8a00251a | 454 | return slots_used; |
54a7357f KS |
455 | } |
456 | ||
80d887db | 457 | static int count_skb_frag_slots(struct sk_buff *skb) |
458 | { | |
459 | int i, frags = skb_shinfo(skb)->nr_frags; | |
460 | int pages = 0; | |
461 | ||
462 | for (i = 0; i < frags; i++) { | |
463 | skb_frag_t *frag = skb_shinfo(skb)->frags + i; | |
464 | unsigned long size = skb_frag_size(frag); | |
465 | unsigned long offset = frag->page_offset; | |
466 | ||
467 | /* Skip unused frames from start of page */ | |
468 | offset &= ~PAGE_MASK; | |
469 | pages += PFN_UP(offset + size); | |
470 | } | |
471 | return pages; | |
472 | } | |
473 | ||
474 | static int netvsc_get_slots(struct sk_buff *skb) | |
54a7357f | 475 | { |
80d887db | 476 | char *data = skb->data; |
477 | unsigned int offset = offset_in_page(data); | |
478 | unsigned int len = skb_headlen(skb); | |
479 | int slots; | |
480 | int frag_slots; | |
481 | ||
482 | slots = DIV_ROUND_UP(offset + len, PAGE_SIZE); | |
483 | frag_slots = count_skb_frag_slots(skb); | |
484 | return slots + frag_slots; | |
54a7357f KS |
485 | } |
486 | ||
23312a3b | 487 | static u32 net_checksum_info(struct sk_buff *skb) |
08cd04bf | 488 | { |
23312a3b | 489 | if (skb->protocol == htons(ETH_P_IP)) { |
490 | struct iphdr *ip = ip_hdr(skb); | |
08cd04bf | 491 | |
23312a3b | 492 | if (ip->protocol == IPPROTO_TCP) |
493 | return TRANSPORT_INFO_IPV4_TCP; | |
494 | else if (ip->protocol == IPPROTO_UDP) | |
495 | return TRANSPORT_INFO_IPV4_UDP; | |
08cd04bf | 496 | } else { |
23312a3b | 497 | struct ipv6hdr *ip6 = ipv6_hdr(skb); |
498 | ||
499 | if (ip6->nexthdr == IPPROTO_TCP) | |
500 | return TRANSPORT_INFO_IPV6_TCP; | |
37b9dfa0 | 501 | else if (ip6->nexthdr == IPPROTO_UDP) |
23312a3b | 502 | return TRANSPORT_INFO_IPV6_UDP; |
08cd04bf KS |
503 | } |
504 | ||
23312a3b | 505 | return TRANSPORT_INFO_NOT_IP; |
08cd04bf KS |
506 | } |
507 | ||
0c195567 | 508 | /* Send skb on the slave VF device. */ |
509 | static int netvsc_vf_xmit(struct net_device *net, struct net_device *vf_netdev, | |
510 | struct sk_buff *skb) | |
511 | { | |
512 | struct net_device_context *ndev_ctx = netdev_priv(net); | |
513 | unsigned int len = skb->len; | |
514 | int rc; | |
515 | ||
516 | skb->dev = vf_netdev; | |
517 | skb->queue_mapping = qdisc_skb_cb(skb)->slave_dev_queue_mapping; | |
518 | ||
519 | rc = dev_queue_xmit(skb); | |
520 | if (likely(rc == NET_XMIT_SUCCESS || rc == NET_XMIT_CN)) { | |
521 | struct netvsc_vf_pcpu_stats *pcpu_stats | |
522 | = this_cpu_ptr(ndev_ctx->vf_stats); | |
523 | ||
524 | u64_stats_update_begin(&pcpu_stats->syncp); | |
525 | pcpu_stats->tx_packets++; | |
526 | pcpu_stats->tx_bytes += len; | |
527 | u64_stats_update_end(&pcpu_stats->syncp); | |
528 | } else { | |
529 | this_cpu_inc(ndev_ctx->vf_stats->tx_dropped); | |
530 | } | |
531 | ||
532 | return rc; | |
533 | } | |
534 | ||
02fafbc6 | 535 | static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net) |
fceaf24a | 536 | { |
fceaf24a | 537 | struct net_device_context *net_device_ctx = netdev_priv(net); |
981a1bd8 | 538 | struct hv_netvsc_packet *packet = NULL; |
02fafbc6 | 539 | int ret; |
8a00251a KS |
540 | unsigned int num_data_pgs; |
541 | struct rndis_message *rndis_msg; | |
542 | struct rndis_packet *rndis_pkt; | |
0c195567 | 543 | struct net_device *vf_netdev; |
8a00251a | 544 | u32 rndis_msg_size; |
8a00251a | 545 | struct rndis_per_packet_info *ppi; |
307f0995 | 546 | u32 hash; |
02b6de01 | 547 | struct hv_page_buffer pb[MAX_PAGE_BUFFER_COUNT]; |
fceaf24a | 548 | |
0c195567 | 549 | /* if VF is present and up then redirect packets |
550 | * already called with rcu_read_lock_bh | |
551 | */ | |
552 | vf_netdev = rcu_dereference_bh(net_device_ctx->vf_netdev); | |
553 | if (vf_netdev && netif_running(vf_netdev) && | |
554 | !netpoll_tx_running(net)) | |
555 | return netvsc_vf_xmit(net, vf_netdev, skb); | |
556 | ||
80d887db | 557 | /* We will atmost need two pages to describe the rndis |
558 | * header. We can only transmit MAX_PAGE_BUFFER_COUNT number | |
e88f7e07 VK |
559 | * of pages in a single packet. If skb is scattered around |
560 | * more pages we try linearizing it. | |
54a7357f | 561 | */ |
80d887db | 562 | |
563 | num_data_pgs = netvsc_get_slots(skb) + 2; | |
564 | ||
0ab05141 | 565 | if (unlikely(num_data_pgs > MAX_PAGE_BUFFER_COUNT)) { |
4323b47c SH |
566 | ++net_device_ctx->eth_stats.tx_scattered; |
567 | ||
568 | if (skb_linearize(skb)) | |
569 | goto no_memory; | |
0ab05141 | 570 | |
80d887db | 571 | num_data_pgs = netvsc_get_slots(skb) + 2; |
0ab05141 | 572 | if (num_data_pgs > MAX_PAGE_BUFFER_COUNT) { |
4323b47c | 573 | ++net_device_ctx->eth_stats.tx_too_big; |
0ab05141 SH |
574 | goto drop; |
575 | } | |
54a7357f | 576 | } |
fceaf24a | 577 | |
c0eb4540 KS |
578 | /* |
579 | * Place the rndis header in the skb head room and | |
580 | * the skb->cb will be used for hv_netvsc_packet | |
581 | * structure. | |
582 | */ | |
583 | ret = skb_cow_head(skb, RNDIS_AND_PPI_SIZE); | |
4323b47c SH |
584 | if (ret) |
585 | goto no_memory; | |
586 | ||
c0eb4540 KS |
587 | /* Use the skb control buffer for building up the packet */ |
588 | BUILD_BUG_ON(sizeof(struct hv_netvsc_packet) > | |
589 | FIELD_SIZEOF(struct sk_buff, cb)); | |
590 | packet = (struct hv_netvsc_packet *)skb->cb; | |
fceaf24a | 591 | |
5b54dac8 HZ |
592 | packet->q_idx = skb_get_queue_mapping(skb); |
593 | ||
4d447c9a | 594 | packet->total_data_buflen = skb->len; |
793e3955 | 595 | packet->total_bytes = skb->len; |
596 | packet->total_packets = 1; | |
fceaf24a | 597 | |
c0eb4540 | 598 | rndis_msg = (struct rndis_message *)skb->head; |
b08cc791 | 599 | |
24476760 | 600 | memset(rndis_msg, 0, RNDIS_AND_PPI_SIZE); |
fceaf24a | 601 | |
8a00251a | 602 | /* Add the rndis header */ |
8a00251a KS |
603 | rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET; |
604 | rndis_msg->msg_len = packet->total_data_buflen; | |
605 | rndis_pkt = &rndis_msg->msg.pkt; | |
606 | rndis_pkt->data_offset = sizeof(struct rndis_packet); | |
607 | rndis_pkt->data_len = packet->total_data_buflen; | |
608 | rndis_pkt->per_pkt_info_offset = sizeof(struct rndis_packet); | |
609 | ||
610 | rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet); | |
611 | ||
307f0995 HZ |
612 | hash = skb_get_hash_raw(skb); |
613 | if (hash != 0 && net->real_num_tx_queues > 1) { | |
614 | rndis_msg_size += NDIS_HASH_PPI_SIZE; | |
615 | ppi = init_ppi_data(rndis_msg, NDIS_HASH_PPI_SIZE, | |
616 | NBL_HASH_VALUE); | |
617 | *(u32 *)((void *)ppi + ppi->ppi_offset) = hash; | |
618 | } | |
619 | ||
0ab05141 | 620 | if (skb_vlan_tag_present(skb)) { |
8a00251a KS |
621 | struct ndis_pkt_8021q_info *vlan; |
622 | ||
623 | rndis_msg_size += NDIS_VLAN_PPI_SIZE; | |
624 | ppi = init_ppi_data(rndis_msg, NDIS_VLAN_PPI_SIZE, | |
00f5024e | 625 | IEEE_8021Q_INFO); |
626 | ||
627 | vlan = (void *)ppi + ppi->ppi_offset; | |
760d1e36 KS |
628 | vlan->vlanid = skb->vlan_tci & VLAN_VID_MASK; |
629 | vlan->pri = (skb->vlan_tci & VLAN_PRIO_MASK) >> | |
8a00251a KS |
630 | VLAN_PRIO_SHIFT; |
631 | } | |
632 | ||
23312a3b | 633 | if (skb_is_gso(skb)) { |
0ab05141 SH |
634 | struct ndis_tcp_lso_info *lso_info; |
635 | ||
636 | rndis_msg_size += NDIS_LSO_PPI_SIZE; | |
637 | ppi = init_ppi_data(rndis_msg, NDIS_LSO_PPI_SIZE, | |
638 | TCP_LARGESEND_PKTINFO); | |
639 | ||
00f5024e | 640 | lso_info = (void *)ppi + ppi->ppi_offset; |
0ab05141 SH |
641 | |
642 | lso_info->lso_v2_transmit.type = NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE; | |
23312a3b | 643 | if (skb->protocol == htons(ETH_P_IP)) { |
0ab05141 SH |
644 | lso_info->lso_v2_transmit.ip_version = |
645 | NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4; | |
646 | ip_hdr(skb)->tot_len = 0; | |
647 | ip_hdr(skb)->check = 0; | |
648 | tcp_hdr(skb)->check = | |
649 | ~csum_tcpudp_magic(ip_hdr(skb)->saddr, | |
650 | ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0); | |
651 | } else { | |
652 | lso_info->lso_v2_transmit.ip_version = | |
653 | NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6; | |
654 | ipv6_hdr(skb)->payload_len = 0; | |
655 | tcp_hdr(skb)->check = | |
656 | ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr, | |
657 | &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0); | |
658 | } | |
23312a3b | 659 | lso_info->lso_v2_transmit.tcp_header_offset = skb_transport_offset(skb); |
0ab05141 | 660 | lso_info->lso_v2_transmit.mss = skb_shinfo(skb)->gso_size; |
ad19bc8a | 661 | } else if (skb->ip_summed == CHECKSUM_PARTIAL) { |
23312a3b | 662 | if (net_checksum_info(skb) & net_device_ctx->tx_checksum_mask) { |
663 | struct ndis_tcp_ip_checksum_info *csum_info; | |
664 | ||
ad19bc8a | 665 | rndis_msg_size += NDIS_CSUM_PPI_SIZE; |
666 | ppi = init_ppi_data(rndis_msg, NDIS_CSUM_PPI_SIZE, | |
667 | TCPIP_CHKSUM_PKTINFO); | |
668 | ||
669 | csum_info = (struct ndis_tcp_ip_checksum_info *)((void *)ppi + | |
670 | ppi->ppi_offset); | |
671 | ||
23312a3b | 672 | csum_info->transmit.tcp_header_offset = skb_transport_offset(skb); |
673 | ||
674 | if (skb->protocol == htons(ETH_P_IP)) { | |
ad19bc8a | 675 | csum_info->transmit.is_ipv4 = 1; |
23312a3b | 676 | |
677 | if (ip_hdr(skb)->protocol == IPPROTO_TCP) | |
678 | csum_info->transmit.tcp_checksum = 1; | |
679 | else | |
680 | csum_info->transmit.udp_checksum = 1; | |
681 | } else { | |
ad19bc8a | 682 | csum_info->transmit.is_ipv6 = 1; |
683 | ||
23312a3b | 684 | if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP) |
685 | csum_info->transmit.tcp_checksum = 1; | |
686 | else | |
687 | csum_info->transmit.udp_checksum = 1; | |
688 | } | |
ad19bc8a | 689 | } else { |
23312a3b | 690 | /* Can't do offload of this type of checksum */ |
ad19bc8a | 691 | if (skb_checksum_help(skb)) |
692 | goto drop; | |
693 | } | |
08cd04bf KS |
694 | } |
695 | ||
8a00251a KS |
696 | /* Start filling in the page buffers with the rndis hdr */ |
697 | rndis_msg->msg_len += rndis_msg_size; | |
942396b0 | 698 | packet->total_data_buflen = rndis_msg->msg_len; |
8a00251a | 699 | packet->page_buf_cnt = init_page_array(rndis_msg, rndis_msg_size, |
02b6de01 | 700 | skb, packet, pb); |
8a00251a | 701 | |
76d13b56 | 702 | /* timestamp packet in software */ |
703 | skb_tx_timestamp(skb); | |
2a926f79 | 704 | |
706b71d5 | 705 | ret = netvsc_send(net, packet, rndis_msg, pb, skb); |
793e3955 | 706 | if (likely(ret == 0)) |
0ab05141 | 707 | return NETDEV_TX_OK; |
4323b47c SH |
708 | |
709 | if (ret == -EAGAIN) { | |
710 | ++net_device_ctx->eth_stats.tx_busy; | |
0ab05141 | 711 | return NETDEV_TX_BUSY; |
4323b47c SH |
712 | } |
713 | ||
714 | if (ret == -ENOSPC) | |
715 | ++net_device_ctx->eth_stats.tx_no_space; | |
0ab05141 SH |
716 | |
717 | drop: | |
718 | dev_kfree_skb_any(skb); | |
719 | net->stats.tx_dropped++; | |
fceaf24a | 720 | |
0ab05141 | 721 | return NETDEV_TX_OK; |
4323b47c SH |
722 | |
723 | no_memory: | |
724 | ++net_device_ctx->eth_stats.tx_no_memory; | |
725 | goto drop; | |
fceaf24a | 726 | } |
89bb42b1 | 727 | |
3e189519 | 728 | /* |
02fafbc6 GKH |
729 | * netvsc_linkstatus_callback - Link up/down notification |
730 | */ | |
90ef117a | 731 | void netvsc_linkstatus_callback(struct hv_device *device_obj, |
3a494e71 | 732 | struct rndis_message *resp) |
fceaf24a | 733 | { |
3a494e71 | 734 | struct rndis_indicate_status *indicate = &resp->msg.indicate_status; |
2ddd5e5f | 735 | struct net_device *net; |
c996edcf | 736 | struct net_device_context *ndev_ctx; |
27a70af3 VK |
737 | struct netvsc_reconfig *event; |
738 | unsigned long flags; | |
891de74d | 739 | |
7f5d5af0 HZ |
740 | net = hv_get_drvdata(device_obj); |
741 | ||
742 | if (!net) | |
743 | return; | |
744 | ||
745 | ndev_ctx = netdev_priv(net); | |
746 | ||
747 | /* Update the physical link speed when changing to another vSwitch */ | |
748 | if (indicate->status == RNDIS_STATUS_LINK_SPEED_CHANGE) { | |
749 | u32 speed; | |
750 | ||
89bb42b1 | 751 | speed = *(u32 *)((void *)indicate |
752 | + indicate->status_buf_offset) / 10000; | |
7f5d5af0 HZ |
753 | ndev_ctx->speed = speed; |
754 | return; | |
755 | } | |
756 | ||
757 | /* Handle these link change statuses below */ | |
27a70af3 VK |
758 | if (indicate->status != RNDIS_STATUS_NETWORK_CHANGE && |
759 | indicate->status != RNDIS_STATUS_MEDIA_CONNECT && | |
760 | indicate->status != RNDIS_STATUS_MEDIA_DISCONNECT) | |
3a494e71 | 761 | return; |
891de74d | 762 | |
7f5d5af0 | 763 | if (net->reg_state != NETREG_REGISTERED) |
fceaf24a | 764 | return; |
fceaf24a | 765 | |
27a70af3 VK |
766 | event = kzalloc(sizeof(*event), GFP_ATOMIC); |
767 | if (!event) | |
768 | return; | |
769 | event->event = indicate->status; | |
770 | ||
771 | spin_lock_irqsave(&ndev_ctx->lock, flags); | |
772 | list_add_tail(&event->list, &ndev_ctx->reconfig_events); | |
773 | spin_unlock_irqrestore(&ndev_ctx->lock, flags); | |
774 | ||
775 | schedule_delayed_work(&ndev_ctx->dwork, 0); | |
fceaf24a HJ |
776 | } |
777 | ||
f328020d HZ |
778 | static void netvsc_comp_ipcsum(struct sk_buff *skb) |
779 | { | |
780 | struct iphdr *iph = (struct iphdr *)skb->data; | |
781 | ||
782 | iph->check = 0; | |
783 | iph->check = ip_fast_csum(iph, iph->ihl); | |
784 | } | |
785 | ||
84bf9cef | 786 | static struct sk_buff *netvsc_alloc_recv_skb(struct net_device *net, |
e91e7dd7 | 787 | struct napi_struct *napi, |
dc54a08c | 788 | const struct ndis_tcp_ip_checksum_info *csum_info, |
789 | const struct ndis_pkt_8021q_info *vlan, | |
790 | void *data, u32 buflen) | |
fceaf24a | 791 | { |
fceaf24a | 792 | struct sk_buff *skb; |
fceaf24a | 793 | |
e91e7dd7 | 794 | skb = napi_alloc_skb(napi, buflen); |
84bf9cef KS |
795 | if (!skb) |
796 | return skb; | |
fceaf24a | 797 | |
02fafbc6 GKH |
798 | /* |
799 | * Copy to skb. This copy is needed here since the memory pointed by | |
800 | * hv_netvsc_packet cannot be deallocated | |
801 | */ | |
59ae1d12 | 802 | skb_put_data(skb, data, buflen); |
fceaf24a HJ |
803 | |
804 | skb->protocol = eth_type_trans(skb, net); | |
e52fed71 SH |
805 | |
806 | /* skb is already created with CHECKSUM_NONE */ | |
807 | skb_checksum_none_assert(skb); | |
808 | ||
f328020d HZ |
809 | /* Incoming packets may have IP header checksum verified by the host. |
810 | * They may not have IP header checksum computed after coalescing. | |
811 | * We compute it here if the flags are set, because on Linux, the IP | |
812 | * checksum is always checked. | |
813 | */ | |
814 | if (csum_info && csum_info->receive.ip_checksum_value_invalid && | |
815 | csum_info->receive.ip_checksum_succeeded && | |
816 | skb->protocol == htons(ETH_P_IP)) | |
817 | netvsc_comp_ipcsum(skb); | |
818 | ||
819 | /* Do L4 checksum offload if enabled and present. | |
e52fed71 SH |
820 | */ |
821 | if (csum_info && (net->features & NETIF_F_RXCSUM)) { | |
822 | if (csum_info->receive.tcp_checksum_succeeded || | |
823 | csum_info->receive.udp_checksum_succeeded) | |
e3d605ed | 824 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
e3d605ed KS |
825 | } |
826 | ||
dc54a08c | 827 | if (vlan) { |
828 | u16 vlan_tci = vlan->vlanid | (vlan->pri << VLAN_PRIO_SHIFT); | |
829 | ||
93725cbd | 830 | __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), |
760d1e36 | 831 | vlan_tci); |
dc54a08c | 832 | } |
fceaf24a | 833 | |
84bf9cef KS |
834 | return skb; |
835 | } | |
836 | ||
837 | /* | |
838 | * netvsc_recv_callback - Callback when we receive a packet from the | |
839 | * "wire" on the specified device. | |
840 | */ | |
dc54a08c | 841 | int netvsc_recv_callback(struct net_device *net, |
842 | struct vmbus_channel *channel, | |
843 | void *data, u32 len, | |
844 | const struct ndis_tcp_ip_checksum_info *csum_info, | |
845 | const struct ndis_pkt_8021q_info *vlan) | |
84bf9cef | 846 | { |
3d541ac5 | 847 | struct net_device_context *net_device_ctx = netdev_priv(net); |
545a8e79 | 848 | struct netvsc_device *net_device; |
742fe54c | 849 | u16 q_idx = channel->offermsg.offer.sub_channel_index; |
545a8e79 | 850 | struct netvsc_channel *nvchan; |
84bf9cef | 851 | struct sk_buff *skb; |
84bf9cef | 852 | struct netvsc_stats *rx_stats; |
84bf9cef | 853 | |
9cbcc428 | 854 | if (net->reg_state != NETREG_REGISTERED) |
84bf9cef KS |
855 | return NVSP_STAT_FAIL; |
856 | ||
0719e72c | 857 | rcu_read_lock(); |
545a8e79 | 858 | net_device = rcu_dereference(net_device_ctx->nvdev); |
859 | if (unlikely(!net_device)) | |
860 | goto drop; | |
861 | ||
862 | nvchan = &net_device->chan_table[q_idx]; | |
84bf9cef KS |
863 | |
864 | /* Allocate a skb - TODO direct I/O to pages? */ | |
e91e7dd7 | 865 | skb = netvsc_alloc_recv_skb(net, &nvchan->napi, |
866 | csum_info, vlan, data, len); | |
84bf9cef | 867 | if (unlikely(!skb)) { |
545a8e79 | 868 | drop: |
84bf9cef | 869 | ++net->stats.rx_dropped; |
0719e72c | 870 | rcu_read_unlock(); |
84bf9cef KS |
871 | return NVSP_STAT_FAIL; |
872 | } | |
5b54dac8 | 873 | |
0c195567 | 874 | skb_record_rx_queue(skb, q_idx); |
9cbcc428 SH |
875 | |
876 | /* | |
877 | * Even if injecting the packet, record the statistics | |
878 | * on the synthetic device because modifying the VF device | |
879 | * statistics will not work correctly. | |
880 | */ | |
742fe54c | 881 | rx_stats = &nvchan->rx_stats; |
4b02b58b | 882 | u64_stats_update_begin(&rx_stats->syncp); |
7eafd9b4 | 883 | rx_stats->packets++; |
dc54a08c | 884 | rx_stats->bytes += len; |
f7ad75b7 SH |
885 | |
886 | if (skb->pkt_type == PACKET_BROADCAST) | |
887 | ++rx_stats->broadcast; | |
888 | else if (skb->pkt_type == PACKET_MULTICAST) | |
889 | ++rx_stats->multicast; | |
4b02b58b | 890 | u64_stats_update_end(&rx_stats->syncp); |
9495c282 | 891 | |
742fe54c | 892 | napi_gro_receive(&nvchan->napi, skb); |
0719e72c | 893 | rcu_read_unlock(); |
fceaf24a | 894 | |
8139bb20 | 895 | return NVSP_STAT_SUCCESS; |
fceaf24a HJ |
896 | } |
897 | ||
f82f4ad7 SH |
898 | static void netvsc_get_drvinfo(struct net_device *net, |
899 | struct ethtool_drvinfo *info) | |
900 | { | |
7826d43f | 901 | strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver)); |
7826d43f | 902 | strlcpy(info->fw_version, "N/A", sizeof(info->fw_version)); |
f82f4ad7 SH |
903 | } |
904 | ||
59995370 AS |
905 | static void netvsc_get_channels(struct net_device *net, |
906 | struct ethtool_channels *channel) | |
907 | { | |
908 | struct net_device_context *net_device_ctx = netdev_priv(net); | |
545a8e79 | 909 | struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev); |
59995370 AS |
910 | |
911 | if (nvdev) { | |
912 | channel->max_combined = nvdev->max_chn; | |
913 | channel->combined_count = nvdev->num_chn; | |
914 | } | |
915 | } | |
916 | ||
cef03981 SH |
917 | static int netvsc_detach(struct net_device *ndev, |
918 | struct netvsc_device *nvdev) | |
919 | { | |
920 | struct net_device_context *ndev_ctx = netdev_priv(ndev); | |
921 | struct hv_device *hdev = ndev_ctx->device_ctx; | |
922 | int ret; | |
923 | ||
924 | /* Don't try continuing to try and setup sub channels */ | |
925 | if (cancel_work_sync(&nvdev->subchan_work)) | |
926 | nvdev->num_chn = 1; | |
927 | ||
928 | /* If device was up (receiving) then shutdown */ | |
929 | if (netif_running(ndev)) { | |
a1979526 | 930 | netvsc_tx_disable(nvdev, ndev); |
cef03981 SH |
931 | |
932 | ret = rndis_filter_close(nvdev); | |
933 | if (ret) { | |
934 | netdev_err(ndev, | |
935 | "unable to close device (ret %d).\n", ret); | |
936 | return ret; | |
937 | } | |
938 | ||
939 | ret = netvsc_wait_until_empty(nvdev); | |
940 | if (ret) { | |
941 | netdev_err(ndev, | |
942 | "Ring buffer not empty after closing rndis\n"); | |
943 | return ret; | |
944 | } | |
945 | } | |
946 | ||
947 | netif_device_detach(ndev); | |
948 | ||
949 | rndis_filter_device_remove(hdev, nvdev); | |
950 | ||
951 | return 0; | |
952 | } | |
953 | ||
954 | static int netvsc_attach(struct net_device *ndev, | |
955 | struct netvsc_device_info *dev_info) | |
956 | { | |
957 | struct net_device_context *ndev_ctx = netdev_priv(ndev); | |
958 | struct hv_device *hdev = ndev_ctx->device_ctx; | |
959 | struct netvsc_device *nvdev; | |
960 | struct rndis_device *rdev; | |
961 | int ret; | |
962 | ||
963 | nvdev = rndis_filter_device_add(hdev, dev_info); | |
964 | if (IS_ERR(nvdev)) | |
965 | return PTR_ERR(nvdev); | |
966 | ||
2950481a SH |
967 | if (nvdev->num_chn > 1) { |
968 | ret = rndis_set_subchannel(ndev, nvdev); | |
969 | ||
970 | /* if unavailable, just proceed with one queue */ | |
971 | if (ret) { | |
972 | nvdev->max_chn = 1; | |
973 | nvdev->num_chn = 1; | |
974 | } | |
975 | } | |
976 | ||
977 | /* In any case device is now ready */ | |
978 | netif_device_attach(ndev); | |
cef03981 | 979 | |
2950481a | 980 | /* Note: enable and attach happen when sub-channels setup */ |
cef03981 SH |
981 | netif_carrier_off(ndev); |
982 | ||
983 | if (netif_running(ndev)) { | |
984 | ret = rndis_filter_open(nvdev); | |
985 | if (ret) | |
986 | return ret; | |
987 | ||
988 | rdev = nvdev->extension; | |
989 | if (!rdev->link_state) | |
990 | netif_carrier_on(ndev); | |
991 | } | |
992 | ||
993 | return 0; | |
994 | } | |
995 | ||
b5960e6e AS |
996 | static int netvsc_set_channels(struct net_device *net, |
997 | struct ethtool_channels *channels) | |
998 | { | |
999 | struct net_device_context *net_device_ctx = netdev_priv(net); | |
545a8e79 | 1000 | struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev); |
7ca45933 | 1001 | unsigned int orig, count = channels->combined_count; |
1002 | struct netvsc_device_info device_info; | |
cef03981 | 1003 | int ret; |
2b01888d | 1004 | |
1005 | /* We do not support separate count for rx, tx, or other */ | |
1006 | if (count == 0 || | |
1007 | channels->rx_count || channels->tx_count || channels->other_count) | |
1008 | return -EINVAL; | |
1009 | ||
a0be450e | 1010 | if (!nvdev || nvdev->destroy) |
b5960e6e AS |
1011 | return -ENODEV; |
1012 | ||
2b01888d | 1013 | if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5) |
b5960e6e | 1014 | return -EINVAL; |
b5960e6e | 1015 | |
2b01888d | 1016 | if (count > nvdev->max_chn) |
b5960e6e AS |
1017 | return -EINVAL; |
1018 | ||
7ca45933 | 1019 | orig = nvdev->num_chn; |
b5960e6e | 1020 | |
7ca45933 | 1021 | memset(&device_info, 0, sizeof(device_info)); |
1022 | device_info.num_chn = count; | |
1023 | device_info.ring_size = ring_size; | |
8b532797 | 1024 | device_info.send_sections = nvdev->send_section_cnt; |
0ab09bef | 1025 | device_info.send_section_size = nvdev->send_section_size; |
8b532797 | 1026 | device_info.recv_sections = nvdev->recv_section_cnt; |
0ab09bef | 1027 | device_info.recv_section_size = nvdev->recv_section_size; |
8b532797 | 1028 | |
cef03981 SH |
1029 | ret = netvsc_detach(net, nvdev); |
1030 | if (ret) | |
1031 | return ret; | |
7ca45933 | 1032 | |
cef03981 SH |
1033 | ret = netvsc_attach(net, &device_info); |
1034 | if (ret) { | |
7ca45933 | 1035 | device_info.num_chn = orig; |
cef03981 SH |
1036 | if (netvsc_attach(net, &device_info)) |
1037 | netdev_err(net, "restoring channel setting failed\n"); | |
7ca45933 | 1038 | } |
b5960e6e | 1039 | |
b5960e6e | 1040 | return ret; |
b5960e6e AS |
1041 | } |
1042 | ||
5e8456fd PR |
1043 | static bool |
1044 | netvsc_validate_ethtool_ss_cmd(const struct ethtool_link_ksettings *cmd) | |
49eb9389 | 1045 | { |
5e8456fd PR |
1046 | struct ethtool_link_ksettings diff1 = *cmd; |
1047 | struct ethtool_link_ksettings diff2 = {}; | |
49eb9389 | 1048 | |
5e8456fd PR |
1049 | diff1.base.speed = 0; |
1050 | diff1.base.duplex = 0; | |
49eb9389 | 1051 | /* advertising and cmd are usually set */ |
5e8456fd PR |
1052 | ethtool_link_ksettings_zero_link_mode(&diff1, advertising); |
1053 | diff1.base.cmd = 0; | |
49eb9389 | 1054 | /* We set port to PORT_OTHER */ |
5e8456fd | 1055 | diff2.base.port = PORT_OTHER; |
49eb9389 | 1056 | |
1057 | return !memcmp(&diff1, &diff2, sizeof(diff1)); | |
1058 | } | |
1059 | ||
1060 | static void netvsc_init_settings(struct net_device *dev) | |
1061 | { | |
1062 | struct net_device_context *ndc = netdev_priv(dev); | |
1063 | ||
486e3981 | 1064 | ndc->l4_hash = HV_DEFAULT_L4HASH; |
4823eb2f | 1065 | |
49eb9389 | 1066 | ndc->speed = SPEED_UNKNOWN; |
f3c9d40e | 1067 | ndc->duplex = DUPLEX_FULL; |
49eb9389 | 1068 | } |
1069 | ||
5e8456fd PR |
1070 | static int netvsc_get_link_ksettings(struct net_device *dev, |
1071 | struct ethtool_link_ksettings *cmd) | |
49eb9389 | 1072 | { |
1073 | struct net_device_context *ndc = netdev_priv(dev); | |
1074 | ||
5e8456fd PR |
1075 | cmd->base.speed = ndc->speed; |
1076 | cmd->base.duplex = ndc->duplex; | |
1077 | cmd->base.port = PORT_OTHER; | |
49eb9389 | 1078 | |
1079 | return 0; | |
1080 | } | |
1081 | ||
5e8456fd PR |
1082 | static int netvsc_set_link_ksettings(struct net_device *dev, |
1083 | const struct ethtool_link_ksettings *cmd) | |
49eb9389 | 1084 | { |
1085 | struct net_device_context *ndc = netdev_priv(dev); | |
1086 | u32 speed; | |
1087 | ||
5e8456fd | 1088 | speed = cmd->base.speed; |
49eb9389 | 1089 | if (!ethtool_validate_speed(speed) || |
5e8456fd | 1090 | !ethtool_validate_duplex(cmd->base.duplex) || |
49eb9389 | 1091 | !netvsc_validate_ethtool_ss_cmd(cmd)) |
1092 | return -EINVAL; | |
1093 | ||
1094 | ndc->speed = speed; | |
5e8456fd | 1095 | ndc->duplex = cmd->base.duplex; |
49eb9389 | 1096 | |
1097 | return 0; | |
1098 | } | |
1099 | ||
4d447c9a HZ |
1100 | static int netvsc_change_mtu(struct net_device *ndev, int mtu) |
1101 | { | |
1102 | struct net_device_context *ndevctx = netdev_priv(ndev); | |
0c195567 | 1103 | struct net_device *vf_netdev = rtnl_dereference(ndevctx->vf_netdev); |
545a8e79 | 1104 | struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev); |
9749fed5 | 1105 | int orig_mtu = ndev->mtu; |
4d447c9a | 1106 | struct netvsc_device_info device_info; |
9749fed5 | 1107 | int ret = 0; |
4d447c9a | 1108 | |
a0be450e | 1109 | if (!nvdev || nvdev->destroy) |
4d447c9a HZ |
1110 | return -ENODEV; |
1111 | ||
0c195567 | 1112 | /* Change MTU of underlying VF netdev first. */ |
1113 | if (vf_netdev) { | |
1114 | ret = dev_set_mtu(vf_netdev, mtu); | |
1115 | if (ret) | |
1116 | return ret; | |
1117 | } | |
1118 | ||
152669bd DC |
1119 | memset(&device_info, 0, sizeof(device_info)); |
1120 | device_info.ring_size = ring_size; | |
1121 | device_info.num_chn = nvdev->num_chn; | |
8b532797 | 1122 | device_info.send_sections = nvdev->send_section_cnt; |
0ab09bef | 1123 | device_info.send_section_size = nvdev->send_section_size; |
8b532797 | 1124 | device_info.recv_sections = nvdev->recv_section_cnt; |
0ab09bef | 1125 | device_info.recv_section_size = nvdev->recv_section_size; |
152669bd | 1126 | |
cef03981 SH |
1127 | ret = netvsc_detach(ndev, nvdev); |
1128 | if (ret) | |
1129 | goto rollback_vf; | |
4d447c9a HZ |
1130 | |
1131 | ndev->mtu = mtu; | |
1132 | ||
cef03981 SH |
1133 | ret = netvsc_attach(ndev, &device_info); |
1134 | if (ret) | |
1135 | goto rollback; | |
4d447c9a | 1136 | |
cef03981 | 1137 | return 0; |
ea383bf1 | 1138 | |
cef03981 SH |
1139 | rollback: |
1140 | /* Attempt rollback to original MTU */ | |
1141 | ndev->mtu = orig_mtu; | |
163891d7 | 1142 | |
cef03981 SH |
1143 | if (netvsc_attach(ndev, &device_info)) |
1144 | netdev_err(ndev, "restoring mtu failed\n"); | |
1145 | rollback_vf: | |
1146 | if (vf_netdev) | |
1147 | dev_set_mtu(vf_netdev, orig_mtu); | |
1bdcec8a | 1148 | |
9749fed5 | 1149 | return ret; |
4d447c9a HZ |
1150 | } |
1151 | ||
0c195567 | 1152 | static void netvsc_get_vf_stats(struct net_device *net, |
1153 | struct netvsc_vf_pcpu_stats *tot) | |
1154 | { | |
1155 | struct net_device_context *ndev_ctx = netdev_priv(net); | |
1156 | int i; | |
1157 | ||
1158 | memset(tot, 0, sizeof(*tot)); | |
1159 | ||
1160 | for_each_possible_cpu(i) { | |
1161 | const struct netvsc_vf_pcpu_stats *stats | |
1162 | = per_cpu_ptr(ndev_ctx->vf_stats, i); | |
1163 | u64 rx_packets, rx_bytes, tx_packets, tx_bytes; | |
1164 | unsigned int start; | |
1165 | ||
1166 | do { | |
1167 | start = u64_stats_fetch_begin_irq(&stats->syncp); | |
1168 | rx_packets = stats->rx_packets; | |
1169 | tx_packets = stats->tx_packets; | |
1170 | rx_bytes = stats->rx_bytes; | |
1171 | tx_bytes = stats->tx_bytes; | |
1172 | } while (u64_stats_fetch_retry_irq(&stats->syncp, start)); | |
1173 | ||
1174 | tot->rx_packets += rx_packets; | |
1175 | tot->tx_packets += tx_packets; | |
1176 | tot->rx_bytes += rx_bytes; | |
1177 | tot->tx_bytes += tx_bytes; | |
1178 | tot->tx_dropped += stats->tx_dropped; | |
1179 | } | |
1180 | } | |
1181 | ||
bc1f4470 | 1182 | static void netvsc_get_stats64(struct net_device *net, |
1183 | struct rtnl_link_stats64 *t) | |
7eafd9b4 | 1184 | { |
1185 | struct net_device_context *ndev_ctx = netdev_priv(net); | |
776e726b | 1186 | struct netvsc_device *nvdev = rcu_dereference_rtnl(ndev_ctx->nvdev); |
0c195567 | 1187 | struct netvsc_vf_pcpu_stats vf_tot; |
89bb42b1 | 1188 | int i; |
6c80f3fc SX |
1189 | |
1190 | if (!nvdev) | |
1191 | return; | |
1192 | ||
0c195567 | 1193 | netdev_stats_to_stats64(t, &net->stats); |
1194 | ||
1195 | netvsc_get_vf_stats(net, &vf_tot); | |
1196 | t->rx_packets += vf_tot.rx_packets; | |
1197 | t->tx_packets += vf_tot.tx_packets; | |
1198 | t->rx_bytes += vf_tot.rx_bytes; | |
1199 | t->tx_bytes += vf_tot.tx_bytes; | |
1200 | t->tx_dropped += vf_tot.tx_dropped; | |
1201 | ||
6c80f3fc SX |
1202 | for (i = 0; i < nvdev->num_chn; i++) { |
1203 | const struct netvsc_channel *nvchan = &nvdev->chan_table[i]; | |
1204 | const struct netvsc_stats *stats; | |
1205 | u64 packets, bytes, multicast; | |
7eafd9b4 | 1206 | unsigned int start; |
1207 | ||
6c80f3fc | 1208 | stats = &nvchan->tx_stats; |
7eafd9b4 | 1209 | do { |
6c80f3fc SX |
1210 | start = u64_stats_fetch_begin_irq(&stats->syncp); |
1211 | packets = stats->packets; | |
1212 | bytes = stats->bytes; | |
1213 | } while (u64_stats_fetch_retry_irq(&stats->syncp, start)); | |
1214 | ||
1215 | t->tx_bytes += bytes; | |
1216 | t->tx_packets += packets; | |
7eafd9b4 | 1217 | |
6c80f3fc | 1218 | stats = &nvchan->rx_stats; |
7eafd9b4 | 1219 | do { |
6c80f3fc SX |
1220 | start = u64_stats_fetch_begin_irq(&stats->syncp); |
1221 | packets = stats->packets; | |
1222 | bytes = stats->bytes; | |
1223 | multicast = stats->multicast + stats->broadcast; | |
1224 | } while (u64_stats_fetch_retry_irq(&stats->syncp, start)); | |
1225 | ||
1226 | t->rx_bytes += bytes; | |
1227 | t->rx_packets += packets; | |
1228 | t->multicast += multicast; | |
7eafd9b4 | 1229 | } |
7eafd9b4 | 1230 | } |
1ce09e89 HZ |
1231 | |
1232 | static int netvsc_set_mac_addr(struct net_device *ndev, void *p) | |
1233 | { | |
867047c4 | 1234 | struct net_device_context *ndc = netdev_priv(ndev); |
16ba3266 | 1235 | struct net_device *vf_netdev = rtnl_dereference(ndc->vf_netdev); |
867047c4 | 1236 | struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev); |
1ce09e89 | 1237 | struct sockaddr *addr = p; |
1ce09e89 HZ |
1238 | int err; |
1239 | ||
16ba3266 | 1240 | err = eth_prepare_mac_addr_change(ndev, p); |
1241 | if (err) | |
1ce09e89 HZ |
1242 | return err; |
1243 | ||
867047c4 | 1244 | if (!nvdev) |
1245 | return -ENODEV; | |
1246 | ||
16ba3266 | 1247 | if (vf_netdev) { |
1248 | err = dev_set_mac_address(vf_netdev, addr); | |
1249 | if (err) | |
1250 | return err; | |
1251 | } | |
1252 | ||
867047c4 | 1253 | err = rndis_filter_set_device_mac(nvdev, addr->sa_data); |
16ba3266 | 1254 | if (!err) { |
1255 | eth_commit_mac_addr_change(ndev, p); | |
1256 | } else if (vf_netdev) { | |
1257 | /* rollback change on VF */ | |
1258 | memcpy(addr->sa_data, ndev->dev_addr, ETH_ALEN); | |
1259 | dev_set_mac_address(vf_netdev, addr); | |
1ce09e89 HZ |
1260 | } |
1261 | ||
1262 | return err; | |
1263 | } | |
1264 | ||
4323b47c SH |
1265 | static const struct { |
1266 | char name[ETH_GSTRING_LEN]; | |
1267 | u16 offset; | |
1268 | } netvsc_stats[] = { | |
1269 | { "tx_scattered", offsetof(struct netvsc_ethtool_stats, tx_scattered) }, | |
1270 | { "tx_no_memory", offsetof(struct netvsc_ethtool_stats, tx_no_memory) }, | |
1271 | { "tx_no_space", offsetof(struct netvsc_ethtool_stats, tx_no_space) }, | |
1272 | { "tx_too_big", offsetof(struct netvsc_ethtool_stats, tx_too_big) }, | |
1273 | { "tx_busy", offsetof(struct netvsc_ethtool_stats, tx_busy) }, | |
cad5c197 | 1274 | { "tx_send_full", offsetof(struct netvsc_ethtool_stats, tx_send_full) }, |
1275 | { "rx_comp_busy", offsetof(struct netvsc_ethtool_stats, rx_comp_busy) }, | |
09af87d1 SX |
1276 | { "stop_queue", offsetof(struct netvsc_ethtool_stats, stop_queue) }, |
1277 | { "wake_queue", offsetof(struct netvsc_ethtool_stats, wake_queue) }, | |
0c195567 | 1278 | }, vf_stats[] = { |
1279 | { "vf_rx_packets", offsetof(struct netvsc_vf_pcpu_stats, rx_packets) }, | |
1280 | { "vf_rx_bytes", offsetof(struct netvsc_vf_pcpu_stats, rx_bytes) }, | |
1281 | { "vf_tx_packets", offsetof(struct netvsc_vf_pcpu_stats, tx_packets) }, | |
1282 | { "vf_tx_bytes", offsetof(struct netvsc_vf_pcpu_stats, tx_bytes) }, | |
1283 | { "vf_tx_dropped", offsetof(struct netvsc_vf_pcpu_stats, tx_dropped) }, | |
4323b47c SH |
1284 | }; |
1285 | ||
6c80f3fc | 1286 | #define NETVSC_GLOBAL_STATS_LEN ARRAY_SIZE(netvsc_stats) |
0c195567 | 1287 | #define NETVSC_VF_STATS_LEN ARRAY_SIZE(vf_stats) |
6c80f3fc SX |
1288 | |
1289 | /* 4 statistics per queue (rx/tx packets/bytes) */ | |
1290 | #define NETVSC_QUEUE_STATS_LEN(dev) ((dev)->num_chn * 4) | |
1291 | ||
4323b47c SH |
1292 | static int netvsc_get_sset_count(struct net_device *dev, int string_set) |
1293 | { | |
6c80f3fc | 1294 | struct net_device_context *ndc = netdev_priv(dev); |
fbd4c7e7 | 1295 | struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev); |
545a8e79 | 1296 | |
1297 | if (!nvdev) | |
1298 | return -ENODEV; | |
6c80f3fc | 1299 | |
4323b47c SH |
1300 | switch (string_set) { |
1301 | case ETH_SS_STATS: | |
0c195567 | 1302 | return NETVSC_GLOBAL_STATS_LEN |
1303 | + NETVSC_VF_STATS_LEN | |
1304 | + NETVSC_QUEUE_STATS_LEN(nvdev); | |
4323b47c SH |
1305 | default: |
1306 | return -EINVAL; | |
1307 | } | |
1308 | } | |
1309 | ||
1310 | static void netvsc_get_ethtool_stats(struct net_device *dev, | |
1311 | struct ethtool_stats *stats, u64 *data) | |
1312 | { | |
1313 | struct net_device_context *ndc = netdev_priv(dev); | |
867047c4 | 1314 | struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev); |
4323b47c | 1315 | const void *nds = &ndc->eth_stats; |
6c80f3fc | 1316 | const struct netvsc_stats *qstats; |
0c195567 | 1317 | struct netvsc_vf_pcpu_stats sum; |
6c80f3fc SX |
1318 | unsigned int start; |
1319 | u64 packets, bytes; | |
1320 | int i, j; | |
4323b47c | 1321 | |
545a8e79 | 1322 | if (!nvdev) |
1323 | return; | |
1324 | ||
6c80f3fc | 1325 | for (i = 0; i < NETVSC_GLOBAL_STATS_LEN; i++) |
4323b47c | 1326 | data[i] = *(unsigned long *)(nds + netvsc_stats[i].offset); |
6c80f3fc | 1327 | |
0c195567 | 1328 | netvsc_get_vf_stats(dev, &sum); |
1329 | for (j = 0; j < NETVSC_VF_STATS_LEN; j++) | |
1330 | data[i++] = *(u64 *)((void *)&sum + vf_stats[j].offset); | |
1331 | ||
6c80f3fc SX |
1332 | for (j = 0; j < nvdev->num_chn; j++) { |
1333 | qstats = &nvdev->chan_table[j].tx_stats; | |
1334 | ||
1335 | do { | |
1336 | start = u64_stats_fetch_begin_irq(&qstats->syncp); | |
1337 | packets = qstats->packets; | |
1338 | bytes = qstats->bytes; | |
1339 | } while (u64_stats_fetch_retry_irq(&qstats->syncp, start)); | |
1340 | data[i++] = packets; | |
1341 | data[i++] = bytes; | |
1342 | ||
1343 | qstats = &nvdev->chan_table[j].rx_stats; | |
1344 | do { | |
1345 | start = u64_stats_fetch_begin_irq(&qstats->syncp); | |
1346 | packets = qstats->packets; | |
1347 | bytes = qstats->bytes; | |
1348 | } while (u64_stats_fetch_retry_irq(&qstats->syncp, start)); | |
1349 | data[i++] = packets; | |
1350 | data[i++] = bytes; | |
1351 | } | |
4323b47c SH |
1352 | } |
1353 | ||
1354 | static void netvsc_get_strings(struct net_device *dev, u32 stringset, u8 *data) | |
1355 | { | |
6c80f3fc | 1356 | struct net_device_context *ndc = netdev_priv(dev); |
867047c4 | 1357 | struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev); |
6c80f3fc | 1358 | u8 *p = data; |
4323b47c SH |
1359 | int i; |
1360 | ||
545a8e79 | 1361 | if (!nvdev) |
1362 | return; | |
1363 | ||
4323b47c SH |
1364 | switch (stringset) { |
1365 | case ETH_SS_STATS: | |
0c195567 | 1366 | for (i = 0; i < ARRAY_SIZE(netvsc_stats); i++) { |
1367 | memcpy(p, netvsc_stats[i].name, ETH_GSTRING_LEN); | |
1368 | p += ETH_GSTRING_LEN; | |
1369 | } | |
1370 | ||
1371 | for (i = 0; i < ARRAY_SIZE(vf_stats); i++) { | |
1372 | memcpy(p, vf_stats[i].name, ETH_GSTRING_LEN); | |
1373 | p += ETH_GSTRING_LEN; | |
1374 | } | |
6c80f3fc | 1375 | |
6c80f3fc SX |
1376 | for (i = 0; i < nvdev->num_chn; i++) { |
1377 | sprintf(p, "tx_queue_%u_packets", i); | |
1378 | p += ETH_GSTRING_LEN; | |
1379 | sprintf(p, "tx_queue_%u_bytes", i); | |
1380 | p += ETH_GSTRING_LEN; | |
1381 | sprintf(p, "rx_queue_%u_packets", i); | |
1382 | p += ETH_GSTRING_LEN; | |
1383 | sprintf(p, "rx_queue_%u_bytes", i); | |
1384 | p += ETH_GSTRING_LEN; | |
1385 | } | |
1386 | ||
4323b47c SH |
1387 | break; |
1388 | } | |
1389 | } | |
1390 | ||
b5a5dc8d | 1391 | static int |
4823eb2f HZ |
1392 | netvsc_get_rss_hash_opts(struct net_device_context *ndc, |
1393 | struct ethtool_rxnfc *info) | |
b5a5dc8d | 1394 | { |
486e3981 HZ |
1395 | const u32 l4_flag = RXH_L4_B_0_1 | RXH_L4_B_2_3; |
1396 | ||
b5a5dc8d | 1397 | info->data = RXH_IP_SRC | RXH_IP_DST; |
1398 | ||
1399 | switch (info->flow_type) { | |
1400 | case TCP_V4_FLOW: | |
0518ec4f HZ |
1401 | if (ndc->l4_hash & HV_TCP4_L4HASH) |
1402 | info->data |= l4_flag; | |
1403 | ||
1404 | break; | |
1405 | ||
b5a5dc8d | 1406 | case TCP_V6_FLOW: |
0518ec4f HZ |
1407 | if (ndc->l4_hash & HV_TCP6_L4HASH) |
1408 | info->data |= l4_flag; | |
1409 | ||
4823eb2f HZ |
1410 | break; |
1411 | ||
b5a5dc8d | 1412 | case UDP_V4_FLOW: |
486e3981 HZ |
1413 | if (ndc->l4_hash & HV_UDP4_L4HASH) |
1414 | info->data |= l4_flag; | |
4823eb2f HZ |
1415 | |
1416 | break; | |
1417 | ||
b5a5dc8d | 1418 | case UDP_V6_FLOW: |
486e3981 HZ |
1419 | if (ndc->l4_hash & HV_UDP6_L4HASH) |
1420 | info->data |= l4_flag; | |
4823eb2f HZ |
1421 | |
1422 | break; | |
1423 | ||
b5a5dc8d | 1424 | case IPV4_FLOW: |
1425 | case IPV6_FLOW: | |
1426 | break; | |
1427 | default: | |
1428 | info->data = 0; | |
1429 | break; | |
1430 | } | |
1431 | ||
1432 | return 0; | |
1433 | } | |
1434 | ||
b448f4e8 | 1435 | static int |
1436 | netvsc_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info, | |
1437 | u32 *rules) | |
1438 | { | |
1439 | struct net_device_context *ndc = netdev_priv(dev); | |
867047c4 | 1440 | struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev); |
545a8e79 | 1441 | |
1442 | if (!nvdev) | |
1443 | return -ENODEV; | |
b448f4e8 | 1444 | |
1445 | switch (info->cmd) { | |
1446 | case ETHTOOL_GRXRINGS: | |
1447 | info->data = nvdev->num_chn; | |
1448 | return 0; | |
b5a5dc8d | 1449 | |
1450 | case ETHTOOL_GRXFH: | |
4823eb2f | 1451 | return netvsc_get_rss_hash_opts(ndc, info); |
b448f4e8 | 1452 | } |
1453 | return -EOPNOTSUPP; | |
1454 | } | |
1455 | ||
4823eb2f HZ |
1456 | static int netvsc_set_rss_hash_opts(struct net_device_context *ndc, |
1457 | struct ethtool_rxnfc *info) | |
1458 | { | |
1459 | if (info->data == (RXH_IP_SRC | RXH_IP_DST | | |
1460 | RXH_L4_B_0_1 | RXH_L4_B_2_3)) { | |
486e3981 | 1461 | switch (info->flow_type) { |
0518ec4f HZ |
1462 | case TCP_V4_FLOW: |
1463 | ndc->l4_hash |= HV_TCP4_L4HASH; | |
1464 | break; | |
1465 | ||
1466 | case TCP_V6_FLOW: | |
1467 | ndc->l4_hash |= HV_TCP6_L4HASH; | |
1468 | break; | |
1469 | ||
486e3981 HZ |
1470 | case UDP_V4_FLOW: |
1471 | ndc->l4_hash |= HV_UDP4_L4HASH; | |
1472 | break; | |
1473 | ||
1474 | case UDP_V6_FLOW: | |
1475 | ndc->l4_hash |= HV_UDP6_L4HASH; | |
1476 | break; | |
1477 | ||
1478 | default: | |
4823eb2f | 1479 | return -EOPNOTSUPP; |
486e3981 | 1480 | } |
4823eb2f HZ |
1481 | |
1482 | return 0; | |
1483 | } | |
1484 | ||
1485 | if (info->data == (RXH_IP_SRC | RXH_IP_DST)) { | |
486e3981 | 1486 | switch (info->flow_type) { |
0518ec4f HZ |
1487 | case TCP_V4_FLOW: |
1488 | ndc->l4_hash &= ~HV_TCP4_L4HASH; | |
1489 | break; | |
1490 | ||
1491 | case TCP_V6_FLOW: | |
1492 | ndc->l4_hash &= ~HV_TCP6_L4HASH; | |
1493 | break; | |
1494 | ||
486e3981 HZ |
1495 | case UDP_V4_FLOW: |
1496 | ndc->l4_hash &= ~HV_UDP4_L4HASH; | |
1497 | break; | |
1498 | ||
1499 | case UDP_V6_FLOW: | |
1500 | ndc->l4_hash &= ~HV_UDP6_L4HASH; | |
1501 | break; | |
1502 | ||
1503 | default: | |
4823eb2f | 1504 | return -EOPNOTSUPP; |
486e3981 | 1505 | } |
4823eb2f HZ |
1506 | |
1507 | return 0; | |
1508 | } | |
1509 | ||
1510 | return -EOPNOTSUPP; | |
1511 | } | |
1512 | ||
1513 | static int | |
1514 | netvsc_set_rxnfc(struct net_device *ndev, struct ethtool_rxnfc *info) | |
1515 | { | |
1516 | struct net_device_context *ndc = netdev_priv(ndev); | |
1517 | ||
1518 | if (info->cmd == ETHTOOL_SRXFH) | |
1519 | return netvsc_set_rss_hash_opts(ndc, info); | |
1520 | ||
1521 | return -EOPNOTSUPP; | |
1522 | } | |
1523 | ||
316158fe | 1524 | #ifdef CONFIG_NET_POLL_CONTROLLER |
a5ecd439 | 1525 | static void netvsc_poll_controller(struct net_device *dev) |
316158fe | 1526 | { |
a5ecd439 | 1527 | struct net_device_context *ndc = netdev_priv(dev); |
1528 | struct netvsc_device *ndev; | |
1529 | int i; | |
1530 | ||
1531 | rcu_read_lock(); | |
1532 | ndev = rcu_dereference(ndc->nvdev); | |
1533 | if (ndev) { | |
1534 | for (i = 0; i < ndev->num_chn; i++) { | |
1535 | struct netvsc_channel *nvchan = &ndev->chan_table[i]; | |
1536 | ||
1537 | napi_schedule(&nvchan->napi); | |
1538 | } | |
1539 | } | |
1540 | rcu_read_unlock(); | |
316158fe RW |
1541 | } |
1542 | #endif | |
1ce09e89 | 1543 | |
962f3fee | 1544 | static u32 netvsc_get_rxfh_key_size(struct net_device *dev) |
1545 | { | |
1546 | return NETVSC_HASH_KEYLEN; | |
1547 | } | |
1548 | ||
1549 | static u32 netvsc_rss_indir_size(struct net_device *dev) | |
1550 | { | |
ff4a4419 | 1551 | return ITAB_NUM; |
962f3fee | 1552 | } |
1553 | ||
1554 | static int netvsc_get_rxfh(struct net_device *dev, u32 *indir, u8 *key, | |
1555 | u8 *hfunc) | |
1556 | { | |
1557 | struct net_device_context *ndc = netdev_priv(dev); | |
867047c4 | 1558 | struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev); |
eb996edb | 1559 | struct rndis_device *rndis_dev; |
ff4a4419 | 1560 | int i; |
962f3fee | 1561 | |
545a8e79 | 1562 | if (!ndev) |
1563 | return -ENODEV; | |
1564 | ||
962f3fee | 1565 | if (hfunc) |
1566 | *hfunc = ETH_RSS_HASH_TOP; /* Toeplitz */ | |
1567 | ||
eb996edb | 1568 | rndis_dev = ndev->extension; |
ff4a4419 | 1569 | if (indir) { |
1570 | for (i = 0; i < ITAB_NUM; i++) | |
47371300 | 1571 | indir[i] = rndis_dev->rx_table[i]; |
ff4a4419 | 1572 | } |
1573 | ||
962f3fee | 1574 | if (key) |
1575 | memcpy(key, rndis_dev->rss_key, NETVSC_HASH_KEYLEN); | |
1576 | ||
1577 | return 0; | |
1578 | } | |
1579 | ||
1580 | static int netvsc_set_rxfh(struct net_device *dev, const u32 *indir, | |
1581 | const u8 *key, const u8 hfunc) | |
1582 | { | |
1583 | struct net_device_context *ndc = netdev_priv(dev); | |
545a8e79 | 1584 | struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev); |
eb996edb | 1585 | struct rndis_device *rndis_dev; |
ff4a4419 | 1586 | int i; |
962f3fee | 1587 | |
545a8e79 | 1588 | if (!ndev) |
1589 | return -ENODEV; | |
1590 | ||
962f3fee | 1591 | if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP) |
1592 | return -EOPNOTSUPP; | |
1593 | ||
eb996edb | 1594 | rndis_dev = ndev->extension; |
ff4a4419 | 1595 | if (indir) { |
1596 | for (i = 0; i < ITAB_NUM; i++) | |
db3cd7af | 1597 | if (indir[i] >= ndev->num_chn) |
ff4a4419 | 1598 | return -EINVAL; |
1599 | ||
1600 | for (i = 0; i < ITAB_NUM; i++) | |
47371300 | 1601 | rndis_dev->rx_table[i] = indir[i]; |
ff4a4419 | 1602 | } |
1603 | ||
1604 | if (!key) { | |
1605 | if (!indir) | |
1606 | return 0; | |
1607 | ||
1608 | key = rndis_dev->rss_key; | |
1609 | } | |
962f3fee | 1610 | |
715e2ec5 | 1611 | return rndis_filter_set_rss_param(rndis_dev, key); |
962f3fee | 1612 | } |
1613 | ||
8b532797 | 1614 | /* Hyper-V RNDIS protocol does not have ring in the HW sense. |
1615 | * It does have pre-allocated receive area which is divided into sections. | |
1616 | */ | |
1617 | static void __netvsc_get_ringparam(struct netvsc_device *nvdev, | |
1618 | struct ethtool_ringparam *ring) | |
1619 | { | |
1620 | u32 max_buf_size; | |
1621 | ||
1622 | ring->rx_pending = nvdev->recv_section_cnt; | |
1623 | ring->tx_pending = nvdev->send_section_cnt; | |
1624 | ||
1625 | if (nvdev->nvsp_version <= NVSP_PROTOCOL_VERSION_2) | |
1626 | max_buf_size = NETVSC_RECEIVE_BUFFER_SIZE_LEGACY; | |
1627 | else | |
1628 | max_buf_size = NETVSC_RECEIVE_BUFFER_SIZE; | |
1629 | ||
1630 | ring->rx_max_pending = max_buf_size / nvdev->recv_section_size; | |
1631 | ring->tx_max_pending = NETVSC_SEND_BUFFER_SIZE | |
1632 | / nvdev->send_section_size; | |
1633 | } | |
1634 | ||
1635 | static void netvsc_get_ringparam(struct net_device *ndev, | |
1636 | struct ethtool_ringparam *ring) | |
1637 | { | |
1638 | struct net_device_context *ndevctx = netdev_priv(ndev); | |
1639 | struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev); | |
1640 | ||
1641 | if (!nvdev) | |
1642 | return; | |
1643 | ||
1644 | __netvsc_get_ringparam(nvdev, ring); | |
1645 | } | |
1646 | ||
1647 | static int netvsc_set_ringparam(struct net_device *ndev, | |
1648 | struct ethtool_ringparam *ring) | |
1649 | { | |
1650 | struct net_device_context *ndevctx = netdev_priv(ndev); | |
1651 | struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev); | |
8b532797 | 1652 | struct netvsc_device_info device_info; |
1653 | struct ethtool_ringparam orig; | |
1654 | u32 new_tx, new_rx; | |
8b532797 | 1655 | int ret = 0; |
1656 | ||
1657 | if (!nvdev || nvdev->destroy) | |
1658 | return -ENODEV; | |
1659 | ||
1660 | memset(&orig, 0, sizeof(orig)); | |
1661 | __netvsc_get_ringparam(nvdev, &orig); | |
1662 | ||
1663 | new_tx = clamp_t(u32, ring->tx_pending, | |
1664 | NETVSC_MIN_TX_SECTIONS, orig.tx_max_pending); | |
1665 | new_rx = clamp_t(u32, ring->rx_pending, | |
1666 | NETVSC_MIN_RX_SECTIONS, orig.rx_max_pending); | |
1667 | ||
1668 | if (new_tx == orig.tx_pending && | |
1669 | new_rx == orig.rx_pending) | |
1670 | return 0; /* no change */ | |
1671 | ||
1672 | memset(&device_info, 0, sizeof(device_info)); | |
1673 | device_info.num_chn = nvdev->num_chn; | |
1674 | device_info.ring_size = ring_size; | |
1675 | device_info.send_sections = new_tx; | |
0ab09bef | 1676 | device_info.send_section_size = nvdev->send_section_size; |
8b532797 | 1677 | device_info.recv_sections = new_rx; |
0ab09bef | 1678 | device_info.recv_section_size = nvdev->recv_section_size; |
8b532797 | 1679 | |
cef03981 SH |
1680 | ret = netvsc_detach(ndev, nvdev); |
1681 | if (ret) | |
1682 | return ret; | |
8b532797 | 1683 | |
cef03981 SH |
1684 | ret = netvsc_attach(ndev, &device_info); |
1685 | if (ret) { | |
8b532797 | 1686 | device_info.send_sections = orig.tx_pending; |
1687 | device_info.recv_sections = orig.rx_pending; | |
8b532797 | 1688 | |
cef03981 SH |
1689 | if (netvsc_attach(ndev, &device_info)) |
1690 | netdev_err(ndev, "restoring ringparam failed"); | |
1691 | } | |
8b532797 | 1692 | |
1693 | return ret; | |
1694 | } | |
1695 | ||
f82f4ad7 SH |
1696 | static const struct ethtool_ops ethtool_ops = { |
1697 | .get_drvinfo = netvsc_get_drvinfo, | |
f82f4ad7 | 1698 | .get_link = ethtool_op_get_link, |
4323b47c SH |
1699 | .get_ethtool_stats = netvsc_get_ethtool_stats, |
1700 | .get_sset_count = netvsc_get_sset_count, | |
1701 | .get_strings = netvsc_get_strings, | |
59995370 | 1702 | .get_channels = netvsc_get_channels, |
b5960e6e | 1703 | .set_channels = netvsc_set_channels, |
76d13b56 | 1704 | .get_ts_info = ethtool_op_get_ts_info, |
b448f4e8 | 1705 | .get_rxnfc = netvsc_get_rxnfc, |
4823eb2f | 1706 | .set_rxnfc = netvsc_set_rxnfc, |
962f3fee | 1707 | .get_rxfh_key_size = netvsc_get_rxfh_key_size, |
1708 | .get_rxfh_indir_size = netvsc_rss_indir_size, | |
1709 | .get_rxfh = netvsc_get_rxfh, | |
1710 | .set_rxfh = netvsc_set_rxfh, | |
5e8456fd PR |
1711 | .get_link_ksettings = netvsc_get_link_ksettings, |
1712 | .set_link_ksettings = netvsc_set_link_ksettings, | |
8b532797 | 1713 | .get_ringparam = netvsc_get_ringparam, |
1714 | .set_ringparam = netvsc_set_ringparam, | |
f82f4ad7 SH |
1715 | }; |
1716 | ||
df2fff28 GKH |
1717 | static const struct net_device_ops device_ops = { |
1718 | .ndo_open = netvsc_open, | |
1719 | .ndo_stop = netvsc_close, | |
1720 | .ndo_start_xmit = netvsc_start_xmit, | |
18cc16d1 SH |
1721 | .ndo_change_rx_flags = netvsc_change_rx_flags, |
1722 | .ndo_set_rx_mode = netvsc_set_rx_mode, | |
4d447c9a | 1723 | .ndo_change_mtu = netvsc_change_mtu, |
b681b588 | 1724 | .ndo_validate_addr = eth_validate_addr, |
1ce09e89 | 1725 | .ndo_set_mac_address = netvsc_set_mac_addr, |
5b54dac8 | 1726 | .ndo_select_queue = netvsc_select_queue, |
7eafd9b4 | 1727 | .ndo_get_stats64 = netvsc_get_stats64, |
316158fe RW |
1728 | #ifdef CONFIG_NET_POLL_CONTROLLER |
1729 | .ndo_poll_controller = netvsc_poll_controller, | |
1730 | #endif | |
df2fff28 GKH |
1731 | }; |
1732 | ||
c996edcf | 1733 | /* |
27a70af3 VK |
1734 | * Handle link status changes. For RNDIS_STATUS_NETWORK_CHANGE emulate link |
1735 | * down/up sequence. In case of RNDIS_STATUS_MEDIA_CONNECT when carrier is | |
1736 | * present send GARP packet to network peers with netif_notify_peers(). | |
c996edcf | 1737 | */ |
891de74d | 1738 | static void netvsc_link_change(struct work_struct *w) |
c996edcf | 1739 | { |
0a1275ca VK |
1740 | struct net_device_context *ndev_ctx = |
1741 | container_of(w, struct net_device_context, dwork.work); | |
1742 | struct hv_device *device_obj = ndev_ctx->device_ctx; | |
1743 | struct net_device *net = hv_get_drvdata(device_obj); | |
2ddd5e5f | 1744 | struct netvsc_device *net_device; |
891de74d | 1745 | struct rndis_device *rdev; |
27a70af3 VK |
1746 | struct netvsc_reconfig *event = NULL; |
1747 | bool notify = false, reschedule = false; | |
1748 | unsigned long flags, next_reconfig, delay; | |
c996edcf | 1749 | |
9b4e946c | 1750 | /* if changes are happening, comeback later */ |
1751 | if (!rtnl_trylock()) { | |
1752 | schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT); | |
1753 | return; | |
1754 | } | |
1755 | ||
a0be450e | 1756 | net_device = rtnl_dereference(ndev_ctx->nvdev); |
1757 | if (!net_device) | |
1bdcec8a VK |
1758 | goto out_unlock; |
1759 | ||
891de74d | 1760 | rdev = net_device->extension; |
891de74d | 1761 | |
27a70af3 VK |
1762 | next_reconfig = ndev_ctx->last_reconfig + LINKCHANGE_INT; |
1763 | if (time_is_after_jiffies(next_reconfig)) { | |
1764 | /* link_watch only sends one notification with current state | |
1765 | * per second, avoid doing reconfig more frequently. Handle | |
1766 | * wrap around. | |
1767 | */ | |
1768 | delay = next_reconfig - jiffies; | |
1769 | delay = delay < LINKCHANGE_INT ? delay : LINKCHANGE_INT; | |
1770 | schedule_delayed_work(&ndev_ctx->dwork, delay); | |
1bdcec8a | 1771 | goto out_unlock; |
27a70af3 VK |
1772 | } |
1773 | ndev_ctx->last_reconfig = jiffies; | |
1774 | ||
1775 | spin_lock_irqsave(&ndev_ctx->lock, flags); | |
1776 | if (!list_empty(&ndev_ctx->reconfig_events)) { | |
1777 | event = list_first_entry(&ndev_ctx->reconfig_events, | |
1778 | struct netvsc_reconfig, list); | |
1779 | list_del(&event->list); | |
1780 | reschedule = !list_empty(&ndev_ctx->reconfig_events); | |
1781 | } | |
1782 | spin_unlock_irqrestore(&ndev_ctx->lock, flags); | |
1783 | ||
1784 | if (!event) | |
1bdcec8a | 1785 | goto out_unlock; |
27a70af3 VK |
1786 | |
1787 | switch (event->event) { | |
1788 | /* Only the following events are possible due to the check in | |
1789 | * netvsc_linkstatus_callback() | |
1790 | */ | |
1791 | case RNDIS_STATUS_MEDIA_CONNECT: | |
1792 | if (rdev->link_state) { | |
1793 | rdev->link_state = false; | |
0c195567 | 1794 | netif_carrier_on(net); |
a1979526 | 1795 | netvsc_tx_enable(net_device, net); |
27a70af3 VK |
1796 | } else { |
1797 | notify = true; | |
1798 | } | |
1799 | kfree(event); | |
1800 | break; | |
1801 | case RNDIS_STATUS_MEDIA_DISCONNECT: | |
1802 | if (!rdev->link_state) { | |
1803 | rdev->link_state = true; | |
1804 | netif_carrier_off(net); | |
a1979526 | 1805 | netvsc_tx_disable(net_device, net); |
27a70af3 VK |
1806 | } |
1807 | kfree(event); | |
1808 | break; | |
1809 | case RNDIS_STATUS_NETWORK_CHANGE: | |
1810 | /* Only makes sense if carrier is present */ | |
1811 | if (!rdev->link_state) { | |
1812 | rdev->link_state = true; | |
1813 | netif_carrier_off(net); | |
a1979526 | 1814 | netvsc_tx_disable(net_device, net); |
27a70af3 VK |
1815 | event->event = RNDIS_STATUS_MEDIA_CONNECT; |
1816 | spin_lock_irqsave(&ndev_ctx->lock, flags); | |
15cfd407 | 1817 | list_add(&event->list, &ndev_ctx->reconfig_events); |
27a70af3 VK |
1818 | spin_unlock_irqrestore(&ndev_ctx->lock, flags); |
1819 | reschedule = true; | |
3a494e71 | 1820 | } |
27a70af3 | 1821 | break; |
891de74d HZ |
1822 | } |
1823 | ||
1824 | rtnl_unlock(); | |
1825 | ||
1826 | if (notify) | |
1827 | netdev_notify_peers(net); | |
27a70af3 VK |
1828 | |
1829 | /* link_watch only sends one notification with current state per | |
1830 | * second, handle next reconfig event in 2 seconds. | |
1831 | */ | |
1832 | if (reschedule) | |
1833 | schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT); | |
1bdcec8a VK |
1834 | |
1835 | return; | |
1836 | ||
1837 | out_unlock: | |
1838 | rtnl_unlock(); | |
c996edcf HZ |
1839 | } |
1840 | ||
e8ff40d4 | 1841 | static struct net_device *get_netvsc_bymac(const u8 *mac) |
84bf9cef | 1842 | { |
ad375c00 | 1843 | struct net_device_context *ndev_ctx; |
84bf9cef | 1844 | |
ad375c00 SH |
1845 | list_for_each_entry(ndev_ctx, &netvsc_dev_list, list) { |
1846 | struct net_device *dev = hv_get_drvdata(ndev_ctx->device_ctx); | |
e8ff40d4 SH |
1847 | |
1848 | if (ether_addr_equal(mac, dev->perm_addr)) | |
1849 | return dev; | |
1850 | } | |
1851 | ||
1852 | return NULL; | |
1853 | } | |
1854 | ||
f207c10d | 1855 | static struct net_device *get_netvsc_byref(struct net_device *vf_netdev) |
e8ff40d4 | 1856 | { |
ad375c00 | 1857 | struct net_device_context *net_device_ctx; |
e8ff40d4 SH |
1858 | struct net_device *dev; |
1859 | ||
ad375c00 SH |
1860 | dev = netdev_master_upper_dev_get(vf_netdev); |
1861 | if (!dev || dev->netdev_ops != &device_ops) | |
1862 | return NULL; /* not a netvsc device */ | |
e8ff40d4 | 1863 | |
ad375c00 SH |
1864 | net_device_ctx = netdev_priv(dev); |
1865 | if (!rtnl_dereference(net_device_ctx->nvdev)) | |
1866 | return NULL; /* device is removed */ | |
e8ff40d4 | 1867 | |
ad375c00 | 1868 | return dev; |
84bf9cef KS |
1869 | } |
1870 | ||
0c195567 | 1871 | /* Called when VF is injecting data into network stack. |
1872 | * Change the associated network device from VF to netvsc. | |
1873 | * note: already called with rcu_read_lock | |
1874 | */ | |
1875 | static rx_handler_result_t netvsc_vf_handle_frame(struct sk_buff **pskb) | |
1876 | { | |
1877 | struct sk_buff *skb = *pskb; | |
1878 | struct net_device *ndev = rcu_dereference(skb->dev->rx_handler_data); | |
1879 | struct net_device_context *ndev_ctx = netdev_priv(ndev); | |
1880 | struct netvsc_vf_pcpu_stats *pcpu_stats | |
1881 | = this_cpu_ptr(ndev_ctx->vf_stats); | |
1882 | ||
1883 | skb->dev = ndev; | |
1884 | ||
1885 | u64_stats_update_begin(&pcpu_stats->syncp); | |
1886 | pcpu_stats->rx_packets++; | |
1887 | pcpu_stats->rx_bytes += skb->len; | |
1888 | u64_stats_update_end(&pcpu_stats->syncp); | |
1889 | ||
1890 | return RX_HANDLER_ANOTHER; | |
1891 | } | |
1892 | ||
1893 | static int netvsc_vf_join(struct net_device *vf_netdev, | |
1894 | struct net_device *ndev) | |
1895 | { | |
1896 | struct net_device_context *ndev_ctx = netdev_priv(ndev); | |
1897 | int ret; | |
1898 | ||
1899 | ret = netdev_rx_handler_register(vf_netdev, | |
1900 | netvsc_vf_handle_frame, ndev); | |
1901 | if (ret != 0) { | |
1902 | netdev_err(vf_netdev, | |
1903 | "can not register netvsc VF receive handler (err = %d)\n", | |
1904 | ret); | |
1905 | goto rx_handler_failed; | |
1906 | } | |
1907 | ||
1c688ab2 SH |
1908 | ret = netdev_master_upper_dev_link(vf_netdev, ndev, |
1909 | NULL, NULL, NULL); | |
0c195567 | 1910 | if (ret != 0) { |
1911 | netdev_err(vf_netdev, | |
1912 | "can not set master device %s (err = %d)\n", | |
1913 | ndev->name, ret); | |
1914 | goto upper_link_failed; | |
1915 | } | |
1916 | ||
1917 | /* set slave flag before open to prevent IPv6 addrconf */ | |
1918 | vf_netdev->flags |= IFF_SLAVE; | |
1919 | ||
6123c668 | 1920 | schedule_delayed_work(&ndev_ctx->vf_takeover, VF_TAKEOVER_INT); |
1921 | ||
1922 | call_netdevice_notifiers(NETDEV_JOIN, vf_netdev); | |
0c195567 | 1923 | |
1924 | netdev_info(vf_netdev, "joined to %s\n", ndev->name); | |
1925 | return 0; | |
1926 | ||
1927 | upper_link_failed: | |
1928 | netdev_rx_handler_unregister(vf_netdev); | |
1929 | rx_handler_failed: | |
1930 | return ret; | |
1931 | } | |
1932 | ||
1933 | static void __netvsc_vf_setup(struct net_device *ndev, | |
1934 | struct net_device *vf_netdev) | |
1935 | { | |
1936 | int ret; | |
1937 | ||
0c195567 | 1938 | /* Align MTU of VF with master */ |
1939 | ret = dev_set_mtu(vf_netdev, ndev->mtu); | |
1940 | if (ret) | |
1941 | netdev_warn(vf_netdev, | |
1942 | "unable to change mtu to %u\n", ndev->mtu); | |
1943 | ||
18cc16d1 SH |
1944 | /* set multicast etc flags on VF */ |
1945 | dev_change_flags(vf_netdev, ndev->flags | IFF_SLAVE); | |
a6e213b1 SH |
1946 | |
1947 | /* sync address list from ndev to VF */ | |
1948 | netif_addr_lock_bh(ndev); | |
18cc16d1 SH |
1949 | dev_uc_sync(vf_netdev, ndev); |
1950 | dev_mc_sync(vf_netdev, ndev); | |
a6e213b1 | 1951 | netif_addr_unlock_bh(ndev); |
18cc16d1 | 1952 | |
0c195567 | 1953 | if (netif_running(ndev)) { |
1954 | ret = dev_open(vf_netdev); | |
1955 | if (ret) | |
1956 | netdev_warn(vf_netdev, | |
1957 | "unable to open: %d\n", ret); | |
1958 | } | |
1959 | } | |
1960 | ||
1961 | /* Setup VF as slave of the synthetic device. | |
1962 | * Runs in workqueue to avoid recursion in netlink callbacks. | |
1963 | */ | |
1964 | static void netvsc_vf_setup(struct work_struct *w) | |
1965 | { | |
1966 | struct net_device_context *ndev_ctx | |
6123c668 | 1967 | = container_of(w, struct net_device_context, vf_takeover.work); |
0c195567 | 1968 | struct net_device *ndev = hv_get_drvdata(ndev_ctx->device_ctx); |
1969 | struct net_device *vf_netdev; | |
1970 | ||
fb84af8a | 1971 | if (!rtnl_trylock()) { |
6123c668 | 1972 | schedule_delayed_work(&ndev_ctx->vf_takeover, 0); |
fb84af8a | 1973 | return; |
1974 | } | |
1975 | ||
0c195567 | 1976 | vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev); |
1977 | if (vf_netdev) | |
1978 | __netvsc_vf_setup(ndev, vf_netdev); | |
1979 | ||
1980 | rtnl_unlock(); | |
1981 | } | |
1982 | ||
84bf9cef KS |
1983 | static int netvsc_register_vf(struct net_device *vf_netdev) |
1984 | { | |
0a1275ca VK |
1985 | struct net_device *ndev; |
1986 | struct net_device_context *net_device_ctx; | |
4d0ce115 | 1987 | struct device *pdev = vf_netdev->dev.parent; |
84bf9cef | 1988 | struct netvsc_device *netvsc_dev; |
84bf9cef | 1989 | |
e8ff40d4 SH |
1990 | if (vf_netdev->addr_len != ETH_ALEN) |
1991 | return NOTIFY_DONE; | |
1992 | ||
4d0ce115 SH |
1993 | if (!pdev || !dev_is_pci(pdev) || dev_is_pf(pdev)) |
1994 | return NOTIFY_DONE; | |
1995 | ||
84bf9cef KS |
1996 | /* |
1997 | * We will use the MAC address to locate the synthetic interface to | |
1998 | * associate with the VF interface. If we don't find a matching | |
1999 | * synthetic interface, move on. | |
2000 | */ | |
e8ff40d4 | 2001 | ndev = get_netvsc_bymac(vf_netdev->perm_addr); |
0a1275ca VK |
2002 | if (!ndev) |
2003 | return NOTIFY_DONE; | |
2004 | ||
2005 | net_device_ctx = netdev_priv(ndev); | |
545a8e79 | 2006 | netvsc_dev = rtnl_dereference(net_device_ctx->nvdev); |
f207c10d | 2007 | if (!netvsc_dev || rtnl_dereference(net_device_ctx->vf_netdev)) |
84bf9cef KS |
2008 | return NOTIFY_DONE; |
2009 | ||
0c195567 | 2010 | if (netvsc_vf_join(vf_netdev, ndev) != 0) |
2011 | return NOTIFY_DONE; | |
2012 | ||
0a1275ca | 2013 | netdev_info(ndev, "VF registering: %s\n", vf_netdev->name); |
0c195567 | 2014 | |
07d0f000 | 2015 | dev_hold(vf_netdev); |
f207c10d | 2016 | rcu_assign_pointer(net_device_ctx->vf_netdev, vf_netdev); |
84bf9cef KS |
2017 | return NOTIFY_OK; |
2018 | } | |
2019 | ||
9a0c48df SH |
2020 | /* VF up/down change detected, schedule to change data path */ |
2021 | static int netvsc_vf_changed(struct net_device *vf_netdev) | |
84bf9cef | 2022 | { |
7b83f520 | 2023 | struct net_device_context *net_device_ctx; |
84bf9cef | 2024 | struct netvsc_device *netvsc_dev; |
7b83f520 | 2025 | struct net_device *ndev; |
9a0c48df | 2026 | bool vf_is_up = netif_running(vf_netdev); |
fb84af8a | 2027 | |
7b83f520 | 2028 | ndev = get_netvsc_byref(vf_netdev); |
2029 | if (!ndev) | |
2030 | return NOTIFY_DONE; | |
0c195567 | 2031 | |
7b83f520 | 2032 | net_device_ctx = netdev_priv(ndev); |
2033 | netvsc_dev = rtnl_dereference(net_device_ctx->nvdev); | |
0c195567 | 2034 | if (!netvsc_dev) |
7b83f520 | 2035 | return NOTIFY_DONE; |
84bf9cef | 2036 | |
9a0c48df SH |
2037 | netvsc_switch_datapath(ndev, vf_is_up); |
2038 | netdev_info(ndev, "Data path switched %s VF: %s\n", | |
2039 | vf_is_up ? "to" : "from", vf_netdev->name); | |
84bf9cef KS |
2040 | |
2041 | return NOTIFY_OK; | |
2042 | } | |
2043 | ||
84bf9cef KS |
2044 | static int netvsc_unregister_vf(struct net_device *vf_netdev) |
2045 | { | |
0a1275ca | 2046 | struct net_device *ndev; |
0a1275ca | 2047 | struct net_device_context *net_device_ctx; |
84bf9cef | 2048 | |
e8ff40d4 | 2049 | ndev = get_netvsc_byref(vf_netdev); |
0a1275ca VK |
2050 | if (!ndev) |
2051 | return NOTIFY_DONE; | |
2052 | ||
2053 | net_device_ctx = netdev_priv(ndev); | |
6123c668 | 2054 | cancel_delayed_work_sync(&net_device_ctx->vf_takeover); |
e8ff40d4 | 2055 | |
0a1275ca | 2056 | netdev_info(ndev, "VF unregistering: %s\n", vf_netdev->name); |
f207c10d | 2057 | |
ec158f77 | 2058 | netdev_rx_handler_unregister(vf_netdev); |
0c195567 | 2059 | netdev_upper_dev_unlink(vf_netdev, ndev); |
f207c10d | 2060 | RCU_INIT_POINTER(net_device_ctx->vf_netdev, NULL); |
07d0f000 | 2061 | dev_put(vf_netdev); |
ec158f77 | 2062 | |
84bf9cef KS |
2063 | return NOTIFY_OK; |
2064 | } | |
2065 | ||
84946899 S |
2066 | static int netvsc_probe(struct hv_device *dev, |
2067 | const struct hv_vmbus_device_id *dev_id) | |
df2fff28 | 2068 | { |
df2fff28 GKH |
2069 | struct net_device *net = NULL; |
2070 | struct net_device_context *net_device_ctx; | |
2071 | struct netvsc_device_info device_info; | |
5b54dac8 | 2072 | struct netvsc_device *nvdev; |
0c195567 | 2073 | int ret = -ENOMEM; |
df2fff28 | 2074 | |
5b54dac8 | 2075 | net = alloc_etherdev_mq(sizeof(struct net_device_context), |
2b01888d | 2076 | VRSS_CHANNEL_MAX); |
df2fff28 | 2077 | if (!net) |
0c195567 | 2078 | goto no_net; |
df2fff28 | 2079 | |
1b07da51 HZ |
2080 | netif_carrier_off(net); |
2081 | ||
b37879e6 HZ |
2082 | netvsc_init_settings(net); |
2083 | ||
df2fff28 | 2084 | net_device_ctx = netdev_priv(net); |
9efd21e1 | 2085 | net_device_ctx->device_ctx = dev; |
3f300ff4 SX |
2086 | net_device_ctx->msg_enable = netif_msg_init(debug, default_msg); |
2087 | if (netif_msg_probe(net_device_ctx)) | |
2088 | netdev_dbg(net, "netvsc msg_enable: %d\n", | |
2089 | net_device_ctx->msg_enable); | |
2090 | ||
2ddd5e5f | 2091 | hv_set_drvdata(dev, net); |
f580aec4 | 2092 | |
891de74d | 2093 | INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change); |
df2fff28 | 2094 | |
27a70af3 VK |
2095 | spin_lock_init(&net_device_ctx->lock); |
2096 | INIT_LIST_HEAD(&net_device_ctx->reconfig_events); | |
6123c668 | 2097 | INIT_DELAYED_WORK(&net_device_ctx->vf_takeover, netvsc_vf_setup); |
0c195567 | 2098 | |
2099 | net_device_ctx->vf_stats | |
2100 | = netdev_alloc_pcpu_stats(struct netvsc_vf_pcpu_stats); | |
2101 | if (!net_device_ctx->vf_stats) | |
2102 | goto no_stats; | |
27a70af3 | 2103 | |
df2fff28 | 2104 | net->netdev_ops = &device_ops; |
7ad24ea4 | 2105 | net->ethtool_ops = ðtool_ops; |
9efd21e1 | 2106 | SET_NETDEV_DEV(net, &dev->device); |
df2fff28 | 2107 | |
14a03cf8 VK |
2108 | /* We always need headroom for rndis header */ |
2109 | net->needed_headroom = RNDIS_AND_PPI_SIZE; | |
2110 | ||
6450f8f2 HZ |
2111 | /* Initialize the number of queues to be 1, we may change it if more |
2112 | * channels are offered later. | |
2113 | */ | |
2114 | netif_set_real_num_tx_queues(net, 1); | |
2115 | netif_set_real_num_rx_queues(net, 1); | |
2116 | ||
692e084e | 2117 | /* Notify the netvsc driver of the new device */ |
8ebdcc52 | 2118 | memset(&device_info, 0, sizeof(device_info)); |
692e084e | 2119 | device_info.ring_size = ring_size; |
3071ada4 | 2120 | device_info.num_chn = VRSS_CHANNEL_DEFAULT; |
8b532797 | 2121 | device_info.send_sections = NETVSC_DEFAULT_TX; |
0ab09bef | 2122 | device_info.send_section_size = NETVSC_SEND_SECTION_SIZE; |
8b532797 | 2123 | device_info.recv_sections = NETVSC_DEFAULT_RX; |
0ab09bef | 2124 | device_info.recv_section_size = NETVSC_RECV_SECTION_SIZE; |
9749fed5 | 2125 | |
2126 | nvdev = rndis_filter_device_add(dev, &device_info); | |
2127 | if (IS_ERR(nvdev)) { | |
2128 | ret = PTR_ERR(nvdev); | |
692e084e | 2129 | netdev_err(net, "unable to add netvsc device (ret %d)\n", ret); |
0c195567 | 2130 | goto rndis_failed; |
df2fff28 | 2131 | } |
0c195567 | 2132 | |
692e084e HZ |
2133 | memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN); |
2134 | ||
e70b3343 DC |
2135 | /* We must get rtnl lock before scheduling nvdev->subchan_work, |
2136 | * otherwise netvsc_subchan_work() can get rtnl lock first and wait | |
2137 | * all subchannels to show up, but that may not happen because | |
2138 | * netvsc_probe() can't get rtnl lock and as a result vmbus_onoffer() | |
2139 | * -> ... -> device_add() -> ... -> __device_attach() can't get | |
2140 | * the device lock, so all the subchannels can't be processed -- | |
2141 | * finally netvsc_subchan_work() hangs for ever. | |
2142 | */ | |
2143 | rtnl_lock(); | |
2144 | ||
2950481a SH |
2145 | if (nvdev->num_chn > 1) |
2146 | schedule_work(&nvdev->subchan_work); | |
2147 | ||
aefd80e8 | 2148 | /* hw_features computed in rndis_netdev_set_hwcaps() */ |
23312a3b | 2149 | net->features = net->hw_features | |
2150 | NETIF_F_HIGHDMA | NETIF_F_SG | | |
2151 | NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX; | |
2152 | net->vlan_features = net->features; | |
2153 | ||
9749fed5 | 2154 | netdev_lockdep_set_classes(net); |
2155 | ||
d0c2c997 JW |
2156 | /* MTU range: 68 - 1500 or 65521 */ |
2157 | net->min_mtu = NETVSC_MTU_MIN; | |
2158 | if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2) | |
2159 | net->max_mtu = NETVSC_MTU - ETH_HLEN; | |
2160 | else | |
2161 | net->max_mtu = ETH_DATA_LEN; | |
2162 | ||
ad375c00 | 2163 | ret = register_netdevice(net); |
a68f9614 HZ |
2164 | if (ret != 0) { |
2165 | pr_err("Unable to register netdev.\n"); | |
0c195567 | 2166 | goto register_failed; |
a68f9614 HZ |
2167 | } |
2168 | ||
ad375c00 SH |
2169 | list_add(&net_device_ctx->list, &netvsc_dev_list); |
2170 | rtnl_unlock(); | |
2171 | return 0; | |
0c195567 | 2172 | |
2173 | register_failed: | |
ad375c00 | 2174 | rtnl_unlock(); |
0c195567 | 2175 | rndis_filter_device_remove(dev, nvdev); |
2176 | rndis_failed: | |
2177 | free_percpu(net_device_ctx->vf_stats); | |
2178 | no_stats: | |
2179 | hv_set_drvdata(dev, NULL); | |
2180 | free_netdev(net); | |
2181 | no_net: | |
2182 | return ret; | |
df2fff28 GKH |
2183 | } |
2184 | ||
415b023a | 2185 | static int netvsc_remove(struct hv_device *dev) |
df2fff28 | 2186 | { |
122a5f64 | 2187 | struct net_device_context *ndev_ctx; |
cef03981 SH |
2188 | struct net_device *vf_netdev, *net; |
2189 | struct netvsc_device *nvdev; | |
2ddd5e5f | 2190 | |
3d541ac5 | 2191 | net = hv_get_drvdata(dev); |
df2fff28 | 2192 | if (net == NULL) { |
415b023a | 2193 | dev_err(&dev->device, "No net device to remove\n"); |
df2fff28 GKH |
2194 | return 0; |
2195 | } | |
2196 | ||
122a5f64 | 2197 | ndev_ctx = netdev_priv(net); |
3d541ac5 | 2198 | |
122a5f64 HZ |
2199 | cancel_delayed_work_sync(&ndev_ctx->dwork); |
2200 | ||
e01fcdde SH |
2201 | rtnl_lock(); |
2202 | nvdev = rtnl_dereference(ndev_ctx->nvdev); | |
2203 | if (nvdev) | |
cef03981 SH |
2204 | cancel_work_sync(&nvdev->subchan_work); |
2205 | ||
df2fff28 GKH |
2206 | /* |
2207 | * Call to the vsc driver to let it know that the device is being | |
a0be450e | 2208 | * removed. Also blocks mtu and channel changes. |
df2fff28 | 2209 | */ |
ec158f77 SH |
2210 | vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev); |
2211 | if (vf_netdev) | |
2212 | netvsc_unregister_vf(vf_netdev); | |
2213 | ||
cef03981 SH |
2214 | if (nvdev) |
2215 | rndis_filter_device_remove(dev, nvdev); | |
2216 | ||
8195b139 | 2217 | unregister_netdevice(net); |
ad375c00 | 2218 | list_del(&ndev_ctx->list); |
8195b139 | 2219 | |
a0be450e | 2220 | rtnl_unlock(); |
2221 | ||
3d541ac5 VK |
2222 | hv_set_drvdata(dev, NULL); |
2223 | ||
0c195567 | 2224 | free_percpu(ndev_ctx->vf_stats); |
6c80f3fc | 2225 | free_netdev(net); |
df06bcff | 2226 | return 0; |
df2fff28 GKH |
2227 | } |
2228 | ||
345c4cc3 | 2229 | static const struct hv_vmbus_device_id id_table[] = { |
c45cf2d4 | 2230 | /* Network guid */ |
8f505944 | 2231 | { HV_NIC_GUID, }, |
c45cf2d4 | 2232 | { }, |
345c4cc3 S |
2233 | }; |
2234 | ||
2235 | MODULE_DEVICE_TABLE(vmbus, id_table); | |
2236 | ||
f1542a66 | 2237 | /* The one and only one */ |
fde0ef9b | 2238 | static struct hv_driver netvsc_drv = { |
d31b20fc | 2239 | .name = KBUILD_MODNAME, |
345c4cc3 | 2240 | .id_table = id_table, |
fde0ef9b S |
2241 | .probe = netvsc_probe, |
2242 | .remove = netvsc_remove, | |
d4890970 | 2243 | }; |
f1542a66 | 2244 | |
84bf9cef KS |
2245 | /* |
2246 | * On Hyper-V, every VF interface is matched with a corresponding | |
2247 | * synthetic interface. The synthetic interface is presented first | |
2248 | * to the guest. When the corresponding VF instance is registered, | |
2249 | * we will take care of switching the data path. | |
2250 | */ | |
2251 | static int netvsc_netdev_event(struct notifier_block *this, | |
2252 | unsigned long event, void *ptr) | |
2253 | { | |
2254 | struct net_device *event_dev = netdev_notifier_info_to_dev(ptr); | |
2255 | ||
ee837a13 SH |
2256 | /* Skip our own events */ |
2257 | if (event_dev->netdev_ops == &device_ops) | |
2258 | return NOTIFY_DONE; | |
2259 | ||
2260 | /* Avoid non-Ethernet type devices */ | |
2261 | if (event_dev->type != ARPHRD_ETHER) | |
2262 | return NOTIFY_DONE; | |
2263 | ||
0dbff144 | 2264 | /* Avoid Vlan dev with same MAC registering as VF */ |
d0d7b10b | 2265 | if (is_vlan_dev(event_dev)) |
0dbff144 VK |
2266 | return NOTIFY_DONE; |
2267 | ||
2268 | /* Avoid Bonding master dev with same MAC registering as VF */ | |
ee837a13 SH |
2269 | if ((event_dev->priv_flags & IFF_BONDING) && |
2270 | (event_dev->flags & IFF_MASTER)) | |
cb2911fe HZ |
2271 | return NOTIFY_DONE; |
2272 | ||
84bf9cef KS |
2273 | switch (event) { |
2274 | case NETDEV_REGISTER: | |
2275 | return netvsc_register_vf(event_dev); | |
2276 | case NETDEV_UNREGISTER: | |
2277 | return netvsc_unregister_vf(event_dev); | |
2278 | case NETDEV_UP: | |
84bf9cef | 2279 | case NETDEV_DOWN: |
9a0c48df | 2280 | return netvsc_vf_changed(event_dev); |
84bf9cef KS |
2281 | default: |
2282 | return NOTIFY_DONE; | |
2283 | } | |
2284 | } | |
2285 | ||
2286 | static struct notifier_block netvsc_netdev_notifier = { | |
2287 | .notifier_call = netvsc_netdev_event, | |
2288 | }; | |
2289 | ||
a9869c94 | 2290 | static void __exit netvsc_drv_exit(void) |
fceaf24a | 2291 | { |
84bf9cef | 2292 | unregister_netdevice_notifier(&netvsc_netdev_notifier); |
768fa219 | 2293 | vmbus_driver_unregister(&netvsc_drv); |
fceaf24a HJ |
2294 | } |
2295 | ||
1fde28cf | 2296 | static int __init netvsc_drv_init(void) |
df2fff28 | 2297 | { |
84bf9cef KS |
2298 | int ret; |
2299 | ||
fa85a6c2 HZ |
2300 | if (ring_size < RING_SIZE_MIN) { |
2301 | ring_size = RING_SIZE_MIN; | |
2302 | pr_info("Increased ring_size to %d (min allowed)\n", | |
2303 | ring_size); | |
2304 | } | |
84bf9cef KS |
2305 | ret = vmbus_driver_register(&netvsc_drv); |
2306 | ||
2307 | if (ret) | |
2308 | return ret; | |
2309 | ||
2310 | register_netdevice_notifier(&netvsc_netdev_notifier); | |
2311 | return 0; | |
df2fff28 GKH |
2312 | } |
2313 | ||
26c14cc1 | 2314 | MODULE_LICENSE("GPL"); |
7880fc54 | 2315 | MODULE_DESCRIPTION("Microsoft Hyper-V network driver"); |
fceaf24a | 2316 | |
1fde28cf | 2317 | module_init(netvsc_drv_init); |
a9869c94 | 2318 | module_exit(netvsc_drv_exit); |