mthca_mad.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * Copyright (c) 2004 Topspin Communications. All rights reserved.
  3. * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
  4. * Copyright (c) 2004 Voltaire, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #include <linux/string.h>
  35. #include <linux/slab.h>
  36. #include <rdma/ib_verbs.h>
  37. #include <rdma/ib_mad.h>
  38. #include <rdma/ib_smi.h>
  39. #include "mthca_dev.h"
  40. #include "mthca_cmd.h"
  41. enum {
  42. MTHCA_VENDOR_CLASS1 = 0x9,
  43. MTHCA_VENDOR_CLASS2 = 0xa
  44. };
  45. static int mthca_update_rate(struct mthca_dev *dev, u8 port_num)
  46. {
  47. struct ib_port_attr *tprops = NULL;
  48. int ret;
  49. tprops = kmalloc(sizeof *tprops, GFP_KERNEL);
  50. if (!tprops)
  51. return -ENOMEM;
  52. ret = ib_query_port(&dev->ib_dev, port_num, tprops);
  53. if (ret) {
  54. dev_warn(&dev->ib_dev.dev,
  55. "ib_query_port failed (%d) forport %d\n", ret,
  56. port_num);
  57. goto out;
  58. }
  59. dev->rate[port_num - 1] = tprops->active_speed *
  60. ib_width_enum_to_int(tprops->active_width);
  61. out:
  62. kfree(tprops);
  63. return ret;
  64. }
  65. static void update_sm_ah(struct mthca_dev *dev,
  66. u8 port_num, u16 lid, u8 sl)
  67. {
  68. struct ib_ah *new_ah;
  69. struct rdma_ah_attr ah_attr;
  70. unsigned long flags;
  71. if (!dev->send_agent[port_num - 1][0])
  72. return;
  73. memset(&ah_attr, 0, sizeof ah_attr);
  74. ah_attr.type = rdma_ah_find_type(&dev->ib_dev, port_num);
  75. rdma_ah_set_dlid(&ah_attr, lid);
  76. rdma_ah_set_sl(&ah_attr, sl);
  77. rdma_ah_set_port_num(&ah_attr, port_num);
  78. new_ah = rdma_create_ah(dev->send_agent[port_num - 1][0]->qp->pd,
  79. &ah_attr, 0);
  80. if (IS_ERR(new_ah))
  81. return;
  82. spin_lock_irqsave(&dev->sm_lock, flags);
  83. if (dev->sm_ah[port_num - 1])
  84. rdma_destroy_ah(dev->sm_ah[port_num - 1], 0);
  85. dev->sm_ah[port_num - 1] = new_ah;
  86. spin_unlock_irqrestore(&dev->sm_lock, flags);
  87. }
  88. /*
  89. * Snoop SM MADs for port info and P_Key table sets, so we can
  90. * synthesize LID change and P_Key change events.
  91. */
  92. static void smp_snoop(struct ib_device *ibdev,
  93. u8 port_num,
  94. const struct ib_mad *mad,
  95. u16 prev_lid)
  96. {
  97. struct ib_event event;
  98. if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
  99. mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
  100. mad->mad_hdr.method == IB_MGMT_METHOD_SET) {
  101. if (mad->mad_hdr.attr_id == IB_SMP_ATTR_PORT_INFO) {
  102. struct ib_port_info *pinfo =
  103. (struct ib_port_info *) ((struct ib_smp *) mad)->data;
  104. u16 lid = be16_to_cpu(pinfo->lid);
  105. mthca_update_rate(to_mdev(ibdev), port_num);
  106. update_sm_ah(to_mdev(ibdev), port_num,
  107. be16_to_cpu(pinfo->sm_lid),
  108. pinfo->neighbormtu_mastersmsl & 0xf);
  109. event.device = ibdev;
  110. event.element.port_num = port_num;
  111. if (pinfo->clientrereg_resv_subnetto & 0x80) {
  112. event.event = IB_EVENT_CLIENT_REREGISTER;
  113. ib_dispatch_event(&event);
  114. }
  115. if (prev_lid != lid) {
  116. event.event = IB_EVENT_LID_CHANGE;
  117. ib_dispatch_event(&event);
  118. }
  119. }
  120. if (mad->mad_hdr.attr_id == IB_SMP_ATTR_PKEY_TABLE) {
  121. event.device = ibdev;
  122. event.event = IB_EVENT_PKEY_CHANGE;
  123. event.element.port_num = port_num;
  124. ib_dispatch_event(&event);
  125. }
  126. }
  127. }
  128. static void node_desc_override(struct ib_device *dev,
  129. struct ib_mad *mad)
  130. {
  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_GET_RESP &&
  134. mad->mad_hdr.attr_id == IB_SMP_ATTR_NODE_DESC) {
  135. mutex_lock(&to_mdev(dev)->cap_mask_mutex);
  136. memcpy(((struct ib_smp *) mad)->data, dev->node_desc,
  137. IB_DEVICE_NODE_DESC_MAX);
  138. mutex_unlock(&to_mdev(dev)->cap_mask_mutex);
  139. }
  140. }
  141. static void forward_trap(struct mthca_dev *dev,
  142. u32 port_num,
  143. const struct ib_mad *mad)
  144. {
  145. int qpn = mad->mad_hdr.mgmt_class != IB_MGMT_CLASS_SUBN_LID_ROUTED;
  146. struct ib_mad_send_buf *send_buf;
  147. struct ib_mad_agent *agent = dev->send_agent[port_num - 1][qpn];
  148. int ret;
  149. unsigned long flags;
  150. if (agent) {
  151. send_buf = ib_create_send_mad(agent, qpn, 0, 0, IB_MGMT_MAD_HDR,
  152. IB_MGMT_MAD_DATA, GFP_ATOMIC,
  153. IB_MGMT_BASE_VERSION);
  154. if (IS_ERR(send_buf))
  155. return;
  156. /*
  157. * We rely here on the fact that MLX QPs don't use the
  158. * address handle after the send is posted (this is
  159. * wrong following the IB spec strictly, but we know
  160. * it's OK for our devices).
  161. */
  162. spin_lock_irqsave(&dev->sm_lock, flags);
  163. memcpy(send_buf->mad, mad, sizeof *mad);
  164. if ((send_buf->ah = dev->sm_ah[port_num - 1]))
  165. ret = ib_post_send_mad(send_buf, NULL);
  166. else
  167. ret = -EINVAL;
  168. spin_unlock_irqrestore(&dev->sm_lock, flags);
  169. if (ret)
  170. ib_free_send_mad(send_buf);
  171. }
  172. }
  173. int mthca_process_mad(struct ib_device *ibdev, int mad_flags, u32 port_num,
  174. const struct ib_wc *in_wc, const struct ib_grh *in_grh,
  175. const struct ib_mad *in, struct ib_mad *out,
  176. size_t *out_mad_size, u16 *out_mad_pkey_index)
  177. {
  178. int err;
  179. u16 slid = in_wc ? ib_lid_cpu16(in_wc->slid) : be16_to_cpu(IB_LID_PERMISSIVE);
  180. u16 prev_lid = 0;
  181. struct ib_port_attr pattr;
  182. /* Forward locally generated traps to the SM */
  183. if (in->mad_hdr.method == IB_MGMT_METHOD_TRAP && !slid) {
  184. forward_trap(to_mdev(ibdev), port_num, in);
  185. return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
  186. }
  187. /*
  188. * Only handle SM gets, sets and trap represses for SM class
  189. *
  190. * Only handle PMA and Mellanox vendor-specific class gets and
  191. * sets for other classes.
  192. */
  193. if (in->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
  194. in->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
  195. if (in->mad_hdr.method != IB_MGMT_METHOD_GET &&
  196. in->mad_hdr.method != IB_MGMT_METHOD_SET &&
  197. in->mad_hdr.method != IB_MGMT_METHOD_TRAP_REPRESS)
  198. return IB_MAD_RESULT_SUCCESS;
  199. /*
  200. * Don't process SMInfo queries or vendor-specific
  201. * MADs -- the SMA can't handle them.
  202. */
  203. if (in->mad_hdr.attr_id == IB_SMP_ATTR_SM_INFO ||
  204. ((in->mad_hdr.attr_id & IB_SMP_ATTR_VENDOR_MASK) ==
  205. IB_SMP_ATTR_VENDOR_MASK))
  206. return IB_MAD_RESULT_SUCCESS;
  207. } else if (in->mad_hdr.mgmt_class == IB_MGMT_CLASS_PERF_MGMT ||
  208. in->mad_hdr.mgmt_class == MTHCA_VENDOR_CLASS1 ||
  209. in->mad_hdr.mgmt_class == MTHCA_VENDOR_CLASS2) {
  210. if (in->mad_hdr.method != IB_MGMT_METHOD_GET &&
  211. in->mad_hdr.method != IB_MGMT_METHOD_SET)
  212. return IB_MAD_RESULT_SUCCESS;
  213. } else
  214. return IB_MAD_RESULT_SUCCESS;
  215. if ((in->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
  216. in->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
  217. in->mad_hdr.method == IB_MGMT_METHOD_SET &&
  218. in->mad_hdr.attr_id == IB_SMP_ATTR_PORT_INFO &&
  219. !ib_query_port(ibdev, port_num, &pattr))
  220. prev_lid = ib_lid_cpu16(pattr.lid);
  221. err = mthca_MAD_IFC(to_mdev(ibdev), mad_flags & IB_MAD_IGNORE_MKEY,
  222. mad_flags & IB_MAD_IGNORE_BKEY, port_num, in_wc,
  223. in_grh, in, out);
  224. if (err == -EBADMSG)
  225. return IB_MAD_RESULT_SUCCESS;
  226. else if (err) {
  227. mthca_err(to_mdev(ibdev), "MAD_IFC returned %d\n", err);
  228. return IB_MAD_RESULT_FAILURE;
  229. }
  230. if (!out->mad_hdr.status) {
  231. smp_snoop(ibdev, port_num, in, prev_lid);
  232. node_desc_override(ibdev, out);
  233. }
  234. /* set return bit in status of directed route responses */
  235. if (in->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
  236. out->mad_hdr.status |= cpu_to_be16(1 << 15);
  237. if (in->mad_hdr.method == IB_MGMT_METHOD_TRAP_REPRESS)
  238. /* no response for trap repress */
  239. return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
  240. return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
  241. }
  242. static void send_handler(struct ib_mad_agent *agent,
  243. struct ib_mad_send_wc *mad_send_wc)
  244. {
  245. ib_free_send_mad(mad_send_wc->send_buf);
  246. }
  247. int mthca_create_agents(struct mthca_dev *dev)
  248. {
  249. struct ib_mad_agent *agent;
  250. int p, q;
  251. int ret;
  252. spin_lock_init(&dev->sm_lock);
  253. for (p = 0; p < dev->limits.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, 0);
  259. if (IS_ERR(agent)) {
  260. ret = PTR_ERR(agent);
  261. goto err;
  262. }
  263. dev->send_agent[p][q] = agent;
  264. }
  265. for (p = 1; p <= dev->limits.num_ports; ++p) {
  266. ret = mthca_update_rate(dev, p);
  267. if (ret) {
  268. mthca_err(dev, "Failed to obtain port %d rate."
  269. " aborting.\n", p);
  270. goto err;
  271. }
  272. }
  273. return 0;
  274. err:
  275. for (p = 0; p < dev->limits.num_ports; ++p)
  276. for (q = 0; q <= 1; ++q)
  277. if (dev->send_agent[p][q])
  278. ib_unregister_mad_agent(dev->send_agent[p][q]);
  279. return ret;
  280. }
  281. void mthca_free_agents(struct mthca_dev *dev)
  282. {
  283. struct ib_mad_agent *agent;
  284. int p, q;
  285. for (p = 0; p < dev->limits.num_ports; ++p) {
  286. for (q = 0; q <= 1; ++q) {
  287. agent = dev->send_agent[p][q];
  288. dev->send_agent[p][q] = NULL;
  289. ib_unregister_mad_agent(agent);
  290. }
  291. if (dev->sm_ah[p])
  292. rdma_destroy_ah(dev->sm_ah[p],
  293. RDMA_DESTROY_AH_SLEEPABLE);
  294. }
  295. }