zfcp_qdio.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * zfcp device driver
  4. *
  5. * Header file for zfcp qdio interface
  6. *
  7. * Copyright IBM Corp. 2010
  8. */
  9. #ifndef ZFCP_QDIO_H
  10. #define ZFCP_QDIO_H
  11. #include <asm/qdio.h>
  12. #define ZFCP_QDIO_SBALE_LEN PAGE_SIZE
  13. /* Max SBALS for chaining */
  14. #define ZFCP_QDIO_MAX_SBALS_PER_REQ 36
  15. /**
  16. * struct zfcp_qdio - basic qdio data structure
  17. * @res_q: response queue
  18. * @req_q: request queue
  19. * @req_q_idx: index of next free buffer
  20. * @req_q_free: number of free buffers in queue
  21. * @stat_lock: lock to protect req_q_util and req_q_time
  22. * @req_q_lock: lock to serialize access to request queue
  23. * @req_q_time: time of last fill level change
  24. * @req_q_util: used for accounting
  25. * @req_q_full: queue full incidents
  26. * @req_q_wq: used to wait for SBAL availability
  27. * @adapter: adapter used in conjunction with this qdio structure
  28. */
  29. struct zfcp_qdio {
  30. struct qdio_buffer *res_q[QDIO_MAX_BUFFERS_PER_Q];
  31. struct qdio_buffer *req_q[QDIO_MAX_BUFFERS_PER_Q];
  32. u8 req_q_idx;
  33. atomic_t req_q_free;
  34. spinlock_t stat_lock;
  35. spinlock_t req_q_lock;
  36. unsigned long long req_q_time;
  37. u64 req_q_util;
  38. atomic_t req_q_full;
  39. wait_queue_head_t req_q_wq;
  40. struct zfcp_adapter *adapter;
  41. u16 max_sbale_per_sbal;
  42. u16 max_sbale_per_req;
  43. };
  44. /**
  45. * struct zfcp_qdio_req - qdio queue related values for a request
  46. * @sbtype: sbal type flags for sbale 0
  47. * @sbal_number: number of free sbals
  48. * @sbal_first: first sbal for this request
  49. * @sbal_last: last sbal for this request
  50. * @sbal_limit: last possible sbal for this request
  51. * @sbale_curr: current sbale at creation of this request
  52. * @qdio_outb_usage: usage of outbound queue
  53. */
  54. struct zfcp_qdio_req {
  55. u8 sbtype;
  56. u8 sbal_number;
  57. u8 sbal_first;
  58. u8 sbal_last;
  59. u8 sbal_limit;
  60. u8 sbale_curr;
  61. u16 qdio_outb_usage;
  62. };
  63. /**
  64. * zfcp_qdio_sbale_req - return pointer to sbale on req_q for a request
  65. * @qdio: pointer to struct zfcp_qdio
  66. * @q_rec: pointer to struct zfcp_qdio_req
  67. * Returns: pointer to qdio_buffer_element (sbale) structure
  68. */
  69. static inline struct qdio_buffer_element *
  70. zfcp_qdio_sbale_req(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
  71. {
  72. return &qdio->req_q[q_req->sbal_last]->element[0];
  73. }
  74. /**
  75. * zfcp_qdio_sbale_curr - return current sbale on req_q for a request
  76. * @qdio: pointer to struct zfcp_qdio
  77. * @fsf_req: pointer to struct zfcp_fsf_req
  78. * Returns: pointer to qdio_buffer_element (sbale) structure
  79. */
  80. static inline struct qdio_buffer_element *
  81. zfcp_qdio_sbale_curr(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
  82. {
  83. return &qdio->req_q[q_req->sbal_last]->element[q_req->sbale_curr];
  84. }
  85. /**
  86. * zfcp_qdio_req_init - initialize qdio request
  87. * @qdio: request queue where to start putting the request
  88. * @q_req: the qdio request to start
  89. * @req_id: The request id
  90. * @sbtype: type flags to set for all sbals
  91. * @data: First data block
  92. * @len: Length of first data block
  93. *
  94. * This is the start of putting the request into the queue, the last
  95. * step is passing the request to zfcp_qdio_send. The request queue
  96. * lock must be held during the whole process from init to send.
  97. */
  98. static inline
  99. void zfcp_qdio_req_init(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req,
  100. unsigned long req_id, u8 sbtype, void *data, u32 len)
  101. {
  102. struct qdio_buffer_element *sbale;
  103. int count = min(atomic_read(&qdio->req_q_free),
  104. ZFCP_QDIO_MAX_SBALS_PER_REQ);
  105. q_req->sbal_first = q_req->sbal_last = qdio->req_q_idx;
  106. q_req->sbal_number = 1;
  107. q_req->sbtype = sbtype;
  108. q_req->sbale_curr = 1;
  109. q_req->sbal_limit = (q_req->sbal_first + count - 1)
  110. % QDIO_MAX_BUFFERS_PER_Q;
  111. sbale = zfcp_qdio_sbale_req(qdio, q_req);
  112. sbale->addr = (void *) req_id;
  113. sbale->eflags = 0;
  114. sbale->sflags = SBAL_SFLAGS0_COMMAND | sbtype;
  115. if (unlikely(!data))
  116. return;
  117. sbale++;
  118. sbale->addr = data;
  119. sbale->length = len;
  120. }
  121. /**
  122. * zfcp_qdio_fill_next - Fill next sbale, only for single sbal requests
  123. * @qdio: pointer to struct zfcp_qdio
  124. * @q_req: pointer to struct zfcp_queue_req
  125. *
  126. * This is only required for single sbal requests, calling it when
  127. * wrapping around to the next sbal is a bug.
  128. */
  129. static inline
  130. void zfcp_qdio_fill_next(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req,
  131. void *data, u32 len)
  132. {
  133. struct qdio_buffer_element *sbale;
  134. BUG_ON(q_req->sbale_curr == qdio->max_sbale_per_sbal - 1);
  135. q_req->sbale_curr++;
  136. sbale = zfcp_qdio_sbale_curr(qdio, q_req);
  137. sbale->addr = data;
  138. sbale->length = len;
  139. }
  140. /**
  141. * zfcp_qdio_set_sbale_last - set last entry flag in current sbale
  142. * @qdio: pointer to struct zfcp_qdio
  143. * @q_req: pointer to struct zfcp_queue_req
  144. */
  145. static inline
  146. void zfcp_qdio_set_sbale_last(struct zfcp_qdio *qdio,
  147. struct zfcp_qdio_req *q_req)
  148. {
  149. struct qdio_buffer_element *sbale;
  150. sbale = zfcp_qdio_sbale_curr(qdio, q_req);
  151. sbale->eflags |= SBAL_EFLAGS_LAST_ENTRY;
  152. }
  153. /**
  154. * zfcp_qdio_sg_one_sbal - check if one sbale is enough for sg data
  155. * @sg: The scatterlist where to check the data size
  156. *
  157. * Returns: 1 when one sbale is enough for the data in the scatterlist,
  158. * 0 if not.
  159. */
  160. static inline
  161. int zfcp_qdio_sg_one_sbale(struct scatterlist *sg)
  162. {
  163. return sg_is_last(sg) && sg->length <= ZFCP_QDIO_SBALE_LEN;
  164. }
  165. /**
  166. * zfcp_qdio_skip_to_last_sbale - skip to last sbale in sbal
  167. * @q_req: The current zfcp_qdio_req
  168. */
  169. static inline
  170. void zfcp_qdio_skip_to_last_sbale(struct zfcp_qdio *qdio,
  171. struct zfcp_qdio_req *q_req)
  172. {
  173. q_req->sbale_curr = qdio->max_sbale_per_sbal - 1;
  174. }
  175. /**
  176. * zfcp_qdio_sbal_limit - set the sbal limit for a request in q_req
  177. * @qdio: pointer to struct zfcp_qdio
  178. * @q_req: The current zfcp_qdio_req
  179. * @max_sbals: maximum number of SBALs allowed
  180. */
  181. static inline
  182. void zfcp_qdio_sbal_limit(struct zfcp_qdio *qdio,
  183. struct zfcp_qdio_req *q_req, int max_sbals)
  184. {
  185. int count = min(atomic_read(&qdio->req_q_free), max_sbals);
  186. q_req->sbal_limit = (q_req->sbal_first + count - 1) %
  187. QDIO_MAX_BUFFERS_PER_Q;
  188. }
  189. /**
  190. * zfcp_qdio_set_data_div - set data division count
  191. * @qdio: pointer to struct zfcp_qdio
  192. * @q_req: The current zfcp_qdio_req
  193. * @count: The data division count
  194. */
  195. static inline
  196. void zfcp_qdio_set_data_div(struct zfcp_qdio *qdio,
  197. struct zfcp_qdio_req *q_req, u32 count)
  198. {
  199. struct qdio_buffer_element *sbale;
  200. sbale = qdio->req_q[q_req->sbal_first]->element;
  201. sbale->length = count;
  202. }
  203. /**
  204. * zfcp_qdio_real_bytes - count bytes used
  205. * @sg: pointer to struct scatterlist
  206. */
  207. static inline
  208. unsigned int zfcp_qdio_real_bytes(struct scatterlist *sg)
  209. {
  210. unsigned int real_bytes = 0;
  211. for (; sg; sg = sg_next(sg))
  212. real_bytes += sg->length;
  213. return real_bytes;
  214. }
  215. /**
  216. * zfcp_qdio_set_scount - set SBAL count value
  217. * @qdio: pointer to struct zfcp_qdio
  218. * @q_req: The current zfcp_qdio_req
  219. */
  220. static inline
  221. void zfcp_qdio_set_scount(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
  222. {
  223. struct qdio_buffer_element *sbale;
  224. sbale = qdio->req_q[q_req->sbal_first]->element;
  225. sbale->scount = q_req->sbal_number - 1;
  226. }
  227. #endif /* ZFCP_QDIO_H */