mad.c 12 KB

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