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], 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, 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, sizeof(*gid)))
240 			break;
241 	}
242 	if (index == sgid_tbl->max) {
243 		dev_warn(&res->pdev->dev, "GID not found in the SGID table\n");
244 		return 0;
245 	}
246 	/* Remove GID from the SGID table */
247 	if (update) {
248 		struct cmdq_delete_gid req;
249 		struct creq_delete_gid_resp resp;
250 		u16 cmd_flags = 0;
251 		int rc;
252 
253 		RCFW_CMD_PREP(req, DELETE_GID, cmd_flags);
254 		if (sgid_tbl->hw_id[index] == 0xFFFF) {
255 			dev_err(&res->pdev->dev,
256 				"GID entry contains an invalid HW id\n");
257 			return -EINVAL;
258 		}
259 		req.gid_index = cpu_to_le16(sgid_tbl->hw_id[index]);
260 		rc = bnxt_qplib_rcfw_send_message(rcfw, (void *)&req,
261 						  (void *)&resp, NULL, 0);
262 		if (rc)
263 			return rc;
264 	}
265 	memcpy(&sgid_tbl->tbl[index], &bnxt_qplib_gid_zero,
266 	       sizeof(bnxt_qplib_gid_zero));
267 	sgid_tbl->vlan[index] = 0;
268 	sgid_tbl->active--;
269 	dev_dbg(&res->pdev->dev,
270 		"SGID deleted hw_id[0x%x] = 0x%x active = 0x%x\n",
271 		 index, sgid_tbl->hw_id[index], sgid_tbl->active);
272 	sgid_tbl->hw_id[index] = (u16)-1;
273 
274 	/* unlock */
275 	return 0;
276 }
277 
278 int bnxt_qplib_add_sgid(struct bnxt_qplib_sgid_tbl *sgid_tbl,
279 			struct bnxt_qplib_gid *gid, u8 *smac, u16 vlan_id,
280 			bool update, u32 *index)
281 {
282 	struct bnxt_qplib_res *res = to_bnxt_qplib(sgid_tbl,
283 						   struct bnxt_qplib_res,
284 						   sgid_tbl);
285 	struct bnxt_qplib_rcfw *rcfw = res->rcfw;
286 	int i, free_idx;
287 
288 	if (!sgid_tbl) {
289 		dev_err(&res->pdev->dev, "SGID table not allocated\n");
290 		return -EINVAL;
291 	}
292 	/* Do we need a sgid_lock here? */
293 	if (sgid_tbl->active == sgid_tbl->max) {
294 		dev_err(&res->pdev->dev, "SGID table is full\n");
295 		return -ENOMEM;
296 	}
297 	free_idx = sgid_tbl->max;
298 	for (i = 0; i < sgid_tbl->max; i++) {
299 		if (!memcmp(&sgid_tbl->tbl[i], gid, sizeof(*gid))) {
300 			dev_dbg(&res->pdev->dev,
301 				"SGID entry already exist in entry %d!\n", i);
302 			*index = i;
303 			return -EALREADY;
304 		} else if (!memcmp(&sgid_tbl->tbl[i], &bnxt_qplib_gid_zero,
305 				   sizeof(bnxt_qplib_gid_zero)) &&
306 			   free_idx == sgid_tbl->max) {
307 			free_idx = i;
308 		}
309 	}
310 	if (free_idx == sgid_tbl->max) {
311 		dev_err(&res->pdev->dev,
312 			"SGID table is FULL but count is not MAX??\n");
313 		return -ENOMEM;
314 	}
315 	if (update) {
316 		struct cmdq_add_gid req;
317 		struct creq_add_gid_resp resp;
318 		u16 cmd_flags = 0;
319 		int rc;
320 
321 		RCFW_CMD_PREP(req, ADD_GID, cmd_flags);
322 
323 		req.gid[0] = cpu_to_be32(((u32 *)gid->data)[3]);
324 		req.gid[1] = cpu_to_be32(((u32 *)gid->data)[2]);
325 		req.gid[2] = cpu_to_be32(((u32 *)gid->data)[1]);
326 		req.gid[3] = cpu_to_be32(((u32 *)gid->data)[0]);
327 		/*
328 		 * driver should ensure that all RoCE traffic is always VLAN
329 		 * tagged if RoCE traffic is running on non-zero VLAN ID or
330 		 * RoCE traffic is running on non-zero Priority.
331 		 */
332 		if ((vlan_id != 0xFFFF) || res->prio) {
333 			if (vlan_id != 0xFFFF)
334 				req.vlan = cpu_to_le16
335 				(vlan_id & CMDQ_ADD_GID_VLAN_VLAN_ID_MASK);
336 			req.vlan |= cpu_to_le16
337 					(CMDQ_ADD_GID_VLAN_TPID_TPID_8100 |
338 					 CMDQ_ADD_GID_VLAN_VLAN_EN);
339 		}
340 
341 		/* MAC in network format */
342 		req.src_mac[0] = cpu_to_be16(((u16 *)smac)[0]);
343 		req.src_mac[1] = cpu_to_be16(((u16 *)smac)[1]);
344 		req.src_mac[2] = cpu_to_be16(((u16 *)smac)[2]);
345 
346 		rc = bnxt_qplib_rcfw_send_message(rcfw, (void *)&req,
347 						  (void *)&resp, NULL, 0);
348 		if (rc)
349 			return rc;
350 		sgid_tbl->hw_id[free_idx] = le32_to_cpu(resp.xid);
351 	}
352 	/* Add GID to the sgid_tbl */
353 	memcpy(&sgid_tbl->tbl[free_idx], gid, sizeof(*gid));
354 	sgid_tbl->active++;
355 	if (vlan_id != 0xFFFF)
356 		sgid_tbl->vlan[free_idx] = 1;
357 
358 	dev_dbg(&res->pdev->dev,
359 		"SGID added hw_id[0x%x] = 0x%x active = 0x%x\n",
360 		 free_idx, sgid_tbl->hw_id[free_idx], sgid_tbl->active);
361 
362 	*index = free_idx;
363 	/* unlock */
364 	return 0;
365 }
366 
367 int bnxt_qplib_update_sgid(struct bnxt_qplib_sgid_tbl *sgid_tbl,
368 			   struct bnxt_qplib_gid *gid, u16 gid_idx,
369 			   u8 *smac)
370 {
371 	struct bnxt_qplib_res *res = to_bnxt_qplib(sgid_tbl,
372 						   struct bnxt_qplib_res,
373 						   sgid_tbl);
374 	struct bnxt_qplib_rcfw *rcfw = res->rcfw;
375 	struct creq_modify_gid_resp resp;
376 	struct cmdq_modify_gid req;
377 	int rc;
378 	u16 cmd_flags = 0;
379 
380 	RCFW_CMD_PREP(req, MODIFY_GID, cmd_flags);
381 
382 	req.gid[0] = cpu_to_be32(((u32 *)gid->data)[3]);
383 	req.gid[1] = cpu_to_be32(((u32 *)gid->data)[2]);
384 	req.gid[2] = cpu_to_be32(((u32 *)gid->data)[1]);
385 	req.gid[3] = cpu_to_be32(((u32 *)gid->data)[0]);
386 	if (res->prio) {
387 		req.vlan |= cpu_to_le16
388 			(CMDQ_ADD_GID_VLAN_TPID_TPID_8100 |
389 			 CMDQ_ADD_GID_VLAN_VLAN_EN);
390 	}
391 
392 	/* MAC in network format */
393 	req.src_mac[0] = cpu_to_be16(((u16 *)smac)[0]);
394 	req.src_mac[1] = cpu_to_be16(((u16 *)smac)[1]);
395 	req.src_mac[2] = cpu_to_be16(((u16 *)smac)[2]);
396 
397 	req.gid_index = cpu_to_le16(gid_idx);
398 
399 	rc = bnxt_qplib_rcfw_send_message(rcfw, (void *)&req,
400 					  (void *)&resp, NULL, 0);
401 	return rc;
402 }
403 
404 /* pkeys */
405 int bnxt_qplib_get_pkey(struct bnxt_qplib_res *res,
406 			struct bnxt_qplib_pkey_tbl *pkey_tbl, u16 index,
407 			u16 *pkey)
408 {
409 	if (index == 0xFFFF) {
410 		*pkey = 0xFFFF;
411 		return 0;
412 	}
413 	if (index >= pkey_tbl->max) {
414 		dev_err(&res->pdev->dev,
415 			"Index %d exceeded PKEY table max (%d)\n",
416 			index, pkey_tbl->max);
417 		return -EINVAL;
418 	}
419 	memcpy(pkey, &pkey_tbl->tbl[index], sizeof(*pkey));
420 	return 0;
421 }
422 
423 int bnxt_qplib_del_pkey(struct bnxt_qplib_res *res,
424 			struct bnxt_qplib_pkey_tbl *pkey_tbl, u16 *pkey,
425 			bool update)
426 {
427 	int i, rc = 0;
428 
429 	if (!pkey_tbl) {
430 		dev_err(&res->pdev->dev, "PKEY table not allocated\n");
431 		return -EINVAL;
432 	}
433 
434 	/* Do we need a pkey_lock here? */
435 	if (!pkey_tbl->active) {
436 		dev_err(&res->pdev->dev, "PKEY table has no active entries\n");
437 		return -ENOMEM;
438 	}
439 	for (i = 0; i < pkey_tbl->max; i++) {
440 		if (!memcmp(&pkey_tbl->tbl[i], pkey, sizeof(*pkey)))
441 			break;
442 	}
443 	if (i == pkey_tbl->max) {
444 		dev_err(&res->pdev->dev,
445 			"PKEY 0x%04x not found in the pkey table\n", *pkey);
446 		return -ENOMEM;
447 	}
448 	memset(&pkey_tbl->tbl[i], 0, sizeof(*pkey));
449 	pkey_tbl->active--;
450 
451 	/* unlock */
452 	return rc;
453 }
454 
455 int bnxt_qplib_add_pkey(struct bnxt_qplib_res *res,
456 			struct bnxt_qplib_pkey_tbl *pkey_tbl, u16 *pkey,
457 			bool update)
458 {
459 	int i, free_idx, rc = 0;
460 
461 	if (!pkey_tbl) {
462 		dev_err(&res->pdev->dev, "PKEY table not allocated\n");
463 		return -EINVAL;
464 	}
465 
466 	/* Do we need a pkey_lock here? */
467 	if (pkey_tbl->active == pkey_tbl->max) {
468 		dev_err(&res->pdev->dev, "PKEY table is full\n");
469 		return -ENOMEM;
470 	}
471 	free_idx = pkey_tbl->max;
472 	for (i = 0; i < pkey_tbl->max; i++) {
473 		if (!memcmp(&pkey_tbl->tbl[i], pkey, sizeof(*pkey)))
474 			return -EALREADY;
475 		else if (!pkey_tbl->tbl[i] && free_idx == pkey_tbl->max)
476 			free_idx = i;
477 	}
478 	if (free_idx == pkey_tbl->max) {
479 		dev_err(&res->pdev->dev,
480 			"PKEY table is FULL but count is not MAX??\n");
481 		return -ENOMEM;
482 	}
483 	/* Add PKEY to the pkey_tbl */
484 	memcpy(&pkey_tbl->tbl[free_idx], pkey, sizeof(*pkey));
485 	pkey_tbl->active++;
486 
487 	/* unlock */
488 	return rc;
489 }
490 
491 /* AH */
492 int bnxt_qplib_create_ah(struct bnxt_qplib_res *res, struct bnxt_qplib_ah *ah,
493 			 bool block)
494 {
495 	struct bnxt_qplib_rcfw *rcfw = res->rcfw;
496 	struct cmdq_create_ah req;
497 	struct creq_create_ah_resp resp;
498 	u16 cmd_flags = 0;
499 	u32 temp32[4];
500 	u16 temp16[3];
501 	int rc;
502 
503 	RCFW_CMD_PREP(req, CREATE_AH, cmd_flags);
504 
505 	memcpy(temp32, ah->dgid.data, sizeof(struct bnxt_qplib_gid));
506 	req.dgid[0] = cpu_to_le32(temp32[0]);
507 	req.dgid[1] = cpu_to_le32(temp32[1]);
508 	req.dgid[2] = cpu_to_le32(temp32[2]);
509 	req.dgid[3] = cpu_to_le32(temp32[3]);
510 
511 	req.type = ah->nw_type;
512 	req.hop_limit = ah->hop_limit;
513 	req.sgid_index = cpu_to_le16(res->sgid_tbl.hw_id[ah->sgid_index]);
514 	req.dest_vlan_id_flow_label = cpu_to_le32((ah->flow_label &
515 					CMDQ_CREATE_AH_FLOW_LABEL_MASK) |
516 					CMDQ_CREATE_AH_DEST_VLAN_ID_MASK);
517 	req.pd_id = cpu_to_le32(ah->pd->id);
518 	req.traffic_class = ah->traffic_class;
519 
520 	/* MAC in network format */
521 	memcpy(temp16, ah->dmac, 6);
522 	req.dest_mac[0] = cpu_to_le16(temp16[0]);
523 	req.dest_mac[1] = cpu_to_le16(temp16[1]);
524 	req.dest_mac[2] = cpu_to_le16(temp16[2]);
525 
526 	rc = bnxt_qplib_rcfw_send_message(rcfw, (void *)&req, (void *)&resp,
527 					  NULL, block);
528 	if (rc)
529 		return rc;
530 
531 	ah->id = le32_to_cpu(resp.xid);
532 	return 0;
533 }
534 
535 void bnxt_qplib_destroy_ah(struct bnxt_qplib_res *res, struct bnxt_qplib_ah *ah,
536 			   bool block)
537 {
538 	struct bnxt_qplib_rcfw *rcfw = res->rcfw;
539 	struct cmdq_destroy_ah req;
540 	struct creq_destroy_ah_resp resp;
541 	u16 cmd_flags = 0;
542 
543 	/* Clean up the AH table in the device */
544 	RCFW_CMD_PREP(req, DESTROY_AH, cmd_flags);
545 
546 	req.ah_cid = cpu_to_le32(ah->id);
547 
548 	bnxt_qplib_rcfw_send_message(rcfw, (void *)&req, (void *)&resp, NULL,
549 				     block);
550 }
551 
552 /* MRW */
553 int bnxt_qplib_free_mrw(struct bnxt_qplib_res *res, struct bnxt_qplib_mrw *mrw)
554 {
555 	struct bnxt_qplib_rcfw *rcfw = res->rcfw;
556 	struct cmdq_deallocate_key req;
557 	struct creq_deallocate_key_resp resp;
558 	u16 cmd_flags = 0;
559 	int rc;
560 
561 	if (mrw->lkey == 0xFFFFFFFF) {
562 		dev_info(&res->pdev->dev, "SP: Free a reserved lkey MRW\n");
563 		return 0;
564 	}
565 
566 	RCFW_CMD_PREP(req, DEALLOCATE_KEY, cmd_flags);
567 
568 	req.mrw_flags = mrw->type;
569 
570 	if ((mrw->type == CMDQ_ALLOCATE_MRW_MRW_FLAGS_MW_TYPE1)  ||
571 	    (mrw->type == CMDQ_ALLOCATE_MRW_MRW_FLAGS_MW_TYPE2A) ||
572 	    (mrw->type == CMDQ_ALLOCATE_MRW_MRW_FLAGS_MW_TYPE2B))
573 		req.key = cpu_to_le32(mrw->rkey);
574 	else
575 		req.key = cpu_to_le32(mrw->lkey);
576 
577 	rc = bnxt_qplib_rcfw_send_message(rcfw, (void *)&req, (void *)&resp,
578 					  NULL, 0);
579 	if (rc)
580 		return rc;
581 
582 	/* Free the qplib's MRW memory */
583 	if (mrw->hwq.max_elements)
584 		bnxt_qplib_free_hwq(res->pdev, &mrw->hwq);
585 
586 	return 0;
587 }
588 
589 int bnxt_qplib_alloc_mrw(struct bnxt_qplib_res *res, struct bnxt_qplib_mrw *mrw)
590 {
591 	struct bnxt_qplib_rcfw *rcfw = res->rcfw;
592 	struct cmdq_allocate_mrw req;
593 	struct creq_allocate_mrw_resp resp;
594 	u16 cmd_flags = 0;
595 	unsigned long tmp;
596 	int rc;
597 
598 	RCFW_CMD_PREP(req, ALLOCATE_MRW, cmd_flags);
599 
600 	req.pd_id = cpu_to_le32(mrw->pd->id);
601 	req.mrw_flags = mrw->type;
602 	if ((mrw->type == CMDQ_ALLOCATE_MRW_MRW_FLAGS_PMR &&
603 	     mrw->flags & BNXT_QPLIB_FR_PMR) ||
604 	    mrw->type == CMDQ_ALLOCATE_MRW_MRW_FLAGS_MW_TYPE2A ||
605 	    mrw->type == CMDQ_ALLOCATE_MRW_MRW_FLAGS_MW_TYPE2B)
606 		req.access = CMDQ_ALLOCATE_MRW_ACCESS_CONSUMER_OWNED_KEY;
607 	tmp = (unsigned long)mrw;
608 	req.mrw_handle = cpu_to_le64(tmp);
609 
610 	rc = bnxt_qplib_rcfw_send_message(rcfw, (void *)&req,
611 					  (void *)&resp, NULL, 0);
612 	if (rc)
613 		return rc;
614 
615 	if ((mrw->type == CMDQ_ALLOCATE_MRW_MRW_FLAGS_MW_TYPE1)  ||
616 	    (mrw->type == CMDQ_ALLOCATE_MRW_MRW_FLAGS_MW_TYPE2A) ||
617 	    (mrw->type == CMDQ_ALLOCATE_MRW_MRW_FLAGS_MW_TYPE2B))
618 		mrw->rkey = le32_to_cpu(resp.xid);
619 	else
620 		mrw->lkey = le32_to_cpu(resp.xid);
621 	return 0;
622 }
623 
624 int bnxt_qplib_dereg_mrw(struct bnxt_qplib_res *res, struct bnxt_qplib_mrw *mrw,
625 			 bool block)
626 {
627 	struct bnxt_qplib_rcfw *rcfw = res->rcfw;
628 	struct cmdq_deregister_mr req;
629 	struct creq_deregister_mr_resp resp;
630 	u16 cmd_flags = 0;
631 	int rc;
632 
633 	RCFW_CMD_PREP(req, DEREGISTER_MR, cmd_flags);
634 
635 	req.lkey = cpu_to_le32(mrw->lkey);
636 	rc = bnxt_qplib_rcfw_send_message(rcfw, (void *)&req,
637 					  (void *)&resp, NULL, block);
638 	if (rc)
639 		return rc;
640 
641 	/* Free the qplib's MR memory */
642 	if (mrw->hwq.max_elements) {
643 		mrw->va = 0;
644 		mrw->total_size = 0;
645 		bnxt_qplib_free_hwq(res->pdev, &mrw->hwq);
646 	}
647 
648 	return 0;
649 }
650 
651 int bnxt_qplib_reg_mr(struct bnxt_qplib_res *res, struct bnxt_qplib_mrw *mr,
652 		      u64 *pbl_tbl, int num_pbls, bool block, u32 buf_pg_size)
653 {
654 	struct bnxt_qplib_rcfw *rcfw = res->rcfw;
655 	struct cmdq_register_mr req;
656 	struct creq_register_mr_resp resp;
657 	u16 cmd_flags = 0, level;
658 	int pg_ptrs, pages, i, rc;
659 	dma_addr_t **pbl_ptr;
660 	u32 pg_size;
661 
662 	if (num_pbls) {
663 		/* Allocate memory for the non-leaf pages to store buf ptrs.
664 		 * Non-leaf pages always uses system PAGE_SIZE
665 		 */
666 		pg_ptrs = roundup_pow_of_two(num_pbls);
667 		pages = pg_ptrs >> MAX_PBL_LVL_1_PGS_SHIFT;
668 		if (!pages)
669 			pages++;
670 
671 		if (pages > MAX_PBL_LVL_1_PGS) {
672 			dev_err(&res->pdev->dev,
673 				"SP: Reg MR pages requested (0x%x) exceeded max (0x%x)\n",
674 				pages, MAX_PBL_LVL_1_PGS);
675 			return -ENOMEM;
676 		}
677 		/* Free the hwq if it already exist, must be a rereg */
678 		if (mr->hwq.max_elements)
679 			bnxt_qplib_free_hwq(res->pdev, &mr->hwq);
680 
681 		mr->hwq.max_elements = pages;
682 		/* Use system PAGE_SIZE */
683 		rc = bnxt_qplib_alloc_init_hwq(res->pdev, &mr->hwq, NULL,
684 					       &mr->hwq.max_elements,
685 					       PAGE_SIZE, 0, PAGE_SIZE,
686 					       HWQ_TYPE_CTX);
687 		if (rc) {
688 			dev_err(&res->pdev->dev,
689 				"SP: Reg MR memory allocation failed\n");
690 			return -ENOMEM;
691 		}
692 		/* Write to the hwq */
693 		pbl_ptr = (dma_addr_t **)mr->hwq.pbl_ptr;
694 		for (i = 0; i < num_pbls; i++)
695 			pbl_ptr[PTR_PG(i)][PTR_IDX(i)] =
696 				(pbl_tbl[i] & PAGE_MASK) | PTU_PTE_VALID;
697 	}
698 
699 	RCFW_CMD_PREP(req, REGISTER_MR, cmd_flags);
700 
701 	/* Configure the request */
702 	if (mr->hwq.level == PBL_LVL_MAX) {
703 		/* No PBL provided, just use system PAGE_SIZE */
704 		level = 0;
705 		req.pbl = 0;
706 		pg_size = PAGE_SIZE;
707 	} else {
708 		level = mr->hwq.level + 1;
709 		req.pbl = cpu_to_le64(mr->hwq.pbl[PBL_LVL_0].pg_map_arr[0]);
710 	}
711 	pg_size = buf_pg_size ? buf_pg_size : PAGE_SIZE;
712 	req.log2_pg_size_lvl = (level << CMDQ_REGISTER_MR_LVL_SFT) |
713 			       ((ilog2(pg_size) <<
714 				 CMDQ_REGISTER_MR_LOG2_PG_SIZE_SFT) &
715 				CMDQ_REGISTER_MR_LOG2_PG_SIZE_MASK);
716 	req.log2_pbl_pg_size = cpu_to_le16(((ilog2(PAGE_SIZE) <<
717 				 CMDQ_REGISTER_MR_LOG2_PBL_PG_SIZE_SFT) &
718 				CMDQ_REGISTER_MR_LOG2_PBL_PG_SIZE_MASK));
719 	req.access = (mr->flags & 0xFFFF);
720 	req.va = cpu_to_le64(mr->va);
721 	req.key = cpu_to_le32(mr->lkey);
722 	req.mr_size = cpu_to_le64(mr->total_size);
723 
724 	rc = bnxt_qplib_rcfw_send_message(rcfw, (void *)&req,
725 					  (void *)&resp, NULL, block);
726 	if (rc)
727 		goto fail;
728 
729 	return 0;
730 
731 fail:
732 	if (mr->hwq.max_elements)
733 		bnxt_qplib_free_hwq(res->pdev, &mr->hwq);
734 	return rc;
735 }
736 
737 int bnxt_qplib_alloc_fast_reg_page_list(struct bnxt_qplib_res *res,
738 					struct bnxt_qplib_frpl *frpl,
739 					int max_pg_ptrs)
740 {
741 	int pg_ptrs, pages, rc;
742 
743 	/* Re-calculate the max to fit the HWQ allocation model */
744 	pg_ptrs = roundup_pow_of_two(max_pg_ptrs);
745 	pages = pg_ptrs >> MAX_PBL_LVL_1_PGS_SHIFT;
746 	if (!pages)
747 		pages++;
748 
749 	if (pages > MAX_PBL_LVL_1_PGS)
750 		return -ENOMEM;
751 
752 	frpl->hwq.max_elements = pages;
753 	rc = bnxt_qplib_alloc_init_hwq(res->pdev, &frpl->hwq, NULL,
754 				       &frpl->hwq.max_elements, PAGE_SIZE, 0,
755 				       PAGE_SIZE, HWQ_TYPE_CTX);
756 	if (!rc)
757 		frpl->max_pg_ptrs = pg_ptrs;
758 
759 	return rc;
760 }
761 
762 int bnxt_qplib_free_fast_reg_page_list(struct bnxt_qplib_res *res,
763 				       struct bnxt_qplib_frpl *frpl)
764 {
765 	bnxt_qplib_free_hwq(res->pdev, &frpl->hwq);
766 	return 0;
767 }
768 
769 int bnxt_qplib_map_tc2cos(struct bnxt_qplib_res *res, u16 *cids)
770 {
771 	struct bnxt_qplib_rcfw *rcfw = res->rcfw;
772 	struct cmdq_map_tc_to_cos req;
773 	struct creq_map_tc_to_cos_resp resp;
774 	u16 cmd_flags = 0;
775 
776 	RCFW_CMD_PREP(req, MAP_TC_TO_COS, cmd_flags);
777 	req.cos0 = cpu_to_le16(cids[0]);
778 	req.cos1 = cpu_to_le16(cids[1]);
779 
780 	return bnxt_qplib_rcfw_send_message(rcfw, (void *)&req, (void *)&resp,
781 						NULL, 0);
782 }
783 
784 int bnxt_qplib_get_roce_stats(struct bnxt_qplib_rcfw *rcfw,
785 			      struct bnxt_qplib_roce_stats *stats)
786 {
787 	struct cmdq_query_roce_stats req;
788 	struct creq_query_roce_stats_resp resp;
789 	struct bnxt_qplib_rcfw_sbuf *sbuf;
790 	struct creq_query_roce_stats_resp_sb *sb;
791 	u16 cmd_flags = 0;
792 	int rc = 0;
793 
794 	RCFW_CMD_PREP(req, QUERY_ROCE_STATS, cmd_flags);
795 
796 	sbuf = bnxt_qplib_rcfw_alloc_sbuf(rcfw, sizeof(*sb));
797 	if (!sbuf) {
798 		dev_err(&rcfw->pdev->dev,
799 			"SP: QUERY_ROCE_STATS alloc side buffer failed\n");
800 		return -ENOMEM;
801 	}
802 
803 	sb = sbuf->sb;
804 	req.resp_size = sizeof(*sb) / BNXT_QPLIB_CMDQE_UNITS;
805 	rc = bnxt_qplib_rcfw_send_message(rcfw, (void *)&req, (void *)&resp,
806 					  (void *)sbuf, 0);
807 	if (rc)
808 		goto bail;
809 	/* Extract the context from the side buffer */
810 	stats->to_retransmits = le64_to_cpu(sb->to_retransmits);
811 	stats->seq_err_naks_rcvd = le64_to_cpu(sb->seq_err_naks_rcvd);
812 	stats->max_retry_exceeded = le64_to_cpu(sb->max_retry_exceeded);
813 	stats->rnr_naks_rcvd = le64_to_cpu(sb->rnr_naks_rcvd);
814 	stats->missing_resp = le64_to_cpu(sb->missing_resp);
815 	stats->unrecoverable_err = le64_to_cpu(sb->unrecoverable_err);
816 	stats->bad_resp_err = le64_to_cpu(sb->bad_resp_err);
817 	stats->local_qp_op_err = le64_to_cpu(sb->local_qp_op_err);
818 	stats->local_protection_err = le64_to_cpu(sb->local_protection_err);
819 	stats->mem_mgmt_op_err = le64_to_cpu(sb->mem_mgmt_op_err);
820 	stats->remote_invalid_req_err = le64_to_cpu(sb->remote_invalid_req_err);
821 	stats->remote_access_err = le64_to_cpu(sb->remote_access_err);
822 	stats->remote_op_err = le64_to_cpu(sb->remote_op_err);
823 	stats->dup_req = le64_to_cpu(sb->dup_req);
824 	stats->res_exceed_max = le64_to_cpu(sb->res_exceed_max);
825 	stats->res_length_mismatch = le64_to_cpu(sb->res_length_mismatch);
826 	stats->res_exceeds_wqe = le64_to_cpu(sb->res_exceeds_wqe);
827 	stats->res_opcode_err = le64_to_cpu(sb->res_opcode_err);
828 	stats->res_rx_invalid_rkey = le64_to_cpu(sb->res_rx_invalid_rkey);
829 	stats->res_rx_domain_err = le64_to_cpu(sb->res_rx_domain_err);
830 	stats->res_rx_no_perm = le64_to_cpu(sb->res_rx_no_perm);
831 	stats->res_rx_range_err = le64_to_cpu(sb->res_rx_range_err);
832 	stats->res_tx_invalid_rkey = le64_to_cpu(sb->res_tx_invalid_rkey);
833 	stats->res_tx_domain_err = le64_to_cpu(sb->res_tx_domain_err);
834 	stats->res_tx_no_perm = le64_to_cpu(sb->res_tx_no_perm);
835 	stats->res_tx_range_err = le64_to_cpu(sb->res_tx_range_err);
836 	stats->res_irrq_oflow = le64_to_cpu(sb->res_irrq_oflow);
837 	stats->res_unsup_opcode = le64_to_cpu(sb->res_unsup_opcode);
838 	stats->res_unaligned_atomic = le64_to_cpu(sb->res_unaligned_atomic);
839 	stats->res_rem_inv_err = le64_to_cpu(sb->res_rem_inv_err);
840 	stats->res_mem_error = le64_to_cpu(sb->res_mem_error);
841 	stats->res_srq_err = le64_to_cpu(sb->res_srq_err);
842 	stats->res_cmp_err = le64_to_cpu(sb->res_cmp_err);
843 	stats->res_invalid_dup_rkey = le64_to_cpu(sb->res_invalid_dup_rkey);
844 	stats->res_wqe_format_err = le64_to_cpu(sb->res_wqe_format_err);
845 	stats->res_cq_load_err = le64_to_cpu(sb->res_cq_load_err);
846 	stats->res_srq_load_err = le64_to_cpu(sb->res_srq_load_err);
847 	stats->res_tx_pci_err = le64_to_cpu(sb->res_tx_pci_err);
848 	stats->res_rx_pci_err = le64_to_cpu(sb->res_rx_pci_err);
849 	if (!rcfw->init_oos_stats) {
850 		rcfw->oos_prev = le64_to_cpu(sb->res_oos_drop_count);
851 		rcfw->init_oos_stats = 1;
852 	} else {
853 		stats->res_oos_drop_count +=
854 				(le64_to_cpu(sb->res_oos_drop_count) -
855 				 rcfw->oos_prev) & BNXT_QPLIB_OOS_COUNT_MASK;
856 		rcfw->oos_prev = le64_to_cpu(sb->res_oos_drop_count);
857 	}
858 
859 bail:
860 	bnxt_qplib_rcfw_free_sbuf(rcfw, sbuf);
861 	return rc;
862 }
863