Linux Audio

Check our new training course

Loading...
v5.14.15
  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
 35#include <linux/string.h>
 36#include <linux/slab.h>
 37
 38#include <rdma/ib_verbs.h>
 39#include <rdma/ib_mad.h>
 40#include <rdma/ib_smi.h>
 41
 42#include "mthca_dev.h"
 43#include "mthca_cmd.h"
 44
 45enum {
 46	MTHCA_VENDOR_CLASS1 = 0x9,
 47	MTHCA_VENDOR_CLASS2 = 0xa
 48};
 49
 50static int mthca_update_rate(struct mthca_dev *dev, u8 port_num)
 51{
 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		dev_warn(&dev->ib_dev.dev,
 62			 "ib_query_port failed (%d) forport %d\n", ret,
 63			 port_num);
 64		goto out;
 65	}
 66
 67	dev->rate[port_num - 1] = tprops->active_speed *
 68				  ib_width_enum_to_int(tprops->active_width);
 69
 70out:
 71	kfree(tprops);
 72	return ret;
 73}
 74
 75static void update_sm_ah(struct mthca_dev *dev,
 76			 u8 port_num, u16 lid, u8 sl)
 77{
 78	struct ib_ah *new_ah;
 79	struct rdma_ah_attr ah_attr;
 80	unsigned long flags;
 81
 82	if (!dev->send_agent[port_num - 1][0])
 83		return;
 84
 85	memset(&ah_attr, 0, sizeof ah_attr);
 86	ah_attr.type = rdma_ah_find_type(&dev->ib_dev, port_num);
 87	rdma_ah_set_dlid(&ah_attr, lid);
 88	rdma_ah_set_sl(&ah_attr, sl);
 89	rdma_ah_set_port_num(&ah_attr, port_num);
 90
 91	new_ah = rdma_create_ah(dev->send_agent[port_num - 1][0]->qp->pd,
 92				&ah_attr, 0);
 93	if (IS_ERR(new_ah))
 94		return;
 95
 96	spin_lock_irqsave(&dev->sm_lock, flags);
 97	if (dev->sm_ah[port_num - 1])
 98		rdma_destroy_ah(dev->sm_ah[port_num - 1], 0);
 99	dev->sm_ah[port_num - 1] = new_ah;
100	spin_unlock_irqrestore(&dev->sm_lock, flags);
101}
102
103/*
104 * Snoop SM MADs for port info and P_Key table sets, so we can
105 * synthesize LID change and P_Key change events.
106 */
107static void smp_snoop(struct ib_device *ibdev,
108		      u8 port_num,
109		      const struct ib_mad *mad,
110		      u16 prev_lid)
111{
112	struct ib_event event;
113
114	if ((mad->mad_hdr.mgmt_class  == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
115	     mad->mad_hdr.mgmt_class  == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
116	    mad->mad_hdr.method     == IB_MGMT_METHOD_SET) {
117		if (mad->mad_hdr.attr_id == IB_SMP_ATTR_PORT_INFO) {
118			struct ib_port_info *pinfo =
119				(struct ib_port_info *) ((struct ib_smp *) mad)->data;
120			u16 lid = be16_to_cpu(pinfo->lid);
121
122			mthca_update_rate(to_mdev(ibdev), port_num);
123			update_sm_ah(to_mdev(ibdev), port_num,
124				     be16_to_cpu(pinfo->sm_lid),
125				     pinfo->neighbormtu_mastersmsl & 0xf);
126
127			event.device           = ibdev;
128			event.element.port_num = port_num;
129
130			if (pinfo->clientrereg_resv_subnetto & 0x80) {
131				event.event    = IB_EVENT_CLIENT_REREGISTER;
132				ib_dispatch_event(&event);
133			}
134
135			if (prev_lid != lid) {
136				event.event    = IB_EVENT_LID_CHANGE;
137				ib_dispatch_event(&event);
138			}
139		}
140
141		if (mad->mad_hdr.attr_id == IB_SMP_ATTR_PKEY_TABLE) {
142			event.device           = ibdev;
143			event.event            = IB_EVENT_PKEY_CHANGE;
144			event.element.port_num = port_num;
145			ib_dispatch_event(&event);
146		}
147	}
148}
149
150static void node_desc_override(struct ib_device *dev,
151			       struct ib_mad *mad)
152{
153	if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
154	     mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
155	    mad->mad_hdr.method == IB_MGMT_METHOD_GET_RESP &&
156	    mad->mad_hdr.attr_id == IB_SMP_ATTR_NODE_DESC) {
157		mutex_lock(&to_mdev(dev)->cap_mask_mutex);
158		memcpy(((struct ib_smp *) mad)->data, dev->node_desc,
159		       IB_DEVICE_NODE_DESC_MAX);
160		mutex_unlock(&to_mdev(dev)->cap_mask_mutex);
161	}
162}
163
164static void forward_trap(struct mthca_dev *dev,
165			 u32 port_num,
166			 const struct ib_mad *mad)
167{
168	int qpn = mad->mad_hdr.mgmt_class != IB_MGMT_CLASS_SUBN_LID_ROUTED;
169	struct ib_mad_send_buf *send_buf;
170	struct ib_mad_agent *agent = dev->send_agent[port_num - 1][qpn];
171	int ret;
172	unsigned long flags;
173
174	if (agent) {
175		send_buf = ib_create_send_mad(agent, qpn, 0, 0, IB_MGMT_MAD_HDR,
176					      IB_MGMT_MAD_DATA, GFP_ATOMIC,
177					      IB_MGMT_BASE_VERSION);
178		if (IS_ERR(send_buf))
179			return;
180		/*
181		 * We rely here on the fact that MLX QPs don't use the
182		 * address handle after the send is posted (this is
183		 * wrong following the IB spec strictly, but we know
184		 * it's OK for our devices).
185		 */
186		spin_lock_irqsave(&dev->sm_lock, flags);
187		memcpy(send_buf->mad, mad, sizeof *mad);
188		if ((send_buf->ah = dev->sm_ah[port_num - 1]))
189			ret = ib_post_send_mad(send_buf, NULL);
190		else
191			ret = -EINVAL;
192		spin_unlock_irqrestore(&dev->sm_lock, flags);
193
194		if (ret)
195			ib_free_send_mad(send_buf);
196	}
197}
198
199int mthca_process_mad(struct ib_device *ibdev, int mad_flags, u32 port_num,
200		      const struct ib_wc *in_wc, const struct ib_grh *in_grh,
201		      const struct ib_mad *in, struct ib_mad *out,
202		      size_t *out_mad_size, u16 *out_mad_pkey_index)
 
 
 
 
203{
204	int err;
205	u16 slid = in_wc ? ib_lid_cpu16(in_wc->slid) : be16_to_cpu(IB_LID_PERMISSIVE);
206	u16 prev_lid = 0;
207	struct ib_port_attr pattr;
 
 
 
 
 
 
208
209	/* Forward locally generated traps to the SM */
210	if (in->mad_hdr.method == IB_MGMT_METHOD_TRAP && !slid) {
211		forward_trap(to_mdev(ibdev), port_num, in);
 
212		return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
213	}
214
215	/*
216	 * Only handle SM gets, sets and trap represses for SM class
217	 *
218	 * Only handle PMA and Mellanox vendor-specific class gets and
219	 * sets for other classes.
220	 */
221	if (in->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
222	    in->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
223		if (in->mad_hdr.method != IB_MGMT_METHOD_GET &&
224		    in->mad_hdr.method != IB_MGMT_METHOD_SET &&
225		    in->mad_hdr.method != IB_MGMT_METHOD_TRAP_REPRESS)
226			return IB_MAD_RESULT_SUCCESS;
227
228		/*
229		 * Don't process SMInfo queries or vendor-specific
230		 * MADs -- the SMA can't handle them.
231		 */
232		if (in->mad_hdr.attr_id == IB_SMP_ATTR_SM_INFO ||
233		    ((in->mad_hdr.attr_id & IB_SMP_ATTR_VENDOR_MASK) ==
234		     IB_SMP_ATTR_VENDOR_MASK))
235			return IB_MAD_RESULT_SUCCESS;
236	} else if (in->mad_hdr.mgmt_class == IB_MGMT_CLASS_PERF_MGMT ||
237		   in->mad_hdr.mgmt_class == MTHCA_VENDOR_CLASS1     ||
238		   in->mad_hdr.mgmt_class == MTHCA_VENDOR_CLASS2) {
239		if (in->mad_hdr.method != IB_MGMT_METHOD_GET &&
240		    in->mad_hdr.method != IB_MGMT_METHOD_SET)
241			return IB_MAD_RESULT_SUCCESS;
242	} else
243		return IB_MAD_RESULT_SUCCESS;
244	if ((in->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
245	     in->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
246	    in->mad_hdr.method == IB_MGMT_METHOD_SET &&
247	    in->mad_hdr.attr_id == IB_SMP_ATTR_PORT_INFO &&
248	    !ib_query_port(ibdev, port_num, &pattr))
249		prev_lid = ib_lid_cpu16(pattr.lid);
250
251	err = mthca_MAD_IFC(to_mdev(ibdev), mad_flags & IB_MAD_IGNORE_MKEY,
252			    mad_flags & IB_MAD_IGNORE_BKEY, port_num, in_wc,
253			    in_grh, in, out);
 
254	if (err == -EBADMSG)
255		return IB_MAD_RESULT_SUCCESS;
256	else if (err) {
257		mthca_err(to_mdev(ibdev), "MAD_IFC returned %d\n", err);
258		return IB_MAD_RESULT_FAILURE;
259	}
260
261	if (!out->mad_hdr.status) {
262		smp_snoop(ibdev, port_num, in, prev_lid);
263		node_desc_override(ibdev, out);
264	}
265
266	/* set return bit in status of directed route responses */
267	if (in->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
268		out->mad_hdr.status |= cpu_to_be16(1 << 15);
269
270	if (in->mad_hdr.method == IB_MGMT_METHOD_TRAP_REPRESS)
271		/* no response for trap repress */
272		return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
273
274	return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
275}
276
277static void send_handler(struct ib_mad_agent *agent,
278			 struct ib_mad_send_wc *mad_send_wc)
279{
280	ib_free_send_mad(mad_send_wc->send_buf);
281}
282
283int mthca_create_agents(struct mthca_dev *dev)
284{
285	struct ib_mad_agent *agent;
286	int p, q;
287	int ret;
288
289	spin_lock_init(&dev->sm_lock);
290
291	for (p = 0; p < dev->limits.num_ports; ++p)
292		for (q = 0; q <= 1; ++q) {
293			agent = ib_register_mad_agent(&dev->ib_dev, p + 1,
294						      q ? IB_QPT_GSI : IB_QPT_SMI,
295						      NULL, 0, send_handler,
296						      NULL, NULL, 0);
297			if (IS_ERR(agent)) {
298				ret = PTR_ERR(agent);
299				goto err;
300			}
301			dev->send_agent[p][q] = agent;
302		}
303
304
305	for (p = 1; p <= dev->limits.num_ports; ++p) {
306		ret = mthca_update_rate(dev, p);
307		if (ret) {
308			mthca_err(dev, "Failed to obtain port %d rate."
309				  " aborting.\n", p);
310			goto err;
311		}
312	}
313
314	return 0;
315
316err:
317	for (p = 0; p < dev->limits.num_ports; ++p)
318		for (q = 0; q <= 1; ++q)
319			if (dev->send_agent[p][q])
320				ib_unregister_mad_agent(dev->send_agent[p][q]);
321
322	return ret;
323}
324
325void mthca_free_agents(struct mthca_dev *dev)
326{
327	struct ib_mad_agent *agent;
328	int p, q;
329
330	for (p = 0; p < dev->limits.num_ports; ++p) {
331		for (q = 0; q <= 1; ++q) {
332			agent = dev->send_agent[p][q];
333			dev->send_agent[p][q] = NULL;
334			ib_unregister_mad_agent(agent);
335		}
336
337		if (dev->sm_ah[p])
338			rdma_destroy_ah(dev->sm_ah[p],
339					RDMA_DESTROY_AH_SLEEPABLE);
340	}
341}
v4.17
  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
 35#include <linux/string.h>
 36#include <linux/slab.h>
 37
 38#include <rdma/ib_verbs.h>
 39#include <rdma/ib_mad.h>
 40#include <rdma/ib_smi.h>
 41
 42#include "mthca_dev.h"
 43#include "mthca_cmd.h"
 44
 45enum {
 46	MTHCA_VENDOR_CLASS1 = 0x9,
 47	MTHCA_VENDOR_CLASS2 = 0xa
 48};
 49
 50static int mthca_update_rate(struct mthca_dev *dev, u8 port_num)
 51{
 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
 74static 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 rdma_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.type = rdma_ah_find_type(&dev->ib_dev, port_num);
 86	rdma_ah_set_dlid(&ah_attr, lid);
 87	rdma_ah_set_sl(&ah_attr, sl);
 88	rdma_ah_set_port_num(&ah_attr, port_num);
 89
 90	new_ah = rdma_create_ah(dev->send_agent[port_num - 1][0]->qp->pd,
 91				&ah_attr);
 92	if (IS_ERR(new_ah))
 93		return;
 94
 95	spin_lock_irqsave(&dev->sm_lock, flags);
 96	if (dev->sm_ah[port_num - 1])
 97		rdma_destroy_ah(dev->sm_ah[port_num - 1]);
 98	dev->sm_ah[port_num - 1] = new_ah;
 99	spin_unlock_irqrestore(&dev->sm_lock, flags);
100}
101
102/*
103 * Snoop SM MADs for port info and P_Key table sets, so we can
104 * synthesize LID change and P_Key change events.
105 */
106static void smp_snoop(struct ib_device *ibdev,
107		      u8 port_num,
108		      const struct ib_mad *mad,
109		      u16 prev_lid)
110{
111	struct ib_event event;
112
113	if ((mad->mad_hdr.mgmt_class  == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
114	     mad->mad_hdr.mgmt_class  == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
115	    mad->mad_hdr.method     == IB_MGMT_METHOD_SET) {
116		if (mad->mad_hdr.attr_id == IB_SMP_ATTR_PORT_INFO) {
117			struct ib_port_info *pinfo =
118				(struct ib_port_info *) ((struct ib_smp *) mad)->data;
119			u16 lid = be16_to_cpu(pinfo->lid);
120
121			mthca_update_rate(to_mdev(ibdev), port_num);
122			update_sm_ah(to_mdev(ibdev), port_num,
123				     be16_to_cpu(pinfo->sm_lid),
124				     pinfo->neighbormtu_mastersmsl & 0xf);
125
126			event.device           = ibdev;
127			event.element.port_num = port_num;
128
129			if (pinfo->clientrereg_resv_subnetto & 0x80) {
130				event.event    = IB_EVENT_CLIENT_REREGISTER;
131				ib_dispatch_event(&event);
132			}
133
134			if (prev_lid != lid) {
135				event.event    = IB_EVENT_LID_CHANGE;
136				ib_dispatch_event(&event);
137			}
138		}
139
140		if (mad->mad_hdr.attr_id == IB_SMP_ATTR_PKEY_TABLE) {
141			event.device           = ibdev;
142			event.event            = IB_EVENT_PKEY_CHANGE;
143			event.element.port_num = port_num;
144			ib_dispatch_event(&event);
145		}
146	}
147}
148
149static void node_desc_override(struct ib_device *dev,
150			       struct ib_mad *mad)
151{
152	if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
153	     mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
154	    mad->mad_hdr.method == IB_MGMT_METHOD_GET_RESP &&
155	    mad->mad_hdr.attr_id == IB_SMP_ATTR_NODE_DESC) {
156		mutex_lock(&to_mdev(dev)->cap_mask_mutex);
157		memcpy(((struct ib_smp *) mad)->data, dev->node_desc,
158		       IB_DEVICE_NODE_DESC_MAX);
159		mutex_unlock(&to_mdev(dev)->cap_mask_mutex);
160	}
161}
162
163static void forward_trap(struct mthca_dev *dev,
164			 u8 port_num,
165			 const struct ib_mad *mad)
166{
167	int qpn = mad->mad_hdr.mgmt_class != IB_MGMT_CLASS_SUBN_LID_ROUTED;
168	struct ib_mad_send_buf *send_buf;
169	struct ib_mad_agent *agent = dev->send_agent[port_num - 1][qpn];
170	int ret;
171	unsigned long flags;
172
173	if (agent) {
174		send_buf = ib_create_send_mad(agent, qpn, 0, 0, IB_MGMT_MAD_HDR,
175					      IB_MGMT_MAD_DATA, GFP_ATOMIC,
176					      IB_MGMT_BASE_VERSION);
177		if (IS_ERR(send_buf))
178			return;
179		/*
180		 * We rely here on the fact that MLX QPs don't use the
181		 * address handle after the send is posted (this is
182		 * wrong following the IB spec strictly, but we know
183		 * it's OK for our devices).
184		 */
185		spin_lock_irqsave(&dev->sm_lock, flags);
186		memcpy(send_buf->mad, mad, sizeof *mad);
187		if ((send_buf->ah = dev->sm_ah[port_num - 1]))
188			ret = ib_post_send_mad(send_buf, NULL);
189		else
190			ret = -EINVAL;
191		spin_unlock_irqrestore(&dev->sm_lock, flags);
192
193		if (ret)
194			ib_free_send_mad(send_buf);
195	}
196}
197
198int mthca_process_mad(struct ib_device *ibdev,
199		      int mad_flags,
200		      u8 port_num,
201		      const struct ib_wc *in_wc,
202		      const struct ib_grh *in_grh,
203		      const struct ib_mad_hdr *in, size_t in_mad_size,
204		      struct ib_mad_hdr *out, size_t *out_mad_size,
205		      u16 *out_mad_pkey_index)
206{
207	int err;
208	u16 slid = in_wc ? ib_lid_cpu16(in_wc->slid) : be16_to_cpu(IB_LID_PERMISSIVE);
209	u16 prev_lid = 0;
210	struct ib_port_attr pattr;
211	const struct ib_mad *in_mad = (const struct ib_mad *)in;
212	struct ib_mad *out_mad = (struct ib_mad *)out;
213
214	if (WARN_ON_ONCE(in_mad_size != sizeof(*in_mad) ||
215			 *out_mad_size != sizeof(*out_mad)))
216		return IB_MAD_RESULT_FAILURE;
217
218	/* Forward locally generated traps to the SM */
219	if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP &&
220	    slid == 0) {
221		forward_trap(to_mdev(ibdev), port_num, in_mad);
222		return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
223	}
224
225	/*
226	 * Only handle SM gets, sets and trap represses for SM class
227	 *
228	 * Only handle PMA and Mellanox vendor-specific class gets and
229	 * sets for other classes.
230	 */
231	if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
232	    in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
233		if (in_mad->mad_hdr.method   != IB_MGMT_METHOD_GET &&
234		    in_mad->mad_hdr.method   != IB_MGMT_METHOD_SET &&
235		    in_mad->mad_hdr.method   != IB_MGMT_METHOD_TRAP_REPRESS)
236			return IB_MAD_RESULT_SUCCESS;
237
238		/*
239		 * Don't process SMInfo queries or vendor-specific
240		 * MADs -- the SMA can't handle them.
241		 */
242		if (in_mad->mad_hdr.attr_id == IB_SMP_ATTR_SM_INFO ||
243		    ((in_mad->mad_hdr.attr_id & IB_SMP_ATTR_VENDOR_MASK) ==
244		     IB_SMP_ATTR_VENDOR_MASK))
245			return IB_MAD_RESULT_SUCCESS;
246	} else if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_PERF_MGMT ||
247		   in_mad->mad_hdr.mgmt_class == MTHCA_VENDOR_CLASS1     ||
248		   in_mad->mad_hdr.mgmt_class == MTHCA_VENDOR_CLASS2) {
249		if (in_mad->mad_hdr.method  != IB_MGMT_METHOD_GET &&
250		    in_mad->mad_hdr.method  != IB_MGMT_METHOD_SET)
251			return IB_MAD_RESULT_SUCCESS;
252	} else
253		return IB_MAD_RESULT_SUCCESS;
254	if ((in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
255	     in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
256	    in_mad->mad_hdr.method == IB_MGMT_METHOD_SET &&
257	    in_mad->mad_hdr.attr_id == IB_SMP_ATTR_PORT_INFO &&
258	    !ib_query_port(ibdev, port_num, &pattr))
259		prev_lid = ib_lid_cpu16(pattr.lid);
260
261	err = mthca_MAD_IFC(to_mdev(ibdev),
262			    mad_flags & IB_MAD_IGNORE_MKEY,
263			    mad_flags & IB_MAD_IGNORE_BKEY,
264			    port_num, in_wc, in_grh, in_mad, out_mad);
265	if (err == -EBADMSG)
266		return IB_MAD_RESULT_SUCCESS;
267	else if (err) {
268		mthca_err(to_mdev(ibdev), "MAD_IFC returned %d\n", err);
269		return IB_MAD_RESULT_FAILURE;
270	}
271
272	if (!out_mad->mad_hdr.status) {
273		smp_snoop(ibdev, port_num, in_mad, prev_lid);
274		node_desc_override(ibdev, out_mad);
275	}
276
277	/* set return bit in status of directed route responses */
278	if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
279		out_mad->mad_hdr.status |= cpu_to_be16(1 << 15);
280
281	if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP_REPRESS)
282		/* no response for trap repress */
283		return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
284
285	return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
286}
287
288static void send_handler(struct ib_mad_agent *agent,
289			 struct ib_mad_send_wc *mad_send_wc)
290{
291	ib_free_send_mad(mad_send_wc->send_buf);
292}
293
294int mthca_create_agents(struct mthca_dev *dev)
295{
296	struct ib_mad_agent *agent;
297	int p, q;
298	int ret;
299
300	spin_lock_init(&dev->sm_lock);
301
302	for (p = 0; p < dev->limits.num_ports; ++p)
303		for (q = 0; q <= 1; ++q) {
304			agent = ib_register_mad_agent(&dev->ib_dev, p + 1,
305						      q ? IB_QPT_GSI : IB_QPT_SMI,
306						      NULL, 0, send_handler,
307						      NULL, NULL, 0);
308			if (IS_ERR(agent)) {
309				ret = PTR_ERR(agent);
310				goto err;
311			}
312			dev->send_agent[p][q] = agent;
313		}
314
315
316	for (p = 1; p <= dev->limits.num_ports; ++p) {
317		ret = mthca_update_rate(dev, p);
318		if (ret) {
319			mthca_err(dev, "Failed to obtain port %d rate."
320				  " aborting.\n", p);
321			goto err;
322		}
323	}
324
325	return 0;
326
327err:
328	for (p = 0; p < dev->limits.num_ports; ++p)
329		for (q = 0; q <= 1; ++q)
330			if (dev->send_agent[p][q])
331				ib_unregister_mad_agent(dev->send_agent[p][q]);
332
333	return ret;
334}
335
336void mthca_free_agents(struct mthca_dev *dev)
337{
338	struct ib_mad_agent *agent;
339	int p, q;
340
341	for (p = 0; p < dev->limits.num_ports; ++p) {
342		for (q = 0; q <= 1; ++q) {
343			agent = dev->send_agent[p][q];
344			dev->send_agent[p][q] = NULL;
345			ib_unregister_mad_agent(agent);
346		}
347
348		if (dev->sm_ah[p])
349			rdma_destroy_ah(dev->sm_ah[p]);
 
350	}
351}