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 if (HN_CAN_SLEEP(sc))
113 hdr = vmbus_xact_wait(xact, &resplen);
115 hdr = vmbus_xact_busywait(xact, &resplen);
118 * Check this NVS response message.
120 if (resplen < min_resplen) {
121 if_printf(sc->hn_ifp, "invalid NVS resp len %zu\n", resplen);
124 if (hdr->nvs_type != type) {
125 if_printf(sc->hn_ifp, "unexpected NVS resp 0x%08x, "
126 "expect 0x%08x\n", hdr->nvs_type, type);
135 hn_nvs_req_send(struct hn_softc *sc, void *req, int reqlen)
138 return (hn_nvs_send(sc->hn_prichan, VMBUS_CHANPKT_FLAG_NONE,
139 req, reqlen, &hn_nvs_sendctx_none));
143 hn_nvs_conn_rxbuf(struct hn_softc *sc)
145 struct vmbus_xact *xact = NULL;
146 struct hn_nvs_rxbuf_conn *conn;
147 const struct hn_nvs_rxbuf_connresp *resp;
150 int error, rxbuf_size;
153 * Limit RXBUF size for old NVS.
155 if (sc->hn_nvs_ver <= HN_NVS_VERSION_2)
156 rxbuf_size = HN_RXBUF_SIZE_COMPAT;
158 rxbuf_size = HN_RXBUF_SIZE;
161 * Connect the RXBUF GPADL to the primary channel.
164 * Only primary channel has RXBUF connected to it. Sub-channels
165 * just share this RXBUF.
167 error = vmbus_chan_gpadl_connect(sc->hn_prichan,
168 sc->hn_rxbuf_dma.hv_paddr, rxbuf_size, &sc->hn_rxbuf_gpadl);
170 if_printf(sc->hn_ifp, "rxbuf gpadl conn failed: %d\n",
176 * Connect RXBUF to NVS.
179 xact = vmbus_xact_get(sc->hn_xact, sizeof(*conn));
181 if_printf(sc->hn_ifp, "no xact for nvs rxbuf conn\n");
185 conn = vmbus_xact_req_data(xact);
186 conn->nvs_type = HN_NVS_TYPE_RXBUF_CONN;
187 conn->nvs_gpadl = sc->hn_rxbuf_gpadl;
188 conn->nvs_sig = HN_NVS_RXBUF_SIG;
190 resp_len = sizeof(*resp);
191 resp = hn_nvs_xact_execute(sc, xact, conn, sizeof(*conn), &resp_len,
192 HN_NVS_TYPE_RXBUF_CONNRESP);
194 if_printf(sc->hn_ifp, "exec nvs rxbuf conn failed\n");
199 status = resp->nvs_status;
200 vmbus_xact_put(xact);
203 if (status != HN_NVS_STATUS_OK) {
204 if_printf(sc->hn_ifp, "nvs rxbuf conn failed: %x\n", status);
208 sc->hn_flags |= HN_FLAG_RXBUF_CONNECTED;
214 vmbus_xact_put(xact);
215 hn_nvs_disconn_rxbuf(sc);
220 hn_nvs_conn_chim(struct hn_softc *sc)
222 struct vmbus_xact *xact = NULL;
223 struct hn_nvs_chim_conn *chim;
224 const struct hn_nvs_chim_connresp *resp;
226 uint32_t status, sectsz;
230 * Connect chimney sending buffer GPADL to the primary channel.
233 * Only primary channel has chimney sending buffer connected to it.
234 * Sub-channels just share this chimney sending buffer.
236 error = vmbus_chan_gpadl_connect(sc->hn_prichan,
237 sc->hn_chim_dma.hv_paddr, HN_CHIM_SIZE, &sc->hn_chim_gpadl);
239 if_printf(sc->hn_ifp, "chim gpadl conn failed: %d\n", error);
244 * Connect chimney sending buffer to NVS
247 xact = vmbus_xact_get(sc->hn_xact, sizeof(*chim));
249 if_printf(sc->hn_ifp, "no xact for nvs chim conn\n");
253 chim = vmbus_xact_req_data(xact);
254 chim->nvs_type = HN_NVS_TYPE_CHIM_CONN;
255 chim->nvs_gpadl = sc->hn_chim_gpadl;
256 chim->nvs_sig = HN_NVS_CHIM_SIG;
258 resp_len = sizeof(*resp);
259 resp = hn_nvs_xact_execute(sc, xact, chim, sizeof(*chim), &resp_len,
260 HN_NVS_TYPE_CHIM_CONNRESP);
262 if_printf(sc->hn_ifp, "exec nvs chim conn failed\n");
267 status = resp->nvs_status;
268 sectsz = resp->nvs_sectsz;
269 vmbus_xact_put(xact);
272 if (status != HN_NVS_STATUS_OK) {
273 if_printf(sc->hn_ifp, "nvs chim conn failed: %x\n", status);
278 if_printf(sc->hn_ifp, "zero chimney sending buffer "
283 sc->hn_chim_szmax = sectsz;
284 sc->hn_chim_cnt = HN_CHIM_SIZE / sc->hn_chim_szmax;
285 if (HN_CHIM_SIZE % sc->hn_chim_szmax != 0) {
286 if_printf(sc->hn_ifp, "chimney sending sections are "
287 "not properly aligned\n");
289 if (sc->hn_chim_cnt % LONG_BIT != 0) {
290 if_printf(sc->hn_ifp, "discard %d chimney sending sections\n",
291 sc->hn_chim_cnt % LONG_BIT);
294 sc->hn_chim_bmap_cnt = sc->hn_chim_cnt / LONG_BIT;
295 sc->hn_chim_bmap = malloc(sc->hn_chim_bmap_cnt * sizeof(u_long),
296 M_DEVBUF, M_WAITOK | M_ZERO);
299 sc->hn_flags |= HN_FLAG_CHIM_CONNECTED;
301 if_printf(sc->hn_ifp, "chimney sending buffer %d/%d\n",
302 sc->hn_chim_szmax, sc->hn_chim_cnt);
308 vmbus_xact_put(xact);
309 hn_nvs_disconn_chim(sc);
314 hn_nvs_disconn_rxbuf(struct hn_softc *sc)
318 if (sc->hn_flags & HN_FLAG_RXBUF_CONNECTED) {
319 struct hn_nvs_rxbuf_disconn disconn;
322 * Disconnect RXBUF from NVS.
324 memset(&disconn, 0, sizeof(disconn));
325 disconn.nvs_type = HN_NVS_TYPE_RXBUF_DISCONN;
326 disconn.nvs_sig = HN_NVS_RXBUF_SIG;
328 /* NOTE: No response. */
329 error = hn_nvs_req_send(sc, &disconn, sizeof(disconn));
331 if_printf(sc->hn_ifp,
332 "send nvs rxbuf disconn failed: %d\n", error);
335 sc->hn_flags &= ~HN_FLAG_RXBUF_CONNECTED;
338 * Wait for the hypervisor to receive this NVS request.
341 * The TX bufring will not be drained by the hypervisor,
342 * if the primary channel is revoked.
344 while (!vmbus_chan_tx_empty(sc->hn_prichan) &&
345 !vmbus_chan_is_revoked(sc->hn_prichan))
348 * Linger long enough for NVS to disconnect RXBUF.
350 pause("lingtx", (200 * hz) / 1000);
353 if (sc->hn_rxbuf_gpadl != 0) {
355 * Disconnect RXBUF from primary channel.
357 error = vmbus_chan_gpadl_disconnect(sc->hn_prichan,
360 if_printf(sc->hn_ifp,
361 "rxbuf gpadl disconn failed: %d\n", error);
364 sc->hn_rxbuf_gpadl = 0;
370 hn_nvs_disconn_chim(struct hn_softc *sc)
374 if (sc->hn_flags & HN_FLAG_CHIM_CONNECTED) {
375 struct hn_nvs_chim_disconn disconn;
378 * Disconnect chimney sending buffer from NVS.
380 memset(&disconn, 0, sizeof(disconn));
381 disconn.nvs_type = HN_NVS_TYPE_CHIM_DISCONN;
382 disconn.nvs_sig = HN_NVS_CHIM_SIG;
384 /* NOTE: No response. */
385 error = hn_nvs_req_send(sc, &disconn, sizeof(disconn));
387 if_printf(sc->hn_ifp,
388 "send nvs chim disconn failed: %d\n", error);
391 sc->hn_flags &= ~HN_FLAG_CHIM_CONNECTED;
394 * Wait for the hypervisor to receive this NVS request.
397 * The TX bufring will not be drained by the hypervisor,
398 * if the primary channel is revoked.
400 while (!vmbus_chan_tx_empty(sc->hn_prichan) &&
401 !vmbus_chan_is_revoked(sc->hn_prichan))
404 * Linger long enough for NVS to disconnect chimney
407 pause("lingtx", (200 * hz) / 1000);
410 if (sc->hn_chim_gpadl != 0) {
412 * Disconnect chimney sending buffer from primary channel.
414 error = vmbus_chan_gpadl_disconnect(sc->hn_prichan,
417 if_printf(sc->hn_ifp,
418 "chim gpadl disconn failed: %d\n", error);
421 sc->hn_chim_gpadl = 0;
424 if (sc->hn_chim_bmap != NULL) {
425 free(sc->hn_chim_bmap, M_DEVBUF);
426 sc->hn_chim_bmap = NULL;
432 hn_nvs_doinit(struct hn_softc *sc, uint32_t nvs_ver)
434 struct vmbus_xact *xact;
435 struct hn_nvs_init *init;
436 const struct hn_nvs_init_resp *resp;
440 xact = vmbus_xact_get(sc->hn_xact, sizeof(*init));
442 if_printf(sc->hn_ifp, "no xact for nvs init\n");
445 init = vmbus_xact_req_data(xact);
446 init->nvs_type = HN_NVS_TYPE_INIT;
447 init->nvs_ver_min = nvs_ver;
448 init->nvs_ver_max = nvs_ver;
450 resp_len = sizeof(*resp);
451 resp = hn_nvs_xact_execute(sc, xact, init, sizeof(*init), &resp_len,
452 HN_NVS_TYPE_INIT_RESP);
454 if_printf(sc->hn_ifp, "exec init failed\n");
455 vmbus_xact_put(xact);
459 status = resp->nvs_status;
460 vmbus_xact_put(xact);
462 if (status != HN_NVS_STATUS_OK) {
465 * Caller may try another NVS version, and will log
466 * error if there are no more NVS versions to try,
467 * so don't bark out loud here.
469 if_printf(sc->hn_ifp, "nvs init failed for ver 0x%x\n",
478 * Configure MTU and enable VLAN.
481 hn_nvs_conf_ndis(struct hn_softc *sc, int mtu)
483 struct hn_nvs_ndis_conf conf;
486 memset(&conf, 0, sizeof(conf));
487 conf.nvs_type = HN_NVS_TYPE_NDIS_CONF;
489 conf.nvs_caps = HN_NVS_NDIS_CONF_VLAN;
491 /* NOTE: No response. */
492 error = hn_nvs_req_send(sc, &conf, sizeof(conf));
494 if_printf(sc->hn_ifp, "send nvs ndis conf failed: %d\n", error);
499 if_printf(sc->hn_ifp, "nvs ndis conf done\n");
500 sc->hn_caps |= HN_CAP_MTU | HN_CAP_VLAN;
505 hn_nvs_init_ndis(struct hn_softc *sc)
507 struct hn_nvs_ndis_init ndis;
510 memset(&ndis, 0, sizeof(ndis));
511 ndis.nvs_type = HN_NVS_TYPE_NDIS_INIT;
512 ndis.nvs_ndis_major = HN_NDIS_VERSION_MAJOR(sc->hn_ndis_ver);
513 ndis.nvs_ndis_minor = HN_NDIS_VERSION_MINOR(sc->hn_ndis_ver);
515 /* NOTE: No response. */
516 error = hn_nvs_req_send(sc, &ndis, sizeof(ndis));
518 if_printf(sc->hn_ifp, "send nvs ndis init failed: %d\n", error);
523 hn_nvs_init(struct hn_softc *sc)
527 if (device_is_attached(sc->hn_dev)) {
529 * NVS version and NDIS version MUST NOT be changed.
532 if_printf(sc->hn_ifp, "reinit NVS version 0x%x, "
533 "NDIS version %u.%u\n", sc->hn_nvs_ver,
534 HN_NDIS_VERSION_MAJOR(sc->hn_ndis_ver),
535 HN_NDIS_VERSION_MINOR(sc->hn_ndis_ver));
538 error = hn_nvs_doinit(sc, sc->hn_nvs_ver);
540 if_printf(sc->hn_ifp, "reinit NVS version 0x%x "
541 "failed: %d\n", sc->hn_nvs_ver, error);
548 * Find the supported NVS version and set NDIS version accordingly.
550 for (i = 0; i < nitems(hn_nvs_version); ++i) {
551 error = hn_nvs_doinit(sc, hn_nvs_version[i]);
553 sc->hn_nvs_ver = hn_nvs_version[i];
555 /* Set NDIS version according to NVS version. */
556 sc->hn_ndis_ver = HN_NDIS_VERSION_6_30;
557 if (sc->hn_nvs_ver <= HN_NVS_VERSION_4)
558 sc->hn_ndis_ver = HN_NDIS_VERSION_6_1;
561 if_printf(sc->hn_ifp, "NVS version 0x%x, "
562 "NDIS version %u.%u\n", sc->hn_nvs_ver,
563 HN_NDIS_VERSION_MAJOR(sc->hn_ndis_ver),
564 HN_NDIS_VERSION_MINOR(sc->hn_ndis_ver));
569 if_printf(sc->hn_ifp, "no NVS available\n");
573 if (sc->hn_nvs_ver >= HN_NVS_VERSION_5)
574 sc->hn_caps |= HN_CAP_HASHVAL;
579 hn_nvs_attach(struct hn_softc *sc, int mtu)
586 error = hn_nvs_init(sc);
590 if (sc->hn_nvs_ver >= HN_NVS_VERSION_2) {
592 * Configure NDIS before initializing it.
594 error = hn_nvs_conf_ndis(sc, mtu);
602 error = hn_nvs_init_ndis(sc);
609 error = hn_nvs_conn_rxbuf(sc);
614 * Connect chimney sending buffer.
616 error = hn_nvs_conn_chim(sc);
623 hn_nvs_detach(struct hn_softc *sc)
626 /* NOTE: there are no requests to stop the NVS. */
627 hn_nvs_disconn_rxbuf(sc);
628 hn_nvs_disconn_chim(sc);
632 hn_nvs_sent_xact(struct hn_nvs_sendctx *sndc,
633 struct hn_softc *sc __unused, struct vmbus_channel *chan __unused,
634 const void *data, int dlen)
637 vmbus_xact_wakeup(sndc->hn_cbarg, data, dlen);
641 hn_nvs_sent_none(struct hn_nvs_sendctx *sndc __unused,
642 struct hn_softc *sc __unused, struct vmbus_channel *chan __unused,
643 const void *data __unused, int dlen __unused)
649 hn_nvs_alloc_subchans(struct hn_softc *sc, int *nsubch0)
651 struct vmbus_xact *xact;
652 struct hn_nvs_subch_req *req;
653 const struct hn_nvs_subch_resp *resp;
654 int error, nsubch_req;
658 nsubch_req = *nsubch0;
659 KASSERT(nsubch_req > 0, ("invalid # of sub-channels %d", nsubch_req));
661 xact = vmbus_xact_get(sc->hn_xact, sizeof(*req));
663 if_printf(sc->hn_ifp, "no xact for nvs subch alloc\n");
666 req = vmbus_xact_req_data(xact);
667 req->nvs_type = HN_NVS_TYPE_SUBCH_REQ;
668 req->nvs_op = HN_NVS_SUBCH_OP_ALLOC;
669 req->nvs_nsubch = nsubch_req;
671 resp_len = sizeof(*resp);
672 resp = hn_nvs_xact_execute(sc, xact, req, sizeof(*req), &resp_len,
673 HN_NVS_TYPE_SUBCH_RESP);
675 if_printf(sc->hn_ifp, "exec nvs subch alloc failed\n");
679 if (resp->nvs_status != HN_NVS_STATUS_OK) {
680 if_printf(sc->hn_ifp, "nvs subch alloc failed: %x\n",
686 nsubch = resp->nvs_nsubch;
687 if (nsubch > nsubch_req) {
688 if_printf(sc->hn_ifp, "%u subchans are allocated, "
689 "requested %d\n", nsubch, nsubch_req);
695 vmbus_xact_put(xact);
700 hn_nvs_send_rndis_ctrl(struct vmbus_channel *chan,
701 struct hn_nvs_sendctx *sndc, struct vmbus_gpa *gpa, int gpa_cnt)
704 return hn_nvs_send_rndis_sglist(chan, HN_NVS_RNDIS_MTYPE_CTRL,