fcx.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * Functions for assembling 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 "cio.h"
  15. /**
  16. * tcw_get_intrg - return pointer to associated interrogate tcw
  17. * @tcw: pointer to the original tcw
  18. *
  19. * Return a pointer to the interrogate tcw associated with the specified tcw
  20. * or %NULL if there is no associated interrogate tcw.
  21. */
  22. struct tcw *tcw_get_intrg(struct tcw *tcw)
  23. {
  24. return (struct tcw *) ((addr_t) tcw->intrg);
  25. }
  26. EXPORT_SYMBOL(tcw_get_intrg);
  27. /**
  28. * tcw_get_data - return pointer to input/output data associated with tcw
  29. * @tcw: pointer to the tcw
  30. *
  31. * Return the input or output data address specified in the tcw depending
  32. * on whether the r-bit or the w-bit is set. If neither bit is set, return
  33. * %NULL.
  34. */
  35. void *tcw_get_data(struct tcw *tcw)
  36. {
  37. if (tcw->r)
  38. return (void *) ((addr_t) tcw->input);
  39. if (tcw->w)
  40. return (void *) ((addr_t) tcw->output);
  41. return NULL;
  42. }
  43. EXPORT_SYMBOL(tcw_get_data);
  44. /**
  45. * tcw_get_tccb - return pointer to tccb associated with tcw
  46. * @tcw: pointer to the tcw
  47. *
  48. * Return pointer to the tccb associated with this tcw.
  49. */
  50. struct tccb *tcw_get_tccb(struct tcw *tcw)
  51. {
  52. return (struct tccb *) ((addr_t) tcw->tccb);
  53. }
  54. EXPORT_SYMBOL(tcw_get_tccb);
  55. /**
  56. * tcw_get_tsb - return pointer to tsb associated with tcw
  57. * @tcw: pointer to the tcw
  58. *
  59. * Return pointer to the tsb associated with this tcw.
  60. */
  61. struct tsb *tcw_get_tsb(struct tcw *tcw)
  62. {
  63. return (struct tsb *) ((addr_t) tcw->tsb);
  64. }
  65. EXPORT_SYMBOL(tcw_get_tsb);
  66. /**
  67. * tcw_init - initialize tcw data structure
  68. * @tcw: pointer to the tcw to be initialized
  69. * @r: initial value of the r-bit
  70. * @w: initial value of the w-bit
  71. *
  72. * Initialize all fields of the specified tcw data structure with zero and
  73. * fill in the format, flags, r and w fields.
  74. */
  75. void tcw_init(struct tcw *tcw, int r, int w)
  76. {
  77. memset(tcw, 0, sizeof(struct tcw));
  78. tcw->format = TCW_FORMAT_DEFAULT;
  79. tcw->flags = TCW_FLAGS_TIDAW_FORMAT(TCW_TIDAW_FORMAT_DEFAULT);
  80. if (r)
  81. tcw->r = 1;
  82. if (w)
  83. tcw->w = 1;
  84. }
  85. EXPORT_SYMBOL(tcw_init);
  86. static inline size_t tca_size(struct tccb *tccb)
  87. {
  88. return tccb->tcah.tcal - 12;
  89. }
  90. static u32 calc_dcw_count(struct tccb *tccb)
  91. {
  92. int offset;
  93. struct dcw *dcw;
  94. u32 count = 0;
  95. size_t size;
  96. size = tca_size(tccb);
  97. for (offset = 0; offset < size;) {
  98. dcw = (struct dcw *) &tccb->tca[offset];
  99. count += dcw->count;
  100. if (!(dcw->flags & DCW_FLAGS_CC))
  101. break;
  102. offset += sizeof(struct dcw) + ALIGN((int) dcw->cd_count, 4);
  103. }
  104. return count;
  105. }
  106. static u32 calc_cbc_size(struct tidaw *tidaw, int num)
  107. {
  108. int i;
  109. u32 cbc_data;
  110. u32 cbc_count = 0;
  111. u64 data_count = 0;
  112. for (i = 0; i < num; i++) {
  113. if (tidaw[i].flags & TIDAW_FLAGS_LAST)
  114. break;
  115. /* TODO: find out if padding applies to total of data
  116. * transferred or data transferred by this tidaw. Assumption:
  117. * applies to total. */
  118. data_count += tidaw[i].count;
  119. if (tidaw[i].flags & TIDAW_FLAGS_INSERT_CBC) {
  120. cbc_data = 4 + ALIGN(data_count, 4) - data_count;
  121. cbc_count += cbc_data;
  122. data_count += cbc_data;
  123. }
  124. }
  125. return cbc_count;
  126. }
  127. /**
  128. * tcw_finalize - finalize tcw length fields and tidaw list
  129. * @tcw: pointer to the tcw
  130. * @num_tidaws: the number of tidaws used to address input/output data or zero
  131. * if no tida is used
  132. *
  133. * Calculate the input-/output-count and tccbl field in the tcw, add a
  134. * tcat the tccb and terminate the data tidaw list if used.
  135. *
  136. * Note: in case input- or output-tida is used, the tidaw-list must be stored
  137. * in contiguous storage (no ttic). The tcal field in the tccb must be
  138. * up-to-date.
  139. */
  140. void tcw_finalize(struct tcw *tcw, int num_tidaws)
  141. {
  142. struct tidaw *tidaw;
  143. struct tccb *tccb;
  144. struct tccb_tcat *tcat;
  145. u32 count;
  146. /* Terminate tidaw list. */
  147. tidaw = tcw_get_data(tcw);
  148. if (num_tidaws > 0)
  149. tidaw[num_tidaws - 1].flags |= TIDAW_FLAGS_LAST;
  150. /* Add tcat to tccb. */
  151. tccb = tcw_get_tccb(tcw);
  152. tcat = (struct tccb_tcat *) &tccb->tca[tca_size(tccb)];
  153. memset(tcat, 0, sizeof(*tcat));
  154. /* Calculate tcw input/output count and tcat transport count. */
  155. count = calc_dcw_count(tccb);
  156. if (tcw->w && (tcw->flags & TCW_FLAGS_OUTPUT_TIDA))
  157. count += calc_cbc_size(tidaw, num_tidaws);
  158. if (tcw->r)
  159. tcw->input_count = count;
  160. else if (tcw->w)
  161. tcw->output_count = count;
  162. tcat->count = ALIGN(count, 4) + 4;
  163. /* Calculate tccbl. */
  164. tcw->tccbl = (sizeof(struct tccb) + tca_size(tccb) +
  165. sizeof(struct tccb_tcat) - 20) >> 2;
  166. }
  167. EXPORT_SYMBOL(tcw_finalize);
  168. /**
  169. * tcw_set_intrg - set the interrogate tcw address of a tcw
  170. * @tcw: the tcw address
  171. * @intrg_tcw: the address of the interrogate tcw
  172. *
  173. * Set the address of the interrogate tcw in the specified tcw.
  174. */
  175. void tcw_set_intrg(struct tcw *tcw, struct tcw *intrg_tcw)
  176. {
  177. tcw->intrg = (u32) ((addr_t) intrg_tcw);
  178. }
  179. EXPORT_SYMBOL(tcw_set_intrg);
  180. /**
  181. * tcw_set_data - set data address and tida flag of a tcw
  182. * @tcw: the tcw address
  183. * @data: the data address
  184. * @use_tidal: zero of the data address specifies a contiguous block of data,
  185. * non-zero if it specifies a list if tidaws.
  186. *
  187. * Set the input/output data address of a tcw (depending on the value of the
  188. * r-flag and w-flag). If @use_tidal is non-zero, the corresponding tida flag
  189. * is set as well.
  190. */
  191. void tcw_set_data(struct tcw *tcw, void *data, int use_tidal)
  192. {
  193. if (tcw->r) {
  194. tcw->input = (u64) ((addr_t) data);
  195. if (use_tidal)
  196. tcw->flags |= TCW_FLAGS_INPUT_TIDA;
  197. } else if (tcw->w) {
  198. tcw->output = (u64) ((addr_t) data);
  199. if (use_tidal)
  200. tcw->flags |= TCW_FLAGS_OUTPUT_TIDA;
  201. }
  202. }
  203. EXPORT_SYMBOL(tcw_set_data);
  204. /**
  205. * tcw_set_tccb - set tccb address of a tcw
  206. * @tcw: the tcw address
  207. * @tccb: the tccb address
  208. *
  209. * Set the address of the tccb in the specified tcw.
  210. */
  211. void tcw_set_tccb(struct tcw *tcw, struct tccb *tccb)
  212. {
  213. tcw->tccb = (u64) ((addr_t) tccb);
  214. }
  215. EXPORT_SYMBOL(tcw_set_tccb);
  216. /**
  217. * tcw_set_tsb - set tsb address of a tcw
  218. * @tcw: the tcw address
  219. * @tsb: the tsb address
  220. *
  221. * Set the address of the tsb in the specified tcw.
  222. */
  223. void tcw_set_tsb(struct tcw *tcw, struct tsb *tsb)
  224. {
  225. tcw->tsb = (u64) ((addr_t) tsb);
  226. }
  227. EXPORT_SYMBOL(tcw_set_tsb);
  228. /**
  229. * tccb_init - initialize tccb
  230. * @tccb: the tccb address
  231. * @size: the maximum size of the tccb
  232. * @sac: the service-action-code to be user
  233. *
  234. * Initialize the header of the specified tccb by resetting all values to zero
  235. * and filling in defaults for format, sac and initial tcal fields.
  236. */
  237. void tccb_init(struct tccb *tccb, size_t size, u32 sac)
  238. {
  239. memset(tccb, 0, size);
  240. tccb->tcah.format = TCCB_FORMAT_DEFAULT;
  241. tccb->tcah.sac = sac;
  242. tccb->tcah.tcal = 12;
  243. }
  244. EXPORT_SYMBOL(tccb_init);
  245. /**
  246. * tsb_init - initialize tsb
  247. * @tsb: the tsb address
  248. *
  249. * Initialize the specified tsb by resetting all values to zero.
  250. */
  251. void tsb_init(struct tsb *tsb)
  252. {
  253. memset(tsb, 0, sizeof(*tsb));
  254. }
  255. EXPORT_SYMBOL(tsb_init);
  256. /**
  257. * tccb_add_dcw - add a dcw to the tccb
  258. * @tccb: the tccb address
  259. * @tccb_size: the maximum tccb size
  260. * @cmd: the dcw command
  261. * @flags: flags for the dcw
  262. * @cd: pointer to control data for this dcw or NULL if none is required
  263. * @cd_count: number of control data bytes for this dcw
  264. * @count: number of data bytes for this dcw
  265. *
  266. * Add a new dcw to the specified tccb by writing the dcw information specified
  267. * by @cmd, @flags, @cd, @cd_count and @count to the tca of the tccb. Return
  268. * a pointer to the newly added dcw on success or -%ENOSPC if the new dcw
  269. * would exceed the available space as defined by @tccb_size.
  270. *
  271. * Note: the tcal field of the tccb header will be updates to reflect added
  272. * content.
  273. */
  274. struct dcw *tccb_add_dcw(struct tccb *tccb, size_t tccb_size, u8 cmd, u8 flags,
  275. void *cd, u8 cd_count, u32 count)
  276. {
  277. struct dcw *dcw;
  278. int size;
  279. int tca_offset;
  280. /* Check for space. */
  281. tca_offset = tca_size(tccb);
  282. size = ALIGN(sizeof(struct dcw) + cd_count, 4);
  283. if (sizeof(struct tccb_tcah) + tca_offset + size +
  284. sizeof(struct tccb_tcat) > tccb_size)
  285. return ERR_PTR(-ENOSPC);
  286. /* Add dcw to tca. */
  287. dcw = (struct dcw *) &tccb->tca[tca_offset];
  288. memset(dcw, 0, size);
  289. dcw->cmd = cmd;
  290. dcw->flags = flags;
  291. dcw->count = count;
  292. dcw->cd_count = cd_count;
  293. if (cd)
  294. memcpy(&dcw->cd[0], cd, cd_count);
  295. tccb->tcah.tcal += size;
  296. return dcw;
  297. }
  298. EXPORT_SYMBOL(tccb_add_dcw);
  299. /**
  300. * tcw_add_tidaw - add a tidaw to a tcw
  301. * @tcw: the tcw address
  302. * @num_tidaws: the current number of tidaws
  303. * @flags: flags for the new tidaw
  304. * @addr: address value for the new tidaw
  305. * @count: count value for the new tidaw
  306. *
  307. * Add a new tidaw to the input/output data tidaw-list of the specified tcw
  308. * (depending on the value of the r-flag and w-flag) and return a pointer to
  309. * the new tidaw.
  310. *
  311. * Note: the tidaw-list is assumed to be contiguous with no ttics. The caller
  312. * must ensure that there is enough space for the new tidaw. The last-tidaw
  313. * flag for the last tidaw in the list will be set by tcw_finalize.
  314. */
  315. struct tidaw *tcw_add_tidaw(struct tcw *tcw, int num_tidaws, u8 flags,
  316. void *addr, u32 count)
  317. {
  318. struct tidaw *tidaw;
  319. /* Add tidaw to tidaw-list. */
  320. tidaw = ((struct tidaw *) tcw_get_data(tcw)) + num_tidaws;
  321. memset(tidaw, 0, sizeof(struct tidaw));
  322. tidaw->flags = flags;
  323. tidaw->count = count;
  324. tidaw->addr = (u64) ((addr_t) addr);
  325. return tidaw;
  326. }
  327. EXPORT_SYMBOL(tcw_add_tidaw);