xref: /OK3568_Linux_fs/kernel/drivers/infiniband/core/agent.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright (c) 2004, 2005 Mellanox Technologies Ltd.  All rights reserved.
3*4882a593Smuzhiyun  * Copyright (c) 2004, 2005 Infinicon Corporation.  All rights reserved.
4*4882a593Smuzhiyun  * Copyright (c) 2004, 2005 Intel Corporation.  All rights reserved.
5*4882a593Smuzhiyun  * Copyright (c) 2004, 2005 Topspin Corporation.  All rights reserved.
6*4882a593Smuzhiyun  * Copyright (c) 2004-2007 Voltaire Corporation.  All rights reserved.
7*4882a593Smuzhiyun  * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * This software is available to you under a choice of one of two
10*4882a593Smuzhiyun  * licenses.  You may choose to be licensed under the terms of the GNU
11*4882a593Smuzhiyun  * General Public License (GPL) Version 2, available from the file
12*4882a593Smuzhiyun  * COPYING in the main directory of this source tree, or the
13*4882a593Smuzhiyun  * OpenIB.org BSD license below:
14*4882a593Smuzhiyun  *
15*4882a593Smuzhiyun  *     Redistribution and use in source and binary forms, with or
16*4882a593Smuzhiyun  *     without modification, are permitted provided that the following
17*4882a593Smuzhiyun  *     conditions are met:
18*4882a593Smuzhiyun  *
19*4882a593Smuzhiyun  *      - Redistributions of source code must retain the above
20*4882a593Smuzhiyun  *        copyright notice, this list of conditions and the following
21*4882a593Smuzhiyun  *        disclaimer.
22*4882a593Smuzhiyun  *
23*4882a593Smuzhiyun  *      - Redistributions in binary form must reproduce the above
24*4882a593Smuzhiyun  *        copyright notice, this list of conditions and the following
25*4882a593Smuzhiyun  *        disclaimer in the documentation and/or other materials
26*4882a593Smuzhiyun  *        provided with the distribution.
27*4882a593Smuzhiyun  *
28*4882a593Smuzhiyun  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29*4882a593Smuzhiyun  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30*4882a593Smuzhiyun  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31*4882a593Smuzhiyun  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
32*4882a593Smuzhiyun  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
33*4882a593Smuzhiyun  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34*4882a593Smuzhiyun  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35*4882a593Smuzhiyun  * SOFTWARE.
36*4882a593Smuzhiyun  *
37*4882a593Smuzhiyun  */
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun #include <linux/slab.h>
40*4882a593Smuzhiyun #include <linux/string.h>
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun #include "agent.h"
43*4882a593Smuzhiyun #include "smi.h"
44*4882a593Smuzhiyun #include "mad_priv.h"
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun #define SPFX "ib_agent: "
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun struct ib_agent_port_private {
49*4882a593Smuzhiyun 	struct list_head port_list;
50*4882a593Smuzhiyun 	struct ib_mad_agent *agent[2];
51*4882a593Smuzhiyun };
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun static DEFINE_SPINLOCK(ib_agent_port_list_lock);
54*4882a593Smuzhiyun static LIST_HEAD(ib_agent_port_list);
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun static struct ib_agent_port_private *
__ib_get_agent_port(const struct ib_device * device,int port_num)57*4882a593Smuzhiyun __ib_get_agent_port(const struct ib_device *device, int port_num)
58*4882a593Smuzhiyun {
59*4882a593Smuzhiyun 	struct ib_agent_port_private *entry;
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun 	list_for_each_entry(entry, &ib_agent_port_list, port_list) {
62*4882a593Smuzhiyun 		if (entry->agent[1]->device == device &&
63*4882a593Smuzhiyun 		    entry->agent[1]->port_num == port_num)
64*4882a593Smuzhiyun 			return entry;
65*4882a593Smuzhiyun 	}
66*4882a593Smuzhiyun 	return NULL;
67*4882a593Smuzhiyun }
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun static struct ib_agent_port_private *
ib_get_agent_port(const struct ib_device * device,int port_num)70*4882a593Smuzhiyun ib_get_agent_port(const struct ib_device *device, int port_num)
71*4882a593Smuzhiyun {
72*4882a593Smuzhiyun 	struct ib_agent_port_private *entry;
73*4882a593Smuzhiyun 	unsigned long flags;
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun 	spin_lock_irqsave(&ib_agent_port_list_lock, flags);
76*4882a593Smuzhiyun 	entry = __ib_get_agent_port(device, port_num);
77*4882a593Smuzhiyun 	spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
78*4882a593Smuzhiyun 	return entry;
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun 
agent_send_response(const struct ib_mad_hdr * mad_hdr,const struct ib_grh * grh,const struct ib_wc * wc,const struct ib_device * device,int port_num,int qpn,size_t resp_mad_len,bool opa)81*4882a593Smuzhiyun void agent_send_response(const struct ib_mad_hdr *mad_hdr, const struct ib_grh *grh,
82*4882a593Smuzhiyun 			 const struct ib_wc *wc, const struct ib_device *device,
83*4882a593Smuzhiyun 			 int port_num, int qpn, size_t resp_mad_len, bool opa)
84*4882a593Smuzhiyun {
85*4882a593Smuzhiyun 	struct ib_agent_port_private *port_priv;
86*4882a593Smuzhiyun 	struct ib_mad_agent *agent;
87*4882a593Smuzhiyun 	struct ib_mad_send_buf *send_buf;
88*4882a593Smuzhiyun 	struct ib_ah *ah;
89*4882a593Smuzhiyun 	struct ib_mad_send_wr_private *mad_send_wr;
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 	if (rdma_cap_ib_switch(device))
92*4882a593Smuzhiyun 		port_priv = ib_get_agent_port(device, 0);
93*4882a593Smuzhiyun 	else
94*4882a593Smuzhiyun 		port_priv = ib_get_agent_port(device, port_num);
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun 	if (!port_priv) {
97*4882a593Smuzhiyun 		dev_err(&device->dev, "Unable to find port agent\n");
98*4882a593Smuzhiyun 		return;
99*4882a593Smuzhiyun 	}
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 	agent = port_priv->agent[qpn];
102*4882a593Smuzhiyun 	ah = ib_create_ah_from_wc(agent->qp->pd, wc, grh, port_num);
103*4882a593Smuzhiyun 	if (IS_ERR(ah)) {
104*4882a593Smuzhiyun 		dev_err(&device->dev, "ib_create_ah_from_wc error %ld\n",
105*4882a593Smuzhiyun 			PTR_ERR(ah));
106*4882a593Smuzhiyun 		return;
107*4882a593Smuzhiyun 	}
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 	if (opa && mad_hdr->base_version != OPA_MGMT_BASE_VERSION)
110*4882a593Smuzhiyun 		resp_mad_len = IB_MGMT_MAD_SIZE;
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 	send_buf = ib_create_send_mad(agent, wc->src_qp, wc->pkey_index, 0,
113*4882a593Smuzhiyun 				      IB_MGMT_MAD_HDR,
114*4882a593Smuzhiyun 				      resp_mad_len - IB_MGMT_MAD_HDR,
115*4882a593Smuzhiyun 				      GFP_KERNEL,
116*4882a593Smuzhiyun 				      mad_hdr->base_version);
117*4882a593Smuzhiyun 	if (IS_ERR(send_buf)) {
118*4882a593Smuzhiyun 		dev_err(&device->dev, "ib_create_send_mad error\n");
119*4882a593Smuzhiyun 		goto err1;
120*4882a593Smuzhiyun 	}
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun 	memcpy(send_buf->mad, mad_hdr, resp_mad_len);
123*4882a593Smuzhiyun 	send_buf->ah = ah;
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun 	if (rdma_cap_ib_switch(device)) {
126*4882a593Smuzhiyun 		mad_send_wr = container_of(send_buf,
127*4882a593Smuzhiyun 					   struct ib_mad_send_wr_private,
128*4882a593Smuzhiyun 					   send_buf);
129*4882a593Smuzhiyun 		mad_send_wr->send_wr.port_num = port_num;
130*4882a593Smuzhiyun 	}
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 	if (ib_post_send_mad(send_buf, NULL)) {
133*4882a593Smuzhiyun 		dev_err(&device->dev, "ib_post_send_mad error\n");
134*4882a593Smuzhiyun 		goto err2;
135*4882a593Smuzhiyun 	}
136*4882a593Smuzhiyun 	return;
137*4882a593Smuzhiyun err2:
138*4882a593Smuzhiyun 	ib_free_send_mad(send_buf);
139*4882a593Smuzhiyun err1:
140*4882a593Smuzhiyun 	rdma_destroy_ah(ah, RDMA_DESTROY_AH_SLEEPABLE);
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun 
agent_send_handler(struct ib_mad_agent * mad_agent,struct ib_mad_send_wc * mad_send_wc)143*4882a593Smuzhiyun static void agent_send_handler(struct ib_mad_agent *mad_agent,
144*4882a593Smuzhiyun 			       struct ib_mad_send_wc *mad_send_wc)
145*4882a593Smuzhiyun {
146*4882a593Smuzhiyun 	rdma_destroy_ah(mad_send_wc->send_buf->ah, RDMA_DESTROY_AH_SLEEPABLE);
147*4882a593Smuzhiyun 	ib_free_send_mad(mad_send_wc->send_buf);
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun 
ib_agent_port_open(struct ib_device * device,int port_num)150*4882a593Smuzhiyun int ib_agent_port_open(struct ib_device *device, int port_num)
151*4882a593Smuzhiyun {
152*4882a593Smuzhiyun 	struct ib_agent_port_private *port_priv;
153*4882a593Smuzhiyun 	unsigned long flags;
154*4882a593Smuzhiyun 	int ret;
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 	/* Create new device info */
157*4882a593Smuzhiyun 	port_priv = kzalloc(sizeof *port_priv, GFP_KERNEL);
158*4882a593Smuzhiyun 	if (!port_priv) {
159*4882a593Smuzhiyun 		ret = -ENOMEM;
160*4882a593Smuzhiyun 		goto error1;
161*4882a593Smuzhiyun 	}
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	if (rdma_cap_ib_smi(device, port_num)) {
164*4882a593Smuzhiyun 		/* Obtain send only MAD agent for SMI QP */
165*4882a593Smuzhiyun 		port_priv->agent[0] = ib_register_mad_agent(device, port_num,
166*4882a593Smuzhiyun 							    IB_QPT_SMI, NULL, 0,
167*4882a593Smuzhiyun 							    &agent_send_handler,
168*4882a593Smuzhiyun 							    NULL, NULL, 0);
169*4882a593Smuzhiyun 		if (IS_ERR(port_priv->agent[0])) {
170*4882a593Smuzhiyun 			ret = PTR_ERR(port_priv->agent[0]);
171*4882a593Smuzhiyun 			goto error2;
172*4882a593Smuzhiyun 		}
173*4882a593Smuzhiyun 	}
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun 	/* Obtain send only MAD agent for GSI QP */
176*4882a593Smuzhiyun 	port_priv->agent[1] = ib_register_mad_agent(device, port_num,
177*4882a593Smuzhiyun 						    IB_QPT_GSI, NULL, 0,
178*4882a593Smuzhiyun 						    &agent_send_handler,
179*4882a593Smuzhiyun 						    NULL, NULL, 0);
180*4882a593Smuzhiyun 	if (IS_ERR(port_priv->agent[1])) {
181*4882a593Smuzhiyun 		ret = PTR_ERR(port_priv->agent[1]);
182*4882a593Smuzhiyun 		goto error3;
183*4882a593Smuzhiyun 	}
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 	spin_lock_irqsave(&ib_agent_port_list_lock, flags);
186*4882a593Smuzhiyun 	list_add_tail(&port_priv->port_list, &ib_agent_port_list);
187*4882a593Smuzhiyun 	spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 	return 0;
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun error3:
192*4882a593Smuzhiyun 	if (port_priv->agent[0])
193*4882a593Smuzhiyun 		ib_unregister_mad_agent(port_priv->agent[0]);
194*4882a593Smuzhiyun error2:
195*4882a593Smuzhiyun 	kfree(port_priv);
196*4882a593Smuzhiyun error1:
197*4882a593Smuzhiyun 	return ret;
198*4882a593Smuzhiyun }
199*4882a593Smuzhiyun 
ib_agent_port_close(struct ib_device * device,int port_num)200*4882a593Smuzhiyun int ib_agent_port_close(struct ib_device *device, int port_num)
201*4882a593Smuzhiyun {
202*4882a593Smuzhiyun 	struct ib_agent_port_private *port_priv;
203*4882a593Smuzhiyun 	unsigned long flags;
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun 	spin_lock_irqsave(&ib_agent_port_list_lock, flags);
206*4882a593Smuzhiyun 	port_priv = __ib_get_agent_port(device, port_num);
207*4882a593Smuzhiyun 	if (port_priv == NULL) {
208*4882a593Smuzhiyun 		spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
209*4882a593Smuzhiyun 		dev_err(&device->dev, "Port %d not found\n", port_num);
210*4882a593Smuzhiyun 		return -ENODEV;
211*4882a593Smuzhiyun 	}
212*4882a593Smuzhiyun 	list_del(&port_priv->port_list);
213*4882a593Smuzhiyun 	spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun 	ib_unregister_mad_agent(port_priv->agent[1]);
216*4882a593Smuzhiyun 	if (port_priv->agent[0])
217*4882a593Smuzhiyun 		ib_unregister_mad_agent(port_priv->agent[0]);
218*4882a593Smuzhiyun 
219*4882a593Smuzhiyun 	kfree(port_priv);
220*4882a593Smuzhiyun 	return 0;
221*4882a593Smuzhiyun }
222