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 <linux/pci.h>
35 #include <linux/platform_device.h>
36 #include <rdma/ib_addr.h>
37 #include <rdma/ib_umem.h>
38 #include <rdma/uverbs_ioctl.h>
39 #include "hns_roce_common.h"
40 #include "hns_roce_device.h"
41 #include "hns_roce_hem.h"
42 #include <rdma/hns-abi.h>
43
flush_work_handle(struct work_struct * work)44 static void flush_work_handle(struct work_struct *work)
45 {
46 struct hns_roce_work *flush_work = container_of(work,
47 struct hns_roce_work, work);
48 struct hns_roce_qp *hr_qp = container_of(flush_work,
49 struct hns_roce_qp, flush_work);
50 struct device *dev = flush_work->hr_dev->dev;
51 struct ib_qp_attr attr;
52 int attr_mask;
53 int ret;
54
55 attr_mask = IB_QP_STATE;
56 attr.qp_state = IB_QPS_ERR;
57
58 if (test_and_clear_bit(HNS_ROCE_FLUSH_FLAG, &hr_qp->flush_flag)) {
59 ret = hns_roce_modify_qp(&hr_qp->ibqp, &attr, attr_mask, NULL);
60 if (ret)
61 dev_err(dev, "Modify QP to error state failed(%d) during CQE flush\n",
62 ret);
63 }
64
65 /*
66 * make sure we signal QP destroy leg that flush QP was completed
67 * so that it can safely proceed ahead now and destroy QP
68 */
69 if (atomic_dec_and_test(&hr_qp->refcount))
70 complete(&hr_qp->free);
71 }
72
init_flush_work(struct hns_roce_dev * hr_dev,struct hns_roce_qp * hr_qp)73 void init_flush_work(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp)
74 {
75 struct hns_roce_work *flush_work = &hr_qp->flush_work;
76
77 flush_work->hr_dev = hr_dev;
78 INIT_WORK(&flush_work->work, flush_work_handle);
79 atomic_inc(&hr_qp->refcount);
80 queue_work(hr_dev->irq_workq, &flush_work->work);
81 }
82
hns_roce_qp_event(struct hns_roce_dev * hr_dev,u32 qpn,int event_type)83 void hns_roce_qp_event(struct hns_roce_dev *hr_dev, u32 qpn, int event_type)
84 {
85 struct device *dev = hr_dev->dev;
86 struct hns_roce_qp *qp;
87
88 xa_lock(&hr_dev->qp_table_xa);
89 qp = __hns_roce_qp_lookup(hr_dev, qpn);
90 if (qp)
91 atomic_inc(&qp->refcount);
92 xa_unlock(&hr_dev->qp_table_xa);
93
94 if (!qp) {
95 dev_warn(dev, "Async event for bogus QP %08x\n", qpn);
96 return;
97 }
98
99 if (hr_dev->hw_rev != HNS_ROCE_HW_VER1 &&
100 (event_type == HNS_ROCE_EVENT_TYPE_WQ_CATAS_ERROR ||
101 event_type == HNS_ROCE_EVENT_TYPE_INV_REQ_LOCAL_WQ_ERROR ||
102 event_type == HNS_ROCE_EVENT_TYPE_LOCAL_WQ_ACCESS_ERROR)) {
103 qp->state = IB_QPS_ERR;
104 if (!test_and_set_bit(HNS_ROCE_FLUSH_FLAG, &qp->flush_flag))
105 init_flush_work(hr_dev, qp);
106 }
107
108 qp->event(qp, (enum hns_roce_event)event_type);
109
110 if (atomic_dec_and_test(&qp->refcount))
111 complete(&qp->free);
112 }
113
hns_roce_ib_qp_event(struct hns_roce_qp * hr_qp,enum hns_roce_event type)114 static void hns_roce_ib_qp_event(struct hns_roce_qp *hr_qp,
115 enum hns_roce_event type)
116 {
117 struct ib_event event;
118 struct ib_qp *ibqp = &hr_qp->ibqp;
119
120 if (ibqp->event_handler) {
121 event.device = ibqp->device;
122 event.element.qp = ibqp;
123 switch (type) {
124 case HNS_ROCE_EVENT_TYPE_PATH_MIG:
125 event.event = IB_EVENT_PATH_MIG;
126 break;
127 case HNS_ROCE_EVENT_TYPE_COMM_EST:
128 event.event = IB_EVENT_COMM_EST;
129 break;
130 case HNS_ROCE_EVENT_TYPE_SQ_DRAINED:
131 event.event = IB_EVENT_SQ_DRAINED;
132 break;
133 case HNS_ROCE_EVENT_TYPE_SRQ_LAST_WQE_REACH:
134 event.event = IB_EVENT_QP_LAST_WQE_REACHED;
135 break;
136 case HNS_ROCE_EVENT_TYPE_WQ_CATAS_ERROR:
137 event.event = IB_EVENT_QP_FATAL;
138 break;
139 case HNS_ROCE_EVENT_TYPE_PATH_MIG_FAILED:
140 event.event = IB_EVENT_PATH_MIG_ERR;
141 break;
142 case HNS_ROCE_EVENT_TYPE_INV_REQ_LOCAL_WQ_ERROR:
143 event.event = IB_EVENT_QP_REQ_ERR;
144 break;
145 case HNS_ROCE_EVENT_TYPE_LOCAL_WQ_ACCESS_ERROR:
146 event.event = IB_EVENT_QP_ACCESS_ERR;
147 break;
148 default:
149 dev_dbg(ibqp->device->dev.parent, "roce_ib: Unexpected event type %d on QP %06lx\n",
150 type, hr_qp->qpn);
151 return;
152 }
153 ibqp->event_handler(&event, ibqp->qp_context);
154 }
155 }
156
alloc_qpn(struct hns_roce_dev * hr_dev,struct hns_roce_qp * hr_qp)157 static int alloc_qpn(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp)
158 {
159 unsigned long num = 0;
160 int ret;
161
162 if (hr_qp->ibqp.qp_type == IB_QPT_GSI) {
163 /* when hw version is v1, the sqpn is allocated */
164 if (hr_dev->hw_rev == HNS_ROCE_HW_VER1)
165 num = HNS_ROCE_MAX_PORTS +
166 hr_dev->iboe.phy_port[hr_qp->port];
167 else
168 num = 1;
169
170 hr_qp->doorbell_qpn = 1;
171 } else {
172 ret = hns_roce_bitmap_alloc_range(&hr_dev->qp_table.bitmap,
173 1, 1, &num);
174 if (ret) {
175 ibdev_err(&hr_dev->ib_dev, "Failed to alloc bitmap\n");
176 return -ENOMEM;
177 }
178
179 hr_qp->doorbell_qpn = (u32)num;
180 }
181
182 hr_qp->qpn = num;
183
184 return 0;
185 }
186
to_hns_roce_state(enum ib_qp_state state)187 enum hns_roce_qp_state to_hns_roce_state(enum ib_qp_state state)
188 {
189 switch (state) {
190 case IB_QPS_RESET:
191 return HNS_ROCE_QP_STATE_RST;
192 case IB_QPS_INIT:
193 return HNS_ROCE_QP_STATE_INIT;
194 case IB_QPS_RTR:
195 return HNS_ROCE_QP_STATE_RTR;
196 case IB_QPS_RTS:
197 return HNS_ROCE_QP_STATE_RTS;
198 case IB_QPS_SQD:
199 return HNS_ROCE_QP_STATE_SQD;
200 case IB_QPS_ERR:
201 return HNS_ROCE_QP_STATE_ERR;
202 default:
203 return HNS_ROCE_QP_NUM_STATE;
204 }
205 }
206
add_qp_to_list(struct hns_roce_dev * hr_dev,struct hns_roce_qp * hr_qp,struct ib_cq * send_cq,struct ib_cq * recv_cq)207 static void add_qp_to_list(struct hns_roce_dev *hr_dev,
208 struct hns_roce_qp *hr_qp,
209 struct ib_cq *send_cq, struct ib_cq *recv_cq)
210 {
211 struct hns_roce_cq *hr_send_cq, *hr_recv_cq;
212 unsigned long flags;
213
214 hr_send_cq = send_cq ? to_hr_cq(send_cq) : NULL;
215 hr_recv_cq = recv_cq ? to_hr_cq(recv_cq) : NULL;
216
217 spin_lock_irqsave(&hr_dev->qp_list_lock, flags);
218 hns_roce_lock_cqs(hr_send_cq, hr_recv_cq);
219
220 list_add_tail(&hr_qp->node, &hr_dev->qp_list);
221 if (hr_send_cq)
222 list_add_tail(&hr_qp->sq_node, &hr_send_cq->sq_list);
223 if (hr_recv_cq)
224 list_add_tail(&hr_qp->rq_node, &hr_recv_cq->rq_list);
225
226 hns_roce_unlock_cqs(hr_send_cq, hr_recv_cq);
227 spin_unlock_irqrestore(&hr_dev->qp_list_lock, flags);
228 }
229
hns_roce_qp_store(struct hns_roce_dev * hr_dev,struct hns_roce_qp * hr_qp,struct ib_qp_init_attr * init_attr)230 static int hns_roce_qp_store(struct hns_roce_dev *hr_dev,
231 struct hns_roce_qp *hr_qp,
232 struct ib_qp_init_attr *init_attr)
233 {
234 struct xarray *xa = &hr_dev->qp_table_xa;
235 int ret;
236
237 if (!hr_qp->qpn)
238 return -EINVAL;
239
240 ret = xa_err(xa_store_irq(xa, hr_qp->qpn, hr_qp, GFP_KERNEL));
241 if (ret)
242 dev_err(hr_dev->dev, "Failed to xa store for QPC\n");
243 else
244 /* add QP to device's QP list for softwc */
245 add_qp_to_list(hr_dev, hr_qp, init_attr->send_cq,
246 init_attr->recv_cq);
247
248 return ret;
249 }
250
alloc_qpc(struct hns_roce_dev * hr_dev,struct hns_roce_qp * hr_qp)251 static int alloc_qpc(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp)
252 {
253 struct hns_roce_qp_table *qp_table = &hr_dev->qp_table;
254 struct device *dev = hr_dev->dev;
255 int ret;
256
257 if (!hr_qp->qpn)
258 return -EINVAL;
259
260 /* In v1 engine, GSI QP context is saved in the RoCE hw's register */
261 if (hr_qp->ibqp.qp_type == IB_QPT_GSI &&
262 hr_dev->hw_rev == HNS_ROCE_HW_VER1)
263 return 0;
264
265 /* Alloc memory for QPC */
266 ret = hns_roce_table_get(hr_dev, &qp_table->qp_table, hr_qp->qpn);
267 if (ret) {
268 dev_err(dev, "Failed to get QPC table\n");
269 goto err_out;
270 }
271
272 /* Alloc memory for IRRL */
273 ret = hns_roce_table_get(hr_dev, &qp_table->irrl_table, hr_qp->qpn);
274 if (ret) {
275 dev_err(dev, "Failed to get IRRL table\n");
276 goto err_put_qp;
277 }
278
279 if (hr_dev->caps.trrl_entry_sz) {
280 /* Alloc memory for TRRL */
281 ret = hns_roce_table_get(hr_dev, &qp_table->trrl_table,
282 hr_qp->qpn);
283 if (ret) {
284 dev_err(dev, "Failed to get TRRL table\n");
285 goto err_put_irrl;
286 }
287 }
288
289 if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_QP_FLOW_CTRL) {
290 /* Alloc memory for SCC CTX */
291 ret = hns_roce_table_get(hr_dev, &qp_table->sccc_table,
292 hr_qp->qpn);
293 if (ret) {
294 dev_err(dev, "Failed to get SCC CTX table\n");
295 goto err_put_trrl;
296 }
297 }
298
299 return 0;
300
301 err_put_trrl:
302 if (hr_dev->caps.trrl_entry_sz)
303 hns_roce_table_put(hr_dev, &qp_table->trrl_table, hr_qp->qpn);
304
305 err_put_irrl:
306 hns_roce_table_put(hr_dev, &qp_table->irrl_table, hr_qp->qpn);
307
308 err_put_qp:
309 hns_roce_table_put(hr_dev, &qp_table->qp_table, hr_qp->qpn);
310
311 err_out:
312 return ret;
313 }
314
hns_roce_qp_remove(struct hns_roce_dev * hr_dev,struct hns_roce_qp * hr_qp)315 void hns_roce_qp_remove(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp)
316 {
317 struct xarray *xa = &hr_dev->qp_table_xa;
318 unsigned long flags;
319
320 list_del(&hr_qp->node);
321 list_del(&hr_qp->sq_node);
322 list_del(&hr_qp->rq_node);
323
324 xa_lock_irqsave(xa, flags);
325 __xa_erase(xa, hr_qp->qpn & (hr_dev->caps.num_qps - 1));
326 xa_unlock_irqrestore(xa, flags);
327 }
328
free_qpc(struct hns_roce_dev * hr_dev,struct hns_roce_qp * hr_qp)329 static void free_qpc(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp)
330 {
331 struct hns_roce_qp_table *qp_table = &hr_dev->qp_table;
332
333 /* In v1 engine, GSI QP context is saved in the RoCE hw's register */
334 if (hr_qp->ibqp.qp_type == IB_QPT_GSI &&
335 hr_dev->hw_rev == HNS_ROCE_HW_VER1)
336 return;
337
338 if (hr_dev->caps.trrl_entry_sz)
339 hns_roce_table_put(hr_dev, &qp_table->trrl_table, hr_qp->qpn);
340 hns_roce_table_put(hr_dev, &qp_table->irrl_table, hr_qp->qpn);
341 }
342
free_qpn(struct hns_roce_dev * hr_dev,struct hns_roce_qp * hr_qp)343 static void free_qpn(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp)
344 {
345 struct hns_roce_qp_table *qp_table = &hr_dev->qp_table;
346
347 if (hr_qp->ibqp.qp_type == IB_QPT_GSI)
348 return;
349
350 if (hr_qp->qpn < hr_dev->caps.reserved_qps)
351 return;
352
353 hns_roce_bitmap_free_range(&qp_table->bitmap, hr_qp->qpn, 1, BITMAP_RR);
354 }
355
set_rq_size(struct hns_roce_dev * hr_dev,struct ib_qp_cap * cap,struct hns_roce_qp * hr_qp,int has_rq)356 static int set_rq_size(struct hns_roce_dev *hr_dev, struct ib_qp_cap *cap,
357 struct hns_roce_qp *hr_qp, int has_rq)
358 {
359 u32 cnt;
360
361 /* If srq exist, set zero for relative number of rq */
362 if (!has_rq) {
363 hr_qp->rq.wqe_cnt = 0;
364 hr_qp->rq.max_gs = 0;
365 hr_qp->rq_inl_buf.wqe_cnt = 0;
366 cap->max_recv_wr = 0;
367 cap->max_recv_sge = 0;
368
369 return 0;
370 }
371
372 /* Check the validity of QP support capacity */
373 if (!cap->max_recv_wr || cap->max_recv_wr > hr_dev->caps.max_wqes ||
374 cap->max_recv_sge > hr_dev->caps.max_rq_sg) {
375 ibdev_err(&hr_dev->ib_dev, "RQ config error, depth=%u, sge=%d\n",
376 cap->max_recv_wr, cap->max_recv_sge);
377 return -EINVAL;
378 }
379
380 cnt = roundup_pow_of_two(max(cap->max_recv_wr, hr_dev->caps.min_wqes));
381 if (cnt > hr_dev->caps.max_wqes) {
382 ibdev_err(&hr_dev->ib_dev, "rq depth %u too large\n",
383 cap->max_recv_wr);
384 return -EINVAL;
385 }
386
387 hr_qp->rq.max_gs = roundup_pow_of_two(max(1U, cap->max_recv_sge));
388
389 if (hr_dev->caps.max_rq_sg <= HNS_ROCE_SGE_IN_WQE)
390 hr_qp->rq.wqe_shift = ilog2(hr_dev->caps.max_rq_desc_sz);
391 else
392 hr_qp->rq.wqe_shift = ilog2(hr_dev->caps.max_rq_desc_sz *
393 hr_qp->rq.max_gs);
394
395 hr_qp->rq.wqe_cnt = cnt;
396 if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_RQ_INLINE)
397 hr_qp->rq_inl_buf.wqe_cnt = cnt;
398 else
399 hr_qp->rq_inl_buf.wqe_cnt = 0;
400
401 cap->max_recv_wr = cnt;
402 cap->max_recv_sge = hr_qp->rq.max_gs;
403
404 return 0;
405 }
406
set_extend_sge_param(struct hns_roce_dev * hr_dev,u32 sq_wqe_cnt,struct hns_roce_qp * hr_qp,struct ib_qp_cap * cap)407 static int set_extend_sge_param(struct hns_roce_dev *hr_dev, u32 sq_wqe_cnt,
408 struct hns_roce_qp *hr_qp,
409 struct ib_qp_cap *cap)
410 {
411 u32 cnt;
412
413 cnt = max(1U, cap->max_send_sge);
414 if (hr_dev->hw_rev == HNS_ROCE_HW_VER1) {
415 hr_qp->sq.max_gs = roundup_pow_of_two(cnt);
416 hr_qp->sge.sge_cnt = 0;
417
418 return 0;
419 }
420
421 hr_qp->sq.max_gs = cnt;
422
423 /* UD sqwqe's sge use extend sge */
424 if (hr_qp->ibqp.qp_type == IB_QPT_GSI ||
425 hr_qp->ibqp.qp_type == IB_QPT_UD) {
426 cnt = roundup_pow_of_two(sq_wqe_cnt * hr_qp->sq.max_gs);
427 } else if (hr_qp->sq.max_gs > HNS_ROCE_SGE_IN_WQE) {
428 cnt = roundup_pow_of_two(sq_wqe_cnt *
429 (hr_qp->sq.max_gs - HNS_ROCE_SGE_IN_WQE));
430 } else {
431 cnt = 0;
432 }
433
434 hr_qp->sge.sge_shift = HNS_ROCE_SGE_SHIFT;
435
436 /* If the number of extended sge is not zero, they MUST use the
437 * space of HNS_HW_PAGE_SIZE at least.
438 */
439 hr_qp->sge.sge_cnt = cnt ?
440 max(cnt, (u32)HNS_HW_PAGE_SIZE / HNS_ROCE_SGE_SIZE) : 0;
441
442 return 0;
443 }
444
check_sq_size_with_integrity(struct hns_roce_dev * hr_dev,struct ib_qp_cap * cap,struct hns_roce_ib_create_qp * ucmd)445 static int check_sq_size_with_integrity(struct hns_roce_dev *hr_dev,
446 struct ib_qp_cap *cap,
447 struct hns_roce_ib_create_qp *ucmd)
448 {
449 u32 roundup_sq_stride = roundup_pow_of_two(hr_dev->caps.max_sq_desc_sz);
450 u8 max_sq_stride = ilog2(roundup_sq_stride);
451
452 /* Sanity check SQ size before proceeding */
453 if (ucmd->log_sq_stride > max_sq_stride ||
454 ucmd->log_sq_stride < HNS_ROCE_IB_MIN_SQ_STRIDE) {
455 ibdev_err(&hr_dev->ib_dev, "failed to check SQ stride size.\n");
456 return -EINVAL;
457 }
458
459 if (cap->max_send_sge > hr_dev->caps.max_sq_sg) {
460 ibdev_err(&hr_dev->ib_dev, "failed to check SQ SGE size %u.\n",
461 cap->max_send_sge);
462 return -EINVAL;
463 }
464
465 return 0;
466 }
467
set_user_sq_size(struct hns_roce_dev * hr_dev,struct ib_qp_cap * cap,struct hns_roce_qp * hr_qp,struct hns_roce_ib_create_qp * ucmd)468 static int set_user_sq_size(struct hns_roce_dev *hr_dev,
469 struct ib_qp_cap *cap, struct hns_roce_qp *hr_qp,
470 struct hns_roce_ib_create_qp *ucmd)
471 {
472 struct ib_device *ibdev = &hr_dev->ib_dev;
473 u32 cnt = 0;
474 int ret;
475
476 if (check_shl_overflow(1, ucmd->log_sq_bb_count, &cnt) ||
477 cnt > hr_dev->caps.max_wqes)
478 return -EINVAL;
479
480 ret = check_sq_size_with_integrity(hr_dev, cap, ucmd);
481 if (ret) {
482 ibdev_err(ibdev, "failed to check user SQ size, ret = %d.\n",
483 ret);
484 return ret;
485 }
486
487 ret = set_extend_sge_param(hr_dev, cnt, hr_qp, cap);
488 if (ret)
489 return ret;
490
491 hr_qp->sq.wqe_shift = ucmd->log_sq_stride;
492 hr_qp->sq.wqe_cnt = cnt;
493
494 return 0;
495 }
496
set_wqe_buf_attr(struct hns_roce_dev * hr_dev,struct hns_roce_qp * hr_qp,struct hns_roce_buf_attr * buf_attr)497 static int set_wqe_buf_attr(struct hns_roce_dev *hr_dev,
498 struct hns_roce_qp *hr_qp,
499 struct hns_roce_buf_attr *buf_attr)
500 {
501 int buf_size;
502 int idx = 0;
503
504 hr_qp->buff_size = 0;
505
506 /* SQ WQE */
507 hr_qp->sq.offset = 0;
508 buf_size = to_hr_hem_entries_size(hr_qp->sq.wqe_cnt,
509 hr_qp->sq.wqe_shift);
510 if (buf_size > 0 && idx < ARRAY_SIZE(buf_attr->region)) {
511 buf_attr->region[idx].size = buf_size;
512 buf_attr->region[idx].hopnum = hr_dev->caps.wqe_sq_hop_num;
513 idx++;
514 hr_qp->buff_size += buf_size;
515 }
516
517 /* extend SGE WQE in SQ */
518 hr_qp->sge.offset = hr_qp->buff_size;
519 buf_size = to_hr_hem_entries_size(hr_qp->sge.sge_cnt,
520 hr_qp->sge.sge_shift);
521 if (buf_size > 0 && idx < ARRAY_SIZE(buf_attr->region)) {
522 buf_attr->region[idx].size = buf_size;
523 buf_attr->region[idx].hopnum = hr_dev->caps.wqe_sge_hop_num;
524 idx++;
525 hr_qp->buff_size += buf_size;
526 }
527
528 /* RQ WQE */
529 hr_qp->rq.offset = hr_qp->buff_size;
530 buf_size = to_hr_hem_entries_size(hr_qp->rq.wqe_cnt,
531 hr_qp->rq.wqe_shift);
532 if (buf_size > 0 && idx < ARRAY_SIZE(buf_attr->region)) {
533 buf_attr->region[idx].size = buf_size;
534 buf_attr->region[idx].hopnum = hr_dev->caps.wqe_rq_hop_num;
535 idx++;
536 hr_qp->buff_size += buf_size;
537 }
538
539 if (hr_qp->buff_size < 1)
540 return -EINVAL;
541
542 buf_attr->page_shift = HNS_HW_PAGE_SHIFT + hr_dev->caps.mtt_buf_pg_sz;
543 buf_attr->fixed_page = true;
544 buf_attr->region_count = idx;
545
546 return 0;
547 }
548
set_kernel_sq_size(struct hns_roce_dev * hr_dev,struct ib_qp_cap * cap,struct hns_roce_qp * hr_qp)549 static int set_kernel_sq_size(struct hns_roce_dev *hr_dev,
550 struct ib_qp_cap *cap, struct hns_roce_qp *hr_qp)
551 {
552 struct ib_device *ibdev = &hr_dev->ib_dev;
553 u32 cnt;
554 int ret;
555
556 if (!cap->max_send_wr || cap->max_send_wr > hr_dev->caps.max_wqes ||
557 cap->max_send_sge > hr_dev->caps.max_sq_sg) {
558 ibdev_err(ibdev,
559 "failed to check SQ WR or SGE num, ret = %d.\n",
560 -EINVAL);
561 return -EINVAL;
562 }
563
564 cnt = roundup_pow_of_two(max(cap->max_send_wr, hr_dev->caps.min_wqes));
565 if (cnt > hr_dev->caps.max_wqes) {
566 ibdev_err(ibdev, "failed to check WQE num, WQE num = %u.\n",
567 cnt);
568 return -EINVAL;
569 }
570
571 hr_qp->sq.wqe_shift = ilog2(hr_dev->caps.max_sq_desc_sz);
572 hr_qp->sq.wqe_cnt = cnt;
573
574 ret = set_extend_sge_param(hr_dev, cnt, hr_qp, cap);
575 if (ret)
576 return ret;
577
578 /* sync the parameters of kernel QP to user's configuration */
579 cap->max_send_wr = cnt;
580 cap->max_send_sge = hr_qp->sq.max_gs;
581
582 return 0;
583 }
584
hns_roce_qp_has_sq(struct ib_qp_init_attr * attr)585 static int hns_roce_qp_has_sq(struct ib_qp_init_attr *attr)
586 {
587 if (attr->qp_type == IB_QPT_XRC_TGT || !attr->cap.max_send_wr)
588 return 0;
589
590 return 1;
591 }
592
hns_roce_qp_has_rq(struct ib_qp_init_attr * attr)593 static int hns_roce_qp_has_rq(struct ib_qp_init_attr *attr)
594 {
595 if (attr->qp_type == IB_QPT_XRC_INI ||
596 attr->qp_type == IB_QPT_XRC_TGT || attr->srq ||
597 !attr->cap.max_recv_wr)
598 return 0;
599
600 return 1;
601 }
602
alloc_rq_inline_buf(struct hns_roce_qp * hr_qp,struct ib_qp_init_attr * init_attr)603 static int alloc_rq_inline_buf(struct hns_roce_qp *hr_qp,
604 struct ib_qp_init_attr *init_attr)
605 {
606 u32 max_recv_sge = init_attr->cap.max_recv_sge;
607 u32 wqe_cnt = hr_qp->rq_inl_buf.wqe_cnt;
608 struct hns_roce_rinl_wqe *wqe_list;
609 int i;
610
611 /* allocate recv inline buf */
612 wqe_list = kcalloc(wqe_cnt, sizeof(struct hns_roce_rinl_wqe),
613 GFP_KERNEL);
614
615 if (!wqe_list)
616 goto err;
617
618 /* Allocate a continuous buffer for all inline sge we need */
619 wqe_list[0].sg_list = kcalloc(wqe_cnt, (max_recv_sge *
620 sizeof(struct hns_roce_rinl_sge)),
621 GFP_KERNEL);
622 if (!wqe_list[0].sg_list)
623 goto err_wqe_list;
624
625 /* Assign buffers of sg_list to each inline wqe */
626 for (i = 1; i < wqe_cnt; i++)
627 wqe_list[i].sg_list = &wqe_list[0].sg_list[i * max_recv_sge];
628
629 hr_qp->rq_inl_buf.wqe_list = wqe_list;
630
631 return 0;
632
633 err_wqe_list:
634 kfree(wqe_list);
635
636 err:
637 return -ENOMEM;
638 }
639
free_rq_inline_buf(struct hns_roce_qp * hr_qp)640 static void free_rq_inline_buf(struct hns_roce_qp *hr_qp)
641 {
642 if (hr_qp->rq_inl_buf.wqe_list)
643 kfree(hr_qp->rq_inl_buf.wqe_list[0].sg_list);
644 kfree(hr_qp->rq_inl_buf.wqe_list);
645 }
646
alloc_qp_buf(struct hns_roce_dev * hr_dev,struct hns_roce_qp * hr_qp,struct ib_qp_init_attr * init_attr,struct ib_udata * udata,unsigned long addr)647 static int alloc_qp_buf(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp,
648 struct ib_qp_init_attr *init_attr,
649 struct ib_udata *udata, unsigned long addr)
650 {
651 struct ib_device *ibdev = &hr_dev->ib_dev;
652 struct hns_roce_buf_attr buf_attr = {};
653 int ret;
654
655 if (!udata && hr_qp->rq_inl_buf.wqe_cnt) {
656 ret = alloc_rq_inline_buf(hr_qp, init_attr);
657 if (ret) {
658 ibdev_err(ibdev,
659 "failed to alloc inline buf, ret = %d.\n",
660 ret);
661 return ret;
662 }
663 } else {
664 hr_qp->rq_inl_buf.wqe_list = NULL;
665 }
666
667 ret = set_wqe_buf_attr(hr_dev, hr_qp, &buf_attr);
668 if (ret) {
669 ibdev_err(ibdev, "failed to split WQE buf, ret = %d.\n", ret);
670 goto err_inline;
671 }
672 ret = hns_roce_mtr_create(hr_dev, &hr_qp->mtr, &buf_attr,
673 HNS_HW_PAGE_SHIFT + hr_dev->caps.mtt_ba_pg_sz,
674 udata, addr);
675 if (ret) {
676 ibdev_err(ibdev, "failed to create WQE mtr, ret = %d.\n", ret);
677 goto err_inline;
678 }
679
680 return 0;
681 err_inline:
682 free_rq_inline_buf(hr_qp);
683
684 return ret;
685 }
686
free_qp_buf(struct hns_roce_dev * hr_dev,struct hns_roce_qp * hr_qp)687 static void free_qp_buf(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp)
688 {
689 hns_roce_mtr_destroy(hr_dev, &hr_qp->mtr);
690 free_rq_inline_buf(hr_qp);
691 }
692
user_qp_has_sdb(struct hns_roce_dev * hr_dev,struct ib_qp_init_attr * init_attr,struct ib_udata * udata,struct hns_roce_ib_create_qp_resp * resp,struct hns_roce_ib_create_qp * ucmd)693 static inline bool user_qp_has_sdb(struct hns_roce_dev *hr_dev,
694 struct ib_qp_init_attr *init_attr,
695 struct ib_udata *udata,
696 struct hns_roce_ib_create_qp_resp *resp,
697 struct hns_roce_ib_create_qp *ucmd)
698 {
699 return ((hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_SQ_RECORD_DB) &&
700 udata->outlen >= offsetofend(typeof(*resp), cap_flags) &&
701 hns_roce_qp_has_sq(init_attr) &&
702 udata->inlen >= offsetofend(typeof(*ucmd), sdb_addr));
703 }
704
user_qp_has_rdb(struct hns_roce_dev * hr_dev,struct ib_qp_init_attr * init_attr,struct ib_udata * udata,struct hns_roce_ib_create_qp_resp * resp)705 static inline bool user_qp_has_rdb(struct hns_roce_dev *hr_dev,
706 struct ib_qp_init_attr *init_attr,
707 struct ib_udata *udata,
708 struct hns_roce_ib_create_qp_resp *resp)
709 {
710 return ((hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_RECORD_DB) &&
711 udata->outlen >= offsetofend(typeof(*resp), cap_flags) &&
712 hns_roce_qp_has_rq(init_attr));
713 }
714
kernel_qp_has_rdb(struct hns_roce_dev * hr_dev,struct ib_qp_init_attr * init_attr)715 static inline bool kernel_qp_has_rdb(struct hns_roce_dev *hr_dev,
716 struct ib_qp_init_attr *init_attr)
717 {
718 return ((hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_RECORD_DB) &&
719 hns_roce_qp_has_rq(init_attr));
720 }
721
alloc_qp_db(struct hns_roce_dev * hr_dev,struct hns_roce_qp * hr_qp,struct ib_qp_init_attr * init_attr,struct ib_udata * udata,struct hns_roce_ib_create_qp * ucmd,struct hns_roce_ib_create_qp_resp * resp)722 static int alloc_qp_db(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp,
723 struct ib_qp_init_attr *init_attr,
724 struct ib_udata *udata,
725 struct hns_roce_ib_create_qp *ucmd,
726 struct hns_roce_ib_create_qp_resp *resp)
727 {
728 struct hns_roce_ucontext *uctx = rdma_udata_to_drv_context(
729 udata, struct hns_roce_ucontext, ibucontext);
730 struct ib_device *ibdev = &hr_dev->ib_dev;
731 int ret;
732
733 if (udata) {
734 if (user_qp_has_sdb(hr_dev, init_attr, udata, resp, ucmd)) {
735 ret = hns_roce_db_map_user(uctx, udata, ucmd->sdb_addr,
736 &hr_qp->sdb);
737 if (ret) {
738 ibdev_err(ibdev,
739 "failed to map user SQ doorbell, ret = %d.\n",
740 ret);
741 goto err_out;
742 }
743 hr_qp->en_flags |= HNS_ROCE_QP_CAP_SQ_RECORD_DB;
744 }
745
746 if (user_qp_has_rdb(hr_dev, init_attr, udata, resp)) {
747 ret = hns_roce_db_map_user(uctx, udata, ucmd->db_addr,
748 &hr_qp->rdb);
749 if (ret) {
750 ibdev_err(ibdev,
751 "failed to map user RQ doorbell, ret = %d.\n",
752 ret);
753 goto err_sdb;
754 }
755 hr_qp->en_flags |= HNS_ROCE_QP_CAP_RQ_RECORD_DB;
756 }
757 } else {
758 /* QP doorbell register address */
759 hr_qp->sq.db_reg_l = hr_dev->reg_base + hr_dev->sdb_offset +
760 DB_REG_OFFSET * hr_dev->priv_uar.index;
761 hr_qp->rq.db_reg_l = hr_dev->reg_base + hr_dev->odb_offset +
762 DB_REG_OFFSET * hr_dev->priv_uar.index;
763
764 if (kernel_qp_has_rdb(hr_dev, init_attr)) {
765 ret = hns_roce_alloc_db(hr_dev, &hr_qp->rdb, 0);
766 if (ret) {
767 ibdev_err(ibdev,
768 "failed to alloc kernel RQ doorbell, ret = %d.\n",
769 ret);
770 goto err_out;
771 }
772 *hr_qp->rdb.db_record = 0;
773 hr_qp->en_flags |= HNS_ROCE_QP_CAP_RQ_RECORD_DB;
774 }
775 }
776
777 return 0;
778 err_sdb:
779 if (udata && hr_qp->en_flags & HNS_ROCE_QP_CAP_SQ_RECORD_DB)
780 hns_roce_db_unmap_user(uctx, &hr_qp->sdb);
781 err_out:
782 return ret;
783 }
784
free_qp_db(struct hns_roce_dev * hr_dev,struct hns_roce_qp * hr_qp,struct ib_udata * udata)785 static void free_qp_db(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp,
786 struct ib_udata *udata)
787 {
788 struct hns_roce_ucontext *uctx = rdma_udata_to_drv_context(
789 udata, struct hns_roce_ucontext, ibucontext);
790
791 if (udata) {
792 if (hr_qp->en_flags & HNS_ROCE_QP_CAP_RQ_RECORD_DB)
793 hns_roce_db_unmap_user(uctx, &hr_qp->rdb);
794 if (hr_qp->en_flags & HNS_ROCE_QP_CAP_SQ_RECORD_DB)
795 hns_roce_db_unmap_user(uctx, &hr_qp->sdb);
796 } else {
797 if (hr_qp->en_flags & HNS_ROCE_QP_CAP_RQ_RECORD_DB)
798 hns_roce_free_db(hr_dev, &hr_qp->rdb);
799 }
800 }
801
alloc_kernel_wrid(struct hns_roce_dev * hr_dev,struct hns_roce_qp * hr_qp)802 static int alloc_kernel_wrid(struct hns_roce_dev *hr_dev,
803 struct hns_roce_qp *hr_qp)
804 {
805 struct ib_device *ibdev = &hr_dev->ib_dev;
806 u64 *sq_wrid = NULL;
807 u64 *rq_wrid = NULL;
808 int ret;
809
810 sq_wrid = kcalloc(hr_qp->sq.wqe_cnt, sizeof(u64), GFP_KERNEL);
811 if (ZERO_OR_NULL_PTR(sq_wrid)) {
812 ibdev_err(ibdev, "failed to alloc SQ wrid.\n");
813 return -ENOMEM;
814 }
815
816 if (hr_qp->rq.wqe_cnt) {
817 rq_wrid = kcalloc(hr_qp->rq.wqe_cnt, sizeof(u64), GFP_KERNEL);
818 if (ZERO_OR_NULL_PTR(rq_wrid)) {
819 ibdev_err(ibdev, "failed to alloc RQ wrid.\n");
820 ret = -ENOMEM;
821 goto err_sq;
822 }
823 }
824
825 hr_qp->sq.wrid = sq_wrid;
826 hr_qp->rq.wrid = rq_wrid;
827 return 0;
828 err_sq:
829 kfree(sq_wrid);
830
831 return ret;
832 }
833
free_kernel_wrid(struct hns_roce_qp * hr_qp)834 static void free_kernel_wrid(struct hns_roce_qp *hr_qp)
835 {
836 kfree(hr_qp->rq.wrid);
837 kfree(hr_qp->sq.wrid);
838 }
839
set_qp_param(struct hns_roce_dev * hr_dev,struct hns_roce_qp * hr_qp,struct ib_qp_init_attr * init_attr,struct ib_udata * udata,struct hns_roce_ib_create_qp * ucmd)840 static int set_qp_param(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp,
841 struct ib_qp_init_attr *init_attr,
842 struct ib_udata *udata,
843 struct hns_roce_ib_create_qp *ucmd)
844 {
845 struct ib_device *ibdev = &hr_dev->ib_dev;
846 int ret;
847
848 hr_qp->ibqp.qp_type = init_attr->qp_type;
849
850 if (init_attr->cap.max_inline_data > hr_dev->caps.max_sq_inline)
851 init_attr->cap.max_inline_data = hr_dev->caps.max_sq_inline;
852
853 hr_qp->max_inline_data = init_attr->cap.max_inline_data;
854
855 if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR)
856 hr_qp->sq_signal_bits = IB_SIGNAL_ALL_WR;
857 else
858 hr_qp->sq_signal_bits = IB_SIGNAL_REQ_WR;
859
860 ret = set_rq_size(hr_dev, &init_attr->cap, hr_qp,
861 hns_roce_qp_has_rq(init_attr));
862 if (ret) {
863 ibdev_err(ibdev, "failed to set user RQ size, ret = %d.\n",
864 ret);
865 return ret;
866 }
867
868 if (udata) {
869 ret = ib_copy_from_udata(ucmd, udata,
870 min(udata->inlen, sizeof(*ucmd)));
871 if (ret) {
872 ibdev_err(ibdev,
873 "failed to copy QP ucmd, ret = %d\n", ret);
874 return ret;
875 }
876
877 ret = set_user_sq_size(hr_dev, &init_attr->cap, hr_qp, ucmd);
878 if (ret)
879 ibdev_err(ibdev,
880 "failed to set user SQ size, ret = %d.\n",
881 ret);
882 } else {
883 if (init_attr->create_flags &
884 IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK) {
885 ibdev_err(ibdev, "Failed to check multicast loopback\n");
886 return -EINVAL;
887 }
888
889 if (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO) {
890 ibdev_err(ibdev, "Failed to check ipoib ud lso\n");
891 return -EINVAL;
892 }
893
894 ret = set_kernel_sq_size(hr_dev, &init_attr->cap, hr_qp);
895 if (ret)
896 ibdev_err(ibdev,
897 "failed to set kernel SQ size, ret = %d.\n",
898 ret);
899 }
900
901 return ret;
902 }
903
hns_roce_create_qp_common(struct hns_roce_dev * hr_dev,struct ib_pd * ib_pd,struct ib_qp_init_attr * init_attr,struct ib_udata * udata,struct hns_roce_qp * hr_qp)904 static int hns_roce_create_qp_common(struct hns_roce_dev *hr_dev,
905 struct ib_pd *ib_pd,
906 struct ib_qp_init_attr *init_attr,
907 struct ib_udata *udata,
908 struct hns_roce_qp *hr_qp)
909 {
910 struct hns_roce_ib_create_qp_resp resp = {};
911 struct ib_device *ibdev = &hr_dev->ib_dev;
912 struct hns_roce_ib_create_qp ucmd;
913 int ret;
914
915 mutex_init(&hr_qp->mutex);
916 spin_lock_init(&hr_qp->sq.lock);
917 spin_lock_init(&hr_qp->rq.lock);
918
919 hr_qp->state = IB_QPS_RESET;
920 hr_qp->flush_flag = 0;
921
922 ret = set_qp_param(hr_dev, hr_qp, init_attr, udata, &ucmd);
923 if (ret) {
924 ibdev_err(ibdev, "failed to set QP param, ret = %d.\n", ret);
925 return ret;
926 }
927
928 if (!udata) {
929 ret = alloc_kernel_wrid(hr_dev, hr_qp);
930 if (ret) {
931 ibdev_err(ibdev, "failed to alloc wrid, ret = %d.\n",
932 ret);
933 return ret;
934 }
935 }
936
937 ret = alloc_qp_db(hr_dev, hr_qp, init_attr, udata, &ucmd, &resp);
938 if (ret) {
939 ibdev_err(ibdev, "failed to alloc QP doorbell, ret = %d.\n",
940 ret);
941 goto err_wrid;
942 }
943
944 ret = alloc_qp_buf(hr_dev, hr_qp, init_attr, udata, ucmd.buf_addr);
945 if (ret) {
946 ibdev_err(ibdev, "failed to alloc QP buffer, ret = %d.\n", ret);
947 goto err_db;
948 }
949
950 ret = alloc_qpn(hr_dev, hr_qp);
951 if (ret) {
952 ibdev_err(ibdev, "failed to alloc QPN, ret = %d.\n", ret);
953 goto err_buf;
954 }
955
956 ret = alloc_qpc(hr_dev, hr_qp);
957 if (ret) {
958 ibdev_err(ibdev, "failed to alloc QP context, ret = %d.\n",
959 ret);
960 goto err_qpn;
961 }
962
963 ret = hns_roce_qp_store(hr_dev, hr_qp, init_attr);
964 if (ret) {
965 ibdev_err(ibdev, "failed to store QP, ret = %d.\n", ret);
966 goto err_qpc;
967 }
968
969 if (udata) {
970 resp.cap_flags = hr_qp->en_flags;
971 ret = ib_copy_to_udata(udata, &resp,
972 min(udata->outlen, sizeof(resp)));
973 if (ret) {
974 ibdev_err(ibdev, "copy qp resp failed!\n");
975 goto err_store;
976 }
977 }
978
979 if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_QP_FLOW_CTRL) {
980 ret = hr_dev->hw->qp_flow_control_init(hr_dev, hr_qp);
981 if (ret)
982 goto err_store;
983 }
984
985 hr_qp->ibqp.qp_num = hr_qp->qpn;
986 hr_qp->event = hns_roce_ib_qp_event;
987 atomic_set(&hr_qp->refcount, 1);
988 init_completion(&hr_qp->free);
989
990 return 0;
991
992 err_store:
993 hns_roce_qp_remove(hr_dev, hr_qp);
994 err_qpc:
995 free_qpc(hr_dev, hr_qp);
996 err_qpn:
997 free_qpn(hr_dev, hr_qp);
998 err_buf:
999 free_qp_buf(hr_dev, hr_qp);
1000 err_db:
1001 free_qp_db(hr_dev, hr_qp, udata);
1002 err_wrid:
1003 free_kernel_wrid(hr_qp);
1004 return ret;
1005 }
1006
hns_roce_qp_destroy(struct hns_roce_dev * hr_dev,struct hns_roce_qp * hr_qp,struct ib_udata * udata)1007 void hns_roce_qp_destroy(struct hns_roce_dev *hr_dev, struct hns_roce_qp *hr_qp,
1008 struct ib_udata *udata)
1009 {
1010 if (atomic_dec_and_test(&hr_qp->refcount))
1011 complete(&hr_qp->free);
1012 wait_for_completion(&hr_qp->free);
1013
1014 free_qpc(hr_dev, hr_qp);
1015 free_qpn(hr_dev, hr_qp);
1016 free_qp_buf(hr_dev, hr_qp);
1017 free_kernel_wrid(hr_qp);
1018 free_qp_db(hr_dev, hr_qp, udata);
1019
1020 kfree(hr_qp);
1021 }
1022
hns_roce_create_qp(struct ib_pd * pd,struct ib_qp_init_attr * init_attr,struct ib_udata * udata)1023 struct ib_qp *hns_roce_create_qp(struct ib_pd *pd,
1024 struct ib_qp_init_attr *init_attr,
1025 struct ib_udata *udata)
1026 {
1027 struct hns_roce_dev *hr_dev = to_hr_dev(pd->device);
1028 struct ib_device *ibdev = &hr_dev->ib_dev;
1029 struct hns_roce_qp *hr_qp;
1030 int ret;
1031
1032 switch (init_attr->qp_type) {
1033 case IB_QPT_RC:
1034 case IB_QPT_GSI:
1035 break;
1036 default:
1037 ibdev_err(ibdev, "not support QP type %d\n",
1038 init_attr->qp_type);
1039 return ERR_PTR(-EOPNOTSUPP);
1040 }
1041
1042 hr_qp = kzalloc(sizeof(*hr_qp), GFP_KERNEL);
1043 if (!hr_qp)
1044 return ERR_PTR(-ENOMEM);
1045
1046 if (init_attr->qp_type == IB_QPT_GSI) {
1047 hr_qp->port = init_attr->port_num - 1;
1048 hr_qp->phy_port = hr_dev->iboe.phy_port[hr_qp->port];
1049 }
1050
1051 ret = hns_roce_create_qp_common(hr_dev, pd, init_attr, udata, hr_qp);
1052 if (ret) {
1053 ibdev_err(ibdev, "Create QP type 0x%x failed(%d)\n",
1054 init_attr->qp_type, ret);
1055 ibdev_err(ibdev, "Create GSI QP failed!\n");
1056 kfree(hr_qp);
1057 return ERR_PTR(ret);
1058 }
1059 return &hr_qp->ibqp;
1060 }
1061
to_hr_qp_type(int qp_type)1062 int to_hr_qp_type(int qp_type)
1063 {
1064 int transport_type;
1065
1066 if (qp_type == IB_QPT_RC)
1067 transport_type = SERV_TYPE_RC;
1068 else if (qp_type == IB_QPT_UC)
1069 transport_type = SERV_TYPE_UC;
1070 else if (qp_type == IB_QPT_UD)
1071 transport_type = SERV_TYPE_UD;
1072 else if (qp_type == IB_QPT_GSI)
1073 transport_type = SERV_TYPE_UD;
1074 else
1075 transport_type = -1;
1076
1077 return transport_type;
1078 }
1079
check_mtu_validate(struct hns_roce_dev * hr_dev,struct hns_roce_qp * hr_qp,struct ib_qp_attr * attr,int attr_mask)1080 static int check_mtu_validate(struct hns_roce_dev *hr_dev,
1081 struct hns_roce_qp *hr_qp,
1082 struct ib_qp_attr *attr, int attr_mask)
1083 {
1084 enum ib_mtu active_mtu;
1085 int p;
1086
1087 p = attr_mask & IB_QP_PORT ? (attr->port_num - 1) : hr_qp->port;
1088 active_mtu = iboe_get_mtu(hr_dev->iboe.netdevs[p]->mtu);
1089
1090 if ((hr_dev->caps.max_mtu >= IB_MTU_2048 &&
1091 attr->path_mtu > hr_dev->caps.max_mtu) ||
1092 attr->path_mtu < IB_MTU_256 || attr->path_mtu > active_mtu) {
1093 ibdev_err(&hr_dev->ib_dev,
1094 "attr path_mtu(%d)invalid while modify qp",
1095 attr->path_mtu);
1096 return -EINVAL;
1097 }
1098
1099 return 0;
1100 }
1101
hns_roce_check_qp_attr(struct ib_qp * ibqp,struct ib_qp_attr * attr,int attr_mask)1102 static int hns_roce_check_qp_attr(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1103 int attr_mask)
1104 {
1105 struct hns_roce_dev *hr_dev = to_hr_dev(ibqp->device);
1106 struct hns_roce_qp *hr_qp = to_hr_qp(ibqp);
1107 int p;
1108
1109 if ((attr_mask & IB_QP_PORT) &&
1110 (attr->port_num == 0 || attr->port_num > hr_dev->caps.num_ports)) {
1111 ibdev_err(&hr_dev->ib_dev, "invalid attr, port_num = %u.\n",
1112 attr->port_num);
1113 return -EINVAL;
1114 }
1115
1116 if (attr_mask & IB_QP_PKEY_INDEX) {
1117 p = attr_mask & IB_QP_PORT ? (attr->port_num - 1) : hr_qp->port;
1118 if (attr->pkey_index >= hr_dev->caps.pkey_table_len[p]) {
1119 ibdev_err(&hr_dev->ib_dev,
1120 "invalid attr, pkey_index = %u.\n",
1121 attr->pkey_index);
1122 return -EINVAL;
1123 }
1124 }
1125
1126 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
1127 attr->max_rd_atomic > hr_dev->caps.max_qp_init_rdma) {
1128 ibdev_err(&hr_dev->ib_dev,
1129 "invalid attr, max_rd_atomic = %u.\n",
1130 attr->max_rd_atomic);
1131 return -EINVAL;
1132 }
1133
1134 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
1135 attr->max_dest_rd_atomic > hr_dev->caps.max_qp_dest_rdma) {
1136 ibdev_err(&hr_dev->ib_dev,
1137 "invalid attr, max_dest_rd_atomic = %u.\n",
1138 attr->max_dest_rd_atomic);
1139 return -EINVAL;
1140 }
1141
1142 if (attr_mask & IB_QP_PATH_MTU)
1143 return check_mtu_validate(hr_dev, hr_qp, attr, attr_mask);
1144
1145 return 0;
1146 }
1147
hns_roce_modify_qp(struct ib_qp * ibqp,struct ib_qp_attr * attr,int attr_mask,struct ib_udata * udata)1148 int hns_roce_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1149 int attr_mask, struct ib_udata *udata)
1150 {
1151 struct hns_roce_dev *hr_dev = to_hr_dev(ibqp->device);
1152 struct hns_roce_qp *hr_qp = to_hr_qp(ibqp);
1153 enum ib_qp_state cur_state, new_state;
1154 int ret = -EINVAL;
1155
1156 mutex_lock(&hr_qp->mutex);
1157
1158 if (attr_mask & IB_QP_CUR_STATE && attr->cur_qp_state != hr_qp->state)
1159 goto out;
1160
1161 cur_state = hr_qp->state;
1162 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
1163
1164 if (ibqp->uobject &&
1165 (attr_mask & IB_QP_STATE) && new_state == IB_QPS_ERR) {
1166 if (hr_qp->en_flags & HNS_ROCE_QP_CAP_SQ_RECORD_DB) {
1167 hr_qp->sq.head = *(int *)(hr_qp->sdb.virt_addr);
1168
1169 if (hr_qp->en_flags & HNS_ROCE_QP_CAP_RQ_RECORD_DB)
1170 hr_qp->rq.head = *(int *)(hr_qp->rdb.virt_addr);
1171 } else {
1172 ibdev_warn(&hr_dev->ib_dev,
1173 "flush cqe is not supported in userspace!\n");
1174 goto out;
1175 }
1176 }
1177
1178 if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
1179 attr_mask)) {
1180 ibdev_err(&hr_dev->ib_dev, "ib_modify_qp_is_ok failed\n");
1181 goto out;
1182 }
1183
1184 ret = hns_roce_check_qp_attr(ibqp, attr, attr_mask);
1185 if (ret)
1186 goto out;
1187
1188 if (cur_state == new_state && cur_state == IB_QPS_RESET) {
1189 if (hr_dev->hw_rev == HNS_ROCE_HW_VER1) {
1190 ret = -EPERM;
1191 ibdev_err(&hr_dev->ib_dev,
1192 "RST2RST state is not supported\n");
1193 } else {
1194 ret = 0;
1195 }
1196
1197 goto out;
1198 }
1199
1200 ret = hr_dev->hw->modify_qp(ibqp, attr, attr_mask, cur_state,
1201 new_state);
1202
1203 out:
1204 mutex_unlock(&hr_qp->mutex);
1205
1206 return ret;
1207 }
1208
hns_roce_lock_cqs(struct hns_roce_cq * send_cq,struct hns_roce_cq * recv_cq)1209 void hns_roce_lock_cqs(struct hns_roce_cq *send_cq, struct hns_roce_cq *recv_cq)
1210 __acquires(&send_cq->lock) __acquires(&recv_cq->lock)
1211 {
1212 if (unlikely(send_cq == NULL && recv_cq == NULL)) {
1213 __acquire(&send_cq->lock);
1214 __acquire(&recv_cq->lock);
1215 } else if (unlikely(send_cq != NULL && recv_cq == NULL)) {
1216 spin_lock_irq(&send_cq->lock);
1217 __acquire(&recv_cq->lock);
1218 } else if (unlikely(send_cq == NULL && recv_cq != NULL)) {
1219 spin_lock_irq(&recv_cq->lock);
1220 __acquire(&send_cq->lock);
1221 } else if (send_cq == recv_cq) {
1222 spin_lock_irq(&send_cq->lock);
1223 __acquire(&recv_cq->lock);
1224 } else if (send_cq->cqn < recv_cq->cqn) {
1225 spin_lock_irq(&send_cq->lock);
1226 spin_lock_nested(&recv_cq->lock, SINGLE_DEPTH_NESTING);
1227 } else {
1228 spin_lock_irq(&recv_cq->lock);
1229 spin_lock_nested(&send_cq->lock, SINGLE_DEPTH_NESTING);
1230 }
1231 }
1232
hns_roce_unlock_cqs(struct hns_roce_cq * send_cq,struct hns_roce_cq * recv_cq)1233 void hns_roce_unlock_cqs(struct hns_roce_cq *send_cq,
1234 struct hns_roce_cq *recv_cq) __releases(&send_cq->lock)
1235 __releases(&recv_cq->lock)
1236 {
1237 if (unlikely(send_cq == NULL && recv_cq == NULL)) {
1238 __release(&recv_cq->lock);
1239 __release(&send_cq->lock);
1240 } else if (unlikely(send_cq != NULL && recv_cq == NULL)) {
1241 __release(&recv_cq->lock);
1242 spin_unlock(&send_cq->lock);
1243 } else if (unlikely(send_cq == NULL && recv_cq != NULL)) {
1244 __release(&send_cq->lock);
1245 spin_unlock(&recv_cq->lock);
1246 } else if (send_cq == recv_cq) {
1247 __release(&recv_cq->lock);
1248 spin_unlock_irq(&send_cq->lock);
1249 } else if (send_cq->cqn < recv_cq->cqn) {
1250 spin_unlock(&recv_cq->lock);
1251 spin_unlock_irq(&send_cq->lock);
1252 } else {
1253 spin_unlock(&send_cq->lock);
1254 spin_unlock_irq(&recv_cq->lock);
1255 }
1256 }
1257
get_wqe(struct hns_roce_qp * hr_qp,int offset)1258 static inline void *get_wqe(struct hns_roce_qp *hr_qp, int offset)
1259 {
1260 return hns_roce_buf_offset(hr_qp->mtr.kmem, offset);
1261 }
1262
hns_roce_get_recv_wqe(struct hns_roce_qp * hr_qp,int n)1263 void *hns_roce_get_recv_wqe(struct hns_roce_qp *hr_qp, int n)
1264 {
1265 return get_wqe(hr_qp, hr_qp->rq.offset + (n << hr_qp->rq.wqe_shift));
1266 }
1267
hns_roce_get_send_wqe(struct hns_roce_qp * hr_qp,int n)1268 void *hns_roce_get_send_wqe(struct hns_roce_qp *hr_qp, int n)
1269 {
1270 return get_wqe(hr_qp, hr_qp->sq.offset + (n << hr_qp->sq.wqe_shift));
1271 }
1272
hns_roce_get_extend_sge(struct hns_roce_qp * hr_qp,int n)1273 void *hns_roce_get_extend_sge(struct hns_roce_qp *hr_qp, int n)
1274 {
1275 return get_wqe(hr_qp, hr_qp->sge.offset + (n << hr_qp->sge.sge_shift));
1276 }
1277
hns_roce_wq_overflow(struct hns_roce_wq * hr_wq,int nreq,struct ib_cq * ib_cq)1278 bool hns_roce_wq_overflow(struct hns_roce_wq *hr_wq, int nreq,
1279 struct ib_cq *ib_cq)
1280 {
1281 struct hns_roce_cq *hr_cq;
1282 u32 cur;
1283
1284 cur = hr_wq->head - hr_wq->tail;
1285 if (likely(cur + nreq < hr_wq->wqe_cnt))
1286 return false;
1287
1288 hr_cq = to_hr_cq(ib_cq);
1289 spin_lock(&hr_cq->lock);
1290 cur = hr_wq->head - hr_wq->tail;
1291 spin_unlock(&hr_cq->lock);
1292
1293 return cur + nreq >= hr_wq->wqe_cnt;
1294 }
1295
hns_roce_init_qp_table(struct hns_roce_dev * hr_dev)1296 int hns_roce_init_qp_table(struct hns_roce_dev *hr_dev)
1297 {
1298 struct hns_roce_qp_table *qp_table = &hr_dev->qp_table;
1299 int reserved_from_top = 0;
1300 int reserved_from_bot;
1301 int ret;
1302
1303 mutex_init(&qp_table->scc_mutex);
1304 xa_init(&hr_dev->qp_table_xa);
1305
1306 reserved_from_bot = hr_dev->caps.reserved_qps;
1307
1308 ret = hns_roce_bitmap_init(&qp_table->bitmap, hr_dev->caps.num_qps,
1309 hr_dev->caps.num_qps - 1, reserved_from_bot,
1310 reserved_from_top);
1311 if (ret) {
1312 dev_err(hr_dev->dev, "qp bitmap init failed!error=%d\n",
1313 ret);
1314 return ret;
1315 }
1316
1317 return 0;
1318 }
1319
hns_roce_cleanup_qp_table(struct hns_roce_dev * hr_dev)1320 void hns_roce_cleanup_qp_table(struct hns_roce_dev *hr_dev)
1321 {
1322 hns_roce_bitmap_cleanup(&hr_dev->qp_table.bitmap);
1323 }
1324