1 /*
2  * Broadcom NetXtreme-E RoCE driver.
3  *
4  * Copyright (c) 2016 - 2017, Broadcom. All rights reserved.  The term
5  * Broadcom refers to Broadcom Limited and/or its subsidiaries.
6  *
7  * This software is available to you under a choice of one of two
8  * licenses.  You may choose to be licensed under the terms of the GNU
9  * General Public License (GPL) Version 2, available from the file
10  * COPYING in the main directory of this source tree, or the
11  * BSD license below:
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  *
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in
21  *    the documentation and/or other materials provided with the
22  *    distribution.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
25  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
26  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
28  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
31  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
33  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
34  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  *
36  * Description: Slow Path Operators
37  */
38 
39 #define dev_fmt(fmt) "QPLIB: " fmt
40 
41 #include <linux/interrupt.h>
42 #include <linux/spinlock.h>
43 #include <linux/sched.h>
44 #include <linux/pci.h>
45 
46 #include "roce_hsi.h"
47 
48 #include "qplib_res.h"
49 #include "qplib_rcfw.h"
50 #include "qplib_sp.h"
51 
52 const struct bnxt_qplib_gid bnxt_qplib_gid_zero = {{ 0, 0, 0, 0, 0, 0, 0, 0,
53 						     0, 0, 0, 0, 0, 0, 0, 0 } };
54 
55 /* Device */
56 
57 static bool bnxt_qplib_is_atomic_cap(struct bnxt_qplib_rcfw *rcfw)
58 {
59 	u16 pcie_ctl2 = 0;
60 
61 	if (!bnxt_qplib_is_chip_gen_p5(rcfw->res->cctx))
62 		return false;
63 
64 	pcie_capability_read_word(rcfw->pdev, PCI_EXP_DEVCTL2, &pcie_ctl2);
65 	return (pcie_ctl2 & PCI_EXP_DEVCTL2_ATOMIC_REQ);
66 }
67 
68 static void bnxt_qplib_query_version(struct bnxt_qplib_rcfw *rcfw,
69 				     char *fw_ver)
70 {
71 	struct cmdq_query_version req;
72 	struct creq_query_version_resp resp;
73 	u16 cmd_flags = 0;
74 	int rc = 0;
75 
76 	RCFW_CMD_PREP(req, QUERY_VERSION, cmd_flags);
77 
78 	rc = bnxt_qplib_rcfw_send_message(rcfw, (void *)&req,
79 					  (void *)&resp, NULL, 0);
80 	if (rc)
81 		return;
82 	fw_ver[0] = resp.fw_maj;
83 	fw_ver[1] = resp.fw_minor;
84 	fw_ver[2] = resp.fw_bld;
85 	fw_ver[3] = resp.fw_rsvd;
86 }
87 
88 int bnxt_qplib_get_dev_attr(struct bnxt_qplib_rcfw *rcfw,
89 			    struct bnxt_qplib_dev_attr *attr, bool vf)
90 {
91 	struct cmdq_query_func req;
92 	struct creq_query_func_resp resp;
93 	struct bnxt_qplib_rcfw_sbuf *sbuf;
94 	struct creq_query_func_resp_sb *sb;
95 	u16 cmd_flags = 0;
96 	u32 temp;
97 	u8 *tqm_alloc;
98 	int i, rc = 0;
99 
100 	RCFW_CMD_PREP(req, QUERY_FUNC, cmd_flags);
101 
102 	sbuf = bnxt_qplib_rcfw_alloc_sbuf(rcfw, sizeof(*sb));
103 	if (!sbuf) {
104 		dev_err(&rcfw->pdev->dev,
105 			"SP: QUERY_FUNC alloc side buffer failed\n");
106 		return -ENOMEM;
107 	}
108 
109 	sb = sbuf->sb;
110 	req.resp_size = sizeof(*sb) / BNXT_QPLIB_CMDQE_UNITS;
111 	rc = bnxt_qplib_rcfw_send_message(rcfw, (void *)&req, (void *)&resp,
112 					  (void *)sbuf, 0);
113 	if (rc)
114 		goto bail;
115 
116 	/* Extract the context from the side buffer */
117 	attr->max_qp = le32_to_cpu(sb->max_qp);
118 	/* max_qp value reported by FW for PF doesn't include the QP1 for PF */
119 	if (!vf)
120 		attr->max_qp += 1;
121 	attr->max_qp_rd_atom =
122 		sb->max_qp_rd_atom > BNXT_QPLIB_MAX_OUT_RD_ATOM ?
123 		BNXT_QPLIB_MAX_OUT_RD_ATOM : sb->max_qp_rd_atom;
124 	attr->max_qp_init_rd_atom =
125 		sb->max_qp_init_rd_atom > BNXT_QPLIB_MAX_OUT_RD_ATOM ?
126 		BNXT_QPLIB_MAX_OUT_RD_ATOM : sb->max_qp_init_rd_atom;
127 	attr->max_qp_wqes = le16_to_cpu(sb->max_qp_wr);
128 	/*
129 	 * 128 WQEs needs to be reserved for the HW (8916). Prevent
130 	 * reporting the max number
131 	 */
132 	attr->max_qp_wqes -= BNXT_QPLIB_RESERVED_QP_WRS + 1;
133 	attr->max_qp_sges = bnxt_qplib_is_chip_gen_p5(rcfw->res->cctx) ?
134 			    6 : sb->max_sge;
135 	attr->max_cq = le32_to_cpu(sb->max_cq);
136 	attr->max_cq_wqes = le32_to_cpu(sb->max_cqe);
137 	attr->max_cq_sges = attr->max_qp_sges;
138 	attr->max_mr = le32_to_cpu(sb->max_mr);
139 	attr->max_mw = le32_to_cpu(sb->max_mw);
140 
141 	attr->max_mr_size = le64_to_cpu(sb->max_mr_size);
142 	attr->max_pd = 64 * 1024;
143 	attr->max_raw_ethy_qp = le32_to_cpu(sb->max_raw_eth_qp);
144 	attr->max_ah = le32_to_cpu(sb->max_ah);
145 
146 	attr->max_srq = le16_to_cpu(sb->max_srq);
147 	attr->max_srq_wqes = le32_to_cpu(sb->max_srq_wr) - 1;
148 	attr->max_srq_sges = sb->max_srq_sge;
149 	attr->max_pkey = le32_to_cpu(sb->max_pkeys);
150 	/*
151 	 * Some versions of FW reports more than 0xFFFF.
152 	 * Restrict it for now to 0xFFFF to avoid
153 	 * reporting trucated value
154 	 */
155 	if (attr->max_pkey > 0xFFFF) {
156 		/* ib_port_attr::pkey_tbl_len is u16 */
157 		attr->max_pkey = 0xFFFF;
158 	}
159 
160 	attr->max_inline_data = le32_to_cpu(sb->max_inline_data);
161 	attr->l2_db_size = (sb->l2_db_space_size + 1) *
162 			    (0x01 << RCFW_DBR_BASE_PAGE_SHIFT);
163 	attr->max_sgid = BNXT_QPLIB_NUM_GIDS_SUPPORTED;
164 	attr->dev_cap_flags = le16_to_cpu(sb->dev_cap_flags);
165 
166 	bnxt_qplib_query_version(rcfw, attr->fw_ver);
167 
168 	for (i = 0; i < MAX_TQM_ALLOC_REQ / 4; i++) {
169 		temp = le32_to_cpu(sb->tqm_alloc_reqs[i]);
170 		tqm_alloc = (u8 *)&temp;
171 		attr->tqm_alloc_reqs[i * 4] = *tqm_alloc;
172 		attr->tqm_alloc_reqs[i * 4 + 1] = *(++tqm_alloc);
173 		attr->tqm_alloc_reqs[i * 4 + 2] = *(++tqm_alloc);
174 		attr->tqm_alloc_reqs[i * 4 + 3] = *(++tqm_alloc);
175 	}
176 
177 	attr->is_atomic = bnxt_qplib_is_atomic_cap(rcfw);
178 bail:
179 	bnxt_qplib_rcfw_free_sbuf(rcfw, sbuf);
180 	return rc;
181 }
182 
183 int bnxt_qplib_set_func_resources(struct bnxt_qplib_res *res,
184 				  struct bnxt_qplib_rcfw *rcfw,
185 				  struct bnxt_qplib_ctx *ctx)
186 {
187 	struct cmdq_set_func_resources req;
188 	struct creq_set_func_resources_resp resp;
189 	u16 cmd_flags = 0;
190 	int rc = 0;
191 
192 	RCFW_CMD_PREP(req, SET_FUNC_RESOURCES, cmd_flags);
193 
194 	req.number_of_qp = cpu_to_le32(ctx->qpc_count);
195 	req.number_of_mrw = cpu_to_le32(ctx->mrw_count);
196 	req.number_of_srq =  cpu_to_le32(ctx->srqc_count);
197 	req.number_of_cq = cpu_to_le32(ctx->cq_count);
198 
199 	req.max_qp_per_vf = cpu_to_le32(ctx->vf_res.max_qp_per_vf);
200 	req.max_mrw_per_vf = cpu_to_le32(ctx->vf_res.max_mrw_per_vf);
201 	req.max_srq_per_vf = cpu_to_le32(ctx->vf_res.max_srq_per_vf);
202 	req.max_cq_per_vf = cpu_to_le32(ctx->vf_res.max_cq_per_vf);
203 	req.max_gid_per_vf = cpu_to_le32(ctx->vf_res.max_gid_per_vf);
204 
205 	rc = bnxt_qplib_rcfw_send_message(rcfw, (void *)&req,
206 					  (void *)&resp,
207 					  NULL, 0);
208 	if (rc) {
209 		dev_err(&res->pdev->dev, "Failed to set function resources\n");
210 	}
211 	return rc;
212 }
213 
214 /* SGID */
215 int bnxt_qplib_get_sgid(struct bnxt_qplib_res *res,
216 			struct bnxt_qplib_sgid_tbl *sgid_tbl, int index,
217 			struct bnxt_qplib_gid *gid)
218 {
219 	if (index >= sgid_tbl->max) {
220 		dev_err(&res->pdev->dev,
221 			"Index %d exceeded SGID table max (%d)\n",
222 			index, sgid_tbl->max);
223 		return -EINVAL;
224 	}
225 	memcpy(gid, &sgid_tbl->tbl[index].gid, sizeof(*gid));
226 	return 0;
227 }
228 
229 int bnxt_qplib_del_sgid(struct bnxt_qplib_sgid_tbl *sgid_tbl,
230 			struct bnxt_qplib_gid *gid, u16 vlan_id, bool update)
231 {
232 	struct bnxt_qplib_res *res = to_bnxt_qplib(sgid_tbl,
233 						   struct bnxt_qplib_res,
234 						   sgid_tbl);
235 	struct bnxt_qplib_rcfw *rcfw = res->rcfw;
236 	int index;
237 
238 	if (!sgid_tbl) {
239 		dev_err(&res->pdev->dev, "SGID table not allocated\n");
240 		return -EINVAL;
241 	}
242 	/* Do we need a sgid_lock here? */
243 	if (!sgid_tbl->active) {
244 		dev_err(&res->pdev->dev, "SGID table has no active entries\n");
245 		return -ENOMEM;
246 	}
247 	for (index = 0; index < sgid_tbl->max; index++) {
248 		if (!memcmp(&sgid_tbl->tbl[index].gid, gid, sizeof(*gid)) &&
249 		    vlan_id == sgid_tbl->tbl[index].vlan_id)
250 			break;
251 	}
252 	if (index == sgid_tbl->max) {
253 		dev_warn(&res->pdev->dev, "GID not found in the SGID table\n");
254 		return 0;
255 	}
256 	/* Remove GID from the SGID table */
257 	if (update) {
258 		struct cmdq_delete_gid req;
259 		struct creq_delete_gid_resp resp;
260 		u16 cmd_flags = 0;
261 		int rc;
262 
263 		RCFW_CMD_PREP(req, DELETE_GID, cmd_flags);
264 		if (sgid_tbl->hw_id[index] == 0xFFFF) {
265 			dev_err(&res->pdev->dev,
266 				"GID entry contains an invalid HW id\n");
267 			return -EINVAL;
268 		}
269 		req.gid_index = cpu_to_le16(sgid_tbl->hw_id[index]);
270 		rc = bnxt_qplib_rcfw_send_message(rcfw, (void *)&req,
271 						  (void *)&resp, NULL, 0);
272 		if (rc)
273 			return rc;
274 	}
275 	memcpy(&sgid_tbl->tbl[index].gid, &bnxt_qplib_gid_zero,
276 	       sizeof(bnxt_qplib_gid_zero));
277 	sgid_tbl->tbl[index].vlan_id = 0xFFFF;
278 	sgid_tbl->vlan[index] = 0;
279 	sgid_tbl->active--;
280 	dev_dbg(&res->pdev->dev,
281 		"SGID deleted hw_id[0x%x] = 0x%x active = 0x%x\n",
282 		 index, sgid_tbl->hw_id[index], sgid_tbl->active);
283 	sgid_tbl->hw_id[index] = (u16)-1;
284 
285 	/* unlock */
286 	return 0;
287 }
288 
289 int bnxt_qplib_add_sgid(struct bnxt_qplib_sgid_tbl *sgid_tbl,
290 			struct bnxt_qplib_gid *gid, const u8 *smac,
291 			u16 vlan_id, bool update, u32 *index)
292 {
293 	struct bnxt_qplib_res *res = to_bnxt_qplib(sgid_tbl,
294 						   struct bnxt_qplib_res,
295 						   sgid_tbl);
296 	struct bnxt_qplib_rcfw *rcfw = res->rcfw;
297 	int i, free_idx;
298 
299 	if (!sgid_tbl) {
300 		dev_err(&res->pdev->dev, "SGID table not allocated\n");
301 		return -EINVAL;
302 	}
303 	/* Do we need a sgid_lock here? */
304 	if (sgid_tbl->active == sgid_tbl->max) {
305 		dev_err(&res->pdev->dev, "SGID table is full\n");
306 		return -ENOMEM;
307 	}
308 	free_idx = sgid_tbl->max;
309 	for (i = 0; i < sgid_tbl->max; i++) {
310 		if (!memcmp(&sgid_tbl->tbl[i], gid, sizeof(*gid)) &&
311 		    sgid_tbl->tbl[i].vlan_id == vlan_id) {
312 			dev_dbg(&res->pdev->dev,
313 				"SGID entry already exist in entry %d!\n", i);
314 			*index = i;
315 			return -EALREADY;
316 		} else if (!memcmp(&sgid_tbl->tbl[i], &bnxt_qplib_gid_zero,
317 				   sizeof(bnxt_qplib_gid_zero)) &&
318 			   free_idx == sgid_tbl->max) {
319 			free_idx = i;
320 		}
321 	}
322 	if (free_idx == sgid_tbl->max) {
323 		dev_err(&res->pdev->dev,
324 			"SGID table is FULL but count is not MAX??\n");
325 		return -ENOMEM;
326 	}
327 	if (update) {
328 		struct cmdq_add_gid req;
329 		struct creq_add_gid_resp resp;
330 		u16 cmd_flags = 0;
331 		int rc;
332 
333 		RCFW_CMD_PREP(req, ADD_GID, cmd_flags);
334 
335 		req.gid[0] = cpu_to_be32(((u32 *)gid->data)[3]);
336 		req.gid[1] = cpu_to_be32(((u32 *)gid->data)[2]);
337 		req.gid[2] = cpu_to_be32(((u32 *)gid->data)[1]);
338 		req.gid[3] = cpu_to_be32(((u32 *)gid->data)[0]);
339 		/*
340 		 * driver should ensure that all RoCE traffic is always VLAN
341 		 * tagged if RoCE traffic is running on non-zero VLAN ID or
342 		 * RoCE traffic is running on non-zero Priority.
343 		 */
344 		if ((vlan_id != 0xFFFF) || res->prio) {
345 			if (vlan_id != 0xFFFF)
346 				req.vlan = cpu_to_le16
347 				(vlan_id & CMDQ_ADD_GID_VLAN_VLAN_ID_MASK);
348 			req.vlan |= cpu_to_le16
349 					(CMDQ_ADD_GID_VLAN_TPID_TPID_8100 |
350 					 CMDQ_ADD_GID_VLAN_VLAN_EN);
351 		}
352 
353 		/* MAC in network format */
354 		req.src_mac[0] = cpu_to_be16(((u16 *)smac)[0]);
355 		req.src_mac[1] = cpu_to_be16(((u16 *)smac)[1]);
356 		req.src_mac[2] = cpu_to_be16(((u16 *)smac)[2]);
357 
358 		rc = bnxt_qplib_rcfw_send_message(rcfw, (void *)&req,
359 						  (void *)&resp, NULL, 0);
360 		if (rc)
361 			return rc;
362 		sgid_tbl->hw_id[free_idx] = le32_to_cpu(resp.xid);
363 	}
364 	/* Add GID to the sgid_tbl */
365 	memcpy(&sgid_tbl->tbl[free_idx], gid, sizeof(*gid));
366 	sgid_tbl->tbl[free_idx].vlan_id = vlan_id;
367 	sgid_tbl->active++;
368 	if (vlan_id != 0xFFFF)
369 		sgid_tbl->vlan[free_idx] = 1;
370 
371 	dev_dbg(&res->pdev->dev,
372 		"SGID added hw_id[0x%x] = 0x%x active = 0x%x\n",
373 		 free_idx, sgid_tbl->hw_id[free_idx], sgid_tbl->active);
374 
375 	*index = free_idx;
376 	/* unlock */
377 	return 0;
378 }
379 
380 int bnxt_qplib_update_sgid(struct bnxt_qplib_sgid_tbl *sgid_tbl,
381 			   struct bnxt_qplib_gid *gid, u16 gid_idx,
382 			   const u8 *smac)
383 {
384 	struct bnxt_qplib_res *res = to_bnxt_qplib(sgid_tbl,
385 						   struct bnxt_qplib_res,
386 						   sgid_tbl);
387 	struct bnxt_qplib_rcfw *rcfw = res->rcfw;
388 	struct creq_modify_gid_resp resp;
389 	struct cmdq_modify_gid req;
390 	int rc;
391 	u16 cmd_flags = 0;
392 
393 	RCFW_CMD_PREP(req, MODIFY_GID, cmd_flags);
394 
395 	req.gid[0] = cpu_to_be32(((u32 *)gid->data)[3]);
396 	req.gid[1] = cpu_to_be32(((u32 *)gid->data)[2]);
397 	req.gid[2] = cpu_to_be32(((u32 *)gid->data)[1]);
398 	req.gid[3] = cpu_to_be32(((u32 *)gid->data)[0]);
399 	if (res->prio) {
400 		req.vlan |= cpu_to_le16
401 			(CMDQ_ADD_GID_VLAN_TPID_TPID_8100 |
402 			 CMDQ_ADD_GID_VLAN_VLAN_EN);
403 	}
404 
405 	/* MAC in network format */
406 	req.src_mac[0] = cpu_to_be16(((u16 *)smac)[0]);
407 	req.src_mac[1] = cpu_to_be16(((u16 *)smac)[1]);
408 	req.src_mac[2] = cpu_to_be16(((u16 *)smac)[2]);
409 
410 	req.gid_index = cpu_to_le16(gid_idx);
411 
412 	rc = bnxt_qplib_rcfw_send_message(rcfw, (void *)&req,
413 					  (void *)&resp, NULL, 0);
414 	return rc;
415 }
416 
417 /* pkeys */
418 int bnxt_qplib_get_pkey(struct bnxt_qplib_res *res,
419 			struct bnxt_qplib_pkey_tbl *pkey_tbl, u16 index,
420 			u16 *pkey)
421 {
422 	if (index == 0xFFFF) {
423 		*pkey = 0xFFFF;
424 		return 0;
425 	}
426 	if (index >= pkey_tbl->max) {
427 		dev_err(&res->pdev->dev,
428 			"Index %d exceeded PKEY table max (%d)\n",
429 			index, pkey_tbl->max);
430 		return -EINVAL;
431 	}
432 	memcpy(pkey, &pkey_tbl->tbl[index], sizeof(*pkey));
433 	return 0;
434 }
435 
436 int bnxt_qplib_del_pkey(struct bnxt_qplib_res *res,
437 			struct bnxt_qplib_pkey_tbl *pkey_tbl, u16 *pkey,
438 			bool update)
439 {
440 	int i, rc = 0;
441 
442 	if (!pkey_tbl) {
443 		dev_err(&res->pdev->dev, "PKEY table not allocated\n");
444 		return -EINVAL;
445 	}
446 
447 	/* Do we need a pkey_lock here? */
448 	if (!pkey_tbl->active) {
449 		dev_err(&res->pdev->dev, "PKEY table has no active entries\n");
450 		return -ENOMEM;
451 	}
452 	for (i = 0; i < pkey_tbl->max; i++) {
453 		if (!memcmp(&pkey_tbl->tbl[i], pkey, sizeof(*pkey)))
454 			break;
455 	}
456 	if (i == pkey_tbl->max) {
457 		dev_err(&res->pdev->dev,
458 			"PKEY 0x%04x not found in the pkey table\n", *pkey);
459 		return -ENOMEM;
460 	}
461 	memset(&pkey_tbl->tbl[i], 0, sizeof(*pkey));
462 	pkey_tbl->active--;
463 
464 	/* unlock */
465 	return rc;
466 }
467 
468 int bnxt_qplib_add_pkey(struct bnxt_qplib_res *res,
469 			struct bnxt_qplib_pkey_tbl *pkey_tbl, u16 *pkey,
470 			bool update)
471 {
472 	int i, free_idx, rc = 0;
473 
474 	if (!pkey_tbl) {
475 		dev_err(&res->pdev->dev, "PKEY table not allocated\n");
476 		return -EINVAL;
477 	}
478 
479 	/* Do we need a pkey_lock here? */
480 	if (pkey_tbl->active == pkey_tbl->max) {
481 		dev_err(&res->pdev->dev, "PKEY table is full\n");
482 		return -ENOMEM;
483 	}
484 	free_idx = pkey_tbl->max;
485 	for (i = 0; i < pkey_tbl->max; i++) {
486 		if (!memcmp(&pkey_tbl->tbl[i], pkey, sizeof(*pkey)))
487 			return -EALREADY;
488 		else if (!pkey_tbl->tbl[i] && free_idx == pkey_tbl->max)
489 			free_idx = i;
490 	}
491 	if (free_idx == pkey_tbl->max) {
492 		dev_err(&res->pdev->dev,
493 			"PKEY table is FULL but count is not MAX??\n");
494 		return -ENOMEM;
495 	}
496 	/* Add PKEY to the pkey_tbl */
497 	memcpy(&pkey_tbl->tbl[free_idx], pkey, sizeof(*pkey));
498 	pkey_tbl->active++;
499 
500 	/* unlock */
501 	return rc;
502 }
503 
504 /* AH */
505 int bnxt_qplib_create_ah(struct bnxt_qplib_res *res, struct bnxt_qplib_ah *ah,
506 			 bool block)
507 {
508 	struct bnxt_qplib_rcfw *rcfw = res->rcfw;
509 	struct cmdq_create_ah req;
510 	struct creq_create_ah_resp resp;
511 	u16 cmd_flags = 0;
512 	u32 temp32[4];
513 	u16 temp16[3];
514 	int rc;
515 
516 	RCFW_CMD_PREP(req, CREATE_AH, cmd_flags);
517 
518 	memcpy(temp32, ah->dgid.data, sizeof(struct bnxt_qplib_gid));
519 	req.dgid[0] = cpu_to_le32(temp32[0]);
520 	req.dgid[1] = cpu_to_le32(temp32[1]);
521 	req.dgid[2] = cpu_to_le32(temp32[2]);
522 	req.dgid[3] = cpu_to_le32(temp32[3]);
523 
524 	req.type = ah->nw_type;
525 	req.hop_limit = ah->hop_limit;
526 	req.sgid_index = cpu_to_le16(res->sgid_tbl.hw_id[ah->sgid_index]);
527 	req.dest_vlan_id_flow_label = cpu_to_le32((ah->flow_label &
528 					CMDQ_CREATE_AH_FLOW_LABEL_MASK) |
529 					CMDQ_CREATE_AH_DEST_VLAN_ID_MASK);
530 	req.pd_id = cpu_to_le32(ah->pd->id);
531 	req.traffic_class = ah->traffic_class;
532 
533 	/* MAC in network format */
534 	memcpy(temp16, ah->dmac, 6);
535 	req.dest_mac[0] = cpu_to_le16(temp16[0]);
536 	req.dest_mac[1] = cpu_to_le16(temp16[1]);
537 	req.dest_mac[2] = cpu_to_le16(temp16[2]);
538 
539 	rc = bnxt_qplib_rcfw_send_message(rcfw, (void *)&req, (void *)&resp,
540 					  NULL, block);
541 	if (rc)
542 		return rc;
543 
544 	ah->id = le32_to_cpu(resp.xid);
545 	return 0;
546 }
547 
548 void bnxt_qplib_destroy_ah(struct bnxt_qplib_res *res, struct bnxt_qplib_ah *ah,
549 			   bool block)
550 {
551 	struct bnxt_qplib_rcfw *rcfw = res->rcfw;
552 	struct cmdq_destroy_ah req;
553 	struct creq_destroy_ah_resp resp;
554 	u16 cmd_flags = 0;
555 
556 	/* Clean up the AH table in the device */
557 	RCFW_CMD_PREP(req, DESTROY_AH, cmd_flags);
558 
559 	req.ah_cid = cpu_to_le32(ah->id);
560 
561 	bnxt_qplib_rcfw_send_message(rcfw, (void *)&req, (void *)&resp, NULL,
562 				     block);
563 }
564 
565 /* MRW */
566 int bnxt_qplib_free_mrw(struct bnxt_qplib_res *res, struct bnxt_qplib_mrw *mrw)
567 {
568 	struct bnxt_qplib_rcfw *rcfw = res->rcfw;
569 	struct cmdq_deallocate_key req;
570 	struct creq_deallocate_key_resp resp;
571 	u16 cmd_flags = 0;
572 	int rc;
573 
574 	if (mrw->lkey == 0xFFFFFFFF) {
575 		dev_info(&res->pdev->dev, "SP: Free a reserved lkey MRW\n");
576 		return 0;
577 	}
578 
579 	RCFW_CMD_PREP(req, DEALLOCATE_KEY, cmd_flags);
580 
581 	req.mrw_flags = mrw->type;
582 
583 	if ((mrw->type == CMDQ_ALLOCATE_MRW_MRW_FLAGS_MW_TYPE1)  ||
584 	    (mrw->type == CMDQ_ALLOCATE_MRW_MRW_FLAGS_MW_TYPE2A) ||
585 	    (mrw->type == CMDQ_ALLOCATE_MRW_MRW_FLAGS_MW_TYPE2B))
586 		req.key = cpu_to_le32(mrw->rkey);
587 	else
588 		req.key = cpu_to_le32(mrw->lkey);
589 
590 	rc = bnxt_qplib_rcfw_send_message(rcfw, (void *)&req, (void *)&resp,
591 					  NULL, 0);
592 	if (rc)
593 		return rc;
594 
595 	/* Free the qplib's MRW memory */
596 	if (mrw->hwq.max_elements)
597 		bnxt_qplib_free_hwq(res, &mrw->hwq);
598 
599 	return 0;
600 }
601 
602 int bnxt_qplib_alloc_mrw(struct bnxt_qplib_res *res, struct bnxt_qplib_mrw *mrw)
603 {
604 	struct bnxt_qplib_rcfw *rcfw = res->rcfw;
605 	struct cmdq_allocate_mrw req;
606 	struct creq_allocate_mrw_resp resp;
607 	u16 cmd_flags = 0;
608 	unsigned long tmp;
609 	int rc;
610 
611 	RCFW_CMD_PREP(req, ALLOCATE_MRW, cmd_flags);
612 
613 	req.pd_id = cpu_to_le32(mrw->pd->id);
614 	req.mrw_flags = mrw->type;
615 	if ((mrw->type == CMDQ_ALLOCATE_MRW_MRW_FLAGS_PMR &&
616 	     mrw->flags & BNXT_QPLIB_FR_PMR) ||
617 	    mrw->type == CMDQ_ALLOCATE_MRW_MRW_FLAGS_MW_TYPE2A ||
618 	    mrw->type == CMDQ_ALLOCATE_MRW_MRW_FLAGS_MW_TYPE2B)
619 		req.access = CMDQ_ALLOCATE_MRW_ACCESS_CONSUMER_OWNED_KEY;
620 	tmp = (unsigned long)mrw;
621 	req.mrw_handle = cpu_to_le64(tmp);
622 
623 	rc = bnxt_qplib_rcfw_send_message(rcfw, (void *)&req,
624 					  (void *)&resp, NULL, 0);
625 	if (rc)
626 		return rc;
627 
628 	if ((mrw->type == CMDQ_ALLOCATE_MRW_MRW_FLAGS_MW_TYPE1)  ||
629 	    (mrw->type == CMDQ_ALLOCATE_MRW_MRW_FLAGS_MW_TYPE2A) ||
630 	    (mrw->type == CMDQ_ALLOCATE_MRW_MRW_FLAGS_MW_TYPE2B))
631 		mrw->rkey = le32_to_cpu(resp.xid);
632 	else
633 		mrw->lkey = le32_to_cpu(resp.xid);
634 	return 0;
635 }
636 
637 int bnxt_qplib_dereg_mrw(struct bnxt_qplib_res *res, struct bnxt_qplib_mrw *mrw,
638 			 bool block)
639 {
640 	struct bnxt_qplib_rcfw *rcfw = res->rcfw;
641 	struct cmdq_deregister_mr req;
642 	struct creq_deregister_mr_resp resp;
643 	u16 cmd_flags = 0;
644 	int rc;
645 
646 	RCFW_CMD_PREP(req, DEREGISTER_MR, cmd_flags);
647 
648 	req.lkey = cpu_to_le32(mrw->lkey);
649 	rc = bnxt_qplib_rcfw_send_message(rcfw, (void *)&req,
650 					  (void *)&resp, NULL, block);
651 	if (rc)
652 		return rc;
653 
654 	/* Free the qplib's MR memory */
655 	if (mrw->hwq.max_elements) {
656 		mrw->va = 0;
657 		mrw->total_size = 0;
658 		bnxt_qplib_free_hwq(res, &mrw->hwq);
659 	}
660 
661 	return 0;
662 }
663 
664 int bnxt_qplib_reg_mr(struct bnxt_qplib_res *res, struct bnxt_qplib_mrw *mr,
665 		      struct ib_umem *umem, int num_pbls, u32 buf_pg_size)
666 {
667 	struct bnxt_qplib_rcfw *rcfw = res->rcfw;
668 	struct bnxt_qplib_hwq_attr hwq_attr = {};
669 	struct bnxt_qplib_sg_info sginfo = {};
670 	struct creq_register_mr_resp resp;
671 	struct cmdq_register_mr req;
672 	u16 cmd_flags = 0, level;
673 	int pages, rc;
674 	u32 pg_size;
675 
676 	if (num_pbls) {
677 		pages = roundup_pow_of_two(num_pbls);
678 		/* Allocate memory for the non-leaf pages to store buf ptrs.
679 		 * Non-leaf pages always uses system PAGE_SIZE
680 		 */
681 		/* Free the hwq if it already exist, must be a rereg */
682 		if (mr->hwq.max_elements)
683 			bnxt_qplib_free_hwq(res, &mr->hwq);
684 		/* Use system PAGE_SIZE */
685 		hwq_attr.res = res;
686 		hwq_attr.depth = pages;
687 		hwq_attr.stride = buf_pg_size;
688 		hwq_attr.type = HWQ_TYPE_MR;
689 		hwq_attr.sginfo = &sginfo;
690 		hwq_attr.sginfo->umem = umem;
691 		hwq_attr.sginfo->npages = pages;
692 		hwq_attr.sginfo->pgsize = PAGE_SIZE;
693 		hwq_attr.sginfo->pgshft = PAGE_SHIFT;
694 		rc = bnxt_qplib_alloc_init_hwq(&mr->hwq, &hwq_attr);
695 		if (rc) {
696 			dev_err(&res->pdev->dev,
697 				"SP: Reg MR memory allocation failed\n");
698 			return -ENOMEM;
699 		}
700 	}
701 
702 	RCFW_CMD_PREP(req, REGISTER_MR, cmd_flags);
703 
704 	/* Configure the request */
705 	if (mr->hwq.level == PBL_LVL_MAX) {
706 		/* No PBL provided, just use system PAGE_SIZE */
707 		level = 0;
708 		req.pbl = 0;
709 		pg_size = PAGE_SIZE;
710 	} else {
711 		level = mr->hwq.level;
712 		req.pbl = cpu_to_le64(mr->hwq.pbl[PBL_LVL_0].pg_map_arr[0]);
713 	}
714 	pg_size = buf_pg_size ? buf_pg_size : PAGE_SIZE;
715 	req.log2_pg_size_lvl = (level << CMDQ_REGISTER_MR_LVL_SFT) |
716 			       ((ilog2(pg_size) <<
717 				 CMDQ_REGISTER_MR_LOG2_PG_SIZE_SFT) &
718 				CMDQ_REGISTER_MR_LOG2_PG_SIZE_MASK);
719 	req.log2_pbl_pg_size = cpu_to_le16(((ilog2(PAGE_SIZE) <<
720 				 CMDQ_REGISTER_MR_LOG2_PBL_PG_SIZE_SFT) &
721 				CMDQ_REGISTER_MR_LOG2_PBL_PG_SIZE_MASK));
722 	req.access = (mr->flags & 0xFFFF);
723 	req.va = cpu_to_le64(mr->va);
724 	req.key = cpu_to_le32(mr->lkey);
725 	req.mr_size = cpu_to_le64(mr->total_size);
726 
727 	rc = bnxt_qplib_rcfw_send_message(rcfw, (void *)&req,
728 					  (void *)&resp, NULL, false);
729 	if (rc)
730 		goto fail;
731 
732 	return 0;
733 
734 fail:
735 	if (mr->hwq.max_elements)
736 		bnxt_qplib_free_hwq(res, &mr->hwq);
737 	return rc;
738 }
739 
740 int bnxt_qplib_alloc_fast_reg_page_list(struct bnxt_qplib_res *res,
741 					struct bnxt_qplib_frpl *frpl,
742 					int max_pg_ptrs)
743 {
744 	struct bnxt_qplib_hwq_attr hwq_attr = {};
745 	struct bnxt_qplib_sg_info sginfo = {};
746 	int pg_ptrs, pages, rc;
747 
748 	/* Re-calculate the max to fit the HWQ allocation model */
749 	pg_ptrs = roundup_pow_of_two(max_pg_ptrs);
750 	pages = pg_ptrs >> MAX_PBL_LVL_1_PGS_SHIFT;
751 	if (!pages)
752 		pages++;
753 
754 	if (pages > MAX_PBL_LVL_1_PGS)
755 		return -ENOMEM;
756 
757 	sginfo.pgsize = PAGE_SIZE;
758 	sginfo.nopte = true;
759 
760 	hwq_attr.res = res;
761 	hwq_attr.depth = pg_ptrs;
762 	hwq_attr.stride = PAGE_SIZE;
763 	hwq_attr.sginfo = &sginfo;
764 	hwq_attr.type = HWQ_TYPE_CTX;
765 	rc = bnxt_qplib_alloc_init_hwq(&frpl->hwq, &hwq_attr);
766 	if (!rc)
767 		frpl->max_pg_ptrs = pg_ptrs;
768 
769 	return rc;
770 }
771 
772 int bnxt_qplib_free_fast_reg_page_list(struct bnxt_qplib_res *res,
773 				       struct bnxt_qplib_frpl *frpl)
774 {
775 	bnxt_qplib_free_hwq(res, &frpl->hwq);
776 	return 0;
777 }
778 
779 int bnxt_qplib_map_tc2cos(struct bnxt_qplib_res *res, u16 *cids)
780 {
781 	struct bnxt_qplib_rcfw *rcfw = res->rcfw;
782 	struct cmdq_map_tc_to_cos req;
783 	struct creq_map_tc_to_cos_resp resp;
784 	u16 cmd_flags = 0;
785 
786 	RCFW_CMD_PREP(req, MAP_TC_TO_COS, cmd_flags);
787 	req.cos0 = cpu_to_le16(cids[0]);
788 	req.cos1 = cpu_to_le16(cids[1]);
789 
790 	return bnxt_qplib_rcfw_send_message(rcfw, (void *)&req, (void *)&resp,
791 						NULL, 0);
792 }
793 
794 int bnxt_qplib_get_roce_stats(struct bnxt_qplib_rcfw *rcfw,
795 			      struct bnxt_qplib_roce_stats *stats)
796 {
797 	struct cmdq_query_roce_stats req;
798 	struct creq_query_roce_stats_resp resp;
799 	struct bnxt_qplib_rcfw_sbuf *sbuf;
800 	struct creq_query_roce_stats_resp_sb *sb;
801 	u16 cmd_flags = 0;
802 	int rc = 0;
803 
804 	RCFW_CMD_PREP(req, QUERY_ROCE_STATS, cmd_flags);
805 
806 	sbuf = bnxt_qplib_rcfw_alloc_sbuf(rcfw, sizeof(*sb));
807 	if (!sbuf) {
808 		dev_err(&rcfw->pdev->dev,
809 			"SP: QUERY_ROCE_STATS alloc side buffer failed\n");
810 		return -ENOMEM;
811 	}
812 
813 	sb = sbuf->sb;
814 	req.resp_size = sizeof(*sb) / BNXT_QPLIB_CMDQE_UNITS;
815 	rc = bnxt_qplib_rcfw_send_message(rcfw, (void *)&req, (void *)&resp,
816 					  (void *)sbuf, 0);
817 	if (rc)
818 		goto bail;
819 	/* Extract the context from the side buffer */
820 	stats->to_retransmits = le64_to_cpu(sb->to_retransmits);
821 	stats->seq_err_naks_rcvd = le64_to_cpu(sb->seq_err_naks_rcvd);
822 	stats->max_retry_exceeded = le64_to_cpu(sb->max_retry_exceeded);
823 	stats->rnr_naks_rcvd = le64_to_cpu(sb->rnr_naks_rcvd);
824 	stats->missing_resp = le64_to_cpu(sb->missing_resp);
825 	stats->unrecoverable_err = le64_to_cpu(sb->unrecoverable_err);
826 	stats->bad_resp_err = le64_to_cpu(sb->bad_resp_err);
827 	stats->local_qp_op_err = le64_to_cpu(sb->local_qp_op_err);
828 	stats->local_protection_err = le64_to_cpu(sb->local_protection_err);
829 	stats->mem_mgmt_op_err = le64_to_cpu(sb->mem_mgmt_op_err);
830 	stats->remote_invalid_req_err = le64_to_cpu(sb->remote_invalid_req_err);
831 	stats->remote_access_err = le64_to_cpu(sb->remote_access_err);
832 	stats->remote_op_err = le64_to_cpu(sb->remote_op_err);
833 	stats->dup_req = le64_to_cpu(sb->dup_req);
834 	stats->res_exceed_max = le64_to_cpu(sb->res_exceed_max);
835 	stats->res_length_mismatch = le64_to_cpu(sb->res_length_mismatch);
836 	stats->res_exceeds_wqe = le64_to_cpu(sb->res_exceeds_wqe);
837 	stats->res_opcode_err = le64_to_cpu(sb->res_opcode_err);
838 	stats->res_rx_invalid_rkey = le64_to_cpu(sb->res_rx_invalid_rkey);
839 	stats->res_rx_domain_err = le64_to_cpu(sb->res_rx_domain_err);
840 	stats->res_rx_no_perm = le64_to_cpu(sb->res_rx_no_perm);
841 	stats->res_rx_range_err = le64_to_cpu(sb->res_rx_range_err);
842 	stats->res_tx_invalid_rkey = le64_to_cpu(sb->res_tx_invalid_rkey);
843 	stats->res_tx_domain_err = le64_to_cpu(sb->res_tx_domain_err);
844 	stats->res_tx_no_perm = le64_to_cpu(sb->res_tx_no_perm);
845 	stats->res_tx_range_err = le64_to_cpu(sb->res_tx_range_err);
846 	stats->res_irrq_oflow = le64_to_cpu(sb->res_irrq_oflow);
847 	stats->res_unsup_opcode = le64_to_cpu(sb->res_unsup_opcode);
848 	stats->res_unaligned_atomic = le64_to_cpu(sb->res_unaligned_atomic);
849 	stats->res_rem_inv_err = le64_to_cpu(sb->res_rem_inv_err);
850 	stats->res_mem_error = le64_to_cpu(sb->res_mem_error);
851 	stats->res_srq_err = le64_to_cpu(sb->res_srq_err);
852 	stats->res_cmp_err = le64_to_cpu(sb->res_cmp_err);
853 	stats->res_invalid_dup_rkey = le64_to_cpu(sb->res_invalid_dup_rkey);
854 	stats->res_wqe_format_err = le64_to_cpu(sb->res_wqe_format_err);
855 	stats->res_cq_load_err = le64_to_cpu(sb->res_cq_load_err);
856 	stats->res_srq_load_err = le64_to_cpu(sb->res_srq_load_err);
857 	stats->res_tx_pci_err = le64_to_cpu(sb->res_tx_pci_err);
858 	stats->res_rx_pci_err = le64_to_cpu(sb->res_rx_pci_err);
859 	if (!rcfw->init_oos_stats) {
860 		rcfw->oos_prev = le64_to_cpu(sb->res_oos_drop_count);
861 		rcfw->init_oos_stats = 1;
862 	} else {
863 		stats->res_oos_drop_count +=
864 				(le64_to_cpu(sb->res_oos_drop_count) -
865 				 rcfw->oos_prev) & BNXT_QPLIB_OOS_COUNT_MASK;
866 		rcfw->oos_prev = le64_to_cpu(sb->res_oos_drop_count);
867 	}
868 
869 bail:
870 	bnxt_qplib_rcfw_free_sbuf(rcfw, sbuf);
871 	return rc;
872 }
873 
874 int bnxt_qplib_qext_stat(struct bnxt_qplib_rcfw *rcfw, u32 fid,
875 			 struct bnxt_qplib_ext_stat *estat)
876 {
877 	struct creq_query_roce_stats_ext_resp resp = {};
878 	struct creq_query_roce_stats_ext_resp_sb *sb;
879 	struct cmdq_query_roce_stats_ext req = {};
880 	struct bnxt_qplib_rcfw_sbuf *sbuf;
881 	u16 cmd_flags = 0;
882 	int rc;
883 
884 	sbuf = bnxt_qplib_rcfw_alloc_sbuf(rcfw, sizeof(*sb));
885 	if (!sbuf) {
886 		dev_err(&rcfw->pdev->dev,
887 			"SP: QUERY_ROCE_STATS_EXT alloc sb failed");
888 		return -ENOMEM;
889 	}
890 
891 	RCFW_CMD_PREP(req, QUERY_ROCE_STATS_EXT, cmd_flags);
892 
893 	req.resp_size = ALIGN(sizeof(*sb), BNXT_QPLIB_CMDQE_UNITS);
894 	req.resp_addr = cpu_to_le64(sbuf->dma_addr);
895 	req.function_id = cpu_to_le32(fid);
896 	req.flags = cpu_to_le16(CMDQ_QUERY_ROCE_STATS_EXT_FLAGS_FUNCTION_ID);
897 
898 	rc = bnxt_qplib_rcfw_send_message(rcfw, (void *)&req,
899 					  (void *)&resp, (void *)sbuf, 0);
900 	if (rc)
901 		goto bail;
902 
903 	sb = sbuf->sb;
904 	estat->tx_atomic_req = le64_to_cpu(sb->tx_atomic_req_pkts);
905 	estat->tx_read_req = le64_to_cpu(sb->tx_read_req_pkts);
906 	estat->tx_read_res = le64_to_cpu(sb->tx_read_res_pkts);
907 	estat->tx_write_req = le64_to_cpu(sb->tx_write_req_pkts);
908 	estat->tx_send_req = le64_to_cpu(sb->tx_send_req_pkts);
909 	estat->rx_atomic_req = le64_to_cpu(sb->rx_atomic_req_pkts);
910 	estat->rx_read_req = le64_to_cpu(sb->rx_read_req_pkts);
911 	estat->rx_read_res = le64_to_cpu(sb->rx_read_res_pkts);
912 	estat->rx_write_req = le64_to_cpu(sb->rx_write_req_pkts);
913 	estat->rx_send_req = le64_to_cpu(sb->rx_send_req_pkts);
914 	estat->rx_roce_good_pkts = le64_to_cpu(sb->rx_roce_good_pkts);
915 	estat->rx_roce_good_bytes = le64_to_cpu(sb->rx_roce_good_bytes);
916 	estat->rx_out_of_buffer = le64_to_cpu(sb->rx_out_of_buffer_pkts);
917 	estat->rx_out_of_sequence = le64_to_cpu(sb->rx_out_of_sequence_pkts);
918 
919 bail:
920 	bnxt_qplib_rcfw_free_sbuf(rcfw, sbuf);
921 	return rc;
922 }
923