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 if (net_dev->nvsp_version <= NVSP_PROTOCOL_VERSION_2)
645 net_dev->rx_buf_size = NETVSC_RECEIVE_BUFFER_SIZE_LEGACY;
647 net_dev->rx_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
648 net_dev->send_buf_size = NETVSC_SEND_BUFFER_SIZE;
650 ret = hv_nv_init_rx_buffer_with_net_vsp(device);
652 ret = hv_nv_init_send_buffer_with_net_vsp(device);
659 * Net VSC disconnect from VSP
662 hv_nv_disconnect_from_vsp(netvsc_dev *net_dev)
664 hv_nv_destroy_rx_buffer(net_dev);
665 hv_nv_destroy_send_buffer(net_dev);
669 * Net VSC on device add
671 * Callback when the device belonging to this driver is added
674 hv_nv_on_device_add(struct hv_device *device, void *additional_info)
679 net_dev = hv_nv_alloc_net_device(device);
683 /* Initialize the NetVSC channel extension */
685 sema_init(&net_dev->channel_init_sema, 0, "netdev_sema");
690 ret = hv_vmbus_channel_open(device->channel,
691 NETVSC_DEVICE_RING_BUFFER_SIZE, NETVSC_DEVICE_RING_BUFFER_SIZE,
692 NULL, 0, hv_nv_on_channel_callback, device);
697 * Connect with the NetVsp
699 ret = hv_nv_connect_to_vsp(device);
706 /* Now, we can close the channel safely */
708 hv_vmbus_channel_close(device->channel);
712 * Free the packet buffers on the netvsc device packet queue.
713 * Release other resources.
716 sema_destroy(&net_dev->channel_init_sema);
717 free(net_dev, M_NETVSC);
724 * Net VSC on device remove
727 hv_nv_on_device_remove(struct hv_device *device, boolean_t destroy_channel)
729 hn_softc_t *sc = device_get_softc(device->device);
730 netvsc_dev *net_dev = sc->net_dev;;
732 /* Stop outbound traffic ie sends and receives completions */
733 mtx_lock(&device->channel->inbound_lock);
734 net_dev->destroy = TRUE;
735 mtx_unlock(&device->channel->inbound_lock);
737 /* Wait for all send completions */
738 while (net_dev->num_outstanding_sends) {
742 hv_nv_disconnect_from_vsp(net_dev);
744 /* At this point, no one should be accessing net_dev except in here */
746 /* Now, we can close the channel safely */
748 if (!destroy_channel) {
749 device->channel->state =
750 HV_CHANNEL_CLOSING_NONDESTRUCTIVE_STATE;
753 hv_vmbus_channel_close(device->channel);
755 sema_destroy(&net_dev->channel_init_sema);
756 free(net_dev, M_NETVSC);
762 * Net VSC on send completion
765 hv_nv_on_send_completion(netvsc_dev *net_dev,
766 struct hv_device *device, hv_vm_packet_descriptor *pkt)
768 nvsp_msg *nvsp_msg_pkt;
769 netvsc_packet *net_vsc_pkt;
772 (nvsp_msg *)((unsigned long)pkt + (pkt->data_offset8 << 3));
774 if (nvsp_msg_pkt->hdr.msg_type == nvsp_msg_type_init_complete
775 || nvsp_msg_pkt->hdr.msg_type
776 == nvsp_msg_1_type_send_rx_buf_complete
777 || nvsp_msg_pkt->hdr.msg_type
778 == nvsp_msg_1_type_send_send_buf_complete) {
779 /* Copy the response back */
780 memcpy(&net_dev->channel_init_packet, nvsp_msg_pkt,
782 sema_post(&net_dev->channel_init_sema);
783 } else if (nvsp_msg_pkt->hdr.msg_type ==
784 nvsp_msg_1_type_send_rndis_pkt_complete) {
785 /* Get the send context */
787 (netvsc_packet *)(unsigned long)pkt->transaction_id;
788 if (NULL != net_vsc_pkt) {
789 if (net_vsc_pkt->send_buf_section_idx !=
790 NVSP_1_CHIMNEY_SEND_INVALID_SECTION_INDEX) {
791 synch_change_bit(net_vsc_pkt->send_buf_section_idx,
792 net_dev->send_section_bitsmap);
795 /* Notify the layer above us */
796 net_vsc_pkt->compl.send.on_send_completion(
797 net_vsc_pkt->compl.send.send_completion_context);
801 atomic_subtract_int(&net_dev->num_outstanding_sends, 1);
807 * Sends a packet on the specified Hyper-V device.
808 * Returns 0 on success, non-zero on failure.
811 hv_nv_on_send(struct hv_device *device, netvsc_packet *pkt)
817 net_dev = hv_nv_get_outbound_net_device(device);
821 send_msg.hdr.msg_type = nvsp_msg_1_type_send_rndis_pkt;
822 if (pkt->is_data_pkt) {
824 send_msg.msgs.vers_1_msgs.send_rndis_pkt.chan_type = 0;
826 /* 1 is RMC_CONTROL */
827 send_msg.msgs.vers_1_msgs.send_rndis_pkt.chan_type = 1;
830 send_msg.msgs.vers_1_msgs.send_rndis_pkt.send_buf_section_idx =
831 pkt->send_buf_section_idx;
832 send_msg.msgs.vers_1_msgs.send_rndis_pkt.send_buf_section_size =
833 pkt->send_buf_section_size;
835 if (pkt->page_buf_count) {
836 ret = hv_vmbus_channel_send_packet_pagebuffer(device->channel,
837 pkt->page_buffers, pkt->page_buf_count,
838 &send_msg, sizeof(nvsp_msg), (uint64_t)(uintptr_t)pkt);
840 ret = hv_vmbus_channel_send_packet(device->channel,
841 &send_msg, sizeof(nvsp_msg), (uint64_t)(uintptr_t)pkt,
842 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND,
843 HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
846 /* Record outstanding send only if send_packet() succeeded */
848 atomic_add_int(&net_dev->num_outstanding_sends, 1);
856 * In the FreeBSD Hyper-V virtual world, this function deals exclusively
857 * with virtual addresses.
860 hv_nv_on_receive(netvsc_dev *net_dev, struct hv_device *device,
861 hv_vm_packet_descriptor *pkt)
863 hv_vm_transfer_page_packet_header *vm_xfer_page_pkt;
864 nvsp_msg *nvsp_msg_pkt;
865 netvsc_packet vsc_pkt;
866 netvsc_packet *net_vsc_pkt = &vsc_pkt;
867 device_t dev = device->device;
870 int status = nvsp_status_success;
873 * All inbound packets other than send completion should be
876 if (pkt->type != HV_VMBUS_PACKET_TYPE_DATA_USING_TRANSFER_PAGES) {
877 device_printf(dev, "packet type %d is invalid!\n", pkt->type);
881 nvsp_msg_pkt = (nvsp_msg *)((unsigned long)pkt
882 + (pkt->data_offset8 << 3));
884 /* Make sure this is a valid nvsp packet */
885 if (nvsp_msg_pkt->hdr.msg_type != nvsp_msg_1_type_send_rndis_pkt) {
886 device_printf(dev, "packet hdr type %d is invalid!\n",
891 vm_xfer_page_pkt = (hv_vm_transfer_page_packet_header *)pkt;
893 if (vm_xfer_page_pkt->transfer_page_set_id !=
894 NETVSC_RECEIVE_BUFFER_ID) {
895 device_printf(dev, "transfer_page_set_id %d is invalid!\n",
896 vm_xfer_page_pkt->transfer_page_set_id);
900 count = vm_xfer_page_pkt->range_count;
901 net_vsc_pkt->device = device;
903 /* Each range represents 1 RNDIS pkt that contains 1 Ethernet frame */
904 for (i = 0; i < count; i++) {
905 net_vsc_pkt->status = nvsp_status_success;
906 net_vsc_pkt->data = (void *)((unsigned long)net_dev->rx_buf +
907 vm_xfer_page_pkt->ranges[i].byte_offset);
908 net_vsc_pkt->tot_data_buf_len =
909 vm_xfer_page_pkt->ranges[i].byte_count;
911 hv_rf_on_receive(net_dev, device, net_vsc_pkt);
912 if (net_vsc_pkt->status != nvsp_status_success) {
913 status = nvsp_status_failure;
918 * Moved completion call back here so that all received
919 * messages (not just data messages) will trigger a response
920 * message back to the host.
922 hv_nv_on_receive_completion(device, vm_xfer_page_pkt->d.transaction_id,
924 hv_rf_receive_rollup(net_dev);
928 * Net VSC on receive completion
930 * Send a receive completion packet to RNDIS device (ie NetVsp)
933 hv_nv_on_receive_completion(struct hv_device *device, uint64_t tid,
936 nvsp_msg rx_comp_msg;
940 rx_comp_msg.hdr.msg_type = nvsp_msg_1_type_send_rndis_pkt_complete;
942 /* Pass in the status */
943 rx_comp_msg.msgs.vers_1_msgs.send_rndis_pkt_complete.status =
947 /* Send the completion */
948 ret = hv_vmbus_channel_send_packet(device->channel, &rx_comp_msg,
949 sizeof(nvsp_msg), tid, HV_VMBUS_PACKET_TYPE_COMPLETION, 0);
953 } else if (ret == EAGAIN) {
954 /* no more room... wait a bit and attempt to retry 3 times */
959 goto retry_send_cmplt;
965 * Net VSC on channel callback
968 hv_nv_on_channel_callback(void *context)
970 struct hv_device *device = (struct hv_device *)context;
972 device_t dev = device->device;
975 hv_vm_packet_descriptor *desc;
977 int bufferlen = NETVSC_PACKET_SIZE;
980 net_dev = hv_nv_get_inbound_net_device(device);
984 buffer = net_dev->callback_buf;
987 ret = hv_vmbus_channel_recv_packet_raw(device->channel,
988 buffer, bufferlen, &bytes_rxed, &request_id);
990 if (bytes_rxed > 0) {
991 desc = (hv_vm_packet_descriptor *)buffer;
992 switch (desc->type) {
993 case HV_VMBUS_PACKET_TYPE_COMPLETION:
994 hv_nv_on_send_completion(net_dev, device, desc);
996 case HV_VMBUS_PACKET_TYPE_DATA_USING_TRANSFER_PAGES:
997 hv_nv_on_receive(net_dev, device, desc);
1001 "hv_cb recv unknow type %d "
1002 " packet\n", desc->type);
1008 } else if (ret == ENOBUFS) {
1009 /* Handle large packet */
1010 if (bufferlen > NETVSC_PACKET_SIZE) {
1011 free(buffer, M_NETVSC);
1015 /* alloc new buffer */
1016 buffer = malloc(bytes_rxed, M_NETVSC, M_NOWAIT);
1017 if (buffer == NULL) {
1019 "hv_cb malloc buffer failed, len=%u\n",
1024 bufferlen = bytes_rxed;
1028 if (bufferlen > NETVSC_PACKET_SIZE)
1029 free(buffer, M_NETVSC);
1031 hv_rf_channel_rollup(net_dev);