mad.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * Copyright (c) 2007 Cisco Systems, Inc. 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 <rdma/ib_mad.h>
  33. #include <rdma/ib_smi.h>
  34. #include <linux/mlx4/cmd.h>
  35. #include "mlx4_ib.h"
  36. enum {
  37. MLX4_IB_VENDOR_CLASS1 = 0x9,
  38. MLX4_IB_VENDOR_CLASS2 = 0xa
  39. };
  40. int mlx4_MAD_IFC(struct mlx4_ib_dev *dev, int ignore_mkey, int ignore_bkey,
  41. int port, struct ib_wc *in_wc, struct ib_grh *in_grh,
  42. void *in_mad, void *response_mad)
  43. {
  44. struct mlx4_cmd_mailbox *inmailbox, *outmailbox;
  45. void *inbox;
  46. int err;
  47. u32 in_modifier = port;
  48. u8 op_modifier = 0;
  49. inmailbox = mlx4_alloc_cmd_mailbox(dev->dev);
  50. if (IS_ERR(inmailbox))
  51. return PTR_ERR(inmailbox);
  52. inbox = inmailbox->buf;
  53. outmailbox = mlx4_alloc_cmd_mailbox(dev->dev);
  54. if (IS_ERR(outmailbox)) {
  55. mlx4_free_cmd_mailbox(dev->dev, inmailbox);
  56. return PTR_ERR(outmailbox);
  57. }
  58. memcpy(inbox, in_mad, 256);
  59. /*
  60. * Key check traps can't be generated unless we have in_wc to
  61. * tell us where to send the trap.
  62. */
  63. if (ignore_mkey || !in_wc)
  64. op_modifier |= 0x1;
  65. if (ignore_bkey || !in_wc)
  66. op_modifier |= 0x2;
  67. if (in_wc) {
  68. struct {
  69. __be32 my_qpn;
  70. u32 reserved1;
  71. __be32 rqpn;
  72. u8 sl;
  73. u8 g_path;
  74. u16 reserved2[2];
  75. __be16 pkey;
  76. u32 reserved3[11];
  77. u8 grh[40];
  78. } *ext_info;
  79. memset(inbox + 256, 0, 256);
  80. ext_info = inbox + 256;
  81. ext_info->my_qpn = cpu_to_be32(in_wc->qp->qp_num);
  82. ext_info->rqpn = cpu_to_be32(in_wc->src_qp);
  83. ext_info->sl = in_wc->sl << 4;
  84. ext_info->g_path = in_wc->dlid_path_bits |
  85. (in_wc->wc_flags & IB_WC_GRH ? 0x80 : 0);
  86. ext_info->pkey = cpu_to_be16(in_wc->pkey_index);
  87. if (in_grh)
  88. memcpy(ext_info->grh, in_grh, 40);
  89. op_modifier |= 0x4;
  90. in_modifier |= in_wc->slid << 16;
  91. }
  92. err = mlx4_cmd_box(dev->dev, inmailbox->dma, outmailbox->dma,
  93. in_modifier, op_modifier,
  94. MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C);
  95. if (!err)
  96. memcpy(response_mad, outmailbox->buf, 256);
  97. mlx4_free_cmd_mailbox(dev->dev, inmailbox);
  98. mlx4_free_cmd_mailbox(dev->dev, outmailbox);
  99. return err;
  100. }
  101. static void update_sm_ah(struct mlx4_ib_dev *dev, u8 port_num, u16 lid, u8 sl)
  102. {
  103. struct ib_ah *new_ah;
  104. struct ib_ah_attr ah_attr;
  105. if (!dev->send_agent[port_num - 1][0])
  106. return;
  107. memset(&ah_attr, 0, sizeof ah_attr);
  108. ah_attr.dlid = lid;
  109. ah_attr.sl = sl;
  110. ah_attr.port_num = port_num;
  111. new_ah = ib_create_ah(dev->send_agent[port_num - 1][0]->qp->pd,
  112. &ah_attr);
  113. if (IS_ERR(new_ah))
  114. return;
  115. spin_lock(&dev->sm_lock);
  116. if (dev->sm_ah[port_num - 1])
  117. ib_destroy_ah(dev->sm_ah[port_num - 1]);
  118. dev->sm_ah[port_num - 1] = new_ah;
  119. spin_unlock(&dev->sm_lock);
  120. }
  121. /*
  122. * Snoop SM MADs for port info and P_Key table sets, so we can
  123. * synthesize LID change and P_Key change events.
  124. */
  125. static void smp_snoop(struct ib_device *ibdev, u8 port_num, struct ib_mad *mad)
  126. {
  127. struct ib_event event;
  128. if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
  129. mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
  130. mad->mad_hdr.method == IB_MGMT_METHOD_SET) {
  131. if (mad->mad_hdr.attr_id == IB_SMP_ATTR_PORT_INFO) {
  132. struct ib_port_info *pinfo =
  133. (struct ib_port_info *) ((struct ib_smp *) mad)->data;
  134. update_sm_ah(to_mdev(ibdev), port_num,
  135. be16_to_cpu(pinfo->sm_lid),
  136. pinfo->neighbormtu_mastersmsl & 0xf);
  137. event.device = ibdev;
  138. event.element.port_num = port_num;
  139. if (pinfo->clientrereg_resv_subnetto & 0x80)
  140. event.event = IB_EVENT_CLIENT_REREGISTER;
  141. else
  142. event.event = IB_EVENT_LID_CHANGE;
  143. ib_dispatch_event(&event);
  144. }
  145. if (mad->mad_hdr.attr_id == IB_SMP_ATTR_PKEY_TABLE) {
  146. event.device = ibdev;
  147. event.event = IB_EVENT_PKEY_CHANGE;
  148. event.element.port_num = port_num;
  149. ib_dispatch_event(&event);
  150. }
  151. }
  152. }
  153. static void node_desc_override(struct ib_device *dev,
  154. struct ib_mad *mad)
  155. {
  156. if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
  157. mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
  158. mad->mad_hdr.method == IB_MGMT_METHOD_GET_RESP &&
  159. mad->mad_hdr.attr_id == IB_SMP_ATTR_NODE_DESC) {
  160. spin_lock(&to_mdev(dev)->sm_lock);
  161. memcpy(((struct ib_smp *) mad)->data, dev->node_desc, 64);
  162. spin_unlock(&to_mdev(dev)->sm_lock);
  163. }
  164. }
  165. static void forward_trap(struct mlx4_ib_dev *dev, u8 port_num, struct ib_mad *mad)
  166. {
  167. int qpn = mad->mad_hdr.mgmt_class != IB_MGMT_CLASS_SUBN_LID_ROUTED;
  168. struct ib_mad_send_buf *send_buf;
  169. struct ib_mad_agent *agent = dev->send_agent[port_num - 1][qpn];
  170. int ret;
  171. if (agent) {
  172. send_buf = ib_create_send_mad(agent, qpn, 0, 0, IB_MGMT_MAD_HDR,
  173. IB_MGMT_MAD_DATA, GFP_ATOMIC);
  174. /*
  175. * We rely here on the fact that MLX QPs don't use the
  176. * address handle after the send is posted (this is
  177. * wrong following the IB spec strictly, but we know
  178. * it's OK for our devices).
  179. */
  180. spin_lock(&dev->sm_lock);
  181. memcpy(send_buf->mad, mad, sizeof *mad);
  182. if ((send_buf->ah = dev->sm_ah[port_num - 1]))
  183. ret = ib_post_send_mad(send_buf, NULL);
  184. else
  185. ret = -EINVAL;
  186. spin_unlock(&dev->sm_lock);
  187. if (ret)
  188. ib_free_send_mad(send_buf);
  189. }
  190. }
  191. int mlx4_ib_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
  192. struct ib_wc *in_wc, struct ib_grh *in_grh,
  193. struct ib_mad *in_mad, struct ib_mad *out_mad)
  194. {
  195. u16 slid;
  196. int err;
  197. slid = in_wc ? in_wc->slid : be16_to_cpu(IB_LID_PERMISSIVE);
  198. if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP && slid == 0) {
  199. forward_trap(to_mdev(ibdev), port_num, in_mad);
  200. return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
  201. }
  202. if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
  203. in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
  204. if (in_mad->mad_hdr.method != IB_MGMT_METHOD_GET &&
  205. in_mad->mad_hdr.method != IB_MGMT_METHOD_SET &&
  206. in_mad->mad_hdr.method != IB_MGMT_METHOD_TRAP_REPRESS)
  207. return IB_MAD_RESULT_SUCCESS;
  208. /*
  209. * Don't process SMInfo queries or vendor-specific
  210. * MADs -- the SMA can't handle them.
  211. */
  212. if (in_mad->mad_hdr.attr_id == IB_SMP_ATTR_SM_INFO ||
  213. ((in_mad->mad_hdr.attr_id & IB_SMP_ATTR_VENDOR_MASK) ==
  214. IB_SMP_ATTR_VENDOR_MASK))
  215. return IB_MAD_RESULT_SUCCESS;
  216. } else if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_PERF_MGMT ||
  217. in_mad->mad_hdr.mgmt_class == MLX4_IB_VENDOR_CLASS1 ||
  218. in_mad->mad_hdr.mgmt_class == MLX4_IB_VENDOR_CLASS2 ||
  219. in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_CONG_MGMT) {
  220. if (in_mad->mad_hdr.method != IB_MGMT_METHOD_GET &&
  221. in_mad->mad_hdr.method != IB_MGMT_METHOD_SET)
  222. return IB_MAD_RESULT_SUCCESS;
  223. } else
  224. return IB_MAD_RESULT_SUCCESS;
  225. err = mlx4_MAD_IFC(to_mdev(ibdev),
  226. mad_flags & IB_MAD_IGNORE_MKEY,
  227. mad_flags & IB_MAD_IGNORE_BKEY,
  228. port_num, in_wc, in_grh, in_mad, out_mad);
  229. if (err)
  230. return IB_MAD_RESULT_FAILURE;
  231. if (!out_mad->mad_hdr.status) {
  232. smp_snoop(ibdev, port_num, in_mad);
  233. node_desc_override(ibdev, out_mad);
  234. }
  235. /* set return bit in status of directed route responses */
  236. if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
  237. out_mad->mad_hdr.status |= cpu_to_be16(1 << 15);
  238. if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP_REPRESS)
  239. /* no response for trap repress */
  240. return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
  241. return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
  242. }
  243. static void send_handler(struct ib_mad_agent *agent,
  244. struct ib_mad_send_wc *mad_send_wc)
  245. {
  246. ib_free_send_mad(mad_send_wc->send_buf);
  247. }
  248. int mlx4_ib_mad_init(struct mlx4_ib_dev *dev)
  249. {
  250. struct ib_mad_agent *agent;
  251. int p, q;
  252. int ret;
  253. for (p = 0; p < dev->dev->caps.num_ports; ++p)
  254. for (q = 0; q <= 1; ++q) {
  255. agent = ib_register_mad_agent(&dev->ib_dev, p + 1,
  256. q ? IB_QPT_GSI : IB_QPT_SMI,
  257. NULL, 0, send_handler,
  258. NULL, NULL);
  259. if (IS_ERR(agent)) {
  260. ret = PTR_ERR(agent);
  261. goto err;
  262. }
  263. dev->send_agent[p][q] = agent;
  264. }
  265. return 0;
  266. err:
  267. for (p = 0; p < dev->dev->caps.num_ports; ++p)
  268. for (q = 0; q <= 1; ++q)
  269. if (dev->send_agent[p][q])
  270. ib_unregister_mad_agent(dev->send_agent[p][q]);
  271. return ret;
  272. }
  273. void mlx4_ib_mad_cleanup(struct mlx4_ib_dev *dev)
  274. {
  275. struct ib_mad_agent *agent;
  276. int p, q;
  277. for (p = 0; p < dev->dev->caps.num_ports; ++p) {
  278. for (q = 0; q <= 1; ++q) {
  279. agent = dev->send_agent[p][q];
  280. dev->send_agent[p][q] = NULL;
  281. ib_unregister_mad_agent(agent);
  282. }
  283. if (dev->sm_ah[p])
  284. ib_destroy_ah(dev->sm_ah[p]);
  285. }
  286. }