c2_alloc.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. dma_addr_t dma_addr;
  43. new_head = dma_alloc_coherent(&c2dev->pcidev->dev, PAGE_SIZE,
  44. &dma_addr, gfp_mask);
  45. if (new_head == NULL)
  46. return -ENOMEM;
  47. new_head->dma_addr = dma_addr;
  48. pci_unmap_addr_set(new_head, mapping, new_head->dma_addr);
  49. new_head->next = NULL;
  50. new_head->head = 0;
  51. /* build list where each index is the next free slot */
  52. for (i = 0;
  53. i < (PAGE_SIZE - sizeof(struct sp_chunk) -
  54. sizeof(u16)) / sizeof(u16) - 1;
  55. i++) {
  56. new_head->shared_ptr[i] = i + 1;
  57. }
  58. /* terminate list */
  59. new_head->shared_ptr[i] = 0xFFFF;
  60. *head = new_head;
  61. return 0;
  62. }
  63. int c2_init_mqsp_pool(struct c2_dev *c2dev, gfp_t gfp_mask,
  64. struct sp_chunk **root)
  65. {
  66. return c2_alloc_mqsp_chunk(c2dev, gfp_mask, root);
  67. }
  68. void c2_free_mqsp_pool(struct c2_dev *c2dev, struct sp_chunk *root)
  69. {
  70. struct sp_chunk *next;
  71. while (root) {
  72. next = root->next;
  73. dma_free_coherent(&c2dev->pcidev->dev, PAGE_SIZE, root,
  74. pci_unmap_addr(root, mapping));
  75. root = next;
  76. }
  77. }
  78. __be16 *c2_alloc_mqsp(struct c2_dev *c2dev, struct sp_chunk *head,
  79. dma_addr_t *dma_addr, gfp_t gfp_mask)
  80. {
  81. u16 mqsp;
  82. while (head) {
  83. mqsp = head->head;
  84. if (mqsp != 0xFFFF) {
  85. head->head = head->shared_ptr[mqsp];
  86. break;
  87. } else if (head->next == NULL) {
  88. if (c2_alloc_mqsp_chunk(c2dev, gfp_mask, &head->next) ==
  89. 0) {
  90. head = head->next;
  91. mqsp = head->head;
  92. head->head = head->shared_ptr[mqsp];
  93. break;
  94. } else
  95. return NULL;
  96. } else
  97. head = head->next;
  98. }
  99. if (head) {
  100. *dma_addr = head->dma_addr +
  101. ((unsigned long) &(head->shared_ptr[mqsp]) -
  102. (unsigned long) head);
  103. pr_debug("%s addr %p dma_addr %llx\n", __func__,
  104. &(head->shared_ptr[mqsp]), (unsigned long long) *dma_addr);
  105. return (__force __be16 *) &(head->shared_ptr[mqsp]);
  106. }
  107. return NULL;
  108. }
  109. void c2_free_mqsp(__be16 *mqsp)
  110. {
  111. struct sp_chunk *head;
  112. u16 idx;
  113. /* The chunk containing this ptr begins at the page boundary */
  114. head = (struct sp_chunk *) ((unsigned long) mqsp & PAGE_MASK);
  115. /* Link head to new mqsp */
  116. *mqsp = (__force __be16) head->head;
  117. /* Compute the shared_ptr index */
  118. idx = ((unsigned long) mqsp & ~PAGE_MASK) >> 1;
  119. idx -= (unsigned long) &(((struct sp_chunk *) 0)->shared_ptr[0]) >> 1;
  120. /* Point this index at the head */
  121. head->shared_ptr[idx] = head->head;
  122. /* Point head at this index */
  123. head->head = idx;
  124. }