| 1 |
/* |
| 2 |
* Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved. |
| 3 |
* Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved. |
| 4 |
* |
| 5 |
* This software is available to you under a choice of one of two |
| 6 |
* licenses. You may choose to be licensed under the terms of the GNU |
| 7 |
* General Public License (GPL) Version 2, available from the file |
| 8 |
* COPYING in the main directory of this source tree, or the |
| 9 |
* OpenIB.org BSD license below: |
| 10 |
* |
| 11 |
* Redistribution and use in source and binary forms, with or |
| 12 |
* without modification, are permitted provided that the following |
| 13 |
* conditions are met: |
| 14 |
* |
| 15 |
* - Redistributions of source code must retain the above |
| 16 |
* copyright notice, this list of conditions and the following |
| 17 |
* disclaimer. |
| 18 |
* |
| 19 |
* - Redistributions in binary form must reproduce the above |
| 20 |
* copyright notice, this list of conditions and the following |
| 21 |
* disclaimer in the documentation and/or other materials |
| 22 |
* provided with the distribution. |
| 23 |
* |
| 24 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 25 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 26 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 27 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 28 |
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 29 |
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 30 |
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 31 |
* SOFTWARE. |
| 32 |
*/ |
| 33 |
|
| 34 |
|
| 35 |
#include <linux/mlx4/cmd.h> |
| 36 |
#include <linux/mlx4/srq.h> |
| 37 |
#include <linux/export.h> |
| 38 |
#include <linux/gfp.h> |
| 39 |
|
| 40 |
#include "mlx4.h" |
| 41 |
#include "icm.h" |
| 42 |
|
| 43 |
void mlx4_srq_event(struct mlx4_dev *dev, u32 srqn, int event_type) |
| 44 |
{ |
| 45 |
struct mlx4_srq_table *srq_table = &mlx4_priv(dev)->srq_table; |
| 46 |
struct mlx4_srq *srq; |
| 47 |
unsigned long flags; |
| 48 |
|
| 49 |
spin_lock_irqsave(&srq_table->lock, flags); |
| 50 |
srq = radix_tree_lookup(&srq_table->tree, srqn & (dev->caps.num_srqs - 1)); |
| 51 |
if (!srq || !refcount_inc_not_zero(&srq->refcount)) |
| 52 |
srq = NULL; |
| 53 |
spin_unlock_irqrestore(&srq_table->lock, flags); |
| 54 |
if (!srq) { |
| 55 |
mlx4_warn(dev, "Async event for bogus SRQ %08x\n", srqn); |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
srq->event(srq, event_type); |
| 60 |
|
| 61 |
if (refcount_dec_and_test(&srq->refcount)) |
| 62 |
complete(&srq->free); |
| 63 |
} |
| 64 |
|
| 65 |
static int mlx4_SW2HW_SRQ(struct mlx4_dev *dev, struct mlx4_cmd_mailbox *mailbox, |
| 66 |
int srq_num) |
| 67 |
{ |
| 68 |
return mlx4_cmd(dev, mailbox->dma, srq_num, 0, |
| 69 |
MLX4_CMD_SW2HW_SRQ, MLX4_CMD_TIME_CLASS_A, |
| 70 |
MLX4_CMD_WRAPPED); |
| 71 |
} |
| 72 |
|
| 73 |
static int mlx4_HW2SW_SRQ(struct mlx4_dev *dev, struct mlx4_cmd_mailbox *mailbox, |
| 74 |
int srq_num) |
| 75 |
{ |
| 76 |
return mlx4_cmd_box(dev, 0, mailbox ? mailbox->dma : 0, srq_num, |
| 77 |
mailbox ? 0 : 1, MLX4_CMD_HW2SW_SRQ, |
| 78 |
MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED); |
| 79 |
} |
| 80 |
|
| 81 |
static int mlx4_ARM_SRQ(struct mlx4_dev *dev, int srq_num, int limit_watermark) |
| 82 |
{ |
| 83 |
return mlx4_cmd(dev, limit_watermark, srq_num, 0, MLX4_CMD_ARM_SRQ, |
| 84 |
MLX4_CMD_TIME_CLASS_B, MLX4_CMD_WRAPPED); |
| 85 |
} |
| 86 |
|
| 87 |
static int mlx4_QUERY_SRQ(struct mlx4_dev *dev, struct mlx4_cmd_mailbox *mailbox, |
| 88 |
int srq_num) |
| 89 |
{ |
| 90 |
return mlx4_cmd_box(dev, 0, mailbox->dma, srq_num, 0, MLX4_CMD_QUERY_SRQ, |
| 91 |
MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED); |
| 92 |
} |
| 93 |
|
| 94 |
int __mlx4_srq_alloc_icm(struct mlx4_dev *dev, int *srqn) |
| 95 |
{ |
| 96 |
struct mlx4_srq_table *srq_table = &mlx4_priv(dev)->srq_table; |
| 97 |
int err; |
| 98 |
|
| 99 |
|
| 100 |
*srqn = mlx4_bitmap_alloc(&srq_table->bitmap); |
| 101 |
if (*srqn == -1) |
| 102 |
return -ENOMEM; |
| 103 |
|
| 104 |
err = mlx4_table_get(dev, &srq_table->table, *srqn); |
| 105 |
if (err) |
| 106 |
goto err_out; |
| 107 |
|
| 108 |
err = mlx4_table_get(dev, &srq_table->cmpt_table, *srqn); |
| 109 |
if (err) |
| 110 |
goto err_put; |
| 111 |
return 0; |
| 112 |
|
| 113 |
err_put: |
| 114 |
mlx4_table_put(dev, &srq_table->table, *srqn); |
| 115 |
|
| 116 |
err_out: |
| 117 |
mlx4_bitmap_free(&srq_table->bitmap, *srqn, MLX4_NO_RR); |
| 118 |
return err; |
| 119 |
} |
| 120 |
|
| 121 |
static int mlx4_srq_alloc_icm(struct mlx4_dev *dev, int *srqn) |
| 122 |
{ |
| 123 |
u64 out_param; |
| 124 |
int err; |
| 125 |
|
| 126 |
if (mlx4_is_mfunc(dev)) { |
| 127 |
err = mlx4_cmd_imm(dev, 0, &out_param, RES_SRQ, |
| 128 |
RES_OP_RESERVE_AND_MAP, |
| 129 |
MLX4_CMD_ALLOC_RES, |
| 130 |
MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED); |
| 131 |
if (!err) |
| 132 |
*srqn = get_param_l(&out_param); |
| 133 |
|
| 134 |
return err; |
| 135 |
} |
| 136 |
return __mlx4_srq_alloc_icm(dev, srqn); |
| 137 |
} |
| 138 |
|
| 139 |
void __mlx4_srq_free_icm(struct mlx4_dev *dev, int srqn) |
| 140 |
{ |
| 141 |
struct mlx4_srq_table *srq_table = &mlx4_priv(dev)->srq_table; |
| 142 |
|
| 143 |
mlx4_table_put(dev, &srq_table->cmpt_table, srqn); |
| 144 |
mlx4_table_put(dev, &srq_table->table, srqn); |
| 145 |
mlx4_bitmap_free(&srq_table->bitmap, srqn, MLX4_NO_RR); |
| 146 |
} |
| 147 |
|
| 148 |
static void mlx4_srq_free_icm(struct mlx4_dev *dev, int srqn) |
| 149 |
{ |
| 150 |
u64 in_param = 0; |
| 151 |
|
| 152 |
if (mlx4_is_mfunc(dev)) { |
| 153 |
set_param_l(&in_param, srqn); |
| 154 |
if (mlx4_cmd(dev, in_param, RES_SRQ, RES_OP_RESERVE_AND_MAP, |
| 155 |
MLX4_CMD_FREE_RES, |
| 156 |
MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED)) |
| 157 |
mlx4_warn(dev, "Failed freeing cq:%d\n", srqn); |
| 158 |
return; |
| 159 |
} |
| 160 |
__mlx4_srq_free_icm(dev, srqn); |
| 161 |
} |
| 162 |
|
| 163 |
int mlx4_srq_alloc(struct mlx4_dev *dev, u32 pdn, u32 cqn, u16 xrcd, |
| 164 |
struct mlx4_mtt *mtt, u64 db_rec, struct mlx4_srq *srq) |
| 165 |
{ |
| 166 |
struct mlx4_srq_table *srq_table = &mlx4_priv(dev)->srq_table; |
| 167 |
struct mlx4_cmd_mailbox *mailbox; |
| 168 |
struct mlx4_srq_context *srq_context; |
| 169 |
u64 mtt_addr; |
| 170 |
int err; |
| 171 |
|
| 172 |
err = mlx4_srq_alloc_icm(dev, &srq->srqn); |
| 173 |
if (err) |
| 174 |
return err; |
| 175 |
|
| 176 |
spin_lock_irq(&srq_table->lock); |
| 177 |
err = radix_tree_insert(&srq_table->tree, srq->srqn, srq); |
| 178 |
spin_unlock_irq(&srq_table->lock); |
| 179 |
if (err) |
| 180 |
goto err_icm; |
| 181 |
|
| 182 |
mailbox = mlx4_alloc_cmd_mailbox(dev); |
| 183 |
if (IS_ERR(mailbox)) { |
| 184 |
err = PTR_ERR(mailbox); |
| 185 |
goto err_radix; |
| 186 |
} |
| 187 |
|
| 188 |
srq_context = mailbox->buf; |
| 189 |
srq_context->state_logsize_srqn = cpu_to_be32((ilog2(srq->max) << 24) | |
| 190 |
srq->srqn); |
| 191 |
srq_context->logstride = srq->wqe_shift - 4; |
| 192 |
srq_context->xrcd = cpu_to_be16(xrcd); |
| 193 |
srq_context->pg_offset_cqn = cpu_to_be32(cqn & 0xffffff); |
| 194 |
srq_context->log_page_size = mtt->page_shift - MLX4_ICM_PAGE_SHIFT; |
| 195 |
|
| 196 |
mtt_addr = mlx4_mtt_addr(dev, mtt); |
| 197 |
srq_context->mtt_base_addr_h = mtt_addr >> 32; |
| 198 |
srq_context->mtt_base_addr_l = cpu_to_be32(mtt_addr & 0xffffffff); |
| 199 |
srq_context->pd = cpu_to_be32(pdn); |
| 200 |
srq_context->db_rec_addr = cpu_to_be64(db_rec); |
| 201 |
|
| 202 |
err = mlx4_SW2HW_SRQ(dev, mailbox, srq->srqn); |
| 203 |
mlx4_free_cmd_mailbox(dev, mailbox); |
| 204 |
if (err) |
| 205 |
goto err_radix; |
| 206 |
|
| 207 |
init_completion(&srq->free); |
| 208 |
refcount_set_release(&srq->refcount, 1); |
| 209 |
|
| 210 |
return 0; |
| 211 |
|
| 212 |
err_radix: |
| 213 |
spin_lock_irq(&srq_table->lock); |
| 214 |
radix_tree_delete(&srq_table->tree, srq->srqn); |
| 215 |
spin_unlock_irq(&srq_table->lock); |
| 216 |
|
| 217 |
err_icm: |
| 218 |
mlx4_srq_free_icm(dev, srq->srqn); |
| 219 |
return err; |
| 220 |
} |
| 221 |
EXPORT_SYMBOL_GPL(mlx4_srq_alloc); |
| 222 |
|
| 223 |
void mlx4_srq_free(struct mlx4_dev *dev, struct mlx4_srq *srq) |
| 224 |
{ |
| 225 |
struct mlx4_srq_table *srq_table = &mlx4_priv(dev)->srq_table; |
| 226 |
int err; |
| 227 |
|
| 228 |
err = mlx4_HW2SW_SRQ(dev, NULL, srq->srqn); |
| 229 |
if (err) |
| 230 |
mlx4_warn(dev, "HW2SW_SRQ failed (%d) for SRQN %06x\n", err, srq->srqn); |
| 231 |
|
| 232 |
spin_lock_irq(&srq_table->lock); |
| 233 |
radix_tree_delete(&srq_table->tree, srq->srqn); |
| 234 |
spin_unlock_irq(&srq_table->lock); |
| 235 |
|
| 236 |
if (refcount_dec_and_test(&srq->refcount)) |
| 237 |
complete(&srq->free); |
| 238 |
wait_for_completion(&srq->free); |
| 239 |
|
| 240 |
mlx4_srq_free_icm(dev, srq->srqn); |
| 241 |
} |
| 242 |
EXPORT_SYMBOL_GPL(mlx4_srq_free); |
| 243 |
|
| 244 |
int mlx4_srq_arm(struct mlx4_dev *dev, struct mlx4_srq *srq, int limit_watermark) |
| 245 |
{ |
| 246 |
return mlx4_ARM_SRQ(dev, srq->srqn, limit_watermark); |
| 247 |
} |
| 248 |
EXPORT_SYMBOL_GPL(mlx4_srq_arm); |
| 249 |
|
| 250 |
int mlx4_srq_query(struct mlx4_dev *dev, struct mlx4_srq *srq, int *limit_watermark) |
| 251 |
{ |
| 252 |
struct mlx4_cmd_mailbox *mailbox; |
| 253 |
struct mlx4_srq_context *srq_context; |
| 254 |
int err; |
| 255 |
|
| 256 |
mailbox = mlx4_alloc_cmd_mailbox(dev); |
| 257 |
if (IS_ERR(mailbox)) |
| 258 |
return PTR_ERR(mailbox); |
| 259 |
|
| 260 |
srq_context = mailbox->buf; |
| 261 |
|
| 262 |
err = mlx4_QUERY_SRQ(dev, mailbox, srq->srqn); |
| 263 |
if (err) |
| 264 |
goto err_out; |
| 265 |
*limit_watermark = be16_to_cpu(srq_context->limit_watermark); |
| 266 |
|
| 267 |
err_out: |
| 268 |
mlx4_free_cmd_mailbox(dev, mailbox); |
| 269 |
return err; |
| 270 |
} |
| 271 |
EXPORT_SYMBOL_GPL(mlx4_srq_query); |
| 272 |
|
| 273 |
int mlx4_init_srq_table(struct mlx4_dev *dev) |
| 274 |
{ |
| 275 |
struct mlx4_srq_table *srq_table = &mlx4_priv(dev)->srq_table; |
| 276 |
|
| 277 |
spin_lock_init(&srq_table->lock); |
| 278 |
INIT_RADIX_TREE(&srq_table->tree, GFP_ATOMIC); |
| 279 |
if (mlx4_is_slave(dev)) |
| 280 |
return 0; |
| 281 |
|
| 282 |
return mlx4_bitmap_init(&srq_table->bitmap, dev->caps.num_srqs, |
| 283 |
dev->caps.num_srqs - 1, dev->caps.reserved_srqs, 0); |
| 284 |
} |
| 285 |
|
| 286 |
void mlx4_cleanup_srq_table(struct mlx4_dev *dev) |
| 287 |
{ |
| 288 |
if (mlx4_is_slave(dev)) |
| 289 |
return; |
| 290 |
mlx4_bitmap_cleanup(&mlx4_priv(dev)->srq_table.bitmap); |
| 291 |
} |
| 292 |
|
| 293 |
struct mlx4_srq *mlx4_srq_lookup(struct mlx4_dev *dev, u32 srqn) |
| 294 |
{ |
| 295 |
struct mlx4_srq_table *srq_table = &mlx4_priv(dev)->srq_table; |
| 296 |
struct mlx4_srq *srq; |
| 297 |
|
| 298 |
rcu_read_lock(); |
| 299 |
srq = radix_tree_lookup(&srq_table->tree, |
| 300 |
srqn & (dev->caps.num_srqs - 1)); |
| 301 |
rcu_read_unlock(); |
| 302 |
|
| 303 |
return srq; |
| 304 |
} |
| 305 |
EXPORT_SYMBOL_GPL(mlx4_srq_lookup); |