]>
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> |
fceaf24a HJ |
36 | #include <net/arp.h> |
37 | #include <net/route.h> | |
38 | #include <net/sock.h> | |
39 | #include <net/pkt_sched.h> | |
8eb1b3c3 MK |
40 | #include <net/checksum.h> |
41 | #include <net/ip6_checksum.h> | |
3f335ea2 | 42 | |
5ca7252a | 43 | #include "hyperv_net.h" |
fceaf24a | 44 | |
fa85a6c2 | 45 | #define RING_SIZE_MIN 64 |
27a70af3 | 46 | #define LINKCHANGE_INT (2 * HZ) |
a50af86d | 47 | |
99c8da0f | 48 | static int ring_size = 128; |
450d7a4b SH |
49 | module_param(ring_size, int, S_IRUGO); |
50 | MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)"); | |
fceaf24a | 51 | |
3f300ff4 SX |
52 | static const u32 default_msg = NETIF_MSG_DRV | NETIF_MSG_PROBE | |
53 | NETIF_MSG_LINK | NETIF_MSG_IFUP | | |
54 | NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR | | |
55 | NETIF_MSG_TX_ERR; | |
56 | ||
57 | static int debug = -1; | |
58 | module_param(debug, int, S_IRUGO); | |
59 | MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)"); | |
60 | ||
4e9bfefa | 61 | static void netvsc_set_multicast_list(struct net_device *net) |
fceaf24a | 62 | { |
792df872 | 63 | struct net_device_context *net_device_ctx = netdev_priv(net); |
4f19c0d8 | 64 | struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev); |
d426b2e3 | 65 | |
4f19c0d8 | 66 | rndis_filter_update(nvdev); |
fceaf24a HJ |
67 | } |
68 | ||
fceaf24a HJ |
69 | static int netvsc_open(struct net_device *net) |
70 | { | |
53fa1a6f HZ |
71 | struct net_device_context *ndev_ctx = netdev_priv(net); |
72 | struct netvsc_device *nvdev = ndev_ctx->nvdev; | |
891de74d | 73 | struct rndis_device *rdev; |
02fafbc6 | 74 | int ret = 0; |
fceaf24a | 75 | |
891de74d HZ |
76 | netif_carrier_off(net); |
77 | ||
d515d0ff | 78 | /* Open up the device */ |
2f5fa6c8 | 79 | ret = rndis_filter_open(nvdev); |
d515d0ff HZ |
80 | if (ret != 0) { |
81 | netdev_err(net, "unable to open device (ret %d).\n", ret); | |
82 | return ret; | |
fceaf24a HJ |
83 | } |
84 | ||
2de8530b | 85 | netif_tx_wake_all_queues(net); |
d515d0ff | 86 | |
891de74d | 87 | rdev = nvdev->extension; |
53fa1a6f | 88 | if (!rdev->link_state && !ndev_ctx->datapath) |
891de74d HZ |
89 | netif_carrier_on(net); |
90 | ||
fceaf24a HJ |
91 | return ret; |
92 | } | |
93 | ||
fceaf24a HJ |
94 | static int netvsc_close(struct net_device *net) |
95 | { | |
fceaf24a | 96 | struct net_device_context *net_device_ctx = netdev_priv(net); |
545a8e79 | 97 | struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev); |
02fafbc6 | 98 | int ret; |
40975962 | 99 | u32 aread, i, msec = 10, retry = 0, retry_max = 20; |
2de8530b | 100 | struct vmbus_channel *chn; |
fceaf24a | 101 | |
0a282538 | 102 | netif_tx_disable(net); |
fceaf24a | 103 | |
2f5fa6c8 | 104 | ret = rndis_filter_close(nvdev); |
2de8530b | 105 | if (ret != 0) { |
eb335bc4 | 106 | netdev_err(net, "unable to close device (ret %d).\n", ret); |
2de8530b HZ |
107 | return ret; |
108 | } | |
109 | ||
110 | /* Ensure pending bytes in ring are read */ | |
111 | while (true) { | |
112 | aread = 0; | |
113 | for (i = 0; i < nvdev->num_chn; i++) { | |
b8b835a8 | 114 | chn = nvdev->chan_table[i].channel; |
2de8530b HZ |
115 | if (!chn) |
116 | continue; | |
117 | ||
40975962 | 118 | aread = hv_get_bytes_to_read(&chn->inbound); |
2de8530b HZ |
119 | if (aread) |
120 | break; | |
121 | ||
40975962 | 122 | aread = hv_get_bytes_to_read(&chn->outbound); |
2de8530b HZ |
123 | if (aread) |
124 | break; | |
125 | } | |
126 | ||
127 | retry++; | |
128 | if (retry > retry_max || aread == 0) | |
129 | break; | |
130 | ||
131 | msleep(msec); | |
132 | ||
133 | if (msec < 1000) | |
134 | msec *= 2; | |
135 | } | |
136 | ||
137 | if (aread) { | |
138 | netdev_err(net, "Ring buffer not empty after closing rndis\n"); | |
139 | ret = -ETIMEDOUT; | |
140 | } | |
fceaf24a | 141 | |
fceaf24a HJ |
142 | return ret; |
143 | } | |
144 | ||
8a00251a KS |
145 | static void *init_ppi_data(struct rndis_message *msg, u32 ppi_size, |
146 | int pkt_type) | |
147 | { | |
148 | struct rndis_packet *rndis_pkt; | |
149 | struct rndis_per_packet_info *ppi; | |
150 | ||
151 | rndis_pkt = &msg->msg.pkt; | |
152 | rndis_pkt->data_offset += ppi_size; | |
153 | ||
154 | ppi = (struct rndis_per_packet_info *)((void *)rndis_pkt + | |
155 | rndis_pkt->per_pkt_info_offset + rndis_pkt->per_pkt_info_len); | |
156 | ||
157 | ppi->size = ppi_size; | |
158 | ppi->type = pkt_type; | |
159 | ppi->ppi_offset = sizeof(struct rndis_per_packet_info); | |
160 | ||
161 | rndis_pkt->per_pkt_info_len += ppi_size; | |
162 | ||
163 | return ppi; | |
164 | } | |
165 | ||
f72860af HZ |
166 | /* Azure hosts don't support non-TCP port numbers in hashing yet. We compute |
167 | * hash for non-TCP traffic with only IP numbers. | |
168 | */ | |
169 | static inline u32 netvsc_get_hash(struct sk_buff *skb, struct sock *sk) | |
170 | { | |
171 | struct flow_keys flow; | |
172 | u32 hash; | |
173 | static u32 hashrnd __read_mostly; | |
174 | ||
175 | net_get_random_once(&hashrnd, sizeof(hashrnd)); | |
176 | ||
177 | if (!skb_flow_dissect_flow_keys(skb, &flow, 0)) | |
178 | return 0; | |
179 | ||
180 | if (flow.basic.ip_proto == IPPROTO_TCP) { | |
181 | return skb_get_hash(skb); | |
182 | } else { | |
183 | if (flow.basic.n_proto == htons(ETH_P_IP)) | |
184 | hash = jhash2((u32 *)&flow.addrs.v4addrs, 2, hashrnd); | |
185 | else if (flow.basic.n_proto == htons(ETH_P_IPV6)) | |
186 | hash = jhash2((u32 *)&flow.addrs.v6addrs, 8, hashrnd); | |
187 | else | |
188 | hash = 0; | |
189 | ||
190 | skb_set_hash(skb, hash, PKT_HASH_TYPE_L3); | |
191 | } | |
192 | ||
193 | return hash; | |
194 | } | |
195 | ||
8db91f6a HZ |
196 | static inline int netvsc_get_tx_queue(struct net_device *ndev, |
197 | struct sk_buff *skb, int old_idx) | |
198 | { | |
199 | const struct net_device_context *ndc = netdev_priv(ndev); | |
200 | struct sock *sk = skb->sk; | |
201 | int q_idx; | |
202 | ||
f72860af | 203 | q_idx = ndc->tx_send_table[netvsc_get_hash(skb, sk) & |
8db91f6a HZ |
204 | (VRSS_SEND_TAB_SIZE - 1)]; |
205 | ||
206 | /* If queue index changed record the new value */ | |
207 | if (q_idx != old_idx && | |
208 | sk && sk_fullsock(sk) && rcu_access_pointer(sk->sk_dst_cache)) | |
209 | sk_tx_queue_set(sk, q_idx); | |
210 | ||
211 | return q_idx; | |
212 | } | |
213 | ||
d8e18ee0 | 214 | /* |
215 | * Select queue for transmit. | |
216 | * | |
217 | * If a valid queue has already been assigned, then use that. | |
218 | * Otherwise compute tx queue based on hash and the send table. | |
219 | * | |
220 | * This is basically similar to default (__netdev_pick_tx) with the added step | |
221 | * of using the host send_table when no other queue has been assigned. | |
222 | * | |
223 | * TODO support XPS - but get_xps_queue not exported | |
224 | */ | |
5b54dac8 HZ |
225 | static u16 netvsc_select_queue(struct net_device *ndev, struct sk_buff *skb, |
226 | void *accel_priv, select_queue_fallback_t fallback) | |
227 | { | |
7ce10124 | 228 | unsigned int num_tx_queues = ndev->real_num_tx_queues; |
8db91f6a HZ |
229 | int q_idx = sk_tx_queue_get(skb->sk); |
230 | ||
231 | if (q_idx < 0 || skb->ooo_okay) { | |
232 | /* If forwarding a packet, we use the recorded queue when | |
233 | * available for better cache locality. | |
234 | */ | |
235 | if (skb_rx_queue_recorded(skb)) | |
236 | q_idx = skb_get_rx_queue(skb); | |
237 | else | |
238 | q_idx = netvsc_get_tx_queue(ndev, skb, q_idx); | |
d8e18ee0 | 239 | } |
5b54dac8 | 240 | |
8db91f6a HZ |
241 | while (unlikely(q_idx >= num_tx_queues)) |
242 | q_idx -= num_tx_queues; | |
243 | ||
5b54dac8 HZ |
244 | return q_idx; |
245 | } | |
246 | ||
54a7357f KS |
247 | static u32 fill_pg_buf(struct page *page, u32 offset, u32 len, |
248 | struct hv_page_buffer *pb) | |
249 | { | |
250 | int j = 0; | |
251 | ||
252 | /* Deal with compund pages by ignoring unused part | |
253 | * of the page. | |
254 | */ | |
255 | page += (offset >> PAGE_SHIFT); | |
256 | offset &= ~PAGE_MASK; | |
257 | ||
258 | while (len > 0) { | |
259 | unsigned long bytes; | |
260 | ||
261 | bytes = PAGE_SIZE - offset; | |
262 | if (bytes > len) | |
263 | bytes = len; | |
264 | pb[j].pfn = page_to_pfn(page); | |
265 | pb[j].offset = offset; | |
266 | pb[j].len = bytes; | |
267 | ||
268 | offset += bytes; | |
269 | len -= bytes; | |
270 | ||
271 | if (offset == PAGE_SIZE && len) { | |
272 | page++; | |
273 | offset = 0; | |
274 | j++; | |
275 | } | |
276 | } | |
277 | ||
278 | return j + 1; | |
279 | } | |
280 | ||
8a00251a | 281 | static u32 init_page_array(void *hdr, u32 len, struct sk_buff *skb, |
a9f2e2d6 KS |
282 | struct hv_netvsc_packet *packet, |
283 | struct hv_page_buffer **page_buf) | |
54a7357f | 284 | { |
a9f2e2d6 | 285 | struct hv_page_buffer *pb = *page_buf; |
54a7357f KS |
286 | u32 slots_used = 0; |
287 | char *data = skb->data; | |
288 | int frags = skb_shinfo(skb)->nr_frags; | |
289 | int i; | |
290 | ||
291 | /* The packet is laid out thus: | |
aa0a34be | 292 | * 1. hdr: RNDIS header and PPI |
54a7357f KS |
293 | * 2. skb linear data |
294 | * 3. skb fragment data | |
295 | */ | |
296 | if (hdr != NULL) | |
297 | slots_used += fill_pg_buf(virt_to_page(hdr), | |
298 | offset_in_page(hdr), | |
299 | len, &pb[slots_used]); | |
300 | ||
aa0a34be HZ |
301 | packet->rmsg_size = len; |
302 | packet->rmsg_pgcnt = slots_used; | |
303 | ||
54a7357f KS |
304 | slots_used += fill_pg_buf(virt_to_page(data), |
305 | offset_in_page(data), | |
306 | skb_headlen(skb), &pb[slots_used]); | |
307 | ||
308 | for (i = 0; i < frags; i++) { | |
309 | skb_frag_t *frag = skb_shinfo(skb)->frags + i; | |
310 | ||
311 | slots_used += fill_pg_buf(skb_frag_page(frag), | |
312 | frag->page_offset, | |
313 | skb_frag_size(frag), &pb[slots_used]); | |
314 | } | |
8a00251a | 315 | return slots_used; |
54a7357f KS |
316 | } |
317 | ||
80d887db | 318 | static int count_skb_frag_slots(struct sk_buff *skb) |
319 | { | |
320 | int i, frags = skb_shinfo(skb)->nr_frags; | |
321 | int pages = 0; | |
322 | ||
323 | for (i = 0; i < frags; i++) { | |
324 | skb_frag_t *frag = skb_shinfo(skb)->frags + i; | |
325 | unsigned long size = skb_frag_size(frag); | |
326 | unsigned long offset = frag->page_offset; | |
327 | ||
328 | /* Skip unused frames from start of page */ | |
329 | offset &= ~PAGE_MASK; | |
330 | pages += PFN_UP(offset + size); | |
331 | } | |
332 | return pages; | |
333 | } | |
334 | ||
335 | static int netvsc_get_slots(struct sk_buff *skb) | |
54a7357f | 336 | { |
80d887db | 337 | char *data = skb->data; |
338 | unsigned int offset = offset_in_page(data); | |
339 | unsigned int len = skb_headlen(skb); | |
340 | int slots; | |
341 | int frag_slots; | |
342 | ||
343 | slots = DIV_ROUND_UP(offset + len, PAGE_SIZE); | |
344 | frag_slots = count_skb_frag_slots(skb); | |
345 | return slots + frag_slots; | |
54a7357f KS |
346 | } |
347 | ||
23312a3b | 348 | static u32 net_checksum_info(struct sk_buff *skb) |
08cd04bf | 349 | { |
23312a3b | 350 | if (skb->protocol == htons(ETH_P_IP)) { |
351 | struct iphdr *ip = ip_hdr(skb); | |
08cd04bf | 352 | |
23312a3b | 353 | if (ip->protocol == IPPROTO_TCP) |
354 | return TRANSPORT_INFO_IPV4_TCP; | |
355 | else if (ip->protocol == IPPROTO_UDP) | |
356 | return TRANSPORT_INFO_IPV4_UDP; | |
08cd04bf | 357 | } else { |
23312a3b | 358 | struct ipv6hdr *ip6 = ipv6_hdr(skb); |
359 | ||
360 | if (ip6->nexthdr == IPPROTO_TCP) | |
361 | return TRANSPORT_INFO_IPV6_TCP; | |
08cd04bf | 362 | else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP) |
23312a3b | 363 | return TRANSPORT_INFO_IPV6_UDP; |
08cd04bf KS |
364 | } |
365 | ||
23312a3b | 366 | return TRANSPORT_INFO_NOT_IP; |
08cd04bf KS |
367 | } |
368 | ||
02fafbc6 | 369 | static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net) |
fceaf24a | 370 | { |
fceaf24a | 371 | struct net_device_context *net_device_ctx = netdev_priv(net); |
981a1bd8 | 372 | struct hv_netvsc_packet *packet = NULL; |
02fafbc6 | 373 | int ret; |
8a00251a KS |
374 | unsigned int num_data_pgs; |
375 | struct rndis_message *rndis_msg; | |
376 | struct rndis_packet *rndis_pkt; | |
377 | u32 rndis_msg_size; | |
8a00251a | 378 | struct rndis_per_packet_info *ppi; |
307f0995 | 379 | u32 hash; |
b08cc791 | 380 | struct hv_page_buffer page_buf[MAX_PAGE_BUFFER_COUNT]; |
a9f2e2d6 | 381 | struct hv_page_buffer *pb = page_buf; |
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, |
a9f2e2d6 | 527 | skb, packet, &pb); |
8a00251a | 528 | |
76d13b56 | 529 | /* timestamp packet in software */ |
530 | skb_tx_timestamp(skb); | |
3a3d9a0a KS |
531 | ret = netvsc_send(net_device_ctx->device_ctx, packet, |
532 | 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 | ||
2b01888d | 739 | static int netvsc_set_queues(struct net_device *net, struct hv_device *dev, |
740 | u32 num_chn) | |
741 | { | |
742 | struct netvsc_device_info device_info; | |
743 | int ret; | |
744 | ||
745 | memset(&device_info, 0, sizeof(device_info)); | |
746 | device_info.num_chn = num_chn; | |
747 | device_info.ring_size = ring_size; | |
748 | device_info.max_num_vrss_chns = num_chn; | |
749 | ||
750 | ret = rndis_filter_device_add(dev, &device_info); | |
751 | if (ret) | |
752 | return ret; | |
753 | ||
754 | ret = netif_set_real_num_tx_queues(net, num_chn); | |
755 | if (ret) | |
756 | return ret; | |
757 | ||
758 | ret = netif_set_real_num_rx_queues(net, num_chn); | |
759 | ||
760 | return ret; | |
761 | } | |
762 | ||
b5960e6e AS |
763 | static int netvsc_set_channels(struct net_device *net, |
764 | struct ethtool_channels *channels) | |
765 | { | |
766 | struct net_device_context *net_device_ctx = netdev_priv(net); | |
767 | struct hv_device *dev = net_device_ctx->device_ctx; | |
545a8e79 | 768 | struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev); |
2b01888d | 769 | unsigned int count = channels->combined_count; |
163891d7 | 770 | bool was_running; |
2b01888d | 771 | int ret; |
772 | ||
773 | /* We do not support separate count for rx, tx, or other */ | |
774 | if (count == 0 || | |
775 | channels->rx_count || channels->tx_count || channels->other_count) | |
776 | return -EINVAL; | |
777 | ||
b92b7d33 | 778 | if (count > net->num_tx_queues || count > VRSS_CHANNEL_MAX) |
2b01888d | 779 | return -EINVAL; |
b5960e6e | 780 | |
a0be450e | 781 | if (!nvdev || nvdev->destroy) |
b5960e6e AS |
782 | return -ENODEV; |
783 | ||
2b01888d | 784 | if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5) |
b5960e6e | 785 | return -EINVAL; |
b5960e6e | 786 | |
2b01888d | 787 | if (count > nvdev->max_chn) |
b5960e6e AS |
788 | return -EINVAL; |
789 | ||
163891d7 | 790 | was_running = netif_running(net); |
791 | if (was_running) { | |
792 | ret = netvsc_close(net); | |
793 | if (ret) | |
794 | return ret; | |
795 | } | |
b5960e6e | 796 | |
2289f0aa | 797 | rndis_filter_device_remove(dev, nvdev); |
b5960e6e | 798 | |
2b01888d | 799 | ret = netvsc_set_queues(net, dev, count); |
800 | if (ret == 0) | |
801 | nvdev->num_chn = count; | |
802 | else | |
803 | netvsc_set_queues(net, dev, nvdev->num_chn); | |
b5960e6e | 804 | |
163891d7 | 805 | if (was_running) |
806 | ret = netvsc_open(net); | |
807 | ||
1bdcec8a VK |
808 | /* We may have missed link change notifications */ |
809 | schedule_delayed_work(&net_device_ctx->dwork, 0); | |
b5960e6e AS |
810 | |
811 | return ret; | |
b5960e6e AS |
812 | } |
813 | ||
5e8456fd PR |
814 | static bool |
815 | netvsc_validate_ethtool_ss_cmd(const struct ethtool_link_ksettings *cmd) | |
49eb9389 | 816 | { |
5e8456fd PR |
817 | struct ethtool_link_ksettings diff1 = *cmd; |
818 | struct ethtool_link_ksettings diff2 = {}; | |
49eb9389 | 819 | |
5e8456fd PR |
820 | diff1.base.speed = 0; |
821 | diff1.base.duplex = 0; | |
49eb9389 | 822 | /* advertising and cmd are usually set */ |
5e8456fd PR |
823 | ethtool_link_ksettings_zero_link_mode(&diff1, advertising); |
824 | diff1.base.cmd = 0; | |
49eb9389 | 825 | /* We set port to PORT_OTHER */ |
5e8456fd | 826 | diff2.base.port = PORT_OTHER; |
49eb9389 | 827 | |
828 | return !memcmp(&diff1, &diff2, sizeof(diff1)); | |
829 | } | |
830 | ||
831 | static void netvsc_init_settings(struct net_device *dev) | |
832 | { | |
833 | struct net_device_context *ndc = netdev_priv(dev); | |
834 | ||
835 | ndc->speed = SPEED_UNKNOWN; | |
f3c9d40e | 836 | ndc->duplex = DUPLEX_FULL; |
49eb9389 | 837 | } |
838 | ||
5e8456fd PR |
839 | static int netvsc_get_link_ksettings(struct net_device *dev, |
840 | struct ethtool_link_ksettings *cmd) | |
49eb9389 | 841 | { |
842 | struct net_device_context *ndc = netdev_priv(dev); | |
843 | ||
5e8456fd PR |
844 | cmd->base.speed = ndc->speed; |
845 | cmd->base.duplex = ndc->duplex; | |
846 | cmd->base.port = PORT_OTHER; | |
49eb9389 | 847 | |
848 | return 0; | |
849 | } | |
850 | ||
5e8456fd PR |
851 | static int netvsc_set_link_ksettings(struct net_device *dev, |
852 | const struct ethtool_link_ksettings *cmd) | |
49eb9389 | 853 | { |
854 | struct net_device_context *ndc = netdev_priv(dev); | |
855 | u32 speed; | |
856 | ||
5e8456fd | 857 | speed = cmd->base.speed; |
49eb9389 | 858 | if (!ethtool_validate_speed(speed) || |
5e8456fd | 859 | !ethtool_validate_duplex(cmd->base.duplex) || |
49eb9389 | 860 | !netvsc_validate_ethtool_ss_cmd(cmd)) |
861 | return -EINVAL; | |
862 | ||
863 | ndc->speed = speed; | |
5e8456fd | 864 | ndc->duplex = cmd->base.duplex; |
49eb9389 | 865 | |
866 | return 0; | |
867 | } | |
868 | ||
4d447c9a HZ |
869 | static int netvsc_change_mtu(struct net_device *ndev, int mtu) |
870 | { | |
871 | struct net_device_context *ndevctx = netdev_priv(ndev); | |
545a8e79 | 872 | struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev); |
3d541ac5 | 873 | struct hv_device *hdev = ndevctx->device_ctx; |
4d447c9a | 874 | struct netvsc_device_info device_info; |
163891d7 | 875 | bool was_running; |
386f5762 | 876 | int ret = 0; |
4d447c9a | 877 | |
a0be450e | 878 | if (!nvdev || nvdev->destroy) |
4d447c9a HZ |
879 | return -ENODEV; |
880 | ||
163891d7 | 881 | was_running = netif_running(ndev); |
882 | if (was_running) { | |
883 | ret = netvsc_close(ndev); | |
884 | if (ret) | |
885 | return ret; | |
886 | } | |
2de8530b | 887 | |
152669bd DC |
888 | memset(&device_info, 0, sizeof(device_info)); |
889 | device_info.ring_size = ring_size; | |
890 | device_info.num_chn = nvdev->num_chn; | |
891 | device_info.max_num_vrss_chns = nvdev->num_chn; | |
892 | ||
2289f0aa | 893 | rndis_filter_device_remove(hdev, nvdev); |
4d447c9a | 894 | |
152669bd DC |
895 | /* 'nvdev' has been freed in rndis_filter_device_remove() -> |
896 | * netvsc_device_remove () -> free_netvsc_device(). | |
897 | * We mustn't access it before it's re-created in | |
898 | * rndis_filter_device_add() -> netvsc_device_add(). | |
899 | */ | |
900 | ||
4d447c9a HZ |
901 | ndev->mtu = mtu; |
902 | ||
4d447c9a | 903 | rndis_filter_device_add(hdev, &device_info); |
4d447c9a | 904 | |
163891d7 | 905 | if (was_running) |
906 | ret = netvsc_open(ndev); | |
907 | ||
1bdcec8a VK |
908 | /* We may have missed link change notifications */ |
909 | schedule_delayed_work(&ndevctx->dwork, 0); | |
910 | ||
2de8530b | 911 | return ret; |
4d447c9a HZ |
912 | } |
913 | ||
bc1f4470 | 914 | static void netvsc_get_stats64(struct net_device *net, |
915 | struct rtnl_link_stats64 *t) | |
7eafd9b4 | 916 | { |
917 | struct net_device_context *ndev_ctx = netdev_priv(net); | |
776e726b | 918 | struct netvsc_device *nvdev = rcu_dereference_rtnl(ndev_ctx->nvdev); |
6c80f3fc SX |
919 | int i; |
920 | ||
921 | if (!nvdev) | |
922 | return; | |
923 | ||
924 | for (i = 0; i < nvdev->num_chn; i++) { | |
925 | const struct netvsc_channel *nvchan = &nvdev->chan_table[i]; | |
926 | const struct netvsc_stats *stats; | |
927 | u64 packets, bytes, multicast; | |
7eafd9b4 | 928 | unsigned int start; |
929 | ||
6c80f3fc | 930 | stats = &nvchan->tx_stats; |
7eafd9b4 | 931 | do { |
6c80f3fc SX |
932 | start = u64_stats_fetch_begin_irq(&stats->syncp); |
933 | packets = stats->packets; | |
934 | bytes = stats->bytes; | |
935 | } while (u64_stats_fetch_retry_irq(&stats->syncp, start)); | |
936 | ||
937 | t->tx_bytes += bytes; | |
938 | t->tx_packets += packets; | |
7eafd9b4 | 939 | |
6c80f3fc | 940 | stats = &nvchan->rx_stats; |
7eafd9b4 | 941 | do { |
6c80f3fc SX |
942 | start = u64_stats_fetch_begin_irq(&stats->syncp); |
943 | packets = stats->packets; | |
944 | bytes = stats->bytes; | |
945 | multicast = stats->multicast + stats->broadcast; | |
946 | } while (u64_stats_fetch_retry_irq(&stats->syncp, start)); | |
947 | ||
948 | t->rx_bytes += bytes; | |
949 | t->rx_packets += packets; | |
950 | t->multicast += multicast; | |
7eafd9b4 | 951 | } |
952 | ||
953 | t->tx_dropped = net->stats.tx_dropped; | |
b5124720 | 954 | t->tx_errors = net->stats.tx_errors; |
7eafd9b4 | 955 | |
956 | t->rx_dropped = net->stats.rx_dropped; | |
957 | t->rx_errors = net->stats.rx_errors; | |
7eafd9b4 | 958 | } |
1ce09e89 HZ |
959 | |
960 | static int netvsc_set_mac_addr(struct net_device *ndev, void *p) | |
961 | { | |
1ce09e89 | 962 | struct sockaddr *addr = p; |
9a4c831e | 963 | char save_adr[ETH_ALEN]; |
1ce09e89 HZ |
964 | unsigned char save_aatype; |
965 | int err; | |
966 | ||
967 | memcpy(save_adr, ndev->dev_addr, ETH_ALEN); | |
968 | save_aatype = ndev->addr_assign_type; | |
969 | ||
970 | err = eth_mac_addr(ndev, p); | |
971 | if (err != 0) | |
972 | return err; | |
973 | ||
e834da9a | 974 | err = rndis_filter_set_device_mac(ndev, addr->sa_data); |
1ce09e89 HZ |
975 | if (err != 0) { |
976 | /* roll back to saved MAC */ | |
977 | memcpy(ndev->dev_addr, save_adr, ETH_ALEN); | |
978 | ndev->addr_assign_type = save_aatype; | |
979 | } | |
980 | ||
981 | return err; | |
982 | } | |
983 | ||
4323b47c SH |
984 | static const struct { |
985 | char name[ETH_GSTRING_LEN]; | |
986 | u16 offset; | |
987 | } netvsc_stats[] = { | |
988 | { "tx_scattered", offsetof(struct netvsc_ethtool_stats, tx_scattered) }, | |
989 | { "tx_no_memory", offsetof(struct netvsc_ethtool_stats, tx_no_memory) }, | |
990 | { "tx_no_space", offsetof(struct netvsc_ethtool_stats, tx_no_space) }, | |
991 | { "tx_too_big", offsetof(struct netvsc_ethtool_stats, tx_too_big) }, | |
992 | { "tx_busy", offsetof(struct netvsc_ethtool_stats, tx_busy) }, | |
993 | }; | |
994 | ||
6c80f3fc SX |
995 | #define NETVSC_GLOBAL_STATS_LEN ARRAY_SIZE(netvsc_stats) |
996 | ||
997 | /* 4 statistics per queue (rx/tx packets/bytes) */ | |
998 | #define NETVSC_QUEUE_STATS_LEN(dev) ((dev)->num_chn * 4) | |
999 | ||
4323b47c SH |
1000 | static int netvsc_get_sset_count(struct net_device *dev, int string_set) |
1001 | { | |
6c80f3fc | 1002 | struct net_device_context *ndc = netdev_priv(dev); |
fbd4c7e7 | 1003 | struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev); |
545a8e79 | 1004 | |
1005 | if (!nvdev) | |
1006 | return -ENODEV; | |
6c80f3fc | 1007 | |
4323b47c SH |
1008 | switch (string_set) { |
1009 | case ETH_SS_STATS: | |
6c80f3fc | 1010 | return NETVSC_GLOBAL_STATS_LEN + NETVSC_QUEUE_STATS_LEN(nvdev); |
4323b47c SH |
1011 | default: |
1012 | return -EINVAL; | |
1013 | } | |
1014 | } | |
1015 | ||
1016 | static void netvsc_get_ethtool_stats(struct net_device *dev, | |
1017 | struct ethtool_stats *stats, u64 *data) | |
1018 | { | |
1019 | struct net_device_context *ndc = netdev_priv(dev); | |
545a8e79 | 1020 | struct netvsc_device *nvdev = rcu_dereference(ndc->nvdev); |
4323b47c | 1021 | const void *nds = &ndc->eth_stats; |
6c80f3fc SX |
1022 | const struct netvsc_stats *qstats; |
1023 | unsigned int start; | |
1024 | u64 packets, bytes; | |
1025 | int i, j; | |
4323b47c | 1026 | |
545a8e79 | 1027 | if (!nvdev) |
1028 | return; | |
1029 | ||
6c80f3fc | 1030 | for (i = 0; i < NETVSC_GLOBAL_STATS_LEN; i++) |
4323b47c | 1031 | data[i] = *(unsigned long *)(nds + netvsc_stats[i].offset); |
6c80f3fc SX |
1032 | |
1033 | for (j = 0; j < nvdev->num_chn; j++) { | |
1034 | qstats = &nvdev->chan_table[j].tx_stats; | |
1035 | ||
1036 | do { | |
1037 | start = u64_stats_fetch_begin_irq(&qstats->syncp); | |
1038 | packets = qstats->packets; | |
1039 | bytes = qstats->bytes; | |
1040 | } while (u64_stats_fetch_retry_irq(&qstats->syncp, start)); | |
1041 | data[i++] = packets; | |
1042 | data[i++] = bytes; | |
1043 | ||
1044 | qstats = &nvdev->chan_table[j].rx_stats; | |
1045 | do { | |
1046 | start = u64_stats_fetch_begin_irq(&qstats->syncp); | |
1047 | packets = qstats->packets; | |
1048 | bytes = qstats->bytes; | |
1049 | } while (u64_stats_fetch_retry_irq(&qstats->syncp, start)); | |
1050 | data[i++] = packets; | |
1051 | data[i++] = bytes; | |
1052 | } | |
4323b47c SH |
1053 | } |
1054 | ||
1055 | static void netvsc_get_strings(struct net_device *dev, u32 stringset, u8 *data) | |
1056 | { | |
6c80f3fc | 1057 | struct net_device_context *ndc = netdev_priv(dev); |
545a8e79 | 1058 | struct netvsc_device *nvdev = rcu_dereference(ndc->nvdev); |
6c80f3fc | 1059 | u8 *p = data; |
4323b47c SH |
1060 | int i; |
1061 | ||
545a8e79 | 1062 | if (!nvdev) |
1063 | return; | |
1064 | ||
4323b47c SH |
1065 | switch (stringset) { |
1066 | case ETH_SS_STATS: | |
1067 | for (i = 0; i < ARRAY_SIZE(netvsc_stats); i++) | |
6c80f3fc | 1068 | memcpy(p + i * ETH_GSTRING_LEN, |
4323b47c | 1069 | netvsc_stats[i].name, ETH_GSTRING_LEN); |
6c80f3fc SX |
1070 | |
1071 | p += i * ETH_GSTRING_LEN; | |
1072 | for (i = 0; i < nvdev->num_chn; i++) { | |
1073 | sprintf(p, "tx_queue_%u_packets", i); | |
1074 | p += ETH_GSTRING_LEN; | |
1075 | sprintf(p, "tx_queue_%u_bytes", i); | |
1076 | p += ETH_GSTRING_LEN; | |
1077 | sprintf(p, "rx_queue_%u_packets", i); | |
1078 | p += ETH_GSTRING_LEN; | |
1079 | sprintf(p, "rx_queue_%u_bytes", i); | |
1080 | p += ETH_GSTRING_LEN; | |
1081 | } | |
1082 | ||
4323b47c SH |
1083 | break; |
1084 | } | |
1085 | } | |
1086 | ||
b5a5dc8d | 1087 | static int |
1088 | netvsc_get_rss_hash_opts(struct netvsc_device *nvdev, | |
1089 | struct ethtool_rxnfc *info) | |
1090 | { | |
1091 | info->data = RXH_IP_SRC | RXH_IP_DST; | |
1092 | ||
1093 | switch (info->flow_type) { | |
1094 | case TCP_V4_FLOW: | |
1095 | case TCP_V6_FLOW: | |
1096 | info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3; | |
1097 | /* fallthrough */ | |
1098 | case UDP_V4_FLOW: | |
1099 | case UDP_V6_FLOW: | |
1100 | case IPV4_FLOW: | |
1101 | case IPV6_FLOW: | |
1102 | break; | |
1103 | default: | |
1104 | info->data = 0; | |
1105 | break; | |
1106 | } | |
1107 | ||
1108 | return 0; | |
1109 | } | |
1110 | ||
b448f4e8 | 1111 | static int |
1112 | netvsc_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info, | |
1113 | u32 *rules) | |
1114 | { | |
1115 | struct net_device_context *ndc = netdev_priv(dev); | |
545a8e79 | 1116 | struct netvsc_device *nvdev = rcu_dereference(ndc->nvdev); |
1117 | ||
1118 | if (!nvdev) | |
1119 | return -ENODEV; | |
b448f4e8 | 1120 | |
1121 | switch (info->cmd) { | |
1122 | case ETHTOOL_GRXRINGS: | |
1123 | info->data = nvdev->num_chn; | |
1124 | return 0; | |
b5a5dc8d | 1125 | |
1126 | case ETHTOOL_GRXFH: | |
1127 | return netvsc_get_rss_hash_opts(nvdev, info); | |
b448f4e8 | 1128 | } |
1129 | return -EOPNOTSUPP; | |
1130 | } | |
1131 | ||
316158fe | 1132 | #ifdef CONFIG_NET_POLL_CONTROLLER |
a5ecd439 | 1133 | static void netvsc_poll_controller(struct net_device *dev) |
316158fe | 1134 | { |
a5ecd439 | 1135 | struct net_device_context *ndc = netdev_priv(dev); |
1136 | struct netvsc_device *ndev; | |
1137 | int i; | |
1138 | ||
1139 | rcu_read_lock(); | |
1140 | ndev = rcu_dereference(ndc->nvdev); | |
1141 | if (ndev) { | |
1142 | for (i = 0; i < ndev->num_chn; i++) { | |
1143 | struct netvsc_channel *nvchan = &ndev->chan_table[i]; | |
1144 | ||
1145 | napi_schedule(&nvchan->napi); | |
1146 | } | |
1147 | } | |
1148 | rcu_read_unlock(); | |
316158fe RW |
1149 | } |
1150 | #endif | |
1ce09e89 | 1151 | |
962f3fee | 1152 | static u32 netvsc_get_rxfh_key_size(struct net_device *dev) |
1153 | { | |
1154 | return NETVSC_HASH_KEYLEN; | |
1155 | } | |
1156 | ||
1157 | static u32 netvsc_rss_indir_size(struct net_device *dev) | |
1158 | { | |
ff4a4419 | 1159 | return ITAB_NUM; |
962f3fee | 1160 | } |
1161 | ||
1162 | static int netvsc_get_rxfh(struct net_device *dev, u32 *indir, u8 *key, | |
1163 | u8 *hfunc) | |
1164 | { | |
1165 | struct net_device_context *ndc = netdev_priv(dev); | |
545a8e79 | 1166 | struct netvsc_device *ndev = rcu_dereference(ndc->nvdev); |
eb996edb | 1167 | struct rndis_device *rndis_dev; |
ff4a4419 | 1168 | int i; |
962f3fee | 1169 | |
545a8e79 | 1170 | if (!ndev) |
1171 | return -ENODEV; | |
1172 | ||
962f3fee | 1173 | if (hfunc) |
1174 | *hfunc = ETH_RSS_HASH_TOP; /* Toeplitz */ | |
1175 | ||
eb996edb | 1176 | rndis_dev = ndev->extension; |
ff4a4419 | 1177 | if (indir) { |
1178 | for (i = 0; i < ITAB_NUM; i++) | |
1179 | indir[i] = rndis_dev->ind_table[i]; | |
1180 | } | |
1181 | ||
962f3fee | 1182 | if (key) |
1183 | memcpy(key, rndis_dev->rss_key, NETVSC_HASH_KEYLEN); | |
1184 | ||
1185 | return 0; | |
1186 | } | |
1187 | ||
1188 | static int netvsc_set_rxfh(struct net_device *dev, const u32 *indir, | |
1189 | const u8 *key, const u8 hfunc) | |
1190 | { | |
1191 | struct net_device_context *ndc = netdev_priv(dev); | |
545a8e79 | 1192 | struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev); |
eb996edb | 1193 | struct rndis_device *rndis_dev; |
ff4a4419 | 1194 | int i; |
962f3fee | 1195 | |
545a8e79 | 1196 | if (!ndev) |
1197 | return -ENODEV; | |
1198 | ||
962f3fee | 1199 | if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP) |
1200 | return -EOPNOTSUPP; | |
1201 | ||
eb996edb | 1202 | rndis_dev = ndev->extension; |
ff4a4419 | 1203 | if (indir) { |
1204 | for (i = 0; i < ITAB_NUM; i++) | |
b92b7d33 | 1205 | if (indir[i] >= VRSS_CHANNEL_MAX) |
ff4a4419 | 1206 | return -EINVAL; |
1207 | ||
1208 | for (i = 0; i < ITAB_NUM; i++) | |
1209 | rndis_dev->ind_table[i] = indir[i]; | |
1210 | } | |
1211 | ||
1212 | if (!key) { | |
1213 | if (!indir) | |
1214 | return 0; | |
1215 | ||
1216 | key = rndis_dev->rss_key; | |
1217 | } | |
962f3fee | 1218 | |
1219 | return rndis_filter_set_rss_param(rndis_dev, key, ndev->num_chn); | |
1220 | } | |
1221 | ||
f82f4ad7 SH |
1222 | static const struct ethtool_ops ethtool_ops = { |
1223 | .get_drvinfo = netvsc_get_drvinfo, | |
f82f4ad7 | 1224 | .get_link = ethtool_op_get_link, |
4323b47c SH |
1225 | .get_ethtool_stats = netvsc_get_ethtool_stats, |
1226 | .get_sset_count = netvsc_get_sset_count, | |
1227 | .get_strings = netvsc_get_strings, | |
59995370 | 1228 | .get_channels = netvsc_get_channels, |
b5960e6e | 1229 | .set_channels = netvsc_set_channels, |
76d13b56 | 1230 | .get_ts_info = ethtool_op_get_ts_info, |
b448f4e8 | 1231 | .get_rxnfc = netvsc_get_rxnfc, |
962f3fee | 1232 | .get_rxfh_key_size = netvsc_get_rxfh_key_size, |
1233 | .get_rxfh_indir_size = netvsc_rss_indir_size, | |
1234 | .get_rxfh = netvsc_get_rxfh, | |
1235 | .set_rxfh = netvsc_set_rxfh, | |
5e8456fd PR |
1236 | .get_link_ksettings = netvsc_get_link_ksettings, |
1237 | .set_link_ksettings = netvsc_set_link_ksettings, | |
f82f4ad7 SH |
1238 | }; |
1239 | ||
df2fff28 GKH |
1240 | static const struct net_device_ops device_ops = { |
1241 | .ndo_open = netvsc_open, | |
1242 | .ndo_stop = netvsc_close, | |
1243 | .ndo_start_xmit = netvsc_start_xmit, | |
afc4b13d | 1244 | .ndo_set_rx_mode = netvsc_set_multicast_list, |
4d447c9a | 1245 | .ndo_change_mtu = netvsc_change_mtu, |
b681b588 | 1246 | .ndo_validate_addr = eth_validate_addr, |
1ce09e89 | 1247 | .ndo_set_mac_address = netvsc_set_mac_addr, |
5b54dac8 | 1248 | .ndo_select_queue = netvsc_select_queue, |
7eafd9b4 | 1249 | .ndo_get_stats64 = netvsc_get_stats64, |
316158fe RW |
1250 | #ifdef CONFIG_NET_POLL_CONTROLLER |
1251 | .ndo_poll_controller = netvsc_poll_controller, | |
1252 | #endif | |
df2fff28 GKH |
1253 | }; |
1254 | ||
c996edcf | 1255 | /* |
27a70af3 VK |
1256 | * Handle link status changes. For RNDIS_STATUS_NETWORK_CHANGE emulate link |
1257 | * down/up sequence. In case of RNDIS_STATUS_MEDIA_CONNECT when carrier is | |
1258 | * present send GARP packet to network peers with netif_notify_peers(). | |
c996edcf | 1259 | */ |
891de74d | 1260 | static void netvsc_link_change(struct work_struct *w) |
c996edcf | 1261 | { |
0a1275ca VK |
1262 | struct net_device_context *ndev_ctx = |
1263 | container_of(w, struct net_device_context, dwork.work); | |
1264 | struct hv_device *device_obj = ndev_ctx->device_ctx; | |
1265 | struct net_device *net = hv_get_drvdata(device_obj); | |
2ddd5e5f | 1266 | struct netvsc_device *net_device; |
891de74d | 1267 | struct rndis_device *rdev; |
27a70af3 VK |
1268 | struct netvsc_reconfig *event = NULL; |
1269 | bool notify = false, reschedule = false; | |
1270 | unsigned long flags, next_reconfig, delay; | |
c996edcf | 1271 | |
1bdcec8a | 1272 | rtnl_lock(); |
a0be450e | 1273 | net_device = rtnl_dereference(ndev_ctx->nvdev); |
1274 | if (!net_device) | |
1bdcec8a VK |
1275 | goto out_unlock; |
1276 | ||
891de74d | 1277 | rdev = net_device->extension; |
891de74d | 1278 | |
27a70af3 VK |
1279 | next_reconfig = ndev_ctx->last_reconfig + LINKCHANGE_INT; |
1280 | if (time_is_after_jiffies(next_reconfig)) { | |
1281 | /* link_watch only sends one notification with current state | |
1282 | * per second, avoid doing reconfig more frequently. Handle | |
1283 | * wrap around. | |
1284 | */ | |
1285 | delay = next_reconfig - jiffies; | |
1286 | delay = delay < LINKCHANGE_INT ? delay : LINKCHANGE_INT; | |
1287 | schedule_delayed_work(&ndev_ctx->dwork, delay); | |
1bdcec8a | 1288 | goto out_unlock; |
27a70af3 VK |
1289 | } |
1290 | ndev_ctx->last_reconfig = jiffies; | |
1291 | ||
1292 | spin_lock_irqsave(&ndev_ctx->lock, flags); | |
1293 | if (!list_empty(&ndev_ctx->reconfig_events)) { | |
1294 | event = list_first_entry(&ndev_ctx->reconfig_events, | |
1295 | struct netvsc_reconfig, list); | |
1296 | list_del(&event->list); | |
1297 | reschedule = !list_empty(&ndev_ctx->reconfig_events); | |
1298 | } | |
1299 | spin_unlock_irqrestore(&ndev_ctx->lock, flags); | |
1300 | ||
1301 | if (!event) | |
1bdcec8a | 1302 | goto out_unlock; |
27a70af3 VK |
1303 | |
1304 | switch (event->event) { | |
1305 | /* Only the following events are possible due to the check in | |
1306 | * netvsc_linkstatus_callback() | |
1307 | */ | |
1308 | case RNDIS_STATUS_MEDIA_CONNECT: | |
1309 | if (rdev->link_state) { | |
1310 | rdev->link_state = false; | |
53fa1a6f HZ |
1311 | if (!ndev_ctx->datapath) |
1312 | netif_carrier_on(net); | |
27a70af3 VK |
1313 | netif_tx_wake_all_queues(net); |
1314 | } else { | |
1315 | notify = true; | |
1316 | } | |
1317 | kfree(event); | |
1318 | break; | |
1319 | case RNDIS_STATUS_MEDIA_DISCONNECT: | |
1320 | if (!rdev->link_state) { | |
1321 | rdev->link_state = true; | |
1322 | netif_carrier_off(net); | |
1323 | netif_tx_stop_all_queues(net); | |
1324 | } | |
1325 | kfree(event); | |
1326 | break; | |
1327 | case RNDIS_STATUS_NETWORK_CHANGE: | |
1328 | /* Only makes sense if carrier is present */ | |
1329 | if (!rdev->link_state) { | |
1330 | rdev->link_state = true; | |
1331 | netif_carrier_off(net); | |
1332 | netif_tx_stop_all_queues(net); | |
1333 | event->event = RNDIS_STATUS_MEDIA_CONNECT; | |
1334 | spin_lock_irqsave(&ndev_ctx->lock, flags); | |
15cfd407 | 1335 | list_add(&event->list, &ndev_ctx->reconfig_events); |
27a70af3 VK |
1336 | spin_unlock_irqrestore(&ndev_ctx->lock, flags); |
1337 | reschedule = true; | |
3a494e71 | 1338 | } |
27a70af3 | 1339 | break; |
891de74d HZ |
1340 | } |
1341 | ||
1342 | rtnl_unlock(); | |
1343 | ||
1344 | if (notify) | |
1345 | netdev_notify_peers(net); | |
27a70af3 VK |
1346 | |
1347 | /* link_watch only sends one notification with current state per | |
1348 | * second, handle next reconfig event in 2 seconds. | |
1349 | */ | |
1350 | if (reschedule) | |
1351 | schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT); | |
1bdcec8a VK |
1352 | |
1353 | return; | |
1354 | ||
1355 | out_unlock: | |
1356 | rtnl_unlock(); | |
c996edcf HZ |
1357 | } |
1358 | ||
e8ff40d4 | 1359 | static struct net_device *get_netvsc_bymac(const u8 *mac) |
84bf9cef | 1360 | { |
e8ff40d4 | 1361 | struct net_device *dev; |
84bf9cef | 1362 | |
8737caaf | 1363 | ASSERT_RTNL(); |
84bf9cef KS |
1364 | |
1365 | for_each_netdev(&init_net, dev) { | |
e8ff40d4 SH |
1366 | if (dev->netdev_ops != &device_ops) |
1367 | continue; /* not a netvsc device */ | |
1368 | ||
1369 | if (ether_addr_equal(mac, dev->perm_addr)) | |
1370 | return dev; | |
1371 | } | |
1372 | ||
1373 | return NULL; | |
1374 | } | |
1375 | ||
f207c10d | 1376 | static struct net_device *get_netvsc_byref(struct net_device *vf_netdev) |
e8ff40d4 SH |
1377 | { |
1378 | struct net_device *dev; | |
1379 | ||
1380 | ASSERT_RTNL(); | |
1381 | ||
1382 | for_each_netdev(&init_net, dev) { | |
1383 | struct net_device_context *net_device_ctx; | |
1384 | ||
1385 | if (dev->netdev_ops != &device_ops) | |
1386 | continue; /* not a netvsc device */ | |
1387 | ||
1388 | net_device_ctx = netdev_priv(dev); | |
1389 | if (net_device_ctx->nvdev == NULL) | |
1390 | continue; /* device is removed */ | |
1391 | ||
f207c10d | 1392 | if (rtnl_dereference(net_device_ctx->vf_netdev) == vf_netdev) |
e8ff40d4 | 1393 | return dev; /* a match */ |
84bf9cef | 1394 | } |
84bf9cef | 1395 | |
e8ff40d4 | 1396 | return NULL; |
84bf9cef KS |
1397 | } |
1398 | ||
1399 | static int netvsc_register_vf(struct net_device *vf_netdev) | |
1400 | { | |
0a1275ca VK |
1401 | struct net_device *ndev; |
1402 | struct net_device_context *net_device_ctx; | |
84bf9cef | 1403 | struct netvsc_device *netvsc_dev; |
84bf9cef | 1404 | |
e8ff40d4 SH |
1405 | if (vf_netdev->addr_len != ETH_ALEN) |
1406 | return NOTIFY_DONE; | |
1407 | ||
84bf9cef KS |
1408 | /* |
1409 | * We will use the MAC address to locate the synthetic interface to | |
1410 | * associate with the VF interface. If we don't find a matching | |
1411 | * synthetic interface, move on. | |
1412 | */ | |
e8ff40d4 | 1413 | ndev = get_netvsc_bymac(vf_netdev->perm_addr); |
0a1275ca VK |
1414 | if (!ndev) |
1415 | return NOTIFY_DONE; | |
1416 | ||
1417 | net_device_ctx = netdev_priv(ndev); | |
545a8e79 | 1418 | netvsc_dev = rtnl_dereference(net_device_ctx->nvdev); |
f207c10d | 1419 | if (!netvsc_dev || rtnl_dereference(net_device_ctx->vf_netdev)) |
84bf9cef KS |
1420 | return NOTIFY_DONE; |
1421 | ||
0a1275ca | 1422 | netdev_info(ndev, "VF registering: %s\n", vf_netdev->name); |
84bf9cef KS |
1423 | /* |
1424 | * Take a reference on the module. | |
1425 | */ | |
1426 | try_module_get(THIS_MODULE); | |
07d0f000 SH |
1427 | |
1428 | dev_hold(vf_netdev); | |
f207c10d | 1429 | rcu_assign_pointer(net_device_ctx->vf_netdev, vf_netdev); |
84bf9cef KS |
1430 | return NOTIFY_OK; |
1431 | } | |
1432 | ||
84bf9cef KS |
1433 | static int netvsc_vf_up(struct net_device *vf_netdev) |
1434 | { | |
0a1275ca | 1435 | struct net_device *ndev; |
84bf9cef | 1436 | struct netvsc_device *netvsc_dev; |
84bf9cef KS |
1437 | struct net_device_context *net_device_ctx; |
1438 | ||
e8ff40d4 | 1439 | ndev = get_netvsc_byref(vf_netdev); |
0a1275ca VK |
1440 | if (!ndev) |
1441 | return NOTIFY_DONE; | |
1442 | ||
1443 | net_device_ctx = netdev_priv(ndev); | |
545a8e79 | 1444 | netvsc_dev = rtnl_dereference(net_device_ctx->nvdev); |
84bf9cef | 1445 | |
0a1275ca | 1446 | netdev_info(ndev, "VF up: %s\n", vf_netdev->name); |
84bf9cef KS |
1447 | |
1448 | /* | |
1449 | * Open the device before switching data path. | |
1450 | */ | |
2f5fa6c8 | 1451 | rndis_filter_open(netvsc_dev); |
84bf9cef KS |
1452 | |
1453 | /* | |
1454 | * notify the host to switch the data path. | |
1455 | */ | |
0a1275ca VK |
1456 | netvsc_switch_datapath(ndev, true); |
1457 | netdev_info(ndev, "Data path switched to VF: %s\n", vf_netdev->name); | |
84bf9cef | 1458 | |
0a1275ca | 1459 | netif_carrier_off(ndev); |
84bf9cef | 1460 | |
d072218f VK |
1461 | /* Now notify peers through VF device. */ |
1462 | call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, vf_netdev); | |
84bf9cef KS |
1463 | |
1464 | return NOTIFY_OK; | |
1465 | } | |
1466 | ||
84bf9cef KS |
1467 | static int netvsc_vf_down(struct net_device *vf_netdev) |
1468 | { | |
0a1275ca | 1469 | struct net_device *ndev; |
84bf9cef KS |
1470 | struct netvsc_device *netvsc_dev; |
1471 | struct net_device_context *net_device_ctx; | |
84bf9cef | 1472 | |
e8ff40d4 | 1473 | ndev = get_netvsc_byref(vf_netdev); |
0a1275ca VK |
1474 | if (!ndev) |
1475 | return NOTIFY_DONE; | |
1476 | ||
1477 | net_device_ctx = netdev_priv(ndev); | |
545a8e79 | 1478 | netvsc_dev = rtnl_dereference(net_device_ctx->nvdev); |
84bf9cef | 1479 | |
0a1275ca | 1480 | netdev_info(ndev, "VF down: %s\n", vf_netdev->name); |
0a1275ca VK |
1481 | netvsc_switch_datapath(ndev, false); |
1482 | netdev_info(ndev, "Data path switched from VF: %s\n", vf_netdev->name); | |
2f5fa6c8 | 1483 | rndis_filter_close(netvsc_dev); |
0a1275ca | 1484 | netif_carrier_on(ndev); |
d072218f VK |
1485 | |
1486 | /* Now notify peers through netvsc device. */ | |
1487 | call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, ndev); | |
84bf9cef KS |
1488 | |
1489 | return NOTIFY_OK; | |
1490 | } | |
1491 | ||
84bf9cef KS |
1492 | static int netvsc_unregister_vf(struct net_device *vf_netdev) |
1493 | { | |
0a1275ca | 1494 | struct net_device *ndev; |
0a1275ca | 1495 | struct net_device_context *net_device_ctx; |
84bf9cef | 1496 | |
e8ff40d4 | 1497 | ndev = get_netvsc_byref(vf_netdev); |
0a1275ca VK |
1498 | if (!ndev) |
1499 | return NOTIFY_DONE; | |
1500 | ||
1501 | net_device_ctx = netdev_priv(ndev); | |
e8ff40d4 | 1502 | |
0a1275ca | 1503 | netdev_info(ndev, "VF unregistering: %s\n", vf_netdev->name); |
f207c10d SH |
1504 | |
1505 | RCU_INIT_POINTER(net_device_ctx->vf_netdev, NULL); | |
07d0f000 | 1506 | dev_put(vf_netdev); |
84bf9cef KS |
1507 | module_put(THIS_MODULE); |
1508 | return NOTIFY_OK; | |
1509 | } | |
1510 | ||
84946899 S |
1511 | static int netvsc_probe(struct hv_device *dev, |
1512 | const struct hv_vmbus_device_id *dev_id) | |
df2fff28 | 1513 | { |
df2fff28 GKH |
1514 | struct net_device *net = NULL; |
1515 | struct net_device_context *net_device_ctx; | |
1516 | struct netvsc_device_info device_info; | |
5b54dac8 | 1517 | struct netvsc_device *nvdev; |
df2fff28 GKH |
1518 | int ret; |
1519 | ||
5b54dac8 | 1520 | net = alloc_etherdev_mq(sizeof(struct net_device_context), |
2b01888d | 1521 | VRSS_CHANNEL_MAX); |
df2fff28 | 1522 | if (!net) |
51a805d0 | 1523 | return -ENOMEM; |
df2fff28 | 1524 | |
1b07da51 HZ |
1525 | netif_carrier_off(net); |
1526 | ||
b37879e6 HZ |
1527 | netvsc_init_settings(net); |
1528 | ||
df2fff28 | 1529 | net_device_ctx = netdev_priv(net); |
9efd21e1 | 1530 | net_device_ctx->device_ctx = dev; |
3f300ff4 SX |
1531 | net_device_ctx->msg_enable = netif_msg_init(debug, default_msg); |
1532 | if (netif_msg_probe(net_device_ctx)) | |
1533 | netdev_dbg(net, "netvsc msg_enable: %d\n", | |
1534 | net_device_ctx->msg_enable); | |
1535 | ||
2ddd5e5f | 1536 | hv_set_drvdata(dev, net); |
f580aec4 | 1537 | |
891de74d | 1538 | INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change); |
df2fff28 | 1539 | |
27a70af3 VK |
1540 | spin_lock_init(&net_device_ctx->lock); |
1541 | INIT_LIST_HEAD(&net_device_ctx->reconfig_events); | |
1542 | ||
df2fff28 | 1543 | net->netdev_ops = &device_ops; |
7ad24ea4 | 1544 | net->ethtool_ops = ðtool_ops; |
9efd21e1 | 1545 | SET_NETDEV_DEV(net, &dev->device); |
df2fff28 | 1546 | |
14a03cf8 VK |
1547 | /* We always need headroom for rndis header */ |
1548 | net->needed_headroom = RNDIS_AND_PPI_SIZE; | |
1549 | ||
692e084e | 1550 | /* Notify the netvsc driver of the new device */ |
8ebdcc52 | 1551 | memset(&device_info, 0, sizeof(device_info)); |
692e084e | 1552 | device_info.ring_size = ring_size; |
3071ada4 | 1553 | device_info.num_chn = VRSS_CHANNEL_DEFAULT; |
692e084e HZ |
1554 | ret = rndis_filter_device_add(dev, &device_info); |
1555 | if (ret != 0) { | |
1556 | netdev_err(net, "unable to add netvsc device (ret %d)\n", ret); | |
6c80f3fc | 1557 | free_netdev(net); |
2ddd5e5f | 1558 | hv_set_drvdata(dev, NULL); |
692e084e | 1559 | return ret; |
df2fff28 | 1560 | } |
692e084e HZ |
1561 | memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN); |
1562 | ||
23312a3b | 1563 | /* hw_features computed in rndis_filter_device_add */ |
1564 | net->features = net->hw_features | | |
1565 | NETIF_F_HIGHDMA | NETIF_F_SG | | |
1566 | NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX; | |
1567 | net->vlan_features = net->features; | |
1568 | ||
545a8e79 | 1569 | /* RCU not necessary here, device not registered */ |
3d541ac5 | 1570 | nvdev = net_device_ctx->nvdev; |
5b54dac8 HZ |
1571 | netif_set_real_num_tx_queues(net, nvdev->num_chn); |
1572 | netif_set_real_num_rx_queues(net, nvdev->num_chn); | |
5b54dac8 | 1573 | |
d0c2c997 JW |
1574 | /* MTU range: 68 - 1500 or 65521 */ |
1575 | net->min_mtu = NETVSC_MTU_MIN; | |
1576 | if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2) | |
1577 | net->max_mtu = NETVSC_MTU - ETH_HLEN; | |
1578 | else | |
1579 | net->max_mtu = ETH_DATA_LEN; | |
1580 | ||
a68f9614 HZ |
1581 | ret = register_netdev(net); |
1582 | if (ret != 0) { | |
1583 | pr_err("Unable to register netdev.\n"); | |
2289f0aa | 1584 | rndis_filter_device_remove(dev, nvdev); |
6c80f3fc | 1585 | free_netdev(net); |
a68f9614 HZ |
1586 | } |
1587 | ||
df2fff28 GKH |
1588 | return ret; |
1589 | } | |
1590 | ||
415b023a | 1591 | static int netvsc_remove(struct hv_device *dev) |
df2fff28 | 1592 | { |
2ddd5e5f | 1593 | struct net_device *net; |
122a5f64 | 1594 | struct net_device_context *ndev_ctx; |
2ddd5e5f | 1595 | |
3d541ac5 | 1596 | net = hv_get_drvdata(dev); |
df2fff28 | 1597 | |
df2fff28 | 1598 | if (net == NULL) { |
415b023a | 1599 | dev_err(&dev->device, "No net device to remove\n"); |
df2fff28 GKH |
1600 | return 0; |
1601 | } | |
1602 | ||
122a5f64 | 1603 | ndev_ctx = netdev_priv(net); |
3d541ac5 | 1604 | |
a0be450e | 1605 | netif_device_detach(net); |
f580aec4 | 1606 | |
122a5f64 HZ |
1607 | cancel_delayed_work_sync(&ndev_ctx->dwork); |
1608 | ||
df2fff28 GKH |
1609 | /* |
1610 | * Call to the vsc driver to let it know that the device is being | |
a0be450e | 1611 | * removed. Also blocks mtu and channel changes. |
df2fff28 | 1612 | */ |
a0be450e | 1613 | rtnl_lock(); |
2289f0aa | 1614 | rndis_filter_device_remove(dev, ndev_ctx->nvdev); |
a0be450e | 1615 | rtnl_unlock(); |
1616 | ||
1617 | unregister_netdev(net); | |
df2fff28 | 1618 | |
3d541ac5 VK |
1619 | hv_set_drvdata(dev, NULL); |
1620 | ||
6c80f3fc | 1621 | free_netdev(net); |
df06bcff | 1622 | return 0; |
df2fff28 GKH |
1623 | } |
1624 | ||
345c4cc3 | 1625 | static const struct hv_vmbus_device_id id_table[] = { |
c45cf2d4 | 1626 | /* Network guid */ |
8f505944 | 1627 | { HV_NIC_GUID, }, |
c45cf2d4 | 1628 | { }, |
345c4cc3 S |
1629 | }; |
1630 | ||
1631 | MODULE_DEVICE_TABLE(vmbus, id_table); | |
1632 | ||
f1542a66 | 1633 | /* The one and only one */ |
fde0ef9b | 1634 | static struct hv_driver netvsc_drv = { |
d31b20fc | 1635 | .name = KBUILD_MODNAME, |
345c4cc3 | 1636 | .id_table = id_table, |
fde0ef9b S |
1637 | .probe = netvsc_probe, |
1638 | .remove = netvsc_remove, | |
d4890970 | 1639 | }; |
f1542a66 | 1640 | |
84bf9cef KS |
1641 | /* |
1642 | * On Hyper-V, every VF interface is matched with a corresponding | |
1643 | * synthetic interface. The synthetic interface is presented first | |
1644 | * to the guest. When the corresponding VF instance is registered, | |
1645 | * we will take care of switching the data path. | |
1646 | */ | |
1647 | static int netvsc_netdev_event(struct notifier_block *this, | |
1648 | unsigned long event, void *ptr) | |
1649 | { | |
1650 | struct net_device *event_dev = netdev_notifier_info_to_dev(ptr); | |
1651 | ||
ee837a13 SH |
1652 | /* Skip our own events */ |
1653 | if (event_dev->netdev_ops == &device_ops) | |
1654 | return NOTIFY_DONE; | |
1655 | ||
1656 | /* Avoid non-Ethernet type devices */ | |
1657 | if (event_dev->type != ARPHRD_ETHER) | |
1658 | return NOTIFY_DONE; | |
1659 | ||
0dbff144 | 1660 | /* Avoid Vlan dev with same MAC registering as VF */ |
d0d7b10b | 1661 | if (is_vlan_dev(event_dev)) |
0dbff144 VK |
1662 | return NOTIFY_DONE; |
1663 | ||
1664 | /* Avoid Bonding master dev with same MAC registering as VF */ | |
ee837a13 SH |
1665 | if ((event_dev->priv_flags & IFF_BONDING) && |
1666 | (event_dev->flags & IFF_MASTER)) | |
cb2911fe HZ |
1667 | return NOTIFY_DONE; |
1668 | ||
84bf9cef KS |
1669 | switch (event) { |
1670 | case NETDEV_REGISTER: | |
1671 | return netvsc_register_vf(event_dev); | |
1672 | case NETDEV_UNREGISTER: | |
1673 | return netvsc_unregister_vf(event_dev); | |
1674 | case NETDEV_UP: | |
1675 | return netvsc_vf_up(event_dev); | |
1676 | case NETDEV_DOWN: | |
1677 | return netvsc_vf_down(event_dev); | |
1678 | default: | |
1679 | return NOTIFY_DONE; | |
1680 | } | |
1681 | } | |
1682 | ||
1683 | static struct notifier_block netvsc_netdev_notifier = { | |
1684 | .notifier_call = netvsc_netdev_event, | |
1685 | }; | |
1686 | ||
a9869c94 | 1687 | static void __exit netvsc_drv_exit(void) |
fceaf24a | 1688 | { |
84bf9cef | 1689 | unregister_netdevice_notifier(&netvsc_netdev_notifier); |
768fa219 | 1690 | vmbus_driver_unregister(&netvsc_drv); |
fceaf24a HJ |
1691 | } |
1692 | ||
1fde28cf | 1693 | static int __init netvsc_drv_init(void) |
df2fff28 | 1694 | { |
84bf9cef KS |
1695 | int ret; |
1696 | ||
fa85a6c2 HZ |
1697 | if (ring_size < RING_SIZE_MIN) { |
1698 | ring_size = RING_SIZE_MIN; | |
1699 | pr_info("Increased ring_size to %d (min allowed)\n", | |
1700 | ring_size); | |
1701 | } | |
84bf9cef KS |
1702 | ret = vmbus_driver_register(&netvsc_drv); |
1703 | ||
1704 | if (ret) | |
1705 | return ret; | |
1706 | ||
1707 | register_netdevice_notifier(&netvsc_netdev_notifier); | |
1708 | return 0; | |
df2fff28 GKH |
1709 | } |
1710 | ||
26c14cc1 | 1711 | MODULE_LICENSE("GPL"); |
7880fc54 | 1712 | MODULE_DESCRIPTION("Microsoft Hyper-V network driver"); |
fceaf24a | 1713 | |
1fde28cf | 1714 | module_init(netvsc_drv_init); |
a9869c94 | 1715 | module_exit(netvsc_drv_exit); |