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