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