xref: /openbmc/linux/drivers/infiniband/sw/rxe/rxe.c (revision 5d0e4d78)
1 /*
2  * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
3  * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
4  *
5  * This software is available to you under a choice of one of two
6  * licenses.  You may choose to be licensed under the terms of the GNU
7  * General Public License (GPL) Version 2, available from the file
8  * COPYING in the main directory of this source tree, or the
9  * OpenIB.org BSD license below:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *	- Redistributions of source code must retain the above
16  *	  copyright notice, this list of conditions and the following
17  *	  disclaimer.
18  *
19  *	- Redistributions in binary form must reproduce the above
20  *	  copyright notice, this list of conditions and the following
21  *	  disclaimer in the documentation and/or other materials
22  *	  provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33 
34 #include <net/addrconf.h>
35 #include "rxe.h"
36 #include "rxe_loc.h"
37 
38 MODULE_AUTHOR("Bob Pearson, Frank Zago, John Groves, Kamal Heib");
39 MODULE_DESCRIPTION("Soft RDMA transport");
40 MODULE_LICENSE("Dual BSD/GPL");
41 MODULE_VERSION("0.2");
42 
43 /* free resources for all ports on a device */
44 static void rxe_cleanup_ports(struct rxe_dev *rxe)
45 {
46 	kfree(rxe->port.pkey_tbl);
47 	rxe->port.pkey_tbl = NULL;
48 
49 }
50 
51 /* free resources for a rxe device all objects created for this device must
52  * have been destroyed
53  */
54 static void rxe_cleanup(struct rxe_dev *rxe)
55 {
56 	rxe_pool_cleanup(&rxe->uc_pool);
57 	rxe_pool_cleanup(&rxe->pd_pool);
58 	rxe_pool_cleanup(&rxe->ah_pool);
59 	rxe_pool_cleanup(&rxe->srq_pool);
60 	rxe_pool_cleanup(&rxe->qp_pool);
61 	rxe_pool_cleanup(&rxe->cq_pool);
62 	rxe_pool_cleanup(&rxe->mr_pool);
63 	rxe_pool_cleanup(&rxe->mw_pool);
64 	rxe_pool_cleanup(&rxe->mc_grp_pool);
65 	rxe_pool_cleanup(&rxe->mc_elem_pool);
66 
67 	rxe_cleanup_ports(rxe);
68 
69 	crypto_free_shash(rxe->tfm);
70 }
71 
72 /* called when all references have been dropped */
73 void rxe_release(struct kref *kref)
74 {
75 	struct rxe_dev *rxe = container_of(kref, struct rxe_dev, ref_cnt);
76 
77 	rxe_cleanup(rxe);
78 	ib_dealloc_device(&rxe->ib_dev);
79 }
80 
81 void rxe_dev_put(struct rxe_dev *rxe)
82 {
83 	kref_put(&rxe->ref_cnt, rxe_release);
84 }
85 EXPORT_SYMBOL_GPL(rxe_dev_put);
86 
87 /* initialize rxe device parameters */
88 static int rxe_init_device_param(struct rxe_dev *rxe)
89 {
90 	rxe->max_inline_data			= RXE_MAX_INLINE_DATA;
91 
92 	rxe->attr.fw_ver			= RXE_FW_VER;
93 	rxe->attr.max_mr_size			= RXE_MAX_MR_SIZE;
94 	rxe->attr.page_size_cap			= RXE_PAGE_SIZE_CAP;
95 	rxe->attr.vendor_id			= RXE_VENDOR_ID;
96 	rxe->attr.vendor_part_id		= RXE_VENDOR_PART_ID;
97 	rxe->attr.hw_ver			= RXE_HW_VER;
98 	rxe->attr.max_qp			= RXE_MAX_QP;
99 	rxe->attr.max_qp_wr			= RXE_MAX_QP_WR;
100 	rxe->attr.device_cap_flags		= RXE_DEVICE_CAP_FLAGS;
101 	rxe->attr.max_sge			= RXE_MAX_SGE;
102 	rxe->attr.max_sge_rd			= RXE_MAX_SGE_RD;
103 	rxe->attr.max_cq			= RXE_MAX_CQ;
104 	rxe->attr.max_cqe			= (1 << RXE_MAX_LOG_CQE) - 1;
105 	rxe->attr.max_mr			= RXE_MAX_MR;
106 	rxe->attr.max_pd			= RXE_MAX_PD;
107 	rxe->attr.max_qp_rd_atom		= RXE_MAX_QP_RD_ATOM;
108 	rxe->attr.max_ee_rd_atom		= RXE_MAX_EE_RD_ATOM;
109 	rxe->attr.max_res_rd_atom		= RXE_MAX_RES_RD_ATOM;
110 	rxe->attr.max_qp_init_rd_atom		= RXE_MAX_QP_INIT_RD_ATOM;
111 	rxe->attr.max_ee_init_rd_atom		= RXE_MAX_EE_INIT_RD_ATOM;
112 	rxe->attr.atomic_cap			= RXE_ATOMIC_CAP;
113 	rxe->attr.max_ee			= RXE_MAX_EE;
114 	rxe->attr.max_rdd			= RXE_MAX_RDD;
115 	rxe->attr.max_mw			= RXE_MAX_MW;
116 	rxe->attr.max_raw_ipv6_qp		= RXE_MAX_RAW_IPV6_QP;
117 	rxe->attr.max_raw_ethy_qp		= RXE_MAX_RAW_ETHY_QP;
118 	rxe->attr.max_mcast_grp			= RXE_MAX_MCAST_GRP;
119 	rxe->attr.max_mcast_qp_attach		= RXE_MAX_MCAST_QP_ATTACH;
120 	rxe->attr.max_total_mcast_qp_attach	= RXE_MAX_TOT_MCAST_QP_ATTACH;
121 	rxe->attr.max_ah			= RXE_MAX_AH;
122 	rxe->attr.max_fmr			= RXE_MAX_FMR;
123 	rxe->attr.max_map_per_fmr		= RXE_MAX_MAP_PER_FMR;
124 	rxe->attr.max_srq			= RXE_MAX_SRQ;
125 	rxe->attr.max_srq_wr			= RXE_MAX_SRQ_WR;
126 	rxe->attr.max_srq_sge			= RXE_MAX_SRQ_SGE;
127 	rxe->attr.max_fast_reg_page_list_len	= RXE_MAX_FMR_PAGE_LIST_LEN;
128 	rxe->attr.max_pkeys			= RXE_MAX_PKEYS;
129 	rxe->attr.local_ca_ack_delay		= RXE_LOCAL_CA_ACK_DELAY;
130 
131 	rxe->max_ucontext			= RXE_MAX_UCONTEXT;
132 
133 	return 0;
134 }
135 
136 /* initialize port attributes */
137 static int rxe_init_port_param(struct rxe_port *port)
138 {
139 	port->attr.state		= RXE_PORT_STATE;
140 	port->attr.max_mtu		= RXE_PORT_MAX_MTU;
141 	port->attr.active_mtu		= RXE_PORT_ACTIVE_MTU;
142 	port->attr.gid_tbl_len		= RXE_PORT_GID_TBL_LEN;
143 	port->attr.port_cap_flags	= RXE_PORT_PORT_CAP_FLAGS;
144 	port->attr.max_msg_sz		= RXE_PORT_MAX_MSG_SZ;
145 	port->attr.bad_pkey_cntr	= RXE_PORT_BAD_PKEY_CNTR;
146 	port->attr.qkey_viol_cntr	= RXE_PORT_QKEY_VIOL_CNTR;
147 	port->attr.pkey_tbl_len		= RXE_PORT_PKEY_TBL_LEN;
148 	port->attr.lid			= RXE_PORT_LID;
149 	port->attr.sm_lid		= RXE_PORT_SM_LID;
150 	port->attr.lmc			= RXE_PORT_LMC;
151 	port->attr.max_vl_num		= RXE_PORT_MAX_VL_NUM;
152 	port->attr.sm_sl		= RXE_PORT_SM_SL;
153 	port->attr.subnet_timeout	= RXE_PORT_SUBNET_TIMEOUT;
154 	port->attr.init_type_reply	= RXE_PORT_INIT_TYPE_REPLY;
155 	port->attr.active_width		= RXE_PORT_ACTIVE_WIDTH;
156 	port->attr.active_speed		= RXE_PORT_ACTIVE_SPEED;
157 	port->attr.phys_state		= RXE_PORT_PHYS_STATE;
158 	port->mtu_cap			=
159 				ib_mtu_enum_to_int(RXE_PORT_ACTIVE_MTU);
160 	port->subnet_prefix		= cpu_to_be64(RXE_PORT_SUBNET_PREFIX);
161 
162 	return 0;
163 }
164 
165 /* initialize port state, note IB convention that HCA ports are always
166  * numbered from 1
167  */
168 static int rxe_init_ports(struct rxe_dev *rxe)
169 {
170 	struct rxe_port *port = &rxe->port;
171 
172 	rxe_init_port_param(port);
173 
174 	if (!port->attr.pkey_tbl_len || !port->attr.gid_tbl_len)
175 		return -EINVAL;
176 
177 	port->pkey_tbl = kcalloc(port->attr.pkey_tbl_len,
178 			sizeof(*port->pkey_tbl), GFP_KERNEL);
179 
180 	if (!port->pkey_tbl)
181 		return -ENOMEM;
182 
183 	port->pkey_tbl[0] = 0xffff;
184 	addrconf_addr_eui48((unsigned char *)&port->port_guid,
185 			    rxe->ndev->dev_addr);
186 
187 	spin_lock_init(&port->port_lock);
188 
189 	return 0;
190 }
191 
192 /* init pools of managed objects */
193 static int rxe_init_pools(struct rxe_dev *rxe)
194 {
195 	int err;
196 
197 	err = rxe_pool_init(rxe, &rxe->uc_pool, RXE_TYPE_UC,
198 			    rxe->max_ucontext);
199 	if (err)
200 		goto err1;
201 
202 	err = rxe_pool_init(rxe, &rxe->pd_pool, RXE_TYPE_PD,
203 			    rxe->attr.max_pd);
204 	if (err)
205 		goto err2;
206 
207 	err = rxe_pool_init(rxe, &rxe->ah_pool, RXE_TYPE_AH,
208 			    rxe->attr.max_ah);
209 	if (err)
210 		goto err3;
211 
212 	err = rxe_pool_init(rxe, &rxe->srq_pool, RXE_TYPE_SRQ,
213 			    rxe->attr.max_srq);
214 	if (err)
215 		goto err4;
216 
217 	err = rxe_pool_init(rxe, &rxe->qp_pool, RXE_TYPE_QP,
218 			    rxe->attr.max_qp);
219 	if (err)
220 		goto err5;
221 
222 	err = rxe_pool_init(rxe, &rxe->cq_pool, RXE_TYPE_CQ,
223 			    rxe->attr.max_cq);
224 	if (err)
225 		goto err6;
226 
227 	err = rxe_pool_init(rxe, &rxe->mr_pool, RXE_TYPE_MR,
228 			    rxe->attr.max_mr);
229 	if (err)
230 		goto err7;
231 
232 	err = rxe_pool_init(rxe, &rxe->mw_pool, RXE_TYPE_MW,
233 			    rxe->attr.max_mw);
234 	if (err)
235 		goto err8;
236 
237 	err = rxe_pool_init(rxe, &rxe->mc_grp_pool, RXE_TYPE_MC_GRP,
238 			    rxe->attr.max_mcast_grp);
239 	if (err)
240 		goto err9;
241 
242 	err = rxe_pool_init(rxe, &rxe->mc_elem_pool, RXE_TYPE_MC_ELEM,
243 			    rxe->attr.max_total_mcast_qp_attach);
244 	if (err)
245 		goto err10;
246 
247 	return 0;
248 
249 err10:
250 	rxe_pool_cleanup(&rxe->mc_grp_pool);
251 err9:
252 	rxe_pool_cleanup(&rxe->mw_pool);
253 err8:
254 	rxe_pool_cleanup(&rxe->mr_pool);
255 err7:
256 	rxe_pool_cleanup(&rxe->cq_pool);
257 err6:
258 	rxe_pool_cleanup(&rxe->qp_pool);
259 err5:
260 	rxe_pool_cleanup(&rxe->srq_pool);
261 err4:
262 	rxe_pool_cleanup(&rxe->ah_pool);
263 err3:
264 	rxe_pool_cleanup(&rxe->pd_pool);
265 err2:
266 	rxe_pool_cleanup(&rxe->uc_pool);
267 err1:
268 	return err;
269 }
270 
271 /* initialize rxe device state */
272 static int rxe_init(struct rxe_dev *rxe)
273 {
274 	int err;
275 
276 	/* init default device parameters */
277 	rxe_init_device_param(rxe);
278 
279 	err = rxe_init_ports(rxe);
280 	if (err)
281 		goto err1;
282 
283 	err = rxe_init_pools(rxe);
284 	if (err)
285 		goto err2;
286 
287 	/* init pending mmap list */
288 	spin_lock_init(&rxe->mmap_offset_lock);
289 	spin_lock_init(&rxe->pending_lock);
290 	INIT_LIST_HEAD(&rxe->pending_mmaps);
291 	INIT_LIST_HEAD(&rxe->list);
292 
293 	mutex_init(&rxe->usdev_lock);
294 
295 	return 0;
296 
297 err2:
298 	rxe_cleanup_ports(rxe);
299 err1:
300 	return err;
301 }
302 
303 int rxe_set_mtu(struct rxe_dev *rxe, unsigned int ndev_mtu)
304 {
305 	struct rxe_port *port = &rxe->port;
306 	enum ib_mtu mtu;
307 
308 	mtu = eth_mtu_int_to_enum(ndev_mtu);
309 
310 	/* Make sure that new MTU in range */
311 	mtu = mtu ? min_t(enum ib_mtu, mtu, RXE_PORT_MAX_MTU) : IB_MTU_256;
312 
313 	port->attr.active_mtu = mtu;
314 	port->mtu_cap = ib_mtu_enum_to_int(mtu);
315 
316 	return 0;
317 }
318 EXPORT_SYMBOL(rxe_set_mtu);
319 
320 /* called by ifc layer to create new rxe device.
321  * The caller should allocate memory for rxe by calling ib_alloc_device.
322  */
323 int rxe_add(struct rxe_dev *rxe, unsigned int mtu)
324 {
325 	int err;
326 
327 	kref_init(&rxe->ref_cnt);
328 
329 	err = rxe_init(rxe);
330 	if (err)
331 		goto err1;
332 
333 	err = rxe_set_mtu(rxe, mtu);
334 	if (err)
335 		goto err1;
336 
337 	err = rxe_register_device(rxe);
338 	if (err)
339 		goto err1;
340 
341 	return 0;
342 
343 err1:
344 	rxe_dev_put(rxe);
345 	return err;
346 }
347 EXPORT_SYMBOL(rxe_add);
348 
349 /* called by the ifc layer to remove a device */
350 void rxe_remove(struct rxe_dev *rxe)
351 {
352 	rxe_unregister_device(rxe);
353 
354 	rxe_dev_put(rxe);
355 }
356 EXPORT_SYMBOL(rxe_remove);
357 
358 static int __init rxe_module_init(void)
359 {
360 	int err;
361 
362 	/* initialize slab caches for managed objects */
363 	err = rxe_cache_init();
364 	if (err) {
365 		pr_err("unable to init object pools\n");
366 		return err;
367 	}
368 
369 	err = rxe_net_init();
370 	if (err)
371 		return err;
372 
373 	pr_info("loaded\n");
374 	return 0;
375 }
376 
377 static void __exit rxe_module_exit(void)
378 {
379 	rxe_remove_all();
380 	rxe_net_exit();
381 	rxe_cache_exit();
382 
383 	pr_info("unloaded\n");
384 }
385 
386 late_initcall(rxe_module_init);
387 module_exit(rxe_module_exit);
388