xref: /OK3568_Linux_fs/kernel/drivers/infiniband/sw/rdmavt/mad.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright(c) 2016 Intel Corporation.
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * This file is provided under a dual BSD/GPLv2 license.  When using or
5*4882a593Smuzhiyun  * redistributing this file, you may do so under either license.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * GPL LICENSE SUMMARY
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * This program is free software; you can redistribute it and/or modify
10*4882a593Smuzhiyun  * it under the terms of version 2 of the GNU General Public License as
11*4882a593Smuzhiyun  * published by the Free Software Foundation.
12*4882a593Smuzhiyun  *
13*4882a593Smuzhiyun  * This program is distributed in the hope that it will be useful, but
14*4882a593Smuzhiyun  * WITHOUT ANY WARRANTY; without even the implied warranty of
15*4882a593Smuzhiyun  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16*4882a593Smuzhiyun  * General Public License for more details.
17*4882a593Smuzhiyun  *
18*4882a593Smuzhiyun  * BSD LICENSE
19*4882a593Smuzhiyun  *
20*4882a593Smuzhiyun  * Redistribution and use in source and binary forms, with or without
21*4882a593Smuzhiyun  * modification, are permitted provided that the following conditions
22*4882a593Smuzhiyun  * are met:
23*4882a593Smuzhiyun  *
24*4882a593Smuzhiyun  *  - Redistributions of source code must retain the above copyright
25*4882a593Smuzhiyun  *    notice, this list of conditions and the following disclaimer.
26*4882a593Smuzhiyun  *  - Redistributions in binary form must reproduce the above copyright
27*4882a593Smuzhiyun  *    notice, this list of conditions and the following disclaimer in
28*4882a593Smuzhiyun  *    the documentation and/or other materials provided with the
29*4882a593Smuzhiyun  *    distribution.
30*4882a593Smuzhiyun  *  - Neither the name of Intel Corporation nor the names of its
31*4882a593Smuzhiyun  *    contributors may be used to endorse or promote products derived
32*4882a593Smuzhiyun  *    from this software without specific prior written permission.
33*4882a593Smuzhiyun  *
34*4882a593Smuzhiyun  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35*4882a593Smuzhiyun  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36*4882a593Smuzhiyun  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37*4882a593Smuzhiyun  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38*4882a593Smuzhiyun  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39*4882a593Smuzhiyun  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40*4882a593Smuzhiyun  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41*4882a593Smuzhiyun  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42*4882a593Smuzhiyun  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43*4882a593Smuzhiyun  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44*4882a593Smuzhiyun  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45*4882a593Smuzhiyun  *
46*4882a593Smuzhiyun  */
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun #include <rdma/ib_mad.h>
49*4882a593Smuzhiyun #include "mad.h"
50*4882a593Smuzhiyun #include "vt.h"
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun /**
53*4882a593Smuzhiyun  * rvt_process_mad - process an incoming MAD packet
54*4882a593Smuzhiyun  * @ibdev: the infiniband device this packet came in on
55*4882a593Smuzhiyun  * @mad_flags: MAD flags
56*4882a593Smuzhiyun  * @port_num: the port number this packet came in on, 1 based from ib core
57*4882a593Smuzhiyun  * @in_wc: the work completion entry for this packet
58*4882a593Smuzhiyun  * @in_grh: the global route header for this packet
59*4882a593Smuzhiyun  * @in_mad: the incoming MAD
60*4882a593Smuzhiyun  * @out_mad: any outgoing MAD reply
61*4882a593Smuzhiyun  *
62*4882a593Smuzhiyun  * Note that the verbs framework has already done the MAD sanity checks,
63*4882a593Smuzhiyun  * and hop count/pointer updating for IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
64*4882a593Smuzhiyun  * MADs.
65*4882a593Smuzhiyun  *
66*4882a593Smuzhiyun  * This is called by the ib_mad module.
67*4882a593Smuzhiyun  *
68*4882a593Smuzhiyun  * Return: IB_MAD_RESULT_SUCCESS or error
69*4882a593Smuzhiyun  */
rvt_process_mad(struct ib_device * ibdev,int mad_flags,u8 port_num,const struct ib_wc * in_wc,const struct ib_grh * in_grh,const struct ib_mad_hdr * in,size_t in_mad_size,struct ib_mad_hdr * out,size_t * out_mad_size,u16 * out_mad_pkey_index)70*4882a593Smuzhiyun int rvt_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
71*4882a593Smuzhiyun 		    const struct ib_wc *in_wc, const struct ib_grh *in_grh,
72*4882a593Smuzhiyun 		    const struct ib_mad_hdr *in, size_t in_mad_size,
73*4882a593Smuzhiyun 		    struct ib_mad_hdr *out, size_t *out_mad_size,
74*4882a593Smuzhiyun 		    u16 *out_mad_pkey_index)
75*4882a593Smuzhiyun {
76*4882a593Smuzhiyun 	/*
77*4882a593Smuzhiyun 	 * MAD processing is quite different between hfi1 and qib. Therefore
78*4882a593Smuzhiyun 	 * this is expected to be provided by the driver. Other drivers in the
79*4882a593Smuzhiyun 	 * future may choose to implement this but it should not be made into a
80*4882a593Smuzhiyun 	 * requirement.
81*4882a593Smuzhiyun 	 */
82*4882a593Smuzhiyun 	if (ibport_num_to_idx(ibdev, port_num) < 0)
83*4882a593Smuzhiyun 		return -EINVAL;
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 	return IB_MAD_RESULT_FAILURE;
86*4882a593Smuzhiyun }
87*4882a593Smuzhiyun 
rvt_send_mad_handler(struct ib_mad_agent * agent,struct ib_mad_send_wc * mad_send_wc)88*4882a593Smuzhiyun static void rvt_send_mad_handler(struct ib_mad_agent *agent,
89*4882a593Smuzhiyun 				 struct ib_mad_send_wc *mad_send_wc)
90*4882a593Smuzhiyun {
91*4882a593Smuzhiyun 	ib_free_send_mad(mad_send_wc->send_buf);
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun /**
95*4882a593Smuzhiyun  * rvt_create_mad_agents - create mad agents
96*4882a593Smuzhiyun  * @rdi: rvt dev struct
97*4882a593Smuzhiyun  *
98*4882a593Smuzhiyun  * If driver needs to be notified of mad agent creation then call back
99*4882a593Smuzhiyun  *
100*4882a593Smuzhiyun  * Return 0 on success
101*4882a593Smuzhiyun  */
rvt_create_mad_agents(struct rvt_dev_info * rdi)102*4882a593Smuzhiyun int rvt_create_mad_agents(struct rvt_dev_info *rdi)
103*4882a593Smuzhiyun {
104*4882a593Smuzhiyun 	struct ib_mad_agent *agent;
105*4882a593Smuzhiyun 	struct rvt_ibport *rvp;
106*4882a593Smuzhiyun 	int p;
107*4882a593Smuzhiyun 	int ret;
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 	for (p = 0; p < rdi->dparms.nports; p++) {
110*4882a593Smuzhiyun 		rvp = rdi->ports[p];
111*4882a593Smuzhiyun 		agent = ib_register_mad_agent(&rdi->ibdev, p + 1,
112*4882a593Smuzhiyun 					      IB_QPT_SMI,
113*4882a593Smuzhiyun 					      NULL, 0, rvt_send_mad_handler,
114*4882a593Smuzhiyun 					      NULL, NULL, 0);
115*4882a593Smuzhiyun 		if (IS_ERR(agent)) {
116*4882a593Smuzhiyun 			ret = PTR_ERR(agent);
117*4882a593Smuzhiyun 			goto err;
118*4882a593Smuzhiyun 		}
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 		rvp->send_agent = agent;
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun 		if (rdi->driver_f.notify_create_mad_agent)
123*4882a593Smuzhiyun 			rdi->driver_f.notify_create_mad_agent(rdi, p);
124*4882a593Smuzhiyun 	}
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun 	return 0;
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun err:
129*4882a593Smuzhiyun 	for (p = 0; p < rdi->dparms.nports; p++) {
130*4882a593Smuzhiyun 		rvp = rdi->ports[p];
131*4882a593Smuzhiyun 		if (rvp->send_agent) {
132*4882a593Smuzhiyun 			agent = rvp->send_agent;
133*4882a593Smuzhiyun 			rvp->send_agent = NULL;
134*4882a593Smuzhiyun 			ib_unregister_mad_agent(agent);
135*4882a593Smuzhiyun 			if (rdi->driver_f.notify_free_mad_agent)
136*4882a593Smuzhiyun 				rdi->driver_f.notify_free_mad_agent(rdi, p);
137*4882a593Smuzhiyun 		}
138*4882a593Smuzhiyun 	}
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun 	return ret;
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun /**
144*4882a593Smuzhiyun  * rvt_free_mad_agents - free up mad agents
145*4882a593Smuzhiyun  * @rdi: rvt dev struct
146*4882a593Smuzhiyun  *
147*4882a593Smuzhiyun  * If driver needs notification of mad agent removal make the call back
148*4882a593Smuzhiyun  */
rvt_free_mad_agents(struct rvt_dev_info * rdi)149*4882a593Smuzhiyun void rvt_free_mad_agents(struct rvt_dev_info *rdi)
150*4882a593Smuzhiyun {
151*4882a593Smuzhiyun 	struct ib_mad_agent *agent;
152*4882a593Smuzhiyun 	struct rvt_ibport *rvp;
153*4882a593Smuzhiyun 	int p;
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 	for (p = 0; p < rdi->dparms.nports; p++) {
156*4882a593Smuzhiyun 		rvp = rdi->ports[p];
157*4882a593Smuzhiyun 		if (rvp->send_agent) {
158*4882a593Smuzhiyun 			agent = rvp->send_agent;
159*4882a593Smuzhiyun 			rvp->send_agent = NULL;
160*4882a593Smuzhiyun 			ib_unregister_mad_agent(agent);
161*4882a593Smuzhiyun 		}
162*4882a593Smuzhiyun 		if (rvp->sm_ah) {
163*4882a593Smuzhiyun 			rdma_destroy_ah(&rvp->sm_ah->ibah,
164*4882a593Smuzhiyun 					RDMA_DESTROY_AH_SLEEPABLE);
165*4882a593Smuzhiyun 			rvp->sm_ah = NULL;
166*4882a593Smuzhiyun 		}
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun 		if (rdi->driver_f.notify_free_mad_agent)
169*4882a593Smuzhiyun 			rdi->driver_f.notify_free_mad_agent(rdi, p);
170*4882a593Smuzhiyun 	}
171*4882a593Smuzhiyun }
172*4882a593Smuzhiyun 
173