idals.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
  4. * Martin Schwidefsky <schwidefsky@de.ibm.com>
  5. * Bugreports.to..: <Linux390@de.ibm.com>
  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[0];
  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(sizeof(struct idal_buffer) + nr_ptrs*sizeof(void *),
  112. GFP_DMA | GFP_KERNEL);
  113. if (ib == NULL)
  114. return ERR_PTR(-ENOMEM);
  115. ib->size = size;
  116. ib->page_order = page_order;
  117. for (i = 0; i < nr_ptrs; i++) {
  118. if ((i & (nr_chunks - 1)) != 0) {
  119. ib->data[i] = ib->data[i-1] + IDA_BLOCK_SIZE;
  120. continue;
  121. }
  122. ib->data[i] = (void *)
  123. __get_free_pages(GFP_KERNEL, page_order);
  124. if (ib->data[i] != NULL)
  125. continue;
  126. // Not enough memory
  127. while (i >= nr_chunks) {
  128. i -= nr_chunks;
  129. free_pages((unsigned long) ib->data[i],
  130. ib->page_order);
  131. }
  132. kfree(ib);
  133. return ERR_PTR(-ENOMEM);
  134. }
  135. return ib;
  136. }
  137. /*
  138. * Free an idal buffer.
  139. */
  140. static inline void
  141. idal_buffer_free(struct idal_buffer *ib)
  142. {
  143. int nr_chunks, nr_ptrs, i;
  144. nr_ptrs = (ib->size + IDA_BLOCK_SIZE - 1) >> IDA_SIZE_LOG;
  145. nr_chunks = (4096 << ib->page_order) >> IDA_SIZE_LOG;
  146. for (i = 0; i < nr_ptrs; i += nr_chunks)
  147. free_pages((unsigned long) ib->data[i], ib->page_order);
  148. kfree(ib);
  149. }
  150. /*
  151. * Test if a idal list is really needed.
  152. */
  153. static inline int
  154. __idal_buffer_is_needed(struct idal_buffer *ib)
  155. {
  156. return ib->size > (4096ul << ib->page_order) ||
  157. idal_is_needed(ib->data[0], ib->size);
  158. }
  159. /*
  160. * Set channel data address to idal buffer.
  161. */
  162. static inline void
  163. idal_buffer_set_cda(struct idal_buffer *ib, struct ccw1 *ccw)
  164. {
  165. if (__idal_buffer_is_needed(ib)) {
  166. // setup idals;
  167. ccw->cda = (u32)(addr_t) ib->data;
  168. ccw->flags |= CCW_FLAG_IDA;
  169. } else
  170. // we do not need idals - use direct addressing
  171. ccw->cda = (u32)(addr_t) ib->data[0];
  172. ccw->count = ib->size;
  173. }
  174. /*
  175. * Copy count bytes from an idal buffer to user memory
  176. */
  177. static inline size_t
  178. idal_buffer_to_user(struct idal_buffer *ib, void __user *to, size_t count)
  179. {
  180. size_t left;
  181. int i;
  182. BUG_ON(count > ib->size);
  183. for (i = 0; count > IDA_BLOCK_SIZE; i++) {
  184. left = copy_to_user(to, ib->data[i], IDA_BLOCK_SIZE);
  185. if (left)
  186. return left + count - IDA_BLOCK_SIZE;
  187. to = (void __user *) to + IDA_BLOCK_SIZE;
  188. count -= IDA_BLOCK_SIZE;
  189. }
  190. return copy_to_user(to, ib->data[i], count);
  191. }
  192. /*
  193. * Copy count bytes from user memory to an idal buffer
  194. */
  195. static inline size_t
  196. idal_buffer_from_user(struct idal_buffer *ib, const void __user *from, size_t count)
  197. {
  198. size_t left;
  199. int i;
  200. BUG_ON(count > ib->size);
  201. for (i = 0; count > IDA_BLOCK_SIZE; i++) {
  202. left = copy_from_user(ib->data[i], from, IDA_BLOCK_SIZE);
  203. if (left)
  204. return left + count - IDA_BLOCK_SIZE;
  205. from = (void __user *) from + IDA_BLOCK_SIZE;
  206. count -= IDA_BLOCK_SIZE;
  207. }
  208. return copy_from_user(ib->data[i], from, count);
  209. }
  210. #endif