if_hvn.c revision 1.6 1 /* $NetBSD: if_hvn.c,v 1.6 2019/11/15 08:51:27 nonaka Exp $ */
2 /* $OpenBSD: if_hvn.c,v 1.39 2018/03/11 14:31:34 mikeb Exp $ */
3
4 /*-
5 * Copyright (c) 2009-2012,2016 Microsoft Corp.
6 * Copyright (c) 2010-2012 Citrix Inc.
7 * Copyright (c) 2012 NetApp Inc.
8 * Copyright (c) 2016 Mike Belopuhov <mike (at) esdenera.com>
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice unmodified, this list of conditions, and the following
16 * disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * The OpenBSD port was done under funding by Esdenera Networks GmbH.
35 */
36
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: if_hvn.c,v 1.6 2019/11/15 08:51:27 nonaka Exp $");
39
40 #ifdef _KERNEL_OPT
41 #include "opt_inet.h"
42 #include "opt_inet6.h"
43 #include "opt_net_mpsafe.h"
44 #endif
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/device.h>
50 #include <sys/atomic.h>
51 #include <sys/bus.h>
52 #include <sys/intr.h>
53 #include <sys/kmem.h>
54
55 #include <net/if.h>
56 #include <net/if_ether.h>
57 #include <net/if_media.h>
58
59 #include <net/bpf.h>
60
61 #include <dev/ic/ndisreg.h>
62 #include <dev/ic/rndisreg.h>
63
64 #include <dev/hyperv/vmbusvar.h>
65 #include <dev/hyperv/if_hvnreg.h>
66
67 #ifndef EVL_PRIO_BITS
68 #define EVL_PRIO_BITS 13
69 #endif
70
71 #define HVN_NVS_MSGSIZE 32
72 #define HVN_NVS_BUFSIZE PAGE_SIZE
73
74 /*
75 * RNDIS control interface
76 */
77 #define HVN_RNDIS_CTLREQS 4
78 #define HVN_RNDIS_BUFSIZE 512
79
80 struct rndis_cmd {
81 uint32_t rc_id;
82 struct hvn_nvs_rndis rc_msg;
83 void *rc_req;
84 bus_dmamap_t rc_dmap;
85 bus_dma_segment_t rc_segs;
86 int rc_nsegs;
87 uint64_t rc_gpa;
88 struct rndis_packet_msg rc_cmp;
89 uint32_t rc_cmplen;
90 uint8_t rc_cmpbuf[HVN_RNDIS_BUFSIZE];
91 int rc_done;
92 TAILQ_ENTRY(rndis_cmd) rc_entry;
93 };
94 TAILQ_HEAD(rndis_queue, rndis_cmd);
95
96 #define HVN_MAXMTU (9 * 1024)
97
98 #define HVN_RNDIS_XFER_SIZE 2048
99
100 /*
101 * Tx ring
102 */
103 #define HVN_TX_DESC 256
104 #define HVN_TX_FRAGS 15 /* 31 is the max */
105 #define HVN_TX_FRAG_SIZE PAGE_SIZE
106 #define HVN_TX_PKT_SIZE 16384
107
108 #define HVN_RNDIS_PKT_LEN \
109 (sizeof(struct rndis_packet_msg) + \
110 sizeof(struct rndis_pktinfo) + NDIS_VLAN_INFO_SIZE + \
111 sizeof(struct rndis_pktinfo) + NDIS_TXCSUM_INFO_SIZE)
112
113 struct hvn_tx_desc {
114 uint32_t txd_id;
115 int txd_ready;
116 struct vmbus_gpa txd_sgl[HVN_TX_FRAGS + 1];
117 int txd_nsge;
118 struct mbuf *txd_buf;
119 bus_dmamap_t txd_dmap;
120 struct vmbus_gpa txd_gpa;
121 struct rndis_packet_msg *txd_req;
122 };
123
124 struct hvn_softc {
125 device_t sc_dev;
126
127 struct vmbus_softc *sc_vmbus;
128 struct vmbus_channel *sc_chan;
129 bus_dma_tag_t sc_dmat;
130
131 struct ethercom sc_ec;
132 struct ifmedia sc_media;
133 struct if_percpuq *sc_ipq;
134 int sc_link_state;
135 int sc_promisc;
136
137 uint32_t sc_flags;
138 #define HVN_SCF_ATTACHED __BIT(0)
139
140 /* NVS protocol */
141 int sc_proto;
142 uint32_t sc_nvstid;
143 uint8_t sc_nvsrsp[HVN_NVS_MSGSIZE];
144 uint8_t *sc_nvsbuf;
145 int sc_nvsdone;
146
147 /* RNDIS protocol */
148 int sc_ndisver;
149 uint32_t sc_rndisrid;
150 struct rndis_queue sc_cntl_sq; /* submission queue */
151 kmutex_t sc_cntl_sqlck;
152 struct rndis_queue sc_cntl_cq; /* completion queue */
153 kmutex_t sc_cntl_cqlck;
154 struct rndis_queue sc_cntl_fq; /* free queue */
155 kmutex_t sc_cntl_fqlck;
156 struct rndis_cmd sc_cntl_msgs[HVN_RNDIS_CTLREQS];
157 struct hvn_nvs_rndis sc_data_msg;
158
159 /* Rx ring */
160 uint8_t *sc_rx_ring;
161 int sc_rx_size;
162 uint32_t sc_rx_hndl;
163 struct hyperv_dma sc_rx_dma;
164
165 /* Tx ring */
166 uint32_t sc_tx_next;
167 uint32_t sc_tx_avail;
168 struct hvn_tx_desc sc_tx_desc[HVN_TX_DESC];
169 bus_dmamap_t sc_tx_rmap;
170 uint8_t *sc_tx_msgs;
171 bus_dma_segment_t sc_tx_mseg;
172 };
173
174 #define SC2IFP(_sc_) (&(_sc_)->sc_ec.ec_if)
175 #define IFP2SC(_ifp_) ((_ifp_)->if_softc)
176
177
178 static int hvn_match(device_t, cfdata_t, void *);
179 static void hvn_attach(device_t, device_t, void *);
180 static int hvn_detach(device_t, int);
181
182 CFATTACH_DECL_NEW(hvn, sizeof(struct hvn_softc),
183 hvn_match, hvn_attach, hvn_detach, NULL);
184
185 static int hvn_ioctl(struct ifnet *, u_long, void *);
186 static int hvn_media_change(struct ifnet *);
187 static void hvn_media_status(struct ifnet *, struct ifmediareq *);
188 static int hvn_iff(struct hvn_softc *);
189 static int hvn_init(struct ifnet *);
190 static void hvn_stop(struct ifnet *, int);
191 static void hvn_start(struct ifnet *);
192 static int hvn_encap(struct hvn_softc *, struct mbuf *,
193 struct hvn_tx_desc **);
194 static void hvn_decap(struct hvn_softc *, struct hvn_tx_desc *);
195 static void hvn_txeof(struct hvn_softc *, uint64_t);
196 static int hvn_rx_ring_create(struct hvn_softc *);
197 static int hvn_rx_ring_destroy(struct hvn_softc *);
198 static int hvn_tx_ring_create(struct hvn_softc *);
199 static void hvn_tx_ring_destroy(struct hvn_softc *);
200 static int hvn_set_capabilities(struct hvn_softc *);
201 static int hvn_get_lladdr(struct hvn_softc *, uint8_t *);
202 static void hvn_get_link_status(struct hvn_softc *);
203
204 /* NSVP */
205 static int hvn_nvs_attach(struct hvn_softc *);
206 static void hvn_nvs_intr(void *);
207 static int hvn_nvs_cmd(struct hvn_softc *, void *, size_t, uint64_t, int);
208 static int hvn_nvs_ack(struct hvn_softc *, uint64_t);
209 static void hvn_nvs_detach(struct hvn_softc *);
210
211 /* RNDIS */
212 static int hvn_rndis_attach(struct hvn_softc *);
213 static int hvn_rndis_cmd(struct hvn_softc *, struct rndis_cmd *, int);
214 static void hvn_rndis_input(struct hvn_softc *, uint64_t, void *);
215 static void hvn_rxeof(struct hvn_softc *, uint8_t *, uint32_t);
216 static void hvn_rndis_complete(struct hvn_softc *, uint8_t *, uint32_t);
217 static int hvn_rndis_output(struct hvn_softc *, struct hvn_tx_desc *);
218 static void hvn_rndis_status(struct hvn_softc *, uint8_t *, uint32_t);
219 static int hvn_rndis_query(struct hvn_softc *, uint32_t, void *, size_t *);
220 static int hvn_rndis_set(struct hvn_softc *, uint32_t, void *, size_t);
221 static int hvn_rndis_open(struct hvn_softc *);
222 static int hvn_rndis_close(struct hvn_softc *);
223 static void hvn_rndis_detach(struct hvn_softc *);
224
225 static int
226 hvn_match(device_t parent, cfdata_t match, void *aux)
227 {
228 struct vmbus_attach_args *aa = aux;
229
230 if (memcmp(aa->aa_type, &hyperv_guid_network, sizeof(*aa->aa_type)))
231 return 0;
232 return 1;
233 }
234
235 static void
236 hvn_attach(device_t parent, device_t self, void *aux)
237 {
238 struct hvn_softc *sc = device_private(self);
239 struct vmbus_attach_args *aa = aux;
240 struct ifnet *ifp = SC2IFP(sc);
241 uint8_t enaddr[ETHER_ADDR_LEN];
242 int error;
243
244 sc->sc_dev = self;
245 sc->sc_vmbus = (struct vmbus_softc *)device_private(parent);
246 sc->sc_chan = aa->aa_chan;
247 sc->sc_dmat = sc->sc_vmbus->sc_dmat;
248
249 aprint_naive("\n");
250 aprint_normal(": Hyper-V NetVSC\n");
251
252 strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
253
254 if (hvn_nvs_attach(sc)) {
255 aprint_error_dev(self, "failed to init NVSP\n");
256 return;
257 }
258
259 if (hvn_rx_ring_create(sc)) {
260 aprint_error_dev(self, "failed to create Rx ring\n");
261 goto fail1;
262 }
263
264 if (hvn_tx_ring_create(sc)) {
265 aprint_error_dev(self, "failed to create Tx ring\n");
266 goto fail1;
267 }
268
269 ifp->if_softc = sc;
270 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
271 ifp->if_ioctl = hvn_ioctl;
272 ifp->if_start = hvn_start;
273 ifp->if_init = hvn_init;
274 ifp->if_stop = hvn_stop;
275 ifp->if_capabilities = IFCAP_CSUM_IPv4_Tx | IFCAP_CSUM_IPv4_Rx;
276 ifp->if_capabilities |= IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx;
277 ifp->if_capabilities |= IFCAP_CSUM_TCPv6_Tx | IFCAP_CSUM_TCPv6_Rx;
278 if (sc->sc_ndisver > NDIS_VERSION_6_30) {
279 ifp->if_capabilities |= IFCAP_CSUM_UDPv4_Tx;
280 ifp->if_capabilities |= IFCAP_CSUM_UDPv4_Rx;
281 ifp->if_capabilities |= IFCAP_CSUM_UDPv6_Tx;
282 ifp->if_capabilities |= IFCAP_CSUM_UDPv6_Rx;
283 }
284 if (sc->sc_proto >= HVN_NVS_PROTO_VERSION_2) {
285 sc->sc_ec.ec_capabilities |= ETHERCAP_VLAN_HWTAGGING;
286 sc->sc_ec.ec_capabilities |= ETHERCAP_VLAN_MTU;
287 sc->sc_ec.ec_capenable |= ETHERCAP_VLAN_HWTAGGING;
288 }
289
290 IFQ_SET_MAXLEN(&ifp->if_snd, HVN_TX_DESC - 1);
291 IFQ_SET_READY(&ifp->if_snd);
292
293 /* Initialize ifmedia structures. */
294 sc->sc_ec.ec_ifmedia = &sc->sc_media;
295 ifmedia_init(&sc->sc_media, IFM_IMASK, hvn_media_change,
296 hvn_media_status);
297 ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_MANUAL, 0, NULL);
298 ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_MANUAL);
299
300 error = if_initialize(ifp);
301 if (error) {
302 aprint_error_dev(self, "if_initialize failed(%d)\n", error);
303 goto fail2;
304 }
305 sc->sc_ipq = if_percpuq_create(ifp);
306 if_deferred_start_init(ifp, NULL);
307
308 if (hvn_rndis_attach(sc)) {
309 aprint_error_dev(self, "failed to init RNDIS\n");
310 goto fail1;
311 }
312
313 aprint_normal_dev(self, "NVS %d.%d NDIS %d.%d\n",
314 sc->sc_proto >> 16, sc->sc_proto & 0xffff,
315 sc->sc_ndisver >> 16 , sc->sc_ndisver & 0xffff);
316
317 if (hvn_set_capabilities(sc)) {
318 aprint_error_dev(self, "failed to setup offloading\n");
319 goto fail2;
320 }
321
322 if (hvn_get_lladdr(sc, enaddr)) {
323 aprint_error_dev(self,
324 "failed to obtain an ethernet address\n");
325 goto fail2;
326 }
327 aprint_normal_dev(self, "Ethernet address %s\n", ether_sprintf(enaddr));
328
329 ether_ifattach(ifp, enaddr);
330 if_register(ifp);
331
332 if (pmf_device_register(self, NULL, NULL))
333 pmf_class_network_register(self, ifp);
334 else
335 aprint_error_dev(self, "couldn't establish power handler\n");
336
337 SET(sc->sc_flags, HVN_SCF_ATTACHED);
338 return;
339
340 fail2: hvn_rndis_detach(sc);
341 fail1: hvn_rx_ring_destroy(sc);
342 hvn_tx_ring_destroy(sc);
343 hvn_nvs_detach(sc);
344 }
345
346 static int
347 hvn_detach(device_t self, int flags)
348 {
349 struct hvn_softc *sc = device_private(self);
350 struct ifnet *ifp = SC2IFP(sc);
351
352 if (!ISSET(sc->sc_flags, HVN_SCF_ATTACHED))
353 return 0;
354
355 hvn_stop(ifp, 1);
356
357 pmf_device_deregister(self);
358
359 ether_ifdetach(ifp);
360 if_detach(ifp);
361 if_percpuq_destroy(sc->sc_ipq);
362
363 hvn_rndis_detach(sc);
364 hvn_rx_ring_destroy(sc);
365 hvn_tx_ring_destroy(sc);
366 hvn_nvs_detach(sc);
367
368 return 0;
369 }
370
371 static int
372 hvn_ioctl(struct ifnet *ifp, u_long command, void * data)
373 {
374 struct hvn_softc *sc = IFP2SC(ifp);
375 int s, error = 0;
376
377 s = splnet();
378
379 switch (command) {
380 case SIOCSIFFLAGS:
381 if (ifp->if_flags & IFF_UP) {
382 if (ifp->if_flags & IFF_RUNNING)
383 error = ENETRESET;
384 else {
385 error = hvn_init(ifp);
386 if (error)
387 ifp->if_flags &= ~IFF_UP;
388 }
389 } else {
390 if (ifp->if_flags & IFF_RUNNING)
391 hvn_stop(ifp, 1);
392 }
393 break;
394 default:
395 error = ether_ioctl(ifp, command, data);
396 break;
397 }
398
399 if (error == ENETRESET) {
400 if (ifp->if_flags & IFF_RUNNING)
401 hvn_iff(sc);
402 error = 0;
403 }
404
405 splx(s);
406
407 return error;
408 }
409
410 static int
411 hvn_media_change(struct ifnet *ifp)
412 {
413
414 return 0;
415 }
416
417 static void
418 hvn_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
419 {
420 struct hvn_softc *sc = IFP2SC(ifp);
421 int link_state;
422
423 link_state = sc->sc_link_state;
424 hvn_get_link_status(sc);
425 if (link_state != sc->sc_link_state)
426 if_link_state_change(ifp, sc->sc_link_state);
427
428 ifmr->ifm_status = IFM_AVALID;
429 ifmr->ifm_active = IFM_ETHER | IFM_MANUAL;
430 if (sc->sc_link_state == LINK_STATE_UP)
431 ifmr->ifm_status |= IFM_ACTIVE;
432 }
433
434 static int
435 hvn_iff(struct hvn_softc *sc)
436 {
437
438 /* XXX */
439 sc->sc_promisc = 0;
440
441 return 0;
442 }
443
444 static int
445 hvn_init(struct ifnet *ifp)
446 {
447 struct hvn_softc *sc = IFP2SC(ifp);
448 int error;
449
450 hvn_stop(ifp, 0);
451
452 error = hvn_iff(sc);
453 if (error)
454 return error;
455
456 error = hvn_rndis_open(sc);
457 if (error == 0) {
458 ifp->if_flags |= IFF_RUNNING;
459 ifp->if_flags &= ~IFF_OACTIVE;
460 }
461 return error;
462 }
463
464 static void
465 hvn_stop(struct ifnet *ifp, int disable)
466 {
467 struct hvn_softc *sc = IFP2SC(ifp);
468
469 hvn_rndis_close(sc);
470
471 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
472 }
473
474 static void
475 hvn_start(struct ifnet *ifp)
476 {
477 struct hvn_softc *sc = IFP2SC(ifp);
478 struct hvn_tx_desc *txd;
479 struct mbuf *m;
480
481 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
482 return;
483
484 for (;;) {
485 if (!sc->sc_tx_avail) {
486 /* transient */
487 ifp->if_flags |= IFF_OACTIVE;
488 break;
489 }
490
491 IFQ_DEQUEUE(&ifp->if_snd, m);
492 if (m == NULL)
493 break;
494
495 if (hvn_encap(sc, m, &txd)) {
496 /* the chain is too large */
497 ifp->if_oerrors++;
498 m_freem(m);
499 continue;
500 }
501
502 bpf_mtap(ifp, m, BPF_D_OUT);
503
504 if (hvn_rndis_output(sc, txd)) {
505 hvn_decap(sc, txd);
506 ifp->if_oerrors++;
507 m_freem(m);
508 continue;
509 }
510
511 sc->sc_tx_next++;
512 }
513 }
514
515 static inline char *
516 hvn_rndis_pktinfo_append(struct rndis_packet_msg *pkt, size_t pktsize,
517 size_t datalen, uint32_t type)
518 {
519 struct rndis_pktinfo *pi;
520 size_t pi_size = sizeof(*pi) + datalen;
521 char *cp;
522
523 KASSERT(pkt->rm_pktinfooffset + pkt->rm_pktinfolen + pi_size <=
524 pktsize);
525
526 cp = (char *)pkt + pkt->rm_pktinfooffset + pkt->rm_pktinfolen;
527 pi = (struct rndis_pktinfo *)cp;
528 pi->rm_size = pi_size;
529 pi->rm_type = type;
530 pi->rm_pktinfooffset = sizeof(*pi);
531 pkt->rm_pktinfolen += pi_size;
532 pkt->rm_dataoffset += pi_size;
533 pkt->rm_len += pi_size;
534
535 return (char *)pi->rm_data;
536 }
537
538 static int
539 hvn_encap(struct hvn_softc *sc, struct mbuf *m, struct hvn_tx_desc **txd0)
540 {
541 struct hvn_tx_desc *txd;
542 struct rndis_packet_msg *pkt;
543 bus_dma_segment_t *seg;
544 size_t pktlen;
545 int i, rv;
546
547 do {
548 txd = &sc->sc_tx_desc[sc->sc_tx_next % HVN_TX_DESC];
549 sc->sc_tx_next++;
550 } while (!txd->txd_ready);
551 txd->txd_ready = 0;
552
553 pkt = txd->txd_req;
554 memset(pkt, 0, HVN_RNDIS_PKT_LEN);
555 pkt->rm_type = REMOTE_NDIS_PACKET_MSG;
556 pkt->rm_len = sizeof(*pkt) + m->m_pkthdr.len;
557 pkt->rm_dataoffset = RNDIS_DATA_OFFSET;
558 pkt->rm_datalen = m->m_pkthdr.len;
559 pkt->rm_pktinfooffset = sizeof(*pkt); /* adjusted below */
560 pkt->rm_pktinfolen = 0;
561
562 rv = bus_dmamap_load_mbuf(sc->sc_dmat, txd->txd_dmap, m, BUS_DMA_READ |
563 BUS_DMA_NOWAIT);
564 switch (rv) {
565 case 0:
566 break;
567 case EFBIG:
568 if (m_defrag(m, M_NOWAIT) == 0 &&
569 bus_dmamap_load_mbuf(sc->sc_dmat, txd->txd_dmap, m,
570 BUS_DMA_READ | BUS_DMA_NOWAIT) == 0)
571 break;
572 /* FALLTHROUGH */
573 default:
574 DPRINTF("%s: failed to load mbuf\n", device_xname(sc->sc_dev));
575 return -1;
576 }
577 txd->txd_buf = m;
578
579 if (m->m_flags & M_VLANTAG) {
580 uint32_t vlan;
581 char *cp;
582
583 vlan = NDIS_VLAN_INFO_MAKE(
584 EVL_VLANOFTAG(m->m_pkthdr.ether_vtag),
585 EVL_PRIOFTAG(m->m_pkthdr.ether_vtag), 0);
586 cp = hvn_rndis_pktinfo_append(pkt, HVN_RNDIS_PKT_LEN,
587 NDIS_VLAN_INFO_SIZE, NDIS_PKTINFO_TYPE_VLAN);
588 memcpy(cp, &vlan, NDIS_VLAN_INFO_SIZE);
589 }
590
591 if (m->m_pkthdr.csum_flags & (M_CSUM_IPv4 | M_CSUM_UDPv4 |
592 M_CSUM_TCPv4)) {
593 uint32_t csum = NDIS_TXCSUM_INFO_IPV4;
594 char *cp;
595
596 if (m->m_pkthdr.csum_flags & M_CSUM_IPv4)
597 csum |= NDIS_TXCSUM_INFO_IPCS;
598 if (m->m_pkthdr.csum_flags & M_CSUM_TCPv4)
599 csum |= NDIS_TXCSUM_INFO_TCPCS;
600 if (m->m_pkthdr.csum_flags & M_CSUM_UDPv4)
601 csum |= NDIS_TXCSUM_INFO_UDPCS;
602 cp = hvn_rndis_pktinfo_append(pkt, HVN_RNDIS_PKT_LEN,
603 NDIS_TXCSUM_INFO_SIZE, NDIS_PKTINFO_TYPE_CSUM);
604 memcpy(cp, &csum, NDIS_TXCSUM_INFO_SIZE);
605 }
606
607 pktlen = pkt->rm_pktinfooffset + pkt->rm_pktinfolen;
608 pkt->rm_pktinfooffset -= RNDIS_HEADER_OFFSET;
609
610 /* Attach an RNDIS message to the first slot */
611 txd->txd_sgl[0].gpa_page = txd->txd_gpa.gpa_page;
612 txd->txd_sgl[0].gpa_ofs = txd->txd_gpa.gpa_ofs;
613 txd->txd_sgl[0].gpa_len = pktlen;
614 txd->txd_nsge = txd->txd_dmap->dm_nsegs + 1;
615
616 for (i = 0; i < txd->txd_dmap->dm_nsegs; i++) {
617 seg = &txd->txd_dmap->dm_segs[i];
618 txd->txd_sgl[1 + i].gpa_page = atop(seg->ds_addr);
619 txd->txd_sgl[1 + i].gpa_ofs = seg->ds_addr & PAGE_MASK;
620 txd->txd_sgl[1 + i].gpa_len = seg->ds_len;
621 }
622
623 *txd0 = txd;
624
625 atomic_dec_uint(&sc->sc_tx_avail);
626
627 return 0;
628 }
629
630 static void
631 hvn_decap(struct hvn_softc *sc, struct hvn_tx_desc *txd)
632 {
633 struct ifnet *ifp = SC2IFP(sc);
634
635 bus_dmamap_sync(sc->sc_dmat, txd->txd_dmap, 0, 0,
636 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
637 bus_dmamap_unload(sc->sc_dmat, txd->txd_dmap);
638 txd->txd_buf = NULL;
639 txd->txd_nsge = 0;
640 txd->txd_ready = 1;
641 atomic_inc_uint(&sc->sc_tx_avail);
642 ifp->if_flags &= ~IFF_OACTIVE;
643 }
644
645 static void
646 hvn_txeof(struct hvn_softc *sc, uint64_t tid)
647 {
648 struct ifnet *ifp = SC2IFP(sc);
649 struct hvn_tx_desc *txd;
650 struct mbuf *m;
651 uint32_t id = tid >> 32;
652
653 if ((tid & 0xffffffffU) != 0)
654 return;
655
656 id -= HVN_NVS_CHIM_SIG;
657 if (id >= HVN_TX_DESC) {
658 device_printf(sc->sc_dev, "tx packet index too large: %u", id);
659 return;
660 }
661
662 txd = &sc->sc_tx_desc[id];
663
664 if ((m = txd->txd_buf) == NULL) {
665 device_printf(sc->sc_dev, "no mbuf @%u\n", id);
666 return;
667 }
668 txd->txd_buf = NULL;
669
670 bus_dmamap_sync(sc->sc_dmat, txd->txd_dmap, 0, 0,
671 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
672 bus_dmamap_unload(sc->sc_dmat, txd->txd_dmap);
673 m_freem(m);
674 ifp->if_opackets++;
675
676 txd->txd_ready = 1;
677
678 atomic_inc_uint(&sc->sc_tx_avail);
679 ifp->if_flags &= ~IFF_OACTIVE;
680 }
681
682 static int
683 hvn_rx_ring_create(struct hvn_softc *sc)
684 {
685 struct hvn_nvs_rxbuf_conn cmd;
686 struct hvn_nvs_rxbuf_conn_resp *rsp;
687 uint64_t tid;
688
689 if (sc->sc_proto <= HVN_NVS_PROTO_VERSION_2)
690 sc->sc_rx_size = 15 * 1024 * 1024; /* 15MB */
691 else
692 sc->sc_rx_size = 16 * 1024 * 1024; /* 16MB */
693 sc->sc_rx_ring = hyperv_dma_alloc(sc->sc_dmat, &sc->sc_rx_dma,
694 sc->sc_rx_size, PAGE_SIZE, PAGE_SIZE, sc->sc_rx_size / PAGE_SIZE);
695 if (sc->sc_rx_ring == NULL) {
696 DPRINTF("%s: failed to allocate Rx ring buffer\n",
697 device_xname(sc->sc_dev));
698 return -1;
699 }
700 if (vmbus_handle_alloc(sc->sc_chan, &sc->sc_rx_dma, sc->sc_rx_size,
701 &sc->sc_rx_hndl)) {
702 DPRINTF("%s: failed to obtain a PA handle\n",
703 device_xname(sc->sc_dev));
704 goto errout;
705 }
706
707 memset(&cmd, 0, sizeof(cmd));
708 cmd.nvs_type = HVN_NVS_TYPE_RXBUF_CONN;
709 cmd.nvs_gpadl = sc->sc_rx_hndl;
710 cmd.nvs_sig = HVN_NVS_RXBUF_SIG;
711
712 tid = atomic_inc_uint_nv(&sc->sc_nvstid);
713 if (hvn_nvs_cmd(sc, &cmd, sizeof(cmd), tid, 100))
714 goto errout;
715
716 rsp = (struct hvn_nvs_rxbuf_conn_resp *)&sc->sc_nvsrsp;
717 if (rsp->nvs_status != HVN_NVS_STATUS_OK) {
718 DPRINTF("%s: failed to set up the Rx ring\n",
719 device_xname(sc->sc_dev));
720 goto errout;
721 }
722 if (rsp->nvs_nsect > 1) {
723 DPRINTF("%s: invalid number of Rx ring sections: %u\n",
724 device_xname(sc->sc_dev), rsp->nvs_nsect);
725 hvn_rx_ring_destroy(sc);
726 return -1;
727 }
728 return 0;
729
730 errout:
731 if (sc->sc_rx_hndl) {
732 vmbus_handle_free(sc->sc_chan, sc->sc_rx_hndl);
733 sc->sc_rx_hndl = 0;
734 }
735 if (sc->sc_rx_ring) {
736 kmem_free(sc->sc_rx_ring, sc->sc_rx_size);
737 sc->sc_rx_ring = NULL;
738 }
739 return -1;
740 }
741
742 static int
743 hvn_rx_ring_destroy(struct hvn_softc *sc)
744 {
745 struct hvn_nvs_rxbuf_disconn cmd;
746 uint64_t tid;
747
748 if (sc->sc_rx_ring == NULL)
749 return 0;
750
751 memset(&cmd, 0, sizeof(cmd));
752 cmd.nvs_type = HVN_NVS_TYPE_RXBUF_DISCONN;
753 cmd.nvs_sig = HVN_NVS_RXBUF_SIG;
754
755 tid = atomic_inc_uint_nv(&sc->sc_nvstid);
756 if (hvn_nvs_cmd(sc, &cmd, sizeof(cmd), tid, 0))
757 return -1;
758
759 delay(100);
760
761 vmbus_handle_free(sc->sc_chan, sc->sc_rx_hndl);
762
763 sc->sc_rx_hndl = 0;
764
765 kmem_free(sc->sc_rx_ring, sc->sc_rx_size);
766 sc->sc_rx_ring = NULL;
767
768 return 0;
769 }
770
771 static int
772 hvn_tx_ring_create(struct hvn_softc *sc)
773 {
774 const int dmaflags = cold ? BUS_DMA_NOWAIT : BUS_DMA_WAITOK;
775 struct hvn_tx_desc *txd;
776 bus_dma_segment_t *seg;
777 size_t msgsize;
778 int i, rsegs;
779 paddr_t pa;
780
781 msgsize = roundup(HVN_RNDIS_PKT_LEN, 128);
782
783 /* Allocate memory to store RNDIS messages */
784 if (bus_dmamem_alloc(sc->sc_dmat, msgsize * HVN_TX_DESC, PAGE_SIZE, 0,
785 &sc->sc_tx_mseg, 1, &rsegs, dmaflags)) {
786 DPRINTF("%s: failed to allocate memory for RDNIS messages\n",
787 device_xname(sc->sc_dev));
788 goto errout;
789 }
790 if (bus_dmamem_map(sc->sc_dmat, &sc->sc_tx_mseg, 1, msgsize *
791 HVN_TX_DESC, (void **)&sc->sc_tx_msgs, dmaflags)) {
792 DPRINTF("%s: failed to establish mapping for RDNIS messages\n",
793 device_xname(sc->sc_dev));
794 goto errout;
795 }
796 memset(sc->sc_tx_msgs, 0, msgsize * HVN_TX_DESC);
797 if (bus_dmamap_create(sc->sc_dmat, msgsize * HVN_TX_DESC, 1,
798 msgsize * HVN_TX_DESC, 0, dmaflags, &sc->sc_tx_rmap)) {
799 DPRINTF("%s: failed to create map for RDNIS messages\n",
800 device_xname(sc->sc_dev));
801 goto errout;
802 }
803 if (bus_dmamap_load(sc->sc_dmat, sc->sc_tx_rmap, sc->sc_tx_msgs,
804 msgsize * HVN_TX_DESC, NULL, dmaflags)) {
805 DPRINTF("%s: failed to create map for RDNIS messages\n",
806 device_xname(sc->sc_dev));
807 goto errout;
808 }
809
810 for (i = 0; i < HVN_TX_DESC; i++) {
811 txd = &sc->sc_tx_desc[i];
812 if (bus_dmamap_create(sc->sc_dmat, HVN_TX_PKT_SIZE,
813 HVN_TX_FRAGS, HVN_TX_FRAG_SIZE, PAGE_SIZE, dmaflags,
814 &txd->txd_dmap)) {
815 DPRINTF("%s: failed to create map for TX descriptors\n",
816 device_xname(sc->sc_dev));
817 goto errout;
818 }
819 seg = &sc->sc_tx_rmap->dm_segs[0];
820 pa = seg->ds_addr + (msgsize * i);
821 txd->txd_gpa.gpa_page = atop(pa);
822 txd->txd_gpa.gpa_ofs = pa & PAGE_MASK;
823 txd->txd_gpa.gpa_len = msgsize;
824 txd->txd_req = (void *)(sc->sc_tx_msgs + (msgsize * i));
825 txd->txd_id = i + HVN_NVS_CHIM_SIG;
826 txd->txd_ready = 1;
827 }
828 sc->sc_tx_avail = HVN_TX_DESC;
829
830 return 0;
831
832 errout:
833 hvn_tx_ring_destroy(sc);
834 return -1;
835 }
836
837 static void
838 hvn_tx_ring_destroy(struct hvn_softc *sc)
839 {
840 struct hvn_tx_desc *txd;
841 int i;
842
843 for (i = 0; i < HVN_TX_DESC; i++) {
844 txd = &sc->sc_tx_desc[i];
845 if (txd->txd_dmap == NULL)
846 continue;
847 bus_dmamap_sync(sc->sc_dmat, txd->txd_dmap, 0, 0,
848 BUS_DMASYNC_POSTWRITE);
849 bus_dmamap_unload(sc->sc_dmat, txd->txd_dmap);
850 bus_dmamap_destroy(sc->sc_dmat, txd->txd_dmap);
851 txd->txd_dmap = NULL;
852 if (txd->txd_buf == NULL)
853 continue;
854 m_free(txd->txd_buf);
855 txd->txd_buf = NULL;
856 }
857 if (sc->sc_tx_rmap) {
858 bus_dmamap_sync(sc->sc_dmat, sc->sc_tx_rmap, 0, 0,
859 BUS_DMASYNC_POSTWRITE);
860 bus_dmamap_unload(sc->sc_dmat, sc->sc_tx_rmap);
861 bus_dmamap_destroy(sc->sc_dmat, sc->sc_tx_rmap);
862 }
863 if (sc->sc_tx_msgs) {
864 size_t msgsize = roundup(HVN_RNDIS_PKT_LEN, 128);
865
866 bus_dmamem_unmap(sc->sc_dmat, sc->sc_tx_msgs,
867 msgsize * HVN_TX_DESC);
868 bus_dmamem_free(sc->sc_dmat, &sc->sc_tx_mseg, 1);
869 }
870 sc->sc_tx_rmap = NULL;
871 sc->sc_tx_msgs = NULL;
872 }
873
874 static int
875 hvn_get_lladdr(struct hvn_softc *sc, uint8_t *enaddr)
876 {
877 size_t addrlen = ETHER_ADDR_LEN;
878 int rv;
879
880 rv = hvn_rndis_query(sc, OID_802_3_PERMANENT_ADDRESS, enaddr, &addrlen);
881 if (rv == 0 && addrlen != ETHER_ADDR_LEN)
882 rv = -1;
883 return rv;
884 }
885
886 static void
887 hvn_get_link_status(struct hvn_softc *sc)
888 {
889 uint32_t state;
890 size_t len = sizeof(state);
891
892 if (hvn_rndis_query(sc, OID_GEN_MEDIA_CONNECT_STATUS,
893 &state, &len) == 0)
894 sc->sc_link_state = (state == NDIS_MEDIA_STATE_CONNECTED) ?
895 LINK_STATE_UP : LINK_STATE_DOWN;
896 }
897
898 static int
899 hvn_nvs_attach(struct hvn_softc *sc)
900 {
901 static const uint32_t protos[] = {
902 HVN_NVS_PROTO_VERSION_5,
903 HVN_NVS_PROTO_VERSION_4,
904 HVN_NVS_PROTO_VERSION_2,
905 HVN_NVS_PROTO_VERSION_1
906 };
907 struct hvn_nvs_init cmd;
908 struct hvn_nvs_init_resp *rsp;
909 struct hvn_nvs_ndis_init ncmd;
910 struct hvn_nvs_ndis_conf ccmd;
911 uint32_t ndisver, ringsize;
912 uint64_t tid;
913 int i;
914
915 sc->sc_nvsbuf = kmem_zalloc(HVN_NVS_BUFSIZE, KM_SLEEP);
916
917 /* We need to be able to fit all RNDIS control and data messages */
918 ringsize = HVN_RNDIS_CTLREQS *
919 (sizeof(struct hvn_nvs_rndis) + sizeof(struct vmbus_gpa)) +
920 HVN_TX_DESC * (sizeof(struct hvn_nvs_rndis) +
921 (HVN_TX_FRAGS + 1) * sizeof(struct vmbus_gpa));
922
923 sc->sc_chan->ch_flags &= ~CHF_BATCHED;
924
925 /* Associate our interrupt handler with the channel */
926 if (vmbus_channel_open(sc->sc_chan, ringsize, NULL, 0,
927 hvn_nvs_intr, sc)) {
928 DPRINTF("%s: failed to open channel\n",
929 device_xname(sc->sc_dev));
930 kmem_free(sc->sc_nvsbuf, HVN_NVS_BUFSIZE);
931 return -1;
932 }
933
934 memset(&cmd, 0, sizeof(cmd));
935 cmd.nvs_type = HVN_NVS_TYPE_INIT;
936 for (i = 0; i < __arraycount(protos); i++) {
937 cmd.nvs_ver_min = cmd.nvs_ver_max = protos[i];
938 tid = atomic_inc_uint_nv(&sc->sc_nvstid);
939 if (hvn_nvs_cmd(sc, &cmd, sizeof(cmd), tid, 100))
940 return -1;
941
942 rsp = (struct hvn_nvs_init_resp *)&sc->sc_nvsrsp;
943 if (rsp->nvs_status == HVN_NVS_STATUS_OK) {
944 sc->sc_proto = protos[i];
945 break;
946 }
947 }
948 if (i == __arraycount(protos)) {
949 DPRINTF("%s: failed to negotiate NVSP version\n",
950 device_xname(sc->sc_dev));
951 return -1;
952 }
953
954 if (sc->sc_proto >= HVN_NVS_PROTO_VERSION_2) {
955 memset(&ccmd, 0, sizeof(ccmd));
956 ccmd.nvs_type = HVN_NVS_TYPE_NDIS_CONF;
957 ccmd.nvs_mtu = HVN_MAXMTU;
958 ccmd.nvs_caps = HVN_NVS_NDIS_CONF_VLAN;
959
960 tid = atomic_inc_uint_nv(&sc->sc_nvstid);
961 if (hvn_nvs_cmd(sc, &ccmd, sizeof(ccmd), tid, 100))
962 return -1;
963 }
964
965 memset(&ncmd, 0, sizeof(ncmd));
966 ncmd.nvs_type = HVN_NVS_TYPE_NDIS_INIT;
967 if (sc->sc_proto <= HVN_NVS_PROTO_VERSION_4)
968 ndisver = NDIS_VERSION_6_1;
969 else
970 ndisver = NDIS_VERSION_6_30;
971 ncmd.nvs_ndis_major = (ndisver & 0xffff0000) >> 16;
972 ncmd.nvs_ndis_minor = ndisver & 0x0000ffff;
973
974 tid = atomic_inc_uint_nv(&sc->sc_nvstid);
975 if (hvn_nvs_cmd(sc, &ncmd, sizeof(ncmd), tid, 100))
976 return -1;
977
978 sc->sc_ndisver = ndisver;
979
980 return 0;
981 }
982
983 static void
984 hvn_nvs_intr(void *arg)
985 {
986 struct hvn_softc *sc = arg;
987 struct ifnet *ifp = SC2IFP(sc);
988 struct vmbus_chanpkt_hdr *cph;
989 const struct hvn_nvs_hdr *nvs;
990 uint64_t rid;
991 uint32_t rlen;
992 int rv;
993 bool dotx = false;
994
995 for (;;) {
996 rv = vmbus_channel_recv(sc->sc_chan, sc->sc_nvsbuf,
997 HVN_NVS_BUFSIZE, &rlen, &rid, 1);
998 if (rv != 0 || rlen == 0) {
999 if (rv != EAGAIN)
1000 device_printf(sc->sc_dev,
1001 "failed to receive an NVSP packet\n");
1002 break;
1003 }
1004 cph = (struct vmbus_chanpkt_hdr *)sc->sc_nvsbuf;
1005 nvs = (const struct hvn_nvs_hdr *)VMBUS_CHANPKT_CONST_DATA(cph);
1006
1007 if (cph->cph_type == VMBUS_CHANPKT_TYPE_COMP) {
1008 switch (nvs->nvs_type) {
1009 case HVN_NVS_TYPE_INIT_RESP:
1010 case HVN_NVS_TYPE_RXBUF_CONNRESP:
1011 case HVN_NVS_TYPE_CHIM_CONNRESP:
1012 case HVN_NVS_TYPE_SUBCH_RESP:
1013 /* copy the response back */
1014 memcpy(&sc->sc_nvsrsp, nvs, HVN_NVS_MSGSIZE);
1015 sc->sc_nvsdone = 1;
1016 wakeup(&sc->sc_nvsrsp);
1017 break;
1018 case HVN_NVS_TYPE_RNDIS_ACK:
1019 dotx = true;
1020 hvn_txeof(sc, cph->cph_tid);
1021 break;
1022 default:
1023 device_printf(sc->sc_dev,
1024 "unhandled NVSP packet type %u "
1025 "on completion\n", nvs->nvs_type);
1026 break;
1027 }
1028 } else if (cph->cph_type == VMBUS_CHANPKT_TYPE_RXBUF) {
1029 switch (nvs->nvs_type) {
1030 case HVN_NVS_TYPE_RNDIS:
1031 hvn_rndis_input(sc, cph->cph_tid, cph);
1032 break;
1033 default:
1034 device_printf(sc->sc_dev,
1035 "unhandled NVSP packet type %u "
1036 "on receive\n", nvs->nvs_type);
1037 break;
1038 }
1039 } else
1040 device_printf(sc->sc_dev,
1041 "unknown NVSP packet type %u\n", cph->cph_type);
1042 }
1043
1044 if (dotx)
1045 if_schedule_deferred_start(ifp);
1046 }
1047
1048 static int
1049 hvn_nvs_cmd(struct hvn_softc *sc, void *cmd, size_t cmdsize, uint64_t tid,
1050 int timo)
1051 {
1052 struct hvn_nvs_hdr *hdr = cmd;
1053 int tries = 10;
1054 int rv, s;
1055
1056 sc->sc_nvsdone = 0;
1057
1058 do {
1059 rv = vmbus_channel_send(sc->sc_chan, cmd, cmdsize,
1060 tid, VMBUS_CHANPKT_TYPE_INBAND,
1061 timo ? VMBUS_CHANPKT_FLAG_RC : 0);
1062 if (rv == EAGAIN) {
1063 if (cold)
1064 delay(1000);
1065 else
1066 tsleep(cmd, PRIBIO, "nvsout", 1);
1067 } else if (rv) {
1068 DPRINTF("%s: NVSP operation %u send error %d\n",
1069 device_xname(sc->sc_dev), hdr->nvs_type, rv);
1070 return rv;
1071 }
1072 } while (rv != 0 && --tries > 0);
1073
1074 if (tries == 0 && rv != 0) {
1075 device_printf(sc->sc_dev,
1076 "NVSP operation %u send error %d\n", hdr->nvs_type, rv);
1077 return rv;
1078 }
1079
1080 if (timo == 0)
1081 return 0;
1082
1083 do {
1084 if (cold)
1085 delay(1000);
1086 else
1087 tsleep(sc, PRIBIO | PCATCH, "nvscmd", 1);
1088 s = splnet();
1089 hvn_nvs_intr(sc);
1090 splx(s);
1091 } while (--timo > 0 && sc->sc_nvsdone != 1);
1092
1093 if (timo == 0 && sc->sc_nvsdone != 1) {
1094 device_printf(sc->sc_dev, "NVSP operation %u timed out\n",
1095 hdr->nvs_type);
1096 return ETIMEDOUT;
1097 }
1098 return 0;
1099 }
1100
1101 static int
1102 hvn_nvs_ack(struct hvn_softc *sc, uint64_t tid)
1103 {
1104 struct hvn_nvs_rndis_ack cmd;
1105 int tries = 5;
1106 int rv;
1107
1108 cmd.nvs_type = HVN_NVS_TYPE_RNDIS_ACK;
1109 cmd.nvs_status = HVN_NVS_STATUS_OK;
1110 do {
1111 rv = vmbus_channel_send(sc->sc_chan, &cmd, sizeof(cmd),
1112 tid, VMBUS_CHANPKT_TYPE_COMP, 0);
1113 if (rv == EAGAIN)
1114 delay(10);
1115 else if (rv) {
1116 DPRINTF("%s: NVSP acknowledgement error %d\n",
1117 device_xname(sc->sc_dev), rv);
1118 return rv;
1119 }
1120 } while (rv != 0 && --tries > 0);
1121 return rv;
1122 }
1123
1124 static void
1125 hvn_nvs_detach(struct hvn_softc *sc)
1126 {
1127
1128 if (vmbus_channel_close(sc->sc_chan) == 0) {
1129 kmem_free(sc->sc_nvsbuf, HVN_NVS_BUFSIZE);
1130 sc->sc_nvsbuf = NULL;
1131 }
1132 }
1133
1134 static inline struct rndis_cmd *
1135 hvn_alloc_cmd(struct hvn_softc *sc)
1136 {
1137 struct rndis_cmd *rc;
1138
1139 mutex_enter(&sc->sc_cntl_fqlck);
1140 while ((rc = TAILQ_FIRST(&sc->sc_cntl_fq)) == NULL)
1141 /* XXX use condvar(9) instead of mtsleep */
1142 mtsleep(&sc->sc_cntl_fq, PRIBIO, "nvsalloc", 1,
1143 &sc->sc_cntl_fqlck);
1144 TAILQ_REMOVE(&sc->sc_cntl_fq, rc, rc_entry);
1145 mutex_exit(&sc->sc_cntl_fqlck);
1146 return rc;
1147 }
1148
1149 static inline void
1150 hvn_submit_cmd(struct hvn_softc *sc, struct rndis_cmd *rc)
1151 {
1152
1153 mutex_enter(&sc->sc_cntl_sqlck);
1154 TAILQ_INSERT_TAIL(&sc->sc_cntl_sq, rc, rc_entry);
1155 mutex_exit(&sc->sc_cntl_sqlck);
1156 }
1157
1158 static inline struct rndis_cmd *
1159 hvn_complete_cmd(struct hvn_softc *sc, uint32_t id)
1160 {
1161 struct rndis_cmd *rc;
1162
1163 mutex_enter(&sc->sc_cntl_sqlck);
1164 TAILQ_FOREACH(rc, &sc->sc_cntl_sq, rc_entry) {
1165 if (rc->rc_id == id) {
1166 TAILQ_REMOVE(&sc->sc_cntl_sq, rc, rc_entry);
1167 break;
1168 }
1169 }
1170 mutex_exit(&sc->sc_cntl_sqlck);
1171 if (rc != NULL) {
1172 mutex_enter(&sc->sc_cntl_cqlck);
1173 TAILQ_INSERT_TAIL(&sc->sc_cntl_cq, rc, rc_entry);
1174 mutex_exit(&sc->sc_cntl_cqlck);
1175 }
1176 return rc;
1177 }
1178
1179 static inline void
1180 hvn_release_cmd(struct hvn_softc *sc, struct rndis_cmd *rc)
1181 {
1182
1183 mutex_enter(&sc->sc_cntl_cqlck);
1184 TAILQ_REMOVE(&sc->sc_cntl_cq, rc, rc_entry);
1185 mutex_exit(&sc->sc_cntl_cqlck);
1186 }
1187
1188 static inline int
1189 hvn_rollback_cmd(struct hvn_softc *sc, struct rndis_cmd *rc)
1190 {
1191 struct rndis_cmd *rn;
1192
1193 mutex_enter(&sc->sc_cntl_sqlck);
1194 TAILQ_FOREACH(rn, &sc->sc_cntl_sq, rc_entry) {
1195 if (rn == rc) {
1196 TAILQ_REMOVE(&sc->sc_cntl_sq, rc, rc_entry);
1197 mutex_exit(&sc->sc_cntl_sqlck);
1198 return 0;
1199 }
1200 }
1201 mutex_exit(&sc->sc_cntl_sqlck);
1202 return -1;
1203 }
1204
1205 static inline void
1206 hvn_free_cmd(struct hvn_softc *sc, struct rndis_cmd *rc)
1207 {
1208
1209 memset(rc->rc_req, 0, sizeof(struct rndis_packet_msg));
1210 memset(&rc->rc_cmp, 0, sizeof(rc->rc_cmp));
1211 memset(&rc->rc_msg, 0, sizeof(rc->rc_msg));
1212 mutex_enter(&sc->sc_cntl_fqlck);
1213 TAILQ_INSERT_TAIL(&sc->sc_cntl_fq, rc, rc_entry);
1214 mutex_exit(&sc->sc_cntl_fqlck);
1215 wakeup(&sc->sc_cntl_fq);
1216 }
1217
1218 static int
1219 hvn_rndis_attach(struct hvn_softc *sc)
1220 {
1221 const int dmaflags = cold ? BUS_DMA_NOWAIT : BUS_DMA_WAITOK;
1222 struct rndis_init_req *req;
1223 struct rndis_init_comp *cmp;
1224 struct rndis_cmd *rc;
1225 int i, rv;
1226
1227 /* RNDIS control message queues */
1228 TAILQ_INIT(&sc->sc_cntl_sq);
1229 TAILQ_INIT(&sc->sc_cntl_cq);
1230 TAILQ_INIT(&sc->sc_cntl_fq);
1231 mutex_init(&sc->sc_cntl_sqlck, MUTEX_DEFAULT, IPL_NET);
1232 mutex_init(&sc->sc_cntl_cqlck, MUTEX_DEFAULT, IPL_NET);
1233 mutex_init(&sc->sc_cntl_fqlck, MUTEX_DEFAULT, IPL_NET);
1234
1235 for (i = 0; i < HVN_RNDIS_CTLREQS; i++) {
1236 rc = &sc->sc_cntl_msgs[i];
1237 if (bus_dmamap_create(sc->sc_dmat, PAGE_SIZE, 1, PAGE_SIZE, 0,
1238 dmaflags, &rc->rc_dmap)) {
1239 DPRINTF("%s: failed to create RNDIS command map\n",
1240 device_xname(sc->sc_dev));
1241 goto errout;
1242 }
1243 if (bus_dmamem_alloc(sc->sc_dmat, PAGE_SIZE, PAGE_SIZE,
1244 0, &rc->rc_segs, 1, &rc->rc_nsegs, dmaflags)) {
1245 DPRINTF("%s: failed to allocate RNDIS command\n",
1246 device_xname(sc->sc_dev));
1247 bus_dmamap_destroy(sc->sc_dmat, rc->rc_dmap);
1248 goto errout;
1249 }
1250 if (bus_dmamem_map(sc->sc_dmat, &rc->rc_segs, rc->rc_nsegs,
1251 PAGE_SIZE, (void **)&rc->rc_req, dmaflags)) {
1252 DPRINTF("%s: failed to allocate RNDIS command\n",
1253 device_xname(sc->sc_dev));
1254 bus_dmamem_free(sc->sc_dmat, &rc->rc_segs,
1255 rc->rc_nsegs);
1256 bus_dmamap_destroy(sc->sc_dmat, rc->rc_dmap);
1257 goto errout;
1258 }
1259 memset(rc->rc_req, 0, PAGE_SIZE);
1260 if (bus_dmamap_load(sc->sc_dmat, rc->rc_dmap, rc->rc_req,
1261 PAGE_SIZE, NULL, dmaflags)) {
1262 DPRINTF("%s: failed to load RNDIS command map\n",
1263 device_xname(sc->sc_dev));
1264 bus_dmamem_free(sc->sc_dmat, &rc->rc_segs,
1265 rc->rc_nsegs);
1266 bus_dmamap_destroy(sc->sc_dmat, rc->rc_dmap);
1267 goto errout;
1268 }
1269 rc->rc_gpa = atop(rc->rc_dmap->dm_segs[0].ds_addr);
1270 TAILQ_INSERT_TAIL(&sc->sc_cntl_fq, rc, rc_entry);
1271 }
1272
1273 rc = hvn_alloc_cmd(sc);
1274
1275 bus_dmamap_sync(sc->sc_dmat, rc->rc_dmap, 0, PAGE_SIZE,
1276 BUS_DMASYNC_PREREAD);
1277
1278 rc->rc_id = atomic_inc_uint_nv(&sc->sc_rndisrid);
1279
1280 req = rc->rc_req;
1281 req->rm_type = REMOTE_NDIS_INITIALIZE_MSG;
1282 req->rm_len = sizeof(*req);
1283 req->rm_rid = rc->rc_id;
1284 req->rm_ver_major = RNDIS_VERSION_MAJOR;
1285 req->rm_ver_minor = RNDIS_VERSION_MINOR;
1286 req->rm_max_xfersz = HVN_RNDIS_XFER_SIZE;
1287
1288 rc->rc_cmplen = sizeof(*cmp);
1289
1290 bus_dmamap_sync(sc->sc_dmat, rc->rc_dmap, 0, PAGE_SIZE,
1291 BUS_DMASYNC_PREWRITE);
1292
1293 if ((rv = hvn_rndis_cmd(sc, rc, 500)) != 0) {
1294 DPRINTF("%s: INITIALIZE_MSG failed, error %d\n",
1295 device_xname(sc->sc_dev), rv);
1296 hvn_free_cmd(sc, rc);
1297 goto errout;
1298 }
1299 cmp = (struct rndis_init_comp *)&rc->rc_cmp;
1300 if (cmp->rm_status != RNDIS_STATUS_SUCCESS) {
1301 DPRINTF("%s: failed to init RNDIS, error %#x\n",
1302 device_xname(sc->sc_dev), cmp->rm_status);
1303 hvn_free_cmd(sc, rc);
1304 goto errout;
1305 }
1306
1307 hvn_free_cmd(sc, rc);
1308
1309 /* Initialize RNDIS Data command */
1310 memset(&sc->sc_data_msg, 0, sizeof(sc->sc_data_msg));
1311 sc->sc_data_msg.nvs_type = HVN_NVS_TYPE_RNDIS;
1312 sc->sc_data_msg.nvs_rndis_mtype = HVN_NVS_RNDIS_MTYPE_DATA;
1313 sc->sc_data_msg.nvs_chim_idx = HVN_NVS_CHIM_IDX_INVALID;
1314
1315 return 0;
1316
1317 errout:
1318 for (i = 0; i < HVN_RNDIS_CTLREQS; i++) {
1319 rc = &sc->sc_cntl_msgs[i];
1320 if (rc->rc_req == NULL)
1321 continue;
1322 TAILQ_REMOVE(&sc->sc_cntl_fq, rc, rc_entry);
1323 bus_dmamem_free(sc->sc_dmat, &rc->rc_segs, rc->rc_nsegs);
1324 rc->rc_req = NULL;
1325 bus_dmamap_destroy(sc->sc_dmat, rc->rc_dmap);
1326 }
1327 return -1;
1328 }
1329
1330 static int
1331 hvn_set_capabilities(struct hvn_softc *sc)
1332 {
1333 struct ndis_offload_params params;
1334 size_t len = sizeof(params);
1335
1336 memset(¶ms, 0, sizeof(params));
1337
1338 params.ndis_hdr.ndis_type = NDIS_OBJTYPE_DEFAULT;
1339 if (sc->sc_ndisver < NDIS_VERSION_6_30) {
1340 params.ndis_hdr.ndis_rev = NDIS_OFFLOAD_PARAMS_REV_2;
1341 len = params.ndis_hdr.ndis_size = NDIS_OFFLOAD_PARAMS_SIZE_6_1;
1342 } else {
1343 params.ndis_hdr.ndis_rev = NDIS_OFFLOAD_PARAMS_REV_3;
1344 len = params.ndis_hdr.ndis_size = NDIS_OFFLOAD_PARAMS_SIZE;
1345 }
1346
1347 params.ndis_ip4csum = NDIS_OFFLOAD_PARAM_TXRX;
1348 params.ndis_tcp4csum = NDIS_OFFLOAD_PARAM_TXRX;
1349 params.ndis_tcp6csum = NDIS_OFFLOAD_PARAM_TXRX;
1350 if (sc->sc_ndisver >= NDIS_VERSION_6_30) {
1351 params.ndis_udp4csum = NDIS_OFFLOAD_PARAM_TXRX;
1352 params.ndis_udp6csum = NDIS_OFFLOAD_PARAM_TXRX;
1353 }
1354
1355 return hvn_rndis_set(sc, OID_TCP_OFFLOAD_PARAMETERS, ¶ms, len);
1356 }
1357
1358 static int
1359 hvn_rndis_cmd(struct hvn_softc *sc, struct rndis_cmd *rc, int timo)
1360 {
1361 struct hvn_nvs_rndis *msg = &rc->rc_msg;
1362 struct rndis_msghdr *hdr = rc->rc_req;
1363 struct vmbus_gpa sgl[1];
1364 int tries = 10;
1365 int rv, s;
1366
1367 KASSERT(timo > 0);
1368
1369 msg->nvs_type = HVN_NVS_TYPE_RNDIS;
1370 msg->nvs_rndis_mtype = HVN_NVS_RNDIS_MTYPE_CTRL;
1371 msg->nvs_chim_idx = HVN_NVS_CHIM_IDX_INVALID;
1372
1373 sgl[0].gpa_page = rc->rc_gpa;
1374 sgl[0].gpa_len = hdr->rm_len;
1375 sgl[0].gpa_ofs = 0;
1376
1377 rc->rc_done = 0;
1378
1379 hvn_submit_cmd(sc, rc);
1380
1381 do {
1382 rv = vmbus_channel_send_sgl(sc->sc_chan, sgl, 1, &rc->rc_msg,
1383 sizeof(*msg), rc->rc_id);
1384 if (rv == EAGAIN) {
1385 if (cold)
1386 delay(1000);
1387 else
1388 tsleep(rc, PRIBIO, "rndisout", 1);
1389 } else if (rv) {
1390 DPRINTF("%s: RNDIS operation %u send error %d\n",
1391 device_xname(sc->sc_dev), hdr->rm_type, rv);
1392 hvn_rollback_cmd(sc, rc);
1393 return rv;
1394 }
1395 } while (rv != 0 && --tries > 0);
1396
1397 if (tries == 0 && rv != 0) {
1398 device_printf(sc->sc_dev,
1399 "RNDIS operation %u send error %d\n", hdr->rm_type, rv);
1400 return rv;
1401 }
1402
1403 bus_dmamap_sync(sc->sc_dmat, rc->rc_dmap, 0, PAGE_SIZE,
1404 BUS_DMASYNC_POSTWRITE);
1405
1406 do {
1407 if (cold)
1408 delay(1000);
1409 else
1410 tsleep(rc, PRIBIO | PCATCH, "rndiscmd", 1);
1411 s = splnet();
1412 hvn_nvs_intr(sc);
1413 splx(s);
1414 } while (--timo > 0 && rc->rc_done != 1);
1415
1416 bus_dmamap_sync(sc->sc_dmat, rc->rc_dmap, 0, PAGE_SIZE,
1417 BUS_DMASYNC_POSTREAD);
1418
1419 if (rc->rc_done != 1) {
1420 rv = timo == 0 ? ETIMEDOUT : EINTR;
1421 if (hvn_rollback_cmd(sc, rc)) {
1422 hvn_release_cmd(sc, rc);
1423 rv = 0;
1424 } else if (rv == ETIMEDOUT) {
1425 device_printf(sc->sc_dev,
1426 "RNDIS operation %u timed out\n", hdr->rm_type);
1427 }
1428 return rv;
1429 }
1430
1431 hvn_release_cmd(sc, rc);
1432 return 0;
1433 }
1434
1435 static void
1436 hvn_rndis_input(struct hvn_softc *sc, uint64_t tid, void *arg)
1437 {
1438 struct vmbus_chanpkt_prplist *cp = arg;
1439 uint32_t off, len, type;
1440 int i;
1441
1442 if (sc->sc_rx_ring == NULL) {
1443 DPRINTF("%s: invalid rx ring\n", device_xname(sc->sc_dev));
1444 return;
1445 }
1446
1447 for (i = 0; i < cp->cp_range_cnt; i++) {
1448 off = cp->cp_range[i].gpa_ofs;
1449 len = cp->cp_range[i].gpa_len;
1450
1451 KASSERT(off + len <= sc->sc_rx_size);
1452 KASSERT(len >= RNDIS_HEADER_OFFSET + 4);
1453
1454 memcpy(&type, sc->sc_rx_ring + off, sizeof(type));
1455 switch (type) {
1456 /* data message */
1457 case REMOTE_NDIS_PACKET_MSG:
1458 hvn_rxeof(sc, sc->sc_rx_ring + off, len);
1459 break;
1460 /* completion messages */
1461 case REMOTE_NDIS_INITIALIZE_CMPLT:
1462 case REMOTE_NDIS_QUERY_CMPLT:
1463 case REMOTE_NDIS_SET_CMPLT:
1464 case REMOTE_NDIS_RESET_CMPLT:
1465 case REMOTE_NDIS_KEEPALIVE_CMPLT:
1466 hvn_rndis_complete(sc, sc->sc_rx_ring + off, len);
1467 break;
1468 /* notification message */
1469 case REMOTE_NDIS_INDICATE_STATUS_MSG:
1470 hvn_rndis_status(sc, sc->sc_rx_ring + off, len);
1471 break;
1472 default:
1473 device_printf(sc->sc_dev,
1474 "unhandled RNDIS message type %u\n", type);
1475 break;
1476 }
1477 }
1478
1479 hvn_nvs_ack(sc, tid);
1480 }
1481
1482 static inline struct mbuf *
1483 hvn_devget(struct hvn_softc *sc, void *buf, uint32_t len)
1484 {
1485 struct ifnet *ifp = SC2IFP(sc);
1486 struct mbuf *m;
1487 size_t size = len + ETHER_ALIGN;
1488
1489 MGETHDR(m, M_NOWAIT, MT_DATA);
1490 if (m == NULL)
1491 return NULL;
1492
1493 if (size > MHLEN) {
1494 if (size <= MCLBYTES)
1495 MCLGET(m, M_NOWAIT);
1496 else
1497 MEXTMALLOC(m, size, M_NOWAIT);
1498 if ((m->m_flags & M_EXT) == 0) {
1499 m_freem(m);
1500 return NULL;
1501 }
1502 }
1503
1504 m->m_len = m->m_pkthdr.len = size;
1505 m_adj(m, ETHER_ALIGN);
1506 m_copyback(m, 0, len, buf);
1507 m_set_rcvif(m, ifp);
1508 return m;
1509 }
1510
1511 static void
1512 hvn_rxeof(struct hvn_softc *sc, uint8_t *buf, uint32_t len)
1513 {
1514 struct ifnet *ifp = SC2IFP(sc);
1515 struct rndis_packet_msg *pkt;
1516 struct rndis_pktinfo *pi;
1517 uint32_t csum, vlan;
1518 struct mbuf *m;
1519
1520 if (!(ifp->if_flags & IFF_RUNNING))
1521 return;
1522
1523 if (len < sizeof(*pkt)) {
1524 device_printf(sc->sc_dev, "data packet too short: %u\n",
1525 len);
1526 return;
1527 }
1528
1529 pkt = (struct rndis_packet_msg *)buf;
1530 if (pkt->rm_dataoffset + pkt->rm_datalen > len) {
1531 device_printf(sc->sc_dev,
1532 "data packet out of bounds: %u@%u\n", pkt->rm_dataoffset,
1533 pkt->rm_datalen);
1534 return;
1535 }
1536
1537 if ((m = hvn_devget(sc, buf + RNDIS_HEADER_OFFSET + pkt->rm_dataoffset,
1538 pkt->rm_datalen)) == NULL) {
1539 ifp->if_ierrors++;
1540 return;
1541 }
1542
1543 if (pkt->rm_pktinfooffset + pkt->rm_pktinfolen > len) {
1544 device_printf(sc->sc_dev,
1545 "pktinfo is out of bounds: %u@%u vs %u\n",
1546 pkt->rm_pktinfolen, pkt->rm_pktinfooffset, len);
1547 goto done;
1548 }
1549
1550 pi = (struct rndis_pktinfo *)(buf + RNDIS_HEADER_OFFSET +
1551 pkt->rm_pktinfooffset);
1552 while (pkt->rm_pktinfolen > 0) {
1553 if (pi->rm_size > pkt->rm_pktinfolen) {
1554 device_printf(sc->sc_dev,
1555 "invalid pktinfo size: %u/%u\n", pi->rm_size,
1556 pkt->rm_pktinfolen);
1557 break;
1558 }
1559
1560 switch (pi->rm_type) {
1561 case NDIS_PKTINFO_TYPE_CSUM:
1562 memcpy(&csum, pi->rm_data, sizeof(csum));
1563 if (csum & NDIS_RXCSUM_INFO_IPCS_OK)
1564 m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
1565 if (csum & NDIS_RXCSUM_INFO_TCPCS_OK)
1566 m->m_pkthdr.csum_flags |= M_CSUM_TCPv4;
1567 if (csum & NDIS_RXCSUM_INFO_UDPCS_OK)
1568 m->m_pkthdr.csum_flags |= M_CSUM_UDPv4;
1569 break;
1570 case NDIS_PKTINFO_TYPE_VLAN:
1571 memcpy(&vlan, pi->rm_data, sizeof(vlan));
1572 if (vlan != 0xffffffff) {
1573 m->m_pkthdr.ether_vtag =
1574 NDIS_VLAN_INFO_ID(vlan) |
1575 (NDIS_VLAN_INFO_PRI(vlan) << EVL_PRIO_BITS);
1576 m->m_flags |= M_VLANTAG;
1577 }
1578 break;
1579 default:
1580 DPRINTF("%s: unhandled pktinfo type %u\n",
1581 device_xname(sc->sc_dev), pi->rm_type);
1582 break;
1583 }
1584
1585 pkt->rm_pktinfolen -= pi->rm_size;
1586 pi = (struct rndis_pktinfo *)((char *)pi + pi->rm_size);
1587 }
1588
1589 done:
1590 if_percpuq_enqueue(sc->sc_ipq, m);
1591 }
1592
1593 static void
1594 hvn_rndis_complete(struct hvn_softc *sc, uint8_t *buf, uint32_t len)
1595 {
1596 struct rndis_cmd *rc;
1597 uint32_t id;
1598
1599 memcpy(&id, buf + RNDIS_HEADER_OFFSET, sizeof(id));
1600 if ((rc = hvn_complete_cmd(sc, id)) != NULL) {
1601 if (len < rc->rc_cmplen)
1602 device_printf(sc->sc_dev,
1603 "RNDIS response %u too short: %u\n", id, len);
1604 else
1605 memcpy(&rc->rc_cmp, buf, rc->rc_cmplen);
1606 if (len > rc->rc_cmplen &&
1607 len - rc->rc_cmplen > HVN_RNDIS_BUFSIZE)
1608 device_printf(sc->sc_dev,
1609 "RNDIS response %u too large: %u\n", id, len);
1610 else if (len > rc->rc_cmplen)
1611 memcpy(&rc->rc_cmpbuf, buf + rc->rc_cmplen,
1612 len - rc->rc_cmplen);
1613 rc->rc_done = 1;
1614 wakeup(rc);
1615 } else {
1616 DPRINTF("%s: failed to complete RNDIS request id %u\n",
1617 device_xname(sc->sc_dev), id);
1618 }
1619 }
1620
1621 static int
1622 hvn_rndis_output(struct hvn_softc *sc, struct hvn_tx_desc *txd)
1623 {
1624 uint64_t rid = (uint64_t)txd->txd_id << 32;
1625 int rv;
1626
1627 rv = vmbus_channel_send_sgl(sc->sc_chan, txd->txd_sgl, txd->txd_nsge,
1628 &sc->sc_data_msg, sizeof(sc->sc_data_msg), rid);
1629 if (rv) {
1630 DPRINTF("%s: RNDIS data send error %d\n",
1631 device_xname(sc->sc_dev), rv);
1632 return rv;
1633 }
1634 return 0;
1635 }
1636
1637 static void
1638 hvn_rndis_status(struct hvn_softc *sc, uint8_t *buf, uint32_t len)
1639 {
1640 struct ifnet *ifp = SC2IFP(sc);
1641 uint32_t status;
1642 int link_state = sc->sc_link_state;
1643
1644 memcpy(&status, buf + RNDIS_HEADER_OFFSET, sizeof(status));
1645 switch (status) {
1646 case RNDIS_STATUS_MEDIA_CONNECT:
1647 sc->sc_link_state = LINK_STATE_UP;
1648 break;
1649 case RNDIS_STATUS_MEDIA_DISCONNECT:
1650 sc->sc_link_state = LINK_STATE_DOWN;
1651 break;
1652 /* Ignore these */
1653 case RNDIS_STATUS_OFFLOAD_CURRENT_CONFIG:
1654 return;
1655 default:
1656 DPRINTF("%s: unhandled status %#x\n", device_xname(sc->sc_dev),
1657 status);
1658 return;
1659 }
1660 if (link_state != sc->sc_link_state)
1661 if_link_state_change(ifp, sc->sc_link_state);
1662 }
1663
1664 static int
1665 hvn_rndis_query(struct hvn_softc *sc, uint32_t oid, void *res, size_t *length)
1666 {
1667 struct rndis_cmd *rc;
1668 struct rndis_query_req *req;
1669 struct rndis_query_comp *cmp;
1670 size_t olength = *length;
1671 int rv;
1672
1673 rc = hvn_alloc_cmd(sc);
1674
1675 bus_dmamap_sync(sc->sc_dmat, rc->rc_dmap, 0, PAGE_SIZE,
1676 BUS_DMASYNC_PREREAD);
1677
1678 rc->rc_id = atomic_inc_uint_nv(&sc->sc_rndisrid);
1679
1680 req = rc->rc_req;
1681 req->rm_type = REMOTE_NDIS_QUERY_MSG;
1682 req->rm_len = sizeof(*req);
1683 req->rm_rid = rc->rc_id;
1684 req->rm_oid = oid;
1685 req->rm_infobufoffset = sizeof(*req) - RNDIS_HEADER_OFFSET;
1686
1687 rc->rc_cmplen = sizeof(*cmp);
1688
1689 bus_dmamap_sync(sc->sc_dmat, rc->rc_dmap, 0, PAGE_SIZE,
1690 BUS_DMASYNC_PREWRITE);
1691
1692 if ((rv = hvn_rndis_cmd(sc, rc, 500)) != 0) {
1693 DPRINTF("%s: QUERY_MSG failed, error %d\n",
1694 device_xname(sc->sc_dev), rv);
1695 hvn_free_cmd(sc, rc);
1696 return rv;
1697 }
1698
1699 cmp = (struct rndis_query_comp *)&rc->rc_cmp;
1700 switch (cmp->rm_status) {
1701 case RNDIS_STATUS_SUCCESS:
1702 if (cmp->rm_infobuflen > olength) {
1703 rv = EINVAL;
1704 break;
1705 }
1706 memcpy(res, rc->rc_cmpbuf, cmp->rm_infobuflen);
1707 *length = cmp->rm_infobuflen;
1708 break;
1709 default:
1710 *length = 0;
1711 rv = EIO;
1712 break;
1713 }
1714
1715 hvn_free_cmd(sc, rc);
1716 return rv;
1717 }
1718
1719 static int
1720 hvn_rndis_set(struct hvn_softc *sc, uint32_t oid, void *data, size_t length)
1721 {
1722 struct rndis_cmd *rc;
1723 struct rndis_set_req *req;
1724 struct rndis_set_comp *cmp;
1725 int rv;
1726
1727 rc = hvn_alloc_cmd(sc);
1728
1729 bus_dmamap_sync(sc->sc_dmat, rc->rc_dmap, 0, PAGE_SIZE,
1730 BUS_DMASYNC_PREREAD);
1731
1732 rc->rc_id = atomic_inc_uint_nv(&sc->sc_rndisrid);
1733
1734 req = rc->rc_req;
1735 req->rm_type = REMOTE_NDIS_SET_MSG;
1736 req->rm_len = sizeof(*req) + length;
1737 req->rm_rid = rc->rc_id;
1738 req->rm_oid = oid;
1739 req->rm_infobufoffset = sizeof(*req) - RNDIS_HEADER_OFFSET;
1740
1741 rc->rc_cmplen = sizeof(*cmp);
1742
1743 if (length > 0) {
1744 KASSERT(sizeof(*req) + length < PAGE_SIZE);
1745 req->rm_infobuflen = length;
1746 memcpy(req + 1, data, length);
1747 }
1748
1749 bus_dmamap_sync(sc->sc_dmat, rc->rc_dmap, 0, PAGE_SIZE,
1750 BUS_DMASYNC_PREWRITE);
1751
1752 if ((rv = hvn_rndis_cmd(sc, rc, 500)) != 0) {
1753 DPRINTF("%s: SET_MSG failed, error %d\n",
1754 device_xname(sc->sc_dev), rv);
1755 hvn_free_cmd(sc, rc);
1756 return rv;
1757 }
1758
1759 cmp = (struct rndis_set_comp *)&rc->rc_cmp;
1760 if (cmp->rm_status != RNDIS_STATUS_SUCCESS)
1761 rv = EIO;
1762
1763 hvn_free_cmd(sc, rc);
1764 return rv;
1765 }
1766
1767 static int
1768 hvn_rndis_open(struct hvn_softc *sc)
1769 {
1770 uint32_t filter;
1771 int rv;
1772
1773 if (sc->sc_promisc)
1774 filter = RNDIS_PACKET_TYPE_PROMISCUOUS;
1775 else
1776 filter = RNDIS_PACKET_TYPE_BROADCAST |
1777 RNDIS_PACKET_TYPE_ALL_MULTICAST |
1778 RNDIS_PACKET_TYPE_DIRECTED;
1779
1780 rv = hvn_rndis_set(sc, OID_GEN_CURRENT_PACKET_FILTER,
1781 &filter, sizeof(filter));
1782 if (rv) {
1783 DPRINTF("%s: failed to set RNDIS filter to %#x\n",
1784 device_xname(sc->sc_dev), filter);
1785 }
1786 return rv;
1787 }
1788
1789 static int
1790 hvn_rndis_close(struct hvn_softc *sc)
1791 {
1792 uint32_t filter = 0;
1793 int rv;
1794
1795 rv = hvn_rndis_set(sc, OID_GEN_CURRENT_PACKET_FILTER,
1796 &filter, sizeof(filter));
1797 if (rv) {
1798 DPRINTF("%s: failed to clear RNDIS filter\n",
1799 device_xname(sc->sc_dev));
1800 }
1801 return rv;
1802 }
1803
1804 static void
1805 hvn_rndis_detach(struct hvn_softc *sc)
1806 {
1807 struct rndis_cmd *rc;
1808 struct rndis_halt_req *req;
1809 int rv;
1810
1811 rc = hvn_alloc_cmd(sc);
1812
1813 bus_dmamap_sync(sc->sc_dmat, rc->rc_dmap, 0, PAGE_SIZE,
1814 BUS_DMASYNC_PREREAD);
1815
1816 rc->rc_id = atomic_inc_uint_nv(&sc->sc_rndisrid);
1817
1818 req = rc->rc_req;
1819 req->rm_type = REMOTE_NDIS_HALT_MSG;
1820 req->rm_len = sizeof(*req);
1821 req->rm_rid = rc->rc_id;
1822
1823 bus_dmamap_sync(sc->sc_dmat, rc->rc_dmap, 0, PAGE_SIZE,
1824 BUS_DMASYNC_PREWRITE);
1825
1826 if ((rv = hvn_rndis_cmd(sc, rc, 500)) != 0) {
1827 DPRINTF("%s: HALT_MSG failed, error %d\n",
1828 device_xname(sc->sc_dev), rv);
1829 }
1830 hvn_free_cmd(sc, rc);
1831 }
1832