ah.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright(c) 2016 Intel Corporation.
  3. *
  4. * This file is provided under a dual BSD/GPLv2 license. When using or
  5. * redistributing this file, you may do so under either license.
  6. *
  7. * GPL LICENSE SUMMARY
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * BSD LICENSE
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions
  22. * are met:
  23. *
  24. * - Redistributions of source code must retain the above copyright
  25. * notice, this list of conditions and the following disclaimer.
  26. * - Redistributions in binary form must reproduce the above copyright
  27. * notice, this list of conditions and the following disclaimer in
  28. * the documentation and/or other materials provided with the
  29. * distribution.
  30. * - Neither the name of Intel Corporation nor the names of its
  31. * contributors may be used to endorse or promote products derived
  32. * from this software without specific prior written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  37. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  38. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  40. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  41. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  42. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  44. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. *
  46. */
  47. #include <linux/slab.h>
  48. #include "ah.h"
  49. #include "vt.h" /* for prints */
  50. /**
  51. * rvt_check_ah - validate the attributes of AH
  52. * @ibdev: the ib device
  53. * @ah_attr: the attributes of the AH
  54. *
  55. * If driver supports a more detailed check_ah function call back to it
  56. * otherwise just check the basics.
  57. *
  58. * Return: 0 on success
  59. */
  60. int rvt_check_ah(struct ib_device *ibdev,
  61. struct rdma_ah_attr *ah_attr)
  62. {
  63. int err;
  64. int port_num = rdma_ah_get_port_num(ah_attr);
  65. struct ib_port_attr port_attr;
  66. struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
  67. enum rdma_link_layer link = rdma_port_get_link_layer(ibdev, port_num);
  68. u32 dlid = rdma_ah_get_dlid(ah_attr);
  69. u8 ah_flags = rdma_ah_get_ah_flags(ah_attr);
  70. u8 static_rate = rdma_ah_get_static_rate(ah_attr);
  71. err = ib_query_port(ibdev, port_num, &port_attr);
  72. if (err)
  73. return -EINVAL;
  74. if (port_num < 1 ||
  75. port_num > ibdev->phys_port_cnt)
  76. return -EINVAL;
  77. if (static_rate != IB_RATE_PORT_CURRENT &&
  78. ib_rate_to_mbps(static_rate) < 0)
  79. return -EINVAL;
  80. if ((ah_flags & IB_AH_GRH) &&
  81. rdma_ah_read_grh(ah_attr)->sgid_index >= port_attr.gid_tbl_len)
  82. return -EINVAL;
  83. if (link != IB_LINK_LAYER_ETHERNET) {
  84. if (dlid == 0)
  85. return -EINVAL;
  86. if (dlid >= be16_to_cpu(IB_MULTICAST_LID_BASE) &&
  87. dlid != be16_to_cpu(IB_LID_PERMISSIVE) &&
  88. !(ah_flags & IB_AH_GRH))
  89. return -EINVAL;
  90. }
  91. if (rdi->driver_f.check_ah)
  92. return rdi->driver_f.check_ah(ibdev, ah_attr);
  93. return 0;
  94. }
  95. EXPORT_SYMBOL(rvt_check_ah);
  96. /**
  97. * rvt_create_ah - create an address handle
  98. * @pd: the protection domain
  99. * @ah_attr: the attributes of the AH
  100. *
  101. * This may be called from interrupt context.
  102. *
  103. * Return: newly allocated ah
  104. */
  105. struct ib_ah *rvt_create_ah(struct ib_pd *pd,
  106. struct rdma_ah_attr *ah_attr)
  107. {
  108. struct rvt_ah *ah;
  109. struct rvt_dev_info *dev = ib_to_rvt(pd->device);
  110. unsigned long flags;
  111. if (rvt_check_ah(pd->device, ah_attr))
  112. return ERR_PTR(-EINVAL);
  113. ah = kmalloc(sizeof(*ah), GFP_ATOMIC);
  114. if (!ah)
  115. return ERR_PTR(-ENOMEM);
  116. spin_lock_irqsave(&dev->n_ahs_lock, flags);
  117. if (dev->n_ahs_allocated == dev->dparms.props.max_ah) {
  118. spin_unlock_irqrestore(&dev->n_ahs_lock, flags);
  119. kfree(ah);
  120. return ERR_PTR(-ENOMEM);
  121. }
  122. dev->n_ahs_allocated++;
  123. spin_unlock_irqrestore(&dev->n_ahs_lock, flags);
  124. ah->attr = *ah_attr;
  125. atomic_set(&ah->refcount, 0);
  126. if (dev->driver_f.notify_new_ah)
  127. dev->driver_f.notify_new_ah(pd->device, ah_attr, ah);
  128. return &ah->ibah;
  129. }
  130. /**
  131. * rvt_destory_ah - Destory an address handle
  132. * @ibah: address handle
  133. *
  134. * Return: 0 on success
  135. */
  136. int rvt_destroy_ah(struct ib_ah *ibah)
  137. {
  138. struct rvt_dev_info *dev = ib_to_rvt(ibah->device);
  139. struct rvt_ah *ah = ibah_to_rvtah(ibah);
  140. unsigned long flags;
  141. if (atomic_read(&ah->refcount) != 0)
  142. return -EBUSY;
  143. spin_lock_irqsave(&dev->n_ahs_lock, flags);
  144. dev->n_ahs_allocated--;
  145. spin_unlock_irqrestore(&dev->n_ahs_lock, flags);
  146. kfree(ah);
  147. return 0;
  148. }
  149. /**
  150. * rvt_modify_ah - modify an ah with given attrs
  151. * @ibah: address handle to modify
  152. * @ah_attr: attrs to apply
  153. *
  154. * Return: 0 on success
  155. */
  156. int rvt_modify_ah(struct ib_ah *ibah, struct rdma_ah_attr *ah_attr)
  157. {
  158. struct rvt_ah *ah = ibah_to_rvtah(ibah);
  159. if (rvt_check_ah(ibah->device, ah_attr))
  160. return -EINVAL;
  161. ah->attr = *ah_attr;
  162. return 0;
  163. }
  164. /**
  165. * rvt_query_ah - return attrs for ah
  166. * @ibah: address handle to query
  167. * @ah_attr: return info in this
  168. *
  169. * Return: always 0
  170. */
  171. int rvt_query_ah(struct ib_ah *ibah, struct rdma_ah_attr *ah_attr)
  172. {
  173. struct rvt_ah *ah = ibah_to_rvtah(ibah);
  174. *ah_attr = ah->attr;
  175. return 0;
  176. }