blob: 621afc305183b6f28c9b70ef6bba31389a1f9db9 [file] [log] [blame]
Dennis Dalessandro4c1e4972016-01-06 09:56:41 -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
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
56 */
57int rvt_check_ah(struct ib_device *ibdev,
58 struct ib_ah_attr *ah_attr)
59{
60 int err;
61 struct ib_port_attr port_attr;
62 struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
63 enum rdma_link_layer link = rdma_port_get_link_layer(ibdev,
64 ah_attr->port_num);
65
66 err = ib_query_port(ibdev, ah_attr->port_num, &port_attr);
67 if (err)
68 return -EINVAL;
69 if (ah_attr->port_num < 1 ||
70 ah_attr->port_num > ibdev->phys_port_cnt)
71 return -EINVAL;
72 if (ah_attr->static_rate != IB_RATE_PORT_CURRENT &&
73 ib_rate_to_mbps(ah_attr->static_rate) < 0)
74 return -EINVAL;
75 if ((ah_attr->ah_flags & IB_AH_GRH) &&
76 ah_attr->grh.sgid_index >= port_attr.gid_tbl_len)
77 return -EINVAL;
78 if (link != IB_LINK_LAYER_ETHERNET) {
79 if (ah_attr->dlid == 0)
80 return -EINVAL;
81 if (ah_attr->dlid >= RVT_MULTICAST_LID_BASE &&
82 ah_attr->dlid != RVT_PERMISSIVE_LID &&
83 !(ah_attr->ah_flags & IB_AH_GRH))
84 return -EINVAL;
85 }
Dennis Dalessandrob036db82016-01-06 10:04:23 -080086 if (rdi->driver_f.check_ah)
87 return rdi->driver_f.check_ah(ibdev, ah_attr);
Kamal Heib119a8e72016-01-06 10:03:59 -080088 return 0;
89}
90EXPORT_SYMBOL(rvt_check_ah);
Dennis Dalessandro4c1e4972016-01-06 09:56:41 -080091
92/**
93 * rvt_create_ah - create an address handle
94 * @pd: the protection domain
95 * @ah_attr: the attributes of the AH
96 *
97 * This may be called from interrupt context.
98 */
99struct ib_ah *rvt_create_ah(struct ib_pd *pd,
100 struct ib_ah_attr *ah_attr)
101{
Kamal Heib119a8e72016-01-06 10:03:59 -0800102 struct rvt_ah *ah;
103 struct rvt_dev_info *dev = ib_to_rvt(pd->device);
104 unsigned long flags;
105
106 if (rvt_check_ah(pd->device, ah_attr))
107 return ERR_PTR(-EINVAL);
108
109 ah = kmalloc(sizeof(*ah), GFP_ATOMIC);
110 if (!ah)
111 return ERR_PTR(-ENOMEM);
112
113 spin_lock_irqsave(&dev->n_ahs_lock, flags);
114 if (dev->n_ahs_allocated == dev->dparms.props.max_ah) {
115 spin_unlock(&dev->n_ahs_lock);
116 kfree(ah);
117 return ERR_PTR(-ENOMEM);
118 }
119
120 dev->n_ahs_allocated++;
121 spin_unlock_irqrestore(&dev->n_ahs_lock, flags);
122
123 ah->attr = *ah_attr;
124 atomic_set(&ah->refcount, 0);
125
Dennis Dalessandrob036db82016-01-06 10:04:23 -0800126 if (dev->driver_f.notify_new_ah)
127 dev->driver_f.notify_new_ah(pd->device, ah_attr, ah);
128
Kamal Heib119a8e72016-01-06 10:03:59 -0800129 return &ah->ibah;
Dennis Dalessandro4c1e4972016-01-06 09:56:41 -0800130}
131
132int rvt_destroy_ah(struct ib_ah *ibah)
133{
Kamal Heib119a8e72016-01-06 10:03:59 -0800134 struct rvt_dev_info *dev = ib_to_rvt(ibah->device);
135 struct rvt_ah *ah = ibah_to_rvtah(ibah);
136 unsigned long flags;
137
138 if (atomic_read(&ah->refcount) != 0)
139 return -EBUSY;
140
141 spin_lock_irqsave(&dev->n_ahs_lock, flags);
142 dev->n_ahs_allocated--;
143 spin_unlock_irqrestore(&dev->n_ahs_lock, flags);
144
145 kfree(ah);
146
147 return 0;
Dennis Dalessandro4c1e4972016-01-06 09:56:41 -0800148}
149
150int rvt_modify_ah(struct ib_ah *ibah, struct ib_ah_attr *ah_attr)
151{
Kamal Heib119a8e72016-01-06 10:03:59 -0800152 struct rvt_ah *ah = ibah_to_rvtah(ibah);
153
154 if (rvt_check_ah(ibah->device, ah_attr))
155 return -EINVAL;
156
157 ah->attr = *ah_attr;
158
159 return 0;
Dennis Dalessandro4c1e4972016-01-06 09:56:41 -0800160}
161
162int rvt_query_ah(struct ib_ah *ibah, struct ib_ah_attr *ah_attr)
163{
Kamal Heib119a8e72016-01-06 10:03:59 -0800164 struct rvt_ah *ah = ibah_to_rvtah(ibah);
165
166 *ah_attr = ah->attr;
167
168 return 0;
Dennis Dalessandro4c1e4972016-01-06 09:56:41 -0800169}