blob: 7c3f2fb44ba51d8f288221df7b4d316a56adab28 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) 2004 Topspin Communications. All rights reserved.
Roland Dreier2a1d9b72005-08-10 23:03:10 -07003 * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
4 * Copyright (c) 2004 Voltaire, Inc. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 */
34
Tim Schmielau4e57b682005-10-30 15:03:48 -080035#include <linux/string.h>
36#include <linux/slab.h>
37
Roland Dreiera4d61e82005-08-25 13:40:04 -070038#include <rdma/ib_verbs.h>
39#include <rdma/ib_mad.h>
40#include <rdma/ib_smi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#include "mthca_dev.h"
43#include "mthca_cmd.h"
44
45enum {
46 MTHCA_VENDOR_CLASS1 = 0x9,
47 MTHCA_VENDOR_CLASS2 = 0xa
48};
49
Adrian Bunk415dcd92006-04-19 00:15:35 +020050static int mthca_update_rate(struct mthca_dev *dev, u8 port_num)
Jack Morgensteinbf6a9e32006-04-10 09:43:47 -070051{
52 struct ib_port_attr *tprops = NULL;
53 int ret;
54
55 tprops = kmalloc(sizeof *tprops, GFP_KERNEL);
56 if (!tprops)
57 return -ENOMEM;
58
59 ret = ib_query_port(&dev->ib_dev, port_num, tprops);
60 if (ret) {
61 printk(KERN_WARNING "ib_query_port failed (%d) for %s port %d\n",
62 ret, dev->ib_dev.name, port_num);
63 goto out;
64 }
65
66 dev->rate[port_num - 1] = tprops->active_speed *
67 ib_width_enum_to_int(tprops->active_width);
68
69out:
70 kfree(tprops);
71 return ret;
72}
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074static void update_sm_ah(struct mthca_dev *dev,
75 u8 port_num, u16 lid, u8 sl)
76{
77 struct ib_ah *new_ah;
78 struct ib_ah_attr ah_attr;
79 unsigned long flags;
80
81 if (!dev->send_agent[port_num - 1][0])
82 return;
83
84 memset(&ah_attr, 0, sizeof ah_attr);
85 ah_attr.dlid = lid;
86 ah_attr.sl = sl;
87 ah_attr.port_num = port_num;
88
89 new_ah = ib_create_ah(dev->send_agent[port_num - 1][0]->qp->pd,
90 &ah_attr);
91 if (IS_ERR(new_ah))
92 return;
93
94 spin_lock_irqsave(&dev->sm_lock, flags);
95 if (dev->sm_ah[port_num - 1])
96 ib_destroy_ah(dev->sm_ah[port_num - 1]);
97 dev->sm_ah[port_num - 1] = new_ah;
98 spin_unlock_irqrestore(&dev->sm_lock, flags);
99}
100
101/*
102 * Snoop SM MADs for port info and P_Key table sets, so we can
103 * synthesize LID change and P_Key change events.
104 */
105static void smp_snoop(struct ib_device *ibdev,
106 u8 port_num,
Ira Weinya97e2d82015-05-31 17:15:30 -0400107 const struct ib_mad *mad,
Moni Shoua270b8b82009-01-28 15:15:56 -0800108 u16 prev_lid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
110 struct ib_event event;
111
112 if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
113 mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
114 mad->mad_hdr.method == IB_MGMT_METHOD_SET) {
115 if (mad->mad_hdr.attr_id == IB_SMP_ATTR_PORT_INFO) {
Leonid Arsh12bbb2b2006-06-17 20:37:36 -0700116 struct ib_port_info *pinfo =
117 (struct ib_port_info *) ((struct ib_smp *) mad)->data;
Moni Shoua270b8b82009-01-28 15:15:56 -0800118 u16 lid = be16_to_cpu(pinfo->lid);
Leonid Arsh12bbb2b2006-06-17 20:37:36 -0700119
Jack Morgensteinbf6a9e32006-04-10 09:43:47 -0700120 mthca_update_rate(to_mdev(ibdev), port_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 update_sm_ah(to_mdev(ibdev), port_num,
Jack Morgensteinb2707572006-09-19 11:13:24 +0300122 be16_to_cpu(pinfo->sm_lid),
Leonid Arsh12bbb2b2006-06-17 20:37:36 -0700123 pinfo->neighbormtu_mastersmsl & 0xf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 event.device = ibdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 event.element.port_num = port_num;
Leonid Arsh12bbb2b2006-06-17 20:37:36 -0700127
Moni Shoua270b8b82009-01-28 15:15:56 -0800128 if (pinfo->clientrereg_resv_subnetto & 0x80) {
Leonid Arsh12bbb2b2006-06-17 20:37:36 -0700129 event.event = IB_EVENT_CLIENT_REREGISTER;
Moni Shoua270b8b82009-01-28 15:15:56 -0800130 ib_dispatch_event(&event);
131 }
Leonid Arsh12bbb2b2006-06-17 20:37:36 -0700132
Moni Shoua270b8b82009-01-28 15:15:56 -0800133 if (prev_lid != lid) {
134 event.event = IB_EVENT_LID_CHANGE;
135 ib_dispatch_event(&event);
136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 }
138
139 if (mad->mad_hdr.attr_id == IB_SMP_ATTR_PKEY_TABLE) {
140 event.device = ibdev;
141 event.event = IB_EVENT_PKEY_CHANGE;
142 event.element.port_num = port_num;
143 ib_dispatch_event(&event);
144 }
145 }
146}
147
Roland Dreier6dfc3902006-02-02 10:04:19 -0800148static void node_desc_override(struct ib_device *dev,
149 struct ib_mad *mad)
150{
151 if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
152 mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
153 mad->mad_hdr.method == IB_MGMT_METHOD_GET_RESP &&
154 mad->mad_hdr.attr_id == IB_SMP_ATTR_NODE_DESC) {
155 mutex_lock(&to_mdev(dev)->cap_mask_mutex);
156 memcpy(((struct ib_smp *) mad)->data, dev->node_desc, 64);
157 mutex_unlock(&to_mdev(dev)->cap_mask_mutex);
158 }
159}
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161static void forward_trap(struct mthca_dev *dev,
162 u8 port_num,
Ira Weinya97e2d82015-05-31 17:15:30 -0400163 const struct ib_mad *mad)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
165 int qpn = mad->mad_hdr.mgmt_class != IB_MGMT_CLASS_SUBN_LID_ROUTED;
Sean Hefty34816ad2005-10-25 10:51:39 -0700166 struct ib_mad_send_buf *send_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 struct ib_mad_agent *agent = dev->send_agent[port_num - 1][qpn];
168 int ret;
169 unsigned long flags;
170
171 if (agent) {
Sean Hefty34816ad2005-10-25 10:51:39 -0700172 send_buf = ib_create_send_mad(agent, qpn, 0, 0, IB_MGMT_MAD_HDR,
Ira Weinyda2dfaa2015-06-06 14:38:28 -0400173 IB_MGMT_MAD_DATA, GFP_ATOMIC,
174 IB_MGMT_BASE_VERSION);
Dan Carpenterd0444f12011-01-10 17:42:10 -0800175 if (IS_ERR(send_buf))
176 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 /*
178 * We rely here on the fact that MLX QPs don't use the
179 * address handle after the send is posted (this is
180 * wrong following the IB spec strictly, but we know
181 * it's OK for our devices).
182 */
183 spin_lock_irqsave(&dev->sm_lock, flags);
Sean Hefty34816ad2005-10-25 10:51:39 -0700184 memcpy(send_buf->mad, mad, sizeof *mad);
185 if ((send_buf->ah = dev->sm_ah[port_num - 1]))
186 ret = ib_post_send_mad(send_buf, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 else
188 ret = -EINVAL;
189 spin_unlock_irqrestore(&dev->sm_lock, flags);
190
Sean Hefty34816ad2005-10-25 10:51:39 -0700191 if (ret)
192 ib_free_send_mad(send_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 }
194}
195
196int mthca_process_mad(struct ib_device *ibdev,
197 int mad_flags,
198 u8 port_num,
Ira Weinya97e2d82015-05-31 17:15:30 -0400199 const struct ib_wc *in_wc,
200 const struct ib_grh *in_grh,
Ira Weiny4cd7c942015-06-06 14:38:31 -0400201 const struct ib_mad_hdr *in, size_t in_mad_size,
202 struct ib_mad_hdr *out, size_t *out_mad_size,
203 u16 *out_mad_pkey_index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 int err;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700206 u16 slid = in_wc ? in_wc->slid : be16_to_cpu(IB_LID_PERMISSIVE);
Moni Shoua270b8b82009-01-28 15:15:56 -0800207 u16 prev_lid = 0;
208 struct ib_port_attr pattr;
Ira Weiny4cd7c942015-06-06 14:38:31 -0400209 const struct ib_mad *in_mad = (const struct ib_mad *)in;
210 struct ib_mad *out_mad = (struct ib_mad *)out;
211
Ira Weiny3b8ab702015-06-25 09:52:50 -0400212 if (WARN_ON_ONCE(in_mad_size != sizeof(*in_mad) ||
213 *out_mad_size != sizeof(*out_mad)))
214 return IB_MAD_RESULT_FAILURE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 /* Forward locally generated traps to the SM */
217 if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP &&
218 slid == 0) {
219 forward_trap(to_mdev(ibdev), port_num, in_mad);
220 return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
221 }
222
223 /*
224 * Only handle SM gets, sets and trap represses for SM class
225 *
226 * Only handle PMA and Mellanox vendor-specific class gets and
227 * sets for other classes.
228 */
229 if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
230 in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
231 if (in_mad->mad_hdr.method != IB_MGMT_METHOD_GET &&
232 in_mad->mad_hdr.method != IB_MGMT_METHOD_SET &&
233 in_mad->mad_hdr.method != IB_MGMT_METHOD_TRAP_REPRESS)
234 return IB_MAD_RESULT_SUCCESS;
235
236 /*
237 * Don't process SMInfo queries or vendor-specific
238 * MADs -- the SMA can't handle them.
239 */
240 if (in_mad->mad_hdr.attr_id == IB_SMP_ATTR_SM_INFO ||
241 ((in_mad->mad_hdr.attr_id & IB_SMP_ATTR_VENDOR_MASK) ==
242 IB_SMP_ATTR_VENDOR_MASK))
243 return IB_MAD_RESULT_SUCCESS;
244 } else if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_PERF_MGMT ||
245 in_mad->mad_hdr.mgmt_class == MTHCA_VENDOR_CLASS1 ||
246 in_mad->mad_hdr.mgmt_class == MTHCA_VENDOR_CLASS2) {
247 if (in_mad->mad_hdr.method != IB_MGMT_METHOD_GET &&
248 in_mad->mad_hdr.method != IB_MGMT_METHOD_SET)
249 return IB_MAD_RESULT_SUCCESS;
250 } else
251 return IB_MAD_RESULT_SUCCESS;
Moni Shoua270b8b82009-01-28 15:15:56 -0800252 if ((in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
253 in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
254 in_mad->mad_hdr.method == IB_MGMT_METHOD_SET &&
255 in_mad->mad_hdr.attr_id == IB_SMP_ATTR_PORT_INFO &&
256 !ib_query_port(ibdev, port_num, &pattr))
257 prev_lid = pattr.lid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 err = mthca_MAD_IFC(to_mdev(ibdev),
260 mad_flags & IB_MAD_IGNORE_MKEY,
261 mad_flags & IB_MAD_IGNORE_BKEY,
Goldwyn Rodriguescdb73db2011-07-07 17:20:40 +0000262 port_num, in_wc, in_grh, in_mad, out_mad);
263 if (err == -EBADMSG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 return IB_MAD_RESULT_SUCCESS;
Goldwyn Rodriguescdb73db2011-07-07 17:20:40 +0000265 else if (err) {
266 mthca_err(to_mdev(ibdev), "MAD_IFC returned %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 return IB_MAD_RESULT_FAILURE;
268 }
269
Roland Dreier6dfc3902006-02-02 10:04:19 -0800270 if (!out_mad->mad_hdr.status) {
Moni Shoua270b8b82009-01-28 15:15:56 -0800271 smp_snoop(ibdev, port_num, in_mad, prev_lid);
Roland Dreier6dfc3902006-02-02 10:04:19 -0800272 node_desc_override(ibdev, out_mad);
273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 /* set return bit in status of directed route responses */
276 if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
277 out_mad->mad_hdr.status |= cpu_to_be16(1 << 15);
278
279 if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP_REPRESS)
280 /* no response for trap repress */
281 return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
282
283 return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
284}
285
286static void send_handler(struct ib_mad_agent *agent,
287 struct ib_mad_send_wc *mad_send_wc)
288{
Sean Hefty34816ad2005-10-25 10:51:39 -0700289 ib_free_send_mad(mad_send_wc->send_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
292int mthca_create_agents(struct mthca_dev *dev)
293{
294 struct ib_mad_agent *agent;
295 int p, q;
Jack Morgensteinbf6a9e32006-04-10 09:43:47 -0700296 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 spin_lock_init(&dev->sm_lock);
299
300 for (p = 0; p < dev->limits.num_ports; ++p)
301 for (q = 0; q <= 1; ++q) {
302 agent = ib_register_mad_agent(&dev->ib_dev, p + 1,
303 q ? IB_QPT_GSI : IB_QPT_SMI,
304 NULL, 0, send_handler,
Ira Weiny0f29b462014-08-08 19:00:55 -0400305 NULL, NULL, 0);
Jack Morgensteinbf6a9e32006-04-10 09:43:47 -0700306 if (IS_ERR(agent)) {
307 ret = PTR_ERR(agent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 goto err;
Jack Morgensteinbf6a9e32006-04-10 09:43:47 -0700309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 dev->send_agent[p][q] = agent;
311 }
312
Jack Morgensteinbf6a9e32006-04-10 09:43:47 -0700313
314 for (p = 1; p <= dev->limits.num_ports; ++p) {
315 ret = mthca_update_rate(dev, p);
316 if (ret) {
317 mthca_err(dev, "Failed to obtain port %d rate."
318 " aborting.\n", p);
319 goto err;
320 }
321 }
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 return 0;
324
325err:
326 for (p = 0; p < dev->limits.num_ports; ++p)
327 for (q = 0; q <= 1; ++q)
328 if (dev->send_agent[p][q])
329 ib_unregister_mad_agent(dev->send_agent[p][q]);
330
Jack Morgensteinbf6a9e32006-04-10 09:43:47 -0700331 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
Roland Dreierf4f3d0f2006-11-29 15:33:06 -0800334void mthca_free_agents(struct mthca_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
336 struct ib_mad_agent *agent;
337 int p, q;
338
339 for (p = 0; p < dev->limits.num_ports; ++p) {
340 for (q = 0; q <= 1; ++q) {
341 agent = dev->send_agent[p][q];
342 dev->send_agent[p][q] = NULL;
343 ib_unregister_mad_agent(agent);
344 }
345
346 if (dev->sm_ah[p])
347 ib_destroy_ah(dev->sm_ah[p]);
348 }
349}