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_arp.h>
43 #include <machine/bus.h>
44 #include <machine/atomic.h>
46 #include <dev/hyperv/include/hyperv.h>
47 #include <dev/hyperv/include/vmbus_xact.h>
48 #include <dev/hyperv/netvsc/hv_net_vsc.h>
49 #include <dev/hyperv/netvsc/hv_rndis.h>
50 #include <dev/hyperv/netvsc/hv_rndis_filter.h>
51 #include <dev/hyperv/netvsc/if_hnreg.h>
53 MALLOC_DEFINE(M_NETVSC, "netvsc", "Hyper-V netvsc driver");
56 * Forward declarations
58 static void hv_nv_on_channel_callback(struct vmbus_channel *chan,
60 static int hv_nv_init_send_buffer_with_net_vsp(struct hn_softc *sc);
61 static int hv_nv_init_rx_buffer_with_net_vsp(struct hn_softc *);
62 static int hv_nv_destroy_send_buffer(netvsc_dev *net_dev);
63 static int hv_nv_destroy_rx_buffer(netvsc_dev *net_dev);
64 static int hv_nv_connect_to_vsp(struct hn_softc *sc);
65 static void hv_nv_on_send_completion(netvsc_dev *net_dev,
66 struct vmbus_channel *, const struct vmbus_chanpkt_hdr *pkt);
67 static void hv_nv_on_receive_completion(struct vmbus_channel *chan,
69 static void hv_nv_on_receive(netvsc_dev *net_dev,
70 struct hn_rx_ring *rxr, struct vmbus_channel *chan,
71 const struct vmbus_chanpkt_hdr *pkt);
72 static void hn_nvs_sent_none(struct hn_send_ctx *sndc,
73 struct netvsc_dev_ *net_dev, struct vmbus_channel *chan,
76 static struct hn_send_ctx hn_send_ctx_none =
77 HN_SEND_CTX_INITIALIZER(hn_nvs_sent_none, NULL);
82 static inline netvsc_dev *
83 hv_nv_alloc_net_device(struct hn_softc *sc)
87 net_dev = malloc(sizeof(netvsc_dev), M_NETVSC, M_WAITOK | M_ZERO);
90 net_dev->destroy = FALSE;
91 sc->net_dev = net_dev;
97 * XXX unnecessary; nuke it.
99 static inline netvsc_dev *
100 hv_nv_get_outbound_net_device(struct hn_softc *sc)
106 * XXX unnecessary; nuke it.
108 static inline netvsc_dev *
109 hv_nv_get_inbound_net_device(struct hn_softc *sc)
115 hv_nv_get_next_send_section(netvsc_dev *net_dev)
117 unsigned long bitsmap_words = net_dev->bitsmap_words;
118 unsigned long *bitsmap = net_dev->send_section_bitsmap;
120 int ret = HN_NVS_CHIM_IDX_INVALID;
123 for (i = 0; i < bitsmap_words; i++) {
124 idx = ffsl(~bitsmap[i]);
129 KASSERT(i * BITS_PER_LONG + idx < net_dev->send_section_count,
130 ("invalid i %d and idx %lu", i, idx));
132 if (atomic_testandset_long(&bitsmap[i], idx))
135 ret = i * BITS_PER_LONG + idx;
143 * Net VSC initialize receive buffer with net VSP
145 * Net VSP: Network virtual services client, also known as the
146 * Hyper-V extensible switch and the synthetic data path.
149 hv_nv_init_rx_buffer_with_net_vsp(struct hn_softc *sc)
151 struct vmbus_xact *xact;
152 struct hn_nvs_rxbuf_conn *conn;
153 const struct hn_nvs_rxbuf_connresp *resp;
155 struct hn_send_ctx sndc;
160 net_dev = hv_nv_get_outbound_net_device(sc);
165 net_dev->rx_buf = hyperv_dmamem_alloc(bus_get_dma_tag(sc->hn_dev),
166 PAGE_SIZE, 0, net_dev->rx_buf_size, &net_dev->rxbuf_dma,
167 BUS_DMA_WAITOK | BUS_DMA_ZERO);
168 if (net_dev->rx_buf == NULL) {
169 device_printf(sc->hn_dev, "allocate rxbuf failed\n");
174 * Connect the RXBUF GPADL to the primary channel.
177 * Only primary channel has RXBUF connected to it. Sub-channels
178 * just share this RXBUF.
180 error = vmbus_chan_gpadl_connect(sc->hn_prichan,
181 net_dev->rxbuf_dma.hv_paddr, net_dev->rx_buf_size,
182 &net_dev->rx_buf_gpadl_handle);
184 if_printf(sc->hn_ifp, "rxbuf gpadl connect failed: %d\n",
190 * Connect RXBUF to NVS.
193 xact = vmbus_xact_get(sc->hn_xact, sizeof(*conn));
195 if_printf(sc->hn_ifp, "no xact for nvs rxbuf conn\n");
200 conn = vmbus_xact_req_data(xact);
201 conn->nvs_type = HN_NVS_TYPE_RXBUF_CONN;
202 conn->nvs_gpadl = net_dev->rx_buf_gpadl_handle;
203 conn->nvs_sig = HN_NVS_RXBUF_SIG;
205 hn_send_ctx_init_simple(&sndc, hn_nvs_sent_xact, xact);
206 vmbus_xact_activate(xact);
208 error = hn_nvs_send(sc->hn_prichan, VMBUS_CHANPKT_FLAG_RC,
209 conn, sizeof(*conn), &sndc);
211 if_printf(sc->hn_ifp, "send nvs rxbuf conn failed: %d\n",
213 vmbus_xact_deactivate(xact);
214 vmbus_xact_put(xact);
218 resp = vmbus_xact_wait(xact, &resp_len);
219 if (resp_len < sizeof(*resp)) {
220 if_printf(sc->hn_ifp, "invalid rxbuf conn resp length %zu\n",
222 vmbus_xact_put(xact);
226 if (resp->nvs_type != HN_NVS_TYPE_RXBUF_CONNRESP) {
227 if_printf(sc->hn_ifp, "not rxbuf conn resp, type %u\n",
229 vmbus_xact_put(xact);
234 status = resp->nvs_status;
235 vmbus_xact_put(xact);
237 if (status != HN_NVS_STATUS_OK) {
238 if_printf(sc->hn_ifp, "rxbuf conn failed: %x\n", status);
242 net_dev->rx_section_count = 1;
247 hv_nv_destroy_rx_buffer(net_dev);
252 * Net VSC initialize send buffer with net VSP
255 hv_nv_init_send_buffer_with_net_vsp(struct hn_softc *sc)
257 struct hn_send_ctx sndc;
258 struct vmbus_xact *xact;
259 struct hn_nvs_chim_conn *chim;
260 const struct hn_nvs_chim_connresp *resp;
262 uint32_t status, sectsz;
266 net_dev = hv_nv_get_outbound_net_device(sc);
271 net_dev->send_buf = hyperv_dmamem_alloc(bus_get_dma_tag(sc->hn_dev),
272 PAGE_SIZE, 0, net_dev->send_buf_size, &net_dev->txbuf_dma,
273 BUS_DMA_WAITOK | BUS_DMA_ZERO);
274 if (net_dev->send_buf == NULL) {
275 device_printf(sc->hn_dev, "allocate chimney txbuf failed\n");
280 * Connect chimney sending buffer GPADL to the primary channel.
283 * Only primary channel has chimney sending buffer connected to it.
284 * Sub-channels just share this chimney sending buffer.
286 error = vmbus_chan_gpadl_connect(sc->hn_prichan,
287 net_dev->txbuf_dma.hv_paddr, net_dev->send_buf_size,
288 &net_dev->send_buf_gpadl_handle);
290 if_printf(sc->hn_ifp, "chimney sending buffer gpadl "
291 "connect failed: %d\n", error);
296 * Connect chimney sending buffer to NVS
299 xact = vmbus_xact_get(sc->hn_xact, sizeof(*chim));
301 if_printf(sc->hn_ifp, "no xact for nvs chim conn\n");
306 chim = vmbus_xact_req_data(xact);
307 chim->nvs_type = HN_NVS_TYPE_CHIM_CONN;
308 chim->nvs_gpadl = net_dev->send_buf_gpadl_handle;
309 chim->nvs_sig = HN_NVS_CHIM_SIG;
311 hn_send_ctx_init_simple(&sndc, hn_nvs_sent_xact, xact);
312 vmbus_xact_activate(xact);
314 error = hn_nvs_send(sc->hn_prichan, VMBUS_CHANPKT_FLAG_RC,
315 chim, sizeof(*chim), &sndc);
317 if_printf(sc->hn_ifp, "send nvs chim conn failed: %d\n",
319 vmbus_xact_deactivate(xact);
320 vmbus_xact_put(xact);
324 resp = vmbus_xact_wait(xact, &resp_len);
325 if (resp_len < sizeof(*resp)) {
326 if_printf(sc->hn_ifp, "invalid chim conn resp length %zu\n",
328 vmbus_xact_put(xact);
332 if (resp->nvs_type != HN_NVS_TYPE_CHIM_CONNRESP) {
333 if_printf(sc->hn_ifp, "not chim conn resp, type %u\n",
335 vmbus_xact_put(xact);
340 status = resp->nvs_status;
341 sectsz = resp->nvs_sectsz;
342 vmbus_xact_put(xact);
344 if (status != HN_NVS_STATUS_OK) {
345 if_printf(sc->hn_ifp, "chim conn failed: %x\n", status);
350 if_printf(sc->hn_ifp, "zero chimney sending buffer "
355 net_dev->send_section_size = sectsz;
356 net_dev->send_section_count =
357 net_dev->send_buf_size / net_dev->send_section_size;
358 net_dev->bitsmap_words = howmany(net_dev->send_section_count,
360 net_dev->send_section_bitsmap =
361 malloc(net_dev->bitsmap_words * sizeof(long), M_NETVSC,
365 if_printf(sc->hn_ifp, "chimney sending buffer %u/%u\n",
366 net_dev->send_section_size, net_dev->send_section_count);
371 hv_nv_destroy_send_buffer(net_dev);
376 * Net VSC destroy receive buffer
379 hv_nv_destroy_rx_buffer(netvsc_dev *net_dev)
383 if (net_dev->rx_section_count) {
384 struct hn_nvs_rxbuf_disconn disconn;
387 * Disconnect RXBUF from NVS.
389 memset(&disconn, 0, sizeof(disconn));
390 disconn.nvs_type = HN_NVS_TYPE_RXBUF_DISCONN;
391 disconn.nvs_sig = HN_NVS_RXBUF_SIG;
393 /* NOTE: No response. */
394 ret = hn_nvs_send(net_dev->sc->hn_prichan,
395 VMBUS_CHANPKT_FLAG_NONE, &disconn, sizeof(disconn),
398 if_printf(net_dev->sc->hn_ifp,
399 "send rxbuf disconn failed: %d\n", ret);
402 net_dev->rx_section_count = 0;
405 /* Tear down the gpadl on the vsp end */
406 if (net_dev->rx_buf_gpadl_handle) {
407 ret = vmbus_chan_gpadl_disconnect(net_dev->sc->hn_prichan,
408 net_dev->rx_buf_gpadl_handle);
410 * If we failed here, we might as well return and have a leak
411 * rather than continue and a bugchk
416 net_dev->rx_buf_gpadl_handle = 0;
419 if (net_dev->rx_buf) {
420 /* Free up the receive buffer */
421 hyperv_dmamem_free(&net_dev->rxbuf_dma, net_dev->rx_buf);
422 net_dev->rx_buf = NULL;
429 * Net VSC destroy send buffer
432 hv_nv_destroy_send_buffer(netvsc_dev *net_dev)
436 if (net_dev->send_section_size) {
437 struct hn_nvs_chim_disconn disconn;
440 * Disconnect chimney sending buffer from NVS.
442 memset(&disconn, 0, sizeof(disconn));
443 disconn.nvs_type = HN_NVS_TYPE_CHIM_DISCONN;
444 disconn.nvs_sig = HN_NVS_CHIM_SIG;
446 /* NOTE: No response. */
447 ret = hn_nvs_send(net_dev->sc->hn_prichan,
448 VMBUS_CHANPKT_FLAG_NONE, &disconn, sizeof(disconn),
451 if_printf(net_dev->sc->hn_ifp,
452 "send chim disconn failed: %d\n", ret);
457 /* Tear down the gpadl on the vsp end */
458 if (net_dev->send_buf_gpadl_handle) {
459 ret = vmbus_chan_gpadl_disconnect(net_dev->sc->hn_prichan,
460 net_dev->send_buf_gpadl_handle);
463 * If we failed here, we might as well return and have a leak
464 * rather than continue and a bugchk
469 net_dev->send_buf_gpadl_handle = 0;
472 if (net_dev->send_buf) {
473 /* Free up the receive buffer */
474 hyperv_dmamem_free(&net_dev->txbuf_dma, net_dev->send_buf);
475 net_dev->send_buf = NULL;
478 if (net_dev->send_section_bitsmap) {
479 free(net_dev->send_section_bitsmap, M_NETVSC);
486 hv_nv_negotiate_nvsp_protocol(struct hn_softc *sc, netvsc_dev *net_dev,
489 struct hn_send_ctx sndc;
490 struct vmbus_xact *xact;
491 struct hn_nvs_init *init;
492 const struct hn_nvs_init_resp *resp;
497 xact = vmbus_xact_get(sc->hn_xact, sizeof(*init));
499 if_printf(sc->hn_ifp, "no xact for nvs init\n");
503 init = vmbus_xact_req_data(xact);
504 init->nvs_type = HN_NVS_TYPE_INIT;
505 init->nvs_ver_min = nvs_ver;
506 init->nvs_ver_max = nvs_ver;
508 vmbus_xact_activate(xact);
509 hn_send_ctx_init_simple(&sndc, hn_nvs_sent_xact, xact);
511 error = hn_nvs_send(sc->hn_prichan, VMBUS_CHANPKT_FLAG_RC,
512 init, sizeof(*init), &sndc);
514 if_printf(sc->hn_ifp, "send nvs init failed: %d\n", error);
515 vmbus_xact_deactivate(xact);
516 vmbus_xact_put(xact);
520 resp = vmbus_xact_wait(xact, &resp_len);
521 if (resp_len < sizeof(*resp)) {
522 if_printf(sc->hn_ifp, "invalid init resp length %zu\n",
524 vmbus_xact_put(xact);
527 if (resp->nvs_type != HN_NVS_TYPE_INIT_RESP) {
528 if_printf(sc->hn_ifp, "not init resp, type %u\n",
530 vmbus_xact_put(xact);
534 status = resp->nvs_status;
535 vmbus_xact_put(xact);
537 if (status != HN_NVS_STATUS_OK) {
538 if_printf(sc->hn_ifp, "nvs init failed for ver 0x%x\n",
546 * Send NDIS version 2 config packet containing MTU.
548 * Not valid for NDIS version 1.
551 hv_nv_send_ndis_config(struct hn_softc *sc, uint32_t mtu)
553 struct hn_nvs_ndis_conf conf;
556 memset(&conf, 0, sizeof(conf));
557 conf.nvs_type = HN_NVS_TYPE_NDIS_CONF;
559 conf.nvs_caps = HN_NVS_NDIS_CONF_VLAN;
561 /* NOTE: No response. */
562 error = hn_nvs_send(sc->hn_prichan, VMBUS_CHANPKT_FLAG_NONE,
563 &conf, sizeof(conf), &hn_send_ctx_none);
565 if_printf(sc->hn_ifp, "send nvs ndis conf failed: %d\n", error);
570 * Net VSC connect to VSP
573 hv_nv_connect_to_vsp(struct hn_softc *sc)
576 uint32_t protocol_list[] = { NVSP_PROTOCOL_VERSION_1,
577 NVSP_PROTOCOL_VERSION_2,
578 NVSP_PROTOCOL_VERSION_4,
579 NVSP_PROTOCOL_VERSION_5 };
581 int protocol_number = nitems(protocol_list);
583 device_t dev = sc->hn_dev;
584 struct ifnet *ifp = sc->arpcom.ac_ifp;
585 struct hn_nvs_ndis_init ndis;
587 net_dev = hv_nv_get_outbound_net_device(sc);
590 * Negotiate the NVSP version. Try the latest NVSP first.
592 for (i = protocol_number - 1; i >= 0; i--) {
593 if (hv_nv_negotiate_nvsp_protocol(sc, net_dev,
594 protocol_list[i]) == 0) {
595 net_dev->nvsp_version = protocol_list[i];
597 device_printf(dev, "Netvsc: got version 0x%x\n",
598 net_dev->nvsp_version);
605 device_printf(dev, "failed to negotiate a valid "
611 * Set the MTU if supported by this NVSP protocol version
612 * This needs to be right after the NVSP init message per Haiyang
614 if (net_dev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
615 ret = hv_nv_send_ndis_config(sc, ifp->if_mtu);
621 memset(&ndis, 0, sizeof(ndis));
622 ndis.nvs_type = HN_NVS_TYPE_NDIS_INIT;
623 ndis.nvs_ndis_major = NDIS_VERSION_MAJOR_6;
624 if (net_dev->nvsp_version <= NVSP_PROTOCOL_VERSION_4)
625 ndis.nvs_ndis_minor = NDIS_VERSION_MINOR_1;
627 ndis.nvs_ndis_minor = NDIS_VERSION_MINOR_30;
629 /* NOTE: No response. */
630 ret = hn_nvs_send(sc->hn_prichan, VMBUS_CHANPKT_FLAG_NONE,
631 &ndis, sizeof(ndis), &hn_send_ctx_none);
633 if_printf(sc->hn_ifp, "send nvs ndis init failed: %d\n", ret);
637 /* Post the big receive buffer to NetVSP */
638 if (net_dev->nvsp_version <= NVSP_PROTOCOL_VERSION_2)
639 net_dev->rx_buf_size = NETVSC_RECEIVE_BUFFER_SIZE_LEGACY;
641 net_dev->rx_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
642 net_dev->send_buf_size = NETVSC_SEND_BUFFER_SIZE;
644 ret = hv_nv_init_rx_buffer_with_net_vsp(sc);
646 ret = hv_nv_init_send_buffer_with_net_vsp(sc);
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 hv_nv_subchan_attach(struct vmbus_channel *chan, struct hn_rx_ring *rxr)
665 KASSERT(rxr->hn_rx_idx == vmbus_chan_subidx(chan),
666 ("chan%u subidx %u, rxr%d mismatch",
667 vmbus_chan_id(chan), vmbus_chan_subidx(chan), rxr->hn_rx_idx));
668 vmbus_chan_open(chan, NETVSC_DEVICE_RING_BUFFER_SIZE,
669 NETVSC_DEVICE_RING_BUFFER_SIZE, NULL, 0,
670 hv_nv_on_channel_callback, rxr);
674 * Net VSC on device add
676 * Callback when the device belonging to this driver is added
679 hv_nv_on_device_add(struct hn_softc *sc, void *additional_info,
680 struct hn_rx_ring *rxr)
682 struct vmbus_channel *chan = sc->hn_prichan;
686 net_dev = hv_nv_alloc_net_device(sc);
690 /* Initialize the NetVSC channel extension */
695 KASSERT(rxr->hn_rx_idx == vmbus_chan_subidx(chan),
696 ("chan%u subidx %u, rxr%d mismatch",
697 vmbus_chan_id(chan), vmbus_chan_subidx(chan), rxr->hn_rx_idx));
698 ret = vmbus_chan_open(chan,
699 NETVSC_DEVICE_RING_BUFFER_SIZE, NETVSC_DEVICE_RING_BUFFER_SIZE,
700 NULL, 0, hv_nv_on_channel_callback, rxr);
705 * Connect with the NetVsp
707 ret = hv_nv_connect_to_vsp(sc);
714 /* Now, we can close the channel safely */
715 vmbus_chan_close(chan);
719 * Free the packet buffers on the netvsc device packet queue.
720 * Release other resources.
722 free(net_dev, M_NETVSC);
728 * Net VSC on device remove
731 hv_nv_on_device_remove(struct hn_softc *sc, boolean_t destroy_channel)
733 netvsc_dev *net_dev = sc->net_dev;;
735 /* Stop outbound traffic ie sends and receives completions */
736 net_dev->destroy = TRUE;
738 hv_nv_disconnect_from_vsp(net_dev);
740 /* At this point, no one should be accessing net_dev except in here */
742 /* Now, we can close the channel safely */
744 vmbus_chan_close(sc->hn_prichan);
746 free(net_dev, M_NETVSC);
752 hn_nvs_sent_xact(struct hn_send_ctx *sndc,
753 struct netvsc_dev_ *net_dev __unused, struct vmbus_channel *chan __unused,
754 const void *data, int dlen)
757 vmbus_xact_wakeup(sndc->hn_cbarg, data, dlen);
761 hn_nvs_sent_none(struct hn_send_ctx *sndc __unused,
762 struct netvsc_dev_ *net_dev __unused, struct vmbus_channel *chan __unused,
763 const void *data __unused, int dlen __unused)
769 hn_chim_free(struct netvsc_dev_ *net_dev, uint32_t chim_idx)
774 idx = chim_idx / BITS_PER_LONG;
775 KASSERT(idx < net_dev->bitsmap_words,
776 ("invalid chimney index 0x%x", chim_idx));
778 mask = 1UL << (chim_idx % BITS_PER_LONG);
779 KASSERT(net_dev->send_section_bitsmap[idx] & mask,
780 ("index bitmap 0x%lx, chimney index %u, "
781 "bitmap idx %d, bitmask 0x%lx",
782 net_dev->send_section_bitsmap[idx], chim_idx, idx, mask));
784 atomic_clear_long(&net_dev->send_section_bitsmap[idx], mask);
788 * Net VSC on send completion
791 hv_nv_on_send_completion(netvsc_dev *net_dev, struct vmbus_channel *chan,
792 const struct vmbus_chanpkt_hdr *pkt)
794 struct hn_send_ctx *sndc;
796 sndc = (struct hn_send_ctx *)(uintptr_t)pkt->cph_xactid;
797 sndc->hn_cb(sndc, net_dev, chan, VMBUS_CHANPKT_CONST_DATA(pkt),
798 VMBUS_CHANPKT_DATALEN(pkt));
801 * 'sndc' CAN NOT be accessed anymore, since it can be freed by
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 vmbus_channel *chan, uint32_t rndis_mtype,
813 struct hn_send_ctx *sndc, struct vmbus_gpa *gpa, int gpa_cnt)
815 struct hn_nvs_rndis rndis;
818 rndis.nvs_type = HN_NVS_TYPE_RNDIS;
819 rndis.nvs_rndis_mtype = rndis_mtype;
820 rndis.nvs_chim_idx = sndc->hn_chim_idx;
821 rndis.nvs_chim_sz = sndc->hn_chim_sz;
824 ret = hn_nvs_send_sglist(chan, gpa, gpa_cnt,
825 &rndis, sizeof(rndis), sndc);
827 ret = hn_nvs_send(chan, VMBUS_CHANPKT_FLAG_RC,
828 &rndis, sizeof(rndis), sndc);
837 * In the FreeBSD Hyper-V virtual world, this function deals exclusively
838 * with virtual addresses.
841 hv_nv_on_receive(netvsc_dev *net_dev, struct hn_rx_ring *rxr,
842 struct vmbus_channel *chan, const struct vmbus_chanpkt_hdr *pkthdr)
844 const struct vmbus_chanpkt_rxbuf *pkt;
845 const struct hn_nvs_hdr *nvs_hdr;
849 /* Make sure that this is a RNDIS message. */
850 nvs_hdr = VMBUS_CHANPKT_CONST_DATA(pkthdr);
851 if (__predict_false(nvs_hdr->nvs_type != HN_NVS_TYPE_RNDIS)) {
852 if_printf(rxr->hn_ifp, "nvs type %u, not RNDIS\n",
857 pkt = (const struct vmbus_chanpkt_rxbuf *)pkthdr;
859 if (pkt->cp_rxbuf_id != NETVSC_RECEIVE_BUFFER_ID) {
860 if_printf(rxr->hn_ifp, "rxbuf_id %d is invalid!\n",
865 count = pkt->cp_rxbuf_cnt;
867 /* Each range represents 1 RNDIS pkt that contains 1 Ethernet frame */
868 for (i = 0; i < count; i++) {
869 hv_rf_on_receive(net_dev, rxr,
870 (const uint8_t *)net_dev->rx_buf + pkt->cp_rxbuf[i].rb_ofs,
871 pkt->cp_rxbuf[i].rb_len);
875 * Moved completion call back here so that all received
876 * messages (not just data messages) will trigger a response
877 * message back to the host.
879 hv_nv_on_receive_completion(chan, pkt->cp_hdr.cph_xactid);
883 * Net VSC on receive completion
885 * Send a receive completion packet to RNDIS device (ie NetVsp)
888 hv_nv_on_receive_completion(struct vmbus_channel *chan, uint64_t tid)
890 struct hn_nvs_rndis_ack ack;
894 ack.nvs_type = HN_NVS_TYPE_RNDIS_ACK;
895 ack.nvs_status = HN_NVS_STATUS_OK;
898 /* Send the completion */
899 ret = vmbus_chan_send(chan, VMBUS_CHANPKT_TYPE_COMP,
900 VMBUS_CHANPKT_FLAG_NONE, &ack, sizeof(ack), tid);
904 } else if (ret == EAGAIN) {
905 /* no more room... wait a bit and attempt to retry 3 times */
910 goto retry_send_cmplt;
916 hn_proc_notify(struct hn_softc *sc, const struct vmbus_chanpkt_hdr *pkt)
918 const struct hn_nvs_hdr *hdr;
920 hdr = VMBUS_CHANPKT_CONST_DATA(pkt);
921 if (hdr->nvs_type == HN_NVS_TYPE_TXTBL_NOTE) {
922 /* Useless; ignore */
925 if_printf(sc->hn_ifp, "got notify, nvs type %u\n", hdr->nvs_type);
929 * Net VSC on channel callback
932 hv_nv_on_channel_callback(struct vmbus_channel *chan, void *xrxr)
934 struct hn_rx_ring *rxr = xrxr;
935 struct hn_softc *sc = rxr->hn_ifp->if_softc;
938 int bufferlen = NETVSC_PACKET_SIZE;
940 net_dev = hv_nv_get_inbound_net_device(sc);
944 buffer = rxr->hn_rdbuf;
946 struct vmbus_chanpkt_hdr *pkt = buffer;
950 bytes_rxed = bufferlen;
951 ret = vmbus_chan_recv_pkt(chan, pkt, &bytes_rxed);
953 if (bytes_rxed > 0) {
954 switch (pkt->cph_type) {
955 case VMBUS_CHANPKT_TYPE_COMP:
956 hv_nv_on_send_completion(net_dev, chan,
959 case VMBUS_CHANPKT_TYPE_RXBUF:
960 hv_nv_on_receive(net_dev, rxr, chan, pkt);
962 case VMBUS_CHANPKT_TYPE_INBAND:
963 hn_proc_notify(sc, pkt);
966 if_printf(rxr->hn_ifp,
967 "unknown chan pkt %u\n",
972 } else if (ret == ENOBUFS) {
973 /* Handle large packet */
974 if (bufferlen > NETVSC_PACKET_SIZE) {
975 free(buffer, M_NETVSC);
979 /* alloc new buffer */
980 buffer = malloc(bytes_rxed, M_NETVSC, M_NOWAIT);
981 if (buffer == NULL) {
982 if_printf(rxr->hn_ifp,
983 "hv_cb malloc buffer failed, len=%u\n",
988 bufferlen = bytes_rxed;
990 /* No more packets */
995 if (bufferlen > NETVSC_PACKET_SIZE)
996 free(buffer, M_NETVSC);
998 hv_rf_channel_rollup(rxr, rxr->hn_txr);