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_arp.h>
43 #include <machine/bus.h>
44 #include <machine/atomic.h>
46 #include <dev/hyperv/include/hyperv.h>
47 #include "hv_net_vsc.h"
49 #include "hv_rndis_filter.h"
51 MALLOC_DEFINE(M_NETVSC, "netvsc", "Hyper-V netvsc driver");
54 * Forward declarations
56 static void hv_nv_on_channel_callback(void *context);
57 static int hv_nv_init_send_buffer_with_net_vsp(struct hv_device *device);
58 static int hv_nv_init_rx_buffer_with_net_vsp(struct hv_device *device);
59 static int hv_nv_destroy_send_buffer(netvsc_dev *net_dev);
60 static int hv_nv_destroy_rx_buffer(netvsc_dev *net_dev);
61 static int hv_nv_connect_to_vsp(struct hv_device *device);
62 static void hv_nv_on_send_completion(netvsc_dev *net_dev,
63 struct hv_device *device, hv_vm_packet_descriptor *pkt);
64 static void hv_nv_on_receive(netvsc_dev *net_dev,
65 struct hv_device *device, hv_vm_packet_descriptor *pkt);
70 static inline netvsc_dev *
71 hv_nv_alloc_net_device(struct hv_device *device)
74 hn_softc_t *sc = device_get_softc(device->device);
76 net_dev = malloc(sizeof(netvsc_dev), M_NETVSC, M_NOWAIT | M_ZERO);
77 if (net_dev == NULL) {
81 net_dev->dev = device;
82 net_dev->destroy = FALSE;
83 sc->net_dev = net_dev;
91 static inline netvsc_dev *
92 hv_nv_get_outbound_net_device(struct hv_device *device)
94 hn_softc_t *sc = device_get_softc(device->device);
95 netvsc_dev *net_dev = sc->net_dev;;
97 if ((net_dev != NULL) && net_dev->destroy) {
107 static inline netvsc_dev *
108 hv_nv_get_inbound_net_device(struct hv_device *device)
110 hn_softc_t *sc = device_get_softc(device->device);
111 netvsc_dev *net_dev = sc->net_dev;;
113 if (net_dev == NULL) {
117 * When the device is being destroyed; we only
118 * permit incoming packets if and only if there
119 * are outstanding sends.
121 if (net_dev->destroy && net_dev->num_outstanding_sends == 0) {
129 hv_nv_get_next_send_section(netvsc_dev *net_dev)
131 unsigned long bitsmap_words = net_dev->bitsmap_words;
132 unsigned long *bitsmap = net_dev->send_section_bitsmap;
134 int ret = NVSP_1_CHIMNEY_SEND_INVALID_SECTION_INDEX;
137 for (i = 0; i < bitsmap_words; i++) {
138 idx = ffs(~bitsmap[i]);
143 if (i * BITS_PER_LONG + idx >= net_dev->send_section_count)
146 if (synch_test_and_set_bit(idx, &bitsmap[i]))
149 ret = i * BITS_PER_LONG + idx;
157 * Net VSC initialize receive buffer with net VSP
159 * Net VSP: Network virtual services client, also known as the
160 * Hyper-V extensible switch and the synthetic data path.
163 hv_nv_init_rx_buffer_with_net_vsp(struct hv_device *device)
169 net_dev = hv_nv_get_outbound_net_device(device);
174 net_dev->rx_buf = contigmalloc(net_dev->rx_buf_size, M_NETVSC,
175 M_ZERO, 0UL, BUS_SPACE_MAXADDR, PAGE_SIZE, 0);
178 * Establish the GPADL handle for this buffer on this channel.
179 * Note: This call uses the vmbus connection rather than the
180 * channel to establish the gpadl handle.
181 * GPADL: Guest physical address descriptor list.
183 ret = hv_vmbus_channel_establish_gpadl(
184 device->channel, net_dev->rx_buf,
185 net_dev->rx_buf_size, &net_dev->rx_buf_gpadl_handle);
190 /* sema_wait(&ext->channel_init_sema); KYS CHECK */
192 /* Notify the NetVsp of the gpadl handle */
193 init_pkt = &net_dev->channel_init_packet;
195 memset(init_pkt, 0, sizeof(nvsp_msg));
197 init_pkt->hdr.msg_type = nvsp_msg_1_type_send_rx_buf;
198 init_pkt->msgs.vers_1_msgs.send_rx_buf.gpadl_handle =
199 net_dev->rx_buf_gpadl_handle;
200 init_pkt->msgs.vers_1_msgs.send_rx_buf.id =
201 NETVSC_RECEIVE_BUFFER_ID;
203 /* Send the gpadl notification request */
205 ret = hv_vmbus_channel_send_packet(device->channel, init_pkt,
206 sizeof(nvsp_msg), (uint64_t)(uintptr_t)init_pkt,
207 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND,
208 HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
213 sema_wait(&net_dev->channel_init_sema);
215 /* Check the response */
216 if (init_pkt->msgs.vers_1_msgs.send_rx_buf_complete.status
217 != nvsp_status_success) {
222 net_dev->rx_section_count =
223 init_pkt->msgs.vers_1_msgs.send_rx_buf_complete.num_sections;
225 net_dev->rx_sections = malloc(net_dev->rx_section_count *
226 sizeof(nvsp_1_rx_buf_section), M_NETVSC, M_NOWAIT);
227 if (net_dev->rx_sections == NULL) {
231 memcpy(net_dev->rx_sections,
232 init_pkt->msgs.vers_1_msgs.send_rx_buf_complete.sections,
233 net_dev->rx_section_count * sizeof(nvsp_1_rx_buf_section));
237 * For first release, there should only be 1 section that represents
238 * the entire receive buffer
240 if (net_dev->rx_section_count != 1
241 || net_dev->rx_sections->offset != 0) {
249 hv_nv_destroy_rx_buffer(net_dev);
256 * Net VSC initialize send buffer with net VSP
259 hv_nv_init_send_buffer_with_net_vsp(struct hv_device *device)
265 net_dev = hv_nv_get_outbound_net_device(device);
270 net_dev->send_buf = contigmalloc(net_dev->send_buf_size, M_NETVSC,
271 M_ZERO, 0UL, BUS_SPACE_MAXADDR, PAGE_SIZE, 0);
272 if (net_dev->send_buf == NULL) {
278 * Establish the gpadl handle for this buffer on this channel.
279 * Note: This call uses the vmbus connection rather than the
280 * channel to establish the gpadl handle.
282 ret = hv_vmbus_channel_establish_gpadl(device->channel,
283 net_dev->send_buf, net_dev->send_buf_size,
284 &net_dev->send_buf_gpadl_handle);
289 /* Notify the NetVsp of the gpadl handle */
291 init_pkt = &net_dev->channel_init_packet;
293 memset(init_pkt, 0, sizeof(nvsp_msg));
295 init_pkt->hdr.msg_type = nvsp_msg_1_type_send_send_buf;
296 init_pkt->msgs.vers_1_msgs.send_rx_buf.gpadl_handle =
297 net_dev->send_buf_gpadl_handle;
298 init_pkt->msgs.vers_1_msgs.send_rx_buf.id =
299 NETVSC_SEND_BUFFER_ID;
301 /* Send the gpadl notification request */
303 ret = hv_vmbus_channel_send_packet(device->channel, init_pkt,
304 sizeof(nvsp_msg), (uint64_t)init_pkt,
305 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND,
306 HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
311 sema_wait(&net_dev->channel_init_sema);
313 /* Check the response */
314 if (init_pkt->msgs.vers_1_msgs.send_send_buf_complete.status
315 != nvsp_status_success) {
320 net_dev->send_section_size =
321 init_pkt->msgs.vers_1_msgs.send_send_buf_complete.section_size;
322 net_dev->send_section_count =
323 net_dev->send_buf_size / net_dev->send_section_size;
324 net_dev->bitsmap_words = howmany(net_dev->send_section_count,
326 net_dev->send_section_bitsmap =
327 malloc(net_dev->bitsmap_words * sizeof(long), M_NETVSC,
329 if (NULL == net_dev->send_section_bitsmap) {
337 hv_nv_destroy_send_buffer(net_dev);
344 * Net VSC destroy receive buffer
347 hv_nv_destroy_rx_buffer(netvsc_dev *net_dev)
349 nvsp_msg *revoke_pkt;
353 * If we got a section count, it means we received a
354 * send_rx_buf_complete msg
355 * (ie sent nvsp_msg_1_type_send_rx_buf msg) therefore,
356 * we need to send a revoke msg here
358 if (net_dev->rx_section_count) {
359 /* Send the revoke receive buffer */
360 revoke_pkt = &net_dev->revoke_packet;
361 memset(revoke_pkt, 0, sizeof(nvsp_msg));
363 revoke_pkt->hdr.msg_type = nvsp_msg_1_type_revoke_rx_buf;
364 revoke_pkt->msgs.vers_1_msgs.revoke_rx_buf.id =
365 NETVSC_RECEIVE_BUFFER_ID;
367 ret = hv_vmbus_channel_send_packet(net_dev->dev->channel,
368 revoke_pkt, sizeof(nvsp_msg),
369 (uint64_t)(uintptr_t)revoke_pkt,
370 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND, 0);
373 * If we failed here, we might as well return and have a leak
374 * rather than continue and a bugchk
381 /* Tear down the gpadl on the vsp end */
382 if (net_dev->rx_buf_gpadl_handle) {
383 ret = hv_vmbus_channel_teardown_gpdal(net_dev->dev->channel,
384 net_dev->rx_buf_gpadl_handle);
386 * If we failed here, we might as well return and have a leak
387 * rather than continue and a bugchk
392 net_dev->rx_buf_gpadl_handle = 0;
395 if (net_dev->rx_buf) {
396 /* Free up the receive buffer */
397 contigfree(net_dev->rx_buf, net_dev->rx_buf_size, M_NETVSC);
398 net_dev->rx_buf = NULL;
401 if (net_dev->rx_sections) {
402 free(net_dev->rx_sections, M_NETVSC);
403 net_dev->rx_sections = NULL;
404 net_dev->rx_section_count = 0;
411 * Net VSC destroy send buffer
414 hv_nv_destroy_send_buffer(netvsc_dev *net_dev)
416 nvsp_msg *revoke_pkt;
420 * If we got a section count, it means we received a
421 * send_rx_buf_complete msg
422 * (ie sent nvsp_msg_1_type_send_rx_buf msg) therefore,
423 * we need to send a revoke msg here
425 if (net_dev->send_section_size) {
426 /* Send the revoke send buffer */
427 revoke_pkt = &net_dev->revoke_packet;
428 memset(revoke_pkt, 0, sizeof(nvsp_msg));
430 revoke_pkt->hdr.msg_type =
431 nvsp_msg_1_type_revoke_send_buf;
432 revoke_pkt->msgs.vers_1_msgs.revoke_send_buf.id =
433 NETVSC_SEND_BUFFER_ID;
435 ret = hv_vmbus_channel_send_packet(net_dev->dev->channel,
436 revoke_pkt, sizeof(nvsp_msg),
437 (uint64_t)(uintptr_t)revoke_pkt,
438 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND, 0);
440 * If we failed here, we might as well return and have a leak
441 * rather than continue and a bugchk
448 /* Tear down the gpadl on the vsp end */
449 if (net_dev->send_buf_gpadl_handle) {
450 ret = hv_vmbus_channel_teardown_gpdal(net_dev->dev->channel,
451 net_dev->send_buf_gpadl_handle);
454 * If we failed here, we might as well return and have a leak
455 * rather than continue and a bugchk
460 net_dev->send_buf_gpadl_handle = 0;
463 if (net_dev->send_buf) {
464 /* Free up the receive buffer */
465 contigfree(net_dev->send_buf, net_dev->send_buf_size, M_NETVSC);
466 net_dev->send_buf = NULL;
469 if (net_dev->send_section_bitsmap) {
470 free(net_dev->send_section_bitsmap, M_NETVSC);
478 * Attempt to negotiate the caller-specified NVSP version
480 * For NVSP v2, Server 2008 R2 does not set
481 * init_pkt->msgs.init_msgs.init_compl.negotiated_prot_vers
482 * to the negotiated version, so we cannot rely on that.
485 hv_nv_negotiate_nvsp_protocol(struct hv_device *device, netvsc_dev *net_dev,
491 init_pkt = &net_dev->channel_init_packet;
492 memset(init_pkt, 0, sizeof(nvsp_msg));
493 init_pkt->hdr.msg_type = nvsp_msg_type_init;
496 * Specify parameter as the only acceptable protocol version
498 init_pkt->msgs.init_msgs.init.p1.protocol_version = nvsp_ver;
499 init_pkt->msgs.init_msgs.init.protocol_version_2 = nvsp_ver;
501 /* Send the init request */
502 ret = hv_vmbus_channel_send_packet(device->channel, init_pkt,
503 sizeof(nvsp_msg), (uint64_t)(uintptr_t)init_pkt,
504 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND,
505 HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
509 sema_wait(&net_dev->channel_init_sema);
511 if (init_pkt->msgs.init_msgs.init_compl.status != nvsp_status_success)
518 * Send NDIS version 2 config packet containing MTU.
520 * Not valid for NDIS version 1.
523 hv_nv_send_ndis_config(struct hv_device *device, uint32_t mtu)
529 net_dev = hv_nv_get_outbound_net_device(device);
534 * Set up configuration packet, write MTU
535 * Indicate we are capable of handling VLAN tags
537 init_pkt = &net_dev->channel_init_packet;
538 memset(init_pkt, 0, sizeof(nvsp_msg));
539 init_pkt->hdr.msg_type = nvsp_msg_2_type_send_ndis_config;
540 init_pkt->msgs.vers_2_msgs.send_ndis_config.mtu = mtu;
542 msgs.vers_2_msgs.send_ndis_config.capabilities.u1.u2.ieee8021q
545 /* Send the configuration packet */
546 ret = hv_vmbus_channel_send_packet(device->channel, init_pkt,
547 sizeof(nvsp_msg), (uint64_t)(uintptr_t)init_pkt,
548 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND, 0);
556 * Net VSC connect to VSP
559 hv_nv_connect_to_vsp(struct hv_device *device)
563 uint32_t ndis_version;
564 uint32_t protocol_list[] = { NVSP_PROTOCOL_VERSION_1,
565 NVSP_PROTOCOL_VERSION_2,
566 NVSP_PROTOCOL_VERSION_4,
567 NVSP_PROTOCOL_VERSION_5 };
569 int protocol_number = nitems(protocol_list);
571 device_t dev = device->device;
572 hn_softc_t *sc = device_get_softc(dev);
573 struct ifnet *ifp = sc->arpcom.ac_ifp;
575 net_dev = hv_nv_get_outbound_net_device(device);
581 * Negotiate the NVSP version. Try the latest NVSP first.
583 for (i = protocol_number - 1; i >= 0; i--) {
584 if (hv_nv_negotiate_nvsp_protocol(device, net_dev,
585 protocol_list[i]) == 0) {
586 net_dev->nvsp_version = protocol_list[i];
588 device_printf(dev, "Netvsc: got version 0x%x\n",
589 net_dev->nvsp_version);
596 device_printf(dev, "failed to negotiate a valid "
602 * Set the MTU if supported by this NVSP protocol version
603 * This needs to be right after the NVSP init message per Haiyang
605 if (net_dev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
606 ret = hv_nv_send_ndis_config(device, ifp->if_mtu);
609 * Send the NDIS version
611 init_pkt = &net_dev->channel_init_packet;
613 memset(init_pkt, 0, sizeof(nvsp_msg));
615 if (net_dev->nvsp_version <= NVSP_PROTOCOL_VERSION_4) {
616 ndis_version = NDIS_VERSION_6_1;
618 ndis_version = NDIS_VERSION_6_30;
621 init_pkt->hdr.msg_type = nvsp_msg_1_type_send_ndis_vers;
622 init_pkt->msgs.vers_1_msgs.send_ndis_vers.ndis_major_vers =
623 (ndis_version & 0xFFFF0000) >> 16;
624 init_pkt->msgs.vers_1_msgs.send_ndis_vers.ndis_minor_vers =
625 ndis_version & 0xFFFF;
627 /* Send the init request */
629 ret = hv_vmbus_channel_send_packet(device->channel, init_pkt,
630 sizeof(nvsp_msg), (uint64_t)(uintptr_t)init_pkt,
631 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND, 0);
636 * TODO: BUGBUG - We have to wait for the above msg since the netvsp
637 * uses KMCL which acknowledges packet (completion packet)
638 * since our Vmbus always set the
639 * HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED flag
641 /* sema_wait(&NetVscChannel->channel_init_sema); */
643 /* Post the big receive buffer to NetVSP */
644 ret = hv_nv_init_rx_buffer_with_net_vsp(device);
646 ret = hv_nv_init_send_buffer_with_net_vsp(device);
653 * Net VSC disconnect from VSP
656 hv_nv_disconnect_from_vsp(netvsc_dev *net_dev)
658 hv_nv_destroy_rx_buffer(net_dev);
659 hv_nv_destroy_send_buffer(net_dev);
663 * Net VSC on device add
665 * Callback when the device belonging to this driver is added
668 hv_nv_on_device_add(struct hv_device *device, void *additional_info)
673 net_dev = hv_nv_alloc_net_device(device);
677 /* Initialize the NetVSC channel extension */
678 net_dev->rx_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
680 net_dev->send_buf_size = NETVSC_SEND_BUFFER_SIZE;
682 sema_init(&net_dev->channel_init_sema, 0, "netdev_sema");
687 ret = hv_vmbus_channel_open(device->channel,
688 NETVSC_DEVICE_RING_BUFFER_SIZE, NETVSC_DEVICE_RING_BUFFER_SIZE,
689 NULL, 0, hv_nv_on_channel_callback, device);
694 * Connect with the NetVsp
696 ret = hv_nv_connect_to_vsp(device);
703 /* Now, we can close the channel safely */
705 hv_vmbus_channel_close(device->channel);
709 * Free the packet buffers on the netvsc device packet queue.
710 * Release other resources.
713 sema_destroy(&net_dev->channel_init_sema);
714 free(net_dev, M_NETVSC);
721 * Net VSC on device remove
724 hv_nv_on_device_remove(struct hv_device *device, boolean_t destroy_channel)
726 hn_softc_t *sc = device_get_softc(device->device);
727 netvsc_dev *net_dev = sc->net_dev;;
729 /* Stop outbound traffic ie sends and receives completions */
730 mtx_lock(&device->channel->inbound_lock);
731 net_dev->destroy = TRUE;
732 mtx_unlock(&device->channel->inbound_lock);
734 /* Wait for all send completions */
735 while (net_dev->num_outstanding_sends) {
739 hv_nv_disconnect_from_vsp(net_dev);
741 /* At this point, no one should be accessing net_dev except in here */
743 /* Now, we can close the channel safely */
745 if (!destroy_channel) {
746 device->channel->state =
747 HV_CHANNEL_CLOSING_NONDESTRUCTIVE_STATE;
750 hv_vmbus_channel_close(device->channel);
752 sema_destroy(&net_dev->channel_init_sema);
753 free(net_dev, M_NETVSC);
759 * Net VSC on send completion
762 hv_nv_on_send_completion(netvsc_dev *net_dev,
763 struct hv_device *device, hv_vm_packet_descriptor *pkt)
765 nvsp_msg *nvsp_msg_pkt;
766 netvsc_packet *net_vsc_pkt;
769 (nvsp_msg *)((unsigned long)pkt + (pkt->data_offset8 << 3));
771 if (nvsp_msg_pkt->hdr.msg_type == nvsp_msg_type_init_complete
772 || nvsp_msg_pkt->hdr.msg_type
773 == nvsp_msg_1_type_send_rx_buf_complete
774 || nvsp_msg_pkt->hdr.msg_type
775 == nvsp_msg_1_type_send_send_buf_complete) {
776 /* Copy the response back */
777 memcpy(&net_dev->channel_init_packet, nvsp_msg_pkt,
779 sema_post(&net_dev->channel_init_sema);
780 } else if (nvsp_msg_pkt->hdr.msg_type ==
781 nvsp_msg_1_type_send_rndis_pkt_complete) {
782 /* Get the send context */
784 (netvsc_packet *)(unsigned long)pkt->transaction_id;
785 if (NULL != net_vsc_pkt) {
786 if (net_vsc_pkt->send_buf_section_idx !=
787 NVSP_1_CHIMNEY_SEND_INVALID_SECTION_INDEX) {
788 synch_change_bit(net_vsc_pkt->send_buf_section_idx,
789 net_dev->send_section_bitsmap);
792 /* Notify the layer above us */
793 net_vsc_pkt->compl.send.on_send_completion(
794 net_vsc_pkt->compl.send.send_completion_context);
798 atomic_subtract_int(&net_dev->num_outstanding_sends, 1);
804 * Sends a packet on the specified Hyper-V device.
805 * Returns 0 on success, non-zero on failure.
808 hv_nv_on_send(struct hv_device *device, netvsc_packet *pkt)
814 net_dev = hv_nv_get_outbound_net_device(device);
818 send_msg.hdr.msg_type = nvsp_msg_1_type_send_rndis_pkt;
819 if (pkt->is_data_pkt) {
821 send_msg.msgs.vers_1_msgs.send_rndis_pkt.chan_type = 0;
823 /* 1 is RMC_CONTROL */
824 send_msg.msgs.vers_1_msgs.send_rndis_pkt.chan_type = 1;
827 send_msg.msgs.vers_1_msgs.send_rndis_pkt.send_buf_section_idx =
828 pkt->send_buf_section_idx;
829 send_msg.msgs.vers_1_msgs.send_rndis_pkt.send_buf_section_size =
830 pkt->send_buf_section_size;
832 if (pkt->page_buf_count) {
833 ret = hv_vmbus_channel_send_packet_pagebuffer(device->channel,
834 pkt->page_buffers, pkt->page_buf_count,
835 &send_msg, sizeof(nvsp_msg), (uint64_t)(uintptr_t)pkt);
837 ret = hv_vmbus_channel_send_packet(device->channel,
838 &send_msg, sizeof(nvsp_msg), (uint64_t)(uintptr_t)pkt,
839 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND,
840 HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
843 /* Record outstanding send only if send_packet() succeeded */
845 atomic_add_int(&net_dev->num_outstanding_sends, 1);
853 * In the FreeBSD Hyper-V virtual world, this function deals exclusively
854 * with virtual addresses.
857 hv_nv_on_receive(netvsc_dev *net_dev, struct hv_device *device,
858 hv_vm_packet_descriptor *pkt)
860 hv_vm_transfer_page_packet_header *vm_xfer_page_pkt;
861 nvsp_msg *nvsp_msg_pkt;
862 netvsc_packet vsc_pkt;
863 netvsc_packet *net_vsc_pkt = &vsc_pkt;
864 device_t dev = device->device;
867 int status = nvsp_status_success;
870 * All inbound packets other than send completion should be
873 if (pkt->type != HV_VMBUS_PACKET_TYPE_DATA_USING_TRANSFER_PAGES) {
874 device_printf(dev, "packet type %d is invalid!\n", pkt->type);
878 nvsp_msg_pkt = (nvsp_msg *)((unsigned long)pkt
879 + (pkt->data_offset8 << 3));
881 /* Make sure this is a valid nvsp packet */
882 if (nvsp_msg_pkt->hdr.msg_type != nvsp_msg_1_type_send_rndis_pkt) {
883 device_printf(dev, "packet hdr type %d is invalid!\n",
888 vm_xfer_page_pkt = (hv_vm_transfer_page_packet_header *)pkt;
890 if (vm_xfer_page_pkt->transfer_page_set_id !=
891 NETVSC_RECEIVE_BUFFER_ID) {
892 device_printf(dev, "transfer_page_set_id %d is invalid!\n",
893 vm_xfer_page_pkt->transfer_page_set_id);
897 count = vm_xfer_page_pkt->range_count;
898 net_vsc_pkt->device = device;
900 /* Each range represents 1 RNDIS pkt that contains 1 Ethernet frame */
901 for (i = 0; i < count; i++) {
902 net_vsc_pkt->status = nvsp_status_success;
903 net_vsc_pkt->data = (void *)((unsigned long)net_dev->rx_buf +
904 vm_xfer_page_pkt->ranges[i].byte_offset);
905 net_vsc_pkt->tot_data_buf_len =
906 vm_xfer_page_pkt->ranges[i].byte_count;
908 hv_rf_on_receive(net_dev, device, net_vsc_pkt);
909 if (net_vsc_pkt->status != nvsp_status_success) {
910 status = nvsp_status_failure;
915 * Moved completion call back here so that all received
916 * messages (not just data messages) will trigger a response
917 * message back to the host.
919 hv_nv_on_receive_completion(device, vm_xfer_page_pkt->d.transaction_id,
924 * Net VSC on receive completion
926 * Send a receive completion packet to RNDIS device (ie NetVsp)
929 hv_nv_on_receive_completion(struct hv_device *device, uint64_t tid,
932 nvsp_msg rx_comp_msg;
936 rx_comp_msg.hdr.msg_type = nvsp_msg_1_type_send_rndis_pkt_complete;
938 /* Pass in the status */
939 rx_comp_msg.msgs.vers_1_msgs.send_rndis_pkt_complete.status =
943 /* Send the completion */
944 ret = hv_vmbus_channel_send_packet(device->channel, &rx_comp_msg,
945 sizeof(nvsp_msg), tid, HV_VMBUS_PACKET_TYPE_COMPLETION, 0);
949 } else if (ret == EAGAIN) {
950 /* no more room... wait a bit and attempt to retry 3 times */
955 goto retry_send_cmplt;
961 * Net VSC on channel callback
964 hv_nv_on_channel_callback(void *context)
966 struct hv_device *device = (struct hv_device *)context;
968 device_t dev = device->device;
971 hv_vm_packet_descriptor *desc;
973 int bufferlen = NETVSC_PACKET_SIZE;
976 net_dev = hv_nv_get_inbound_net_device(device);
980 buffer = net_dev->callback_buf;
983 ret = hv_vmbus_channel_recv_packet_raw(device->channel,
984 buffer, bufferlen, &bytes_rxed, &request_id);
986 if (bytes_rxed > 0) {
987 desc = (hv_vm_packet_descriptor *)buffer;
988 switch (desc->type) {
989 case HV_VMBUS_PACKET_TYPE_COMPLETION:
990 hv_nv_on_send_completion(net_dev, device, desc);
992 case HV_VMBUS_PACKET_TYPE_DATA_USING_TRANSFER_PAGES:
993 hv_nv_on_receive(net_dev, device, desc);
997 "hv_cb recv unknow type %d "
998 " packet\n", desc->type);
1004 } else if (ret == ENOBUFS) {
1005 /* Handle large packet */
1006 if (bufferlen > NETVSC_PACKET_SIZE) {
1007 free(buffer, M_NETVSC);
1011 /* alloc new buffer */
1012 buffer = malloc(bytes_rxed, M_NETVSC, M_NOWAIT);
1013 if (buffer == NULL) {
1015 "hv_cb malloc buffer failed, len=%u\n",
1020 bufferlen = bytes_rxed;
1024 if (bufferlen > NETVSC_PACKET_SIZE)
1025 free(buffer, M_NETVSC);