| 1 |
/* |
| 2 |
* Copyright (c) 2016 Hisilicon Limited. |
| 3 |
* |
| 4 |
* This software is available to you under a choice of one of two |
| 5 |
* licenses. You may choose to be licensed under the terms of the GNU |
| 6 |
* General Public License (GPL) Version 2, available from the file |
| 7 |
* COPYING in the main directory of this source tree, or the |
| 8 |
* OpenIB.org BSD license below: |
| 9 |
* |
| 10 |
* Redistribution and use in source and binary forms, with or |
| 11 |
* without modification, are permitted provided that the following |
| 12 |
* conditions are met: |
| 13 |
* |
| 14 |
* - Redistributions of source code must retain the above |
| 15 |
* copyright notice, this list of conditions and the following |
| 16 |
* disclaimer. |
| 17 |
* |
| 18 |
* - Redistributions in binary form must reproduce the above |
| 19 |
* copyright notice, this list of conditions and the following |
| 20 |
* disclaimer in the documentation and/or other materials |
| 21 |
* provided with the distribution. |
| 22 |
* |
| 23 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 24 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 25 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 26 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 27 |
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 28 |
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 29 |
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 30 |
* SOFTWARE. |
| 31 |
*/ |
| 32 |
|
| 33 |
#include <linux/pci.h> |
| 34 |
#include <rdma/ib_umem.h> |
| 35 |
#include <rdma/uverbs_ioctl.h> |
| 36 |
#include "hns_roce_device.h" |
| 37 |
#include "hns_roce_cmd.h" |
| 38 |
#include "hns_roce_hem.h" |
| 39 |
#include "hns_roce_common.h" |
| 40 |
|
| 41 |
void hns_roce_put_cq_bankid_for_uctx(struct hns_roce_ucontext *uctx) |
| 42 |
{ |
| 43 |
struct hns_roce_dev *hr_dev = to_hr_dev(uctx->ibucontext.device); |
| 44 |
struct hns_roce_cq_table *cq_table = &hr_dev->cq_table; |
| 45 |
|
| 46 |
if (hr_dev->pci_dev->revision < PCI_REVISION_ID_HIP09) |
| 47 |
return; |
| 48 |
|
| 49 |
mutex_lock(&cq_table->bank_mutex); |
| 50 |
cq_table->ctx_num[uctx->cq_bank_id]--; |
| 51 |
mutex_unlock(&cq_table->bank_mutex); |
| 52 |
} |
| 53 |
|
| 54 |
void hns_roce_get_cq_bankid_for_uctx(struct hns_roce_ucontext *uctx) |
| 55 |
{ |
| 56 |
struct hns_roce_dev *hr_dev = to_hr_dev(uctx->ibucontext.device); |
| 57 |
struct hns_roce_cq_table *cq_table = &hr_dev->cq_table; |
| 58 |
u32 least_load = U32_MAX; |
| 59 |
u8 bankid = 0; |
| 60 |
u8 i; |
| 61 |
|
| 62 |
if (hr_dev->pci_dev->revision < PCI_REVISION_ID_HIP09) |
| 63 |
return; |
| 64 |
|
| 65 |
mutex_lock(&cq_table->bank_mutex); |
| 66 |
for (i = 0; i < HNS_ROCE_CQ_BANK_NUM; i++) { |
| 67 |
if (!(cq_table->valid_cq_bank_mask & BIT(i))) |
| 68 |
continue; |
| 69 |
|
| 70 |
if (cq_table->ctx_num[i] < least_load) { |
| 71 |
least_load = cq_table->ctx_num[i]; |
| 72 |
bankid = i; |
| 73 |
} |
| 74 |
} |
| 75 |
cq_table->ctx_num[bankid]++; |
| 76 |
mutex_unlock(&cq_table->bank_mutex); |
| 77 |
|
| 78 |
uctx->cq_bank_id = bankid; |
| 79 |
} |
| 80 |
|
| 81 |
static u8 get_least_load_bankid_for_cq(struct hns_roce_bank *bank) |
| 82 |
{ |
| 83 |
u32 least_load = bank[0].inuse; |
| 84 |
u8 bankid = 0; |
| 85 |
u32 bankcnt; |
| 86 |
u8 i; |
| 87 |
|
| 88 |
for (i = 1; i < HNS_ROCE_CQ_BANK_NUM; i++) { |
| 89 |
bankcnt = bank[i].inuse; |
| 90 |
if (bankcnt < least_load) { |
| 91 |
least_load = bankcnt; |
| 92 |
bankid = i; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
return bankid; |
| 97 |
} |
| 98 |
|
| 99 |
static u8 select_cq_bankid(struct hns_roce_dev *hr_dev, |
| 100 |
struct hns_roce_bank *bank, struct ib_udata *udata) |
| 101 |
{ |
| 102 |
struct hns_roce_ucontext *uctx = udata ? |
| 103 |
rdma_udata_to_drv_context(udata, struct hns_roce_ucontext, |
| 104 |
ibucontext) : NULL; |
| 105 |
|
| 106 |
if (hr_dev->pci_dev->revision >= PCI_REVISION_ID_HIP09) |
| 107 |
return uctx ? uctx->cq_bank_id : 0; |
| 108 |
|
| 109 |
return get_least_load_bankid_for_cq(bank); |
| 110 |
} |
| 111 |
|
| 112 |
static int alloc_cqn(struct hns_roce_dev *hr_dev, struct hns_roce_cq *hr_cq, |
| 113 |
struct ib_udata *udata) |
| 114 |
{ |
| 115 |
struct hns_roce_cq_table *cq_table = &hr_dev->cq_table; |
| 116 |
struct hns_roce_bank *bank; |
| 117 |
u8 bankid; |
| 118 |
int id; |
| 119 |
|
| 120 |
mutex_lock(&cq_table->bank_mutex); |
| 121 |
bankid = select_cq_bankid(hr_dev, cq_table->bank, udata); |
| 122 |
bank = &cq_table->bank[bankid]; |
| 123 |
|
| 124 |
id = ida_alloc_range(&bank->ida, bank->min, bank->max, GFP_KERNEL); |
| 125 |
if (id < 0) { |
| 126 |
mutex_unlock(&cq_table->bank_mutex); |
| 127 |
return id; |
| 128 |
} |
| 129 |
|
| 130 |
/* the lower 2 bits is bankid */ |
| 131 |
hr_cq->cqn = (id << CQ_BANKID_SHIFT) | bankid; |
| 132 |
bank->inuse++; |
| 133 |
mutex_unlock(&cq_table->bank_mutex); |
| 134 |
|
| 135 |
return 0; |
| 136 |
} |
| 137 |
|
| 138 |
static inline u8 get_cq_bankid(unsigned long cqn) |
| 139 |
{ |
| 140 |
/* The lower 2 bits of CQN are used to hash to different banks */ |
| 141 |
return (u8)(cqn & GENMASK(1, 0)); |
| 142 |
} |
| 143 |
|
| 144 |
static void free_cqn(struct hns_roce_dev *hr_dev, unsigned long cqn) |
| 145 |
{ |
| 146 |
struct hns_roce_cq_table *cq_table = &hr_dev->cq_table; |
| 147 |
struct hns_roce_bank *bank; |
| 148 |
|
| 149 |
bank = &cq_table->bank[get_cq_bankid(cqn)]; |
| 150 |
|
| 151 |
ida_free(&bank->ida, cqn >> CQ_BANKID_SHIFT); |
| 152 |
|
| 153 |
mutex_lock(&cq_table->bank_mutex); |
| 154 |
bank->inuse--; |
| 155 |
mutex_unlock(&cq_table->bank_mutex); |
| 156 |
} |
| 157 |
|
| 158 |
static int hns_roce_create_cqc(struct hns_roce_dev *hr_dev, |
| 159 |
struct hns_roce_cq *hr_cq, |
| 160 |
u64 *mtts, dma_addr_t dma_handle) |
| 161 |
{ |
| 162 |
struct ib_device *ibdev = &hr_dev->ib_dev; |
| 163 |
struct hns_roce_cmd_mailbox *mailbox; |
| 164 |
int ret; |
| 165 |
|
| 166 |
mailbox = hns_roce_alloc_cmd_mailbox(hr_dev); |
| 167 |
if (IS_ERR(mailbox)) { |
| 168 |
ibdev_err(ibdev, "failed to alloc mailbox for CQC.\n"); |
| 169 |
return PTR_ERR(mailbox); |
| 170 |
} |
| 171 |
|
| 172 |
hr_dev->hw->write_cqc(hr_dev, hr_cq, mailbox->buf, mtts, dma_handle); |
| 173 |
|
| 174 |
ret = hns_roce_create_hw_ctx(hr_dev, mailbox, HNS_ROCE_CMD_CREATE_CQC, |
| 175 |
hr_cq->cqn); |
| 176 |
if (ret) |
| 177 |
ibdev_err(ibdev, |
| 178 |
"failed to send create cmd for CQ(0x%lx), ret = %d.\n", |
| 179 |
hr_cq->cqn, ret); |
| 180 |
|
| 181 |
hns_roce_free_cmd_mailbox(hr_dev, mailbox); |
| 182 |
|
| 183 |
return ret; |
| 184 |
} |
| 185 |
|
| 186 |
static int alloc_cqc(struct hns_roce_dev *hr_dev, struct hns_roce_cq *hr_cq) |
| 187 |
{ |
| 188 |
struct hns_roce_cq_table *cq_table = &hr_dev->cq_table; |
| 189 |
struct ib_device *ibdev = &hr_dev->ib_dev; |
| 190 |
u64 mtts[MTT_MIN_COUNT] = {}; |
| 191 |
int ret; |
| 192 |
|
| 193 |
ret = hns_roce_mtr_find(hr_dev, &hr_cq->mtr, 0, mtts, ARRAY_SIZE(mtts)); |
| 194 |
if (ret) { |
| 195 |
ibdev_err(ibdev, "failed to find CQ mtr, ret = %d.\n", ret); |
| 196 |
return ret; |
| 197 |
} |
| 198 |
|
| 199 |
/* Get CQC memory HEM(Hardware Entry Memory) table */ |
| 200 |
ret = hns_roce_table_get(hr_dev, &cq_table->table, hr_cq->cqn); |
| 201 |
if (ret) { |
| 202 |
ibdev_err(ibdev, "failed to get CQ(0x%lx) context, ret = %d.\n", |
| 203 |
hr_cq->cqn, ret); |
| 204 |
return ret; |
| 205 |
} |
| 206 |
|
| 207 |
ret = xa_err(xa_store_irq(&cq_table->array, hr_cq->cqn, hr_cq, GFP_KERNEL)); |
| 208 |
if (ret) { |
| 209 |
ibdev_err(ibdev, "failed to xa_store CQ, ret = %d.\n", ret); |
| 210 |
goto err_put; |
| 211 |
} |
| 212 |
|
| 213 |
ret = hns_roce_create_cqc(hr_dev, hr_cq, mtts, |
| 214 |
hns_roce_get_mtr_ba(&hr_cq->mtr)); |
| 215 |
if (ret) |
| 216 |
goto err_xa; |
| 217 |
|
| 218 |
return 0; |
| 219 |
|
| 220 |
err_xa: |
| 221 |
xa_erase_irq(&cq_table->array, hr_cq->cqn); |
| 222 |
err_put: |
| 223 |
hns_roce_table_put(hr_dev, &cq_table->table, hr_cq->cqn); |
| 224 |
|
| 225 |
return ret; |
| 226 |
} |
| 227 |
|
| 228 |
static void free_cqc(struct hns_roce_dev *hr_dev, struct hns_roce_cq *hr_cq) |
| 229 |
{ |
| 230 |
struct hns_roce_cq_table *cq_table = &hr_dev->cq_table; |
| 231 |
struct device *dev = hr_dev->dev; |
| 232 |
int ret; |
| 233 |
|
| 234 |
ret = hns_roce_destroy_hw_ctx(hr_dev, HNS_ROCE_CMD_DESTROY_CQC, |
| 235 |
hr_cq->cqn); |
| 236 |
if (ret) |
| 237 |
dev_err_ratelimited(dev, "DESTROY_CQ failed (%d) for CQN %06lx\n", |
| 238 |
ret, hr_cq->cqn); |
| 239 |
|
| 240 |
xa_erase_irq(&cq_table->array, hr_cq->cqn); |
| 241 |
|
| 242 |
/* Waiting interrupt process procedure carried out */ |
| 243 |
synchronize_irq(hr_dev->eq_table.eq[hr_cq->vector].irq); |
| 244 |
|
| 245 |
/* wait for all interrupt processed */ |
| 246 |
if (refcount_dec_and_test(&hr_cq->refcount)) |
| 247 |
complete(&hr_cq->free); |
| 248 |
wait_for_completion(&hr_cq->free); |
| 249 |
|
| 250 |
hns_roce_table_put(hr_dev, &cq_table->table, hr_cq->cqn); |
| 251 |
} |
| 252 |
|
| 253 |
static int alloc_cq_buf(struct hns_roce_dev *hr_dev, struct hns_roce_cq *hr_cq, |
| 254 |
struct ib_udata *udata, unsigned long addr) |
| 255 |
{ |
| 256 |
struct ib_device *ibdev = &hr_dev->ib_dev; |
| 257 |
struct hns_roce_buf_attr buf_attr = {}; |
| 258 |
int ret; |
| 259 |
|
| 260 |
buf_attr.page_shift = hr_dev->caps.cqe_buf_pg_sz + PAGE_SHIFT; |
| 261 |
buf_attr.region[0].size = hr_cq->cq_depth * hr_cq->cqe_size; |
| 262 |
buf_attr.region[0].hopnum = hr_dev->caps.cqe_hop_num; |
| 263 |
buf_attr.region_count = 1; |
| 264 |
|
| 265 |
ret = hns_roce_mtr_create(hr_dev, &hr_cq->mtr, &buf_attr, |
| 266 |
hr_dev->caps.cqe_ba_pg_sz + PAGE_SHIFT, |
| 267 |
udata, addr); |
| 268 |
if (ret) |
| 269 |
ibdev_err(ibdev, "failed to alloc CQ mtr, ret = %d.\n", ret); |
| 270 |
|
| 271 |
return ret; |
| 272 |
} |
| 273 |
|
| 274 |
static void free_cq_buf(struct hns_roce_dev *hr_dev, struct hns_roce_cq *hr_cq) |
| 275 |
{ |
| 276 |
hns_roce_mtr_destroy(hr_dev, &hr_cq->mtr); |
| 277 |
} |
| 278 |
|
| 279 |
static int alloc_cq_db(struct hns_roce_dev *hr_dev, struct hns_roce_cq *hr_cq, |
| 280 |
struct ib_udata *udata, unsigned long addr, |
| 281 |
struct hns_roce_ib_create_cq_resp *resp) |
| 282 |
{ |
| 283 |
bool has_db = hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_CQ_RECORD_DB; |
| 284 |
struct hns_roce_ucontext *uctx; |
| 285 |
int err; |
| 286 |
|
| 287 |
if (udata) { |
| 288 |
if (has_db && |
| 289 |
udata->outlen >= offsetofend(typeof(*resp), cap_flags)) { |
| 290 |
uctx = rdma_udata_to_drv_context(udata, |
| 291 |
struct hns_roce_ucontext, ibucontext); |
| 292 |
err = hns_roce_db_map_user(uctx, addr, &hr_cq->db); |
| 293 |
if (err) |
| 294 |
return err; |
| 295 |
hr_cq->flags |= HNS_ROCE_CQ_FLAG_RECORD_DB; |
| 296 |
resp->cap_flags |= HNS_ROCE_CQ_FLAG_RECORD_DB; |
| 297 |
} |
| 298 |
} else { |
| 299 |
if (has_db) { |
| 300 |
err = hns_roce_alloc_db(hr_dev, &hr_cq->db, 1); |
| 301 |
if (err) |
| 302 |
return err; |
| 303 |
hr_cq->set_ci_db = hr_cq->db.db_record; |
| 304 |
*hr_cq->set_ci_db = 0; |
| 305 |
hr_cq->flags |= HNS_ROCE_CQ_FLAG_RECORD_DB; |
| 306 |
} |
| 307 |
hr_cq->db_reg = hr_dev->reg_base + hr_dev->odb_offset + |
| 308 |
DB_REG_OFFSET * hr_dev->priv_uar.index; |
| 309 |
} |
| 310 |
|
| 311 |
return 0; |
| 312 |
} |
| 313 |
|
| 314 |
static void free_cq_db(struct hns_roce_dev *hr_dev, struct hns_roce_cq *hr_cq, |
| 315 |
struct ib_udata *udata) |
| 316 |
{ |
| 317 |
struct hns_roce_ucontext *uctx; |
| 318 |
|
| 319 |
if (!(hr_cq->flags & HNS_ROCE_CQ_FLAG_RECORD_DB)) |
| 320 |
return; |
| 321 |
|
| 322 |
hr_cq->flags &= ~HNS_ROCE_CQ_FLAG_RECORD_DB; |
| 323 |
if (udata) { |
| 324 |
uctx = rdma_udata_to_drv_context(udata, |
| 325 |
struct hns_roce_ucontext, |
| 326 |
ibucontext); |
| 327 |
hns_roce_db_unmap_user(uctx, &hr_cq->db); |
| 328 |
} else { |
| 329 |
hns_roce_free_db(hr_dev, &hr_cq->db); |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
static int verify_cq_create_attr(struct hns_roce_dev *hr_dev, |
| 334 |
const struct ib_cq_init_attr *attr) |
| 335 |
{ |
| 336 |
struct ib_device *ibdev = &hr_dev->ib_dev; |
| 337 |
|
| 338 |
if (!attr->cqe || attr->cqe > hr_dev->caps.max_cqes) { |
| 339 |
ibdev_err(ibdev, "failed to check CQ count %u, max = %u.\n", |
| 340 |
attr->cqe, hr_dev->caps.max_cqes); |
| 341 |
return -EINVAL; |
| 342 |
} |
| 343 |
|
| 344 |
if (attr->comp_vector >= hr_dev->caps.num_comp_vectors) { |
| 345 |
ibdev_err(ibdev, "failed to check CQ vector = %u, max = %d.\n", |
| 346 |
attr->comp_vector, hr_dev->caps.num_comp_vectors); |
| 347 |
return -EINVAL; |
| 348 |
} |
| 349 |
|
| 350 |
return 0; |
| 351 |
} |
| 352 |
|
| 353 |
static int get_cq_ucmd(struct hns_roce_cq *hr_cq, struct ib_udata *udata, |
| 354 |
struct hns_roce_ib_create_cq *ucmd) |
| 355 |
{ |
| 356 |
struct ib_device *ibdev = hr_cq->ib_cq.device; |
| 357 |
int ret; |
| 358 |
|
| 359 |
ret = ib_copy_from_udata(ucmd, udata, min(udata->inlen, sizeof(*ucmd))); |
| 360 |
if (ret) { |
| 361 |
ibdev_err(ibdev, "failed to copy CQ udata, ret = %d.\n", ret); |
| 362 |
return ret; |
| 363 |
} |
| 364 |
|
| 365 |
return 0; |
| 366 |
} |
| 367 |
|
| 368 |
static void set_cq_param(struct hns_roce_cq *hr_cq, u32 cq_entries, int vector, |
| 369 |
struct hns_roce_ib_create_cq *ucmd) |
| 370 |
{ |
| 371 |
struct hns_roce_dev *hr_dev = to_hr_dev(hr_cq->ib_cq.device); |
| 372 |
|
| 373 |
cq_entries = max(cq_entries, hr_dev->caps.min_cqes); |
| 374 |
cq_entries = roundup_pow_of_two(cq_entries); |
| 375 |
hr_cq->ib_cq.cqe = cq_entries - 1; /* used as cqe index */ |
| 376 |
hr_cq->cq_depth = cq_entries; |
| 377 |
hr_cq->vector = vector; |
| 378 |
|
| 379 |
spin_lock_init(&hr_cq->lock); |
| 380 |
INIT_LIST_HEAD(&hr_cq->sq_list); |
| 381 |
INIT_LIST_HEAD(&hr_cq->rq_list); |
| 382 |
} |
| 383 |
|
| 384 |
static int set_cqe_size(struct hns_roce_cq *hr_cq, struct ib_udata *udata, |
| 385 |
struct hns_roce_ib_create_cq *ucmd) |
| 386 |
{ |
| 387 |
struct hns_roce_dev *hr_dev = to_hr_dev(hr_cq->ib_cq.device); |
| 388 |
|
| 389 |
if (!udata) { |
| 390 |
hr_cq->cqe_size = hr_dev->caps.cqe_sz; |
| 391 |
return 0; |
| 392 |
} |
| 393 |
|
| 394 |
if (udata->inlen >= offsetofend(typeof(*ucmd), cqe_size)) { |
| 395 |
if (ucmd->cqe_size != HNS_ROCE_V2_CQE_SIZE && |
| 396 |
ucmd->cqe_size != HNS_ROCE_V3_CQE_SIZE) { |
| 397 |
ibdev_err(&hr_dev->ib_dev, |
| 398 |
"invalid cqe size %u.\n", ucmd->cqe_size); |
| 399 |
return -EINVAL; |
| 400 |
} |
| 401 |
|
| 402 |
hr_cq->cqe_size = ucmd->cqe_size; |
| 403 |
} else { |
| 404 |
hr_cq->cqe_size = HNS_ROCE_V2_CQE_SIZE; |
| 405 |
} |
| 406 |
|
| 407 |
return 0; |
| 408 |
} |
| 409 |
|
| 410 |
int hns_roce_create_cq(struct ib_cq *ib_cq, const struct ib_cq_init_attr *attr, |
| 411 |
struct uverbs_attr_bundle *attrs) |
| 412 |
{ |
| 413 |
struct hns_roce_dev *hr_dev = to_hr_dev(ib_cq->device); |
| 414 |
struct ib_udata *udata = &attrs->driver_udata; |
| 415 |
struct hns_roce_ib_create_cq_resp resp = {}; |
| 416 |
struct hns_roce_cq *hr_cq = to_hr_cq(ib_cq); |
| 417 |
struct ib_device *ibdev = &hr_dev->ib_dev; |
| 418 |
struct hns_roce_ib_create_cq ucmd = {}; |
| 419 |
int ret; |
| 420 |
|
| 421 |
if (attr->flags) { |
| 422 |
ret = -EOPNOTSUPP; |
| 423 |
goto err_out; |
| 424 |
} |
| 425 |
|
| 426 |
ret = verify_cq_create_attr(hr_dev, attr); |
| 427 |
if (ret) |
| 428 |
goto err_out; |
| 429 |
|
| 430 |
if (udata) { |
| 431 |
ret = get_cq_ucmd(hr_cq, udata, &ucmd); |
| 432 |
if (ret) |
| 433 |
goto err_out; |
| 434 |
} |
| 435 |
|
| 436 |
set_cq_param(hr_cq, attr->cqe, attr->comp_vector, &ucmd); |
| 437 |
|
| 438 |
ret = set_cqe_size(hr_cq, udata, &ucmd); |
| 439 |
if (ret) |
| 440 |
goto err_out; |
| 441 |
|
| 442 |
ret = alloc_cq_buf(hr_dev, hr_cq, udata, ucmd.buf_addr); |
| 443 |
if (ret) { |
| 444 |
ibdev_err(ibdev, "failed to alloc CQ buf, ret = %d.\n", ret); |
| 445 |
goto err_out; |
| 446 |
} |
| 447 |
|
| 448 |
ret = alloc_cq_db(hr_dev, hr_cq, udata, ucmd.db_addr, &resp); |
| 449 |
if (ret) { |
| 450 |
ibdev_err(ibdev, "failed to alloc CQ db, ret = %d.\n", ret); |
| 451 |
goto err_cq_buf; |
| 452 |
} |
| 453 |
|
| 454 |
ret = alloc_cqn(hr_dev, hr_cq, udata); |
| 455 |
if (ret) { |
| 456 |
ibdev_err(ibdev, "failed to alloc CQN, ret = %d.\n", ret); |
| 457 |
goto err_cq_db; |
| 458 |
} |
| 459 |
|
| 460 |
ret = alloc_cqc(hr_dev, hr_cq); |
| 461 |
if (ret) { |
| 462 |
ibdev_err(ibdev, |
| 463 |
"failed to alloc CQ context, ret = %d.\n", ret); |
| 464 |
goto err_cqn; |
| 465 |
} |
| 466 |
|
| 467 |
if (udata) { |
| 468 |
resp.cqn = hr_cq->cqn; |
| 469 |
ret = ib_copy_to_udata(udata, &resp, |
| 470 |
min(udata->outlen, sizeof(resp))); |
| 471 |
if (ret) |
| 472 |
goto err_cqc; |
| 473 |
} |
| 474 |
|
| 475 |
hr_cq->cons_index = 0; |
| 476 |
hr_cq->arm_sn = 1; |
| 477 |
refcount_set(&hr_cq->refcount, 1); |
| 478 |
init_completion(&hr_cq->free); |
| 479 |
|
| 480 |
return 0; |
| 481 |
|
| 482 |
err_cqc: |
| 483 |
free_cqc(hr_dev, hr_cq); |
| 484 |
err_cqn: |
| 485 |
free_cqn(hr_dev, hr_cq->cqn); |
| 486 |
err_cq_db: |
| 487 |
free_cq_db(hr_dev, hr_cq, udata); |
| 488 |
err_cq_buf: |
| 489 |
free_cq_buf(hr_dev, hr_cq); |
| 490 |
err_out: |
| 491 |
atomic64_inc(&hr_dev->dfx_cnt[HNS_ROCE_DFX_CQ_CREATE_ERR_CNT]); |
| 492 |
|
| 493 |
return ret; |
| 494 |
} |
| 495 |
|
| 496 |
int hns_roce_destroy_cq(struct ib_cq *ib_cq, struct ib_udata *udata) |
| 497 |
{ |
| 498 |
struct hns_roce_dev *hr_dev = to_hr_dev(ib_cq->device); |
| 499 |
struct hns_roce_cq *hr_cq = to_hr_cq(ib_cq); |
| 500 |
|
| 501 |
free_cqc(hr_dev, hr_cq); |
| 502 |
free_cqn(hr_dev, hr_cq->cqn); |
| 503 |
free_cq_db(hr_dev, hr_cq, udata); |
| 504 |
free_cq_buf(hr_dev, hr_cq); |
| 505 |
|
| 506 |
return 0; |
| 507 |
} |
| 508 |
|
| 509 |
void hns_roce_cq_completion(struct hns_roce_dev *hr_dev, u32 cqn) |
| 510 |
{ |
| 511 |
struct hns_roce_cq *hr_cq; |
| 512 |
struct ib_cq *ibcq; |
| 513 |
|
| 514 |
hr_cq = xa_load(&hr_dev->cq_table.array, |
| 515 |
cqn & (hr_dev->caps.num_cqs - 1)); |
| 516 |
if (!hr_cq) { |
| 517 |
dev_warn(hr_dev->dev, "completion event for bogus CQ 0x%06x\n", |
| 518 |
cqn); |
| 519 |
return; |
| 520 |
} |
| 521 |
|
| 522 |
++hr_cq->arm_sn; |
| 523 |
ibcq = &hr_cq->ib_cq; |
| 524 |
if (ibcq->comp_handler) |
| 525 |
ibcq->comp_handler(ibcq, ibcq->cq_context); |
| 526 |
} |
| 527 |
|
| 528 |
void hns_roce_cq_event(struct hns_roce_dev *hr_dev, u32 cqn, int event_type) |
| 529 |
{ |
| 530 |
struct device *dev = hr_dev->dev; |
| 531 |
struct hns_roce_cq *hr_cq; |
| 532 |
struct ib_event event; |
| 533 |
struct ib_cq *ibcq; |
| 534 |
|
| 535 |
if (event_type != HNS_ROCE_EVENT_TYPE_CQ_ID_INVALID && |
| 536 |
event_type != HNS_ROCE_EVENT_TYPE_CQ_ACCESS_ERROR && |
| 537 |
event_type != HNS_ROCE_EVENT_TYPE_CQ_OVERFLOW) { |
| 538 |
dev_err(dev, "unexpected event type 0x%x on CQ 0x%06x\n", |
| 539 |
event_type, cqn); |
| 540 |
return; |
| 541 |
} |
| 542 |
|
| 543 |
xa_lock(&hr_dev->cq_table.array); |
| 544 |
hr_cq = xa_load(&hr_dev->cq_table.array, |
| 545 |
cqn & (hr_dev->caps.num_cqs - 1)); |
| 546 |
if (hr_cq) |
| 547 |
refcount_inc(&hr_cq->refcount); |
| 548 |
xa_unlock(&hr_dev->cq_table.array); |
| 549 |
if (!hr_cq) { |
| 550 |
dev_warn(dev, "async event for bogus CQ 0x%06x\n", cqn); |
| 551 |
return; |
| 552 |
} |
| 553 |
|
| 554 |
ibcq = &hr_cq->ib_cq; |
| 555 |
if (ibcq->event_handler) { |
| 556 |
event.device = ibcq->device; |
| 557 |
event.element.cq = ibcq; |
| 558 |
event.event = IB_EVENT_CQ_ERR; |
| 559 |
ibcq->event_handler(&event, ibcq->cq_context); |
| 560 |
} |
| 561 |
|
| 562 |
if (refcount_dec_and_test(&hr_cq->refcount)) |
| 563 |
complete(&hr_cq->free); |
| 564 |
} |
| 565 |
|
| 566 |
void hns_roce_init_cq_table(struct hns_roce_dev *hr_dev) |
| 567 |
{ |
| 568 |
struct hns_roce_cq_table *cq_table = &hr_dev->cq_table; |
| 569 |
unsigned int reserved_from_bot; |
| 570 |
unsigned int i; |
| 571 |
|
| 572 |
mutex_init(&cq_table->bank_mutex); |
| 573 |
xa_init(&cq_table->array); |
| 574 |
|
| 575 |
reserved_from_bot = hr_dev->caps.reserved_cqs; |
| 576 |
|
| 577 |
for (i = 0; i < reserved_from_bot; i++) { |
| 578 |
cq_table->bank[get_cq_bankid(i)].inuse++; |
| 579 |
cq_table->bank[get_cq_bankid(i)].min++; |
| 580 |
} |
| 581 |
|
| 582 |
for (i = 0; i < HNS_ROCE_CQ_BANK_NUM; i++) { |
| 583 |
ida_init(&cq_table->bank[i].ida); |
| 584 |
cq_table->bank[i].max = hr_dev->caps.num_cqs / |
| 585 |
HNS_ROCE_CQ_BANK_NUM - 1; |
| 586 |
} |
| 587 |
|
| 588 |
if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_LIMIT_BANK) |
| 589 |
cq_table->valid_cq_bank_mask = VALID_CQ_BANK_MASK_LIMIT; |
| 590 |
else |
| 591 |
cq_table->valid_cq_bank_mask = VALID_CQ_BANK_MASK_DEFAULT; |
| 592 |
} |
| 593 |
|
| 594 |
void hns_roce_cleanup_cq_table(struct hns_roce_dev *hr_dev) |
| 595 |
{ |
| 596 |
int i; |
| 597 |
|
| 598 |
for (i = 0; i < HNS_ROCE_CQ_BANK_NUM; i++) |
| 599 |
ida_destroy(&hr_dev->cq_table.bank[i].ida); |
| 600 |
xa_destroy(&hr_dev->cq_table.array); |
| 601 |
mutex_destroy(&hr_dev->cq_table.bank_mutex); |
| 602 |
} |