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