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