agent.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright (c) 2004, 2005 Mellanox Technologies Ltd. All rights reserved.
  3. * Copyright (c) 2004, 2005 Infinicon Corporation. All rights reserved.
  4. * Copyright (c) 2004, 2005 Intel Corporation. All rights reserved.
  5. * Copyright (c) 2004, 2005 Topspin Corporation. All rights reserved.
  6. * Copyright (c) 2004-2007 Voltaire Corporation. All rights reserved.
  7. * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
  8. *
  9. * This software is available to you under a choice of one of two
  10. * licenses. You may choose to be licensed under the terms of the GNU
  11. * General Public License (GPL) Version 2, available from the file
  12. * COPYING in the main directory of this source tree, or the
  13. * OpenIB.org BSD license below:
  14. *
  15. * Redistribution and use in source and binary forms, with or
  16. * without modification, are permitted provided that the following
  17. * conditions are met:
  18. *
  19. * - Redistributions of source code must retain the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer.
  22. *
  23. * - Redistributions in binary form must reproduce the above
  24. * copyright notice, this list of conditions and the following
  25. * disclaimer in the documentation and/or other materials
  26. * provided with the distribution.
  27. *
  28. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  29. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  30. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  31. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  32. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  33. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  34. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  35. * SOFTWARE.
  36. *
  37. */
  38. #include <linux/slab.h>
  39. #include <linux/string.h>
  40. #include "agent.h"
  41. #include "smi.h"
  42. #include "mad_priv.h"
  43. #define SPFX "ib_agent: "
  44. struct ib_agent_port_private {
  45. struct list_head port_list;
  46. struct ib_mad_agent *agent[2];
  47. };
  48. static DEFINE_SPINLOCK(ib_agent_port_list_lock);
  49. static LIST_HEAD(ib_agent_port_list);
  50. static struct ib_agent_port_private *
  51. __ib_get_agent_port(const struct ib_device *device, int port_num)
  52. {
  53. struct ib_agent_port_private *entry;
  54. list_for_each_entry(entry, &ib_agent_port_list, port_list) {
  55. /* Need to check both agent[0] and agent[1], as an agent port
  56. * may only have one of them
  57. */
  58. if (entry->agent[0] &&
  59. entry->agent[0]->device == device &&
  60. entry->agent[0]->port_num == port_num)
  61. return entry;
  62. if (entry->agent[1] &&
  63. entry->agent[1]->device == device &&
  64. entry->agent[1]->port_num == port_num)
  65. return entry;
  66. }
  67. return NULL;
  68. }
  69. static struct ib_agent_port_private *
  70. ib_get_agent_port(const struct ib_device *device, int port_num)
  71. {
  72. struct ib_agent_port_private *entry;
  73. unsigned long flags;
  74. spin_lock_irqsave(&ib_agent_port_list_lock, flags);
  75. entry = __ib_get_agent_port(device, port_num);
  76. spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
  77. return entry;
  78. }
  79. void agent_send_response(const struct ib_mad_hdr *mad_hdr, const struct ib_grh *grh,
  80. const struct ib_wc *wc, const struct ib_device *device,
  81. int port_num, int qpn, size_t resp_mad_len, bool opa)
  82. {
  83. struct ib_agent_port_private *port_priv;
  84. struct ib_mad_agent *agent;
  85. struct ib_mad_send_buf *send_buf;
  86. struct ib_ah *ah;
  87. struct ib_mad_send_wr_private *mad_send_wr;
  88. if (rdma_cap_ib_switch(device))
  89. port_priv = ib_get_agent_port(device, 0);
  90. else
  91. port_priv = ib_get_agent_port(device, port_num);
  92. if (!port_priv) {
  93. dev_err(&device->dev, "Unable to find port agent\n");
  94. return;
  95. }
  96. agent = port_priv->agent[qpn];
  97. ah = ib_create_ah_from_wc(agent->qp->pd, wc, grh, port_num);
  98. if (IS_ERR(ah)) {
  99. dev_err(&device->dev, "ib_create_ah_from_wc error %ld\n",
  100. PTR_ERR(ah));
  101. return;
  102. }
  103. if (opa && mad_hdr->base_version != OPA_MGMT_BASE_VERSION)
  104. resp_mad_len = IB_MGMT_MAD_SIZE;
  105. send_buf = ib_create_send_mad(agent, wc->src_qp, wc->pkey_index, 0,
  106. IB_MGMT_MAD_HDR,
  107. resp_mad_len - IB_MGMT_MAD_HDR,
  108. GFP_KERNEL,
  109. mad_hdr->base_version);
  110. if (IS_ERR(send_buf)) {
  111. dev_err(&device->dev, "ib_create_send_mad error\n");
  112. goto err1;
  113. }
  114. memcpy(send_buf->mad, mad_hdr, resp_mad_len);
  115. send_buf->ah = ah;
  116. if (rdma_cap_ib_switch(device)) {
  117. mad_send_wr = container_of(send_buf,
  118. struct ib_mad_send_wr_private,
  119. send_buf);
  120. mad_send_wr->send_wr.port_num = port_num;
  121. }
  122. if (ib_post_send_mad(send_buf, NULL)) {
  123. dev_err(&device->dev, "ib_post_send_mad error\n");
  124. goto err2;
  125. }
  126. return;
  127. err2:
  128. ib_free_send_mad(send_buf);
  129. err1:
  130. rdma_destroy_ah(ah, RDMA_DESTROY_AH_SLEEPABLE);
  131. }
  132. static void agent_send_handler(struct ib_mad_agent *mad_agent,
  133. struct ib_mad_send_wc *mad_send_wc)
  134. {
  135. rdma_destroy_ah(mad_send_wc->send_buf->ah, RDMA_DESTROY_AH_SLEEPABLE);
  136. ib_free_send_mad(mad_send_wc->send_buf);
  137. }
  138. int ib_agent_port_open(struct ib_device *device, int port_num)
  139. {
  140. struct ib_agent_port_private *port_priv;
  141. unsigned long flags;
  142. int ret;
  143. /* Create new device info */
  144. port_priv = kzalloc(sizeof *port_priv, GFP_KERNEL);
  145. if (!port_priv) {
  146. ret = -ENOMEM;
  147. goto error1;
  148. }
  149. if (rdma_cap_ib_smi(device, port_num)) {
  150. /* Obtain send only MAD agent for SMI QP */
  151. port_priv->agent[0] = ib_register_mad_agent(device, port_num,
  152. IB_QPT_SMI, NULL, 0,
  153. &agent_send_handler,
  154. NULL, NULL, 0);
  155. if (IS_ERR(port_priv->agent[0])) {
  156. ret = PTR_ERR(port_priv->agent[0]);
  157. goto error2;
  158. }
  159. }
  160. if (rdma_cap_ib_cm(device, port_num)) {
  161. /* Obtain send only MAD agent for GSI QP */
  162. port_priv->agent[1] = ib_register_mad_agent(device, port_num,
  163. IB_QPT_GSI, NULL, 0,
  164. &agent_send_handler,
  165. NULL, NULL, 0);
  166. if (IS_ERR(port_priv->agent[1])) {
  167. ret = PTR_ERR(port_priv->agent[1]);
  168. goto error3;
  169. }
  170. }
  171. spin_lock_irqsave(&ib_agent_port_list_lock, flags);
  172. list_add_tail(&port_priv->port_list, &ib_agent_port_list);
  173. spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
  174. return 0;
  175. error3:
  176. if (port_priv->agent[0])
  177. ib_unregister_mad_agent(port_priv->agent[0]);
  178. error2:
  179. kfree(port_priv);
  180. error1:
  181. return ret;
  182. }
  183. int ib_agent_port_close(struct ib_device *device, int port_num)
  184. {
  185. struct ib_agent_port_private *port_priv;
  186. unsigned long flags;
  187. spin_lock_irqsave(&ib_agent_port_list_lock, flags);
  188. port_priv = __ib_get_agent_port(device, port_num);
  189. if (port_priv == NULL) {
  190. spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
  191. dev_err(&device->dev, "Port %d not found\n", port_num);
  192. return -ENODEV;
  193. }
  194. list_del(&port_priv->port_list);
  195. spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
  196. if (port_priv->agent[1])
  197. ib_unregister_mad_agent(port_priv->agent[1]);
  198. if (port_priv->agent[0])
  199. ib_unregister_mad_agent(port_priv->agent[0]);
  200. kfree(port_priv);
  201. return 0;
  202. }