]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/infiniband/hw/bnxt_re/qplib_sp.c
RDMA/bnxt_re: Fixing the Control path command and response handling
[karo-tx-linux.git] / drivers / infiniband / hw / bnxt_re / qplib_sp.c
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 int bnxt_qplib_get_dev_attr(struct bnxt_qplib_rcfw *rcfw,
55                             struct bnxt_qplib_dev_attr *attr)
56 {
57         struct cmdq_query_func req;
58         struct creq_query_func_resp resp;
59         struct bnxt_qplib_rcfw_sbuf *sbuf;
60         struct creq_query_func_resp_sb *sb;
61         u16 cmd_flags = 0;
62         u32 temp;
63         u8 *tqm_alloc;
64         int i, rc = 0;
65
66         RCFW_CMD_PREP(req, QUERY_FUNC, cmd_flags);
67
68         sbuf = bnxt_qplib_rcfw_alloc_sbuf(rcfw, sizeof(*sb));
69         if (!sbuf) {
70                 dev_err(&rcfw->pdev->dev,
71                         "QPLIB: SP: QUERY_FUNC alloc side buffer failed");
72                 return -ENOMEM;
73         }
74
75         sb = sbuf->sb;
76         req.resp_size = sizeof(*sb) / BNXT_QPLIB_CMDQE_UNITS;
77         rc = bnxt_qplib_rcfw_send_message(rcfw, (void *)&req, (void *)&resp,
78                                           (void *)sbuf, 0);
79         if (rc)
80                 goto bail;
81
82         /* Extract the context from the side buffer */
83         attr->max_qp = le32_to_cpu(sb->max_qp);
84         attr->max_qp_rd_atom =
85                 sb->max_qp_rd_atom > BNXT_QPLIB_MAX_OUT_RD_ATOM ?
86                 BNXT_QPLIB_MAX_OUT_RD_ATOM : sb->max_qp_rd_atom;
87         attr->max_qp_init_rd_atom =
88                 sb->max_qp_init_rd_atom > BNXT_QPLIB_MAX_OUT_RD_ATOM ?
89                 BNXT_QPLIB_MAX_OUT_RD_ATOM : sb->max_qp_init_rd_atom;
90         attr->max_qp_wqes = le16_to_cpu(sb->max_qp_wr);
91         attr->max_qp_sges = sb->max_sge;
92         attr->max_cq = le32_to_cpu(sb->max_cq);
93         attr->max_cq_wqes = le32_to_cpu(sb->max_cqe);
94         attr->max_cq_sges = attr->max_qp_sges;
95         attr->max_mr = le32_to_cpu(sb->max_mr);
96         attr->max_mw = le32_to_cpu(sb->max_mw);
97
98         attr->max_mr_size = le64_to_cpu(sb->max_mr_size);
99         attr->max_pd = 64 * 1024;
100         attr->max_raw_ethy_qp = le32_to_cpu(sb->max_raw_eth_qp);
101         attr->max_ah = le32_to_cpu(sb->max_ah);
102
103         attr->max_fmr = le32_to_cpu(sb->max_fmr);
104         attr->max_map_per_fmr = sb->max_map_per_fmr;
105
106         attr->max_srq = le16_to_cpu(sb->max_srq);
107         attr->max_srq_wqes = le32_to_cpu(sb->max_srq_wr) - 1;
108         attr->max_srq_sges = sb->max_srq_sge;
109         /* Bono only reports 1 PKEY for now, but it can support > 1 */
110         attr->max_pkey = le32_to_cpu(sb->max_pkeys);
111
112         attr->max_inline_data = le32_to_cpu(sb->max_inline_data);
113         attr->l2_db_size = (sb->l2_db_space_size + 1) * PAGE_SIZE;
114         attr->max_sgid = le32_to_cpu(sb->max_gid);
115
116         strlcpy(attr->fw_ver, "20.6.28.0", sizeof(attr->fw_ver));
117
118         for (i = 0; i < MAX_TQM_ALLOC_REQ / 4; i++) {
119                 temp = le32_to_cpu(sb->tqm_alloc_reqs[i]);
120                 tqm_alloc = (u8 *)&temp;
121                 attr->tqm_alloc_reqs[i * 4] = *tqm_alloc;
122                 attr->tqm_alloc_reqs[i * 4 + 1] = *(++tqm_alloc);
123                 attr->tqm_alloc_reqs[i * 4 + 2] = *(++tqm_alloc);
124                 attr->tqm_alloc_reqs[i * 4 + 3] = *(++tqm_alloc);
125         }
126
127 bail:
128         bnxt_qplib_rcfw_free_sbuf(rcfw, sbuf);
129         return rc;
130 }
131
132 /* SGID */
133 int bnxt_qplib_get_sgid(struct bnxt_qplib_res *res,
134                         struct bnxt_qplib_sgid_tbl *sgid_tbl, int index,
135                         struct bnxt_qplib_gid *gid)
136 {
137         if (index > sgid_tbl->max) {
138                 dev_err(&res->pdev->dev,
139                         "QPLIB: Index %d exceeded SGID table max (%d)",
140                         index, sgid_tbl->max);
141                 return -EINVAL;
142         }
143         memcpy(gid, &sgid_tbl->tbl[index], sizeof(*gid));
144         return 0;
145 }
146
147 int bnxt_qplib_del_sgid(struct bnxt_qplib_sgid_tbl *sgid_tbl,
148                         struct bnxt_qplib_gid *gid, bool update)
149 {
150         struct bnxt_qplib_res *res = to_bnxt_qplib(sgid_tbl,
151                                                    struct bnxt_qplib_res,
152                                                    sgid_tbl);
153         struct bnxt_qplib_rcfw *rcfw = res->rcfw;
154         int index;
155
156         if (!sgid_tbl) {
157                 dev_err(&res->pdev->dev, "QPLIB: SGID table not allocated");
158                 return -EINVAL;
159         }
160         /* Do we need a sgid_lock here? */
161         if (!sgid_tbl->active) {
162                 dev_err(&res->pdev->dev,
163                         "QPLIB: SGID table has no active entries");
164                 return -ENOMEM;
165         }
166         for (index = 0; index < sgid_tbl->max; index++) {
167                 if (!memcmp(&sgid_tbl->tbl[index], gid, sizeof(*gid)))
168                         break;
169         }
170         if (index == sgid_tbl->max) {
171                 dev_warn(&res->pdev->dev, "GID not found in the SGID table");
172                 return 0;
173         }
174         /* Remove GID from the SGID table */
175         if (update) {
176                 struct cmdq_delete_gid req;
177                 struct creq_delete_gid_resp resp;
178                 u16 cmd_flags = 0;
179                 int rc;
180
181                 RCFW_CMD_PREP(req, DELETE_GID, cmd_flags);
182                 if (sgid_tbl->hw_id[index] == 0xFFFF) {
183                         dev_err(&res->pdev->dev,
184                                 "QPLIB: GID entry contains an invalid HW id");
185                         return -EINVAL;
186                 }
187                 req.gid_index = cpu_to_le16(sgid_tbl->hw_id[index]);
188                 rc = bnxt_qplib_rcfw_send_message(rcfw, (void *)&req,
189                                                   (void *)&resp, NULL, 0);
190                 if (rc)
191                         return rc;
192         }
193         memcpy(&sgid_tbl->tbl[index], &bnxt_qplib_gid_zero,
194                sizeof(bnxt_qplib_gid_zero));
195         sgid_tbl->active--;
196         dev_dbg(&res->pdev->dev,
197                 "QPLIB: SGID deleted hw_id[0x%x] = 0x%x active = 0x%x",
198                  index, sgid_tbl->hw_id[index], sgid_tbl->active);
199         sgid_tbl->hw_id[index] = (u16)-1;
200
201         /* unlock */
202         return 0;
203 }
204
205 int bnxt_qplib_add_sgid(struct bnxt_qplib_sgid_tbl *sgid_tbl,
206                         struct bnxt_qplib_gid *gid, u8 *smac, u16 vlan_id,
207                         bool update, u32 *index)
208 {
209         struct bnxt_qplib_res *res = to_bnxt_qplib(sgid_tbl,
210                                                    struct bnxt_qplib_res,
211                                                    sgid_tbl);
212         struct bnxt_qplib_rcfw *rcfw = res->rcfw;
213         int i, free_idx;
214
215         if (!sgid_tbl) {
216                 dev_err(&res->pdev->dev, "QPLIB: SGID table not allocated");
217                 return -EINVAL;
218         }
219         /* Do we need a sgid_lock here? */
220         if (sgid_tbl->active == sgid_tbl->max) {
221                 dev_err(&res->pdev->dev, "QPLIB: SGID table is full");
222                 return -ENOMEM;
223         }
224         free_idx = sgid_tbl->max;
225         for (i = 0; i < sgid_tbl->max; i++) {
226                 if (!memcmp(&sgid_tbl->tbl[i], gid, sizeof(*gid))) {
227                         dev_dbg(&res->pdev->dev,
228                                 "QPLIB: SGID entry already exist in entry %d!",
229                                 i);
230                         *index = i;
231                         return -EALREADY;
232                 } else if (!memcmp(&sgid_tbl->tbl[i], &bnxt_qplib_gid_zero,
233                                    sizeof(bnxt_qplib_gid_zero)) &&
234                            free_idx == sgid_tbl->max) {
235                         free_idx = i;
236                 }
237         }
238         if (free_idx == sgid_tbl->max) {
239                 dev_err(&res->pdev->dev,
240                         "QPLIB: SGID table is FULL but count is not MAX??");
241                 return -ENOMEM;
242         }
243         if (update) {
244                 struct cmdq_add_gid req;
245                 struct creq_add_gid_resp resp;
246                 u16 cmd_flags = 0;
247                 u32 temp32[4];
248                 u16 temp16[3];
249                 int rc;
250
251                 RCFW_CMD_PREP(req, ADD_GID, cmd_flags);
252
253                 memcpy(temp32, gid->data, sizeof(struct bnxt_qplib_gid));
254                 req.gid[0] = cpu_to_be32(temp32[3]);
255                 req.gid[1] = cpu_to_be32(temp32[2]);
256                 req.gid[2] = cpu_to_be32(temp32[1]);
257                 req.gid[3] = cpu_to_be32(temp32[0]);
258                 if (vlan_id != 0xFFFF)
259                         req.vlan = cpu_to_le16((vlan_id &
260                                         CMDQ_ADD_GID_VLAN_VLAN_ID_MASK) |
261                                         CMDQ_ADD_GID_VLAN_TPID_TPID_8100 |
262                                         CMDQ_ADD_GID_VLAN_VLAN_EN);
263
264                 /* MAC in network format */
265                 memcpy(temp16, smac, 6);
266                 req.src_mac[0] = cpu_to_be16(temp16[0]);
267                 req.src_mac[1] = cpu_to_be16(temp16[1]);
268                 req.src_mac[2] = cpu_to_be16(temp16[2]);
269
270                 rc = bnxt_qplib_rcfw_send_message(rcfw, (void *)&req,
271                                                   (void *)&resp, NULL, 0);
272                 if (rc)
273                         return rc;
274                 sgid_tbl->hw_id[free_idx] = le32_to_cpu(resp.xid);
275         }
276         /* Add GID to the sgid_tbl */
277         memcpy(&sgid_tbl->tbl[free_idx], gid, sizeof(*gid));
278         sgid_tbl->active++;
279         dev_dbg(&res->pdev->dev,
280                 "QPLIB: SGID added hw_id[0x%x] = 0x%x active = 0x%x",
281                  free_idx, sgid_tbl->hw_id[free_idx], sgid_tbl->active);
282
283         *index = free_idx;
284         /* unlock */
285         return 0;
286 }
287
288 /* pkeys */
289 int bnxt_qplib_get_pkey(struct bnxt_qplib_res *res,
290                         struct bnxt_qplib_pkey_tbl *pkey_tbl, u16 index,
291                         u16 *pkey)
292 {
293         if (index == 0xFFFF) {
294                 *pkey = 0xFFFF;
295                 return 0;
296         }
297         if (index > pkey_tbl->max) {
298                 dev_err(&res->pdev->dev,
299                         "QPLIB: Index %d exceeded PKEY table max (%d)",
300                         index, pkey_tbl->max);
301                 return -EINVAL;
302         }
303         memcpy(pkey, &pkey_tbl->tbl[index], sizeof(*pkey));
304         return 0;
305 }
306
307 int bnxt_qplib_del_pkey(struct bnxt_qplib_res *res,
308                         struct bnxt_qplib_pkey_tbl *pkey_tbl, u16 *pkey,
309                         bool update)
310 {
311         int i, rc = 0;
312
313         if (!pkey_tbl) {
314                 dev_err(&res->pdev->dev, "QPLIB: PKEY table not allocated");
315                 return -EINVAL;
316         }
317
318         /* Do we need a pkey_lock here? */
319         if (!pkey_tbl->active) {
320                 dev_err(&res->pdev->dev,
321                         "QPLIB: PKEY table has no active entries");
322                 return -ENOMEM;
323         }
324         for (i = 0; i < pkey_tbl->max; i++) {
325                 if (!memcmp(&pkey_tbl->tbl[i], pkey, sizeof(*pkey)))
326                         break;
327         }
328         if (i == pkey_tbl->max) {
329                 dev_err(&res->pdev->dev,
330                         "QPLIB: PKEY 0x%04x not found in the pkey table",
331                         *pkey);
332                 return -ENOMEM;
333         }
334         memset(&pkey_tbl->tbl[i], 0, sizeof(*pkey));
335         pkey_tbl->active--;
336
337         /* unlock */
338         return rc;
339 }
340
341 int bnxt_qplib_add_pkey(struct bnxt_qplib_res *res,
342                         struct bnxt_qplib_pkey_tbl *pkey_tbl, u16 *pkey,
343                         bool update)
344 {
345         int i, free_idx, rc = 0;
346
347         if (!pkey_tbl) {
348                 dev_err(&res->pdev->dev, "QPLIB: PKEY table not allocated");
349                 return -EINVAL;
350         }
351
352         /* Do we need a pkey_lock here? */
353         if (pkey_tbl->active == pkey_tbl->max) {
354                 dev_err(&res->pdev->dev, "QPLIB: PKEY table is full");
355                 return -ENOMEM;
356         }
357         free_idx = pkey_tbl->max;
358         for (i = 0; i < pkey_tbl->max; i++) {
359                 if (!memcmp(&pkey_tbl->tbl[i], pkey, sizeof(*pkey)))
360                         return -EALREADY;
361                 else if (!pkey_tbl->tbl[i] && free_idx == pkey_tbl->max)
362                         free_idx = i;
363         }
364         if (free_idx == pkey_tbl->max) {
365                 dev_err(&res->pdev->dev,
366                         "QPLIB: PKEY table is FULL but count is not MAX??");
367                 return -ENOMEM;
368         }
369         /* Add PKEY to the pkey_tbl */
370         memcpy(&pkey_tbl->tbl[free_idx], pkey, sizeof(*pkey));
371         pkey_tbl->active++;
372
373         /* unlock */
374         return rc;
375 }
376
377 /* AH */
378 int bnxt_qplib_create_ah(struct bnxt_qplib_res *res, struct bnxt_qplib_ah *ah)
379 {
380         struct bnxt_qplib_rcfw *rcfw = res->rcfw;
381         struct cmdq_create_ah req;
382         struct creq_create_ah_resp resp;
383         u16 cmd_flags = 0;
384         u32 temp32[4];
385         u16 temp16[3];
386         int rc;
387
388         RCFW_CMD_PREP(req, CREATE_AH, cmd_flags);
389
390         memcpy(temp32, ah->dgid.data, sizeof(struct bnxt_qplib_gid));
391         req.dgid[0] = cpu_to_le32(temp32[0]);
392         req.dgid[1] = cpu_to_le32(temp32[1]);
393         req.dgid[2] = cpu_to_le32(temp32[2]);
394         req.dgid[3] = cpu_to_le32(temp32[3]);
395
396         req.type = ah->nw_type;
397         req.hop_limit = ah->hop_limit;
398         req.sgid_index = cpu_to_le16(res->sgid_tbl.hw_id[ah->sgid_index]);
399         req.dest_vlan_id_flow_label = cpu_to_le32((ah->flow_label &
400                                         CMDQ_CREATE_AH_FLOW_LABEL_MASK) |
401                                         CMDQ_CREATE_AH_DEST_VLAN_ID_MASK);
402         req.pd_id = cpu_to_le32(ah->pd->id);
403         req.traffic_class = ah->traffic_class;
404
405         /* MAC in network format */
406         memcpy(temp16, ah->dmac, 6);
407         req.dest_mac[0] = cpu_to_le16(temp16[0]);
408         req.dest_mac[1] = cpu_to_le16(temp16[1]);
409         req.dest_mac[2] = cpu_to_le16(temp16[2]);
410
411         rc = bnxt_qplib_rcfw_send_message(rcfw, (void *)&req, (void *)&resp,
412                                           NULL, 1);
413         if (rc)
414                 return rc;
415
416         ah->id = le32_to_cpu(resp.xid);
417         return 0;
418 }
419
420 int bnxt_qplib_destroy_ah(struct bnxt_qplib_res *res, struct bnxt_qplib_ah *ah)
421 {
422         struct bnxt_qplib_rcfw *rcfw = res->rcfw;
423         struct cmdq_destroy_ah req;
424         struct creq_destroy_ah_resp resp;
425         u16 cmd_flags = 0;
426         int rc;
427
428         /* Clean up the AH table in the device */
429         RCFW_CMD_PREP(req, DESTROY_AH, cmd_flags);
430
431         req.ah_cid = cpu_to_le32(ah->id);
432
433         rc = bnxt_qplib_rcfw_send_message(rcfw, (void *)&req, (void *)&resp,
434                                           NULL, 1);
435         if (rc)
436                 return rc;
437         return 0;
438 }
439
440 /* MRW */
441 int bnxt_qplib_free_mrw(struct bnxt_qplib_res *res, struct bnxt_qplib_mrw *mrw)
442 {
443         struct bnxt_qplib_rcfw *rcfw = res->rcfw;
444         struct cmdq_deallocate_key req;
445         struct creq_deallocate_key_resp resp;
446         u16 cmd_flags = 0;
447         int rc;
448
449         if (mrw->lkey == 0xFFFFFFFF) {
450                 dev_info(&res->pdev->dev,
451                          "QPLIB: SP: Free a reserved lkey MRW");
452                 return 0;
453         }
454
455         RCFW_CMD_PREP(req, DEALLOCATE_KEY, cmd_flags);
456
457         req.mrw_flags = mrw->type;
458
459         if ((mrw->type == CMDQ_ALLOCATE_MRW_MRW_FLAGS_MW_TYPE1)  ||
460             (mrw->type == CMDQ_ALLOCATE_MRW_MRW_FLAGS_MW_TYPE2A) ||
461             (mrw->type == CMDQ_ALLOCATE_MRW_MRW_FLAGS_MW_TYPE2B))
462                 req.key = cpu_to_le32(mrw->rkey);
463         else
464                 req.key = cpu_to_le32(mrw->lkey);
465
466         rc = bnxt_qplib_rcfw_send_message(rcfw, (void *)&req, (void *)&resp,
467                                           NULL, 0);
468         if (rc)
469                 return rc;
470
471         /* Free the qplib's MRW memory */
472         if (mrw->hwq.max_elements)
473                 bnxt_qplib_free_hwq(res->pdev, &mrw->hwq);
474
475         return 0;
476 }
477
478 int bnxt_qplib_alloc_mrw(struct bnxt_qplib_res *res, struct bnxt_qplib_mrw *mrw)
479 {
480         struct bnxt_qplib_rcfw *rcfw = res->rcfw;
481         struct cmdq_allocate_mrw req;
482         struct creq_allocate_mrw_resp resp;
483         u16 cmd_flags = 0;
484         unsigned long tmp;
485         int rc;
486
487         RCFW_CMD_PREP(req, ALLOCATE_MRW, cmd_flags);
488
489         req.pd_id = cpu_to_le32(mrw->pd->id);
490         req.mrw_flags = mrw->type;
491         if ((mrw->type == CMDQ_ALLOCATE_MRW_MRW_FLAGS_PMR &&
492              mrw->flags & BNXT_QPLIB_FR_PMR) ||
493             mrw->type == CMDQ_ALLOCATE_MRW_MRW_FLAGS_MW_TYPE2A ||
494             mrw->type == CMDQ_ALLOCATE_MRW_MRW_FLAGS_MW_TYPE2B)
495                 req.access = CMDQ_ALLOCATE_MRW_ACCESS_CONSUMER_OWNED_KEY;
496         tmp = (unsigned long)mrw;
497         req.mrw_handle = cpu_to_le64(tmp);
498
499         rc = bnxt_qplib_rcfw_send_message(rcfw, (void *)&req,
500                                           (void *)&resp, NULL, 0);
501         if (rc)
502                 return rc;
503
504         if ((mrw->type == CMDQ_ALLOCATE_MRW_MRW_FLAGS_MW_TYPE1)  ||
505             (mrw->type == CMDQ_ALLOCATE_MRW_MRW_FLAGS_MW_TYPE2A) ||
506             (mrw->type == CMDQ_ALLOCATE_MRW_MRW_FLAGS_MW_TYPE2B))
507                 mrw->rkey = le32_to_cpu(resp.xid);
508         else
509                 mrw->lkey = le32_to_cpu(resp.xid);
510         return 0;
511 }
512
513 int bnxt_qplib_dereg_mrw(struct bnxt_qplib_res *res, struct bnxt_qplib_mrw *mrw,
514                          bool block)
515 {
516         struct bnxt_qplib_rcfw *rcfw = res->rcfw;
517         struct cmdq_deregister_mr req;
518         struct creq_deregister_mr_resp resp;
519         u16 cmd_flags = 0;
520         int rc;
521
522         RCFW_CMD_PREP(req, DEREGISTER_MR, cmd_flags);
523
524         req.lkey = cpu_to_le32(mrw->lkey);
525         rc = bnxt_qplib_rcfw_send_message(rcfw, (void *)&req,
526                                           (void *)&resp, NULL, block);
527         if (rc)
528                 return rc;
529
530         /* Free the qplib's MR memory */
531         if (mrw->hwq.max_elements) {
532                 mrw->va = 0;
533                 mrw->total_size = 0;
534                 bnxt_qplib_free_hwq(res->pdev, &mrw->hwq);
535         }
536
537         return 0;
538 }
539
540 int bnxt_qplib_reg_mr(struct bnxt_qplib_res *res, struct bnxt_qplib_mrw *mr,
541                       u64 *pbl_tbl, int num_pbls, bool block)
542 {
543         struct bnxt_qplib_rcfw *rcfw = res->rcfw;
544         struct cmdq_register_mr req;
545         struct creq_register_mr_resp resp;
546         u16 cmd_flags = 0, level;
547         int pg_ptrs, pages, i, rc;
548         dma_addr_t **pbl_ptr;
549         u32 pg_size;
550
551         if (num_pbls) {
552                 pg_ptrs = roundup_pow_of_two(num_pbls);
553                 pages = pg_ptrs >> MAX_PBL_LVL_1_PGS_SHIFT;
554                 if (!pages)
555                         pages++;
556
557                 if (pages > MAX_PBL_LVL_1_PGS) {
558                         dev_err(&res->pdev->dev, "QPLIB: SP: Reg MR pages ");
559                         dev_err(&res->pdev->dev,
560                                 "requested (0x%x) exceeded max (0x%x)",
561                                 pages, MAX_PBL_LVL_1_PGS);
562                         return -ENOMEM;
563                 }
564                 /* Free the hwq if it already exist, must be a rereg */
565                 if (mr->hwq.max_elements)
566                         bnxt_qplib_free_hwq(res->pdev, &mr->hwq);
567
568                 mr->hwq.max_elements = pages;
569                 rc = bnxt_qplib_alloc_init_hwq(res->pdev, &mr->hwq, NULL, 0,
570                                                &mr->hwq.max_elements,
571                                                PAGE_SIZE, 0, PAGE_SIZE,
572                                                HWQ_TYPE_CTX);
573                 if (rc) {
574                         dev_err(&res->pdev->dev,
575                                 "SP: Reg MR memory allocation failed");
576                         return -ENOMEM;
577                 }
578                 /* Write to the hwq */
579                 pbl_ptr = (dma_addr_t **)mr->hwq.pbl_ptr;
580                 for (i = 0; i < num_pbls; i++)
581                         pbl_ptr[PTR_PG(i)][PTR_IDX(i)] =
582                                 (pbl_tbl[i] & PAGE_MASK) | PTU_PTE_VALID;
583         }
584
585         RCFW_CMD_PREP(req, REGISTER_MR, cmd_flags);
586
587         /* Configure the request */
588         if (mr->hwq.level == PBL_LVL_MAX) {
589                 level = 0;
590                 req.pbl = 0;
591                 pg_size = PAGE_SIZE;
592         } else {
593                 level = mr->hwq.level + 1;
594                 req.pbl = cpu_to_le64(mr->hwq.pbl[PBL_LVL_0].pg_map_arr[0]);
595                 pg_size = mr->hwq.pbl[PBL_LVL_0].pg_size;
596         }
597         req.log2_pg_size_lvl = (level << CMDQ_REGISTER_MR_LVL_SFT) |
598                                ((ilog2(pg_size) <<
599                                  CMDQ_REGISTER_MR_LOG2_PG_SIZE_SFT) &
600                                 CMDQ_REGISTER_MR_LOG2_PG_SIZE_MASK);
601         req.access = (mr->flags & 0xFFFF);
602         req.va = cpu_to_le64(mr->va);
603         req.key = cpu_to_le32(mr->lkey);
604         req.mr_size = cpu_to_le64(mr->total_size);
605
606         rc = bnxt_qplib_rcfw_send_message(rcfw, (void *)&req,
607                                           (void *)&resp, NULL, block);
608         if (rc)
609                 goto fail;
610
611         return 0;
612
613 fail:
614         if (mr->hwq.max_elements)
615                 bnxt_qplib_free_hwq(res->pdev, &mr->hwq);
616         return rc;
617 }
618
619 int bnxt_qplib_alloc_fast_reg_page_list(struct bnxt_qplib_res *res,
620                                         struct bnxt_qplib_frpl *frpl,
621                                         int max_pg_ptrs)
622 {
623         int pg_ptrs, pages, rc;
624
625         /* Re-calculate the max to fit the HWQ allocation model */
626         pg_ptrs = roundup_pow_of_two(max_pg_ptrs);
627         pages = pg_ptrs >> MAX_PBL_LVL_1_PGS_SHIFT;
628         if (!pages)
629                 pages++;
630
631         if (pages > MAX_PBL_LVL_1_PGS)
632                 return -ENOMEM;
633
634         frpl->hwq.max_elements = pages;
635         rc = bnxt_qplib_alloc_init_hwq(res->pdev, &frpl->hwq, NULL, 0,
636                                        &frpl->hwq.max_elements, PAGE_SIZE, 0,
637                                        PAGE_SIZE, HWQ_TYPE_CTX);
638         if (!rc)
639                 frpl->max_pg_ptrs = pg_ptrs;
640
641         return rc;
642 }
643
644 int bnxt_qplib_free_fast_reg_page_list(struct bnxt_qplib_res *res,
645                                        struct bnxt_qplib_frpl *frpl)
646 {
647         bnxt_qplib_free_hwq(res->pdev, &frpl->hwq);
648         return 0;
649 }
650
651 int bnxt_qplib_map_tc2cos(struct bnxt_qplib_res *res, u16 *cids)
652 {
653         struct bnxt_qplib_rcfw *rcfw = res->rcfw;
654         struct cmdq_map_tc_to_cos req;
655         struct creq_map_tc_to_cos_resp resp;
656         u16 cmd_flags = 0;
657         int rc = 0;
658
659         RCFW_CMD_PREP(req, MAP_TC_TO_COS, cmd_flags);
660         req.cos0 = cpu_to_le16(cids[0]);
661         req.cos1 = cpu_to_le16(cids[1]);
662
663         rc = bnxt_qplib_rcfw_send_message(rcfw, (void *)&req,
664                                           (void *)&resp, NULL, 0);
665         return 0;
666 }