xref: /openbmc/linux/drivers/infiniband/sw/rxe/rxe.c (revision 015d239a)
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 <rdma/rdma_netlink.h>
35 #include <net/addrconf.h>
36 #include "rxe.h"
37 #include "rxe_loc.h"
38 
39 MODULE_AUTHOR("Bob Pearson, Frank Zago, John Groves, Kamal Heib");
40 MODULE_DESCRIPTION("Soft RDMA transport");
41 MODULE_LICENSE("Dual BSD/GPL");
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 void rxe_dealloc(struct ib_device *ib_dev)
55 {
56 	struct rxe_dev *rxe = container_of(ib_dev, struct rxe_dev, ib_dev);
57 
58 	rxe_pool_cleanup(&rxe->uc_pool);
59 	rxe_pool_cleanup(&rxe->pd_pool);
60 	rxe_pool_cleanup(&rxe->ah_pool);
61 	rxe_pool_cleanup(&rxe->srq_pool);
62 	rxe_pool_cleanup(&rxe->qp_pool);
63 	rxe_pool_cleanup(&rxe->cq_pool);
64 	rxe_pool_cleanup(&rxe->mr_pool);
65 	rxe_pool_cleanup(&rxe->mw_pool);
66 	rxe_pool_cleanup(&rxe->mc_grp_pool);
67 	rxe_pool_cleanup(&rxe->mc_elem_pool);
68 
69 	rxe_cleanup_ports(rxe);
70 
71 	if (rxe->tfm)
72 		crypto_free_shash(rxe->tfm);
73 }
74 
75 /* initialize rxe device parameters */
76 static void rxe_init_device_param(struct rxe_dev *rxe)
77 {
78 	rxe->max_inline_data			= RXE_MAX_INLINE_DATA;
79 
80 	rxe->attr.max_mr_size			= RXE_MAX_MR_SIZE;
81 	rxe->attr.page_size_cap			= RXE_PAGE_SIZE_CAP;
82 	rxe->attr.max_qp			= RXE_MAX_QP;
83 	rxe->attr.max_qp_wr			= RXE_MAX_QP_WR;
84 	rxe->attr.device_cap_flags		= RXE_DEVICE_CAP_FLAGS;
85 	rxe->attr.max_send_sge			= RXE_MAX_SGE;
86 	rxe->attr.max_recv_sge			= RXE_MAX_SGE;
87 	rxe->attr.max_sge_rd			= RXE_MAX_SGE_RD;
88 	rxe->attr.max_cq			= RXE_MAX_CQ;
89 	rxe->attr.max_cqe			= (1 << RXE_MAX_LOG_CQE) - 1;
90 	rxe->attr.max_mr			= RXE_MAX_MR;
91 	rxe->attr.max_pd			= RXE_MAX_PD;
92 	rxe->attr.max_qp_rd_atom		= RXE_MAX_QP_RD_ATOM;
93 	rxe->attr.max_res_rd_atom		= RXE_MAX_RES_RD_ATOM;
94 	rxe->attr.max_qp_init_rd_atom		= RXE_MAX_QP_INIT_RD_ATOM;
95 	rxe->attr.atomic_cap			= IB_ATOMIC_HCA;
96 	rxe->attr.max_mcast_grp			= RXE_MAX_MCAST_GRP;
97 	rxe->attr.max_mcast_qp_attach		= RXE_MAX_MCAST_QP_ATTACH;
98 	rxe->attr.max_total_mcast_qp_attach	= RXE_MAX_TOT_MCAST_QP_ATTACH;
99 	rxe->attr.max_ah			= RXE_MAX_AH;
100 	rxe->attr.max_srq			= RXE_MAX_SRQ;
101 	rxe->attr.max_srq_wr			= RXE_MAX_SRQ_WR;
102 	rxe->attr.max_srq_sge			= RXE_MAX_SRQ_SGE;
103 	rxe->attr.max_fast_reg_page_list_len	= RXE_MAX_FMR_PAGE_LIST_LEN;
104 	rxe->attr.max_pkeys			= RXE_MAX_PKEYS;
105 	rxe->attr.local_ca_ack_delay		= RXE_LOCAL_CA_ACK_DELAY;
106 
107 	rxe->max_ucontext			= RXE_MAX_UCONTEXT;
108 }
109 
110 /* initialize port attributes */
111 static int rxe_init_port_param(struct rxe_port *port)
112 {
113 	port->attr.state		= IB_PORT_DOWN;
114 	port->attr.max_mtu		= IB_MTU_4096;
115 	port->attr.active_mtu		= IB_MTU_256;
116 	port->attr.gid_tbl_len		= RXE_PORT_GID_TBL_LEN;
117 	port->attr.port_cap_flags	= RXE_PORT_PORT_CAP_FLAGS;
118 	port->attr.max_msg_sz		= RXE_PORT_MAX_MSG_SZ;
119 	port->attr.bad_pkey_cntr	= RXE_PORT_BAD_PKEY_CNTR;
120 	port->attr.qkey_viol_cntr	= RXE_PORT_QKEY_VIOL_CNTR;
121 	port->attr.pkey_tbl_len		= RXE_PORT_PKEY_TBL_LEN;
122 	port->attr.lid			= RXE_PORT_LID;
123 	port->attr.sm_lid		= RXE_PORT_SM_LID;
124 	port->attr.lmc			= RXE_PORT_LMC;
125 	port->attr.max_vl_num		= RXE_PORT_MAX_VL_NUM;
126 	port->attr.sm_sl		= RXE_PORT_SM_SL;
127 	port->attr.subnet_timeout	= RXE_PORT_SUBNET_TIMEOUT;
128 	port->attr.init_type_reply	= RXE_PORT_INIT_TYPE_REPLY;
129 	port->attr.active_width		= RXE_PORT_ACTIVE_WIDTH;
130 	port->attr.active_speed		= RXE_PORT_ACTIVE_SPEED;
131 	port->attr.phys_state		= RXE_PORT_PHYS_STATE;
132 	port->mtu_cap			= ib_mtu_enum_to_int(IB_MTU_256);
133 	port->subnet_prefix		= cpu_to_be64(RXE_PORT_SUBNET_PREFIX);
134 
135 	return 0;
136 }
137 
138 /* initialize port state, note IB convention that HCA ports are always
139  * numbered from 1
140  */
141 static int rxe_init_ports(struct rxe_dev *rxe)
142 {
143 	struct rxe_port *port = &rxe->port;
144 
145 	rxe_init_port_param(port);
146 
147 	if (!port->attr.pkey_tbl_len || !port->attr.gid_tbl_len)
148 		return -EINVAL;
149 
150 	port->pkey_tbl = kcalloc(port->attr.pkey_tbl_len,
151 			sizeof(*port->pkey_tbl), GFP_KERNEL);
152 
153 	if (!port->pkey_tbl)
154 		return -ENOMEM;
155 
156 	port->pkey_tbl[0] = 0xffff;
157 	addrconf_addr_eui48((unsigned char *)&port->port_guid,
158 			    rxe->ndev->dev_addr);
159 
160 	spin_lock_init(&port->port_lock);
161 
162 	return 0;
163 }
164 
165 /* init pools of managed objects */
166 static int rxe_init_pools(struct rxe_dev *rxe)
167 {
168 	int err;
169 
170 	err = rxe_pool_init(rxe, &rxe->uc_pool, RXE_TYPE_UC,
171 			    rxe->max_ucontext);
172 	if (err)
173 		goto err1;
174 
175 	err = rxe_pool_init(rxe, &rxe->pd_pool, RXE_TYPE_PD,
176 			    rxe->attr.max_pd);
177 	if (err)
178 		goto err2;
179 
180 	err = rxe_pool_init(rxe, &rxe->ah_pool, RXE_TYPE_AH,
181 			    rxe->attr.max_ah);
182 	if (err)
183 		goto err3;
184 
185 	err = rxe_pool_init(rxe, &rxe->srq_pool, RXE_TYPE_SRQ,
186 			    rxe->attr.max_srq);
187 	if (err)
188 		goto err4;
189 
190 	err = rxe_pool_init(rxe, &rxe->qp_pool, RXE_TYPE_QP,
191 			    rxe->attr.max_qp);
192 	if (err)
193 		goto err5;
194 
195 	err = rxe_pool_init(rxe, &rxe->cq_pool, RXE_TYPE_CQ,
196 			    rxe->attr.max_cq);
197 	if (err)
198 		goto err6;
199 
200 	err = rxe_pool_init(rxe, &rxe->mr_pool, RXE_TYPE_MR,
201 			    rxe->attr.max_mr);
202 	if (err)
203 		goto err7;
204 
205 	err = rxe_pool_init(rxe, &rxe->mw_pool, RXE_TYPE_MW,
206 			    rxe->attr.max_mw);
207 	if (err)
208 		goto err8;
209 
210 	err = rxe_pool_init(rxe, &rxe->mc_grp_pool, RXE_TYPE_MC_GRP,
211 			    rxe->attr.max_mcast_grp);
212 	if (err)
213 		goto err9;
214 
215 	err = rxe_pool_init(rxe, &rxe->mc_elem_pool, RXE_TYPE_MC_ELEM,
216 			    rxe->attr.max_total_mcast_qp_attach);
217 	if (err)
218 		goto err10;
219 
220 	return 0;
221 
222 err10:
223 	rxe_pool_cleanup(&rxe->mc_grp_pool);
224 err9:
225 	rxe_pool_cleanup(&rxe->mw_pool);
226 err8:
227 	rxe_pool_cleanup(&rxe->mr_pool);
228 err7:
229 	rxe_pool_cleanup(&rxe->cq_pool);
230 err6:
231 	rxe_pool_cleanup(&rxe->qp_pool);
232 err5:
233 	rxe_pool_cleanup(&rxe->srq_pool);
234 err4:
235 	rxe_pool_cleanup(&rxe->ah_pool);
236 err3:
237 	rxe_pool_cleanup(&rxe->pd_pool);
238 err2:
239 	rxe_pool_cleanup(&rxe->uc_pool);
240 err1:
241 	return err;
242 }
243 
244 /* initialize rxe device state */
245 static int rxe_init(struct rxe_dev *rxe)
246 {
247 	int err;
248 
249 	/* init default device parameters */
250 	rxe_init_device_param(rxe);
251 
252 	err = rxe_init_ports(rxe);
253 	if (err)
254 		goto err1;
255 
256 	err = rxe_init_pools(rxe);
257 	if (err)
258 		goto err2;
259 
260 	/* init pending mmap list */
261 	spin_lock_init(&rxe->mmap_offset_lock);
262 	spin_lock_init(&rxe->pending_lock);
263 	INIT_LIST_HEAD(&rxe->pending_mmaps);
264 
265 	mutex_init(&rxe->usdev_lock);
266 
267 	return 0;
268 
269 err2:
270 	rxe_cleanup_ports(rxe);
271 err1:
272 	return err;
273 }
274 
275 void rxe_set_mtu(struct rxe_dev *rxe, unsigned int ndev_mtu)
276 {
277 	struct rxe_port *port = &rxe->port;
278 	enum ib_mtu mtu;
279 
280 	mtu = eth_mtu_int_to_enum(ndev_mtu);
281 
282 	/* Make sure that new MTU in range */
283 	mtu = mtu ? min_t(enum ib_mtu, mtu, IB_MTU_4096) : IB_MTU_256;
284 
285 	port->attr.active_mtu = mtu;
286 	port->mtu_cap = ib_mtu_enum_to_int(mtu);
287 }
288 
289 /* called by ifc layer to create new rxe device.
290  * The caller should allocate memory for rxe by calling ib_alloc_device.
291  */
292 int rxe_add(struct rxe_dev *rxe, unsigned int mtu, const char *ibdev_name)
293 {
294 	int err;
295 
296 	err = rxe_init(rxe);
297 	if (err)
298 		return err;
299 
300 	rxe_set_mtu(rxe, mtu);
301 
302 	return rxe_register_device(rxe, ibdev_name);
303 }
304 
305 static int rxe_newlink(const char *ibdev_name, struct net_device *ndev)
306 {
307 	struct rxe_dev *exists;
308 	int err = 0;
309 
310 	exists = rxe_get_dev_from_net(ndev);
311 	if (exists) {
312 		ib_device_put(&exists->ib_dev);
313 		pr_err("already configured on %s\n", ndev->name);
314 		err = -EEXIST;
315 		goto err;
316 	}
317 
318 	err = rxe_net_add(ibdev_name, ndev);
319 	if (err) {
320 		pr_err("failed to add %s\n", ndev->name);
321 		goto err;
322 	}
323 err:
324 	return err;
325 }
326 
327 static struct rdma_link_ops rxe_link_ops = {
328 	.type = "rxe",
329 	.newlink = rxe_newlink,
330 };
331 
332 static int __init rxe_module_init(void)
333 {
334 	int err;
335 
336 	/* initialize slab caches for managed objects */
337 	err = rxe_cache_init();
338 	if (err) {
339 		pr_err("unable to init object pools\n");
340 		return err;
341 	}
342 
343 	err = rxe_net_init();
344 	if (err)
345 		return err;
346 
347 	rdma_link_register(&rxe_link_ops);
348 	pr_info("loaded\n");
349 	return 0;
350 }
351 
352 static void __exit rxe_module_exit(void)
353 {
354 	rdma_link_unregister(&rxe_link_ops);
355 	ib_unregister_driver(RDMA_DRIVER_RXE);
356 	rxe_net_exit();
357 	rxe_cache_exit();
358 
359 	pr_info("unloaded\n");
360 }
361 
362 late_initcall(rxe_module_init);
363 module_exit(rxe_module_exit);
364 
365 MODULE_ALIAS_RDMA_LINK("rxe");
366