| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* Copyright (c) 2022, Microsoft Corporation. All rights reserved. |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "mana_ib.h" |
| 7 |
|
| 8 |
int mana_ib_create_cq(struct ib_cq *ibcq, const struct ib_cq_init_attr *attr, |
| 9 |
struct uverbs_attr_bundle *attrs) |
| 10 |
{ |
| 11 |
struct ib_udata *udata = &attrs->driver_udata; |
| 12 |
struct mana_ib_cq *cq = container_of(ibcq, struct mana_ib_cq, ibcq); |
| 13 |
struct mana_ib_create_cq_resp resp = {}; |
| 14 |
struct mana_ib_ucontext *mana_ucontext; |
| 15 |
struct ib_device *ibdev = ibcq->device; |
| 16 |
struct mana_ib_create_cq ucmd = {}; |
| 17 |
struct mana_ib_dev *mdev; |
| 18 |
bool is_rnic_cq; |
| 19 |
u32 doorbell; |
| 20 |
u32 buf_size; |
| 21 |
int err; |
| 22 |
|
| 23 |
mdev = container_of(ibdev, struct mana_ib_dev, ib_dev); |
| 24 |
|
| 25 |
cq->comp_vector = attr->comp_vector % ibdev->num_comp_vectors; |
| 26 |
cq->cq_handle = INVALID_MANA_HANDLE; |
| 27 |
is_rnic_cq = mana_ib_is_rnic(mdev); |
| 28 |
|
| 29 |
if (udata) { |
| 30 |
if (udata->inlen < offsetof(struct mana_ib_create_cq, flags)) |
| 31 |
return -EINVAL; |
| 32 |
|
| 33 |
err = ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen)); |
| 34 |
if (err) { |
| 35 |
ibdev_dbg(ibdev, "Failed to copy from udata for create cq, %d\n", err); |
| 36 |
return err; |
| 37 |
} |
| 38 |
|
| 39 |
if ((!is_rnic_cq && attr->cqe > mdev->adapter_caps.max_qp_wr) || |
| 40 |
attr->cqe > U32_MAX / COMP_ENTRY_SIZE) { |
| 41 |
ibdev_dbg(ibdev, "CQE %d exceeding limit\n", attr->cqe); |
| 42 |
return -EINVAL; |
| 43 |
} |
| 44 |
|
| 45 |
cq->cqe = attr->cqe; |
| 46 |
err = mana_ib_create_queue(mdev, ucmd.buf_addr, cq->cqe * COMP_ENTRY_SIZE, |
| 47 |
&cq->queue); |
| 48 |
if (err) { |
| 49 |
ibdev_dbg(ibdev, "Failed to create queue for create cq, %d\n", err); |
| 50 |
return err; |
| 51 |
} |
| 52 |
|
| 53 |
mana_ucontext = rdma_udata_to_drv_context(udata, struct mana_ib_ucontext, |
| 54 |
ibucontext); |
| 55 |
doorbell = mana_ucontext->doorbell; |
| 56 |
} else { |
| 57 |
if (attr->cqe > U32_MAX / COMP_ENTRY_SIZE / 2 + 1) { |
| 58 |
ibdev_dbg(ibdev, "CQE %d exceeding limit\n", attr->cqe); |
| 59 |
return -EINVAL; |
| 60 |
} |
| 61 |
buf_size = MANA_PAGE_ALIGN(roundup_pow_of_two(attr->cqe * COMP_ENTRY_SIZE)); |
| 62 |
cq->cqe = buf_size / COMP_ENTRY_SIZE; |
| 63 |
err = mana_ib_create_kernel_queue(mdev, buf_size, GDMA_CQ, &cq->queue); |
| 64 |
if (err) { |
| 65 |
ibdev_dbg(ibdev, "Failed to create kernel queue for create cq, %d\n", err); |
| 66 |
return err; |
| 67 |
} |
| 68 |
doorbell = mdev->gdma_dev->doorbell; |
| 69 |
} |
| 70 |
|
| 71 |
if (is_rnic_cq) { |
| 72 |
err = mana_ib_gd_create_cq(mdev, cq, doorbell); |
| 73 |
if (err) { |
| 74 |
ibdev_dbg(ibdev, "Failed to create RNIC cq, %d\n", err); |
| 75 |
goto err_destroy_queue; |
| 76 |
} |
| 77 |
|
| 78 |
err = mana_ib_install_cq_cb(mdev, cq); |
| 79 |
if (err) { |
| 80 |
ibdev_dbg(ibdev, "Failed to install cq callback, %d\n", err); |
| 81 |
goto err_destroy_rnic_cq; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
if (udata) { |
| 86 |
resp.cqid = cq->queue.id; |
| 87 |
err = ib_copy_to_udata(udata, &resp, min(sizeof(resp), udata->outlen)); |
| 88 |
if (err) { |
| 89 |
ibdev_dbg(&mdev->ib_dev, "Failed to copy to udata, %d\n", err); |
| 90 |
goto err_remove_cq_cb; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
spin_lock_init(&cq->cq_lock); |
| 95 |
INIT_LIST_HEAD(&cq->list_send_qp); |
| 96 |
INIT_LIST_HEAD(&cq->list_recv_qp); |
| 97 |
|
| 98 |
return 0; |
| 99 |
|
| 100 |
err_remove_cq_cb: |
| 101 |
mana_ib_remove_cq_cb(mdev, cq); |
| 102 |
err_destroy_rnic_cq: |
| 103 |
mana_ib_gd_destroy_cq(mdev, cq); |
| 104 |
err_destroy_queue: |
| 105 |
mana_ib_destroy_queue(mdev, &cq->queue); |
| 106 |
|
| 107 |
return err; |
| 108 |
} |
| 109 |
|
| 110 |
int mana_ib_destroy_cq(struct ib_cq *ibcq, struct ib_udata *udata) |
| 111 |
{ |
| 112 |
struct mana_ib_cq *cq = container_of(ibcq, struct mana_ib_cq, ibcq); |
| 113 |
struct ib_device *ibdev = ibcq->device; |
| 114 |
struct mana_ib_dev *mdev; |
| 115 |
|
| 116 |
mdev = container_of(ibdev, struct mana_ib_dev, ib_dev); |
| 117 |
|
| 118 |
mana_ib_remove_cq_cb(mdev, cq); |
| 119 |
|
| 120 |
/* Ignore return code as there is not much we can do about it. |
| 121 |
* The error message is printed inside. |
| 122 |
*/ |
| 123 |
mana_ib_gd_destroy_cq(mdev, cq); |
| 124 |
|
| 125 |
mana_ib_destroy_queue(mdev, &cq->queue); |
| 126 |
|
| 127 |
return 0; |
| 128 |
} |
| 129 |
|
| 130 |
static void mana_ib_cq_handler(void *ctx, struct gdma_queue *gdma_cq) |
| 131 |
{ |
| 132 |
struct mana_ib_cq *cq = ctx; |
| 133 |
|
| 134 |
if (cq->ibcq.comp_handler) |
| 135 |
cq->ibcq.comp_handler(&cq->ibcq, cq->ibcq.cq_context); |
| 136 |
} |
| 137 |
|
| 138 |
int mana_ib_install_cq_cb(struct mana_ib_dev *mdev, struct mana_ib_cq *cq) |
| 139 |
{ |
| 140 |
struct gdma_context *gc = mdev_to_gc(mdev); |
| 141 |
struct gdma_queue *gdma_cq; |
| 142 |
|
| 143 |
if (cq->queue.id >= gc->max_num_cqs) |
| 144 |
return -EINVAL; |
| 145 |
/* Create CQ table entry, sharing a CQ between WQs is not supported */ |
| 146 |
if (gc->cq_table[cq->queue.id]) |
| 147 |
return -EINVAL; |
| 148 |
if (cq->queue.kmem) |
| 149 |
gdma_cq = cq->queue.kmem; |
| 150 |
else |
| 151 |
gdma_cq = kzalloc_obj(*gdma_cq); |
| 152 |
if (!gdma_cq) |
| 153 |
return -ENOMEM; |
| 154 |
|
| 155 |
gdma_cq->cq.context = cq; |
| 156 |
gdma_cq->type = GDMA_CQ; |
| 157 |
gdma_cq->cq.callback = mana_ib_cq_handler; |
| 158 |
gdma_cq->id = cq->queue.id; |
| 159 |
gc->cq_table[cq->queue.id] = gdma_cq; |
| 160 |
return 0; |
| 161 |
} |
| 162 |
|
| 163 |
void mana_ib_remove_cq_cb(struct mana_ib_dev *mdev, struct mana_ib_cq *cq) |
| 164 |
{ |
| 165 |
struct gdma_context *gc = mdev_to_gc(mdev); |
| 166 |
|
| 167 |
if (cq->queue.id >= gc->max_num_cqs || cq->queue.id == INVALID_QUEUE_ID) |
| 168 |
return; |
| 169 |
|
| 170 |
if (cq->queue.kmem) |
| 171 |
/* Then it will be cleaned and removed by the mana */ |
| 172 |
return; |
| 173 |
|
| 174 |
kfree(gc->cq_table[cq->queue.id]); |
| 175 |
gc->cq_table[cq->queue.id] = NULL; |
| 176 |
} |
| 177 |
|
| 178 |
int mana_ib_arm_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags flags) |
| 179 |
{ |
| 180 |
struct mana_ib_cq *cq = container_of(ibcq, struct mana_ib_cq, ibcq); |
| 181 |
struct gdma_queue *gdma_cq = cq->queue.kmem; |
| 182 |
|
| 183 |
if (!gdma_cq) |
| 184 |
return -EINVAL; |
| 185 |
|
| 186 |
mana_gd_ring_cq(gdma_cq, SET_ARM_BIT); |
| 187 |
return 0; |
| 188 |
} |
| 189 |
|
| 190 |
static inline void handle_ud_sq_cqe(struct mana_ib_qp *qp, struct gdma_comp *cqe) |
| 191 |
{ |
| 192 |
struct mana_rdma_cqe *rdma_cqe = (struct mana_rdma_cqe *)cqe->cqe_data; |
| 193 |
struct gdma_queue *wq = qp->ud_qp.queues[MANA_UD_SEND_QUEUE].kmem; |
| 194 |
struct ud_sq_shadow_wqe *shadow_wqe; |
| 195 |
|
| 196 |
shadow_wqe = shadow_queue_get_next_to_complete(&qp->shadow_sq); |
| 197 |
if (!shadow_wqe) |
| 198 |
return; |
| 199 |
|
| 200 |
shadow_wqe->header.error_code = rdma_cqe->ud_send.vendor_error; |
| 201 |
|
| 202 |
wq->tail += shadow_wqe->header.posted_wqe_size; |
| 203 |
shadow_queue_advance_next_to_complete(&qp->shadow_sq); |
| 204 |
} |
| 205 |
|
| 206 |
static inline void handle_ud_rq_cqe(struct mana_ib_qp *qp, struct gdma_comp *cqe) |
| 207 |
{ |
| 208 |
struct mana_rdma_cqe *rdma_cqe = (struct mana_rdma_cqe *)cqe->cqe_data; |
| 209 |
struct gdma_queue *wq = qp->ud_qp.queues[MANA_UD_RECV_QUEUE].kmem; |
| 210 |
struct ud_rq_shadow_wqe *shadow_wqe; |
| 211 |
|
| 212 |
shadow_wqe = shadow_queue_get_next_to_complete(&qp->shadow_rq); |
| 213 |
if (!shadow_wqe) |
| 214 |
return; |
| 215 |
|
| 216 |
shadow_wqe->byte_len = rdma_cqe->ud_recv.msg_len; |
| 217 |
shadow_wqe->src_qpn = rdma_cqe->ud_recv.src_qpn; |
| 218 |
shadow_wqe->header.error_code = IB_WC_SUCCESS; |
| 219 |
|
| 220 |
wq->tail += shadow_wqe->header.posted_wqe_size; |
| 221 |
shadow_queue_advance_next_to_complete(&qp->shadow_rq); |
| 222 |
} |
| 223 |
|
| 224 |
static void mana_handle_cqe(struct mana_ib_dev *mdev, struct gdma_comp *cqe) |
| 225 |
{ |
| 226 |
struct mana_ib_qp *qp = mana_get_qp_ref(mdev, cqe->wq_num, cqe->is_sq); |
| 227 |
|
| 228 |
if (!qp) |
| 229 |
return; |
| 230 |
|
| 231 |
if (qp->ibqp.qp_type == IB_QPT_GSI || qp->ibqp.qp_type == IB_QPT_UD) { |
| 232 |
if (cqe->is_sq) |
| 233 |
handle_ud_sq_cqe(qp, cqe); |
| 234 |
else |
| 235 |
handle_ud_rq_cqe(qp, cqe); |
| 236 |
} |
| 237 |
|
| 238 |
mana_put_qp_ref(qp); |
| 239 |
} |
| 240 |
|
| 241 |
static void fill_verbs_from_shadow_wqe(struct mana_ib_qp *qp, struct ib_wc *wc, |
| 242 |
const struct shadow_wqe_header *shadow_wqe) |
| 243 |
{ |
| 244 |
const struct ud_rq_shadow_wqe *ud_wqe = (const struct ud_rq_shadow_wqe *)shadow_wqe; |
| 245 |
|
| 246 |
wc->wr_id = shadow_wqe->wr_id; |
| 247 |
wc->status = shadow_wqe->error_code; |
| 248 |
wc->opcode = shadow_wqe->opcode; |
| 249 |
wc->vendor_err = shadow_wqe->error_code; |
| 250 |
wc->wc_flags = 0; |
| 251 |
wc->qp = &qp->ibqp; |
| 252 |
wc->pkey_index = 0; |
| 253 |
|
| 254 |
if (shadow_wqe->opcode == IB_WC_RECV) { |
| 255 |
wc->byte_len = ud_wqe->byte_len; |
| 256 |
wc->src_qp = ud_wqe->src_qpn; |
| 257 |
wc->wc_flags |= IB_WC_GRH; |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
static int mana_process_completions(struct mana_ib_cq *cq, int nwc, struct ib_wc *wc) |
| 262 |
{ |
| 263 |
struct shadow_wqe_header *shadow_wqe; |
| 264 |
struct mana_ib_qp *qp; |
| 265 |
int wc_index = 0; |
| 266 |
|
| 267 |
/* process send shadow queue completions */ |
| 268 |
list_for_each_entry(qp, &cq->list_send_qp, cq_send_list) { |
| 269 |
while ((shadow_wqe = shadow_queue_get_next_to_consume(&qp->shadow_sq)) |
| 270 |
!= NULL) { |
| 271 |
if (wc_index >= nwc) |
| 272 |
goto out; |
| 273 |
|
| 274 |
fill_verbs_from_shadow_wqe(qp, &wc[wc_index], shadow_wqe); |
| 275 |
shadow_queue_advance_consumer(&qp->shadow_sq); |
| 276 |
wc_index++; |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
/* process recv shadow queue completions */ |
| 281 |
list_for_each_entry(qp, &cq->list_recv_qp, cq_recv_list) { |
| 282 |
while ((shadow_wqe = shadow_queue_get_next_to_consume(&qp->shadow_rq)) |
| 283 |
!= NULL) { |
| 284 |
if (wc_index >= nwc) |
| 285 |
goto out; |
| 286 |
|
| 287 |
fill_verbs_from_shadow_wqe(qp, &wc[wc_index], shadow_wqe); |
| 288 |
shadow_queue_advance_consumer(&qp->shadow_rq); |
| 289 |
wc_index++; |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
out: |
| 294 |
return wc_index; |
| 295 |
} |
| 296 |
|
| 297 |
void mana_drain_gsi_sqs(struct mana_ib_dev *mdev) |
| 298 |
{ |
| 299 |
struct mana_ib_qp *qp = mana_get_qp_ref(mdev, MANA_GSI_QPN, false); |
| 300 |
struct ud_sq_shadow_wqe *shadow_wqe; |
| 301 |
struct mana_ib_cq *cq; |
| 302 |
unsigned long flags; |
| 303 |
|
| 304 |
if (!qp) |
| 305 |
return; |
| 306 |
|
| 307 |
cq = container_of(qp->ibqp.send_cq, struct mana_ib_cq, ibcq); |
| 308 |
|
| 309 |
spin_lock_irqsave(&cq->cq_lock, flags); |
| 310 |
while ((shadow_wqe = shadow_queue_get_next_to_complete(&qp->shadow_sq)) |
| 311 |
!= NULL) { |
| 312 |
shadow_wqe->header.error_code = IB_WC_GENERAL_ERR; |
| 313 |
shadow_queue_advance_next_to_complete(&qp->shadow_sq); |
| 314 |
} |
| 315 |
spin_unlock_irqrestore(&cq->cq_lock, flags); |
| 316 |
|
| 317 |
if (cq->ibcq.comp_handler) |
| 318 |
cq->ibcq.comp_handler(&cq->ibcq, cq->ibcq.cq_context); |
| 319 |
|
| 320 |
mana_put_qp_ref(qp); |
| 321 |
} |
| 322 |
|
| 323 |
int mana_ib_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc) |
| 324 |
{ |
| 325 |
struct mana_ib_cq *cq = container_of(ibcq, struct mana_ib_cq, ibcq); |
| 326 |
struct mana_ib_dev *mdev = container_of(ibcq->device, struct mana_ib_dev, ib_dev); |
| 327 |
struct gdma_queue *queue = cq->queue.kmem; |
| 328 |
struct gdma_comp gdma_cqe; |
| 329 |
unsigned long flags; |
| 330 |
int num_polled = 0; |
| 331 |
int comp_read, i; |
| 332 |
|
| 333 |
spin_lock_irqsave(&cq->cq_lock, flags); |
| 334 |
for (i = 0; i < num_entries; i++) { |
| 335 |
comp_read = mana_gd_poll_cq(queue, &gdma_cqe, 1); |
| 336 |
if (comp_read < 1) |
| 337 |
break; |
| 338 |
mana_handle_cqe(mdev, &gdma_cqe); |
| 339 |
} |
| 340 |
|
| 341 |
num_polled = mana_process_completions(cq, num_entries, wc); |
| 342 |
spin_unlock_irqrestore(&cq->cq_lock, flags); |
| 343 |
|
| 344 |
return num_polled; |
| 345 |
} |