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