mad.c 10 KB

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