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.
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 int hn_nvs_disconn_chim(struct hn_softc *);
66 static int 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_xact_wait(xact, &resplen);
115 * Check this NVS response message.
117 if (resplen < min_resplen) {
118 if_printf(sc->hn_ifp, "invalid NVS resp len %zu\n", resplen);
121 if (hdr->nvs_type != type) {
122 if_printf(sc->hn_ifp, "unexpected NVS resp 0x%08x, "
123 "expect 0x%08x\n", hdr->nvs_type, type);
132 hn_nvs_req_send(struct hn_softc *sc, void *req, int reqlen)
135 return (hn_nvs_send(sc->hn_prichan, VMBUS_CHANPKT_FLAG_NONE,
136 req, reqlen, &hn_nvs_sendctx_none));
140 hn_nvs_conn_rxbuf(struct hn_softc *sc)
142 struct vmbus_xact *xact = NULL;
143 struct hn_nvs_rxbuf_conn *conn;
144 const struct hn_nvs_rxbuf_connresp *resp;
147 int error, rxbuf_size;
150 * Limit RXBUF size for old NVS.
152 if (sc->hn_nvs_ver <= HN_NVS_VERSION_2)
153 rxbuf_size = HN_RXBUF_SIZE_COMPAT;
155 rxbuf_size = HN_RXBUF_SIZE;
158 * Connect the RXBUF GPADL to the primary channel.
161 * Only primary channel has RXBUF connected to it. Sub-channels
162 * just share this RXBUF.
164 error = vmbus_chan_gpadl_connect(sc->hn_prichan,
165 sc->hn_rxbuf_dma.hv_paddr, rxbuf_size, &sc->hn_rxbuf_gpadl);
167 if_printf(sc->hn_ifp, "rxbuf gpadl conn failed: %d\n",
173 * Connect RXBUF to NVS.
176 xact = vmbus_xact_get(sc->hn_xact, sizeof(*conn));
178 if_printf(sc->hn_ifp, "no xact for nvs rxbuf conn\n");
182 conn = vmbus_xact_req_data(xact);
183 conn->nvs_type = HN_NVS_TYPE_RXBUF_CONN;
184 conn->nvs_gpadl = sc->hn_rxbuf_gpadl;
185 conn->nvs_sig = HN_NVS_RXBUF_SIG;
187 resp_len = sizeof(*resp);
188 resp = hn_nvs_xact_execute(sc, xact, conn, sizeof(*conn), &resp_len,
189 HN_NVS_TYPE_RXBUF_CONNRESP);
191 if_printf(sc->hn_ifp, "exec nvs rxbuf conn failed\n");
196 status = resp->nvs_status;
197 vmbus_xact_put(xact);
200 if (status != HN_NVS_STATUS_OK) {
201 if_printf(sc->hn_ifp, "nvs rxbuf conn failed: %x\n", status);
205 sc->hn_flags |= HN_FLAG_RXBUF_CONNECTED;
211 vmbus_xact_put(xact);
212 hn_nvs_disconn_rxbuf(sc);
217 hn_nvs_conn_chim(struct hn_softc *sc)
219 struct vmbus_xact *xact = NULL;
220 struct hn_nvs_chim_conn *chim;
221 const struct hn_nvs_chim_connresp *resp;
223 uint32_t status, sectsz;
227 * Connect chimney sending buffer GPADL to the primary channel.
230 * Only primary channel has chimney sending buffer connected to it.
231 * Sub-channels just share this chimney sending buffer.
233 error = vmbus_chan_gpadl_connect(sc->hn_prichan,
234 sc->hn_chim_dma.hv_paddr, HN_CHIM_SIZE, &sc->hn_chim_gpadl);
236 if_printf(sc->hn_ifp, "chim gpadl conn failed: %d\n", error);
241 * Connect chimney sending buffer to NVS
244 xact = vmbus_xact_get(sc->hn_xact, sizeof(*chim));
246 if_printf(sc->hn_ifp, "no xact for nvs chim conn\n");
250 chim = vmbus_xact_req_data(xact);
251 chim->nvs_type = HN_NVS_TYPE_CHIM_CONN;
252 chim->nvs_gpadl = sc->hn_chim_gpadl;
253 chim->nvs_sig = HN_NVS_CHIM_SIG;
255 resp_len = sizeof(*resp);
256 resp = hn_nvs_xact_execute(sc, xact, chim, sizeof(*chim), &resp_len,
257 HN_NVS_TYPE_CHIM_CONNRESP);
259 if_printf(sc->hn_ifp, "exec nvs chim conn failed\n");
264 status = resp->nvs_status;
265 sectsz = resp->nvs_sectsz;
266 vmbus_xact_put(xact);
269 if (status != HN_NVS_STATUS_OK) {
270 if_printf(sc->hn_ifp, "nvs chim conn failed: %x\n", status);
275 if_printf(sc->hn_ifp, "zero chimney sending buffer "
280 sc->hn_chim_szmax = sectsz;
281 sc->hn_chim_cnt = HN_CHIM_SIZE / sc->hn_chim_szmax;
282 if (HN_CHIM_SIZE % sc->hn_chim_szmax != 0) {
283 if_printf(sc->hn_ifp, "chimney sending sections are "
284 "not properly aligned\n");
286 if (sc->hn_chim_cnt % LONG_BIT != 0) {
287 if_printf(sc->hn_ifp, "discard %d chimney sending sections\n",
288 sc->hn_chim_cnt % LONG_BIT);
291 sc->hn_chim_bmap_cnt = sc->hn_chim_cnt / LONG_BIT;
292 sc->hn_chim_bmap = malloc(sc->hn_chim_bmap_cnt * sizeof(u_long),
293 M_DEVBUF, M_WAITOK | M_ZERO);
296 sc->hn_flags |= HN_FLAG_CHIM_CONNECTED;
298 if_printf(sc->hn_ifp, "chimney sending buffer %d/%d\n",
299 sc->hn_chim_szmax, sc->hn_chim_cnt);
305 vmbus_xact_put(xact);
306 hn_nvs_disconn_chim(sc);
311 hn_nvs_disconn_rxbuf(struct hn_softc *sc)
315 if (sc->hn_flags & HN_FLAG_RXBUF_CONNECTED) {
316 struct hn_nvs_rxbuf_disconn disconn;
319 * Disconnect RXBUF from NVS.
321 memset(&disconn, 0, sizeof(disconn));
322 disconn.nvs_type = HN_NVS_TYPE_RXBUF_DISCONN;
323 disconn.nvs_sig = HN_NVS_RXBUF_SIG;
325 /* NOTE: No response. */
326 error = hn_nvs_req_send(sc, &disconn, sizeof(disconn));
328 if_printf(sc->hn_ifp,
329 "send nvs rxbuf disconn failed: %d\n", error);
332 sc->hn_flags &= ~HN_FLAG_RXBUF_CONNECTED;
335 * Wait for the hypervisor to receive this NVS request.
337 while (!vmbus_chan_tx_empty(sc->hn_prichan))
340 * Linger long enough for NVS to disconnect RXBUF.
342 pause("lingtx", (200 * hz) / 1000);
345 if (sc->hn_rxbuf_gpadl != 0) {
347 * Disconnect RXBUF from primary channel.
349 error = vmbus_chan_gpadl_disconnect(sc->hn_prichan,
352 if_printf(sc->hn_ifp,
353 "rxbuf gpadl disconn failed: %d\n", error);
356 sc->hn_rxbuf_gpadl = 0;
362 hn_nvs_disconn_chim(struct hn_softc *sc)
366 if (sc->hn_flags & HN_FLAG_CHIM_CONNECTED) {
367 struct hn_nvs_chim_disconn disconn;
370 * Disconnect chimney sending buffer from NVS.
372 memset(&disconn, 0, sizeof(disconn));
373 disconn.nvs_type = HN_NVS_TYPE_CHIM_DISCONN;
374 disconn.nvs_sig = HN_NVS_CHIM_SIG;
376 /* NOTE: No response. */
377 error = hn_nvs_req_send(sc, &disconn, sizeof(disconn));
379 if_printf(sc->hn_ifp,
380 "send nvs chim disconn failed: %d\n", error);
383 sc->hn_flags &= ~HN_FLAG_CHIM_CONNECTED;
386 * Wait for the hypervisor to receive this NVS request.
388 while (!vmbus_chan_tx_empty(sc->hn_prichan))
391 * Linger long enough for NVS to disconnect chimney
394 pause("lingtx", (200 * hz) / 1000);
397 if (sc->hn_chim_gpadl != 0) {
399 * Disconnect chimney sending buffer from primary channel.
401 error = vmbus_chan_gpadl_disconnect(sc->hn_prichan,
404 if_printf(sc->hn_ifp,
405 "chim gpadl disconn failed: %d\n", error);
408 sc->hn_chim_gpadl = 0;
411 if (sc->hn_chim_bmap != NULL) {
412 free(sc->hn_chim_bmap, M_DEVBUF);
413 sc->hn_chim_bmap = NULL;
419 hn_nvs_doinit(struct hn_softc *sc, uint32_t nvs_ver)
421 struct vmbus_xact *xact;
422 struct hn_nvs_init *init;
423 const struct hn_nvs_init_resp *resp;
427 xact = vmbus_xact_get(sc->hn_xact, sizeof(*init));
429 if_printf(sc->hn_ifp, "no xact for nvs init\n");
432 init = vmbus_xact_req_data(xact);
433 init->nvs_type = HN_NVS_TYPE_INIT;
434 init->nvs_ver_min = nvs_ver;
435 init->nvs_ver_max = nvs_ver;
437 resp_len = sizeof(*resp);
438 resp = hn_nvs_xact_execute(sc, xact, init, sizeof(*init), &resp_len,
439 HN_NVS_TYPE_INIT_RESP);
441 if_printf(sc->hn_ifp, "exec init failed\n");
442 vmbus_xact_put(xact);
446 status = resp->nvs_status;
447 vmbus_xact_put(xact);
449 if (status != HN_NVS_STATUS_OK) {
452 * Caller may try another NVS version, and will log
453 * error if there are no more NVS versions to try,
454 * so don't bark out loud here.
456 if_printf(sc->hn_ifp, "nvs init failed for ver 0x%x\n",
465 * Configure MTU and enable VLAN.
468 hn_nvs_conf_ndis(struct hn_softc *sc, int mtu)
470 struct hn_nvs_ndis_conf conf;
473 memset(&conf, 0, sizeof(conf));
474 conf.nvs_type = HN_NVS_TYPE_NDIS_CONF;
476 conf.nvs_caps = HN_NVS_NDIS_CONF_VLAN;
478 /* NOTE: No response. */
479 error = hn_nvs_req_send(sc, &conf, sizeof(conf));
481 if_printf(sc->hn_ifp, "send nvs ndis conf failed: %d\n", error);
486 if_printf(sc->hn_ifp, "nvs ndis conf done\n");
487 sc->hn_caps |= HN_CAP_MTU | HN_CAP_VLAN;
492 hn_nvs_init_ndis(struct hn_softc *sc)
494 struct hn_nvs_ndis_init ndis;
497 memset(&ndis, 0, sizeof(ndis));
498 ndis.nvs_type = HN_NVS_TYPE_NDIS_INIT;
499 ndis.nvs_ndis_major = HN_NDIS_VERSION_MAJOR(sc->hn_ndis_ver);
500 ndis.nvs_ndis_minor = HN_NDIS_VERSION_MINOR(sc->hn_ndis_ver);
502 /* NOTE: No response. */
503 error = hn_nvs_req_send(sc, &ndis, sizeof(ndis));
505 if_printf(sc->hn_ifp, "send nvs ndis init failed: %d\n", error);
510 hn_nvs_init(struct hn_softc *sc)
514 if (device_is_attached(sc->hn_dev)) {
516 * NVS version and NDIS version MUST NOT be changed.
519 if_printf(sc->hn_ifp, "reinit NVS version 0x%x, "
520 "NDIS version %u.%u\n", sc->hn_nvs_ver,
521 HN_NDIS_VERSION_MAJOR(sc->hn_ndis_ver),
522 HN_NDIS_VERSION_MINOR(sc->hn_ndis_ver));
525 error = hn_nvs_doinit(sc, sc->hn_nvs_ver);
527 if_printf(sc->hn_ifp, "reinit NVS version 0x%x "
528 "failed: %d\n", sc->hn_nvs_ver, error);
535 * Find the supported NVS version and set NDIS version accordingly.
537 for (i = 0; i < nitems(hn_nvs_version); ++i) {
538 error = hn_nvs_doinit(sc, hn_nvs_version[i]);
540 sc->hn_nvs_ver = hn_nvs_version[i];
542 /* Set NDIS version according to NVS version. */
543 sc->hn_ndis_ver = HN_NDIS_VERSION_6_30;
544 if (sc->hn_nvs_ver <= HN_NVS_VERSION_4)
545 sc->hn_ndis_ver = HN_NDIS_VERSION_6_1;
548 if_printf(sc->hn_ifp, "NVS version 0x%x, "
549 "NDIS version %u.%u\n", sc->hn_nvs_ver,
550 HN_NDIS_VERSION_MAJOR(sc->hn_ndis_ver),
551 HN_NDIS_VERSION_MINOR(sc->hn_ndis_ver));
556 if_printf(sc->hn_ifp, "no NVS available\n");
560 if (sc->hn_nvs_ver >= HN_NVS_VERSION_5)
561 sc->hn_caps |= HN_CAP_HASHVAL;
566 hn_nvs_attach(struct hn_softc *sc, int mtu)
573 error = hn_nvs_init(sc);
577 if (sc->hn_nvs_ver >= HN_NVS_VERSION_2) {
579 * Configure NDIS before initializing it.
581 error = hn_nvs_conf_ndis(sc, mtu);
589 error = hn_nvs_init_ndis(sc);
596 error = hn_nvs_conn_rxbuf(sc);
601 * Connect chimney sending buffer.
603 error = hn_nvs_conn_chim(sc);
610 hn_nvs_detach(struct hn_softc *sc)
613 /* NOTE: there are no requests to stop the NVS. */
614 hn_nvs_disconn_rxbuf(sc);
615 hn_nvs_disconn_chim(sc);
619 hn_nvs_sent_xact(struct hn_nvs_sendctx *sndc,
620 struct hn_softc *sc __unused, struct vmbus_channel *chan __unused,
621 const void *data, int dlen)
624 vmbus_xact_wakeup(sndc->hn_cbarg, data, dlen);
628 hn_nvs_sent_none(struct hn_nvs_sendctx *sndc __unused,
629 struct hn_softc *sc __unused, struct vmbus_channel *chan __unused,
630 const void *data __unused, int dlen __unused)
636 hn_nvs_alloc_subchans(struct hn_softc *sc, int *nsubch0)
638 struct vmbus_xact *xact;
639 struct hn_nvs_subch_req *req;
640 const struct hn_nvs_subch_resp *resp;
641 int error, nsubch_req;
645 nsubch_req = *nsubch0;
646 KASSERT(nsubch_req > 0, ("invalid # of sub-channels %d", nsubch_req));
648 xact = vmbus_xact_get(sc->hn_xact, sizeof(*req));
650 if_printf(sc->hn_ifp, "no xact for nvs subch alloc\n");
653 req = vmbus_xact_req_data(xact);
654 req->nvs_type = HN_NVS_TYPE_SUBCH_REQ;
655 req->nvs_op = HN_NVS_SUBCH_OP_ALLOC;
656 req->nvs_nsubch = nsubch_req;
658 resp_len = sizeof(*resp);
659 resp = hn_nvs_xact_execute(sc, xact, req, sizeof(*req), &resp_len,
660 HN_NVS_TYPE_SUBCH_RESP);
662 if_printf(sc->hn_ifp, "exec nvs subch alloc failed\n");
666 if (resp->nvs_status != HN_NVS_STATUS_OK) {
667 if_printf(sc->hn_ifp, "nvs subch alloc failed: %x\n",
673 nsubch = resp->nvs_nsubch;
674 if (nsubch > nsubch_req) {
675 if_printf(sc->hn_ifp, "%u subchans are allocated, "
676 "requested %d\n", nsubch, nsubch_req);
682 vmbus_xact_put(xact);
687 hn_nvs_send_rndis_ctrl(struct vmbus_channel *chan,
688 struct hn_nvs_sendctx *sndc, struct vmbus_gpa *gpa, int gpa_cnt)
691 return hn_nvs_send_rndis_sglist(chan, HN_NVS_RNDIS_MTYPE_CTRL,