blob: cfadd8d8593a74acf6ce958a42f48cb927a75128 [file] [log] [blame]
Moni Shoua8700e3e2016-06-16 16:45:23 +03001/*
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 "rxe.h"
35#include "rxe_loc.h"
36
37MODULE_AUTHOR("Bob Pearson, Frank Zago, John Groves, Kamal Heib");
38MODULE_DESCRIPTION("Soft RDMA transport");
39MODULE_LICENSE("Dual BSD/GPL");
40MODULE_VERSION("0.2");
41
42/* free resources for all ports on a device */
43static void rxe_cleanup_ports(struct rxe_dev *rxe)
44{
45 kfree(rxe->port.pkey_tbl);
46 rxe->port.pkey_tbl = NULL;
47
48}
49
50/* free resources for a rxe device all objects created for this device must
51 * have been destroyed
52 */
53static void rxe_cleanup(struct rxe_dev *rxe)
54{
55 rxe_pool_cleanup(&rxe->uc_pool);
56 rxe_pool_cleanup(&rxe->pd_pool);
57 rxe_pool_cleanup(&rxe->ah_pool);
58 rxe_pool_cleanup(&rxe->srq_pool);
59 rxe_pool_cleanup(&rxe->qp_pool);
60 rxe_pool_cleanup(&rxe->cq_pool);
61 rxe_pool_cleanup(&rxe->mr_pool);
62 rxe_pool_cleanup(&rxe->mw_pool);
63 rxe_pool_cleanup(&rxe->mc_grp_pool);
64 rxe_pool_cleanup(&rxe->mc_elem_pool);
65
66 rxe_cleanup_ports(rxe);
yonatanccee26882017-04-20 20:55:55 +030067
68 crypto_free_shash(rxe->tfm);
Moni Shoua8700e3e2016-06-16 16:45:23 +030069}
70
71/* called when all references have been dropped */
72void rxe_release(struct kref *kref)
73{
74 struct rxe_dev *rxe = container_of(kref, struct rxe_dev, ref_cnt);
75
76 rxe_cleanup(rxe);
77 ib_dealloc_device(&rxe->ib_dev);
78}
79
80void rxe_dev_put(struct rxe_dev *rxe)
81{
82 kref_put(&rxe->ref_cnt, rxe_release);
83}
84EXPORT_SYMBOL_GPL(rxe_dev_put);
85
86/* initialize rxe device parameters */
87static int rxe_init_device_param(struct rxe_dev *rxe)
88{
89 rxe->max_inline_data = RXE_MAX_INLINE_DATA;
90
91 rxe->attr.fw_ver = RXE_FW_VER;
92 rxe->attr.max_mr_size = RXE_MAX_MR_SIZE;
93 rxe->attr.page_size_cap = RXE_PAGE_SIZE_CAP;
94 rxe->attr.vendor_id = RXE_VENDOR_ID;
95 rxe->attr.vendor_part_id = RXE_VENDOR_PART_ID;
96 rxe->attr.hw_ver = RXE_HW_VER;
97 rxe->attr.max_qp = RXE_MAX_QP;
98 rxe->attr.max_qp_wr = RXE_MAX_QP_WR;
99 rxe->attr.device_cap_flags = RXE_DEVICE_CAP_FLAGS;
100 rxe->attr.max_sge = RXE_MAX_SGE;
101 rxe->attr.max_sge_rd = RXE_MAX_SGE_RD;
102 rxe->attr.max_cq = RXE_MAX_CQ;
103 rxe->attr.max_cqe = (1 << RXE_MAX_LOG_CQE) - 1;
104 rxe->attr.max_mr = RXE_MAX_MR;
105 rxe->attr.max_pd = RXE_MAX_PD;
106 rxe->attr.max_qp_rd_atom = RXE_MAX_QP_RD_ATOM;
107 rxe->attr.max_ee_rd_atom = RXE_MAX_EE_RD_ATOM;
108 rxe->attr.max_res_rd_atom = RXE_MAX_RES_RD_ATOM;
109 rxe->attr.max_qp_init_rd_atom = RXE_MAX_QP_INIT_RD_ATOM;
110 rxe->attr.max_ee_init_rd_atom = RXE_MAX_EE_INIT_RD_ATOM;
111 rxe->attr.atomic_cap = RXE_ATOMIC_CAP;
112 rxe->attr.max_ee = RXE_MAX_EE;
113 rxe->attr.max_rdd = RXE_MAX_RDD;
114 rxe->attr.max_mw = RXE_MAX_MW;
115 rxe->attr.max_raw_ipv6_qp = RXE_MAX_RAW_IPV6_QP;
116 rxe->attr.max_raw_ethy_qp = RXE_MAX_RAW_ETHY_QP;
117 rxe->attr.max_mcast_grp = RXE_MAX_MCAST_GRP;
118 rxe->attr.max_mcast_qp_attach = RXE_MAX_MCAST_QP_ATTACH;
119 rxe->attr.max_total_mcast_qp_attach = RXE_MAX_TOT_MCAST_QP_ATTACH;
120 rxe->attr.max_ah = RXE_MAX_AH;
121 rxe->attr.max_fmr = RXE_MAX_FMR;
122 rxe->attr.max_map_per_fmr = RXE_MAX_MAP_PER_FMR;
123 rxe->attr.max_srq = RXE_MAX_SRQ;
124 rxe->attr.max_srq_wr = RXE_MAX_SRQ_WR;
125 rxe->attr.max_srq_sge = RXE_MAX_SRQ_SGE;
126 rxe->attr.max_fast_reg_page_list_len = RXE_MAX_FMR_PAGE_LIST_LEN;
127 rxe->attr.max_pkeys = RXE_MAX_PKEYS;
128 rxe->attr.local_ca_ack_delay = RXE_LOCAL_CA_ACK_DELAY;
129
130 rxe->max_ucontext = RXE_MAX_UCONTEXT;
131
132 return 0;
133}
134
135/* initialize port attributes */
136static int rxe_init_port_param(struct rxe_port *port)
137{
138 port->attr.state = RXE_PORT_STATE;
139 port->attr.max_mtu = RXE_PORT_MAX_MTU;
140 port->attr.active_mtu = RXE_PORT_ACTIVE_MTU;
141 port->attr.gid_tbl_len = RXE_PORT_GID_TBL_LEN;
142 port->attr.port_cap_flags = RXE_PORT_PORT_CAP_FLAGS;
143 port->attr.max_msg_sz = RXE_PORT_MAX_MSG_SZ;
144 port->attr.bad_pkey_cntr = RXE_PORT_BAD_PKEY_CNTR;
145 port->attr.qkey_viol_cntr = RXE_PORT_QKEY_VIOL_CNTR;
146 port->attr.pkey_tbl_len = RXE_PORT_PKEY_TBL_LEN;
147 port->attr.lid = RXE_PORT_LID;
148 port->attr.sm_lid = RXE_PORT_SM_LID;
149 port->attr.lmc = RXE_PORT_LMC;
150 port->attr.max_vl_num = RXE_PORT_MAX_VL_NUM;
151 port->attr.sm_sl = RXE_PORT_SM_SL;
152 port->attr.subnet_timeout = RXE_PORT_SUBNET_TIMEOUT;
153 port->attr.init_type_reply = RXE_PORT_INIT_TYPE_REPLY;
154 port->attr.active_width = RXE_PORT_ACTIVE_WIDTH;
155 port->attr.active_speed = RXE_PORT_ACTIVE_SPEED;
156 port->attr.phys_state = RXE_PORT_PHYS_STATE;
157 port->mtu_cap =
158 ib_mtu_enum_to_int(RXE_PORT_ACTIVE_MTU);
159 port->subnet_prefix = cpu_to_be64(RXE_PORT_SUBNET_PREFIX);
160
161 return 0;
162}
163
164/* initialize port state, note IB convention that HCA ports are always
165 * numbered from 1
166 */
167static int rxe_init_ports(struct rxe_dev *rxe)
168{
169 struct rxe_port *port = &rxe->port;
170
171 rxe_init_port_param(port);
172
173 if (!port->attr.pkey_tbl_len || !port->attr.gid_tbl_len)
174 return -EINVAL;
175
176 port->pkey_tbl = kcalloc(port->attr.pkey_tbl_len,
177 sizeof(*port->pkey_tbl), GFP_KERNEL);
178
179 if (!port->pkey_tbl)
180 return -ENOMEM;
181
182 port->pkey_tbl[0] = 0xffff;
Bart Van Assche839f5ac2017-01-10 11:15:53 -0800183 port->port_guid = rxe_port_guid(rxe);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300184
185 spin_lock_init(&port->port_lock);
186
187 return 0;
188}
189
190/* init pools of managed objects */
191static int rxe_init_pools(struct rxe_dev *rxe)
192{
193 int err;
194
195 err = rxe_pool_init(rxe, &rxe->uc_pool, RXE_TYPE_UC,
196 rxe->max_ucontext);
197 if (err)
198 goto err1;
199
200 err = rxe_pool_init(rxe, &rxe->pd_pool, RXE_TYPE_PD,
201 rxe->attr.max_pd);
202 if (err)
203 goto err2;
204
205 err = rxe_pool_init(rxe, &rxe->ah_pool, RXE_TYPE_AH,
206 rxe->attr.max_ah);
207 if (err)
208 goto err3;
209
210 err = rxe_pool_init(rxe, &rxe->srq_pool, RXE_TYPE_SRQ,
211 rxe->attr.max_srq);
212 if (err)
213 goto err4;
214
215 err = rxe_pool_init(rxe, &rxe->qp_pool, RXE_TYPE_QP,
216 rxe->attr.max_qp);
217 if (err)
218 goto err5;
219
220 err = rxe_pool_init(rxe, &rxe->cq_pool, RXE_TYPE_CQ,
221 rxe->attr.max_cq);
222 if (err)
223 goto err6;
224
225 err = rxe_pool_init(rxe, &rxe->mr_pool, RXE_TYPE_MR,
226 rxe->attr.max_mr);
227 if (err)
228 goto err7;
229
230 err = rxe_pool_init(rxe, &rxe->mw_pool, RXE_TYPE_MW,
231 rxe->attr.max_mw);
232 if (err)
233 goto err8;
234
235 err = rxe_pool_init(rxe, &rxe->mc_grp_pool, RXE_TYPE_MC_GRP,
236 rxe->attr.max_mcast_grp);
237 if (err)
238 goto err9;
239
240 err = rxe_pool_init(rxe, &rxe->mc_elem_pool, RXE_TYPE_MC_ELEM,
241 rxe->attr.max_total_mcast_qp_attach);
242 if (err)
243 goto err10;
244
245 return 0;
246
247err10:
248 rxe_pool_cleanup(&rxe->mc_grp_pool);
249err9:
250 rxe_pool_cleanup(&rxe->mw_pool);
251err8:
252 rxe_pool_cleanup(&rxe->mr_pool);
253err7:
254 rxe_pool_cleanup(&rxe->cq_pool);
255err6:
256 rxe_pool_cleanup(&rxe->qp_pool);
257err5:
258 rxe_pool_cleanup(&rxe->srq_pool);
259err4:
260 rxe_pool_cleanup(&rxe->ah_pool);
261err3:
262 rxe_pool_cleanup(&rxe->pd_pool);
263err2:
264 rxe_pool_cleanup(&rxe->uc_pool);
265err1:
266 return err;
267}
268
269/* initialize rxe device state */
270static int rxe_init(struct rxe_dev *rxe)
271{
272 int err;
273
274 /* init default device parameters */
275 rxe_init_device_param(rxe);
276
277 err = rxe_init_ports(rxe);
278 if (err)
279 goto err1;
280
281 err = rxe_init_pools(rxe);
282 if (err)
283 goto err2;
284
285 /* init pending mmap list */
286 spin_lock_init(&rxe->mmap_offset_lock);
287 spin_lock_init(&rxe->pending_lock);
288 INIT_LIST_HEAD(&rxe->pending_mmaps);
289 INIT_LIST_HEAD(&rxe->list);
290
291 mutex_init(&rxe->usdev_lock);
292
293 return 0;
294
295err2:
296 rxe_cleanup_ports(rxe);
297err1:
298 return err;
299}
300
301int rxe_set_mtu(struct rxe_dev *rxe, unsigned int ndev_mtu)
302{
303 struct rxe_port *port = &rxe->port;
304 enum ib_mtu mtu;
305
306 mtu = eth_mtu_int_to_enum(ndev_mtu);
307
308 /* Make sure that new MTU in range */
309 mtu = mtu ? min_t(enum ib_mtu, mtu, RXE_PORT_MAX_MTU) : IB_MTU_256;
310
311 port->attr.active_mtu = mtu;
312 port->mtu_cap = ib_mtu_enum_to_int(mtu);
313
314 return 0;
315}
316EXPORT_SYMBOL(rxe_set_mtu);
317
318/* called by ifc layer to create new rxe device.
319 * The caller should allocate memory for rxe by calling ib_alloc_device.
320 */
321int rxe_add(struct rxe_dev *rxe, unsigned int mtu)
322{
323 int err;
324
325 kref_init(&rxe->ref_cnt);
326
327 err = rxe_init(rxe);
328 if (err)
329 goto err1;
330
331 err = rxe_set_mtu(rxe, mtu);
332 if (err)
333 goto err1;
334
335 err = rxe_register_device(rxe);
336 if (err)
337 goto err1;
338
339 return 0;
340
341err1:
342 rxe_dev_put(rxe);
343 return err;
344}
345EXPORT_SYMBOL(rxe_add);
346
347/* called by the ifc layer to remove a device */
348void rxe_remove(struct rxe_dev *rxe)
349{
350 rxe_unregister_device(rxe);
351
352 rxe_dev_put(rxe);
353}
354EXPORT_SYMBOL(rxe_remove);
355
356static int __init rxe_module_init(void)
357{
358 int err;
359
360 /* initialize slab caches for managed objects */
361 err = rxe_cache_init();
362 if (err) {
Parav Pandite404f942016-09-28 20:26:26 +0000363 pr_err("unable to init object pools\n");
Moni Shoua8700e3e2016-06-16 16:45:23 +0300364 return err;
365 }
366
Parav Pandite404f942016-09-28 20:26:26 +0000367 err = rxe_net_init();
368 if (err)
369 return err;
Yonatan Cohendfdd6152016-09-07 14:04:04 +0300370
Parav Pandite404f942016-09-28 20:26:26 +0000371 pr_info("loaded\n");
Moni Shoua8700e3e2016-06-16 16:45:23 +0300372 return 0;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300373}
374
375static void __exit rxe_module_exit(void)
376{
377 rxe_remove_all();
378 rxe_net_exit();
379 rxe_cache_exit();
380
Parav Pandite404f942016-09-28 20:26:26 +0000381 pr_info("unloaded\n");
Moni Shoua8700e3e2016-06-16 16:45:23 +0300382}
383
Stephen Batesb9fe8562016-09-23 09:32:11 -0600384late_initcall(rxe_module_init);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300385module_exit(rxe_module_exit);