]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/hyperv/netvsc/hn_rndis.c
MFC 308908,308909
[FreeBSD/stable/10.git] / sys / dev / hyperv / netvsc / hn_rndis.c
1 /*-
2  * Copyright (c) 2009-2012,2016 Microsoft Corp.
3  * Copyright (c) 2010-2012 Citrix Inc.
4  * Copyright (c) 2012 NetApp Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice unmodified, this list of conditions, and the following
12  *    disclaimer.
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.
16  *
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.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include "opt_inet6.h"
33 #include "opt_inet.h"
34
35 #include <sys/param.h>
36 #include <sys/socket.h>
37 #include <sys/systm.h>
38 #include <sys/taskqueue.h>
39
40 #include <machine/atomic.h>
41
42 #include <net/ethernet.h>
43 #include <net/if.h>
44 #include <net/if_arp.h>
45 #include <net/if_var.h>
46 #include <net/if_media.h>
47 #include <net/rndis.h>
48
49 #include <netinet/in.h>
50 #include <netinet/ip.h>
51 #include <netinet/tcp_lro.h>
52
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>
57
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>
62 #include <dev/hyperv/netvsc/hn_rndis.h>
63
64 #define HN_RNDIS_RID_COMPAT_MASK        0xffff
65 #define HN_RNDIS_RID_COMPAT_MAX         HN_RNDIS_RID_COMPAT_MASK
66
67 #define HN_RNDIS_XFER_SIZE              2048
68
69 #define HN_NDIS_TXCSUM_CAP_IP4          \
70         (NDIS_TXCSUM_CAP_IP4 | NDIS_TXCSUM_CAP_IP4OPT)
71 #define HN_NDIS_TXCSUM_CAP_TCP4         \
72         (NDIS_TXCSUM_CAP_TCP4 | NDIS_TXCSUM_CAP_TCP4OPT)
73 #define HN_NDIS_TXCSUM_CAP_TCP6         \
74         (NDIS_TXCSUM_CAP_TCP6 | NDIS_TXCSUM_CAP_TCP6OPT | \
75          NDIS_TXCSUM_CAP_IP6EXT)
76 #define HN_NDIS_TXCSUM_CAP_UDP6         \
77         (NDIS_TXCSUM_CAP_UDP6 | NDIS_TXCSUM_CAP_IP6EXT)
78 #define HN_NDIS_LSOV2_CAP_IP6           \
79         (NDIS_LSOV2_CAP_IP6EXT | NDIS_LSOV2_CAP_TCP6OPT)
80
81 static const void       *hn_rndis_xact_exec1(struct hn_softc *,
82                             struct vmbus_xact *, size_t,
83                             struct hn_nvs_sendctx *, size_t *);
84 static const void       *hn_rndis_xact_execute(struct hn_softc *,
85                             struct vmbus_xact *, uint32_t, size_t, size_t *,
86                             uint32_t);
87 static int              hn_rndis_query(struct hn_softc *, uint32_t,
88                             const void *, size_t, void *, size_t *);
89 static int              hn_rndis_query2(struct hn_softc *, uint32_t,
90                             const void *, size_t, void *, size_t *, size_t);
91 static int              hn_rndis_set(struct hn_softc *, uint32_t,
92                             const void *, size_t);
93 static int              hn_rndis_init(struct hn_softc *);
94 static int              hn_rndis_halt(struct hn_softc *);
95 static int              hn_rndis_conf_offload(struct hn_softc *, int);
96 static int              hn_rndis_query_hwcaps(struct hn_softc *,
97                             struct ndis_offload *);
98
99 static __inline uint32_t
100 hn_rndis_rid(struct hn_softc *sc)
101 {
102         uint32_t rid;
103
104 again:
105         rid = atomic_fetchadd_int(&sc->hn_rndis_rid, 1);
106         if (rid == 0)
107                 goto again;
108
109         /* Use upper 16 bits for non-compat RNDIS messages. */
110         return ((rid & 0xffff) << 16);
111 }
112
113 void
114 hn_rndis_rx_ctrl(struct hn_softc *sc, const void *data, int dlen)
115 {
116         const struct rndis_comp_hdr *comp;
117         const struct rndis_msghdr *hdr;
118
119         KASSERT(dlen >= sizeof(*hdr), ("invalid RNDIS msg\n"));
120         hdr = data;
121
122         switch (hdr->rm_type) {
123         case REMOTE_NDIS_INITIALIZE_CMPLT:
124         case REMOTE_NDIS_QUERY_CMPLT:
125         case REMOTE_NDIS_SET_CMPLT:
126         case REMOTE_NDIS_KEEPALIVE_CMPLT:       /* unused */
127                 if (dlen < sizeof(*comp)) {
128                         if_printf(sc->hn_ifp, "invalid RNDIS cmplt\n");
129                         return;
130                 }
131                 comp = data;
132
133                 KASSERT(comp->rm_rid > HN_RNDIS_RID_COMPAT_MAX,
134                     ("invalid RNDIS rid 0x%08x\n", comp->rm_rid));
135                 vmbus_xact_ctx_wakeup(sc->hn_xact, comp, dlen);
136                 break;
137
138         case REMOTE_NDIS_RESET_CMPLT:
139                 /*
140                  * Reset completed, no rid.
141                  *
142                  * NOTE:
143                  * RESET is not issued by hn(4), so this message should
144                  * _not_ be observed.
145                  */
146                 if_printf(sc->hn_ifp, "RESET cmplt received\n");
147                 break;
148
149         default:
150                 if_printf(sc->hn_ifp, "unknown RNDIS msg 0x%x\n",
151                     hdr->rm_type);
152                 break;
153         }
154 }
155
156 int
157 hn_rndis_get_eaddr(struct hn_softc *sc, uint8_t *eaddr)
158 {
159         size_t eaddr_len;
160         int error;
161
162         eaddr_len = ETHER_ADDR_LEN;
163         error = hn_rndis_query(sc, OID_802_3_PERMANENT_ADDRESS, NULL, 0,
164             eaddr, &eaddr_len);
165         if (error)
166                 return (error);
167         if (eaddr_len != ETHER_ADDR_LEN) {
168                 if_printf(sc->hn_ifp, "invalid eaddr len %zu\n", eaddr_len);
169                 return (EINVAL);
170         }
171         return (0);
172 }
173
174 int
175 hn_rndis_get_linkstatus(struct hn_softc *sc, uint32_t *link_status)
176 {
177         size_t size;
178         int error;
179
180         size = sizeof(*link_status);
181         error = hn_rndis_query(sc, OID_GEN_MEDIA_CONNECT_STATUS, NULL, 0,
182             link_status, &size);
183         if (error)
184                 return (error);
185         if (size != sizeof(uint32_t)) {
186                 if_printf(sc->hn_ifp, "invalid link status len %zu\n", size);
187                 return (EINVAL);
188         }
189         return (0);
190 }
191
192 static const void *
193 hn_rndis_xact_exec1(struct hn_softc *sc, struct vmbus_xact *xact, size_t reqlen,
194     struct hn_nvs_sendctx *sndc, size_t *comp_len)
195 {
196         struct vmbus_gpa gpa[HN_XACT_REQ_PGCNT];
197         int gpa_cnt, error;
198         bus_addr_t paddr;
199
200         KASSERT(reqlen <= HN_XACT_REQ_SIZE && reqlen > 0,
201             ("invalid request length %zu", reqlen));
202
203         /*
204          * Setup the SG list.
205          */
206         paddr = vmbus_xact_req_paddr(xact);
207         KASSERT((paddr & PAGE_MASK) == 0,
208             ("vmbus xact request is not page aligned 0x%jx", (uintmax_t)paddr));
209         for (gpa_cnt = 0; gpa_cnt < HN_XACT_REQ_PGCNT; ++gpa_cnt) {
210                 int len = PAGE_SIZE;
211
212                 if (reqlen == 0)
213                         break;
214                 if (reqlen < len)
215                         len = reqlen;
216
217                 gpa[gpa_cnt].gpa_page = atop(paddr) + gpa_cnt;
218                 gpa[gpa_cnt].gpa_len = len;
219                 gpa[gpa_cnt].gpa_ofs = 0;
220
221                 reqlen -= len;
222         }
223         KASSERT(reqlen == 0, ("still have %zu request data left", reqlen));
224
225         /*
226          * Send this RNDIS control message and wait for its completion
227          * message.
228          */
229         vmbus_xact_activate(xact);
230         error = hn_nvs_send_rndis_ctrl(sc->hn_prichan, sndc, gpa, gpa_cnt);
231         if (error) {
232                 vmbus_xact_deactivate(xact);
233                 if_printf(sc->hn_ifp, "RNDIS ctrl send failed: %d\n", error);
234                 return (NULL);
235         }
236         if (HN_CAN_SLEEP(sc))
237                 return (vmbus_xact_wait(xact, comp_len));
238         else
239                 return (vmbus_xact_busywait(xact, comp_len));
240 }
241
242 static const void *
243 hn_rndis_xact_execute(struct hn_softc *sc, struct vmbus_xact *xact, uint32_t rid,
244     size_t reqlen, size_t *comp_len0, uint32_t comp_type)
245 {
246         const struct rndis_comp_hdr *comp;
247         size_t comp_len, min_complen = *comp_len0;
248
249         KASSERT(rid > HN_RNDIS_RID_COMPAT_MAX, ("invalid rid %u\n", rid));
250         KASSERT(min_complen >= sizeof(*comp),
251             ("invalid minimum complete len %zu", min_complen));
252
253         /*
254          * Execute the xact setup by the caller.
255          */
256         comp = hn_rndis_xact_exec1(sc, xact, reqlen, &hn_nvs_sendctx_none,
257             &comp_len);
258         if (comp == NULL)
259                 return (NULL);
260
261         /*
262          * Check this RNDIS complete message.
263          */
264         if (comp_len < min_complen) {
265                 if (comp_len >= sizeof(*comp)) {
266                         /* rm_status field is valid */
267                         if_printf(sc->hn_ifp, "invalid RNDIS comp len %zu, "
268                             "status 0x%08x\n", comp_len, comp->rm_status);
269                 } else {
270                         if_printf(sc->hn_ifp, "invalid RNDIS comp len %zu\n",
271                             comp_len);
272                 }
273                 return (NULL);
274         }
275         if (comp->rm_len < min_complen) {
276                 if_printf(sc->hn_ifp, "invalid RNDIS comp msglen %u\n",
277                     comp->rm_len);
278                 return (NULL);
279         }
280         if (comp->rm_type != comp_type) {
281                 if_printf(sc->hn_ifp, "unexpected RNDIS comp 0x%08x, "
282                     "expect 0x%08x\n", comp->rm_type, comp_type);
283                 return (NULL);
284         }
285         if (comp->rm_rid != rid) {
286                 if_printf(sc->hn_ifp, "RNDIS comp rid mismatch %u, "
287                     "expect %u\n", comp->rm_rid, rid);
288                 return (NULL);
289         }
290         /* All pass! */
291         *comp_len0 = comp_len;
292         return (comp);
293 }
294
295 static int
296 hn_rndis_query(struct hn_softc *sc, uint32_t oid,
297     const void *idata, size_t idlen, void *odata, size_t *odlen0)
298 {
299
300         return (hn_rndis_query2(sc, oid, idata, idlen, odata, odlen0, *odlen0));
301 }
302
303 static int
304 hn_rndis_query2(struct hn_softc *sc, uint32_t oid,
305     const void *idata, size_t idlen, void *odata, size_t *odlen0,
306     size_t min_odlen)
307 {
308         struct rndis_query_req *req;
309         const struct rndis_query_comp *comp;
310         struct vmbus_xact *xact;
311         size_t reqlen, odlen = *odlen0, comp_len;
312         int error, ofs;
313         uint32_t rid;
314
315         reqlen = sizeof(*req) + idlen;
316         xact = vmbus_xact_get(sc->hn_xact, reqlen);
317         if (xact == NULL) {
318                 if_printf(sc->hn_ifp, "no xact for RNDIS query 0x%08x\n", oid);
319                 return (ENXIO);
320         }
321         rid = hn_rndis_rid(sc);
322         req = vmbus_xact_req_data(xact);
323         req->rm_type = REMOTE_NDIS_QUERY_MSG;
324         req->rm_len = reqlen;
325         req->rm_rid = rid;
326         req->rm_oid = oid;
327         /*
328          * XXX
329          * This is _not_ RNDIS Spec conforming:
330          * "This MUST be set to 0 when there is no input data
331          *  associated with the OID."
332          *
333          * If this field was set to 0 according to the RNDIS Spec,
334          * Hyper-V would set non-SUCCESS status in the query
335          * completion.
336          */
337         req->rm_infobufoffset = RNDIS_QUERY_REQ_INFOBUFOFFSET;
338
339         if (idlen > 0) {
340                 req->rm_infobuflen = idlen;
341                 /* Input data immediately follows RNDIS query. */
342                 memcpy(req + 1, idata, idlen);
343         }
344
345         comp_len = sizeof(*comp) + min_odlen;
346         comp = hn_rndis_xact_execute(sc, xact, rid, reqlen, &comp_len,
347             REMOTE_NDIS_QUERY_CMPLT);
348         if (comp == NULL) {
349                 if_printf(sc->hn_ifp, "exec RNDIS query 0x%08x failed\n", oid);
350                 error = EIO;
351                 goto done;
352         }
353
354         if (comp->rm_status != RNDIS_STATUS_SUCCESS) {
355                 if_printf(sc->hn_ifp, "RNDIS query 0x%08x failed: "
356                     "status 0x%08x\n", oid, comp->rm_status);
357                 error = EIO;
358                 goto done;
359         }
360         if (comp->rm_infobuflen == 0 || comp->rm_infobufoffset == 0) {
361                 /* No output data! */
362                 if_printf(sc->hn_ifp, "RNDIS query 0x%08x, no data\n", oid);
363                 *odlen0 = 0;
364                 error = 0;
365                 goto done;
366         }
367
368         /*
369          * Check output data length and offset.
370          */
371         /* ofs is the offset from the beginning of comp. */
372         ofs = RNDIS_QUERY_COMP_INFOBUFOFFSET_ABS(comp->rm_infobufoffset);
373         if (ofs < sizeof(*comp) || ofs + comp->rm_infobuflen > comp_len) {
374                 if_printf(sc->hn_ifp, "RNDIS query invalid comp ib off/len, "
375                     "%u/%u\n", comp->rm_infobufoffset, comp->rm_infobuflen);
376                 error = EINVAL;
377                 goto done;
378         }
379
380         /*
381          * Save output data.
382          */
383         if (comp->rm_infobuflen < odlen)
384                 odlen = comp->rm_infobuflen;
385         memcpy(odata, ((const uint8_t *)comp) + ofs, odlen);
386         *odlen0 = odlen;
387
388         error = 0;
389 done:
390         vmbus_xact_put(xact);
391         return (error);
392 }
393
394 int
395 hn_rndis_query_rsscaps(struct hn_softc *sc, int *rxr_cnt0)
396 {
397         struct ndis_rss_caps in, caps;
398         size_t caps_len;
399         int error, indsz, rxr_cnt, hash_fnidx;
400         uint32_t hash_func = 0, hash_types = 0;
401
402         *rxr_cnt0 = 0;
403
404         if (sc->hn_ndis_ver < HN_NDIS_VERSION_6_20)
405                 return (EOPNOTSUPP);
406
407         memset(&in, 0, sizeof(in));
408         in.ndis_hdr.ndis_type = NDIS_OBJTYPE_RSS_CAPS;
409         in.ndis_hdr.ndis_rev = NDIS_RSS_CAPS_REV_2;
410         in.ndis_hdr.ndis_size = NDIS_RSS_CAPS_SIZE;
411
412         caps_len = NDIS_RSS_CAPS_SIZE;
413         error = hn_rndis_query2(sc, OID_GEN_RECEIVE_SCALE_CAPABILITIES,
414             &in, NDIS_RSS_CAPS_SIZE, &caps, &caps_len, NDIS_RSS_CAPS_SIZE_6_0);
415         if (error)
416                 return (error);
417
418         /*
419          * Preliminary verification.
420          */
421         if (caps.ndis_hdr.ndis_type != NDIS_OBJTYPE_RSS_CAPS) {
422                 if_printf(sc->hn_ifp, "invalid NDIS objtype 0x%02x\n",
423                     caps.ndis_hdr.ndis_type);
424                 return (EINVAL);
425         }
426         if (caps.ndis_hdr.ndis_rev < NDIS_RSS_CAPS_REV_1) {
427                 if_printf(sc->hn_ifp, "invalid NDIS objrev 0x%02x\n",
428                     caps.ndis_hdr.ndis_rev);
429                 return (EINVAL);
430         }
431         if (caps.ndis_hdr.ndis_size > caps_len) {
432                 if_printf(sc->hn_ifp, "invalid NDIS objsize %u, "
433                     "data size %zu\n", caps.ndis_hdr.ndis_size, caps_len);
434                 return (EINVAL);
435         } else if (caps.ndis_hdr.ndis_size < NDIS_RSS_CAPS_SIZE_6_0) {
436                 if_printf(sc->hn_ifp, "invalid NDIS objsize %u\n",
437                     caps.ndis_hdr.ndis_size);
438                 return (EINVAL);
439         }
440
441         /*
442          * Save information for later RSS configuration.
443          */
444         if (caps.ndis_nrxr == 0) {
445                 if_printf(sc->hn_ifp, "0 RX rings!?\n");
446                 return (EINVAL);
447         }
448         if (bootverbose)
449                 if_printf(sc->hn_ifp, "%u RX rings\n", caps.ndis_nrxr);
450         rxr_cnt = caps.ndis_nrxr;
451
452         if (caps.ndis_hdr.ndis_size == NDIS_RSS_CAPS_SIZE &&
453             caps.ndis_hdr.ndis_rev >= NDIS_RSS_CAPS_REV_2) {
454                 if (caps.ndis_nind > NDIS_HASH_INDCNT) {
455                         if_printf(sc->hn_ifp,
456                             "too many RSS indirect table entries %u\n",
457                             caps.ndis_nind);
458                         return (EOPNOTSUPP);
459                 }
460                 if (!powerof2(caps.ndis_nind)) {
461                         if_printf(sc->hn_ifp, "RSS indirect table size is not "
462                             "power-of-2 %u\n", caps.ndis_nind);
463                 }
464
465                 if (bootverbose) {
466                         if_printf(sc->hn_ifp, "RSS indirect table size %u\n",
467                             caps.ndis_nind);
468                 }
469                 indsz = caps.ndis_nind;
470         } else {
471                 indsz = NDIS_HASH_INDCNT;
472         }
473         if (indsz < rxr_cnt) {
474                 if_printf(sc->hn_ifp, "# of RX rings (%d) > "
475                     "RSS indirect table size %d\n", rxr_cnt, indsz);
476                 rxr_cnt = indsz;
477         }
478
479         /*
480          * NOTE:
481          * Toeplitz is at the lowest bit, and it is prefered; so ffs(),
482          * instead of fls(), is used here.
483          */
484         hash_fnidx = ffs(caps.ndis_caps & NDIS_RSS_CAP_HASHFUNC_MASK);
485         if (hash_fnidx == 0) {
486                 if_printf(sc->hn_ifp, "no hash functions, caps 0x%08x\n",
487                     caps.ndis_caps);
488                 return (EOPNOTSUPP);
489         }
490         hash_func = 1 << (hash_fnidx - 1); /* ffs is 1-based */
491
492         if (caps.ndis_caps & NDIS_RSS_CAP_IPV4)
493                 hash_types |= NDIS_HASH_IPV4 | NDIS_HASH_TCP_IPV4;
494         if (caps.ndis_caps & NDIS_RSS_CAP_IPV6)
495                 hash_types |= NDIS_HASH_IPV6 | NDIS_HASH_TCP_IPV6;
496         if (caps.ndis_caps & NDIS_RSS_CAP_IPV6_EX)
497                 hash_types |= NDIS_HASH_IPV6_EX | NDIS_HASH_TCP_IPV6_EX;
498         if (hash_types == 0) {
499                 if_printf(sc->hn_ifp, "no hash types, caps 0x%08x\n",
500                     caps.ndis_caps);
501                 return (EOPNOTSUPP);
502         }
503
504         /* Commit! */
505         sc->hn_rss_ind_size = indsz;
506         sc->hn_rss_hash = hash_func | hash_types;
507         *rxr_cnt0 = rxr_cnt;
508         return (0);
509 }
510
511 static int
512 hn_rndis_set(struct hn_softc *sc, uint32_t oid, const void *data, size_t dlen)
513 {
514         struct rndis_set_req *req;
515         const struct rndis_set_comp *comp;
516         struct vmbus_xact *xact;
517         size_t reqlen, comp_len;
518         uint32_t rid;
519         int error;
520
521         KASSERT(dlen > 0, ("invalid dlen %zu", dlen));
522
523         reqlen = sizeof(*req) + dlen;
524         xact = vmbus_xact_get(sc->hn_xact, reqlen);
525         if (xact == NULL) {
526                 if_printf(sc->hn_ifp, "no xact for RNDIS set 0x%08x\n", oid);
527                 return (ENXIO);
528         }
529         rid = hn_rndis_rid(sc);
530         req = vmbus_xact_req_data(xact);
531         req->rm_type = REMOTE_NDIS_SET_MSG;
532         req->rm_len = reqlen;
533         req->rm_rid = rid;
534         req->rm_oid = oid;
535         req->rm_infobuflen = dlen;
536         req->rm_infobufoffset = RNDIS_SET_REQ_INFOBUFOFFSET;
537         /* Data immediately follows RNDIS set. */
538         memcpy(req + 1, data, dlen);
539
540         comp_len = sizeof(*comp);
541         comp = hn_rndis_xact_execute(sc, xact, rid, reqlen, &comp_len,
542             REMOTE_NDIS_SET_CMPLT);
543         if (comp == NULL) {
544                 if_printf(sc->hn_ifp, "exec RNDIS set 0x%08x failed\n", oid);
545                 error = EIO;
546                 goto done;
547         }
548
549         if (comp->rm_status != RNDIS_STATUS_SUCCESS) {
550                 if_printf(sc->hn_ifp, "RNDIS set 0x%08x failed: "
551                     "status 0x%08x\n", oid, comp->rm_status);
552                 error = EIO;
553                 goto done;
554         }
555         error = 0;
556 done:
557         vmbus_xact_put(xact);
558         return (error);
559 }
560
561 static int
562 hn_rndis_conf_offload(struct hn_softc *sc, int mtu)
563 {
564         struct ndis_offload hwcaps;
565         struct ndis_offload_params params;
566         uint32_t caps = 0;
567         size_t paramsz;
568         int error, tso_maxsz, tso_minsg;
569
570         error = hn_rndis_query_hwcaps(sc, &hwcaps);
571         if (error) {
572                 if_printf(sc->hn_ifp, "hwcaps query failed: %d\n", error);
573                 return (error);
574         }
575
576         /* NOTE: 0 means "no change" */
577         memset(&params, 0, sizeof(params));
578
579         params.ndis_hdr.ndis_type = NDIS_OBJTYPE_DEFAULT;
580         if (sc->hn_ndis_ver < HN_NDIS_VERSION_6_30) {
581                 params.ndis_hdr.ndis_rev = NDIS_OFFLOAD_PARAMS_REV_2;
582                 paramsz = NDIS_OFFLOAD_PARAMS_SIZE_6_1;
583         } else {
584                 params.ndis_hdr.ndis_rev = NDIS_OFFLOAD_PARAMS_REV_3;
585                 paramsz = NDIS_OFFLOAD_PARAMS_SIZE;
586         }
587         params.ndis_hdr.ndis_size = paramsz;
588
589         /*
590          * TSO4/TSO6 setup.
591          */
592         tso_maxsz = IP_MAXPACKET;
593         tso_minsg = 2;
594         if (hwcaps.ndis_lsov2.ndis_ip4_encap & NDIS_OFFLOAD_ENCAP_8023) {
595                 caps |= HN_CAP_TSO4;
596                 params.ndis_lsov2_ip4 = NDIS_OFFLOAD_LSOV2_ON;
597
598                 if (hwcaps.ndis_lsov2.ndis_ip4_maxsz < tso_maxsz)
599                         tso_maxsz = hwcaps.ndis_lsov2.ndis_ip4_maxsz;
600                 if (hwcaps.ndis_lsov2.ndis_ip4_minsg > tso_minsg)
601                         tso_minsg = hwcaps.ndis_lsov2.ndis_ip4_minsg;
602         }
603         if ((hwcaps.ndis_lsov2.ndis_ip6_encap & NDIS_OFFLOAD_ENCAP_8023) &&
604             (hwcaps.ndis_lsov2.ndis_ip6_opts & HN_NDIS_LSOV2_CAP_IP6) ==
605             HN_NDIS_LSOV2_CAP_IP6) {
606                 caps |= HN_CAP_TSO6;
607                 params.ndis_lsov2_ip6 = NDIS_OFFLOAD_LSOV2_ON;
608
609                 if (hwcaps.ndis_lsov2.ndis_ip6_maxsz < tso_maxsz)
610                         tso_maxsz = hwcaps.ndis_lsov2.ndis_ip6_maxsz;
611                 if (hwcaps.ndis_lsov2.ndis_ip6_minsg > tso_minsg)
612                         tso_minsg = hwcaps.ndis_lsov2.ndis_ip6_minsg;
613         }
614         sc->hn_ndis_tso_szmax = 0;
615         sc->hn_ndis_tso_sgmin = 0;
616         if (caps & (HN_CAP_TSO4 | HN_CAP_TSO6)) {
617                 KASSERT(tso_maxsz <= IP_MAXPACKET,
618                     ("invalid NDIS TSO maxsz %d", tso_maxsz));
619                 KASSERT(tso_minsg >= 2,
620                     ("invalid NDIS TSO minsg %d", tso_minsg));
621                 if (tso_maxsz < tso_minsg * mtu) {
622                         if_printf(sc->hn_ifp, "invalid NDIS TSO config: "
623                             "maxsz %d, minsg %d, mtu %d; "
624                             "disable TSO4 and TSO6\n",
625                             tso_maxsz, tso_minsg, mtu);
626                         caps &= ~(HN_CAP_TSO4 | HN_CAP_TSO6);
627                         params.ndis_lsov2_ip4 = NDIS_OFFLOAD_LSOV2_OFF;
628                         params.ndis_lsov2_ip6 = NDIS_OFFLOAD_LSOV2_OFF;
629                 } else {
630                         sc->hn_ndis_tso_szmax = tso_maxsz;
631                         sc->hn_ndis_tso_sgmin = tso_minsg;
632                         if (bootverbose) {
633                                 if_printf(sc->hn_ifp, "NDIS TSO "
634                                     "szmax %d sgmin %d\n",
635                                     sc->hn_ndis_tso_szmax,
636                                     sc->hn_ndis_tso_sgmin);
637                         }
638                 }
639         }
640
641         /* IPv4 checksum */
642         if ((hwcaps.ndis_csum.ndis_ip4_txcsum & HN_NDIS_TXCSUM_CAP_IP4) ==
643             HN_NDIS_TXCSUM_CAP_IP4) {
644                 caps |= HN_CAP_IPCS;
645                 params.ndis_ip4csum = NDIS_OFFLOAD_PARAM_TX;
646         }
647         if (hwcaps.ndis_csum.ndis_ip4_rxcsum & NDIS_RXCSUM_CAP_IP4) {
648                 if (params.ndis_ip4csum == NDIS_OFFLOAD_PARAM_TX)
649                         params.ndis_ip4csum = NDIS_OFFLOAD_PARAM_TXRX;
650                 else
651                         params.ndis_ip4csum = NDIS_OFFLOAD_PARAM_RX;
652         }
653
654         /* TCP4 checksum */
655         if ((hwcaps.ndis_csum.ndis_ip4_txcsum & HN_NDIS_TXCSUM_CAP_TCP4) ==
656             HN_NDIS_TXCSUM_CAP_TCP4) {
657                 caps |= HN_CAP_TCP4CS;
658                 params.ndis_tcp4csum = NDIS_OFFLOAD_PARAM_TX;
659         }
660         if (hwcaps.ndis_csum.ndis_ip4_rxcsum & NDIS_RXCSUM_CAP_TCP4) {
661                 if (params.ndis_tcp4csum == NDIS_OFFLOAD_PARAM_TX)
662                         params.ndis_tcp4csum = NDIS_OFFLOAD_PARAM_TXRX;
663                 else
664                         params.ndis_tcp4csum = NDIS_OFFLOAD_PARAM_RX;
665         }
666
667         /* UDP4 checksum */
668         if (hwcaps.ndis_csum.ndis_ip4_txcsum & NDIS_TXCSUM_CAP_UDP4) {
669                 caps |= HN_CAP_UDP4CS;
670                 params.ndis_udp4csum = NDIS_OFFLOAD_PARAM_TX;
671         }
672         if (hwcaps.ndis_csum.ndis_ip4_rxcsum & NDIS_RXCSUM_CAP_UDP4) {
673                 if (params.ndis_udp4csum == NDIS_OFFLOAD_PARAM_TX)
674                         params.ndis_udp4csum = NDIS_OFFLOAD_PARAM_TXRX;
675                 else
676                         params.ndis_udp4csum = NDIS_OFFLOAD_PARAM_RX;
677         }
678
679         /* TCP6 checksum */
680         if ((hwcaps.ndis_csum.ndis_ip6_txcsum & HN_NDIS_TXCSUM_CAP_TCP6) ==
681             HN_NDIS_TXCSUM_CAP_TCP6) {
682                 caps |= HN_CAP_TCP6CS;
683                 params.ndis_tcp6csum = NDIS_OFFLOAD_PARAM_TX;
684         }
685         if (hwcaps.ndis_csum.ndis_ip6_rxcsum & NDIS_RXCSUM_CAP_TCP6) {
686                 if (params.ndis_tcp6csum == NDIS_OFFLOAD_PARAM_TX)
687                         params.ndis_tcp6csum = NDIS_OFFLOAD_PARAM_TXRX;
688                 else
689                         params.ndis_tcp6csum = NDIS_OFFLOAD_PARAM_RX;
690         }
691
692         /* UDP6 checksum */
693         if ((hwcaps.ndis_csum.ndis_ip6_txcsum & HN_NDIS_TXCSUM_CAP_UDP6) ==
694             HN_NDIS_TXCSUM_CAP_UDP6) {
695                 caps |= HN_CAP_UDP6CS;
696                 params.ndis_udp6csum = NDIS_OFFLOAD_PARAM_TX;
697         }
698         if (hwcaps.ndis_csum.ndis_ip6_rxcsum & NDIS_RXCSUM_CAP_UDP6) {
699                 if (params.ndis_udp6csum == NDIS_OFFLOAD_PARAM_TX)
700                         params.ndis_udp6csum = NDIS_OFFLOAD_PARAM_TXRX;
701                 else
702                         params.ndis_udp6csum = NDIS_OFFLOAD_PARAM_RX;
703         }
704
705         if (bootverbose) {
706                 if_printf(sc->hn_ifp, "offload csum: "
707                     "ip4 %u, tcp4 %u, udp4 %u, tcp6 %u, udp6 %u\n",
708                     params.ndis_ip4csum,
709                     params.ndis_tcp4csum,
710                     params.ndis_udp4csum,
711                     params.ndis_tcp6csum,
712                     params.ndis_udp6csum);
713                 if_printf(sc->hn_ifp, "offload lsov2: ip4 %u, ip6 %u\n",
714                     params.ndis_lsov2_ip4,
715                     params.ndis_lsov2_ip6);
716         }
717
718         error = hn_rndis_set(sc, OID_TCP_OFFLOAD_PARAMETERS, &params, paramsz);
719         if (error) {
720                 if_printf(sc->hn_ifp, "offload config failed: %d\n", error);
721                 return (error);
722         }
723
724         if (bootverbose)
725                 if_printf(sc->hn_ifp, "offload config done\n");
726         sc->hn_caps |= caps;
727         return (0);
728 }
729
730 int
731 hn_rndis_conf_rss(struct hn_softc *sc, uint16_t flags)
732 {
733         struct ndis_rssprm_toeplitz *rss = &sc->hn_rss;
734         struct ndis_rss_params *prm = &rss->rss_params;
735         int error, rss_size;
736
737         /*
738          * Only NDIS 6.20+ is supported:
739          * We only support 4bytes element in indirect table, which has been
740          * adopted since NDIS 6.20.
741          */
742         KASSERT(sc->hn_ndis_ver >= HN_NDIS_VERSION_6_20,
743             ("NDIS 6.20+ is required, NDIS version 0x%08x", sc->hn_ndis_ver));
744
745         /* XXX only one can be specified through, popcnt? */
746         KASSERT((sc->hn_rss_hash & NDIS_HASH_FUNCTION_MASK), ("no hash func"));
747         KASSERT((sc->hn_rss_hash & NDIS_HASH_TYPE_MASK), ("no hash types"));
748         KASSERT(sc->hn_rss_ind_size > 0, ("no indirect table size"));
749
750         if (bootverbose) {
751                 if_printf(sc->hn_ifp, "RSS indirect table size %d, "
752                     "hash 0x%08x\n", sc->hn_rss_ind_size, sc->hn_rss_hash);
753         }
754
755         /*
756          * NOTE:
757          * DO NOT whack rss_key and rss_ind, which are setup by the caller.
758          */
759         memset(prm, 0, sizeof(*prm));
760         rss_size = NDIS_RSSPRM_TOEPLITZ_SIZE(sc->hn_rss_ind_size);
761
762         prm->ndis_hdr.ndis_type = NDIS_OBJTYPE_RSS_PARAMS;
763         prm->ndis_hdr.ndis_rev = NDIS_RSS_PARAMS_REV_2;
764         prm->ndis_hdr.ndis_size = rss_size;
765         prm->ndis_flags = flags;
766         prm->ndis_hash = sc->hn_rss_hash;
767         prm->ndis_indsize = sizeof(rss->rss_ind[0]) * sc->hn_rss_ind_size;
768         prm->ndis_indoffset =
769             __offsetof(struct ndis_rssprm_toeplitz, rss_ind[0]);
770         prm->ndis_keysize = sizeof(rss->rss_key);
771         prm->ndis_keyoffset =
772             __offsetof(struct ndis_rssprm_toeplitz, rss_key[0]);
773
774         error = hn_rndis_set(sc, OID_GEN_RECEIVE_SCALE_PARAMETERS,
775             rss, rss_size);
776         if (error) {
777                 if_printf(sc->hn_ifp, "RSS config failed: %d\n", error);
778         } else {
779                 if (bootverbose)
780                         if_printf(sc->hn_ifp, "RSS config done\n");
781         }
782         return (error);
783 }
784
785 int
786 hn_rndis_set_rxfilter(struct hn_softc *sc, uint32_t filter)
787 {
788         int error;
789
790         error = hn_rndis_set(sc, OID_GEN_CURRENT_PACKET_FILTER,
791             &filter, sizeof(filter));
792         if (error) {
793                 if_printf(sc->hn_ifp, "set RX filter 0x%08x failed: %d\n",
794                     filter, error);
795         } else {
796                 if (bootverbose) {
797                         if_printf(sc->hn_ifp, "set RX filter 0x%08x done\n",
798                             filter);
799                 }
800         }
801         return (error);
802 }
803
804 static int
805 hn_rndis_init(struct hn_softc *sc)
806 {
807         struct rndis_init_req *req;
808         const struct rndis_init_comp *comp;
809         struct vmbus_xact *xact;
810         size_t comp_len;
811         uint32_t rid;
812         int error;
813
814         xact = vmbus_xact_get(sc->hn_xact, sizeof(*req));
815         if (xact == NULL) {
816                 if_printf(sc->hn_ifp, "no xact for RNDIS init\n");
817                 return (ENXIO);
818         }
819         rid = hn_rndis_rid(sc);
820         req = vmbus_xact_req_data(xact);
821         req->rm_type = REMOTE_NDIS_INITIALIZE_MSG;
822         req->rm_len = sizeof(*req);
823         req->rm_rid = rid;
824         req->rm_ver_major = RNDIS_VERSION_MAJOR;
825         req->rm_ver_minor = RNDIS_VERSION_MINOR;
826         req->rm_max_xfersz = HN_RNDIS_XFER_SIZE;
827
828         comp_len = RNDIS_INIT_COMP_SIZE_MIN;
829         comp = hn_rndis_xact_execute(sc, xact, rid, sizeof(*req), &comp_len,
830             REMOTE_NDIS_INITIALIZE_CMPLT);
831         if (comp == NULL) {
832                 if_printf(sc->hn_ifp, "exec RNDIS init failed\n");
833                 error = EIO;
834                 goto done;
835         }
836
837         if (comp->rm_status != RNDIS_STATUS_SUCCESS) {
838                 if_printf(sc->hn_ifp, "RNDIS init failed: status 0x%08x\n",
839                     comp->rm_status);
840                 error = EIO;
841                 goto done;
842         }
843         sc->hn_rndis_agg_size = comp->rm_pktmaxsz;
844         sc->hn_rndis_agg_pkts = comp->rm_pktmaxcnt;
845         sc->hn_rndis_agg_align = 1U << comp->rm_align;
846
847         if (bootverbose) {
848                 if_printf(sc->hn_ifp, "RNDIS ver %u.%u, pktsz %u, pktcnt %u, "
849                     "align %u\n", comp->rm_ver_major, comp->rm_ver_minor,
850                     sc->hn_rndis_agg_size, sc->hn_rndis_agg_pkts,
851                     sc->hn_rndis_agg_align);
852         }
853         error = 0;
854 done:
855         vmbus_xact_put(xact);
856         return (error);
857 }
858
859 static int
860 hn_rndis_halt(struct hn_softc *sc)
861 {
862         struct vmbus_xact *xact;
863         struct rndis_halt_req *halt;
864         struct hn_nvs_sendctx sndc;
865         size_t comp_len;
866
867         xact = vmbus_xact_get(sc->hn_xact, sizeof(*halt));
868         if (xact == NULL) {
869                 if_printf(sc->hn_ifp, "no xact for RNDIS halt\n");
870                 return (ENXIO);
871         }
872         halt = vmbus_xact_req_data(xact);
873         halt->rm_type = REMOTE_NDIS_HALT_MSG;
874         halt->rm_len = sizeof(*halt);
875         halt->rm_rid = hn_rndis_rid(sc);
876
877         /* No RNDIS completion; rely on NVS message send completion */
878         hn_nvs_sendctx_init(&sndc, hn_nvs_sent_xact, xact);
879         hn_rndis_xact_exec1(sc, xact, sizeof(*halt), &sndc, &comp_len);
880
881         vmbus_xact_put(xact);
882         if (bootverbose)
883                 if_printf(sc->hn_ifp, "RNDIS halt done\n");
884         return (0);
885 }
886
887 static int
888 hn_rndis_query_hwcaps(struct hn_softc *sc, struct ndis_offload *caps)
889 {
890         struct ndis_offload in;
891         size_t caps_len, size;
892         int error;
893
894         memset(&in, 0, sizeof(in));
895         in.ndis_hdr.ndis_type = NDIS_OBJTYPE_OFFLOAD;
896         if (sc->hn_ndis_ver >= HN_NDIS_VERSION_6_30) {
897                 in.ndis_hdr.ndis_rev = NDIS_OFFLOAD_REV_3;
898                 size = NDIS_OFFLOAD_SIZE;
899         } else if (sc->hn_ndis_ver >= HN_NDIS_VERSION_6_1) {
900                 in.ndis_hdr.ndis_rev = NDIS_OFFLOAD_REV_2;
901                 size = NDIS_OFFLOAD_SIZE_6_1;
902         } else {
903                 in.ndis_hdr.ndis_rev = NDIS_OFFLOAD_REV_1;
904                 size = NDIS_OFFLOAD_SIZE_6_0;
905         }
906         in.ndis_hdr.ndis_size = size;
907
908         caps_len = NDIS_OFFLOAD_SIZE;
909         error = hn_rndis_query2(sc, OID_TCP_OFFLOAD_HARDWARE_CAPABILITIES,
910             &in, size, caps, &caps_len, NDIS_OFFLOAD_SIZE_6_0);
911         if (error)
912                 return (error);
913
914         /*
915          * Preliminary verification.
916          */
917         if (caps->ndis_hdr.ndis_type != NDIS_OBJTYPE_OFFLOAD) {
918                 if_printf(sc->hn_ifp, "invalid NDIS objtype 0x%02x\n",
919                     caps->ndis_hdr.ndis_type);
920                 return (EINVAL);
921         }
922         if (caps->ndis_hdr.ndis_rev < NDIS_OFFLOAD_REV_1) {
923                 if_printf(sc->hn_ifp, "invalid NDIS objrev 0x%02x\n",
924                     caps->ndis_hdr.ndis_rev);
925                 return (EINVAL);
926         }
927         if (caps->ndis_hdr.ndis_size > caps_len) {
928                 if_printf(sc->hn_ifp, "invalid NDIS objsize %u, "
929                     "data size %zu\n", caps->ndis_hdr.ndis_size, caps_len);
930                 return (EINVAL);
931         } else if (caps->ndis_hdr.ndis_size < NDIS_OFFLOAD_SIZE_6_0) {
932                 if_printf(sc->hn_ifp, "invalid NDIS objsize %u\n",
933                     caps->ndis_hdr.ndis_size);
934                 return (EINVAL);
935         }
936
937         if (bootverbose) {
938                 /*
939                  * NOTE:
940                  * caps->ndis_hdr.ndis_size MUST be checked before accessing
941                  * NDIS 6.1+ specific fields.
942                  */
943                 if_printf(sc->hn_ifp, "hwcaps rev %u\n",
944                     caps->ndis_hdr.ndis_rev);
945
946                 if_printf(sc->hn_ifp, "hwcaps csum: "
947                     "ip4 tx 0x%x/0x%x rx 0x%x/0x%x, "
948                     "ip6 tx 0x%x/0x%x rx 0x%x/0x%x\n",
949                     caps->ndis_csum.ndis_ip4_txcsum,
950                     caps->ndis_csum.ndis_ip4_txenc,
951                     caps->ndis_csum.ndis_ip4_rxcsum,
952                     caps->ndis_csum.ndis_ip4_rxenc,
953                     caps->ndis_csum.ndis_ip6_txcsum,
954                     caps->ndis_csum.ndis_ip6_txenc,
955                     caps->ndis_csum.ndis_ip6_rxcsum,
956                     caps->ndis_csum.ndis_ip6_rxenc);
957                 if_printf(sc->hn_ifp, "hwcaps lsov2: "
958                     "ip4 maxsz %u minsg %u encap 0x%x, "
959                     "ip6 maxsz %u minsg %u encap 0x%x opts 0x%x\n",
960                     caps->ndis_lsov2.ndis_ip4_maxsz,
961                     caps->ndis_lsov2.ndis_ip4_minsg,
962                     caps->ndis_lsov2.ndis_ip4_encap,
963                     caps->ndis_lsov2.ndis_ip6_maxsz,
964                     caps->ndis_lsov2.ndis_ip6_minsg,
965                     caps->ndis_lsov2.ndis_ip6_encap,
966                     caps->ndis_lsov2.ndis_ip6_opts);
967         }
968         return (0);
969 }
970
971 int
972 hn_rndis_attach(struct hn_softc *sc, int mtu)
973 {
974         int error;
975
976         /*
977          * Initialize RNDIS.
978          */
979         error = hn_rndis_init(sc);
980         if (error)
981                 return (error);
982
983         /*
984          * Configure NDIS offload settings.
985          * XXX no offloading, if error happened?
986          */
987         hn_rndis_conf_offload(sc, mtu);
988         return (0);
989 }
990
991 void
992 hn_rndis_detach(struct hn_softc *sc)
993 {
994
995         /* Halt the RNDIS. */
996         hn_rndis_halt(sc);
997 }