1 /*-
2 * Copyright (c) 2009-2012,2016-2017 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 /*
30 * Network Virtualization Service.
31 */
32
33 #include <sys/cdefs.h>
34 #include "opt_inet6.h"
35 #include "opt_inet.h"
36
37 #include <sys/param.h>
38 #include <sys/kernel.h>
39 #include <sys/limits.h>
40 #include <sys/socket.h>
41 #include <sys/systm.h>
42 #include <sys/taskqueue.h>
43
44 #include <net/ethernet.h>
45 #include <net/if.h>
46 #include <net/if_var.h>
47 #include <net/if_media.h>
48
49 #include <netinet/in.h>
50 #include <netinet/tcp_lro.h>
51
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>
56
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
62 static int hn_nvs_conn_chim(struct hn_softc *);
63 static int hn_nvs_conn_rxbuf(struct hn_softc *);
64 static void hn_nvs_disconn_chim(struct hn_softc *);
65 static void hn_nvs_disconn_rxbuf(struct hn_softc *);
66 static int hn_nvs_conf_ndis(struct hn_softc *, int);
67 static int hn_nvs_init_ndis(struct hn_softc *);
68 static int hn_nvs_doinit(struct hn_softc *, uint32_t);
69 static int hn_nvs_init(struct hn_softc *);
70 static const void *hn_nvs_xact_execute(struct hn_softc *,
71 struct vmbus_xact *, void *, int,
72 size_t *, uint32_t);
73 static void hn_nvs_sent_none(struct hn_nvs_sendctx *,
74 struct hn_softc *, struct vmbus_channel *,
75 const void *, int);
76
77 struct hn_nvs_sendctx hn_nvs_sendctx_none =
78 HN_NVS_SENDCTX_INITIALIZER(hn_nvs_sent_none, NULL);
79
80 static const uint32_t hn_nvs_version[] = {
81 HN_NVS_VERSION_61,
82 HN_NVS_VERSION_6,
83 HN_NVS_VERSION_5,
84 HN_NVS_VERSION_4,
85 HN_NVS_VERSION_2,
86 HN_NVS_VERSION_1
87 };
88
89 static const void *
hn_nvs_xact_execute(struct hn_softc * sc,struct vmbus_xact * xact,void * req,int reqlen,size_t * resplen0,uint32_t type)90 hn_nvs_xact_execute(struct hn_softc *sc, struct vmbus_xact *xact,
91 void *req, int reqlen, size_t *resplen0, uint32_t type)
92 {
93 struct hn_nvs_sendctx sndc;
94 size_t resplen, min_resplen = *resplen0;
95 const struct hn_nvs_hdr *hdr;
96 int error;
97
98 KASSERT(min_resplen >= sizeof(*hdr),
99 ("invalid minimum response len %zu", min_resplen));
100
101 /*
102 * Execute the xact setup by the caller.
103 */
104 hn_nvs_sendctx_init(&sndc, hn_nvs_sent_xact, xact);
105
106 vmbus_xact_activate(xact);
107 error = hn_nvs_send(sc->hn_prichan, VMBUS_CHANPKT_FLAG_RC,
108 req, reqlen, &sndc);
109 if (error) {
110 vmbus_xact_deactivate(xact);
111 return (NULL);
112 }
113 hdr = vmbus_chan_xact_wait(sc->hn_prichan, xact, &resplen,
114 HN_CAN_SLEEP(sc));
115
116 /*
117 * Check this NVS response message.
118 */
119 if (resplen < min_resplen) {
120 if_printf(sc->hn_ifp, "invalid NVS resp len %zu\n", resplen);
121 return (NULL);
122 }
123 if (hdr->nvs_type != type) {
124 if_printf(sc->hn_ifp, "unexpected NVS resp 0x%08x, "
125 "expect 0x%08x\n", hdr->nvs_type, type);
126 return (NULL);
127 }
128 /* All pass! */
129 *resplen0 = resplen;
130 return (hdr);
131 }
132
133 static __inline int
hn_nvs_req_send(struct hn_softc * sc,void * req,int reqlen)134 hn_nvs_req_send(struct hn_softc *sc, void *req, int reqlen)
135 {
136
137 return (hn_nvs_send(sc->hn_prichan, VMBUS_CHANPKT_FLAG_NONE,
138 req, reqlen, &hn_nvs_sendctx_none));
139 }
140
141 static int
hn_nvs_conn_rxbuf(struct hn_softc * sc)142 hn_nvs_conn_rxbuf(struct hn_softc *sc)
143 {
144 struct vmbus_xact *xact = NULL;
145 struct hn_nvs_rxbuf_conn *conn;
146 const struct hn_nvs_rxbuf_connresp *resp;
147 size_t resp_len;
148 uint32_t status;
149 int error, rxbuf_size;
150
151 /*
152 * Limit RXBUF size for old NVS.
153 */
154 if (sc->hn_nvs_ver <= HN_NVS_VERSION_2)
155 rxbuf_size = HN_RXBUF_SIZE_COMPAT;
156 else
157 rxbuf_size = HN_RXBUF_SIZE;
158
159 /*
160 * Connect the RXBUF GPADL to the primary channel.
161 *
162 * NOTE:
163 * Only primary channel has RXBUF connected to it. Sub-channels
164 * just share this RXBUF.
165 */
166 error = vmbus_chan_gpadl_connect(sc->hn_prichan,
167 sc->hn_rxbuf_dma.hv_paddr, rxbuf_size, &sc->hn_rxbuf_gpadl);
168 if (error) {
169 if_printf(sc->hn_ifp, "rxbuf gpadl conn failed: %d\n",
170 error);
171 goto cleanup;
172 }
173
174 /*
175 * Connect RXBUF to NVS.
176 */
177
178 xact = vmbus_xact_get(sc->hn_xact, sizeof(*conn));
179 if (xact == NULL) {
180 if_printf(sc->hn_ifp, "no xact for nvs rxbuf conn\n");
181 error = ENXIO;
182 goto cleanup;
183 }
184 conn = vmbus_xact_req_data(xact);
185 conn->nvs_type = HN_NVS_TYPE_RXBUF_CONN;
186 conn->nvs_gpadl = sc->hn_rxbuf_gpadl;
187 conn->nvs_sig = HN_NVS_RXBUF_SIG;
188
189 resp_len = sizeof(*resp);
190 resp = hn_nvs_xact_execute(sc, xact, conn, sizeof(*conn), &resp_len,
191 HN_NVS_TYPE_RXBUF_CONNRESP);
192 if (resp == NULL) {
193 if_printf(sc->hn_ifp, "exec nvs rxbuf conn failed\n");
194 error = EIO;
195 goto cleanup;
196 }
197
198 status = resp->nvs_status;
199 vmbus_xact_put(xact);
200 xact = NULL;
201
202 if (status != HN_NVS_STATUS_OK) {
203 if_printf(sc->hn_ifp, "nvs rxbuf conn failed: %x\n", status);
204 error = EIO;
205 goto cleanup;
206 }
207 sc->hn_flags |= HN_FLAG_RXBUF_CONNECTED;
208
209 return (0);
210
211 cleanup:
212 if (xact != NULL)
213 vmbus_xact_put(xact);
214 hn_nvs_disconn_rxbuf(sc);
215 return (error);
216 }
217
218 static int
hn_nvs_conn_chim(struct hn_softc * sc)219 hn_nvs_conn_chim(struct hn_softc *sc)
220 {
221 struct vmbus_xact *xact = NULL;
222 struct hn_nvs_chim_conn *chim;
223 const struct hn_nvs_chim_connresp *resp;
224 size_t resp_len;
225 uint32_t status, sectsz;
226 int error;
227
228 /*
229 * Connect chimney sending buffer GPADL to the primary channel.
230 *
231 * NOTE:
232 * Only primary channel has chimney sending buffer connected to it.
233 * Sub-channels just share this chimney sending buffer.
234 */
235 error = vmbus_chan_gpadl_connect(sc->hn_prichan,
236 sc->hn_chim_dma.hv_paddr, HN_CHIM_SIZE, &sc->hn_chim_gpadl);
237 if (error) {
238 if_printf(sc->hn_ifp, "chim gpadl conn failed: %d\n", error);
239 goto cleanup;
240 }
241
242 /*
243 * Connect chimney sending buffer to NVS
244 */
245
246 xact = vmbus_xact_get(sc->hn_xact, sizeof(*chim));
247 if (xact == NULL) {
248 if_printf(sc->hn_ifp, "no xact for nvs chim conn\n");
249 error = ENXIO;
250 goto cleanup;
251 }
252 chim = vmbus_xact_req_data(xact);
253 chim->nvs_type = HN_NVS_TYPE_CHIM_CONN;
254 chim->nvs_gpadl = sc->hn_chim_gpadl;
255 chim->nvs_sig = HN_NVS_CHIM_SIG;
256
257 resp_len = sizeof(*resp);
258 resp = hn_nvs_xact_execute(sc, xact, chim, sizeof(*chim), &resp_len,
259 HN_NVS_TYPE_CHIM_CONNRESP);
260 if (resp == NULL) {
261 if_printf(sc->hn_ifp, "exec nvs chim conn failed\n");
262 error = EIO;
263 goto cleanup;
264 }
265
266 status = resp->nvs_status;
267 sectsz = resp->nvs_sectsz;
268 vmbus_xact_put(xact);
269 xact = NULL;
270
271 if (status != HN_NVS_STATUS_OK) {
272 if_printf(sc->hn_ifp, "nvs chim conn failed: %x\n", status);
273 error = EIO;
274 goto cleanup;
275 }
276 if (sectsz == 0 || sectsz % sizeof(uint32_t) != 0) {
277 /*
278 * Can't use chimney sending buffer; done!
279 */
280 if (sectsz == 0) {
281 if_printf(sc->hn_ifp, "zero chimney sending buffer "
282 "section size\n");
283 } else {
284 if_printf(sc->hn_ifp, "misaligned chimney sending "
285 "buffers, section size: %u\n", sectsz);
286 }
287 sc->hn_chim_szmax = 0;
288 sc->hn_chim_cnt = 0;
289 sc->hn_flags |= HN_FLAG_CHIM_CONNECTED;
290 return (0);
291 }
292
293 sc->hn_chim_szmax = sectsz;
294 sc->hn_chim_cnt = HN_CHIM_SIZE / sc->hn_chim_szmax;
295 if (HN_CHIM_SIZE % sc->hn_chim_szmax != 0) {
296 if_printf(sc->hn_ifp, "chimney sending sections are "
297 "not properly aligned\n");
298 }
299 if (sc->hn_chim_cnt % LONG_BIT != 0) {
300 if_printf(sc->hn_ifp, "discard %d chimney sending sections\n",
301 sc->hn_chim_cnt % LONG_BIT);
302 }
303
304 sc->hn_chim_bmap_cnt = sc->hn_chim_cnt / LONG_BIT;
305 sc->hn_chim_bmap = malloc(sc->hn_chim_bmap_cnt * sizeof(u_long),
306 M_DEVBUF, M_WAITOK | M_ZERO);
307
308 /* Done! */
309 sc->hn_flags |= HN_FLAG_CHIM_CONNECTED;
310 if (bootverbose) {
311 if_printf(sc->hn_ifp, "chimney sending buffer %d/%d\n",
312 sc->hn_chim_szmax, sc->hn_chim_cnt);
313 }
314 return (0);
315
316 cleanup:
317 if (xact != NULL)
318 vmbus_xact_put(xact);
319 hn_nvs_disconn_chim(sc);
320 return (error);
321 }
322
323 static void
hn_nvs_disconn_rxbuf(struct hn_softc * sc)324 hn_nvs_disconn_rxbuf(struct hn_softc *sc)
325 {
326 int error;
327
328 if (sc->hn_flags & HN_FLAG_RXBUF_CONNECTED) {
329 struct hn_nvs_rxbuf_disconn disconn;
330
331 /*
332 * Disconnect RXBUF from NVS.
333 */
334 memset(&disconn, 0, sizeof(disconn));
335 disconn.nvs_type = HN_NVS_TYPE_RXBUF_DISCONN;
336 disconn.nvs_sig = HN_NVS_RXBUF_SIG;
337
338 /* NOTE: No response. */
339 error = hn_nvs_req_send(sc, &disconn, sizeof(disconn));
340 if (error) {
341 if_printf(sc->hn_ifp,
342 "send nvs rxbuf disconn failed: %d\n", error);
343 /*
344 * Fine for a revoked channel, since the hypervisor
345 * does not drain TX bufring for a revoked channel.
346 */
347 if (!vmbus_chan_is_revoked(sc->hn_prichan))
348 sc->hn_flags |= HN_FLAG_RXBUF_REF;
349 }
350 sc->hn_flags &= ~HN_FLAG_RXBUF_CONNECTED;
351
352 /*
353 * Wait for the hypervisor to receive this NVS request.
354 *
355 * NOTE:
356 * The TX bufring will not be drained by the hypervisor,
357 * if the primary channel is revoked.
358 */
359 while (!vmbus_chan_tx_empty(sc->hn_prichan) &&
360 !vmbus_chan_is_revoked(sc->hn_prichan))
361 pause("waittx", 1);
362 /*
363 * Linger long enough for NVS to disconnect RXBUF.
364 */
365 pause("lingtx", (200 * hz) / 1000);
366 }
367
368 if (vmbus_current_version < VMBUS_VERSION_WIN10 && sc->hn_rxbuf_gpadl != 0) {
369 /*
370 * Disconnect RXBUF from primary channel.
371 */
372 error = vmbus_chan_gpadl_disconnect(sc->hn_prichan,
373 sc->hn_rxbuf_gpadl);
374 if (error) {
375 if_printf(sc->hn_ifp,
376 "rxbuf gpadl disconn failed: %d\n", error);
377 sc->hn_flags |= HN_FLAG_RXBUF_REF;
378 }
379 sc->hn_rxbuf_gpadl = 0;
380 }
381 }
382
383 static void
hn_nvs_disconn_chim(struct hn_softc * sc)384 hn_nvs_disconn_chim(struct hn_softc *sc)
385 {
386 int error;
387
388 if (sc->hn_flags & HN_FLAG_CHIM_CONNECTED) {
389 struct hn_nvs_chim_disconn disconn;
390
391 /*
392 * Disconnect chimney sending buffer from NVS.
393 */
394 memset(&disconn, 0, sizeof(disconn));
395 disconn.nvs_type = HN_NVS_TYPE_CHIM_DISCONN;
396 disconn.nvs_sig = HN_NVS_CHIM_SIG;
397
398 /* NOTE: No response. */
399 error = hn_nvs_req_send(sc, &disconn, sizeof(disconn));
400 if (error) {
401 if_printf(sc->hn_ifp,
402 "send nvs chim disconn failed: %d\n", error);
403 /*
404 * Fine for a revoked channel, since the hypervisor
405 * does not drain TX bufring for a revoked channel.
406 */
407 if (!vmbus_chan_is_revoked(sc->hn_prichan))
408 sc->hn_flags |= HN_FLAG_CHIM_REF;
409 }
410 sc->hn_flags &= ~HN_FLAG_CHIM_CONNECTED;
411
412 /*
413 * Wait for the hypervisor to receive this NVS request.
414 *
415 * NOTE:
416 * The TX bufring will not be drained by the hypervisor,
417 * if the primary channel is revoked.
418 */
419 while (!vmbus_chan_tx_empty(sc->hn_prichan) &&
420 !vmbus_chan_is_revoked(sc->hn_prichan))
421 pause("waittx", 1);
422 /*
423 * Linger long enough for NVS to disconnect chimney
424 * sending buffer.
425 */
426 pause("lingtx", (200 * hz) / 1000);
427 }
428
429 if (vmbus_current_version < VMBUS_VERSION_WIN10 && sc->hn_chim_gpadl != 0) {
430 /*
431 * Disconnect chimney sending buffer from primary channel.
432 */
433 error = vmbus_chan_gpadl_disconnect(sc->hn_prichan,
434 sc->hn_chim_gpadl);
435 if (error) {
436 if_printf(sc->hn_ifp,
437 "chim gpadl disconn failed: %d\n", error);
438 sc->hn_flags |= HN_FLAG_CHIM_REF;
439 }
440 sc->hn_chim_gpadl = 0;
441 }
442
443 if (sc->hn_chim_bmap != NULL) {
444 free(sc->hn_chim_bmap, M_DEVBUF);
445 sc->hn_chim_bmap = NULL;
446 sc->hn_chim_bmap_cnt = 0;
447 }
448 }
449
450 static int
hn_nvs_doinit(struct hn_softc * sc,uint32_t nvs_ver)451 hn_nvs_doinit(struct hn_softc *sc, uint32_t nvs_ver)
452 {
453 struct vmbus_xact *xact;
454 struct hn_nvs_init *init;
455 const struct hn_nvs_init_resp *resp;
456 size_t resp_len;
457 uint32_t status;
458
459 xact = vmbus_xact_get(sc->hn_xact, sizeof(*init));
460 if (xact == NULL) {
461 if_printf(sc->hn_ifp, "no xact for nvs init\n");
462 return (ENXIO);
463 }
464 init = vmbus_xact_req_data(xact);
465 init->nvs_type = HN_NVS_TYPE_INIT;
466 init->nvs_ver_min = nvs_ver;
467 init->nvs_ver_max = nvs_ver;
468
469 resp_len = sizeof(*resp);
470 resp = hn_nvs_xact_execute(sc, xact, init, sizeof(*init), &resp_len,
471 HN_NVS_TYPE_INIT_RESP);
472 if (resp == NULL) {
473 if_printf(sc->hn_ifp, "exec init failed\n");
474 vmbus_xact_put(xact);
475 return (EIO);
476 }
477
478 status = resp->nvs_status;
479 vmbus_xact_put(xact);
480
481 if (status != HN_NVS_STATUS_OK) {
482 if (bootverbose) {
483 /*
484 * Caller may try another NVS version, and will log
485 * error if there are no more NVS versions to try,
486 * so don't bark out loud here.
487 */
488 if_printf(sc->hn_ifp, "nvs init failed for ver 0x%x\n",
489 nvs_ver);
490 }
491 return (EINVAL);
492 }
493 return (0);
494 }
495
496 /*
497 * Configure MTU and enable VLAN.
498 */
499 static int
hn_nvs_conf_ndis(struct hn_softc * sc,int mtu)500 hn_nvs_conf_ndis(struct hn_softc *sc, int mtu)
501 {
502 struct hn_nvs_ndis_conf conf;
503 int error;
504
505 memset(&conf, 0, sizeof(conf));
506 conf.nvs_type = HN_NVS_TYPE_NDIS_CONF;
507 conf.nvs_mtu = mtu + ETHER_HDR_LEN;
508 conf.nvs_caps = HN_NVS_NDIS_CONF_VLAN;
509 if (sc->hn_nvs_ver >= HN_NVS_VERSION_5)
510 conf.nvs_caps |= HN_NVS_NDIS_CONF_SRIOV;
511 if (sc->hn_nvs_ver >= HN_NVS_VERSION_61)
512 conf.nvs_caps |= HN_NVS_NDIS_CONF_RSC;
513
514
515 /* NOTE: No response. */
516 error = hn_nvs_req_send(sc, &conf, sizeof(conf));
517 if (error) {
518 if_printf(sc->hn_ifp, "send nvs ndis conf failed: %d\n", error);
519 return (error);
520 }
521
522 if (bootverbose)
523 if_printf(sc->hn_ifp, "nvs ndis conf done\n");
524 sc->hn_caps |= HN_CAP_MTU | HN_CAP_VLAN;
525 return (0);
526 }
527
528 static int
hn_nvs_init_ndis(struct hn_softc * sc)529 hn_nvs_init_ndis(struct hn_softc *sc)
530 {
531 struct hn_nvs_ndis_init ndis;
532 int error;
533
534 memset(&ndis, 0, sizeof(ndis));
535 ndis.nvs_type = HN_NVS_TYPE_NDIS_INIT;
536 ndis.nvs_ndis_major = HN_NDIS_VERSION_MAJOR(sc->hn_ndis_ver);
537 ndis.nvs_ndis_minor = HN_NDIS_VERSION_MINOR(sc->hn_ndis_ver);
538
539 /* NOTE: No response. */
540 error = hn_nvs_req_send(sc, &ndis, sizeof(ndis));
541 if (error)
542 if_printf(sc->hn_ifp, "send nvs ndis init failed: %d\n", error);
543 return (error);
544 }
545
546 static int
hn_nvs_init(struct hn_softc * sc)547 hn_nvs_init(struct hn_softc *sc)
548 {
549 int i, error;
550
551 if (device_is_attached(sc->hn_dev)) {
552 /*
553 * NVS version and NDIS version MUST NOT be changed.
554 */
555 if (bootverbose) {
556 if_printf(sc->hn_ifp, "reinit NVS version 0x%x, "
557 "NDIS version %u.%u\n", sc->hn_nvs_ver,
558 HN_NDIS_VERSION_MAJOR(sc->hn_ndis_ver),
559 HN_NDIS_VERSION_MINOR(sc->hn_ndis_ver));
560 }
561
562 error = hn_nvs_doinit(sc, sc->hn_nvs_ver);
563 if (error) {
564 if_printf(sc->hn_ifp, "reinit NVS version 0x%x "
565 "failed: %d\n", sc->hn_nvs_ver, error);
566 return (error);
567 }
568 goto done;
569 }
570
571 /*
572 * Find the supported NVS version and set NDIS version accordingly.
573 */
574 for (i = 0; i < nitems(hn_nvs_version); ++i) {
575 error = hn_nvs_doinit(sc, hn_nvs_version[i]);
576 if (!error) {
577 sc->hn_nvs_ver = hn_nvs_version[i];
578
579 /* Set NDIS version according to NVS version. */
580 sc->hn_ndis_ver = HN_NDIS_VERSION_6_30;
581 if (sc->hn_nvs_ver <= HN_NVS_VERSION_4)
582 sc->hn_ndis_ver = HN_NDIS_VERSION_6_1;
583
584 if (bootverbose) {
585 if_printf(sc->hn_ifp, "NVS version 0x%x, "
586 "NDIS version %u.%u\n", sc->hn_nvs_ver,
587 HN_NDIS_VERSION_MAJOR(sc->hn_ndis_ver),
588 HN_NDIS_VERSION_MINOR(sc->hn_ndis_ver));
589 }
590 goto done;
591 }
592 }
593 if_printf(sc->hn_ifp, "no NVS available\n");
594 return (ENXIO);
595
596 done:
597 if (sc->hn_nvs_ver >= HN_NVS_VERSION_5)
598 sc->hn_caps |= HN_CAP_HASHVAL;
599 return (0);
600 }
601
602 int
hn_nvs_attach(struct hn_softc * sc,int mtu)603 hn_nvs_attach(struct hn_softc *sc, int mtu)
604 {
605 int error;
606
607 if (hyperv_ver_major >= 10) {
608 /* UDP 4-tuple hash is enforced. */
609 sc->hn_caps |= HN_CAP_UDPHASH;
610 }
611
612 /*
613 * Initialize NVS.
614 */
615 error = hn_nvs_init(sc);
616 if (error)
617 return (error);
618
619 if (sc->hn_nvs_ver >= HN_NVS_VERSION_2) {
620 /*
621 * Configure NDIS before initializing it.
622 */
623 error = hn_nvs_conf_ndis(sc, mtu);
624 if (error)
625 return (error);
626 }
627
628 /*
629 * Initialize NDIS.
630 */
631 error = hn_nvs_init_ndis(sc);
632 if (error)
633 return (error);
634
635 /*
636 * Connect RXBUF.
637 */
638 error = hn_nvs_conn_rxbuf(sc);
639 if (error)
640 return (error);
641
642 /*
643 * Connect chimney sending buffer.
644 */
645 error = hn_nvs_conn_chim(sc);
646 if (error) {
647 hn_nvs_disconn_rxbuf(sc);
648 return (error);
649 }
650 return (0);
651 }
652
653 void
hn_nvs_detach(struct hn_softc * sc)654 hn_nvs_detach(struct hn_softc *sc)
655 {
656
657 /* NOTE: there are no requests to stop the NVS. */
658 hn_nvs_disconn_rxbuf(sc);
659 hn_nvs_disconn_chim(sc);
660 }
661
662 void
hn_nvs_sent_xact(struct hn_nvs_sendctx * sndc,struct hn_softc * sc __unused,struct vmbus_channel * chan __unused,const void * data,int dlen)663 hn_nvs_sent_xact(struct hn_nvs_sendctx *sndc,
664 struct hn_softc *sc __unused, struct vmbus_channel *chan __unused,
665 const void *data, int dlen)
666 {
667
668 vmbus_xact_wakeup(sndc->hn_cbarg, data, dlen);
669 }
670
671 static void
hn_nvs_sent_none(struct hn_nvs_sendctx * sndc __unused,struct hn_softc * sc __unused,struct vmbus_channel * chan __unused,const void * data __unused,int dlen __unused)672 hn_nvs_sent_none(struct hn_nvs_sendctx *sndc __unused,
673 struct hn_softc *sc __unused, struct vmbus_channel *chan __unused,
674 const void *data __unused, int dlen __unused)
675 {
676 /* EMPTY */
677 }
678
679 int
hn_nvs_alloc_subchans(struct hn_softc * sc,int * nsubch0)680 hn_nvs_alloc_subchans(struct hn_softc *sc, int *nsubch0)
681 {
682 struct vmbus_xact *xact;
683 struct hn_nvs_subch_req *req;
684 const struct hn_nvs_subch_resp *resp;
685 int error, nsubch_req;
686 uint32_t nsubch;
687 size_t resp_len;
688
689 nsubch_req = *nsubch0;
690 KASSERT(nsubch_req > 0, ("invalid # of sub-channels %d", nsubch_req));
691
692 xact = vmbus_xact_get(sc->hn_xact, sizeof(*req));
693 if (xact == NULL) {
694 if_printf(sc->hn_ifp, "no xact for nvs subch alloc\n");
695 return (ENXIO);
696 }
697 req = vmbus_xact_req_data(xact);
698 req->nvs_type = HN_NVS_TYPE_SUBCH_REQ;
699 req->nvs_op = HN_NVS_SUBCH_OP_ALLOC;
700 req->nvs_nsubch = nsubch_req;
701
702 resp_len = sizeof(*resp);
703 resp = hn_nvs_xact_execute(sc, xact, req, sizeof(*req), &resp_len,
704 HN_NVS_TYPE_SUBCH_RESP);
705 if (resp == NULL) {
706 if_printf(sc->hn_ifp, "exec nvs subch alloc failed\n");
707 error = EIO;
708 goto done;
709 }
710 if (resp->nvs_status != HN_NVS_STATUS_OK) {
711 if_printf(sc->hn_ifp, "nvs subch alloc failed: %x\n",
712 resp->nvs_status);
713 error = EIO;
714 goto done;
715 }
716
717 nsubch = resp->nvs_nsubch;
718 if (nsubch > nsubch_req) {
719 if_printf(sc->hn_ifp, "%u subchans are allocated, "
720 "requested %d\n", nsubch, nsubch_req);
721 nsubch = nsubch_req;
722 }
723 *nsubch0 = nsubch;
724 error = 0;
725 done:
726 vmbus_xact_put(xact);
727 return (error);
728 }
729
730 int
hn_nvs_send_rndis_ctrl(struct vmbus_channel * chan,struct hn_nvs_sendctx * sndc,struct vmbus_gpa * gpa,int gpa_cnt)731 hn_nvs_send_rndis_ctrl(struct vmbus_channel *chan,
732 struct hn_nvs_sendctx *sndc, struct vmbus_gpa *gpa, int gpa_cnt)
733 {
734
735 return hn_nvs_send_rndis_sglist(chan, HN_NVS_RNDIS_MTYPE_CTRL,
736 sndc, gpa, gpa_cnt);
737 }
738
739 void
hn_nvs_set_datapath(struct hn_softc * sc,uint32_t path)740 hn_nvs_set_datapath(struct hn_softc *sc, uint32_t path)
741 {
742 struct hn_nvs_datapath dp;
743
744 memset(&dp, 0, sizeof(dp));
745 dp.nvs_type = HN_NVS_TYPE_SET_DATAPATH;
746 dp.nvs_active_path = path;
747
748 hn_nvs_req_send(sc, &dp, sizeof(dp));
749 }
750