ipath_mmap.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright (c) 2006, 2007 QLogic Corporation. 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/module.h>
  33. #include <linux/vmalloc.h>
  34. #include <linux/mm.h>
  35. #include <linux/errno.h>
  36. #include <asm/pgtable.h>
  37. #include "ipath_verbs.h"
  38. /**
  39. * ipath_release_mmap_info - free mmap info structure
  40. * @ref: a pointer to the kref within struct ipath_mmap_info
  41. */
  42. void ipath_release_mmap_info(struct kref *ref)
  43. {
  44. struct ipath_mmap_info *ip =
  45. container_of(ref, struct ipath_mmap_info, ref);
  46. struct ipath_ibdev *dev = to_idev(ip->context->device);
  47. spin_lock_irq(&dev->pending_lock);
  48. list_del(&ip->pending_mmaps);
  49. spin_unlock_irq(&dev->pending_lock);
  50. vfree(ip->obj);
  51. kfree(ip);
  52. }
  53. /*
  54. * open and close keep track of how many times the CQ is mapped,
  55. * to avoid releasing it.
  56. */
  57. static void ipath_vma_open(struct vm_area_struct *vma)
  58. {
  59. struct ipath_mmap_info *ip = vma->vm_private_data;
  60. kref_get(&ip->ref);
  61. }
  62. static void ipath_vma_close(struct vm_area_struct *vma)
  63. {
  64. struct ipath_mmap_info *ip = vma->vm_private_data;
  65. kref_put(&ip->ref, ipath_release_mmap_info);
  66. }
  67. static struct vm_operations_struct ipath_vm_ops = {
  68. .open = ipath_vma_open,
  69. .close = ipath_vma_close,
  70. };
  71. /**
  72. * ipath_mmap - create a new mmap region
  73. * @context: the IB user context of the process making the mmap() call
  74. * @vma: the VMA to be initialized
  75. * Return zero if the mmap is OK. Otherwise, return an errno.
  76. */
  77. int ipath_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
  78. {
  79. struct ipath_ibdev *dev = to_idev(context->device);
  80. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  81. unsigned long size = vma->vm_end - vma->vm_start;
  82. struct ipath_mmap_info *ip, *pp;
  83. int ret = -EINVAL;
  84. /*
  85. * Search the device's list of objects waiting for a mmap call.
  86. * Normally, this list is very short since a call to create a
  87. * CQ, QP, or SRQ is soon followed by a call to mmap().
  88. */
  89. spin_lock_irq(&dev->pending_lock);
  90. list_for_each_entry_safe(ip, pp, &dev->pending_mmaps,
  91. pending_mmaps) {
  92. /* Only the creator is allowed to mmap the object */
  93. if (context != ip->context || (__u64) offset != ip->offset)
  94. continue;
  95. /* Don't allow a mmap larger than the object. */
  96. if (size > ip->size)
  97. break;
  98. list_del_init(&ip->pending_mmaps);
  99. spin_unlock_irq(&dev->pending_lock);
  100. ret = remap_vmalloc_range(vma, ip->obj, 0);
  101. if (ret)
  102. goto done;
  103. vma->vm_ops = &ipath_vm_ops;
  104. vma->vm_private_data = ip;
  105. ipath_vma_open(vma);
  106. goto done;
  107. }
  108. spin_unlock_irq(&dev->pending_lock);
  109. done:
  110. return ret;
  111. }
  112. /*
  113. * Allocate information for ipath_mmap
  114. */
  115. struct ipath_mmap_info *ipath_create_mmap_info(struct ipath_ibdev *dev,
  116. u32 size,
  117. struct ib_ucontext *context,
  118. void *obj) {
  119. struct ipath_mmap_info *ip;
  120. ip = kmalloc(sizeof *ip, GFP_KERNEL);
  121. if (!ip)
  122. goto bail;
  123. size = PAGE_ALIGN(size);
  124. spin_lock_irq(&dev->mmap_offset_lock);
  125. if (dev->mmap_offset == 0)
  126. dev->mmap_offset = PAGE_SIZE;
  127. ip->offset = dev->mmap_offset;
  128. dev->mmap_offset += size;
  129. spin_unlock_irq(&dev->mmap_offset_lock);
  130. INIT_LIST_HEAD(&ip->pending_mmaps);
  131. ip->size = size;
  132. ip->context = context;
  133. ip->obj = obj;
  134. kref_init(&ip->ref);
  135. bail:
  136. return ip;
  137. }
  138. void ipath_update_mmap_info(struct ipath_ibdev *dev,
  139. struct ipath_mmap_info *ip,
  140. u32 size, void *obj) {
  141. size = PAGE_ALIGN(size);
  142. spin_lock_irq(&dev->mmap_offset_lock);
  143. if (dev->mmap_offset == 0)
  144. dev->mmap_offset = PAGE_SIZE;
  145. ip->offset = dev->mmap_offset;
  146. dev->mmap_offset += size;
  147. spin_unlock_irq(&dev->mmap_offset_lock);
  148. ip->size = size;
  149. ip->obj = obj;
  150. }