c2_alloc.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (c) 2004 Topspin Communications. All rights reserved.
  3. * Copyright (c) 2005 Open Grid Computing, Inc. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #include <linux/errno.h>
  34. #include <linux/slab.h>
  35. #include <linux/bitmap.h>
  36. #include "c2.h"
  37. static int c2_alloc_mqsp_chunk(struct c2_dev *c2dev, gfp_t gfp_mask,
  38. struct sp_chunk **head)
  39. {
  40. int i;
  41. struct sp_chunk *new_head;
  42. new_head = (struct sp_chunk *) __get_free_page(gfp_mask);
  43. if (new_head == NULL)
  44. return -ENOMEM;
  45. new_head->dma_addr = dma_map_single(c2dev->ibdev.dma_device, new_head,
  46. PAGE_SIZE, DMA_FROM_DEVICE);
  47. pci_unmap_addr_set(new_head, mapping, new_head->dma_addr);
  48. new_head->next = NULL;
  49. new_head->head = 0;
  50. /* build list where each index is the next free slot */
  51. for (i = 0;
  52. i < (PAGE_SIZE - sizeof(struct sp_chunk) -
  53. sizeof(u16)) / sizeof(u16) - 1;
  54. i++) {
  55. new_head->shared_ptr[i] = i + 1;
  56. }
  57. /* terminate list */
  58. new_head->shared_ptr[i] = 0xFFFF;
  59. *head = new_head;
  60. return 0;
  61. }
  62. int c2_init_mqsp_pool(struct c2_dev *c2dev, gfp_t gfp_mask,
  63. struct sp_chunk **root)
  64. {
  65. return c2_alloc_mqsp_chunk(c2dev, gfp_mask, root);
  66. }
  67. void c2_free_mqsp_pool(struct c2_dev *c2dev, struct sp_chunk *root)
  68. {
  69. struct sp_chunk *next;
  70. while (root) {
  71. next = root->next;
  72. dma_unmap_single(c2dev->ibdev.dma_device,
  73. pci_unmap_addr(root, mapping), PAGE_SIZE,
  74. DMA_FROM_DEVICE);
  75. __free_page((struct page *) root);
  76. root = next;
  77. }
  78. }
  79. u16 *c2_alloc_mqsp(struct c2_dev *c2dev, struct sp_chunk *head,
  80. dma_addr_t *dma_addr, gfp_t gfp_mask)
  81. {
  82. u16 mqsp;
  83. while (head) {
  84. mqsp = head->head;
  85. if (mqsp != 0xFFFF) {
  86. head->head = head->shared_ptr[mqsp];
  87. break;
  88. } else if (head->next == NULL) {
  89. if (c2_alloc_mqsp_chunk(c2dev, gfp_mask, &head->next) ==
  90. 0) {
  91. head = head->next;
  92. mqsp = head->head;
  93. head->head = head->shared_ptr[mqsp];
  94. break;
  95. } else
  96. return NULL;
  97. } else
  98. head = head->next;
  99. }
  100. if (head) {
  101. *dma_addr = head->dma_addr +
  102. ((unsigned long) &(head->shared_ptr[mqsp]) -
  103. (unsigned long) head);
  104. pr_debug("%s addr %p dma_addr %llx\n", __FUNCTION__,
  105. &(head->shared_ptr[mqsp]), (unsigned long long) *dma_addr);
  106. return &(head->shared_ptr[mqsp]);
  107. }
  108. return NULL;
  109. }
  110. void c2_free_mqsp(u16 * mqsp)
  111. {
  112. struct sp_chunk *head;
  113. u16 idx;
  114. /* The chunk containing this ptr begins at the page boundary */
  115. head = (struct sp_chunk *) ((unsigned long) mqsp & PAGE_MASK);
  116. /* Link head to new mqsp */
  117. *mqsp = head->head;
  118. /* Compute the shared_ptr index */
  119. idx = ((unsigned long) mqsp & ~PAGE_MASK) >> 1;
  120. idx -= (unsigned long) &(((struct sp_chunk *) 0)->shared_ptr[0]) >> 1;
  121. /* Point this index at the head */
  122. head->shared_ptr[idx] = head->head;
  123. /* Point head at this index */
  124. head->head = idx;
  125. }