blob: b938c4ffa99aa3656bb832dbd17115e25b06b861 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001/*
David Brazdil0f672f62019-12-10 10:32:29 +00002 * Copyright(c) 2016 - 2019 Intel Corporation.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003 *
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/slab.h>
49#include "ah.h"
50#include "vt.h" /* for prints */
51
52/**
53 * rvt_check_ah - validate the attributes of AH
54 * @ibdev: the ib device
55 * @ah_attr: the attributes of the AH
56 *
57 * If driver supports a more detailed check_ah function call back to it
58 * otherwise just check the basics.
59 *
60 * Return: 0 on success
61 */
62int rvt_check_ah(struct ib_device *ibdev,
63 struct rdma_ah_attr *ah_attr)
64{
65 int err;
66 int port_num = rdma_ah_get_port_num(ah_attr);
67 struct ib_port_attr port_attr;
68 struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
69 u8 ah_flags = rdma_ah_get_ah_flags(ah_attr);
70 u8 static_rate = rdma_ah_get_static_rate(ah_attr);
71
72 err = ib_query_port(ibdev, port_num, &port_attr);
73 if (err)
74 return -EINVAL;
75 if (port_num < 1 ||
76 port_num > ibdev->phys_port_cnt)
77 return -EINVAL;
78 if (static_rate != IB_RATE_PORT_CURRENT &&
79 ib_rate_to_mbps(static_rate) < 0)
80 return -EINVAL;
81 if ((ah_flags & IB_AH_GRH) &&
82 rdma_ah_read_grh(ah_attr)->sgid_index >= port_attr.gid_tbl_len)
83 return -EINVAL;
84 if (rdi->driver_f.check_ah)
85 return rdi->driver_f.check_ah(ibdev, ah_attr);
86 return 0;
87}
88EXPORT_SYMBOL(rvt_check_ah);
89
90/**
91 * rvt_create_ah - create an address handle
David Brazdil0f672f62019-12-10 10:32:29 +000092 * @ibah: the IB address handle
Olivier Deprez157378f2022-04-04 15:47:50 +020093 * @init_attr: the attributes of the AH
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000094 * @udata: pointer to user's input output buffer information.
95 *
96 * This may be called from interrupt context.
97 *
David Brazdil0f672f62019-12-10 10:32:29 +000098 * Return: 0 on success
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000099 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200100int rvt_create_ah(struct ib_ah *ibah, struct rdma_ah_init_attr *init_attr,
101 struct ib_udata *udata)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000102{
David Brazdil0f672f62019-12-10 10:32:29 +0000103 struct rvt_ah *ah = ibah_to_rvtah(ibah);
104 struct rvt_dev_info *dev = ib_to_rvt(ibah->device);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000105 unsigned long flags;
106
Olivier Deprez157378f2022-04-04 15:47:50 +0200107 if (rvt_check_ah(ibah->device, init_attr->ah_attr))
David Brazdil0f672f62019-12-10 10:32:29 +0000108 return -EINVAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000109
110 spin_lock_irqsave(&dev->n_ahs_lock, flags);
111 if (dev->n_ahs_allocated == dev->dparms.props.max_ah) {
112 spin_unlock_irqrestore(&dev->n_ahs_lock, flags);
David Brazdil0f672f62019-12-10 10:32:29 +0000113 return -ENOMEM;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000114 }
115
116 dev->n_ahs_allocated++;
117 spin_unlock_irqrestore(&dev->n_ahs_lock, flags);
118
Olivier Deprez157378f2022-04-04 15:47:50 +0200119 rdma_copy_ah_attr(&ah->attr, init_attr->ah_attr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000120
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000121 if (dev->driver_f.notify_new_ah)
Olivier Deprez157378f2022-04-04 15:47:50 +0200122 dev->driver_f.notify_new_ah(ibah->device,
123 init_attr->ah_attr, ah);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000124
David Brazdil0f672f62019-12-10 10:32:29 +0000125 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000126}
127
128/**
129 * rvt_destory_ah - Destory an address handle
130 * @ibah: address handle
David Brazdil0f672f62019-12-10 10:32:29 +0000131 * @destroy_flags: destroy address handle flags (see enum rdma_destroy_ah_flags)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000132 *
133 * Return: 0 on success
134 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200135int rvt_destroy_ah(struct ib_ah *ibah, u32 destroy_flags)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000136{
137 struct rvt_dev_info *dev = ib_to_rvt(ibah->device);
138 struct rvt_ah *ah = ibah_to_rvtah(ibah);
139 unsigned long flags;
140
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000141 spin_lock_irqsave(&dev->n_ahs_lock, flags);
142 dev->n_ahs_allocated--;
143 spin_unlock_irqrestore(&dev->n_ahs_lock, flags);
144
145 rdma_destroy_ah_attr(&ah->attr);
Olivier Deprez157378f2022-04-04 15:47:50 +0200146 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000147}
148
149/**
150 * rvt_modify_ah - modify an ah with given attrs
151 * @ibah: address handle to modify
152 * @ah_attr: attrs to apply
153 *
154 * Return: 0 on success
155 */
156int rvt_modify_ah(struct ib_ah *ibah, struct rdma_ah_attr *ah_attr)
157{
158 struct rvt_ah *ah = ibah_to_rvtah(ibah);
159
160 if (rvt_check_ah(ibah->device, ah_attr))
161 return -EINVAL;
162
163 ah->attr = *ah_attr;
164
165 return 0;
166}
167
168/**
169 * rvt_query_ah - return attrs for ah
170 * @ibah: address handle to query
171 * @ah_attr: return info in this
172 *
173 * Return: always 0
174 */
175int rvt_query_ah(struct ib_ah *ibah, struct rdma_ah_attr *ah_attr)
176{
177 struct rvt_ah *ah = ibah_to_rvtah(ibah);
178
179 *ah_attr = ah->attr;
180
181 return 0;
182}