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