These changes are the raw update to linux-4.4.6-rt14. Kernel sources
[kvmfornfv.git] / kernel / drivers / staging / rdma / hfi1 / iowait.h
1 #ifndef _HFI1_IOWAIT_H
2 #define _HFI1_IOWAIT_H
3 /*
4  *
5  * This file is provided under a dual BSD/GPLv2 license.  When using or
6  * redistributing this file, you may do so under either license.
7  *
8  * GPL LICENSE SUMMARY
9  *
10  * Copyright(c) 2015 Intel Corporation.
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of version 2 of the GNU General Public License as
14  * published by the Free Software Foundation.
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * General Public License for more details.
20  *
21  * BSD LICENSE
22  *
23  * Copyright(c) 2015 Intel Corporation.
24  *
25  * Redistribution and use in source and binary forms, with or without
26  * modification, are permitted provided that the following conditions
27  * are met:
28  *
29  *  - Redistributions of source code must retain the above copyright
30  *    notice, this list of conditions and the following disclaimer.
31  *  - Redistributions in binary form must reproduce the above copyright
32  *    notice, this list of conditions and the following disclaimer in
33  *    the documentation and/or other materials provided with the
34  *    distribution.
35  *  - Neither the name of Intel Corporation nor the names of its
36  *    contributors may be used to endorse or promote products derived
37  *    from this software without specific prior written permission.
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
40  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
41  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
42  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
43  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
46  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
47  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
48  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
49  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50  *
51  */
52
53 #include <linux/list.h>
54 #include <linux/workqueue.h>
55 #include <linux/sched.h>
56
57 /*
58  * typedef (*restart_t)() - restart callback
59  * @work: pointer to work structure
60  */
61 typedef void (*restart_t)(struct work_struct *work);
62
63 struct sdma_txreq;
64 struct sdma_engine;
65 /**
66  * struct iowait - linkage for delayed progress/waiting
67  * @list: used to add/insert into QP/PQ wait lists
68  * @tx_head: overflow list of sdma_txreq's
69  * @sleep: no space callback
70  * @wakeup: space callback
71  * @iowork: workqueue overhead
72  * @wait_dma: wait for sdma_busy == 0
73  * @sdma_busy: # of packets in flight
74  * @count: total number of descriptors in tx_head'ed list
75  * @tx_limit: limit for overflow queuing
76  * @tx_count: number of tx entry's in tx_head'ed list
77  *
78  * This is to be embedded in user's state structure
79  * (QP or PQ).
80  *
81  * The sleep and wakeup members are a
82  * bit misnamed.   They do not strictly
83  * speaking sleep or wake up, but they
84  * are callbacks for the ULP to implement
85  * what ever queuing/dequeuing of
86  * the embedded iowait and its containing struct
87  * when a resource shortage like SDMA ring space is seen.
88  *
89  * Both potentially have locks help
90  * so sleeping is not allowed.
91  *
92  * The wait_dma member along with the iow
93  */
94
95 struct iowait {
96         struct list_head list;
97         struct list_head tx_head;
98         int (*sleep)(
99                 struct sdma_engine *sde,
100                 struct iowait *wait,
101                 struct sdma_txreq *tx,
102                 unsigned seq);
103         void (*wakeup)(struct iowait *wait, int reason);
104         struct work_struct iowork;
105         wait_queue_head_t wait_dma;
106         atomic_t sdma_busy;
107         u32 count;
108         u32 tx_limit;
109         u32 tx_count;
110 };
111
112 #define SDMA_AVAIL_REASON 0
113
114 /**
115  * iowait_init() - initialize wait structure
116  * @wait: wait struct to initialize
117  * @tx_limit: limit for overflow queuing
118  * @func: restart function for workqueue
119  * @sleep: sleep function for no space
120  * @wakeup: wakeup function for no space
121  *
122  * This function initializes the iowait
123  * structure embedded in the QP or PQ.
124  *
125  */
126
127 static inline void iowait_init(
128         struct iowait *wait,
129         u32 tx_limit,
130         void (*func)(struct work_struct *work),
131         int (*sleep)(
132                 struct sdma_engine *sde,
133                 struct iowait *wait,
134                 struct sdma_txreq *tx,
135                 unsigned seq),
136         void (*wakeup)(struct iowait *wait, int reason))
137 {
138         wait->count = 0;
139         INIT_LIST_HEAD(&wait->list);
140         INIT_LIST_HEAD(&wait->tx_head);
141         INIT_WORK(&wait->iowork, func);
142         init_waitqueue_head(&wait->wait_dma);
143         atomic_set(&wait->sdma_busy, 0);
144         wait->tx_limit = tx_limit;
145         wait->sleep = sleep;
146         wait->wakeup = wakeup;
147 }
148
149 /**
150  * iowait_schedule() - initialize wait structure
151  * @wait: wait struct to schedule
152  * @wq: workqueue for schedule
153  */
154 static inline void iowait_schedule(
155         struct iowait *wait,
156         struct workqueue_struct *wq)
157 {
158         queue_work(wq, &wait->iowork);
159 }
160
161 /**
162  * iowait_sdma_drain() - wait for DMAs to drain
163  *
164  * @wait: iowait structure
165  *
166  * This will delay until the iowait sdmas have
167  * completed.
168  */
169 static inline void iowait_sdma_drain(struct iowait *wait)
170 {
171         wait_event(wait->wait_dma, !atomic_read(&wait->sdma_busy));
172 }
173
174 /**
175  * iowait_drain_wakeup() - trigger iowait_drain() waiter
176  *
177  * @wait: iowait structure
178  *
179  * This will trigger any waiters.
180  */
181 static inline void iowait_drain_wakeup(struct iowait *wait)
182 {
183         wake_up(&wait->wait_dma);
184 }
185
186 #endif