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 <dev/hyperv/include/vmbus_xact.h>
49 #include <dev/hyperv/netvsc/hv_net_vsc.h>
50 #include <dev/hyperv/netvsc/hv_rndis.h>
51 #include <dev/hyperv/netvsc/hv_rndis_filter.h>
52 #include <dev/hyperv/netvsc/if_hnreg.h>
54 MALLOC_DEFINE(M_NETVSC, "netvsc", "Hyper-V netvsc driver");
57 * Forward declarations
59 static void hv_nv_on_channel_callback(struct vmbus_channel *chan,
61 static int hv_nv_init_send_buffer_with_net_vsp(struct hn_softc *sc);
62 static int hv_nv_init_rx_buffer_with_net_vsp(struct hn_softc *);
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 hn_softc *sc);
66 static void hv_nv_on_send_completion(netvsc_dev *net_dev,
67 struct vmbus_channel *, const struct vmbus_chanpkt_hdr *pkt);
68 static void hv_nv_on_receive_completion(struct vmbus_channel *chan,
69 uint64_t tid, uint32_t status);
70 static void hv_nv_on_receive(netvsc_dev *net_dev,
71 struct hn_rx_ring *rxr, struct vmbus_channel *chan,
72 const struct vmbus_chanpkt_hdr *pkt);
73 static void hn_nvs_sent_none(struct hn_send_ctx *sndc,
74 struct netvsc_dev_ *net_dev, struct vmbus_channel *chan,
75 const struct nvsp_msg_ *msg, int);
76 static void hn_nvs_sent_xact(struct hn_send_ctx *sndc,
77 struct netvsc_dev_ *net_dev, struct vmbus_channel *chan,
78 const struct nvsp_msg_ *msg, int dlen);
80 static struct hn_send_ctx hn_send_ctx_none =
81 HN_SEND_CTX_INITIALIZER(hn_nvs_sent_none, NULL);
86 static inline netvsc_dev *
87 hv_nv_alloc_net_device(struct hn_softc *sc)
91 net_dev = malloc(sizeof(netvsc_dev), M_NETVSC, M_WAITOK | M_ZERO);
94 net_dev->destroy = FALSE;
95 sc->net_dev = net_dev;
101 * XXX unnecessary; nuke it.
103 static inline netvsc_dev *
104 hv_nv_get_outbound_net_device(struct hn_softc *sc)
110 * XXX unnecessary; nuke it.
112 static inline netvsc_dev *
113 hv_nv_get_inbound_net_device(struct hn_softc *sc)
119 hv_nv_get_next_send_section(netvsc_dev *net_dev)
121 unsigned long bitsmap_words = net_dev->bitsmap_words;
122 unsigned long *bitsmap = net_dev->send_section_bitsmap;
124 int ret = NVSP_1_CHIMNEY_SEND_INVALID_SECTION_INDEX;
127 for (i = 0; i < bitsmap_words; i++) {
128 idx = ffsl(~bitsmap[i]);
133 KASSERT(i * BITS_PER_LONG + idx < net_dev->send_section_count,
134 ("invalid i %d and idx %lu", i, idx));
136 if (atomic_testandset_long(&bitsmap[i], idx))
139 ret = i * BITS_PER_LONG + idx;
147 * Net VSC initialize receive buffer with net VSP
149 * Net VSP: Network virtual services client, also known as the
150 * Hyper-V extensible switch and the synthetic data path.
153 hv_nv_init_rx_buffer_with_net_vsp(struct hn_softc *sc)
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 ret = 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 device_printf(sc->hn_dev, "rxbuf gpadl connect failed: %d\n",
189 /* sema_wait(&ext->channel_init_sema); KYS CHECK */
191 /* Notify the NetVsp of the gpadl handle */
192 init_pkt = &net_dev->channel_init_packet;
194 memset(init_pkt, 0, sizeof(nvsp_msg));
196 init_pkt->hdr.msg_type = nvsp_msg_1_type_send_rx_buf;
197 init_pkt->msgs.vers_1_msgs.send_rx_buf.gpadl_handle =
198 net_dev->rx_buf_gpadl_handle;
199 init_pkt->msgs.vers_1_msgs.send_rx_buf.id =
200 NETVSC_RECEIVE_BUFFER_ID;
202 /* Send the gpadl notification request */
204 hn_send_ctx_init_simple(&sndc, hn_nvs_sent_wakeup, NULL);
205 ret = vmbus_chan_send(sc->hn_prichan,
206 VMBUS_CHANPKT_TYPE_INBAND, VMBUS_CHANPKT_FLAG_RC,
207 init_pkt, sizeof(nvsp_msg), (uint64_t)(uintptr_t)&sndc);
212 sema_wait(&net_dev->channel_init_sema);
214 /* Check the response */
215 if (init_pkt->msgs.vers_1_msgs.send_rx_buf_complete.status
216 != nvsp_status_success) {
221 net_dev->rx_section_count =
222 init_pkt->msgs.vers_1_msgs.send_rx_buf_complete.num_sections;
224 net_dev->rx_sections = malloc(net_dev->rx_section_count *
225 sizeof(nvsp_1_rx_buf_section), M_NETVSC, M_WAITOK);
226 memcpy(net_dev->rx_sections,
227 init_pkt->msgs.vers_1_msgs.send_rx_buf_complete.sections,
228 net_dev->rx_section_count * sizeof(nvsp_1_rx_buf_section));
232 * For first release, there should only be 1 section that represents
233 * the entire receive buffer
235 if (net_dev->rx_section_count != 1
236 || net_dev->rx_sections->offset != 0) {
244 hv_nv_destroy_rx_buffer(net_dev);
251 * Net VSC initialize send buffer with net VSP
254 hv_nv_init_send_buffer_with_net_vsp(struct hn_softc *sc)
256 struct hn_send_ctx sndc;
261 net_dev = hv_nv_get_outbound_net_device(sc);
266 net_dev->send_buf = hyperv_dmamem_alloc(bus_get_dma_tag(sc->hn_dev),
267 PAGE_SIZE, 0, net_dev->send_buf_size, &net_dev->txbuf_dma,
268 BUS_DMA_WAITOK | BUS_DMA_ZERO);
269 if (net_dev->send_buf == NULL) {
270 device_printf(sc->hn_dev, "allocate chimney txbuf failed\n");
275 * Connect chimney sending buffer GPADL to the primary channel.
278 * Only primary channel has chimney sending buffer connected to it.
279 * Sub-channels just share this chimney sending buffer.
281 ret = vmbus_chan_gpadl_connect(sc->hn_prichan,
282 net_dev->txbuf_dma.hv_paddr, net_dev->send_buf_size,
283 &net_dev->send_buf_gpadl_handle);
285 device_printf(sc->hn_dev, "chimney sending buffer gpadl "
286 "connect failed: %d\n", ret);
290 /* Notify the NetVsp of the gpadl handle */
292 init_pkt = &net_dev->channel_init_packet;
294 memset(init_pkt, 0, sizeof(nvsp_msg));
296 init_pkt->hdr.msg_type = nvsp_msg_1_type_send_send_buf;
297 init_pkt->msgs.vers_1_msgs.send_rx_buf.gpadl_handle =
298 net_dev->send_buf_gpadl_handle;
299 init_pkt->msgs.vers_1_msgs.send_rx_buf.id =
300 NETVSC_SEND_BUFFER_ID;
302 /* Send the gpadl notification request */
304 hn_send_ctx_init_simple(&sndc, hn_nvs_sent_wakeup, NULL);
305 ret = vmbus_chan_send(sc->hn_prichan,
306 VMBUS_CHANPKT_TYPE_INBAND, VMBUS_CHANPKT_FLAG_RC,
307 init_pkt, sizeof(nvsp_msg), (uint64_t)(uintptr_t)&sndc);
312 sema_wait(&net_dev->channel_init_sema);
314 /* Check the response */
315 if (init_pkt->msgs.vers_1_msgs.send_send_buf_complete.status
316 != nvsp_status_success) {
321 net_dev->send_section_size =
322 init_pkt->msgs.vers_1_msgs.send_send_buf_complete.section_size;
323 net_dev->send_section_count =
324 net_dev->send_buf_size / net_dev->send_section_size;
325 net_dev->bitsmap_words = howmany(net_dev->send_section_count,
327 net_dev->send_section_bitsmap =
328 malloc(net_dev->bitsmap_words * sizeof(long), M_NETVSC,
334 hv_nv_destroy_send_buffer(net_dev);
341 * Net VSC destroy receive buffer
344 hv_nv_destroy_rx_buffer(netvsc_dev *net_dev)
346 nvsp_msg *revoke_pkt;
350 * If we got a section count, it means we received a
351 * send_rx_buf_complete msg
352 * (ie sent nvsp_msg_1_type_send_rx_buf msg) therefore,
353 * we need to send a revoke msg here
355 if (net_dev->rx_section_count) {
356 /* Send the revoke receive buffer */
357 revoke_pkt = &net_dev->revoke_packet;
358 memset(revoke_pkt, 0, sizeof(nvsp_msg));
360 revoke_pkt->hdr.msg_type = nvsp_msg_1_type_revoke_rx_buf;
361 revoke_pkt->msgs.vers_1_msgs.revoke_rx_buf.id =
362 NETVSC_RECEIVE_BUFFER_ID;
364 ret = vmbus_chan_send(net_dev->sc->hn_prichan,
365 VMBUS_CHANPKT_TYPE_INBAND, 0, revoke_pkt, sizeof(nvsp_msg),
366 (uint64_t)(uintptr_t)&hn_send_ctx_none);
368 * If we failed here, we might as well return and have a leak
369 * rather than continue and a bugchk
376 /* Tear down the gpadl on the vsp end */
377 if (net_dev->rx_buf_gpadl_handle) {
378 ret = vmbus_chan_gpadl_disconnect(net_dev->sc->hn_prichan,
379 net_dev->rx_buf_gpadl_handle);
381 * If we failed here, we might as well return and have a leak
382 * rather than continue and a bugchk
387 net_dev->rx_buf_gpadl_handle = 0;
390 if (net_dev->rx_buf) {
391 /* Free up the receive buffer */
392 hyperv_dmamem_free(&net_dev->rxbuf_dma, net_dev->rx_buf);
393 net_dev->rx_buf = NULL;
396 if (net_dev->rx_sections) {
397 free(net_dev->rx_sections, M_NETVSC);
398 net_dev->rx_sections = NULL;
399 net_dev->rx_section_count = 0;
406 * Net VSC destroy send buffer
409 hv_nv_destroy_send_buffer(netvsc_dev *net_dev)
411 nvsp_msg *revoke_pkt;
415 * If we got a section count, it means we received a
416 * send_rx_buf_complete msg
417 * (ie sent nvsp_msg_1_type_send_rx_buf msg) therefore,
418 * we need to send a revoke msg here
420 if (net_dev->send_section_size) {
421 /* Send the revoke send buffer */
422 revoke_pkt = &net_dev->revoke_packet;
423 memset(revoke_pkt, 0, sizeof(nvsp_msg));
425 revoke_pkt->hdr.msg_type =
426 nvsp_msg_1_type_revoke_send_buf;
427 revoke_pkt->msgs.vers_1_msgs.revoke_send_buf.id =
428 NETVSC_SEND_BUFFER_ID;
430 ret = vmbus_chan_send(net_dev->sc->hn_prichan,
431 VMBUS_CHANPKT_TYPE_INBAND, 0, revoke_pkt, sizeof(nvsp_msg),
432 (uint64_t)(uintptr_t)&hn_send_ctx_none);
434 * If we failed here, we might as well return and have a leak
435 * rather than continue and a bugchk
442 /* Tear down the gpadl on the vsp end */
443 if (net_dev->send_buf_gpadl_handle) {
444 ret = vmbus_chan_gpadl_disconnect(net_dev->sc->hn_prichan,
445 net_dev->send_buf_gpadl_handle);
448 * If we failed here, we might as well return and have a leak
449 * rather than continue and a bugchk
454 net_dev->send_buf_gpadl_handle = 0;
457 if (net_dev->send_buf) {
458 /* Free up the receive buffer */
459 hyperv_dmamem_free(&net_dev->txbuf_dma, net_dev->send_buf);
460 net_dev->send_buf = NULL;
463 if (net_dev->send_section_bitsmap) {
464 free(net_dev->send_section_bitsmap, M_NETVSC);
471 hv_nv_negotiate_nvsp_protocol(struct hn_softc *sc, netvsc_dev *net_dev,
474 struct hn_send_ctx sndc;
475 struct vmbus_xact *xact;
476 struct hn_nvs_init *init;
477 const struct hn_nvs_init_resp *resp;
482 xact = vmbus_xact_get(sc->hn_xact, sizeof(*init));
484 if_printf(sc->hn_ifp, "no xact for nvs init\n");
488 init = vmbus_xact_req_data(xact);
489 init->nvs_type = HN_NVS_TYPE_INIT;
490 init->nvs_ver_min = nvs_ver;
491 init->nvs_ver_max = nvs_ver;
493 vmbus_xact_activate(xact);
494 hn_send_ctx_init_simple(&sndc, hn_nvs_sent_xact, xact);
496 error = vmbus_chan_send(sc->hn_prichan,
497 VMBUS_CHANPKT_TYPE_INBAND, VMBUS_CHANPKT_FLAG_RC,
498 init, sizeof(*init), (uint64_t)(uintptr_t)&sndc);
500 if_printf(sc->hn_ifp, "send nvs init failed: %d\n", error);
501 vmbus_xact_deactivate(xact);
502 vmbus_xact_put(xact);
506 resp = vmbus_xact_wait(xact, &resp_len);
507 if (resp_len < sizeof(*resp)) {
508 if_printf(sc->hn_ifp, "invalid init resp length %zu\n",
510 vmbus_xact_put(xact);
513 if (resp->nvs_type != HN_NVS_TYPE_INIT_RESP) {
514 if_printf(sc->hn_ifp, "not init resp, type %u\n",
516 vmbus_xact_put(xact);
520 status = resp->nvs_status;
521 vmbus_xact_put(xact);
523 if (status != HN_NVS_STATUS_OK) {
524 if_printf(sc->hn_ifp, "nvs init failed for ver 0x%x\n",
532 * Send NDIS version 2 config packet containing MTU.
534 * Not valid for NDIS version 1.
537 hv_nv_send_ndis_config(struct hn_softc *sc, uint32_t mtu)
539 struct hn_nvs_ndis_conf conf;
542 memset(&conf, 0, sizeof(conf));
543 conf.nvs_type = HN_NVS_TYPE_NDIS_CONF;
545 conf.nvs_caps = HN_NVS_NDIS_CONF_VLAN;
547 error = vmbus_chan_send(sc->hn_prichan, VMBUS_CHANPKT_TYPE_INBAND, 0,
548 &conf, sizeof(conf), (uint64_t)(uintptr_t)&hn_send_ctx_none);
550 if_printf(sc->hn_ifp, "send nvs ndis conf failed: %d\n", error);
555 * Net VSC connect to VSP
558 hv_nv_connect_to_vsp(struct hn_softc *sc)
561 uint32_t protocol_list[] = { NVSP_PROTOCOL_VERSION_1,
562 NVSP_PROTOCOL_VERSION_2,
563 NVSP_PROTOCOL_VERSION_4,
564 NVSP_PROTOCOL_VERSION_5 };
566 int protocol_number = nitems(protocol_list);
568 device_t dev = sc->hn_dev;
569 struct ifnet *ifp = sc->hn_ifp;
570 struct hn_nvs_ndis_init ndis;
572 net_dev = hv_nv_get_outbound_net_device(sc);
575 * Negotiate the NVSP version. Try the latest NVSP first.
577 for (i = protocol_number - 1; i >= 0; i--) {
578 if (hv_nv_negotiate_nvsp_protocol(sc, net_dev,
579 protocol_list[i]) == 0) {
580 net_dev->nvsp_version = protocol_list[i];
582 device_printf(dev, "Netvsc: got version 0x%x\n",
583 net_dev->nvsp_version);
590 device_printf(dev, "failed to negotiate a valid "
596 * Set the MTU if supported by this NVSP protocol version
597 * This needs to be right after the NVSP init message per Haiyang
599 if (net_dev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
600 ret = hv_nv_send_ndis_config(sc, ifp->if_mtu);
606 memset(&ndis, 0, sizeof(ndis));
607 ndis.nvs_type = HN_NVS_TYPE_NDIS_INIT;
608 ndis.nvs_ndis_major = NDIS_VERSION_MAJOR_6;
609 if (net_dev->nvsp_version <= NVSP_PROTOCOL_VERSION_4)
610 ndis.nvs_ndis_minor = NDIS_VERSION_MINOR_1;
612 ndis.nvs_ndis_minor = NDIS_VERSION_MINOR_30;
614 ret = vmbus_chan_send(sc->hn_prichan, VMBUS_CHANPKT_TYPE_INBAND, 0,
615 &ndis, sizeof(ndis), (uint64_t)(uintptr_t)&hn_send_ctx_none);
617 if_printf(sc->hn_ifp, "send nvs ndis init failed: %d\n", ret);
621 /* Post the big receive buffer to NetVSP */
622 if (net_dev->nvsp_version <= NVSP_PROTOCOL_VERSION_2)
623 net_dev->rx_buf_size = NETVSC_RECEIVE_BUFFER_SIZE_LEGACY;
625 net_dev->rx_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
626 net_dev->send_buf_size = NETVSC_SEND_BUFFER_SIZE;
628 ret = hv_nv_init_rx_buffer_with_net_vsp(sc);
630 ret = hv_nv_init_send_buffer_with_net_vsp(sc);
637 * Net VSC disconnect from VSP
640 hv_nv_disconnect_from_vsp(netvsc_dev *net_dev)
642 hv_nv_destroy_rx_buffer(net_dev);
643 hv_nv_destroy_send_buffer(net_dev);
647 hv_nv_subchan_attach(struct vmbus_channel *chan, struct hn_rx_ring *rxr)
649 KASSERT(rxr->hn_rx_idx == vmbus_chan_subidx(chan),
650 ("chan%u subidx %u, rxr%d mismatch",
651 vmbus_chan_id(chan), vmbus_chan_subidx(chan), rxr->hn_rx_idx));
652 vmbus_chan_open(chan, NETVSC_DEVICE_RING_BUFFER_SIZE,
653 NETVSC_DEVICE_RING_BUFFER_SIZE, NULL, 0,
654 hv_nv_on_channel_callback, rxr);
658 * Net VSC on device add
660 * Callback when the device belonging to this driver is added
663 hv_nv_on_device_add(struct hn_softc *sc, void *additional_info,
664 struct hn_rx_ring *rxr)
666 struct vmbus_channel *chan = sc->hn_prichan;
670 net_dev = hv_nv_alloc_net_device(sc);
674 /* Initialize the NetVSC channel extension */
676 sema_init(&net_dev->channel_init_sema, 0, "netdev_sema");
681 KASSERT(rxr->hn_rx_idx == vmbus_chan_subidx(chan),
682 ("chan%u subidx %u, rxr%d mismatch",
683 vmbus_chan_id(chan), vmbus_chan_subidx(chan), rxr->hn_rx_idx));
684 ret = vmbus_chan_open(chan,
685 NETVSC_DEVICE_RING_BUFFER_SIZE, NETVSC_DEVICE_RING_BUFFER_SIZE,
686 NULL, 0, hv_nv_on_channel_callback, rxr);
691 * Connect with the NetVsp
693 ret = hv_nv_connect_to_vsp(sc);
700 /* Now, we can close the channel safely */
701 vmbus_chan_close(chan);
705 * Free the packet buffers on the netvsc device packet queue.
706 * Release other resources.
708 sema_destroy(&net_dev->channel_init_sema);
709 free(net_dev, M_NETVSC);
715 * Net VSC on device remove
718 hv_nv_on_device_remove(struct hn_softc *sc, boolean_t destroy_channel)
720 netvsc_dev *net_dev = sc->net_dev;;
722 /* Stop outbound traffic ie sends and receives completions */
723 net_dev->destroy = TRUE;
725 hv_nv_disconnect_from_vsp(net_dev);
727 /* At this point, no one should be accessing net_dev except in here */
729 /* Now, we can close the channel safely */
731 vmbus_chan_close(sc->hn_prichan);
733 sema_destroy(&net_dev->channel_init_sema);
734 free(net_dev, M_NETVSC);
740 hn_nvs_sent_wakeup(struct hn_send_ctx *sndc __unused,
741 struct netvsc_dev_ *net_dev, struct vmbus_channel *chan __unused,
742 const struct nvsp_msg_ *msg, int dlen __unused)
744 /* Copy the response back */
745 memcpy(&net_dev->channel_init_packet, msg, sizeof(nvsp_msg));
746 sema_post(&net_dev->channel_init_sema);
750 hn_nvs_sent_xact(struct hn_send_ctx *sndc,
751 struct netvsc_dev_ *net_dev __unused, struct vmbus_channel *chan __unused,
752 const struct nvsp_msg_ *msg, int dlen)
755 vmbus_xact_wakeup(sndc->hn_cbarg, msg, dlen);
759 hn_nvs_sent_none(struct hn_send_ctx *sndc __unused,
760 struct netvsc_dev_ *net_dev __unused, struct vmbus_channel *chan __unused,
761 const struct nvsp_msg_ *msg __unused, int dlen __unused)
767 hn_chim_free(struct netvsc_dev_ *net_dev, uint32_t chim_idx)
772 idx = chim_idx / BITS_PER_LONG;
773 KASSERT(idx < net_dev->bitsmap_words,
774 ("invalid chimney index 0x%x", chim_idx));
776 mask = 1UL << (chim_idx % BITS_PER_LONG);
777 KASSERT(net_dev->send_section_bitsmap[idx] & mask,
778 ("index bitmap 0x%lx, chimney index %u, "
779 "bitmap idx %d, bitmask 0x%lx",
780 net_dev->send_section_bitsmap[idx], chim_idx, idx, mask));
782 atomic_clear_long(&net_dev->send_section_bitsmap[idx], mask);
786 * Net VSC on send completion
789 hv_nv_on_send_completion(netvsc_dev *net_dev, struct vmbus_channel *chan,
790 const struct vmbus_chanpkt_hdr *pkt)
792 struct hn_send_ctx *sndc;
794 sndc = (struct hn_send_ctx *)(uintptr_t)pkt->cph_xactid;
795 sndc->hn_cb(sndc, net_dev, chan, VMBUS_CHANPKT_CONST_DATA(pkt),
796 VMBUS_CHANPKT_DATALEN(pkt));
799 * 'sndc' CAN NOT be accessed anymore, since it can be freed by
806 * Sends a packet on the specified Hyper-V device.
807 * Returns 0 on success, non-zero on failure.
810 hv_nv_on_send(struct vmbus_channel *chan, bool is_data_pkt,
811 struct hn_send_ctx *sndc, struct vmbus_gpa *gpa, int gpa_cnt)
816 send_msg.hdr.msg_type = nvsp_msg_1_type_send_rndis_pkt;
819 send_msg.msgs.vers_1_msgs.send_rndis_pkt.chan_type = 0;
821 /* 1 is RMC_CONTROL */
822 send_msg.msgs.vers_1_msgs.send_rndis_pkt.chan_type = 1;
825 send_msg.msgs.vers_1_msgs.send_rndis_pkt.send_buf_section_idx =
827 send_msg.msgs.vers_1_msgs.send_rndis_pkt.send_buf_section_size =
831 ret = vmbus_chan_send_sglist(chan, gpa, gpa_cnt,
832 &send_msg, sizeof(nvsp_msg), (uint64_t)(uintptr_t)sndc);
834 ret = vmbus_chan_send(chan,
835 VMBUS_CHANPKT_TYPE_INBAND, VMBUS_CHANPKT_FLAG_RC,
836 &send_msg, sizeof(nvsp_msg), (uint64_t)(uintptr_t)sndc);
845 * In the FreeBSD Hyper-V virtual world, this function deals exclusively
846 * with virtual addresses.
849 hv_nv_on_receive(netvsc_dev *net_dev, struct hn_rx_ring *rxr,
850 struct vmbus_channel *chan, const struct vmbus_chanpkt_hdr *pkthdr)
852 const struct vmbus_chanpkt_rxbuf *pkt;
853 const nvsp_msg *nvsp_msg_pkt;
854 netvsc_packet vsc_pkt;
855 netvsc_packet *net_vsc_pkt = &vsc_pkt;
858 int status = nvsp_status_success;
860 nvsp_msg_pkt = VMBUS_CHANPKT_CONST_DATA(pkthdr);
862 /* Make sure this is a valid nvsp packet */
863 if (nvsp_msg_pkt->hdr.msg_type != nvsp_msg_1_type_send_rndis_pkt) {
864 if_printf(rxr->hn_ifp, "packet hdr type %u is invalid!\n",
865 nvsp_msg_pkt->hdr.msg_type);
869 pkt = (const struct vmbus_chanpkt_rxbuf *)pkthdr;
871 if (pkt->cp_rxbuf_id != NETVSC_RECEIVE_BUFFER_ID) {
872 if_printf(rxr->hn_ifp, "rxbuf_id %d is invalid!\n",
877 count = pkt->cp_rxbuf_cnt;
879 /* Each range represents 1 RNDIS pkt that contains 1 Ethernet frame */
880 for (i = 0; i < count; i++) {
881 net_vsc_pkt->status = nvsp_status_success;
882 net_vsc_pkt->data = ((uint8_t *)net_dev->rx_buf +
883 pkt->cp_rxbuf[i].rb_ofs);
884 net_vsc_pkt->tot_data_buf_len = pkt->cp_rxbuf[i].rb_len;
886 hv_rf_on_receive(net_dev, rxr, net_vsc_pkt);
887 if (net_vsc_pkt->status != nvsp_status_success) {
888 status = nvsp_status_failure;
893 * Moved completion call back here so that all received
894 * messages (not just data messages) will trigger a response
895 * message back to the host.
897 hv_nv_on_receive_completion(chan, pkt->cp_hdr.cph_xactid, status);
901 * Net VSC on receive completion
903 * Send a receive completion packet to RNDIS device (ie NetVsp)
906 hv_nv_on_receive_completion(struct vmbus_channel *chan, uint64_t tid,
909 nvsp_msg rx_comp_msg;
913 rx_comp_msg.hdr.msg_type = nvsp_msg_1_type_send_rndis_pkt_complete;
915 /* Pass in the status */
916 rx_comp_msg.msgs.vers_1_msgs.send_rndis_pkt_complete.status =
920 /* Send the completion */
921 ret = vmbus_chan_send(chan, VMBUS_CHANPKT_TYPE_COMP, 0,
922 &rx_comp_msg, sizeof(nvsp_msg), tid);
926 } else if (ret == EAGAIN) {
927 /* no more room... wait a bit and attempt to retry 3 times */
932 goto retry_send_cmplt;
938 * Net VSC receiving vRSS send table from VSP
941 hv_nv_send_table(struct hn_softc *sc, const struct vmbus_chanpkt_hdr *pkt)
944 const nvsp_msg *nvsp_msg_pkt;
947 const uint32_t *table;
949 net_dev = hv_nv_get_inbound_net_device(sc);
953 nvsp_msg_pkt = VMBUS_CHANPKT_CONST_DATA(pkt);
955 if (nvsp_msg_pkt->hdr.msg_type !=
956 nvsp_msg5_type_send_indirection_table) {
957 printf("Netvsc: !Warning! receive msg type not "
958 "send_indirection_table. type = %d\n",
959 nvsp_msg_pkt->hdr.msg_type);
963 count = nvsp_msg_pkt->msgs.vers_5_msgs.send_table.count;
964 if (count != VRSS_SEND_TABLE_SIZE) {
965 printf("Netvsc: Received wrong send table size: %u\n", count);
969 table = (const uint32_t *)
970 ((const uint8_t *)&nvsp_msg_pkt->msgs.vers_5_msgs.send_table +
971 nvsp_msg_pkt->msgs.vers_5_msgs.send_table.offset);
973 for (i = 0; i < count; i++)
974 net_dev->vrss_send_table[i] = table[i];
978 * Net VSC on channel callback
981 hv_nv_on_channel_callback(struct vmbus_channel *chan, void *xrxr)
983 struct hn_rx_ring *rxr = xrxr;
984 struct hn_softc *sc = rxr->hn_ifp->if_softc;
987 int bufferlen = NETVSC_PACKET_SIZE;
989 net_dev = hv_nv_get_inbound_net_device(sc);
993 buffer = rxr->hn_rdbuf;
995 struct vmbus_chanpkt_hdr *pkt = buffer;
999 bytes_rxed = bufferlen;
1000 ret = vmbus_chan_recv_pkt(chan, pkt, &bytes_rxed);
1002 if (bytes_rxed > 0) {
1003 switch (pkt->cph_type) {
1004 case VMBUS_CHANPKT_TYPE_COMP:
1005 hv_nv_on_send_completion(net_dev, chan,
1008 case VMBUS_CHANPKT_TYPE_RXBUF:
1009 hv_nv_on_receive(net_dev, rxr, chan, pkt);
1011 case VMBUS_CHANPKT_TYPE_INBAND:
1012 hv_nv_send_table(sc, pkt);
1015 if_printf(rxr->hn_ifp,
1016 "unknown chan pkt %u\n",
1021 } else if (ret == ENOBUFS) {
1022 /* Handle large packet */
1023 if (bufferlen > NETVSC_PACKET_SIZE) {
1024 free(buffer, M_NETVSC);
1028 /* alloc new buffer */
1029 buffer = malloc(bytes_rxed, M_NETVSC, M_NOWAIT);
1030 if (buffer == NULL) {
1031 if_printf(rxr->hn_ifp,
1032 "hv_cb malloc buffer failed, len=%u\n",
1037 bufferlen = bytes_rxed;
1039 /* No more packets */
1044 if (bufferlen > NETVSC_PACKET_SIZE)
1045 free(buffer, M_NETVSC);
1047 hv_rf_channel_rollup(rxr, rxr->hn_txr);