idals.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Author(s)......: Holger Smolinski <[email protected]>
  4. * Martin Schwidefsky <[email protected]>
  5. * Bugreports.to..: <[email protected]>
  6. * Copyright IBM Corp. 2000
  7. *
  8. * History of changes
  9. * 07/24/00 new file
  10. * 05/04/02 code restructuring.
  11. */
  12. #ifndef _S390_IDALS_H
  13. #define _S390_IDALS_H
  14. #include <linux/errno.h>
  15. #include <linux/err.h>
  16. #include <linux/types.h>
  17. #include <linux/slab.h>
  18. #include <asm/cio.h>
  19. #include <linux/uaccess.h>
  20. #define IDA_SIZE_LOG 12 /* 11 for 2k , 12 for 4k */
  21. #define IDA_BLOCK_SIZE (1L<<IDA_SIZE_LOG)
  22. /*
  23. * Test if an address/length pair needs an idal list.
  24. */
  25. static inline int
  26. idal_is_needed(void *vaddr, unsigned int length)
  27. {
  28. return ((__pa(vaddr) + length - 1) >> 31) != 0;
  29. }
  30. /*
  31. * Return the number of idal words needed for an address/length pair.
  32. */
  33. static inline unsigned int idal_nr_words(void *vaddr, unsigned int length)
  34. {
  35. return ((__pa(vaddr) & (IDA_BLOCK_SIZE-1)) + length +
  36. (IDA_BLOCK_SIZE-1)) >> IDA_SIZE_LOG;
  37. }
  38. /*
  39. * Create the list of idal words for an address/length pair.
  40. */
  41. static inline unsigned long *idal_create_words(unsigned long *idaws,
  42. void *vaddr, unsigned int length)
  43. {
  44. unsigned long paddr;
  45. unsigned int cidaw;
  46. paddr = __pa(vaddr);
  47. cidaw = ((paddr & (IDA_BLOCK_SIZE-1)) + length +
  48. (IDA_BLOCK_SIZE-1)) >> IDA_SIZE_LOG;
  49. *idaws++ = paddr;
  50. paddr &= -IDA_BLOCK_SIZE;
  51. while (--cidaw > 0) {
  52. paddr += IDA_BLOCK_SIZE;
  53. *idaws++ = paddr;
  54. }
  55. return idaws;
  56. }
  57. /*
  58. * Sets the address of the data in CCW.
  59. * If necessary it allocates an IDAL and sets the appropriate flags.
  60. */
  61. static inline int
  62. set_normalized_cda(struct ccw1 * ccw, void *vaddr)
  63. {
  64. unsigned int nridaws;
  65. unsigned long *idal;
  66. if (ccw->flags & CCW_FLAG_IDA)
  67. return -EINVAL;
  68. nridaws = idal_nr_words(vaddr, ccw->count);
  69. if (nridaws > 0) {
  70. idal = kmalloc(nridaws * sizeof(unsigned long),
  71. GFP_ATOMIC | GFP_DMA );
  72. if (idal == NULL)
  73. return -ENOMEM;
  74. idal_create_words(idal, vaddr, ccw->count);
  75. ccw->flags |= CCW_FLAG_IDA;
  76. vaddr = idal;
  77. }
  78. ccw->cda = (__u32)(unsigned long) vaddr;
  79. return 0;
  80. }
  81. /*
  82. * Releases any allocated IDAL related to the CCW.
  83. */
  84. static inline void
  85. clear_normalized_cda(struct ccw1 * ccw)
  86. {
  87. if (ccw->flags & CCW_FLAG_IDA) {
  88. kfree((void *)(unsigned long) ccw->cda);
  89. ccw->flags &= ~CCW_FLAG_IDA;
  90. }
  91. ccw->cda = 0;
  92. }
  93. /*
  94. * Idal buffer extension
  95. */
  96. struct idal_buffer {
  97. size_t size;
  98. size_t page_order;
  99. void *data[];
  100. };
  101. /*
  102. * Allocate an idal buffer
  103. */
  104. static inline struct idal_buffer *
  105. idal_buffer_alloc(size_t size, int page_order)
  106. {
  107. struct idal_buffer *ib;
  108. int nr_chunks, nr_ptrs, i;
  109. nr_ptrs = (size + IDA_BLOCK_SIZE - 1) >> IDA_SIZE_LOG;
  110. nr_chunks = (4096 << page_order) >> IDA_SIZE_LOG;
  111. ib = kmalloc(struct_size(ib, data, nr_ptrs), GFP_DMA | GFP_KERNEL);
  112. if (ib == NULL)
  113. return ERR_PTR(-ENOMEM);
  114. ib->size = size;
  115. ib->page_order = page_order;
  116. for (i = 0; i < nr_ptrs; i++) {
  117. if ((i & (nr_chunks - 1)) != 0) {
  118. ib->data[i] = ib->data[i-1] + IDA_BLOCK_SIZE;
  119. continue;
  120. }
  121. ib->data[i] = (void *)
  122. __get_free_pages(GFP_KERNEL, page_order);
  123. if (ib->data[i] != NULL)
  124. continue;
  125. // Not enough memory
  126. while (i >= nr_chunks) {
  127. i -= nr_chunks;
  128. free_pages((unsigned long) ib->data[i],
  129. ib->page_order);
  130. }
  131. kfree(ib);
  132. return ERR_PTR(-ENOMEM);
  133. }
  134. return ib;
  135. }
  136. /*
  137. * Free an idal buffer.
  138. */
  139. static inline void
  140. idal_buffer_free(struct idal_buffer *ib)
  141. {
  142. int nr_chunks, nr_ptrs, i;
  143. nr_ptrs = (ib->size + IDA_BLOCK_SIZE - 1) >> IDA_SIZE_LOG;
  144. nr_chunks = (4096 << ib->page_order) >> IDA_SIZE_LOG;
  145. for (i = 0; i < nr_ptrs; i += nr_chunks)
  146. free_pages((unsigned long) ib->data[i], ib->page_order);
  147. kfree(ib);
  148. }
  149. /*
  150. * Test if a idal list is really needed.
  151. */
  152. static inline int
  153. __idal_buffer_is_needed(struct idal_buffer *ib)
  154. {
  155. return ib->size > (4096ul << ib->page_order) ||
  156. idal_is_needed(ib->data[0], ib->size);
  157. }
  158. /*
  159. * Set channel data address to idal buffer.
  160. */
  161. static inline void
  162. idal_buffer_set_cda(struct idal_buffer *ib, struct ccw1 *ccw)
  163. {
  164. if (__idal_buffer_is_needed(ib)) {
  165. // setup idals;
  166. ccw->cda = (u32)(addr_t) ib->data;
  167. ccw->flags |= CCW_FLAG_IDA;
  168. } else
  169. // we do not need idals - use direct addressing
  170. ccw->cda = (u32)(addr_t) ib->data[0];
  171. ccw->count = ib->size;
  172. }
  173. /*
  174. * Copy count bytes from an idal buffer to user memory
  175. */
  176. static inline size_t
  177. idal_buffer_to_user(struct idal_buffer *ib, void __user *to, size_t count)
  178. {
  179. size_t left;
  180. int i;
  181. BUG_ON(count > ib->size);
  182. for (i = 0; count > IDA_BLOCK_SIZE; i++) {
  183. left = copy_to_user(to, ib->data[i], IDA_BLOCK_SIZE);
  184. if (left)
  185. return left + count - IDA_BLOCK_SIZE;
  186. to = (void __user *) to + IDA_BLOCK_SIZE;
  187. count -= IDA_BLOCK_SIZE;
  188. }
  189. return copy_to_user(to, ib->data[i], count);
  190. }
  191. /*
  192. * Copy count bytes from user memory to an idal buffer
  193. */
  194. static inline size_t
  195. idal_buffer_from_user(struct idal_buffer *ib, const void __user *from, size_t count)
  196. {
  197. size_t left;
  198. int i;
  199. BUG_ON(count > ib->size);
  200. for (i = 0; count > IDA_BLOCK_SIZE; i++) {
  201. left = copy_from_user(ib->data[i], from, IDA_BLOCK_SIZE);
  202. if (left)
  203. return left + count - IDA_BLOCK_SIZE;
  204. from = (void __user *) from + IDA_BLOCK_SIZE;
  205. count -= IDA_BLOCK_SIZE;
  206. }
  207. return copy_from_user(ib->data[i], from, count);
  208. }
  209. #endif