]>
Commit | Line | Data |
---|---|---|
29c8d9eb AR |
1 | /* |
2 | * Copyright (c) 2012-2016 VMware, Inc. All rights reserved. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of EITHER the GNU General Public License | |
6 | * version 2 as published by the Free Software Foundation or the BSD | |
7 | * 2-Clause License. This program is distributed in the hope that it | |
8 | * will be useful, but WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED | |
9 | * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. | |
10 | * See the GNU General Public License version 2 for more details at | |
11 | * http://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License | |
14 | * along with this program available in the file COPYING in the main | |
15 | * directory of this source tree. | |
16 | * | |
17 | * The BSD 2-Clause License | |
18 | * | |
19 | * Redistribution and use in source and binary forms, with or | |
20 | * without modification, are permitted provided that the following | |
21 | * conditions are met: | |
22 | * | |
23 | * - Redistributions of source code must retain the above | |
24 | * copyright notice, this list of conditions and the following | |
25 | * disclaimer. | |
26 | * | |
27 | * - Redistributions in binary form must reproduce the above | |
28 | * copyright notice, this list of conditions and the following | |
29 | * disclaimer in the documentation and/or other materials | |
30 | * provided with the distribution. | |
31 | * | |
32 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
33 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
34 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
35 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
36 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
37 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
38 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
39 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
40 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
41 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
42 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
43 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
44 | */ | |
45 | ||
46 | #include <asm/page.h> | |
47 | #include <linux/inet.h> | |
48 | #include <linux/io.h> | |
49 | #include <rdma/ib_addr.h> | |
50 | #include <rdma/ib_smi.h> | |
51 | #include <rdma/ib_user_verbs.h> | |
52 | #include <rdma/vmw_pvrdma-abi.h> | |
53 | ||
54 | #include "pvrdma.h" | |
55 | ||
56 | /** | |
57 | * pvrdma_query_device - query device | |
58 | * @ibdev: the device to query | |
59 | * @props: the device properties | |
60 | * @uhw: user data | |
61 | * | |
62 | * @return: 0 on success, otherwise negative errno | |
63 | */ | |
64 | int pvrdma_query_device(struct ib_device *ibdev, | |
65 | struct ib_device_attr *props, | |
66 | struct ib_udata *uhw) | |
67 | { | |
68 | struct pvrdma_dev *dev = to_vdev(ibdev); | |
69 | ||
70 | if (uhw->inlen || uhw->outlen) | |
71 | return -EINVAL; | |
72 | ||
29c8d9eb AR |
73 | props->fw_ver = dev->dsr->caps.fw_ver; |
74 | props->sys_image_guid = dev->dsr->caps.sys_image_guid; | |
75 | props->max_mr_size = dev->dsr->caps.max_mr_size; | |
76 | props->page_size_cap = dev->dsr->caps.page_size_cap; | |
77 | props->vendor_id = dev->dsr->caps.vendor_id; | |
78 | props->vendor_part_id = dev->pdev->device; | |
79 | props->hw_ver = dev->dsr->caps.hw_ver; | |
80 | props->max_qp = dev->dsr->caps.max_qp; | |
81 | props->max_qp_wr = dev->dsr->caps.max_qp_wr; | |
82 | props->device_cap_flags = dev->dsr->caps.device_cap_flags; | |
33023fb8 SW |
83 | props->max_send_sge = dev->dsr->caps.max_sge; |
84 | props->max_recv_sge = dev->dsr->caps.max_sge; | |
a31a2a3b AR |
85 | props->max_sge_rd = PVRDMA_GET_CAP(dev, dev->dsr->caps.max_sge, |
86 | dev->dsr->caps.max_sge_rd); | |
8b10ba78 BT |
87 | props->max_srq = dev->dsr->caps.max_srq; |
88 | props->max_srq_wr = dev->dsr->caps.max_srq_wr; | |
89 | props->max_srq_sge = dev->dsr->caps.max_srq_sge; | |
29c8d9eb AR |
90 | props->max_cq = dev->dsr->caps.max_cq; |
91 | props->max_cqe = dev->dsr->caps.max_cqe; | |
92 | props->max_mr = dev->dsr->caps.max_mr; | |
93 | props->max_pd = dev->dsr->caps.max_pd; | |
94 | props->max_qp_rd_atom = dev->dsr->caps.max_qp_rd_atom; | |
95 | props->max_qp_init_rd_atom = dev->dsr->caps.max_qp_init_rd_atom; | |
96 | props->atomic_cap = | |
97 | dev->dsr->caps.atomic_ops & | |
98 | (PVRDMA_ATOMIC_OP_COMP_SWAP | PVRDMA_ATOMIC_OP_FETCH_ADD) ? | |
99 | IB_ATOMIC_HCA : IB_ATOMIC_NONE; | |
100 | props->masked_atomic_cap = props->atomic_cap; | |
101 | props->max_ah = dev->dsr->caps.max_ah; | |
102 | props->max_pkeys = dev->dsr->caps.max_pkeys; | |
103 | props->local_ca_ack_delay = dev->dsr->caps.local_ca_ack_delay; | |
104 | if ((dev->dsr->caps.bmme_flags & PVRDMA_BMME_FLAG_LOCAL_INV) && | |
105 | (dev->dsr->caps.bmme_flags & PVRDMA_BMME_FLAG_REMOTE_INV) && | |
106 | (dev->dsr->caps.bmme_flags & PVRDMA_BMME_FLAG_FAST_REG_WR)) { | |
107 | props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS; | |
a31a2a3b AR |
108 | props->max_fast_reg_page_list_len = PVRDMA_GET_CAP(dev, |
109 | PVRDMA_MAX_FAST_REG_PAGES, | |
110 | dev->dsr->caps.max_fast_reg_page_list_len); | |
29c8d9eb AR |
111 | } |
112 | ||
a31a2a3b AR |
113 | props->device_cap_flags |= IB_DEVICE_PORT_ACTIVE_EVENT | |
114 | IB_DEVICE_RC_RNR_NAK_GEN; | |
115 | ||
29c8d9eb AR |
116 | return 0; |
117 | } | |
118 | ||
119 | /** | |
120 | * pvrdma_query_port - query device port attributes | |
121 | * @ibdev: the device to query | |
122 | * @port: the port number | |
123 | * @props: the device properties | |
124 | * | |
125 | * @return: 0 on success, otherwise negative errno | |
126 | */ | |
127 | int pvrdma_query_port(struct ib_device *ibdev, u8 port, | |
128 | struct ib_port_attr *props) | |
129 | { | |
130 | struct pvrdma_dev *dev = to_vdev(ibdev); | |
131 | union pvrdma_cmd_req req; | |
132 | union pvrdma_cmd_resp rsp; | |
133 | struct pvrdma_cmd_query_port *cmd = &req.query_port; | |
134 | struct pvrdma_cmd_query_port_resp *resp = &rsp.query_port_resp; | |
135 | int err; | |
136 | ||
137 | memset(cmd, 0, sizeof(*cmd)); | |
138 | cmd->hdr.cmd = PVRDMA_CMD_QUERY_PORT; | |
139 | cmd->port_num = port; | |
140 | ||
141 | err = pvrdma_cmd_post(dev, &req, &rsp, PVRDMA_CMD_QUERY_PORT_RESP); | |
142 | if (err < 0) { | |
143 | dev_warn(&dev->pdev->dev, | |
144 | "could not query port, error: %d\n", err); | |
145 | return err; | |
146 | } | |
147 | ||
c4550c63 | 148 | /* props being zeroed by the caller, avoid zeroing it here */ |
29c8d9eb AR |
149 | |
150 | props->state = pvrdma_port_state_to_ib(resp->attrs.state); | |
151 | props->max_mtu = pvrdma_mtu_to_ib(resp->attrs.max_mtu); | |
152 | props->active_mtu = pvrdma_mtu_to_ib(resp->attrs.active_mtu); | |
153 | props->gid_tbl_len = resp->attrs.gid_tbl_len; | |
154 | props->port_cap_flags = | |
155 | pvrdma_port_cap_flags_to_ib(resp->attrs.port_cap_flags); | |
2f944c0f JG |
156 | props->port_cap_flags |= IB_PORT_CM_SUP; |
157 | props->ip_gids = true; | |
29c8d9eb AR |
158 | props->max_msg_sz = resp->attrs.max_msg_sz; |
159 | props->bad_pkey_cntr = resp->attrs.bad_pkey_cntr; | |
160 | props->qkey_viol_cntr = resp->attrs.qkey_viol_cntr; | |
161 | props->pkey_tbl_len = resp->attrs.pkey_tbl_len; | |
162 | props->lid = resp->attrs.lid; | |
163 | props->sm_lid = resp->attrs.sm_lid; | |
164 | props->lmc = resp->attrs.lmc; | |
165 | props->max_vl_num = resp->attrs.max_vl_num; | |
166 | props->sm_sl = resp->attrs.sm_sl; | |
167 | props->subnet_timeout = resp->attrs.subnet_timeout; | |
168 | props->init_type_reply = resp->attrs.init_type_reply; | |
169 | props->active_width = pvrdma_port_width_to_ib(resp->attrs.active_width); | |
170 | props->active_speed = pvrdma_port_speed_to_ib(resp->attrs.active_speed); | |
171 | props->phys_state = resp->attrs.phys_state; | |
172 | ||
173 | return 0; | |
174 | } | |
175 | ||
176 | /** | |
177 | * pvrdma_query_gid - query device gid | |
178 | * @ibdev: the device to query | |
179 | * @port: the port number | |
180 | * @index: the index | |
181 | * @gid: the device gid value | |
182 | * | |
183 | * @return: 0 on success, otherwise negative errno | |
184 | */ | |
185 | int pvrdma_query_gid(struct ib_device *ibdev, u8 port, int index, | |
186 | union ib_gid *gid) | |
187 | { | |
188 | struct pvrdma_dev *dev = to_vdev(ibdev); | |
189 | ||
190 | if (index >= dev->dsr->caps.gid_tbl_len) | |
191 | return -EINVAL; | |
192 | ||
193 | memcpy(gid, &dev->sgid_tbl[index], sizeof(union ib_gid)); | |
194 | ||
195 | return 0; | |
196 | } | |
197 | ||
198 | /** | |
199 | * pvrdma_query_pkey - query device port's P_Key table | |
200 | * @ibdev: the device to query | |
201 | * @port: the port number | |
202 | * @index: the index | |
203 | * @pkey: the device P_Key value | |
204 | * | |
205 | * @return: 0 on success, otherwise negative errno | |
206 | */ | |
207 | int pvrdma_query_pkey(struct ib_device *ibdev, u8 port, u16 index, | |
208 | u16 *pkey) | |
209 | { | |
210 | int err = 0; | |
211 | union pvrdma_cmd_req req; | |
212 | union pvrdma_cmd_resp rsp; | |
213 | struct pvrdma_cmd_query_pkey *cmd = &req.query_pkey; | |
214 | ||
215 | memset(cmd, 0, sizeof(*cmd)); | |
216 | cmd->hdr.cmd = PVRDMA_CMD_QUERY_PKEY; | |
217 | cmd->port_num = port; | |
218 | cmd->index = index; | |
219 | ||
220 | err = pvrdma_cmd_post(to_vdev(ibdev), &req, &rsp, | |
221 | PVRDMA_CMD_QUERY_PKEY_RESP); | |
222 | if (err < 0) { | |
223 | dev_warn(&to_vdev(ibdev)->pdev->dev, | |
224 | "could not query pkey, error: %d\n", err); | |
225 | return err; | |
226 | } | |
227 | ||
228 | *pkey = rsp.query_pkey_resp.pkey; | |
229 | ||
230 | return 0; | |
231 | } | |
232 | ||
233 | enum rdma_link_layer pvrdma_port_link_layer(struct ib_device *ibdev, | |
234 | u8 port) | |
235 | { | |
236 | return IB_LINK_LAYER_ETHERNET; | |
237 | } | |
238 | ||
239 | int pvrdma_modify_device(struct ib_device *ibdev, int mask, | |
240 | struct ib_device_modify *props) | |
241 | { | |
242 | unsigned long flags; | |
243 | ||
244 | if (mask & ~(IB_DEVICE_MODIFY_SYS_IMAGE_GUID | | |
245 | IB_DEVICE_MODIFY_NODE_DESC)) { | |
246 | dev_warn(&to_vdev(ibdev)->pdev->dev, | |
247 | "unsupported device modify mask %#x\n", mask); | |
248 | return -EOPNOTSUPP; | |
249 | } | |
250 | ||
251 | if (mask & IB_DEVICE_MODIFY_NODE_DESC) { | |
252 | spin_lock_irqsave(&to_vdev(ibdev)->desc_lock, flags); | |
253 | memcpy(ibdev->node_desc, props->node_desc, 64); | |
254 | spin_unlock_irqrestore(&to_vdev(ibdev)->desc_lock, flags); | |
255 | } | |
256 | ||
257 | if (mask & IB_DEVICE_MODIFY_SYS_IMAGE_GUID) { | |
258 | mutex_lock(&to_vdev(ibdev)->port_mutex); | |
259 | to_vdev(ibdev)->sys_image_guid = | |
260 | cpu_to_be64(props->sys_image_guid); | |
261 | mutex_unlock(&to_vdev(ibdev)->port_mutex); | |
262 | } | |
263 | ||
264 | return 0; | |
265 | } | |
266 | ||
267 | /** | |
268 | * pvrdma_modify_port - modify device port attributes | |
269 | * @ibdev: the device to modify | |
270 | * @port: the port number | |
271 | * @mask: attributes to modify | |
272 | * @props: the device properties | |
273 | * | |
274 | * @return: 0 on success, otherwise negative errno | |
275 | */ | |
276 | int pvrdma_modify_port(struct ib_device *ibdev, u8 port, int mask, | |
277 | struct ib_port_modify *props) | |
278 | { | |
279 | struct ib_port_attr attr; | |
280 | struct pvrdma_dev *vdev = to_vdev(ibdev); | |
281 | int ret; | |
282 | ||
283 | if (mask & ~IB_PORT_SHUTDOWN) { | |
284 | dev_warn(&vdev->pdev->dev, | |
285 | "unsupported port modify mask %#x\n", mask); | |
286 | return -EOPNOTSUPP; | |
287 | } | |
288 | ||
289 | mutex_lock(&vdev->port_mutex); | |
c4550c63 | 290 | ret = ib_query_port(ibdev, port, &attr); |
29c8d9eb AR |
291 | if (ret) |
292 | goto out; | |
293 | ||
294 | vdev->port_cap_mask |= props->set_port_cap_mask; | |
295 | vdev->port_cap_mask &= ~props->clr_port_cap_mask; | |
296 | ||
297 | if (mask & IB_PORT_SHUTDOWN) | |
298 | vdev->ib_active = false; | |
299 | ||
300 | out: | |
301 | mutex_unlock(&vdev->port_mutex); | |
302 | return ret; | |
303 | } | |
304 | ||
305 | /** | |
306 | * pvrdma_alloc_ucontext - allocate ucontext | |
a2a074ef | 307 | * @uctx: the uverbs countext |
29c8d9eb AR |
308 | * @udata: user data |
309 | * | |
a2a074ef | 310 | * @return: zero on success, otherwise errno. |
29c8d9eb | 311 | */ |
a2a074ef | 312 | int pvrdma_alloc_ucontext(struct ib_ucontext *uctx, struct ib_udata *udata) |
29c8d9eb | 313 | { |
a2a074ef | 314 | struct ib_device *ibdev = uctx->device; |
29c8d9eb | 315 | struct pvrdma_dev *vdev = to_vdev(ibdev); |
a2a074ef LR |
316 | struct pvrdma_ucontext *context = to_vucontext(uctx); |
317 | union pvrdma_cmd_req req = {}; | |
318 | union pvrdma_cmd_resp rsp = {}; | |
29c8d9eb AR |
319 | struct pvrdma_cmd_create_uc *cmd = &req.create_uc; |
320 | struct pvrdma_cmd_create_uc_resp *resp = &rsp.create_uc_resp; | |
a2a074ef | 321 | struct pvrdma_alloc_ucontext_resp uresp = {}; |
29c8d9eb | 322 | int ret; |
29c8d9eb AR |
323 | |
324 | if (!vdev->ib_active) | |
a2a074ef | 325 | return -EAGAIN; |
29c8d9eb AR |
326 | |
327 | context->dev = vdev; | |
328 | ret = pvrdma_uar_alloc(vdev, &context->uar); | |
a2a074ef LR |
329 | if (ret) |
330 | return -ENOMEM; | |
29c8d9eb AR |
331 | |
332 | /* get ctx_handle from host */ | |
8aa04ad3 AR |
333 | if (vdev->dsr_version < PVRDMA_PPN64_VERSION) |
334 | cmd->pfn = context->uar.pfn; | |
335 | else | |
336 | cmd->pfn64 = context->uar.pfn; | |
337 | ||
29c8d9eb AR |
338 | cmd->hdr.cmd = PVRDMA_CMD_CREATE_UC; |
339 | ret = pvrdma_cmd_post(vdev, &req, &rsp, PVRDMA_CMD_CREATE_UC_RESP); | |
340 | if (ret < 0) { | |
341 | dev_warn(&vdev->pdev->dev, | |
342 | "could not create ucontext, error: %d\n", ret); | |
29c8d9eb AR |
343 | goto err; |
344 | } | |
345 | ||
346 | context->ctx_handle = resp->ctx_handle; | |
347 | ||
348 | /* copy back to user */ | |
349 | uresp.qp_tab_size = vdev->dsr->caps.max_qp; | |
350 | ret = ib_copy_to_udata(udata, &uresp, sizeof(uresp)); | |
351 | if (ret) { | |
352 | pvrdma_uar_free(vdev, &context->uar); | |
29c8d9eb | 353 | pvrdma_dealloc_ucontext(&context->ibucontext); |
a2a074ef | 354 | return -EFAULT; |
29c8d9eb AR |
355 | } |
356 | ||
a2a074ef | 357 | return 0; |
29c8d9eb AR |
358 | |
359 | err: | |
360 | pvrdma_uar_free(vdev, &context->uar); | |
a2a074ef | 361 | return ret; |
29c8d9eb AR |
362 | } |
363 | ||
364 | /** | |
365 | * pvrdma_dealloc_ucontext - deallocate ucontext | |
366 | * @ibcontext: the ucontext | |
29c8d9eb | 367 | */ |
a2a074ef | 368 | void pvrdma_dealloc_ucontext(struct ib_ucontext *ibcontext) |
29c8d9eb AR |
369 | { |
370 | struct pvrdma_ucontext *context = to_vucontext(ibcontext); | |
a2a074ef | 371 | union pvrdma_cmd_req req = {}; |
29c8d9eb AR |
372 | struct pvrdma_cmd_destroy_uc *cmd = &req.destroy_uc; |
373 | int ret; | |
374 | ||
29c8d9eb AR |
375 | cmd->hdr.cmd = PVRDMA_CMD_DESTROY_UC; |
376 | cmd->ctx_handle = context->ctx_handle; | |
377 | ||
378 | ret = pvrdma_cmd_post(context->dev, &req, NULL, 0); | |
379 | if (ret < 0) | |
380 | dev_warn(&context->dev->pdev->dev, | |
381 | "destroy ucontext failed, error: %d\n", ret); | |
382 | ||
383 | /* Free the UAR even if the device command failed */ | |
384 | pvrdma_uar_free(to_vdev(ibcontext->device), &context->uar); | |
29c8d9eb AR |
385 | } |
386 | ||
387 | /** | |
388 | * pvrdma_mmap - create mmap region | |
389 | * @ibcontext: the user context | |
390 | * @vma: the VMA | |
391 | * | |
392 | * @return: 0 on success, otherwise errno. | |
393 | */ | |
394 | int pvrdma_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma) | |
395 | { | |
396 | struct pvrdma_ucontext *context = to_vucontext(ibcontext); | |
397 | unsigned long start = vma->vm_start; | |
398 | unsigned long size = vma->vm_end - vma->vm_start; | |
399 | unsigned long offset = vma->vm_pgoff << PAGE_SHIFT; | |
400 | ||
401 | dev_dbg(&context->dev->pdev->dev, "create mmap region\n"); | |
402 | ||
403 | if ((size != PAGE_SIZE) || (offset & ~PAGE_MASK)) { | |
404 | dev_warn(&context->dev->pdev->dev, | |
405 | "invalid params for mmap region\n"); | |
406 | return -EINVAL; | |
407 | } | |
408 | ||
409 | /* Map UAR to kernel space, VM_LOCKED? */ | |
410 | vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND; | |
411 | vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); | |
412 | if (io_remap_pfn_range(vma, start, context->uar.pfn, size, | |
413 | vma->vm_page_prot)) | |
414 | return -EAGAIN; | |
415 | ||
416 | return 0; | |
417 | } | |
418 | ||
419 | /** | |
420 | * pvrdma_alloc_pd - allocate protection domain | |
21a428a0 | 421 | * @ibpd: PD pointer |
29c8d9eb AR |
422 | * @context: user context |
423 | * @udata: user data | |
424 | * | |
425 | * @return: the ib_pd protection domain pointer on success, otherwise errno. | |
426 | */ | |
21a428a0 LR |
427 | int pvrdma_alloc_pd(struct ib_pd *ibpd, struct ib_ucontext *context, |
428 | struct ib_udata *udata) | |
29c8d9eb | 429 | { |
21a428a0 LR |
430 | struct ib_device *ibdev = ibpd->device; |
431 | struct pvrdma_pd *pd = to_vpd(ibpd); | |
29c8d9eb | 432 | struct pvrdma_dev *dev = to_vdev(ibdev); |
21a428a0 LR |
433 | union pvrdma_cmd_req req = {}; |
434 | union pvrdma_cmd_resp rsp = {}; | |
29c8d9eb AR |
435 | struct pvrdma_cmd_create_pd *cmd = &req.create_pd; |
436 | struct pvrdma_cmd_create_pd_resp *resp = &rsp.create_pd_resp; | |
1f5a6c47 | 437 | struct pvrdma_alloc_pd_resp pd_resp = {0}; |
29c8d9eb | 438 | int ret; |
29c8d9eb AR |
439 | |
440 | /* Check allowed max pds */ | |
441 | if (!atomic_add_unless(&dev->num_pds, 1, dev->dsr->caps.max_pd)) | |
21a428a0 | 442 | return -ENOMEM; |
29c8d9eb | 443 | |
29c8d9eb AR |
444 | cmd->hdr.cmd = PVRDMA_CMD_CREATE_PD; |
445 | cmd->ctx_handle = (context) ? to_vucontext(context)->ctx_handle : 0; | |
446 | ret = pvrdma_cmd_post(dev, &req, &rsp, PVRDMA_CMD_CREATE_PD_RESP); | |
447 | if (ret < 0) { | |
448 | dev_warn(&dev->pdev->dev, | |
449 | "failed to allocate protection domain, error: %d\n", | |
450 | ret); | |
21a428a0 | 451 | goto err; |
29c8d9eb AR |
452 | } |
453 | ||
454 | pd->privileged = !context; | |
455 | pd->pd_handle = resp->pd_handle; | |
456 | pd->pdn = resp->pd_handle; | |
1f5a6c47 | 457 | pd_resp.pdn = resp->pd_handle; |
29c8d9eb AR |
458 | |
459 | if (context) { | |
1f5a6c47 | 460 | if (ib_copy_to_udata(udata, &pd_resp, sizeof(pd_resp))) { |
29c8d9eb AR |
461 | dev_warn(&dev->pdev->dev, |
462 | "failed to copy back protection domain\n"); | |
c4367a26 | 463 | pvrdma_dealloc_pd(&pd->ibpd, udata); |
21a428a0 | 464 | return -EFAULT; |
29c8d9eb AR |
465 | } |
466 | } | |
467 | ||
468 | /* u32 pd handle */ | |
21a428a0 | 469 | return 0; |
29c8d9eb | 470 | |
29c8d9eb AR |
471 | err: |
472 | atomic_dec(&dev->num_pds); | |
21a428a0 | 473 | return ret; |
29c8d9eb AR |
474 | } |
475 | ||
476 | /** | |
477 | * pvrdma_dealloc_pd - deallocate protection domain | |
478 | * @pd: the protection domain to be released | |
c4367a26 | 479 | * @udata: user data or null for kernel object |
29c8d9eb AR |
480 | * |
481 | * @return: 0 on success, otherwise errno. | |
482 | */ | |
c4367a26 | 483 | void pvrdma_dealloc_pd(struct ib_pd *pd, struct ib_udata *udata) |
29c8d9eb AR |
484 | { |
485 | struct pvrdma_dev *dev = to_vdev(pd->device); | |
21a428a0 | 486 | union pvrdma_cmd_req req = {}; |
29c8d9eb AR |
487 | struct pvrdma_cmd_destroy_pd *cmd = &req.destroy_pd; |
488 | int ret; | |
489 | ||
29c8d9eb AR |
490 | cmd->hdr.cmd = PVRDMA_CMD_DESTROY_PD; |
491 | cmd->pd_handle = to_vpd(pd)->pd_handle; | |
492 | ||
493 | ret = pvrdma_cmd_post(dev, &req, NULL, 0); | |
494 | if (ret) | |
495 | dev_warn(&dev->pdev->dev, | |
496 | "could not dealloc protection domain, error: %d\n", | |
497 | ret); | |
498 | ||
29c8d9eb | 499 | atomic_dec(&dev->num_pds); |
29c8d9eb AR |
500 | } |
501 | ||
502 | /** | |
503 | * pvrdma_create_ah - create an address handle | |
504 | * @pd: the protection domain | |
505 | * @ah_attr: the attributes of the AH | |
506 | * @udata: user data blob | |
b090c4e3 | 507 | * @flags: create address handle flags (see enum rdma_create_ah_flags) |
29c8d9eb AR |
508 | * |
509 | * @return: the ib_ah pointer on success, otherwise errno. | |
510 | */ | |
90898850 | 511 | struct ib_ah *pvrdma_create_ah(struct ib_pd *pd, struct rdma_ah_attr *ah_attr, |
b090c4e3 | 512 | u32 flags, struct ib_udata *udata) |
29c8d9eb AR |
513 | { |
514 | struct pvrdma_dev *dev = to_vdev(pd->device); | |
515 | struct pvrdma_ah *ah; | |
d8966fcd DC |
516 | const struct ib_global_route *grh; |
517 | u8 port_num = rdma_ah_get_port_num(ah_attr); | |
29c8d9eb | 518 | |
d8966fcd | 519 | if (!(rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH)) |
29c8d9eb | 520 | return ERR_PTR(-EINVAL); |
29c8d9eb | 521 | |
44c58487 DC |
522 | grh = rdma_ah_read_grh(ah_attr); |
523 | if ((ah_attr->type != RDMA_AH_ATTR_TYPE_ROCE) || | |
d8966fcd | 524 | rdma_is_multicast_addr((struct in6_addr *)grh->dgid.raw)) |
29c8d9eb AR |
525 | return ERR_PTR(-EINVAL); |
526 | ||
527 | if (!atomic_add_unless(&dev->num_ahs, 1, dev->dsr->caps.max_ah)) | |
528 | return ERR_PTR(-ENOMEM); | |
529 | ||
a276a4d9 | 530 | ah = kzalloc(sizeof(*ah), GFP_ATOMIC); |
29c8d9eb AR |
531 | if (!ah) { |
532 | atomic_dec(&dev->num_ahs); | |
533 | return ERR_PTR(-ENOMEM); | |
534 | } | |
535 | ||
d8966fcd DC |
536 | ah->av.port_pd = to_vpd(pd)->pd_handle | (port_num << 24); |
537 | ah->av.src_path_bits = rdma_ah_get_path_bits(ah_attr); | |
29c8d9eb | 538 | ah->av.src_path_bits |= 0x80; |
d8966fcd DC |
539 | ah->av.gid_index = grh->sgid_index; |
540 | ah->av.hop_limit = grh->hop_limit; | |
541 | ah->av.sl_tclass_flowlabel = (grh->traffic_class << 20) | | |
542 | grh->flow_label; | |
543 | memcpy(ah->av.dgid, grh->dgid.raw, 16); | |
44c58487 | 544 | memcpy(ah->av.dmac, ah_attr->roce.dmac, ETH_ALEN); |
29c8d9eb AR |
545 | |
546 | ah->ibah.device = pd->device; | |
547 | ah->ibah.pd = pd; | |
548 | ah->ibah.uobject = NULL; | |
549 | ||
550 | return &ah->ibah; | |
551 | } | |
552 | ||
553 | /** | |
554 | * pvrdma_destroy_ah - destroy an address handle | |
555 | * @ah: the address handle to destroyed | |
2553ba21 | 556 | * @flags: destroy address handle flags (see enum rdma_destroy_ah_flags) |
29c8d9eb AR |
557 | * |
558 | * @return: 0 on success. | |
559 | */ | |
c4367a26 | 560 | int pvrdma_destroy_ah(struct ib_ah *ah, u32 flags, struct ib_udata *udata) |
29c8d9eb AR |
561 | { |
562 | struct pvrdma_dev *dev = to_vdev(ah->device); | |
563 | ||
564 | kfree(to_vah(ah)); | |
565 | atomic_dec(&dev->num_ahs); | |
566 | ||
567 | return 0; | |
568 | } |