xref: /openbmc/linux/drivers/infiniband/sw/rxe/rxe.c (revision 8dda2eac)
1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /*
3  * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
4  * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
5  */
6 
7 #include <rdma/rdma_netlink.h>
8 #include <net/addrconf.h>
9 #include "rxe.h"
10 #include "rxe_loc.h"
11 
12 MODULE_AUTHOR("Bob Pearson, Frank Zago, John Groves, Kamal Heib");
13 MODULE_DESCRIPTION("Soft RDMA transport");
14 MODULE_LICENSE("Dual BSD/GPL");
15 
16 bool rxe_initialized;
17 
18 /* free resources for a rxe device all objects created for this device must
19  * have been destroyed
20  */
21 void rxe_dealloc(struct ib_device *ib_dev)
22 {
23 	struct rxe_dev *rxe = container_of(ib_dev, struct rxe_dev, ib_dev);
24 
25 	rxe_pool_cleanup(&rxe->uc_pool);
26 	rxe_pool_cleanup(&rxe->pd_pool);
27 	rxe_pool_cleanup(&rxe->ah_pool);
28 	rxe_pool_cleanup(&rxe->srq_pool);
29 	rxe_pool_cleanup(&rxe->qp_pool);
30 	rxe_pool_cleanup(&rxe->cq_pool);
31 	rxe_pool_cleanup(&rxe->mr_pool);
32 	rxe_pool_cleanup(&rxe->mw_pool);
33 	rxe_pool_cleanup(&rxe->mc_grp_pool);
34 	rxe_pool_cleanup(&rxe->mc_elem_pool);
35 
36 	if (rxe->tfm)
37 		crypto_free_shash(rxe->tfm);
38 }
39 
40 /* initialize rxe device parameters */
41 static void rxe_init_device_param(struct rxe_dev *rxe)
42 {
43 	rxe->max_inline_data			= RXE_MAX_INLINE_DATA;
44 
45 	rxe->attr.vendor_id			= RXE_VENDOR_ID;
46 	rxe->attr.max_mr_size			= RXE_MAX_MR_SIZE;
47 	rxe->attr.page_size_cap			= RXE_PAGE_SIZE_CAP;
48 	rxe->attr.max_qp			= RXE_MAX_QP;
49 	rxe->attr.max_qp_wr			= RXE_MAX_QP_WR;
50 	rxe->attr.device_cap_flags		= RXE_DEVICE_CAP_FLAGS;
51 	rxe->attr.max_send_sge			= RXE_MAX_SGE;
52 	rxe->attr.max_recv_sge			= RXE_MAX_SGE;
53 	rxe->attr.max_sge_rd			= RXE_MAX_SGE_RD;
54 	rxe->attr.max_cq			= RXE_MAX_CQ;
55 	rxe->attr.max_cqe			= (1 << RXE_MAX_LOG_CQE) - 1;
56 	rxe->attr.max_mr			= RXE_MAX_MR;
57 	rxe->attr.max_mw			= RXE_MAX_MW;
58 	rxe->attr.max_pd			= RXE_MAX_PD;
59 	rxe->attr.max_qp_rd_atom		= RXE_MAX_QP_RD_ATOM;
60 	rxe->attr.max_res_rd_atom		= RXE_MAX_RES_RD_ATOM;
61 	rxe->attr.max_qp_init_rd_atom		= RXE_MAX_QP_INIT_RD_ATOM;
62 	rxe->attr.atomic_cap			= IB_ATOMIC_HCA;
63 	rxe->attr.max_mcast_grp			= RXE_MAX_MCAST_GRP;
64 	rxe->attr.max_mcast_qp_attach		= RXE_MAX_MCAST_QP_ATTACH;
65 	rxe->attr.max_total_mcast_qp_attach	= RXE_MAX_TOT_MCAST_QP_ATTACH;
66 	rxe->attr.max_ah			= RXE_MAX_AH;
67 	rxe->attr.max_srq			= RXE_MAX_SRQ;
68 	rxe->attr.max_srq_wr			= RXE_MAX_SRQ_WR;
69 	rxe->attr.max_srq_sge			= RXE_MAX_SRQ_SGE;
70 	rxe->attr.max_fast_reg_page_list_len	= RXE_MAX_FMR_PAGE_LIST_LEN;
71 	rxe->attr.max_pkeys			= RXE_MAX_PKEYS;
72 	rxe->attr.local_ca_ack_delay		= RXE_LOCAL_CA_ACK_DELAY;
73 	addrconf_addr_eui48((unsigned char *)&rxe->attr.sys_image_guid,
74 			rxe->ndev->dev_addr);
75 
76 	rxe->max_ucontext			= RXE_MAX_UCONTEXT;
77 }
78 
79 /* initialize port attributes */
80 static void rxe_init_port_param(struct rxe_port *port)
81 {
82 	port->attr.state		= IB_PORT_DOWN;
83 	port->attr.max_mtu		= IB_MTU_4096;
84 	port->attr.active_mtu		= IB_MTU_256;
85 	port->attr.gid_tbl_len		= RXE_PORT_GID_TBL_LEN;
86 	port->attr.port_cap_flags	= RXE_PORT_PORT_CAP_FLAGS;
87 	port->attr.max_msg_sz		= RXE_PORT_MAX_MSG_SZ;
88 	port->attr.bad_pkey_cntr	= RXE_PORT_BAD_PKEY_CNTR;
89 	port->attr.qkey_viol_cntr	= RXE_PORT_QKEY_VIOL_CNTR;
90 	port->attr.pkey_tbl_len		= RXE_PORT_PKEY_TBL_LEN;
91 	port->attr.lid			= RXE_PORT_LID;
92 	port->attr.sm_lid		= RXE_PORT_SM_LID;
93 	port->attr.lmc			= RXE_PORT_LMC;
94 	port->attr.max_vl_num		= RXE_PORT_MAX_VL_NUM;
95 	port->attr.sm_sl		= RXE_PORT_SM_SL;
96 	port->attr.subnet_timeout	= RXE_PORT_SUBNET_TIMEOUT;
97 	port->attr.init_type_reply	= RXE_PORT_INIT_TYPE_REPLY;
98 	port->attr.active_width		= RXE_PORT_ACTIVE_WIDTH;
99 	port->attr.active_speed		= RXE_PORT_ACTIVE_SPEED;
100 	port->attr.phys_state		= RXE_PORT_PHYS_STATE;
101 	port->mtu_cap			= ib_mtu_enum_to_int(IB_MTU_256);
102 	port->subnet_prefix		= cpu_to_be64(RXE_PORT_SUBNET_PREFIX);
103 }
104 
105 /* initialize port state, note IB convention that HCA ports are always
106  * numbered from 1
107  */
108 static void rxe_init_ports(struct rxe_dev *rxe)
109 {
110 	struct rxe_port *port = &rxe->port;
111 
112 	rxe_init_port_param(port);
113 	addrconf_addr_eui48((unsigned char *)&port->port_guid,
114 			    rxe->ndev->dev_addr);
115 	spin_lock_init(&port->port_lock);
116 }
117 
118 /* init pools of managed objects */
119 static int rxe_init_pools(struct rxe_dev *rxe)
120 {
121 	int err;
122 
123 	err = rxe_pool_init(rxe, &rxe->uc_pool, RXE_TYPE_UC,
124 			    rxe->max_ucontext);
125 	if (err)
126 		goto err1;
127 
128 	err = rxe_pool_init(rxe, &rxe->pd_pool, RXE_TYPE_PD,
129 			    rxe->attr.max_pd);
130 	if (err)
131 		goto err2;
132 
133 	err = rxe_pool_init(rxe, &rxe->ah_pool, RXE_TYPE_AH,
134 			    rxe->attr.max_ah);
135 	if (err)
136 		goto err3;
137 
138 	err = rxe_pool_init(rxe, &rxe->srq_pool, RXE_TYPE_SRQ,
139 			    rxe->attr.max_srq);
140 	if (err)
141 		goto err4;
142 
143 	err = rxe_pool_init(rxe, &rxe->qp_pool, RXE_TYPE_QP,
144 			    rxe->attr.max_qp);
145 	if (err)
146 		goto err5;
147 
148 	err = rxe_pool_init(rxe, &rxe->cq_pool, RXE_TYPE_CQ,
149 			    rxe->attr.max_cq);
150 	if (err)
151 		goto err6;
152 
153 	err = rxe_pool_init(rxe, &rxe->mr_pool, RXE_TYPE_MR,
154 			    rxe->attr.max_mr);
155 	if (err)
156 		goto err7;
157 
158 	err = rxe_pool_init(rxe, &rxe->mw_pool, RXE_TYPE_MW,
159 			    rxe->attr.max_mw);
160 	if (err)
161 		goto err8;
162 
163 	err = rxe_pool_init(rxe, &rxe->mc_grp_pool, RXE_TYPE_MC_GRP,
164 			    rxe->attr.max_mcast_grp);
165 	if (err)
166 		goto err9;
167 
168 	err = rxe_pool_init(rxe, &rxe->mc_elem_pool, RXE_TYPE_MC_ELEM,
169 			    rxe->attr.max_total_mcast_qp_attach);
170 	if (err)
171 		goto err10;
172 
173 	return 0;
174 
175 err10:
176 	rxe_pool_cleanup(&rxe->mc_grp_pool);
177 err9:
178 	rxe_pool_cleanup(&rxe->mw_pool);
179 err8:
180 	rxe_pool_cleanup(&rxe->mr_pool);
181 err7:
182 	rxe_pool_cleanup(&rxe->cq_pool);
183 err6:
184 	rxe_pool_cleanup(&rxe->qp_pool);
185 err5:
186 	rxe_pool_cleanup(&rxe->srq_pool);
187 err4:
188 	rxe_pool_cleanup(&rxe->ah_pool);
189 err3:
190 	rxe_pool_cleanup(&rxe->pd_pool);
191 err2:
192 	rxe_pool_cleanup(&rxe->uc_pool);
193 err1:
194 	return err;
195 }
196 
197 /* initialize rxe device state */
198 static int rxe_init(struct rxe_dev *rxe)
199 {
200 	int err;
201 
202 	/* init default device parameters */
203 	rxe_init_device_param(rxe);
204 
205 	rxe_init_ports(rxe);
206 
207 	err = rxe_init_pools(rxe);
208 	if (err)
209 		return err;
210 
211 	/* init pending mmap list */
212 	spin_lock_init(&rxe->mmap_offset_lock);
213 	spin_lock_init(&rxe->pending_lock);
214 	INIT_LIST_HEAD(&rxe->pending_mmaps);
215 
216 	mutex_init(&rxe->usdev_lock);
217 
218 	return 0;
219 }
220 
221 void rxe_set_mtu(struct rxe_dev *rxe, unsigned int ndev_mtu)
222 {
223 	struct rxe_port *port = &rxe->port;
224 	enum ib_mtu mtu;
225 
226 	mtu = eth_mtu_int_to_enum(ndev_mtu);
227 
228 	/* Make sure that new MTU in range */
229 	mtu = mtu ? min_t(enum ib_mtu, mtu, IB_MTU_4096) : IB_MTU_256;
230 
231 	port->attr.active_mtu = mtu;
232 	port->mtu_cap = ib_mtu_enum_to_int(mtu);
233 }
234 
235 /* called by ifc layer to create new rxe device.
236  * The caller should allocate memory for rxe by calling ib_alloc_device.
237  */
238 int rxe_add(struct rxe_dev *rxe, unsigned int mtu, const char *ibdev_name)
239 {
240 	int err;
241 
242 	err = rxe_init(rxe);
243 	if (err)
244 		return err;
245 
246 	rxe_set_mtu(rxe, mtu);
247 
248 	return rxe_register_device(rxe, ibdev_name);
249 }
250 
251 static int rxe_newlink(const char *ibdev_name, struct net_device *ndev)
252 {
253 	struct rxe_dev *exists;
254 	int err = 0;
255 
256 	if (is_vlan_dev(ndev)) {
257 		pr_err("rxe creation allowed on top of a real device only\n");
258 		err = -EPERM;
259 		goto err;
260 	}
261 
262 	exists = rxe_get_dev_from_net(ndev);
263 	if (exists) {
264 		ib_device_put(&exists->ib_dev);
265 		pr_err("already configured on %s\n", ndev->name);
266 		err = -EEXIST;
267 		goto err;
268 	}
269 
270 	err = rxe_net_add(ibdev_name, ndev);
271 	if (err) {
272 		pr_err("failed to add %s\n", ndev->name);
273 		goto err;
274 	}
275 err:
276 	return err;
277 }
278 
279 static struct rdma_link_ops rxe_link_ops = {
280 	.type = "rxe",
281 	.newlink = rxe_newlink,
282 };
283 
284 static int __init rxe_module_init(void)
285 {
286 	int err;
287 
288 	err = rxe_net_init();
289 	if (err)
290 		return err;
291 
292 	rdma_link_register(&rxe_link_ops);
293 	rxe_initialized = true;
294 	pr_info("loaded\n");
295 	return 0;
296 }
297 
298 static void __exit rxe_module_exit(void)
299 {
300 	rdma_link_unregister(&rxe_link_ops);
301 	ib_unregister_driver(RDMA_DRIVER_RXE);
302 	rxe_net_exit();
303 
304 	rxe_initialized = false;
305 	pr_info("unloaded\n");
306 }
307 
308 late_initcall(rxe_module_init);
309 module_exit(rxe_module_exit);
310 
311 MODULE_ALIAS_RDMA_LINK("rxe");
312