blob: 136cc212caa57ea80fd8c2499097d48c56a992ff [file] [log] [blame]
Dennis Dalessandro01946212016-01-06 09:50:24 -08001/*
2 * Copyright(c) 2015 Intel Corporation.
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>
50#include "vt.h"
51
52MODULE_LICENSE("Dual BSD/GPL");
53MODULE_DESCRIPTION("RDMA Verbs Transport Library");
54
55static int rvt_init(void)
56{
57 /* Do any work needed prior to drivers calling for registration*/
58 return 0;
59}
60module_init(rvt_init);
61
62static void rvt_cleanup(void)
63{
64}
65module_exit(rvt_cleanup);
66
Dennis Dalessandro19ef1ed2016-01-06 09:53:05 -080067static int rvt_query_device(struct ib_device *ibdev,
68 struct ib_device_attr *props,
69 struct ib_udata *uhw)
70{
Harish Chegondifeaeb6e2016-01-22 12:50:36 -080071 struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
72
73 if (uhw->inlen || uhw->outlen)
74 return -EINVAL;
Dennis Dalessandro19ef1ed2016-01-06 09:53:05 -080075 /*
Harish Chegondifeaeb6e2016-01-22 12:50:36 -080076 * Return rvt_dev_info.dparms.props contents
Dennis Dalessandro19ef1ed2016-01-06 09:53:05 -080077 */
Harish Chegondifeaeb6e2016-01-22 12:50:36 -080078 *props = rdi->dparms.props;
79 return 0;
Dennis Dalessandro19ef1ed2016-01-06 09:53:05 -080080}
81
82static int rvt_modify_device(struct ib_device *device,
83 int device_modify_mask,
84 struct ib_device_modify *device_modify)
85{
86 /*
87 * Change dev props. Planned support is for node desc change and sys
88 * guid change only. This matches hfi1 and qib behavior. Other drivers
89 * that support existing modifications will need to add their support.
90 */
91
92 /*
93 * VT-DRIVER-API: node_desc_change()
94 * VT-DRIVER-API: sys_guid_change()
95 */
96 return -EOPNOTSUPP;
97}
98
Dennis Dalessandro765525c2016-01-06 09:54:07 -080099/**
100 * rvt_query_port: Passes the query port call to the driver
101 * @ibdev: Verbs IB dev
102 * @port: port number
103 * @props: structure to hold returned properties
104 *
105 * Returns 0 on success
106 */
107static int rvt_query_port(struct ib_device *ibdev, u8 port,
108 struct ib_port_attr *props)
109{
110 /*
111 * VT-DRIVER-API: query_port_state()
112 * driver returns pretty much everything in ib_port_attr
113 */
114 return -EOPNOTSUPP;
115}
116
117/**
118 * rvt_modify_port
119 * @ibdev: Verbs IB dev
120 * @port: Port number
121 * @port_modify_mask: How to change the port
122 * @props: Structure to fill in
123 *
124 * Returns 0 on success
125 */
126static int rvt_modify_port(struct ib_device *ibdev, u8 port,
127 int port_modify_mask, struct ib_port_modify *props)
128{
129 /*
130 * VT-DRIVER-API: set_link_state()
131 * driver will set the link state using the IB enumeration
132 *
133 * VT-DRIVER-API: clear_qkey_violations()
134 * clears driver private qkey counter
135 *
136 * VT-DRIVER-API: get_lid()
137 * driver needs to return the LID
138 *
139 * TBD: send_trap() and post_mad_send() need examined to see where they
140 * fit in.
141 */
142 return -EOPNOTSUPP;
143}
144
Dennis Dalessandro30588642016-01-06 09:54:16 -0800145/**
146 * rvt_query_pkey - Return a pkey from the table at a given index
147 * @ibdev: Verbs IB dev
148 * @port: Port number
149 * @intex: Index into pkey table
150 *
151 * Returns 0 on failure pkey otherwise
152 */
153static int rvt_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
154 u16 *pkey)
155{
156 /*
157 * Driver will be responsible for keeping rvt_dev_info.pkey_table up to
158 * date. This function will just return that value. There is no need to
159 * lock, if a stale value is read and sent to the user so be it there is
160 * no way to protect against that anyway.
161 */
Dennis Dalessandro38ce2c62016-01-06 10:05:12 -0800162 struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
163 int port_index;
164
165 if (index >= rvt_get_npkeys(rdi))
166 return -EINVAL;
167
168 port_index = port - 1; /* IB ports start at 1 our array at 0 */
169 if ((port_index < 0) || (port_index >= rdi->dparms.nports))
170 return -EINVAL;
171
172 *pkey = rvt_get_pkey(rdi, port_index, index);
Dennis Dalessandro30588642016-01-06 09:54:16 -0800173 return 0;
174}
175
Dennis Dalessandro2d092e12016-01-06 09:54:50 -0800176/**
177 * rvt_query_gid - Return a gid from the table
178 * @ibdev: Verbs IB dev
179 * @port: Port number
180 * @index: = Index in table
181 * @gid: Gid to return
182 *
183 * Returns 0 on success
184 */
185static int rvt_query_gid(struct ib_device *ibdev, u8 port,
186 int index, union ib_gid *gid)
187{
188 /*
189 * Driver is responsible for updating the guid table. Which will be used
190 * to craft the return value. This will work similar to how query_pkey()
191 * is being done.
192 */
193
194 return -EOPNOTSUPP;
195}
196
Harish Chegondi6c43cf42016-01-22 12:50:05 -0800197struct rvt_ucontext {
198 struct ib_ucontext ibucontext;
199};
200
201static inline struct rvt_ucontext *to_iucontext(struct ib_ucontext
202 *ibucontext)
203{
204 return container_of(ibucontext, struct rvt_ucontext, ibucontext);
205}
206
Dennis Dalessandroc4ed7d82016-01-06 09:55:39 -0800207/**
208 * rvt_alloc_ucontext - Allocate a user context
209 * @ibdev: Vers IB dev
210 * @data: User data allocated
211 */
212static struct ib_ucontext *rvt_alloc_ucontext(struct ib_device *ibdev,
213 struct ib_udata *udata)
214{
Harish Chegondi6c43cf42016-01-22 12:50:05 -0800215 struct rvt_ucontext *context;
216
217 context = kmalloc(sizeof(*context), GFP_KERNEL);
218 if (!context)
219 return ERR_PTR(-ENOMEM);
220 return &context->ibucontext;
Dennis Dalessandroc4ed7d82016-01-06 09:55:39 -0800221}
222
223/**
224 *rvt_dealloc_ucontext - Free a user context
225 *@context - Free this
226 */
227static int rvt_dealloc_ucontext(struct ib_ucontext *context)
228{
Harish Chegondi6c43cf42016-01-22 12:50:05 -0800229 kfree(to_iucontext(context));
230 return 0;
Dennis Dalessandroc4ed7d82016-01-06 09:55:39 -0800231}
232
Dennis Dalessandroe6a88182016-01-06 09:59:38 -0800233static int rvt_get_port_immutable(struct ib_device *ibdev, u8 port_num,
234 struct ib_port_immutable *immutable)
235{
236 return -EOPNOTSUPP;
237}
238
Dennis Dalessandro49978702016-01-06 09:52:40 -0800239/*
240 * Check driver override. If driver passes a value use it, otherwise we use our
241 * own value.
242 */
243#define CHECK_DRIVER_OVERRIDE(rdi, x) \
244 rdi->ibdev.x = rdi->ibdev.x ? : rvt_ ##x
245
Dennis Dalessandro01946212016-01-06 09:50:24 -0800246int rvt_register_device(struct rvt_dev_info *rdi)
247{
Dennis Dalessandrob5348752016-01-06 10:02:59 -0800248 /* Validate that drivers have provided the right information */
Dennis Dalessandro7b1e2092016-01-06 10:03:31 -0800249 int ret = 0;
250
Dennis Dalessandro01946212016-01-06 09:50:24 -0800251 if (!rdi)
252 return -EINVAL;
253
Dennis Dalessandrob5348752016-01-06 10:02:59 -0800254 if ((!rdi->driver_f.port_callback) ||
255 (!rdi->driver_f.get_card_name) ||
Kamal Heib119a8e72016-01-06 10:03:59 -0800256 (!rdi->driver_f.get_pci_dev) ||
257 (!rdi->driver_f.check_ah)) {
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800258 pr_err("Driver not supporting req func\n");
Dennis Dalessandrob5348752016-01-06 10:02:59 -0800259 return -EINVAL;
260 }
261
262 /* Once we get past here we can use the rvt_pr macros */
Dennis Dalessandro822514d2016-01-06 10:04:57 -0800263 rvt_mmap_init(rdi);
Dennis Dalessandrob5348752016-01-06 10:02:59 -0800264
Dennis Dalessandro19ef1ed2016-01-06 09:53:05 -0800265 /* Dev Ops */
266 CHECK_DRIVER_OVERRIDE(rdi, query_device);
267 CHECK_DRIVER_OVERRIDE(rdi, modify_device);
Dennis Dalessandro765525c2016-01-06 09:54:07 -0800268 CHECK_DRIVER_OVERRIDE(rdi, query_port);
269 CHECK_DRIVER_OVERRIDE(rdi, modify_port);
Dennis Dalessandro30588642016-01-06 09:54:16 -0800270 CHECK_DRIVER_OVERRIDE(rdi, query_pkey);
Dennis Dalessandro2d092e12016-01-06 09:54:50 -0800271 CHECK_DRIVER_OVERRIDE(rdi, query_gid);
Dennis Dalessandroc4ed7d82016-01-06 09:55:39 -0800272 CHECK_DRIVER_OVERRIDE(rdi, alloc_ucontext);
273 CHECK_DRIVER_OVERRIDE(rdi, dealloc_ucontext);
Dennis Dalessandroe6a88182016-01-06 09:59:38 -0800274 CHECK_DRIVER_OVERRIDE(rdi, get_port_immutable);
Dennis Dalessandro19ef1ed2016-01-06 09:53:05 -0800275
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800276 /* Queue Pairs */
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800277 ret = rvt_driver_qp_init(rdi);
278 if (ret) {
279 pr_err("Error in driver QP init.\n");
280 return -EINVAL;
281 }
282
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800283 CHECK_DRIVER_OVERRIDE(rdi, create_qp);
284 CHECK_DRIVER_OVERRIDE(rdi, modify_qp);
285 CHECK_DRIVER_OVERRIDE(rdi, destroy_qp);
286 CHECK_DRIVER_OVERRIDE(rdi, query_qp);
Dennis Dalessandro8cf40202016-01-06 10:01:17 -0800287 CHECK_DRIVER_OVERRIDE(rdi, post_send);
288 CHECK_DRIVER_OVERRIDE(rdi, post_recv);
289 CHECK_DRIVER_OVERRIDE(rdi, post_srq_recv);
Dennis Dalessandrob518d3e2016-01-06 09:56:15 -0800290
Dennis Dalessandro4c1e4972016-01-06 09:56:41 -0800291 /* Address Handle */
292 CHECK_DRIVER_OVERRIDE(rdi, create_ah);
293 CHECK_DRIVER_OVERRIDE(rdi, destroy_ah);
294 CHECK_DRIVER_OVERRIDE(rdi, modify_ah);
295 CHECK_DRIVER_OVERRIDE(rdi, query_ah);
Kamal Heib119a8e72016-01-06 10:03:59 -0800296 spin_lock_init(&rdi->n_ahs_lock);
297 rdi->n_ahs_allocated = 0;
Dennis Dalessandro4c1e4972016-01-06 09:56:41 -0800298
Dennis Dalessandroaad91582016-01-06 09:57:58 -0800299 /* Shared Receive Queue */
300 CHECK_DRIVER_OVERRIDE(rdi, create_srq);
301 CHECK_DRIVER_OVERRIDE(rdi, modify_srq);
302 CHECK_DRIVER_OVERRIDE(rdi, destroy_srq);
303 CHECK_DRIVER_OVERRIDE(rdi, query_srq);
304
Dennis Dalessandro9fa25172016-01-06 09:58:23 -0800305 /* Multicast */
306 CHECK_DRIVER_OVERRIDE(rdi, attach_mcast);
307 CHECK_DRIVER_OVERRIDE(rdi, detach_mcast);
308
Dennis Dalessandro2a055eb2016-01-06 09:57:21 -0800309 /* Mem Region */
Dennis Dalessandro7b1e2092016-01-06 10:03:31 -0800310 ret = rvt_driver_mr_init(rdi);
311 if (ret) {
Dennis Dalessandro36055a02016-01-06 10:03:39 -0800312 pr_err("Error in driver MR init.\n");
Dennis Dalessandro7b1e2092016-01-06 10:03:31 -0800313 goto bail_no_mr;
314 }
315
Dennis Dalessandro2a055eb2016-01-06 09:57:21 -0800316 CHECK_DRIVER_OVERRIDE(rdi, get_dma_mr);
317 CHECK_DRIVER_OVERRIDE(rdi, reg_user_mr);
318 CHECK_DRIVER_OVERRIDE(rdi, dereg_mr);
319 CHECK_DRIVER_OVERRIDE(rdi, alloc_mr);
320 CHECK_DRIVER_OVERRIDE(rdi, alloc_fmr);
321 CHECK_DRIVER_OVERRIDE(rdi, map_phys_fmr);
322 CHECK_DRIVER_OVERRIDE(rdi, unmap_fmr);
323 CHECK_DRIVER_OVERRIDE(rdi, dealloc_fmr);
Dennis Dalessandrodc217522016-01-06 09:59:04 -0800324 CHECK_DRIVER_OVERRIDE(rdi, mmap);
Dennis Dalessandro2a055eb2016-01-06 09:57:21 -0800325
Dennis Dalessandrocf163352016-01-06 10:00:42 -0800326 /* Completion queues */
Dennis Dalessandro6f6387a2016-01-22 13:00:15 -0800327 ret = rvt_driver_cq_init(rdi);
328 if (ret) {
329 pr_err("Error in driver CQ init.\n");
330 goto bail_mr;
331 }
Dennis Dalessandrocf163352016-01-06 10:00:42 -0800332 CHECK_DRIVER_OVERRIDE(rdi, create_cq);
333 CHECK_DRIVER_OVERRIDE(rdi, destroy_cq);
334 CHECK_DRIVER_OVERRIDE(rdi, poll_cq);
335 CHECK_DRIVER_OVERRIDE(rdi, req_notify_cq);
336 CHECK_DRIVER_OVERRIDE(rdi, resize_cq);
337
Dennis Dalessandro8afd32e2016-01-06 09:51:48 -0800338 /* DMA Operations */
Dennis Dalessandroc1b332b2016-01-06 09:51:18 -0800339 rdi->ibdev.dma_ops =
340 rdi->ibdev.dma_ops ? : &rvt_default_dma_mapping_ops;
341
Dennis Dalessandro8afd32e2016-01-06 09:51:48 -0800342 /* Protection Domain */
Dennis Dalessandro49978702016-01-06 09:52:40 -0800343 CHECK_DRIVER_OVERRIDE(rdi, alloc_pd);
344 CHECK_DRIVER_OVERRIDE(rdi, dealloc_pd);
Dennis Dalessandro8afd32e2016-01-06 09:51:48 -0800345 spin_lock_init(&rdi->n_pds_lock);
346 rdi->n_pds_allocated = 0;
347
348 /* We are now good to announce we exist */
Dennis Dalessandro7b1e2092016-01-06 10:03:31 -0800349 ret = ib_register_device(&rdi->ibdev, rdi->driver_f.port_callback);
350 if (ret) {
351 rvt_pr_err(rdi, "Failed to register driver with ib core.\n");
Dennis Dalessandro6f6387a2016-01-22 13:00:15 -0800352 goto bail_cq;
Dennis Dalessandro7b1e2092016-01-06 10:03:31 -0800353 }
354
355 rvt_pr_info(rdi, "Registration with rdmavt done.\n");
356 return ret;
357
Dennis Dalessandro6f6387a2016-01-22 13:00:15 -0800358bail_cq:
359 rvt_cq_exit(rdi);
360
Dennis Dalessandro7b1e2092016-01-06 10:03:31 -0800361bail_mr:
362 rvt_mr_exit(rdi);
363
364bail_no_mr:
Dennis Dalessandro0acb0cc2016-01-06 10:04:46 -0800365 rvt_qp_exit(rdi);
366
Dennis Dalessandro7b1e2092016-01-06 10:03:31 -0800367 return ret;
Dennis Dalessandro01946212016-01-06 09:50:24 -0800368}
369EXPORT_SYMBOL(rvt_register_device);
370
371void rvt_unregister_device(struct rvt_dev_info *rdi)
372{
373 if (!rdi)
374 return;
375
376 ib_unregister_device(&rdi->ibdev);
Dennis Dalessandro6f6387a2016-01-22 13:00:15 -0800377 rvt_cq_exit(rdi);
Dennis Dalessandro7b1e2092016-01-06 10:03:31 -0800378 rvt_mr_exit(rdi);
Dennis Dalessandro515667f2016-01-22 12:50:17 -0800379 rvt_qp_exit(rdi);
Dennis Dalessandro01946212016-01-06 09:50:24 -0800380}
381EXPORT_SYMBOL(rvt_unregister_device);
Dennis Dalessandrof3d01bb2016-01-06 10:04:13 -0800382
383/*
384 * Keep track of a list of ports. No need to have a detach port.
385 * They persist until the driver goes away.
386 */
Dennis Dalessandro38ce2c62016-01-06 10:05:12 -0800387int rvt_init_port(struct rvt_dev_info *rdi, struct rvt_ibport *port,
388 int portnum, u16 *pkey_table)
Dennis Dalessandrof3d01bb2016-01-06 10:04:13 -0800389{
Dennis Dalessandro38ce2c62016-01-06 10:05:12 -0800390 if (!rdi->dparms.nports) {
391 rvt_pr_err(rdi, "Driver says it has no ports.\n");
392 return -EINVAL;
393 }
394
395 rdi->ports = kcalloc(rdi->dparms.nports,
396 sizeof(struct rvt_ibport **),
397 GFP_KERNEL);
398 if (!rdi->ports) {
399 rvt_pr_err(rdi, "Could not allocate port mem.\n");
400 return -ENOMEM;
401 }
402
Dennis Dalessandrof3d01bb2016-01-06 10:04:13 -0800403 rdi->ports[portnum] = port;
Dennis Dalessandro38ce2c62016-01-06 10:05:12 -0800404 rdi->ports[portnum]->pkey_table = pkey_table;
405
406 return 0;
Dennis Dalessandrof3d01bb2016-01-06 10:04:13 -0800407}
Dennis Dalessandro38ce2c62016-01-06 10:05:12 -0800408EXPORT_SYMBOL(rvt_init_port);