agent.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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, 2005 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. * $Id: agent.c 1389 2004-12-27 22:56:47Z roland $
  38. */
  39. #include "agent.h"
  40. #include "smi.h"
  41. #define SPFX "ib_agent: "
  42. struct ib_agent_port_private {
  43. struct list_head port_list;
  44. struct ib_mad_agent *agent[2];
  45. };
  46. static DEFINE_SPINLOCK(ib_agent_port_list_lock);
  47. static LIST_HEAD(ib_agent_port_list);
  48. static struct ib_agent_port_private *
  49. __ib_get_agent_port(struct ib_device *device, int port_num)
  50. {
  51. struct ib_agent_port_private *entry;
  52. list_for_each_entry(entry, &ib_agent_port_list, port_list) {
  53. if (entry->agent[0]->device == device &&
  54. entry->agent[0]->port_num == port_num)
  55. return entry;
  56. }
  57. return NULL;
  58. }
  59. static struct ib_agent_port_private *
  60. ib_get_agent_port(struct ib_device *device, int port_num)
  61. {
  62. struct ib_agent_port_private *entry;
  63. unsigned long flags;
  64. spin_lock_irqsave(&ib_agent_port_list_lock, flags);
  65. entry = __ib_get_agent_port(device, port_num);
  66. spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
  67. return entry;
  68. }
  69. int smi_check_local_dr_smp(struct ib_smp *smp,
  70. struct ib_device *device,
  71. int port_num)
  72. {
  73. struct ib_agent_port_private *port_priv;
  74. if (smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
  75. return 1;
  76. port_priv = ib_get_agent_port(device, port_num);
  77. if (!port_priv) {
  78. printk(KERN_DEBUG SPFX "smi_check_local_dr_smp %s port %d "
  79. "not open\n", device->name, port_num);
  80. return 1;
  81. }
  82. return smi_check_local_smp(port_priv->agent[0], smp);
  83. }
  84. int agent_send_response(struct ib_mad *mad, struct ib_grh *grh,
  85. struct ib_wc *wc, struct ib_device *device,
  86. int port_num, int qpn)
  87. {
  88. struct ib_agent_port_private *port_priv;
  89. struct ib_mad_agent *agent;
  90. struct ib_mad_send_buf *send_buf;
  91. struct ib_ah *ah;
  92. int ret;
  93. port_priv = ib_get_agent_port(device, port_num);
  94. if (!port_priv) {
  95. printk(KERN_ERR SPFX "Unable to find port agent\n");
  96. return -ENODEV;
  97. }
  98. agent = port_priv->agent[qpn];
  99. ah = ib_create_ah_from_wc(agent->qp->pd, wc, grh, port_num);
  100. if (IS_ERR(ah)) {
  101. ret = PTR_ERR(ah);
  102. printk(KERN_ERR SPFX "ib_create_ah_from_wc error:%d\n", ret);
  103. return ret;
  104. }
  105. send_buf = ib_create_send_mad(agent, wc->src_qp, wc->pkey_index, 0,
  106. IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
  107. GFP_KERNEL);
  108. if (IS_ERR(send_buf)) {
  109. ret = PTR_ERR(send_buf);
  110. printk(KERN_ERR SPFX "ib_create_send_mad error:%d\n", ret);
  111. goto err1;
  112. }
  113. memcpy(send_buf->mad, mad, sizeof *mad);
  114. send_buf->ah = ah;
  115. if ((ret = ib_post_send_mad(send_buf, NULL))) {
  116. printk(KERN_ERR SPFX "ib_post_send_mad error:%d\n", ret);
  117. goto err2;
  118. }
  119. return 0;
  120. err2:
  121. ib_free_send_mad(send_buf);
  122. err1:
  123. ib_destroy_ah(ah);
  124. return ret;
  125. }
  126. static void agent_send_handler(struct ib_mad_agent *mad_agent,
  127. struct ib_mad_send_wc *mad_send_wc)
  128. {
  129. ib_destroy_ah(mad_send_wc->send_buf->ah);
  130. ib_free_send_mad(mad_send_wc->send_buf);
  131. }
  132. int ib_agent_port_open(struct ib_device *device, int port_num)
  133. {
  134. struct ib_agent_port_private *port_priv;
  135. unsigned long flags;
  136. int ret;
  137. /* Create new device info */
  138. port_priv = kzalloc(sizeof *port_priv, GFP_KERNEL);
  139. if (!port_priv) {
  140. printk(KERN_ERR SPFX "No memory for ib_agent_port_private\n");
  141. ret = -ENOMEM;
  142. goto error1;
  143. }
  144. /* Obtain send only MAD agent for SMI QP */
  145. port_priv->agent[0] = ib_register_mad_agent(device, port_num,
  146. IB_QPT_SMI, NULL, 0,
  147. &agent_send_handler,
  148. NULL, NULL);
  149. if (IS_ERR(port_priv->agent[0])) {
  150. ret = PTR_ERR(port_priv->agent[0]);
  151. goto error2;
  152. }
  153. /* Obtain send only MAD agent for GSI QP */
  154. port_priv->agent[1] = ib_register_mad_agent(device, port_num,
  155. IB_QPT_GSI, NULL, 0,
  156. &agent_send_handler,
  157. NULL, NULL);
  158. if (IS_ERR(port_priv->agent[1])) {
  159. ret = PTR_ERR(port_priv->agent[1]);
  160. goto error3;
  161. }
  162. spin_lock_irqsave(&ib_agent_port_list_lock, flags);
  163. list_add_tail(&port_priv->port_list, &ib_agent_port_list);
  164. spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
  165. return 0;
  166. error3:
  167. ib_unregister_mad_agent(port_priv->agent[0]);
  168. error2:
  169. kfree(port_priv);
  170. error1:
  171. return ret;
  172. }
  173. int ib_agent_port_close(struct ib_device *device, int port_num)
  174. {
  175. struct ib_agent_port_private *port_priv;
  176. unsigned long flags;
  177. spin_lock_irqsave(&ib_agent_port_list_lock, flags);
  178. port_priv = __ib_get_agent_port(device, port_num);
  179. if (port_priv == NULL) {
  180. spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
  181. printk(KERN_ERR SPFX "Port %d not found\n", port_num);
  182. return -ENODEV;
  183. }
  184. list_del(&port_priv->port_list);
  185. spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
  186. ib_unregister_mad_agent(port_priv->agent[1]);
  187. ib_unregister_mad_agent(port_priv->agent[0]);
  188. kfree(port_priv);
  189. return 0;
  190. }