xref: /linux/drivers/net/hyperv/netvsc.c (revision 004e9ecfe6c5384f9e0b2f6f6389d42ec22789af)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2009, Microsoft Corporation.
4  *
5  * Authors:
6  *   Haiyang Zhang <haiyangz@microsoft.com>
7  *   Hank Janssen  <hjanssen@microsoft.com>
8  */
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 
11 #include <linux/kernel.h>
12 #include <linux/sched.h>
13 #include <linux/wait.h>
14 #include <linux/mm.h>
15 #include <linux/highmem.h>
16 #include <linux/delay.h>
17 #include <linux/io.h>
18 #include <linux/slab.h>
19 #include <linux/netdevice.h>
20 #include <linux/if_ether.h>
21 #include <linux/vmalloc.h>
22 #include <linux/rtnetlink.h>
23 #include <linux/prefetch.h>
24 #include <linux/filter.h>
25 
26 #include <asm/sync_bitops.h>
27 #include <asm/mshyperv.h>
28 
29 #include "hyperv_net.h"
30 #include "netvsc_trace.h"
31 
32 /*
33  * Switch the data path from the synthetic interface to the VF
34  * interface.
35  */
36 int netvsc_switch_datapath(struct net_device *ndev, bool vf)
37 {
38 	struct net_device_context *net_device_ctx = netdev_priv(ndev);
39 	struct hv_device *dev = net_device_ctx->device_ctx;
40 	struct netvsc_device *nv_dev = rtnl_dereference(net_device_ctx->nvdev);
41 	struct nvsp_message *init_pkt = &nv_dev->channel_init_pkt;
42 	int ret, retry = 0;
43 
44 	/* Block sending traffic to VF if it's about to be gone */
45 	if (!vf)
46 		net_device_ctx->data_path_is_vf = vf;
47 
48 	memset(init_pkt, 0, sizeof(struct nvsp_message));
49 	init_pkt->hdr.msg_type = NVSP_MSG4_TYPE_SWITCH_DATA_PATH;
50 	if (vf)
51 		init_pkt->msg.v4_msg.active_dp.active_datapath =
52 			NVSP_DATAPATH_VF;
53 	else
54 		init_pkt->msg.v4_msg.active_dp.active_datapath =
55 			NVSP_DATAPATH_SYNTHETIC;
56 
57 again:
58 	trace_nvsp_send(ndev, init_pkt);
59 
60 	ret = vmbus_sendpacket(dev->channel, init_pkt,
61 			       sizeof(struct nvsp_message),
62 			       (unsigned long)init_pkt, VM_PKT_DATA_INBAND,
63 			       VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
64 
65 	/* If failed to switch to/from VF, let data_path_is_vf stay false,
66 	 * so we use synthetic path to send data.
67 	 */
68 	if (ret) {
69 		if (ret != -EAGAIN) {
70 			netdev_err(ndev,
71 				   "Unable to send sw datapath msg, err: %d\n",
72 				   ret);
73 			return ret;
74 		}
75 
76 		if (retry++ < RETRY_MAX) {
77 			usleep_range(RETRY_US_LO, RETRY_US_HI);
78 			goto again;
79 		} else {
80 			netdev_err(
81 				ndev,
82 				"Retry failed to send sw datapath msg, err: %d\n",
83 				ret);
84 			return ret;
85 		}
86 	}
87 
88 	wait_for_completion(&nv_dev->channel_init_wait);
89 	net_device_ctx->data_path_is_vf = vf;
90 
91 	return 0;
92 }
93 
94 /* Worker to setup sub channels on initial setup
95  * Initial hotplug event occurs in softirq context
96  * and can't wait for channels.
97  */
98 static void netvsc_subchan_work(struct work_struct *w)
99 {
100 	struct netvsc_device *nvdev =
101 		container_of(w, struct netvsc_device, subchan_work);
102 	struct rndis_device *rdev;
103 	int i, ret;
104 
105 	/* Avoid deadlock with device removal already under RTNL */
106 	if (!rtnl_trylock()) {
107 		schedule_work(w);
108 		return;
109 	}
110 
111 	rdev = nvdev->extension;
112 	if (rdev) {
113 		ret = rndis_set_subchannel(rdev->ndev, nvdev, NULL);
114 		if (ret == 0) {
115 			netif_device_attach(rdev->ndev);
116 		} else {
117 			/* fallback to only primary channel */
118 			for (i = 1; i < nvdev->num_chn; i++)
119 				netif_napi_del(&nvdev->chan_table[i].napi);
120 
121 			nvdev->max_chn = 1;
122 			nvdev->num_chn = 1;
123 		}
124 	}
125 
126 	rtnl_unlock();
127 }
128 
129 static struct netvsc_device *alloc_net_device(void)
130 {
131 	struct netvsc_device *net_device;
132 
133 	net_device = kzalloc_obj(struct netvsc_device);
134 	if (!net_device)
135 		return NULL;
136 
137 	init_waitqueue_head(&net_device->wait_drain);
138 	net_device->destroy = false;
139 	net_device->tx_disable = true;
140 
141 	net_device->max_pkt = RNDIS_MAX_PKT_DEFAULT;
142 	net_device->pkt_align = RNDIS_PKT_ALIGN_DEFAULT;
143 
144 	init_completion(&net_device->channel_init_wait);
145 	init_waitqueue_head(&net_device->subchan_open);
146 	INIT_WORK(&net_device->subchan_work, netvsc_subchan_work);
147 
148 	return net_device;
149 }
150 
151 static void free_netvsc_device(struct rcu_head *head)
152 {
153 	struct netvsc_device *nvdev
154 		= container_of(head, struct netvsc_device, rcu);
155 	int i;
156 
157 	kfree(nvdev->extension);
158 
159 	if (!nvdev->recv_buf_gpadl_handle.decrypted)
160 		vfree(nvdev->recv_buf);
161 	if (!nvdev->send_buf_gpadl_handle.decrypted)
162 		vfree(nvdev->send_buf);
163 	bitmap_free(nvdev->send_section_map);
164 
165 	for (i = 0; i < VRSS_CHANNEL_MAX; i++) {
166 		xdp_rxq_info_unreg(&nvdev->chan_table[i].xdp_rxq);
167 		kfree(nvdev->chan_table[i].recv_buf);
168 		vfree(nvdev->chan_table[i].mrc.slots);
169 	}
170 
171 	kfree(nvdev);
172 }
173 
174 static void free_netvsc_device_rcu(struct netvsc_device *nvdev)
175 {
176 	call_rcu(&nvdev->rcu, free_netvsc_device);
177 }
178 
179 static void netvsc_revoke_recv_buf(struct hv_device *device,
180 				   struct netvsc_device *net_device,
181 				   struct net_device *ndev)
182 {
183 	struct nvsp_message *revoke_packet;
184 	int ret;
185 
186 	/*
187 	 * If we got a section count, it means we received a
188 	 * SendReceiveBufferComplete msg (ie sent
189 	 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
190 	 * to send a revoke msg here
191 	 */
192 	if (net_device->recv_section_cnt) {
193 		/* Send the revoke receive buffer */
194 		revoke_packet = &net_device->revoke_packet;
195 		memset(revoke_packet, 0, sizeof(struct nvsp_message));
196 
197 		revoke_packet->hdr.msg_type =
198 			NVSP_MSG1_TYPE_REVOKE_RECV_BUF;
199 		revoke_packet->msg.v1_msg.
200 		revoke_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
201 
202 		trace_nvsp_send(ndev, revoke_packet);
203 
204 		ret = vmbus_sendpacket(device->channel,
205 				       revoke_packet,
206 				       sizeof(struct nvsp_message),
207 				       VMBUS_RQST_ID_NO_RESPONSE,
208 				       VM_PKT_DATA_INBAND, 0);
209 		/* If the failure is because the channel is rescinded;
210 		 * ignore the failure since we cannot send on a rescinded
211 		 * channel. This would allow us to properly cleanup
212 		 * even when the channel is rescinded.
213 		 */
214 		if (device->channel->rescind)
215 			ret = 0;
216 		/*
217 		 * If we failed here, we might as well return and
218 		 * have a leak rather than continue and a bugchk
219 		 */
220 		if (ret != 0) {
221 			netdev_err(ndev, "unable to send "
222 				"revoke receive buffer to netvsp\n");
223 			return;
224 		}
225 		net_device->recv_section_cnt = 0;
226 	}
227 }
228 
229 static void netvsc_revoke_send_buf(struct hv_device *device,
230 				   struct netvsc_device *net_device,
231 				   struct net_device *ndev)
232 {
233 	struct nvsp_message *revoke_packet;
234 	int ret;
235 
236 	/* Deal with the send buffer we may have setup.
237 	 * If we got a  send section size, it means we received a
238 	 * NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE msg (ie sent
239 	 * NVSP_MSG1_TYPE_SEND_SEND_BUF msg) therefore, we need
240 	 * to send a revoke msg here
241 	 */
242 	if (net_device->send_section_cnt) {
243 		/* Send the revoke receive buffer */
244 		revoke_packet = &net_device->revoke_packet;
245 		memset(revoke_packet, 0, sizeof(struct nvsp_message));
246 
247 		revoke_packet->hdr.msg_type =
248 			NVSP_MSG1_TYPE_REVOKE_SEND_BUF;
249 		revoke_packet->msg.v1_msg.revoke_send_buf.id =
250 			NETVSC_SEND_BUFFER_ID;
251 
252 		trace_nvsp_send(ndev, revoke_packet);
253 
254 		ret = vmbus_sendpacket(device->channel,
255 				       revoke_packet,
256 				       sizeof(struct nvsp_message),
257 				       VMBUS_RQST_ID_NO_RESPONSE,
258 				       VM_PKT_DATA_INBAND, 0);
259 
260 		/* If the failure is because the channel is rescinded;
261 		 * ignore the failure since we cannot send on a rescinded
262 		 * channel. This would allow us to properly cleanup
263 		 * even when the channel is rescinded.
264 		 */
265 		if (device->channel->rescind)
266 			ret = 0;
267 
268 		/* If we failed here, we might as well return and
269 		 * have a leak rather than continue and a bugchk
270 		 */
271 		if (ret != 0) {
272 			netdev_err(ndev, "unable to send "
273 				   "revoke send buffer to netvsp\n");
274 			return;
275 		}
276 		net_device->send_section_cnt = 0;
277 	}
278 }
279 
280 static void netvsc_teardown_recv_gpadl(struct hv_device *device,
281 				       struct netvsc_device *net_device,
282 				       struct net_device *ndev)
283 {
284 	int ret;
285 
286 	if (net_device->recv_buf_gpadl_handle.gpadl_handle) {
287 		ret = vmbus_teardown_gpadl(device->channel,
288 					   &net_device->recv_buf_gpadl_handle);
289 
290 		/* If we failed here, we might as well return and have a leak
291 		 * rather than continue and a bugchk
292 		 */
293 		if (ret != 0) {
294 			netdev_err(ndev,
295 				   "unable to teardown receive buffer's gpadl\n");
296 			return;
297 		}
298 	}
299 }
300 
301 static void netvsc_teardown_send_gpadl(struct hv_device *device,
302 				       struct netvsc_device *net_device,
303 				       struct net_device *ndev)
304 {
305 	int ret;
306 
307 	if (net_device->send_buf_gpadl_handle.gpadl_handle) {
308 		ret = vmbus_teardown_gpadl(device->channel,
309 					   &net_device->send_buf_gpadl_handle);
310 
311 		/* If we failed here, we might as well return and have a leak
312 		 * rather than continue and a bugchk
313 		 */
314 		if (ret != 0) {
315 			netdev_err(ndev,
316 				   "unable to teardown send buffer's gpadl\n");
317 			return;
318 		}
319 	}
320 }
321 
322 int netvsc_alloc_recv_comp_ring(struct netvsc_device *net_device, u32 q_idx)
323 {
324 	struct netvsc_channel *nvchan = &net_device->chan_table[q_idx];
325 	int node = cpu_to_node(nvchan->channel->target_cpu);
326 	size_t size;
327 
328 	size = net_device->recv_completion_cnt * sizeof(struct recv_comp_data);
329 	nvchan->mrc.slots = vzalloc_node(size, node);
330 	if (!nvchan->mrc.slots)
331 		nvchan->mrc.slots = vzalloc(size);
332 
333 	return nvchan->mrc.slots ? 0 : -ENOMEM;
334 }
335 
336 static int netvsc_init_buf(struct hv_device *device,
337 			   struct netvsc_device *net_device,
338 			   const struct netvsc_device_info *device_info)
339 {
340 	struct nvsp_1_message_send_receive_buffer_complete *resp;
341 	struct net_device *ndev = hv_get_drvdata(device);
342 	struct nvsp_message *init_packet;
343 	unsigned int buf_size;
344 	int i, ret = 0;
345 
346 	/* Get receive buffer area. */
347 	buf_size = device_info->recv_sections * device_info->recv_section_size;
348 	buf_size = roundup(buf_size, PAGE_SIZE);
349 
350 	/* Legacy hosts only allow smaller receive buffer */
351 	if (net_device->nvsp_version <= NVSP_PROTOCOL_VERSION_2)
352 		buf_size = min_t(unsigned int, buf_size,
353 				 NETVSC_RECEIVE_BUFFER_SIZE_LEGACY);
354 
355 	net_device->recv_buf = vzalloc(buf_size);
356 	if (!net_device->recv_buf) {
357 		netdev_err(ndev,
358 			   "unable to allocate receive buffer of size %u\n",
359 			   buf_size);
360 		ret = -ENOMEM;
361 		goto cleanup;
362 	}
363 
364 	net_device->recv_buf_size = buf_size;
365 
366 	/*
367 	 * Establish the gpadl handle for this buffer on this
368 	 * channel.  Note: This call uses the vmbus connection rather
369 	 * than the channel to establish the gpadl handle.
370 	 */
371 	ret = vmbus_establish_gpadl(device->channel, net_device->recv_buf,
372 				    buf_size,
373 				    &net_device->recv_buf_gpadl_handle);
374 	if (ret != 0) {
375 		netdev_err(ndev,
376 			"unable to establish receive buffer's gpadl\n");
377 		goto cleanup;
378 	}
379 
380 	/* Notify the NetVsp of the gpadl handle */
381 	init_packet = &net_device->channel_init_pkt;
382 	memset(init_packet, 0, sizeof(struct nvsp_message));
383 	init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_RECV_BUF;
384 	init_packet->msg.v1_msg.send_recv_buf.
385 		gpadl_handle = net_device->recv_buf_gpadl_handle.gpadl_handle;
386 	init_packet->msg.v1_msg.
387 		send_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
388 
389 	trace_nvsp_send(ndev, init_packet);
390 
391 	/* Send the gpadl notification request */
392 	ret = vmbus_sendpacket(device->channel, init_packet,
393 			       sizeof(struct nvsp_message),
394 			       (unsigned long)init_packet,
395 			       VM_PKT_DATA_INBAND,
396 			       VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
397 	if (ret != 0) {
398 		netdev_err(ndev,
399 			"unable to send receive buffer's gpadl to netvsp\n");
400 		goto cleanup;
401 	}
402 
403 	wait_for_completion(&net_device->channel_init_wait);
404 
405 	/* Check the response */
406 	resp = &init_packet->msg.v1_msg.send_recv_buf_complete;
407 	if (resp->status != NVSP_STAT_SUCCESS) {
408 		netdev_err(ndev,
409 			   "Unable to complete receive buffer initialization with NetVsp - status %d\n",
410 			   resp->status);
411 		ret = -EINVAL;
412 		goto cleanup;
413 	}
414 
415 	/* Parse the response */
416 	netdev_dbg(ndev, "Receive sections: %u sub_allocs: size %u count: %u\n",
417 		   resp->num_sections, resp->sections[0].sub_alloc_size,
418 		   resp->sections[0].num_sub_allocs);
419 
420 	/* There should only be one section for the entire receive buffer */
421 	if (resp->num_sections != 1 || resp->sections[0].offset != 0) {
422 		ret = -EINVAL;
423 		goto cleanup;
424 	}
425 
426 	net_device->recv_section_size = resp->sections[0].sub_alloc_size;
427 	net_device->recv_section_cnt = resp->sections[0].num_sub_allocs;
428 
429 	/* Ensure buffer will not overflow */
430 	if (net_device->recv_section_size < NETVSC_MTU_MIN || (u64)net_device->recv_section_size *
431 	    (u64)net_device->recv_section_cnt > (u64)buf_size) {
432 		netdev_err(ndev, "invalid recv_section_size %u\n",
433 			   net_device->recv_section_size);
434 		ret = -EINVAL;
435 		goto cleanup;
436 	}
437 
438 	for (i = 0; i < VRSS_CHANNEL_MAX; i++) {
439 		struct netvsc_channel *nvchan = &net_device->chan_table[i];
440 
441 		nvchan->recv_buf = kzalloc(net_device->recv_section_size, GFP_KERNEL);
442 		if (nvchan->recv_buf == NULL) {
443 			ret = -ENOMEM;
444 			goto cleanup;
445 		}
446 	}
447 
448 	/* Setup receive completion ring.
449 	 * Add 1 to the recv_section_cnt because at least one entry in a
450 	 * ring buffer has to be empty.
451 	 */
452 	net_device->recv_completion_cnt = net_device->recv_section_cnt + 1;
453 	ret = netvsc_alloc_recv_comp_ring(net_device, 0);
454 	if (ret)
455 		goto cleanup;
456 
457 	/* Now setup the send buffer. */
458 	buf_size = device_info->send_sections * device_info->send_section_size;
459 	buf_size = round_up(buf_size, PAGE_SIZE);
460 
461 	net_device->send_buf = vzalloc(buf_size);
462 	if (!net_device->send_buf) {
463 		netdev_err(ndev, "unable to allocate send buffer of size %u\n",
464 			   buf_size);
465 		ret = -ENOMEM;
466 		goto cleanup;
467 	}
468 	net_device->send_buf_size = buf_size;
469 
470 	/* Establish the gpadl handle for this buffer on this
471 	 * channel.  Note: This call uses the vmbus connection rather
472 	 * than the channel to establish the gpadl handle.
473 	 */
474 	ret = vmbus_establish_gpadl(device->channel, net_device->send_buf,
475 				    buf_size,
476 				    &net_device->send_buf_gpadl_handle);
477 	if (ret != 0) {
478 		netdev_err(ndev,
479 			   "unable to establish send buffer's gpadl\n");
480 		goto cleanup;
481 	}
482 
483 	/* Notify the NetVsp of the gpadl handle */
484 	init_packet = &net_device->channel_init_pkt;
485 	memset(init_packet, 0, sizeof(struct nvsp_message));
486 	init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_SEND_BUF;
487 	init_packet->msg.v1_msg.send_send_buf.gpadl_handle =
488 		net_device->send_buf_gpadl_handle.gpadl_handle;
489 	init_packet->msg.v1_msg.send_send_buf.id = NETVSC_SEND_BUFFER_ID;
490 
491 	trace_nvsp_send(ndev, init_packet);
492 
493 	/* Send the gpadl notification request */
494 	ret = vmbus_sendpacket(device->channel, init_packet,
495 			       sizeof(struct nvsp_message),
496 			       (unsigned long)init_packet,
497 			       VM_PKT_DATA_INBAND,
498 			       VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
499 	if (ret != 0) {
500 		netdev_err(ndev,
501 			   "unable to send send buffer's gpadl to netvsp\n");
502 		goto cleanup;
503 	}
504 
505 	wait_for_completion(&net_device->channel_init_wait);
506 
507 	/* Check the response */
508 	if (init_packet->msg.v1_msg.
509 	    send_send_buf_complete.status != NVSP_STAT_SUCCESS) {
510 		netdev_err(ndev, "Unable to complete send buffer "
511 			   "initialization with NetVsp - status %d\n",
512 			   init_packet->msg.v1_msg.
513 			   send_send_buf_complete.status);
514 		ret = -EINVAL;
515 		goto cleanup;
516 	}
517 
518 	/* Parse the response */
519 	net_device->send_section_size = init_packet->msg.
520 				v1_msg.send_send_buf_complete.section_size;
521 	if (net_device->send_section_size < NETVSC_MTU_MIN) {
522 		netdev_err(ndev, "invalid send_section_size %u\n",
523 			   net_device->send_section_size);
524 		ret = -EINVAL;
525 		goto cleanup;
526 	}
527 
528 	/* Section count is simply the size divided by the section size. */
529 	net_device->send_section_cnt = buf_size / net_device->send_section_size;
530 
531 	netdev_dbg(ndev, "Send section size: %d, Section count:%d\n",
532 		   net_device->send_section_size, net_device->send_section_cnt);
533 
534 	/* Setup state for managing the send buffer. */
535 	net_device->send_section_map = bitmap_zalloc(net_device->send_section_cnt,
536 						     GFP_KERNEL);
537 	if (!net_device->send_section_map) {
538 		ret = -ENOMEM;
539 		goto cleanup;
540 	}
541 
542 	goto exit;
543 
544 cleanup:
545 	netvsc_revoke_recv_buf(device, net_device, ndev);
546 	netvsc_revoke_send_buf(device, net_device, ndev);
547 	netvsc_teardown_recv_gpadl(device, net_device, ndev);
548 	netvsc_teardown_send_gpadl(device, net_device, ndev);
549 
550 exit:
551 	return ret;
552 }
553 
554 /* Negotiate NVSP protocol version */
555 static int negotiate_nvsp_ver(struct hv_device *device,
556 			      struct netvsc_device *net_device,
557 			      struct nvsp_message *init_packet,
558 			      u32 nvsp_ver)
559 {
560 	struct net_device *ndev = hv_get_drvdata(device);
561 	int ret;
562 
563 	memset(init_packet, 0, sizeof(struct nvsp_message));
564 	init_packet->hdr.msg_type = NVSP_MSG_TYPE_INIT;
565 	init_packet->msg.init_msg.init.min_protocol_ver = nvsp_ver;
566 	init_packet->msg.init_msg.init.max_protocol_ver = nvsp_ver;
567 	trace_nvsp_send(ndev, init_packet);
568 
569 	/* Send the init request */
570 	ret = vmbus_sendpacket(device->channel, init_packet,
571 			       sizeof(struct nvsp_message),
572 			       (unsigned long)init_packet,
573 			       VM_PKT_DATA_INBAND,
574 			       VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
575 
576 	if (ret != 0)
577 		return ret;
578 
579 	wait_for_completion(&net_device->channel_init_wait);
580 
581 	if (init_packet->msg.init_msg.init_complete.status !=
582 	    NVSP_STAT_SUCCESS)
583 		return -EINVAL;
584 
585 	if (nvsp_ver == NVSP_PROTOCOL_VERSION_1)
586 		return 0;
587 
588 	/* NVSPv2 or later: Send NDIS config */
589 	memset(init_packet, 0, sizeof(struct nvsp_message));
590 	init_packet->hdr.msg_type = NVSP_MSG2_TYPE_SEND_NDIS_CONFIG;
591 	init_packet->msg.v2_msg.send_ndis_config.mtu = ndev->mtu + ETH_HLEN;
592 	init_packet->msg.v2_msg.send_ndis_config.capability.ieee8021q = 1;
593 
594 	if (nvsp_ver >= NVSP_PROTOCOL_VERSION_5) {
595 		if (hv_is_isolation_supported())
596 			netdev_info(ndev, "SR-IOV not advertised by guests on the host supporting isolation\n");
597 		else
598 			init_packet->msg.v2_msg.send_ndis_config.capability.sriov = 1;
599 
600 		/* Teaming bit is needed to receive link speed updates */
601 		init_packet->msg.v2_msg.send_ndis_config.capability.teaming = 1;
602 	}
603 
604 	if (nvsp_ver >= NVSP_PROTOCOL_VERSION_61)
605 		init_packet->msg.v2_msg.send_ndis_config.capability.rsc = 1;
606 
607 	trace_nvsp_send(ndev, init_packet);
608 
609 	ret = vmbus_sendpacket(device->channel, init_packet,
610 				sizeof(struct nvsp_message),
611 				VMBUS_RQST_ID_NO_RESPONSE,
612 				VM_PKT_DATA_INBAND, 0);
613 
614 	return ret;
615 }
616 
617 static int netvsc_connect_vsp(struct hv_device *device,
618 			      struct netvsc_device *net_device,
619 			      const struct netvsc_device_info *device_info)
620 {
621 	struct net_device *ndev = hv_get_drvdata(device);
622 	static const u32 ver_list[] = {
623 		NVSP_PROTOCOL_VERSION_1, NVSP_PROTOCOL_VERSION_2,
624 		NVSP_PROTOCOL_VERSION_4, NVSP_PROTOCOL_VERSION_5,
625 		NVSP_PROTOCOL_VERSION_6, NVSP_PROTOCOL_VERSION_61
626 	};
627 	struct nvsp_message *init_packet;
628 	int ndis_version, i, ret;
629 
630 	init_packet = &net_device->channel_init_pkt;
631 
632 	/* Negotiate the latest NVSP protocol supported */
633 	for (i = ARRAY_SIZE(ver_list) - 1; i >= 0; i--)
634 		if (negotiate_nvsp_ver(device, net_device, init_packet,
635 				       ver_list[i])  == 0) {
636 			net_device->nvsp_version = ver_list[i];
637 			break;
638 		}
639 
640 	if (i < 0) {
641 		ret = -EPROTO;
642 		goto cleanup;
643 	}
644 
645 	if (hv_is_isolation_supported() && net_device->nvsp_version < NVSP_PROTOCOL_VERSION_61) {
646 		netdev_err(ndev, "Invalid NVSP version 0x%x (expected >= 0x%x) from the host supporting isolation\n",
647 			   net_device->nvsp_version, NVSP_PROTOCOL_VERSION_61);
648 		ret = -EPROTO;
649 		goto cleanup;
650 	}
651 
652 	pr_debug("Negotiated NVSP version:%x\n", net_device->nvsp_version);
653 
654 	/* Send the ndis version */
655 	memset(init_packet, 0, sizeof(struct nvsp_message));
656 
657 	if (net_device->nvsp_version <= NVSP_PROTOCOL_VERSION_4)
658 		ndis_version = 0x00060001;
659 	else
660 		ndis_version = 0x0006001e;
661 
662 	init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_NDIS_VER;
663 	init_packet->msg.v1_msg.
664 		send_ndis_ver.ndis_major_ver =
665 				(ndis_version & 0xFFFF0000) >> 16;
666 	init_packet->msg.v1_msg.
667 		send_ndis_ver.ndis_minor_ver =
668 				ndis_version & 0xFFFF;
669 
670 	trace_nvsp_send(ndev, init_packet);
671 
672 	/* Send the init request */
673 	ret = vmbus_sendpacket(device->channel, init_packet,
674 				sizeof(struct nvsp_message),
675 				VMBUS_RQST_ID_NO_RESPONSE,
676 				VM_PKT_DATA_INBAND, 0);
677 	if (ret != 0)
678 		goto cleanup;
679 
680 
681 	ret = netvsc_init_buf(device, net_device, device_info);
682 
683 cleanup:
684 	return ret;
685 }
686 
687 /*
688  * netvsc_device_remove - Callback when the root bus device is removed
689  */
690 void netvsc_device_remove(struct hv_device *device)
691 {
692 	struct net_device *ndev = hv_get_drvdata(device);
693 	struct net_device_context *net_device_ctx = netdev_priv(ndev);
694 	struct netvsc_device *net_device
695 		= rtnl_dereference(net_device_ctx->nvdev);
696 	int i;
697 
698 	/*
699 	 * Revoke receive buffer. If host is pre-Win2016 then tear down
700 	 * receive buffer GPADL. Do the same for send buffer.
701 	 */
702 	netvsc_revoke_recv_buf(device, net_device, ndev);
703 	if (vmbus_proto_version < VERSION_WIN10)
704 		netvsc_teardown_recv_gpadl(device, net_device, ndev);
705 
706 	netvsc_revoke_send_buf(device, net_device, ndev);
707 	if (vmbus_proto_version < VERSION_WIN10)
708 		netvsc_teardown_send_gpadl(device, net_device, ndev);
709 
710 	RCU_INIT_POINTER(net_device_ctx->nvdev, NULL);
711 
712 	/* Disable NAPI and disassociate its context from the device. */
713 	for (i = 0; i < net_device->num_chn; i++) {
714 		/* See also vmbus_reset_channel_cb(). */
715 		/* only disable enabled NAPI channel */
716 		if (i < ndev->real_num_rx_queues) {
717 			netif_queue_set_napi(ndev, i, NETDEV_QUEUE_TYPE_TX,
718 					     NULL);
719 			netif_queue_set_napi(ndev, i, NETDEV_QUEUE_TYPE_RX,
720 					     NULL);
721 			napi_disable(&net_device->chan_table[i].napi);
722 		}
723 
724 		netif_napi_del(&net_device->chan_table[i].napi);
725 	}
726 
727 	/*
728 	 * At this point, no one should be accessing net_device
729 	 * except in here
730 	 */
731 	netdev_dbg(ndev, "net device safe to remove\n");
732 
733 	/* Now, we can close the channel safely */
734 	vmbus_close(device->channel);
735 
736 	/*
737 	 * If host is Win2016 or higher then we do the GPADL tear down
738 	 * here after VMBus is closed.
739 	*/
740 	if (vmbus_proto_version >= VERSION_WIN10) {
741 		netvsc_teardown_recv_gpadl(device, net_device, ndev);
742 		netvsc_teardown_send_gpadl(device, net_device, ndev);
743 	}
744 
745 	/* Release all resources */
746 	free_netvsc_device_rcu(net_device);
747 }
748 
749 #define RING_AVAIL_PERCENT_HIWATER 20
750 #define RING_AVAIL_PERCENT_LOWATER 10
751 
752 static inline void netvsc_free_send_slot(struct netvsc_device *net_device,
753 					 u32 index)
754 {
755 	sync_change_bit(index, net_device->send_section_map);
756 }
757 
758 static void netvsc_send_tx_complete(struct net_device *ndev,
759 				    struct netvsc_device *net_device,
760 				    struct vmbus_channel *channel,
761 				    const struct vmpacket_descriptor *desc,
762 				    int budget)
763 {
764 	struct net_device_context *ndev_ctx = netdev_priv(ndev);
765 	struct sk_buff *skb;
766 	u16 q_idx = 0;
767 	int queue_sends;
768 	u64 cmd_rqst;
769 
770 	cmd_rqst = channel->request_addr_callback(channel, desc->trans_id);
771 	if (cmd_rqst == VMBUS_RQST_ERROR) {
772 		netdev_err(ndev, "Invalid transaction ID %llx\n", desc->trans_id);
773 		return;
774 	}
775 
776 	skb = (struct sk_buff *)(unsigned long)cmd_rqst;
777 
778 	/* Notify the layer above us */
779 	if (likely(skb)) {
780 		struct hv_netvsc_packet *packet
781 			= (struct hv_netvsc_packet *)skb->cb;
782 		u32 send_index = packet->send_buf_index;
783 		struct netvsc_stats_tx *tx_stats;
784 
785 		if (send_index != NETVSC_INVALID_INDEX)
786 			netvsc_free_send_slot(net_device, send_index);
787 		q_idx = packet->q_idx;
788 
789 		tx_stats = &net_device->chan_table[q_idx].tx_stats;
790 
791 		u64_stats_update_begin(&tx_stats->syncp);
792 		tx_stats->packets += packet->total_packets;
793 		tx_stats->bytes += packet->total_bytes;
794 		u64_stats_update_end(&tx_stats->syncp);
795 
796 		netvsc_dma_unmap(ndev_ctx->device_ctx, packet);
797 		napi_consume_skb(skb, budget);
798 	}
799 
800 	queue_sends =
801 		atomic_dec_return(&net_device->chan_table[q_idx].queue_sends);
802 
803 	if (unlikely(net_device->destroy)) {
804 		if (queue_sends == 0)
805 			wake_up(&net_device->wait_drain);
806 	} else {
807 		struct netdev_queue *txq = netdev_get_tx_queue(ndev, q_idx);
808 
809 		if (netif_tx_queue_stopped(txq) && !net_device->tx_disable &&
810 		    (hv_get_avail_to_write_percent(&channel->outbound) >
811 		     RING_AVAIL_PERCENT_HIWATER || queue_sends < 1)) {
812 			netif_tx_wake_queue(txq);
813 			ndev_ctx->eth_stats.wake_queue++;
814 		}
815 	}
816 }
817 
818 static void netvsc_send_completion(struct net_device *ndev,
819 				   struct netvsc_device *net_device,
820 				   struct vmbus_channel *incoming_channel,
821 				   const struct vmpacket_descriptor *desc,
822 				   int budget)
823 {
824 	const struct nvsp_message *nvsp_packet;
825 	u32 msglen = hv_pkt_datalen(desc);
826 	struct nvsp_message *pkt_rqst;
827 	u64 cmd_rqst;
828 	u32 status;
829 
830 	/* First check if this is a VMBUS completion without data payload */
831 	if (!msglen) {
832 		cmd_rqst = incoming_channel->request_addr_callback(incoming_channel,
833 								   desc->trans_id);
834 		if (cmd_rqst == VMBUS_RQST_ERROR) {
835 			netdev_err(ndev, "Invalid transaction ID %llx\n", desc->trans_id);
836 			return;
837 		}
838 
839 		pkt_rqst = (struct nvsp_message *)(uintptr_t)cmd_rqst;
840 		switch (pkt_rqst->hdr.msg_type) {
841 		case NVSP_MSG4_TYPE_SWITCH_DATA_PATH:
842 			complete(&net_device->channel_init_wait);
843 			break;
844 
845 		default:
846 			netdev_err(ndev, "Unexpected VMBUS completion!!\n");
847 		}
848 		return;
849 	}
850 
851 	/* Ensure packet is big enough to read header fields */
852 	if (msglen < sizeof(struct nvsp_message_header)) {
853 		netdev_err(ndev, "nvsp_message length too small: %u\n", msglen);
854 		return;
855 	}
856 
857 	nvsp_packet = hv_pkt_data(desc);
858 	switch (nvsp_packet->hdr.msg_type) {
859 	case NVSP_MSG_TYPE_INIT_COMPLETE:
860 		if (msglen < sizeof(struct nvsp_message_header) +
861 				sizeof(struct nvsp_message_init_complete)) {
862 			netdev_err(ndev, "nvsp_msg length too small: %u\n",
863 				   msglen);
864 			return;
865 		}
866 		break;
867 
868 	case NVSP_MSG1_TYPE_SEND_RECV_BUF_COMPLETE:
869 		if (msglen < sizeof(struct nvsp_message_header) +
870 				struct_size_t(struct nvsp_1_message_send_receive_buffer_complete,
871 					      sections, 1)) {
872 			netdev_err(ndev, "nvsp_msg1 length too small: %u\n",
873 				   msglen);
874 			return;
875 		}
876 		break;
877 
878 	case NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE:
879 		if (msglen < sizeof(struct nvsp_message_header) +
880 				sizeof(struct nvsp_1_message_send_send_buffer_complete)) {
881 			netdev_err(ndev, "nvsp_msg1 length too small: %u\n",
882 				   msglen);
883 			return;
884 		}
885 		break;
886 
887 	case NVSP_MSG5_TYPE_SUBCHANNEL:
888 		if (msglen < sizeof(struct nvsp_message_header) +
889 				sizeof(struct nvsp_5_subchannel_complete)) {
890 			netdev_err(ndev, "nvsp_msg5 length too small: %u\n",
891 				   msglen);
892 			return;
893 		}
894 		break;
895 
896 	case NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE:
897 		if (msglen < sizeof(struct nvsp_message_header) +
898 		    sizeof(struct nvsp_1_message_send_rndis_packet_complete)) {
899 			if (net_ratelimit())
900 				netdev_err(ndev, "nvsp_rndis_pkt_complete length too small: %u\n",
901 					   msglen);
902 			return;
903 		}
904 
905 		/* If status indicates an error, output a message so we know
906 		 * there's a problem. But process the completion anyway so the
907 		 * resources are released.
908 		 */
909 		status = nvsp_packet->msg.v1_msg.send_rndis_pkt_complete.status;
910 		if (status != NVSP_STAT_SUCCESS && net_ratelimit())
911 			netdev_err(ndev, "nvsp_rndis_pkt_complete error status: %x\n",
912 				   status);
913 
914 		netvsc_send_tx_complete(ndev, net_device, incoming_channel,
915 					desc, budget);
916 		return;
917 
918 	default:
919 		netdev_err(ndev,
920 			   "Unknown send completion type %d received!!\n",
921 			   nvsp_packet->hdr.msg_type);
922 		return;
923 	}
924 
925 	/* Copy the response back */
926 	memcpy(&net_device->channel_init_pkt, nvsp_packet,
927 	       sizeof(struct nvsp_message));
928 	complete(&net_device->channel_init_wait);
929 }
930 
931 static u32 netvsc_get_next_send_section(struct netvsc_device *net_device)
932 {
933 	unsigned long *map_addr = net_device->send_section_map;
934 	unsigned int i;
935 
936 	for_each_clear_bit(i, map_addr, net_device->send_section_cnt) {
937 		if (sync_test_and_set_bit(i, map_addr) == 0)
938 			return i;
939 	}
940 
941 	return NETVSC_INVALID_INDEX;
942 }
943 
944 static void netvsc_copy_to_send_buf(struct netvsc_device *net_device,
945 				    unsigned int section_index,
946 				    u32 pend_size,
947 				    struct hv_netvsc_packet *packet,
948 				    struct rndis_message *rndis_msg,
949 				    struct hv_page_buffer *pb,
950 				    bool xmit_more)
951 {
952 	char *start = net_device->send_buf;
953 	char *dest = start + (section_index * net_device->send_section_size)
954 		     + pend_size;
955 	int i;
956 	u32 padding = 0;
957 	u32 page_count = packet->cp_partial ? 1 : packet->page_buf_cnt;
958 	u32 remain;
959 
960 	/* Add padding */
961 	remain = packet->total_data_buflen & (net_device->pkt_align - 1);
962 	if (xmit_more && remain) {
963 		padding = net_device->pkt_align - remain;
964 		rndis_msg->msg_len += padding;
965 		packet->total_data_buflen += padding;
966 	}
967 
968 	for (i = 0; i < page_count; i++) {
969 		phys_addr_t paddr = (pb[i].pfn << HV_HYP_PAGE_SHIFT) +
970 				    pb[i].offset;
971 		u32 len = pb[i].len;
972 
973 		while (len) {
974 			struct page *page = phys_to_page(paddr);
975 			u32 off = offset_in_page(paddr);
976 			u32 chunk = min_t(u32, len, PAGE_SIZE - off);
977 			char *src = kmap_local_page(page);
978 
979 			memcpy(dest, src + off, chunk);
980 			kunmap_local(src);
981 			dest += chunk;
982 			paddr += chunk;
983 			len -= chunk;
984 		}
985 	}
986 
987 	if (padding)
988 		memset(dest, 0, padding);
989 }
990 
991 void netvsc_dma_unmap(struct hv_device *hv_dev,
992 		      struct hv_netvsc_packet *packet)
993 {
994 	int i;
995 
996 	if (!hv_is_isolation_supported())
997 		return;
998 
999 	if (!packet->dma_range)
1000 		return;
1001 
1002 	for (i = 0; i < packet->page_buf_cnt; i++)
1003 		dma_unmap_single(&hv_dev->device, packet->dma_range[i].dma,
1004 				 packet->dma_range[i].mapping_size,
1005 				 DMA_TO_DEVICE);
1006 
1007 	kfree(packet->dma_range);
1008 }
1009 
1010 /* netvsc_dma_map - Map swiotlb bounce buffer with data page of
1011  * packet sent by vmbus_sendpacket_pagebuffer() in the Isolation
1012  * VM.
1013  *
1014  * In isolation VM, netvsc send buffer has been marked visible to
1015  * host and so the data copied to send buffer doesn't need to use
1016  * bounce buffer. The data pages handled by vmbus_sendpacket_pagebuffer()
1017  * may not be copied to send buffer and so these pages need to be
1018  * mapped with swiotlb bounce buffer. netvsc_dma_map() is to do
1019  * that. The pfns in the struct hv_page_buffer need to be converted
1020  * to bounce buffer's pfn. The loop here is necessary because the
1021  * entries in the page buffer array are not necessarily full
1022  * pages of data.  Each entry in the array has a separate offset and
1023  * len that may be non-zero, even for entries in the middle of the
1024  * array.  And the entries are not physically contiguous.  So each
1025  * entry must be individually mapped rather than as a contiguous unit.
1026  * So not use dma_map_sg() here.
1027  */
1028 static int netvsc_dma_map(struct hv_device *hv_dev,
1029 			  struct hv_netvsc_packet *packet,
1030 			  struct hv_page_buffer *pb)
1031 {
1032 	u32 page_count = packet->page_buf_cnt;
1033 	dma_addr_t dma;
1034 	int i;
1035 
1036 	if (!hv_is_isolation_supported())
1037 		return 0;
1038 
1039 	packet->dma_range = kzalloc_objs(*packet->dma_range, page_count,
1040 					 GFP_ATOMIC);
1041 	if (!packet->dma_range)
1042 		return -ENOMEM;
1043 
1044 	for (i = 0; i < page_count; i++) {
1045 		char *src = phys_to_virt((pb[i].pfn << HV_HYP_PAGE_SHIFT)
1046 					 + pb[i].offset);
1047 		u32 len = pb[i].len;
1048 
1049 		dma = dma_map_single(&hv_dev->device, src, len,
1050 				     DMA_TO_DEVICE);
1051 		if (dma_mapping_error(&hv_dev->device, dma)) {
1052 			kfree(packet->dma_range);
1053 			return -ENOMEM;
1054 		}
1055 
1056 		/* pb[].offset and pb[].len are not changed during dma mapping
1057 		 * and so not reassign.
1058 		 */
1059 		packet->dma_range[i].dma = dma;
1060 		packet->dma_range[i].mapping_size = len;
1061 		pb[i].pfn = dma >> HV_HYP_PAGE_SHIFT;
1062 	}
1063 
1064 	return 0;
1065 }
1066 
1067 /* Build an "array" of mpb entries describing the data to be transferred
1068  * over VMBus. After the desc header fields, each "array" entry is variable
1069  * size, and each entry starts after the end of the previous entry. The
1070  * "offset" and "len" fields for each entry imply the size of the entry.
1071  *
1072  * The pfns are in HV_HYP_PAGE_SIZE, because all communication with Hyper-V
1073  * uses that granularity, even if the system page size of the guest is larger.
1074  * Each entry in the input "pb" array must describe a contiguous range of
1075  * guest physical memory so that the pfns are sequential if the range crosses
1076  * a page boundary. The offset field must be < HV_HYP_PAGE_SIZE.
1077  */
1078 static inline void netvsc_build_mpb_array(struct hv_page_buffer *pb,
1079 				u32 page_buffer_count,
1080 				struct vmbus_packet_mpb_array *desc,
1081 				u32 *desc_size)
1082 {
1083 	struct hv_mpb_array *mpb_entry = &desc->range;
1084 	int i, j;
1085 
1086 	for (i = 0; i < page_buffer_count; i++) {
1087 		u32 offset = pb[i].offset;
1088 		u32 len = pb[i].len;
1089 
1090 		mpb_entry->offset = offset;
1091 		mpb_entry->len = len;
1092 
1093 		for (j = 0; j < HVPFN_UP(offset + len); j++)
1094 			mpb_entry->pfn_array[j] = pb[i].pfn + j;
1095 
1096 		mpb_entry = (struct hv_mpb_array *)&mpb_entry->pfn_array[j];
1097 	}
1098 
1099 	desc->rangecount = page_buffer_count;
1100 	*desc_size = (char *)mpb_entry - (char *)desc;
1101 }
1102 
1103 static inline int netvsc_send_pkt(
1104 	struct hv_device *device,
1105 	struct hv_netvsc_packet *packet,
1106 	struct netvsc_device *net_device,
1107 	struct hv_page_buffer *pb,
1108 	struct sk_buff *skb)
1109 {
1110 	struct nvsp_message nvmsg;
1111 	struct nvsp_1_message_send_rndis_packet *rpkt =
1112 		&nvmsg.msg.v1_msg.send_rndis_pkt;
1113 	struct netvsc_channel * const nvchan =
1114 		&net_device->chan_table[packet->q_idx];
1115 	struct vmbus_channel *out_channel = nvchan->channel;
1116 	struct net_device *ndev = hv_get_drvdata(device);
1117 	struct net_device_context *ndev_ctx = netdev_priv(ndev);
1118 	struct netdev_queue *txq = netdev_get_tx_queue(ndev, packet->q_idx);
1119 	u64 req_id;
1120 	int ret;
1121 	u32 ring_avail = hv_get_avail_to_write_percent(&out_channel->outbound);
1122 
1123 	memset(&nvmsg, 0, sizeof(struct nvsp_message));
1124 	nvmsg.hdr.msg_type = NVSP_MSG1_TYPE_SEND_RNDIS_PKT;
1125 	if (skb)
1126 		rpkt->channel_type = 0;		/* 0 is RMC_DATA */
1127 	else
1128 		rpkt->channel_type = 1;		/* 1 is RMC_CONTROL */
1129 
1130 	rpkt->send_buf_section_index = packet->send_buf_index;
1131 	if (packet->send_buf_index == NETVSC_INVALID_INDEX)
1132 		rpkt->send_buf_section_size = 0;
1133 	else
1134 		rpkt->send_buf_section_size = packet->total_data_buflen;
1135 
1136 	req_id = (ulong)skb;
1137 
1138 	if (out_channel->rescind)
1139 		return -ENODEV;
1140 
1141 	trace_nvsp_send_pkt(ndev, out_channel, rpkt);
1142 
1143 	packet->dma_range = NULL;
1144 	if (packet->page_buf_cnt) {
1145 		struct vmbus_channel_packet_page_buffer desc;
1146 		u32 desc_size;
1147 
1148 		if (packet->cp_partial)
1149 			pb++;
1150 
1151 		ret = netvsc_dma_map(ndev_ctx->device_ctx, packet, pb);
1152 		if (ret) {
1153 			ret = -EAGAIN;
1154 			goto exit;
1155 		}
1156 
1157 		netvsc_build_mpb_array(pb, packet->page_buf_cnt,
1158 				(struct vmbus_packet_mpb_array *)&desc,
1159 				 &desc_size);
1160 		ret = vmbus_sendpacket_mpb_desc(out_channel,
1161 				(struct vmbus_packet_mpb_array *)&desc,
1162 				desc_size, &nvmsg, sizeof(nvmsg), req_id);
1163 		if (ret)
1164 			netvsc_dma_unmap(ndev_ctx->device_ctx, packet);
1165 	} else {
1166 		ret = vmbus_sendpacket(out_channel,
1167 				       &nvmsg, sizeof(nvmsg),
1168 				       req_id, VM_PKT_DATA_INBAND,
1169 				       VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1170 	}
1171 
1172 exit:
1173 	if (ret == 0) {
1174 		atomic_inc_return(&nvchan->queue_sends);
1175 
1176 		if (ring_avail < RING_AVAIL_PERCENT_LOWATER) {
1177 			netif_tx_stop_queue(txq);
1178 			ndev_ctx->eth_stats.stop_queue++;
1179 		}
1180 	} else if (ret == -EAGAIN) {
1181 		netif_tx_stop_queue(txq);
1182 		ndev_ctx->eth_stats.stop_queue++;
1183 	} else {
1184 		netdev_err(ndev,
1185 			   "Unable to send packet pages %u len %u, ret %d\n",
1186 			   packet->page_buf_cnt, packet->total_data_buflen,
1187 			   ret);
1188 	}
1189 
1190 	if (netif_tx_queue_stopped(txq) &&
1191 	    atomic_read(&nvchan->queue_sends) < 1 &&
1192 	    !net_device->tx_disable) {
1193 		netif_tx_wake_queue(txq);
1194 		ndev_ctx->eth_stats.wake_queue++;
1195 		if (ret == -EAGAIN)
1196 			ret = -ENOSPC;
1197 	}
1198 
1199 	return ret;
1200 }
1201 
1202 /* Move packet out of multi send data (msd), and clear msd */
1203 static inline void move_pkt_msd(struct hv_netvsc_packet **msd_send,
1204 				struct sk_buff **msd_skb,
1205 				struct multi_send_data *msdp)
1206 {
1207 	*msd_skb = msdp->skb;
1208 	*msd_send = msdp->pkt;
1209 	msdp->skb = NULL;
1210 	msdp->pkt = NULL;
1211 	msdp->count = 0;
1212 }
1213 
1214 /* RCU already held by caller */
1215 /* Batching/bouncing logic is designed to attempt to optimize
1216  * performance.
1217  *
1218  * For small, non-LSO packets we copy the packet to a send buffer
1219  * which is pre-registered with the Hyper-V side. This enables the
1220  * hypervisor to avoid remapping the aperture to access the packet
1221  * descriptor and data.
1222  *
1223  * If we already started using a buffer and the netdev is transmitting
1224  * a burst of packets, keep on copying into the buffer until it is
1225  * full or we are done collecting a burst. If there is an existing
1226  * buffer with space for the RNDIS descriptor but not the packet, copy
1227  * the RNDIS descriptor to the buffer, keeping the packet in place.
1228  *
1229  * If we do batching and send more than one packet using a single
1230  * NetVSC message, free the SKBs of the packets copied, except for the
1231  * last packet. This is done to streamline the handling of the case
1232  * where the last packet only had the RNDIS descriptor copied to the
1233  * send buffer, with the data pointers included in the NetVSC message.
1234  */
1235 int netvsc_send(struct net_device *ndev,
1236 		struct hv_netvsc_packet *packet,
1237 		struct rndis_message *rndis_msg,
1238 		struct hv_page_buffer *pb,
1239 		struct sk_buff *skb,
1240 		bool xdp_tx)
1241 {
1242 	struct net_device_context *ndev_ctx = netdev_priv(ndev);
1243 	struct netvsc_device *net_device
1244 		= rcu_dereference_bh(ndev_ctx->nvdev);
1245 	struct hv_device *device = ndev_ctx->device_ctx;
1246 	int ret = 0;
1247 	struct netvsc_channel *nvchan;
1248 	u32 pktlen = packet->total_data_buflen, msd_len = 0;
1249 	unsigned int section_index = NETVSC_INVALID_INDEX;
1250 	struct multi_send_data *msdp;
1251 	struct hv_netvsc_packet *msd_send = NULL, *cur_send = NULL;
1252 	struct sk_buff *msd_skb = NULL;
1253 	bool try_batch, xmit_more;
1254 
1255 	/* If device is rescinded, return error and packet will get dropped. */
1256 	if (unlikely(!net_device || net_device->destroy))
1257 		return -ENODEV;
1258 
1259 	nvchan = &net_device->chan_table[packet->q_idx];
1260 	packet->send_buf_index = NETVSC_INVALID_INDEX;
1261 	packet->cp_partial = false;
1262 
1263 	/* Send a control message or XDP packet directly without accessing
1264 	 * msd (Multi-Send Data) field which may be changed during data packet
1265 	 * processing.
1266 	 */
1267 	if (!skb || xdp_tx)
1268 		return netvsc_send_pkt(device, packet, net_device, pb, skb);
1269 
1270 	/* batch packets in send buffer if possible */
1271 	msdp = &nvchan->msd;
1272 	if (msdp->pkt)
1273 		msd_len = msdp->pkt->total_data_buflen;
1274 
1275 	try_batch =  msd_len > 0 && msdp->count < net_device->max_pkt;
1276 	if (try_batch && msd_len + pktlen + net_device->pkt_align <
1277 	    net_device->send_section_size) {
1278 		section_index = msdp->pkt->send_buf_index;
1279 
1280 	} else if (try_batch && msd_len + packet->rmsg_size <
1281 		   net_device->send_section_size) {
1282 		section_index = msdp->pkt->send_buf_index;
1283 		packet->cp_partial = true;
1284 
1285 	} else if (pktlen + net_device->pkt_align <
1286 		   net_device->send_section_size) {
1287 		section_index = netvsc_get_next_send_section(net_device);
1288 		if (unlikely(section_index == NETVSC_INVALID_INDEX)) {
1289 			++ndev_ctx->eth_stats.tx_send_full;
1290 		} else {
1291 			move_pkt_msd(&msd_send, &msd_skb, msdp);
1292 			msd_len = 0;
1293 		}
1294 	}
1295 
1296 	/* Keep aggregating only if stack says more data is coming
1297 	 * and not doing mixed modes send and not flow blocked
1298 	 */
1299 	xmit_more = netdev_xmit_more() &&
1300 		!packet->cp_partial &&
1301 		!netif_xmit_stopped(netdev_get_tx_queue(ndev, packet->q_idx));
1302 
1303 	if (section_index != NETVSC_INVALID_INDEX) {
1304 		netvsc_copy_to_send_buf(net_device,
1305 					section_index, msd_len,
1306 					packet, rndis_msg, pb, xmit_more);
1307 
1308 		packet->send_buf_index = section_index;
1309 
1310 		if (packet->cp_partial) {
1311 			packet->page_buf_cnt--;
1312 			packet->total_data_buflen = msd_len + packet->rmsg_size;
1313 		} else {
1314 			packet->page_buf_cnt = 0;
1315 			packet->total_data_buflen += msd_len;
1316 		}
1317 
1318 		if (msdp->pkt) {
1319 			packet->total_packets += msdp->pkt->total_packets;
1320 			packet->total_bytes += msdp->pkt->total_bytes;
1321 		}
1322 
1323 		if (msdp->skb)
1324 			dev_consume_skb_any(msdp->skb);
1325 
1326 		if (xmit_more) {
1327 			msdp->skb = skb;
1328 			msdp->pkt = packet;
1329 			msdp->count++;
1330 		} else {
1331 			cur_send = packet;
1332 			msdp->skb = NULL;
1333 			msdp->pkt = NULL;
1334 			msdp->count = 0;
1335 		}
1336 	} else {
1337 		move_pkt_msd(&msd_send, &msd_skb, msdp);
1338 		cur_send = packet;
1339 	}
1340 
1341 	if (msd_send) {
1342 		int m_ret = netvsc_send_pkt(device, msd_send, net_device,
1343 					    NULL, msd_skb);
1344 
1345 		if (m_ret != 0) {
1346 			netvsc_free_send_slot(net_device,
1347 					      msd_send->send_buf_index);
1348 			dev_kfree_skb_any(msd_skb);
1349 		}
1350 	}
1351 
1352 	if (cur_send)
1353 		ret = netvsc_send_pkt(device, cur_send, net_device, pb, skb);
1354 
1355 	if (ret != 0 && section_index != NETVSC_INVALID_INDEX)
1356 		netvsc_free_send_slot(net_device, section_index);
1357 
1358 	return ret;
1359 }
1360 
1361 /* Send pending recv completions */
1362 static int send_recv_completions(struct net_device *ndev,
1363 				 struct netvsc_device *nvdev,
1364 				 struct netvsc_channel *nvchan)
1365 {
1366 	struct multi_recv_comp *mrc = &nvchan->mrc;
1367 	struct recv_comp_msg {
1368 		struct nvsp_message_header hdr;
1369 		u32 status;
1370 	}  __packed;
1371 	struct recv_comp_msg msg = {
1372 		.hdr.msg_type = NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE,
1373 	};
1374 	int ret;
1375 
1376 	while (mrc->first != mrc->next) {
1377 		const struct recv_comp_data *rcd
1378 			= mrc->slots + mrc->first;
1379 
1380 		msg.status = rcd->status;
1381 		ret = vmbus_sendpacket(nvchan->channel, &msg, sizeof(msg),
1382 				       rcd->tid, VM_PKT_COMP, 0);
1383 		if (unlikely(ret)) {
1384 			struct net_device_context *ndev_ctx = netdev_priv(ndev);
1385 
1386 			++ndev_ctx->eth_stats.rx_comp_busy;
1387 			return ret;
1388 		}
1389 
1390 		if (++mrc->first == nvdev->recv_completion_cnt)
1391 			mrc->first = 0;
1392 	}
1393 
1394 	/* receive completion ring has been emptied */
1395 	if (unlikely(nvdev->destroy))
1396 		wake_up(&nvdev->wait_drain);
1397 
1398 	return 0;
1399 }
1400 
1401 /* Count how many receive completions are outstanding */
1402 static void recv_comp_slot_avail(const struct netvsc_device *nvdev,
1403 				 const struct multi_recv_comp *mrc,
1404 				 u32 *filled, u32 *avail)
1405 {
1406 	u32 count = nvdev->recv_completion_cnt;
1407 
1408 	if (mrc->next >= mrc->first)
1409 		*filled = mrc->next - mrc->first;
1410 	else
1411 		*filled = (count - mrc->first) + mrc->next;
1412 
1413 	*avail = count - *filled - 1;
1414 }
1415 
1416 /* Add receive complete to ring to send to host. */
1417 static void enq_receive_complete(struct net_device *ndev,
1418 				 struct netvsc_device *nvdev, u16 q_idx,
1419 				 u64 tid, u32 status)
1420 {
1421 	struct netvsc_channel *nvchan = &nvdev->chan_table[q_idx];
1422 	struct multi_recv_comp *mrc = &nvchan->mrc;
1423 	struct recv_comp_data *rcd;
1424 	u32 filled, avail;
1425 
1426 	recv_comp_slot_avail(nvdev, mrc, &filled, &avail);
1427 
1428 	if (unlikely(filled > NAPI_POLL_WEIGHT)) {
1429 		send_recv_completions(ndev, nvdev, nvchan);
1430 		recv_comp_slot_avail(nvdev, mrc, &filled, &avail);
1431 	}
1432 
1433 	if (unlikely(!avail)) {
1434 		netdev_err(ndev, "Recv_comp full buf q:%hd, tid:%llx\n",
1435 			   q_idx, tid);
1436 		return;
1437 	}
1438 
1439 	rcd = mrc->slots + mrc->next;
1440 	rcd->tid = tid;
1441 	rcd->status = status;
1442 
1443 	if (++mrc->next == nvdev->recv_completion_cnt)
1444 		mrc->next = 0;
1445 }
1446 
1447 static int netvsc_receive(struct net_device *ndev,
1448 			  struct netvsc_device *net_device,
1449 			  struct netvsc_channel *nvchan,
1450 			  const struct vmpacket_descriptor *desc)
1451 {
1452 	struct net_device_context *net_device_ctx = netdev_priv(ndev);
1453 	struct vmbus_channel *channel = nvchan->channel;
1454 	const struct vmtransfer_page_packet_header *vmxferpage_packet
1455 		= container_of(desc, const struct vmtransfer_page_packet_header, d);
1456 	const struct nvsp_message *nvsp = hv_pkt_data(desc);
1457 	u32 msglen = hv_pkt_datalen(desc);
1458 	u16 q_idx = channel->offermsg.offer.sub_channel_index;
1459 	char *recv_buf = net_device->recv_buf;
1460 	u32 status = NVSP_STAT_SUCCESS;
1461 	int i;
1462 	int count = 0;
1463 
1464 	/* Ensure packet is big enough to read header fields */
1465 	if (msglen < sizeof(struct nvsp_message_header)) {
1466 		netif_err(net_device_ctx, rx_err, ndev,
1467 			  "invalid nvsp header, length too small: %u\n",
1468 			  msglen);
1469 		return 0;
1470 	}
1471 
1472 	/* Make sure this is a valid nvsp packet */
1473 	if (unlikely(nvsp->hdr.msg_type != NVSP_MSG1_TYPE_SEND_RNDIS_PKT)) {
1474 		netif_err(net_device_ctx, rx_err, ndev,
1475 			  "Unknown nvsp packet type received %u\n",
1476 			  nvsp->hdr.msg_type);
1477 		return 0;
1478 	}
1479 
1480 	/* Validate xfer page pkt header */
1481 	if ((desc->offset8 << 3) < sizeof(struct vmtransfer_page_packet_header)) {
1482 		netif_err(net_device_ctx, rx_err, ndev,
1483 			  "Invalid xfer page pkt, offset too small: %u\n",
1484 			  desc->offset8 << 3);
1485 		return 0;
1486 	}
1487 
1488 	if (unlikely(vmxferpage_packet->xfer_pageset_id != NETVSC_RECEIVE_BUFFER_ID)) {
1489 		netif_err(net_device_ctx, rx_err, ndev,
1490 			  "Invalid xfer page set id - expecting %x got %x\n",
1491 			  NETVSC_RECEIVE_BUFFER_ID,
1492 			  vmxferpage_packet->xfer_pageset_id);
1493 		return 0;
1494 	}
1495 
1496 	count = vmxferpage_packet->range_cnt;
1497 
1498 	/* Check count for a valid value */
1499 	if (NETVSC_XFER_HEADER_SIZE(count) > desc->offset8 << 3) {
1500 		netif_err(net_device_ctx, rx_err, ndev,
1501 			  "Range count is not valid: %d\n",
1502 			  count);
1503 		return 0;
1504 	}
1505 
1506 	/* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
1507 	for (i = 0; i < count; i++) {
1508 		u32 offset = vmxferpage_packet->ranges[i].byte_offset;
1509 		u32 buflen = vmxferpage_packet->ranges[i].byte_count;
1510 		void *data;
1511 		int ret;
1512 
1513 		if (unlikely(offset > net_device->recv_buf_size ||
1514 			     buflen > net_device->recv_buf_size - offset)) {
1515 			nvchan->rsc.cnt = 0;
1516 			status = NVSP_STAT_FAIL;
1517 			netif_err(net_device_ctx, rx_err, ndev,
1518 				  "Packet offset:%u + len:%u too big\n",
1519 				  offset, buflen);
1520 
1521 			continue;
1522 		}
1523 
1524 		/* We're going to copy (sections of) the packet into nvchan->recv_buf;
1525 		 * make sure that nvchan->recv_buf is large enough to hold the packet.
1526 		 */
1527 		if (unlikely(buflen > net_device->recv_section_size)) {
1528 			nvchan->rsc.cnt = 0;
1529 			status = NVSP_STAT_FAIL;
1530 			netif_err(net_device_ctx, rx_err, ndev,
1531 				  "Packet too big: buflen=%u recv_section_size=%u\n",
1532 				  buflen, net_device->recv_section_size);
1533 
1534 			continue;
1535 		}
1536 
1537 		data = recv_buf + offset;
1538 
1539 		nvchan->rsc.is_last = (i == count - 1);
1540 
1541 		trace_rndis_recv(ndev, q_idx, data);
1542 
1543 		/* Pass it to the upper layer */
1544 		ret = rndis_filter_receive(ndev, net_device,
1545 					   nvchan, data, buflen);
1546 
1547 		if (unlikely(ret != NVSP_STAT_SUCCESS)) {
1548 			/* Drop incomplete packet */
1549 			nvchan->rsc.cnt = 0;
1550 			status = NVSP_STAT_FAIL;
1551 		}
1552 	}
1553 
1554 	enq_receive_complete(ndev, net_device, q_idx,
1555 			     vmxferpage_packet->d.trans_id, status);
1556 
1557 	return count;
1558 }
1559 
1560 static void netvsc_send_table(struct net_device *ndev,
1561 			      struct netvsc_device *nvscdev,
1562 			      const struct nvsp_message *nvmsg,
1563 			      u32 msglen)
1564 {
1565 	struct net_device_context *net_device_ctx = netdev_priv(ndev);
1566 	u32 count, offset, *tab;
1567 	int i;
1568 
1569 	/* Ensure packet is big enough to read send_table fields */
1570 	if (msglen < sizeof(struct nvsp_message_header) +
1571 		     sizeof(struct nvsp_5_send_indirect_table)) {
1572 		netdev_err(ndev, "nvsp_v5_msg length too small: %u\n", msglen);
1573 		return;
1574 	}
1575 
1576 	count = nvmsg->msg.v5_msg.send_table.count;
1577 	offset = nvmsg->msg.v5_msg.send_table.offset;
1578 
1579 	if (count != VRSS_SEND_TAB_SIZE) {
1580 		netdev_err(ndev, "Received wrong send-table size:%u\n", count);
1581 		return;
1582 	}
1583 
1584 	/* If negotiated version <= NVSP_PROTOCOL_VERSION_6, the offset may be
1585 	 * wrong due to a host bug. So fix the offset here.
1586 	 */
1587 	if (nvscdev->nvsp_version <= NVSP_PROTOCOL_VERSION_6 &&
1588 	    msglen >= sizeof(struct nvsp_message_header) +
1589 	    sizeof(union nvsp_6_message_uber) + count * sizeof(u32))
1590 		offset = sizeof(struct nvsp_message_header) +
1591 			 sizeof(union nvsp_6_message_uber);
1592 
1593 	/* Boundary check for all versions */
1594 	if (msglen < count * sizeof(u32) || offset > msglen - count * sizeof(u32)) {
1595 		netdev_err(ndev, "Received send-table offset too big:%u\n",
1596 			   offset);
1597 		return;
1598 	}
1599 
1600 	tab = (void *)nvmsg + offset;
1601 
1602 	for (i = 0; i < count; i++)
1603 		net_device_ctx->tx_table[i] = tab[i];
1604 }
1605 
1606 static void netvsc_send_vf(struct net_device *ndev,
1607 			   const struct nvsp_message *nvmsg,
1608 			   u32 msglen)
1609 {
1610 	struct net_device_context *net_device_ctx = netdev_priv(ndev);
1611 
1612 	/* Ensure packet is big enough to read its fields */
1613 	if (msglen < sizeof(struct nvsp_message_header) +
1614 		     sizeof(struct nvsp_4_send_vf_association)) {
1615 		netdev_err(ndev, "nvsp_v4_msg length too small: %u\n", msglen);
1616 		return;
1617 	}
1618 
1619 	net_device_ctx->vf_alloc = nvmsg->msg.v4_msg.vf_assoc.allocated;
1620 	net_device_ctx->vf_serial = nvmsg->msg.v4_msg.vf_assoc.serial;
1621 
1622 	if (net_device_ctx->vf_alloc)
1623 		complete(&net_device_ctx->vf_add);
1624 
1625 	netdev_info(ndev, "VF slot %u %s\n",
1626 		    net_device_ctx->vf_serial,
1627 		    net_device_ctx->vf_alloc ? "added" : "removed");
1628 }
1629 
1630 static void netvsc_receive_inband(struct net_device *ndev,
1631 				  struct netvsc_device *nvscdev,
1632 				  const struct vmpacket_descriptor *desc)
1633 {
1634 	const struct nvsp_message *nvmsg = hv_pkt_data(desc);
1635 	u32 msglen = hv_pkt_datalen(desc);
1636 
1637 	/* Ensure packet is big enough to read header fields */
1638 	if (msglen < sizeof(struct nvsp_message_header)) {
1639 		netdev_err(ndev, "inband nvsp_message length too small: %u\n", msglen);
1640 		return;
1641 	}
1642 
1643 	switch (nvmsg->hdr.msg_type) {
1644 	case NVSP_MSG5_TYPE_SEND_INDIRECTION_TABLE:
1645 		netvsc_send_table(ndev, nvscdev, nvmsg, msglen);
1646 		break;
1647 
1648 	case NVSP_MSG4_TYPE_SEND_VF_ASSOCIATION:
1649 		if (hv_is_isolation_supported())
1650 			netdev_err(ndev, "Ignore VF_ASSOCIATION msg from the host supporting isolation\n");
1651 		else
1652 			netvsc_send_vf(ndev, nvmsg, msglen);
1653 		break;
1654 	}
1655 }
1656 
1657 static int netvsc_process_raw_pkt(struct hv_device *device,
1658 				  struct netvsc_channel *nvchan,
1659 				  struct netvsc_device *net_device,
1660 				  struct net_device *ndev,
1661 				  const struct vmpacket_descriptor *desc,
1662 				  int budget)
1663 {
1664 	struct vmbus_channel *channel = nvchan->channel;
1665 	const struct nvsp_message *nvmsg = hv_pkt_data(desc);
1666 
1667 	trace_nvsp_recv(ndev, channel, nvmsg);
1668 
1669 	switch (desc->type) {
1670 	case VM_PKT_COMP:
1671 		netvsc_send_completion(ndev, net_device, channel, desc, budget);
1672 		break;
1673 
1674 	case VM_PKT_DATA_USING_XFER_PAGES:
1675 		return netvsc_receive(ndev, net_device, nvchan, desc);
1676 
1677 	case VM_PKT_DATA_INBAND:
1678 		netvsc_receive_inband(ndev, net_device, desc);
1679 		break;
1680 
1681 	default:
1682 		netdev_err(ndev, "unhandled packet type %d, tid %llx\n",
1683 			   desc->type, desc->trans_id);
1684 		break;
1685 	}
1686 
1687 	return 0;
1688 }
1689 
1690 static struct hv_device *netvsc_channel_to_device(struct vmbus_channel *channel)
1691 {
1692 	struct vmbus_channel *primary = channel->primary_channel;
1693 
1694 	return primary ? primary->device_obj : channel->device_obj;
1695 }
1696 
1697 /* Network processing softirq
1698  * Process data in incoming ring buffer from host
1699  * Stops when ring is empty or budget is met or exceeded.
1700  */
1701 int netvsc_poll(struct napi_struct *napi, int budget)
1702 {
1703 	struct netvsc_channel *nvchan
1704 		= container_of(napi, struct netvsc_channel, napi);
1705 	struct netvsc_device *net_device = nvchan->net_device;
1706 	struct vmbus_channel *channel = nvchan->channel;
1707 	struct hv_device *device = netvsc_channel_to_device(channel);
1708 	struct net_device *ndev = hv_get_drvdata(device);
1709 	int work_done = 0;
1710 	int ret;
1711 
1712 	/* If starting a new interval */
1713 	if (!nvchan->desc)
1714 		nvchan->desc = hv_pkt_iter_first(channel);
1715 
1716 	nvchan->xdp_flush = false;
1717 
1718 	while (nvchan->desc && work_done < budget) {
1719 		work_done += netvsc_process_raw_pkt(device, nvchan, net_device,
1720 						    ndev, nvchan->desc, budget);
1721 		nvchan->desc = hv_pkt_iter_next(channel, nvchan->desc);
1722 	}
1723 
1724 	if (nvchan->xdp_flush)
1725 		xdp_do_flush();
1726 
1727 	/* Send any pending receive completions */
1728 	ret = send_recv_completions(ndev, net_device, nvchan);
1729 
1730 	/* If it did not exhaust NAPI budget this time
1731 	 *  and not doing busy poll
1732 	 * then re-enable host interrupts
1733 	 *  and reschedule if ring is not empty
1734 	 *   or sending receive completion failed.
1735 	 */
1736 	if (work_done < budget &&
1737 	    napi_complete_done(napi, work_done) &&
1738 	    (ret || hv_end_read(&channel->inbound)) &&
1739 	    napi_schedule_prep(napi)) {
1740 		hv_begin_read(&channel->inbound);
1741 		__napi_schedule(napi);
1742 	}
1743 
1744 	/* Driver may overshoot since multiple packets per descriptor */
1745 	return min(work_done, budget);
1746 }
1747 
1748 /* Call back when data is available in host ring buffer.
1749  * Processing is deferred until network softirq (NAPI)
1750  */
1751 void netvsc_channel_cb(void *context)
1752 {
1753 	struct netvsc_channel *nvchan = context;
1754 	struct vmbus_channel *channel = nvchan->channel;
1755 	struct hv_ring_buffer_info *rbi = &channel->inbound;
1756 
1757 	/* preload first vmpacket descriptor */
1758 	prefetch(hv_get_ring_buffer(rbi) + rbi->priv_read_index);
1759 
1760 	if (napi_schedule_prep(&nvchan->napi)) {
1761 		/* disable interrupts from host */
1762 		hv_begin_read(rbi);
1763 
1764 		__napi_schedule_irqoff(&nvchan->napi);
1765 	}
1766 }
1767 
1768 /*
1769  * netvsc_device_add - Callback when the device belonging to this
1770  * driver is added
1771  */
1772 struct netvsc_device *netvsc_device_add(struct hv_device *device,
1773 				const struct netvsc_device_info *device_info)
1774 {
1775 	int i, ret = 0;
1776 	struct netvsc_device *net_device;
1777 	struct net_device *ndev = hv_get_drvdata(device);
1778 	struct net_device_context *net_device_ctx = netdev_priv(ndev);
1779 
1780 	net_device = alloc_net_device();
1781 	if (!net_device)
1782 		return ERR_PTR(-ENOMEM);
1783 
1784 	for (i = 0; i < VRSS_SEND_TAB_SIZE; i++)
1785 		net_device_ctx->tx_table[i] = 0;
1786 
1787 	/* Because the device uses NAPI, all the interrupt batching and
1788 	 * control is done via Net softirq, not the channel handling
1789 	 */
1790 	set_channel_read_mode(device->channel, HV_CALL_ISR);
1791 
1792 	/* If we're reopening the device we may have multiple queues, fill the
1793 	 * chn_table with the default channel to use it before subchannels are
1794 	 * opened.
1795 	 * Initialize the channel state before we open;
1796 	 * we can be interrupted as soon as we open the channel.
1797 	 */
1798 
1799 	for (i = 0; i < VRSS_CHANNEL_MAX; i++) {
1800 		struct netvsc_channel *nvchan = &net_device->chan_table[i];
1801 
1802 		nvchan->channel = device->channel;
1803 		nvchan->net_device = net_device;
1804 		u64_stats_init(&nvchan->tx_stats.syncp);
1805 		u64_stats_init(&nvchan->rx_stats.syncp);
1806 
1807 		ret = xdp_rxq_info_reg(&nvchan->xdp_rxq, ndev, i, 0);
1808 
1809 		if (ret) {
1810 			netdev_err(ndev, "xdp_rxq_info_reg fail: %d\n", ret);
1811 			goto cleanup2;
1812 		}
1813 
1814 		ret = xdp_rxq_info_reg_mem_model(&nvchan->xdp_rxq,
1815 						 MEM_TYPE_PAGE_SHARED, NULL);
1816 
1817 		if (ret) {
1818 			netdev_err(ndev, "xdp reg_mem_model fail: %d\n", ret);
1819 			goto cleanup2;
1820 		}
1821 	}
1822 
1823 	/* Enable NAPI handler before init callbacks */
1824 	netif_napi_add(ndev, &net_device->chan_table[0].napi, netvsc_poll);
1825 	napi_enable(&net_device->chan_table[0].napi);
1826 	netif_queue_set_napi(ndev, 0, NETDEV_QUEUE_TYPE_RX,
1827 			     &net_device->chan_table[0].napi);
1828 	netif_queue_set_napi(ndev, 0, NETDEV_QUEUE_TYPE_TX,
1829 			     &net_device->chan_table[0].napi);
1830 
1831 	/* Open the channel */
1832 	device->channel->next_request_id_callback = vmbus_next_request_id;
1833 	device->channel->request_addr_callback = vmbus_request_addr;
1834 	device->channel->rqstor_size = netvsc_rqstor_size(netvsc_ring_bytes);
1835 	device->channel->max_pkt_size = NETVSC_MAX_PKT_SIZE;
1836 
1837 	ret = vmbus_open(device->channel, netvsc_ring_bytes,
1838 			 netvsc_ring_bytes,  NULL, 0,
1839 			 netvsc_channel_cb, net_device->chan_table);
1840 
1841 	if (ret != 0) {
1842 		netdev_err(ndev, "unable to open channel: %d\n", ret);
1843 		goto cleanup;
1844 	}
1845 
1846 	/* Channel is opened */
1847 	netdev_dbg(ndev, "hv_netvsc channel opened successfully\n");
1848 
1849 	/* Connect with the NetVsp */
1850 	ret = netvsc_connect_vsp(device, net_device, device_info);
1851 	if (ret != 0) {
1852 		netdev_err(ndev,
1853 			"unable to connect to NetVSP - %d\n", ret);
1854 		goto close;
1855 	}
1856 
1857 	/* Writing nvdev pointer unlocks netvsc_send(), make sure chn_table is
1858 	 * populated.
1859 	 */
1860 	rcu_assign_pointer(net_device_ctx->nvdev, net_device);
1861 
1862 	return net_device;
1863 
1864 close:
1865 	RCU_INIT_POINTER(net_device_ctx->nvdev, NULL);
1866 
1867 	/* Now, we can close the channel safely */
1868 	vmbus_close(device->channel);
1869 
1870 cleanup:
1871 	netif_queue_set_napi(ndev, 0, NETDEV_QUEUE_TYPE_TX, NULL);
1872 	netif_queue_set_napi(ndev, 0, NETDEV_QUEUE_TYPE_RX, NULL);
1873 	napi_disable(&net_device->chan_table[0].napi);
1874 	netif_napi_del(&net_device->chan_table[0].napi);
1875 
1876 cleanup2:
1877 	free_netvsc_device(&net_device->rcu);
1878 
1879 	return ERR_PTR(ret);
1880 }
1881