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