]>
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> | |
32 | #include <linux/skbuff.h> | |
c802db11 | 33 | #include <linux/if_vlan.h> |
fceaf24a | 34 | #include <linux/in.h> |
5a0e3ad6 | 35 | #include <linux/slab.h> |
27f5aa92 | 36 | #include <linux/rtnetlink.h> |
37 | ||
fceaf24a HJ |
38 | #include <net/arp.h> |
39 | #include <net/route.h> | |
40 | #include <net/sock.h> | |
41 | #include <net/pkt_sched.h> | |
8eb1b3c3 MK |
42 | #include <net/checksum.h> |
43 | #include <net/ip6_checksum.h> | |
3f335ea2 | 44 | |
5ca7252a | 45 | #include "hyperv_net.h" |
fceaf24a | 46 | |
fa85a6c2 | 47 | #define RING_SIZE_MIN 64 |
27a70af3 | 48 | #define LINKCHANGE_INT (2 * HZ) |
a50af86d | 49 | |
99c8da0f | 50 | static int ring_size = 128; |
450d7a4b SH |
51 | module_param(ring_size, int, S_IRUGO); |
52 | MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)"); | |
fceaf24a | 53 | |
3f300ff4 SX |
54 | static 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 | ||
59 | static int debug = -1; | |
60 | module_param(debug, int, S_IRUGO); | |
61 | MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)"); | |
62 | ||
4e9bfefa | 63 | static void netvsc_set_multicast_list(struct net_device *net) |
fceaf24a | 64 | { |
792df872 | 65 | struct net_device_context *net_device_ctx = netdev_priv(net); |
4f19c0d8 | 66 | struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev); |
d426b2e3 | 67 | |
4f19c0d8 | 68 | rndis_filter_update(nvdev); |
fceaf24a HJ |
69 | } |
70 | ||
fceaf24a HJ |
71 | static int netvsc_open(struct net_device *net) |
72 | { | |
53fa1a6f | 73 | struct net_device_context *ndev_ctx = netdev_priv(net); |
79e8cbe7 | 74 | struct netvsc_device *nvdev = rtnl_dereference(ndev_ctx->nvdev); |
891de74d | 75 | struct rndis_device *rdev; |
02fafbc6 | 76 | int ret = 0; |
fceaf24a | 77 | |
891de74d HZ |
78 | netif_carrier_off(net); |
79 | ||
d515d0ff | 80 | /* Open up the device */ |
2f5fa6c8 | 81 | ret = rndis_filter_open(nvdev); |
d515d0ff HZ |
82 | if (ret != 0) { |
83 | netdev_err(net, "unable to open device (ret %d).\n", ret); | |
84 | return ret; | |
fceaf24a HJ |
85 | } |
86 | ||
2de8530b | 87 | netif_tx_wake_all_queues(net); |
d515d0ff | 88 | |
891de74d | 89 | rdev = nvdev->extension; |
53fa1a6f | 90 | if (!rdev->link_state && !ndev_ctx->datapath) |
891de74d HZ |
91 | netif_carrier_on(net); |
92 | ||
fceaf24a HJ |
93 | return ret; |
94 | } | |
95 | ||
fceaf24a HJ |
96 | static int netvsc_close(struct net_device *net) |
97 | { | |
fceaf24a | 98 | struct net_device_context *net_device_ctx = netdev_priv(net); |
545a8e79 | 99 | struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev); |
02fafbc6 | 100 | int ret; |
40975962 | 101 | u32 aread, i, msec = 10, retry = 0, retry_max = 20; |
2de8530b | 102 | struct vmbus_channel *chn; |
fceaf24a | 103 | |
0a282538 | 104 | netif_tx_disable(net); |
fceaf24a | 105 | |
2f5fa6c8 | 106 | ret = rndis_filter_close(nvdev); |
2de8530b | 107 | if (ret != 0) { |
eb335bc4 | 108 | netdev_err(net, "unable to close device (ret %d).\n", ret); |
2de8530b HZ |
109 | 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++) { | |
b8b835a8 | 116 | chn = nvdev->chan_table[i].channel; |
2de8530b HZ |
117 | if (!chn) |
118 | continue; | |
119 | ||
40975962 | 120 | aread = hv_get_bytes_to_read(&chn->inbound); |
2de8530b HZ |
121 | if (aread) |
122 | break; | |
123 | ||
40975962 | 124 | aread = hv_get_bytes_to_read(&chn->outbound); |
2de8530b HZ |
125 | 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 | } | |
fceaf24a | 143 | |
fceaf24a HJ |
144 | return ret; |
145 | } | |
146 | ||
8a00251a KS |
147 | static 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 | ||
f72860af HZ |
168 | /* 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 | */ | |
171 | static 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 | ||
8db91f6a HZ |
198 | static 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 | ||
f72860af | 205 | q_idx = ndc->tx_send_table[netvsc_get_hash(skb, sk) & |
8db91f6a HZ |
206 | (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 | ||
d8e18ee0 | 216 | /* |
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 | */ | |
5b54dac8 HZ |
227 | static u16 netvsc_select_queue(struct net_device *ndev, struct sk_buff *skb, |
228 | void *accel_priv, select_queue_fallback_t fallback) | |
229 | { | |
7ce10124 | 230 | unsigned int num_tx_queues = ndev->real_num_tx_queues; |
8db91f6a HZ |
231 | int q_idx = sk_tx_queue_get(skb->sk); |
232 | ||
233 | 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); | |
d8e18ee0 | 241 | } |
5b54dac8 | 242 | |
8db91f6a HZ |
243 | while (unlikely(q_idx >= num_tx_queues)) |
244 | q_idx -= num_tx_queues; | |
245 | ||
5b54dac8 HZ |
246 | return q_idx; |
247 | } | |
248 | ||
54a7357f KS |
249 | static 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 | ||
8a00251a | 283 | static u32 init_page_array(void *hdr, u32 len, struct sk_buff *skb, |
a9f2e2d6 | 284 | struct hv_netvsc_packet *packet, |
02b6de01 | 285 | struct hv_page_buffer *pb) |
54a7357f KS |
286 | { |
287 | u32 slots_used = 0; | |
288 | char *data = skb->data; | |
289 | int frags = skb_shinfo(skb)->nr_frags; | |
290 | int i; | |
291 | ||
292 | /* The packet is laid out thus: | |
aa0a34be | 293 | * 1. hdr: RNDIS header and PPI |
54a7357f KS |
294 | * 2. skb linear data |
295 | * 3. skb fragment data | |
296 | */ | |
297 | if (hdr != NULL) | |
298 | slots_used += fill_pg_buf(virt_to_page(hdr), | |
299 | offset_in_page(hdr), | |
300 | len, &pb[slots_used]); | |
301 | ||
aa0a34be HZ |
302 | packet->rmsg_size = len; |
303 | packet->rmsg_pgcnt = slots_used; | |
304 | ||
54a7357f KS |
305 | slots_used += fill_pg_buf(virt_to_page(data), |
306 | offset_in_page(data), | |
307 | skb_headlen(skb), &pb[slots_used]); | |
308 | ||
309 | for (i = 0; i < frags; i++) { | |
310 | skb_frag_t *frag = skb_shinfo(skb)->frags + i; | |
311 | ||
312 | slots_used += fill_pg_buf(skb_frag_page(frag), | |
313 | frag->page_offset, | |
314 | skb_frag_size(frag), &pb[slots_used]); | |
315 | } | |
8a00251a | 316 | return slots_used; |
54a7357f KS |
317 | } |
318 | ||
80d887db | 319 | static int count_skb_frag_slots(struct sk_buff *skb) |
320 | { | |
321 | int i, frags = skb_shinfo(skb)->nr_frags; | |
322 | int pages = 0; | |
323 | ||
324 | for (i = 0; i < frags; i++) { | |
325 | skb_frag_t *frag = skb_shinfo(skb)->frags + i; | |
326 | unsigned long size = skb_frag_size(frag); | |
327 | unsigned long offset = frag->page_offset; | |
328 | ||
329 | /* Skip unused frames from start of page */ | |
330 | offset &= ~PAGE_MASK; | |
331 | pages += PFN_UP(offset + size); | |
332 | } | |
333 | return pages; | |
334 | } | |
335 | ||
336 | static int netvsc_get_slots(struct sk_buff *skb) | |
54a7357f | 337 | { |
80d887db | 338 | char *data = skb->data; |
339 | unsigned int offset = offset_in_page(data); | |
340 | unsigned int len = skb_headlen(skb); | |
341 | int slots; | |
342 | int frag_slots; | |
343 | ||
344 | slots = DIV_ROUND_UP(offset + len, PAGE_SIZE); | |
345 | frag_slots = count_skb_frag_slots(skb); | |
346 | return slots + frag_slots; | |
54a7357f KS |
347 | } |
348 | ||
23312a3b | 349 | static u32 net_checksum_info(struct sk_buff *skb) |
08cd04bf | 350 | { |
23312a3b | 351 | if (skb->protocol == htons(ETH_P_IP)) { |
352 | struct iphdr *ip = ip_hdr(skb); | |
08cd04bf | 353 | |
23312a3b | 354 | if (ip->protocol == IPPROTO_TCP) |
355 | return TRANSPORT_INFO_IPV4_TCP; | |
356 | else if (ip->protocol == IPPROTO_UDP) | |
357 | return TRANSPORT_INFO_IPV4_UDP; | |
08cd04bf | 358 | } else { |
23312a3b | 359 | struct ipv6hdr *ip6 = ipv6_hdr(skb); |
360 | ||
361 | if (ip6->nexthdr == IPPROTO_TCP) | |
362 | return TRANSPORT_INFO_IPV6_TCP; | |
37b9dfa0 | 363 | else if (ip6->nexthdr == IPPROTO_UDP) |
23312a3b | 364 | return TRANSPORT_INFO_IPV6_UDP; |
08cd04bf KS |
365 | } |
366 | ||
23312a3b | 367 | return TRANSPORT_INFO_NOT_IP; |
08cd04bf KS |
368 | } |
369 | ||
02fafbc6 | 370 | static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net) |
fceaf24a | 371 | { |
fceaf24a | 372 | struct net_device_context *net_device_ctx = netdev_priv(net); |
981a1bd8 | 373 | struct hv_netvsc_packet *packet = NULL; |
02fafbc6 | 374 | int ret; |
8a00251a KS |
375 | unsigned int num_data_pgs; |
376 | struct rndis_message *rndis_msg; | |
377 | struct rndis_packet *rndis_pkt; | |
378 | u32 rndis_msg_size; | |
8a00251a | 379 | struct rndis_per_packet_info *ppi; |
307f0995 | 380 | u32 hash; |
02b6de01 | 381 | struct hv_page_buffer pb[MAX_PAGE_BUFFER_COUNT]; |
fceaf24a | 382 | |
80d887db | 383 | /* We will atmost need two pages to describe the rndis |
384 | * header. We can only transmit MAX_PAGE_BUFFER_COUNT number | |
e88f7e07 VK |
385 | * of pages in a single packet. If skb is scattered around |
386 | * more pages we try linearizing it. | |
54a7357f | 387 | */ |
80d887db | 388 | |
389 | num_data_pgs = netvsc_get_slots(skb) + 2; | |
390 | ||
0ab05141 | 391 | if (unlikely(num_data_pgs > MAX_PAGE_BUFFER_COUNT)) { |
4323b47c SH |
392 | ++net_device_ctx->eth_stats.tx_scattered; |
393 | ||
394 | if (skb_linearize(skb)) | |
395 | goto no_memory; | |
0ab05141 | 396 | |
80d887db | 397 | num_data_pgs = netvsc_get_slots(skb) + 2; |
0ab05141 | 398 | if (num_data_pgs > MAX_PAGE_BUFFER_COUNT) { |
4323b47c | 399 | ++net_device_ctx->eth_stats.tx_too_big; |
0ab05141 SH |
400 | goto drop; |
401 | } | |
54a7357f | 402 | } |
fceaf24a | 403 | |
c0eb4540 KS |
404 | /* |
405 | * Place the rndis header in the skb head room and | |
406 | * the skb->cb will be used for hv_netvsc_packet | |
407 | * structure. | |
408 | */ | |
409 | ret = skb_cow_head(skb, RNDIS_AND_PPI_SIZE); | |
4323b47c SH |
410 | if (ret) |
411 | goto no_memory; | |
412 | ||
c0eb4540 KS |
413 | /* Use the skb control buffer for building up the packet */ |
414 | BUILD_BUG_ON(sizeof(struct hv_netvsc_packet) > | |
415 | FIELD_SIZEOF(struct sk_buff, cb)); | |
416 | packet = (struct hv_netvsc_packet *)skb->cb; | |
fceaf24a | 417 | |
5b54dac8 HZ |
418 | packet->q_idx = skb_get_queue_mapping(skb); |
419 | ||
4d447c9a | 420 | packet->total_data_buflen = skb->len; |
793e3955 | 421 | packet->total_bytes = skb->len; |
422 | packet->total_packets = 1; | |
fceaf24a | 423 | |
c0eb4540 | 424 | rndis_msg = (struct rndis_message *)skb->head; |
b08cc791 | 425 | |
24476760 | 426 | memset(rndis_msg, 0, RNDIS_AND_PPI_SIZE); |
fceaf24a | 427 | |
8a00251a | 428 | /* Add the rndis header */ |
8a00251a KS |
429 | rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET; |
430 | rndis_msg->msg_len = packet->total_data_buflen; | |
431 | rndis_pkt = &rndis_msg->msg.pkt; | |
432 | rndis_pkt->data_offset = sizeof(struct rndis_packet); | |
433 | rndis_pkt->data_len = packet->total_data_buflen; | |
434 | rndis_pkt->per_pkt_info_offset = sizeof(struct rndis_packet); | |
435 | ||
436 | rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet); | |
437 | ||
307f0995 HZ |
438 | hash = skb_get_hash_raw(skb); |
439 | if (hash != 0 && net->real_num_tx_queues > 1) { | |
440 | rndis_msg_size += NDIS_HASH_PPI_SIZE; | |
441 | ppi = init_ppi_data(rndis_msg, NDIS_HASH_PPI_SIZE, | |
442 | NBL_HASH_VALUE); | |
443 | *(u32 *)((void *)ppi + ppi->ppi_offset) = hash; | |
444 | } | |
445 | ||
0ab05141 | 446 | if (skb_vlan_tag_present(skb)) { |
8a00251a KS |
447 | struct ndis_pkt_8021q_info *vlan; |
448 | ||
449 | rndis_msg_size += NDIS_VLAN_PPI_SIZE; | |
450 | ppi = init_ppi_data(rndis_msg, NDIS_VLAN_PPI_SIZE, | |
451 | IEEE_8021Q_INFO); | |
452 | vlan = (struct ndis_pkt_8021q_info *)((void *)ppi + | |
453 | ppi->ppi_offset); | |
760d1e36 KS |
454 | vlan->vlanid = skb->vlan_tci & VLAN_VID_MASK; |
455 | vlan->pri = (skb->vlan_tci & VLAN_PRIO_MASK) >> | |
8a00251a KS |
456 | VLAN_PRIO_SHIFT; |
457 | } | |
458 | ||
23312a3b | 459 | if (skb_is_gso(skb)) { |
0ab05141 SH |
460 | struct ndis_tcp_lso_info *lso_info; |
461 | ||
462 | rndis_msg_size += NDIS_LSO_PPI_SIZE; | |
463 | ppi = init_ppi_data(rndis_msg, NDIS_LSO_PPI_SIZE, | |
464 | TCP_LARGESEND_PKTINFO); | |
465 | ||
466 | lso_info = (struct ndis_tcp_lso_info *)((void *)ppi + | |
467 | ppi->ppi_offset); | |
468 | ||
469 | lso_info->lso_v2_transmit.type = NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE; | |
23312a3b | 470 | if (skb->protocol == htons(ETH_P_IP)) { |
0ab05141 SH |
471 | lso_info->lso_v2_transmit.ip_version = |
472 | NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4; | |
473 | ip_hdr(skb)->tot_len = 0; | |
474 | ip_hdr(skb)->check = 0; | |
475 | tcp_hdr(skb)->check = | |
476 | ~csum_tcpudp_magic(ip_hdr(skb)->saddr, | |
477 | ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0); | |
478 | } else { | |
479 | lso_info->lso_v2_transmit.ip_version = | |
480 | NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6; | |
481 | ipv6_hdr(skb)->payload_len = 0; | |
482 | tcp_hdr(skb)->check = | |
483 | ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr, | |
484 | &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0); | |
485 | } | |
23312a3b | 486 | lso_info->lso_v2_transmit.tcp_header_offset = skb_transport_offset(skb); |
0ab05141 | 487 | lso_info->lso_v2_transmit.mss = skb_shinfo(skb)->gso_size; |
ad19bc8a | 488 | } else if (skb->ip_summed == CHECKSUM_PARTIAL) { |
23312a3b | 489 | if (net_checksum_info(skb) & net_device_ctx->tx_checksum_mask) { |
490 | struct ndis_tcp_ip_checksum_info *csum_info; | |
491 | ||
ad19bc8a | 492 | rndis_msg_size += NDIS_CSUM_PPI_SIZE; |
493 | ppi = init_ppi_data(rndis_msg, NDIS_CSUM_PPI_SIZE, | |
494 | TCPIP_CHKSUM_PKTINFO); | |
495 | ||
496 | csum_info = (struct ndis_tcp_ip_checksum_info *)((void *)ppi + | |
497 | ppi->ppi_offset); | |
498 | ||
23312a3b | 499 | csum_info->transmit.tcp_header_offset = skb_transport_offset(skb); |
500 | ||
501 | if (skb->protocol == htons(ETH_P_IP)) { | |
ad19bc8a | 502 | csum_info->transmit.is_ipv4 = 1; |
23312a3b | 503 | |
504 | if (ip_hdr(skb)->protocol == IPPROTO_TCP) | |
505 | csum_info->transmit.tcp_checksum = 1; | |
506 | else | |
507 | csum_info->transmit.udp_checksum = 1; | |
508 | } else { | |
ad19bc8a | 509 | csum_info->transmit.is_ipv6 = 1; |
510 | ||
23312a3b | 511 | if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP) |
512 | csum_info->transmit.tcp_checksum = 1; | |
513 | else | |
514 | csum_info->transmit.udp_checksum = 1; | |
515 | } | |
ad19bc8a | 516 | } else { |
23312a3b | 517 | /* Can't do offload of this type of checksum */ |
ad19bc8a | 518 | if (skb_checksum_help(skb)) |
519 | goto drop; | |
520 | } | |
08cd04bf KS |
521 | } |
522 | ||
8a00251a KS |
523 | /* Start filling in the page buffers with the rndis hdr */ |
524 | rndis_msg->msg_len += rndis_msg_size; | |
942396b0 | 525 | packet->total_data_buflen = rndis_msg->msg_len; |
8a00251a | 526 | packet->page_buf_cnt = init_page_array(rndis_msg, rndis_msg_size, |
02b6de01 | 527 | skb, packet, pb); |
8a00251a | 528 | |
76d13b56 | 529 | /* timestamp packet in software */ |
530 | skb_tx_timestamp(skb); | |
2a926f79 | 531 | |
02b6de01 | 532 | ret = netvsc_send(net_device_ctx, packet, rndis_msg, pb, skb); |
793e3955 | 533 | if (likely(ret == 0)) |
0ab05141 | 534 | return NETDEV_TX_OK; |
4323b47c SH |
535 | |
536 | if (ret == -EAGAIN) { | |
537 | ++net_device_ctx->eth_stats.tx_busy; | |
0ab05141 | 538 | return NETDEV_TX_BUSY; |
4323b47c SH |
539 | } |
540 | ||
541 | if (ret == -ENOSPC) | |
542 | ++net_device_ctx->eth_stats.tx_no_space; | |
0ab05141 SH |
543 | |
544 | drop: | |
545 | dev_kfree_skb_any(skb); | |
546 | net->stats.tx_dropped++; | |
fceaf24a | 547 | |
0ab05141 | 548 | return NETDEV_TX_OK; |
4323b47c SH |
549 | |
550 | no_memory: | |
551 | ++net_device_ctx->eth_stats.tx_no_memory; | |
552 | goto drop; | |
fceaf24a | 553 | } |
3e189519 | 554 | /* |
02fafbc6 GKH |
555 | * netvsc_linkstatus_callback - Link up/down notification |
556 | */ | |
90ef117a | 557 | void netvsc_linkstatus_callback(struct hv_device *device_obj, |
3a494e71 | 558 | struct rndis_message *resp) |
fceaf24a | 559 | { |
3a494e71 | 560 | struct rndis_indicate_status *indicate = &resp->msg.indicate_status; |
2ddd5e5f | 561 | struct net_device *net; |
c996edcf | 562 | struct net_device_context *ndev_ctx; |
27a70af3 VK |
563 | struct netvsc_reconfig *event; |
564 | unsigned long flags; | |
891de74d | 565 | |
7f5d5af0 HZ |
566 | net = hv_get_drvdata(device_obj); |
567 | ||
568 | if (!net) | |
569 | return; | |
570 | ||
571 | ndev_ctx = netdev_priv(net); | |
572 | ||
573 | /* Update the physical link speed when changing to another vSwitch */ | |
574 | if (indicate->status == RNDIS_STATUS_LINK_SPEED_CHANGE) { | |
575 | u32 speed; | |
576 | ||
577 | speed = *(u32 *)((void *)indicate + indicate-> | |
578 | status_buf_offset) / 10000; | |
579 | ndev_ctx->speed = speed; | |
580 | return; | |
581 | } | |
582 | ||
583 | /* Handle these link change statuses below */ | |
27a70af3 VK |
584 | if (indicate->status != RNDIS_STATUS_NETWORK_CHANGE && |
585 | indicate->status != RNDIS_STATUS_MEDIA_CONNECT && | |
586 | indicate->status != RNDIS_STATUS_MEDIA_DISCONNECT) | |
3a494e71 | 587 | return; |
891de74d | 588 | |
7f5d5af0 | 589 | if (net->reg_state != NETREG_REGISTERED) |
fceaf24a | 590 | return; |
fceaf24a | 591 | |
27a70af3 VK |
592 | event = kzalloc(sizeof(*event), GFP_ATOMIC); |
593 | if (!event) | |
594 | return; | |
595 | event->event = indicate->status; | |
596 | ||
597 | spin_lock_irqsave(&ndev_ctx->lock, flags); | |
598 | list_add_tail(&event->list, &ndev_ctx->reconfig_events); | |
599 | spin_unlock_irqrestore(&ndev_ctx->lock, flags); | |
600 | ||
601 | schedule_delayed_work(&ndev_ctx->dwork, 0); | |
fceaf24a HJ |
602 | } |
603 | ||
84bf9cef | 604 | static struct sk_buff *netvsc_alloc_recv_skb(struct net_device *net, |
e91e7dd7 | 605 | struct napi_struct *napi, |
dc54a08c | 606 | const struct ndis_tcp_ip_checksum_info *csum_info, |
607 | const struct ndis_pkt_8021q_info *vlan, | |
608 | void *data, u32 buflen) | |
fceaf24a | 609 | { |
fceaf24a | 610 | struct sk_buff *skb; |
fceaf24a | 611 | |
e91e7dd7 | 612 | skb = napi_alloc_skb(napi, buflen); |
84bf9cef KS |
613 | if (!skb) |
614 | return skb; | |
fceaf24a | 615 | |
02fafbc6 GKH |
616 | /* |
617 | * Copy to skb. This copy is needed here since the memory pointed by | |
618 | * hv_netvsc_packet cannot be deallocated | |
619 | */ | |
59ae1d12 | 620 | skb_put_data(skb, data, buflen); |
fceaf24a HJ |
621 | |
622 | skb->protocol = eth_type_trans(skb, net); | |
e52fed71 SH |
623 | |
624 | /* skb is already created with CHECKSUM_NONE */ | |
625 | skb_checksum_none_assert(skb); | |
626 | ||
627 | /* | |
628 | * In Linux, the IP checksum is always checked. | |
629 | * Do L4 checksum offload if enabled and present. | |
630 | */ | |
631 | if (csum_info && (net->features & NETIF_F_RXCSUM)) { | |
632 | if (csum_info->receive.tcp_checksum_succeeded || | |
633 | csum_info->receive.udp_checksum_succeeded) | |
e3d605ed | 634 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
e3d605ed KS |
635 | } |
636 | ||
dc54a08c | 637 | if (vlan) { |
638 | u16 vlan_tci = vlan->vlanid | (vlan->pri << VLAN_PRIO_SHIFT); | |
639 | ||
93725cbd | 640 | __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), |
760d1e36 | 641 | vlan_tci); |
dc54a08c | 642 | } |
fceaf24a | 643 | |
84bf9cef KS |
644 | return skb; |
645 | } | |
646 | ||
647 | /* | |
648 | * netvsc_recv_callback - Callback when we receive a packet from the | |
649 | * "wire" on the specified device. | |
650 | */ | |
dc54a08c | 651 | int netvsc_recv_callback(struct net_device *net, |
652 | struct vmbus_channel *channel, | |
653 | void *data, u32 len, | |
654 | const struct ndis_tcp_ip_checksum_info *csum_info, | |
655 | const struct ndis_pkt_8021q_info *vlan) | |
84bf9cef | 656 | { |
3d541ac5 | 657 | struct net_device_context *net_device_ctx = netdev_priv(net); |
545a8e79 | 658 | struct netvsc_device *net_device; |
742fe54c | 659 | u16 q_idx = channel->offermsg.offer.sub_channel_index; |
545a8e79 | 660 | struct netvsc_channel *nvchan; |
f207c10d | 661 | struct net_device *vf_netdev; |
84bf9cef | 662 | struct sk_buff *skb; |
84bf9cef | 663 | struct netvsc_stats *rx_stats; |
84bf9cef | 664 | |
9cbcc428 | 665 | if (net->reg_state != NETREG_REGISTERED) |
84bf9cef KS |
666 | return NVSP_STAT_FAIL; |
667 | ||
9cbcc428 SH |
668 | /* |
669 | * If necessary, inject this packet into the VF interface. | |
670 | * On Hyper-V, multicast and brodcast packets are only delivered | |
671 | * to the synthetic interface (after subjecting these to | |
672 | * policy filters on the host). Deliver these via the VF | |
673 | * interface in the guest. | |
674 | */ | |
0719e72c | 675 | rcu_read_lock(); |
545a8e79 | 676 | net_device = rcu_dereference(net_device_ctx->nvdev); |
677 | if (unlikely(!net_device)) | |
678 | goto drop; | |
679 | ||
680 | nvchan = &net_device->chan_table[q_idx]; | |
f207c10d | 681 | vf_netdev = rcu_dereference(net_device_ctx->vf_netdev); |
9cbcc428 SH |
682 | if (vf_netdev && (vf_netdev->flags & IFF_UP)) |
683 | net = vf_netdev; | |
84bf9cef KS |
684 | |
685 | /* Allocate a skb - TODO direct I/O to pages? */ | |
e91e7dd7 | 686 | skb = netvsc_alloc_recv_skb(net, &nvchan->napi, |
687 | csum_info, vlan, data, len); | |
84bf9cef | 688 | if (unlikely(!skb)) { |
545a8e79 | 689 | drop: |
84bf9cef | 690 | ++net->stats.rx_dropped; |
0719e72c | 691 | rcu_read_unlock(); |
84bf9cef KS |
692 | return NVSP_STAT_FAIL; |
693 | } | |
5b54dac8 | 694 | |
9cbcc428 | 695 | if (net != vf_netdev) |
6c80f3fc | 696 | skb_record_rx_queue(skb, q_idx); |
9cbcc428 SH |
697 | |
698 | /* | |
699 | * Even if injecting the packet, record the statistics | |
700 | * on the synthetic device because modifying the VF device | |
701 | * statistics will not work correctly. | |
702 | */ | |
742fe54c | 703 | rx_stats = &nvchan->rx_stats; |
4b02b58b | 704 | u64_stats_update_begin(&rx_stats->syncp); |
7eafd9b4 | 705 | rx_stats->packets++; |
dc54a08c | 706 | rx_stats->bytes += len; |
f7ad75b7 SH |
707 | |
708 | if (skb->pkt_type == PACKET_BROADCAST) | |
709 | ++rx_stats->broadcast; | |
710 | else if (skb->pkt_type == PACKET_MULTICAST) | |
711 | ++rx_stats->multicast; | |
4b02b58b | 712 | u64_stats_update_end(&rx_stats->syncp); |
9495c282 | 713 | |
742fe54c | 714 | napi_gro_receive(&nvchan->napi, skb); |
0719e72c | 715 | rcu_read_unlock(); |
fceaf24a | 716 | |
fceaf24a HJ |
717 | return 0; |
718 | } | |
719 | ||
f82f4ad7 SH |
720 | static void netvsc_get_drvinfo(struct net_device *net, |
721 | struct ethtool_drvinfo *info) | |
722 | { | |
7826d43f | 723 | strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver)); |
7826d43f | 724 | strlcpy(info->fw_version, "N/A", sizeof(info->fw_version)); |
f82f4ad7 SH |
725 | } |
726 | ||
59995370 AS |
727 | static void netvsc_get_channels(struct net_device *net, |
728 | struct ethtool_channels *channel) | |
729 | { | |
730 | struct net_device_context *net_device_ctx = netdev_priv(net); | |
545a8e79 | 731 | struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev); |
59995370 AS |
732 | |
733 | if (nvdev) { | |
734 | channel->max_combined = nvdev->max_chn; | |
735 | channel->combined_count = nvdev->num_chn; | |
736 | } | |
737 | } | |
738 | ||
b5960e6e AS |
739 | static int netvsc_set_channels(struct net_device *net, |
740 | struct ethtool_channels *channels) | |
741 | { | |
742 | struct net_device_context *net_device_ctx = netdev_priv(net); | |
743 | struct hv_device *dev = net_device_ctx->device_ctx; | |
545a8e79 | 744 | struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev); |
7ca45933 | 745 | unsigned int orig, count = channels->combined_count; |
746 | struct netvsc_device_info device_info; | |
ea383bf1 | 747 | bool was_opened; |
7ca45933 | 748 | int ret = 0; |
2b01888d | 749 | |
750 | /* We do not support separate count for rx, tx, or other */ | |
751 | if (count == 0 || | |
752 | channels->rx_count || channels->tx_count || channels->other_count) | |
753 | return -EINVAL; | |
754 | ||
b92b7d33 | 755 | if (count > net->num_tx_queues || count > VRSS_CHANNEL_MAX) |
2b01888d | 756 | return -EINVAL; |
b5960e6e | 757 | |
a0be450e | 758 | if (!nvdev || nvdev->destroy) |
b5960e6e AS |
759 | return -ENODEV; |
760 | ||
2b01888d | 761 | if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5) |
b5960e6e | 762 | return -EINVAL; |
b5960e6e | 763 | |
2b01888d | 764 | if (count > nvdev->max_chn) |
b5960e6e AS |
765 | return -EINVAL; |
766 | ||
7ca45933 | 767 | orig = nvdev->num_chn; |
ea383bf1 | 768 | was_opened = rndis_filter_opened(nvdev); |
769 | if (was_opened) | |
770 | rndis_filter_close(nvdev); | |
b5960e6e | 771 | |
2289f0aa | 772 | rndis_filter_device_remove(dev, nvdev); |
b5960e6e | 773 | |
7ca45933 | 774 | memset(&device_info, 0, sizeof(device_info)); |
775 | device_info.num_chn = count; | |
776 | device_info.ring_size = ring_size; | |
7ca45933 | 777 | |
778 | nvdev = rndis_filter_device_add(dev, &device_info); | |
779 | if (!IS_ERR(nvdev)) { | |
780 | netif_set_real_num_tx_queues(net, nvdev->num_chn); | |
781 | netif_set_real_num_rx_queues(net, nvdev->num_chn); | |
7ca45933 | 782 | } else { |
d6aac1f2 | 783 | ret = PTR_ERR(nvdev); |
7ca45933 | 784 | device_info.num_chn = orig; |
7ca45933 | 785 | rndis_filter_device_add(dev, &device_info); |
786 | } | |
b5960e6e | 787 | |
ea383bf1 | 788 | if (was_opened) |
789 | rndis_filter_open(nvdev); | |
163891d7 | 790 | |
1bdcec8a | 791 | /* We may have missed link change notifications */ |
1b01994a | 792 | net_device_ctx->last_reconfig = 0; |
1bdcec8a | 793 | schedule_delayed_work(&net_device_ctx->dwork, 0); |
b5960e6e AS |
794 | |
795 | return ret; | |
b5960e6e AS |
796 | } |
797 | ||
5e8456fd PR |
798 | static bool |
799 | netvsc_validate_ethtool_ss_cmd(const struct ethtool_link_ksettings *cmd) | |
49eb9389 | 800 | { |
5e8456fd PR |
801 | struct ethtool_link_ksettings diff1 = *cmd; |
802 | struct ethtool_link_ksettings diff2 = {}; | |
49eb9389 | 803 | |
5e8456fd PR |
804 | diff1.base.speed = 0; |
805 | diff1.base.duplex = 0; | |
49eb9389 | 806 | /* advertising and cmd are usually set */ |
5e8456fd PR |
807 | ethtool_link_ksettings_zero_link_mode(&diff1, advertising); |
808 | diff1.base.cmd = 0; | |
49eb9389 | 809 | /* We set port to PORT_OTHER */ |
5e8456fd | 810 | diff2.base.port = PORT_OTHER; |
49eb9389 | 811 | |
812 | return !memcmp(&diff1, &diff2, sizeof(diff1)); | |
813 | } | |
814 | ||
815 | static void netvsc_init_settings(struct net_device *dev) | |
816 | { | |
817 | struct net_device_context *ndc = netdev_priv(dev); | |
818 | ||
819 | ndc->speed = SPEED_UNKNOWN; | |
f3c9d40e | 820 | ndc->duplex = DUPLEX_FULL; |
49eb9389 | 821 | } |
822 | ||
5e8456fd PR |
823 | static int netvsc_get_link_ksettings(struct net_device *dev, |
824 | struct ethtool_link_ksettings *cmd) | |
49eb9389 | 825 | { |
826 | struct net_device_context *ndc = netdev_priv(dev); | |
827 | ||
5e8456fd PR |
828 | cmd->base.speed = ndc->speed; |
829 | cmd->base.duplex = ndc->duplex; | |
830 | cmd->base.port = PORT_OTHER; | |
49eb9389 | 831 | |
832 | return 0; | |
833 | } | |
834 | ||
5e8456fd PR |
835 | static int netvsc_set_link_ksettings(struct net_device *dev, |
836 | const struct ethtool_link_ksettings *cmd) | |
49eb9389 | 837 | { |
838 | struct net_device_context *ndc = netdev_priv(dev); | |
839 | u32 speed; | |
840 | ||
5e8456fd | 841 | speed = cmd->base.speed; |
49eb9389 | 842 | if (!ethtool_validate_speed(speed) || |
5e8456fd | 843 | !ethtool_validate_duplex(cmd->base.duplex) || |
49eb9389 | 844 | !netvsc_validate_ethtool_ss_cmd(cmd)) |
845 | return -EINVAL; | |
846 | ||
847 | ndc->speed = speed; | |
5e8456fd | 848 | ndc->duplex = cmd->base.duplex; |
49eb9389 | 849 | |
850 | return 0; | |
851 | } | |
852 | ||
4d447c9a HZ |
853 | static int netvsc_change_mtu(struct net_device *ndev, int mtu) |
854 | { | |
855 | struct net_device_context *ndevctx = netdev_priv(ndev); | |
545a8e79 | 856 | struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev); |
3d541ac5 | 857 | struct hv_device *hdev = ndevctx->device_ctx; |
9749fed5 | 858 | int orig_mtu = ndev->mtu; |
4d447c9a | 859 | struct netvsc_device_info device_info; |
ea383bf1 | 860 | bool was_opened; |
9749fed5 | 861 | int ret = 0; |
4d447c9a | 862 | |
a0be450e | 863 | if (!nvdev || nvdev->destroy) |
4d447c9a HZ |
864 | return -ENODEV; |
865 | ||
ea383bf1 | 866 | netif_device_detach(ndev); |
867 | was_opened = rndis_filter_opened(nvdev); | |
868 | if (was_opened) | |
869 | rndis_filter_close(nvdev); | |
2de8530b | 870 | |
152669bd DC |
871 | memset(&device_info, 0, sizeof(device_info)); |
872 | device_info.ring_size = ring_size; | |
873 | device_info.num_chn = nvdev->num_chn; | |
152669bd | 874 | |
2289f0aa | 875 | rndis_filter_device_remove(hdev, nvdev); |
4d447c9a HZ |
876 | |
877 | ndev->mtu = mtu; | |
878 | ||
9749fed5 | 879 | nvdev = rndis_filter_device_add(hdev, &device_info); |
880 | if (IS_ERR(nvdev)) { | |
881 | ret = PTR_ERR(nvdev); | |
882 | ||
883 | /* Attempt rollback to original MTU */ | |
884 | ndev->mtu = orig_mtu; | |
885 | rndis_filter_device_add(hdev, &device_info); | |
886 | } | |
4d447c9a | 887 | |
ea383bf1 | 888 | if (was_opened) |
889 | rndis_filter_open(nvdev); | |
890 | ||
891 | netif_device_attach(ndev); | |
163891d7 | 892 | |
1bdcec8a VK |
893 | /* We may have missed link change notifications */ |
894 | schedule_delayed_work(&ndevctx->dwork, 0); | |
895 | ||
9749fed5 | 896 | return ret; |
4d447c9a HZ |
897 | } |
898 | ||
bc1f4470 | 899 | static void netvsc_get_stats64(struct net_device *net, |
900 | struct rtnl_link_stats64 *t) | |
7eafd9b4 | 901 | { |
902 | struct net_device_context *ndev_ctx = netdev_priv(net); | |
776e726b | 903 | struct netvsc_device *nvdev = rcu_dereference_rtnl(ndev_ctx->nvdev); |
6c80f3fc SX |
904 | int i; |
905 | ||
906 | if (!nvdev) | |
907 | return; | |
908 | ||
909 | for (i = 0; i < nvdev->num_chn; i++) { | |
910 | const struct netvsc_channel *nvchan = &nvdev->chan_table[i]; | |
911 | const struct netvsc_stats *stats; | |
912 | u64 packets, bytes, multicast; | |
7eafd9b4 | 913 | unsigned int start; |
914 | ||
6c80f3fc | 915 | stats = &nvchan->tx_stats; |
7eafd9b4 | 916 | do { |
6c80f3fc SX |
917 | start = u64_stats_fetch_begin_irq(&stats->syncp); |
918 | packets = stats->packets; | |
919 | bytes = stats->bytes; | |
920 | } while (u64_stats_fetch_retry_irq(&stats->syncp, start)); | |
921 | ||
922 | t->tx_bytes += bytes; | |
923 | t->tx_packets += packets; | |
7eafd9b4 | 924 | |
6c80f3fc | 925 | stats = &nvchan->rx_stats; |
7eafd9b4 | 926 | do { |
6c80f3fc SX |
927 | start = u64_stats_fetch_begin_irq(&stats->syncp); |
928 | packets = stats->packets; | |
929 | bytes = stats->bytes; | |
930 | multicast = stats->multicast + stats->broadcast; | |
931 | } while (u64_stats_fetch_retry_irq(&stats->syncp, start)); | |
932 | ||
933 | t->rx_bytes += bytes; | |
934 | t->rx_packets += packets; | |
935 | t->multicast += multicast; | |
7eafd9b4 | 936 | } |
937 | ||
938 | t->tx_dropped = net->stats.tx_dropped; | |
b5124720 | 939 | t->tx_errors = net->stats.tx_errors; |
7eafd9b4 | 940 | |
941 | t->rx_dropped = net->stats.rx_dropped; | |
942 | t->rx_errors = net->stats.rx_errors; | |
7eafd9b4 | 943 | } |
1ce09e89 HZ |
944 | |
945 | static int netvsc_set_mac_addr(struct net_device *ndev, void *p) | |
946 | { | |
867047c4 | 947 | struct net_device_context *ndc = netdev_priv(ndev); |
948 | struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev); | |
1ce09e89 | 949 | struct sockaddr *addr = p; |
9a4c831e | 950 | char save_adr[ETH_ALEN]; |
1ce09e89 HZ |
951 | unsigned char save_aatype; |
952 | int err; | |
953 | ||
954 | memcpy(save_adr, ndev->dev_addr, ETH_ALEN); | |
955 | save_aatype = ndev->addr_assign_type; | |
956 | ||
957 | err = eth_mac_addr(ndev, p); | |
958 | if (err != 0) | |
959 | return err; | |
960 | ||
867047c4 | 961 | if (!nvdev) |
962 | return -ENODEV; | |
963 | ||
964 | err = rndis_filter_set_device_mac(nvdev, addr->sa_data); | |
1ce09e89 HZ |
965 | if (err != 0) { |
966 | /* roll back to saved MAC */ | |
967 | memcpy(ndev->dev_addr, save_adr, ETH_ALEN); | |
968 | ndev->addr_assign_type = save_aatype; | |
969 | } | |
970 | ||
971 | return err; | |
972 | } | |
973 | ||
4323b47c SH |
974 | static const struct { |
975 | char name[ETH_GSTRING_LEN]; | |
976 | u16 offset; | |
977 | } netvsc_stats[] = { | |
978 | { "tx_scattered", offsetof(struct netvsc_ethtool_stats, tx_scattered) }, | |
979 | { "tx_no_memory", offsetof(struct netvsc_ethtool_stats, tx_no_memory) }, | |
980 | { "tx_no_space", offsetof(struct netvsc_ethtool_stats, tx_no_space) }, | |
981 | { "tx_too_big", offsetof(struct netvsc_ethtool_stats, tx_too_big) }, | |
982 | { "tx_busy", offsetof(struct netvsc_ethtool_stats, tx_busy) }, | |
983 | }; | |
984 | ||
6c80f3fc SX |
985 | #define NETVSC_GLOBAL_STATS_LEN ARRAY_SIZE(netvsc_stats) |
986 | ||
987 | /* 4 statistics per queue (rx/tx packets/bytes) */ | |
988 | #define NETVSC_QUEUE_STATS_LEN(dev) ((dev)->num_chn * 4) | |
989 | ||
4323b47c SH |
990 | static int netvsc_get_sset_count(struct net_device *dev, int string_set) |
991 | { | |
6c80f3fc | 992 | struct net_device_context *ndc = netdev_priv(dev); |
fbd4c7e7 | 993 | struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev); |
545a8e79 | 994 | |
995 | if (!nvdev) | |
996 | return -ENODEV; | |
6c80f3fc | 997 | |
4323b47c SH |
998 | switch (string_set) { |
999 | case ETH_SS_STATS: | |
6c80f3fc | 1000 | return NETVSC_GLOBAL_STATS_LEN + NETVSC_QUEUE_STATS_LEN(nvdev); |
4323b47c SH |
1001 | default: |
1002 | return -EINVAL; | |
1003 | } | |
1004 | } | |
1005 | ||
1006 | static void netvsc_get_ethtool_stats(struct net_device *dev, | |
1007 | struct ethtool_stats *stats, u64 *data) | |
1008 | { | |
1009 | struct net_device_context *ndc = netdev_priv(dev); | |
867047c4 | 1010 | struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev); |
4323b47c | 1011 | const void *nds = &ndc->eth_stats; |
6c80f3fc SX |
1012 | const struct netvsc_stats *qstats; |
1013 | unsigned int start; | |
1014 | u64 packets, bytes; | |
1015 | int i, j; | |
4323b47c | 1016 | |
545a8e79 | 1017 | if (!nvdev) |
1018 | return; | |
1019 | ||
6c80f3fc | 1020 | for (i = 0; i < NETVSC_GLOBAL_STATS_LEN; i++) |
4323b47c | 1021 | data[i] = *(unsigned long *)(nds + netvsc_stats[i].offset); |
6c80f3fc SX |
1022 | |
1023 | for (j = 0; j < nvdev->num_chn; j++) { | |
1024 | qstats = &nvdev->chan_table[j].tx_stats; | |
1025 | ||
1026 | do { | |
1027 | start = u64_stats_fetch_begin_irq(&qstats->syncp); | |
1028 | packets = qstats->packets; | |
1029 | bytes = qstats->bytes; | |
1030 | } while (u64_stats_fetch_retry_irq(&qstats->syncp, start)); | |
1031 | data[i++] = packets; | |
1032 | data[i++] = bytes; | |
1033 | ||
1034 | qstats = &nvdev->chan_table[j].rx_stats; | |
1035 | do { | |
1036 | start = u64_stats_fetch_begin_irq(&qstats->syncp); | |
1037 | packets = qstats->packets; | |
1038 | bytes = qstats->bytes; | |
1039 | } while (u64_stats_fetch_retry_irq(&qstats->syncp, start)); | |
1040 | data[i++] = packets; | |
1041 | data[i++] = bytes; | |
1042 | } | |
4323b47c SH |
1043 | } |
1044 | ||
1045 | static void netvsc_get_strings(struct net_device *dev, u32 stringset, u8 *data) | |
1046 | { | |
6c80f3fc | 1047 | struct net_device_context *ndc = netdev_priv(dev); |
867047c4 | 1048 | struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev); |
6c80f3fc | 1049 | u8 *p = data; |
4323b47c SH |
1050 | int i; |
1051 | ||
545a8e79 | 1052 | if (!nvdev) |
1053 | return; | |
1054 | ||
4323b47c SH |
1055 | switch (stringset) { |
1056 | case ETH_SS_STATS: | |
1057 | for (i = 0; i < ARRAY_SIZE(netvsc_stats); i++) | |
6c80f3fc | 1058 | memcpy(p + i * ETH_GSTRING_LEN, |
4323b47c | 1059 | netvsc_stats[i].name, ETH_GSTRING_LEN); |
6c80f3fc SX |
1060 | |
1061 | p += i * ETH_GSTRING_LEN; | |
1062 | for (i = 0; i < nvdev->num_chn; i++) { | |
1063 | sprintf(p, "tx_queue_%u_packets", i); | |
1064 | p += ETH_GSTRING_LEN; | |
1065 | sprintf(p, "tx_queue_%u_bytes", i); | |
1066 | p += ETH_GSTRING_LEN; | |
1067 | sprintf(p, "rx_queue_%u_packets", i); | |
1068 | p += ETH_GSTRING_LEN; | |
1069 | sprintf(p, "rx_queue_%u_bytes", i); | |
1070 | p += ETH_GSTRING_LEN; | |
1071 | } | |
1072 | ||
4323b47c SH |
1073 | break; |
1074 | } | |
1075 | } | |
1076 | ||
b5a5dc8d | 1077 | static int |
1078 | netvsc_get_rss_hash_opts(struct netvsc_device *nvdev, | |
1079 | struct ethtool_rxnfc *info) | |
1080 | { | |
1081 | info->data = RXH_IP_SRC | RXH_IP_DST; | |
1082 | ||
1083 | switch (info->flow_type) { | |
1084 | case TCP_V4_FLOW: | |
1085 | case TCP_V6_FLOW: | |
1086 | info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3; | |
1087 | /* fallthrough */ | |
1088 | case UDP_V4_FLOW: | |
1089 | case UDP_V6_FLOW: | |
1090 | case IPV4_FLOW: | |
1091 | case IPV6_FLOW: | |
1092 | break; | |
1093 | default: | |
1094 | info->data = 0; | |
1095 | break; | |
1096 | } | |
1097 | ||
1098 | return 0; | |
1099 | } | |
1100 | ||
b448f4e8 | 1101 | static int |
1102 | netvsc_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info, | |
1103 | u32 *rules) | |
1104 | { | |
1105 | struct net_device_context *ndc = netdev_priv(dev); | |
867047c4 | 1106 | struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev); |
545a8e79 | 1107 | |
1108 | if (!nvdev) | |
1109 | return -ENODEV; | |
b448f4e8 | 1110 | |
1111 | switch (info->cmd) { | |
1112 | case ETHTOOL_GRXRINGS: | |
1113 | info->data = nvdev->num_chn; | |
1114 | return 0; | |
b5a5dc8d | 1115 | |
1116 | case ETHTOOL_GRXFH: | |
1117 | return netvsc_get_rss_hash_opts(nvdev, info); | |
b448f4e8 | 1118 | } |
1119 | return -EOPNOTSUPP; | |
1120 | } | |
1121 | ||
316158fe | 1122 | #ifdef CONFIG_NET_POLL_CONTROLLER |
a5ecd439 | 1123 | static void netvsc_poll_controller(struct net_device *dev) |
316158fe | 1124 | { |
a5ecd439 | 1125 | struct net_device_context *ndc = netdev_priv(dev); |
1126 | struct netvsc_device *ndev; | |
1127 | int i; | |
1128 | ||
1129 | rcu_read_lock(); | |
1130 | ndev = rcu_dereference(ndc->nvdev); | |
1131 | if (ndev) { | |
1132 | for (i = 0; i < ndev->num_chn; i++) { | |
1133 | struct netvsc_channel *nvchan = &ndev->chan_table[i]; | |
1134 | ||
1135 | napi_schedule(&nvchan->napi); | |
1136 | } | |
1137 | } | |
1138 | rcu_read_unlock(); | |
316158fe RW |
1139 | } |
1140 | #endif | |
1ce09e89 | 1141 | |
962f3fee | 1142 | static u32 netvsc_get_rxfh_key_size(struct net_device *dev) |
1143 | { | |
1144 | return NETVSC_HASH_KEYLEN; | |
1145 | } | |
1146 | ||
1147 | static u32 netvsc_rss_indir_size(struct net_device *dev) | |
1148 | { | |
ff4a4419 | 1149 | return ITAB_NUM; |
962f3fee | 1150 | } |
1151 | ||
1152 | static int netvsc_get_rxfh(struct net_device *dev, u32 *indir, u8 *key, | |
1153 | u8 *hfunc) | |
1154 | { | |
1155 | struct net_device_context *ndc = netdev_priv(dev); | |
867047c4 | 1156 | struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev); |
eb996edb | 1157 | struct rndis_device *rndis_dev; |
ff4a4419 | 1158 | int i; |
962f3fee | 1159 | |
545a8e79 | 1160 | if (!ndev) |
1161 | return -ENODEV; | |
1162 | ||
962f3fee | 1163 | if (hfunc) |
1164 | *hfunc = ETH_RSS_HASH_TOP; /* Toeplitz */ | |
1165 | ||
eb996edb | 1166 | rndis_dev = ndev->extension; |
ff4a4419 | 1167 | if (indir) { |
1168 | for (i = 0; i < ITAB_NUM; i++) | |
1169 | indir[i] = rndis_dev->ind_table[i]; | |
1170 | } | |
1171 | ||
962f3fee | 1172 | if (key) |
1173 | memcpy(key, rndis_dev->rss_key, NETVSC_HASH_KEYLEN); | |
1174 | ||
1175 | return 0; | |
1176 | } | |
1177 | ||
1178 | static int netvsc_set_rxfh(struct net_device *dev, const u32 *indir, | |
1179 | const u8 *key, const u8 hfunc) | |
1180 | { | |
1181 | struct net_device_context *ndc = netdev_priv(dev); | |
545a8e79 | 1182 | struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev); |
eb996edb | 1183 | struct rndis_device *rndis_dev; |
ff4a4419 | 1184 | int i; |
962f3fee | 1185 | |
545a8e79 | 1186 | if (!ndev) |
1187 | return -ENODEV; | |
1188 | ||
962f3fee | 1189 | if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP) |
1190 | return -EOPNOTSUPP; | |
1191 | ||
eb996edb | 1192 | rndis_dev = ndev->extension; |
ff4a4419 | 1193 | if (indir) { |
1194 | for (i = 0; i < ITAB_NUM; i++) | |
b92b7d33 | 1195 | if (indir[i] >= VRSS_CHANNEL_MAX) |
ff4a4419 | 1196 | return -EINVAL; |
1197 | ||
1198 | for (i = 0; i < ITAB_NUM; i++) | |
1199 | rndis_dev->ind_table[i] = indir[i]; | |
1200 | } | |
1201 | ||
1202 | if (!key) { | |
1203 | if (!indir) | |
1204 | return 0; | |
1205 | ||
1206 | key = rndis_dev->rss_key; | |
1207 | } | |
962f3fee | 1208 | |
1209 | return rndis_filter_set_rss_param(rndis_dev, key, ndev->num_chn); | |
1210 | } | |
1211 | ||
f82f4ad7 SH |
1212 | static const struct ethtool_ops ethtool_ops = { |
1213 | .get_drvinfo = netvsc_get_drvinfo, | |
f82f4ad7 | 1214 | .get_link = ethtool_op_get_link, |
4323b47c SH |
1215 | .get_ethtool_stats = netvsc_get_ethtool_stats, |
1216 | .get_sset_count = netvsc_get_sset_count, | |
1217 | .get_strings = netvsc_get_strings, | |
59995370 | 1218 | .get_channels = netvsc_get_channels, |
b5960e6e | 1219 | .set_channels = netvsc_set_channels, |
76d13b56 | 1220 | .get_ts_info = ethtool_op_get_ts_info, |
b448f4e8 | 1221 | .get_rxnfc = netvsc_get_rxnfc, |
962f3fee | 1222 | .get_rxfh_key_size = netvsc_get_rxfh_key_size, |
1223 | .get_rxfh_indir_size = netvsc_rss_indir_size, | |
1224 | .get_rxfh = netvsc_get_rxfh, | |
1225 | .set_rxfh = netvsc_set_rxfh, | |
5e8456fd PR |
1226 | .get_link_ksettings = netvsc_get_link_ksettings, |
1227 | .set_link_ksettings = netvsc_set_link_ksettings, | |
f82f4ad7 SH |
1228 | }; |
1229 | ||
df2fff28 GKH |
1230 | static const struct net_device_ops device_ops = { |
1231 | .ndo_open = netvsc_open, | |
1232 | .ndo_stop = netvsc_close, | |
1233 | .ndo_start_xmit = netvsc_start_xmit, | |
afc4b13d | 1234 | .ndo_set_rx_mode = netvsc_set_multicast_list, |
4d447c9a | 1235 | .ndo_change_mtu = netvsc_change_mtu, |
b681b588 | 1236 | .ndo_validate_addr = eth_validate_addr, |
1ce09e89 | 1237 | .ndo_set_mac_address = netvsc_set_mac_addr, |
5b54dac8 | 1238 | .ndo_select_queue = netvsc_select_queue, |
7eafd9b4 | 1239 | .ndo_get_stats64 = netvsc_get_stats64, |
316158fe RW |
1240 | #ifdef CONFIG_NET_POLL_CONTROLLER |
1241 | .ndo_poll_controller = netvsc_poll_controller, | |
1242 | #endif | |
df2fff28 GKH |
1243 | }; |
1244 | ||
c996edcf | 1245 | /* |
27a70af3 VK |
1246 | * Handle link status changes. For RNDIS_STATUS_NETWORK_CHANGE emulate link |
1247 | * down/up sequence. In case of RNDIS_STATUS_MEDIA_CONNECT when carrier is | |
1248 | * present send GARP packet to network peers with netif_notify_peers(). | |
c996edcf | 1249 | */ |
891de74d | 1250 | static void netvsc_link_change(struct work_struct *w) |
c996edcf | 1251 | { |
0a1275ca VK |
1252 | struct net_device_context *ndev_ctx = |
1253 | container_of(w, struct net_device_context, dwork.work); | |
1254 | struct hv_device *device_obj = ndev_ctx->device_ctx; | |
1255 | struct net_device *net = hv_get_drvdata(device_obj); | |
2ddd5e5f | 1256 | struct netvsc_device *net_device; |
891de74d | 1257 | struct rndis_device *rdev; |
27a70af3 VK |
1258 | struct netvsc_reconfig *event = NULL; |
1259 | bool notify = false, reschedule = false; | |
1260 | unsigned long flags, next_reconfig, delay; | |
c996edcf | 1261 | |
1bdcec8a | 1262 | rtnl_lock(); |
a0be450e | 1263 | net_device = rtnl_dereference(ndev_ctx->nvdev); |
1264 | if (!net_device) | |
1bdcec8a VK |
1265 | goto out_unlock; |
1266 | ||
891de74d | 1267 | rdev = net_device->extension; |
891de74d | 1268 | |
27a70af3 VK |
1269 | next_reconfig = ndev_ctx->last_reconfig + LINKCHANGE_INT; |
1270 | if (time_is_after_jiffies(next_reconfig)) { | |
1271 | /* link_watch only sends one notification with current state | |
1272 | * per second, avoid doing reconfig more frequently. Handle | |
1273 | * wrap around. | |
1274 | */ | |
1275 | delay = next_reconfig - jiffies; | |
1276 | delay = delay < LINKCHANGE_INT ? delay : LINKCHANGE_INT; | |
1277 | schedule_delayed_work(&ndev_ctx->dwork, delay); | |
1bdcec8a | 1278 | goto out_unlock; |
27a70af3 VK |
1279 | } |
1280 | ndev_ctx->last_reconfig = jiffies; | |
1281 | ||
1282 | spin_lock_irqsave(&ndev_ctx->lock, flags); | |
1283 | if (!list_empty(&ndev_ctx->reconfig_events)) { | |
1284 | event = list_first_entry(&ndev_ctx->reconfig_events, | |
1285 | struct netvsc_reconfig, list); | |
1286 | list_del(&event->list); | |
1287 | reschedule = !list_empty(&ndev_ctx->reconfig_events); | |
1288 | } | |
1289 | spin_unlock_irqrestore(&ndev_ctx->lock, flags); | |
1290 | ||
1291 | if (!event) | |
1bdcec8a | 1292 | goto out_unlock; |
27a70af3 VK |
1293 | |
1294 | switch (event->event) { | |
1295 | /* Only the following events are possible due to the check in | |
1296 | * netvsc_linkstatus_callback() | |
1297 | */ | |
1298 | case RNDIS_STATUS_MEDIA_CONNECT: | |
1299 | if (rdev->link_state) { | |
1300 | rdev->link_state = false; | |
53fa1a6f HZ |
1301 | if (!ndev_ctx->datapath) |
1302 | netif_carrier_on(net); | |
27a70af3 VK |
1303 | netif_tx_wake_all_queues(net); |
1304 | } else { | |
1305 | notify = true; | |
1306 | } | |
1307 | kfree(event); | |
1308 | break; | |
1309 | case RNDIS_STATUS_MEDIA_DISCONNECT: | |
1310 | if (!rdev->link_state) { | |
1311 | rdev->link_state = true; | |
1312 | netif_carrier_off(net); | |
1313 | netif_tx_stop_all_queues(net); | |
1314 | } | |
1315 | kfree(event); | |
1316 | break; | |
1317 | case RNDIS_STATUS_NETWORK_CHANGE: | |
1318 | /* Only makes sense if carrier is present */ | |
1319 | if (!rdev->link_state) { | |
1320 | rdev->link_state = true; | |
1321 | netif_carrier_off(net); | |
1322 | netif_tx_stop_all_queues(net); | |
1323 | event->event = RNDIS_STATUS_MEDIA_CONNECT; | |
1324 | spin_lock_irqsave(&ndev_ctx->lock, flags); | |
15cfd407 | 1325 | list_add(&event->list, &ndev_ctx->reconfig_events); |
27a70af3 VK |
1326 | spin_unlock_irqrestore(&ndev_ctx->lock, flags); |
1327 | reschedule = true; | |
3a494e71 | 1328 | } |
27a70af3 | 1329 | break; |
891de74d HZ |
1330 | } |
1331 | ||
1332 | rtnl_unlock(); | |
1333 | ||
1334 | if (notify) | |
1335 | netdev_notify_peers(net); | |
27a70af3 VK |
1336 | |
1337 | /* link_watch only sends one notification with current state per | |
1338 | * second, handle next reconfig event in 2 seconds. | |
1339 | */ | |
1340 | if (reschedule) | |
1341 | schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT); | |
1bdcec8a VK |
1342 | |
1343 | return; | |
1344 | ||
1345 | out_unlock: | |
1346 | rtnl_unlock(); | |
c996edcf HZ |
1347 | } |
1348 | ||
e8ff40d4 | 1349 | static struct net_device *get_netvsc_bymac(const u8 *mac) |
84bf9cef | 1350 | { |
e8ff40d4 | 1351 | struct net_device *dev; |
84bf9cef | 1352 | |
8737caaf | 1353 | ASSERT_RTNL(); |
84bf9cef KS |
1354 | |
1355 | for_each_netdev(&init_net, dev) { | |
e8ff40d4 SH |
1356 | if (dev->netdev_ops != &device_ops) |
1357 | continue; /* not a netvsc device */ | |
1358 | ||
1359 | if (ether_addr_equal(mac, dev->perm_addr)) | |
1360 | return dev; | |
1361 | } | |
1362 | ||
1363 | return NULL; | |
1364 | } | |
1365 | ||
f207c10d | 1366 | static struct net_device *get_netvsc_byref(struct net_device *vf_netdev) |
e8ff40d4 SH |
1367 | { |
1368 | struct net_device *dev; | |
1369 | ||
1370 | ASSERT_RTNL(); | |
1371 | ||
1372 | for_each_netdev(&init_net, dev) { | |
1373 | struct net_device_context *net_device_ctx; | |
1374 | ||
1375 | if (dev->netdev_ops != &device_ops) | |
1376 | continue; /* not a netvsc device */ | |
1377 | ||
1378 | net_device_ctx = netdev_priv(dev); | |
79e8cbe7 | 1379 | if (!rtnl_dereference(net_device_ctx->nvdev)) |
e8ff40d4 SH |
1380 | continue; /* device is removed */ |
1381 | ||
f207c10d | 1382 | if (rtnl_dereference(net_device_ctx->vf_netdev) == vf_netdev) |
e8ff40d4 | 1383 | return dev; /* a match */ |
84bf9cef | 1384 | } |
84bf9cef | 1385 | |
e8ff40d4 | 1386 | return NULL; |
84bf9cef KS |
1387 | } |
1388 | ||
1389 | static int netvsc_register_vf(struct net_device *vf_netdev) | |
1390 | { | |
0a1275ca VK |
1391 | struct net_device *ndev; |
1392 | struct net_device_context *net_device_ctx; | |
84bf9cef | 1393 | struct netvsc_device *netvsc_dev; |
84bf9cef | 1394 | |
e8ff40d4 SH |
1395 | if (vf_netdev->addr_len != ETH_ALEN) |
1396 | return NOTIFY_DONE; | |
1397 | ||
84bf9cef KS |
1398 | /* |
1399 | * We will use the MAC address to locate the synthetic interface to | |
1400 | * associate with the VF interface. If we don't find a matching | |
1401 | * synthetic interface, move on. | |
1402 | */ | |
e8ff40d4 | 1403 | ndev = get_netvsc_bymac(vf_netdev->perm_addr); |
0a1275ca VK |
1404 | if (!ndev) |
1405 | return NOTIFY_DONE; | |
1406 | ||
1407 | net_device_ctx = netdev_priv(ndev); | |
545a8e79 | 1408 | netvsc_dev = rtnl_dereference(net_device_ctx->nvdev); |
f207c10d | 1409 | if (!netvsc_dev || rtnl_dereference(net_device_ctx->vf_netdev)) |
84bf9cef KS |
1410 | return NOTIFY_DONE; |
1411 | ||
0a1275ca | 1412 | netdev_info(ndev, "VF registering: %s\n", vf_netdev->name); |
84bf9cef KS |
1413 | /* |
1414 | * Take a reference on the module. | |
1415 | */ | |
1416 | try_module_get(THIS_MODULE); | |
07d0f000 SH |
1417 | |
1418 | dev_hold(vf_netdev); | |
f207c10d | 1419 | rcu_assign_pointer(net_device_ctx->vf_netdev, vf_netdev); |
84bf9cef KS |
1420 | return NOTIFY_OK; |
1421 | } | |
1422 | ||
84bf9cef KS |
1423 | static int netvsc_vf_up(struct net_device *vf_netdev) |
1424 | { | |
0a1275ca | 1425 | struct net_device *ndev; |
84bf9cef | 1426 | struct netvsc_device *netvsc_dev; |
84bf9cef KS |
1427 | struct net_device_context *net_device_ctx; |
1428 | ||
e8ff40d4 | 1429 | ndev = get_netvsc_byref(vf_netdev); |
0a1275ca VK |
1430 | if (!ndev) |
1431 | return NOTIFY_DONE; | |
1432 | ||
1433 | net_device_ctx = netdev_priv(ndev); | |
545a8e79 | 1434 | netvsc_dev = rtnl_dereference(net_device_ctx->nvdev); |
84bf9cef | 1435 | |
0a1275ca | 1436 | netdev_info(ndev, "VF up: %s\n", vf_netdev->name); |
84bf9cef KS |
1437 | |
1438 | /* | |
1439 | * Open the device before switching data path. | |
1440 | */ | |
2f5fa6c8 | 1441 | rndis_filter_open(netvsc_dev); |
84bf9cef KS |
1442 | |
1443 | /* | |
1444 | * notify the host to switch the data path. | |
1445 | */ | |
0a1275ca VK |
1446 | netvsc_switch_datapath(ndev, true); |
1447 | netdev_info(ndev, "Data path switched to VF: %s\n", vf_netdev->name); | |
84bf9cef | 1448 | |
0a1275ca | 1449 | netif_carrier_off(ndev); |
84bf9cef | 1450 | |
d072218f VK |
1451 | /* Now notify peers through VF device. */ |
1452 | call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, vf_netdev); | |
84bf9cef KS |
1453 | |
1454 | return NOTIFY_OK; | |
1455 | } | |
1456 | ||
84bf9cef KS |
1457 | static int netvsc_vf_down(struct net_device *vf_netdev) |
1458 | { | |
0a1275ca | 1459 | struct net_device *ndev; |
84bf9cef KS |
1460 | struct netvsc_device *netvsc_dev; |
1461 | struct net_device_context *net_device_ctx; | |
84bf9cef | 1462 | |
e8ff40d4 | 1463 | ndev = get_netvsc_byref(vf_netdev); |
0a1275ca VK |
1464 | if (!ndev) |
1465 | return NOTIFY_DONE; | |
1466 | ||
1467 | net_device_ctx = netdev_priv(ndev); | |
545a8e79 | 1468 | netvsc_dev = rtnl_dereference(net_device_ctx->nvdev); |
84bf9cef | 1469 | |
0a1275ca | 1470 | netdev_info(ndev, "VF down: %s\n", vf_netdev->name); |
0a1275ca VK |
1471 | netvsc_switch_datapath(ndev, false); |
1472 | netdev_info(ndev, "Data path switched from VF: %s\n", vf_netdev->name); | |
2f5fa6c8 | 1473 | rndis_filter_close(netvsc_dev); |
0a1275ca | 1474 | netif_carrier_on(ndev); |
d072218f VK |
1475 | |
1476 | /* Now notify peers through netvsc device. */ | |
1477 | call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, ndev); | |
84bf9cef KS |
1478 | |
1479 | return NOTIFY_OK; | |
1480 | } | |
1481 | ||
84bf9cef KS |
1482 | static int netvsc_unregister_vf(struct net_device *vf_netdev) |
1483 | { | |
0a1275ca | 1484 | struct net_device *ndev; |
0a1275ca | 1485 | struct net_device_context *net_device_ctx; |
84bf9cef | 1486 | |
e8ff40d4 | 1487 | ndev = get_netvsc_byref(vf_netdev); |
0a1275ca VK |
1488 | if (!ndev) |
1489 | return NOTIFY_DONE; | |
1490 | ||
1491 | net_device_ctx = netdev_priv(ndev); | |
e8ff40d4 | 1492 | |
0a1275ca | 1493 | netdev_info(ndev, "VF unregistering: %s\n", vf_netdev->name); |
f207c10d SH |
1494 | |
1495 | RCU_INIT_POINTER(net_device_ctx->vf_netdev, NULL); | |
07d0f000 | 1496 | dev_put(vf_netdev); |
84bf9cef KS |
1497 | module_put(THIS_MODULE); |
1498 | return NOTIFY_OK; | |
1499 | } | |
1500 | ||
84946899 S |
1501 | static int netvsc_probe(struct hv_device *dev, |
1502 | const struct hv_vmbus_device_id *dev_id) | |
df2fff28 | 1503 | { |
df2fff28 GKH |
1504 | struct net_device *net = NULL; |
1505 | struct net_device_context *net_device_ctx; | |
1506 | struct netvsc_device_info device_info; | |
5b54dac8 | 1507 | struct netvsc_device *nvdev; |
df2fff28 GKH |
1508 | int ret; |
1509 | ||
5b54dac8 | 1510 | net = alloc_etherdev_mq(sizeof(struct net_device_context), |
2b01888d | 1511 | VRSS_CHANNEL_MAX); |
df2fff28 | 1512 | if (!net) |
51a805d0 | 1513 | return -ENOMEM; |
df2fff28 | 1514 | |
1b07da51 HZ |
1515 | netif_carrier_off(net); |
1516 | ||
b37879e6 HZ |
1517 | netvsc_init_settings(net); |
1518 | ||
df2fff28 | 1519 | net_device_ctx = netdev_priv(net); |
9efd21e1 | 1520 | net_device_ctx->device_ctx = dev; |
3f300ff4 SX |
1521 | net_device_ctx->msg_enable = netif_msg_init(debug, default_msg); |
1522 | if (netif_msg_probe(net_device_ctx)) | |
1523 | netdev_dbg(net, "netvsc msg_enable: %d\n", | |
1524 | net_device_ctx->msg_enable); | |
1525 | ||
2ddd5e5f | 1526 | hv_set_drvdata(dev, net); |
f580aec4 | 1527 | |
891de74d | 1528 | INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change); |
df2fff28 | 1529 | |
27a70af3 VK |
1530 | spin_lock_init(&net_device_ctx->lock); |
1531 | INIT_LIST_HEAD(&net_device_ctx->reconfig_events); | |
1532 | ||
df2fff28 | 1533 | net->netdev_ops = &device_ops; |
7ad24ea4 | 1534 | net->ethtool_ops = ðtool_ops; |
9efd21e1 | 1535 | SET_NETDEV_DEV(net, &dev->device); |
df2fff28 | 1536 | |
14a03cf8 VK |
1537 | /* We always need headroom for rndis header */ |
1538 | net->needed_headroom = RNDIS_AND_PPI_SIZE; | |
1539 | ||
692e084e | 1540 | /* Notify the netvsc driver of the new device */ |
8ebdcc52 | 1541 | memset(&device_info, 0, sizeof(device_info)); |
692e084e | 1542 | device_info.ring_size = ring_size; |
3071ada4 | 1543 | device_info.num_chn = VRSS_CHANNEL_DEFAULT; |
9749fed5 | 1544 | |
1545 | nvdev = rndis_filter_device_add(dev, &device_info); | |
1546 | if (IS_ERR(nvdev)) { | |
1547 | ret = PTR_ERR(nvdev); | |
692e084e | 1548 | netdev_err(net, "unable to add netvsc device (ret %d)\n", ret); |
6c80f3fc | 1549 | free_netdev(net); |
2ddd5e5f | 1550 | hv_set_drvdata(dev, NULL); |
692e084e | 1551 | return ret; |
df2fff28 | 1552 | } |
692e084e HZ |
1553 | memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN); |
1554 | ||
23312a3b | 1555 | /* hw_features computed in rndis_filter_device_add */ |
1556 | net->features = net->hw_features | | |
1557 | NETIF_F_HIGHDMA | NETIF_F_SG | | |
1558 | NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX; | |
1559 | net->vlan_features = net->features; | |
1560 | ||
5b54dac8 HZ |
1561 | netif_set_real_num_tx_queues(net, nvdev->num_chn); |
1562 | netif_set_real_num_rx_queues(net, nvdev->num_chn); | |
5b54dac8 | 1563 | |
9749fed5 | 1564 | netdev_lockdep_set_classes(net); |
1565 | ||
d0c2c997 JW |
1566 | /* MTU range: 68 - 1500 or 65521 */ |
1567 | net->min_mtu = NETVSC_MTU_MIN; | |
1568 | if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2) | |
1569 | net->max_mtu = NETVSC_MTU - ETH_HLEN; | |
1570 | else | |
1571 | net->max_mtu = ETH_DATA_LEN; | |
1572 | ||
a68f9614 HZ |
1573 | ret = register_netdev(net); |
1574 | if (ret != 0) { | |
1575 | pr_err("Unable to register netdev.\n"); | |
2289f0aa | 1576 | rndis_filter_device_remove(dev, nvdev); |
6c80f3fc | 1577 | free_netdev(net); |
a68f9614 HZ |
1578 | } |
1579 | ||
df2fff28 GKH |
1580 | return ret; |
1581 | } | |
1582 | ||
415b023a | 1583 | static int netvsc_remove(struct hv_device *dev) |
df2fff28 | 1584 | { |
2ddd5e5f | 1585 | struct net_device *net; |
122a5f64 | 1586 | struct net_device_context *ndev_ctx; |
2ddd5e5f | 1587 | |
3d541ac5 | 1588 | net = hv_get_drvdata(dev); |
df2fff28 | 1589 | |
df2fff28 | 1590 | if (net == NULL) { |
415b023a | 1591 | dev_err(&dev->device, "No net device to remove\n"); |
df2fff28 GKH |
1592 | return 0; |
1593 | } | |
1594 | ||
122a5f64 | 1595 | ndev_ctx = netdev_priv(net); |
3d541ac5 | 1596 | |
a0be450e | 1597 | netif_device_detach(net); |
f580aec4 | 1598 | |
122a5f64 HZ |
1599 | cancel_delayed_work_sync(&ndev_ctx->dwork); |
1600 | ||
df2fff28 GKH |
1601 | /* |
1602 | * Call to the vsc driver to let it know that the device is being | |
a0be450e | 1603 | * removed. Also blocks mtu and channel changes. |
df2fff28 | 1604 | */ |
a0be450e | 1605 | rtnl_lock(); |
79e8cbe7 | 1606 | rndis_filter_device_remove(dev, |
1607 | rtnl_dereference(ndev_ctx->nvdev)); | |
a0be450e | 1608 | rtnl_unlock(); |
1609 | ||
1610 | unregister_netdev(net); | |
df2fff28 | 1611 | |
3d541ac5 VK |
1612 | hv_set_drvdata(dev, NULL); |
1613 | ||
6c80f3fc | 1614 | free_netdev(net); |
df06bcff | 1615 | return 0; |
df2fff28 GKH |
1616 | } |
1617 | ||
345c4cc3 | 1618 | static const struct hv_vmbus_device_id id_table[] = { |
c45cf2d4 | 1619 | /* Network guid */ |
8f505944 | 1620 | { HV_NIC_GUID, }, |
c45cf2d4 | 1621 | { }, |
345c4cc3 S |
1622 | }; |
1623 | ||
1624 | MODULE_DEVICE_TABLE(vmbus, id_table); | |
1625 | ||
f1542a66 | 1626 | /* The one and only one */ |
fde0ef9b | 1627 | static struct hv_driver netvsc_drv = { |
d31b20fc | 1628 | .name = KBUILD_MODNAME, |
345c4cc3 | 1629 | .id_table = id_table, |
fde0ef9b S |
1630 | .probe = netvsc_probe, |
1631 | .remove = netvsc_remove, | |
d4890970 | 1632 | }; |
f1542a66 | 1633 | |
84bf9cef KS |
1634 | /* |
1635 | * On Hyper-V, every VF interface is matched with a corresponding | |
1636 | * synthetic interface. The synthetic interface is presented first | |
1637 | * to the guest. When the corresponding VF instance is registered, | |
1638 | * we will take care of switching the data path. | |
1639 | */ | |
1640 | static int netvsc_netdev_event(struct notifier_block *this, | |
1641 | unsigned long event, void *ptr) | |
1642 | { | |
1643 | struct net_device *event_dev = netdev_notifier_info_to_dev(ptr); | |
1644 | ||
ee837a13 SH |
1645 | /* Skip our own events */ |
1646 | if (event_dev->netdev_ops == &device_ops) | |
1647 | return NOTIFY_DONE; | |
1648 | ||
1649 | /* Avoid non-Ethernet type devices */ | |
1650 | if (event_dev->type != ARPHRD_ETHER) | |
1651 | return NOTIFY_DONE; | |
1652 | ||
0dbff144 | 1653 | /* Avoid Vlan dev with same MAC registering as VF */ |
d0d7b10b | 1654 | if (is_vlan_dev(event_dev)) |
0dbff144 VK |
1655 | return NOTIFY_DONE; |
1656 | ||
1657 | /* Avoid Bonding master dev with same MAC registering as VF */ | |
ee837a13 SH |
1658 | if ((event_dev->priv_flags & IFF_BONDING) && |
1659 | (event_dev->flags & IFF_MASTER)) | |
cb2911fe HZ |
1660 | return NOTIFY_DONE; |
1661 | ||
84bf9cef KS |
1662 | switch (event) { |
1663 | case NETDEV_REGISTER: | |
1664 | return netvsc_register_vf(event_dev); | |
1665 | case NETDEV_UNREGISTER: | |
1666 | return netvsc_unregister_vf(event_dev); | |
1667 | case NETDEV_UP: | |
1668 | return netvsc_vf_up(event_dev); | |
1669 | case NETDEV_DOWN: | |
1670 | return netvsc_vf_down(event_dev); | |
1671 | default: | |
1672 | return NOTIFY_DONE; | |
1673 | } | |
1674 | } | |
1675 | ||
1676 | static struct notifier_block netvsc_netdev_notifier = { | |
1677 | .notifier_call = netvsc_netdev_event, | |
1678 | }; | |
1679 | ||
a9869c94 | 1680 | static void __exit netvsc_drv_exit(void) |
fceaf24a | 1681 | { |
84bf9cef | 1682 | unregister_netdevice_notifier(&netvsc_netdev_notifier); |
768fa219 | 1683 | vmbus_driver_unregister(&netvsc_drv); |
fceaf24a HJ |
1684 | } |
1685 | ||
1fde28cf | 1686 | static int __init netvsc_drv_init(void) |
df2fff28 | 1687 | { |
84bf9cef KS |
1688 | int ret; |
1689 | ||
fa85a6c2 HZ |
1690 | if (ring_size < RING_SIZE_MIN) { |
1691 | ring_size = RING_SIZE_MIN; | |
1692 | pr_info("Increased ring_size to %d (min allowed)\n", | |
1693 | ring_size); | |
1694 | } | |
84bf9cef KS |
1695 | ret = vmbus_driver_register(&netvsc_drv); |
1696 | ||
1697 | if (ret) | |
1698 | return ret; | |
1699 | ||
1700 | register_netdevice_notifier(&netvsc_netdev_notifier); | |
1701 | return 0; | |
df2fff28 GKH |
1702 | } |
1703 | ||
26c14cc1 | 1704 | MODULE_LICENSE("GPL"); |
7880fc54 | 1705 | MODULE_DESCRIPTION("Microsoft Hyper-V network driver"); |
fceaf24a | 1706 | |
1fde28cf | 1707 | module_init(netvsc_drv_init); |
a9869c94 | 1708 | module_exit(netvsc_drv_exit); |