]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/infiniband/sw/rdmavt/vt.c
RDMA: Globally allocate and release QP memory
[mirror_ubuntu-jammy-kernel.git] / drivers / infiniband / sw / rdmavt / vt.c
CommitLineData
0194621b 1/*
5d18ee67 2 * Copyright(c) 2016 - 2018 Intel Corporation.
0194621b
DD
3 *
4 * This file is provided under a dual BSD/GPLv2 license. When using or
5 * redistributing this file, you may do so under either license.
6 *
7 * GPL LICENSE SUMMARY
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * BSD LICENSE
19 *
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
22 * are met:
23 *
24 * - Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * - Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in
28 * the documentation and/or other materials provided with the
29 * distribution.
30 * - Neither the name of Intel Corporation nor the names of its
31 * contributors may be used to endorse or promote products derived
32 * from this software without specific prior written permission.
33 *
34 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 *
46 */
47
48#include <linux/module.h>
49#include <linux/kernel.h>
0bbb3b74 50#include <linux/dma-mapping.h>
0194621b 51#include "vt.h"
5d18ee67 52#include "cq.h"
81ba39a8 53#include "trace.h"
0194621b 54
182285d0
DD
55#define RVT_UVERBS_ABI_VERSION 2
56
0194621b
DD
57MODULE_LICENSE("Dual BSD/GPL");
58MODULE_DESCRIPTION("RDMA Verbs Transport Library");
59
60static int rvt_init(void)
61{
5d18ee67
SS
62 int ret = rvt_driver_cq_init();
63
64 if (ret)
65 pr_err("Error in driver CQ init.\n");
66
67 return ret;
0194621b
DD
68}
69module_init(rvt_init);
70
71static void rvt_cleanup(void)
72{
5d18ee67 73 rvt_cq_exit();
0194621b
DD
74}
75module_exit(rvt_cleanup);
76
90793f71
DD
77/**
78 * rvt_alloc_device - allocate rdi
79 * @size: how big of a structure to allocate
80 * @nports: number of ports to allocate array slots for
81 *
82 * Use IB core device alloc to allocate space for the rdi which is assumed to be
83 * inside of the ib_device. Any extra space that drivers require should be
84 * included in size.
85 *
86 * We also allocate a port array based on the number of ports.
87 *
88 * Return: pointer to allocated rdi
89 */
ff6acd69
DD
90struct rvt_dev_info *rvt_alloc_device(size_t size, int nports)
91{
042932f7 92 struct rvt_dev_info *rdi;
ff6acd69 93
459cc69f 94 rdi = container_of(_ib_alloc_device(size), struct rvt_dev_info, ibdev);
ff6acd69
DD
95 if (!rdi)
96 return rdi;
97
8e71f694 98 rdi->ports = kcalloc(nports, sizeof(*rdi->ports), GFP_KERNEL);
ff6acd69
DD
99 if (!rdi->ports)
100 ib_dealloc_device(&rdi->ibdev);
101
102 return rdi;
103}
104EXPORT_SYMBOL(rvt_alloc_device);
105
ea0e4ce3
JJ
106/**
107 * rvt_dealloc_device - deallocate rdi
108 * @rdi: structure to free
109 *
110 * Free a structure allocated with rvt_alloc_device()
111 */
112void rvt_dealloc_device(struct rvt_dev_info *rdi)
113{
114 kfree(rdi->ports);
115 ib_dealloc_device(&rdi->ibdev);
116}
117EXPORT_SYMBOL(rvt_dealloc_device);
118
19ef1edd
DD
119static int rvt_query_device(struct ib_device *ibdev,
120 struct ib_device_attr *props,
121 struct ib_udata *uhw)
122{
feaeb6e2
HC
123 struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
124
125 if (uhw->inlen || uhw->outlen)
126 return -EINVAL;
19ef1edd 127 /*
feaeb6e2 128 * Return rvt_dev_info.dparms.props contents
19ef1edd 129 */
feaeb6e2
HC
130 *props = rdi->dparms.props;
131 return 0;
19ef1edd
DD
132}
133
514aee66
LR
134static int rvt_get_numa_node(struct ib_device *ibdev)
135{
136 struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
137
138 return rdi->dparms.node;
139}
140
19ef1edd
DD
141static int rvt_modify_device(struct ib_device *device,
142 int device_modify_mask,
143 struct ib_device_modify *device_modify)
144{
145 /*
90793f71
DD
146 * There is currently no need to supply this based on qib and hfi1.
147 * Future drivers may need to implement this though.
19ef1edd
DD
148 */
149
19ef1edd
DD
150 return -EOPNOTSUPP;
151}
152
765525c1 153/**
bf194997 154 * rvt_query_port - Passes the query port call to the driver
765525c1 155 * @ibdev: Verbs IB dev
f1badc71 156 * @port_num: port number, 1 based from ib core
765525c1
DD
157 * @props: structure to hold returned properties
158 *
90793f71 159 * Return: 0 on success
765525c1 160 */
1fb7f897 161static int rvt_query_port(struct ib_device *ibdev, u32 port_num,
765525c1
DD
162 struct ib_port_attr *props)
163{
61a650c1
HC
164 struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
165 struct rvt_ibport *rvp;
1fb7f897 166 u32 port_index = ibport_num_to_idx(ibdev, port_num);
f1badc71 167
61a650c1 168 rvp = rdi->ports[port_index];
c4550c63 169 /* props being zeroed by the caller, avoid zeroing it here */
61a650c1
HC
170 props->sm_lid = rvp->sm_lid;
171 props->sm_sl = rvp->sm_sl;
172 props->port_cap_flags = rvp->port_cap_flags;
173 props->max_msg_sz = 0x80000000;
174 props->pkey_tbl_len = rvt_get_npkeys(rdi);
175 props->bad_pkey_cntr = rvp->pkey_violations;
176 props->qkey_viol_cntr = rvp->qkey_violations;
177 props->subnet_timeout = rvp->subnet_timeout;
178 props->init_type_reply = 0;
179
180 /* Populate the remaining ib_port_attr elements */
181 return rdi->driver_f.query_port_state(rdi, port_num, props);
765525c1
DD
182}
183
184/**
bf194997 185 * rvt_modify_port - modify port
765525c1 186 * @ibdev: Verbs IB dev
f1badc71 187 * @port_num: Port number, 1 based from ib core
765525c1
DD
188 * @port_modify_mask: How to change the port
189 * @props: Structure to fill in
190 *
90793f71 191 * Return: 0 on success
765525c1 192 */
1fb7f897 193static int rvt_modify_port(struct ib_device *ibdev, u32 port_num,
765525c1
DD
194 int port_modify_mask, struct ib_port_modify *props)
195{
61a650c1
HC
196 struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
197 struct rvt_ibport *rvp;
198 int ret = 0;
1fb7f897 199 u32 port_index = ibport_num_to_idx(ibdev, port_num);
f1badc71 200
61a650c1 201 rvp = rdi->ports[port_index];
cb49366f
VN
202 if (port_modify_mask & IB_PORT_OPA_MASK_CHG) {
203 rvp->port_cap3_flags |= props->set_port_cap_mask;
204 rvp->port_cap3_flags &= ~props->clr_port_cap_mask;
205 } else {
206 rvp->port_cap_flags |= props->set_port_cap_mask;
207 rvp->port_cap_flags &= ~props->clr_port_cap_mask;
208 }
61a650c1
HC
209
210 if (props->set_port_cap_mask || props->clr_port_cap_mask)
211 rdi->driver_f.cap_mask_chg(rdi, port_num);
212 if (port_modify_mask & IB_PORT_SHUTDOWN)
213 ret = rdi->driver_f.shut_down_port(rdi, port_num);
214 if (port_modify_mask & IB_PORT_RESET_QKEY_CNTR)
215 rvp->qkey_violations = 0;
216
217 return ret;
765525c1
DD
218}
219
30588643
DD
220/**
221 * rvt_query_pkey - Return a pkey from the table at a given index
222 * @ibdev: Verbs IB dev
f1badc71 223 * @port_num: Port number, 1 based from ib core
4f9a3018
RD
224 * @index: Index into pkey table
225 * @pkey: returned pkey from the port pkey table
30588643 226 *
90793f71 227 * Return: 0 on failure pkey otherwise
30588643 228 */
1fb7f897 229static int rvt_query_pkey(struct ib_device *ibdev, u32 port_num, u16 index,
30588643
DD
230 u16 *pkey)
231{
232 /*
233 * Driver will be responsible for keeping rvt_dev_info.pkey_table up to
234 * date. This function will just return that value. There is no need to
235 * lock, if a stale value is read and sent to the user so be it there is
236 * no way to protect against that anyway.
237 */
38ce2c6f 238 struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
1fb7f897 239 u32 port_index;
38ce2c6f 240
f1badc71 241 port_index = ibport_num_to_idx(ibdev, port_num);
38ce2c6f 242
f1badc71 243 if (index >= rvt_get_npkeys(rdi))
38ce2c6f
DD
244 return -EINVAL;
245
246 *pkey = rvt_get_pkey(rdi, port_index, index);
30588643
DD
247 return 0;
248}
249
2d092e11
DD
250/**
251 * rvt_query_gid - Return a gid from the table
252 * @ibdev: Verbs IB dev
f1badc71 253 * @port_num: Port number, 1 based from ib core
4f9a3018 254 * @guid_index: Index in table
2d092e11
DD
255 * @gid: Gid to return
256 *
90793f71 257 * Return: 0 on success
2d092e11 258 */
1fb7f897 259static int rvt_query_gid(struct ib_device *ibdev, u32 port_num,
1f024992 260 int guid_index, union ib_gid *gid)
2d092e11 261{
1f024992
DD
262 struct rvt_dev_info *rdi;
263 struct rvt_ibport *rvp;
1fb7f897 264 u32 port_index;
1f024992 265
2d092e11
DD
266 /*
267 * Driver is responsible for updating the guid table. Which will be used
268 * to craft the return value. This will work similar to how query_pkey()
269 * is being done.
270 */
1f024992 271 port_index = ibport_num_to_idx(ibdev, port_num);
2d092e11 272
1f024992
DD
273 rdi = ib_to_rvt(ibdev);
274 rvp = rdi->ports[port_index];
275
276 gid->global.subnet_prefix = rvp->gid_prefix;
277
278 return rdi->driver_f.get_guid_be(rdi, rvp, guid_index,
279 &gid->global.interface_id);
2d092e11
DD
280}
281
c4ed7d8b
DD
282/**
283 * rvt_alloc_ucontext - Allocate a user context
a2a074ef 284 * @uctx: Verbs context
4f9a3018 285 * @udata: User data allocated
c4ed7d8b 286 */
a2a074ef 287static int rvt_alloc_ucontext(struct ib_ucontext *uctx, struct ib_udata *udata)
c4ed7d8b 288{
a2a074ef 289 return 0;
c4ed7d8b
DD
290}
291
292/**
a2a074ef 293 * rvt_dealloc_ucontext - Free a user context
39412461 294 * @context: Unused
c4ed7d8b 295 */
a2a074ef 296static void rvt_dealloc_ucontext(struct ib_ucontext *context)
c4ed7d8b 297{
a2a074ef 298 return;
c4ed7d8b
DD
299}
300
1fb7f897 301static int rvt_get_port_immutable(struct ib_device *ibdev, u32 port_num,
e6a8818a
DD
302 struct ib_port_immutable *immutable)
303{
61a650c1
HC
304 struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
305 struct ib_port_attr attr;
1fb7f897 306 int err;
61a650c1 307
c4550c63
OG
308 immutable->core_cap_flags = rdi->dparms.core_cap_flags;
309
310 err = ib_query_port(ibdev, port_num, &attr);
61a650c1
HC
311 if (err)
312 return err;
313
314 immutable->pkey_tbl_len = attr.pkey_tbl_len;
315 immutable->gid_tbl_len = attr.gid_tbl_len;
61a650c1
HC
316 immutable->max_mad_size = rdi->dparms.max_mad_size;
317
318 return 0;
e6a8818a
DD
319}
320
1348d706
DD
321enum {
322 MISC,
323 QUERY_DEVICE,
324 MODIFY_DEVICE,
325 QUERY_PORT,
326 MODIFY_PORT,
327 QUERY_PKEY,
328 QUERY_GID,
329 ALLOC_UCONTEXT,
330 DEALLOC_UCONTEXT,
331 GET_PORT_IMMUTABLE,
332 CREATE_QP,
333 MODIFY_QP,
334 DESTROY_QP,
335 QUERY_QP,
336 POST_SEND,
337 POST_RECV,
338 POST_SRQ_RECV,
339 CREATE_AH,
340 DESTROY_AH,
341 MODIFY_AH,
342 QUERY_AH,
343 CREATE_SRQ,
344 MODIFY_SRQ,
345 DESTROY_SRQ,
346 QUERY_SRQ,
347 ATTACH_MCAST,
348 DETACH_MCAST,
349 GET_DMA_MR,
350 REG_USER_MR,
351 DEREG_MR,
352 ALLOC_MR,
a41081aa 353 MAP_MR_SG,
1348d706
DD
354 ALLOC_FMR,
355 MAP_PHYS_FMR,
356 UNMAP_FMR,
357 DEALLOC_FMR,
358 MMAP,
359 CREATE_CQ,
360 DESTROY_CQ,
361 POLL_CQ,
362 REQ_NOTFIY_CQ,
363 RESIZE_CQ,
364 ALLOC_PD,
365 DEALLOC_PD,
366 _VERB_IDX_MAX /* Must always be last! */
367};
368
02a42f8e 369static const struct ib_device_ops rvt_dev_ops = {
72c6ec18
JG
370 .uverbs_abi_ver = RVT_UVERBS_ABI_VERSION,
371
02a42f8e
KH
372 .alloc_mr = rvt_alloc_mr,
373 .alloc_pd = rvt_alloc_pd,
374 .alloc_ucontext = rvt_alloc_ucontext,
375 .attach_mcast = rvt_attach_mcast,
376 .create_ah = rvt_create_ah,
377 .create_cq = rvt_create_cq,
378 .create_qp = rvt_create_qp,
379 .create_srq = rvt_create_srq,
676a80ad 380 .create_user_ah = rvt_create_ah,
02a42f8e
KH
381 .dealloc_pd = rvt_dealloc_pd,
382 .dealloc_ucontext = rvt_dealloc_ucontext,
383 .dereg_mr = rvt_dereg_mr,
384 .destroy_ah = rvt_destroy_ah,
385 .destroy_cq = rvt_destroy_cq,
386 .destroy_qp = rvt_destroy_qp,
387 .destroy_srq = rvt_destroy_srq,
388 .detach_mcast = rvt_detach_mcast,
389 .get_dma_mr = rvt_get_dma_mr,
514aee66 390 .get_numa_node = rvt_get_numa_node,
02a42f8e
KH
391 .get_port_immutable = rvt_get_port_immutable,
392 .map_mr_sg = rvt_map_mr_sg,
02a42f8e
KH
393 .mmap = rvt_mmap,
394 .modify_ah = rvt_modify_ah,
395 .modify_device = rvt_modify_device,
396 .modify_port = rvt_modify_port,
397 .modify_qp = rvt_modify_qp,
398 .modify_srq = rvt_modify_srq,
399 .poll_cq = rvt_poll_cq,
400 .post_recv = rvt_post_recv,
401 .post_send = rvt_post_send,
402 .post_srq_recv = rvt_post_srq_recv,
403 .query_ah = rvt_query_ah,
404 .query_device = rvt_query_device,
405 .query_gid = rvt_query_gid,
406 .query_pkey = rvt_query_pkey,
407 .query_port = rvt_query_port,
408 .query_qp = rvt_query_qp,
409 .query_srq = rvt_query_srq,
410 .reg_user_mr = rvt_reg_user_mr,
411 .req_notify_cq = rvt_req_notify_cq,
412 .resize_cq = rvt_resize_cq,
d3456914
LR
413
414 INIT_RDMA_OBJ_SIZE(ib_ah, rvt_ah, ibah),
e39afe3d 415 INIT_RDMA_OBJ_SIZE(ib_cq, rvt_cq, ibcq),
21a428a0 416 INIT_RDMA_OBJ_SIZE(ib_pd, rvt_pd, ibpd),
514aee66 417 INIT_RDMA_OBJ_SIZE(ib_qp, rvt_qp, ibqp),
68e326de 418 INIT_RDMA_OBJ_SIZE(ib_srq, rvt_srq, ibsrq),
a2a074ef 419 INIT_RDMA_OBJ_SIZE(ib_ucontext, rvt_ucontext, ibucontext),
02a42f8e 420};
1348d706 421
9debaaec 422static noinline int check_support(struct rvt_dev_info *rdi, int verb)
1348d706
DD
423{
424 switch (verb) {
425 case MISC:
426 /*
427 * These functions are not part of verbs specifically but are
428 * required for rdmavt to function.
429 */
d7407d16 430 if ((!rdi->ibdev.ops.port_groups) ||
1348d706
DD
431 (!rdi->driver_f.get_pci_dev))
432 return -EINVAL;
433 break;
434
1348d706
DD
435 case MODIFY_DEVICE:
436 /*
437 * rdmavt does not support modify device currently drivers must
438 * provide.
439 */
3023a1e9 440 if (!rdi->ibdev.ops.modify_device)
1348d706
DD
441 return -EOPNOTSUPP;
442 break;
443
444 case QUERY_PORT:
3023a1e9 445 if (!rdi->ibdev.ops.query_port)
1348d706
DD
446 if (!rdi->driver_f.query_port_state)
447 return -EINVAL;
448 break;
449
450 case MODIFY_PORT:
3023a1e9 451 if (!rdi->ibdev.ops.modify_port)
1348d706
DD
452 if (!rdi->driver_f.cap_mask_chg ||
453 !rdi->driver_f.shut_down_port)
454 return -EINVAL;
455 break;
456
1348d706 457 case QUERY_GID:
3023a1e9 458 if (!rdi->ibdev.ops.query_gid)
1348d706
DD
459 if (!rdi->driver_f.get_guid_be)
460 return -EINVAL;
461 break;
462
1348d706 463 case CREATE_QP:
3023a1e9 464 if (!rdi->ibdev.ops.create_qp)
1348d706
DD
465 if (!rdi->driver_f.qp_priv_alloc ||
466 !rdi->driver_f.qp_priv_free ||
467 !rdi->driver_f.notify_qp_reset ||
468 !rdi->driver_f.flush_qp_waiters ||
469 !rdi->driver_f.stop_send_queue ||
470 !rdi->driver_f.quiesce_qp)
471 return -EINVAL;
472 break;
473
474 case MODIFY_QP:
3023a1e9 475 if (!rdi->ibdev.ops.modify_qp)
1348d706
DD
476 if (!rdi->driver_f.notify_qp_reset ||
477 !rdi->driver_f.schedule_send ||
478 !rdi->driver_f.get_pmtu_from_attr ||
479 !rdi->driver_f.flush_qp_waiters ||
480 !rdi->driver_f.stop_send_queue ||
481 !rdi->driver_f.quiesce_qp ||
482 !rdi->driver_f.notify_error_qp ||
483 !rdi->driver_f.mtu_from_qp ||
96605672 484 !rdi->driver_f.mtu_to_path_mtu)
1348d706
DD
485 return -EINVAL;
486 break;
487
488 case DESTROY_QP:
3023a1e9 489 if (!rdi->ibdev.ops.destroy_qp)
1348d706
DD
490 if (!rdi->driver_f.qp_priv_free ||
491 !rdi->driver_f.notify_qp_reset ||
492 !rdi->driver_f.flush_qp_waiters ||
493 !rdi->driver_f.stop_send_queue ||
494 !rdi->driver_f.quiesce_qp)
495 return -EINVAL;
496 break;
497
1348d706 498 case POST_SEND:
3023a1e9 499 if (!rdi->ibdev.ops.post_send)
1348d706 500 if (!rdi->driver_f.schedule_send ||
2821c509
MM
501 !rdi->driver_f.do_send ||
502 !rdi->post_parms)
1348d706
DD
503 return -EINVAL;
504 break;
505
1348d706
DD
506 }
507
508 return 0;
509}
4997870a 510
90793f71
DD
511/**
512 * rvt_register_device - register a driver
513 * @rdi: main dev structure for all of rdmavt operations
514 *
515 * It is up to drivers to allocate the rdi and fill in the appropriate
516 * information.
517 *
518 * Return: 0 on success otherwise an errno.
519 */
b9560a41 520int rvt_register_device(struct rvt_dev_info *rdi)
0194621b 521{
1348d706 522 int ret = 0, i;
7b1e2099 523
0194621b
DD
524 if (!rdi)
525 return -EINVAL;
526
1348d706
DD
527 /*
528 * Check to ensure drivers have setup the required helpers for the verbs
529 * they want rdmavt to handle
530 */
531 for (i = 0; i < _VERB_IDX_MAX; i++)
532 if (check_support(rdi, i)) {
533 pr_err("Driver support req not met at %d\n", i);
534 return -EINVAL;
535 }
536
02a42f8e 537 ib_set_device_ops(&rdi->ibdev, &rvt_dev_ops);
b534875d 538
81ba39a8
DD
539 /* Once we get past here we can use rvt_pr macros and tracepoints */
540 trace_rvt_dbg(rdi, "Driver attempting registration");
822514d7 541 rvt_mmap_init(rdi);
b534875d 542
b518d3e6 543 /* Queue Pairs */
0acb0cc7
DD
544 ret = rvt_driver_qp_init(rdi);
545 if (ret) {
546 pr_err("Error in driver QP init.\n");
547 return -EINVAL;
548 }
549
4c1e4972 550 /* Address Handle */
119a8e70
KH
551 spin_lock_init(&rdi->n_ahs_lock);
552 rdi->n_ahs_allocated = 0;
4c1e4972 553
aad9158b 554 /* Shared Receive Queue */
b8f881b9 555 rvt_driver_srq_init(rdi);
aad9158b 556
9fa25171 557 /* Multicast */
4e74080b 558 rvt_driver_mcast_init(rdi);
9fa25171 559
2a055eb7 560 /* Mem Region */
7b1e2099
DD
561 ret = rvt_driver_mr_init(rdi);
562 if (ret) {
36055a06 563 pr_err("Error in driver MR init.\n");
7b1e2099
DD
564 goto bail_no_mr;
565 }
566
019f118b
BW
567 /* Memory Working Set Size */
568 ret = rvt_wss_init(rdi);
569 if (ret) {
570 rvt_pr_err(rdi, "Error in WSS init.\n");
571 goto bail_mr;
572 }
573
cf16335a 574 /* Completion queues */
5d18ee67 575 spin_lock_init(&rdi->n_cqs_lock);
cf16335a 576
8afd32eb 577 /* Protection Domain */
8afd32eb
DD
578 spin_lock_init(&rdi->n_pds_lock);
579 rdi->n_pds_allocated = 0;
580
182285d0
DD
581 /*
582 * There are some things which could be set by underlying drivers but
583 * really should be up to rdmavt to set. For instance drivers can't know
584 * exactly which functions rdmavt supports, nor do they know the ABI
585 * version, so we do all of this sort of stuff here.
586 */
c074bb1e 587 rdi->ibdev.uverbs_cmd_mask |=
182285d0
DD
588 (1ull << IB_USER_VERBS_CMD_POLL_CQ) |
589 (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) |
182285d0
DD
590 (1ull << IB_USER_VERBS_CMD_POST_SEND) |
591 (1ull << IB_USER_VERBS_CMD_POST_RECV) |
182285d0
DD
592 (1ull << IB_USER_VERBS_CMD_POST_SRQ_RECV);
593 rdi->ibdev.node_type = RDMA_NODE_IB_CA;
5d18ee67
SS
594 if (!rdi->ibdev.num_comp_vectors)
595 rdi->ibdev.num_comp_vectors = 1;
182285d0 596
7b1e2099 597 /* We are now good to announce we exist */
e0477b34 598 ret = ib_register_device(&rdi->ibdev, dev_name(&rdi->ibdev.dev), NULL);
7b1e2099
DD
599 if (ret) {
600 rvt_pr_err(rdi, "Failed to register driver with ib core.\n");
019f118b 601 goto bail_wss;
7b1e2099
DD
602 }
603
3711baf2
DD
604 rvt_create_mad_agents(rdi);
605
b534875d 606 rvt_pr_info(rdi, "Registration with rdmavt done.\n");
7b1e2099 607 return ret;
aec57787 608
019f118b
BW
609bail_wss:
610 rvt_wss_exit(rdi);
7b1e2099
DD
611bail_mr:
612 rvt_mr_exit(rdi);
613
614bail_no_mr:
0acb0cc7
DD
615 rvt_qp_exit(rdi);
616
7b1e2099 617 return ret;
0194621b
DD
618}
619EXPORT_SYMBOL(rvt_register_device);
620
90793f71
DD
621/**
622 * rvt_unregister_device - remove a driver
623 * @rdi: rvt dev struct
624 */
0194621b
DD
625void rvt_unregister_device(struct rvt_dev_info *rdi)
626{
81ba39a8 627 trace_rvt_dbg(rdi, "Driver is unregistering.");
0194621b
DD
628 if (!rdi)
629 return;
630
3711baf2
DD
631 rvt_free_mad_agents(rdi);
632
0194621b 633 ib_unregister_device(&rdi->ibdev);
019f118b 634 rvt_wss_exit(rdi);
7b1e2099 635 rvt_mr_exit(rdi);
515667f8 636 rvt_qp_exit(rdi);
0194621b
DD
637}
638EXPORT_SYMBOL(rvt_unregister_device);
f3d01bbc 639
90793f71
DD
640/**
641 * rvt_init_port - init internal data for driver port
7c21072d 642 * @rdi: rvt_dev_info struct
90793f71
DD
643 * @port: rvt port
644 * @port_index: 0 based index of ports, different from IB core port num
7c21072d 645 * @pkey_table: pkey_table for @port
90793f71 646 *
f3d01bbc
DD
647 * Keep track of a list of ports. No need to have a detach port.
648 * They persist until the driver goes away.
90793f71
DD
649 *
650 * Return: always 0
f3d01bbc 651 */
38ce2c6f 652int rvt_init_port(struct rvt_dev_info *rdi, struct rvt_ibport *port,
f1badc71 653 int port_index, u16 *pkey_table)
f3d01bbc 654{
38ce2c6f 655
f1badc71
DD
656 rdi->ports[port_index] = port;
657 rdi->ports[port_index]->pkey_table = pkey_table;
38ce2c6f
DD
658
659 return 0;
f3d01bbc 660}
38ce2c6f 661EXPORT_SYMBOL(rvt_init_port);