mthca_allocator.c 7.5 KB

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