xref: /openbmc/linux/drivers/infiniband/hw/hns/hns_roce_cq.c (revision 9144f784f852f9a125cabe9927b986d909bfa439)
19a443537Soulijun /*
29a443537Soulijun  * Copyright (c) 2016 Hisilicon Limited.
39a443537Soulijun  *
49a443537Soulijun  * This software is available to you under a choice of one of two
59a443537Soulijun  * licenses.  You may choose to be licensed under the terms of the GNU
69a443537Soulijun  * General Public License (GPL) Version 2, available from the file
79a443537Soulijun  * COPYING in the main directory of this source tree, or the
89a443537Soulijun  * OpenIB.org BSD license below:
99a443537Soulijun  *
109a443537Soulijun  *     Redistribution and use in source and binary forms, with or
119a443537Soulijun  *     without modification, are permitted provided that the following
129a443537Soulijun  *     conditions are met:
139a443537Soulijun  *
149a443537Soulijun  *      - Redistributions of source code must retain the above
159a443537Soulijun  *        copyright notice, this list of conditions and the following
169a443537Soulijun  *        disclaimer.
179a443537Soulijun  *
189a443537Soulijun  *      - Redistributions in binary form must reproduce the above
199a443537Soulijun  *        copyright notice, this list of conditions and the following
209a443537Soulijun  *        disclaimer in the documentation and/or other materials
219a443537Soulijun  *        provided with the distribution.
229a443537Soulijun  *
239a443537Soulijun  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
249a443537Soulijun  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
259a443537Soulijun  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
269a443537Soulijun  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
279a443537Soulijun  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
289a443537Soulijun  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
299a443537Soulijun  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
309a443537Soulijun  * SOFTWARE.
319a443537Soulijun  */
329a443537Soulijun 
339a443537Soulijun #include <rdma/ib_umem.h>
34bdeacabdSShamir Rabinovitch #include <rdma/uverbs_ioctl.h>
359a443537Soulijun #include "hns_roce_device.h"
369a443537Soulijun #include "hns_roce_cmd.h"
379a443537Soulijun #include "hns_roce_hem.h"
389a443537Soulijun #include "hns_roce_common.h"
399a443537Soulijun 
get_least_load_bankid_for_cq(struct hns_roce_bank * bank)401bbd4380SYangyang Li static u8 get_least_load_bankid_for_cq(struct hns_roce_bank *bank)
411bbd4380SYangyang Li {
421bbd4380SYangyang Li 	u32 least_load = bank[0].inuse;
431bbd4380SYangyang Li 	u8 bankid = 0;
441bbd4380SYangyang Li 	u32 bankcnt;
451bbd4380SYangyang Li 	u8 i;
461bbd4380SYangyang Li 
471bbd4380SYangyang Li 	for (i = 1; i < HNS_ROCE_CQ_BANK_NUM; i++) {
481bbd4380SYangyang Li 		bankcnt = bank[i].inuse;
491bbd4380SYangyang Li 		if (bankcnt < least_load) {
501bbd4380SYangyang Li 			least_load = bankcnt;
511bbd4380SYangyang Li 			bankid = i;
521bbd4380SYangyang Li 		}
531bbd4380SYangyang Li 	}
541bbd4380SYangyang Li 
551bbd4380SYangyang Li 	return bankid;
561bbd4380SYangyang Li }
571bbd4380SYangyang Li 
alloc_cqn(struct hns_roce_dev * hr_dev,struct hns_roce_cq * hr_cq)581bbd4380SYangyang Li static int alloc_cqn(struct hns_roce_dev *hr_dev, struct hns_roce_cq *hr_cq)
591bbd4380SYangyang Li {
601bbd4380SYangyang Li 	struct hns_roce_cq_table *cq_table = &hr_dev->cq_table;
611bbd4380SYangyang Li 	struct hns_roce_bank *bank;
621bbd4380SYangyang Li 	u8 bankid;
631bbd4380SYangyang Li 	int id;
641bbd4380SYangyang Li 
651bbd4380SYangyang Li 	mutex_lock(&cq_table->bank_mutex);
661bbd4380SYangyang Li 	bankid = get_least_load_bankid_for_cq(cq_table->bank);
671bbd4380SYangyang Li 	bank = &cq_table->bank[bankid];
681bbd4380SYangyang Li 
691bbd4380SYangyang Li 	id = ida_alloc_range(&bank->ida, bank->min, bank->max, GFP_KERNEL);
701bbd4380SYangyang Li 	if (id < 0) {
711bbd4380SYangyang Li 		mutex_unlock(&cq_table->bank_mutex);
721bbd4380SYangyang Li 		return id;
731bbd4380SYangyang Li 	}
741bbd4380SYangyang Li 
751bbd4380SYangyang Li 	/* the lower 2 bits is bankid */
761bbd4380SYangyang Li 	hr_cq->cqn = (id << CQ_BANKID_SHIFT) | bankid;
771bbd4380SYangyang Li 	bank->inuse++;
781bbd4380SYangyang Li 	mutex_unlock(&cq_table->bank_mutex);
791bbd4380SYangyang Li 
801bbd4380SYangyang Li 	return 0;
811bbd4380SYangyang Li }
821bbd4380SYangyang Li 
get_cq_bankid(unsigned long cqn)831bbd4380SYangyang Li static inline u8 get_cq_bankid(unsigned long cqn)
841bbd4380SYangyang Li {
851bbd4380SYangyang Li 	/* The lower 2 bits of CQN are used to hash to different banks */
861bbd4380SYangyang Li 	return (u8)(cqn & GENMASK(1, 0));
871bbd4380SYangyang Li }
881bbd4380SYangyang Li 
free_cqn(struct hns_roce_dev * hr_dev,unsigned long cqn)891bbd4380SYangyang Li static void free_cqn(struct hns_roce_dev *hr_dev, unsigned long cqn)
901bbd4380SYangyang Li {
911bbd4380SYangyang Li 	struct hns_roce_cq_table *cq_table = &hr_dev->cq_table;
921bbd4380SYangyang Li 	struct hns_roce_bank *bank;
931bbd4380SYangyang Li 
941bbd4380SYangyang Li 	bank = &cq_table->bank[get_cq_bankid(cqn)];
951bbd4380SYangyang Li 
961bbd4380SYangyang Li 	ida_free(&bank->ida, cqn >> CQ_BANKID_SHIFT);
971bbd4380SYangyang Li 
981bbd4380SYangyang Li 	mutex_lock(&cq_table->bank_mutex);
991bbd4380SYangyang Li 	bank->inuse--;
1001bbd4380SYangyang Li 	mutex_unlock(&cq_table->bank_mutex);
1011bbd4380SYangyang Li }
1021bbd4380SYangyang Li 
hns_roce_create_cqc(struct hns_roce_dev * hr_dev,struct hns_roce_cq * hr_cq,u64 * mtts,dma_addr_t dma_handle)10373f7e056SWenpeng Liang static int hns_roce_create_cqc(struct hns_roce_dev *hr_dev,
10473f7e056SWenpeng Liang 			       struct hns_roce_cq *hr_cq,
10573f7e056SWenpeng Liang 			       u64 *mtts, dma_addr_t dma_handle)
10673f7e056SWenpeng Liang {
10773f7e056SWenpeng Liang 	struct ib_device *ibdev = &hr_dev->ib_dev;
10873f7e056SWenpeng Liang 	struct hns_roce_cmd_mailbox *mailbox;
10973f7e056SWenpeng Liang 	int ret;
11073f7e056SWenpeng Liang 
11173f7e056SWenpeng Liang 	mailbox = hns_roce_alloc_cmd_mailbox(hr_dev);
11273f7e056SWenpeng Liang 	if (IS_ERR(mailbox)) {
11373f7e056SWenpeng Liang 		ibdev_err(ibdev, "failed to alloc mailbox for CQC.\n");
11473f7e056SWenpeng Liang 		return PTR_ERR(mailbox);
11573f7e056SWenpeng Liang 	}
11673f7e056SWenpeng Liang 
11773f7e056SWenpeng Liang 	hr_dev->hw->write_cqc(hr_dev, hr_cq, mailbox->buf, mtts, dma_handle);
11873f7e056SWenpeng Liang 
11973f7e056SWenpeng Liang 	ret = hns_roce_create_hw_ctx(hr_dev, mailbox, HNS_ROCE_CMD_CREATE_CQC,
12073f7e056SWenpeng Liang 				     hr_cq->cqn);
12173f7e056SWenpeng Liang 	if (ret)
12273f7e056SWenpeng Liang 		ibdev_err(ibdev,
12373f7e056SWenpeng Liang 			  "failed to send create cmd for CQ(0x%lx), ret = %d.\n",
12473f7e056SWenpeng Liang 			  hr_cq->cqn, ret);
12573f7e056SWenpeng Liang 
12673f7e056SWenpeng Liang 	hns_roce_free_cmd_mailbox(hr_dev, mailbox);
12773f7e056SWenpeng Liang 
12873f7e056SWenpeng Liang 	return ret;
12973f7e056SWenpeng Liang }
13073f7e056SWenpeng Liang 
alloc_cqc(struct hns_roce_dev * hr_dev,struct hns_roce_cq * hr_cq)131744b7bdfSXi Wang static int alloc_cqc(struct hns_roce_dev *hr_dev, struct hns_roce_cq *hr_cq)
1329a443537Soulijun {
1331bbd4380SYangyang Li 	struct hns_roce_cq_table *cq_table = &hr_dev->cq_table;
134dc93a0d9SLang Cheng 	struct ib_device *ibdev = &hr_dev->ib_dev;
13573f7e056SWenpeng Liang 	u64 mtts[MTT_MIN_COUNT] = {};
1366a93c77aSShaobo Xu 	int ret;
1379a443537Soulijun 
138*363f502cSChengchang Tang 	ret = hns_roce_mtr_find(hr_dev, &hr_cq->mtr, 0, mtts, ARRAY_SIZE(mtts));
139*363f502cSChengchang Tang 	if (ret) {
14061918e9bSYixing Liu 		ibdev_err(ibdev, "failed to find CQ mtr, ret = %d.\n", ret);
141*363f502cSChengchang Tang 		return ret;
1429a443537Soulijun 	}
1439a443537Soulijun 
1449a443537Soulijun 	/* Get CQC memory HEM(Hardware Entry Memory) table */
1459a443537Soulijun 	ret = hns_roce_table_get(hr_dev, &cq_table->table, hr_cq->cqn);
1469a443537Soulijun 	if (ret) {
14761918e9bSYixing Liu 		ibdev_err(ibdev, "failed to get CQ(0x%lx) context, ret = %d.\n",
148744b7bdfSXi Wang 			  hr_cq->cqn, ret);
14973f7e056SWenpeng Liang 		return ret;
1509a443537Soulijun 	}
1519a443537Soulijun 
15263da190eSChengchang Tang 	ret = xa_err(xa_store_irq(&cq_table->array, hr_cq->cqn, hr_cq, GFP_KERNEL));
1539a443537Soulijun 	if (ret) {
15461918e9bSYixing Liu 		ibdev_err(ibdev, "failed to xa_store CQ, ret = %d.\n", ret);
1559a443537Soulijun 		goto err_put;
1569a443537Soulijun 	}
1579a443537Soulijun 
158*363f502cSChengchang Tang 	ret = hns_roce_create_cqc(hr_dev, hr_cq, mtts,
159*363f502cSChengchang Tang 				  hns_roce_get_mtr_ba(&hr_cq->mtr));
16073f7e056SWenpeng Liang 	if (ret)
16127e19f45SMatthew Wilcox 		goto err_xa;
162ed3e6d01SLijun Ou 
1639a443537Soulijun 	return 0;
1649a443537Soulijun 
16527e19f45SMatthew Wilcox err_xa:
16663da190eSChengchang Tang 	xa_erase_irq(&cq_table->array, hr_cq->cqn);
1679a443537Soulijun err_put:
1689a443537Soulijun 	hns_roce_table_put(hr_dev, &cq_table->table, hr_cq->cqn);
1699a443537Soulijun 
1709a443537Soulijun 	return ret;
1719a443537Soulijun }
1729a443537Soulijun 
free_cqc(struct hns_roce_dev * hr_dev,struct hns_roce_cq * hr_cq)173744b7bdfSXi Wang static void free_cqc(struct hns_roce_dev *hr_dev, struct hns_roce_cq *hr_cq)
1749a443537Soulijun {
1759a443537Soulijun 	struct hns_roce_cq_table *cq_table = &hr_dev->cq_table;
17613ca970eSWei Hu(Xavier) 	struct device *dev = hr_dev->dev;
1779a443537Soulijun 	int ret;
1789a443537Soulijun 
179cf7f8f5cSChengchang Tang 	ret = hns_roce_destroy_hw_ctx(hr_dev, HNS_ROCE_CMD_DESTROY_CQC,
180162e29feSChengchang Tang 				      hr_cq->cqn);
1819a443537Soulijun 	if (ret)
182b4ba31e5Swenglianfa 		dev_err_ratelimited(dev, "DESTROY_CQ failed (%d) for CQN %06lx\n",
183b4ba31e5Swenglianfa 				    ret, hr_cq->cqn);
184b16f8188SYixian Liu 
18563da190eSChengchang Tang 	xa_erase_irq(&cq_table->array, hr_cq->cqn);
18627e19f45SMatthew Wilcox 
1879a443537Soulijun 	/* Waiting interrupt process procedure carried out */
1889a443537Soulijun 	synchronize_irq(hr_dev->eq_table.eq[hr_cq->vector].irq);
1899a443537Soulijun 
190ed3e6d01SLijun Ou 	/* wait for all interrupt processed */
191cc9e5a84SWeihang Li 	if (refcount_dec_and_test(&hr_cq->refcount))
192ed3e6d01SLijun Ou 		complete(&hr_cq->free);
193ed3e6d01SLijun Ou 	wait_for_completion(&hr_cq->free);
194ed3e6d01SLijun Ou 
1959a443537Soulijun 	hns_roce_table_put(hr_dev, &cq_table->table, hr_cq->cqn);
1969a443537Soulijun }
1979a443537Soulijun 
alloc_cq_buf(struct hns_roce_dev * hr_dev,struct hns_roce_cq * hr_cq,struct ib_udata * udata,unsigned long addr)198744b7bdfSXi Wang static int alloc_cq_buf(struct hns_roce_dev *hr_dev, struct hns_roce_cq *hr_cq,
199744b7bdfSXi Wang 			struct ib_udata *udata, unsigned long addr)
2009a443537Soulijun {
201744b7bdfSXi Wang 	struct ib_device *ibdev = &hr_dev->ib_dev;
202744b7bdfSXi Wang 	struct hns_roce_buf_attr buf_attr = {};
20361918e9bSYixing Liu 	int ret;
2049a443537Soulijun 
2057b0006dbSXi Wang 	buf_attr.page_shift = hr_dev->caps.cqe_buf_pg_sz + PAGE_SHIFT;
20609a5f210SWenpeng Liang 	buf_attr.region[0].size = hr_cq->cq_depth * hr_cq->cqe_size;
207744b7bdfSXi Wang 	buf_attr.region[0].hopnum = hr_dev->caps.cqe_hop_num;
208744b7bdfSXi Wang 	buf_attr.region_count = 1;
2099a443537Soulijun 
21061918e9bSYixing Liu 	ret = hns_roce_mtr_create(hr_dev, &hr_cq->mtr, &buf_attr,
2117b0006dbSXi Wang 				  hr_dev->caps.cqe_ba_pg_sz + PAGE_SHIFT,
212744b7bdfSXi Wang 				  udata, addr);
21361918e9bSYixing Liu 	if (ret)
21461918e9bSYixing Liu 		ibdev_err(ibdev, "failed to alloc CQ mtr, ret = %d.\n", ret);
2159a8982dcSWei Hu(Xavier) 
21661918e9bSYixing Liu 	return ret;
2179a443537Soulijun }
2189a443537Soulijun 
free_cq_buf(struct hns_roce_dev * hr_dev,struct hns_roce_cq * hr_cq)219707783abSYixian Liu static void free_cq_buf(struct hns_roce_dev *hr_dev, struct hns_roce_cq *hr_cq)
2209a443537Soulijun {
221744b7bdfSXi Wang 	hns_roce_mtr_destroy(hr_dev, &hr_cq->mtr);
2229a443537Soulijun }
2239a443537Soulijun 
alloc_cq_db(struct hns_roce_dev * hr_dev,struct hns_roce_cq * hr_cq,struct ib_udata * udata,unsigned long addr,struct hns_roce_ib_create_cq_resp * resp)224744b7bdfSXi Wang static int alloc_cq_db(struct hns_roce_dev *hr_dev, struct hns_roce_cq *hr_cq,
225744b7bdfSXi Wang 		       struct ib_udata *udata, unsigned long addr,
226e2b2744aSYixian Liu 		       struct hns_roce_ib_create_cq_resp *resp)
2274f8f0d5eSLijun Ou {
228cf8cd4ccSYixian Liu 	bool has_db = hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_CQ_RECORD_DB;
229744b7bdfSXi Wang 	struct hns_roce_ucontext *uctx;
230744b7bdfSXi Wang 	int err;
2314f8f0d5eSLijun Ou 
232744b7bdfSXi Wang 	if (udata) {
233744b7bdfSXi Wang 		if (has_db &&
2349e57a9aaSWeihang Li 		    udata->outlen >= offsetofend(typeof(*resp), cap_flags)) {
235744b7bdfSXi Wang 			uctx = rdma_udata_to_drv_context(udata,
236744b7bdfSXi Wang 					struct hns_roce_ucontext, ibucontext);
23769e0a42fSLang Cheng 			err = hns_roce_db_map_user(uctx, addr, &hr_cq->db);
238744b7bdfSXi Wang 			if (err)
239744b7bdfSXi Wang 				return err;
24005e6a5a6SLang Cheng 			hr_cq->flags |= HNS_ROCE_CQ_FLAG_RECORD_DB;
24105e6a5a6SLang Cheng 			resp->cap_flags |= HNS_ROCE_CQ_FLAG_RECORD_DB;
2424f8f0d5eSLijun Ou 		}
243744b7bdfSXi Wang 	} else {
244744b7bdfSXi Wang 		if (has_db) {
245744b7bdfSXi Wang 			err = hns_roce_alloc_db(hr_dev, &hr_cq->db, 1);
246744b7bdfSXi Wang 			if (err)
247744b7bdfSXi Wang 				return err;
2484f8f0d5eSLijun Ou 			hr_cq->set_ci_db = hr_cq->db.db_record;
2494f8f0d5eSLijun Ou 			*hr_cq->set_ci_db = 0;
25005e6a5a6SLang Cheng 			hr_cq->flags |= HNS_ROCE_CQ_FLAG_RECORD_DB;
2514f8f0d5eSLijun Ou 		}
252704d68f5SYixian Liu 		hr_cq->db_reg = hr_dev->reg_base + hr_dev->odb_offset +
25303ccba5cSYixian Liu 				DB_REG_OFFSET * hr_dev->priv_uar.index;
254744b7bdfSXi Wang 	}
2554f8f0d5eSLijun Ou 
2564f8f0d5eSLijun Ou 	return 0;
2574f8f0d5eSLijun Ou }
2584f8f0d5eSLijun Ou 
free_cq_db(struct hns_roce_dev * hr_dev,struct hns_roce_cq * hr_cq,struct ib_udata * udata)259744b7bdfSXi Wang static void free_cq_db(struct hns_roce_dev *hr_dev, struct hns_roce_cq *hr_cq,
260744b7bdfSXi Wang 		       struct ib_udata *udata)
2614f8f0d5eSLijun Ou {
262744b7bdfSXi Wang 	struct hns_roce_ucontext *uctx;
2634f8f0d5eSLijun Ou 
26405e6a5a6SLang Cheng 	if (!(hr_cq->flags & HNS_ROCE_CQ_FLAG_RECORD_DB))
265744b7bdfSXi Wang 		return;
2664f8f0d5eSLijun Ou 
26705e6a5a6SLang Cheng 	hr_cq->flags &= ~HNS_ROCE_CQ_FLAG_RECORD_DB;
268744b7bdfSXi Wang 	if (udata) {
269744b7bdfSXi Wang 		uctx = rdma_udata_to_drv_context(udata,
270744b7bdfSXi Wang 						 struct hns_roce_ucontext,
271744b7bdfSXi Wang 						 ibucontext);
272744b7bdfSXi Wang 		hns_roce_db_unmap_user(uctx, &hr_cq->db);
273744b7bdfSXi Wang 	} else {
2744f8f0d5eSLijun Ou 		hns_roce_free_db(hr_dev, &hr_cq->db);
2754f8f0d5eSLijun Ou 	}
276744b7bdfSXi Wang }
2774f8f0d5eSLijun Ou 
verify_cq_create_attr(struct hns_roce_dev * hr_dev,const struct ib_cq_init_attr * attr)278d102a6e3SYixing Liu static int verify_cq_create_attr(struct hns_roce_dev *hr_dev,
279d102a6e3SYixing Liu 				 const struct ib_cq_init_attr *attr)
280d102a6e3SYixing Liu {
281d102a6e3SYixing Liu 	struct ib_device *ibdev = &hr_dev->ib_dev;
282d102a6e3SYixing Liu 
283d102a6e3SYixing Liu 	if (!attr->cqe || attr->cqe > hr_dev->caps.max_cqes) {
284d102a6e3SYixing Liu 		ibdev_err(ibdev, "failed to check CQ count %u, max = %u.\n",
285d102a6e3SYixing Liu 			  attr->cqe, hr_dev->caps.max_cqes);
286d102a6e3SYixing Liu 		return -EINVAL;
287d102a6e3SYixing Liu 	}
288d102a6e3SYixing Liu 
289d102a6e3SYixing Liu 	if (attr->comp_vector >= hr_dev->caps.num_comp_vectors) {
290d102a6e3SYixing Liu 		ibdev_err(ibdev, "failed to check CQ vector = %u, max = %d.\n",
291d102a6e3SYixing Liu 			  attr->comp_vector, hr_dev->caps.num_comp_vectors);
292d102a6e3SYixing Liu 		return -EINVAL;
293d102a6e3SYixing Liu 	}
294d102a6e3SYixing Liu 
295d102a6e3SYixing Liu 	return 0;
296d102a6e3SYixing Liu }
297d102a6e3SYixing Liu 
get_cq_ucmd(struct hns_roce_cq * hr_cq,struct ib_udata * udata,struct hns_roce_ib_create_cq * ucmd)298d102a6e3SYixing Liu static int get_cq_ucmd(struct hns_roce_cq *hr_cq, struct ib_udata *udata,
299d102a6e3SYixing Liu 		       struct hns_roce_ib_create_cq *ucmd)
300d102a6e3SYixing Liu {
301d102a6e3SYixing Liu 	struct ib_device *ibdev = hr_cq->ib_cq.device;
302d102a6e3SYixing Liu 	int ret;
303d102a6e3SYixing Liu 
304d102a6e3SYixing Liu 	ret = ib_copy_from_udata(ucmd, udata, min(udata->inlen, sizeof(*ucmd)));
305d102a6e3SYixing Liu 	if (ret) {
306d102a6e3SYixing Liu 		ibdev_err(ibdev, "failed to copy CQ udata, ret = %d.\n", ret);
307d102a6e3SYixing Liu 		return ret;
308d102a6e3SYixing Liu 	}
309d102a6e3SYixing Liu 
310d102a6e3SYixing Liu 	return 0;
311d102a6e3SYixing Liu }
312d102a6e3SYixing Liu 
set_cq_param(struct hns_roce_cq * hr_cq,u32 cq_entries,int vector,struct hns_roce_ib_create_cq * ucmd)313d102a6e3SYixing Liu static void set_cq_param(struct hns_roce_cq *hr_cq, u32 cq_entries, int vector,
314d102a6e3SYixing Liu 			 struct hns_roce_ib_create_cq *ucmd)
315d102a6e3SYixing Liu {
316d102a6e3SYixing Liu 	struct hns_roce_dev *hr_dev = to_hr_dev(hr_cq->ib_cq.device);
317d102a6e3SYixing Liu 
318d102a6e3SYixing Liu 	cq_entries = max(cq_entries, hr_dev->caps.min_cqes);
319d102a6e3SYixing Liu 	cq_entries = roundup_pow_of_two(cq_entries);
320d102a6e3SYixing Liu 	hr_cq->ib_cq.cqe = cq_entries - 1; /* used as cqe index */
321d102a6e3SYixing Liu 	hr_cq->cq_depth = cq_entries;
322d102a6e3SYixing Liu 	hr_cq->vector = vector;
323d102a6e3SYixing Liu 
324d102a6e3SYixing Liu 	spin_lock_init(&hr_cq->lock);
325d102a6e3SYixing Liu 	INIT_LIST_HEAD(&hr_cq->sq_list);
326d102a6e3SYixing Liu 	INIT_LIST_HEAD(&hr_cq->rq_list);
327d102a6e3SYixing Liu }
328d102a6e3SYixing Liu 
set_cqe_size(struct hns_roce_cq * hr_cq,struct ib_udata * udata,struct hns_roce_ib_create_cq * ucmd)329e671f0ecSWenpeng Liang static int set_cqe_size(struct hns_roce_cq *hr_cq, struct ib_udata *udata,
33009a5f210SWenpeng Liang 			struct hns_roce_ib_create_cq *ucmd)
33109a5f210SWenpeng Liang {
33209a5f210SWenpeng Liang 	struct hns_roce_dev *hr_dev = to_hr_dev(hr_cq->ib_cq.device);
33309a5f210SWenpeng Liang 
334e671f0ecSWenpeng Liang 	if (!udata) {
33509a5f210SWenpeng Liang 		hr_cq->cqe_size = hr_dev->caps.cqe_sz;
336e671f0ecSWenpeng Liang 		return 0;
33709a5f210SWenpeng Liang 	}
338e671f0ecSWenpeng Liang 
339e671f0ecSWenpeng Liang 	if (udata->inlen >= offsetofend(typeof(*ucmd), cqe_size)) {
340e671f0ecSWenpeng Liang 		if (ucmd->cqe_size != HNS_ROCE_V2_CQE_SIZE &&
341e671f0ecSWenpeng Liang 		    ucmd->cqe_size != HNS_ROCE_V3_CQE_SIZE) {
342e671f0ecSWenpeng Liang 			ibdev_err(&hr_dev->ib_dev,
343e671f0ecSWenpeng Liang 				  "invalid cqe size %u.\n", ucmd->cqe_size);
344e671f0ecSWenpeng Liang 			return -EINVAL;
345e671f0ecSWenpeng Liang 		}
346e671f0ecSWenpeng Liang 
347e671f0ecSWenpeng Liang 		hr_cq->cqe_size = ucmd->cqe_size;
348e671f0ecSWenpeng Liang 	} else {
349e671f0ecSWenpeng Liang 		hr_cq->cqe_size = HNS_ROCE_V2_CQE_SIZE;
350e671f0ecSWenpeng Liang 	}
351e671f0ecSWenpeng Liang 
352e671f0ecSWenpeng Liang 	return 0;
35309a5f210SWenpeng Liang }
35409a5f210SWenpeng Liang 
hns_roce_create_cq(struct ib_cq * ib_cq,const struct ib_cq_init_attr * attr,struct ib_udata * udata)355707783abSYixian Liu int hns_roce_create_cq(struct ib_cq *ib_cq, const struct ib_cq_init_attr *attr,
3569a443537Soulijun 		       struct ib_udata *udata)
3579a443537Soulijun {
358e39afe3dSLeon Romanovsky 	struct hns_roce_dev *hr_dev = to_hr_dev(ib_cq->device);
3597b48221cSYixian Liu 	struct hns_roce_ib_create_cq_resp resp = {};
360e39afe3dSLeon Romanovsky 	struct hns_roce_cq *hr_cq = to_hr_cq(ib_cq);
361744b7bdfSXi Wang 	struct ib_device *ibdev = &hr_dev->ib_dev;
362744b7bdfSXi Wang 	struct hns_roce_ib_create_cq ucmd = {};
36393aa2187SWei Hu(Xavier) 	int ret;
3649a443537Soulijun 
3651c407cb5SJason Gunthorpe 	if (attr->flags)
3661c407cb5SJason Gunthorpe 		return -EOPNOTSUPP;
3671c407cb5SJason Gunthorpe 
368d102a6e3SYixing Liu 	ret = verify_cq_create_attr(hr_dev, attr);
369d102a6e3SYixing Liu 	if (ret)
370d102a6e3SYixing Liu 		return ret;
3719a443537Soulijun 
372ff23dfa1SShamir Rabinovitch 	if (udata) {
373d102a6e3SYixing Liu 		ret = get_cq_ucmd(hr_cq, udata, &ucmd);
374d102a6e3SYixing Liu 		if (ret)
375744b7bdfSXi Wang 			return ret;
3769a443537Soulijun 	}
377d102a6e3SYixing Liu 
378d102a6e3SYixing Liu 	set_cq_param(hr_cq, attr->cqe, attr->comp_vector, &ucmd);
3799a443537Soulijun 
380e671f0ecSWenpeng Liang 	ret = set_cqe_size(hr_cq, udata, &ucmd);
381e671f0ecSWenpeng Liang 	if (ret)
382e671f0ecSWenpeng Liang 		return ret;
38309a5f210SWenpeng Liang 
384744b7bdfSXi Wang 	ret = alloc_cq_buf(hr_dev, hr_cq, udata, ucmd.buf_addr);
3859a443537Soulijun 	if (ret) {
38661918e9bSYixing Liu 		ibdev_err(ibdev, "failed to alloc CQ buf, ret = %d.\n", ret);
387744b7bdfSXi Wang 		return ret;
388744b7bdfSXi Wang 	}
389744b7bdfSXi Wang 
390744b7bdfSXi Wang 	ret = alloc_cq_db(hr_dev, hr_cq, udata, ucmd.db_addr, &resp);
391744b7bdfSXi Wang 	if (ret) {
39261918e9bSYixing Liu 		ibdev_err(ibdev, "failed to alloc CQ db, ret = %d.\n", ret);
393744b7bdfSXi Wang 		goto err_cq_buf;
394744b7bdfSXi Wang 	}
395744b7bdfSXi Wang 
3961bbd4380SYangyang Li 	ret = alloc_cqn(hr_dev, hr_cq);
3971bbd4380SYangyang Li 	if (ret) {
3981bbd4380SYangyang Li 		ibdev_err(ibdev, "failed to alloc CQN, ret = %d.\n", ret);
3991bbd4380SYangyang Li 		goto err_cq_db;
4001bbd4380SYangyang Li 	}
4011bbd4380SYangyang Li 
402744b7bdfSXi Wang 	ret = alloc_cqc(hr_dev, hr_cq);
403744b7bdfSXi Wang 	if (ret) {
40461918e9bSYixing Liu 		ibdev_err(ibdev,
40561918e9bSYixing Liu 			  "failed to alloc CQ context, ret = %d.\n", ret);
4061bbd4380SYangyang Li 		goto err_cqn;
4079b44703dSYixian Liu 	}
4089b44703dSYixian Liu 
409ff23dfa1SShamir Rabinovitch 	if (udata) {
4109b44703dSYixian Liu 		resp.cqn = hr_cq->cqn;
4111c0ca9cdSWenpeng Liang 		ret = ib_copy_to_udata(udata, &resp,
4121c0ca9cdSWenpeng Liang 				       min(udata->outlen, sizeof(resp)));
4139b44703dSYixian Liu 		if (ret)
414e9595577SYixian Liu 			goto err_cqc;
4159a443537Soulijun 	}
4169a443537Soulijun 
41773f7e056SWenpeng Liang 	hr_cq->cons_index = 0;
41873f7e056SWenpeng Liang 	hr_cq->arm_sn = 1;
41973f7e056SWenpeng Liang 	refcount_set(&hr_cq->refcount, 1);
42073f7e056SWenpeng Liang 	init_completion(&hr_cq->free);
42173f7e056SWenpeng Liang 
422e39afe3dSLeon Romanovsky 	return 0;
4239a443537Soulijun 
424e9595577SYixian Liu err_cqc:
425744b7bdfSXi Wang 	free_cqc(hr_dev, hr_cq);
4261bbd4380SYangyang Li err_cqn:
4271bbd4380SYangyang Li 	free_cqn(hr_dev, hr_cq->cqn);
428744b7bdfSXi Wang err_cq_db:
429744b7bdfSXi Wang 	free_cq_db(hr_dev, hr_cq, udata);
430744b7bdfSXi Wang err_cq_buf:
431744b7bdfSXi Wang 	free_cq_buf(hr_dev, hr_cq);
432e39afe3dSLeon Romanovsky 	return ret;
4339a443537Soulijun }
4349a443537Soulijun 
hns_roce_destroy_cq(struct ib_cq * ib_cq,struct ib_udata * udata)43543d781b9SLeon Romanovsky int hns_roce_destroy_cq(struct ib_cq *ib_cq, struct ib_udata *udata)
4369a443537Soulijun {
4379a443537Soulijun 	struct hns_roce_dev *hr_dev = to_hr_dev(ib_cq->device);
4389a443537Soulijun 	struct hns_roce_cq *hr_cq = to_hr_cq(ib_cq);
4399a443537Soulijun 
440744b7bdfSXi Wang 	free_cqc(hr_dev, hr_cq);
4411bbd4380SYangyang Li 	free_cqn(hr_dev, hr_cq->cqn);
4421bbd4380SYangyang Li 	free_cq_db(hr_dev, hr_cq, udata);
4431bbd4380SYangyang Li 	free_cq_buf(hr_dev, hr_cq);
4441bbd4380SYangyang Li 
44543d781b9SLeon Romanovsky 	return 0;
446afb6b092SShaobo Xu }
4479a443537Soulijun 
hns_roce_cq_completion(struct hns_roce_dev * hr_dev,u32 cqn)4489a443537Soulijun void hns_roce_cq_completion(struct hns_roce_dev *hr_dev, u32 cqn)
4499a443537Soulijun {
450f295e4ceSYixian Liu 	struct hns_roce_cq *hr_cq;
451f295e4ceSYixian Liu 	struct ib_cq *ibcq;
4529a443537Soulijun 
453f295e4ceSYixian Liu 	hr_cq = xa_load(&hr_dev->cq_table.array,
454f295e4ceSYixian Liu 			cqn & (hr_dev->caps.num_cqs - 1));
455f295e4ceSYixian Liu 	if (!hr_cq) {
456f0588567SGuofeng Yue 		dev_warn(hr_dev->dev, "completion event for bogus CQ 0x%06x\n",
457f295e4ceSYixian Liu 			 cqn);
4589a443537Soulijun 		return;
4599a443537Soulijun 	}
4609a443537Soulijun 
461f295e4ceSYixian Liu 	++hr_cq->arm_sn;
462f295e4ceSYixian Liu 	ibcq = &hr_cq->ib_cq;
463f295e4ceSYixian Liu 	if (ibcq->comp_handler)
464f295e4ceSYixian Liu 		ibcq->comp_handler(ibcq, ibcq->cq_context);
4659a443537Soulijun }
4669a443537Soulijun 
hns_roce_cq_event(struct hns_roce_dev * hr_dev,u32 cqn,int event_type)4679a443537Soulijun void hns_roce_cq_event(struct hns_roce_dev *hr_dev, u32 cqn, int event_type)
4689a443537Soulijun {
46913ca970eSWei Hu(Xavier) 	struct device *dev = hr_dev->dev;
470f295e4ceSYixian Liu 	struct hns_roce_cq *hr_cq;
471f295e4ceSYixian Liu 	struct ib_event event;
472f295e4ceSYixian Liu 	struct ib_cq *ibcq;
4739a443537Soulijun 
474f295e4ceSYixian Liu 	if (event_type != HNS_ROCE_EVENT_TYPE_CQ_ID_INVALID &&
475f295e4ceSYixian Liu 	    event_type != HNS_ROCE_EVENT_TYPE_CQ_ACCESS_ERROR &&
476f295e4ceSYixian Liu 	    event_type != HNS_ROCE_EVENT_TYPE_CQ_OVERFLOW) {
477f0588567SGuofeng Yue 		dev_err(dev, "unexpected event type 0x%x on CQ 0x%06x\n",
478f295e4ceSYixian Liu 			event_type, cqn);
479f295e4ceSYixian Liu 		return;
480f295e4ceSYixian Liu 	}
4819a443537Soulijun 
48263da190eSChengchang Tang 	xa_lock(&hr_dev->cq_table.array);
48363da190eSChengchang Tang 	hr_cq = xa_load(&hr_dev->cq_table.array,
48463da190eSChengchang Tang 			cqn & (hr_dev->caps.num_cqs - 1));
48563da190eSChengchang Tang 	if (hr_cq)
486cc9e5a84SWeihang Li 		refcount_inc(&hr_cq->refcount);
48763da190eSChengchang Tang 	xa_unlock(&hr_dev->cq_table.array);
48863da190eSChengchang Tang 	if (!hr_cq) {
48963da190eSChengchang Tang 		dev_warn(dev, "async event for bogus CQ 0x%06x\n", cqn);
49063da190eSChengchang Tang 		return;
49163da190eSChengchang Tang 	}
492f295e4ceSYixian Liu 
493f295e4ceSYixian Liu 	ibcq = &hr_cq->ib_cq;
494f295e4ceSYixian Liu 	if (ibcq->event_handler) {
495f295e4ceSYixian Liu 		event.device = ibcq->device;
496f295e4ceSYixian Liu 		event.element.cq = ibcq;
497f295e4ceSYixian Liu 		event.event = IB_EVENT_CQ_ERR;
498f295e4ceSYixian Liu 		ibcq->event_handler(&event, ibcq->cq_context);
499f295e4ceSYixian Liu 	}
500f295e4ceSYixian Liu 
501cc9e5a84SWeihang Li 	if (refcount_dec_and_test(&hr_cq->refcount))
502f295e4ceSYixian Liu 		complete(&hr_cq->free);
5039a443537Soulijun }
5049a443537Soulijun 
hns_roce_init_cq_table(struct hns_roce_dev * hr_dev)5051bbd4380SYangyang Li void hns_roce_init_cq_table(struct hns_roce_dev *hr_dev)
5069a443537Soulijun {
5079a443537Soulijun 	struct hns_roce_cq_table *cq_table = &hr_dev->cq_table;
5081bbd4380SYangyang Li 	unsigned int reserved_from_bot;
5091bbd4380SYangyang Li 	unsigned int i;
5109a443537Soulijun 
5111bbd4380SYangyang Li 	mutex_init(&cq_table->bank_mutex);
51227e19f45SMatthew Wilcox 	xa_init(&cq_table->array);
5139a443537Soulijun 
5141bbd4380SYangyang Li 	reserved_from_bot = hr_dev->caps.reserved_cqs;
5151bbd4380SYangyang Li 
5161bbd4380SYangyang Li 	for (i = 0; i < reserved_from_bot; i++) {
5171bbd4380SYangyang Li 		cq_table->bank[get_cq_bankid(i)].inuse++;
5181bbd4380SYangyang Li 		cq_table->bank[get_cq_bankid(i)].min++;
5191bbd4380SYangyang Li 	}
5201bbd4380SYangyang Li 
5211bbd4380SYangyang Li 	for (i = 0; i < HNS_ROCE_CQ_BANK_NUM; i++) {
5221bbd4380SYangyang Li 		ida_init(&cq_table->bank[i].ida);
5231bbd4380SYangyang Li 		cq_table->bank[i].max = hr_dev->caps.num_cqs /
5241bbd4380SYangyang Li 					HNS_ROCE_CQ_BANK_NUM - 1;
5251bbd4380SYangyang Li 	}
5269a443537Soulijun }
5279a443537Soulijun 
hns_roce_cleanup_cq_table(struct hns_roce_dev * hr_dev)5289a443537Soulijun void hns_roce_cleanup_cq_table(struct hns_roce_dev *hr_dev)
5299a443537Soulijun {
5301bbd4380SYangyang Li 	int i;
5311bbd4380SYangyang Li 
5321bbd4380SYangyang Li 	for (i = 0; i < HNS_ROCE_CQ_BANK_NUM; i++)
5331bbd4380SYangyang Li 		ida_destroy(&hr_dev->cq_table.bank[i].ida);
5349a443537Soulijun }
535