Debian Codegraph @

drivers/infiniband/hw/hns/hns_roce_qp.c

raw
1 /*
2 * Copyright (c) 2016 Hisilicon Limited.
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 #include <rdma/ib_addr.h>
35 #include <rdma/ib_umem.h>
36 #include <rdma/uverbs_ioctl.h>
37 #include "hns_roce_common.h"
38 #include "hns_roce_device.h"
39 #include "hns_roce_hem.h"
40
41 static struct hns_roce_qp *hns_roce_qp_lookup(struct hns_roce_dev *hr_dev,
42 u32 qpn)
43 {
44 struct device *dev = hr_dev->dev;
45 struct hns_roce_qp *qp;
46 unsigned long flags;
47
48 xa_lock_irqsave(&hr_dev->qp_table_xa, flags);
49 qp = __hns_roce_qp_lookup(hr_dev, qpn);
50 if (qp)
51 refcount_inc(&qp->refcount);
52 xa_unlock_irqrestore(&hr_dev->qp_table_xa, flags);
53
54 if (!qp)
55 dev_warn(dev, "async event for bogus QP %08x\n", qpn);
56
57 return qp;
58 }
59
60 static void flush_work_handle(struct work_struct *work)
61 {
62 struct hns_roce_work *flush_work = container_of(work,
63 struct hns_roce_work, work);
64 struct hns_roce_qp *hr_qp = container_of(flush_work,
65 struct hns_roce_qp, flush_work);
66 struct device *dev = flush_work->hr_dev->dev;
67 struct ib_qp_attr attr;
68 int attr_mask;
69 int ret;
70
71 attr_mask = IB_QP_STATE;
72 attr.qp_state = IB_QPS_ERR;
73
74 if (test_and_clear_bit(HNS_ROCE_FLUSH_FLAG, &hr_qp->flush_flag)) {
75 ret = hns_roce_modify_qp(&hr_qp->ibqp, &attr, attr_mask, NULL);
76 if (ret)
77 dev_err(dev, "modify QP to error state failed(%d) during CQE flush\n",
78 ret);
79 }
80
81 /*
82 * make sure we signal QP destroy leg that flush QP was completed
83 * so that it can safely proceed ahead now and destroy QP
84 */
85 if (refcount_dec_and_test(&hr_qp->refcount))
86 complete(&hr_qp->free);
87 }
88
89 void init_flush_work(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp)
90 {
91 struct hns_roce_work *flush_work = &hr_qp->flush_work;
92 unsigned long flags;
93
94 spin_lock_irqsave(&hr_qp->flush_lock, flags);
95 /* Exit directly after destroy_qp() */
96 if (test_bit(HNS_ROCE_STOP_FLUSH_FLAG, &hr_qp->flush_flag)) {
97 spin_unlock_irqrestore(&hr_qp->flush_lock, flags);
98 return;
99 }
100
101 refcount_inc(&hr_qp->refcount);
102 queue_work(hr_dev->irq_workq, &flush_work->work);
103 spin_unlock_irqrestore(&hr_qp->flush_lock, flags);
104 }
105
106 void flush_cqe(struct hns_roce_dev *dev, struct hns_roce_qp *qp)
107 {
108 /*
109 * Hip08 hardware cannot flush the WQEs in SQ/RQ if the QP state
110 * gets into errored mode. Hence, as a workaround to this
111 * hardware limitation, driver needs to assist in flushing. But
112 * the flushing operation uses mailbox to convey the QP state to
113 * the hardware and which can sleep due to the mutex protection
114 * around the mailbox calls. Hence, use the deferred flush for
115 * now.
116 */
117 if (!test_and_set_bit(HNS_ROCE_FLUSH_FLAG, &qp->flush_flag))
118 init_flush_work(dev, qp);
119 }
120
121 void hns_roce_qp_event(struct hns_roce_dev *hr_dev, u32 qpn, int event_type)
122 {
123 struct hns_roce_qp *qp;
124
125 qp = hns_roce_qp_lookup(hr_dev, qpn);
126 if (!qp)
127 return;
128
129 qp->event(qp, (enum hns_roce_event)event_type);
130
131 if (refcount_dec_and_test(&qp->refcount))
132 complete(&qp->free);
133 }
134
135 void hns_roce_flush_cqe(struct hns_roce_dev *hr_dev, u32 qpn)
136 {
137 struct hns_roce_qp *qp;
138
139 qp = hns_roce_qp_lookup(hr_dev, qpn);
140 if (!qp)
141 return;
142
143 qp->state = IB_QPS_ERR;
144 flush_cqe(hr_dev, qp);
145
146 if (refcount_dec_and_test(&qp->refcount))
147 complete(&qp->free);
148 }
149
150 static void hns_roce_ib_qp_event(struct hns_roce_qp *hr_qp,
151 enum hns_roce_event type)
152 {
153 struct ib_qp *ibqp = &hr_qp->ibqp;
154 struct ib_event event;
155
156 if (ibqp->event_handler) {
157 event.device = ibqp->device;
158 event.element.qp = ibqp;
159 switch (type) {
160 case HNS_ROCE_EVENT_TYPE_PATH_MIG:
161 event.event = IB_EVENT_PATH_MIG;
162 break;
163 case HNS_ROCE_EVENT_TYPE_COMM_EST:
164 event.event = IB_EVENT_COMM_EST;
165 break;
166 case HNS_ROCE_EVENT_TYPE_SQ_DRAINED:
167 event.event = IB_EVENT_SQ_DRAINED;
168 break;
169 case HNS_ROCE_EVENT_TYPE_SRQ_LAST_WQE_REACH:
170 event.event = IB_EVENT_QP_LAST_WQE_REACHED;
171 break;
172 case HNS_ROCE_EVENT_TYPE_WQ_CATAS_ERROR:
173 event.event = IB_EVENT_QP_FATAL;
174 break;
175 case HNS_ROCE_EVENT_TYPE_PATH_MIG_FAILED:
176 event.event = IB_EVENT_PATH_MIG_ERR;
177 break;
178 case HNS_ROCE_EVENT_TYPE_INV_REQ_LOCAL_WQ_ERROR:
179 event.event = IB_EVENT_QP_REQ_ERR;
180 break;
181 case HNS_ROCE_EVENT_TYPE_LOCAL_WQ_ACCESS_ERROR:
182 case HNS_ROCE_EVENT_TYPE_XRCD_VIOLATION:
183 case HNS_ROCE_EVENT_TYPE_INVALID_XRCETH:
184 event.event = IB_EVENT_QP_ACCESS_ERR;
185 break;
186 default:
187 dev_dbg(ibqp->device->dev.parent, "roce_ib: Unexpected event type %d on QP %06lx\n",
188 type, hr_qp->qpn);
189 return;
190 }
191 ibqp->event_handler(&event, ibqp->qp_context);
192 }
193 }
194
195 static u8 get_affinity_cq_bank(u8 qp_bank)
196 {
197 return (qp_bank >> 1) & CQ_BANKID_MASK;
198 }
199
200 static u8 get_least_load_bankid_for_qp(struct hns_roce_bank *bank, u8 valid_qp_bank_mask)
201 {
202 #define INVALID_LOAD_QPNUM 0xFFFFFFFF
203 u32 least_load = INVALID_LOAD_QPNUM;
204 u8 bankid = 0;
205 u32 bankcnt;
206 u8 i;
207
208 for (i = 0; i < HNS_ROCE_QP_BANK_NUM; i++) {
209 if (!(valid_qp_bank_mask & BIT(i)))
210 continue;
211
212 bankcnt = bank[i].inuse;
213 if (bankcnt < least_load) {
214 least_load = bankcnt;
215 bankid = i;
216 }
217 }
218
219 return bankid;
220 }
221
222 static int alloc_qpn_with_bankid(struct hns_roce_bank *bank, u8 bankid,
223 unsigned long *qpn)
224 {
225 int id;
226
227 id = ida_alloc_range(&bank->ida, bank->next, bank->max, GFP_KERNEL);
228 if (id < 0) {
229 id = ida_alloc_range(&bank->ida, bank->min, bank->max,
230 GFP_KERNEL);
231 if (id < 0)
232 return id;
233 }
234
235 /* the QPN should keep increasing until the max value is reached. */
236 bank->next = (id + 1) > bank->max ? bank->min : id + 1;
237
238 /* the lower 3 bits is bankid */
239 *qpn = (id << 3) | bankid;
240
241 return 0;
242 }
243
244 static bool use_ext_sge(struct ib_qp_init_attr *init_attr)
245 {
246 return init_attr->cap.max_send_sge > HNS_ROCE_SGE_IN_WQE ||
247 init_attr->qp_type == IB_QPT_UD ||
248 init_attr->qp_type == IB_QPT_GSI;
249 }
250
251 static u8 select_qp_bankid(struct hns_roce_dev *hr_dev,
252 struct ib_qp_init_attr *init_attr)
253 {
254 struct hns_roce_qp_table *qp_table = &hr_dev->qp_table;
255 struct hns_roce_bank *bank = qp_table->bank;
256 struct ib_cq *scq = init_attr->send_cq;
257 u8 valid_qp_bank_mask = 0;
258 unsigned long cqn = 0;
259 u8 i;
260
261 if (scq)
262 cqn = to_hr_cq(scq)->cqn;
263
264 for (i = 0; i < HNS_ROCE_QP_BANK_NUM; i++) {
265 if (scq && (get_affinity_cq_bank(i) != (cqn & CQ_BANKID_MASK)))
266 continue;
267
268 if ((hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_LIMIT_BANK) &&
269 use_ext_sge(init_attr) &&
270 !(VALID_EXT_SGE_QP_BANK_MASK_LIMIT & BIT(i)))
271 continue;
272
273 valid_qp_bank_mask |= BIT(i);
274 }
275
276 return get_least_load_bankid_for_qp(bank, valid_qp_bank_mask);
277 }
278
279 static int alloc_qpn(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp,
280 struct ib_qp_init_attr *init_attr)
281 {
282 struct hns_roce_qp_table *qp_table = &hr_dev->qp_table;
283 unsigned long num = 0;
284 u8 bankid;
285 int ret;
286
287 if (hr_qp->ibqp.qp_type == IB_QPT_GSI) {
288 num = 1;
289 } else {
290 mutex_lock(&qp_table->bank_mutex);
291 bankid = select_qp_bankid(hr_dev, init_attr);
292 ret = alloc_qpn_with_bankid(&qp_table->bank[bankid], bankid,
293 &num);
294 if (ret) {
295 ibdev_err(&hr_dev->ib_dev,
296 "failed to alloc QPN, ret = %d\n", ret);
297 mutex_unlock(&qp_table->bank_mutex);
298 return ret;
299 }
300
301 qp_table->bank[bankid].inuse++;
302 mutex_unlock(&qp_table->bank_mutex);
303 }
304
305 hr_qp->qpn = num;
306
307 return 0;
308 }
309
310 static void add_qp_to_list(struct hns_roce_dev *hr_dev,
311 struct hns_roce_qp *hr_qp,
312 struct ib_cq *send_cq, struct ib_cq *recv_cq)
313 {
314 struct hns_roce_cq *hr_send_cq, *hr_recv_cq;
315 unsigned long flags;
316
317 hr_send_cq = send_cq ? to_hr_cq(send_cq) : NULL;
318 hr_recv_cq = recv_cq ? to_hr_cq(recv_cq) : NULL;
319
320 spin_lock_irqsave(&hr_dev->qp_list_lock, flags);
321 hns_roce_lock_cqs(hr_send_cq, hr_recv_cq);
322
323 list_add_tail(&hr_qp->node, &hr_dev->qp_list);
324 if (hr_send_cq)
325 list_add_tail(&hr_qp->sq_node, &hr_send_cq->sq_list);
326 if (hr_recv_cq)
327 list_add_tail(&hr_qp->rq_node, &hr_recv_cq->rq_list);
328
329 hns_roce_unlock_cqs(hr_send_cq, hr_recv_cq);
330 spin_unlock_irqrestore(&hr_dev->qp_list_lock, flags);
331 }
332
333 static int hns_roce_qp_store(struct hns_roce_dev *hr_dev,
334 struct hns_roce_qp *hr_qp,
335 struct ib_qp_init_attr *init_attr)
336 {
337 struct xarray *xa = &hr_dev->qp_table_xa;
338 int ret;
339
340 if (!hr_qp->qpn)
341 return -EINVAL;
342
343 ret = xa_err(xa_store_irq(xa, hr_qp->qpn, hr_qp, GFP_KERNEL));
344 if (ret)
345 dev_err(hr_dev->dev, "failed to xa store for QPC\n");
346 else
347 /* add QP to device's QP list for softwc */
348 add_qp_to_list(hr_dev, hr_qp, init_attr->send_cq,
349 init_attr->recv_cq);
350
351 return ret;
352 }
353
354 static int alloc_qpc(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp)
355 {
356 struct hns_roce_qp_table *qp_table = &hr_dev->qp_table;
357 struct device *dev = hr_dev->dev;
358 int ret;
359
360 if (!hr_qp->qpn)
361 return -EINVAL;
362
363 /* Alloc memory for QPC */
364 ret = hns_roce_table_get(hr_dev, &qp_table->qp_table, hr_qp->qpn);
365 if (ret) {
366 dev_err(dev, "failed to get QPC table\n");
367 goto err_out;
368 }
369
370 /* Alloc memory for IRRL */
371 ret = hns_roce_table_get(hr_dev, &qp_table->irrl_table, hr_qp->qpn);
372 if (ret) {
373 dev_err(dev, "failed to get IRRL table\n");
374 goto err_put_qp;
375 }
376
377 if (hr_dev->caps.trrl_entry_sz) {
378 /* Alloc memory for TRRL */
379 ret = hns_roce_table_get(hr_dev, &qp_table->trrl_table,
380 hr_qp->qpn);
381 if (ret) {
382 dev_err(dev, "failed to get TRRL table\n");
383 goto err_put_irrl;
384 }
385 }
386
387 if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_QP_FLOW_CTRL) {
388 /* Alloc memory for SCC CTX */
389 ret = hns_roce_table_get(hr_dev, &qp_table->sccc_table,
390 hr_qp->qpn);
391 if (ret) {
392 dev_err(dev, "failed to get SCC CTX table\n");
393 goto err_put_trrl;
394 }
395 }
396
397 return 0;
398
399 err_put_trrl:
400 if (hr_dev->caps.trrl_entry_sz)
401 hns_roce_table_put(hr_dev, &qp_table->trrl_table, hr_qp->qpn);
402
403 err_put_irrl:
404 hns_roce_table_put(hr_dev, &qp_table->irrl_table, hr_qp->qpn);
405
406 err_put_qp:
407 hns_roce_table_put(hr_dev, &qp_table->qp_table, hr_qp->qpn);
408
409 err_out:
410 return ret;
411 }
412
413 static void qp_user_mmap_entry_remove(struct hns_roce_qp *hr_qp)
414 {
415 rdma_user_mmap_entry_remove(&hr_qp->dwqe_mmap_entry->rdma_entry);
416 }
417
418 void hns_roce_qp_remove(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp)
419 {
420 struct xarray *xa = &hr_dev->qp_table_xa;
421 unsigned long flags;
422
423 list_del(&hr_qp->node);
424
425 if (hr_qp->ibqp.qp_type != IB_QPT_XRC_TGT)
426 list_del(&hr_qp->sq_node);
427
428 if (hr_qp->ibqp.qp_type != IB_QPT_XRC_INI &&
429 hr_qp->ibqp.qp_type != IB_QPT_XRC_TGT)
430 list_del(&hr_qp->rq_node);
431
432 xa_lock_irqsave(xa, flags);
433 __xa_erase(xa, hr_qp->qpn);
434 xa_unlock_irqrestore(xa, flags);
435 }
436
437 static void free_qpc(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp)
438 {
439 struct hns_roce_qp_table *qp_table = &hr_dev->qp_table;
440
441 if (hr_dev->caps.trrl_entry_sz)
442 hns_roce_table_put(hr_dev, &qp_table->trrl_table, hr_qp->qpn);
443 hns_roce_table_put(hr_dev, &qp_table->irrl_table, hr_qp->qpn);
444 }
445
446 static inline u8 get_qp_bankid(unsigned long qpn)
447 {
448 /* The lower 3 bits of QPN are used to hash to different banks */
449 return (u8)(qpn & GENMASK(2, 0));
450 }
451
452 static void free_qpn(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp)
453 {
454 u8 bankid;
455
456 if (hr_qp->ibqp.qp_type == IB_QPT_GSI)
457 return;
458
459 if (hr_qp->qpn < hr_dev->caps.reserved_qps)
460 return;
461
462 bankid = get_qp_bankid(hr_qp->qpn);
463
464 ida_free(&hr_dev->qp_table.bank[bankid].ida,
465 hr_qp->qpn / HNS_ROCE_QP_BANK_NUM);
466
467 mutex_lock(&hr_dev->qp_table.bank_mutex);
468 hr_dev->qp_table.bank[bankid].inuse--;
469 mutex_unlock(&hr_dev->qp_table.bank_mutex);
470 }
471
472 static u32 proc_rq_sge(struct hns_roce_dev *dev, struct hns_roce_qp *hr_qp,
473 bool user)
474 {
475 u32 max_sge = dev->caps.max_rq_sg;
476
477 if (dev->pci_dev->revision >= PCI_REVISION_ID_HIP09)
478 return max_sge;
479
480 /* Reserve SGEs only for HIP08 in kernel; The userspace driver will
481 * calculate number of max_sge with reserved SGEs when allocating wqe
482 * buf, so there is no need to do this again in kernel. But the number
483 * may exceed the capacity of SGEs recorded in the firmware, so the
484 * kernel driver should just adapt the value accordingly.
485 */
486 if (user)
487 max_sge = roundup_pow_of_two(max_sge + 1);
488 else
489 hr_qp->rq.rsv_sge = 1;
490
491 return max_sge;
492 }
493
494 static int set_rq_size(struct hns_roce_dev *hr_dev, struct ib_qp_cap *cap,
495 struct hns_roce_qp *hr_qp, int has_rq, bool user)
496 {
497 u32 max_sge = proc_rq_sge(hr_dev, hr_qp, user);
498 u32 cnt;
499
500 /* If srq exist, set zero for relative number of rq */
501 if (!has_rq) {
502 hr_qp->rq.wqe_cnt = 0;
503 hr_qp->rq.max_gs = 0;
504 cap->max_recv_wr = 0;
505 cap->max_recv_sge = 0;
506
507 return 0;
508 }
509
510 /* Check the validity of QP support capacity */
511 if (!cap->max_recv_wr || cap->max_recv_wr > hr_dev->caps.max_wqes ||
512 cap->max_recv_sge > max_sge) {
513 ibdev_err(&hr_dev->ib_dev,
514 "RQ config error, depth = %u, sge = %u\n",
515 cap->max_recv_wr, cap->max_recv_sge);
516 return -EINVAL;
517 }
518
519 cnt = roundup_pow_of_two(max(cap->max_recv_wr, hr_dev->caps.min_wqes));
520 if (cnt > hr_dev->caps.max_wqes) {
521 ibdev_err(&hr_dev->ib_dev, "rq depth %u too large\n",
522 cap->max_recv_wr);
523 return -EINVAL;
524 }
525
526 hr_qp->rq.max_gs = roundup_pow_of_two(max(1U, cap->max_recv_sge) +
527 hr_qp->rq.rsv_sge);
528
529 hr_qp->rq.wqe_shift = ilog2(hr_dev->caps.max_rq_desc_sz *
530 hr_qp->rq.max_gs);
531
532 hr_qp->rq.wqe_cnt = cnt;
533
534 cap->max_recv_wr = cnt;
535 cap->max_recv_sge = hr_qp->rq.max_gs - hr_qp->rq.rsv_sge;
536
537 return 0;
538 }
539
540 static u32 get_max_inline_data(struct hns_roce_dev *hr_dev,
541 struct ib_qp_cap *cap)
542 {
543 if (cap->max_inline_data) {
544 cap->max_inline_data = roundup_pow_of_two(cap->max_inline_data);
545 return min(cap->max_inline_data,
546 hr_dev->caps.max_sq_inline);
547 }
548
549 return 0;
550 }
551
552 static void update_inline_data(struct hns_roce_qp *hr_qp,
553 struct ib_qp_cap *cap)
554 {
555 u32 sge_num = hr_qp->sq.ext_sge_cnt;
556
557 if (hr_qp->config & HNS_ROCE_EXSGE_FLAGS) {
558 if (!(hr_qp->ibqp.qp_type == IB_QPT_GSI ||
559 hr_qp->ibqp.qp_type == IB_QPT_UD))
560 sge_num = max((u32)HNS_ROCE_SGE_IN_WQE, sge_num);
561
562 cap->max_inline_data = max(cap->max_inline_data,
563 sge_num * HNS_ROCE_SGE_SIZE);
564 }
565
566 hr_qp->max_inline_data = cap->max_inline_data;
567 }
568
569 static u32 get_sge_num_from_max_send_sge(bool is_ud_or_gsi,
570 u32 max_send_sge)
571 {
572 unsigned int std_sge_num;
573 unsigned int min_sge;
574
575 std_sge_num = is_ud_or_gsi ? 0 : HNS_ROCE_SGE_IN_WQE;
576 min_sge = is_ud_or_gsi ? 1 : 0;
577 return max_send_sge > std_sge_num ? (max_send_sge - std_sge_num) :
578 min_sge;
579 }
580
581 static unsigned int get_sge_num_from_max_inl_data(bool is_ud_or_gsi,
582 u32 max_inline_data)
583 {
584 unsigned int inline_sge;
585
586 if (!max_inline_data)
587 return 0;
588
589 /*
590 * if max_inline_data less than
591 * HNS_ROCE_SGE_IN_WQE * HNS_ROCE_SGE_SIZE,
592 * In addition to ud's mode, no need to extend sge.
593 */
594 inline_sge = roundup_pow_of_two(max_inline_data) / HNS_ROCE_SGE_SIZE;
595 if (!is_ud_or_gsi && inline_sge <= HNS_ROCE_SGE_IN_WQE)
596 inline_sge = 0;
597
598 return inline_sge;
599 }
600
601 static void set_ext_sge_param(struct hns_roce_dev *hr_dev, u32 sq_wqe_cnt,
602 struct hns_roce_qp *hr_qp, struct ib_qp_cap *cap)
603 {
604 bool is_ud_or_gsi = (hr_qp->ibqp.qp_type == IB_QPT_GSI ||
605 hr_qp->ibqp.qp_type == IB_QPT_UD);
606 unsigned int std_sge_num;
607 u32 inline_ext_sge = 0;
608 u32 ext_wqe_sge_cnt;
609 u32 total_sge_cnt;
610
611 cap->max_inline_data = get_max_inline_data(hr_dev, cap);
612
613 hr_qp->sge.sge_shift = HNS_ROCE_SGE_SHIFT;
614 std_sge_num = is_ud_or_gsi ? 0 : HNS_ROCE_SGE_IN_WQE;
615 ext_wqe_sge_cnt = get_sge_num_from_max_send_sge(is_ud_or_gsi,
616 cap->max_send_sge);
617
618 if (hr_qp->config & HNS_ROCE_EXSGE_FLAGS) {
619 inline_ext_sge = max(ext_wqe_sge_cnt,
620 get_sge_num_from_max_inl_data(is_ud_or_gsi,
621 cap->max_inline_data));
622 hr_qp->sq.ext_sge_cnt = inline_ext_sge ?
623 roundup_pow_of_two(inline_ext_sge) : 0;
624
625 hr_qp->sq.max_gs = max(1U, (hr_qp->sq.ext_sge_cnt + std_sge_num));
626 hr_qp->sq.max_gs = min(hr_qp->sq.max_gs, hr_dev->caps.max_sq_sg);
627
628 ext_wqe_sge_cnt = hr_qp->sq.ext_sge_cnt;
629 } else {
630 hr_qp->sq.max_gs = max(1U, cap->max_send_sge);
631 hr_qp->sq.max_gs = min(hr_qp->sq.max_gs, hr_dev->caps.max_sq_sg);
632 hr_qp->sq.ext_sge_cnt = hr_qp->sq.max_gs;
633 }
634
635 /* If the number of extended sge is not zero, they MUST use the
636 * space of HNS_HW_PAGE_SIZE at least.
637 */
638 if (ext_wqe_sge_cnt) {
639 total_sge_cnt = roundup_pow_of_two(sq_wqe_cnt * ext_wqe_sge_cnt);
640 hr_qp->sge.sge_cnt = max(total_sge_cnt,
641 (u32)HNS_HW_PAGE_SIZE / HNS_ROCE_SGE_SIZE);
642 }
643
644 update_inline_data(hr_qp, cap);
645 }
646
647 static int check_sq_size_with_integrity(struct hns_roce_dev *hr_dev,
648 struct ib_qp_cap *cap,
649 struct hns_roce_ib_create_qp *ucmd)
650 {
651 u32 roundup_sq_stride = roundup_pow_of_two(hr_dev->caps.max_sq_desc_sz);
652 u8 max_sq_stride = ilog2(roundup_sq_stride);
653
654 /* Sanity check SQ size before proceeding */
655 if (ucmd->log_sq_stride > max_sq_stride ||
656 ucmd->log_sq_stride < HNS_ROCE_IB_MIN_SQ_STRIDE) {
657 ibdev_err(&hr_dev->ib_dev, "failed to check SQ stride size.\n");
658 return -EINVAL;
659 }
660
661 if (cap->max_send_sge > hr_dev->caps.max_sq_sg) {
662 ibdev_err(&hr_dev->ib_dev, "failed to check SQ SGE size %u.\n",
663 cap->max_send_sge);
664 return -EINVAL;
665 }
666
667 return 0;
668 }
669
670 static int set_user_sq_size(struct hns_roce_dev *hr_dev,
671 struct ib_qp_cap *cap, struct hns_roce_qp *hr_qp,
672 struct hns_roce_ib_create_qp *ucmd)
673 {
674 struct ib_device *ibdev = &hr_dev->ib_dev;
675 u32 cnt = 0;
676 int ret;
677
678 if (check_shl_overflow(1, ucmd->log_sq_bb_count, &cnt) ||
679 cnt > hr_dev->caps.max_wqes)
680 return -EINVAL;
681
682 ret = check_sq_size_with_integrity(hr_dev, cap, ucmd);
683 if (ret) {
684 ibdev_err(ibdev, "failed to check user SQ size, ret = %d.\n",
685 ret);
686 return ret;
687 }
688
689 set_ext_sge_param(hr_dev, cnt, hr_qp, cap);
690
691 hr_qp->sq.wqe_shift = ucmd->log_sq_stride;
692 hr_qp->sq.wqe_cnt = cnt;
693
694 return 0;
695 }
696
697 static int set_wqe_buf_attr(struct hns_roce_dev *hr_dev,
698 struct hns_roce_qp *hr_qp,
699 struct hns_roce_buf_attr *buf_attr)
700 {
701 int buf_size;
702 int idx = 0;
703
704 hr_qp->buff_size = 0;
705
706 /* SQ WQE */
707 hr_qp->sq.offset = 0;
708 buf_size = to_hr_hem_entries_size(hr_qp->sq.wqe_cnt,
709 hr_qp->sq.wqe_shift);
710 if (buf_size > 0 && idx < ARRAY_SIZE(buf_attr->region)) {
711 buf_attr->region[idx].size = buf_size;
712 buf_attr->region[idx].hopnum = hr_dev->caps.wqe_sq_hop_num;
713 idx++;
714 hr_qp->buff_size += buf_size;
715 }
716
717 /* extend SGE WQE in SQ */
718 hr_qp->sge.offset = hr_qp->buff_size;
719 buf_size = to_hr_hem_entries_size(hr_qp->sge.sge_cnt,
720 hr_qp->sge.sge_shift);
721 if (buf_size > 0 && idx < ARRAY_SIZE(buf_attr->region)) {
722 buf_attr->region[idx].size = buf_size;
723 buf_attr->region[idx].hopnum = hr_dev->caps.wqe_sge_hop_num;
724 idx++;
725 hr_qp->buff_size += buf_size;
726 }
727
728 /* RQ WQE */
729 hr_qp->rq.offset = hr_qp->buff_size;
730 buf_size = to_hr_hem_entries_size(hr_qp->rq.wqe_cnt,
731 hr_qp->rq.wqe_shift);
732 if (buf_size > 0 && idx < ARRAY_SIZE(buf_attr->region)) {
733 buf_attr->region[idx].size = buf_size;
734 buf_attr->region[idx].hopnum = hr_dev->caps.wqe_rq_hop_num;
735 idx++;
736 hr_qp->buff_size += buf_size;
737 }
738
739 if (hr_qp->buff_size < 1)
740 return -EINVAL;
741
742 buf_attr->page_shift = HNS_HW_PAGE_SHIFT + hr_dev->caps.mtt_buf_pg_sz;
743 buf_attr->region_count = idx;
744
745 return 0;
746 }
747
748 static int set_kernel_sq_size(struct hns_roce_dev *hr_dev,
749 struct ib_qp_cap *cap, struct hns_roce_qp *hr_qp)
750 {
751 struct ib_device *ibdev = &hr_dev->ib_dev;
752 u32 cnt;
753
754 if (!cap->max_send_wr || cap->max_send_wr > hr_dev->caps.max_wqes ||
755 cap->max_send_sge > hr_dev->caps.max_sq_sg) {
756 ibdev_err(ibdev, "failed to check SQ WR or SGE num.\n");
757 return -EINVAL;
758 }
759
760 cnt = roundup_pow_of_two(max(cap->max_send_wr, hr_dev->caps.min_wqes));
761 if (cnt > hr_dev->caps.max_wqes) {
762 ibdev_err(ibdev, "failed to check WQE num, WQE num = %u.\n",
763 cnt);
764 return -EINVAL;
765 }
766
767 hr_qp->sq.wqe_shift = ilog2(hr_dev->caps.max_sq_desc_sz);
768 hr_qp->sq.wqe_cnt = cnt;
769
770 set_ext_sge_param(hr_dev, cnt, hr_qp, cap);
771
772 /* sync the parameters of kernel QP to user's configuration */
773 cap->max_send_wr = cnt;
774
775 return 0;
776 }
777
778 static int hns_roce_qp_has_sq(struct ib_qp_init_attr *attr)
779 {
780 if (attr->qp_type == IB_QPT_XRC_TGT || !attr->cap.max_send_wr)
781 return 0;
782
783 return 1;
784 }
785
786 static int hns_roce_qp_has_rq(struct ib_qp_init_attr *attr)
787 {
788 if (attr->qp_type == IB_QPT_XRC_INI ||
789 attr->qp_type == IB_QPT_XRC_TGT || attr->srq ||
790 !attr->cap.max_recv_wr)
791 return 0;
792
793 return 1;
794 }
795
796 static int alloc_qp_buf(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp,
797 struct ib_qp_init_attr *init_attr,
798 struct ib_udata *udata, unsigned long addr)
799 {
800 struct ib_device *ibdev = &hr_dev->ib_dev;
801 struct hns_roce_buf_attr buf_attr = {};
802 int ret;
803
804 ret = set_wqe_buf_attr(hr_dev, hr_qp, &buf_attr);
805 if (ret) {
806 ibdev_err(ibdev, "failed to split WQE buf, ret = %d.\n", ret);
807 goto err_inline;
808 }
809 ret = hns_roce_mtr_create(hr_dev, &hr_qp->mtr, &buf_attr,
810 PAGE_SHIFT + hr_dev->caps.mtt_ba_pg_sz,
811 udata, addr);
812 if (ret) {
813 ibdev_err(ibdev, "failed to create WQE mtr, ret = %d.\n", ret);
814 goto err_inline;
815 }
816
817 if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_DIRECT_WQE)
818 hr_qp->en_flags |= HNS_ROCE_QP_CAP_DIRECT_WQE;
819
820 return 0;
821
822 err_inline:
823
824 return ret;
825 }
826
827 static void free_qp_buf(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp)
828 {
829 hns_roce_mtr_destroy(hr_dev, &hr_qp->mtr);
830 }
831
832 static inline bool user_qp_has_sdb(struct hns_roce_dev *hr_dev,
833 struct ib_qp_init_attr *init_attr,
834 struct ib_udata *udata,
835 struct hns_roce_ib_create_qp_resp *resp,
836 struct hns_roce_ib_create_qp *ucmd)
837 {
838 return ((hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_QP_RECORD_DB) &&
839 udata->outlen >= offsetofend(typeof(*resp), cap_flags) &&
840 hns_roce_qp_has_sq(init_attr) &&
841 udata->inlen >= offsetofend(typeof(*ucmd), sdb_addr));
842 }
843
844 static inline bool user_qp_has_rdb(struct hns_roce_dev *hr_dev,
845 struct ib_qp_init_attr *init_attr,
846 struct ib_udata *udata,
847 struct hns_roce_ib_create_qp_resp *resp)
848 {
849 return ((hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_QP_RECORD_DB) &&
850 udata->outlen >= offsetofend(typeof(*resp), cap_flags) &&
851 hns_roce_qp_has_rq(init_attr));
852 }
853
854 static inline bool kernel_qp_has_rdb(struct hns_roce_dev *hr_dev,
855 struct ib_qp_init_attr *init_attr)
856 {
857 return ((hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_QP_RECORD_DB) &&
858 hns_roce_qp_has_rq(init_attr));
859 }
860
861 static int qp_mmap_entry(struct hns_roce_qp *hr_qp,
862 struct hns_roce_dev *hr_dev,
863 struct ib_udata *udata,
864 struct hns_roce_ib_create_qp_resp *resp)
865 {
866 struct hns_roce_ucontext *uctx =
867 rdma_udata_to_drv_context(udata,
868 struct hns_roce_ucontext, ibucontext);
869 struct rdma_user_mmap_entry *rdma_entry;
870 u64 address;
871
872 address = hr_dev->dwqe_page + hr_qp->qpn * HNS_ROCE_DWQE_SIZE;
873
874 hr_qp->dwqe_mmap_entry =
875 hns_roce_user_mmap_entry_insert(&uctx->ibucontext, address,
876 HNS_ROCE_DWQE_SIZE,
877 HNS_ROCE_MMAP_TYPE_DWQE);
878
879 if (!hr_qp->dwqe_mmap_entry) {
880 ibdev_err(&hr_dev->ib_dev, "failed to get dwqe mmap entry.\n");
881 return -ENOMEM;
882 }
883
884 rdma_entry = &hr_qp->dwqe_mmap_entry->rdma_entry;
885 resp->dwqe_mmap_key = rdma_user_mmap_get_offset(rdma_entry);
886
887 return 0;
888 }
889
890 static int alloc_user_qp_db(struct hns_roce_dev *hr_dev,
891 struct hns_roce_qp *hr_qp,
892 struct ib_qp_init_attr *init_attr,
893 struct ib_udata *udata,
894 struct hns_roce_ib_create_qp *ucmd,
895 struct hns_roce_ib_create_qp_resp *resp)
896 {
897 bool has_sdb = user_qp_has_sdb(hr_dev, init_attr, udata, resp, ucmd);
898 struct hns_roce_ucontext *uctx = rdma_udata_to_drv_context(udata,
899 struct hns_roce_ucontext, ibucontext);
900 bool has_rdb = user_qp_has_rdb(hr_dev, init_attr, udata, resp);
901 struct ib_device *ibdev = &hr_dev->ib_dev;
902 int ret;
903
904 if (has_sdb) {
905 ret = hns_roce_db_map_user(uctx, ucmd->sdb_addr, &hr_qp->sdb);
906 if (ret) {
907 ibdev_err(ibdev,
908 "failed to map user SQ doorbell, ret = %d.\n",
909 ret);
910 goto err_out;
911 }
912 hr_qp->en_flags |= HNS_ROCE_QP_CAP_SQ_RECORD_DB;
913 }
914
915 if (has_rdb) {
916 ret = hns_roce_db_map_user(uctx, ucmd->db_addr, &hr_qp->rdb);
917 if (ret) {
918 ibdev_err(ibdev,
919 "failed to map user RQ doorbell, ret = %d.\n",
920 ret);
921 goto err_sdb;
922 }
923 hr_qp->en_flags |= HNS_ROCE_QP_CAP_RQ_RECORD_DB;
924 }
925
926 return 0;
927
928 err_sdb:
929 if (has_sdb)
930 hns_roce_db_unmap_user(uctx, &hr_qp->sdb);
931 err_out:
932 return ret;
933 }
934
935 static int alloc_kernel_qp_db(struct hns_roce_dev *hr_dev,
936 struct hns_roce_qp *hr_qp,
937 struct ib_qp_init_attr *init_attr)
938 {
939 struct ib_device *ibdev = &hr_dev->ib_dev;
940 int ret;
941
942 if (hr_dev->pci_dev->revision >= PCI_REVISION_ID_HIP09)
943 hr_qp->sq.db_reg = hr_dev->mem_base +
944 HNS_ROCE_DWQE_SIZE * hr_qp->qpn;
945 else
946 hr_qp->sq.db_reg = hr_dev->reg_base + hr_dev->sdb_offset +
947 DB_REG_OFFSET * hr_dev->priv_uar.index;
948
949 hr_qp->rq.db_reg = hr_dev->reg_base + hr_dev->odb_offset +
950 DB_REG_OFFSET * hr_dev->priv_uar.index;
951
952 if (kernel_qp_has_rdb(hr_dev, init_attr)) {
953 ret = hns_roce_alloc_db(hr_dev, &hr_qp->rdb, 0);
954 if (ret) {
955 ibdev_err(ibdev,
956 "failed to alloc kernel RQ doorbell, ret = %d.\n",
957 ret);
958 return ret;
959 }
960 *hr_qp->rdb.db_record = 0;
961 hr_qp->en_flags |= HNS_ROCE_QP_CAP_RQ_RECORD_DB;
962 }
963
964 return 0;
965 }
966
967 static int alloc_qp_db(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp,
968 struct ib_qp_init_attr *init_attr,
969 struct ib_udata *udata,
970 struct hns_roce_ib_create_qp *ucmd,
971 struct hns_roce_ib_create_qp_resp *resp)
972 {
973 int ret;
974
975 if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_SDI_MODE)
976 hr_qp->en_flags |= HNS_ROCE_QP_CAP_OWNER_DB;
977
978 if (udata) {
979 if (hr_qp->en_flags & HNS_ROCE_QP_CAP_DIRECT_WQE) {
980 ret = qp_mmap_entry(hr_qp, hr_dev, udata, resp);
981 if (ret)
982 return ret;
983 }
984
985 ret = alloc_user_qp_db(hr_dev, hr_qp, init_attr, udata, ucmd,
986 resp);
987 if (ret)
988 goto err_remove_qp;
989 } else {
990 ret = alloc_kernel_qp_db(hr_dev, hr_qp, init_attr);
991 if (ret)
992 return ret;
993 }
994
995 return 0;
996
997 err_remove_qp:
998 if (hr_qp->en_flags & HNS_ROCE_QP_CAP_DIRECT_WQE)
999 qp_user_mmap_entry_remove(hr_qp);
1000
1001 return ret;
1002 }
1003
1004 static void free_qp_db(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp,
1005 struct ib_udata *udata)
1006 {
1007 struct hns_roce_ucontext *uctx = rdma_udata_to_drv_context(
1008 udata, struct hns_roce_ucontext, ibucontext);
1009
1010 if (udata) {
1011 if (hr_qp->en_flags & HNS_ROCE_QP_CAP_RQ_RECORD_DB)
1012 hns_roce_db_unmap_user(uctx, &hr_qp->rdb);
1013 if (hr_qp->en_flags & HNS_ROCE_QP_CAP_SQ_RECORD_DB)
1014 hns_roce_db_unmap_user(uctx, &hr_qp->sdb);
1015 if (hr_qp->en_flags & HNS_ROCE_QP_CAP_DIRECT_WQE)
1016 qp_user_mmap_entry_remove(hr_qp);
1017 } else {
1018 if (hr_qp->en_flags & HNS_ROCE_QP_CAP_RQ_RECORD_DB)
1019 hns_roce_free_db(hr_dev, &hr_qp->rdb);
1020 }
1021 }
1022
1023 static int alloc_kernel_wrid(struct hns_roce_dev *hr_dev,
1024 struct hns_roce_qp *hr_qp)
1025 {
1026 struct ib_device *ibdev = &hr_dev->ib_dev;
1027 u64 *sq_wrid = NULL;
1028 u64 *rq_wrid = NULL;
1029 int ret;
1030
1031 sq_wrid = kcalloc(hr_qp->sq.wqe_cnt, sizeof(u64), GFP_KERNEL);
1032 if (!sq_wrid) {
1033 ibdev_err(ibdev, "failed to alloc SQ wrid.\n");
1034 return -ENOMEM;
1035 }
1036
1037 if (hr_qp->rq.wqe_cnt) {
1038 rq_wrid = kcalloc(hr_qp->rq.wqe_cnt, sizeof(u64), GFP_KERNEL);
1039 if (!rq_wrid) {
1040 ibdev_err(ibdev, "failed to alloc RQ wrid.\n");
1041 ret = -ENOMEM;
1042 goto err_sq;
1043 }
1044 }
1045
1046 hr_qp->sq.wrid = sq_wrid;
1047 hr_qp->rq.wrid = rq_wrid;
1048 return 0;
1049 err_sq:
1050 kfree(sq_wrid);
1051
1052 return ret;
1053 }
1054
1055 static void free_kernel_wrid(struct hns_roce_qp *hr_qp)
1056 {
1057 kfree(hr_qp->rq.wrid);
1058 kfree(hr_qp->sq.wrid);
1059 }
1060
1061 static void default_congest_type(struct hns_roce_dev *hr_dev,
1062 struct hns_roce_qp *hr_qp)
1063 {
1064 if (hr_qp->ibqp.qp_type == IB_QPT_UD ||
1065 hr_qp->ibqp.qp_type == IB_QPT_GSI)
1066 hr_qp->cong_type = CONG_TYPE_DCQCN;
1067 else
1068 hr_qp->cong_type = hr_dev->caps.default_cong_type;
1069 }
1070
1071 static int set_congest_type(struct hns_roce_qp *hr_qp,
1072 struct hns_roce_ib_create_qp *ucmd)
1073 {
1074 struct hns_roce_dev *hr_dev = to_hr_dev(hr_qp->ibqp.device);
1075
1076 switch (ucmd->cong_type_flags) {
1077 case HNS_ROCE_CREATE_QP_FLAGS_DCQCN:
1078 hr_qp->cong_type = CONG_TYPE_DCQCN;
1079 break;
1080 case HNS_ROCE_CREATE_QP_FLAGS_LDCP:
1081 hr_qp->cong_type = CONG_TYPE_LDCP;
1082 break;
1083 case HNS_ROCE_CREATE_QP_FLAGS_HC3:
1084 hr_qp->cong_type = CONG_TYPE_HC3;
1085 break;
1086 case HNS_ROCE_CREATE_QP_FLAGS_DIP:
1087 hr_qp->cong_type = CONG_TYPE_DIP;
1088 break;
1089 default:
1090 return -EINVAL;
1091 }
1092
1093 if (!test_bit(hr_qp->cong_type, (unsigned long *)&hr_dev->caps.cong_cap))
1094 return -EOPNOTSUPP;
1095
1096 if (hr_qp->ibqp.qp_type == IB_QPT_UD &&
1097 hr_qp->cong_type != CONG_TYPE_DCQCN)
1098 return -EOPNOTSUPP;
1099
1100 return 0;
1101 }
1102
1103 static int set_congest_param(struct hns_roce_dev *hr_dev,
1104 struct hns_roce_qp *hr_qp,
1105 struct hns_roce_ib_create_qp *ucmd)
1106 {
1107 if (ucmd->comp_mask & HNS_ROCE_CREATE_QP_MASK_CONGEST_TYPE)
1108 return set_congest_type(hr_qp, ucmd);
1109
1110 default_congest_type(hr_dev, hr_qp);
1111
1112 return 0;
1113 }
1114
1115 static int set_qp_param(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp,
1116 struct ib_qp_init_attr *init_attr,
1117 struct ib_udata *udata,
1118 struct hns_roce_ib_create_qp *ucmd)
1119 {
1120 struct ib_device *ibdev = &hr_dev->ib_dev;
1121 struct hns_roce_ucontext *uctx;
1122 int ret;
1123
1124 if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR)
1125 hr_qp->sq_signal_bits = IB_SIGNAL_ALL_WR;
1126 else
1127 hr_qp->sq_signal_bits = IB_SIGNAL_REQ_WR;
1128
1129 ret = set_rq_size(hr_dev, &init_attr->cap, hr_qp,
1130 hns_roce_qp_has_rq(init_attr), !!udata);
1131 if (ret) {
1132 ibdev_err(ibdev, "failed to set user RQ size, ret = %d.\n",
1133 ret);
1134 return ret;
1135 }
1136
1137 if (udata) {
1138 ret = ib_copy_from_udata(ucmd, udata,
1139 min(udata->inlen, sizeof(*ucmd)));
1140 if (ret) {
1141 ibdev_err(ibdev,
1142 "failed to copy QP ucmd, ret = %d\n", ret);
1143 return ret;
1144 }
1145
1146 uctx = rdma_udata_to_drv_context(udata, struct hns_roce_ucontext,
1147 ibucontext);
1148 hr_qp->config = uctx->config;
1149 ret = set_user_sq_size(hr_dev, &init_attr->cap, hr_qp, ucmd);
1150 if (ret) {
1151 ibdev_err(ibdev,
1152 "failed to set user SQ size, ret = %d.\n",
1153 ret);
1154 return ret;
1155 }
1156
1157 ret = set_congest_param(hr_dev, hr_qp, ucmd);
1158 } else {
1159 if (hr_dev->pci_dev->revision >= PCI_REVISION_ID_HIP09)
1160 hr_qp->config = HNS_ROCE_EXSGE_FLAGS;
1161 default_congest_type(hr_dev, hr_qp);
1162 ret = set_kernel_sq_size(hr_dev, &init_attr->cap, hr_qp);
1163 if (ret)
1164 ibdev_err(ibdev,
1165 "failed to set kernel SQ size, ret = %d.\n",
1166 ret);
1167 }
1168
1169 return ret;
1170 }
1171
1172 static int hns_roce_create_qp_common(struct hns_roce_dev *hr_dev,
1173 struct ib_qp_init_attr *init_attr,
1174 struct ib_udata *udata,
1175 struct hns_roce_qp *hr_qp)
1176 {
1177 struct hns_roce_work *flush_work = &hr_qp->flush_work;
1178 struct hns_roce_ib_create_qp_resp resp = {};
1179 struct ib_device *ibdev = &hr_dev->ib_dev;
1180 struct hns_roce_ib_create_qp ucmd = {};
1181 unsigned long flags;
1182 int ret;
1183
1184 mutex_init(&hr_qp->mutex);
1185 spin_lock_init(&hr_qp->sq.lock);
1186 spin_lock_init(&hr_qp->rq.lock);
1187 spin_lock_init(&hr_qp->flush_lock);
1188
1189 hr_qp->state = IB_QPS_RESET;
1190 hr_qp->flush_flag = 0;
1191 flush_work->hr_dev = hr_dev;
1192 INIT_WORK(&flush_work->work, flush_work_handle);
1193
1194 if (init_attr->create_flags)
1195 return -EOPNOTSUPP;
1196
1197 ret = set_qp_param(hr_dev, hr_qp, init_attr, udata, &ucmd);
1198 if (ret) {
1199 ibdev_err(ibdev, "failed to set QP param, ret = %d.\n", ret);
1200 goto err_out;
1201 }
1202
1203 if (!udata) {
1204 ret = alloc_kernel_wrid(hr_dev, hr_qp);
1205 if (ret) {
1206 ibdev_err(ibdev, "failed to alloc wrid, ret = %d.\n",
1207 ret);
1208 goto err_out;
1209 }
1210 }
1211
1212 ret = alloc_qp_buf(hr_dev, hr_qp, init_attr, udata, ucmd.buf_addr);
1213 if (ret) {
1214 ibdev_err(ibdev, "failed to alloc QP buffer, ret = %d.\n", ret);
1215 goto err_buf;
1216 }
1217
1218 ret = alloc_qpn(hr_dev, hr_qp, init_attr);
1219 if (ret) {
1220 ibdev_err(ibdev, "failed to alloc QPN, ret = %d.\n", ret);
1221 goto err_qpn;
1222 }
1223
1224 ret = alloc_qp_db(hr_dev, hr_qp, init_attr, udata, &ucmd, &resp);
1225 if (ret) {
1226 ibdev_err(ibdev, "failed to alloc QP doorbell, ret = %d.\n",
1227 ret);
1228 goto err_db;
1229 }
1230
1231 ret = alloc_qpc(hr_dev, hr_qp);
1232 if (ret) {
1233 ibdev_err(ibdev, "failed to alloc QP context, ret = %d.\n",
1234 ret);
1235 goto err_qpc;
1236 }
1237
1238 ret = hns_roce_qp_store(hr_dev, hr_qp, init_attr);
1239 if (ret) {
1240 ibdev_err(ibdev, "failed to store QP, ret = %d.\n", ret);
1241 goto err_store;
1242 }
1243
1244 if (udata) {
1245 resp.cap_flags = hr_qp->en_flags;
1246 ret = ib_copy_to_udata(udata, &resp,
1247 min(udata->outlen, sizeof(resp)));
1248 if (ret) {
1249 ibdev_err(ibdev, "copy qp resp failed!\n");
1250 goto err_flow_ctrl;
1251 }
1252 }
1253
1254 if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_QP_FLOW_CTRL) {
1255 ret = hr_dev->hw->qp_flow_control_init(hr_dev, hr_qp);
1256 if (ret)
1257 goto err_flow_ctrl;
1258 }
1259
1260 hr_qp->ibqp.qp_num = hr_qp->qpn;
1261 hr_qp->event = hns_roce_ib_qp_event;
1262 refcount_set(&hr_qp->refcount, 1);
1263 init_completion(&hr_qp->free);
1264
1265 return 0;
1266
1267 err_flow_ctrl:
1268 spin_lock_irqsave(&hr_dev->qp_list_lock, flags);
1269 hns_roce_lock_cqs(init_attr->send_cq ? to_hr_cq(init_attr->send_cq) : NULL,
1270 init_attr->recv_cq ? to_hr_cq(init_attr->recv_cq) : NULL);
1271 hns_roce_qp_remove(hr_dev, hr_qp);
1272 hns_roce_unlock_cqs(init_attr->send_cq ? to_hr_cq(init_attr->send_cq) : NULL,
1273 init_attr->recv_cq ? to_hr_cq(init_attr->recv_cq) : NULL);
1274 spin_unlock_irqrestore(&hr_dev->qp_list_lock, flags);
1275 err_store:
1276 free_qpc(hr_dev, hr_qp);
1277 err_qpc:
1278 free_qp_db(hr_dev, hr_qp, udata);
1279 err_db:
1280 free_qpn(hr_dev, hr_qp);
1281 err_qpn:
1282 free_qp_buf(hr_dev, hr_qp);
1283 err_buf:
1284 free_kernel_wrid(hr_qp);
1285 err_out:
1286 mutex_destroy(&hr_qp->mutex);
1287 return ret;
1288 }
1289
1290 void hns_roce_qp_destroy(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp,
1291 struct ib_udata *udata)
1292 {
1293 if (refcount_dec_and_test(&hr_qp->refcount))
1294 complete(&hr_qp->free);
1295 wait_for_completion(&hr_qp->free);
1296
1297 free_qpc(hr_dev, hr_qp);
1298 free_qpn(hr_dev, hr_qp);
1299 free_qp_buf(hr_dev, hr_qp);
1300 free_kernel_wrid(hr_qp);
1301 free_qp_db(hr_dev, hr_qp, udata);
1302 mutex_destroy(&hr_qp->mutex);
1303 }
1304
1305 static int check_qp_type(struct hns_roce_dev *hr_dev, enum ib_qp_type type,
1306 bool is_user)
1307 {
1308 switch (type) {
1309 case IB_QPT_XRC_INI:
1310 case IB_QPT_XRC_TGT:
1311 if (!(hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_XRC))
1312 goto out;
1313 break;
1314 case IB_QPT_UD:
1315 if (hr_dev->pci_dev->revision == PCI_REVISION_ID_HIP08 &&
1316 is_user)
1317 goto out;
1318 break;
1319 case IB_QPT_RC:
1320 case IB_QPT_GSI:
1321 break;
1322 default:
1323 goto out;
1324 }
1325
1326 return 0;
1327
1328 out:
1329 ibdev_err(&hr_dev->ib_dev, "not support QP type %d\n", type);
1330
1331 return -EOPNOTSUPP;
1332 }
1333
1334 int hns_roce_create_qp(struct ib_qp *qp, struct ib_qp_init_attr *init_attr,
1335 struct ib_udata *udata)
1336 {
1337 struct ib_device *ibdev = qp->device;
1338 struct hns_roce_dev *hr_dev = to_hr_dev(ibdev);
1339 struct hns_roce_qp *hr_qp = to_hr_qp(qp);
1340 int ret;
1341
1342 ret = check_qp_type(hr_dev, init_attr->qp_type, !!udata);
1343 if (ret)
1344 goto err_out;
1345
1346 if (init_attr->qp_type == IB_QPT_XRC_TGT)
1347 hr_qp->xrcdn = to_hr_xrcd(init_attr->xrcd)->xrcdn;
1348
1349 if (init_attr->qp_type == IB_QPT_GSI) {
1350 hr_qp->port = init_attr->port_num - 1;
1351 hr_qp->phy_port = hr_dev->iboe.phy_port[hr_qp->port];
1352 }
1353
1354 ret = hns_roce_create_qp_common(hr_dev, init_attr, udata, hr_qp);
1355 if (ret)
1356 ibdev_err(ibdev, "create QP type %d failed(%d)\n",
1357 init_attr->qp_type, ret);
1358
1359 err_out:
1360 if (ret)
1361 atomic64_inc(&hr_dev->dfx_cnt[HNS_ROCE_DFX_QP_CREATE_ERR_CNT]);
1362
1363 return ret;
1364 }
1365
1366 int to_hr_qp_type(int qp_type)
1367 {
1368 switch (qp_type) {
1369 case IB_QPT_RC:
1370 return SERV_TYPE_RC;
1371 case IB_QPT_UD:
1372 case IB_QPT_GSI:
1373 return SERV_TYPE_UD;
1374 case IB_QPT_XRC_INI:
1375 case IB_QPT_XRC_TGT:
1376 return SERV_TYPE_XRC;
1377 default:
1378 return -1;
1379 }
1380 }
1381
1382 static int check_mtu_validate(struct hns_roce_dev *hr_dev,
1383 struct hns_roce_qp *hr_qp,
1384 struct ib_qp_attr *attr, int attr_mask)
1385 {
1386 struct net_device *net_dev;
1387 enum ib_mtu active_mtu;
1388 int p;
1389
1390 p = attr_mask & IB_QP_PORT ? (attr->port_num - 1) : hr_qp->port;
1391 net_dev = get_hr_netdev(hr_dev, p);
1392 active_mtu = iboe_get_mtu(net_dev->mtu);
1393
1394 if ((hr_dev->caps.max_mtu >= IB_MTU_2048 &&
1395 attr->path_mtu > hr_dev->caps.max_mtu) ||
1396 attr->path_mtu < IB_MTU_256 || attr->path_mtu > active_mtu) {
1397 ibdev_err(&hr_dev->ib_dev,
1398 "attr path_mtu(%d)invalid while modify qp",
1399 attr->path_mtu);
1400 return -EINVAL;
1401 }
1402
1403 return 0;
1404 }
1405
1406 static int hns_roce_check_qp_attr(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1407 int attr_mask)
1408 {
1409 struct hns_roce_dev *hr_dev = to_hr_dev(ibqp->device);
1410 struct hns_roce_qp *hr_qp = to_hr_qp(ibqp);
1411 int p;
1412
1413 if ((attr_mask & IB_QP_PORT) &&
1414 (attr->port_num == 0 || attr->port_num > hr_dev->caps.num_ports)) {
1415 ibdev_err(&hr_dev->ib_dev, "invalid attr, port_num = %u.\n",
1416 attr->port_num);
1417 return -EINVAL;
1418 }
1419
1420 if (attr_mask & IB_QP_PKEY_INDEX) {
1421 p = attr_mask & IB_QP_PORT ? (attr->port_num - 1) : hr_qp->port;
1422 if (attr->pkey_index >= hr_dev->caps.pkey_table_len[p]) {
1423 ibdev_err(&hr_dev->ib_dev,
1424 "invalid attr, pkey_index = %u.\n",
1425 attr->pkey_index);
1426 return -EINVAL;
1427 }
1428 }
1429
1430 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
1431 attr->max_rd_atomic > hr_dev->caps.max_qp_init_rdma) {
1432 ibdev_err(&hr_dev->ib_dev,
1433 "invalid attr, max_rd_atomic = %u.\n",
1434 attr->max_rd_atomic);
1435 return -EINVAL;
1436 }
1437
1438 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
1439 attr->max_dest_rd_atomic > hr_dev->caps.max_qp_dest_rdma) {
1440 ibdev_err(&hr_dev->ib_dev,
1441 "invalid attr, max_dest_rd_atomic = %u.\n",
1442 attr->max_dest_rd_atomic);
1443 return -EINVAL;
1444 }
1445
1446 if (attr_mask & IB_QP_PATH_MTU)
1447 return check_mtu_validate(hr_dev, hr_qp, attr, attr_mask);
1448
1449 return 0;
1450 }
1451
1452 int hns_roce_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1453 int attr_mask, struct ib_udata *udata)
1454 {
1455 struct hns_roce_dev *hr_dev = to_hr_dev(ibqp->device);
1456 struct hns_roce_ib_modify_qp_resp resp = {};
1457 struct hns_roce_qp *hr_qp = to_hr_qp(ibqp);
1458 enum ib_qp_state cur_state, new_state;
1459 int ret = -EINVAL;
1460
1461 mutex_lock(&hr_qp->mutex);
1462
1463 if (attr_mask & IB_QP_CUR_STATE && attr->cur_qp_state != hr_qp->state)
1464 goto out;
1465
1466 cur_state = hr_qp->state;
1467 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
1468
1469 if (ibqp->uobject &&
1470 (attr_mask & IB_QP_STATE) && new_state == IB_QPS_ERR) {
1471 if (hr_qp->en_flags & HNS_ROCE_QP_CAP_SQ_RECORD_DB) {
1472 hr_qp->sq.head = *(int *)(hr_qp->sdb.virt_addr);
1473
1474 if (hr_qp->en_flags & HNS_ROCE_QP_CAP_RQ_RECORD_DB)
1475 hr_qp->rq.head = *(int *)(hr_qp->rdb.virt_addr);
1476 } else {
1477 ibdev_warn(&hr_dev->ib_dev,
1478 "flush cqe is not supported in userspace!\n");
1479 goto out;
1480 }
1481 }
1482
1483 if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
1484 attr_mask)) {
1485 ibdev_err(&hr_dev->ib_dev, "ib_modify_qp_is_ok failed\n");
1486 goto out;
1487 }
1488
1489 ret = hns_roce_check_qp_attr(ibqp, attr, attr_mask);
1490 if (ret)
1491 goto out;
1492
1493 if (cur_state == new_state && cur_state == IB_QPS_RESET)
1494 goto out;
1495
1496 ret = hr_dev->hw->modify_qp(ibqp, attr, attr_mask, cur_state,
1497 new_state, udata);
1498 if (ret)
1499 goto out;
1500
1501 if (udata && udata->outlen) {
1502 resp.tc_mode = hr_qp->tc_mode;
1503 resp.priority = hr_qp->sl;
1504 ret = ib_copy_to_udata(udata, &resp,
1505 min(udata->outlen, sizeof(resp)));
1506 if (ret)
1507 ibdev_err_ratelimited(&hr_dev->ib_dev,
1508 "failed to copy modify qp resp.\n");
1509 }
1510
1511 out:
1512 mutex_unlock(&hr_qp->mutex);
1513 if (ret)
1514 atomic64_inc(&hr_dev->dfx_cnt[HNS_ROCE_DFX_QP_MODIFY_ERR_CNT]);
1515
1516 return ret;
1517 }
1518
1519 void hns_roce_lock_cqs(struct hns_roce_cq *send_cq, struct hns_roce_cq *recv_cq)
1520 __acquires(&send_cq->lock) __acquires(&recv_cq->lock)
1521 {
1522 if (unlikely(send_cq == NULL && recv_cq == NULL)) {
1523 __acquire(&send_cq->lock);
1524 __acquire(&recv_cq->lock);
1525 } else if (unlikely(send_cq != NULL && recv_cq == NULL)) {
1526 spin_lock(&send_cq->lock);
1527 __acquire(&recv_cq->lock);
1528 } else if (unlikely(send_cq == NULL && recv_cq != NULL)) {
1529 spin_lock(&recv_cq->lock);
1530 __acquire(&send_cq->lock);
1531 } else if (send_cq == recv_cq) {
1532 spin_lock(&send_cq->lock);
1533 __acquire(&recv_cq->lock);
1534 } else if (send_cq->cqn < recv_cq->cqn) {
1535 spin_lock(&send_cq->lock);
1536 spin_lock_nested(&recv_cq->lock, SINGLE_DEPTH_NESTING);
1537 } else {
1538 spin_lock(&recv_cq->lock);
1539 spin_lock_nested(&send_cq->lock, SINGLE_DEPTH_NESTING);
1540 }
1541 }
1542
1543 void hns_roce_unlock_cqs(struct hns_roce_cq *send_cq,
1544 struct hns_roce_cq *recv_cq) __releases(&send_cq->lock)
1545 __releases(&recv_cq->lock)
1546 {
1547 if (unlikely(send_cq == NULL && recv_cq == NULL)) {
1548 __release(&recv_cq->lock);
1549 __release(&send_cq->lock);
1550 } else if (unlikely(send_cq != NULL && recv_cq == NULL)) {
1551 __release(&recv_cq->lock);
1552 spin_unlock(&send_cq->lock);
1553 } else if (unlikely(send_cq == NULL && recv_cq != NULL)) {
1554 __release(&send_cq->lock);
1555 spin_unlock(&recv_cq->lock);
1556 } else if (send_cq == recv_cq) {
1557 __release(&recv_cq->lock);
1558 spin_unlock(&send_cq->lock);
1559 } else if (send_cq->cqn < recv_cq->cqn) {
1560 spin_unlock(&recv_cq->lock);
1561 spin_unlock(&send_cq->lock);
1562 } else {
1563 spin_unlock(&send_cq->lock);
1564 spin_unlock(&recv_cq->lock);
1565 }
1566 }
1567
1568 static inline void *get_wqe(struct hns_roce_qp *hr_qp, u32 offset)
1569 {
1570 return hns_roce_buf_offset(hr_qp->mtr.kmem, offset);
1571 }
1572
1573 void *hns_roce_get_recv_wqe(struct hns_roce_qp *hr_qp, unsigned int n)
1574 {
1575 return get_wqe(hr_qp, hr_qp->rq.offset + (n << hr_qp->rq.wqe_shift));
1576 }
1577
1578 void *hns_roce_get_send_wqe(struct hns_roce_qp *hr_qp, unsigned int n)
1579 {
1580 return get_wqe(hr_qp, hr_qp->sq.offset + (n << hr_qp->sq.wqe_shift));
1581 }
1582
1583 void *hns_roce_get_extend_sge(struct hns_roce_qp *hr_qp, unsigned int n)
1584 {
1585 return get_wqe(hr_qp, hr_qp->sge.offset + (n << hr_qp->sge.sge_shift));
1586 }
1587
1588 bool hns_roce_wq_overflow(struct hns_roce_wq *hr_wq, u32 nreq,
1589 struct ib_cq *ib_cq)
1590 {
1591 struct hns_roce_cq *hr_cq;
1592 u32 cur;
1593
1594 cur = hr_wq->head - hr_wq->tail;
1595 if (likely(cur + nreq < hr_wq->wqe_cnt))
1596 return false;
1597
1598 hr_cq = to_hr_cq(ib_cq);
1599 spin_lock(&hr_cq->lock);
1600 cur = hr_wq->head - hr_wq->tail;
1601 spin_unlock(&hr_cq->lock);
1602
1603 return cur + nreq >= hr_wq->wqe_cnt;
1604 }
1605
1606 int hns_roce_init_qp_table(struct hns_roce_dev *hr_dev)
1607 {
1608 struct hns_roce_qp_table *qp_table = &hr_dev->qp_table;
1609 unsigned int reserved_from_bot;
1610 unsigned int i;
1611
1612 mutex_init(&qp_table->scc_mutex);
1613 mutex_init(&qp_table->bank_mutex);
1614 xa_init(&hr_dev->qp_table_xa);
1615 xa_init(&qp_table->dip_xa);
1616
1617 reserved_from_bot = hr_dev->caps.reserved_qps;
1618
1619 for (i = 0; i < reserved_from_bot; i++) {
1620 hr_dev->qp_table.bank[get_qp_bankid(i)].inuse++;
1621 hr_dev->qp_table.bank[get_qp_bankid(i)].min++;
1622 }
1623
1624 for (i = 0; i < HNS_ROCE_QP_BANK_NUM; i++) {
1625 ida_init(&hr_dev->qp_table.bank[i].ida);
1626 hr_dev->qp_table.bank[i].max = hr_dev->caps.num_qps /
1627 HNS_ROCE_QP_BANK_NUM - 1;
1628 hr_dev->qp_table.bank[i].next = hr_dev->qp_table.bank[i].min;
1629 }
1630
1631 return 0;
1632 }
1633
1634 void hns_roce_cleanup_qp_table(struct hns_roce_dev *hr_dev)
1635 {
1636 int i;
1637
1638 for (i = 0; i < HNS_ROCE_QP_BANK_NUM; i++)
1639 ida_destroy(&hr_dev->qp_table.bank[i].ida);
1640 xa_destroy(&hr_dev->qp_table.dip_xa);
1641 xa_destroy(&hr_dev->qp_table_xa);
1642 mutex_destroy(&hr_dev->qp_table.bank_mutex);
1643 mutex_destroy(&hr_dev->qp_table.scc_mutex);
1644 }