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.
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:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
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.
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
35 #include <linux/string.h>
36 #include <linux/slab.h>
38 #include <rdma/ib_verbs.h>
39 #include <rdma/ib_mad.h>
40 #include <rdma/ib_smi.h>
42 #include "mthca_dev.h"
43 #include "mthca_cmd.h"
46 MTHCA_VENDOR_CLASS1
= 0x9,
47 MTHCA_VENDOR_CLASS2
= 0xa
50 static int mthca_update_rate(struct mthca_dev
*dev
, u8 port_num
)
52 struct ib_port_attr
*tprops
= NULL
;
55 tprops
= kmalloc(sizeof *tprops
, GFP_KERNEL
);
59 ret
= ib_query_port(&dev
->ib_dev
, port_num
, tprops
);
61 printk(KERN_WARNING
"ib_query_port failed (%d) for %s port %d\n",
62 ret
, dev
->ib_dev
.name
, port_num
);
66 dev
->rate
[port_num
- 1] = tprops
->active_speed
*
67 ib_width_enum_to_int(tprops
->active_width
);
74 static void update_sm_ah(struct mthca_dev
*dev
,
75 u8 port_num
, u16 lid
, u8 sl
)
78 struct rdma_ah_attr ah_attr
;
81 if (!dev
->send_agent
[port_num
- 1][0])
84 memset(&ah_attr
, 0, sizeof ah_attr
);
85 rdma_ah_set_dlid(&ah_attr
, lid
);
86 rdma_ah_set_sl(&ah_attr
, sl
);
87 rdma_ah_set_port_num(&ah_attr
, port_num
);
89 new_ah
= rdma_create_ah(dev
->send_agent
[port_num
- 1][0]->qp
->pd
,
94 spin_lock_irqsave(&dev
->sm_lock
, flags
);
95 if (dev
->sm_ah
[port_num
- 1])
96 rdma_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
);
102 * Snoop SM MADs for port info and P_Key table sets, so we can
103 * synthesize LID change and P_Key change events.
105 static void smp_snoop(struct ib_device
*ibdev
,
107 const struct ib_mad
*mad
,
110 struct ib_event event
;
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
) {
116 struct ib_port_info
*pinfo
=
117 (struct ib_port_info
*) ((struct ib_smp
*) mad
)->data
;
118 u16 lid
= be16_to_cpu(pinfo
->lid
);
120 mthca_update_rate(to_mdev(ibdev
), port_num
);
121 update_sm_ah(to_mdev(ibdev
), port_num
,
122 be16_to_cpu(pinfo
->sm_lid
),
123 pinfo
->neighbormtu_mastersmsl
& 0xf);
125 event
.device
= ibdev
;
126 event
.element
.port_num
= port_num
;
128 if (pinfo
->clientrereg_resv_subnetto
& 0x80) {
129 event
.event
= IB_EVENT_CLIENT_REREGISTER
;
130 ib_dispatch_event(&event
);
133 if (prev_lid
!= lid
) {
134 event
.event
= IB_EVENT_LID_CHANGE
;
135 ib_dispatch_event(&event
);
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
);
148 static void node_desc_override(struct ib_device
*dev
,
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
,
157 IB_DEVICE_NODE_DESC_MAX
);
158 mutex_unlock(&to_mdev(dev
)->cap_mask_mutex
);
162 static void forward_trap(struct mthca_dev
*dev
,
164 const struct ib_mad
*mad
)
166 int qpn
= mad
->mad_hdr
.mgmt_class
!= IB_MGMT_CLASS_SUBN_LID_ROUTED
;
167 struct ib_mad_send_buf
*send_buf
;
168 struct ib_mad_agent
*agent
= dev
->send_agent
[port_num
- 1][qpn
];
173 send_buf
= ib_create_send_mad(agent
, qpn
, 0, 0, IB_MGMT_MAD_HDR
,
174 IB_MGMT_MAD_DATA
, GFP_ATOMIC
,
175 IB_MGMT_BASE_VERSION
);
176 if (IS_ERR(send_buf
))
179 * We rely here on the fact that MLX QPs don't use the
180 * address handle after the send is posted (this is
181 * wrong following the IB spec strictly, but we know
182 * it's OK for our devices).
184 spin_lock_irqsave(&dev
->sm_lock
, flags
);
185 memcpy(send_buf
->mad
, mad
, sizeof *mad
);
186 if ((send_buf
->ah
= dev
->sm_ah
[port_num
- 1]))
187 ret
= ib_post_send_mad(send_buf
, NULL
);
190 spin_unlock_irqrestore(&dev
->sm_lock
, flags
);
193 ib_free_send_mad(send_buf
);
197 int mthca_process_mad(struct ib_device
*ibdev
,
200 const struct ib_wc
*in_wc
,
201 const struct ib_grh
*in_grh
,
202 const struct ib_mad_hdr
*in
, size_t in_mad_size
,
203 struct ib_mad_hdr
*out
, size_t *out_mad_size
,
204 u16
*out_mad_pkey_index
)
207 u16 slid
= in_wc
? in_wc
->slid
: be16_to_cpu(IB_LID_PERMISSIVE
);
209 struct ib_port_attr pattr
;
210 const struct ib_mad
*in_mad
= (const struct ib_mad
*)in
;
211 struct ib_mad
*out_mad
= (struct ib_mad
*)out
;
213 if (WARN_ON_ONCE(in_mad_size
!= sizeof(*in_mad
) ||
214 *out_mad_size
!= sizeof(*out_mad
)))
215 return IB_MAD_RESULT_FAILURE
;
217 /* Forward locally generated traps to the SM */
218 if (in_mad
->mad_hdr
.method
== IB_MGMT_METHOD_TRAP
&&
220 forward_trap(to_mdev(ibdev
), port_num
, in_mad
);
221 return IB_MAD_RESULT_SUCCESS
| IB_MAD_RESULT_CONSUMED
;
225 * Only handle SM gets, sets and trap represses for SM class
227 * Only handle PMA and Mellanox vendor-specific class gets and
228 * sets for other classes.
230 if (in_mad
->mad_hdr
.mgmt_class
== IB_MGMT_CLASS_SUBN_LID_ROUTED
||
231 in_mad
->mad_hdr
.mgmt_class
== IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
) {
232 if (in_mad
->mad_hdr
.method
!= IB_MGMT_METHOD_GET
&&
233 in_mad
->mad_hdr
.method
!= IB_MGMT_METHOD_SET
&&
234 in_mad
->mad_hdr
.method
!= IB_MGMT_METHOD_TRAP_REPRESS
)
235 return IB_MAD_RESULT_SUCCESS
;
238 * Don't process SMInfo queries or vendor-specific
239 * MADs -- the SMA can't handle them.
241 if (in_mad
->mad_hdr
.attr_id
== IB_SMP_ATTR_SM_INFO
||
242 ((in_mad
->mad_hdr
.attr_id
& IB_SMP_ATTR_VENDOR_MASK
) ==
243 IB_SMP_ATTR_VENDOR_MASK
))
244 return IB_MAD_RESULT_SUCCESS
;
245 } else if (in_mad
->mad_hdr
.mgmt_class
== IB_MGMT_CLASS_PERF_MGMT
||
246 in_mad
->mad_hdr
.mgmt_class
== MTHCA_VENDOR_CLASS1
||
247 in_mad
->mad_hdr
.mgmt_class
== MTHCA_VENDOR_CLASS2
) {
248 if (in_mad
->mad_hdr
.method
!= IB_MGMT_METHOD_GET
&&
249 in_mad
->mad_hdr
.method
!= IB_MGMT_METHOD_SET
)
250 return IB_MAD_RESULT_SUCCESS
;
252 return IB_MAD_RESULT_SUCCESS
;
253 if ((in_mad
->mad_hdr
.mgmt_class
== IB_MGMT_CLASS_SUBN_LID_ROUTED
||
254 in_mad
->mad_hdr
.mgmt_class
== IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
) &&
255 in_mad
->mad_hdr
.method
== IB_MGMT_METHOD_SET
&&
256 in_mad
->mad_hdr
.attr_id
== IB_SMP_ATTR_PORT_INFO
&&
257 !ib_query_port(ibdev
, port_num
, &pattr
))
258 prev_lid
= pattr
.lid
;
260 err
= mthca_MAD_IFC(to_mdev(ibdev
),
261 mad_flags
& IB_MAD_IGNORE_MKEY
,
262 mad_flags
& IB_MAD_IGNORE_BKEY
,
263 port_num
, in_wc
, in_grh
, in_mad
, out_mad
);
265 return IB_MAD_RESULT_SUCCESS
;
267 mthca_err(to_mdev(ibdev
), "MAD_IFC returned %d\n", err
);
268 return IB_MAD_RESULT_FAILURE
;
271 if (!out_mad
->mad_hdr
.status
) {
272 smp_snoop(ibdev
, port_num
, in_mad
, prev_lid
);
273 node_desc_override(ibdev
, out_mad
);
276 /* set return bit in status of directed route responses */
277 if (in_mad
->mad_hdr
.mgmt_class
== IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
)
278 out_mad
->mad_hdr
.status
|= cpu_to_be16(1 << 15);
280 if (in_mad
->mad_hdr
.method
== IB_MGMT_METHOD_TRAP_REPRESS
)
281 /* no response for trap repress */
282 return IB_MAD_RESULT_SUCCESS
| IB_MAD_RESULT_CONSUMED
;
284 return IB_MAD_RESULT_SUCCESS
| IB_MAD_RESULT_REPLY
;
287 static void send_handler(struct ib_mad_agent
*agent
,
288 struct ib_mad_send_wc
*mad_send_wc
)
290 ib_free_send_mad(mad_send_wc
->send_buf
);
293 int mthca_create_agents(struct mthca_dev
*dev
)
295 struct ib_mad_agent
*agent
;
299 spin_lock_init(&dev
->sm_lock
);
301 for (p
= 0; p
< dev
->limits
.num_ports
; ++p
)
302 for (q
= 0; q
<= 1; ++q
) {
303 agent
= ib_register_mad_agent(&dev
->ib_dev
, p
+ 1,
304 q
? IB_QPT_GSI
: IB_QPT_SMI
,
305 NULL
, 0, send_handler
,
308 ret
= PTR_ERR(agent
);
311 dev
->send_agent
[p
][q
] = agent
;
315 for (p
= 1; p
<= dev
->limits
.num_ports
; ++p
) {
316 ret
= mthca_update_rate(dev
, p
);
318 mthca_err(dev
, "Failed to obtain port %d rate."
327 for (p
= 0; p
< dev
->limits
.num_ports
; ++p
)
328 for (q
= 0; q
<= 1; ++q
)
329 if (dev
->send_agent
[p
][q
])
330 ib_unregister_mad_agent(dev
->send_agent
[p
][q
]);
335 void mthca_free_agents(struct mthca_dev
*dev
)
337 struct ib_mad_agent
*agent
;
340 for (p
= 0; p
< dev
->limits
.num_ports
; ++p
) {
341 for (q
= 0; q
<= 1; ++q
) {
342 agent
= dev
->send_agent
[p
][q
];
343 dev
->send_agent
[p
][q
] = NULL
;
344 ib_unregister_mad_agent(agent
);
348 rdma_destroy_ah(dev
->sm_ah
[p
]);