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