itcw.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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_KERNEL | 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. int cross_count;
  93. /* Main data. */
  94. len = sizeof(struct itcw);
  95. len += /* TCW */ sizeof(struct tcw) + /* TCCB */ TCCB_MAX_SIZE +
  96. /* TSB */ sizeof(struct tsb) +
  97. /* TIDAL */ max_tidaws * sizeof(struct tidaw);
  98. /* Interrogate data. */
  99. if (intrg) {
  100. len += /* TCW */ sizeof(struct tcw) + /* TCCB */ TCCB_MAX_SIZE +
  101. /* TSB */ sizeof(struct tsb) +
  102. /* TIDAL */ intrg_max_tidaws * sizeof(struct tidaw);
  103. }
  104. /* Maximum required alignment padding. */
  105. len += /* Initial TCW */ 63 + /* Interrogate TCCB */ 7;
  106. /* TIDAW lists may not cross a 4k boundary. To cross a
  107. * boundary we need to add a TTIC TIDAW. We need to reserve
  108. * one additional TIDAW for a TTIC that we may need to add due
  109. * to the placement of the data chunk in memory, and a further
  110. * TIDAW for each page boundary that the TIDAW list may cross
  111. * due to it's own size.
  112. */
  113. if (max_tidaws) {
  114. cross_count = 1 + ((max_tidaws * sizeof(struct tidaw) - 1)
  115. >> PAGE_SHIFT);
  116. len += cross_count * sizeof(struct tidaw);
  117. }
  118. if (intrg_max_tidaws) {
  119. cross_count = 1 + ((intrg_max_tidaws * sizeof(struct tidaw) - 1)
  120. >> PAGE_SHIFT);
  121. len += cross_count * sizeof(struct tidaw);
  122. }
  123. return len;
  124. }
  125. EXPORT_SYMBOL(itcw_calc_size);
  126. #define CROSS4K(x, l) (((x) & ~4095) != ((x + l) & ~4095))
  127. static inline void *fit_chunk(addr_t *start, addr_t end, size_t len,
  128. int align, int check_4k)
  129. {
  130. addr_t addr;
  131. addr = ALIGN(*start, align);
  132. if (check_4k && CROSS4K(addr, len)) {
  133. addr = ALIGN(addr, 4096);
  134. addr = ALIGN(addr, align);
  135. }
  136. if (addr + len > end)
  137. return ERR_PTR(-ENOSPC);
  138. *start = addr + len;
  139. return (void *) addr;
  140. }
  141. /**
  142. * itcw_init - initialize incremental tcw data structure
  143. * @buffer: address of buffer to use for data structures
  144. * @size: number of bytes in buffer
  145. * @op: %ITCW_OP_READ for a read operation tcw, %ITCW_OP_WRITE for a write
  146. * operation tcw
  147. * @intrg: if non-zero, add and initialize an interrogate tcw
  148. * @max_tidaws: maximum number of tidaws to be used for data addressing or zero
  149. * if no tida is to be used.
  150. * @intrg_max_tidaws: maximum number of tidaws to be used for data addressing
  151. * by the interrogate tcw, if specified
  152. *
  153. * Prepare the specified buffer to be used as an incremental tcw, i.e. a
  154. * helper data structure that can be used to construct a valid tcw by
  155. * successive calls to other helper functions. Note: the buffer needs to be
  156. * located below the 2G address limit. The resulting tcw has the following
  157. * restrictions:
  158. * - no tccb tidal
  159. * - input/output tidal is contiguous (no ttic)
  160. * - total data should not exceed 4k
  161. * - tcw specifies either read or write operation
  162. *
  163. * On success, return pointer to the resulting incremental tcw data structure,
  164. * ERR_PTR otherwise.
  165. */
  166. struct itcw *itcw_init(void *buffer, size_t size, int op, int intrg,
  167. int max_tidaws, int intrg_max_tidaws)
  168. {
  169. struct itcw *itcw;
  170. void *chunk;
  171. addr_t start;
  172. addr_t end;
  173. int cross_count;
  174. /* Check for 2G limit. */
  175. start = (addr_t) buffer;
  176. end = start + size;
  177. if (end > (1 << 31))
  178. return ERR_PTR(-EINVAL);
  179. memset(buffer, 0, size);
  180. /* ITCW. */
  181. chunk = fit_chunk(&start, end, sizeof(struct itcw), 1, 0);
  182. if (IS_ERR(chunk))
  183. return chunk;
  184. itcw = chunk;
  185. /* allow for TTIC tidaws that may be needed to cross a page boundary */
  186. cross_count = 0;
  187. if (max_tidaws)
  188. cross_count = 1 + ((max_tidaws * sizeof(struct tidaw) - 1)
  189. >> PAGE_SHIFT);
  190. itcw->max_tidaws = max_tidaws + cross_count;
  191. cross_count = 0;
  192. if (intrg_max_tidaws)
  193. cross_count = 1 + ((intrg_max_tidaws * sizeof(struct tidaw) - 1)
  194. >> PAGE_SHIFT);
  195. itcw->intrg_max_tidaws = intrg_max_tidaws + cross_count;
  196. /* Main TCW. */
  197. chunk = fit_chunk(&start, end, sizeof(struct tcw), 64, 0);
  198. if (IS_ERR(chunk))
  199. return chunk;
  200. itcw->tcw = chunk;
  201. tcw_init(itcw->tcw, (op == ITCW_OP_READ) ? 1 : 0,
  202. (op == ITCW_OP_WRITE) ? 1 : 0);
  203. /* Interrogate TCW. */
  204. if (intrg) {
  205. chunk = fit_chunk(&start, end, sizeof(struct tcw), 64, 0);
  206. if (IS_ERR(chunk))
  207. return chunk;
  208. itcw->intrg_tcw = chunk;
  209. tcw_init(itcw->intrg_tcw, 1, 0);
  210. tcw_set_intrg(itcw->tcw, itcw->intrg_tcw);
  211. }
  212. /* Data TIDAL. */
  213. if (max_tidaws > 0) {
  214. chunk = fit_chunk(&start, end, sizeof(struct tidaw) *
  215. itcw->max_tidaws, 16, 0);
  216. if (IS_ERR(chunk))
  217. return chunk;
  218. tcw_set_data(itcw->tcw, chunk, 1);
  219. }
  220. /* Interrogate data TIDAL. */
  221. if (intrg && (intrg_max_tidaws > 0)) {
  222. chunk = fit_chunk(&start, end, sizeof(struct tidaw) *
  223. itcw->intrg_max_tidaws, 16, 0);
  224. if (IS_ERR(chunk))
  225. return chunk;
  226. tcw_set_data(itcw->intrg_tcw, chunk, 1);
  227. }
  228. /* TSB. */
  229. chunk = fit_chunk(&start, end, sizeof(struct tsb), 8, 0);
  230. if (IS_ERR(chunk))
  231. return chunk;
  232. tsb_init(chunk);
  233. tcw_set_tsb(itcw->tcw, chunk);
  234. /* Interrogate TSB. */
  235. if (intrg) {
  236. chunk = fit_chunk(&start, end, sizeof(struct tsb), 8, 0);
  237. if (IS_ERR(chunk))
  238. return chunk;
  239. tsb_init(chunk);
  240. tcw_set_tsb(itcw->intrg_tcw, chunk);
  241. }
  242. /* TCCB. */
  243. chunk = fit_chunk(&start, end, TCCB_MAX_SIZE, 8, 0);
  244. if (IS_ERR(chunk))
  245. return chunk;
  246. tccb_init(chunk, TCCB_MAX_SIZE, TCCB_SAC_DEFAULT);
  247. tcw_set_tccb(itcw->tcw, chunk);
  248. /* Interrogate TCCB. */
  249. if (intrg) {
  250. chunk = fit_chunk(&start, end, TCCB_MAX_SIZE, 8, 0);
  251. if (IS_ERR(chunk))
  252. return chunk;
  253. tccb_init(chunk, TCCB_MAX_SIZE, TCCB_SAC_INTRG);
  254. tcw_set_tccb(itcw->intrg_tcw, chunk);
  255. tccb_add_dcw(chunk, TCCB_MAX_SIZE, DCW_CMD_INTRG, 0, NULL,
  256. sizeof(struct dcw_intrg_data), 0);
  257. tcw_finalize(itcw->intrg_tcw, 0);
  258. }
  259. return itcw;
  260. }
  261. EXPORT_SYMBOL(itcw_init);
  262. /**
  263. * itcw_add_dcw - add a dcw to the itcw
  264. * @itcw: address of the itcw
  265. * @cmd: the dcw command
  266. * @flags: flags for the dcw
  267. * @cd: address of control data for this dcw or NULL if none is required
  268. * @cd_count: number of control data bytes for this dcw
  269. * @count: number of data bytes for this dcw
  270. *
  271. * Add a new dcw to the specified itcw by writing the dcw information specified
  272. * by @cmd, @flags, @cd, @cd_count and @count to the tca of the tccb. Return
  273. * a pointer to the newly added dcw on success or -%ENOSPC if the new dcw
  274. * would exceed the available space.
  275. *
  276. * Note: the tcal field of the tccb header will be updated to reflect added
  277. * content.
  278. */
  279. struct dcw *itcw_add_dcw(struct itcw *itcw, u8 cmd, u8 flags, void *cd,
  280. u8 cd_count, u32 count)
  281. {
  282. return tccb_add_dcw(tcw_get_tccb(itcw->tcw), TCCB_MAX_SIZE, cmd,
  283. flags, cd, cd_count, count);
  284. }
  285. EXPORT_SYMBOL(itcw_add_dcw);
  286. /**
  287. * itcw_add_tidaw - add a tidaw to the itcw
  288. * @itcw: address of the itcw
  289. * @flags: flags for the new tidaw
  290. * @addr: address value for the new tidaw
  291. * @count: count value for the new tidaw
  292. *
  293. * Add a new tidaw to the input/output data tidaw-list of the specified itcw
  294. * (depending on the value of the r-flag and w-flag). Return a pointer to
  295. * the new tidaw on success or -%ENOSPC if the new tidaw would exceed the
  296. * available space.
  297. *
  298. * Note: TTIC tidaws are automatically added when needed, so explicitly calling
  299. * this interface with the TTIC flag is not supported. The last-tidaw flag
  300. * for the last tidaw in the list will be set by itcw_finalize.
  301. */
  302. struct tidaw *itcw_add_tidaw(struct itcw *itcw, u8 flags, void *addr, u32 count)
  303. {
  304. struct tidaw *following;
  305. if (itcw->num_tidaws >= itcw->max_tidaws)
  306. return ERR_PTR(-ENOSPC);
  307. /*
  308. * Is the tidaw, which follows the one we are about to fill, on the next
  309. * page? Then we have to insert a TTIC tidaw first, that points to the
  310. * tidaw on the new page.
  311. */
  312. following = ((struct tidaw *) tcw_get_data(itcw->tcw))
  313. + itcw->num_tidaws + 1;
  314. if (itcw->num_tidaws && !((unsigned long) following & ~PAGE_MASK)) {
  315. tcw_add_tidaw(itcw->tcw, itcw->num_tidaws++,
  316. TIDAW_FLAGS_TTIC, following, 0);
  317. if (itcw->num_tidaws >= itcw->max_tidaws)
  318. return ERR_PTR(-ENOSPC);
  319. }
  320. return tcw_add_tidaw(itcw->tcw, itcw->num_tidaws++, flags, addr, count);
  321. }
  322. EXPORT_SYMBOL(itcw_add_tidaw);
  323. /**
  324. * itcw_set_data - set data address and tida flag of the itcw
  325. * @itcw: address of the itcw
  326. * @addr: the data address
  327. * @use_tidal: zero of the data address specifies a contiguous block of data,
  328. * non-zero if it specifies a list if tidaws.
  329. *
  330. * Set the input/output data address of the itcw (depending on the value of the
  331. * r-flag and w-flag). If @use_tidal is non-zero, the corresponding tida flag
  332. * is set as well.
  333. */
  334. void itcw_set_data(struct itcw *itcw, void *addr, int use_tidal)
  335. {
  336. tcw_set_data(itcw->tcw, addr, use_tidal);
  337. }
  338. EXPORT_SYMBOL(itcw_set_data);
  339. /**
  340. * itcw_finalize - calculate length and count fields of the itcw
  341. * @itcw: address of the itcw
  342. *
  343. * Calculate tcw input-/output-count and tccbl fields and add a tcat the tccb.
  344. * In case input- or output-tida is used, the tidaw-list must be stored in
  345. * continuous storage (no ttic). The tcal field in the tccb must be
  346. * up-to-date.
  347. */
  348. void itcw_finalize(struct itcw *itcw)
  349. {
  350. tcw_finalize(itcw->tcw, itcw->num_tidaws);
  351. }
  352. EXPORT_SYMBOL(itcw_finalize);