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