xref: /openbmc/linux/drivers/infiniband/hw/mlx4/mad.c (revision b1d8eb5a213640f1be98a90e73a241d15b70045c)
1225c7b1fSRoland Dreier /*
2225c7b1fSRoland Dreier  * Copyright (c) 2007 Cisco Systems, Inc. All rights reserved.
3225c7b1fSRoland Dreier  *
4225c7b1fSRoland Dreier  * This software is available to you under a choice of one of two
5225c7b1fSRoland Dreier  * licenses.  You may choose to be licensed under the terms of the GNU
6225c7b1fSRoland Dreier  * General Public License (GPL) Version 2, available from the file
7225c7b1fSRoland Dreier  * COPYING in the main directory of this source tree, or the
8225c7b1fSRoland Dreier  * OpenIB.org BSD license below:
9225c7b1fSRoland Dreier  *
10225c7b1fSRoland Dreier  *     Redistribution and use in source and binary forms, with or
11225c7b1fSRoland Dreier  *     without modification, are permitted provided that the following
12225c7b1fSRoland Dreier  *     conditions are met:
13225c7b1fSRoland Dreier  *
14225c7b1fSRoland Dreier  *      - Redistributions of source code must retain the above
15225c7b1fSRoland Dreier  *        copyright notice, this list of conditions and the following
16225c7b1fSRoland Dreier  *        disclaimer.
17225c7b1fSRoland Dreier  *
18225c7b1fSRoland Dreier  *      - Redistributions in binary form must reproduce the above
19225c7b1fSRoland Dreier  *        copyright notice, this list of conditions and the following
20225c7b1fSRoland Dreier  *        disclaimer in the documentation and/or other materials
21225c7b1fSRoland Dreier  *        provided with the distribution.
22225c7b1fSRoland Dreier  *
23225c7b1fSRoland Dreier  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24225c7b1fSRoland Dreier  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25225c7b1fSRoland Dreier  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26225c7b1fSRoland Dreier  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27225c7b1fSRoland Dreier  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28225c7b1fSRoland Dreier  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29225c7b1fSRoland Dreier  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30225c7b1fSRoland Dreier  * SOFTWARE.
31225c7b1fSRoland Dreier  */
32225c7b1fSRoland Dreier 
33225c7b1fSRoland Dreier #include <rdma/ib_mad.h>
34225c7b1fSRoland Dreier #include <rdma/ib_smi.h>
35225c7b1fSRoland Dreier 
36225c7b1fSRoland Dreier #include <linux/mlx4/cmd.h>
375a0e3ad6STejun Heo #include <linux/gfp.h>
38c3779134SOr Gerlitz #include <rdma/ib_pma.h>
39225c7b1fSRoland Dreier 
40225c7b1fSRoland Dreier #include "mlx4_ib.h"
41225c7b1fSRoland Dreier 
42225c7b1fSRoland Dreier enum {
43225c7b1fSRoland Dreier 	MLX4_IB_VENDOR_CLASS1 = 0x9,
44225c7b1fSRoland Dreier 	MLX4_IB_VENDOR_CLASS2 = 0xa
45225c7b1fSRoland Dreier };
46225c7b1fSRoland Dreier 
47225c7b1fSRoland Dreier int mlx4_MAD_IFC(struct mlx4_ib_dev *dev, int ignore_mkey, int ignore_bkey,
48225c7b1fSRoland Dreier 		 int port, struct ib_wc *in_wc, struct ib_grh *in_grh,
49225c7b1fSRoland Dreier 		 void *in_mad, void *response_mad)
50225c7b1fSRoland Dreier {
51225c7b1fSRoland Dreier 	struct mlx4_cmd_mailbox *inmailbox, *outmailbox;
52225c7b1fSRoland Dreier 	void *inbox;
53225c7b1fSRoland Dreier 	int err;
54225c7b1fSRoland Dreier 	u32 in_modifier = port;
55225c7b1fSRoland Dreier 	u8 op_modifier = 0;
56225c7b1fSRoland Dreier 
57225c7b1fSRoland Dreier 	inmailbox = mlx4_alloc_cmd_mailbox(dev->dev);
58225c7b1fSRoland Dreier 	if (IS_ERR(inmailbox))
59225c7b1fSRoland Dreier 		return PTR_ERR(inmailbox);
60225c7b1fSRoland Dreier 	inbox = inmailbox->buf;
61225c7b1fSRoland Dreier 
62225c7b1fSRoland Dreier 	outmailbox = mlx4_alloc_cmd_mailbox(dev->dev);
63225c7b1fSRoland Dreier 	if (IS_ERR(outmailbox)) {
64225c7b1fSRoland Dreier 		mlx4_free_cmd_mailbox(dev->dev, inmailbox);
65225c7b1fSRoland Dreier 		return PTR_ERR(outmailbox);
66225c7b1fSRoland Dreier 	}
67225c7b1fSRoland Dreier 
68225c7b1fSRoland Dreier 	memcpy(inbox, in_mad, 256);
69225c7b1fSRoland Dreier 
70225c7b1fSRoland Dreier 	/*
71225c7b1fSRoland Dreier 	 * Key check traps can't be generated unless we have in_wc to
72225c7b1fSRoland Dreier 	 * tell us where to send the trap.
73225c7b1fSRoland Dreier 	 */
74225c7b1fSRoland Dreier 	if (ignore_mkey || !in_wc)
75225c7b1fSRoland Dreier 		op_modifier |= 0x1;
76225c7b1fSRoland Dreier 	if (ignore_bkey || !in_wc)
77225c7b1fSRoland Dreier 		op_modifier |= 0x2;
78225c7b1fSRoland Dreier 
79225c7b1fSRoland Dreier 	if (in_wc) {
80225c7b1fSRoland Dreier 		struct {
81225c7b1fSRoland Dreier 			__be32		my_qpn;
82225c7b1fSRoland Dreier 			u32		reserved1;
83225c7b1fSRoland Dreier 			__be32		rqpn;
84225c7b1fSRoland Dreier 			u8		sl;
85225c7b1fSRoland Dreier 			u8		g_path;
86225c7b1fSRoland Dreier 			u16		reserved2[2];
87225c7b1fSRoland Dreier 			__be16		pkey;
88225c7b1fSRoland Dreier 			u32		reserved3[11];
89225c7b1fSRoland Dreier 			u8		grh[40];
90225c7b1fSRoland Dreier 		} *ext_info;
91225c7b1fSRoland Dreier 
92225c7b1fSRoland Dreier 		memset(inbox + 256, 0, 256);
93225c7b1fSRoland Dreier 		ext_info = inbox + 256;
94225c7b1fSRoland Dreier 
95225c7b1fSRoland Dreier 		ext_info->my_qpn = cpu_to_be32(in_wc->qp->qp_num);
96225c7b1fSRoland Dreier 		ext_info->rqpn   = cpu_to_be32(in_wc->src_qp);
97225c7b1fSRoland Dreier 		ext_info->sl     = in_wc->sl << 4;
98225c7b1fSRoland Dreier 		ext_info->g_path = in_wc->dlid_path_bits |
99225c7b1fSRoland Dreier 			(in_wc->wc_flags & IB_WC_GRH ? 0x80 : 0);
100225c7b1fSRoland Dreier 		ext_info->pkey   = cpu_to_be16(in_wc->pkey_index);
101225c7b1fSRoland Dreier 
102225c7b1fSRoland Dreier 		if (in_grh)
103225c7b1fSRoland Dreier 			memcpy(ext_info->grh, in_grh, 40);
104225c7b1fSRoland Dreier 
105225c7b1fSRoland Dreier 		op_modifier |= 0x4;
106225c7b1fSRoland Dreier 
107225c7b1fSRoland Dreier 		in_modifier |= in_wc->slid << 16;
108225c7b1fSRoland Dreier 	}
109225c7b1fSRoland Dreier 
110225c7b1fSRoland Dreier 	err = mlx4_cmd_box(dev->dev, inmailbox->dma, outmailbox->dma,
111225c7b1fSRoland Dreier 			   in_modifier, op_modifier,
112f9baff50SJack Morgenstein 			   MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C,
113f9baff50SJack Morgenstein 			   MLX4_CMD_NATIVE);
114225c7b1fSRoland Dreier 
115fe11cb6bSIlpo Järvinen 	if (!err)
116225c7b1fSRoland Dreier 		memcpy(response_mad, outmailbox->buf, 256);
117225c7b1fSRoland Dreier 
118225c7b1fSRoland Dreier 	mlx4_free_cmd_mailbox(dev->dev, inmailbox);
119225c7b1fSRoland Dreier 	mlx4_free_cmd_mailbox(dev->dev, outmailbox);
120225c7b1fSRoland Dreier 
121225c7b1fSRoland Dreier 	return err;
122225c7b1fSRoland Dreier }
123225c7b1fSRoland Dreier 
124225c7b1fSRoland Dreier static void update_sm_ah(struct mlx4_ib_dev *dev, u8 port_num, u16 lid, u8 sl)
125225c7b1fSRoland Dreier {
126225c7b1fSRoland Dreier 	struct ib_ah *new_ah;
127225c7b1fSRoland Dreier 	struct ib_ah_attr ah_attr;
128225c7b1fSRoland Dreier 
129225c7b1fSRoland Dreier 	if (!dev->send_agent[port_num - 1][0])
130225c7b1fSRoland Dreier 		return;
131225c7b1fSRoland Dreier 
132225c7b1fSRoland Dreier 	memset(&ah_attr, 0, sizeof ah_attr);
133225c7b1fSRoland Dreier 	ah_attr.dlid     = lid;
134225c7b1fSRoland Dreier 	ah_attr.sl       = sl;
135225c7b1fSRoland Dreier 	ah_attr.port_num = port_num;
136225c7b1fSRoland Dreier 
137225c7b1fSRoland Dreier 	new_ah = ib_create_ah(dev->send_agent[port_num - 1][0]->qp->pd,
138225c7b1fSRoland Dreier 			      &ah_attr);
139225c7b1fSRoland Dreier 	if (IS_ERR(new_ah))
140225c7b1fSRoland Dreier 		return;
141225c7b1fSRoland Dreier 
142225c7b1fSRoland Dreier 	spin_lock(&dev->sm_lock);
143225c7b1fSRoland Dreier 	if (dev->sm_ah[port_num - 1])
144225c7b1fSRoland Dreier 		ib_destroy_ah(dev->sm_ah[port_num - 1]);
145225c7b1fSRoland Dreier 	dev->sm_ah[port_num - 1] = new_ah;
146225c7b1fSRoland Dreier 	spin_unlock(&dev->sm_lock);
147225c7b1fSRoland Dreier }
148225c7b1fSRoland Dreier 
149225c7b1fSRoland Dreier /*
150225c7b1fSRoland Dreier  * Snoop SM MADs for port info and P_Key table sets, so we can
151225c7b1fSRoland Dreier  * synthesize LID change and P_Key change events.
152225c7b1fSRoland Dreier  */
153f0f6f346SMoni Shoua static void smp_snoop(struct ib_device *ibdev, u8 port_num, struct ib_mad *mad,
154f0f6f346SMoni Shoua 				u16 prev_lid)
155225c7b1fSRoland Dreier {
156225c7b1fSRoland Dreier 	struct ib_event event;
157225c7b1fSRoland Dreier 
158225c7b1fSRoland Dreier 	if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
159225c7b1fSRoland Dreier 	     mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
160225c7b1fSRoland Dreier 	    mad->mad_hdr.method == IB_MGMT_METHOD_SET) {
161225c7b1fSRoland Dreier 		if (mad->mad_hdr.attr_id == IB_SMP_ATTR_PORT_INFO) {
162225c7b1fSRoland Dreier 			struct ib_port_info *pinfo =
163225c7b1fSRoland Dreier 				(struct ib_port_info *) ((struct ib_smp *) mad)->data;
164f0f6f346SMoni Shoua 			u16 lid = be16_to_cpu(pinfo->lid);
165225c7b1fSRoland Dreier 
166225c7b1fSRoland Dreier 			update_sm_ah(to_mdev(ibdev), port_num,
167225c7b1fSRoland Dreier 				     be16_to_cpu(pinfo->sm_lid),
168225c7b1fSRoland Dreier 				     pinfo->neighbormtu_mastersmsl & 0xf);
169225c7b1fSRoland Dreier 
170225c7b1fSRoland Dreier 			event.device	       = ibdev;
171225c7b1fSRoland Dreier 			event.element.port_num = port_num;
172225c7b1fSRoland Dreier 
173f0f6f346SMoni Shoua 			if (pinfo->clientrereg_resv_subnetto & 0x80) {
174225c7b1fSRoland Dreier 				event.event    = IB_EVENT_CLIENT_REREGISTER;
175225c7b1fSRoland Dreier 				ib_dispatch_event(&event);
176225c7b1fSRoland Dreier 			}
177225c7b1fSRoland Dreier 
178f0f6f346SMoni Shoua 			if (prev_lid != lid) {
179f0f6f346SMoni Shoua 				event.event    = IB_EVENT_LID_CHANGE;
180f0f6f346SMoni Shoua 				ib_dispatch_event(&event);
181f0f6f346SMoni Shoua 			}
182f0f6f346SMoni Shoua 		}
183f0f6f346SMoni Shoua 
184225c7b1fSRoland Dreier 		if (mad->mad_hdr.attr_id == IB_SMP_ATTR_PKEY_TABLE) {
185225c7b1fSRoland Dreier 			event.device	       = ibdev;
186225c7b1fSRoland Dreier 			event.event	       = IB_EVENT_PKEY_CHANGE;
187225c7b1fSRoland Dreier 			event.element.port_num = port_num;
188225c7b1fSRoland Dreier 			ib_dispatch_event(&event);
189225c7b1fSRoland Dreier 		}
190225c7b1fSRoland Dreier 	}
191225c7b1fSRoland Dreier }
192225c7b1fSRoland Dreier 
193225c7b1fSRoland Dreier static void node_desc_override(struct ib_device *dev,
194225c7b1fSRoland Dreier 			       struct ib_mad *mad)
195225c7b1fSRoland Dreier {
196225c7b1fSRoland Dreier 	if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
197225c7b1fSRoland Dreier 	     mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
198225c7b1fSRoland Dreier 	    mad->mad_hdr.method == IB_MGMT_METHOD_GET_RESP &&
199225c7b1fSRoland Dreier 	    mad->mad_hdr.attr_id == IB_SMP_ATTR_NODE_DESC) {
200225c7b1fSRoland Dreier 		spin_lock(&to_mdev(dev)->sm_lock);
201225c7b1fSRoland Dreier 		memcpy(((struct ib_smp *) mad)->data, dev->node_desc, 64);
202225c7b1fSRoland Dreier 		spin_unlock(&to_mdev(dev)->sm_lock);
203225c7b1fSRoland Dreier 	}
204225c7b1fSRoland Dreier }
205225c7b1fSRoland Dreier 
206225c7b1fSRoland Dreier static void forward_trap(struct mlx4_ib_dev *dev, u8 port_num, struct ib_mad *mad)
207225c7b1fSRoland Dreier {
208225c7b1fSRoland Dreier 	int qpn = mad->mad_hdr.mgmt_class != IB_MGMT_CLASS_SUBN_LID_ROUTED;
209225c7b1fSRoland Dreier 	struct ib_mad_send_buf *send_buf;
210225c7b1fSRoland Dreier 	struct ib_mad_agent *agent = dev->send_agent[port_num - 1][qpn];
211225c7b1fSRoland Dreier 	int ret;
212225c7b1fSRoland Dreier 
213225c7b1fSRoland Dreier 	if (agent) {
214225c7b1fSRoland Dreier 		send_buf = ib_create_send_mad(agent, qpn, 0, 0, IB_MGMT_MAD_HDR,
215225c7b1fSRoland Dreier 					      IB_MGMT_MAD_DATA, GFP_ATOMIC);
21613974909SDan Carpenter 		if (IS_ERR(send_buf))
21713974909SDan Carpenter 			return;
218225c7b1fSRoland Dreier 		/*
219225c7b1fSRoland Dreier 		 * We rely here on the fact that MLX QPs don't use the
220225c7b1fSRoland Dreier 		 * address handle after the send is posted (this is
221225c7b1fSRoland Dreier 		 * wrong following the IB spec strictly, but we know
222225c7b1fSRoland Dreier 		 * it's OK for our devices).
223225c7b1fSRoland Dreier 		 */
224225c7b1fSRoland Dreier 		spin_lock(&dev->sm_lock);
225225c7b1fSRoland Dreier 		memcpy(send_buf->mad, mad, sizeof *mad);
226225c7b1fSRoland Dreier 		if ((send_buf->ah = dev->sm_ah[port_num - 1]))
227225c7b1fSRoland Dreier 			ret = ib_post_send_mad(send_buf, NULL);
228225c7b1fSRoland Dreier 		else
229225c7b1fSRoland Dreier 			ret = -EINVAL;
230225c7b1fSRoland Dreier 		spin_unlock(&dev->sm_lock);
231225c7b1fSRoland Dreier 
232225c7b1fSRoland Dreier 		if (ret)
233225c7b1fSRoland Dreier 			ib_free_send_mad(send_buf);
234225c7b1fSRoland Dreier 	}
235225c7b1fSRoland Dreier }
236225c7b1fSRoland Dreier 
237c3779134SOr Gerlitz static int ib_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
238225c7b1fSRoland Dreier 			struct ib_wc *in_wc, struct ib_grh *in_grh,
239225c7b1fSRoland Dreier 			struct ib_mad *in_mad, struct ib_mad *out_mad)
240225c7b1fSRoland Dreier {
241f0f6f346SMoni Shoua 	u16 slid, prev_lid = 0;
242225c7b1fSRoland Dreier 	int err;
243f0f6f346SMoni Shoua 	struct ib_port_attr pattr;
244225c7b1fSRoland Dreier 
245*b1d8eb5aSJack Morgenstein 	if (in_wc && in_wc->qp->qp_num) {
246*b1d8eb5aSJack Morgenstein 		pr_debug("received MAD: slid:%d sqpn:%d "
247*b1d8eb5aSJack Morgenstein 			"dlid_bits:%d dqpn:%d wc_flags:0x%x, cls %x, mtd %x, atr %x\n",
248*b1d8eb5aSJack Morgenstein 			in_wc->slid, in_wc->src_qp,
249*b1d8eb5aSJack Morgenstein 			in_wc->dlid_path_bits,
250*b1d8eb5aSJack Morgenstein 			in_wc->qp->qp_num,
251*b1d8eb5aSJack Morgenstein 			in_wc->wc_flags,
252*b1d8eb5aSJack Morgenstein 			in_mad->mad_hdr.mgmt_class, in_mad->mad_hdr.method,
253*b1d8eb5aSJack Morgenstein 			be16_to_cpu(in_mad->mad_hdr.attr_id));
254*b1d8eb5aSJack Morgenstein 		if (in_wc->wc_flags & IB_WC_GRH) {
255*b1d8eb5aSJack Morgenstein 			pr_debug("sgid_hi:0x%016llx sgid_lo:0x%016llx\n",
256*b1d8eb5aSJack Morgenstein 				 be64_to_cpu(in_grh->sgid.global.subnet_prefix),
257*b1d8eb5aSJack Morgenstein 				 be64_to_cpu(in_grh->sgid.global.interface_id));
258*b1d8eb5aSJack Morgenstein 			pr_debug("dgid_hi:0x%016llx dgid_lo:0x%016llx\n",
259*b1d8eb5aSJack Morgenstein 				 be64_to_cpu(in_grh->dgid.global.subnet_prefix),
260*b1d8eb5aSJack Morgenstein 				 be64_to_cpu(in_grh->dgid.global.interface_id));
261*b1d8eb5aSJack Morgenstein 		}
262*b1d8eb5aSJack Morgenstein 	}
263*b1d8eb5aSJack Morgenstein 
264225c7b1fSRoland Dreier 	slid = in_wc ? in_wc->slid : be16_to_cpu(IB_LID_PERMISSIVE);
265225c7b1fSRoland Dreier 
266225c7b1fSRoland Dreier 	if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP && slid == 0) {
267225c7b1fSRoland Dreier 		forward_trap(to_mdev(ibdev), port_num, in_mad);
268225c7b1fSRoland Dreier 		return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
269225c7b1fSRoland Dreier 	}
270225c7b1fSRoland Dreier 
271225c7b1fSRoland Dreier 	if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
272225c7b1fSRoland Dreier 	    in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
273225c7b1fSRoland Dreier 		if (in_mad->mad_hdr.method   != IB_MGMT_METHOD_GET &&
274225c7b1fSRoland Dreier 		    in_mad->mad_hdr.method   != IB_MGMT_METHOD_SET &&
275225c7b1fSRoland Dreier 		    in_mad->mad_hdr.method   != IB_MGMT_METHOD_TRAP_REPRESS)
276225c7b1fSRoland Dreier 			return IB_MAD_RESULT_SUCCESS;
277225c7b1fSRoland Dreier 
278225c7b1fSRoland Dreier 		/*
279a6f7feaeSJack Morgenstein 		 * Don't process SMInfo queries -- the SMA can't handle them.
280225c7b1fSRoland Dreier 		 */
281a6f7feaeSJack Morgenstein 		if (in_mad->mad_hdr.attr_id == IB_SMP_ATTR_SM_INFO)
282225c7b1fSRoland Dreier 			return IB_MAD_RESULT_SUCCESS;
283225c7b1fSRoland Dreier 	} else if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_PERF_MGMT ||
284225c7b1fSRoland Dreier 		   in_mad->mad_hdr.mgmt_class == MLX4_IB_VENDOR_CLASS1   ||
2856578cf33SEli Cohen 		   in_mad->mad_hdr.mgmt_class == MLX4_IB_VENDOR_CLASS2   ||
2866578cf33SEli Cohen 		   in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_CONG_MGMT) {
287225c7b1fSRoland Dreier 		if (in_mad->mad_hdr.method  != IB_MGMT_METHOD_GET &&
288225c7b1fSRoland Dreier 		    in_mad->mad_hdr.method  != IB_MGMT_METHOD_SET)
289225c7b1fSRoland Dreier 			return IB_MAD_RESULT_SUCCESS;
290225c7b1fSRoland Dreier 	} else
291225c7b1fSRoland Dreier 		return IB_MAD_RESULT_SUCCESS;
292225c7b1fSRoland Dreier 
293f0f6f346SMoni Shoua 	if ((in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
294f0f6f346SMoni Shoua 	     in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
295f0f6f346SMoni Shoua 	    in_mad->mad_hdr.method == IB_MGMT_METHOD_SET &&
296f0f6f346SMoni Shoua 	    in_mad->mad_hdr.attr_id == IB_SMP_ATTR_PORT_INFO &&
297f0f6f346SMoni Shoua 	    !ib_query_port(ibdev, port_num, &pattr))
298f0f6f346SMoni Shoua 		prev_lid = pattr.lid;
299f0f6f346SMoni Shoua 
300225c7b1fSRoland Dreier 	err = mlx4_MAD_IFC(to_mdev(ibdev),
301225c7b1fSRoland Dreier 			   mad_flags & IB_MAD_IGNORE_MKEY,
302225c7b1fSRoland Dreier 			   mad_flags & IB_MAD_IGNORE_BKEY,
303225c7b1fSRoland Dreier 			   port_num, in_wc, in_grh, in_mad, out_mad);
304225c7b1fSRoland Dreier 	if (err)
305225c7b1fSRoland Dreier 		return IB_MAD_RESULT_FAILURE;
306225c7b1fSRoland Dreier 
307225c7b1fSRoland Dreier 	if (!out_mad->mad_hdr.status) {
308f0f6f346SMoni Shoua 		smp_snoop(ibdev, port_num, in_mad, prev_lid);
309225c7b1fSRoland Dreier 		node_desc_override(ibdev, out_mad);
310225c7b1fSRoland Dreier 	}
311225c7b1fSRoland Dreier 
312225c7b1fSRoland Dreier 	/* set return bit in status of directed route responses */
313225c7b1fSRoland Dreier 	if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
314225c7b1fSRoland Dreier 		out_mad->mad_hdr.status |= cpu_to_be16(1 << 15);
315225c7b1fSRoland Dreier 
316225c7b1fSRoland Dreier 	if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP_REPRESS)
317225c7b1fSRoland Dreier 		/* no response for trap repress */
318225c7b1fSRoland Dreier 		return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
319225c7b1fSRoland Dreier 
320225c7b1fSRoland Dreier 	return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
321225c7b1fSRoland Dreier }
322225c7b1fSRoland Dreier 
323c3779134SOr Gerlitz static void edit_counter(struct mlx4_counter *cnt,
324c3779134SOr Gerlitz 					struct ib_pma_portcounters *pma_cnt)
325c3779134SOr Gerlitz {
326c3779134SOr Gerlitz 	pma_cnt->port_xmit_data = cpu_to_be32((be64_to_cpu(cnt->tx_bytes)>>2));
327c3779134SOr Gerlitz 	pma_cnt->port_rcv_data  = cpu_to_be32((be64_to_cpu(cnt->rx_bytes)>>2));
328c3779134SOr Gerlitz 	pma_cnt->port_xmit_packets = cpu_to_be32(be64_to_cpu(cnt->tx_frames));
329c3779134SOr Gerlitz 	pma_cnt->port_rcv_packets  = cpu_to_be32(be64_to_cpu(cnt->rx_frames));
330c3779134SOr Gerlitz }
331c3779134SOr Gerlitz 
332c3779134SOr Gerlitz static int iboe_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
333c3779134SOr Gerlitz 			struct ib_wc *in_wc, struct ib_grh *in_grh,
334c3779134SOr Gerlitz 			struct ib_mad *in_mad, struct ib_mad *out_mad)
335c3779134SOr Gerlitz {
336c3779134SOr Gerlitz 	struct mlx4_cmd_mailbox *mailbox;
337c3779134SOr Gerlitz 	struct mlx4_ib_dev *dev = to_mdev(ibdev);
338c3779134SOr Gerlitz 	int err;
339c3779134SOr Gerlitz 	u32 inmod = dev->counters[port_num - 1] & 0xffff;
340c3779134SOr Gerlitz 	u8 mode;
341c3779134SOr Gerlitz 
342c3779134SOr Gerlitz 	if (in_mad->mad_hdr.mgmt_class != IB_MGMT_CLASS_PERF_MGMT)
343c3779134SOr Gerlitz 		return -EINVAL;
344c3779134SOr Gerlitz 
345c3779134SOr Gerlitz 	mailbox = mlx4_alloc_cmd_mailbox(dev->dev);
346c3779134SOr Gerlitz 	if (IS_ERR(mailbox))
347c3779134SOr Gerlitz 		return IB_MAD_RESULT_FAILURE;
348c3779134SOr Gerlitz 
349c3779134SOr Gerlitz 	err = mlx4_cmd_box(dev->dev, 0, mailbox->dma, inmod, 0,
350f9baff50SJack Morgenstein 			   MLX4_CMD_QUERY_IF_STAT, MLX4_CMD_TIME_CLASS_C,
351f9baff50SJack Morgenstein 			   MLX4_CMD_WRAPPED);
352c3779134SOr Gerlitz 	if (err)
353c3779134SOr Gerlitz 		err = IB_MAD_RESULT_FAILURE;
354c3779134SOr Gerlitz 	else {
355c3779134SOr Gerlitz 		memset(out_mad->data, 0, sizeof out_mad->data);
356c3779134SOr Gerlitz 		mode = ((struct mlx4_counter *)mailbox->buf)->counter_mode;
357c3779134SOr Gerlitz 		switch (mode & 0xf) {
358c3779134SOr Gerlitz 		case 0:
359c3779134SOr Gerlitz 			edit_counter(mailbox->buf,
360c3779134SOr Gerlitz 						(void *)(out_mad->data + 40));
361c3779134SOr Gerlitz 			err = IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
362c3779134SOr Gerlitz 			break;
363c3779134SOr Gerlitz 		default:
364c3779134SOr Gerlitz 			err = IB_MAD_RESULT_FAILURE;
365c3779134SOr Gerlitz 		}
366c3779134SOr Gerlitz 	}
367c3779134SOr Gerlitz 
368c3779134SOr Gerlitz 	mlx4_free_cmd_mailbox(dev->dev, mailbox);
369c3779134SOr Gerlitz 
370c3779134SOr Gerlitz 	return err;
371c3779134SOr Gerlitz }
372c3779134SOr Gerlitz 
373c3779134SOr Gerlitz int mlx4_ib_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
374c3779134SOr Gerlitz 			struct ib_wc *in_wc, struct ib_grh *in_grh,
375c3779134SOr Gerlitz 			struct ib_mad *in_mad, struct ib_mad *out_mad)
376c3779134SOr Gerlitz {
377c3779134SOr Gerlitz 	switch (rdma_port_get_link_layer(ibdev, port_num)) {
378c3779134SOr Gerlitz 	case IB_LINK_LAYER_INFINIBAND:
379c3779134SOr Gerlitz 		return ib_process_mad(ibdev, mad_flags, port_num, in_wc,
380c3779134SOr Gerlitz 				      in_grh, in_mad, out_mad);
381c3779134SOr Gerlitz 	case IB_LINK_LAYER_ETHERNET:
382c3779134SOr Gerlitz 		return iboe_process_mad(ibdev, mad_flags, port_num, in_wc,
383c3779134SOr Gerlitz 					  in_grh, in_mad, out_mad);
384c3779134SOr Gerlitz 	default:
385c3779134SOr Gerlitz 		return -EINVAL;
386c3779134SOr Gerlitz 	}
387c3779134SOr Gerlitz }
388c3779134SOr Gerlitz 
389225c7b1fSRoland Dreier static void send_handler(struct ib_mad_agent *agent,
390225c7b1fSRoland Dreier 			 struct ib_mad_send_wc *mad_send_wc)
391225c7b1fSRoland Dreier {
392225c7b1fSRoland Dreier 	ib_free_send_mad(mad_send_wc->send_buf);
393225c7b1fSRoland Dreier }
394225c7b1fSRoland Dreier 
395225c7b1fSRoland Dreier int mlx4_ib_mad_init(struct mlx4_ib_dev *dev)
396225c7b1fSRoland Dreier {
397225c7b1fSRoland Dreier 	struct ib_mad_agent *agent;
398225c7b1fSRoland Dreier 	int p, q;
399225c7b1fSRoland Dreier 	int ret;
400fa417f7bSEli Cohen 	enum rdma_link_layer ll;
401225c7b1fSRoland Dreier 
402fa417f7bSEli Cohen 	for (p = 0; p < dev->num_ports; ++p) {
403fa417f7bSEli Cohen 		ll = rdma_port_get_link_layer(&dev->ib_dev, p + 1);
404225c7b1fSRoland Dreier 		for (q = 0; q <= 1; ++q) {
405fa417f7bSEli Cohen 			if (ll == IB_LINK_LAYER_INFINIBAND) {
406225c7b1fSRoland Dreier 				agent = ib_register_mad_agent(&dev->ib_dev, p + 1,
407225c7b1fSRoland Dreier 							      q ? IB_QPT_GSI : IB_QPT_SMI,
408225c7b1fSRoland Dreier 							      NULL, 0, send_handler,
409225c7b1fSRoland Dreier 							      NULL, NULL);
410225c7b1fSRoland Dreier 				if (IS_ERR(agent)) {
411225c7b1fSRoland Dreier 					ret = PTR_ERR(agent);
412225c7b1fSRoland Dreier 					goto err;
413225c7b1fSRoland Dreier 				}
414225c7b1fSRoland Dreier 				dev->send_agent[p][q] = agent;
415fa417f7bSEli Cohen 			} else
416fa417f7bSEli Cohen 				dev->send_agent[p][q] = NULL;
417fa417f7bSEli Cohen 		}
418225c7b1fSRoland Dreier 	}
419225c7b1fSRoland Dreier 
420225c7b1fSRoland Dreier 	return 0;
421225c7b1fSRoland Dreier 
422225c7b1fSRoland Dreier err:
4237ff93f8bSYevgeny Petrilin 	for (p = 0; p < dev->num_ports; ++p)
424225c7b1fSRoland Dreier 		for (q = 0; q <= 1; ++q)
425225c7b1fSRoland Dreier 			if (dev->send_agent[p][q])
426225c7b1fSRoland Dreier 				ib_unregister_mad_agent(dev->send_agent[p][q]);
427225c7b1fSRoland Dreier 
428225c7b1fSRoland Dreier 	return ret;
429225c7b1fSRoland Dreier }
430225c7b1fSRoland Dreier 
431225c7b1fSRoland Dreier void mlx4_ib_mad_cleanup(struct mlx4_ib_dev *dev)
432225c7b1fSRoland Dreier {
433225c7b1fSRoland Dreier 	struct ib_mad_agent *agent;
434225c7b1fSRoland Dreier 	int p, q;
435225c7b1fSRoland Dreier 
4367ff93f8bSYevgeny Petrilin 	for (p = 0; p < dev->num_ports; ++p) {
437225c7b1fSRoland Dreier 		for (q = 0; q <= 1; ++q) {
438225c7b1fSRoland Dreier 			agent = dev->send_agent[p][q];
439fa417f7bSEli Cohen 			if (agent) {
440225c7b1fSRoland Dreier 				dev->send_agent[p][q] = NULL;
441225c7b1fSRoland Dreier 				ib_unregister_mad_agent(agent);
442225c7b1fSRoland Dreier 			}
443fa417f7bSEli Cohen 		}
444225c7b1fSRoland Dreier 
445225c7b1fSRoland Dreier 		if (dev->sm_ah[p])
446225c7b1fSRoland Dreier 			ib_destroy_ah(dev->sm_ah[p]);
447225c7b1fSRoland Dreier 	}
448225c7b1fSRoland Dreier }
449