staging/rdma/hfi: fix CQ completion order issue
[linux-2.6-block.git] / drivers / staging / rdma / hfi1 / iowait.h
CommitLineData
77241056
MM
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
711e104d 57#include "sdma_txreq.h"
14553ca1 58
77241056
MM
59/*
60 * typedef (*restart_t)() - restart callback
61 * @work: pointer to work structure
62 */
63typedef void (*restart_t)(struct work_struct *work);
64
65struct sdma_txreq;
66struct sdma_engine;
67/**
68 * struct iowait - linkage for delayed progress/waiting
69 * @list: used to add/insert into QP/PQ wait lists
70 * @tx_head: overflow list of sdma_txreq's
71 * @sleep: no space callback
a545f530
MM
72 * @wakeup: space callback wakeup
73 * @sdma_drained: sdma count drained
77241056
MM
74 * @iowork: workqueue overhead
75 * @wait_dma: wait for sdma_busy == 0
14553ca1 76 * @wait_pio: wait for pio_busy == 0
77241056
MM
77 * @sdma_busy: # of packets in flight
78 * @count: total number of descriptors in tx_head'ed list
79 * @tx_limit: limit for overflow queuing
80 * @tx_count: number of tx entry's in tx_head'ed list
81 *
82 * This is to be embedded in user's state structure
83 * (QP or PQ).
84 *
85 * The sleep and wakeup members are a
86 * bit misnamed. They do not strictly
87 * speaking sleep or wake up, but they
88 * are callbacks for the ULP to implement
89 * what ever queuing/dequeuing of
90 * the embedded iowait and its containing struct
91 * when a resource shortage like SDMA ring space is seen.
92 *
93 * Both potentially have locks help
94 * so sleeping is not allowed.
95 *
96 * The wait_dma member along with the iow
97 */
98
99struct iowait {
100 struct list_head list;
101 struct list_head tx_head;
102 int (*sleep)(
103 struct sdma_engine *sde,
104 struct iowait *wait,
105 struct sdma_txreq *tx,
106 unsigned seq);
107 void (*wakeup)(struct iowait *wait, int reason);
a545f530 108 void (*sdma_drained)(struct iowait *wait);
77241056
MM
109 struct work_struct iowork;
110 wait_queue_head_t wait_dma;
14553ca1 111 wait_queue_head_t wait_pio;
77241056 112 atomic_t sdma_busy;
14553ca1 113 atomic_t pio_busy;
77241056
MM
114 u32 count;
115 u32 tx_limit;
116 u32 tx_count;
117};
118
119#define SDMA_AVAIL_REASON 0
120
121/**
122 * iowait_init() - initialize wait structure
123 * @wait: wait struct to initialize
124 * @tx_limit: limit for overflow queuing
125 * @func: restart function for workqueue
126 * @sleep: sleep function for no space
a545f530 127 * @resume: wakeup function for no space
77241056
MM
128 *
129 * This function initializes the iowait
130 * structure embedded in the QP or PQ.
131 *
132 */
133
134static inline void iowait_init(
135 struct iowait *wait,
136 u32 tx_limit,
137 void (*func)(struct work_struct *work),
138 int (*sleep)(
139 struct sdma_engine *sde,
140 struct iowait *wait,
141 struct sdma_txreq *tx,
142 unsigned seq),
a545f530
MM
143 void (*wakeup)(struct iowait *wait, int reason),
144 void (*sdma_drained)(struct iowait *wait))
77241056
MM
145{
146 wait->count = 0;
147 INIT_LIST_HEAD(&wait->list);
148 INIT_LIST_HEAD(&wait->tx_head);
149 INIT_WORK(&wait->iowork, func);
150 init_waitqueue_head(&wait->wait_dma);
14553ca1 151 init_waitqueue_head(&wait->wait_pio);
77241056 152 atomic_set(&wait->sdma_busy, 0);
14553ca1 153 atomic_set(&wait->pio_busy, 0);
77241056
MM
154 wait->tx_limit = tx_limit;
155 wait->sleep = sleep;
156 wait->wakeup = wakeup;
a545f530 157 wait->sdma_drained = sdma_drained;
77241056
MM
158}
159
160/**
161 * iowait_schedule() - initialize wait structure
162 * @wait: wait struct to schedule
163 * @wq: workqueue for schedule
0a226edd 164 * @cpu: cpu
77241056
MM
165 */
166static inline void iowait_schedule(
167 struct iowait *wait,
0a226edd
MM
168 struct workqueue_struct *wq,
169 int cpu)
77241056 170{
0a226edd 171 queue_work_on(cpu, wq, &wait->iowork);
77241056
MM
172}
173
174/**
175 * iowait_sdma_drain() - wait for DMAs to drain
176 *
177 * @wait: iowait structure
178 *
179 * This will delay until the iowait sdmas have
180 * completed.
181 */
182static inline void iowait_sdma_drain(struct iowait *wait)
183{
184 wait_event(wait->wait_dma, !atomic_read(&wait->sdma_busy));
185}
186
14553ca1
MM
187/**
188 * iowait_sdma_pending() - return sdma pending count
189 *
190 * @wait: iowait structure
191 *
192 */
193static inline int iowait_sdma_pending(struct iowait *wait)
194{
195 return atomic_read(&wait->sdma_busy);
196}
197
198/**
199 * iowait_sdma_inc - note sdma io pending
200 * @wait: iowait structure
201 */
202static inline void iowait_sdma_inc(struct iowait *wait)
203{
204 atomic_inc(&wait->sdma_busy);
205}
206
207/**
208 * iowait_sdma_add - add count to pending
209 * @wait: iowait structure
210 */
211static inline void iowait_sdma_add(struct iowait *wait, int count)
212{
213 atomic_add(count, &wait->sdma_busy);
214}
215
216/**
217 * iowait_sdma_dec - note sdma complete
218 * @wait: iowait structure
219 */
220static inline int iowait_sdma_dec(struct iowait *wait)
221{
222 return atomic_dec_and_test(&wait->sdma_busy);
223}
224
225/**
226 * iowait_pio_drain() - wait for pios to drain
227 *
228 * @wait: iowait structure
229 *
230 * This will delay until the iowait pios have
231 * completed.
232 */
233static inline void iowait_pio_drain(struct iowait *wait)
234{
235 wait_event_timeout(wait->wait_pio,
236 !atomic_read(&wait->pio_busy),
237 HZ);
238}
239
240/**
241 * iowait_pio_pending() - return pio pending count
242 *
243 * @wait: iowait structure
244 *
245 */
246static inline int iowait_pio_pending(struct iowait *wait)
247{
248 return atomic_read(&wait->pio_busy);
249}
250
251/**
252 * iowait_pio_inc - note pio pending
253 * @wait: iowait structure
254 */
255static inline void iowait_pio_inc(struct iowait *wait)
256{
257 atomic_inc(&wait->pio_busy);
258}
259
260/**
261 * iowait_sdma_dec - note pio complete
262 * @wait: iowait structure
263 */
264static inline int iowait_pio_dec(struct iowait *wait)
265{
266 return atomic_dec_and_test(&wait->pio_busy);
267}
268
77241056
MM
269/**
270 * iowait_drain_wakeup() - trigger iowait_drain() waiter
271 *
272 * @wait: iowait structure
273 *
274 * This will trigger any waiters.
275 */
276static inline void iowait_drain_wakeup(struct iowait *wait)
277{
278 wake_up(&wait->wait_dma);
14553ca1 279 wake_up(&wait->wait_pio);
a545f530
MM
280 if (wait->sdma_drained)
281 wait->sdma_drained(wait);
77241056
MM
282}
283
711e104d
MM
284/**
285 * iowait_get_txhead() - get packet off of iowait list
286 *
287 * @wait wait struture
288 */
289static inline struct sdma_txreq *iowait_get_txhead(struct iowait *wait)
290{
291 struct sdma_txreq *tx = NULL;
292
293 if (!list_empty(&wait->tx_head)) {
294 tx = list_first_entry(
295 &wait->tx_head,
296 struct sdma_txreq,
297 list);
298 list_del_init(&tx->list);
299 }
300 return tx;
301}
302
77241056 303#endif