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