mthca_allocator.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. int i;
  69. /* num must be a power of 2 */
  70. if (num != 1 << (ffs(num) - 1))
  71. return -EINVAL;
  72. alloc->last = 0;
  73. alloc->top = 0;
  74. alloc->max = num;
  75. alloc->mask = mask;
  76. spin_lock_init(&alloc->lock);
  77. alloc->table = kmalloc(BITS_TO_LONGS(num) * sizeof (long),
  78. GFP_KERNEL);
  79. if (!alloc->table)
  80. return -ENOMEM;
  81. bitmap_zero(alloc->table, num);
  82. for (i = 0; i < reserved; ++i)
  83. set_bit(i, alloc->table);
  84. return 0;
  85. }
  86. void mthca_alloc_cleanup(struct mthca_alloc *alloc)
  87. {
  88. kfree(alloc->table);
  89. }
  90. /*
  91. * Array of pointers with lazy allocation of leaf pages. Callers of
  92. * _get, _set and _clear methods must use a lock or otherwise
  93. * serialize access to the array.
  94. */
  95. #define MTHCA_ARRAY_MASK (PAGE_SIZE / sizeof (void *) - 1)
  96. void *mthca_array_get(struct mthca_array *array, int index)
  97. {
  98. int p = (index * sizeof (void *)) >> PAGE_SHIFT;
  99. if (array->page_list[p].page)
  100. return array->page_list[p].page[index & MTHCA_ARRAY_MASK];
  101. else
  102. return NULL;
  103. }
  104. int mthca_array_set(struct mthca_array *array, int index, void *value)
  105. {
  106. int p = (index * sizeof (void *)) >> PAGE_SHIFT;
  107. /* Allocate with GFP_ATOMIC because we'll be called with locks held. */
  108. if (!array->page_list[p].page)
  109. array->page_list[p].page = (void **) get_zeroed_page(GFP_ATOMIC);
  110. if (!array->page_list[p].page)
  111. return -ENOMEM;
  112. array->page_list[p].page[index & MTHCA_ARRAY_MASK] = value;
  113. ++array->page_list[p].used;
  114. return 0;
  115. }
  116. void mthca_array_clear(struct mthca_array *array, int index)
  117. {
  118. int p = (index * sizeof (void *)) >> PAGE_SHIFT;
  119. if (--array->page_list[p].used == 0) {
  120. free_page((unsigned long) array->page_list[p].page);
  121. array->page_list[p].page = NULL;
  122. } else
  123. array->page_list[p].page[index & MTHCA_ARRAY_MASK] = NULL;
  124. if (array->page_list[p].used < 0)
  125. pr_debug("Array %p index %d page %d with ref count %d < 0\n",
  126. array, index, p, array->page_list[p].used);
  127. }
  128. int mthca_array_init(struct mthca_array *array, int nent)
  129. {
  130. int npage = (nent * sizeof (void *) + PAGE_SIZE - 1) / PAGE_SIZE;
  131. int i;
  132. array->page_list = kmalloc(npage * sizeof *array->page_list, GFP_KERNEL);
  133. if (!array->page_list)
  134. return -ENOMEM;
  135. for (i = 0; i < npage; ++i) {
  136. array->page_list[i].page = NULL;
  137. array->page_list[i].used = 0;
  138. }
  139. return 0;
  140. }
  141. void mthca_array_cleanup(struct mthca_array *array, int nent)
  142. {
  143. int i;
  144. for (i = 0; i < (nent * sizeof (void *) + PAGE_SIZE - 1) / PAGE_SIZE; ++i)
  145. free_page((unsigned long) array->page_list[i].page);
  146. kfree(array->page_list);
  147. }
  148. /*
  149. * Handling for queue buffers -- we allocate a bunch of memory and
  150. * register it in a memory region at HCA virtual address 0. If the
  151. * requested size is > max_direct, we split the allocation into
  152. * multiple pages, so we don't require too much contiguous memory.
  153. */
  154. int mthca_buf_alloc(struct mthca_dev *dev, int size, int max_direct,
  155. union mthca_buf *buf, int *is_direct, struct mthca_pd *pd,
  156. int hca_write, struct mthca_mr *mr)
  157. {
  158. int err = -ENOMEM;
  159. int npages, shift;
  160. u64 *dma_list = NULL;
  161. dma_addr_t t;
  162. int i;
  163. if (size <= max_direct) {
  164. *is_direct = 1;
  165. npages = 1;
  166. shift = get_order(size) + PAGE_SHIFT;
  167. buf->direct.buf = dma_alloc_coherent(&dev->pdev->dev,
  168. size, &t, GFP_KERNEL);
  169. if (!buf->direct.buf)
  170. return -ENOMEM;
  171. dma_unmap_addr_set(&buf->direct, mapping, t);
  172. memset(buf->direct.buf, 0, size);
  173. while (t & ((1 << shift) - 1)) {
  174. --shift;
  175. npages *= 2;
  176. }
  177. dma_list = kmalloc(npages * sizeof *dma_list, GFP_KERNEL);
  178. if (!dma_list)
  179. goto err_free;
  180. for (i = 0; i < npages; ++i)
  181. dma_list[i] = t + i * (1 << shift);
  182. } else {
  183. *is_direct = 0;
  184. npages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
  185. shift = PAGE_SHIFT;
  186. dma_list = kmalloc(npages * sizeof *dma_list, GFP_KERNEL);
  187. if (!dma_list)
  188. return -ENOMEM;
  189. buf->page_list = kmalloc(npages * sizeof *buf->page_list,
  190. GFP_KERNEL);
  191. if (!buf->page_list)
  192. goto err_out;
  193. for (i = 0; i < npages; ++i)
  194. buf->page_list[i].buf = NULL;
  195. for (i = 0; i < npages; ++i) {
  196. buf->page_list[i].buf =
  197. dma_alloc_coherent(&dev->pdev->dev, PAGE_SIZE,
  198. &t, GFP_KERNEL);
  199. if (!buf->page_list[i].buf)
  200. goto err_free;
  201. dma_list[i] = t;
  202. dma_unmap_addr_set(&buf->page_list[i], mapping, t);
  203. clear_page(buf->page_list[i].buf);
  204. }
  205. }
  206. err = mthca_mr_alloc_phys(dev, pd->pd_num,
  207. dma_list, shift, npages,
  208. 0, size,
  209. MTHCA_MPT_FLAG_LOCAL_READ |
  210. (hca_write ? MTHCA_MPT_FLAG_LOCAL_WRITE : 0),
  211. mr);
  212. if (err)
  213. goto err_free;
  214. kfree(dma_list);
  215. return 0;
  216. err_free:
  217. mthca_buf_free(dev, size, buf, *is_direct, NULL);
  218. err_out:
  219. kfree(dma_list);
  220. return err;
  221. }
  222. void mthca_buf_free(struct mthca_dev *dev, int size, union mthca_buf *buf,
  223. int is_direct, struct mthca_mr *mr)
  224. {
  225. int i;
  226. if (mr)
  227. mthca_free_mr(dev, mr);
  228. if (is_direct)
  229. dma_free_coherent(&dev->pdev->dev, size, buf->direct.buf,
  230. dma_unmap_addr(&buf->direct, mapping));
  231. else {
  232. for (i = 0; i < (size + PAGE_SIZE - 1) / PAGE_SIZE; ++i)
  233. dma_free_coherent(&dev->pdev->dev, PAGE_SIZE,
  234. buf->page_list[i].buf,
  235. dma_unmap_addr(&buf->page_list[i],
  236. mapping));
  237. kfree(buf->page_list);
  238. }
  239. }