mthca_allocator.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * Copyright (c) 2004 Topspin Communications. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <linux/errno.h>
  33. #include <linux/slab.h>
  34. #include <linux/bitmap.h>
  35. #include "mthca_dev.h"
  36. /* Trivial bitmap-based allocator */
  37. u32 mthca_alloc(struct mthca_alloc *alloc)
  38. {
  39. unsigned long flags;
  40. u32 obj;
  41. spin_lock_irqsave(&alloc->lock, flags);
  42. obj = find_next_zero_bit(alloc->table, alloc->max, alloc->last);
  43. if (obj >= alloc->max) {
  44. alloc->top = (alloc->top + alloc->max) & alloc->mask;
  45. obj = find_first_zero_bit(alloc->table, alloc->max);
  46. }
  47. if (obj < alloc->max) {
  48. __set_bit(obj, alloc->table);
  49. obj |= alloc->top;
  50. } else
  51. obj = -1;
  52. spin_unlock_irqrestore(&alloc->lock, flags);
  53. return obj;
  54. }
  55. void mthca_free(struct mthca_alloc *alloc, u32 obj)
  56. {
  57. unsigned long flags;
  58. obj &= alloc->max - 1;
  59. spin_lock_irqsave(&alloc->lock, flags);
  60. __clear_bit(obj, alloc->table);
  61. alloc->last = min(alloc->last, obj);
  62. alloc->top = (alloc->top + alloc->max) & alloc->mask;
  63. spin_unlock_irqrestore(&alloc->lock, flags);
  64. }
  65. int mthca_alloc_init(struct mthca_alloc *alloc, u32 num, u32 mask,
  66. u32 reserved)
  67. {
  68. /* num must be a power of 2 */
  69. if (num != 1 << (ffs(num) - 1))
  70. return -EINVAL;
  71. alloc->last = 0;
  72. alloc->top = 0;
  73. alloc->max = num;
  74. alloc->mask = mask;
  75. spin_lock_init(&alloc->lock);
  76. alloc->table = bitmap_zalloc(num, GFP_KERNEL);
  77. if (!alloc->table)
  78. return -ENOMEM;
  79. bitmap_set(alloc->table, 0, reserved);
  80. return 0;
  81. }
  82. void mthca_alloc_cleanup(struct mthca_alloc *alloc)
  83. {
  84. bitmap_free(alloc->table);
  85. }
  86. /*
  87. * Array of pointers with lazy allocation of leaf pages. Callers of
  88. * _get, _set and _clear methods must use a lock or otherwise
  89. * serialize access to the array.
  90. */
  91. #define MTHCA_ARRAY_MASK (PAGE_SIZE / sizeof (void *) - 1)
  92. void *mthca_array_get(struct mthca_array *array, int index)
  93. {
  94. int p = (index * sizeof (void *)) >> PAGE_SHIFT;
  95. if (array->page_list[p].page)
  96. return array->page_list[p].page[index & MTHCA_ARRAY_MASK];
  97. else
  98. return NULL;
  99. }
  100. int mthca_array_set(struct mthca_array *array, int index, void *value)
  101. {
  102. int p = (index * sizeof (void *)) >> PAGE_SHIFT;
  103. /* Allocate with GFP_ATOMIC because we'll be called with locks held. */
  104. if (!array->page_list[p].page)
  105. array->page_list[p].page = (void **) get_zeroed_page(GFP_ATOMIC);
  106. if (!array->page_list[p].page)
  107. return -ENOMEM;
  108. array->page_list[p].page[index & MTHCA_ARRAY_MASK] = value;
  109. ++array->page_list[p].used;
  110. return 0;
  111. }
  112. void mthca_array_clear(struct mthca_array *array, int index)
  113. {
  114. int p = (index * sizeof (void *)) >> PAGE_SHIFT;
  115. if (--array->page_list[p].used == 0) {
  116. free_page((unsigned long) array->page_list[p].page);
  117. array->page_list[p].page = NULL;
  118. } else
  119. array->page_list[p].page[index & MTHCA_ARRAY_MASK] = NULL;
  120. if (array->page_list[p].used < 0)
  121. pr_debug("Array %p index %d page %d with ref count %d < 0\n",
  122. array, index, p, array->page_list[p].used);
  123. }
  124. int mthca_array_init(struct mthca_array *array, int nent)
  125. {
  126. int npage = (nent * sizeof (void *) + PAGE_SIZE - 1) / PAGE_SIZE;
  127. int i;
  128. array->page_list = kmalloc_array(npage, sizeof(*array->page_list),
  129. GFP_KERNEL);
  130. if (!array->page_list)
  131. return -ENOMEM;
  132. for (i = 0; i < npage; ++i) {
  133. array->page_list[i].page = NULL;
  134. array->page_list[i].used = 0;
  135. }
  136. return 0;
  137. }
  138. void mthca_array_cleanup(struct mthca_array *array, int nent)
  139. {
  140. int i;
  141. for (i = 0; i < (nent * sizeof (void *) + PAGE_SIZE - 1) / PAGE_SIZE; ++i)
  142. free_page((unsigned long) array->page_list[i].page);
  143. kfree(array->page_list);
  144. }
  145. /*
  146. * Handling for queue buffers -- we allocate a bunch of memory and
  147. * register it in a memory region at HCA virtual address 0. If the
  148. * requested size is > max_direct, we split the allocation into
  149. * multiple pages, so we don't require too much contiguous memory.
  150. */
  151. int mthca_buf_alloc(struct mthca_dev *dev, int size, int max_direct,
  152. union mthca_buf *buf, int *is_direct, struct mthca_pd *pd,
  153. int hca_write, struct mthca_mr *mr)
  154. {
  155. int err = -ENOMEM;
  156. int npages, shift;
  157. u64 *dma_list = NULL;
  158. dma_addr_t t;
  159. int i;
  160. if (size <= max_direct) {
  161. *is_direct = 1;
  162. npages = 1;
  163. shift = get_order(size) + PAGE_SHIFT;
  164. buf->direct.buf = dma_alloc_coherent(&dev->pdev->dev,
  165. size, &t, GFP_KERNEL);
  166. if (!buf->direct.buf)
  167. return -ENOMEM;
  168. dma_unmap_addr_set(&buf->direct, mapping, t);
  169. while (t & ((1 << shift) - 1)) {
  170. --shift;
  171. npages *= 2;
  172. }
  173. dma_list = kmalloc_array(npages, sizeof(*dma_list),
  174. GFP_KERNEL);
  175. if (!dma_list)
  176. goto err_free;
  177. for (i = 0; i < npages; ++i)
  178. dma_list[i] = t + i * (1 << shift);
  179. } else {
  180. *is_direct = 0;
  181. npages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
  182. shift = PAGE_SHIFT;
  183. dma_list = kmalloc_array(npages, sizeof(*dma_list),
  184. GFP_KERNEL);
  185. if (!dma_list)
  186. return -ENOMEM;
  187. buf->page_list = kmalloc_array(npages,
  188. sizeof(*buf->page_list),
  189. GFP_KERNEL);
  190. if (!buf->page_list)
  191. goto err_out;
  192. for (i = 0; i < npages; ++i)
  193. buf->page_list[i].buf = NULL;
  194. for (i = 0; i < npages; ++i) {
  195. buf->page_list[i].buf =
  196. dma_alloc_coherent(&dev->pdev->dev, PAGE_SIZE,
  197. &t, GFP_KERNEL);
  198. if (!buf->page_list[i].buf)
  199. goto err_free;
  200. dma_list[i] = t;
  201. dma_unmap_addr_set(&buf->page_list[i], mapping, t);
  202. clear_page(buf->page_list[i].buf);
  203. }
  204. }
  205. err = mthca_mr_alloc_phys(dev, pd->pd_num,
  206. dma_list, shift, npages,
  207. 0, size,
  208. MTHCA_MPT_FLAG_LOCAL_READ |
  209. (hca_write ? MTHCA_MPT_FLAG_LOCAL_WRITE : 0),
  210. mr);
  211. if (err)
  212. goto err_free;
  213. kfree(dma_list);
  214. return 0;
  215. err_free:
  216. mthca_buf_free(dev, size, buf, *is_direct, NULL);
  217. err_out:
  218. kfree(dma_list);
  219. return err;
  220. }
  221. void mthca_buf_free(struct mthca_dev *dev, int size, union mthca_buf *buf,
  222. int is_direct, struct mthca_mr *mr)
  223. {
  224. int i;
  225. if (mr)
  226. mthca_free_mr(dev, mr);
  227. if (is_direct)
  228. dma_free_coherent(&dev->pdev->dev, size, buf->direct.buf,
  229. dma_unmap_addr(&buf->direct, mapping));
  230. else {
  231. for (i = 0; i < (size + PAGE_SIZE - 1) / PAGE_SIZE; ++i)
  232. dma_free_coherent(&dev->pdev->dev, PAGE_SIZE,
  233. buf->page_list[i].buf,
  234. dma_unmap_addr(&buf->page_list[i],
  235. mapping));
  236. kfree(buf->page_list);
  237. }
  238. }