iowait.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /* SPDX-License-Identifier: GPL-2.0 or BSD-3-Clause */
  2. /*
  3. * Copyright(c) 2015 - 2018 Intel Corporation.
  4. */
  5. #ifndef _HFI1_IOWAIT_H
  6. #define _HFI1_IOWAIT_H
  7. #include <linux/list.h>
  8. #include <linux/workqueue.h>
  9. #include <linux/wait.h>
  10. #include <linux/sched.h>
  11. #include "sdma_txreq.h"
  12. /*
  13. * typedef (*restart_t)() - restart callback
  14. * @work: pointer to work structure
  15. */
  16. typedef void (*restart_t)(struct work_struct *work);
  17. #define IOWAIT_PENDING_IB 0x0
  18. #define IOWAIT_PENDING_TID 0x1
  19. /*
  20. * A QP can have multiple Send Engines (SEs).
  21. *
  22. * The current use case is for supporting a TID RDMA
  23. * packet build/xmit mechanism independent from verbs.
  24. */
  25. #define IOWAIT_SES 2
  26. #define IOWAIT_IB_SE 0
  27. #define IOWAIT_TID_SE 1
  28. struct sdma_txreq;
  29. struct sdma_engine;
  30. /**
  31. * @iowork: the work struct
  32. * @tx_head: list of prebuilt packets
  33. * @iow: the parent iowait structure
  34. *
  35. * This structure is the work item (process) specific
  36. * details associated with the each of the two SEs of the
  37. * QP.
  38. *
  39. * The workstruct and the queued TXs are unique to each
  40. * SE.
  41. */
  42. struct iowait;
  43. struct iowait_work {
  44. struct work_struct iowork;
  45. struct list_head tx_head;
  46. struct iowait *iow;
  47. };
  48. /**
  49. * @list: used to add/insert into QP/PQ wait lists
  50. * @tx_head: overflow list of sdma_txreq's
  51. * @sleep: no space callback
  52. * @wakeup: space callback wakeup
  53. * @sdma_drained: sdma count drained
  54. * @init_priority: callback to manipulate priority
  55. * @lock: lock protected head of wait queue
  56. * @iowork: workqueue overhead
  57. * @wait_dma: wait for sdma_busy == 0
  58. * @wait_pio: wait for pio_busy == 0
  59. * @sdma_busy: # of packets in flight
  60. * @count: total number of descriptors in tx_head'ed list
  61. * @tx_limit: limit for overflow queuing
  62. * @tx_count: number of tx entry's in tx_head'ed list
  63. * @flags: wait flags (one per QP)
  64. * @wait: SE array for multiple legs
  65. *
  66. * This is to be embedded in user's state structure
  67. * (QP or PQ).
  68. *
  69. * The sleep and wakeup members are a
  70. * bit misnamed. They do not strictly
  71. * speaking sleep or wake up, but they
  72. * are callbacks for the ULP to implement
  73. * what ever queuing/dequeuing of
  74. * the embedded iowait and its containing struct
  75. * when a resource shortage like SDMA ring space
  76. * or PIO credit space is seen.
  77. *
  78. * Both potentially have locks help
  79. * so sleeping is not allowed and it is not
  80. * supported to submit txreqs from the wakeup
  81. * call directly because of lock conflicts.
  82. *
  83. * The wait_dma member along with the iow
  84. *
  85. * The lock field is used by waiters to record
  86. * the seqlock_t that guards the list head.
  87. * Waiters explicity know that, but the destroy
  88. * code that unwaits QPs does not.
  89. */
  90. struct iowait {
  91. struct list_head list;
  92. int (*sleep)(
  93. struct sdma_engine *sde,
  94. struct iowait_work *wait,
  95. struct sdma_txreq *tx,
  96. uint seq,
  97. bool pkts_sent
  98. );
  99. void (*wakeup)(struct iowait *wait, int reason);
  100. void (*sdma_drained)(struct iowait *wait);
  101. void (*init_priority)(struct iowait *wait);
  102. seqlock_t *lock;
  103. wait_queue_head_t wait_dma;
  104. wait_queue_head_t wait_pio;
  105. atomic_t sdma_busy;
  106. atomic_t pio_busy;
  107. u32 count;
  108. u32 tx_limit;
  109. u32 tx_count;
  110. u8 starved_cnt;
  111. u8 priority;
  112. unsigned long flags;
  113. struct iowait_work wait[IOWAIT_SES];
  114. };
  115. #define SDMA_AVAIL_REASON 0
  116. void iowait_set_flag(struct iowait *wait, u32 flag);
  117. bool iowait_flag_set(struct iowait *wait, u32 flag);
  118. void iowait_clear_flag(struct iowait *wait, u32 flag);
  119. void iowait_init(struct iowait *wait, u32 tx_limit,
  120. void (*func)(struct work_struct *work),
  121. void (*tidfunc)(struct work_struct *work),
  122. int (*sleep)(struct sdma_engine *sde,
  123. struct iowait_work *wait,
  124. struct sdma_txreq *tx,
  125. uint seq,
  126. bool pkts_sent),
  127. void (*wakeup)(struct iowait *wait, int reason),
  128. void (*sdma_drained)(struct iowait *wait),
  129. void (*init_priority)(struct iowait *wait));
  130. /**
  131. * iowait_schedule() - schedule the default send engine work
  132. * @wait: wait struct to schedule
  133. * @wq: workqueue for schedule
  134. * @cpu: cpu
  135. */
  136. static inline bool iowait_schedule(struct iowait *wait,
  137. struct workqueue_struct *wq, int cpu)
  138. {
  139. return !!queue_work_on(cpu, wq, &wait->wait[IOWAIT_IB_SE].iowork);
  140. }
  141. /**
  142. * iowait_tid_schedule - schedule the tid SE
  143. * @wait: the iowait structure
  144. * @wq: the work queue
  145. * @cpu: the cpu
  146. */
  147. static inline bool iowait_tid_schedule(struct iowait *wait,
  148. struct workqueue_struct *wq, int cpu)
  149. {
  150. return !!queue_work_on(cpu, wq, &wait->wait[IOWAIT_TID_SE].iowork);
  151. }
  152. /**
  153. * iowait_sdma_drain() - wait for DMAs to drain
  154. *
  155. * @wait: iowait structure
  156. *
  157. * This will delay until the iowait sdmas have
  158. * completed.
  159. */
  160. static inline void iowait_sdma_drain(struct iowait *wait)
  161. {
  162. wait_event(wait->wait_dma, !atomic_read(&wait->sdma_busy));
  163. }
  164. /**
  165. * iowait_sdma_pending() - return sdma pending count
  166. *
  167. * @wait: iowait structure
  168. *
  169. */
  170. static inline int iowait_sdma_pending(struct iowait *wait)
  171. {
  172. return atomic_read(&wait->sdma_busy);
  173. }
  174. /**
  175. * iowait_sdma_inc - note sdma io pending
  176. * @wait: iowait structure
  177. */
  178. static inline void iowait_sdma_inc(struct iowait *wait)
  179. {
  180. atomic_inc(&wait->sdma_busy);
  181. }
  182. /**
  183. * iowait_sdma_add - add count to pending
  184. * @wait: iowait structure
  185. */
  186. static inline void iowait_sdma_add(struct iowait *wait, int count)
  187. {
  188. atomic_add(count, &wait->sdma_busy);
  189. }
  190. /**
  191. * iowait_sdma_dec - note sdma complete
  192. * @wait: iowait structure
  193. */
  194. static inline int iowait_sdma_dec(struct iowait *wait)
  195. {
  196. if (!wait)
  197. return 0;
  198. return atomic_dec_and_test(&wait->sdma_busy);
  199. }
  200. /**
  201. * iowait_pio_drain() - wait for pios to drain
  202. *
  203. * @wait: iowait structure
  204. *
  205. * This will delay until the iowait pios have
  206. * completed.
  207. */
  208. static inline void iowait_pio_drain(struct iowait *wait)
  209. {
  210. wait_event_timeout(wait->wait_pio,
  211. !atomic_read(&wait->pio_busy),
  212. HZ);
  213. }
  214. /**
  215. * iowait_pio_pending() - return pio pending count
  216. *
  217. * @wait: iowait structure
  218. *
  219. */
  220. static inline int iowait_pio_pending(struct iowait *wait)
  221. {
  222. return atomic_read(&wait->pio_busy);
  223. }
  224. /**
  225. * iowait_pio_inc - note pio pending
  226. * @wait: iowait structure
  227. */
  228. static inline void iowait_pio_inc(struct iowait *wait)
  229. {
  230. atomic_inc(&wait->pio_busy);
  231. }
  232. /**
  233. * iowait_pio_dec - note pio complete
  234. * @wait: iowait structure
  235. */
  236. static inline int iowait_pio_dec(struct iowait *wait)
  237. {
  238. if (!wait)
  239. return 0;
  240. return atomic_dec_and_test(&wait->pio_busy);
  241. }
  242. /**
  243. * iowait_drain_wakeup() - trigger iowait_drain() waiter
  244. *
  245. * @wait: iowait structure
  246. *
  247. * This will trigger any waiters.
  248. */
  249. static inline void iowait_drain_wakeup(struct iowait *wait)
  250. {
  251. wake_up(&wait->wait_dma);
  252. wake_up(&wait->wait_pio);
  253. if (wait->sdma_drained)
  254. wait->sdma_drained(wait);
  255. }
  256. /**
  257. * iowait_get_txhead() - get packet off of iowait list
  258. *
  259. * @wait: iowait_work structure
  260. */
  261. static inline struct sdma_txreq *iowait_get_txhead(struct iowait_work *wait)
  262. {
  263. struct sdma_txreq *tx = NULL;
  264. if (!list_empty(&wait->tx_head)) {
  265. tx = list_first_entry(
  266. &wait->tx_head,
  267. struct sdma_txreq,
  268. list);
  269. list_del_init(&tx->list);
  270. }
  271. return tx;
  272. }
  273. static inline u16 iowait_get_desc(struct iowait_work *w)
  274. {
  275. u16 num_desc = 0;
  276. struct sdma_txreq *tx = NULL;
  277. if (!list_empty(&w->tx_head)) {
  278. tx = list_first_entry(&w->tx_head, struct sdma_txreq,
  279. list);
  280. num_desc = tx->num_desc;
  281. if (tx->flags & SDMA_TXREQ_F_VIP)
  282. w->iow->priority++;
  283. }
  284. return num_desc;
  285. }
  286. static inline u32 iowait_get_all_desc(struct iowait *w)
  287. {
  288. u32 num_desc = 0;
  289. num_desc = iowait_get_desc(&w->wait[IOWAIT_IB_SE]);
  290. num_desc += iowait_get_desc(&w->wait[IOWAIT_TID_SE]);
  291. return num_desc;
  292. }
  293. static inline void iowait_update_priority(struct iowait_work *w)
  294. {
  295. struct sdma_txreq *tx = NULL;
  296. if (!list_empty(&w->tx_head)) {
  297. tx = list_first_entry(&w->tx_head, struct sdma_txreq,
  298. list);
  299. if (tx->flags & SDMA_TXREQ_F_VIP)
  300. w->iow->priority++;
  301. }
  302. }
  303. static inline void iowait_update_all_priority(struct iowait *w)
  304. {
  305. iowait_update_priority(&w->wait[IOWAIT_IB_SE]);
  306. iowait_update_priority(&w->wait[IOWAIT_TID_SE]);
  307. }
  308. static inline void iowait_init_priority(struct iowait *w)
  309. {
  310. w->priority = 0;
  311. if (w->init_priority)
  312. w->init_priority(w);
  313. }
  314. static inline void iowait_get_priority(struct iowait *w)
  315. {
  316. iowait_init_priority(w);
  317. iowait_update_all_priority(w);
  318. }
  319. /**
  320. * iowait_queue - Put the iowait on a wait queue
  321. * @pkts_sent: have some packets been sent before queuing?
  322. * @w: the iowait struct
  323. * @wait_head: the wait queue
  324. *
  325. * This function is called to insert an iowait struct into a
  326. * wait queue after a resource (eg, sdma descriptor or pio
  327. * buffer) is run out.
  328. */
  329. static inline void iowait_queue(bool pkts_sent, struct iowait *w,
  330. struct list_head *wait_head)
  331. {
  332. /*
  333. * To play fair, insert the iowait at the tail of the wait queue if it
  334. * has already sent some packets; Otherwise, put it at the head.
  335. * However, if it has priority packets to send, also put it at the
  336. * head.
  337. */
  338. if (pkts_sent)
  339. w->starved_cnt = 0;
  340. else
  341. w->starved_cnt++;
  342. if (w->priority > 0 || !pkts_sent)
  343. list_add(&w->list, wait_head);
  344. else
  345. list_add_tail(&w->list, wait_head);
  346. }
  347. /**
  348. * iowait_starve_clear - clear the wait queue's starve count
  349. * @pkts_sent: have some packets been sent?
  350. * @w: the iowait struct
  351. *
  352. * This function is called to clear the starve count. If no
  353. * packets have been sent, the starve count will not be cleared.
  354. */
  355. static inline void iowait_starve_clear(bool pkts_sent, struct iowait *w)
  356. {
  357. if (pkts_sent)
  358. w->starved_cnt = 0;
  359. }
  360. /* Update the top priority index */
  361. uint iowait_priority_update_top(struct iowait *w,
  362. struct iowait *top,
  363. uint idx, uint top_idx);
  364. /**
  365. * iowait_packet_queued() - determine if a packet is queued
  366. * @wait: the iowait_work structure
  367. */
  368. static inline bool iowait_packet_queued(struct iowait_work *wait)
  369. {
  370. return !list_empty(&wait->tx_head);
  371. }
  372. /**
  373. * inc_wait_count - increment wait counts
  374. * @w: the log work struct
  375. * @n: the count
  376. */
  377. static inline void iowait_inc_wait_count(struct iowait_work *w, u16 n)
  378. {
  379. if (!w)
  380. return;
  381. w->iow->tx_count++;
  382. w->iow->count += n;
  383. }
  384. /**
  385. * iowait_get_tid_work - return iowait_work for tid SE
  386. * @w: the iowait struct
  387. */
  388. static inline struct iowait_work *iowait_get_tid_work(struct iowait *w)
  389. {
  390. return &w->wait[IOWAIT_TID_SE];
  391. }
  392. /**
  393. * iowait_get_ib_work - return iowait_work for ib SE
  394. * @w: the iowait struct
  395. */
  396. static inline struct iowait_work *iowait_get_ib_work(struct iowait *w)
  397. {
  398. return &w->wait[IOWAIT_IB_SE];
  399. }
  400. /**
  401. * iowait_ioww_to_iow - return iowait given iowait_work
  402. * @w: the iowait_work struct
  403. */
  404. static inline struct iowait *iowait_ioww_to_iow(struct iowait_work *w)
  405. {
  406. if (likely(w))
  407. return w->iow;
  408. return NULL;
  409. }
  410. void iowait_cancel_work(struct iowait *w);
  411. int iowait_set_work_flag(struct iowait_work *w);
  412. #endif