idals.h 5.6 KB

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