2 * Copyright (c) 2009-2012,2016 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 /* priv1 and priv2 are consumed by the main driver */
53 #define hv_chan_rdbuf hv_chan_priv3
55 MALLOC_DEFINE(M_NETVSC, "netvsc", "Hyper-V netvsc driver");
58 * Forward declarations
60 static void hv_nv_on_channel_callback(void *xchan);
61 static int hv_nv_init_send_buffer_with_net_vsp(struct hv_device *device);
62 static int hv_nv_init_rx_buffer_with_net_vsp(struct hv_device *device);
63 static int hv_nv_destroy_send_buffer(netvsc_dev *net_dev);
64 static int hv_nv_destroy_rx_buffer(netvsc_dev *net_dev);
65 static int hv_nv_connect_to_vsp(struct hv_device *device);
66 static void hv_nv_on_send_completion(netvsc_dev *net_dev,
67 struct hv_device *device, struct hv_vmbus_channel *, hv_vm_packet_descriptor *pkt);
68 static void hv_nv_on_receive_completion(struct hv_vmbus_channel *chan,
69 uint64_t tid, uint32_t status);
70 static void hv_nv_on_receive(netvsc_dev *net_dev,
71 struct hv_device *device, struct hv_vmbus_channel *chan,
72 hv_vm_packet_descriptor *pkt);
77 static inline netvsc_dev *
78 hv_nv_alloc_net_device(struct hv_device *device)
81 hn_softc_t *sc = device_get_softc(device->device);
83 net_dev = malloc(sizeof(netvsc_dev), M_NETVSC, M_WAITOK | M_ZERO);
85 net_dev->dev = device;
86 net_dev->destroy = FALSE;
87 sc->net_dev = net_dev;
95 static inline netvsc_dev *
96 hv_nv_get_outbound_net_device(struct hv_device *device)
98 hn_softc_t *sc = device_get_softc(device->device);
99 netvsc_dev *net_dev = sc->net_dev;;
101 if ((net_dev != NULL) && net_dev->destroy) {
111 static inline netvsc_dev *
112 hv_nv_get_inbound_net_device(struct hv_device *device)
114 hn_softc_t *sc = device_get_softc(device->device);
115 netvsc_dev *net_dev = sc->net_dev;;
117 if (net_dev == NULL) {
121 * When the device is being destroyed; we only
122 * permit incoming packets if and only if there
123 * are outstanding sends.
125 if (net_dev->destroy) {
133 hv_nv_get_next_send_section(netvsc_dev *net_dev)
135 unsigned long bitsmap_words = net_dev->bitsmap_words;
136 unsigned long *bitsmap = net_dev->send_section_bitsmap;
138 int ret = NVSP_1_CHIMNEY_SEND_INVALID_SECTION_INDEX;
141 for (i = 0; i < bitsmap_words; i++) {
142 idx = ffsl(~bitsmap[i]);
147 KASSERT(i * BITS_PER_LONG + idx < net_dev->send_section_count,
148 ("invalid i %d and idx %lu", i, idx));
150 if (atomic_testandset_long(&bitsmap[i], idx))
153 ret = i * BITS_PER_LONG + idx;
161 * Net VSC initialize receive buffer with net VSP
163 * Net VSP: Network virtual services client, also known as the
164 * Hyper-V extensible switch and the synthetic data path.
167 hv_nv_init_rx_buffer_with_net_vsp(struct hv_device *device)
173 net_dev = hv_nv_get_outbound_net_device(device);
178 net_dev->rx_buf = contigmalloc(net_dev->rx_buf_size, M_NETVSC,
179 M_ZERO, 0UL, BUS_SPACE_MAXADDR, PAGE_SIZE, 0);
182 * Establish the GPADL handle for this buffer on this channel.
183 * Note: This call uses the vmbus connection rather than the
184 * channel to establish the gpadl handle.
185 * GPADL: Guest physical address descriptor list.
187 ret = hv_vmbus_channel_establish_gpadl(
188 device->channel, net_dev->rx_buf,
189 net_dev->rx_buf_size, &net_dev->rx_buf_gpadl_handle);
194 /* sema_wait(&ext->channel_init_sema); KYS CHECK */
196 /* Notify the NetVsp of the gpadl handle */
197 init_pkt = &net_dev->channel_init_packet;
199 memset(init_pkt, 0, sizeof(nvsp_msg));
201 init_pkt->hdr.msg_type = nvsp_msg_1_type_send_rx_buf;
202 init_pkt->msgs.vers_1_msgs.send_rx_buf.gpadl_handle =
203 net_dev->rx_buf_gpadl_handle;
204 init_pkt->msgs.vers_1_msgs.send_rx_buf.id =
205 NETVSC_RECEIVE_BUFFER_ID;
207 /* Send the gpadl notification request */
209 ret = hv_vmbus_channel_send_packet(device->channel, init_pkt,
210 sizeof(nvsp_msg), (uint64_t)(uintptr_t)init_pkt,
211 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND,
212 HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
217 sema_wait(&net_dev->channel_init_sema);
219 /* Check the response */
220 if (init_pkt->msgs.vers_1_msgs.send_rx_buf_complete.status
221 != nvsp_status_success) {
226 net_dev->rx_section_count =
227 init_pkt->msgs.vers_1_msgs.send_rx_buf_complete.num_sections;
229 net_dev->rx_sections = malloc(net_dev->rx_section_count *
230 sizeof(nvsp_1_rx_buf_section), M_NETVSC, M_WAITOK);
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,
333 hv_nv_destroy_send_buffer(net_dev);
340 * Net VSC destroy receive buffer
343 hv_nv_destroy_rx_buffer(netvsc_dev *net_dev)
345 nvsp_msg *revoke_pkt;
349 * If we got a section count, it means we received a
350 * send_rx_buf_complete msg
351 * (ie sent nvsp_msg_1_type_send_rx_buf msg) therefore,
352 * we need to send a revoke msg here
354 if (net_dev->rx_section_count) {
355 /* Send the revoke receive buffer */
356 revoke_pkt = &net_dev->revoke_packet;
357 memset(revoke_pkt, 0, sizeof(nvsp_msg));
359 revoke_pkt->hdr.msg_type = nvsp_msg_1_type_revoke_rx_buf;
360 revoke_pkt->msgs.vers_1_msgs.revoke_rx_buf.id =
361 NETVSC_RECEIVE_BUFFER_ID;
363 ret = hv_vmbus_channel_send_packet(net_dev->dev->channel,
364 revoke_pkt, sizeof(nvsp_msg),
365 (uint64_t)(uintptr_t)revoke_pkt,
366 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND, 0);
369 * If we failed here, we might as well return and have a leak
370 * rather than continue and a bugchk
377 /* Tear down the gpadl on the vsp end */
378 if (net_dev->rx_buf_gpadl_handle) {
379 ret = hv_vmbus_channel_teardown_gpdal(net_dev->dev->channel,
380 net_dev->rx_buf_gpadl_handle);
382 * If we failed here, we might as well return and have a leak
383 * rather than continue and a bugchk
388 net_dev->rx_buf_gpadl_handle = 0;
391 if (net_dev->rx_buf) {
392 /* Free up the receive buffer */
393 contigfree(net_dev->rx_buf, net_dev->rx_buf_size, M_NETVSC);
394 net_dev->rx_buf = NULL;
397 if (net_dev->rx_sections) {
398 free(net_dev->rx_sections, M_NETVSC);
399 net_dev->rx_sections = NULL;
400 net_dev->rx_section_count = 0;
407 * Net VSC destroy send buffer
410 hv_nv_destroy_send_buffer(netvsc_dev *net_dev)
412 nvsp_msg *revoke_pkt;
416 * If we got a section count, it means we received a
417 * send_rx_buf_complete msg
418 * (ie sent nvsp_msg_1_type_send_rx_buf msg) therefore,
419 * we need to send a revoke msg here
421 if (net_dev->send_section_size) {
422 /* Send the revoke send buffer */
423 revoke_pkt = &net_dev->revoke_packet;
424 memset(revoke_pkt, 0, sizeof(nvsp_msg));
426 revoke_pkt->hdr.msg_type =
427 nvsp_msg_1_type_revoke_send_buf;
428 revoke_pkt->msgs.vers_1_msgs.revoke_send_buf.id =
429 NETVSC_SEND_BUFFER_ID;
431 ret = hv_vmbus_channel_send_packet(net_dev->dev->channel,
432 revoke_pkt, sizeof(nvsp_msg),
433 (uint64_t)(uintptr_t)revoke_pkt,
434 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND, 0);
436 * If we failed here, we might as well return and have a leak
437 * rather than continue and a bugchk
444 /* Tear down the gpadl on the vsp end */
445 if (net_dev->send_buf_gpadl_handle) {
446 ret = hv_vmbus_channel_teardown_gpdal(net_dev->dev->channel,
447 net_dev->send_buf_gpadl_handle);
450 * If we failed here, we might as well return and have a leak
451 * rather than continue and a bugchk
456 net_dev->send_buf_gpadl_handle = 0;
459 if (net_dev->send_buf) {
460 /* Free up the receive buffer */
461 contigfree(net_dev->send_buf, net_dev->send_buf_size, M_NETVSC);
462 net_dev->send_buf = NULL;
465 if (net_dev->send_section_bitsmap) {
466 free(net_dev->send_section_bitsmap, M_NETVSC);
474 * Attempt to negotiate the caller-specified NVSP version
476 * For NVSP v2, Server 2008 R2 does not set
477 * init_pkt->msgs.init_msgs.init_compl.negotiated_prot_vers
478 * to the negotiated version, so we cannot rely on that.
481 hv_nv_negotiate_nvsp_protocol(struct hv_device *device, netvsc_dev *net_dev,
487 init_pkt = &net_dev->channel_init_packet;
488 memset(init_pkt, 0, sizeof(nvsp_msg));
489 init_pkt->hdr.msg_type = nvsp_msg_type_init;
492 * Specify parameter as the only acceptable protocol version
494 init_pkt->msgs.init_msgs.init.p1.protocol_version = nvsp_ver;
495 init_pkt->msgs.init_msgs.init.protocol_version_2 = nvsp_ver;
497 /* Send the init request */
498 ret = hv_vmbus_channel_send_packet(device->channel, init_pkt,
499 sizeof(nvsp_msg), (uint64_t)(uintptr_t)init_pkt,
500 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND,
501 HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
505 sema_wait(&net_dev->channel_init_sema);
507 if (init_pkt->msgs.init_msgs.init_compl.status != nvsp_status_success)
514 * Send NDIS version 2 config packet containing MTU.
516 * Not valid for NDIS version 1.
519 hv_nv_send_ndis_config(struct hv_device *device, uint32_t mtu)
525 net_dev = hv_nv_get_outbound_net_device(device);
530 * Set up configuration packet, write MTU
531 * Indicate we are capable of handling VLAN tags
533 init_pkt = &net_dev->channel_init_packet;
534 memset(init_pkt, 0, sizeof(nvsp_msg));
535 init_pkt->hdr.msg_type = nvsp_msg_2_type_send_ndis_config;
536 init_pkt->msgs.vers_2_msgs.send_ndis_config.mtu = mtu;
538 msgs.vers_2_msgs.send_ndis_config.capabilities.u1.u2.ieee8021q
541 /* Send the configuration packet */
542 ret = hv_vmbus_channel_send_packet(device->channel, init_pkt,
543 sizeof(nvsp_msg), (uint64_t)(uintptr_t)init_pkt,
544 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND, 0);
552 * Net VSC connect to VSP
555 hv_nv_connect_to_vsp(struct hv_device *device)
559 uint32_t ndis_version;
560 uint32_t protocol_list[] = { NVSP_PROTOCOL_VERSION_1,
561 NVSP_PROTOCOL_VERSION_2,
562 NVSP_PROTOCOL_VERSION_4,
563 NVSP_PROTOCOL_VERSION_5 };
565 int protocol_number = nitems(protocol_list);
567 device_t dev = device->device;
568 hn_softc_t *sc = device_get_softc(dev);
569 struct ifnet *ifp = sc->hn_ifp;
571 net_dev = hv_nv_get_outbound_net_device(device);
577 * Negotiate the NVSP version. Try the latest NVSP first.
579 for (i = protocol_number - 1; i >= 0; i--) {
580 if (hv_nv_negotiate_nvsp_protocol(device, net_dev,
581 protocol_list[i]) == 0) {
582 net_dev->nvsp_version = protocol_list[i];
584 device_printf(dev, "Netvsc: got version 0x%x\n",
585 net_dev->nvsp_version);
592 device_printf(dev, "failed to negotiate a valid "
598 * Set the MTU if supported by this NVSP protocol version
599 * This needs to be right after the NVSP init message per Haiyang
601 if (net_dev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
602 ret = hv_nv_send_ndis_config(device, ifp->if_mtu);
605 * Send the NDIS version
607 init_pkt = &net_dev->channel_init_packet;
609 memset(init_pkt, 0, sizeof(nvsp_msg));
611 if (net_dev->nvsp_version <= NVSP_PROTOCOL_VERSION_4) {
612 ndis_version = NDIS_VERSION_6_1;
614 ndis_version = NDIS_VERSION_6_30;
617 init_pkt->hdr.msg_type = nvsp_msg_1_type_send_ndis_vers;
618 init_pkt->msgs.vers_1_msgs.send_ndis_vers.ndis_major_vers =
619 (ndis_version & 0xFFFF0000) >> 16;
620 init_pkt->msgs.vers_1_msgs.send_ndis_vers.ndis_minor_vers =
621 ndis_version & 0xFFFF;
623 /* Send the init request */
625 ret = hv_vmbus_channel_send_packet(device->channel, init_pkt,
626 sizeof(nvsp_msg), (uint64_t)(uintptr_t)init_pkt,
627 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND, 0);
632 * TODO: BUGBUG - We have to wait for the above msg since the netvsp
633 * uses KMCL which acknowledges packet (completion packet)
634 * since our Vmbus always set the
635 * HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED flag
637 /* sema_wait(&NetVscChannel->channel_init_sema); */
639 /* Post the big receive buffer to NetVSP */
640 if (net_dev->nvsp_version <= NVSP_PROTOCOL_VERSION_2)
641 net_dev->rx_buf_size = NETVSC_RECEIVE_BUFFER_SIZE_LEGACY;
643 net_dev->rx_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
644 net_dev->send_buf_size = NETVSC_SEND_BUFFER_SIZE;
646 ret = hv_nv_init_rx_buffer_with_net_vsp(device);
648 ret = hv_nv_init_send_buffer_with_net_vsp(device);
655 * Net VSC disconnect from VSP
658 hv_nv_disconnect_from_vsp(netvsc_dev *net_dev)
660 hv_nv_destroy_rx_buffer(net_dev);
661 hv_nv_destroy_send_buffer(net_dev);
665 hv_nv_subchan_attach(struct hv_vmbus_channel *chan)
668 chan->hv_chan_rdbuf = malloc(NETVSC_PACKET_SIZE, M_NETVSC, M_WAITOK);
669 hv_vmbus_channel_open(chan, NETVSC_DEVICE_RING_BUFFER_SIZE,
670 NETVSC_DEVICE_RING_BUFFER_SIZE, NULL, 0,
671 hv_nv_on_channel_callback, chan);
675 * Net VSC on device add
677 * Callback when the device belonging to this driver is added
680 hv_nv_on_device_add(struct hv_device *device, void *additional_info)
682 struct hv_vmbus_channel *chan = device->channel;
686 net_dev = hv_nv_alloc_net_device(device);
690 /* Initialize the NetVSC channel extension */
692 sema_init(&net_dev->channel_init_sema, 0, "netdev_sema");
694 chan->hv_chan_rdbuf = malloc(NETVSC_PACKET_SIZE, M_NETVSC, M_WAITOK);
699 ret = hv_vmbus_channel_open(chan,
700 NETVSC_DEVICE_RING_BUFFER_SIZE, NETVSC_DEVICE_RING_BUFFER_SIZE,
701 NULL, 0, hv_nv_on_channel_callback, chan);
703 free(chan->hv_chan_rdbuf, M_NETVSC);
708 * Connect with the NetVsp
710 ret = hv_nv_connect_to_vsp(device);
717 /* Now, we can close the channel safely */
718 free(chan->hv_chan_rdbuf, M_NETVSC);
719 hv_vmbus_channel_close(chan);
723 * Free the packet buffers on the netvsc device packet queue.
724 * Release other resources.
726 sema_destroy(&net_dev->channel_init_sema);
727 free(net_dev, M_NETVSC);
733 * Net VSC on device remove
736 hv_nv_on_device_remove(struct hv_device *device, boolean_t destroy_channel)
738 hn_softc_t *sc = device_get_softc(device->device);
739 netvsc_dev *net_dev = sc->net_dev;;
741 /* Stop outbound traffic ie sends and receives completions */
742 net_dev->destroy = TRUE;
744 hv_nv_disconnect_from_vsp(net_dev);
746 /* At this point, no one should be accessing net_dev except in here */
748 /* Now, we can close the channel safely */
750 if (!destroy_channel) {
751 device->channel->state =
752 HV_CHANNEL_CLOSING_NONDESTRUCTIVE_STATE;
755 free(device->channel->hv_chan_rdbuf, M_NETVSC);
756 hv_vmbus_channel_close(device->channel);
758 sema_destroy(&net_dev->channel_init_sema);
759 free(net_dev, M_NETVSC);
765 * Net VSC on send completion
768 hv_nv_on_send_completion(netvsc_dev *net_dev,
769 struct hv_device *device, struct hv_vmbus_channel *chan,
770 hv_vm_packet_descriptor *pkt)
772 nvsp_msg *nvsp_msg_pkt;
773 netvsc_packet *net_vsc_pkt;
776 (nvsp_msg *)((unsigned long)pkt + (pkt->data_offset8 << 3));
778 if (nvsp_msg_pkt->hdr.msg_type == nvsp_msg_type_init_complete
779 || nvsp_msg_pkt->hdr.msg_type
780 == nvsp_msg_1_type_send_rx_buf_complete
781 || nvsp_msg_pkt->hdr.msg_type
782 == nvsp_msg_1_type_send_send_buf_complete
783 || nvsp_msg_pkt->hdr.msg_type
784 == nvsp_msg5_type_subchannel) {
785 /* Copy the response back */
786 memcpy(&net_dev->channel_init_packet, nvsp_msg_pkt,
788 sema_post(&net_dev->channel_init_sema);
789 } else if (nvsp_msg_pkt->hdr.msg_type ==
790 nvsp_msg_1_type_send_rndis_pkt_complete) {
791 /* Get the send context */
793 (netvsc_packet *)(unsigned long)pkt->transaction_id;
794 if (NULL != net_vsc_pkt) {
795 if (net_vsc_pkt->send_buf_section_idx !=
796 NVSP_1_CHIMNEY_SEND_INVALID_SECTION_INDEX) {
800 idx = net_vsc_pkt->send_buf_section_idx /
802 KASSERT(idx < net_dev->bitsmap_words,
803 ("invalid section index %u",
804 net_vsc_pkt->send_buf_section_idx));
806 (net_vsc_pkt->send_buf_section_idx %
809 KASSERT(net_dev->send_section_bitsmap[idx] &
811 ("index bitmap 0x%lx, section index %u, "
812 "bitmap idx %d, bitmask 0x%lx",
813 net_dev->send_section_bitsmap[idx],
814 net_vsc_pkt->send_buf_section_idx,
817 &net_dev->send_section_bitsmap[idx], mask);
820 /* Notify the layer above us */
821 net_vsc_pkt->compl.send.on_send_completion(chan,
822 net_vsc_pkt->compl.send.send_completion_context);
830 * Sends a packet on the specified Hyper-V device.
831 * Returns 0 on success, non-zero on failure.
834 hv_nv_on_send(struct hv_vmbus_channel *chan, netvsc_packet *pkt)
839 send_msg.hdr.msg_type = nvsp_msg_1_type_send_rndis_pkt;
840 if (pkt->is_data_pkt) {
842 send_msg.msgs.vers_1_msgs.send_rndis_pkt.chan_type = 0;
844 /* 1 is RMC_CONTROL */
845 send_msg.msgs.vers_1_msgs.send_rndis_pkt.chan_type = 1;
848 send_msg.msgs.vers_1_msgs.send_rndis_pkt.send_buf_section_idx =
849 pkt->send_buf_section_idx;
850 send_msg.msgs.vers_1_msgs.send_rndis_pkt.send_buf_section_size =
851 pkt->send_buf_section_size;
853 if (pkt->page_buf_count) {
854 ret = hv_vmbus_channel_send_packet_pagebuffer(chan,
855 pkt->page_buffers, pkt->page_buf_count,
856 &send_msg, sizeof(nvsp_msg), (uint64_t)(uintptr_t)pkt);
858 ret = hv_vmbus_channel_send_packet(chan,
859 &send_msg, sizeof(nvsp_msg), (uint64_t)(uintptr_t)pkt,
860 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND,
861 HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
870 * In the FreeBSD Hyper-V virtual world, this function deals exclusively
871 * with virtual addresses.
874 hv_nv_on_receive(netvsc_dev *net_dev, struct hv_device *device,
875 struct hv_vmbus_channel *chan, hv_vm_packet_descriptor *pkt)
877 hv_vm_transfer_page_packet_header *vm_xfer_page_pkt;
878 nvsp_msg *nvsp_msg_pkt;
879 netvsc_packet vsc_pkt;
880 netvsc_packet *net_vsc_pkt = &vsc_pkt;
881 device_t dev = device->device;
884 int status = nvsp_status_success;
887 * All inbound packets other than send completion should be
890 if (pkt->type != HV_VMBUS_PACKET_TYPE_DATA_USING_TRANSFER_PAGES) {
891 device_printf(dev, "packet type %d is invalid!\n", pkt->type);
895 nvsp_msg_pkt = (nvsp_msg *)((unsigned long)pkt
896 + (pkt->data_offset8 << 3));
898 /* Make sure this is a valid nvsp packet */
899 if (nvsp_msg_pkt->hdr.msg_type != nvsp_msg_1_type_send_rndis_pkt) {
900 device_printf(dev, "packet hdr type %d is invalid!\n",
905 vm_xfer_page_pkt = (hv_vm_transfer_page_packet_header *)pkt;
907 if (vm_xfer_page_pkt->transfer_page_set_id !=
908 NETVSC_RECEIVE_BUFFER_ID) {
909 device_printf(dev, "transfer_page_set_id %d is invalid!\n",
910 vm_xfer_page_pkt->transfer_page_set_id);
914 count = vm_xfer_page_pkt->range_count;
915 net_vsc_pkt->device = device;
917 /* Each range represents 1 RNDIS pkt that contains 1 Ethernet frame */
918 for (i = 0; i < count; i++) {
919 net_vsc_pkt->status = nvsp_status_success;
920 net_vsc_pkt->data = (void *)((unsigned long)net_dev->rx_buf +
921 vm_xfer_page_pkt->ranges[i].byte_offset);
922 net_vsc_pkt->tot_data_buf_len =
923 vm_xfer_page_pkt->ranges[i].byte_count;
925 hv_rf_on_receive(net_dev, device, chan, net_vsc_pkt);
926 if (net_vsc_pkt->status != nvsp_status_success) {
927 status = nvsp_status_failure;
932 * Moved completion call back here so that all received
933 * messages (not just data messages) will trigger a response
934 * message back to the host.
936 hv_nv_on_receive_completion(chan, vm_xfer_page_pkt->d.transaction_id,
941 * Net VSC on receive completion
943 * Send a receive completion packet to RNDIS device (ie NetVsp)
946 hv_nv_on_receive_completion(struct hv_vmbus_channel *chan, uint64_t tid,
949 nvsp_msg rx_comp_msg;
953 rx_comp_msg.hdr.msg_type = nvsp_msg_1_type_send_rndis_pkt_complete;
955 /* Pass in the status */
956 rx_comp_msg.msgs.vers_1_msgs.send_rndis_pkt_complete.status =
960 /* Send the completion */
961 ret = hv_vmbus_channel_send_packet(chan, &rx_comp_msg,
962 sizeof(nvsp_msg), tid, HV_VMBUS_PACKET_TYPE_COMPLETION, 0);
966 } else if (ret == EAGAIN) {
967 /* no more room... wait a bit and attempt to retry 3 times */
972 goto retry_send_cmplt;
978 * Net VSC receiving vRSS send table from VSP
981 hv_nv_send_table(struct hv_device *device, hv_vm_packet_descriptor *pkt)
984 nvsp_msg *nvsp_msg_pkt;
986 uint32_t count, *table;
988 net_dev = hv_nv_get_inbound_net_device(device);
993 (nvsp_msg *)((unsigned long)pkt + (pkt->data_offset8 << 3));
995 if (nvsp_msg_pkt->hdr.msg_type !=
996 nvsp_msg5_type_send_indirection_table) {
997 printf("Netvsc: !Warning! receive msg type not "
998 "send_indirection_table. type = %d\n",
999 nvsp_msg_pkt->hdr.msg_type);
1003 count = nvsp_msg_pkt->msgs.vers_5_msgs.send_table.count;
1004 if (count != VRSS_SEND_TABLE_SIZE) {
1005 printf("Netvsc: Received wrong send table size: %u\n", count);
1009 table = (uint32_t *)
1010 ((unsigned long)&nvsp_msg_pkt->msgs.vers_5_msgs.send_table +
1011 nvsp_msg_pkt->msgs.vers_5_msgs.send_table.offset);
1013 for (i = 0; i < count; i++)
1014 net_dev->vrss_send_table[i] = table[i];
1018 * Net VSC on channel callback
1021 hv_nv_on_channel_callback(void *xchan)
1023 struct hv_vmbus_channel *chan = xchan;
1024 struct hv_device *device = chan->device;
1025 netvsc_dev *net_dev;
1026 device_t dev = device->device;
1027 uint32_t bytes_rxed;
1028 uint64_t request_id;
1029 hv_vm_packet_descriptor *desc;
1031 int bufferlen = NETVSC_PACKET_SIZE;
1034 net_dev = hv_nv_get_inbound_net_device(device);
1035 if (net_dev == NULL)
1038 buffer = chan->hv_chan_rdbuf;
1041 ret = hv_vmbus_channel_recv_packet_raw(chan,
1042 buffer, bufferlen, &bytes_rxed, &request_id);
1044 if (bytes_rxed > 0) {
1045 desc = (hv_vm_packet_descriptor *)buffer;
1046 switch (desc->type) {
1047 case HV_VMBUS_PACKET_TYPE_COMPLETION:
1048 hv_nv_on_send_completion(net_dev, device,
1051 case HV_VMBUS_PACKET_TYPE_DATA_USING_TRANSFER_PAGES:
1052 hv_nv_on_receive(net_dev, device, chan, desc);
1054 case HV_VMBUS_PACKET_TYPE_DATA_IN_BAND:
1055 hv_nv_send_table(device, desc);
1059 "hv_cb recv unknow type %d "
1060 " packet\n", desc->type);
1066 } else if (ret == ENOBUFS) {
1067 /* Handle large packet */
1068 if (bufferlen > NETVSC_PACKET_SIZE) {
1069 free(buffer, M_NETVSC);
1073 /* alloc new buffer */
1074 buffer = malloc(bytes_rxed, M_NETVSC, M_NOWAIT);
1075 if (buffer == NULL) {
1077 "hv_cb malloc buffer failed, len=%u\n",
1082 bufferlen = bytes_rxed;
1086 if (bufferlen > NETVSC_PACKET_SIZE)
1087 free(buffer, M_NETVSC);
1089 hv_rf_channel_rollup(chan);