1ce110ea1SWei Hu /*-
2ce110ea1SWei Hu * SPDX-License-Identifier: BSD-2-Clause
3ce110ea1SWei Hu *
4ce110ea1SWei Hu * Copyright (c) 2021 Microsoft Corp.
5ce110ea1SWei Hu * All rights reserved.
6ce110ea1SWei Hu *
7ce110ea1SWei Hu * Redistribution and use in source and binary forms, with or without
8ce110ea1SWei Hu * modification, are permitted provided that the following conditions
9ce110ea1SWei Hu * are met:
10ce110ea1SWei Hu *
11ce110ea1SWei Hu * 1. Redistributions of source code must retain the above copyright
12ce110ea1SWei Hu * notice, this list of conditions and the following disclaimer.
13ce110ea1SWei Hu *
14ce110ea1SWei Hu * 2. Redistributions in binary form must reproduce the above copyright
15ce110ea1SWei Hu * notice, this list of conditions and the following disclaimer in the
16ce110ea1SWei Hu * documentation and/or other materials provided with the distribution.
17ce110ea1SWei Hu *
18ce110ea1SWei Hu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19ce110ea1SWei Hu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20ce110ea1SWei Hu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21ce110ea1SWei Hu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22ce110ea1SWei Hu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23ce110ea1SWei Hu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24ce110ea1SWei Hu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25ce110ea1SWei Hu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26ce110ea1SWei Hu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27ce110ea1SWei Hu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28ce110ea1SWei Hu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29ce110ea1SWei Hu */
30*fdafd315SWarner Losh
31ce110ea1SWei Hu #include <sys/types.h>
32ce110ea1SWei Hu #include <sys/mutex.h>
33ce110ea1SWei Hu #include <sys/systm.h>
34ce110ea1SWei Hu
35ce110ea1SWei Hu #include "gdma_util.h"
36ce110ea1SWei Hu
37ce110ea1SWei Hu
38ce110ea1SWei Hu void
init_completion(struct completion * c)39ce110ea1SWei Hu init_completion(struct completion *c)
40ce110ea1SWei Hu {
41ce110ea1SWei Hu memset(c, 0, sizeof(*c));
42ce110ea1SWei Hu mtx_init(&c->lock, "gdma_completion", NULL, MTX_DEF);
43ce110ea1SWei Hu c->done = 0;
44ce110ea1SWei Hu }
45ce110ea1SWei Hu
46ce110ea1SWei Hu void
free_completion(struct completion * c)47ce110ea1SWei Hu free_completion(struct completion *c)
48ce110ea1SWei Hu {
49ce110ea1SWei Hu mtx_destroy(&c->lock);
50ce110ea1SWei Hu }
51ce110ea1SWei Hu
52ce110ea1SWei Hu void
complete(struct completion * c)53ce110ea1SWei Hu complete(struct completion *c)
54ce110ea1SWei Hu {
55ce110ea1SWei Hu mtx_lock(&c->lock);
56ce110ea1SWei Hu c->done++;
57ce110ea1SWei Hu mtx_unlock(&c->lock);
58ce110ea1SWei Hu wakeup(c);
59ce110ea1SWei Hu }
60ce110ea1SWei Hu
61ce110ea1SWei Hu void
wait_for_completion(struct completion * c)62ce110ea1SWei Hu wait_for_completion(struct completion *c)
63ce110ea1SWei Hu {
64ce110ea1SWei Hu mtx_lock(&c->lock);
65ce110ea1SWei Hu while (c->done == 0)
66ce110ea1SWei Hu mtx_sleep(c, &c->lock, 0, "gdma_wfc", 0);
67ce110ea1SWei Hu c->done--;
68ce110ea1SWei Hu mtx_unlock(&c->lock);
69ce110ea1SWei Hu }
70ce110ea1SWei Hu
71ce110ea1SWei Hu /*
72ce110ea1SWei Hu * Return: 0 if completed, a non-zero value if timed out.
73ce110ea1SWei Hu */
74ce110ea1SWei Hu int
wait_for_completion_timeout(struct completion * c,int timeout)75ce110ea1SWei Hu wait_for_completion_timeout(struct completion *c, int timeout)
76ce110ea1SWei Hu {
77ce110ea1SWei Hu int ret;
78ce110ea1SWei Hu
79ce110ea1SWei Hu mtx_lock(&c->lock);
80ce110ea1SWei Hu
81ce110ea1SWei Hu if (c->done == 0)
82ce110ea1SWei Hu mtx_sleep(c, &c->lock, 0, "gdma_wfc", timeout);
83ce110ea1SWei Hu
84ce110ea1SWei Hu if (c->done > 0) {
85ce110ea1SWei Hu c->done--;
86ce110ea1SWei Hu ret = 0;
87ce110ea1SWei Hu } else {
88ce110ea1SWei Hu ret = 1;
89ce110ea1SWei Hu }
90ce110ea1SWei Hu
91ce110ea1SWei Hu mtx_unlock(&c->lock);
92ce110ea1SWei Hu
93ce110ea1SWei Hu return (ret);
94ce110ea1SWei Hu }
95