1 /* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause */
3 * Copyright(c) 2015 - 2018 Intel Corporation.
9 #include <linux/list.h>
10 #include <linux/workqueue.h>
11 #include <linux/wait.h>
12 #include <linux/sched.h>
14 #include "sdma_txreq.h"
17 * typedef (*restart_t)() - restart callback
18 * @work: pointer to work structure
20 typedef void (*restart_t
)(struct work_struct
*work
);
22 #define IOWAIT_PENDING_IB 0x0
23 #define IOWAIT_PENDING_TID 0x1
26 * A QP can have multiple Send Engines (SEs).
28 * The current use case is for supporting a TID RDMA
29 * packet build/xmit mechanism independent from verbs.
32 #define IOWAIT_IB_SE 0
33 #define IOWAIT_TID_SE 1
38 * @iowork: the work struct
39 * @tx_head: list of prebuilt packets
40 * @iow: the parent iowait structure
42 * This structure is the work item (process) specific
43 * details associated with the each of the two SEs of the
46 * The workstruct and the queued TXs are unique to each
51 struct work_struct iowork
;
52 struct list_head tx_head
;
57 * @list: used to add/insert into QP/PQ wait lists
58 * @tx_head: overflow list of sdma_txreq's
59 * @sleep: no space callback
60 * @wakeup: space callback wakeup
61 * @sdma_drained: sdma count drained
62 * @init_priority: callback to manipulate priority
63 * @lock: lock protected head of wait queue
64 * @iowork: workqueue overhead
65 * @wait_dma: wait for sdma_busy == 0
66 * @wait_pio: wait for pio_busy == 0
67 * @sdma_busy: # of packets in flight
68 * @count: total number of descriptors in tx_head'ed list
69 * @tx_limit: limit for overflow queuing
70 * @tx_count: number of tx entry's in tx_head'ed list
71 * @flags: wait flags (one per QP)
72 * @wait: SE array for multiple legs
74 * This is to be embedded in user's state structure
77 * The sleep and wakeup members are a
78 * bit misnamed. They do not strictly
79 * speaking sleep or wake up, but they
80 * are callbacks for the ULP to implement
81 * what ever queuing/dequeuing of
82 * the embedded iowait and its containing struct
83 * when a resource shortage like SDMA ring space
84 * or PIO credit space is seen.
86 * Both potentially have locks help
87 * so sleeping is not allowed and it is not
88 * supported to submit txreqs from the wakeup
89 * call directly because of lock conflicts.
91 * The wait_dma member along with the iow
93 * The lock field is used by waiters to record
94 * the seqlock_t that guards the list head.
95 * Waiters explicity know that, but the destroy
96 * code that unwaits QPs does not.
99 struct list_head list
;
101 struct sdma_engine
*sde
,
102 struct iowait_work
*wait
,
103 struct sdma_txreq
*tx
,
107 void (*wakeup
)(struct iowait
*wait
, int reason
);
108 void (*sdma_drained
)(struct iowait
*wait
);
109 void (*init_priority
)(struct iowait
*wait
);
111 wait_queue_head_t wait_dma
;
112 wait_queue_head_t wait_pio
;
121 struct iowait_work wait
[IOWAIT_SES
];
124 #define SDMA_AVAIL_REASON 0
126 void iowait_set_flag(struct iowait
*wait
, u32 flag
);
127 bool iowait_flag_set(struct iowait
*wait
, u32 flag
);
128 void iowait_clear_flag(struct iowait
*wait
, u32 flag
);
130 void iowait_init(struct iowait
*wait
, u32 tx_limit
,
131 void (*func
)(struct work_struct
*work
),
132 void (*tidfunc
)(struct work_struct
*work
),
133 int (*sleep
)(struct sdma_engine
*sde
,
134 struct iowait_work
*wait
,
135 struct sdma_txreq
*tx
,
138 void (*wakeup
)(struct iowait
*wait
, int reason
),
139 void (*sdma_drained
)(struct iowait
*wait
),
140 void (*init_priority
)(struct iowait
*wait
));
143 * iowait_schedule() - schedule the default send engine work
144 * @wait: wait struct to schedule
145 * @wq: workqueue for schedule
148 static inline bool iowait_schedule(struct iowait
*wait
,
149 struct workqueue_struct
*wq
, int cpu
)
151 return !!queue_work_on(cpu
, wq
, &wait
->wait
[IOWAIT_IB_SE
].iowork
);
155 * iowait_tid_schedule - schedule the tid SE
156 * @wait: the iowait structure
157 * @wq: the work queue
160 static inline bool iowait_tid_schedule(struct iowait
*wait
,
161 struct workqueue_struct
*wq
, int cpu
)
163 return !!queue_work_on(cpu
, wq
, &wait
->wait
[IOWAIT_TID_SE
].iowork
);
167 * iowait_sdma_drain() - wait for DMAs to drain
169 * @wait: iowait structure
171 * This will delay until the iowait sdmas have
174 static inline void iowait_sdma_drain(struct iowait
*wait
)
176 wait_event(wait
->wait_dma
, !atomic_read(&wait
->sdma_busy
));
180 * iowait_sdma_pending() - return sdma pending count
182 * @wait: iowait structure
185 static inline int iowait_sdma_pending(struct iowait
*wait
)
187 return atomic_read(&wait
->sdma_busy
);
191 * iowait_sdma_inc - note sdma io pending
192 * @wait: iowait structure
194 static inline void iowait_sdma_inc(struct iowait
*wait
)
196 atomic_inc(&wait
->sdma_busy
);
200 * iowait_sdma_add - add count to pending
201 * @wait: iowait structure
203 static inline void iowait_sdma_add(struct iowait
*wait
, int count
)
205 atomic_add(count
, &wait
->sdma_busy
);
209 * iowait_sdma_dec - note sdma complete
210 * @wait: iowait structure
212 static inline int iowait_sdma_dec(struct iowait
*wait
)
216 return atomic_dec_and_test(&wait
->sdma_busy
);
220 * iowait_pio_drain() - wait for pios to drain
222 * @wait: iowait structure
224 * This will delay until the iowait pios have
227 static inline void iowait_pio_drain(struct iowait
*wait
)
229 wait_event_timeout(wait
->wait_pio
,
230 !atomic_read(&wait
->pio_busy
),
235 * iowait_pio_pending() - return pio pending count
237 * @wait: iowait structure
240 static inline int iowait_pio_pending(struct iowait
*wait
)
242 return atomic_read(&wait
->pio_busy
);
246 * iowait_pio_inc - note pio pending
247 * @wait: iowait structure
249 static inline void iowait_pio_inc(struct iowait
*wait
)
251 atomic_inc(&wait
->pio_busy
);
255 * iowait_pio_dec - note pio complete
256 * @wait: iowait structure
258 static inline int iowait_pio_dec(struct iowait
*wait
)
262 return atomic_dec_and_test(&wait
->pio_busy
);
266 * iowait_drain_wakeup() - trigger iowait_drain() waiter
268 * @wait: iowait structure
270 * This will trigger any waiters.
272 static inline void iowait_drain_wakeup(struct iowait
*wait
)
274 wake_up(&wait
->wait_dma
);
275 wake_up(&wait
->wait_pio
);
276 if (wait
->sdma_drained
)
277 wait
->sdma_drained(wait
);
281 * iowait_get_txhead() - get packet off of iowait list
283 * @wait: iowait_work structure
285 static inline struct sdma_txreq
*iowait_get_txhead(struct iowait_work
*wait
)
287 struct sdma_txreq
*tx
= NULL
;
289 if (!list_empty(&wait
->tx_head
)) {
290 tx
= list_first_entry(
294 list_del_init(&tx
->list
);
299 static inline u16
iowait_get_desc(struct iowait_work
*w
)
302 struct sdma_txreq
*tx
= NULL
;
304 if (!list_empty(&w
->tx_head
)) {
305 tx
= list_first_entry(&w
->tx_head
, struct sdma_txreq
,
307 num_desc
= tx
->num_desc
;
308 if (tx
->flags
& SDMA_TXREQ_F_VIP
)
314 static inline u32
iowait_get_all_desc(struct iowait
*w
)
318 num_desc
= iowait_get_desc(&w
->wait
[IOWAIT_IB_SE
]);
319 num_desc
+= iowait_get_desc(&w
->wait
[IOWAIT_TID_SE
]);
323 static inline void iowait_update_priority(struct iowait_work
*w
)
325 struct sdma_txreq
*tx
= NULL
;
327 if (!list_empty(&w
->tx_head
)) {
328 tx
= list_first_entry(&w
->tx_head
, struct sdma_txreq
,
330 if (tx
->flags
& SDMA_TXREQ_F_VIP
)
335 static inline void iowait_update_all_priority(struct iowait
*w
)
337 iowait_update_priority(&w
->wait
[IOWAIT_IB_SE
]);
338 iowait_update_priority(&w
->wait
[IOWAIT_TID_SE
]);
341 static inline void iowait_init_priority(struct iowait
*w
)
344 if (w
->init_priority
)
348 static inline void iowait_get_priority(struct iowait
*w
)
350 iowait_init_priority(w
);
351 iowait_update_all_priority(w
);
355 * iowait_queue - Put the iowait on a wait queue
356 * @pkts_sent: have some packets been sent before queuing?
357 * @w: the iowait struct
358 * @wait_head: the wait queue
360 * This function is called to insert an iowait struct into a
361 * wait queue after a resource (eg, sdma descriptor or pio
362 * buffer) is run out.
364 static inline void iowait_queue(bool pkts_sent
, struct iowait
*w
,
365 struct list_head
*wait_head
)
368 * To play fair, insert the iowait at the tail of the wait queue if it
369 * has already sent some packets; Otherwise, put it at the head.
370 * However, if it has priority packets to send, also put it at the
378 if (w
->priority
> 0 || !pkts_sent
)
379 list_add(&w
->list
, wait_head
);
381 list_add_tail(&w
->list
, wait_head
);
385 * iowait_starve_clear - clear the wait queue's starve count
386 * @pkts_sent: have some packets been sent?
387 * @w: the iowait struct
389 * This function is called to clear the starve count. If no
390 * packets have been sent, the starve count will not be cleared.
392 static inline void iowait_starve_clear(bool pkts_sent
, struct iowait
*w
)
398 /* Update the top priority index */
399 uint
iowait_priority_update_top(struct iowait
*w
,
401 uint idx
, uint top_idx
);
404 * iowait_packet_queued() - determine if a packet is queued
405 * @wait: the iowait_work structure
407 static inline bool iowait_packet_queued(struct iowait_work
*wait
)
409 return !list_empty(&wait
->tx_head
);
413 * inc_wait_count - increment wait counts
414 * @w: the log work struct
417 static inline void iowait_inc_wait_count(struct iowait_work
*w
, u16 n
)
426 * iowait_get_tid_work - return iowait_work for tid SE
427 * @w: the iowait struct
429 static inline struct iowait_work
*iowait_get_tid_work(struct iowait
*w
)
431 return &w
->wait
[IOWAIT_TID_SE
];
435 * iowait_get_ib_work - return iowait_work for ib SE
436 * @w: the iowait struct
438 static inline struct iowait_work
*iowait_get_ib_work(struct iowait
*w
)
440 return &w
->wait
[IOWAIT_IB_SE
];
444 * iowait_ioww_to_iow - return iowait given iowait_work
445 * @w: the iowait_work struct
447 static inline struct iowait
*iowait_ioww_to_iow(struct iowait_work
*w
)
454 void iowait_cancel_work(struct iowait
*w
);
455 int iowait_set_work_flag(struct iowait_work
*w
);