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>
47 #include <net/if_arp.h>
48 #include <net/if_media.h>
50 #include <netinet/in.h>
51 #include <netinet/tcp_lro.h>
53 #include <dev/hyperv/include/hyperv.h>
54 #include <dev/hyperv/include/hyperv_busdma.h>
55 #include <dev/hyperv/include/vmbus.h>
56 #include <dev/hyperv/include/vmbus_xact.h>
58 #include <dev/hyperv/netvsc/ndis.h>
59 #include <dev/hyperv/netvsc/if_hnreg.h>
60 #include <dev/hyperv/netvsc/if_hnvar.h>
61 #include <dev/hyperv/netvsc/hn_nvs.h>
63 static int hn_nvs_conn_chim(struct hn_softc *);
64 static int hn_nvs_conn_rxbuf(struct hn_softc *);
65 static void hn_nvs_disconn_chim(struct hn_softc *);
66 static void hn_nvs_disconn_rxbuf(struct hn_softc *);
67 static int hn_nvs_conf_ndis(struct hn_softc *, int);
68 static int hn_nvs_init_ndis(struct hn_softc *);
69 static int hn_nvs_doinit(struct hn_softc *, uint32_t);
70 static int hn_nvs_init(struct hn_softc *);
71 static const void *hn_nvs_xact_execute(struct hn_softc *,
72 struct vmbus_xact *, void *, int,
74 static void hn_nvs_sent_none(struct hn_nvs_sendctx *,
75 struct hn_softc *, struct vmbus_channel *,
78 struct hn_nvs_sendctx hn_nvs_sendctx_none =
79 HN_NVS_SENDCTX_INITIALIZER(hn_nvs_sent_none, NULL);
81 static const uint32_t hn_nvs_version[] = {
89 hn_nvs_xact_execute(struct hn_softc *sc, struct vmbus_xact *xact,
90 void *req, int reqlen, size_t *resplen0, uint32_t type)
92 struct hn_nvs_sendctx sndc;
93 size_t resplen, min_resplen = *resplen0;
94 const struct hn_nvs_hdr *hdr;
97 KASSERT(min_resplen >= sizeof(*hdr),
98 ("invalid minimum response len %zu", min_resplen));
101 * Execute the xact setup by the caller.
103 hn_nvs_sendctx_init(&sndc, hn_nvs_sent_xact, xact);
105 vmbus_xact_activate(xact);
106 error = hn_nvs_send(sc->hn_prichan, VMBUS_CHANPKT_FLAG_RC,
109 vmbus_xact_deactivate(xact);
112 hdr = vmbus_chan_xact_wait(sc->hn_prichan, xact, &resplen,
116 * Check this NVS response message.
118 if (resplen < min_resplen) {
119 if_printf(sc->hn_ifp, "invalid NVS resp len %zu\n", resplen);
122 if (hdr->nvs_type != type) {
123 if_printf(sc->hn_ifp, "unexpected NVS resp 0x%08x, "
124 "expect 0x%08x\n", hdr->nvs_type, type);
133 hn_nvs_req_send(struct hn_softc *sc, void *req, int reqlen)
136 return (hn_nvs_send(sc->hn_prichan, VMBUS_CHANPKT_FLAG_NONE,
137 req, reqlen, &hn_nvs_sendctx_none));
141 hn_nvs_conn_rxbuf(struct hn_softc *sc)
143 struct vmbus_xact *xact = NULL;
144 struct hn_nvs_rxbuf_conn *conn;
145 const struct hn_nvs_rxbuf_connresp *resp;
148 int error, rxbuf_size;
151 * Limit RXBUF size for old NVS.
153 if (sc->hn_nvs_ver <= HN_NVS_VERSION_2)
154 rxbuf_size = HN_RXBUF_SIZE_COMPAT;
156 rxbuf_size = HN_RXBUF_SIZE;
159 * Connect the RXBUF GPADL to the primary channel.
162 * Only primary channel has RXBUF connected to it. Sub-channels
163 * just share this RXBUF.
165 error = vmbus_chan_gpadl_connect(sc->hn_prichan,
166 sc->hn_rxbuf_dma.hv_paddr, rxbuf_size, &sc->hn_rxbuf_gpadl);
168 if_printf(sc->hn_ifp, "rxbuf gpadl conn failed: %d\n",
174 * Connect RXBUF to NVS.
177 xact = vmbus_xact_get(sc->hn_xact, sizeof(*conn));
179 if_printf(sc->hn_ifp, "no xact for nvs rxbuf conn\n");
183 conn = vmbus_xact_req_data(xact);
184 conn->nvs_type = HN_NVS_TYPE_RXBUF_CONN;
185 conn->nvs_gpadl = sc->hn_rxbuf_gpadl;
186 conn->nvs_sig = HN_NVS_RXBUF_SIG;
188 resp_len = sizeof(*resp);
189 resp = hn_nvs_xact_execute(sc, xact, conn, sizeof(*conn), &resp_len,
190 HN_NVS_TYPE_RXBUF_CONNRESP);
192 if_printf(sc->hn_ifp, "exec nvs rxbuf conn failed\n");
197 status = resp->nvs_status;
198 vmbus_xact_put(xact);
201 if (status != HN_NVS_STATUS_OK) {
202 if_printf(sc->hn_ifp, "nvs rxbuf conn failed: %x\n", status);
206 sc->hn_flags |= HN_FLAG_RXBUF_CONNECTED;
212 vmbus_xact_put(xact);
213 hn_nvs_disconn_rxbuf(sc);
218 hn_nvs_conn_chim(struct hn_softc *sc)
220 struct vmbus_xact *xact = NULL;
221 struct hn_nvs_chim_conn *chim;
222 const struct hn_nvs_chim_connresp *resp;
224 uint32_t status, sectsz;
228 * Connect chimney sending buffer GPADL to the primary channel.
231 * Only primary channel has chimney sending buffer connected to it.
232 * Sub-channels just share this chimney sending buffer.
234 error = vmbus_chan_gpadl_connect(sc->hn_prichan,
235 sc->hn_chim_dma.hv_paddr, HN_CHIM_SIZE, &sc->hn_chim_gpadl);
237 if_printf(sc->hn_ifp, "chim gpadl conn failed: %d\n", error);
242 * Connect chimney sending buffer to NVS
245 xact = vmbus_xact_get(sc->hn_xact, sizeof(*chim));
247 if_printf(sc->hn_ifp, "no xact for nvs chim conn\n");
251 chim = vmbus_xact_req_data(xact);
252 chim->nvs_type = HN_NVS_TYPE_CHIM_CONN;
253 chim->nvs_gpadl = sc->hn_chim_gpadl;
254 chim->nvs_sig = HN_NVS_CHIM_SIG;
256 resp_len = sizeof(*resp);
257 resp = hn_nvs_xact_execute(sc, xact, chim, sizeof(*chim), &resp_len,
258 HN_NVS_TYPE_CHIM_CONNRESP);
260 if_printf(sc->hn_ifp, "exec nvs chim conn failed\n");
265 status = resp->nvs_status;
266 sectsz = resp->nvs_sectsz;
267 vmbus_xact_put(xact);
270 if (status != HN_NVS_STATUS_OK) {
271 if_printf(sc->hn_ifp, "nvs chim conn failed: %x\n", status);
275 if (sectsz == 0 || sectsz % sizeof(uint32_t) != 0) {
277 * Can't use chimney sending buffer; done!
280 if_printf(sc->hn_ifp, "zero chimney sending buffer "
283 if_printf(sc->hn_ifp, "misaligned chimney sending "
284 "buffers, section size: %u\n", sectsz);
286 sc->hn_chim_szmax = 0;
288 sc->hn_flags |= HN_FLAG_CHIM_CONNECTED;
292 sc->hn_chim_szmax = sectsz;
293 sc->hn_chim_cnt = HN_CHIM_SIZE / sc->hn_chim_szmax;
294 if (HN_CHIM_SIZE % sc->hn_chim_szmax != 0) {
295 if_printf(sc->hn_ifp, "chimney sending sections are "
296 "not properly aligned\n");
298 if (sc->hn_chim_cnt % LONG_BIT != 0) {
299 if_printf(sc->hn_ifp, "discard %d chimney sending sections\n",
300 sc->hn_chim_cnt % LONG_BIT);
303 sc->hn_chim_bmap_cnt = sc->hn_chim_cnt / LONG_BIT;
304 sc->hn_chim_bmap = malloc(sc->hn_chim_bmap_cnt * sizeof(u_long),
305 M_DEVBUF, M_WAITOK | M_ZERO);
308 sc->hn_flags |= HN_FLAG_CHIM_CONNECTED;
310 if_printf(sc->hn_ifp, "chimney sending buffer %d/%d\n",
311 sc->hn_chim_szmax, sc->hn_chim_cnt);
317 vmbus_xact_put(xact);
318 hn_nvs_disconn_chim(sc);
323 hn_nvs_disconn_rxbuf(struct hn_softc *sc)
327 if (sc->hn_flags & HN_FLAG_RXBUF_CONNECTED) {
328 struct hn_nvs_rxbuf_disconn disconn;
331 * Disconnect RXBUF from NVS.
333 memset(&disconn, 0, sizeof(disconn));
334 disconn.nvs_type = HN_NVS_TYPE_RXBUF_DISCONN;
335 disconn.nvs_sig = HN_NVS_RXBUF_SIG;
337 /* NOTE: No response. */
338 error = hn_nvs_req_send(sc, &disconn, sizeof(disconn));
340 if_printf(sc->hn_ifp,
341 "send nvs rxbuf disconn failed: %d\n", error);
343 * Fine for a revoked channel, since the hypervisor
344 * does not drain TX bufring for a revoked channel.
346 if (!vmbus_chan_is_revoked(sc->hn_prichan))
347 sc->hn_flags |= HN_FLAG_RXBUF_REF;
349 sc->hn_flags &= ~HN_FLAG_RXBUF_CONNECTED;
352 * Wait for the hypervisor to receive this NVS request.
355 * The TX bufring will not be drained by the hypervisor,
356 * if the primary channel is revoked.
358 while (!vmbus_chan_tx_empty(sc->hn_prichan) &&
359 !vmbus_chan_is_revoked(sc->hn_prichan))
362 * Linger long enough for NVS to disconnect RXBUF.
364 pause("lingtx", (200 * hz) / 1000);
367 if (sc->hn_rxbuf_gpadl != 0) {
369 * Disconnect RXBUF from primary channel.
371 error = vmbus_chan_gpadl_disconnect(sc->hn_prichan,
374 if_printf(sc->hn_ifp,
375 "rxbuf gpadl disconn failed: %d\n", error);
376 sc->hn_flags |= HN_FLAG_RXBUF_REF;
378 sc->hn_rxbuf_gpadl = 0;
383 hn_nvs_disconn_chim(struct hn_softc *sc)
387 if (sc->hn_flags & HN_FLAG_CHIM_CONNECTED) {
388 struct hn_nvs_chim_disconn disconn;
391 * Disconnect chimney sending buffer from NVS.
393 memset(&disconn, 0, sizeof(disconn));
394 disconn.nvs_type = HN_NVS_TYPE_CHIM_DISCONN;
395 disconn.nvs_sig = HN_NVS_CHIM_SIG;
397 /* NOTE: No response. */
398 error = hn_nvs_req_send(sc, &disconn, sizeof(disconn));
400 if_printf(sc->hn_ifp,
401 "send nvs chim disconn failed: %d\n", error);
403 * Fine for a revoked channel, since the hypervisor
404 * does not drain TX bufring for a revoked channel.
406 if (!vmbus_chan_is_revoked(sc->hn_prichan))
407 sc->hn_flags |= HN_FLAG_CHIM_REF;
409 sc->hn_flags &= ~HN_FLAG_CHIM_CONNECTED;
412 * Wait for the hypervisor to receive this NVS request.
415 * The TX bufring will not be drained by the hypervisor,
416 * if the primary channel is revoked.
418 while (!vmbus_chan_tx_empty(sc->hn_prichan) &&
419 !vmbus_chan_is_revoked(sc->hn_prichan))
422 * Linger long enough for NVS to disconnect chimney
425 pause("lingtx", (200 * hz) / 1000);
428 if (sc->hn_chim_gpadl != 0) {
430 * Disconnect chimney sending buffer from primary channel.
432 error = vmbus_chan_gpadl_disconnect(sc->hn_prichan,
435 if_printf(sc->hn_ifp,
436 "chim gpadl disconn failed: %d\n", error);
437 sc->hn_flags |= HN_FLAG_CHIM_REF;
439 sc->hn_chim_gpadl = 0;
442 if (sc->hn_chim_bmap != NULL) {
443 free(sc->hn_chim_bmap, M_DEVBUF);
444 sc->hn_chim_bmap = NULL;
445 sc->hn_chim_bmap_cnt = 0;
450 hn_nvs_doinit(struct hn_softc *sc, uint32_t nvs_ver)
452 struct vmbus_xact *xact;
453 struct hn_nvs_init *init;
454 const struct hn_nvs_init_resp *resp;
458 xact = vmbus_xact_get(sc->hn_xact, sizeof(*init));
460 if_printf(sc->hn_ifp, "no xact for nvs init\n");
463 init = vmbus_xact_req_data(xact);
464 init->nvs_type = HN_NVS_TYPE_INIT;
465 init->nvs_ver_min = nvs_ver;
466 init->nvs_ver_max = nvs_ver;
468 resp_len = sizeof(*resp);
469 resp = hn_nvs_xact_execute(sc, xact, init, sizeof(*init), &resp_len,
470 HN_NVS_TYPE_INIT_RESP);
472 if_printf(sc->hn_ifp, "exec init failed\n");
473 vmbus_xact_put(xact);
477 status = resp->nvs_status;
478 vmbus_xact_put(xact);
480 if (status != HN_NVS_STATUS_OK) {
483 * Caller may try another NVS version, and will log
484 * error if there are no more NVS versions to try,
485 * so don't bark out loud here.
487 if_printf(sc->hn_ifp, "nvs init failed for ver 0x%x\n",
496 * Configure MTU and enable VLAN.
499 hn_nvs_conf_ndis(struct hn_softc *sc, int mtu)
501 struct hn_nvs_ndis_conf conf;
504 memset(&conf, 0, sizeof(conf));
505 conf.nvs_type = HN_NVS_TYPE_NDIS_CONF;
507 conf.nvs_caps = HN_NVS_NDIS_CONF_VLAN;
508 if (sc->hn_nvs_ver >= HN_NVS_VERSION_5)
509 conf.nvs_caps |= HN_NVS_NDIS_CONF_SRIOV;
511 /* NOTE: No response. */
512 error = hn_nvs_req_send(sc, &conf, sizeof(conf));
514 if_printf(sc->hn_ifp, "send nvs ndis conf failed: %d\n", error);
519 if_printf(sc->hn_ifp, "nvs ndis conf done\n");
520 sc->hn_caps |= HN_CAP_MTU | HN_CAP_VLAN;
525 hn_nvs_init_ndis(struct hn_softc *sc)
527 struct hn_nvs_ndis_init ndis;
530 memset(&ndis, 0, sizeof(ndis));
531 ndis.nvs_type = HN_NVS_TYPE_NDIS_INIT;
532 ndis.nvs_ndis_major = HN_NDIS_VERSION_MAJOR(sc->hn_ndis_ver);
533 ndis.nvs_ndis_minor = HN_NDIS_VERSION_MINOR(sc->hn_ndis_ver);
535 /* NOTE: No response. */
536 error = hn_nvs_req_send(sc, &ndis, sizeof(ndis));
538 if_printf(sc->hn_ifp, "send nvs ndis init failed: %d\n", error);
543 hn_nvs_init(struct hn_softc *sc)
547 if (device_is_attached(sc->hn_dev)) {
549 * NVS version and NDIS version MUST NOT be changed.
552 if_printf(sc->hn_ifp, "reinit NVS version 0x%x, "
553 "NDIS version %u.%u\n", sc->hn_nvs_ver,
554 HN_NDIS_VERSION_MAJOR(sc->hn_ndis_ver),
555 HN_NDIS_VERSION_MINOR(sc->hn_ndis_ver));
558 error = hn_nvs_doinit(sc, sc->hn_nvs_ver);
560 if_printf(sc->hn_ifp, "reinit NVS version 0x%x "
561 "failed: %d\n", sc->hn_nvs_ver, error);
568 * Find the supported NVS version and set NDIS version accordingly.
570 for (i = 0; i < nitems(hn_nvs_version); ++i) {
571 error = hn_nvs_doinit(sc, hn_nvs_version[i]);
573 sc->hn_nvs_ver = hn_nvs_version[i];
575 /* Set NDIS version according to NVS version. */
576 sc->hn_ndis_ver = HN_NDIS_VERSION_6_30;
577 if (sc->hn_nvs_ver <= HN_NVS_VERSION_4)
578 sc->hn_ndis_ver = HN_NDIS_VERSION_6_1;
581 if_printf(sc->hn_ifp, "NVS version 0x%x, "
582 "NDIS version %u.%u\n", sc->hn_nvs_ver,
583 HN_NDIS_VERSION_MAJOR(sc->hn_ndis_ver),
584 HN_NDIS_VERSION_MINOR(sc->hn_ndis_ver));
589 if_printf(sc->hn_ifp, "no NVS available\n");
593 if (sc->hn_nvs_ver >= HN_NVS_VERSION_5)
594 sc->hn_caps |= HN_CAP_HASHVAL;
599 hn_nvs_attach(struct hn_softc *sc, int mtu)
606 error = hn_nvs_init(sc);
610 if (sc->hn_nvs_ver >= HN_NVS_VERSION_2) {
612 * Configure NDIS before initializing it.
614 error = hn_nvs_conf_ndis(sc, mtu);
622 error = hn_nvs_init_ndis(sc);
629 error = hn_nvs_conn_rxbuf(sc);
634 * Connect chimney sending buffer.
636 error = hn_nvs_conn_chim(sc);
638 hn_nvs_disconn_rxbuf(sc);
645 hn_nvs_detach(struct hn_softc *sc)
648 /* NOTE: there are no requests to stop the NVS. */
649 hn_nvs_disconn_rxbuf(sc);
650 hn_nvs_disconn_chim(sc);
654 hn_nvs_sent_xact(struct hn_nvs_sendctx *sndc,
655 struct hn_softc *sc __unused, struct vmbus_channel *chan __unused,
656 const void *data, int dlen)
659 vmbus_xact_wakeup(sndc->hn_cbarg, data, dlen);
663 hn_nvs_sent_none(struct hn_nvs_sendctx *sndc __unused,
664 struct hn_softc *sc __unused, struct vmbus_channel *chan __unused,
665 const void *data __unused, int dlen __unused)
671 hn_nvs_alloc_subchans(struct hn_softc *sc, int *nsubch0)
673 struct vmbus_xact *xact;
674 struct hn_nvs_subch_req *req;
675 const struct hn_nvs_subch_resp *resp;
676 int error, nsubch_req;
680 nsubch_req = *nsubch0;
681 KASSERT(nsubch_req > 0, ("invalid # of sub-channels %d", nsubch_req));
683 xact = vmbus_xact_get(sc->hn_xact, sizeof(*req));
685 if_printf(sc->hn_ifp, "no xact for nvs subch alloc\n");
688 req = vmbus_xact_req_data(xact);
689 req->nvs_type = HN_NVS_TYPE_SUBCH_REQ;
690 req->nvs_op = HN_NVS_SUBCH_OP_ALLOC;
691 req->nvs_nsubch = nsubch_req;
693 resp_len = sizeof(*resp);
694 resp = hn_nvs_xact_execute(sc, xact, req, sizeof(*req), &resp_len,
695 HN_NVS_TYPE_SUBCH_RESP);
697 if_printf(sc->hn_ifp, "exec nvs subch alloc failed\n");
701 if (resp->nvs_status != HN_NVS_STATUS_OK) {
702 if_printf(sc->hn_ifp, "nvs subch alloc failed: %x\n",
708 nsubch = resp->nvs_nsubch;
709 if (nsubch > nsubch_req) {
710 if_printf(sc->hn_ifp, "%u subchans are allocated, "
711 "requested %d\n", nsubch, nsubch_req);
717 vmbus_xact_put(xact);
722 hn_nvs_send_rndis_ctrl(struct vmbus_channel *chan,
723 struct hn_nvs_sendctx *sndc, struct vmbus_gpa *gpa, int gpa_cnt)
726 return hn_nvs_send_rndis_sglist(chan, HN_NVS_RNDIS_MTYPE_CTRL,
731 hn_nvs_set_datapath(struct hn_softc *sc, uint32_t path)
733 struct hn_nvs_datapath dp;
735 memset(&dp, 0, sizeof(dp));
736 dp.nvs_type = HN_NVS_TYPE_SET_DATAPATH;
737 dp.nvs_active_path = path;
739 hn_nvs_req_send(sc, &dp, sizeof(dp));