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