blob: 9566a920a244b3e1628f611b7467b3a05ecbbfbc [file] [log] [blame]
Dennis Dalessandro01946212016-01-06 09:50:24 -08001/*
Dennis Dalessandrofe314192016-01-22 13:04:58 -08002 * Copyright(c) 2016 Intel Corporation.
Dennis Dalessandro01946212016-01-06 09:50:24 -08003 *
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>
50#include "vt.h"
Dennis Dalessandro81ba39a2016-01-22 13:00:28 -080051#include "trace.h"
Dennis Dalessandro01946212016-01-06 09:50:24 -080052
Dennis Dalessandro182285d2016-01-22 13:01:01 -080053#define RVT_UVERBS_ABI_VERSION 2
54
Dennis Dalessandro01946212016-01-06 09:50:24 -080055MODULE_LICENSE("Dual BSD/GPL");
56MODULE_DESCRIPTION("RDMA Verbs Transport Library");
57
58static int rvt_init(void)
59{
Dennis Dalessandro90793f72016-02-14 12:10:29 -080060 /*
61 * rdmavt does not need to do anything special when it starts up. All it
62 * needs to do is sit and wait until a driver attempts registration.
63 */
Dennis Dalessandro01946212016-01-06 09:50:24 -080064 return 0;
65}
66module_init(rvt_init);
67
68static void rvt_cleanup(void)
69{
Dennis Dalessandro90793f72016-02-14 12:10:29 -080070 /*
71 * Nothing to do at exit time either. The module won't be able to be
72 * removed until all drivers are gone which means all the dev structs
73 * are gone so there is really nothing to do.
74 */
Dennis Dalessandro01946212016-01-06 09:50:24 -080075}
76module_exit(rvt_cleanup);
77
Dennis Dalessandro90793f72016-02-14 12:10:29 -080078/**
79 * rvt_alloc_device - allocate rdi
80 * @size: how big of a structure to allocate
81 * @nports: number of ports to allocate array slots for
82 *
83 * Use IB core device alloc to allocate space for the rdi which is assumed to be
84 * inside of the ib_device. Any extra space that drivers require should be
85 * included in size.
86 *
87 * We also allocate a port array based on the number of ports.
88 *
89 * Return: pointer to allocated rdi
90 */
Dennis Dalessandroff6acd62016-01-22 13:04:45 -080091struct rvt_dev_info *rvt_alloc_device(size_t size, int nports)
92{
93 struct rvt_dev_info *rdi = ERR_PTR(-ENOMEM);
94
95 rdi = (struct rvt_dev_info *)ib_alloc_device(size);
96 if (!rdi)
97 return rdi;
98
99 rdi->ports = kcalloc(nports,
100 sizeof(struct rvt_ibport **),
101 GFP_KERNEL);
102 if (!rdi->ports)
103 ib_dealloc_device(&rdi->ibdev);
104
105 return rdi;
106}
107EXPORT_SYMBOL(rvt_alloc_device);
108
Dennis Dalessandro19ef1ed2016-01-06 09:53:05 -0800109static int rvt_query_device(struct ib_device *ibdev,
110 struct ib_device_attr *props,
111 struct ib_udata *uhw)
112{
Harish Chegondifeaeb6e2016-01-22 12:50:36 -0800113 struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
114
115 if (uhw->inlen || uhw->outlen)
116 return -EINVAL;
Dennis Dalessandro19ef1ed2016-01-06 09:53:05 -0800117 /*
Harish Chegondifeaeb6e2016-01-22 12:50:36 -0800118 * Return rvt_dev_info.dparms.props contents
Dennis Dalessandro19ef1ed2016-01-06 09:53:05 -0800119 */
Harish Chegondifeaeb6e2016-01-22 12:50:36 -0800120 *props = rdi->dparms.props;
121 return 0;
Dennis Dalessandro19ef1ed2016-01-06 09:53:05 -0800122}
123
124static int rvt_modify_device(struct ib_device *device,
125 int device_modify_mask,
126 struct ib_device_modify *device_modify)
127{
128 /*
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800129 * There is currently no need to supply this based on qib and hfi1.
130 * Future drivers may need to implement this though.
Dennis Dalessandro19ef1ed2016-01-06 09:53:05 -0800131 */
132
Dennis Dalessandro19ef1ed2016-01-06 09:53:05 -0800133 return -EOPNOTSUPP;
134}
135
Dennis Dalessandro765525c2016-01-06 09:54:07 -0800136/**
137 * rvt_query_port: Passes the query port call to the driver
138 * @ibdev: Verbs IB dev
Dennis Dalessandrof1badc72016-02-03 14:15:02 -0800139 * @port_num: port number, 1 based from ib core
Dennis Dalessandro765525c2016-01-06 09:54:07 -0800140 * @props: structure to hold returned properties
141 *
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800142 * Return: 0 on success
Dennis Dalessandro765525c2016-01-06 09:54:07 -0800143 */
Dennis Dalessandrof1badc72016-02-03 14:15:02 -0800144static int rvt_query_port(struct ib_device *ibdev, u8 port_num,
Dennis Dalessandro765525c2016-01-06 09:54:07 -0800145 struct ib_port_attr *props)
146{
Harish Chegondi61a650c2016-02-03 14:15:20 -0800147 struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
148 struct rvt_ibport *rvp;
149 int port_index = ibport_num_to_idx(ibdev, port_num);
150
151 if (port_index < 0)
Dennis Dalessandrof1badc72016-02-03 14:15:02 -0800152 return -EINVAL;
153
Harish Chegondi61a650c2016-02-03 14:15:20 -0800154 rvp = rdi->ports[port_index];
155 memset(props, 0, sizeof(*props));
156 props->sm_lid = rvp->sm_lid;
157 props->sm_sl = rvp->sm_sl;
158 props->port_cap_flags = rvp->port_cap_flags;
159 props->max_msg_sz = 0x80000000;
160 props->pkey_tbl_len = rvt_get_npkeys(rdi);
161 props->bad_pkey_cntr = rvp->pkey_violations;
162 props->qkey_viol_cntr = rvp->qkey_violations;
163 props->subnet_timeout = rvp->subnet_timeout;
164 props->init_type_reply = 0;
165
166 /* Populate the remaining ib_port_attr elements */
167 return rdi->driver_f.query_port_state(rdi, port_num, props);
Dennis Dalessandro765525c2016-01-06 09:54:07 -0800168}
169
170/**
171 * rvt_modify_port
172 * @ibdev: Verbs IB dev
Dennis Dalessandrof1badc72016-02-03 14:15:02 -0800173 * @port_num: Port number, 1 based from ib core
Dennis Dalessandro765525c2016-01-06 09:54:07 -0800174 * @port_modify_mask: How to change the port
175 * @props: Structure to fill in
176 *
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800177 * Return: 0 on success
Dennis Dalessandro765525c2016-01-06 09:54:07 -0800178 */
Dennis Dalessandrof1badc72016-02-03 14:15:02 -0800179static int rvt_modify_port(struct ib_device *ibdev, u8 port_num,
Dennis Dalessandro765525c2016-01-06 09:54:07 -0800180 int port_modify_mask, struct ib_port_modify *props)
181{
Harish Chegondi61a650c2016-02-03 14:15:20 -0800182 struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
183 struct rvt_ibport *rvp;
184 int ret = 0;
185 int port_index = ibport_num_to_idx(ibdev, port_num);
186
187 if (port_index < 0)
Dennis Dalessandrof1badc72016-02-03 14:15:02 -0800188 return -EINVAL;
189
Harish Chegondi61a650c2016-02-03 14:15:20 -0800190 rvp = rdi->ports[port_index];
191 rvp->port_cap_flags |= props->set_port_cap_mask;
192 rvp->port_cap_flags &= ~props->clr_port_cap_mask;
193
194 if (props->set_port_cap_mask || props->clr_port_cap_mask)
195 rdi->driver_f.cap_mask_chg(rdi, port_num);
196 if (port_modify_mask & IB_PORT_SHUTDOWN)
197 ret = rdi->driver_f.shut_down_port(rdi, port_num);
198 if (port_modify_mask & IB_PORT_RESET_QKEY_CNTR)
199 rvp->qkey_violations = 0;
200
201 return ret;
Dennis Dalessandro765525c2016-01-06 09:54:07 -0800202}
203
Dennis Dalessandro30588642016-01-06 09:54:16 -0800204/**
205 * rvt_query_pkey - Return a pkey from the table at a given index
206 * @ibdev: Verbs IB dev
Dennis Dalessandrof1badc72016-02-03 14:15:02 -0800207 * @port_num: Port number, 1 based from ib core
Dennis Dalessandro30588642016-01-06 09:54:16 -0800208 * @intex: Index into pkey table
209 *
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800210 * Return: 0 on failure pkey otherwise
Dennis Dalessandro30588642016-01-06 09:54:16 -0800211 */
Dennis Dalessandrof1badc72016-02-03 14:15:02 -0800212static int rvt_query_pkey(struct ib_device *ibdev, u8 port_num, u16 index,
Dennis Dalessandro30588642016-01-06 09:54:16 -0800213 u16 *pkey)
214{
215 /*
216 * Driver will be responsible for keeping rvt_dev_info.pkey_table up to
217 * date. This function will just return that value. There is no need to
218 * lock, if a stale value is read and sent to the user so be it there is
219 * no way to protect against that anyway.
220 */
Dennis Dalessandro38ce2c62016-01-06 10:05:12 -0800221 struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
222 int port_index;
223
Dennis Dalessandrof1badc72016-02-03 14:15:02 -0800224 port_index = ibport_num_to_idx(ibdev, port_num);
225 if (port_index < 0)
Dennis Dalessandro38ce2c62016-01-06 10:05:12 -0800226 return -EINVAL;
227
Dennis Dalessandrof1badc72016-02-03 14:15:02 -0800228 if (index >= rvt_get_npkeys(rdi))
Dennis Dalessandro38ce2c62016-01-06 10:05:12 -0800229 return -EINVAL;
230
231 *pkey = rvt_get_pkey(rdi, port_index, index);
Dennis Dalessandro30588642016-01-06 09:54:16 -0800232 return 0;
233}
234
Dennis Dalessandro2d092e12016-01-06 09:54:50 -0800235/**
236 * rvt_query_gid - Return a gid from the table
237 * @ibdev: Verbs IB dev
Dennis Dalessandrof1badc72016-02-03 14:15:02 -0800238 * @port_num: Port number, 1 based from ib core
Dennis Dalessandro2d092e12016-01-06 09:54:50 -0800239 * @index: = Index in table
240 * @gid: Gid to return
241 *
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800242 * Return: 0 on success
Dennis Dalessandro2d092e12016-01-06 09:54:50 -0800243 */
Dennis Dalessandrof1badc72016-02-03 14:15:02 -0800244static int rvt_query_gid(struct ib_device *ibdev, u8 port_num,
Dennis Dalessandro1f024992016-02-03 14:15:11 -0800245 int guid_index, union ib_gid *gid)
Dennis Dalessandro2d092e12016-01-06 09:54:50 -0800246{
Dennis Dalessandro1f024992016-02-03 14:15:11 -0800247 struct rvt_dev_info *rdi;
248 struct rvt_ibport *rvp;
249 int port_index;
250
Dennis Dalessandro2d092e12016-01-06 09:54:50 -0800251 /*
252 * Driver is responsible for updating the guid table. Which will be used
253 * to craft the return value. This will work similar to how query_pkey()
254 * is being done.
255 */
Dennis Dalessandro1f024992016-02-03 14:15:11 -0800256 port_index = ibport_num_to_idx(ibdev, port_num);
257 if (port_index < 0)
Dennis Dalessandrof1badc72016-02-03 14:15:02 -0800258 return -EINVAL;
Dennis Dalessandro2d092e12016-01-06 09:54:50 -0800259
Dennis Dalessandro1f024992016-02-03 14:15:11 -0800260 rdi = ib_to_rvt(ibdev);
261 rvp = rdi->ports[port_index];
262
263 gid->global.subnet_prefix = rvp->gid_prefix;
264
265 return rdi->driver_f.get_guid_be(rdi, rvp, guid_index,
266 &gid->global.interface_id);
Dennis Dalessandro2d092e12016-01-06 09:54:50 -0800267}
268
Harish Chegondi6c43cf42016-01-22 12:50:05 -0800269struct rvt_ucontext {
270 struct ib_ucontext ibucontext;
271};
272
273static inline struct rvt_ucontext *to_iucontext(struct ib_ucontext
274 *ibucontext)
275{
276 return container_of(ibucontext, struct rvt_ucontext, ibucontext);
277}
278
Dennis Dalessandroc4ed7d82016-01-06 09:55:39 -0800279/**
280 * rvt_alloc_ucontext - Allocate a user context
281 * @ibdev: Vers IB dev
282 * @data: User data allocated
283 */
284static struct ib_ucontext *rvt_alloc_ucontext(struct ib_device *ibdev,
285 struct ib_udata *udata)
286{
Harish Chegondi6c43cf42016-01-22 12:50:05 -0800287 struct rvt_ucontext *context;
288
289 context = kmalloc(sizeof(*context), GFP_KERNEL);
290 if (!context)
291 return ERR_PTR(-ENOMEM);
292 return &context->ibucontext;
Dennis Dalessandroc4ed7d82016-01-06 09:55:39 -0800293}
294
295/**
296 *rvt_dealloc_ucontext - Free a user context
297 *@context - Free this
298 */
299static int rvt_dealloc_ucontext(struct ib_ucontext *context)
300{
Harish Chegondi6c43cf42016-01-22 12:50:05 -0800301 kfree(to_iucontext(context));
302 return 0;
Dennis Dalessandroc4ed7d82016-01-06 09:55:39 -0800303}
304
Dennis Dalessandroe6a88182016-01-06 09:59:38 -0800305static int rvt_get_port_immutable(struct ib_device *ibdev, u8 port_num,
306 struct ib_port_immutable *immutable)
307{
Harish Chegondi61a650c2016-02-03 14:15:20 -0800308 struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
309 struct ib_port_attr attr;
310 int err, port_index;
311
312 port_index = ibport_num_to_idx(ibdev, port_num);
313 if (port_index < 0)
314 return -EINVAL;
315
316 err = rvt_query_port(ibdev, port_num, &attr);
317 if (err)
318 return err;
319
320 immutable->pkey_tbl_len = attr.pkey_tbl_len;
321 immutable->gid_tbl_len = attr.gid_tbl_len;
322 immutable->core_cap_flags = rdi->dparms.core_cap_flags;
323 immutable->max_mad_size = rdi->dparms.max_mad_size;
324
325 return 0;
Dennis Dalessandroe6a88182016-01-06 09:59:38 -0800326}
327
Dennis Dalessandro1348d702016-02-14 12:10:37 -0800328enum {
329 MISC,
330 QUERY_DEVICE,
331 MODIFY_DEVICE,
332 QUERY_PORT,
333 MODIFY_PORT,
334 QUERY_PKEY,
335 QUERY_GID,
336 ALLOC_UCONTEXT,
337 DEALLOC_UCONTEXT,
338 GET_PORT_IMMUTABLE,
339 CREATE_QP,
340 MODIFY_QP,
341 DESTROY_QP,
342 QUERY_QP,
343 POST_SEND,
344 POST_RECV,
345 POST_SRQ_RECV,
346 CREATE_AH,
347 DESTROY_AH,
348 MODIFY_AH,
349 QUERY_AH,
350 CREATE_SRQ,
351 MODIFY_SRQ,
352 DESTROY_SRQ,
353 QUERY_SRQ,
354 ATTACH_MCAST,
355 DETACH_MCAST,
356 GET_DMA_MR,
357 REG_USER_MR,
358 DEREG_MR,
359 ALLOC_MR,
360 ALLOC_FMR,
361 MAP_PHYS_FMR,
362 UNMAP_FMR,
363 DEALLOC_FMR,
364 MMAP,
365 CREATE_CQ,
366 DESTROY_CQ,
367 POLL_CQ,
368 REQ_NOTFIY_CQ,
369 RESIZE_CQ,
370 ALLOC_PD,
371 DEALLOC_PD,
372 _VERB_IDX_MAX /* Must always be last! */
373};
374
375static inline int check_driver_override(struct rvt_dev_info *rdi,
376 size_t offset, void *func)
377{
378 if (!*(void **)((void *)&rdi->ibdev + offset)) {
379 *(void **)((void *)&rdi->ibdev + offset) = func;
380 return 0;
381 }
382
383 return 1;
384}
385
386static int check_support(struct rvt_dev_info *rdi, int verb)
387{
388 switch (verb) {
389 case MISC:
390 /*
391 * These functions are not part of verbs specifically but are
392 * required for rdmavt to function.
393 */
394 if ((!rdi->driver_f.port_callback) ||
395 (!rdi->driver_f.get_card_name) ||
396 (!rdi->driver_f.get_pci_dev))
397 return -EINVAL;
398 break;
399
400 case QUERY_DEVICE:
401 check_driver_override(rdi, offsetof(struct ib_device,
402 query_device),
403 rvt_query_device);
404 break;
405
406 case MODIFY_DEVICE:
407 /*
408 * rdmavt does not support modify device currently drivers must
409 * provide.
410 */
411 if (!check_driver_override(rdi, offsetof(struct ib_device,
412 modify_device),
413 rvt_modify_device))
414 return -EOPNOTSUPP;
415 break;
416
417 case QUERY_PORT:
418 if (!check_driver_override(rdi, offsetof(struct ib_device,
419 query_port),
420 rvt_query_port))
421 if (!rdi->driver_f.query_port_state)
422 return -EINVAL;
423 break;
424
425 case MODIFY_PORT:
426 if (!check_driver_override(rdi, offsetof(struct ib_device,
427 modify_port),
428 rvt_modify_port))
429 if (!rdi->driver_f.cap_mask_chg ||
430 !rdi->driver_f.shut_down_port)
431 return -EINVAL;
432 break;
433
434 case QUERY_PKEY:
435 check_driver_override(rdi, offsetof(struct ib_device,
436 query_pkey),
437 rvt_query_pkey);
438 break;
439
440 case QUERY_GID:
441 if (!check_driver_override(rdi, offsetof(struct ib_device,
442 query_gid),
443 rvt_query_gid))
444 if (!rdi->driver_f.get_guid_be)
445 return -EINVAL;
446 break;
447
448 case ALLOC_UCONTEXT:
449 check_driver_override(rdi, offsetof(struct ib_device,
450 alloc_ucontext),
451 rvt_alloc_ucontext);
452 break;
453
454 case DEALLOC_UCONTEXT:
455 check_driver_override(rdi, offsetof(struct ib_device,
456 dealloc_ucontext),
457 rvt_dealloc_ucontext);
458 break;
459
460 case GET_PORT_IMMUTABLE:
461 check_driver_override(rdi, offsetof(struct ib_device,
462 get_port_immutable),
463 rvt_get_port_immutable);
464 break;
465
466 case CREATE_QP:
467 if (!check_driver_override(rdi, offsetof(struct ib_device,
468 create_qp),
469 rvt_create_qp))
470 if (!rdi->driver_f.qp_priv_alloc ||
471 !rdi->driver_f.qp_priv_free ||
472 !rdi->driver_f.notify_qp_reset ||
473 !rdi->driver_f.flush_qp_waiters ||
474 !rdi->driver_f.stop_send_queue ||
475 !rdi->driver_f.quiesce_qp)
476 return -EINVAL;
477 break;
478
479 case MODIFY_QP:
480 if (!check_driver_override(rdi, offsetof(struct ib_device,
481 modify_qp),
482 rvt_modify_qp))
483 if (!rdi->driver_f.notify_qp_reset ||
484 !rdi->driver_f.schedule_send ||
485 !rdi->driver_f.get_pmtu_from_attr ||
486 !rdi->driver_f.flush_qp_waiters ||
487 !rdi->driver_f.stop_send_queue ||
488 !rdi->driver_f.quiesce_qp ||
489 !rdi->driver_f.notify_error_qp ||
490 !rdi->driver_f.mtu_from_qp ||
491 !rdi->driver_f.mtu_to_path_mtu ||
492 !rdi->driver_f.shut_down_port ||
493 !rdi->driver_f.cap_mask_chg)
494 return -EINVAL;
495 break;
496
497 case DESTROY_QP:
498 if (!check_driver_override(rdi, offsetof(struct ib_device,
499 destroy_qp),
500 rvt_destroy_qp))
501 if (!rdi->driver_f.qp_priv_free ||
502 !rdi->driver_f.notify_qp_reset ||
503 !rdi->driver_f.flush_qp_waiters ||
504 !rdi->driver_f.stop_send_queue ||
505 !rdi->driver_f.quiesce_qp)
506 return -EINVAL;
507 break;
508
509 case QUERY_QP:
510 check_driver_override(rdi, offsetof(struct ib_device,
511 query_qp),
512 rvt_query_qp);
513 break;
514
515 case POST_SEND:
516 if (!check_driver_override(rdi, offsetof(struct ib_device,
517 post_send),
518 rvt_post_send))
519 if (!rdi->driver_f.schedule_send ||
520 !rdi->driver_f.do_send)
521 return -EINVAL;
522 break;
523
524 case POST_RECV:
525 check_driver_override(rdi, offsetof(struct ib_device,
526 post_recv),
527 rvt_post_recv);
528 break;
529 case POST_SRQ_RECV:
530 check_driver_override(rdi, offsetof(struct ib_device,
531 post_srq_recv),
532 rvt_post_srq_recv);
533 break;
534
535 case CREATE_AH:
536 check_driver_override(rdi, offsetof(struct ib_device,
537 create_ah),
538 rvt_create_ah);
539 break;
540
541 case DESTROY_AH:
542 check_driver_override(rdi, offsetof(struct ib_device,
543 destroy_ah),
544 rvt_destroy_ah);
545 break;
546
547 case MODIFY_AH:
548 check_driver_override(rdi, offsetof(struct ib_device,
549 modify_ah),
550 rvt_modify_ah);
551 break;
552
553 case QUERY_AH:
554 check_driver_override(rdi, offsetof(struct ib_device,
555 query_ah),
556 rvt_query_ah);
557 break;
558
559 case CREATE_SRQ:
560 check_driver_override(rdi, offsetof(struct ib_device,
561 create_srq),
562 rvt_create_srq);
563 break;
564
565 case MODIFY_SRQ:
566 check_driver_override(rdi, offsetof(struct ib_device,
567 modify_srq),
568 rvt_modify_srq);
569 break;
570
571 case DESTROY_SRQ:
572 check_driver_override(rdi, offsetof(struct ib_device,
573 destroy_srq),
574 rvt_destroy_srq);
575 break;
576
577 case QUERY_SRQ:
578 check_driver_override(rdi, offsetof(struct ib_device,
579 query_srq),
580 rvt_query_srq);
581 break;
582
583 case ATTACH_MCAST:
584 check_driver_override(rdi, offsetof(struct ib_device,
585 attach_mcast),
586 rvt_attach_mcast);
587 break;
588
589 case DETACH_MCAST:
590 check_driver_override(rdi, offsetof(struct ib_device,
591 detach_mcast),
592 rvt_detach_mcast);
593 break;
594
595 case GET_DMA_MR:
596 check_driver_override(rdi, offsetof(struct ib_device,
597 get_dma_mr),
598 rvt_get_dma_mr);
599 break;
600
601 case REG_USER_MR:
602 check_driver_override(rdi, offsetof(struct ib_device,
603 reg_user_mr),
604 rvt_reg_user_mr);
605 break;
606
607 case DEREG_MR:
608 check_driver_override(rdi, offsetof(struct ib_device,
609 dereg_mr),
610 rvt_dereg_mr);
611 break;
612
613 case ALLOC_FMR:
614 check_driver_override(rdi, offsetof(struct ib_device,
615 alloc_fmr),
616 rvt_alloc_fmr);
617 break;
618
619 case ALLOC_MR:
620 check_driver_override(rdi, offsetof(struct ib_device,
621 alloc_mr),
622 rvt_alloc_mr);
623 break;
624
625 case MAP_PHYS_FMR:
626 check_driver_override(rdi, offsetof(struct ib_device,
627 map_phys_fmr),
628 rvt_map_phys_fmr);
629 break;
630
631 case UNMAP_FMR:
632 check_driver_override(rdi, offsetof(struct ib_device,
633 unmap_fmr),
634 rvt_unmap_fmr);
635 break;
636
637 case DEALLOC_FMR:
638 check_driver_override(rdi, offsetof(struct ib_device,
639 dealloc_fmr),
640 rvt_dealloc_fmr);
641 break;
642
643 case MMAP:
644 check_driver_override(rdi, offsetof(struct ib_device,
645 mmap),
646 rvt_mmap);
647 break;
648
649 case CREATE_CQ:
650 check_driver_override(rdi, offsetof(struct ib_device,
651 create_cq),
652 rvt_create_cq);
653 break;
654
655 case DESTROY_CQ:
656 check_driver_override(rdi, offsetof(struct ib_device,
657 destroy_cq),
658 rvt_destroy_cq);
659 break;
660
661 case POLL_CQ:
662 check_driver_override(rdi, offsetof(struct ib_device,
663 poll_cq),
664 rvt_poll_cq);
665 break;
666
667 case REQ_NOTFIY_CQ:
668 check_driver_override(rdi, offsetof(struct ib_device,
669 req_notify_cq),
670 rvt_req_notify_cq);
671 break;
672
673 case RESIZE_CQ:
674 check_driver_override(rdi, offsetof(struct ib_device,
675 resize_cq),
676 rvt_resize_cq);
677 break;
678
679 case ALLOC_PD:
680 check_driver_override(rdi, offsetof(struct ib_device,
681 alloc_pd),
682 rvt_alloc_pd);
683 break;
684
685 case DEALLOC_PD:
686 check_driver_override(rdi, offsetof(struct ib_device,
687 dealloc_pd),
688 rvt_dealloc_pd);
689 break;
690
691 default:
692 return -EINVAL;
693 }
694
695 return 0;
696}
Dennis Dalessandro49978702016-01-06 09:52:40 -0800697
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800698/**
699 * rvt_register_device - register a driver
700 * @rdi: main dev structure for all of rdmavt operations
701 *
702 * It is up to drivers to allocate the rdi and fill in the appropriate
703 * information.
704 *
705 * Return: 0 on success otherwise an errno.
706 */
Dennis Dalessandro01946212016-01-06 09:50:24 -0800707int rvt_register_device(struct rvt_dev_info *rdi)
708{
Dennis Dalessandro1348d702016-02-14 12:10:37 -0800709 int ret = 0, i;
Dennis Dalessandro7b1e2092016-01-06 10:03:31 -0800710
Dennis Dalessandro01946212016-01-06 09:50:24 -0800711 if (!rdi)
712 return -EINVAL;
713
Dennis Dalessandro1348d702016-02-14 12:10:37 -0800714 /*
715 * Check to ensure drivers have setup the required helpers for the verbs
716 * they want rdmavt to handle
717 */
718 for (i = 0; i < _VERB_IDX_MAX; i++)
719 if (check_support(rdi, i)) {
720 pr_err("Driver support req not met at %d\n", i);
721 return -EINVAL;
722 }
723
Dennis Dalessandrob5348752016-01-06 10:02:59 -0800724
Dennis Dalessandro81ba39a2016-01-22 13:00:28 -0800725 /* Once we get past here we can use rvt_pr macros and tracepoints */
726 trace_rvt_dbg(rdi, "Driver attempting registration");
Dennis Dalessandro822514d2016-01-06 10:04:57 -0800727 rvt_mmap_init(rdi);
Dennis Dalessandrob5348752016-01-06 10:02:59 -0800728
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800729 /* Queue Pairs */
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800730 ret = rvt_driver_qp_init(rdi);
731 if (ret) {
732 pr_err("Error in driver QP init.\n");
733 return -EINVAL;
734 }
735
Dennis Dalessandro4c1e4972016-01-06 09:56:41 -0800736 /* Address Handle */
Kamal Heib119a8e72016-01-06 10:03:59 -0800737 spin_lock_init(&rdi->n_ahs_lock);
738 rdi->n_ahs_allocated = 0;
Dennis Dalessandro4c1e4972016-01-06 09:56:41 -0800739
Dennis Dalessandroaad91582016-01-06 09:57:58 -0800740 /* Shared Receive Queue */
Jubin Johnb8f881b2016-02-03 14:14:36 -0800741 rvt_driver_srq_init(rdi);
Dennis Dalessandroaad91582016-01-06 09:57:58 -0800742
Dennis Dalessandro9fa25172016-01-06 09:58:23 -0800743 /* Multicast */
Dennis Dalessandro4e740802016-01-22 13:00:55 -0800744 rvt_driver_mcast_init(rdi);
Dennis Dalessandro9fa25172016-01-06 09:58:23 -0800745
Dennis Dalessandro2a055eb2016-01-06 09:57:21 -0800746 /* Mem Region */
Dennis Dalessandro7b1e2092016-01-06 10:03:31 -0800747 ret = rvt_driver_mr_init(rdi);
748 if (ret) {
Dennis Dalessandro36055a02016-01-06 10:03:39 -0800749 pr_err("Error in driver MR init.\n");
Dennis Dalessandro7b1e2092016-01-06 10:03:31 -0800750 goto bail_no_mr;
751 }
752
Dennis Dalessandrocf163352016-01-06 10:00:42 -0800753 /* Completion queues */
Dennis Dalessandro6f6387a2016-01-22 13:00:15 -0800754 ret = rvt_driver_cq_init(rdi);
755 if (ret) {
756 pr_err("Error in driver CQ init.\n");
757 goto bail_mr;
758 }
Dennis Dalessandrocf163352016-01-06 10:00:42 -0800759
Dennis Dalessandro8afd32e2016-01-06 09:51:48 -0800760 /* DMA Operations */
Dennis Dalessandroc1b332b2016-01-06 09:51:18 -0800761 rdi->ibdev.dma_ops =
762 rdi->ibdev.dma_ops ? : &rvt_default_dma_mapping_ops;
763
Dennis Dalessandro8afd32e2016-01-06 09:51:48 -0800764 /* Protection Domain */
Dennis Dalessandro8afd32e2016-01-06 09:51:48 -0800765 spin_lock_init(&rdi->n_pds_lock);
766 rdi->n_pds_allocated = 0;
767
Dennis Dalessandro182285d2016-01-22 13:01:01 -0800768 /*
769 * There are some things which could be set by underlying drivers but
770 * really should be up to rdmavt to set. For instance drivers can't know
771 * exactly which functions rdmavt supports, nor do they know the ABI
772 * version, so we do all of this sort of stuff here.
773 */
774 rdi->ibdev.uverbs_abi_ver = RVT_UVERBS_ABI_VERSION;
775 rdi->ibdev.uverbs_cmd_mask =
776 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
777 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
778 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
779 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
780 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
781 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
782 (1ull << IB_USER_VERBS_CMD_MODIFY_AH) |
783 (1ull << IB_USER_VERBS_CMD_QUERY_AH) |
784 (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
785 (1ull << IB_USER_VERBS_CMD_REG_MR) |
786 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
787 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
788 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
789 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
790 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
791 (1ull << IB_USER_VERBS_CMD_POLL_CQ) |
792 (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) |
793 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
794 (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
795 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
796 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
797 (1ull << IB_USER_VERBS_CMD_POST_SEND) |
798 (1ull << IB_USER_VERBS_CMD_POST_RECV) |
799 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
800 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
801 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
802 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
803 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
804 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
805 (1ull << IB_USER_VERBS_CMD_POST_SRQ_RECV);
806 rdi->ibdev.node_type = RDMA_NODE_IB_CA;
807 rdi->ibdev.num_comp_vectors = 1;
808
Dennis Dalessandro8afd32e2016-01-06 09:51:48 -0800809 /* We are now good to announce we exist */
Dennis Dalessandro7b1e2092016-01-06 10:03:31 -0800810 ret = ib_register_device(&rdi->ibdev, rdi->driver_f.port_callback);
811 if (ret) {
812 rvt_pr_err(rdi, "Failed to register driver with ib core.\n");
Dennis Dalessandro6f6387a2016-01-22 13:00:15 -0800813 goto bail_cq;
Dennis Dalessandro7b1e2092016-01-06 10:03:31 -0800814 }
815
Dennis Dalessandro3711baf2016-01-22 13:04:51 -0800816 rvt_create_mad_agents(rdi);
817
Dennis Dalessandro7b1e2092016-01-06 10:03:31 -0800818 rvt_pr_info(rdi, "Registration with rdmavt done.\n");
819 return ret;
820
Dennis Dalessandro6f6387a2016-01-22 13:00:15 -0800821bail_cq:
822 rvt_cq_exit(rdi);
823
Dennis Dalessandro7b1e2092016-01-06 10:03:31 -0800824bail_mr:
825 rvt_mr_exit(rdi);
826
827bail_no_mr:
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800828 rvt_qp_exit(rdi);
829
Dennis Dalessandro7b1e2092016-01-06 10:03:31 -0800830 return ret;
Dennis Dalessandro01946212016-01-06 09:50:24 -0800831}
832EXPORT_SYMBOL(rvt_register_device);
833
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800834/**
835 * rvt_unregister_device - remove a driver
836 * @rdi: rvt dev struct
837 */
Dennis Dalessandro01946212016-01-06 09:50:24 -0800838void rvt_unregister_device(struct rvt_dev_info *rdi)
839{
Dennis Dalessandro81ba39a2016-01-22 13:00:28 -0800840 trace_rvt_dbg(rdi, "Driver is unregistering.");
Dennis Dalessandro01946212016-01-06 09:50:24 -0800841 if (!rdi)
842 return;
843
Dennis Dalessandro3711baf2016-01-22 13:04:51 -0800844 rvt_free_mad_agents(rdi);
845
Dennis Dalessandro01946212016-01-06 09:50:24 -0800846 ib_unregister_device(&rdi->ibdev);
Dennis Dalessandro6f6387a2016-01-22 13:00:15 -0800847 rvt_cq_exit(rdi);
Dennis Dalessandro7b1e2092016-01-06 10:03:31 -0800848 rvt_mr_exit(rdi);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800849 rvt_qp_exit(rdi);
Dennis Dalessandro01946212016-01-06 09:50:24 -0800850}
851EXPORT_SYMBOL(rvt_unregister_device);
Dennis Dalessandrof3d01bb2016-01-06 10:04:13 -0800852
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800853/**
854 * rvt_init_port - init internal data for driver port
855 * @rdi: rvt dev strut
856 * @port: rvt port
857 * @port_index: 0 based index of ports, different from IB core port num
858 *
Dennis Dalessandrof3d01bb2016-01-06 10:04:13 -0800859 * Keep track of a list of ports. No need to have a detach port.
860 * They persist until the driver goes away.
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800861 *
862 * Return: always 0
Dennis Dalessandrof3d01bb2016-01-06 10:04:13 -0800863 */
Dennis Dalessandro38ce2c62016-01-06 10:05:12 -0800864int rvt_init_port(struct rvt_dev_info *rdi, struct rvt_ibport *port,
Dennis Dalessandrof1badc72016-02-03 14:15:02 -0800865 int port_index, u16 *pkey_table)
Dennis Dalessandrof3d01bb2016-01-06 10:04:13 -0800866{
Dennis Dalessandro38ce2c62016-01-06 10:05:12 -0800867
Dennis Dalessandrof1badc72016-02-03 14:15:02 -0800868 rdi->ports[port_index] = port;
869 rdi->ports[port_index]->pkey_table = pkey_table;
Dennis Dalessandro38ce2c62016-01-06 10:05:12 -0800870
871 return 0;
Dennis Dalessandrof3d01bb2016-01-06 10:04:13 -0800872}
Dennis Dalessandro38ce2c62016-01-06 10:05:12 -0800873EXPORT_SYMBOL(rvt_init_port);