itcw.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Functions for incremental construction of fcx enabled I/O control blocks.
  4. *
  5. * Copyright IBM Corp. 2008
  6. * Author(s): Peter Oberparleiter <[email protected]>
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/types.h>
  10. #include <linux/string.h>
  11. #include <linux/errno.h>
  12. #include <linux/err.h>
  13. #include <linux/module.h>
  14. #include <asm/fcx.h>
  15. #include <asm/itcw.h>
  16. /*
  17. * struct itcw - incremental tcw helper data type
  18. *
  19. * This structure serves as a handle for the incremental construction of a
  20. * tcw and associated tccb, tsb, data tidaw-list plus an optional interrogate
  21. * tcw and associated data. The data structures are contained inside a single
  22. * contiguous buffer provided by the user.
  23. *
  24. * The itcw construction functions take care of overall data integrity:
  25. * - reset unused fields to zero
  26. * - fill in required pointers
  27. * - ensure required alignment for data structures
  28. * - prevent data structures to cross 4k-byte boundary where required
  29. * - calculate tccb-related length fields
  30. * - optionally provide ready-made interrogate tcw and associated structures
  31. *
  32. * Restrictions apply to the itcws created with these construction functions:
  33. * - tida only supported for data address, not for tccb
  34. * - only contiguous tidaw-lists (no ttic)
  35. * - total number of bytes required per itcw may not exceed 4k bytes
  36. * - either read or write operation (may not work with r=0 and w=0)
  37. *
  38. * Example:
  39. * struct itcw *itcw;
  40. * void *buffer;
  41. * size_t size;
  42. *
  43. * size = itcw_calc_size(1, 2, 0);
  44. * buffer = kmalloc(size, GFP_KERNEL | GFP_DMA);
  45. * if (!buffer)
  46. * return -ENOMEM;
  47. * itcw = itcw_init(buffer, size, ITCW_OP_READ, 1, 2, 0);
  48. * if (IS_ERR(itcw))
  49. * return PTR_ER(itcw);
  50. * itcw_add_dcw(itcw, 0x2, 0, NULL, 0, 72);
  51. * itcw_add_tidaw(itcw, 0, 0x30000, 20);
  52. * itcw_add_tidaw(itcw, 0, 0x40000, 52);
  53. * itcw_finalize(itcw);
  54. *
  55. */
  56. struct itcw {
  57. struct tcw *tcw;
  58. struct tcw *intrg_tcw;
  59. int num_tidaws;
  60. int max_tidaws;
  61. int intrg_num_tidaws;
  62. int intrg_max_tidaws;
  63. };
  64. /**
  65. * itcw_get_tcw - return pointer to tcw associated with the itcw
  66. * @itcw: address of the itcw
  67. *
  68. * Return pointer to the tcw associated with the itcw.
  69. */
  70. struct tcw *itcw_get_tcw(struct itcw *itcw)
  71. {
  72. return itcw->tcw;
  73. }
  74. EXPORT_SYMBOL(itcw_get_tcw);
  75. /**
  76. * itcw_calc_size - return the size of an itcw with the given parameters
  77. * @intrg: if non-zero, add an interrogate tcw
  78. * @max_tidaws: maximum number of tidaws to be used for data addressing or zero
  79. * if no tida is to be used.
  80. * @intrg_max_tidaws: maximum number of tidaws to be used for data addressing
  81. * by the interrogate tcw, if specified
  82. *
  83. * Calculate and return the number of bytes required to hold an itcw with the
  84. * given parameters and assuming tccbs with maximum size.
  85. *
  86. * Note that the resulting size also contains bytes needed for alignment
  87. * padding as well as padding to ensure that data structures don't cross a
  88. * 4k-boundary where required.
  89. */
  90. size_t itcw_calc_size(int intrg, int max_tidaws, int intrg_max_tidaws)
  91. {
  92. size_t len;
  93. int cross_count;
  94. /* Main data. */
  95. len = sizeof(struct itcw);
  96. len += /* TCW */ sizeof(struct tcw) + /* TCCB */ TCCB_MAX_SIZE +
  97. /* TSB */ sizeof(struct tsb) +
  98. /* TIDAL */ max_tidaws * sizeof(struct tidaw);
  99. /* Interrogate data. */
  100. if (intrg) {
  101. len += /* TCW */ sizeof(struct tcw) + /* TCCB */ TCCB_MAX_SIZE +
  102. /* TSB */ sizeof(struct tsb) +
  103. /* TIDAL */ intrg_max_tidaws * sizeof(struct tidaw);
  104. }
  105. /* Maximum required alignment padding. */
  106. len += /* Initial TCW */ 63 + /* Interrogate TCCB */ 7;
  107. /* TIDAW lists may not cross a 4k boundary. To cross a
  108. * boundary we need to add a TTIC TIDAW. We need to reserve
  109. * one additional TIDAW for a TTIC that we may need to add due
  110. * to the placement of the data chunk in memory, and a further
  111. * TIDAW for each page boundary that the TIDAW list may cross
  112. * due to it's own size.
  113. */
  114. if (max_tidaws) {
  115. cross_count = 1 + ((max_tidaws * sizeof(struct tidaw) - 1)
  116. >> PAGE_SHIFT);
  117. len += cross_count * sizeof(struct tidaw);
  118. }
  119. if (intrg_max_tidaws) {
  120. cross_count = 1 + ((intrg_max_tidaws * sizeof(struct tidaw) - 1)
  121. >> PAGE_SHIFT);
  122. len += cross_count * sizeof(struct tidaw);
  123. }
  124. return len;
  125. }
  126. EXPORT_SYMBOL(itcw_calc_size);
  127. #define CROSS4K(x, l) (((x) & ~4095) != ((x + l) & ~4095))
  128. static inline void *fit_chunk(addr_t *start, addr_t end, size_t len,
  129. int align, int check_4k)
  130. {
  131. addr_t addr;
  132. addr = ALIGN(*start, align);
  133. if (check_4k && CROSS4K(addr, len)) {
  134. addr = ALIGN(addr, 4096);
  135. addr = ALIGN(addr, align);
  136. }
  137. if (addr + len > end)
  138. return ERR_PTR(-ENOSPC);
  139. *start = addr + len;
  140. return (void *) addr;
  141. }
  142. /**
  143. * itcw_init - initialize incremental tcw data structure
  144. * @buffer: address of buffer to use for data structures
  145. * @size: number of bytes in buffer
  146. * @op: %ITCW_OP_READ for a read operation tcw, %ITCW_OP_WRITE for a write
  147. * operation tcw
  148. * @intrg: if non-zero, add and initialize an interrogate tcw
  149. * @max_tidaws: maximum number of tidaws to be used for data addressing or zero
  150. * if no tida is to be used.
  151. * @intrg_max_tidaws: maximum number of tidaws to be used for data addressing
  152. * by the interrogate tcw, if specified
  153. *
  154. * Prepare the specified buffer to be used as an incremental tcw, i.e. a
  155. * helper data structure that can be used to construct a valid tcw by
  156. * successive calls to other helper functions. Note: the buffer needs to be
  157. * located below the 2G address limit. The resulting tcw has the following
  158. * restrictions:
  159. * - no tccb tidal
  160. * - input/output tidal is contiguous (no ttic)
  161. * - total data should not exceed 4k
  162. * - tcw specifies either read or write operation
  163. *
  164. * On success, return pointer to the resulting incremental tcw data structure,
  165. * ERR_PTR otherwise.
  166. */
  167. struct itcw *itcw_init(void *buffer, size_t size, int op, int intrg,
  168. int max_tidaws, int intrg_max_tidaws)
  169. {
  170. struct itcw *itcw;
  171. void *chunk;
  172. addr_t start;
  173. addr_t end;
  174. int cross_count;
  175. /* Check for 2G limit. */
  176. start = (addr_t) buffer;
  177. end = start + size;
  178. if (end > (1 << 31))
  179. return ERR_PTR(-EINVAL);
  180. memset(buffer, 0, size);
  181. /* ITCW. */
  182. chunk = fit_chunk(&start, end, sizeof(struct itcw), 1, 0);
  183. if (IS_ERR(chunk))
  184. return chunk;
  185. itcw = chunk;
  186. /* allow for TTIC tidaws that may be needed to cross a page boundary */
  187. cross_count = 0;
  188. if (max_tidaws)
  189. cross_count = 1 + ((max_tidaws * sizeof(struct tidaw) - 1)
  190. >> PAGE_SHIFT);
  191. itcw->max_tidaws = max_tidaws + cross_count;
  192. cross_count = 0;
  193. if (intrg_max_tidaws)
  194. cross_count = 1 + ((intrg_max_tidaws * sizeof(struct tidaw) - 1)
  195. >> PAGE_SHIFT);
  196. itcw->intrg_max_tidaws = intrg_max_tidaws + cross_count;
  197. /* Main TCW. */
  198. chunk = fit_chunk(&start, end, sizeof(struct tcw), 64, 0);
  199. if (IS_ERR(chunk))
  200. return chunk;
  201. itcw->tcw = chunk;
  202. tcw_init(itcw->tcw, (op == ITCW_OP_READ) ? 1 : 0,
  203. (op == ITCW_OP_WRITE) ? 1 : 0);
  204. /* Interrogate TCW. */
  205. if (intrg) {
  206. chunk = fit_chunk(&start, end, sizeof(struct tcw), 64, 0);
  207. if (IS_ERR(chunk))
  208. return chunk;
  209. itcw->intrg_tcw = chunk;
  210. tcw_init(itcw->intrg_tcw, 1, 0);
  211. tcw_set_intrg(itcw->tcw, itcw->intrg_tcw);
  212. }
  213. /* Data TIDAL. */
  214. if (max_tidaws > 0) {
  215. chunk = fit_chunk(&start, end, sizeof(struct tidaw) *
  216. itcw->max_tidaws, 16, 0);
  217. if (IS_ERR(chunk))
  218. return chunk;
  219. tcw_set_data(itcw->tcw, chunk, 1);
  220. }
  221. /* Interrogate data TIDAL. */
  222. if (intrg && (intrg_max_tidaws > 0)) {
  223. chunk = fit_chunk(&start, end, sizeof(struct tidaw) *
  224. itcw->intrg_max_tidaws, 16, 0);
  225. if (IS_ERR(chunk))
  226. return chunk;
  227. tcw_set_data(itcw->intrg_tcw, chunk, 1);
  228. }
  229. /* TSB. */
  230. chunk = fit_chunk(&start, end, sizeof(struct tsb), 8, 0);
  231. if (IS_ERR(chunk))
  232. return chunk;
  233. tsb_init(chunk);
  234. tcw_set_tsb(itcw->tcw, chunk);
  235. /* Interrogate TSB. */
  236. if (intrg) {
  237. chunk = fit_chunk(&start, end, sizeof(struct tsb), 8, 0);
  238. if (IS_ERR(chunk))
  239. return chunk;
  240. tsb_init(chunk);
  241. tcw_set_tsb(itcw->intrg_tcw, chunk);
  242. }
  243. /* TCCB. */
  244. chunk = fit_chunk(&start, end, TCCB_MAX_SIZE, 8, 0);
  245. if (IS_ERR(chunk))
  246. return chunk;
  247. tccb_init(chunk, TCCB_MAX_SIZE, TCCB_SAC_DEFAULT);
  248. tcw_set_tccb(itcw->tcw, chunk);
  249. /* Interrogate TCCB. */
  250. if (intrg) {
  251. chunk = fit_chunk(&start, end, TCCB_MAX_SIZE, 8, 0);
  252. if (IS_ERR(chunk))
  253. return chunk;
  254. tccb_init(chunk, TCCB_MAX_SIZE, TCCB_SAC_INTRG);
  255. tcw_set_tccb(itcw->intrg_tcw, chunk);
  256. tccb_add_dcw(chunk, TCCB_MAX_SIZE, DCW_CMD_INTRG, 0, NULL,
  257. sizeof(struct dcw_intrg_data), 0);
  258. tcw_finalize(itcw->intrg_tcw, 0);
  259. }
  260. return itcw;
  261. }
  262. EXPORT_SYMBOL(itcw_init);
  263. /**
  264. * itcw_add_dcw - add a dcw to the itcw
  265. * @itcw: address of the itcw
  266. * @cmd: the dcw command
  267. * @flags: flags for the dcw
  268. * @cd: address of control data for this dcw or NULL if none is required
  269. * @cd_count: number of control data bytes for this dcw
  270. * @count: number of data bytes for this dcw
  271. *
  272. * Add a new dcw to the specified itcw by writing the dcw information specified
  273. * by @cmd, @flags, @cd, @cd_count and @count to the tca of the tccb. Return
  274. * a pointer to the newly added dcw on success or -%ENOSPC if the new dcw
  275. * would exceed the available space.
  276. *
  277. * Note: the tcal field of the tccb header will be updated to reflect added
  278. * content.
  279. */
  280. struct dcw *itcw_add_dcw(struct itcw *itcw, u8 cmd, u8 flags, void *cd,
  281. u8 cd_count, u32 count)
  282. {
  283. return tccb_add_dcw(tcw_get_tccb(itcw->tcw), TCCB_MAX_SIZE, cmd,
  284. flags, cd, cd_count, count);
  285. }
  286. EXPORT_SYMBOL(itcw_add_dcw);
  287. /**
  288. * itcw_add_tidaw - add a tidaw to the itcw
  289. * @itcw: address of the itcw
  290. * @flags: flags for the new tidaw
  291. * @addr: address value for the new tidaw
  292. * @count: count value for the new tidaw
  293. *
  294. * Add a new tidaw to the input/output data tidaw-list of the specified itcw
  295. * (depending on the value of the r-flag and w-flag). Return a pointer to
  296. * the new tidaw on success or -%ENOSPC if the new tidaw would exceed the
  297. * available space.
  298. *
  299. * Note: TTIC tidaws are automatically added when needed, so explicitly calling
  300. * this interface with the TTIC flag is not supported. The last-tidaw flag
  301. * for the last tidaw in the list will be set by itcw_finalize.
  302. */
  303. struct tidaw *itcw_add_tidaw(struct itcw *itcw, u8 flags, void *addr, u32 count)
  304. {
  305. struct tidaw *following;
  306. if (itcw->num_tidaws >= itcw->max_tidaws)
  307. return ERR_PTR(-ENOSPC);
  308. /*
  309. * Is the tidaw, which follows the one we are about to fill, on the next
  310. * page? Then we have to insert a TTIC tidaw first, that points to the
  311. * tidaw on the new page.
  312. */
  313. following = ((struct tidaw *) tcw_get_data(itcw->tcw))
  314. + itcw->num_tidaws + 1;
  315. if (itcw->num_tidaws && !((unsigned long) following & ~PAGE_MASK)) {
  316. tcw_add_tidaw(itcw->tcw, itcw->num_tidaws++,
  317. TIDAW_FLAGS_TTIC, following, 0);
  318. if (itcw->num_tidaws >= itcw->max_tidaws)
  319. return ERR_PTR(-ENOSPC);
  320. }
  321. return tcw_add_tidaw(itcw->tcw, itcw->num_tidaws++, flags, addr, count);
  322. }
  323. EXPORT_SYMBOL(itcw_add_tidaw);
  324. /**
  325. * itcw_set_data - set data address and tida flag of the itcw
  326. * @itcw: address of the itcw
  327. * @addr: the data address
  328. * @use_tidal: zero of the data address specifies a contiguous block of data,
  329. * non-zero if it specifies a list if tidaws.
  330. *
  331. * Set the input/output data address of the itcw (depending on the value of the
  332. * r-flag and w-flag). If @use_tidal is non-zero, the corresponding tida flag
  333. * is set as well.
  334. */
  335. void itcw_set_data(struct itcw *itcw, void *addr, int use_tidal)
  336. {
  337. tcw_set_data(itcw->tcw, addr, use_tidal);
  338. }
  339. EXPORT_SYMBOL(itcw_set_data);
  340. /**
  341. * itcw_finalize - calculate length and count fields of the itcw
  342. * @itcw: address of the itcw
  343. *
  344. * Calculate tcw input-/output-count and tccbl fields and add a tcat the tccb.
  345. * In case input- or output-tida is used, the tidaw-list must be stored in
  346. * continuous storage (no ttic). The tcal field in the tccb must be
  347. * up-to-date.
  348. */
  349. void itcw_finalize(struct itcw *itcw)
  350. {
  351. tcw_finalize(itcw->tcw, itcw->num_tidaws);
  352. }
  353. EXPORT_SYMBOL(itcw_finalize);