2 * Copyright (c) 2009-2012,2016-2017 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.
30 * Network Virtualization Service.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include "opt_inet6.h"
39 #include <sys/param.h>
40 #include <sys/kernel.h>
41 #include <sys/limits.h>
42 #include <sys/socket.h>
43 #include <sys/systm.h>
44 #include <sys/taskqueue.h>
46 #include <net/ethernet.h>
48 #include <net/if_arp.h>
49 #include <net/if_media.h>
51 #include <netinet/in.h>
52 #include <netinet/tcp_lro.h>
54 #include <dev/hyperv/include/hyperv.h>
55 #include <dev/hyperv/include/hyperv_busdma.h>
56 #include <dev/hyperv/include/vmbus.h>
57 #include <dev/hyperv/include/vmbus_xact.h>
59 #include <dev/hyperv/netvsc/ndis.h>
60 #include <dev/hyperv/netvsc/if_hnreg.h>
61 #include <dev/hyperv/netvsc/if_hnvar.h>
62 #include <dev/hyperv/netvsc/hn_nvs.h>
64 static int hn_nvs_conn_chim(struct hn_softc *);
65 static int hn_nvs_conn_rxbuf(struct hn_softc *);
66 static void hn_nvs_disconn_chim(struct hn_softc *);
67 static void hn_nvs_disconn_rxbuf(struct hn_softc *);
68 static int hn_nvs_conf_ndis(struct hn_softc *, int);
69 static int hn_nvs_init_ndis(struct hn_softc *);
70 static int hn_nvs_doinit(struct hn_softc *, uint32_t);
71 static int hn_nvs_init(struct hn_softc *);
72 static const void *hn_nvs_xact_execute(struct hn_softc *,
73 struct vmbus_xact *, void *, int,
75 static void hn_nvs_sent_none(struct hn_nvs_sendctx *,
76 struct hn_softc *, struct vmbus_channel *,
79 struct hn_nvs_sendctx hn_nvs_sendctx_none =
80 HN_NVS_SENDCTX_INITIALIZER(hn_nvs_sent_none, NULL);
82 static const uint32_t hn_nvs_version[] = {
90 hn_nvs_xact_execute(struct hn_softc *sc, struct vmbus_xact *xact,
91 void *req, int reqlen, size_t *resplen0, uint32_t type)
93 struct hn_nvs_sendctx sndc;
94 size_t resplen, min_resplen = *resplen0;
95 const struct hn_nvs_hdr *hdr;
98 KASSERT(min_resplen >= sizeof(*hdr),
99 ("invalid minimum response len %zu", min_resplen));
102 * Execute the xact setup by the caller.
104 hn_nvs_sendctx_init(&sndc, hn_nvs_sent_xact, xact);
106 vmbus_xact_activate(xact);
107 error = hn_nvs_send(sc->hn_prichan, VMBUS_CHANPKT_FLAG_RC,
110 vmbus_xact_deactivate(xact);
113 hdr = vmbus_chan_xact_wait(sc->hn_prichan, xact, &resplen,
117 * Check this NVS response message.
119 if (resplen < min_resplen) {
120 if_printf(sc->hn_ifp, "invalid NVS resp len %zu\n", resplen);
123 if (hdr->nvs_type != type) {
124 if_printf(sc->hn_ifp, "unexpected NVS resp 0x%08x, "
125 "expect 0x%08x\n", hdr->nvs_type, type);
134 hn_nvs_req_send(struct hn_softc *sc, void *req, int reqlen)
137 return (hn_nvs_send(sc->hn_prichan, VMBUS_CHANPKT_FLAG_NONE,
138 req, reqlen, &hn_nvs_sendctx_none));
142 hn_nvs_conn_rxbuf(struct hn_softc *sc)
144 struct vmbus_xact *xact = NULL;
145 struct hn_nvs_rxbuf_conn *conn;
146 const struct hn_nvs_rxbuf_connresp *resp;
149 int error, rxbuf_size;
152 * Limit RXBUF size for old NVS.
154 if (sc->hn_nvs_ver <= HN_NVS_VERSION_2)
155 rxbuf_size = HN_RXBUF_SIZE_COMPAT;
157 rxbuf_size = HN_RXBUF_SIZE;
160 * Connect the RXBUF GPADL to the primary channel.
163 * Only primary channel has RXBUF connected to it. Sub-channels
164 * just share this RXBUF.
166 error = vmbus_chan_gpadl_connect(sc->hn_prichan,
167 sc->hn_rxbuf_dma.hv_paddr, rxbuf_size, &sc->hn_rxbuf_gpadl);
169 if_printf(sc->hn_ifp, "rxbuf gpadl conn failed: %d\n",
175 * Connect RXBUF to NVS.
178 xact = vmbus_xact_get(sc->hn_xact, sizeof(*conn));
180 if_printf(sc->hn_ifp, "no xact for nvs rxbuf conn\n");
184 conn = vmbus_xact_req_data(xact);
185 conn->nvs_type = HN_NVS_TYPE_RXBUF_CONN;
186 conn->nvs_gpadl = sc->hn_rxbuf_gpadl;
187 conn->nvs_sig = HN_NVS_RXBUF_SIG;
189 resp_len = sizeof(*resp);
190 resp = hn_nvs_xact_execute(sc, xact, conn, sizeof(*conn), &resp_len,
191 HN_NVS_TYPE_RXBUF_CONNRESP);
193 if_printf(sc->hn_ifp, "exec nvs rxbuf conn failed\n");
198 status = resp->nvs_status;
199 vmbus_xact_put(xact);
202 if (status != HN_NVS_STATUS_OK) {
203 if_printf(sc->hn_ifp, "nvs rxbuf conn failed: %x\n", status);
207 sc->hn_flags |= HN_FLAG_RXBUF_CONNECTED;
213 vmbus_xact_put(xact);
214 hn_nvs_disconn_rxbuf(sc);
219 hn_nvs_conn_chim(struct hn_softc *sc)
221 struct vmbus_xact *xact = NULL;
222 struct hn_nvs_chim_conn *chim;
223 const struct hn_nvs_chim_connresp *resp;
225 uint32_t status, sectsz;
229 * Connect chimney sending buffer GPADL to the primary channel.
232 * Only primary channel has chimney sending buffer connected to it.
233 * Sub-channels just share this chimney sending buffer.
235 error = vmbus_chan_gpadl_connect(sc->hn_prichan,
236 sc->hn_chim_dma.hv_paddr, HN_CHIM_SIZE, &sc->hn_chim_gpadl);
238 if_printf(sc->hn_ifp, "chim gpadl conn failed: %d\n", error);
243 * Connect chimney sending buffer to NVS
246 xact = vmbus_xact_get(sc->hn_xact, sizeof(*chim));
248 if_printf(sc->hn_ifp, "no xact for nvs chim conn\n");
252 chim = vmbus_xact_req_data(xact);
253 chim->nvs_type = HN_NVS_TYPE_CHIM_CONN;
254 chim->nvs_gpadl = sc->hn_chim_gpadl;
255 chim->nvs_sig = HN_NVS_CHIM_SIG;
257 resp_len = sizeof(*resp);
258 resp = hn_nvs_xact_execute(sc, xact, chim, sizeof(*chim), &resp_len,
259 HN_NVS_TYPE_CHIM_CONNRESP);
261 if_printf(sc->hn_ifp, "exec nvs chim conn failed\n");
266 status = resp->nvs_status;
267 sectsz = resp->nvs_sectsz;
268 vmbus_xact_put(xact);
271 if (status != HN_NVS_STATUS_OK) {
272 if_printf(sc->hn_ifp, "nvs chim conn failed: %x\n", status);
276 if (sectsz == 0 || sectsz % sizeof(uint32_t) != 0) {
278 * Can't use chimney sending buffer; done!
281 if_printf(sc->hn_ifp, "zero chimney sending buffer "
284 if_printf(sc->hn_ifp, "misaligned chimney sending "
285 "buffers, section size: %u\n", sectsz);
287 sc->hn_chim_szmax = 0;
289 sc->hn_flags |= HN_FLAG_CHIM_CONNECTED;
293 sc->hn_chim_szmax = sectsz;
294 sc->hn_chim_cnt = HN_CHIM_SIZE / sc->hn_chim_szmax;
295 if (HN_CHIM_SIZE % sc->hn_chim_szmax != 0) {
296 if_printf(sc->hn_ifp, "chimney sending sections are "
297 "not properly aligned\n");
299 if (sc->hn_chim_cnt % LONG_BIT != 0) {
300 if_printf(sc->hn_ifp, "discard %d chimney sending sections\n",
301 sc->hn_chim_cnt % LONG_BIT);
304 sc->hn_chim_bmap_cnt = sc->hn_chim_cnt / LONG_BIT;
305 sc->hn_chim_bmap = malloc(sc->hn_chim_bmap_cnt * sizeof(u_long),
306 M_DEVBUF, M_WAITOK | M_ZERO);
309 sc->hn_flags |= HN_FLAG_CHIM_CONNECTED;
311 if_printf(sc->hn_ifp, "chimney sending buffer %d/%d\n",
312 sc->hn_chim_szmax, sc->hn_chim_cnt);
318 vmbus_xact_put(xact);
319 hn_nvs_disconn_chim(sc);
324 hn_nvs_disconn_rxbuf(struct hn_softc *sc)
328 if (sc->hn_flags & HN_FLAG_RXBUF_CONNECTED) {
329 struct hn_nvs_rxbuf_disconn disconn;
332 * Disconnect RXBUF from NVS.
334 memset(&disconn, 0, sizeof(disconn));
335 disconn.nvs_type = HN_NVS_TYPE_RXBUF_DISCONN;
336 disconn.nvs_sig = HN_NVS_RXBUF_SIG;
338 /* NOTE: No response. */
339 error = hn_nvs_req_send(sc, &disconn, sizeof(disconn));
341 if_printf(sc->hn_ifp,
342 "send nvs rxbuf disconn failed: %d\n", error);
344 * Fine for a revoked channel, since the hypervisor
345 * does not drain TX bufring for a revoked channel.
347 if (!vmbus_chan_is_revoked(sc->hn_prichan))
348 sc->hn_flags |= HN_FLAG_RXBUF_REF;
350 sc->hn_flags &= ~HN_FLAG_RXBUF_CONNECTED;
353 * Wait for the hypervisor to receive this NVS request.
356 * The TX bufring will not be drained by the hypervisor,
357 * if the primary channel is revoked.
359 while (!vmbus_chan_tx_empty(sc->hn_prichan) &&
360 !vmbus_chan_is_revoked(sc->hn_prichan))
363 * Linger long enough for NVS to disconnect RXBUF.
365 pause("lingtx", (200 * hz) / 1000);
368 if (sc->hn_rxbuf_gpadl != 0) {
370 * Disconnect RXBUF from primary channel.
372 error = vmbus_chan_gpadl_disconnect(sc->hn_prichan,
375 if_printf(sc->hn_ifp,
376 "rxbuf gpadl disconn failed: %d\n", error);
377 sc->hn_flags |= HN_FLAG_RXBUF_REF;
379 sc->hn_rxbuf_gpadl = 0;
384 hn_nvs_disconn_chim(struct hn_softc *sc)
388 if (sc->hn_flags & HN_FLAG_CHIM_CONNECTED) {
389 struct hn_nvs_chim_disconn disconn;
392 * Disconnect chimney sending buffer from NVS.
394 memset(&disconn, 0, sizeof(disconn));
395 disconn.nvs_type = HN_NVS_TYPE_CHIM_DISCONN;
396 disconn.nvs_sig = HN_NVS_CHIM_SIG;
398 /* NOTE: No response. */
399 error = hn_nvs_req_send(sc, &disconn, sizeof(disconn));
401 if_printf(sc->hn_ifp,
402 "send nvs chim disconn failed: %d\n", error);
404 * Fine for a revoked channel, since the hypervisor
405 * does not drain TX bufring for a revoked channel.
407 if (!vmbus_chan_is_revoked(sc->hn_prichan))
408 sc->hn_flags |= HN_FLAG_CHIM_REF;
410 sc->hn_flags &= ~HN_FLAG_CHIM_CONNECTED;
413 * Wait for the hypervisor to receive this NVS request.
416 * The TX bufring will not be drained by the hypervisor,
417 * if the primary channel is revoked.
419 while (!vmbus_chan_tx_empty(sc->hn_prichan) &&
420 !vmbus_chan_is_revoked(sc->hn_prichan))
423 * Linger long enough for NVS to disconnect chimney
426 pause("lingtx", (200 * hz) / 1000);
429 if (sc->hn_chim_gpadl != 0) {
431 * Disconnect chimney sending buffer from primary channel.
433 error = vmbus_chan_gpadl_disconnect(sc->hn_prichan,
436 if_printf(sc->hn_ifp,
437 "chim gpadl disconn failed: %d\n", error);
438 sc->hn_flags |= HN_FLAG_CHIM_REF;
440 sc->hn_chim_gpadl = 0;
443 if (sc->hn_chim_bmap != NULL) {
444 free(sc->hn_chim_bmap, M_DEVBUF);
445 sc->hn_chim_bmap = NULL;
446 sc->hn_chim_bmap_cnt = 0;
451 hn_nvs_doinit(struct hn_softc *sc, uint32_t nvs_ver)
453 struct vmbus_xact *xact;
454 struct hn_nvs_init *init;
455 const struct hn_nvs_init_resp *resp;
459 xact = vmbus_xact_get(sc->hn_xact, sizeof(*init));
461 if_printf(sc->hn_ifp, "no xact for nvs init\n");
464 init = vmbus_xact_req_data(xact);
465 init->nvs_type = HN_NVS_TYPE_INIT;
466 init->nvs_ver_min = nvs_ver;
467 init->nvs_ver_max = nvs_ver;
469 resp_len = sizeof(*resp);
470 resp = hn_nvs_xact_execute(sc, xact, init, sizeof(*init), &resp_len,
471 HN_NVS_TYPE_INIT_RESP);
473 if_printf(sc->hn_ifp, "exec init failed\n");
474 vmbus_xact_put(xact);
478 status = resp->nvs_status;
479 vmbus_xact_put(xact);
481 if (status != HN_NVS_STATUS_OK) {
484 * Caller may try another NVS version, and will log
485 * error if there are no more NVS versions to try,
486 * so don't bark out loud here.
488 if_printf(sc->hn_ifp, "nvs init failed for ver 0x%x\n",
497 * Configure MTU and enable VLAN.
500 hn_nvs_conf_ndis(struct hn_softc *sc, int mtu)
502 struct hn_nvs_ndis_conf conf;
505 memset(&conf, 0, sizeof(conf));
506 conf.nvs_type = HN_NVS_TYPE_NDIS_CONF;
507 conf.nvs_mtu = mtu + ETHER_HDR_LEN;
508 conf.nvs_caps = HN_NVS_NDIS_CONF_VLAN;
509 if (sc->hn_nvs_ver >= HN_NVS_VERSION_5)
510 conf.nvs_caps |= HN_NVS_NDIS_CONF_SRIOV;
512 /* NOTE: No response. */
513 error = hn_nvs_req_send(sc, &conf, sizeof(conf));
515 if_printf(sc->hn_ifp, "send nvs ndis conf failed: %d\n", error);
520 if_printf(sc->hn_ifp, "nvs ndis conf done\n");
521 sc->hn_caps |= HN_CAP_MTU | HN_CAP_VLAN;
526 hn_nvs_init_ndis(struct hn_softc *sc)
528 struct hn_nvs_ndis_init ndis;
531 memset(&ndis, 0, sizeof(ndis));
532 ndis.nvs_type = HN_NVS_TYPE_NDIS_INIT;
533 ndis.nvs_ndis_major = HN_NDIS_VERSION_MAJOR(sc->hn_ndis_ver);
534 ndis.nvs_ndis_minor = HN_NDIS_VERSION_MINOR(sc->hn_ndis_ver);
536 /* NOTE: No response. */
537 error = hn_nvs_req_send(sc, &ndis, sizeof(ndis));
539 if_printf(sc->hn_ifp, "send nvs ndis init failed: %d\n", error);
544 hn_nvs_init(struct hn_softc *sc)
548 if (device_is_attached(sc->hn_dev)) {
550 * NVS version and NDIS version MUST NOT be changed.
553 if_printf(sc->hn_ifp, "reinit NVS version 0x%x, "
554 "NDIS version %u.%u\n", sc->hn_nvs_ver,
555 HN_NDIS_VERSION_MAJOR(sc->hn_ndis_ver),
556 HN_NDIS_VERSION_MINOR(sc->hn_ndis_ver));
559 error = hn_nvs_doinit(sc, sc->hn_nvs_ver);
561 if_printf(sc->hn_ifp, "reinit NVS version 0x%x "
562 "failed: %d\n", sc->hn_nvs_ver, error);
569 * Find the supported NVS version and set NDIS version accordingly.
571 for (i = 0; i < nitems(hn_nvs_version); ++i) {
572 error = hn_nvs_doinit(sc, hn_nvs_version[i]);
574 sc->hn_nvs_ver = hn_nvs_version[i];
576 /* Set NDIS version according to NVS version. */
577 sc->hn_ndis_ver = HN_NDIS_VERSION_6_30;
578 if (sc->hn_nvs_ver <= HN_NVS_VERSION_4)
579 sc->hn_ndis_ver = HN_NDIS_VERSION_6_1;
582 if_printf(sc->hn_ifp, "NVS version 0x%x, "
583 "NDIS version %u.%u\n", sc->hn_nvs_ver,
584 HN_NDIS_VERSION_MAJOR(sc->hn_ndis_ver),
585 HN_NDIS_VERSION_MINOR(sc->hn_ndis_ver));
590 if_printf(sc->hn_ifp, "no NVS available\n");
594 if (sc->hn_nvs_ver >= HN_NVS_VERSION_5)
595 sc->hn_caps |= HN_CAP_HASHVAL;
600 hn_nvs_attach(struct hn_softc *sc, int mtu)
604 if (hyperv_ver_major >= 10) {
605 /* UDP 4-tuple hash is enforced. */
606 sc->hn_caps |= HN_CAP_UDPHASH;
612 error = hn_nvs_init(sc);
616 if (sc->hn_nvs_ver >= HN_NVS_VERSION_2) {
618 * Configure NDIS before initializing it.
620 error = hn_nvs_conf_ndis(sc, mtu);
628 error = hn_nvs_init_ndis(sc);
635 error = hn_nvs_conn_rxbuf(sc);
640 * Connect chimney sending buffer.
642 error = hn_nvs_conn_chim(sc);
644 hn_nvs_disconn_rxbuf(sc);
651 hn_nvs_detach(struct hn_softc *sc)
654 /* NOTE: there are no requests to stop the NVS. */
655 hn_nvs_disconn_rxbuf(sc);
656 hn_nvs_disconn_chim(sc);
660 hn_nvs_sent_xact(struct hn_nvs_sendctx *sndc,
661 struct hn_softc *sc __unused, struct vmbus_channel *chan __unused,
662 const void *data, int dlen)
665 vmbus_xact_wakeup(sndc->hn_cbarg, data, dlen);
669 hn_nvs_sent_none(struct hn_nvs_sendctx *sndc __unused,
670 struct hn_softc *sc __unused, struct vmbus_channel *chan __unused,
671 const void *data __unused, int dlen __unused)
677 hn_nvs_alloc_subchans(struct hn_softc *sc, int *nsubch0)
679 struct vmbus_xact *xact;
680 struct hn_nvs_subch_req *req;
681 const struct hn_nvs_subch_resp *resp;
682 int error, nsubch_req;
686 nsubch_req = *nsubch0;
687 KASSERT(nsubch_req > 0, ("invalid # of sub-channels %d", nsubch_req));
689 xact = vmbus_xact_get(sc->hn_xact, sizeof(*req));
691 if_printf(sc->hn_ifp, "no xact for nvs subch alloc\n");
694 req = vmbus_xact_req_data(xact);
695 req->nvs_type = HN_NVS_TYPE_SUBCH_REQ;
696 req->nvs_op = HN_NVS_SUBCH_OP_ALLOC;
697 req->nvs_nsubch = nsubch_req;
699 resp_len = sizeof(*resp);
700 resp = hn_nvs_xact_execute(sc, xact, req, sizeof(*req), &resp_len,
701 HN_NVS_TYPE_SUBCH_RESP);
703 if_printf(sc->hn_ifp, "exec nvs subch alloc failed\n");
707 if (resp->nvs_status != HN_NVS_STATUS_OK) {
708 if_printf(sc->hn_ifp, "nvs subch alloc failed: %x\n",
714 nsubch = resp->nvs_nsubch;
715 if (nsubch > nsubch_req) {
716 if_printf(sc->hn_ifp, "%u subchans are allocated, "
717 "requested %d\n", nsubch, nsubch_req);
723 vmbus_xact_put(xact);
728 hn_nvs_send_rndis_ctrl(struct vmbus_channel *chan,
729 struct hn_nvs_sendctx *sndc, struct vmbus_gpa *gpa, int gpa_cnt)
732 return hn_nvs_send_rndis_sglist(chan, HN_NVS_RNDIS_MTYPE_CTRL,
737 hn_nvs_set_datapath(struct hn_softc *sc, uint32_t path)
739 struct hn_nvs_datapath dp;
741 memset(&dp, 0, sizeof(dp));
742 dp.nvs_type = HN_NVS_TYPE_SET_DATAPATH;
743 dp.nvs_active_path = path;
745 hn_nvs_req_send(sc, &dp, sizeof(dp));