blob: 8561b297a19b4162f8a1b90565327e8585f2c34f [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.
33 *
34 * $Id: mthca_mad.c 1349 2004-12-16 21:09:43Z roland $
35 */
36
Roland Dreiera4d61e82005-08-25 13:40:04 -070037#include <rdma/ib_verbs.h>
38#include <rdma/ib_mad.h>
39#include <rdma/ib_smi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#include "mthca_dev.h"
42#include "mthca_cmd.h"
43
44enum {
45 MTHCA_VENDOR_CLASS1 = 0x9,
46 MTHCA_VENDOR_CLASS2 = 0xa
47};
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049static void update_sm_ah(struct mthca_dev *dev,
50 u8 port_num, u16 lid, u8 sl)
51{
52 struct ib_ah *new_ah;
53 struct ib_ah_attr ah_attr;
54 unsigned long flags;
55
56 if (!dev->send_agent[port_num - 1][0])
57 return;
58
59 memset(&ah_attr, 0, sizeof ah_attr);
60 ah_attr.dlid = lid;
61 ah_attr.sl = sl;
62 ah_attr.port_num = port_num;
63
64 new_ah = ib_create_ah(dev->send_agent[port_num - 1][0]->qp->pd,
65 &ah_attr);
66 if (IS_ERR(new_ah))
67 return;
68
69 spin_lock_irqsave(&dev->sm_lock, flags);
70 if (dev->sm_ah[port_num - 1])
71 ib_destroy_ah(dev->sm_ah[port_num - 1]);
72 dev->sm_ah[port_num - 1] = new_ah;
73 spin_unlock_irqrestore(&dev->sm_lock, flags);
74}
75
76/*
77 * Snoop SM MADs for port info and P_Key table sets, so we can
78 * synthesize LID change and P_Key change events.
79 */
80static void smp_snoop(struct ib_device *ibdev,
81 u8 port_num,
82 struct ib_mad *mad)
83{
84 struct ib_event event;
85
86 if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
87 mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
88 mad->mad_hdr.method == IB_MGMT_METHOD_SET) {
89 if (mad->mad_hdr.attr_id == IB_SMP_ATTR_PORT_INFO) {
90 update_sm_ah(to_mdev(ibdev), port_num,
91 be16_to_cpup((__be16 *) (mad->data + 58)),
92 (*(u8 *) (mad->data + 76)) & 0xf);
93
94 event.device = ibdev;
95 event.event = IB_EVENT_LID_CHANGE;
96 event.element.port_num = port_num;
97 ib_dispatch_event(&event);
98 }
99
100 if (mad->mad_hdr.attr_id == IB_SMP_ATTR_PKEY_TABLE) {
101 event.device = ibdev;
102 event.event = IB_EVENT_PKEY_CHANGE;
103 event.element.port_num = port_num;
104 ib_dispatch_event(&event);
105 }
106 }
107}
108
109static void forward_trap(struct mthca_dev *dev,
110 u8 port_num,
111 struct ib_mad *mad)
112{
113 int qpn = mad->mad_hdr.mgmt_class != IB_MGMT_CLASS_SUBN_LID_ROUTED;
Sean Hefty34816ad2005-10-25 10:51:39 -0700114 struct ib_mad_send_buf *send_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 struct ib_mad_agent *agent = dev->send_agent[port_num - 1][qpn];
116 int ret;
117 unsigned long flags;
118
119 if (agent) {
Sean Hefty34816ad2005-10-25 10:51:39 -0700120 send_buf = ib_create_send_mad(agent, qpn, 0, 0, IB_MGMT_MAD_HDR,
121 IB_MGMT_MAD_DATA, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 /*
123 * We rely here on the fact that MLX QPs don't use the
124 * address handle after the send is posted (this is
125 * wrong following the IB spec strictly, but we know
126 * it's OK for our devices).
127 */
128 spin_lock_irqsave(&dev->sm_lock, flags);
Sean Hefty34816ad2005-10-25 10:51:39 -0700129 memcpy(send_buf->mad, mad, sizeof *mad);
130 if ((send_buf->ah = dev->sm_ah[port_num - 1]))
131 ret = ib_post_send_mad(send_buf, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 else
133 ret = -EINVAL;
134 spin_unlock_irqrestore(&dev->sm_lock, flags);
135
Sean Hefty34816ad2005-10-25 10:51:39 -0700136 if (ret)
137 ib_free_send_mad(send_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 }
139}
140
141int mthca_process_mad(struct ib_device *ibdev,
142 int mad_flags,
143 u8 port_num,
144 struct ib_wc *in_wc,
145 struct ib_grh *in_grh,
146 struct ib_mad *in_mad,
147 struct ib_mad *out_mad)
148{
149 int err;
150 u8 status;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700151 u16 slid = in_wc ? in_wc->slid : be16_to_cpu(IB_LID_PERMISSIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 /* Forward locally generated traps to the SM */
154 if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP &&
155 slid == 0) {
156 forward_trap(to_mdev(ibdev), port_num, in_mad);
157 return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
158 }
159
160 /*
161 * Only handle SM gets, sets and trap represses for SM class
162 *
163 * Only handle PMA and Mellanox vendor-specific class gets and
164 * sets for other classes.
165 */
166 if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
167 in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
168 if (in_mad->mad_hdr.method != IB_MGMT_METHOD_GET &&
169 in_mad->mad_hdr.method != IB_MGMT_METHOD_SET &&
170 in_mad->mad_hdr.method != IB_MGMT_METHOD_TRAP_REPRESS)
171 return IB_MAD_RESULT_SUCCESS;
172
173 /*
174 * Don't process SMInfo queries or vendor-specific
175 * MADs -- the SMA can't handle them.
176 */
177 if (in_mad->mad_hdr.attr_id == IB_SMP_ATTR_SM_INFO ||
178 ((in_mad->mad_hdr.attr_id & IB_SMP_ATTR_VENDOR_MASK) ==
179 IB_SMP_ATTR_VENDOR_MASK))
180 return IB_MAD_RESULT_SUCCESS;
181 } else if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_PERF_MGMT ||
182 in_mad->mad_hdr.mgmt_class == MTHCA_VENDOR_CLASS1 ||
183 in_mad->mad_hdr.mgmt_class == MTHCA_VENDOR_CLASS2) {
184 if (in_mad->mad_hdr.method != IB_MGMT_METHOD_GET &&
185 in_mad->mad_hdr.method != IB_MGMT_METHOD_SET)
186 return IB_MAD_RESULT_SUCCESS;
187 } else
188 return IB_MAD_RESULT_SUCCESS;
189
190 err = mthca_MAD_IFC(to_mdev(ibdev),
191 mad_flags & IB_MAD_IGNORE_MKEY,
192 mad_flags & IB_MAD_IGNORE_BKEY,
193 port_num, in_wc, in_grh, in_mad, out_mad,
194 &status);
195 if (err) {
196 mthca_err(to_mdev(ibdev), "MAD_IFC failed\n");
197 return IB_MAD_RESULT_FAILURE;
198 }
199 if (status == MTHCA_CMD_STAT_BAD_PKT)
200 return IB_MAD_RESULT_SUCCESS;
201 if (status) {
202 mthca_err(to_mdev(ibdev), "MAD_IFC returned status %02x\n",
203 status);
204 return IB_MAD_RESULT_FAILURE;
205 }
206
207 if (!out_mad->mad_hdr.status)
208 smp_snoop(ibdev, port_num, in_mad);
209
210 /* set return bit in status of directed route responses */
211 if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
212 out_mad->mad_hdr.status |= cpu_to_be16(1 << 15);
213
214 if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP_REPRESS)
215 /* no response for trap repress */
216 return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
217
218 return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
219}
220
221static void send_handler(struct ib_mad_agent *agent,
222 struct ib_mad_send_wc *mad_send_wc)
223{
Sean Hefty34816ad2005-10-25 10:51:39 -0700224 ib_free_send_mad(mad_send_wc->send_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
227int mthca_create_agents(struct mthca_dev *dev)
228{
229 struct ib_mad_agent *agent;
230 int p, q;
231
232 spin_lock_init(&dev->sm_lock);
233
234 for (p = 0; p < dev->limits.num_ports; ++p)
235 for (q = 0; q <= 1; ++q) {
236 agent = ib_register_mad_agent(&dev->ib_dev, p + 1,
237 q ? IB_QPT_GSI : IB_QPT_SMI,
238 NULL, 0, send_handler,
239 NULL, NULL);
240 if (IS_ERR(agent))
241 goto err;
242 dev->send_agent[p][q] = agent;
243 }
244
245 return 0;
246
247err:
248 for (p = 0; p < dev->limits.num_ports; ++p)
249 for (q = 0; q <= 1; ++q)
250 if (dev->send_agent[p][q])
251 ib_unregister_mad_agent(dev->send_agent[p][q]);
252
253 return PTR_ERR(agent);
254}
255
256void mthca_free_agents(struct mthca_dev *dev)
257{
258 struct ib_mad_agent *agent;
259 int p, q;
260
261 for (p = 0; p < dev->limits.num_ports; ++p) {
262 for (q = 0; q <= 1; ++q) {
263 agent = dev->send_agent[p][q];
264 dev->send_agent[p][q] = NULL;
265 ib_unregister_mad_agent(agent);
266 }
267
268 if (dev->sm_ah[p])
269 ib_destroy_ah(dev->sm_ah[p]);
270 }
271}