2 * Copyright (c) 2009-2012 Microsoft Corp.
3 * Copyright (c) 2010-2012 Citrix Inc.
4 * Copyright (c) 2012 NetApp Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * HyperV vmbus network VSC (virtual services client) module
37 #include <sys/param.h>
38 #include <sys/kernel.h>
39 #include <sys/socket.h>
42 #include <net/if_var.h>
43 #include <net/if_arp.h>
44 #include <machine/bus.h>
45 #include <machine/atomic.h>
47 #include <dev/hyperv/include/hyperv.h>
48 #include "hv_net_vsc.h"
50 #include "hv_rndis_filter.h"
52 MALLOC_DEFINE(M_NETVSC, "netvsc", "Hyper-V netvsc driver");
55 * Forward declarations
57 static void hv_nv_on_channel_callback(void *context);
58 static int hv_nv_init_send_buffer_with_net_vsp(struct hv_device *device);
59 static int hv_nv_init_rx_buffer_with_net_vsp(struct hv_device *device);
60 static int hv_nv_destroy_send_buffer(netvsc_dev *net_dev);
61 static int hv_nv_destroy_rx_buffer(netvsc_dev *net_dev);
62 static int hv_nv_connect_to_vsp(struct hv_device *device);
63 static void hv_nv_on_send_completion(netvsc_dev *net_dev,
64 struct hv_device *device, hv_vm_packet_descriptor *pkt);
65 static void hv_nv_on_receive(netvsc_dev *net_dev,
66 struct hv_device *device, struct hv_vmbus_channel *chan,
67 hv_vm_packet_descriptor *pkt);
72 static inline netvsc_dev *
73 hv_nv_alloc_net_device(struct hv_device *device)
76 hn_softc_t *sc = device_get_softc(device->device);
78 net_dev = malloc(sizeof(netvsc_dev), M_NETVSC, M_WAITOK | M_ZERO);
80 net_dev->dev = device;
81 net_dev->destroy = FALSE;
82 sc->net_dev = net_dev;
90 static inline netvsc_dev *
91 hv_nv_get_outbound_net_device(struct hv_device *device)
93 hn_softc_t *sc = device_get_softc(device->device);
94 netvsc_dev *net_dev = sc->net_dev;;
96 if ((net_dev != NULL) && net_dev->destroy) {
106 static inline netvsc_dev *
107 hv_nv_get_inbound_net_device(struct hv_device *device)
109 hn_softc_t *sc = device_get_softc(device->device);
110 netvsc_dev *net_dev = sc->net_dev;;
112 if (net_dev == NULL) {
116 * When the device is being destroyed; we only
117 * permit incoming packets if and only if there
118 * are outstanding sends.
120 if (net_dev->destroy) {
128 hv_nv_get_next_send_section(netvsc_dev *net_dev)
130 unsigned long bitsmap_words = net_dev->bitsmap_words;
131 unsigned long *bitsmap = net_dev->send_section_bitsmap;
133 int ret = NVSP_1_CHIMNEY_SEND_INVALID_SECTION_INDEX;
136 for (i = 0; i < bitsmap_words; i++) {
137 idx = ffsl(~bitsmap[i]);
142 KASSERT(i * BITS_PER_LONG + idx < net_dev->send_section_count,
143 ("invalid i %d and idx %lu", i, idx));
145 if (atomic_testandset_long(&bitsmap[i], idx))
148 ret = i * BITS_PER_LONG + idx;
156 * Net VSC initialize receive buffer with net VSP
158 * Net VSP: Network virtual services client, also known as the
159 * Hyper-V extensible switch and the synthetic data path.
162 hv_nv_init_rx_buffer_with_net_vsp(struct hv_device *device)
168 net_dev = hv_nv_get_outbound_net_device(device);
173 net_dev->rx_buf = contigmalloc(net_dev->rx_buf_size, M_NETVSC,
174 M_ZERO, 0UL, BUS_SPACE_MAXADDR, PAGE_SIZE, 0);
177 * Establish the GPADL handle for this buffer on this channel.
178 * Note: This call uses the vmbus connection rather than the
179 * channel to establish the gpadl handle.
180 * GPADL: Guest physical address descriptor list.
182 ret = hv_vmbus_channel_establish_gpadl(
183 device->channel, net_dev->rx_buf,
184 net_dev->rx_buf_size, &net_dev->rx_buf_gpadl_handle);
189 /* sema_wait(&ext->channel_init_sema); KYS CHECK */
191 /* Notify the NetVsp of the gpadl handle */
192 init_pkt = &net_dev->channel_init_packet;
194 memset(init_pkt, 0, sizeof(nvsp_msg));
196 init_pkt->hdr.msg_type = nvsp_msg_1_type_send_rx_buf;
197 init_pkt->msgs.vers_1_msgs.send_rx_buf.gpadl_handle =
198 net_dev->rx_buf_gpadl_handle;
199 init_pkt->msgs.vers_1_msgs.send_rx_buf.id =
200 NETVSC_RECEIVE_BUFFER_ID;
202 /* Send the gpadl notification request */
204 ret = hv_vmbus_channel_send_packet(device->channel, init_pkt,
205 sizeof(nvsp_msg), (uint64_t)(uintptr_t)init_pkt,
206 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND,
207 HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
212 sema_wait(&net_dev->channel_init_sema);
214 /* Check the response */
215 if (init_pkt->msgs.vers_1_msgs.send_rx_buf_complete.status
216 != nvsp_status_success) {
221 net_dev->rx_section_count =
222 init_pkt->msgs.vers_1_msgs.send_rx_buf_complete.num_sections;
224 net_dev->rx_sections = malloc(net_dev->rx_section_count *
225 sizeof(nvsp_1_rx_buf_section), M_NETVSC, M_WAITOK);
226 memcpy(net_dev->rx_sections,
227 init_pkt->msgs.vers_1_msgs.send_rx_buf_complete.sections,
228 net_dev->rx_section_count * sizeof(nvsp_1_rx_buf_section));
232 * For first release, there should only be 1 section that represents
233 * the entire receive buffer
235 if (net_dev->rx_section_count != 1
236 || net_dev->rx_sections->offset != 0) {
244 hv_nv_destroy_rx_buffer(net_dev);
251 * Net VSC initialize send buffer with net VSP
254 hv_nv_init_send_buffer_with_net_vsp(struct hv_device *device)
260 net_dev = hv_nv_get_outbound_net_device(device);
265 net_dev->send_buf = contigmalloc(net_dev->send_buf_size, M_NETVSC,
266 M_ZERO, 0UL, BUS_SPACE_MAXADDR, PAGE_SIZE, 0);
267 if (net_dev->send_buf == NULL) {
273 * Establish the gpadl handle for this buffer on this channel.
274 * Note: This call uses the vmbus connection rather than the
275 * channel to establish the gpadl handle.
277 ret = hv_vmbus_channel_establish_gpadl(device->channel,
278 net_dev->send_buf, net_dev->send_buf_size,
279 &net_dev->send_buf_gpadl_handle);
284 /* Notify the NetVsp of the gpadl handle */
286 init_pkt = &net_dev->channel_init_packet;
288 memset(init_pkt, 0, sizeof(nvsp_msg));
290 init_pkt->hdr.msg_type = nvsp_msg_1_type_send_send_buf;
291 init_pkt->msgs.vers_1_msgs.send_rx_buf.gpadl_handle =
292 net_dev->send_buf_gpadl_handle;
293 init_pkt->msgs.vers_1_msgs.send_rx_buf.id =
294 NETVSC_SEND_BUFFER_ID;
296 /* Send the gpadl notification request */
298 ret = hv_vmbus_channel_send_packet(device->channel, init_pkt,
299 sizeof(nvsp_msg), (uint64_t)init_pkt,
300 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND,
301 HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
306 sema_wait(&net_dev->channel_init_sema);
308 /* Check the response */
309 if (init_pkt->msgs.vers_1_msgs.send_send_buf_complete.status
310 != nvsp_status_success) {
315 net_dev->send_section_size =
316 init_pkt->msgs.vers_1_msgs.send_send_buf_complete.section_size;
317 net_dev->send_section_count =
318 net_dev->send_buf_size / net_dev->send_section_size;
319 net_dev->bitsmap_words = howmany(net_dev->send_section_count,
321 net_dev->send_section_bitsmap =
322 malloc(net_dev->bitsmap_words * sizeof(long), M_NETVSC,
328 hv_nv_destroy_send_buffer(net_dev);
335 * Net VSC destroy receive buffer
338 hv_nv_destroy_rx_buffer(netvsc_dev *net_dev)
340 nvsp_msg *revoke_pkt;
344 * If we got a section count, it means we received a
345 * send_rx_buf_complete msg
346 * (ie sent nvsp_msg_1_type_send_rx_buf msg) therefore,
347 * we need to send a revoke msg here
349 if (net_dev->rx_section_count) {
350 /* Send the revoke receive buffer */
351 revoke_pkt = &net_dev->revoke_packet;
352 memset(revoke_pkt, 0, sizeof(nvsp_msg));
354 revoke_pkt->hdr.msg_type = nvsp_msg_1_type_revoke_rx_buf;
355 revoke_pkt->msgs.vers_1_msgs.revoke_rx_buf.id =
356 NETVSC_RECEIVE_BUFFER_ID;
358 ret = hv_vmbus_channel_send_packet(net_dev->dev->channel,
359 revoke_pkt, sizeof(nvsp_msg),
360 (uint64_t)(uintptr_t)revoke_pkt,
361 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND, 0);
364 * If we failed here, we might as well return and have a leak
365 * rather than continue and a bugchk
372 /* Tear down the gpadl on the vsp end */
373 if (net_dev->rx_buf_gpadl_handle) {
374 ret = hv_vmbus_channel_teardown_gpdal(net_dev->dev->channel,
375 net_dev->rx_buf_gpadl_handle);
377 * If we failed here, we might as well return and have a leak
378 * rather than continue and a bugchk
383 net_dev->rx_buf_gpadl_handle = 0;
386 if (net_dev->rx_buf) {
387 /* Free up the receive buffer */
388 contigfree(net_dev->rx_buf, net_dev->rx_buf_size, M_NETVSC);
389 net_dev->rx_buf = NULL;
392 if (net_dev->rx_sections) {
393 free(net_dev->rx_sections, M_NETVSC);
394 net_dev->rx_sections = NULL;
395 net_dev->rx_section_count = 0;
402 * Net VSC destroy send buffer
405 hv_nv_destroy_send_buffer(netvsc_dev *net_dev)
407 nvsp_msg *revoke_pkt;
411 * If we got a section count, it means we received a
412 * send_rx_buf_complete msg
413 * (ie sent nvsp_msg_1_type_send_rx_buf msg) therefore,
414 * we need to send a revoke msg here
416 if (net_dev->send_section_size) {
417 /* Send the revoke send buffer */
418 revoke_pkt = &net_dev->revoke_packet;
419 memset(revoke_pkt, 0, sizeof(nvsp_msg));
421 revoke_pkt->hdr.msg_type =
422 nvsp_msg_1_type_revoke_send_buf;
423 revoke_pkt->msgs.vers_1_msgs.revoke_send_buf.id =
424 NETVSC_SEND_BUFFER_ID;
426 ret = hv_vmbus_channel_send_packet(net_dev->dev->channel,
427 revoke_pkt, sizeof(nvsp_msg),
428 (uint64_t)(uintptr_t)revoke_pkt,
429 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND, 0);
431 * If we failed here, we might as well return and have a leak
432 * rather than continue and a bugchk
439 /* Tear down the gpadl on the vsp end */
440 if (net_dev->send_buf_gpadl_handle) {
441 ret = hv_vmbus_channel_teardown_gpdal(net_dev->dev->channel,
442 net_dev->send_buf_gpadl_handle);
445 * If we failed here, we might as well return and have a leak
446 * rather than continue and a bugchk
451 net_dev->send_buf_gpadl_handle = 0;
454 if (net_dev->send_buf) {
455 /* Free up the receive buffer */
456 contigfree(net_dev->send_buf, net_dev->send_buf_size, M_NETVSC);
457 net_dev->send_buf = NULL;
460 if (net_dev->send_section_bitsmap) {
461 free(net_dev->send_section_bitsmap, M_NETVSC);
469 * Attempt to negotiate the caller-specified NVSP version
471 * For NVSP v2, Server 2008 R2 does not set
472 * init_pkt->msgs.init_msgs.init_compl.negotiated_prot_vers
473 * to the negotiated version, so we cannot rely on that.
476 hv_nv_negotiate_nvsp_protocol(struct hv_device *device, netvsc_dev *net_dev,
482 init_pkt = &net_dev->channel_init_packet;
483 memset(init_pkt, 0, sizeof(nvsp_msg));
484 init_pkt->hdr.msg_type = nvsp_msg_type_init;
487 * Specify parameter as the only acceptable protocol version
489 init_pkt->msgs.init_msgs.init.p1.protocol_version = nvsp_ver;
490 init_pkt->msgs.init_msgs.init.protocol_version_2 = nvsp_ver;
492 /* Send the init request */
493 ret = hv_vmbus_channel_send_packet(device->channel, init_pkt,
494 sizeof(nvsp_msg), (uint64_t)(uintptr_t)init_pkt,
495 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND,
496 HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
500 sema_wait(&net_dev->channel_init_sema);
502 if (init_pkt->msgs.init_msgs.init_compl.status != nvsp_status_success)
509 * Send NDIS version 2 config packet containing MTU.
511 * Not valid for NDIS version 1.
514 hv_nv_send_ndis_config(struct hv_device *device, uint32_t mtu)
520 net_dev = hv_nv_get_outbound_net_device(device);
525 * Set up configuration packet, write MTU
526 * Indicate we are capable of handling VLAN tags
528 init_pkt = &net_dev->channel_init_packet;
529 memset(init_pkt, 0, sizeof(nvsp_msg));
530 init_pkt->hdr.msg_type = nvsp_msg_2_type_send_ndis_config;
531 init_pkt->msgs.vers_2_msgs.send_ndis_config.mtu = mtu;
533 msgs.vers_2_msgs.send_ndis_config.capabilities.u1.u2.ieee8021q
536 /* Send the configuration packet */
537 ret = hv_vmbus_channel_send_packet(device->channel, init_pkt,
538 sizeof(nvsp_msg), (uint64_t)(uintptr_t)init_pkt,
539 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND, 0);
547 * Net VSC connect to VSP
550 hv_nv_connect_to_vsp(struct hv_device *device)
554 uint32_t ndis_version;
555 uint32_t protocol_list[] = { NVSP_PROTOCOL_VERSION_1,
556 NVSP_PROTOCOL_VERSION_2,
557 NVSP_PROTOCOL_VERSION_4,
558 NVSP_PROTOCOL_VERSION_5 };
560 int protocol_number = nitems(protocol_list);
562 device_t dev = device->device;
563 hn_softc_t *sc = device_get_softc(dev);
564 struct ifnet *ifp = sc->hn_ifp;
566 net_dev = hv_nv_get_outbound_net_device(device);
572 * Negotiate the NVSP version. Try the latest NVSP first.
574 for (i = protocol_number - 1; i >= 0; i--) {
575 if (hv_nv_negotiate_nvsp_protocol(device, net_dev,
576 protocol_list[i]) == 0) {
577 net_dev->nvsp_version = protocol_list[i];
579 device_printf(dev, "Netvsc: got version 0x%x\n",
580 net_dev->nvsp_version);
587 device_printf(dev, "failed to negotiate a valid "
593 * Set the MTU if supported by this NVSP protocol version
594 * This needs to be right after the NVSP init message per Haiyang
596 if (net_dev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
597 ret = hv_nv_send_ndis_config(device, ifp->if_mtu);
600 * Send the NDIS version
602 init_pkt = &net_dev->channel_init_packet;
604 memset(init_pkt, 0, sizeof(nvsp_msg));
606 if (net_dev->nvsp_version <= NVSP_PROTOCOL_VERSION_4) {
607 ndis_version = NDIS_VERSION_6_1;
609 ndis_version = NDIS_VERSION_6_30;
612 init_pkt->hdr.msg_type = nvsp_msg_1_type_send_ndis_vers;
613 init_pkt->msgs.vers_1_msgs.send_ndis_vers.ndis_major_vers =
614 (ndis_version & 0xFFFF0000) >> 16;
615 init_pkt->msgs.vers_1_msgs.send_ndis_vers.ndis_minor_vers =
616 ndis_version & 0xFFFF;
618 /* Send the init request */
620 ret = hv_vmbus_channel_send_packet(device->channel, init_pkt,
621 sizeof(nvsp_msg), (uint64_t)(uintptr_t)init_pkt,
622 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND, 0);
627 * TODO: BUGBUG - We have to wait for the above msg since the netvsp
628 * uses KMCL which acknowledges packet (completion packet)
629 * since our Vmbus always set the
630 * HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED flag
632 /* sema_wait(&NetVscChannel->channel_init_sema); */
634 /* Post the big receive buffer to NetVSP */
635 if (net_dev->nvsp_version <= NVSP_PROTOCOL_VERSION_2)
636 net_dev->rx_buf_size = NETVSC_RECEIVE_BUFFER_SIZE_LEGACY;
638 net_dev->rx_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
639 net_dev->send_buf_size = NETVSC_SEND_BUFFER_SIZE;
641 ret = hv_nv_init_rx_buffer_with_net_vsp(device);
643 ret = hv_nv_init_send_buffer_with_net_vsp(device);
650 * Net VSC disconnect from VSP
653 hv_nv_disconnect_from_vsp(netvsc_dev *net_dev)
655 hv_nv_destroy_rx_buffer(net_dev);
656 hv_nv_destroy_send_buffer(net_dev);
660 * Net VSC on device add
662 * Callback when the device belonging to this driver is added
665 hv_nv_on_device_add(struct hv_device *device, void *additional_info)
670 net_dev = hv_nv_alloc_net_device(device);
674 /* Initialize the NetVSC channel extension */
676 sema_init(&net_dev->channel_init_sema, 0, "netdev_sema");
681 ret = hv_vmbus_channel_open(device->channel,
682 NETVSC_DEVICE_RING_BUFFER_SIZE, NETVSC_DEVICE_RING_BUFFER_SIZE,
683 NULL, 0, hv_nv_on_channel_callback, device->channel);
688 * Connect with the NetVsp
690 ret = hv_nv_connect_to_vsp(device);
697 /* Now, we can close the channel safely */
699 hv_vmbus_channel_close(device->channel);
703 * Free the packet buffers on the netvsc device packet queue.
704 * Release other resources.
707 sema_destroy(&net_dev->channel_init_sema);
708 free(net_dev, M_NETVSC);
715 * Net VSC on device remove
718 hv_nv_on_device_remove(struct hv_device *device, boolean_t destroy_channel)
720 hn_softc_t *sc = device_get_softc(device->device);
721 netvsc_dev *net_dev = sc->net_dev;;
723 /* Stop outbound traffic ie sends and receives completions */
724 net_dev->destroy = TRUE;
726 hv_nv_disconnect_from_vsp(net_dev);
728 /* At this point, no one should be accessing net_dev except in here */
730 /* Now, we can close the channel safely */
732 if (!destroy_channel) {
733 device->channel->state =
734 HV_CHANNEL_CLOSING_NONDESTRUCTIVE_STATE;
737 hv_vmbus_channel_close(device->channel);
739 sema_destroy(&net_dev->channel_init_sema);
740 free(net_dev, M_NETVSC);
746 * Net VSC on send completion
749 hv_nv_on_send_completion(netvsc_dev *net_dev,
750 struct hv_device *device, hv_vm_packet_descriptor *pkt)
752 nvsp_msg *nvsp_msg_pkt;
753 netvsc_packet *net_vsc_pkt;
756 (nvsp_msg *)((unsigned long)pkt + (pkt->data_offset8 << 3));
758 if (nvsp_msg_pkt->hdr.msg_type == nvsp_msg_type_init_complete
759 || nvsp_msg_pkt->hdr.msg_type
760 == nvsp_msg_1_type_send_rx_buf_complete
761 || nvsp_msg_pkt->hdr.msg_type
762 == nvsp_msg_1_type_send_send_buf_complete) {
763 /* Copy the response back */
764 memcpy(&net_dev->channel_init_packet, nvsp_msg_pkt,
766 sema_post(&net_dev->channel_init_sema);
767 } else if (nvsp_msg_pkt->hdr.msg_type ==
768 nvsp_msg_1_type_send_rndis_pkt_complete) {
769 /* Get the send context */
771 (netvsc_packet *)(unsigned long)pkt->transaction_id;
772 if (NULL != net_vsc_pkt) {
773 if (net_vsc_pkt->send_buf_section_idx !=
774 NVSP_1_CHIMNEY_SEND_INVALID_SECTION_INDEX) {
778 idx = net_vsc_pkt->send_buf_section_idx /
780 KASSERT(idx < net_dev->bitsmap_words,
781 ("invalid section index %u",
782 net_vsc_pkt->send_buf_section_idx));
784 (net_vsc_pkt->send_buf_section_idx %
787 KASSERT(net_dev->send_section_bitsmap[idx] &
789 ("index bitmap 0x%lx, section index %u, "
790 "bitmap idx %d, bitmask 0x%lx",
791 net_dev->send_section_bitsmap[idx],
792 net_vsc_pkt->send_buf_section_idx,
795 &net_dev->send_section_bitsmap[idx], mask);
798 /* Notify the layer above us */
799 net_vsc_pkt->compl.send.on_send_completion(
800 net_vsc_pkt->compl.send.send_completion_context);
808 * Sends a packet on the specified Hyper-V device.
809 * Returns 0 on success, non-zero on failure.
812 hv_nv_on_send(struct hv_vmbus_channel *chan, netvsc_packet *pkt)
817 send_msg.hdr.msg_type = nvsp_msg_1_type_send_rndis_pkt;
818 if (pkt->is_data_pkt) {
820 send_msg.msgs.vers_1_msgs.send_rndis_pkt.chan_type = 0;
822 /* 1 is RMC_CONTROL */
823 send_msg.msgs.vers_1_msgs.send_rndis_pkt.chan_type = 1;
826 send_msg.msgs.vers_1_msgs.send_rndis_pkt.send_buf_section_idx =
827 pkt->send_buf_section_idx;
828 send_msg.msgs.vers_1_msgs.send_rndis_pkt.send_buf_section_size =
829 pkt->send_buf_section_size;
831 if (pkt->page_buf_count) {
832 ret = hv_vmbus_channel_send_packet_pagebuffer(chan,
833 pkt->page_buffers, pkt->page_buf_count,
834 &send_msg, sizeof(nvsp_msg), (uint64_t)(uintptr_t)pkt);
836 ret = hv_vmbus_channel_send_packet(chan,
837 &send_msg, sizeof(nvsp_msg), (uint64_t)(uintptr_t)pkt,
838 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND,
839 HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
848 * In the FreeBSD Hyper-V virtual world, this function deals exclusively
849 * with virtual addresses.
852 hv_nv_on_receive(netvsc_dev *net_dev, struct hv_device *device,
853 struct hv_vmbus_channel *chan, hv_vm_packet_descriptor *pkt)
855 hv_vm_transfer_page_packet_header *vm_xfer_page_pkt;
856 nvsp_msg *nvsp_msg_pkt;
857 netvsc_packet vsc_pkt;
858 netvsc_packet *net_vsc_pkt = &vsc_pkt;
859 device_t dev = device->device;
862 int status = nvsp_status_success;
865 * All inbound packets other than send completion should be
868 if (pkt->type != HV_VMBUS_PACKET_TYPE_DATA_USING_TRANSFER_PAGES) {
869 device_printf(dev, "packet type %d is invalid!\n", pkt->type);
873 nvsp_msg_pkt = (nvsp_msg *)((unsigned long)pkt
874 + (pkt->data_offset8 << 3));
876 /* Make sure this is a valid nvsp packet */
877 if (nvsp_msg_pkt->hdr.msg_type != nvsp_msg_1_type_send_rndis_pkt) {
878 device_printf(dev, "packet hdr type %d is invalid!\n",
883 vm_xfer_page_pkt = (hv_vm_transfer_page_packet_header *)pkt;
885 if (vm_xfer_page_pkt->transfer_page_set_id !=
886 NETVSC_RECEIVE_BUFFER_ID) {
887 device_printf(dev, "transfer_page_set_id %d is invalid!\n",
888 vm_xfer_page_pkt->transfer_page_set_id);
892 count = vm_xfer_page_pkt->range_count;
893 net_vsc_pkt->device = device;
895 /* Each range represents 1 RNDIS pkt that contains 1 Ethernet frame */
896 for (i = 0; i < count; i++) {
897 net_vsc_pkt->status = nvsp_status_success;
898 net_vsc_pkt->data = (void *)((unsigned long)net_dev->rx_buf +
899 vm_xfer_page_pkt->ranges[i].byte_offset);
900 net_vsc_pkt->tot_data_buf_len =
901 vm_xfer_page_pkt->ranges[i].byte_count;
903 hv_rf_on_receive(net_dev, device, chan, net_vsc_pkt);
904 if (net_vsc_pkt->status != nvsp_status_success) {
905 status = nvsp_status_failure;
910 * Moved completion call back here so that all received
911 * messages (not just data messages) will trigger a response
912 * message back to the host.
914 hv_nv_on_receive_completion(device, vm_xfer_page_pkt->d.transaction_id,
919 * Net VSC on receive completion
921 * Send a receive completion packet to RNDIS device (ie NetVsp)
924 hv_nv_on_receive_completion(struct hv_device *device, uint64_t tid,
927 nvsp_msg rx_comp_msg;
931 rx_comp_msg.hdr.msg_type = nvsp_msg_1_type_send_rndis_pkt_complete;
933 /* Pass in the status */
934 rx_comp_msg.msgs.vers_1_msgs.send_rndis_pkt_complete.status =
938 /* Send the completion */
939 ret = hv_vmbus_channel_send_packet(device->channel, &rx_comp_msg,
940 sizeof(nvsp_msg), tid, HV_VMBUS_PACKET_TYPE_COMPLETION, 0);
944 } else if (ret == EAGAIN) {
945 /* no more room... wait a bit and attempt to retry 3 times */
950 goto retry_send_cmplt;
956 * Net VSC on channel callback
959 hv_nv_on_channel_callback(void *xchan)
961 struct hv_vmbus_channel *chan = xchan;
962 struct hv_device *device = chan->device;
964 device_t dev = device->device;
967 hv_vm_packet_descriptor *desc;
969 int bufferlen = NETVSC_PACKET_SIZE;
972 net_dev = hv_nv_get_inbound_net_device(device);
976 buffer = net_dev->callback_buf;
979 ret = hv_vmbus_channel_recv_packet_raw(chan,
980 buffer, bufferlen, &bytes_rxed, &request_id);
982 if (bytes_rxed > 0) {
983 desc = (hv_vm_packet_descriptor *)buffer;
984 switch (desc->type) {
985 case HV_VMBUS_PACKET_TYPE_COMPLETION:
986 hv_nv_on_send_completion(net_dev, device, desc);
988 case HV_VMBUS_PACKET_TYPE_DATA_USING_TRANSFER_PAGES:
989 hv_nv_on_receive(net_dev, device, chan, desc);
993 "hv_cb recv unknow type %d "
994 " packet\n", desc->type);
1000 } else if (ret == ENOBUFS) {
1001 /* Handle large packet */
1002 if (bufferlen > NETVSC_PACKET_SIZE) {
1003 free(buffer, M_NETVSC);
1007 /* alloc new buffer */
1008 buffer = malloc(bytes_rxed, M_NETVSC, M_NOWAIT);
1009 if (buffer == NULL) {
1011 "hv_cb malloc buffer failed, len=%u\n",
1016 bufferlen = bytes_rxed;
1020 if (bufferlen > NETVSC_PACKET_SIZE)
1021 free(buffer, M_NETVSC);
1023 hv_rf_channel_rollup(chan);