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 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);
277 * Can't use chimney sending buffer; done!
279 if_printf(sc->hn_ifp, "zero chimney sending buffer "
281 sc->hn_chim_szmax = 0;
283 sc->hn_flags |= HN_FLAG_CHIM_CONNECTED;
287 sc->hn_chim_szmax = sectsz;
288 sc->hn_chim_cnt = HN_CHIM_SIZE / sc->hn_chim_szmax;
289 if (HN_CHIM_SIZE % sc->hn_chim_szmax != 0) {
290 if_printf(sc->hn_ifp, "chimney sending sections are "
291 "not properly aligned\n");
293 if (sc->hn_chim_cnt % LONG_BIT != 0) {
294 if_printf(sc->hn_ifp, "discard %d chimney sending sections\n",
295 sc->hn_chim_cnt % LONG_BIT);
298 sc->hn_chim_bmap_cnt = sc->hn_chim_cnt / LONG_BIT;
299 sc->hn_chim_bmap = malloc(sc->hn_chim_bmap_cnt * sizeof(u_long),
300 M_DEVBUF, M_WAITOK | M_ZERO);
303 sc->hn_flags |= HN_FLAG_CHIM_CONNECTED;
305 if_printf(sc->hn_ifp, "chimney sending buffer %d/%d\n",
306 sc->hn_chim_szmax, sc->hn_chim_cnt);
312 vmbus_xact_put(xact);
313 hn_nvs_disconn_chim(sc);
318 hn_nvs_disconn_rxbuf(struct hn_softc *sc)
322 if (sc->hn_flags & HN_FLAG_RXBUF_CONNECTED) {
323 struct hn_nvs_rxbuf_disconn disconn;
326 * Disconnect RXBUF from NVS.
328 memset(&disconn, 0, sizeof(disconn));
329 disconn.nvs_type = HN_NVS_TYPE_RXBUF_DISCONN;
330 disconn.nvs_sig = HN_NVS_RXBUF_SIG;
332 /* NOTE: No response. */
333 error = hn_nvs_req_send(sc, &disconn, sizeof(disconn));
335 if_printf(sc->hn_ifp,
336 "send nvs rxbuf disconn failed: %d\n", error);
338 * Fine for a revoked channel, since the hypervisor
339 * does not drain TX bufring for a revoked channel.
341 if (!vmbus_chan_is_revoked(sc->hn_prichan))
342 sc->hn_flags |= HN_FLAG_RXBUF_REF;
344 sc->hn_flags &= ~HN_FLAG_RXBUF_CONNECTED;
347 * Wait for the hypervisor to receive this NVS request.
350 * The TX bufring will not be drained by the hypervisor,
351 * if the primary channel is revoked.
353 while (!vmbus_chan_tx_empty(sc->hn_prichan) &&
354 !vmbus_chan_is_revoked(sc->hn_prichan))
357 * Linger long enough for NVS to disconnect RXBUF.
359 pause("lingtx", (200 * hz) / 1000);
362 if (sc->hn_rxbuf_gpadl != 0) {
364 * Disconnect RXBUF from primary channel.
366 error = vmbus_chan_gpadl_disconnect(sc->hn_prichan,
369 if_printf(sc->hn_ifp,
370 "rxbuf gpadl disconn failed: %d\n", error);
371 sc->hn_flags |= HN_FLAG_RXBUF_REF;
373 sc->hn_rxbuf_gpadl = 0;
378 hn_nvs_disconn_chim(struct hn_softc *sc)
382 if (sc->hn_flags & HN_FLAG_CHIM_CONNECTED) {
383 struct hn_nvs_chim_disconn disconn;
386 * Disconnect chimney sending buffer from NVS.
388 memset(&disconn, 0, sizeof(disconn));
389 disconn.nvs_type = HN_NVS_TYPE_CHIM_DISCONN;
390 disconn.nvs_sig = HN_NVS_CHIM_SIG;
392 /* NOTE: No response. */
393 error = hn_nvs_req_send(sc, &disconn, sizeof(disconn));
395 if_printf(sc->hn_ifp,
396 "send nvs chim disconn failed: %d\n", error);
398 * Fine for a revoked channel, since the hypervisor
399 * does not drain TX bufring for a revoked channel.
401 if (!vmbus_chan_is_revoked(sc->hn_prichan))
402 sc->hn_flags |= HN_FLAG_CHIM_REF;
404 sc->hn_flags &= ~HN_FLAG_CHIM_CONNECTED;
407 * Wait for the hypervisor to receive this NVS request.
410 * The TX bufring will not be drained by the hypervisor,
411 * if the primary channel is revoked.
413 while (!vmbus_chan_tx_empty(sc->hn_prichan) &&
414 !vmbus_chan_is_revoked(sc->hn_prichan))
417 * Linger long enough for NVS to disconnect chimney
420 pause("lingtx", (200 * hz) / 1000);
423 if (sc->hn_chim_gpadl != 0) {
425 * Disconnect chimney sending buffer from primary channel.
427 error = vmbus_chan_gpadl_disconnect(sc->hn_prichan,
430 if_printf(sc->hn_ifp,
431 "chim gpadl disconn failed: %d\n", error);
432 sc->hn_flags |= HN_FLAG_CHIM_REF;
434 sc->hn_chim_gpadl = 0;
437 if (sc->hn_chim_bmap != NULL) {
438 free(sc->hn_chim_bmap, M_DEVBUF);
439 sc->hn_chim_bmap = NULL;
440 sc->hn_chim_bmap_cnt = 0;
445 hn_nvs_doinit(struct hn_softc *sc, uint32_t nvs_ver)
447 struct vmbus_xact *xact;
448 struct hn_nvs_init *init;
449 const struct hn_nvs_init_resp *resp;
453 xact = vmbus_xact_get(sc->hn_xact, sizeof(*init));
455 if_printf(sc->hn_ifp, "no xact for nvs init\n");
458 init = vmbus_xact_req_data(xact);
459 init->nvs_type = HN_NVS_TYPE_INIT;
460 init->nvs_ver_min = nvs_ver;
461 init->nvs_ver_max = nvs_ver;
463 resp_len = sizeof(*resp);
464 resp = hn_nvs_xact_execute(sc, xact, init, sizeof(*init), &resp_len,
465 HN_NVS_TYPE_INIT_RESP);
467 if_printf(sc->hn_ifp, "exec init failed\n");
468 vmbus_xact_put(xact);
472 status = resp->nvs_status;
473 vmbus_xact_put(xact);
475 if (status != HN_NVS_STATUS_OK) {
478 * Caller may try another NVS version, and will log
479 * error if there are no more NVS versions to try,
480 * so don't bark out loud here.
482 if_printf(sc->hn_ifp, "nvs init failed for ver 0x%x\n",
491 * Configure MTU and enable VLAN.
494 hn_nvs_conf_ndis(struct hn_softc *sc, int mtu)
496 struct hn_nvs_ndis_conf conf;
499 memset(&conf, 0, sizeof(conf));
500 conf.nvs_type = HN_NVS_TYPE_NDIS_CONF;
502 conf.nvs_caps = HN_NVS_NDIS_CONF_VLAN;
504 /* NOTE: No response. */
505 error = hn_nvs_req_send(sc, &conf, sizeof(conf));
507 if_printf(sc->hn_ifp, "send nvs ndis conf failed: %d\n", error);
512 if_printf(sc->hn_ifp, "nvs ndis conf done\n");
513 sc->hn_caps |= HN_CAP_MTU | HN_CAP_VLAN;
518 hn_nvs_init_ndis(struct hn_softc *sc)
520 struct hn_nvs_ndis_init ndis;
523 memset(&ndis, 0, sizeof(ndis));
524 ndis.nvs_type = HN_NVS_TYPE_NDIS_INIT;
525 ndis.nvs_ndis_major = HN_NDIS_VERSION_MAJOR(sc->hn_ndis_ver);
526 ndis.nvs_ndis_minor = HN_NDIS_VERSION_MINOR(sc->hn_ndis_ver);
528 /* NOTE: No response. */
529 error = hn_nvs_req_send(sc, &ndis, sizeof(ndis));
531 if_printf(sc->hn_ifp, "send nvs ndis init failed: %d\n", error);
536 hn_nvs_init(struct hn_softc *sc)
540 if (device_is_attached(sc->hn_dev)) {
542 * NVS version and NDIS version MUST NOT be changed.
545 if_printf(sc->hn_ifp, "reinit NVS version 0x%x, "
546 "NDIS version %u.%u\n", sc->hn_nvs_ver,
547 HN_NDIS_VERSION_MAJOR(sc->hn_ndis_ver),
548 HN_NDIS_VERSION_MINOR(sc->hn_ndis_ver));
551 error = hn_nvs_doinit(sc, sc->hn_nvs_ver);
553 if_printf(sc->hn_ifp, "reinit NVS version 0x%x "
554 "failed: %d\n", sc->hn_nvs_ver, error);
561 * Find the supported NVS version and set NDIS version accordingly.
563 for (i = 0; i < nitems(hn_nvs_version); ++i) {
564 error = hn_nvs_doinit(sc, hn_nvs_version[i]);
566 sc->hn_nvs_ver = hn_nvs_version[i];
568 /* Set NDIS version according to NVS version. */
569 sc->hn_ndis_ver = HN_NDIS_VERSION_6_30;
570 if (sc->hn_nvs_ver <= HN_NVS_VERSION_4)
571 sc->hn_ndis_ver = HN_NDIS_VERSION_6_1;
574 if_printf(sc->hn_ifp, "NVS version 0x%x, "
575 "NDIS version %u.%u\n", sc->hn_nvs_ver,
576 HN_NDIS_VERSION_MAJOR(sc->hn_ndis_ver),
577 HN_NDIS_VERSION_MINOR(sc->hn_ndis_ver));
582 if_printf(sc->hn_ifp, "no NVS available\n");
586 if (sc->hn_nvs_ver >= HN_NVS_VERSION_5)
587 sc->hn_caps |= HN_CAP_HASHVAL;
592 hn_nvs_attach(struct hn_softc *sc, int mtu)
599 error = hn_nvs_init(sc);
603 if (sc->hn_nvs_ver >= HN_NVS_VERSION_2) {
605 * Configure NDIS before initializing it.
607 error = hn_nvs_conf_ndis(sc, mtu);
615 error = hn_nvs_init_ndis(sc);
622 error = hn_nvs_conn_rxbuf(sc);
627 * Connect chimney sending buffer.
629 error = hn_nvs_conn_chim(sc);
631 hn_nvs_disconn_rxbuf(sc);
638 hn_nvs_detach(struct hn_softc *sc)
641 /* NOTE: there are no requests to stop the NVS. */
642 hn_nvs_disconn_rxbuf(sc);
643 hn_nvs_disconn_chim(sc);
647 hn_nvs_sent_xact(struct hn_nvs_sendctx *sndc,
648 struct hn_softc *sc __unused, struct vmbus_channel *chan __unused,
649 const void *data, int dlen)
652 vmbus_xact_wakeup(sndc->hn_cbarg, data, dlen);
656 hn_nvs_sent_none(struct hn_nvs_sendctx *sndc __unused,
657 struct hn_softc *sc __unused, struct vmbus_channel *chan __unused,
658 const void *data __unused, int dlen __unused)
664 hn_nvs_alloc_subchans(struct hn_softc *sc, int *nsubch0)
666 struct vmbus_xact *xact;
667 struct hn_nvs_subch_req *req;
668 const struct hn_nvs_subch_resp *resp;
669 int error, nsubch_req;
673 nsubch_req = *nsubch0;
674 KASSERT(nsubch_req > 0, ("invalid # of sub-channels %d", nsubch_req));
676 xact = vmbus_xact_get(sc->hn_xact, sizeof(*req));
678 if_printf(sc->hn_ifp, "no xact for nvs subch alloc\n");
681 req = vmbus_xact_req_data(xact);
682 req->nvs_type = HN_NVS_TYPE_SUBCH_REQ;
683 req->nvs_op = HN_NVS_SUBCH_OP_ALLOC;
684 req->nvs_nsubch = nsubch_req;
686 resp_len = sizeof(*resp);
687 resp = hn_nvs_xact_execute(sc, xact, req, sizeof(*req), &resp_len,
688 HN_NVS_TYPE_SUBCH_RESP);
690 if_printf(sc->hn_ifp, "exec nvs subch alloc failed\n");
694 if (resp->nvs_status != HN_NVS_STATUS_OK) {
695 if_printf(sc->hn_ifp, "nvs subch alloc failed: %x\n",
701 nsubch = resp->nvs_nsubch;
702 if (nsubch > nsubch_req) {
703 if_printf(sc->hn_ifp, "%u subchans are allocated, "
704 "requested %d\n", nsubch, nsubch_req);
710 vmbus_xact_put(xact);
715 hn_nvs_send_rndis_ctrl(struct vmbus_channel *chan,
716 struct hn_nvs_sendctx *sndc, struct vmbus_gpa *gpa, int gpa_cnt)
719 return hn_nvs_send_rndis_sglist(chan, HN_NVS_RNDIS_MTYPE_CTRL,