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.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include "opt_inet6.h"
35 #include <sys/param.h>
36 #include <sys/socket.h>
37 #include <sys/systm.h>
38 #include <sys/taskqueue.h>
40 #include <machine/atomic.h>
42 #include <net/ethernet.h>
44 #include <net/if_var.h>
45 #include <net/if_media.h>
46 #include <net/rndis.h>
48 #include <netinet/in.h>
49 #include <netinet/ip.h>
50 #include <netinet/tcp_lro.h>
52 #include <dev/hyperv/include/hyperv.h>
53 #include <dev/hyperv/include/hyperv_busdma.h>
54 #include <dev/hyperv/include/vmbus.h>
55 #include <dev/hyperv/include/vmbus_xact.h>
57 #include <dev/hyperv/netvsc/ndis.h>
58 #include <dev/hyperv/netvsc/if_hnreg.h>
59 #include <dev/hyperv/netvsc/if_hnvar.h>
60 #include <dev/hyperv/netvsc/hn_nvs.h>
61 #include <dev/hyperv/netvsc/hn_rndis.h>
63 #define HN_RNDIS_RID_COMPAT_MASK 0xffff
64 #define HN_RNDIS_RID_COMPAT_MAX HN_RNDIS_RID_COMPAT_MASK
66 #define HN_RNDIS_XFER_SIZE 2048
68 #define HN_NDIS_TXCSUM_CAP_IP4 \
69 (NDIS_TXCSUM_CAP_IP4 | NDIS_TXCSUM_CAP_IP4OPT)
70 #define HN_NDIS_TXCSUM_CAP_TCP4 \
71 (NDIS_TXCSUM_CAP_TCP4 | NDIS_TXCSUM_CAP_TCP4OPT)
72 #define HN_NDIS_TXCSUM_CAP_TCP6 \
73 (NDIS_TXCSUM_CAP_TCP6 | NDIS_TXCSUM_CAP_TCP6OPT | \
74 NDIS_TXCSUM_CAP_IP6EXT)
75 #define HN_NDIS_TXCSUM_CAP_UDP6 \
76 (NDIS_TXCSUM_CAP_UDP6 | NDIS_TXCSUM_CAP_IP6EXT)
77 #define HN_NDIS_LSOV2_CAP_IP6 \
78 (NDIS_LSOV2_CAP_IP6EXT | NDIS_LSOV2_CAP_TCP6OPT)
80 static const void *hn_rndis_xact_exec1(struct hn_softc *,
81 struct vmbus_xact *, size_t,
82 struct hn_nvs_sendctx *, size_t *);
83 static const void *hn_rndis_xact_execute(struct hn_softc *,
84 struct vmbus_xact *, uint32_t, size_t, size_t *,
86 static int hn_rndis_query(struct hn_softc *, uint32_t,
87 const void *, size_t, void *, size_t *);
88 static int hn_rndis_query2(struct hn_softc *, uint32_t,
89 const void *, size_t, void *, size_t *, size_t);
90 static int hn_rndis_set(struct hn_softc *, uint32_t,
91 const void *, size_t);
92 static int hn_rndis_init(struct hn_softc *);
93 static int hn_rndis_halt(struct hn_softc *);
94 static int hn_rndis_conf_offload(struct hn_softc *, int);
95 static int hn_rndis_query_hwcaps(struct hn_softc *,
96 struct ndis_offload *);
98 static __inline uint32_t
99 hn_rndis_rid(struct hn_softc *sc)
104 rid = atomic_fetchadd_int(&sc->hn_rndis_rid, 1);
108 /* Use upper 16 bits for non-compat RNDIS messages. */
109 return ((rid & 0xffff) << 16);
113 hn_rndis_rx_ctrl(struct hn_softc *sc, const void *data, int dlen)
115 const struct rndis_comp_hdr *comp;
116 const struct rndis_msghdr *hdr;
118 KASSERT(dlen >= sizeof(*hdr), ("invalid RNDIS msg\n"));
121 switch (hdr->rm_type) {
122 case REMOTE_NDIS_INITIALIZE_CMPLT:
123 case REMOTE_NDIS_QUERY_CMPLT:
124 case REMOTE_NDIS_SET_CMPLT:
125 case REMOTE_NDIS_KEEPALIVE_CMPLT: /* unused */
126 if (dlen < sizeof(*comp)) {
127 if_printf(sc->hn_ifp, "invalid RNDIS cmplt\n");
132 KASSERT(comp->rm_rid > HN_RNDIS_RID_COMPAT_MAX,
133 ("invalid RNDIS rid 0x%08x\n", comp->rm_rid));
134 vmbus_xact_ctx_wakeup(sc->hn_xact, comp, dlen);
137 case REMOTE_NDIS_RESET_CMPLT:
139 * Reset completed, no rid.
142 * RESET is not issued by hn(4), so this message should
145 if_printf(sc->hn_ifp, "RESET cmplt received\n");
149 if_printf(sc->hn_ifp, "unknown RNDIS msg 0x%x\n",
156 hn_rndis_get_eaddr(struct hn_softc *sc, uint8_t *eaddr)
161 eaddr_len = ETHER_ADDR_LEN;
162 error = hn_rndis_query(sc, OID_802_3_PERMANENT_ADDRESS, NULL, 0,
166 if (eaddr_len != ETHER_ADDR_LEN) {
167 if_printf(sc->hn_ifp, "invalid eaddr len %zu\n", eaddr_len);
174 hn_rndis_get_linkstatus(struct hn_softc *sc, uint32_t *link_status)
179 size = sizeof(*link_status);
180 error = hn_rndis_query(sc, OID_GEN_MEDIA_CONNECT_STATUS, NULL, 0,
184 if (size != sizeof(uint32_t)) {
185 if_printf(sc->hn_ifp, "invalid link status len %zu\n", size);
192 hn_rndis_xact_exec1(struct hn_softc *sc, struct vmbus_xact *xact, size_t reqlen,
193 struct hn_nvs_sendctx *sndc, size_t *comp_len)
195 struct vmbus_gpa gpa[HN_XACT_REQ_PGCNT];
199 KASSERT(reqlen <= HN_XACT_REQ_SIZE && reqlen > 0,
200 ("invalid request length %zu", reqlen));
205 paddr = vmbus_xact_req_paddr(xact);
206 KASSERT((paddr & PAGE_MASK) == 0,
207 ("vmbus xact request is not page aligned 0x%jx", (uintmax_t)paddr));
208 for (gpa_cnt = 0; gpa_cnt < HN_XACT_REQ_PGCNT; ++gpa_cnt) {
216 gpa[gpa_cnt].gpa_page = atop(paddr) + gpa_cnt;
217 gpa[gpa_cnt].gpa_len = len;
218 gpa[gpa_cnt].gpa_ofs = 0;
222 KASSERT(reqlen == 0, ("still have %zu request data left", reqlen));
225 * Send this RNDIS control message and wait for its completion
228 vmbus_xact_activate(xact);
229 error = hn_nvs_send_rndis_ctrl(sc->hn_prichan, sndc, gpa, gpa_cnt);
231 vmbus_xact_deactivate(xact);
232 if_printf(sc->hn_ifp, "RNDIS ctrl send failed: %d\n", error);
235 return (vmbus_chan_xact_wait(sc->hn_prichan, xact, comp_len,
240 hn_rndis_xact_execute(struct hn_softc *sc, struct vmbus_xact *xact, uint32_t rid,
241 size_t reqlen, size_t *comp_len0, uint32_t comp_type)
243 const struct rndis_comp_hdr *comp;
244 size_t comp_len, min_complen = *comp_len0;
246 KASSERT(rid > HN_RNDIS_RID_COMPAT_MAX, ("invalid rid %u\n", rid));
247 KASSERT(min_complen >= sizeof(*comp),
248 ("invalid minimum complete len %zu", min_complen));
251 * Execute the xact setup by the caller.
253 comp = hn_rndis_xact_exec1(sc, xact, reqlen, &hn_nvs_sendctx_none,
259 * Check this RNDIS complete message.
261 if (comp_len < min_complen) {
262 if (comp_len >= sizeof(*comp)) {
263 /* rm_status field is valid */
264 if_printf(sc->hn_ifp, "invalid RNDIS comp len %zu, "
265 "status 0x%08x\n", comp_len, comp->rm_status);
267 if_printf(sc->hn_ifp, "invalid RNDIS comp len %zu\n",
272 if (comp->rm_len < min_complen) {
273 if_printf(sc->hn_ifp, "invalid RNDIS comp msglen %u\n",
277 if (comp->rm_type != comp_type) {
278 if_printf(sc->hn_ifp, "unexpected RNDIS comp 0x%08x, "
279 "expect 0x%08x\n", comp->rm_type, comp_type);
282 if (comp->rm_rid != rid) {
283 if_printf(sc->hn_ifp, "RNDIS comp rid mismatch %u, "
284 "expect %u\n", comp->rm_rid, rid);
288 *comp_len0 = comp_len;
293 hn_rndis_query(struct hn_softc *sc, uint32_t oid,
294 const void *idata, size_t idlen, void *odata, size_t *odlen0)
297 return (hn_rndis_query2(sc, oid, idata, idlen, odata, odlen0, *odlen0));
301 hn_rndis_query2(struct hn_softc *sc, uint32_t oid,
302 const void *idata, size_t idlen, void *odata, size_t *odlen0,
305 struct rndis_query_req *req;
306 const struct rndis_query_comp *comp;
307 struct vmbus_xact *xact;
308 size_t reqlen, odlen = *odlen0, comp_len;
312 reqlen = sizeof(*req) + idlen;
313 xact = vmbus_xact_get(sc->hn_xact, reqlen);
315 if_printf(sc->hn_ifp, "no xact for RNDIS query 0x%08x\n", oid);
318 rid = hn_rndis_rid(sc);
319 req = vmbus_xact_req_data(xact);
320 req->rm_type = REMOTE_NDIS_QUERY_MSG;
321 req->rm_len = reqlen;
326 * This is _not_ RNDIS Spec conforming:
327 * "This MUST be set to 0 when there is no input data
328 * associated with the OID."
330 * If this field was set to 0 according to the RNDIS Spec,
331 * Hyper-V would set non-SUCCESS status in the query
334 req->rm_infobufoffset = RNDIS_QUERY_REQ_INFOBUFOFFSET;
337 req->rm_infobuflen = idlen;
338 /* Input data immediately follows RNDIS query. */
339 memcpy(req + 1, idata, idlen);
342 comp_len = sizeof(*comp) + min_odlen;
343 comp = hn_rndis_xact_execute(sc, xact, rid, reqlen, &comp_len,
344 REMOTE_NDIS_QUERY_CMPLT);
346 if_printf(sc->hn_ifp, "exec RNDIS query 0x%08x failed\n", oid);
351 if (comp->rm_status != RNDIS_STATUS_SUCCESS) {
352 if_printf(sc->hn_ifp, "RNDIS query 0x%08x failed: "
353 "status 0x%08x\n", oid, comp->rm_status);
357 if (comp->rm_infobuflen == 0 || comp->rm_infobufoffset == 0) {
358 /* No output data! */
359 if_printf(sc->hn_ifp, "RNDIS query 0x%08x, no data\n", oid);
366 * Check output data length and offset.
368 /* ofs is the offset from the beginning of comp. */
369 ofs = RNDIS_QUERY_COMP_INFOBUFOFFSET_ABS(comp->rm_infobufoffset);
370 if (ofs < sizeof(*comp) || ofs + comp->rm_infobuflen > comp_len) {
371 if_printf(sc->hn_ifp, "RNDIS query invalid comp ib off/len, "
372 "%u/%u\n", comp->rm_infobufoffset, comp->rm_infobuflen);
380 if (comp->rm_infobuflen < odlen)
381 odlen = comp->rm_infobuflen;
382 memcpy(odata, ((const uint8_t *)comp) + ofs, odlen);
387 vmbus_xact_put(xact);
392 hn_rndis_query_rsscaps(struct hn_softc *sc, int *rxr_cnt0)
394 struct ndis_rss_caps in, caps;
396 int error, indsz, rxr_cnt, hash_fnidx;
397 uint32_t hash_func = 0, hash_types = 0;
401 if (sc->hn_ndis_ver < HN_NDIS_VERSION_6_20)
404 memset(&in, 0, sizeof(in));
405 in.ndis_hdr.ndis_type = NDIS_OBJTYPE_RSS_CAPS;
406 in.ndis_hdr.ndis_rev = NDIS_RSS_CAPS_REV_2;
407 in.ndis_hdr.ndis_size = NDIS_RSS_CAPS_SIZE;
409 caps_len = NDIS_RSS_CAPS_SIZE;
410 error = hn_rndis_query2(sc, OID_GEN_RECEIVE_SCALE_CAPABILITIES,
411 &in, NDIS_RSS_CAPS_SIZE, &caps, &caps_len, NDIS_RSS_CAPS_SIZE_6_0);
416 * Preliminary verification.
418 if (caps.ndis_hdr.ndis_type != NDIS_OBJTYPE_RSS_CAPS) {
419 if_printf(sc->hn_ifp, "invalid NDIS objtype 0x%02x\n",
420 caps.ndis_hdr.ndis_type);
423 if (caps.ndis_hdr.ndis_rev < NDIS_RSS_CAPS_REV_1) {
424 if_printf(sc->hn_ifp, "invalid NDIS objrev 0x%02x\n",
425 caps.ndis_hdr.ndis_rev);
428 if (caps.ndis_hdr.ndis_size > caps_len) {
429 if_printf(sc->hn_ifp, "invalid NDIS objsize %u, "
430 "data size %zu\n", caps.ndis_hdr.ndis_size, caps_len);
432 } else if (caps.ndis_hdr.ndis_size < NDIS_RSS_CAPS_SIZE_6_0) {
433 if_printf(sc->hn_ifp, "invalid NDIS objsize %u\n",
434 caps.ndis_hdr.ndis_size);
439 * Save information for later RSS configuration.
441 if (caps.ndis_nrxr == 0) {
442 if_printf(sc->hn_ifp, "0 RX rings!?\n");
446 if_printf(sc->hn_ifp, "%u RX rings\n", caps.ndis_nrxr);
447 rxr_cnt = caps.ndis_nrxr;
449 if (caps.ndis_hdr.ndis_size == NDIS_RSS_CAPS_SIZE &&
450 caps.ndis_hdr.ndis_rev >= NDIS_RSS_CAPS_REV_2) {
451 if (caps.ndis_nind > NDIS_HASH_INDCNT) {
452 if_printf(sc->hn_ifp,
453 "too many RSS indirect table entries %u\n",
457 if (!powerof2(caps.ndis_nind)) {
458 if_printf(sc->hn_ifp, "RSS indirect table size is not "
459 "power-of-2 %u\n", caps.ndis_nind);
463 if_printf(sc->hn_ifp, "RSS indirect table size %u\n",
466 indsz = caps.ndis_nind;
468 indsz = NDIS_HASH_INDCNT;
470 if (indsz < rxr_cnt) {
471 if_printf(sc->hn_ifp, "# of RX rings (%d) > "
472 "RSS indirect table size %d\n", rxr_cnt, indsz);
478 * Toeplitz is at the lowest bit, and it is prefered; so ffs(),
479 * instead of fls(), is used here.
481 hash_fnidx = ffs(caps.ndis_caps & NDIS_RSS_CAP_HASHFUNC_MASK);
482 if (hash_fnidx == 0) {
483 if_printf(sc->hn_ifp, "no hash functions, caps 0x%08x\n",
487 hash_func = 1 << (hash_fnidx - 1); /* ffs is 1-based */
489 if (caps.ndis_caps & NDIS_RSS_CAP_IPV4)
490 hash_types |= NDIS_HASH_IPV4 | NDIS_HASH_TCP_IPV4;
491 if (caps.ndis_caps & NDIS_RSS_CAP_IPV6)
492 hash_types |= NDIS_HASH_IPV6 | NDIS_HASH_TCP_IPV6;
493 if (caps.ndis_caps & NDIS_RSS_CAP_IPV6_EX)
494 hash_types |= NDIS_HASH_IPV6_EX | NDIS_HASH_TCP_IPV6_EX;
495 if (hash_types == 0) {
496 if_printf(sc->hn_ifp, "no hash types, caps 0x%08x\n",
502 sc->hn_rss_ind_size = indsz;
503 sc->hn_rss_hash = hash_func | hash_types;
509 hn_rndis_set(struct hn_softc *sc, uint32_t oid, const void *data, size_t dlen)
511 struct rndis_set_req *req;
512 const struct rndis_set_comp *comp;
513 struct vmbus_xact *xact;
514 size_t reqlen, comp_len;
518 KASSERT(dlen > 0, ("invalid dlen %zu", dlen));
520 reqlen = sizeof(*req) + dlen;
521 xact = vmbus_xact_get(sc->hn_xact, reqlen);
523 if_printf(sc->hn_ifp, "no xact for RNDIS set 0x%08x\n", oid);
526 rid = hn_rndis_rid(sc);
527 req = vmbus_xact_req_data(xact);
528 req->rm_type = REMOTE_NDIS_SET_MSG;
529 req->rm_len = reqlen;
532 req->rm_infobuflen = dlen;
533 req->rm_infobufoffset = RNDIS_SET_REQ_INFOBUFOFFSET;
534 /* Data immediately follows RNDIS set. */
535 memcpy(req + 1, data, dlen);
537 comp_len = sizeof(*comp);
538 comp = hn_rndis_xact_execute(sc, xact, rid, reqlen, &comp_len,
539 REMOTE_NDIS_SET_CMPLT);
541 if_printf(sc->hn_ifp, "exec RNDIS set 0x%08x failed\n", oid);
546 if (comp->rm_status != RNDIS_STATUS_SUCCESS) {
547 if_printf(sc->hn_ifp, "RNDIS set 0x%08x failed: "
548 "status 0x%08x\n", oid, comp->rm_status);
554 vmbus_xact_put(xact);
559 hn_rndis_conf_offload(struct hn_softc *sc, int mtu)
561 struct ndis_offload hwcaps;
562 struct ndis_offload_params params;
565 int error, tso_maxsz, tso_minsg;
567 error = hn_rndis_query_hwcaps(sc, &hwcaps);
569 if_printf(sc->hn_ifp, "hwcaps query failed: %d\n", error);
573 /* NOTE: 0 means "no change" */
574 memset(¶ms, 0, sizeof(params));
576 params.ndis_hdr.ndis_type = NDIS_OBJTYPE_DEFAULT;
577 if (sc->hn_ndis_ver < HN_NDIS_VERSION_6_30) {
578 params.ndis_hdr.ndis_rev = NDIS_OFFLOAD_PARAMS_REV_2;
579 paramsz = NDIS_OFFLOAD_PARAMS_SIZE_6_1;
581 params.ndis_hdr.ndis_rev = NDIS_OFFLOAD_PARAMS_REV_3;
582 paramsz = NDIS_OFFLOAD_PARAMS_SIZE;
584 params.ndis_hdr.ndis_size = paramsz;
589 tso_maxsz = IP_MAXPACKET;
591 if (hwcaps.ndis_lsov2.ndis_ip4_encap & NDIS_OFFLOAD_ENCAP_8023) {
593 params.ndis_lsov2_ip4 = NDIS_OFFLOAD_LSOV2_ON;
595 if (hwcaps.ndis_lsov2.ndis_ip4_maxsz < tso_maxsz)
596 tso_maxsz = hwcaps.ndis_lsov2.ndis_ip4_maxsz;
597 if (hwcaps.ndis_lsov2.ndis_ip4_minsg > tso_minsg)
598 tso_minsg = hwcaps.ndis_lsov2.ndis_ip4_minsg;
600 if ((hwcaps.ndis_lsov2.ndis_ip6_encap & NDIS_OFFLOAD_ENCAP_8023) &&
601 (hwcaps.ndis_lsov2.ndis_ip6_opts & HN_NDIS_LSOV2_CAP_IP6) ==
602 HN_NDIS_LSOV2_CAP_IP6) {
604 params.ndis_lsov2_ip6 = NDIS_OFFLOAD_LSOV2_ON;
606 if (hwcaps.ndis_lsov2.ndis_ip6_maxsz < tso_maxsz)
607 tso_maxsz = hwcaps.ndis_lsov2.ndis_ip6_maxsz;
608 if (hwcaps.ndis_lsov2.ndis_ip6_minsg > tso_minsg)
609 tso_minsg = hwcaps.ndis_lsov2.ndis_ip6_minsg;
611 sc->hn_ndis_tso_szmax = 0;
612 sc->hn_ndis_tso_sgmin = 0;
613 if (caps & (HN_CAP_TSO4 | HN_CAP_TSO6)) {
614 KASSERT(tso_maxsz <= IP_MAXPACKET,
615 ("invalid NDIS TSO maxsz %d", tso_maxsz));
616 KASSERT(tso_minsg >= 2,
617 ("invalid NDIS TSO minsg %d", tso_minsg));
618 if (tso_maxsz < tso_minsg * mtu) {
619 if_printf(sc->hn_ifp, "invalid NDIS TSO config: "
620 "maxsz %d, minsg %d, mtu %d; "
621 "disable TSO4 and TSO6\n",
622 tso_maxsz, tso_minsg, mtu);
623 caps &= ~(HN_CAP_TSO4 | HN_CAP_TSO6);
624 params.ndis_lsov2_ip4 = NDIS_OFFLOAD_LSOV2_OFF;
625 params.ndis_lsov2_ip6 = NDIS_OFFLOAD_LSOV2_OFF;
627 sc->hn_ndis_tso_szmax = tso_maxsz;
628 sc->hn_ndis_tso_sgmin = tso_minsg;
630 if_printf(sc->hn_ifp, "NDIS TSO "
631 "szmax %d sgmin %d\n",
632 sc->hn_ndis_tso_szmax,
633 sc->hn_ndis_tso_sgmin);
639 if ((hwcaps.ndis_csum.ndis_ip4_txcsum & HN_NDIS_TXCSUM_CAP_IP4) ==
640 HN_NDIS_TXCSUM_CAP_IP4) {
642 params.ndis_ip4csum = NDIS_OFFLOAD_PARAM_TX;
644 if (hwcaps.ndis_csum.ndis_ip4_rxcsum & NDIS_RXCSUM_CAP_IP4) {
645 if (params.ndis_ip4csum == NDIS_OFFLOAD_PARAM_TX)
646 params.ndis_ip4csum = NDIS_OFFLOAD_PARAM_TXRX;
648 params.ndis_ip4csum = NDIS_OFFLOAD_PARAM_RX;
652 if ((hwcaps.ndis_csum.ndis_ip4_txcsum & HN_NDIS_TXCSUM_CAP_TCP4) ==
653 HN_NDIS_TXCSUM_CAP_TCP4) {
654 caps |= HN_CAP_TCP4CS;
655 params.ndis_tcp4csum = NDIS_OFFLOAD_PARAM_TX;
657 if (hwcaps.ndis_csum.ndis_ip4_rxcsum & NDIS_RXCSUM_CAP_TCP4) {
658 if (params.ndis_tcp4csum == NDIS_OFFLOAD_PARAM_TX)
659 params.ndis_tcp4csum = NDIS_OFFLOAD_PARAM_TXRX;
661 params.ndis_tcp4csum = NDIS_OFFLOAD_PARAM_RX;
665 if (hwcaps.ndis_csum.ndis_ip4_txcsum & NDIS_TXCSUM_CAP_UDP4) {
666 caps |= HN_CAP_UDP4CS;
667 params.ndis_udp4csum = NDIS_OFFLOAD_PARAM_TX;
669 if (hwcaps.ndis_csum.ndis_ip4_rxcsum & NDIS_RXCSUM_CAP_UDP4) {
670 if (params.ndis_udp4csum == NDIS_OFFLOAD_PARAM_TX)
671 params.ndis_udp4csum = NDIS_OFFLOAD_PARAM_TXRX;
673 params.ndis_udp4csum = NDIS_OFFLOAD_PARAM_RX;
677 if ((hwcaps.ndis_csum.ndis_ip6_txcsum & HN_NDIS_TXCSUM_CAP_TCP6) ==
678 HN_NDIS_TXCSUM_CAP_TCP6) {
679 caps |= HN_CAP_TCP6CS;
680 params.ndis_tcp6csum = NDIS_OFFLOAD_PARAM_TX;
682 if (hwcaps.ndis_csum.ndis_ip6_rxcsum & NDIS_RXCSUM_CAP_TCP6) {
683 if (params.ndis_tcp6csum == NDIS_OFFLOAD_PARAM_TX)
684 params.ndis_tcp6csum = NDIS_OFFLOAD_PARAM_TXRX;
686 params.ndis_tcp6csum = NDIS_OFFLOAD_PARAM_RX;
690 if ((hwcaps.ndis_csum.ndis_ip6_txcsum & HN_NDIS_TXCSUM_CAP_UDP6) ==
691 HN_NDIS_TXCSUM_CAP_UDP6) {
692 caps |= HN_CAP_UDP6CS;
693 params.ndis_udp6csum = NDIS_OFFLOAD_PARAM_TX;
695 if (hwcaps.ndis_csum.ndis_ip6_rxcsum & NDIS_RXCSUM_CAP_UDP6) {
696 if (params.ndis_udp6csum == NDIS_OFFLOAD_PARAM_TX)
697 params.ndis_udp6csum = NDIS_OFFLOAD_PARAM_TXRX;
699 params.ndis_udp6csum = NDIS_OFFLOAD_PARAM_RX;
703 if_printf(sc->hn_ifp, "offload csum: "
704 "ip4 %u, tcp4 %u, udp4 %u, tcp6 %u, udp6 %u\n",
706 params.ndis_tcp4csum,
707 params.ndis_udp4csum,
708 params.ndis_tcp6csum,
709 params.ndis_udp6csum);
710 if_printf(sc->hn_ifp, "offload lsov2: ip4 %u, ip6 %u\n",
711 params.ndis_lsov2_ip4,
712 params.ndis_lsov2_ip6);
715 error = hn_rndis_set(sc, OID_TCP_OFFLOAD_PARAMETERS, ¶ms, paramsz);
717 if_printf(sc->hn_ifp, "offload config failed: %d\n", error);
722 if_printf(sc->hn_ifp, "offload config done\n");
728 hn_rndis_conf_rss(struct hn_softc *sc, uint16_t flags)
730 struct ndis_rssprm_toeplitz *rss = &sc->hn_rss;
731 struct ndis_rss_params *prm = &rss->rss_params;
735 * Only NDIS 6.20+ is supported:
736 * We only support 4bytes element in indirect table, which has been
737 * adopted since NDIS 6.20.
739 KASSERT(sc->hn_ndis_ver >= HN_NDIS_VERSION_6_20,
740 ("NDIS 6.20+ is required, NDIS version 0x%08x", sc->hn_ndis_ver));
742 /* XXX only one can be specified through, popcnt? */
743 KASSERT((sc->hn_rss_hash & NDIS_HASH_FUNCTION_MASK), ("no hash func"));
744 KASSERT((sc->hn_rss_hash & NDIS_HASH_TYPE_MASK), ("no hash types"));
745 KASSERT(sc->hn_rss_ind_size > 0, ("no indirect table size"));
748 if_printf(sc->hn_ifp, "RSS indirect table size %d, "
749 "hash 0x%08x\n", sc->hn_rss_ind_size, sc->hn_rss_hash);
754 * DO NOT whack rss_key and rss_ind, which are setup by the caller.
756 memset(prm, 0, sizeof(*prm));
757 rss_size = NDIS_RSSPRM_TOEPLITZ_SIZE(sc->hn_rss_ind_size);
759 prm->ndis_hdr.ndis_type = NDIS_OBJTYPE_RSS_PARAMS;
760 prm->ndis_hdr.ndis_rev = NDIS_RSS_PARAMS_REV_2;
761 prm->ndis_hdr.ndis_size = rss_size;
762 prm->ndis_flags = flags;
763 prm->ndis_hash = sc->hn_rss_hash;
764 prm->ndis_indsize = sizeof(rss->rss_ind[0]) * sc->hn_rss_ind_size;
765 prm->ndis_indoffset =
766 __offsetof(struct ndis_rssprm_toeplitz, rss_ind[0]);
767 prm->ndis_keysize = sizeof(rss->rss_key);
768 prm->ndis_keyoffset =
769 __offsetof(struct ndis_rssprm_toeplitz, rss_key[0]);
771 error = hn_rndis_set(sc, OID_GEN_RECEIVE_SCALE_PARAMETERS,
774 if_printf(sc->hn_ifp, "RSS config failed: %d\n", error);
777 if_printf(sc->hn_ifp, "RSS config done\n");
783 hn_rndis_set_rxfilter(struct hn_softc *sc, uint32_t filter)
787 error = hn_rndis_set(sc, OID_GEN_CURRENT_PACKET_FILTER,
788 &filter, sizeof(filter));
790 if_printf(sc->hn_ifp, "set RX filter 0x%08x failed: %d\n",
794 if_printf(sc->hn_ifp, "set RX filter 0x%08x done\n",
802 hn_rndis_init(struct hn_softc *sc)
804 struct rndis_init_req *req;
805 const struct rndis_init_comp *comp;
806 struct vmbus_xact *xact;
811 xact = vmbus_xact_get(sc->hn_xact, sizeof(*req));
813 if_printf(sc->hn_ifp, "no xact for RNDIS init\n");
816 rid = hn_rndis_rid(sc);
817 req = vmbus_xact_req_data(xact);
818 req->rm_type = REMOTE_NDIS_INITIALIZE_MSG;
819 req->rm_len = sizeof(*req);
821 req->rm_ver_major = RNDIS_VERSION_MAJOR;
822 req->rm_ver_minor = RNDIS_VERSION_MINOR;
823 req->rm_max_xfersz = HN_RNDIS_XFER_SIZE;
825 comp_len = RNDIS_INIT_COMP_SIZE_MIN;
826 comp = hn_rndis_xact_execute(sc, xact, rid, sizeof(*req), &comp_len,
827 REMOTE_NDIS_INITIALIZE_CMPLT);
829 if_printf(sc->hn_ifp, "exec RNDIS init failed\n");
834 if (comp->rm_status != RNDIS_STATUS_SUCCESS) {
835 if_printf(sc->hn_ifp, "RNDIS init failed: status 0x%08x\n",
840 sc->hn_rndis_agg_size = comp->rm_pktmaxsz;
841 sc->hn_rndis_agg_pkts = comp->rm_pktmaxcnt;
842 sc->hn_rndis_agg_align = 1U << comp->rm_align;
844 if (sc->hn_rndis_agg_align < sizeof(uint32_t)) {
846 * The RNDIS packet messsage encap assumes that the RNDIS
847 * packet message is at least 4 bytes aligned. Fix up the
848 * alignment here, if the remote side sets the alignment
851 if_printf(sc->hn_ifp, "fixup RNDIS aggpkt align: %u -> %zu\n",
852 sc->hn_rndis_agg_align, sizeof(uint32_t));
853 sc->hn_rndis_agg_align = sizeof(uint32_t);
857 if_printf(sc->hn_ifp, "RNDIS ver %u.%u, "
858 "aggpkt size %u, aggpkt cnt %u, aggpkt align %u\n",
859 comp->rm_ver_major, comp->rm_ver_minor,
860 sc->hn_rndis_agg_size, sc->hn_rndis_agg_pkts,
861 sc->hn_rndis_agg_align);
865 vmbus_xact_put(xact);
870 hn_rndis_halt(struct hn_softc *sc)
872 struct vmbus_xact *xact;
873 struct rndis_halt_req *halt;
874 struct hn_nvs_sendctx sndc;
877 xact = vmbus_xact_get(sc->hn_xact, sizeof(*halt));
879 if_printf(sc->hn_ifp, "no xact for RNDIS halt\n");
882 halt = vmbus_xact_req_data(xact);
883 halt->rm_type = REMOTE_NDIS_HALT_MSG;
884 halt->rm_len = sizeof(*halt);
885 halt->rm_rid = hn_rndis_rid(sc);
887 /* No RNDIS completion; rely on NVS message send completion */
888 hn_nvs_sendctx_init(&sndc, hn_nvs_sent_xact, xact);
889 hn_rndis_xact_exec1(sc, xact, sizeof(*halt), &sndc, &comp_len);
891 vmbus_xact_put(xact);
893 if_printf(sc->hn_ifp, "RNDIS halt done\n");
898 hn_rndis_query_hwcaps(struct hn_softc *sc, struct ndis_offload *caps)
900 struct ndis_offload in;
901 size_t caps_len, size;
904 memset(&in, 0, sizeof(in));
905 in.ndis_hdr.ndis_type = NDIS_OBJTYPE_OFFLOAD;
906 if (sc->hn_ndis_ver >= HN_NDIS_VERSION_6_30) {
907 in.ndis_hdr.ndis_rev = NDIS_OFFLOAD_REV_3;
908 size = NDIS_OFFLOAD_SIZE;
909 } else if (sc->hn_ndis_ver >= HN_NDIS_VERSION_6_1) {
910 in.ndis_hdr.ndis_rev = NDIS_OFFLOAD_REV_2;
911 size = NDIS_OFFLOAD_SIZE_6_1;
913 in.ndis_hdr.ndis_rev = NDIS_OFFLOAD_REV_1;
914 size = NDIS_OFFLOAD_SIZE_6_0;
916 in.ndis_hdr.ndis_size = size;
918 caps_len = NDIS_OFFLOAD_SIZE;
919 error = hn_rndis_query2(sc, OID_TCP_OFFLOAD_HARDWARE_CAPABILITIES,
920 &in, size, caps, &caps_len, NDIS_OFFLOAD_SIZE_6_0);
925 * Preliminary verification.
927 if (caps->ndis_hdr.ndis_type != NDIS_OBJTYPE_OFFLOAD) {
928 if_printf(sc->hn_ifp, "invalid NDIS objtype 0x%02x\n",
929 caps->ndis_hdr.ndis_type);
932 if (caps->ndis_hdr.ndis_rev < NDIS_OFFLOAD_REV_1) {
933 if_printf(sc->hn_ifp, "invalid NDIS objrev 0x%02x\n",
934 caps->ndis_hdr.ndis_rev);
937 if (caps->ndis_hdr.ndis_size > caps_len) {
938 if_printf(sc->hn_ifp, "invalid NDIS objsize %u, "
939 "data size %zu\n", caps->ndis_hdr.ndis_size, caps_len);
941 } else if (caps->ndis_hdr.ndis_size < NDIS_OFFLOAD_SIZE_6_0) {
942 if_printf(sc->hn_ifp, "invalid NDIS objsize %u\n",
943 caps->ndis_hdr.ndis_size);
950 * caps->ndis_hdr.ndis_size MUST be checked before accessing
951 * NDIS 6.1+ specific fields.
953 if_printf(sc->hn_ifp, "hwcaps rev %u\n",
954 caps->ndis_hdr.ndis_rev);
956 if_printf(sc->hn_ifp, "hwcaps csum: "
957 "ip4 tx 0x%x/0x%x rx 0x%x/0x%x, "
958 "ip6 tx 0x%x/0x%x rx 0x%x/0x%x\n",
959 caps->ndis_csum.ndis_ip4_txcsum,
960 caps->ndis_csum.ndis_ip4_txenc,
961 caps->ndis_csum.ndis_ip4_rxcsum,
962 caps->ndis_csum.ndis_ip4_rxenc,
963 caps->ndis_csum.ndis_ip6_txcsum,
964 caps->ndis_csum.ndis_ip6_txenc,
965 caps->ndis_csum.ndis_ip6_rxcsum,
966 caps->ndis_csum.ndis_ip6_rxenc);
967 if_printf(sc->hn_ifp, "hwcaps lsov2: "
968 "ip4 maxsz %u minsg %u encap 0x%x, "
969 "ip6 maxsz %u minsg %u encap 0x%x opts 0x%x\n",
970 caps->ndis_lsov2.ndis_ip4_maxsz,
971 caps->ndis_lsov2.ndis_ip4_minsg,
972 caps->ndis_lsov2.ndis_ip4_encap,
973 caps->ndis_lsov2.ndis_ip6_maxsz,
974 caps->ndis_lsov2.ndis_ip6_minsg,
975 caps->ndis_lsov2.ndis_ip6_encap,
976 caps->ndis_lsov2.ndis_ip6_opts);
982 hn_rndis_attach(struct hn_softc *sc, int mtu, int *init_done)
991 error = hn_rndis_init(sc);
997 * Configure NDIS offload settings.
999 hn_rndis_conf_offload(sc, mtu);
1004 hn_rndis_detach(struct hn_softc *sc)
1007 /* Halt the RNDIS. */