blob: a96d4aa80ae87fe2447cb8b9f0066866ce87e042 [file] [log] [blame]
Dennis Dalessandro4c1e4972016-01-06 09:56:41 -08001/*
Dennis Dalessandrofe314192016-01-22 13:04:58 -08002 * Copyright(c) 2016 Intel Corporation.
Dennis Dalessandro4c1e4972016-01-06 09:56:41 -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
Kamal Heib119a8e72016-01-06 10:03:59 -080048#include <linux/slab.h>
Dennis Dalessandro4c1e4972016-01-06 09:56:41 -080049#include "ah.h"
Kamal Heib119a8e72016-01-06 10:03:59 -080050#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
Dennis Dalessandro90793f72016-02-14 12:10:29 -080056 *
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
Kamal Heib119a8e72016-01-06 10:03:59 -080061 */
62int rvt_check_ah(struct ib_device *ibdev,
Dasaratharaman Chandramouli90898852017-04-29 14:41:18 -040063 struct rdma_ah_attr *ah_attr)
Kamal Heib119a8e72016-01-06 10:03:59 -080064{
65 int err;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -040066 int port_num = rdma_ah_get_port_num(ah_attr);
Kamal Heib119a8e72016-01-06 10:03:59 -080067 struct ib_port_attr port_attr;
68 struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -040069 enum rdma_link_layer link = rdma_port_get_link_layer(ibdev, port_num);
70 u32 dlid = rdma_ah_get_dlid(ah_attr);
71 u8 ah_flags = rdma_ah_get_ah_flags(ah_attr);
72 u8 static_rate = rdma_ah_get_static_rate(ah_attr);
Kamal Heib119a8e72016-01-06 10:03:59 -080073
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -040074 err = ib_query_port(ibdev, port_num, &port_attr);
Kamal Heib119a8e72016-01-06 10:03:59 -080075 if (err)
76 return -EINVAL;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -040077 if (port_num < 1 ||
78 port_num > ibdev->phys_port_cnt)
Kamal Heib119a8e72016-01-06 10:03:59 -080079 return -EINVAL;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -040080 if (static_rate != IB_RATE_PORT_CURRENT &&
81 ib_rate_to_mbps(static_rate) < 0)
Kamal Heib119a8e72016-01-06 10:03:59 -080082 return -EINVAL;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -040083 if ((ah_flags & IB_AH_GRH) &&
84 rdma_ah_read_grh(ah_attr)->sgid_index >= port_attr.gid_tbl_len)
Kamal Heib119a8e72016-01-06 10:03:59 -080085 return -EINVAL;
86 if (link != IB_LINK_LAYER_ETHERNET) {
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -040087 if (dlid == 0)
Kamal Heib119a8e72016-01-06 10:03:59 -080088 return -EINVAL;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -040089 if (dlid >= be16_to_cpu(IB_MULTICAST_LID_BASE) &&
90 dlid != be16_to_cpu(IB_LID_PERMISSIVE) &&
91 !(ah_flags & IB_AH_GRH))
Kamal Heib119a8e72016-01-06 10:03:59 -080092 return -EINVAL;
93 }
Dennis Dalessandrob036db82016-01-06 10:04:23 -080094 if (rdi->driver_f.check_ah)
95 return rdi->driver_f.check_ah(ibdev, ah_attr);
Kamal Heib119a8e72016-01-06 10:03:59 -080096 return 0;
97}
98EXPORT_SYMBOL(rvt_check_ah);
Dennis Dalessandro4c1e4972016-01-06 09:56:41 -080099
100/**
101 * rvt_create_ah - create an address handle
102 * @pd: the protection domain
103 * @ah_attr: the attributes of the AH
104 *
105 * This may be called from interrupt context.
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800106 *
107 * Return: newly allocated ah
Dennis Dalessandro4c1e4972016-01-06 09:56:41 -0800108 */
109struct ib_ah *rvt_create_ah(struct ib_pd *pd,
Dasaratharaman Chandramouli90898852017-04-29 14:41:18 -0400110 struct rdma_ah_attr *ah_attr)
Dennis Dalessandro4c1e4972016-01-06 09:56:41 -0800111{
Kamal Heib119a8e72016-01-06 10:03:59 -0800112 struct rvt_ah *ah;
113 struct rvt_dev_info *dev = ib_to_rvt(pd->device);
114 unsigned long flags;
115
116 if (rvt_check_ah(pd->device, ah_attr))
117 return ERR_PTR(-EINVAL);
118
119 ah = kmalloc(sizeof(*ah), GFP_ATOMIC);
120 if (!ah)
121 return ERR_PTR(-ENOMEM);
122
123 spin_lock_irqsave(&dev->n_ahs_lock, flags);
124 if (dev->n_ahs_allocated == dev->dparms.props.max_ah) {
Dan Carpenterf0bb2d42017-04-27 12:14:20 +0300125 spin_unlock_irqrestore(&dev->n_ahs_lock, flags);
Kamal Heib119a8e72016-01-06 10:03:59 -0800126 kfree(ah);
127 return ERR_PTR(-ENOMEM);
128 }
129
130 dev->n_ahs_allocated++;
131 spin_unlock_irqrestore(&dev->n_ahs_lock, flags);
132
133 ah->attr = *ah_attr;
134 atomic_set(&ah->refcount, 0);
135
Dennis Dalessandrob036db82016-01-06 10:04:23 -0800136 if (dev->driver_f.notify_new_ah)
137 dev->driver_f.notify_new_ah(pd->device, ah_attr, ah);
138
Kamal Heib119a8e72016-01-06 10:03:59 -0800139 return &ah->ibah;
Dennis Dalessandro4c1e4972016-01-06 09:56:41 -0800140}
141
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800142/**
143 * rvt_destory_ah - Destory an address handle
144 * @ibah: address handle
145 *
146 * Return: 0 on success
147 */
Dennis Dalessandro4c1e4972016-01-06 09:56:41 -0800148int rvt_destroy_ah(struct ib_ah *ibah)
149{
Kamal Heib119a8e72016-01-06 10:03:59 -0800150 struct rvt_dev_info *dev = ib_to_rvt(ibah->device);
151 struct rvt_ah *ah = ibah_to_rvtah(ibah);
152 unsigned long flags;
153
154 if (atomic_read(&ah->refcount) != 0)
155 return -EBUSY;
156
157 spin_lock_irqsave(&dev->n_ahs_lock, flags);
158 dev->n_ahs_allocated--;
159 spin_unlock_irqrestore(&dev->n_ahs_lock, flags);
160
161 kfree(ah);
162
163 return 0;
Dennis Dalessandro4c1e4972016-01-06 09:56:41 -0800164}
165
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800166/**
167 * rvt_modify_ah - modify an ah with given attrs
168 * @ibah: address handle to modify
169 * @ah_attr: attrs to apply
170 *
171 * Return: 0 on success
172 */
Dasaratharaman Chandramouli90898852017-04-29 14:41:18 -0400173int rvt_modify_ah(struct ib_ah *ibah, struct rdma_ah_attr *ah_attr)
Dennis Dalessandro4c1e4972016-01-06 09:56:41 -0800174{
Kamal Heib119a8e72016-01-06 10:03:59 -0800175 struct rvt_ah *ah = ibah_to_rvtah(ibah);
176
177 if (rvt_check_ah(ibah->device, ah_attr))
178 return -EINVAL;
179
180 ah->attr = *ah_attr;
181
182 return 0;
Dennis Dalessandro4c1e4972016-01-06 09:56:41 -0800183}
184
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800185/**
186 * rvt_query_ah - return attrs for ah
187 * @ibah: address handle to query
188 * @ah_attr: return info in this
189 *
190 * Return: always 0
191 */
Dasaratharaman Chandramouli90898852017-04-29 14:41:18 -0400192int rvt_query_ah(struct ib_ah *ibah, struct rdma_ah_attr *ah_attr)
Dennis Dalessandro4c1e4972016-01-06 09:56:41 -0800193{
Kamal Heib119a8e72016-01-06 10:03:59 -0800194 struct rvt_ah *ah = ibah_to_rvtah(ibah);
195
196 *ah_attr = ah->attr;
197
198 return 0;
Dennis Dalessandro4c1e4972016-01-06 09:56:41 -0800199}