idals.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
  3. * Martin Schwidefsky <schwidefsky@de.ibm.com>
  4. * Bugreports.to..: <Linux390@de.ibm.com>
  5. * Copyright IBM Corp. 2000
  6. *
  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/errno.h>
  14. #include <linux/err.h>
  15. #include <linux/types.h>
  16. #include <linux/slab.h>
  17. #include <asm/cio.h>
  18. #include <linux/uaccess.h>
  19. #define IDA_SIZE_LOG 12 /* 11 for 2k , 12 for 4k */
  20. #define IDA_BLOCK_SIZE (1L<<IDA_SIZE_LOG)
  21. /*
  22. * Test if an address/length pair needs an idal list.
  23. */
  24. static inline int
  25. idal_is_needed(void *vaddr, unsigned int length)
  26. {
  27. return ((__pa(vaddr) + length - 1) >> 31) != 0;
  28. }
  29. /*
  30. * Return the number of idal words needed for an address/length pair.
  31. */
  32. static inline unsigned int idal_nr_words(void *vaddr, unsigned int length)
  33. {
  34. return ((__pa(vaddr) & (IDA_BLOCK_SIZE-1)) + length +
  35. (IDA_BLOCK_SIZE-1)) >> IDA_SIZE_LOG;
  36. }
  37. /*
  38. * Create the list of idal words for an address/length pair.
  39. */
  40. static inline unsigned long *idal_create_words(unsigned long *idaws,
  41. void *vaddr, unsigned int length)
  42. {
  43. unsigned long paddr;
  44. unsigned int cidaw;
  45. paddr = __pa(vaddr);
  46. cidaw = ((paddr & (IDA_BLOCK_SIZE-1)) + length +
  47. (IDA_BLOCK_SIZE-1)) >> IDA_SIZE_LOG;
  48. *idaws++ = paddr;
  49. paddr &= -IDA_BLOCK_SIZE;
  50. while (--cidaw > 0) {
  51. paddr += IDA_BLOCK_SIZE;
  52. *idaws++ = paddr;
  53. }
  54. return idaws;
  55. }
  56. /*
  57. * Sets the address of the data in CCW.
  58. * If necessary it allocates an IDAL and sets the appropriate flags.
  59. */
  60. static inline int
  61. set_normalized_cda(struct ccw1 * ccw, void *vaddr)
  62. {
  63. unsigned int nridaws;
  64. unsigned long *idal;
  65. if (ccw->flags & CCW_FLAG_IDA)
  66. return -EINVAL;
  67. nridaws = idal_nr_words(vaddr, ccw->count);
  68. if (nridaws > 0) {
  69. idal = kmalloc(nridaws * sizeof(unsigned long),
  70. GFP_ATOMIC | GFP_DMA );
  71. if (idal == NULL)
  72. return -ENOMEM;
  73. idal_create_words(idal, vaddr, ccw->count);
  74. ccw->flags |= CCW_FLAG_IDA;
  75. vaddr = idal;
  76. }
  77. ccw->cda = (__u32)(unsigned long) vaddr;
  78. return 0;
  79. }
  80. /*
  81. * Releases any allocated IDAL related to the CCW.
  82. */
  83. static inline void
  84. clear_normalized_cda(struct ccw1 * ccw)
  85. {
  86. if (ccw->flags & CCW_FLAG_IDA) {
  87. kfree((void *)(unsigned long) ccw->cda);
  88. ccw->flags &= ~CCW_FLAG_IDA;
  89. }
  90. ccw->cda = 0;
  91. }
  92. /*
  93. * Idal buffer extension
  94. */
  95. struct idal_buffer {
  96. size_t size;
  97. size_t page_order;
  98. void *data[0];
  99. };
  100. /*
  101. * Allocate an idal buffer
  102. */
  103. static inline struct idal_buffer *
  104. idal_buffer_alloc(size_t size, int page_order)
  105. {
  106. struct idal_buffer *ib;
  107. int nr_chunks, nr_ptrs, i;
  108. nr_ptrs = (size + IDA_BLOCK_SIZE - 1) >> IDA_SIZE_LOG;
  109. nr_chunks = (4096 << page_order) >> IDA_SIZE_LOG;
  110. ib = kmalloc(sizeof(struct idal_buffer) + nr_ptrs*sizeof(void *),
  111. 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