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