2 * Copyright (c) 2016 Microsoft Corp.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
32 #include <sys/malloc.h>
33 #include <sys/mutex.h>
35 #include <sys/systm.h>
37 #include <dev/hyperv/include/hyperv_busdma.h>
38 #include <dev/hyperv/include/vmbus_xact.h>
41 struct vmbus_xact_ctx *x_ctx;
45 struct hyperv_dma x_req_dma;
52 struct vmbus_xact_ctx {
59 * Protected by xc_lock.
61 uint32_t xc_flags; /* VMBUS_XACT_CTXF_ */
62 struct vmbus_xact *xc_free;
63 struct vmbus_xact *xc_active;
64 struct vmbus_xact *xc_orphan;
67 #define VMBUS_XACT_CTXF_DESTROY 0x0001
69 static struct vmbus_xact *vmbus_xact_alloc(struct vmbus_xact_ctx *,
71 static void vmbus_xact_free(struct vmbus_xact *);
72 static struct vmbus_xact *vmbus_xact_get1(struct vmbus_xact_ctx *,
74 static const void *vmbus_xact_wait1(struct vmbus_xact *, size_t *,
76 static const void *vmbus_xact_return(struct vmbus_xact *,
78 static void vmbus_xact_save_resp(struct vmbus_xact *,
79 const void *, size_t);
80 static void vmbus_xact_ctx_free(struct vmbus_xact_ctx *);
82 static struct vmbus_xact *
83 vmbus_xact_alloc(struct vmbus_xact_ctx *ctx, bus_dma_tag_t parent_dtag)
85 struct vmbus_xact *xact;
87 xact = malloc(sizeof(*xact), M_DEVBUF, M_WAITOK | M_ZERO);
90 /* XXX assume that page aligned is enough */
91 xact->x_req = hyperv_dmamem_alloc(parent_dtag, PAGE_SIZE, 0,
92 ctx->xc_req_size, &xact->x_req_dma, BUS_DMA_WAITOK);
93 if (xact->x_req == NULL) {
97 if (ctx->xc_priv_size != 0)
98 xact->x_priv = malloc(ctx->xc_priv_size, M_DEVBUF, M_WAITOK);
99 xact->x_resp0 = malloc(ctx->xc_resp_size, M_DEVBUF, M_WAITOK);
105 vmbus_xact_free(struct vmbus_xact *xact)
108 hyperv_dmamem_free(&xact->x_req_dma, xact->x_req);
109 free(xact->x_resp0, M_DEVBUF);
110 if (xact->x_priv != NULL)
111 free(xact->x_priv, M_DEVBUF);
112 free(xact, M_DEVBUF);
115 static struct vmbus_xact *
116 vmbus_xact_get1(struct vmbus_xact_ctx *ctx, uint32_t dtor_flag)
118 struct vmbus_xact *xact;
120 mtx_lock(&ctx->xc_lock);
122 while ((ctx->xc_flags & dtor_flag) == 0 && ctx->xc_free == NULL)
123 mtx_sleep(&ctx->xc_free, &ctx->xc_lock, 0, "gxact", 0);
124 if (ctx->xc_flags & dtor_flag) {
125 /* Being destroyed */
129 KASSERT(xact != NULL, ("no free xact"));
130 KASSERT(xact->x_resp == NULL, ("xact has pending response"));
134 mtx_unlock(&ctx->xc_lock);
139 struct vmbus_xact_ctx *
140 vmbus_xact_ctx_create(bus_dma_tag_t dtag, size_t req_size, size_t resp_size,
143 struct vmbus_xact_ctx *ctx;
145 KASSERT(req_size > 0, ("request size is 0"));
146 KASSERT(resp_size > 0, ("response size is 0"));
148 ctx = malloc(sizeof(*ctx), M_DEVBUF, M_WAITOK | M_ZERO);
149 ctx->xc_req_size = req_size;
150 ctx->xc_resp_size = resp_size;
151 ctx->xc_priv_size = priv_size;
153 ctx->xc_free = vmbus_xact_alloc(ctx, dtag);
154 if (ctx->xc_free == NULL) {
159 mtx_init(&ctx->xc_lock, "vmbus xact", NULL, MTX_DEF);
165 vmbus_xact_ctx_orphan(struct vmbus_xact_ctx *ctx)
167 mtx_lock(&ctx->xc_lock);
168 if (ctx->xc_flags & VMBUS_XACT_CTXF_DESTROY) {
169 mtx_unlock(&ctx->xc_lock);
172 ctx->xc_flags |= VMBUS_XACT_CTXF_DESTROY;
173 mtx_unlock(&ctx->xc_lock);
175 wakeup(&ctx->xc_free);
176 wakeup(&ctx->xc_active);
178 ctx->xc_orphan = vmbus_xact_get1(ctx, 0);
179 if (ctx->xc_orphan == NULL)
180 panic("can't get xact");
185 vmbus_xact_ctx_free(struct vmbus_xact_ctx *ctx)
187 KASSERT(ctx->xc_flags & VMBUS_XACT_CTXF_DESTROY,
188 ("xact ctx was not orphaned"));
189 KASSERT(ctx->xc_orphan != NULL, ("no orphaned xact"));
191 vmbus_xact_free(ctx->xc_orphan);
192 mtx_destroy(&ctx->xc_lock);
197 vmbus_xact_ctx_destroy(struct vmbus_xact_ctx *ctx)
200 vmbus_xact_ctx_orphan(ctx);
201 vmbus_xact_ctx_free(ctx);
205 vmbus_xact_get(struct vmbus_xact_ctx *ctx, size_t req_len)
207 struct vmbus_xact *xact;
209 if (req_len > ctx->xc_req_size)
210 panic("invalid request size %zu", req_len);
212 xact = vmbus_xact_get1(ctx, VMBUS_XACT_CTXF_DESTROY);
216 memset(xact->x_req, 0, req_len);
221 vmbus_xact_put(struct vmbus_xact *xact)
223 struct vmbus_xact_ctx *ctx = xact->x_ctx;
225 KASSERT(ctx->xc_active == NULL, ("pending active xact"));
228 mtx_lock(&ctx->xc_lock);
229 KASSERT(ctx->xc_free == NULL, ("has free xact"));
231 mtx_unlock(&ctx->xc_lock);
232 wakeup(&ctx->xc_free);
236 vmbus_xact_req_data(const struct vmbus_xact *xact)
239 return (xact->x_req);
243 vmbus_xact_req_paddr(const struct vmbus_xact *xact)
246 return (xact->x_req_dma.hv_paddr);
250 vmbus_xact_priv(const struct vmbus_xact *xact, size_t priv_len)
253 if (priv_len > xact->x_ctx->xc_priv_size)
254 panic("invalid priv size %zu", priv_len);
255 return (xact->x_priv);
259 vmbus_xact_activate(struct vmbus_xact *xact)
261 struct vmbus_xact_ctx *ctx = xact->x_ctx;
263 KASSERT(xact->x_resp == NULL, ("xact has pending response"));
265 mtx_lock(&ctx->xc_lock);
266 KASSERT(ctx->xc_active == NULL, ("pending active xact"));
267 ctx->xc_active = xact;
268 mtx_unlock(&ctx->xc_lock);
272 vmbus_xact_deactivate(struct vmbus_xact *xact)
274 struct vmbus_xact_ctx *ctx = xact->x_ctx;
276 mtx_lock(&ctx->xc_lock);
277 KASSERT(ctx->xc_active == xact, ("xact mismatch"));
278 ctx->xc_active = NULL;
279 mtx_unlock(&ctx->xc_lock);
283 vmbus_xact_return(struct vmbus_xact *xact, size_t *resp_len)
285 struct vmbus_xact_ctx *ctx = xact->x_ctx;
288 mtx_assert(&ctx->xc_lock, MA_OWNED);
289 KASSERT(ctx->xc_active == xact, ("xact trashed"));
291 if ((ctx->xc_flags & VMBUS_XACT_CTXF_DESTROY) && xact->x_resp == NULL) {
295 * Orphaned and no response was received yet; fake up
296 * an one byte response.
298 printf("vmbus: xact ctx was orphaned w/ pending xact\n");
299 vmbus_xact_save_resp(ctx->xc_active, &b, sizeof(b));
301 KASSERT(xact->x_resp != NULL, ("no response"));
303 ctx->xc_active = NULL;
306 *resp_len = xact->x_resp_len;
312 vmbus_xact_wait1(struct vmbus_xact *xact, size_t *resp_len,
315 struct vmbus_xact_ctx *ctx = xact->x_ctx;
318 mtx_lock(&ctx->xc_lock);
320 KASSERT(ctx->xc_active == xact, ("xact mismatch"));
321 while (xact->x_resp == NULL &&
322 (ctx->xc_flags & VMBUS_XACT_CTXF_DESTROY) == 0) {
324 mtx_sleep(&ctx->xc_active, &ctx->xc_lock, 0,
327 mtx_unlock(&ctx->xc_lock);
329 mtx_lock(&ctx->xc_lock);
332 resp = vmbus_xact_return(xact, resp_len);
334 mtx_unlock(&ctx->xc_lock);
340 vmbus_xact_wait(struct vmbus_xact *xact, size_t *resp_len)
343 return (vmbus_xact_wait1(xact, resp_len, true /* can sleep */));
347 vmbus_xact_busywait(struct vmbus_xact *xact, size_t *resp_len)
350 return (vmbus_xact_wait1(xact, resp_len, false /* can't sleep */));
354 vmbus_xact_poll(struct vmbus_xact *xact, size_t *resp_len)
356 struct vmbus_xact_ctx *ctx = xact->x_ctx;
359 mtx_lock(&ctx->xc_lock);
361 KASSERT(ctx->xc_active == xact, ("xact mismatch"));
362 if (xact->x_resp == NULL &&
363 (ctx->xc_flags & VMBUS_XACT_CTXF_DESTROY) == 0) {
364 mtx_unlock(&ctx->xc_lock);
368 resp = vmbus_xact_return(xact, resp_len);
370 mtx_unlock(&ctx->xc_lock);
376 vmbus_xact_save_resp(struct vmbus_xact *xact, const void *data, size_t dlen)
378 struct vmbus_xact_ctx *ctx = xact->x_ctx;
381 mtx_assert(&ctx->xc_lock, MA_OWNED);
383 if (cplen > ctx->xc_resp_size) {
384 printf("vmbus: xact response truncated %zu -> %zu\n",
385 cplen, ctx->xc_resp_size);
386 cplen = ctx->xc_resp_size;
389 KASSERT(ctx->xc_active == xact, ("xact mismatch"));
390 memcpy(xact->x_resp0, data, cplen);
391 xact->x_resp_len = cplen;
392 xact->x_resp = xact->x_resp0;
396 vmbus_xact_wakeup(struct vmbus_xact *xact, const void *data, size_t dlen)
398 struct vmbus_xact_ctx *ctx = xact->x_ctx;
401 mtx_lock(&ctx->xc_lock);
404 * xc_active could be NULL, if the ctx has been orphaned.
406 if (ctx->xc_active != NULL) {
407 vmbus_xact_save_resp(xact, data, dlen);
410 KASSERT(ctx->xc_flags & VMBUS_XACT_CTXF_DESTROY,
411 ("no active xact pending"));
412 printf("vmbus: drop xact response\n");
414 mtx_unlock(&ctx->xc_lock);
417 wakeup(&ctx->xc_active);
421 vmbus_xact_ctx_wakeup(struct vmbus_xact_ctx *ctx, const void *data, size_t dlen)
425 mtx_lock(&ctx->xc_lock);
428 * xc_active could be NULL, if the ctx has been orphaned.
430 if (ctx->xc_active != NULL) {
431 vmbus_xact_save_resp(ctx->xc_active, data, dlen);
434 KASSERT(ctx->xc_flags & VMBUS_XACT_CTXF_DESTROY,
435 ("no active xact pending"));
436 printf("vmbus: drop xact response\n");
438 mtx_unlock(&ctx->xc_lock);
441 wakeup(&ctx->xc_active);