2 * Copyright(c) 2016 Intel Corporation.
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.
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.
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.
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
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
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.
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.
48 #include <linux/slab.h>
50 #include "vt.h" /* for prints */
53 * rvt_check_ah - validate the attributes of AH
54 * @ibdev: the ib device
55 * @ah_attr: the attributes of the AH
57 * If driver supports a more detailed check_ah function call back to it
58 * otherwise just check the basics.
60 * Return: 0 on success
62 int rvt_check_ah(struct ib_device
*ibdev
,
63 struct rdma_ah_attr
*ah_attr
)
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
);
72 err
= ib_query_port(ibdev
, port_num
, &port_attr
);
76 port_num
> ibdev
->phys_port_cnt
)
78 if (static_rate
!= IB_RATE_PORT_CURRENT
&&
79 ib_rate_to_mbps(static_rate
) < 0)
81 if ((ah_flags
& IB_AH_GRH
) &&
82 rdma_ah_read_grh(ah_attr
)->sgid_index
>= port_attr
.gid_tbl_len
)
84 if (rdi
->driver_f
.check_ah
)
85 return rdi
->driver_f
.check_ah(ibdev
, ah_attr
);
88 EXPORT_SYMBOL(rvt_check_ah
);
91 * rvt_create_ah - create an address handle
92 * @pd: the protection domain
93 * @ah_attr: the attributes of the AH
95 * This may be called from interrupt context.
97 * Return: newly allocated ah
99 struct ib_ah
*rvt_create_ah(struct ib_pd
*pd
,
100 struct rdma_ah_attr
*ah_attr
)
103 struct rvt_dev_info
*dev
= ib_to_rvt(pd
->device
);
106 if (rvt_check_ah(pd
->device
, ah_attr
))
107 return ERR_PTR(-EINVAL
);
109 ah
= kmalloc(sizeof(*ah
), GFP_ATOMIC
);
111 return ERR_PTR(-ENOMEM
);
113 spin_lock_irqsave(&dev
->n_ahs_lock
, flags
);
114 if (dev
->n_ahs_allocated
== dev
->dparms
.props
.max_ah
) {
115 spin_unlock_irqrestore(&dev
->n_ahs_lock
, flags
);
117 return ERR_PTR(-ENOMEM
);
120 dev
->n_ahs_allocated
++;
121 spin_unlock_irqrestore(&dev
->n_ahs_lock
, flags
);
124 atomic_set(&ah
->refcount
, 0);
126 if (dev
->driver_f
.notify_new_ah
)
127 dev
->driver_f
.notify_new_ah(pd
->device
, ah_attr
, ah
);
133 * rvt_destory_ah - Destory an address handle
134 * @ibah: address handle
136 * Return: 0 on success
138 int rvt_destroy_ah(struct ib_ah
*ibah
)
140 struct rvt_dev_info
*dev
= ib_to_rvt(ibah
->device
);
141 struct rvt_ah
*ah
= ibah_to_rvtah(ibah
);
144 if (atomic_read(&ah
->refcount
) != 0)
147 spin_lock_irqsave(&dev
->n_ahs_lock
, flags
);
148 dev
->n_ahs_allocated
--;
149 spin_unlock_irqrestore(&dev
->n_ahs_lock
, flags
);
157 * rvt_modify_ah - modify an ah with given attrs
158 * @ibah: address handle to modify
159 * @ah_attr: attrs to apply
161 * Return: 0 on success
163 int rvt_modify_ah(struct ib_ah
*ibah
, struct rdma_ah_attr
*ah_attr
)
165 struct rvt_ah
*ah
= ibah_to_rvtah(ibah
);
167 if (rvt_check_ah(ibah
->device
, ah_attr
))
176 * rvt_query_ah - return attrs for ah
177 * @ibah: address handle to query
178 * @ah_attr: return info in this
182 int rvt_query_ah(struct ib_ah
*ibah
, struct rdma_ah_attr
*ah_attr
)
184 struct rvt_ah
*ah
= ibah_to_rvtah(ibah
);