itcw.c 10 KB

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