doorbell.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (c) 2007 Cisco Systems, Inc. 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/slab.h>
  33. #include "mlx4_ib.h"
  34. struct mlx4_ib_db_pgdir {
  35. struct list_head list;
  36. DECLARE_BITMAP(order0, MLX4_IB_DB_PER_PAGE);
  37. DECLARE_BITMAP(order1, MLX4_IB_DB_PER_PAGE / 2);
  38. unsigned long *bits[2];
  39. __be32 *db_page;
  40. dma_addr_t db_dma;
  41. };
  42. static struct mlx4_ib_db_pgdir *mlx4_ib_alloc_db_pgdir(struct mlx4_ib_dev *dev)
  43. {
  44. struct mlx4_ib_db_pgdir *pgdir;
  45. pgdir = kzalloc(sizeof *pgdir, GFP_KERNEL);
  46. if (!pgdir)
  47. return NULL;
  48. bitmap_fill(pgdir->order1, MLX4_IB_DB_PER_PAGE / 2);
  49. pgdir->bits[0] = pgdir->order0;
  50. pgdir->bits[1] = pgdir->order1;
  51. pgdir->db_page = dma_alloc_coherent(dev->ib_dev.dma_device,
  52. PAGE_SIZE, &pgdir->db_dma,
  53. GFP_KERNEL);
  54. if (!pgdir->db_page) {
  55. kfree(pgdir);
  56. return NULL;
  57. }
  58. return pgdir;
  59. }
  60. static int mlx4_ib_alloc_db_from_pgdir(struct mlx4_ib_db_pgdir *pgdir,
  61. struct mlx4_ib_db *db, int order)
  62. {
  63. int o;
  64. int i;
  65. for (o = order; o <= 1; ++o) {
  66. i = find_first_bit(pgdir->bits[o], MLX4_IB_DB_PER_PAGE >> o);
  67. if (i < MLX4_IB_DB_PER_PAGE >> o)
  68. goto found;
  69. }
  70. return -ENOMEM;
  71. found:
  72. clear_bit(i, pgdir->bits[o]);
  73. i <<= o;
  74. if (o > order)
  75. set_bit(i ^ 1, pgdir->bits[order]);
  76. db->u.pgdir = pgdir;
  77. db->index = i;
  78. db->db = pgdir->db_page + db->index;
  79. db->dma = pgdir->db_dma + db->index * 4;
  80. db->order = order;
  81. return 0;
  82. }
  83. int mlx4_ib_db_alloc(struct mlx4_ib_dev *dev, struct mlx4_ib_db *db, int order)
  84. {
  85. struct mlx4_ib_db_pgdir *pgdir;
  86. int ret = 0;
  87. mutex_lock(&dev->pgdir_mutex);
  88. list_for_each_entry(pgdir, &dev->pgdir_list, list)
  89. if (!mlx4_ib_alloc_db_from_pgdir(pgdir, db, order))
  90. goto out;
  91. pgdir = mlx4_ib_alloc_db_pgdir(dev);
  92. if (!pgdir) {
  93. ret = -ENOMEM;
  94. goto out;
  95. }
  96. list_add(&pgdir->list, &dev->pgdir_list);
  97. /* This should never fail -- we just allocated an empty page: */
  98. WARN_ON(mlx4_ib_alloc_db_from_pgdir(pgdir, db, order));
  99. out:
  100. mutex_unlock(&dev->pgdir_mutex);
  101. return ret;
  102. }
  103. void mlx4_ib_db_free(struct mlx4_ib_dev *dev, struct mlx4_ib_db *db)
  104. {
  105. int o;
  106. int i;
  107. mutex_lock(&dev->pgdir_mutex);
  108. o = db->order;
  109. i = db->index;
  110. if (db->order == 0 && test_bit(i ^ 1, db->u.pgdir->order0)) {
  111. clear_bit(i ^ 1, db->u.pgdir->order0);
  112. ++o;
  113. }
  114. i >>= o;
  115. set_bit(i, db->u.pgdir->bits[o]);
  116. if (bitmap_full(db->u.pgdir->order1, MLX4_IB_DB_PER_PAGE / 2)) {
  117. dma_free_coherent(dev->ib_dev.dma_device, PAGE_SIZE,
  118. db->u.pgdir->db_page, db->u.pgdir->db_dma);
  119. list_del(&db->u.pgdir->list);
  120. kfree(db->u.pgdir);
  121. }
  122. mutex_unlock(&dev->pgdir_mutex);
  123. }
  124. struct mlx4_ib_user_db_page {
  125. struct list_head list;
  126. struct ib_umem *umem;
  127. unsigned long user_virt;
  128. int refcnt;
  129. };
  130. int mlx4_ib_db_map_user(struct mlx4_ib_ucontext *context, unsigned long virt,
  131. struct mlx4_ib_db *db)
  132. {
  133. struct mlx4_ib_user_db_page *page;
  134. struct ib_umem_chunk *chunk;
  135. int err = 0;
  136. mutex_lock(&context->db_page_mutex);
  137. list_for_each_entry(page, &context->db_page_list, list)
  138. if (page->user_virt == (virt & PAGE_MASK))
  139. goto found;
  140. page = kmalloc(sizeof *page, GFP_KERNEL);
  141. if (!page) {
  142. err = -ENOMEM;
  143. goto out;
  144. }
  145. page->user_virt = (virt & PAGE_MASK);
  146. page->refcnt = 0;
  147. page->umem = ib_umem_get(&context->ibucontext, virt & PAGE_MASK,
  148. PAGE_SIZE, 0);
  149. if (IS_ERR(page->umem)) {
  150. err = PTR_ERR(page->umem);
  151. kfree(page);
  152. goto out;
  153. }
  154. list_add(&page->list, &context->db_page_list);
  155. found:
  156. chunk = list_entry(page->umem->chunk_list.next, struct ib_umem_chunk, list);
  157. db->dma = sg_dma_address(chunk->page_list) + (virt & ~PAGE_MASK);
  158. db->u.user_page = page;
  159. ++page->refcnt;
  160. out:
  161. mutex_unlock(&context->db_page_mutex);
  162. return err;
  163. }
  164. void mlx4_ib_db_unmap_user(struct mlx4_ib_ucontext *context, struct mlx4_ib_db *db)
  165. {
  166. mutex_lock(&context->db_page_mutex);
  167. if (!--db->u.user_page->refcnt) {
  168. list_del(&db->u.user_page->list);
  169. ib_umem_release(db->u.user_page->umem);
  170. kfree(db->u.user_page);
  171. }
  172. mutex_unlock(&context->db_page_mutex);
  173. }