1 /*******************************************************************
2 * This file is part of the Emulex RoCE Device Driver for *
3 * RoCE (RDMA over Converged Ethernet) CNA Adapters. *
4 * Copyright (C) 2008-2012 Emulex. All rights reserved. *
5 * EMULEX and SLI are trademarks of Emulex. *
8 * This program is free software; you can redistribute it and/or *
9 * modify it under the terms of version 2 of the GNU General *
10 * Public License as published by the Free Software Foundation. *
11 * This program is distributed in the hope that it will be useful. *
12 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
13 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
14 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
15 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
16 * TO BE LEGALLY INVALID. See the GNU General Public License for *
17 * more details, a copy of which can be found in the file COPYING *
18 * included with this package. *
20 * Contact Information:
21 * linux-drivers@emulex.com
25 * Costa Mesa, CA 92626
26 *******************************************************************/
28 #include <linux/sched.h>
29 #include <linux/interrupt.h>
30 #include <linux/log2.h>
31 #include <linux/dma-mapping.h>
33 #include <rdma/ib_verbs.h>
34 #include <rdma/ib_user_verbs.h>
37 #include "ocrdma_hw.h"
38 #include "ocrdma_verbs.h"
39 #include "ocrdma_ah.h"
42 OCRDMA_MBX_STATUS_FAILED = 1,
43 OCRDMA_MBX_STATUS_ILLEGAL_FIELD = 3,
44 OCRDMA_MBX_STATUS_OOR = 100,
45 OCRDMA_MBX_STATUS_INVALID_PD = 101,
46 OCRDMA_MBX_STATUS_PD_INUSE = 102,
47 OCRDMA_MBX_STATUS_INVALID_CQ = 103,
48 OCRDMA_MBX_STATUS_INVALID_QP = 104,
49 OCRDMA_MBX_STATUS_INVALID_LKEY = 105,
50 OCRDMA_MBX_STATUS_ORD_EXCEEDS = 106,
51 OCRDMA_MBX_STATUS_IRD_EXCEEDS = 107,
52 OCRDMA_MBX_STATUS_SENDQ_WQE_EXCEEDS = 108,
53 OCRDMA_MBX_STATUS_RECVQ_RQE_EXCEEDS = 109,
54 OCRDMA_MBX_STATUS_SGE_SEND_EXCEEDS = 110,
55 OCRDMA_MBX_STATUS_SGE_WRITE_EXCEEDS = 111,
56 OCRDMA_MBX_STATUS_SGE_RECV_EXCEEDS = 112,
57 OCRDMA_MBX_STATUS_INVALID_STATE_CHANGE = 113,
58 OCRDMA_MBX_STATUS_MW_BOUND = 114,
59 OCRDMA_MBX_STATUS_INVALID_VA = 115,
60 OCRDMA_MBX_STATUS_INVALID_LENGTH = 116,
61 OCRDMA_MBX_STATUS_INVALID_FBO = 117,
62 OCRDMA_MBX_STATUS_INVALID_ACC_RIGHTS = 118,
63 OCRDMA_MBX_STATUS_INVALID_PBE_SIZE = 119,
64 OCRDMA_MBX_STATUS_INVALID_PBL_ENTRY = 120,
65 OCRDMA_MBX_STATUS_INVALID_PBL_SHIFT = 121,
66 OCRDMA_MBX_STATUS_INVALID_SRQ_ID = 129,
67 OCRDMA_MBX_STATUS_SRQ_ERROR = 133,
68 OCRDMA_MBX_STATUS_RQE_EXCEEDS = 134,
69 OCRDMA_MBX_STATUS_MTU_EXCEEDS = 135,
70 OCRDMA_MBX_STATUS_MAX_QP_EXCEEDS = 136,
71 OCRDMA_MBX_STATUS_SRQ_LIMIT_EXCEEDS = 137,
72 OCRDMA_MBX_STATUS_SRQ_SIZE_UNDERUNS = 138,
73 OCRDMA_MBX_STATUS_QP_BOUND = 130,
74 OCRDMA_MBX_STATUS_INVALID_CHANGE = 139,
75 OCRDMA_MBX_STATUS_ATOMIC_OPS_UNSUP = 140,
76 OCRDMA_MBX_STATUS_INVALID_RNR_NAK_TIMER = 141,
77 OCRDMA_MBX_STATUS_MW_STILL_BOUND = 142,
78 OCRDMA_MBX_STATUS_PKEY_INDEX_INVALID = 143,
79 OCRDMA_MBX_STATUS_PKEY_INDEX_EXCEEDS = 144
82 enum additional_status {
83 OCRDMA_MBX_ADDI_STATUS_INSUFFICIENT_RESOURCES = 22
87 OCRDMA_MBX_CQE_STATUS_INSUFFICIENT_PRIVILEDGES = 1,
88 OCRDMA_MBX_CQE_STATUS_INVALID_PARAMETER = 2,
89 OCRDMA_MBX_CQE_STATUS_INSUFFICIENT_RESOURCES = 3,
90 OCRDMA_MBX_CQE_STATUS_QUEUE_FLUSHING = 4,
91 OCRDMA_MBX_CQE_STATUS_DMA_FAILED = 5
94 static inline void *ocrdma_get_eqe(struct ocrdma_eq *eq)
96 return eq->q.va + (eq->q.tail * sizeof(struct ocrdma_eqe));
99 static inline void ocrdma_eq_inc_tail(struct ocrdma_eq *eq)
101 eq->q.tail = (eq->q.tail + 1) & (OCRDMA_EQ_LEN - 1);
104 static inline void *ocrdma_get_mcqe(struct ocrdma_dev *dev)
106 struct ocrdma_mcqe *cqe = (struct ocrdma_mcqe *)
107 (dev->mq.cq.va + (dev->mq.cq.tail * sizeof(struct ocrdma_mcqe)));
109 if (!(le32_to_cpu(cqe->valid_ae_cmpl_cons) & OCRDMA_MCQE_VALID_MASK))
114 static inline void ocrdma_mcq_inc_tail(struct ocrdma_dev *dev)
116 dev->mq.cq.tail = (dev->mq.cq.tail + 1) & (OCRDMA_MQ_CQ_LEN - 1);
119 static inline struct ocrdma_mqe *ocrdma_get_mqe(struct ocrdma_dev *dev)
121 return dev->mq.sq.va + (dev->mq.sq.head * sizeof(struct ocrdma_mqe));
124 static inline void ocrdma_mq_inc_head(struct ocrdma_dev *dev)
126 dev->mq.sq.head = (dev->mq.sq.head + 1) & (OCRDMA_MQ_LEN - 1);
129 static inline void *ocrdma_get_mqe_rsp(struct ocrdma_dev *dev)
131 return dev->mq.sq.va + (dev->mqe_ctx.tag * sizeof(struct ocrdma_mqe));
134 enum ib_qp_state get_ibqp_state(enum ocrdma_qp_state qps)
139 case OCRDMA_QPS_INIT:
146 case OCRDMA_QPS_SQ_DRAINING:
156 static enum ocrdma_qp_state get_ocrdma_qp_state(enum ib_qp_state qps)
160 return OCRDMA_QPS_RST;
162 return OCRDMA_QPS_INIT;
164 return OCRDMA_QPS_RTR;
166 return OCRDMA_QPS_RTS;
168 return OCRDMA_QPS_SQD;
170 return OCRDMA_QPS_SQE;
172 return OCRDMA_QPS_ERR;
174 return OCRDMA_QPS_ERR;
177 static int ocrdma_get_mbx_errno(u32 status)
180 u8 mbox_status = (status & OCRDMA_MBX_RSP_STATUS_MASK) >>
181 OCRDMA_MBX_RSP_STATUS_SHIFT;
182 u8 add_status = (status & OCRDMA_MBX_RSP_ASTATUS_MASK) >>
183 OCRDMA_MBX_RSP_ASTATUS_SHIFT;
185 switch (mbox_status) {
186 case OCRDMA_MBX_STATUS_OOR:
187 case OCRDMA_MBX_STATUS_MAX_QP_EXCEEDS:
191 case OCRDMA_MBX_STATUS_INVALID_PD:
192 case OCRDMA_MBX_STATUS_INVALID_CQ:
193 case OCRDMA_MBX_STATUS_INVALID_SRQ_ID:
194 case OCRDMA_MBX_STATUS_INVALID_QP:
195 case OCRDMA_MBX_STATUS_INVALID_CHANGE:
196 case OCRDMA_MBX_STATUS_MTU_EXCEEDS:
197 case OCRDMA_MBX_STATUS_INVALID_RNR_NAK_TIMER:
198 case OCRDMA_MBX_STATUS_PKEY_INDEX_INVALID:
199 case OCRDMA_MBX_STATUS_PKEY_INDEX_EXCEEDS:
200 case OCRDMA_MBX_STATUS_ILLEGAL_FIELD:
201 case OCRDMA_MBX_STATUS_INVALID_PBL_ENTRY:
202 case OCRDMA_MBX_STATUS_INVALID_LKEY:
203 case OCRDMA_MBX_STATUS_INVALID_VA:
204 case OCRDMA_MBX_STATUS_INVALID_LENGTH:
205 case OCRDMA_MBX_STATUS_INVALID_FBO:
206 case OCRDMA_MBX_STATUS_INVALID_ACC_RIGHTS:
207 case OCRDMA_MBX_STATUS_INVALID_PBE_SIZE:
208 case OCRDMA_MBX_STATUS_ATOMIC_OPS_UNSUP:
209 case OCRDMA_MBX_STATUS_SRQ_ERROR:
210 case OCRDMA_MBX_STATUS_SRQ_SIZE_UNDERUNS:
214 case OCRDMA_MBX_STATUS_PD_INUSE:
215 case OCRDMA_MBX_STATUS_QP_BOUND:
216 case OCRDMA_MBX_STATUS_MW_STILL_BOUND:
217 case OCRDMA_MBX_STATUS_MW_BOUND:
221 case OCRDMA_MBX_STATUS_RECVQ_RQE_EXCEEDS:
222 case OCRDMA_MBX_STATUS_SGE_RECV_EXCEEDS:
223 case OCRDMA_MBX_STATUS_RQE_EXCEEDS:
224 case OCRDMA_MBX_STATUS_SRQ_LIMIT_EXCEEDS:
225 case OCRDMA_MBX_STATUS_ORD_EXCEEDS:
226 case OCRDMA_MBX_STATUS_IRD_EXCEEDS:
227 case OCRDMA_MBX_STATUS_SENDQ_WQE_EXCEEDS:
228 case OCRDMA_MBX_STATUS_SGE_SEND_EXCEEDS:
229 case OCRDMA_MBX_STATUS_SGE_WRITE_EXCEEDS:
233 case OCRDMA_MBX_STATUS_FAILED:
234 switch (add_status) {
235 case OCRDMA_MBX_ADDI_STATUS_INSUFFICIENT_RESOURCES:
245 char *port_speed_string(struct ocrdma_dev *dev)
248 u16 speeds_supported;
250 speeds_supported = dev->phy.fixed_speeds_supported |
251 dev->phy.auto_speeds_supported;
252 if (speeds_supported & OCRDMA_PHY_SPEED_40GBPS)
254 else if (speeds_supported & OCRDMA_PHY_SPEED_10GBPS)
256 else if (speeds_supported & OCRDMA_PHY_SPEED_1GBPS)
262 static int ocrdma_get_mbx_cqe_errno(u16 cqe_status)
264 int err_num = -EINVAL;
266 switch (cqe_status) {
267 case OCRDMA_MBX_CQE_STATUS_INSUFFICIENT_PRIVILEDGES:
270 case OCRDMA_MBX_CQE_STATUS_INVALID_PARAMETER:
273 case OCRDMA_MBX_CQE_STATUS_INSUFFICIENT_RESOURCES:
274 case OCRDMA_MBX_CQE_STATUS_QUEUE_FLUSHING:
277 case OCRDMA_MBX_CQE_STATUS_DMA_FAILED:
285 void ocrdma_ring_cq_db(struct ocrdma_dev *dev, u16 cq_id, bool armed,
286 bool solicited, u16 cqe_popped)
288 u32 val = cq_id & OCRDMA_DB_CQ_RING_ID_MASK;
290 val |= ((cq_id & OCRDMA_DB_CQ_RING_ID_EXT_MASK) <<
291 OCRDMA_DB_CQ_RING_ID_EXT_MASK_SHIFT);
294 val |= (1 << OCRDMA_DB_CQ_REARM_SHIFT);
296 val |= (1 << OCRDMA_DB_CQ_SOLICIT_SHIFT);
297 val |= (cqe_popped << OCRDMA_DB_CQ_NUM_POPPED_SHIFT);
298 iowrite32(val, dev->nic_info.db + OCRDMA_DB_CQ_OFFSET);
301 static void ocrdma_ring_mq_db(struct ocrdma_dev *dev)
305 val |= dev->mq.sq.id & OCRDMA_MQ_ID_MASK;
306 val |= 1 << OCRDMA_MQ_NUM_MQE_SHIFT;
307 iowrite32(val, dev->nic_info.db + OCRDMA_DB_MQ_OFFSET);
310 static void ocrdma_ring_eq_db(struct ocrdma_dev *dev, u16 eq_id,
311 bool arm, bool clear_int, u16 num_eqe)
315 val |= eq_id & OCRDMA_EQ_ID_MASK;
316 val |= ((eq_id & OCRDMA_EQ_ID_EXT_MASK) << OCRDMA_EQ_ID_EXT_MASK_SHIFT);
318 val |= (1 << OCRDMA_REARM_SHIFT);
320 val |= (1 << OCRDMA_EQ_CLR_SHIFT);
321 val |= (1 << OCRDMA_EQ_TYPE_SHIFT);
322 val |= (num_eqe << OCRDMA_NUM_EQE_SHIFT);
323 iowrite32(val, dev->nic_info.db + OCRDMA_DB_EQ_OFFSET);
326 static void ocrdma_init_mch(struct ocrdma_mbx_hdr *cmd_hdr,
327 u8 opcode, u8 subsys, u32 cmd_len)
329 cmd_hdr->subsys_op = (opcode | (subsys << OCRDMA_MCH_SUBSYS_SHIFT));
330 cmd_hdr->timeout = 20; /* seconds */
331 cmd_hdr->cmd_len = cmd_len - sizeof(struct ocrdma_mbx_hdr);
334 static void *ocrdma_init_emb_mqe(u8 opcode, u32 cmd_len)
336 struct ocrdma_mqe *mqe;
338 mqe = kzalloc(sizeof(struct ocrdma_mqe), GFP_KERNEL);
341 mqe->hdr.spcl_sge_cnt_emb |=
342 (OCRDMA_MQE_EMBEDDED << OCRDMA_MQE_HDR_EMB_SHIFT) &
343 OCRDMA_MQE_HDR_EMB_MASK;
344 mqe->hdr.pyld_len = cmd_len - sizeof(struct ocrdma_mqe_hdr);
346 ocrdma_init_mch(&mqe->u.emb_req.mch, opcode, OCRDMA_SUBSYS_ROCE,
351 static void *ocrdma_alloc_mqe(void)
353 return kzalloc(sizeof(struct ocrdma_mqe), GFP_KERNEL);
356 static void ocrdma_free_q(struct ocrdma_dev *dev, struct ocrdma_queue_info *q)
358 dma_free_coherent(&dev->nic_info.pdev->dev, q->size, q->va, q->dma);
361 static int ocrdma_alloc_q(struct ocrdma_dev *dev,
362 struct ocrdma_queue_info *q, u16 len, u16 entry_size)
364 memset(q, 0, sizeof(*q));
366 q->entry_size = entry_size;
367 q->size = len * entry_size;
368 q->va = dma_alloc_coherent(&dev->nic_info.pdev->dev, q->size,
369 &q->dma, GFP_KERNEL);
372 memset(q->va, 0, q->size);
376 static void ocrdma_build_q_pages(struct ocrdma_pa *q_pa, int cnt,
377 dma_addr_t host_pa, int hw_page_size)
381 for (i = 0; i < cnt; i++) {
382 q_pa[i].lo = (u32) (host_pa & 0xffffffff);
383 q_pa[i].hi = (u32) upper_32_bits(host_pa);
384 host_pa += hw_page_size;
388 static int ocrdma_mbx_delete_q(struct ocrdma_dev *dev,
389 struct ocrdma_queue_info *q, int queue_type)
393 struct ocrdma_delete_q_req *cmd = dev->mbx_cmd;
395 switch (queue_type) {
397 opcode = OCRDMA_CMD_DELETE_MQ;
400 opcode = OCRDMA_CMD_DELETE_CQ;
403 opcode = OCRDMA_CMD_DELETE_EQ;
408 memset(cmd, 0, sizeof(*cmd));
409 ocrdma_init_mch(&cmd->req, opcode, OCRDMA_SUBSYS_COMMON, sizeof(*cmd));
412 status = be_roce_mcc_cmd(dev->nic_info.netdev,
413 cmd, sizeof(*cmd), NULL, NULL);
419 static int ocrdma_mbx_create_eq(struct ocrdma_dev *dev, struct ocrdma_eq *eq)
422 struct ocrdma_create_eq_req *cmd = dev->mbx_cmd;
423 struct ocrdma_create_eq_rsp *rsp = dev->mbx_cmd;
425 memset(cmd, 0, sizeof(*cmd));
426 ocrdma_init_mch(&cmd->req, OCRDMA_CMD_CREATE_EQ, OCRDMA_SUBSYS_COMMON,
429 cmd->req.rsvd_version = 2;
431 cmd->valid = OCRDMA_CREATE_EQ_VALID;
432 cmd->cnt = 4 << OCRDMA_CREATE_EQ_CNT_SHIFT;
434 ocrdma_build_q_pages(&cmd->pa[0], cmd->num_pages, eq->q.dma,
436 status = be_roce_mcc_cmd(dev->nic_info.netdev, cmd, sizeof(*cmd), NULL,
439 eq->q.id = rsp->vector_eqid & 0xffff;
440 eq->vector = (rsp->vector_eqid >> 16) & 0xffff;
441 eq->q.created = true;
446 static int ocrdma_create_eq(struct ocrdma_dev *dev,
447 struct ocrdma_eq *eq, u16 q_len)
451 status = ocrdma_alloc_q(dev, &eq->q, OCRDMA_EQ_LEN,
452 sizeof(struct ocrdma_eqe));
456 status = ocrdma_mbx_create_eq(dev, eq);
460 ocrdma_ring_eq_db(dev, eq->q.id, true, true, 0);
464 ocrdma_free_q(dev, &eq->q);
468 int ocrdma_get_irq(struct ocrdma_dev *dev, struct ocrdma_eq *eq)
472 if (dev->nic_info.intr_mode == BE_INTERRUPT_MODE_INTX)
473 irq = dev->nic_info.pdev->irq;
475 irq = dev->nic_info.msix.vector_list[eq->vector];
479 static void _ocrdma_destroy_eq(struct ocrdma_dev *dev, struct ocrdma_eq *eq)
482 ocrdma_mbx_delete_q(dev, &eq->q, QTYPE_EQ);
483 ocrdma_free_q(dev, &eq->q);
487 static void ocrdma_destroy_eq(struct ocrdma_dev *dev, struct ocrdma_eq *eq)
491 /* disarm EQ so that interrupts are not generated
492 * during freeing and EQ delete is in progress.
494 ocrdma_ring_eq_db(dev, eq->q.id, false, false, 0);
496 irq = ocrdma_get_irq(dev, eq);
498 _ocrdma_destroy_eq(dev, eq);
501 static void ocrdma_destroy_eqs(struct ocrdma_dev *dev)
505 for (i = 0; i < dev->eq_cnt; i++)
506 ocrdma_destroy_eq(dev, &dev->eq_tbl[i]);
509 static int ocrdma_mbx_mq_cq_create(struct ocrdma_dev *dev,
510 struct ocrdma_queue_info *cq,
511 struct ocrdma_queue_info *eq)
513 struct ocrdma_create_cq_cmd *cmd = dev->mbx_cmd;
514 struct ocrdma_create_cq_cmd_rsp *rsp = dev->mbx_cmd;
517 memset(cmd, 0, sizeof(*cmd));
518 ocrdma_init_mch(&cmd->req, OCRDMA_CMD_CREATE_CQ,
519 OCRDMA_SUBSYS_COMMON, sizeof(*cmd));
521 cmd->req.rsvd_version = OCRDMA_CREATE_CQ_VER2;
522 cmd->pgsz_pgcnt = (cq->size / OCRDMA_MIN_Q_PAGE_SIZE) <<
523 OCRDMA_CREATE_CQ_PAGE_SIZE_SHIFT;
524 cmd->pgsz_pgcnt |= PAGES_4K_SPANNED(cq->va, cq->size);
526 cmd->ev_cnt_flags = OCRDMA_CREATE_CQ_DEF_FLAGS;
528 cmd->cqe_count = cq->size / sizeof(struct ocrdma_mcqe);
530 ocrdma_build_q_pages(&cmd->pa[0], cq->size / OCRDMA_MIN_Q_PAGE_SIZE,
531 cq->dma, PAGE_SIZE_4K);
532 status = be_roce_mcc_cmd(dev->nic_info.netdev,
533 cmd, sizeof(*cmd), NULL, NULL);
535 cq->id = (u16) (rsp->cq_id & OCRDMA_CREATE_CQ_RSP_CQ_ID_MASK);
541 static u32 ocrdma_encoded_q_len(int q_len)
543 u32 len_encoded = fls(q_len); /* log2(len) + 1 */
545 if (len_encoded == 16)
550 static int ocrdma_mbx_create_mq(struct ocrdma_dev *dev,
551 struct ocrdma_queue_info *mq,
552 struct ocrdma_queue_info *cq)
554 int num_pages, status;
555 struct ocrdma_create_mq_req *cmd = dev->mbx_cmd;
556 struct ocrdma_create_mq_rsp *rsp = dev->mbx_cmd;
557 struct ocrdma_pa *pa;
559 memset(cmd, 0, sizeof(*cmd));
560 num_pages = PAGES_4K_SPANNED(mq->va, mq->size);
562 ocrdma_init_mch(&cmd->req, OCRDMA_CMD_CREATE_MQ_EXT,
563 OCRDMA_SUBSYS_COMMON, sizeof(*cmd));
564 cmd->req.rsvd_version = 1;
565 cmd->cqid_pages = num_pages;
566 cmd->cqid_pages |= (cq->id << OCRDMA_CREATE_MQ_CQ_ID_SHIFT);
567 cmd->async_cqid_valid = OCRDMA_CREATE_MQ_ASYNC_CQ_VALID;
569 cmd->async_event_bitmap = Bit(OCRDMA_ASYNC_GRP5_EVE_CODE);
570 cmd->async_event_bitmap |= Bit(OCRDMA_ASYNC_RDMA_EVE_CODE);
572 cmd->async_cqid_ringsize = cq->id;
573 cmd->async_cqid_ringsize |= (ocrdma_encoded_q_len(mq->len) <<
574 OCRDMA_CREATE_MQ_RING_SIZE_SHIFT);
575 cmd->valid = OCRDMA_CREATE_MQ_VALID;
578 ocrdma_build_q_pages(pa, num_pages, mq->dma, PAGE_SIZE_4K);
579 status = be_roce_mcc_cmd(dev->nic_info.netdev,
580 cmd, sizeof(*cmd), NULL, NULL);
588 static int ocrdma_create_mq(struct ocrdma_dev *dev)
592 /* Alloc completion queue for Mailbox queue */
593 status = ocrdma_alloc_q(dev, &dev->mq.cq, OCRDMA_MQ_CQ_LEN,
594 sizeof(struct ocrdma_mcqe));
598 dev->eq_tbl[0].cq_cnt++;
599 status = ocrdma_mbx_mq_cq_create(dev, &dev->mq.cq, &dev->eq_tbl[0].q);
603 memset(&dev->mqe_ctx, 0, sizeof(dev->mqe_ctx));
604 init_waitqueue_head(&dev->mqe_ctx.cmd_wait);
605 mutex_init(&dev->mqe_ctx.lock);
607 /* Alloc Mailbox queue */
608 status = ocrdma_alloc_q(dev, &dev->mq.sq, OCRDMA_MQ_LEN,
609 sizeof(struct ocrdma_mqe));
612 status = ocrdma_mbx_create_mq(dev, &dev->mq.sq, &dev->mq.cq);
615 ocrdma_ring_cq_db(dev, dev->mq.cq.id, true, false, 0);
619 ocrdma_free_q(dev, &dev->mq.sq);
621 ocrdma_mbx_delete_q(dev, &dev->mq.cq, QTYPE_CQ);
623 ocrdma_free_q(dev, &dev->mq.cq);
628 static void ocrdma_destroy_mq(struct ocrdma_dev *dev)
630 struct ocrdma_queue_info *mbxq, *cq;
632 /* mqe_ctx lock synchronizes with any other pending cmds. */
633 mutex_lock(&dev->mqe_ctx.lock);
636 ocrdma_mbx_delete_q(dev, mbxq, QTYPE_MCCQ);
637 ocrdma_free_q(dev, mbxq);
639 mutex_unlock(&dev->mqe_ctx.lock);
643 ocrdma_mbx_delete_q(dev, cq, QTYPE_CQ);
644 ocrdma_free_q(dev, cq);
648 static void ocrdma_process_qpcat_error(struct ocrdma_dev *dev,
649 struct ocrdma_qp *qp)
651 enum ib_qp_state new_ib_qps = IB_QPS_ERR;
652 enum ib_qp_state old_ib_qps;
656 ocrdma_qp_state_change(qp, new_ib_qps, &old_ib_qps);
659 static void ocrdma_dispatch_ibevent(struct ocrdma_dev *dev,
660 struct ocrdma_ae_mcqe *cqe)
662 struct ocrdma_qp *qp = NULL;
663 struct ocrdma_cq *cq = NULL;
664 struct ib_event ib_evt = { 0 };
669 int type = (cqe->valid_ae_event & OCRDMA_AE_MCQE_EVENT_TYPE_MASK) >>
670 OCRDMA_AE_MCQE_EVENT_TYPE_SHIFT;
672 if (cqe->qpvalid_qpid & OCRDMA_AE_MCQE_QPVALID)
673 qp = dev->qp_tbl[cqe->qpvalid_qpid & OCRDMA_AE_MCQE_QPID_MASK];
674 if (cqe->cqvalid_cqid & OCRDMA_AE_MCQE_CQVALID)
675 cq = dev->cq_tbl[cqe->cqvalid_cqid & OCRDMA_AE_MCQE_CQID_MASK];
677 ib_evt.device = &dev->ibdev;
680 case OCRDMA_CQ_ERROR:
681 ib_evt.element.cq = &cq->ibcq;
682 ib_evt.event = IB_EVENT_CQ_ERR;
686 case OCRDMA_CQ_OVERRUN_ERROR:
687 ib_evt.element.cq = &cq->ibcq;
688 ib_evt.event = IB_EVENT_CQ_ERR;
692 case OCRDMA_CQ_QPCAT_ERROR:
693 ib_evt.element.qp = &qp->ibqp;
694 ib_evt.event = IB_EVENT_QP_FATAL;
695 ocrdma_process_qpcat_error(dev, qp);
697 case OCRDMA_QP_ACCESS_ERROR:
698 ib_evt.element.qp = &qp->ibqp;
699 ib_evt.event = IB_EVENT_QP_ACCESS_ERR;
701 case OCRDMA_QP_COMM_EST_EVENT:
702 ib_evt.element.qp = &qp->ibqp;
703 ib_evt.event = IB_EVENT_COMM_EST;
705 case OCRDMA_SQ_DRAINED_EVENT:
706 ib_evt.element.qp = &qp->ibqp;
707 ib_evt.event = IB_EVENT_SQ_DRAINED;
709 case OCRDMA_DEVICE_FATAL_EVENT:
710 ib_evt.element.port_num = 1;
711 ib_evt.event = IB_EVENT_DEVICE_FATAL;
715 case OCRDMA_SRQCAT_ERROR:
716 ib_evt.element.srq = &qp->srq->ibsrq;
717 ib_evt.event = IB_EVENT_SRQ_ERR;
721 case OCRDMA_SRQ_LIMIT_EVENT:
722 ib_evt.element.srq = &qp->srq->ibsrq;
723 ib_evt.event = IB_EVENT_SRQ_LIMIT_REACHED;
727 case OCRDMA_QP_LAST_WQE_EVENT:
728 ib_evt.element.qp = &qp->ibqp;
729 ib_evt.event = IB_EVENT_QP_LAST_WQE_REACHED;
736 pr_err("%s() unknown type=0x%x\n", __func__, type);
741 if (qp->ibqp.event_handler)
742 qp->ibqp.event_handler(&ib_evt, qp->ibqp.qp_context);
743 } else if (cq_event) {
744 if (cq->ibcq.event_handler)
745 cq->ibcq.event_handler(&ib_evt, cq->ibcq.cq_context);
746 } else if (srq_event) {
747 if (qp->srq->ibsrq.event_handler)
748 qp->srq->ibsrq.event_handler(&ib_evt,
751 } else if (dev_event) {
752 pr_err("%s: Fatal event received\n", dev->ibdev.name);
753 ib_dispatch_event(&ib_evt);
758 static void ocrdma_process_grp5_aync(struct ocrdma_dev *dev,
759 struct ocrdma_ae_mcqe *cqe)
761 struct ocrdma_ae_pvid_mcqe *evt;
762 int type = (cqe->valid_ae_event & OCRDMA_AE_MCQE_EVENT_TYPE_MASK) >>
763 OCRDMA_AE_MCQE_EVENT_TYPE_SHIFT;
766 case OCRDMA_ASYNC_EVENT_PVID_STATE:
767 evt = (struct ocrdma_ae_pvid_mcqe *)cqe;
768 if ((evt->tag_enabled & OCRDMA_AE_PVID_MCQE_ENABLED_MASK) >>
769 OCRDMA_AE_PVID_MCQE_ENABLED_SHIFT)
770 dev->pvid = ((evt->tag_enabled &
771 OCRDMA_AE_PVID_MCQE_TAG_MASK) >>
772 OCRDMA_AE_PVID_MCQE_TAG_SHIFT);
775 /* Not interested evts. */
780 static void ocrdma_process_acqe(struct ocrdma_dev *dev, void *ae_cqe)
782 /* async CQE processing */
783 struct ocrdma_ae_mcqe *cqe = ae_cqe;
784 u32 evt_code = (cqe->valid_ae_event & OCRDMA_AE_MCQE_EVENT_CODE_MASK) >>
785 OCRDMA_AE_MCQE_EVENT_CODE_SHIFT;
787 if (evt_code == OCRDMA_ASYNC_RDMA_EVE_CODE)
788 ocrdma_dispatch_ibevent(dev, cqe);
789 else if (evt_code == OCRDMA_ASYNC_GRP5_EVE_CODE)
790 ocrdma_process_grp5_aync(dev, cqe);
792 pr_err("%s(%d) invalid evt code=0x%x\n", __func__,
796 static void ocrdma_process_mcqe(struct ocrdma_dev *dev, struct ocrdma_mcqe *cqe)
798 if (dev->mqe_ctx.tag == cqe->tag_lo && dev->mqe_ctx.cmd_done == false) {
799 dev->mqe_ctx.cqe_status = (cqe->status &
800 OCRDMA_MCQE_STATUS_MASK) >> OCRDMA_MCQE_STATUS_SHIFT;
801 dev->mqe_ctx.ext_status =
802 (cqe->status & OCRDMA_MCQE_ESTATUS_MASK)
803 >> OCRDMA_MCQE_ESTATUS_SHIFT;
804 dev->mqe_ctx.cmd_done = true;
805 wake_up(&dev->mqe_ctx.cmd_wait);
807 pr_err("%s() cqe for invalid tag0x%x.expected=0x%x\n",
808 __func__, cqe->tag_lo, dev->mqe_ctx.tag);
811 static int ocrdma_mq_cq_handler(struct ocrdma_dev *dev, u16 cq_id)
814 struct ocrdma_mcqe *cqe;
817 cqe = ocrdma_get_mcqe(dev);
820 ocrdma_le32_to_cpu(cqe, sizeof(*cqe));
822 if (cqe->valid_ae_cmpl_cons & OCRDMA_MCQE_AE_MASK)
823 ocrdma_process_acqe(dev, cqe);
824 else if (cqe->valid_ae_cmpl_cons & OCRDMA_MCQE_CMPL_MASK)
825 ocrdma_process_mcqe(dev, cqe);
826 memset(cqe, 0, sizeof(struct ocrdma_mcqe));
827 ocrdma_mcq_inc_tail(dev);
829 ocrdma_ring_cq_db(dev, dev->mq.cq.id, true, false, cqe_popped);
833 static void ocrdma_qp_buddy_cq_handler(struct ocrdma_dev *dev,
834 struct ocrdma_cq *cq)
837 struct ocrdma_qp *qp;
838 bool buddy_cq_found = false;
839 /* Go through list of QPs in error state which are using this CQ
840 * and invoke its callback handler to trigger CQE processing for
841 * error/flushed CQE. It is rare to find more than few entries in
842 * this list as most consumers stops after getting error CQE.
843 * List is traversed only once when a matching buddy cq found for a QP.
845 spin_lock_irqsave(&dev->flush_q_lock, flags);
846 list_for_each_entry(qp, &cq->sq_head, sq_entry) {
849 /* if wq and rq share the same cq, than comp_handler
850 * is already invoked.
852 if (qp->sq_cq == qp->rq_cq)
854 /* if completion came on sq, rq's cq is buddy cq.
855 * if completion came on rq, sq's cq is buddy cq.
861 buddy_cq_found = true;
864 spin_unlock_irqrestore(&dev->flush_q_lock, flags);
865 if (buddy_cq_found == false)
867 if (cq->ibcq.comp_handler) {
868 spin_lock_irqsave(&cq->comp_handler_lock, flags);
869 (*cq->ibcq.comp_handler) (&cq->ibcq, cq->ibcq.cq_context);
870 spin_unlock_irqrestore(&cq->comp_handler_lock, flags);
874 static void ocrdma_qp_cq_handler(struct ocrdma_dev *dev, u16 cq_idx)
877 struct ocrdma_cq *cq;
879 if (cq_idx >= OCRDMA_MAX_CQ)
882 cq = dev->cq_tbl[cq_idx];
886 if (cq->ibcq.comp_handler) {
887 spin_lock_irqsave(&cq->comp_handler_lock, flags);
888 (*cq->ibcq.comp_handler) (&cq->ibcq, cq->ibcq.cq_context);
889 spin_unlock_irqrestore(&cq->comp_handler_lock, flags);
891 ocrdma_qp_buddy_cq_handler(dev, cq);
894 static void ocrdma_cq_handler(struct ocrdma_dev *dev, u16 cq_id)
896 /* process the MQ-CQE. */
897 if (cq_id == dev->mq.cq.id)
898 ocrdma_mq_cq_handler(dev, cq_id);
900 ocrdma_qp_cq_handler(dev, cq_id);
903 static irqreturn_t ocrdma_irq_handler(int irq, void *handle)
905 struct ocrdma_eq *eq = handle;
906 struct ocrdma_dev *dev = eq->dev;
907 struct ocrdma_eqe eqe;
908 struct ocrdma_eqe *ptr;
910 int budget = eq->cq_cnt;
913 ptr = ocrdma_get_eqe(eq);
915 ocrdma_le32_to_cpu(&eqe, sizeof(eqe));
916 if ((eqe.id_valid & OCRDMA_EQE_VALID_MASK) == 0)
920 /* ring eq doorbell as soon as its consumed. */
921 ocrdma_ring_eq_db(dev, eq->q.id, false, true, 1);
922 /* check whether its CQE or not. */
923 if ((eqe.id_valid & OCRDMA_EQE_FOR_CQE_MASK) == 0) {
924 cq_id = eqe.id_valid >> OCRDMA_EQE_RESOURCE_ID_SHIFT;
925 ocrdma_cq_handler(dev, cq_id);
927 ocrdma_eq_inc_tail(eq);
929 /* There can be a stale EQE after the last bound CQ is
930 * destroyed. EQE valid and budget == 0 implies this.
937 ocrdma_ring_eq_db(dev, eq->q.id, true, true, 0);
941 static void ocrdma_post_mqe(struct ocrdma_dev *dev, struct ocrdma_mqe *cmd)
943 struct ocrdma_mqe *mqe;
945 dev->mqe_ctx.tag = dev->mq.sq.head;
946 dev->mqe_ctx.cmd_done = false;
947 mqe = ocrdma_get_mqe(dev);
948 cmd->hdr.tag_lo = dev->mq.sq.head;
949 ocrdma_copy_cpu_to_le32(mqe, cmd, sizeof(*mqe));
950 /* make sure descriptor is written before ringing doorbell */
952 ocrdma_mq_inc_head(dev);
953 ocrdma_ring_mq_db(dev);
956 static int ocrdma_wait_mqe_cmpl(struct ocrdma_dev *dev)
960 status = wait_event_timeout(dev->mqe_ctx.cmd_wait,
961 (dev->mqe_ctx.cmd_done != false),
962 msecs_to_jiffies(30000));
969 /* issue a mailbox command on the MQ */
970 static int ocrdma_mbx_cmd(struct ocrdma_dev *dev, struct ocrdma_mqe *mqe)
973 u16 cqe_status, ext_status;
974 struct ocrdma_mqe *rsp_mqe;
975 struct ocrdma_mbx_rsp *rsp = NULL;
977 mutex_lock(&dev->mqe_ctx.lock);
978 ocrdma_post_mqe(dev, mqe);
979 status = ocrdma_wait_mqe_cmpl(dev);
982 cqe_status = dev->mqe_ctx.cqe_status;
983 ext_status = dev->mqe_ctx.ext_status;
984 rsp_mqe = ocrdma_get_mqe_rsp(dev);
985 ocrdma_copy_le32_to_cpu(mqe, rsp_mqe, (sizeof(*mqe)));
986 if ((mqe->hdr.spcl_sge_cnt_emb & OCRDMA_MQE_HDR_EMB_MASK) >>
987 OCRDMA_MQE_HDR_EMB_SHIFT)
990 if (cqe_status || ext_status) {
991 pr_err("%s() cqe_status=0x%x, ext_status=0x%x,",
992 __func__, cqe_status, ext_status);
994 /* This is for embedded cmds. */
995 pr_err("opcode=0x%x, subsystem=0x%x\n",
996 (rsp->subsys_op & OCRDMA_MBX_RSP_OPCODE_MASK) >>
997 OCRDMA_MBX_RSP_OPCODE_SHIFT,
998 (rsp->subsys_op & OCRDMA_MBX_RSP_SUBSYS_MASK) >>
999 OCRDMA_MBX_RSP_SUBSYS_SHIFT);
1001 status = ocrdma_get_mbx_cqe_errno(cqe_status);
1004 /* For non embedded, rsp errors are handled in ocrdma_nonemb_mbx_cmd */
1005 if (rsp && (mqe->u.rsp.status & OCRDMA_MBX_RSP_STATUS_MASK))
1006 status = ocrdma_get_mbx_errno(mqe->u.rsp.status);
1008 mutex_unlock(&dev->mqe_ctx.lock);
1012 static int ocrdma_nonemb_mbx_cmd(struct ocrdma_dev *dev, struct ocrdma_mqe *mqe,
1016 struct ocrdma_mbx_rsp *rsp = payload_va;
1018 if ((mqe->hdr.spcl_sge_cnt_emb & OCRDMA_MQE_HDR_EMB_MASK) >>
1019 OCRDMA_MQE_HDR_EMB_SHIFT)
1022 status = ocrdma_mbx_cmd(dev, mqe);
1024 /* For non embedded, only CQE failures are handled in
1025 * ocrdma_mbx_cmd. We need to check for RSP errors.
1027 if (rsp->status & OCRDMA_MBX_RSP_STATUS_MASK)
1028 status = ocrdma_get_mbx_errno(rsp->status);
1031 pr_err("opcode=0x%x, subsystem=0x%x\n",
1032 (rsp->subsys_op & OCRDMA_MBX_RSP_OPCODE_MASK) >>
1033 OCRDMA_MBX_RSP_OPCODE_SHIFT,
1034 (rsp->subsys_op & OCRDMA_MBX_RSP_SUBSYS_MASK) >>
1035 OCRDMA_MBX_RSP_SUBSYS_SHIFT);
1039 static void ocrdma_get_attr(struct ocrdma_dev *dev,
1040 struct ocrdma_dev_attr *attr,
1041 struct ocrdma_mbx_query_config *rsp)
1044 (rsp->max_pd_ca_ack_delay & OCRDMA_MBX_QUERY_CFG_MAX_PD_MASK) >>
1045 OCRDMA_MBX_QUERY_CFG_MAX_PD_SHIFT;
1047 (rsp->qp_srq_cq_ird_ord & OCRDMA_MBX_QUERY_CFG_MAX_QP_MASK) >>
1048 OCRDMA_MBX_QUERY_CFG_MAX_QP_SHIFT;
1050 (rsp->max_srq_rpir_qps & OCRDMA_MBX_QUERY_CFG_MAX_SRQ_MASK) >>
1051 OCRDMA_MBX_QUERY_CFG_MAX_SRQ_OFFSET;
1052 attr->max_send_sge = ((rsp->max_write_send_sge &
1053 OCRDMA_MBX_QUERY_CFG_MAX_SEND_SGE_MASK) >>
1054 OCRDMA_MBX_QUERY_CFG_MAX_SEND_SGE_SHIFT);
1055 attr->max_recv_sge = (rsp->max_write_send_sge &
1056 OCRDMA_MBX_QUERY_CFG_MAX_SEND_SGE_MASK) >>
1057 OCRDMA_MBX_QUERY_CFG_MAX_SEND_SGE_SHIFT;
1058 attr->max_srq_sge = (rsp->max_srq_rqe_sge &
1059 OCRDMA_MBX_QUERY_CFG_MAX_SRQ_SGE_MASK) >>
1060 OCRDMA_MBX_QUERY_CFG_MAX_SRQ_SGE_OFFSET;
1061 attr->max_rdma_sge = (rsp->max_write_send_sge &
1062 OCRDMA_MBX_QUERY_CFG_MAX_WRITE_SGE_MASK) >>
1063 OCRDMA_MBX_QUERY_CFG_MAX_WRITE_SGE_SHIFT;
1064 attr->max_ord_per_qp = (rsp->max_ird_ord_per_qp &
1065 OCRDMA_MBX_QUERY_CFG_MAX_ORD_PER_QP_MASK) >>
1066 OCRDMA_MBX_QUERY_CFG_MAX_ORD_PER_QP_SHIFT;
1067 attr->max_ird_per_qp = (rsp->max_ird_ord_per_qp &
1068 OCRDMA_MBX_QUERY_CFG_MAX_IRD_PER_QP_MASK) >>
1069 OCRDMA_MBX_QUERY_CFG_MAX_IRD_PER_QP_SHIFT;
1070 attr->cq_overflow_detect = (rsp->qp_srq_cq_ird_ord &
1071 OCRDMA_MBX_QUERY_CFG_CQ_OVERFLOW_MASK) >>
1072 OCRDMA_MBX_QUERY_CFG_CQ_OVERFLOW_SHIFT;
1073 attr->srq_supported = (rsp->qp_srq_cq_ird_ord &
1074 OCRDMA_MBX_QUERY_CFG_SRQ_SUPPORTED_MASK) >>
1075 OCRDMA_MBX_QUERY_CFG_SRQ_SUPPORTED_SHIFT;
1076 attr->local_ca_ack_delay = (rsp->max_pd_ca_ack_delay &
1077 OCRDMA_MBX_QUERY_CFG_CA_ACK_DELAY_MASK) >>
1078 OCRDMA_MBX_QUERY_CFG_CA_ACK_DELAY_SHIFT;
1079 attr->max_mw = rsp->max_mw;
1080 attr->max_mr = rsp->max_mr;
1081 attr->max_mr_size = ~0ull;
1083 attr->max_pages_per_frmr = rsp->max_pages_per_frmr;
1084 attr->max_num_mr_pbl = rsp->max_num_mr_pbl;
1085 attr->max_cqe = rsp->max_cq_cqes_per_cq &
1086 OCRDMA_MBX_QUERY_CFG_MAX_CQES_PER_CQ_MASK;
1087 attr->max_cq = (rsp->max_cq_cqes_per_cq &
1088 OCRDMA_MBX_QUERY_CFG_MAX_CQ_MASK) >>
1089 OCRDMA_MBX_QUERY_CFG_MAX_CQ_OFFSET;
1090 attr->wqe_size = ((rsp->wqe_rqe_stride_max_dpp_cqs &
1091 OCRDMA_MBX_QUERY_CFG_MAX_WQE_SIZE_MASK) >>
1092 OCRDMA_MBX_QUERY_CFG_MAX_WQE_SIZE_OFFSET) *
1094 attr->rqe_size = ((rsp->wqe_rqe_stride_max_dpp_cqs &
1095 OCRDMA_MBX_QUERY_CFG_MAX_RQE_SIZE_MASK) >>
1096 OCRDMA_MBX_QUERY_CFG_MAX_RQE_SIZE_OFFSET) *
1098 attr->max_inline_data =
1099 attr->wqe_size - (sizeof(struct ocrdma_hdr_wqe) +
1100 sizeof(struct ocrdma_sge));
1101 if (ocrdma_get_asic_type(dev) == OCRDMA_ASIC_GEN_SKH_R) {
1103 attr->ird_page_size = OCRDMA_MIN_Q_PAGE_SIZE;
1104 attr->num_ird_pages = MAX_OCRDMA_IRD_PAGES;
1106 dev->attr.max_wqe = rsp->max_wqes_rqes_per_q >>
1107 OCRDMA_MBX_QUERY_CFG_MAX_WQES_PER_WQ_OFFSET;
1108 dev->attr.max_rqe = rsp->max_wqes_rqes_per_q &
1109 OCRDMA_MBX_QUERY_CFG_MAX_RQES_PER_RQ_MASK;
1112 static int ocrdma_check_fw_config(struct ocrdma_dev *dev,
1113 struct ocrdma_fw_conf_rsp *conf)
1117 fn_mode = conf->fn_mode & OCRDMA_FN_MODE_RDMA;
1118 if (fn_mode != OCRDMA_FN_MODE_RDMA)
1120 dev->base_eqid = conf->base_eqid;
1121 dev->max_eq = conf->max_eq;
1125 /* can be issued only during init time. */
1126 static int ocrdma_mbx_query_fw_ver(struct ocrdma_dev *dev)
1128 int status = -ENOMEM;
1129 struct ocrdma_mqe *cmd;
1130 struct ocrdma_fw_ver_rsp *rsp;
1132 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_GET_FW_VER, sizeof(*cmd));
1135 ocrdma_init_mch((struct ocrdma_mbx_hdr *)&cmd->u.cmd[0],
1136 OCRDMA_CMD_GET_FW_VER,
1137 OCRDMA_SUBSYS_COMMON, sizeof(*cmd));
1139 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1142 rsp = (struct ocrdma_fw_ver_rsp *)cmd;
1143 memset(&dev->attr.fw_ver[0], 0, sizeof(dev->attr.fw_ver));
1144 memcpy(&dev->attr.fw_ver[0], &rsp->running_ver[0],
1145 sizeof(rsp->running_ver));
1146 ocrdma_le32_to_cpu(dev->attr.fw_ver, sizeof(rsp->running_ver));
1152 /* can be issued only during init time. */
1153 static int ocrdma_mbx_query_fw_config(struct ocrdma_dev *dev)
1155 int status = -ENOMEM;
1156 struct ocrdma_mqe *cmd;
1157 struct ocrdma_fw_conf_rsp *rsp;
1159 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_GET_FW_CONFIG, sizeof(*cmd));
1162 ocrdma_init_mch((struct ocrdma_mbx_hdr *)&cmd->u.cmd[0],
1163 OCRDMA_CMD_GET_FW_CONFIG,
1164 OCRDMA_SUBSYS_COMMON, sizeof(*cmd));
1165 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1168 rsp = (struct ocrdma_fw_conf_rsp *)cmd;
1169 status = ocrdma_check_fw_config(dev, rsp);
1175 int ocrdma_mbx_rdma_stats(struct ocrdma_dev *dev, bool reset)
1177 struct ocrdma_rdma_stats_req *req = dev->stats_mem.va;
1178 struct ocrdma_mqe *mqe = &dev->stats_mem.mqe;
1179 struct ocrdma_rdma_stats_resp *old_stats = NULL;
1182 old_stats = kzalloc(sizeof(*old_stats), GFP_KERNEL);
1183 if (old_stats == NULL)
1186 memset(mqe, 0, sizeof(*mqe));
1187 mqe->hdr.pyld_len = dev->stats_mem.size;
1188 mqe->hdr.spcl_sge_cnt_emb |=
1189 (1 << OCRDMA_MQE_HDR_SGE_CNT_SHIFT) &
1190 OCRDMA_MQE_HDR_SGE_CNT_MASK;
1191 mqe->u.nonemb_req.sge[0].pa_lo = (u32) (dev->stats_mem.pa & 0xffffffff);
1192 mqe->u.nonemb_req.sge[0].pa_hi = (u32) upper_32_bits(dev->stats_mem.pa);
1193 mqe->u.nonemb_req.sge[0].len = dev->stats_mem.size;
1195 /* Cache the old stats */
1196 memcpy(old_stats, req, sizeof(struct ocrdma_rdma_stats_resp));
1197 memset(req, 0, dev->stats_mem.size);
1199 ocrdma_init_mch((struct ocrdma_mbx_hdr *)req,
1200 OCRDMA_CMD_GET_RDMA_STATS,
1202 dev->stats_mem.size);
1204 req->reset_stats = reset;
1206 status = ocrdma_nonemb_mbx_cmd(dev, mqe, dev->stats_mem.va);
1208 /* Copy from cache, if mbox fails */
1209 memcpy(req, old_stats, sizeof(struct ocrdma_rdma_stats_resp));
1211 ocrdma_le32_to_cpu(req, dev->stats_mem.size);
1217 static int ocrdma_mbx_get_ctrl_attribs(struct ocrdma_dev *dev)
1219 int status = -ENOMEM;
1220 struct ocrdma_dma_mem dma;
1221 struct ocrdma_mqe *mqe;
1222 struct ocrdma_get_ctrl_attribs_rsp *ctrl_attr_rsp;
1223 struct mgmt_hba_attribs *hba_attribs;
1225 mqe = ocrdma_alloc_mqe();
1228 memset(mqe, 0, sizeof(*mqe));
1230 dma.size = sizeof(struct ocrdma_get_ctrl_attribs_rsp);
1231 dma.va = dma_alloc_coherent(&dev->nic_info.pdev->dev,
1232 dma.size, &dma.pa, GFP_KERNEL);
1236 mqe->hdr.pyld_len = dma.size;
1237 mqe->hdr.spcl_sge_cnt_emb |=
1238 (1 << OCRDMA_MQE_HDR_SGE_CNT_SHIFT) &
1239 OCRDMA_MQE_HDR_SGE_CNT_MASK;
1240 mqe->u.nonemb_req.sge[0].pa_lo = (u32) (dma.pa & 0xffffffff);
1241 mqe->u.nonemb_req.sge[0].pa_hi = (u32) upper_32_bits(dma.pa);
1242 mqe->u.nonemb_req.sge[0].len = dma.size;
1244 memset(dma.va, 0, dma.size);
1245 ocrdma_init_mch((struct ocrdma_mbx_hdr *)dma.va,
1246 OCRDMA_CMD_GET_CTRL_ATTRIBUTES,
1247 OCRDMA_SUBSYS_COMMON,
1250 status = ocrdma_nonemb_mbx_cmd(dev, mqe, dma.va);
1252 ctrl_attr_rsp = (struct ocrdma_get_ctrl_attribs_rsp *)dma.va;
1253 hba_attribs = &ctrl_attr_rsp->ctrl_attribs.hba_attribs;
1255 dev->hba_port_num = hba_attribs->phy_port;
1256 strncpy(dev->model_number,
1257 hba_attribs->controller_model_number, 31);
1259 dma_free_coherent(&dev->nic_info.pdev->dev, dma.size, dma.va, dma.pa);
1265 static int ocrdma_mbx_query_dev(struct ocrdma_dev *dev)
1267 int status = -ENOMEM;
1268 struct ocrdma_mbx_query_config *rsp;
1269 struct ocrdma_mqe *cmd;
1271 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_QUERY_CONFIG, sizeof(*cmd));
1274 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1277 rsp = (struct ocrdma_mbx_query_config *)cmd;
1278 ocrdma_get_attr(dev, &dev->attr, rsp);
1284 int ocrdma_mbx_get_link_speed(struct ocrdma_dev *dev, u8 *lnk_speed)
1286 int status = -ENOMEM;
1287 struct ocrdma_get_link_speed_rsp *rsp;
1288 struct ocrdma_mqe *cmd;
1290 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_QUERY_NTWK_LINK_CONFIG_V1,
1294 ocrdma_init_mch((struct ocrdma_mbx_hdr *)&cmd->u.cmd[0],
1295 OCRDMA_CMD_QUERY_NTWK_LINK_CONFIG_V1,
1296 OCRDMA_SUBSYS_COMMON, sizeof(*cmd));
1298 ((struct ocrdma_mbx_hdr *)cmd->u.cmd)->rsvd_version = 0x1;
1300 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1304 rsp = (struct ocrdma_get_link_speed_rsp *)cmd;
1305 *lnk_speed = rsp->phys_port_speed;
1312 static int ocrdma_mbx_get_phy_info(struct ocrdma_dev *dev)
1314 int status = -ENOMEM;
1315 struct ocrdma_mqe *cmd;
1316 struct ocrdma_get_phy_info_rsp *rsp;
1318 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_PHY_DETAILS, sizeof(*cmd));
1322 ocrdma_init_mch((struct ocrdma_mbx_hdr *)&cmd->u.cmd[0],
1323 OCRDMA_CMD_PHY_DETAILS, OCRDMA_SUBSYS_COMMON,
1326 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1330 rsp = (struct ocrdma_get_phy_info_rsp *)cmd;
1331 dev->phy.phy_type = le16_to_cpu(rsp->phy_type);
1332 dev->phy.auto_speeds_supported =
1333 le16_to_cpu(rsp->auto_speeds_supported);
1334 dev->phy.fixed_speeds_supported =
1335 le16_to_cpu(rsp->fixed_speeds_supported);
1341 int ocrdma_mbx_alloc_pd(struct ocrdma_dev *dev, struct ocrdma_pd *pd)
1343 int status = -ENOMEM;
1344 struct ocrdma_alloc_pd *cmd;
1345 struct ocrdma_alloc_pd_rsp *rsp;
1347 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_ALLOC_PD, sizeof(*cmd));
1350 if (pd->dpp_enabled)
1351 cmd->enable_dpp_rsvd |= OCRDMA_ALLOC_PD_ENABLE_DPP;
1352 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1355 rsp = (struct ocrdma_alloc_pd_rsp *)cmd;
1356 pd->id = rsp->dpp_page_pdid & OCRDMA_ALLOC_PD_RSP_PDID_MASK;
1357 if (rsp->dpp_page_pdid & OCRDMA_ALLOC_PD_RSP_DPP) {
1358 pd->dpp_enabled = true;
1359 pd->dpp_page = rsp->dpp_page_pdid >>
1360 OCRDMA_ALLOC_PD_RSP_DPP_PAGE_SHIFT;
1362 pd->dpp_enabled = false;
1370 int ocrdma_mbx_dealloc_pd(struct ocrdma_dev *dev, struct ocrdma_pd *pd)
1372 int status = -ENOMEM;
1373 struct ocrdma_dealloc_pd *cmd;
1375 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_DEALLOC_PD, sizeof(*cmd));
1379 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1384 static int ocrdma_build_q_conf(u32 *num_entries, int entry_size,
1385 int *num_pages, int *page_size)
1390 *num_entries = roundup_pow_of_two(*num_entries);
1391 mem_size = *num_entries * entry_size;
1392 /* find the possible lowest possible multiplier */
1393 for (i = 0; i < OCRDMA_MAX_Q_PAGE_SIZE_CNT; i++) {
1394 if (mem_size <= (OCRDMA_Q_PAGE_BASE_SIZE << i))
1397 if (i >= OCRDMA_MAX_Q_PAGE_SIZE_CNT)
1399 mem_size = roundup(mem_size,
1400 ((OCRDMA_Q_PAGE_BASE_SIZE << i) / OCRDMA_MAX_Q_PAGES));
1402 mem_size / ((OCRDMA_Q_PAGE_BASE_SIZE << i) / OCRDMA_MAX_Q_PAGES);
1403 *page_size = ((OCRDMA_Q_PAGE_BASE_SIZE << i) / OCRDMA_MAX_Q_PAGES);
1404 *num_entries = mem_size / entry_size;
1408 static int ocrdma_mbx_create_ah_tbl(struct ocrdma_dev *dev)
1413 struct ocrdma_create_ah_tbl *cmd;
1414 struct ocrdma_create_ah_tbl_rsp *rsp;
1415 struct pci_dev *pdev = dev->nic_info.pdev;
1417 struct ocrdma_pbe *pbes;
1419 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_CREATE_AH_TBL, sizeof(*cmd));
1423 max_ah = OCRDMA_MAX_AH;
1424 dev->av_tbl.size = sizeof(struct ocrdma_av) * max_ah;
1426 /* number of PBEs in PBL */
1427 cmd->ah_conf = (OCRDMA_AH_TBL_PAGES <<
1428 OCRDMA_CREATE_AH_NUM_PAGES_SHIFT) &
1429 OCRDMA_CREATE_AH_NUM_PAGES_MASK;
1432 for (i = 0; i < OCRDMA_MAX_Q_PAGE_SIZE_CNT; i++) {
1433 if (PAGE_SIZE == (OCRDMA_MIN_Q_PAGE_SIZE << i))
1436 cmd->ah_conf |= (i << OCRDMA_CREATE_AH_PAGE_SIZE_SHIFT) &
1437 OCRDMA_CREATE_AH_PAGE_SIZE_MASK;
1440 cmd->ah_conf |= (sizeof(struct ocrdma_av) <<
1441 OCRDMA_CREATE_AH_ENTRY_SIZE_SHIFT) &
1442 OCRDMA_CREATE_AH_ENTRY_SIZE_MASK;
1444 dev->av_tbl.pbl.va = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
1445 &dev->av_tbl.pbl.pa,
1447 if (dev->av_tbl.pbl.va == NULL)
1450 dev->av_tbl.va = dma_alloc_coherent(&pdev->dev, dev->av_tbl.size,
1452 if (dev->av_tbl.va == NULL)
1454 dev->av_tbl.pa = pa;
1455 dev->av_tbl.num_ah = max_ah;
1456 memset(dev->av_tbl.va, 0, dev->av_tbl.size);
1458 pbes = (struct ocrdma_pbe *)dev->av_tbl.pbl.va;
1459 for (i = 0; i < dev->av_tbl.size / OCRDMA_MIN_Q_PAGE_SIZE; i++) {
1460 pbes[i].pa_lo = (u32) (pa & 0xffffffff);
1461 pbes[i].pa_hi = (u32) upper_32_bits(pa);
1464 cmd->tbl_addr[0].lo = (u32)(dev->av_tbl.pbl.pa & 0xFFFFFFFF);
1465 cmd->tbl_addr[0].hi = (u32)upper_32_bits(dev->av_tbl.pbl.pa);
1466 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1469 rsp = (struct ocrdma_create_ah_tbl_rsp *)cmd;
1470 dev->av_tbl.ahid = rsp->ahid & 0xFFFF;
1475 dma_free_coherent(&pdev->dev, dev->av_tbl.size, dev->av_tbl.va,
1477 dev->av_tbl.va = NULL;
1479 dma_free_coherent(&pdev->dev, PAGE_SIZE, dev->av_tbl.pbl.va,
1480 dev->av_tbl.pbl.pa);
1481 dev->av_tbl.pbl.va = NULL;
1482 dev->av_tbl.size = 0;
1488 static void ocrdma_mbx_delete_ah_tbl(struct ocrdma_dev *dev)
1490 struct ocrdma_delete_ah_tbl *cmd;
1491 struct pci_dev *pdev = dev->nic_info.pdev;
1493 if (dev->av_tbl.va == NULL)
1496 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_DELETE_AH_TBL, sizeof(*cmd));
1499 cmd->ahid = dev->av_tbl.ahid;
1501 ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1502 dma_free_coherent(&pdev->dev, dev->av_tbl.size, dev->av_tbl.va,
1504 dma_free_coherent(&pdev->dev, PAGE_SIZE, dev->av_tbl.pbl.va,
1505 dev->av_tbl.pbl.pa);
1509 /* Multiple CQs uses the EQ. This routine returns least used
1510 * EQ to associate with CQ. This will distributes the interrupt
1511 * processing and CPU load to associated EQ, vector and so to that CPU.
1513 static u16 ocrdma_bind_eq(struct ocrdma_dev *dev)
1515 int i, selected_eq = 0, cq_cnt = 0;
1518 mutex_lock(&dev->dev_lock);
1519 cq_cnt = dev->eq_tbl[0].cq_cnt;
1520 eq_id = dev->eq_tbl[0].q.id;
1521 /* find the EQ which is has the least number of
1522 * CQs associated with it.
1524 for (i = 0; i < dev->eq_cnt; i++) {
1525 if (dev->eq_tbl[i].cq_cnt < cq_cnt) {
1526 cq_cnt = dev->eq_tbl[i].cq_cnt;
1527 eq_id = dev->eq_tbl[i].q.id;
1531 dev->eq_tbl[selected_eq].cq_cnt += 1;
1532 mutex_unlock(&dev->dev_lock);
1536 static void ocrdma_unbind_eq(struct ocrdma_dev *dev, u16 eq_id)
1540 mutex_lock(&dev->dev_lock);
1541 i = ocrdma_get_eq_table_index(dev, eq_id);
1544 dev->eq_tbl[i].cq_cnt -= 1;
1545 mutex_unlock(&dev->dev_lock);
1548 int ocrdma_mbx_create_cq(struct ocrdma_dev *dev, struct ocrdma_cq *cq,
1549 int entries, int dpp_cq, u16 pd_id)
1551 int status = -ENOMEM; int max_hw_cqe;
1552 struct pci_dev *pdev = dev->nic_info.pdev;
1553 struct ocrdma_create_cq *cmd;
1554 struct ocrdma_create_cq_rsp *rsp;
1555 u32 hw_pages, cqe_size, page_size, cqe_count;
1557 if (entries > dev->attr.max_cqe) {
1558 pr_err("%s(%d) max_cqe=0x%x, requester_cqe=0x%x\n",
1559 __func__, dev->id, dev->attr.max_cqe, entries);
1562 if (dpp_cq && (ocrdma_get_asic_type(dev) != OCRDMA_ASIC_GEN_SKH_R))
1568 cqe_size = OCRDMA_DPP_CQE_SIZE;
1571 cq->max_hw_cqe = dev->attr.max_cqe;
1572 max_hw_cqe = dev->attr.max_cqe;
1573 cqe_size = sizeof(struct ocrdma_cqe);
1574 hw_pages = OCRDMA_CREATE_CQ_MAX_PAGES;
1577 cq->len = roundup(max_hw_cqe * cqe_size, OCRDMA_MIN_Q_PAGE_SIZE);
1579 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_CREATE_CQ, sizeof(*cmd));
1582 ocrdma_init_mch(&cmd->cmd.req, OCRDMA_CMD_CREATE_CQ,
1583 OCRDMA_SUBSYS_COMMON, sizeof(*cmd));
1584 cq->va = dma_alloc_coherent(&pdev->dev, cq->len, &cq->pa, GFP_KERNEL);
1589 memset(cq->va, 0, cq->len);
1590 page_size = cq->len / hw_pages;
1591 cmd->cmd.pgsz_pgcnt = (page_size / OCRDMA_MIN_Q_PAGE_SIZE) <<
1592 OCRDMA_CREATE_CQ_PAGE_SIZE_SHIFT;
1593 cmd->cmd.pgsz_pgcnt |= hw_pages;
1594 cmd->cmd.ev_cnt_flags = OCRDMA_CREATE_CQ_DEF_FLAGS;
1596 cq->eqn = ocrdma_bind_eq(dev);
1597 cmd->cmd.req.rsvd_version = OCRDMA_CREATE_CQ_VER3;
1598 cqe_count = cq->len / cqe_size;
1599 cq->cqe_cnt = cqe_count;
1600 if (cqe_count > 1024) {
1601 /* Set cnt to 3 to indicate more than 1024 cq entries */
1602 cmd->cmd.ev_cnt_flags |= (0x3 << OCRDMA_CREATE_CQ_CNT_SHIFT);
1605 switch (cqe_count) {
1618 cmd->cmd.ev_cnt_flags |= (count << OCRDMA_CREATE_CQ_CNT_SHIFT);
1620 /* shared eq between all the consumer cqs. */
1621 cmd->cmd.eqn = cq->eqn;
1622 if (ocrdma_get_asic_type(dev) == OCRDMA_ASIC_GEN_SKH_R) {
1624 cmd->cmd.pgsz_pgcnt |= OCRDMA_CREATE_CQ_DPP <<
1625 OCRDMA_CREATE_CQ_TYPE_SHIFT;
1626 cq->phase_change = false;
1627 cmd->cmd.cqe_count = (cq->len / cqe_size);
1629 cmd->cmd.cqe_count = (cq->len / cqe_size) - 1;
1630 cmd->cmd.ev_cnt_flags |= OCRDMA_CREATE_CQ_FLAGS_AUTO_VALID;
1631 cq->phase_change = true;
1634 cmd->cmd.pd_id = pd_id; /* valid only for v3 */
1635 ocrdma_build_q_pages(&cmd->cmd.pa[0], hw_pages, cq->pa, page_size);
1636 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1640 rsp = (struct ocrdma_create_cq_rsp *)cmd;
1641 cq->id = (u16) (rsp->rsp.cq_id & OCRDMA_CREATE_CQ_RSP_CQ_ID_MASK);
1645 ocrdma_unbind_eq(dev, cq->eqn);
1646 dma_free_coherent(&pdev->dev, cq->len, cq->va, cq->pa);
1652 int ocrdma_mbx_destroy_cq(struct ocrdma_dev *dev, struct ocrdma_cq *cq)
1654 int status = -ENOMEM;
1655 struct ocrdma_destroy_cq *cmd;
1657 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_DELETE_CQ, sizeof(*cmd));
1660 ocrdma_init_mch(&cmd->req, OCRDMA_CMD_DELETE_CQ,
1661 OCRDMA_SUBSYS_COMMON, sizeof(*cmd));
1663 cmd->bypass_flush_qid |=
1664 (cq->id << OCRDMA_DESTROY_CQ_QID_SHIFT) &
1665 OCRDMA_DESTROY_CQ_QID_MASK;
1667 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1668 ocrdma_unbind_eq(dev, cq->eqn);
1669 dma_free_coherent(&dev->nic_info.pdev->dev, cq->len, cq->va, cq->pa);
1674 int ocrdma_mbx_alloc_lkey(struct ocrdma_dev *dev, struct ocrdma_hw_mr *hwmr,
1675 u32 pdid, int addr_check)
1677 int status = -ENOMEM;
1678 struct ocrdma_alloc_lkey *cmd;
1679 struct ocrdma_alloc_lkey_rsp *rsp;
1681 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_ALLOC_LKEY, sizeof(*cmd));
1685 cmd->pbl_sz_flags |= addr_check;
1686 cmd->pbl_sz_flags |= (hwmr->fr_mr << OCRDMA_ALLOC_LKEY_FMR_SHIFT);
1687 cmd->pbl_sz_flags |=
1688 (hwmr->remote_wr << OCRDMA_ALLOC_LKEY_REMOTE_WR_SHIFT);
1689 cmd->pbl_sz_flags |=
1690 (hwmr->remote_rd << OCRDMA_ALLOC_LKEY_REMOTE_RD_SHIFT);
1691 cmd->pbl_sz_flags |=
1692 (hwmr->local_wr << OCRDMA_ALLOC_LKEY_LOCAL_WR_SHIFT);
1693 cmd->pbl_sz_flags |=
1694 (hwmr->remote_atomic << OCRDMA_ALLOC_LKEY_REMOTE_ATOMIC_SHIFT);
1695 cmd->pbl_sz_flags |=
1696 (hwmr->num_pbls << OCRDMA_ALLOC_LKEY_PBL_SIZE_SHIFT);
1698 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1701 rsp = (struct ocrdma_alloc_lkey_rsp *)cmd;
1702 hwmr->lkey = rsp->lrkey;
1708 int ocrdma_mbx_dealloc_lkey(struct ocrdma_dev *dev, int fr_mr, u32 lkey)
1710 int status = -ENOMEM;
1711 struct ocrdma_dealloc_lkey *cmd;
1713 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_DEALLOC_LKEY, sizeof(*cmd));
1717 cmd->rsvd_frmr = fr_mr ? 1 : 0;
1718 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1726 static int ocrdma_mbx_reg_mr(struct ocrdma_dev *dev, struct ocrdma_hw_mr *hwmr,
1727 u32 pdid, u32 pbl_cnt, u32 pbe_size, u32 last)
1729 int status = -ENOMEM;
1731 struct ocrdma_reg_nsmr *cmd;
1732 struct ocrdma_reg_nsmr_rsp *rsp;
1734 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_REGISTER_NSMR, sizeof(*cmd));
1738 pdid | (hwmr->num_pbls << OCRDMA_REG_NSMR_NUM_PBL_SHIFT);
1739 cmd->fr_mr = hwmr->fr_mr;
1741 cmd->flags_hpage_pbe_sz |= (hwmr->remote_wr <<
1742 OCRDMA_REG_NSMR_REMOTE_WR_SHIFT);
1743 cmd->flags_hpage_pbe_sz |= (hwmr->remote_rd <<
1744 OCRDMA_REG_NSMR_REMOTE_RD_SHIFT);
1745 cmd->flags_hpage_pbe_sz |= (hwmr->local_wr <<
1746 OCRDMA_REG_NSMR_LOCAL_WR_SHIFT);
1747 cmd->flags_hpage_pbe_sz |= (hwmr->remote_atomic <<
1748 OCRDMA_REG_NSMR_REMOTE_ATOMIC_SHIFT);
1749 cmd->flags_hpage_pbe_sz |= (hwmr->mw_bind <<
1750 OCRDMA_REG_NSMR_BIND_MEMWIN_SHIFT);
1751 cmd->flags_hpage_pbe_sz |= (last << OCRDMA_REG_NSMR_LAST_SHIFT);
1753 cmd->flags_hpage_pbe_sz |= (hwmr->pbe_size / OCRDMA_MIN_HPAGE_SIZE);
1754 cmd->flags_hpage_pbe_sz |= (hwmr->pbl_size / OCRDMA_MIN_HPAGE_SIZE) <<
1755 OCRDMA_REG_NSMR_HPAGE_SIZE_SHIFT;
1756 cmd->totlen_low = hwmr->len;
1757 cmd->totlen_high = upper_32_bits(hwmr->len);
1758 cmd->fbo_low = (u32) (hwmr->fbo & 0xffffffff);
1759 cmd->fbo_high = (u32) upper_32_bits(hwmr->fbo);
1760 cmd->va_loaddr = (u32) hwmr->va;
1761 cmd->va_hiaddr = (u32) upper_32_bits(hwmr->va);
1763 for (i = 0; i < pbl_cnt; i++) {
1764 cmd->pbl[i].lo = (u32) (hwmr->pbl_table[i].pa & 0xffffffff);
1765 cmd->pbl[i].hi = upper_32_bits(hwmr->pbl_table[i].pa);
1767 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1770 rsp = (struct ocrdma_reg_nsmr_rsp *)cmd;
1771 hwmr->lkey = rsp->lrkey;
1777 static int ocrdma_mbx_reg_mr_cont(struct ocrdma_dev *dev,
1778 struct ocrdma_hw_mr *hwmr, u32 pbl_cnt,
1779 u32 pbl_offset, u32 last)
1781 int status = -ENOMEM;
1783 struct ocrdma_reg_nsmr_cont *cmd;
1785 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_REGISTER_NSMR_CONT, sizeof(*cmd));
1788 cmd->lrkey = hwmr->lkey;
1789 cmd->num_pbl_offset = (pbl_cnt << OCRDMA_REG_NSMR_CONT_NUM_PBL_SHIFT) |
1790 (pbl_offset & OCRDMA_REG_NSMR_CONT_PBL_SHIFT_MASK);
1791 cmd->last = last << OCRDMA_REG_NSMR_CONT_LAST_SHIFT;
1793 for (i = 0; i < pbl_cnt; i++) {
1795 (u32) (hwmr->pbl_table[i + pbl_offset].pa & 0xffffffff);
1797 upper_32_bits(hwmr->pbl_table[i + pbl_offset].pa);
1799 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1807 int ocrdma_reg_mr(struct ocrdma_dev *dev,
1808 struct ocrdma_hw_mr *hwmr, u32 pdid, int acc)
1812 u32 cur_pbl_cnt, pbl_offset;
1813 u32 pending_pbl_cnt = hwmr->num_pbls;
1816 cur_pbl_cnt = min(pending_pbl_cnt, MAX_OCRDMA_NSMR_PBL);
1817 if (cur_pbl_cnt == pending_pbl_cnt)
1820 status = ocrdma_mbx_reg_mr(dev, hwmr, pdid,
1821 cur_pbl_cnt, hwmr->pbe_size, last);
1823 pr_err("%s() status=%d\n", __func__, status);
1826 /* if there is no more pbls to register then exit. */
1831 pbl_offset += cur_pbl_cnt;
1832 pending_pbl_cnt -= cur_pbl_cnt;
1833 cur_pbl_cnt = min(pending_pbl_cnt, MAX_OCRDMA_NSMR_PBL);
1834 /* if we reach the end of the pbls, then need to set the last
1835 * bit, indicating no more pbls to register for this memory key.
1837 if (cur_pbl_cnt == pending_pbl_cnt)
1840 status = ocrdma_mbx_reg_mr_cont(dev, hwmr, cur_pbl_cnt,
1846 pr_err("%s() err. status=%d\n", __func__, status);
1851 bool ocrdma_is_qp_in_sq_flushlist(struct ocrdma_cq *cq, struct ocrdma_qp *qp)
1853 struct ocrdma_qp *tmp;
1855 list_for_each_entry(tmp, &cq->sq_head, sq_entry) {
1864 bool ocrdma_is_qp_in_rq_flushlist(struct ocrdma_cq *cq, struct ocrdma_qp *qp)
1866 struct ocrdma_qp *tmp;
1868 list_for_each_entry(tmp, &cq->rq_head, rq_entry) {
1877 void ocrdma_flush_qp(struct ocrdma_qp *qp)
1880 unsigned long flags;
1882 spin_lock_irqsave(&qp->dev->flush_q_lock, flags);
1883 found = ocrdma_is_qp_in_sq_flushlist(qp->sq_cq, qp);
1885 list_add_tail(&qp->sq_entry, &qp->sq_cq->sq_head);
1887 found = ocrdma_is_qp_in_rq_flushlist(qp->rq_cq, qp);
1889 list_add_tail(&qp->rq_entry, &qp->rq_cq->rq_head);
1891 spin_unlock_irqrestore(&qp->dev->flush_q_lock, flags);
1894 static void ocrdma_init_hwq_ptr(struct ocrdma_qp *qp)
1902 int ocrdma_qp_state_change(struct ocrdma_qp *qp, enum ib_qp_state new_ib_state,
1903 enum ib_qp_state *old_ib_state)
1905 unsigned long flags;
1907 enum ocrdma_qp_state new_state;
1908 new_state = get_ocrdma_qp_state(new_ib_state);
1910 /* sync with wqe and rqe posting */
1911 spin_lock_irqsave(&qp->q_lock, flags);
1914 *old_ib_state = get_ibqp_state(qp->state);
1915 if (new_state == qp->state) {
1916 spin_unlock_irqrestore(&qp->q_lock, flags);
1921 if (new_state == OCRDMA_QPS_INIT) {
1922 ocrdma_init_hwq_ptr(qp);
1923 ocrdma_del_flush_qp(qp);
1924 } else if (new_state == OCRDMA_QPS_ERR) {
1925 ocrdma_flush_qp(qp);
1928 qp->state = new_state;
1930 spin_unlock_irqrestore(&qp->q_lock, flags);
1934 static u32 ocrdma_set_create_qp_mbx_access_flags(struct ocrdma_qp *qp)
1937 if (qp->cap_flags & OCRDMA_QP_INB_RD)
1938 flags |= OCRDMA_CREATE_QP_REQ_INB_RDEN_MASK;
1939 if (qp->cap_flags & OCRDMA_QP_INB_WR)
1940 flags |= OCRDMA_CREATE_QP_REQ_INB_WREN_MASK;
1941 if (qp->cap_flags & OCRDMA_QP_MW_BIND)
1942 flags |= OCRDMA_CREATE_QP_REQ_BIND_MEMWIN_MASK;
1943 if (qp->cap_flags & OCRDMA_QP_LKEY0)
1944 flags |= OCRDMA_CREATE_QP_REQ_ZERO_LKEYEN_MASK;
1945 if (qp->cap_flags & OCRDMA_QP_FAST_REG)
1946 flags |= OCRDMA_CREATE_QP_REQ_FMR_EN_MASK;
1950 static int ocrdma_set_create_qp_sq_cmd(struct ocrdma_create_qp_req *cmd,
1951 struct ib_qp_init_attr *attrs,
1952 struct ocrdma_qp *qp)
1955 u32 len, hw_pages, hw_page_size;
1957 struct ocrdma_dev *dev = qp->dev;
1958 struct pci_dev *pdev = dev->nic_info.pdev;
1959 u32 max_wqe_allocated;
1960 u32 max_sges = attrs->cap.max_send_sge;
1962 /* QP1 may exceed 127 */
1963 max_wqe_allocated = min_t(u32, attrs->cap.max_send_wr + 1,
1966 status = ocrdma_build_q_conf(&max_wqe_allocated,
1967 dev->attr.wqe_size, &hw_pages, &hw_page_size);
1969 pr_err("%s() req. max_send_wr=0x%x\n", __func__,
1973 qp->sq.max_cnt = max_wqe_allocated;
1974 len = (hw_pages * hw_page_size);
1976 qp->sq.va = dma_alloc_coherent(&pdev->dev, len, &pa, GFP_KERNEL);
1979 memset(qp->sq.va, 0, len);
1982 qp->sq.entry_size = dev->attr.wqe_size;
1983 ocrdma_build_q_pages(&cmd->wq_addr[0], hw_pages, pa, hw_page_size);
1985 cmd->type_pgsz_pdn |= (ilog2(hw_page_size / OCRDMA_MIN_Q_PAGE_SIZE)
1986 << OCRDMA_CREATE_QP_REQ_SQ_PAGE_SIZE_SHIFT);
1987 cmd->num_wq_rq_pages |= (hw_pages <<
1988 OCRDMA_CREATE_QP_REQ_NUM_WQ_PAGES_SHIFT) &
1989 OCRDMA_CREATE_QP_REQ_NUM_WQ_PAGES_MASK;
1990 cmd->max_sge_send_write |= (max_sges <<
1991 OCRDMA_CREATE_QP_REQ_MAX_SGE_SEND_SHIFT) &
1992 OCRDMA_CREATE_QP_REQ_MAX_SGE_SEND_MASK;
1993 cmd->max_sge_send_write |= (max_sges <<
1994 OCRDMA_CREATE_QP_REQ_MAX_SGE_WRITE_SHIFT) &
1995 OCRDMA_CREATE_QP_REQ_MAX_SGE_WRITE_MASK;
1996 cmd->max_wqe_rqe |= (ilog2(qp->sq.max_cnt) <<
1997 OCRDMA_CREATE_QP_REQ_MAX_WQE_SHIFT) &
1998 OCRDMA_CREATE_QP_REQ_MAX_WQE_MASK;
1999 cmd->wqe_rqe_size |= (dev->attr.wqe_size <<
2000 OCRDMA_CREATE_QP_REQ_WQE_SIZE_SHIFT) &
2001 OCRDMA_CREATE_QP_REQ_WQE_SIZE_MASK;
2005 static int ocrdma_set_create_qp_rq_cmd(struct ocrdma_create_qp_req *cmd,
2006 struct ib_qp_init_attr *attrs,
2007 struct ocrdma_qp *qp)
2010 u32 len, hw_pages, hw_page_size;
2012 struct ocrdma_dev *dev = qp->dev;
2013 struct pci_dev *pdev = dev->nic_info.pdev;
2014 u32 max_rqe_allocated = attrs->cap.max_recv_wr + 1;
2016 status = ocrdma_build_q_conf(&max_rqe_allocated, dev->attr.rqe_size,
2017 &hw_pages, &hw_page_size);
2019 pr_err("%s() req. max_recv_wr=0x%x\n", __func__,
2020 attrs->cap.max_recv_wr + 1);
2023 qp->rq.max_cnt = max_rqe_allocated;
2024 len = (hw_pages * hw_page_size);
2026 qp->rq.va = dma_alloc_coherent(&pdev->dev, len, &pa, GFP_KERNEL);
2029 memset(qp->rq.va, 0, len);
2032 qp->rq.entry_size = dev->attr.rqe_size;
2034 ocrdma_build_q_pages(&cmd->rq_addr[0], hw_pages, pa, hw_page_size);
2035 cmd->type_pgsz_pdn |= (ilog2(hw_page_size / OCRDMA_MIN_Q_PAGE_SIZE) <<
2036 OCRDMA_CREATE_QP_REQ_RQ_PAGE_SIZE_SHIFT);
2037 cmd->num_wq_rq_pages |=
2038 (hw_pages << OCRDMA_CREATE_QP_REQ_NUM_RQ_PAGES_SHIFT) &
2039 OCRDMA_CREATE_QP_REQ_NUM_RQ_PAGES_MASK;
2040 cmd->max_sge_recv_flags |= (attrs->cap.max_recv_sge <<
2041 OCRDMA_CREATE_QP_REQ_MAX_SGE_RECV_SHIFT) &
2042 OCRDMA_CREATE_QP_REQ_MAX_SGE_RECV_MASK;
2043 cmd->max_wqe_rqe |= (ilog2(qp->rq.max_cnt) <<
2044 OCRDMA_CREATE_QP_REQ_MAX_RQE_SHIFT) &
2045 OCRDMA_CREATE_QP_REQ_MAX_RQE_MASK;
2046 cmd->wqe_rqe_size |= (dev->attr.rqe_size <<
2047 OCRDMA_CREATE_QP_REQ_RQE_SIZE_SHIFT) &
2048 OCRDMA_CREATE_QP_REQ_RQE_SIZE_MASK;
2052 static void ocrdma_set_create_qp_dpp_cmd(struct ocrdma_create_qp_req *cmd,
2053 struct ocrdma_pd *pd,
2054 struct ocrdma_qp *qp,
2055 u8 enable_dpp_cq, u16 dpp_cq_id)
2058 qp->dpp_enabled = true;
2059 cmd->max_sge_recv_flags |= OCRDMA_CREATE_QP_REQ_ENABLE_DPP_MASK;
2062 cmd->max_sge_recv_flags |= OCRDMA_CREATE_QP_REQ_ENABLE_DPP_MASK;
2063 cmd->dpp_credits_cqid = dpp_cq_id;
2064 cmd->dpp_credits_cqid |= OCRDMA_CREATE_QP_REQ_DPP_CREDIT_LIMIT <<
2065 OCRDMA_CREATE_QP_REQ_DPP_CREDIT_SHIFT;
2068 static int ocrdma_set_create_qp_ird_cmd(struct ocrdma_create_qp_req *cmd,
2069 struct ocrdma_qp *qp)
2071 struct ocrdma_dev *dev = qp->dev;
2072 struct pci_dev *pdev = dev->nic_info.pdev;
2074 int ird_page_size = dev->attr.ird_page_size;
2075 int ird_q_len = dev->attr.num_ird_pages * ird_page_size;
2076 struct ocrdma_hdr_wqe *rqe;
2079 if (dev->attr.ird == 0)
2082 qp->ird_q_va = dma_alloc_coherent(&pdev->dev, ird_q_len,
2086 memset(qp->ird_q_va, 0, ird_q_len);
2087 ocrdma_build_q_pages(&cmd->ird_addr[0], dev->attr.num_ird_pages,
2089 for (; i < ird_q_len / dev->attr.rqe_size; i++) {
2090 rqe = (struct ocrdma_hdr_wqe *)(qp->ird_q_va +
2091 (i * dev->attr.rqe_size));
2094 rqe->cw |= (OCRDMA_TYPE_LKEY << OCRDMA_WQE_TYPE_SHIFT);
2095 rqe->cw |= (8 << OCRDMA_WQE_SIZE_SHIFT);
2096 rqe->cw |= (8 << OCRDMA_WQE_NXT_WQE_SIZE_SHIFT);
2101 static void ocrdma_get_create_qp_rsp(struct ocrdma_create_qp_rsp *rsp,
2102 struct ocrdma_qp *qp,
2103 struct ib_qp_init_attr *attrs,
2104 u16 *dpp_offset, u16 *dpp_credit_lmt)
2106 u32 max_wqe_allocated, max_rqe_allocated;
2107 qp->id = rsp->qp_id & OCRDMA_CREATE_QP_RSP_QP_ID_MASK;
2108 qp->rq.dbid = rsp->sq_rq_id & OCRDMA_CREATE_QP_RSP_RQ_ID_MASK;
2109 qp->sq.dbid = rsp->sq_rq_id >> OCRDMA_CREATE_QP_RSP_SQ_ID_SHIFT;
2110 qp->max_ird = rsp->max_ord_ird & OCRDMA_CREATE_QP_RSP_MAX_IRD_MASK;
2111 qp->max_ord = (rsp->max_ord_ird >> OCRDMA_CREATE_QP_RSP_MAX_ORD_SHIFT);
2112 qp->dpp_enabled = false;
2113 if (rsp->dpp_response & OCRDMA_CREATE_QP_RSP_DPP_ENABLED_MASK) {
2114 qp->dpp_enabled = true;
2115 *dpp_credit_lmt = (rsp->dpp_response &
2116 OCRDMA_CREATE_QP_RSP_DPP_CREDITS_MASK) >>
2117 OCRDMA_CREATE_QP_RSP_DPP_CREDITS_SHIFT;
2118 *dpp_offset = (rsp->dpp_response &
2119 OCRDMA_CREATE_QP_RSP_DPP_PAGE_OFFSET_MASK) >>
2120 OCRDMA_CREATE_QP_RSP_DPP_PAGE_OFFSET_SHIFT;
2123 rsp->max_wqe_rqe >> OCRDMA_CREATE_QP_RSP_MAX_WQE_SHIFT;
2124 max_wqe_allocated = 1 << max_wqe_allocated;
2125 max_rqe_allocated = 1 << ((u16)rsp->max_wqe_rqe);
2127 qp->sq.max_cnt = max_wqe_allocated;
2128 qp->sq.max_wqe_idx = max_wqe_allocated - 1;
2131 qp->rq.max_cnt = max_rqe_allocated;
2132 qp->rq.max_wqe_idx = max_rqe_allocated - 1;
2136 int ocrdma_mbx_create_qp(struct ocrdma_qp *qp, struct ib_qp_init_attr *attrs,
2137 u8 enable_dpp_cq, u16 dpp_cq_id, u16 *dpp_offset,
2138 u16 *dpp_credit_lmt)
2140 int status = -ENOMEM;
2142 struct ocrdma_dev *dev = qp->dev;
2143 struct ocrdma_pd *pd = qp->pd;
2144 struct pci_dev *pdev = dev->nic_info.pdev;
2145 struct ocrdma_cq *cq;
2146 struct ocrdma_create_qp_req *cmd;
2147 struct ocrdma_create_qp_rsp *rsp;
2150 switch (attrs->qp_type) {
2152 qptype = OCRDMA_QPT_GSI;
2155 qptype = OCRDMA_QPT_RC;
2158 qptype = OCRDMA_QPT_UD;
2164 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_CREATE_QP, sizeof(*cmd));
2167 cmd->type_pgsz_pdn |= (qptype << OCRDMA_CREATE_QP_REQ_QPT_SHIFT) &
2168 OCRDMA_CREATE_QP_REQ_QPT_MASK;
2169 status = ocrdma_set_create_qp_sq_cmd(cmd, attrs, qp);
2174 struct ocrdma_srq *srq = get_ocrdma_srq(attrs->srq);
2175 cmd->max_sge_recv_flags |= OCRDMA_CREATE_QP_REQ_USE_SRQ_MASK;
2176 cmd->rq_addr[0].lo = srq->id;
2179 status = ocrdma_set_create_qp_rq_cmd(cmd, attrs, qp);
2184 status = ocrdma_set_create_qp_ird_cmd(cmd, qp);
2188 cmd->type_pgsz_pdn |= (pd->id << OCRDMA_CREATE_QP_REQ_PD_ID_SHIFT) &
2189 OCRDMA_CREATE_QP_REQ_PD_ID_MASK;
2191 flags = ocrdma_set_create_qp_mbx_access_flags(qp);
2193 cmd->max_sge_recv_flags |= flags;
2194 cmd->max_ord_ird |= (dev->attr.max_ord_per_qp <<
2195 OCRDMA_CREATE_QP_REQ_MAX_ORD_SHIFT) &
2196 OCRDMA_CREATE_QP_REQ_MAX_ORD_MASK;
2197 cmd->max_ord_ird |= (dev->attr.max_ird_per_qp <<
2198 OCRDMA_CREATE_QP_REQ_MAX_IRD_SHIFT) &
2199 OCRDMA_CREATE_QP_REQ_MAX_IRD_MASK;
2200 cq = get_ocrdma_cq(attrs->send_cq);
2201 cmd->wq_rq_cqid |= (cq->id << OCRDMA_CREATE_QP_REQ_WQ_CQID_SHIFT) &
2202 OCRDMA_CREATE_QP_REQ_WQ_CQID_MASK;
2204 cq = get_ocrdma_cq(attrs->recv_cq);
2205 cmd->wq_rq_cqid |= (cq->id << OCRDMA_CREATE_QP_REQ_RQ_CQID_SHIFT) &
2206 OCRDMA_CREATE_QP_REQ_RQ_CQID_MASK;
2209 if (pd->dpp_enabled && attrs->cap.max_inline_data && pd->num_dpp_qp &&
2210 (attrs->cap.max_inline_data <= dev->attr.max_inline_data)) {
2211 ocrdma_set_create_qp_dpp_cmd(cmd, pd, qp, enable_dpp_cq,
2215 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
2218 rsp = (struct ocrdma_create_qp_rsp *)cmd;
2219 ocrdma_get_create_qp_rsp(rsp, qp, attrs, dpp_offset, dpp_credit_lmt);
2220 qp->state = OCRDMA_QPS_RST;
2225 dma_free_coherent(&pdev->dev, qp->rq.len, qp->rq.va, qp->rq.pa);
2227 pr_err("%s(%d) rq_err\n", __func__, dev->id);
2228 dma_free_coherent(&pdev->dev, qp->sq.len, qp->sq.va, qp->sq.pa);
2230 pr_err("%s(%d) sq_err\n", __func__, dev->id);
2235 int ocrdma_mbx_query_qp(struct ocrdma_dev *dev, struct ocrdma_qp *qp,
2236 struct ocrdma_qp_params *param)
2238 int status = -ENOMEM;
2239 struct ocrdma_query_qp *cmd;
2240 struct ocrdma_query_qp_rsp *rsp;
2242 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_QUERY_QP, sizeof(*cmd));
2245 cmd->qp_id = qp->id;
2246 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
2249 rsp = (struct ocrdma_query_qp_rsp *)cmd;
2250 memcpy(param, &rsp->params, sizeof(struct ocrdma_qp_params));
2256 static int ocrdma_set_av_params(struct ocrdma_qp *qp,
2257 struct ocrdma_modify_qp *cmd,
2258 struct ib_qp_attr *attrs)
2261 struct ib_ah_attr *ah_attr = &attrs->ah_attr;
2262 union ib_gid sgid, zgid;
2266 if ((ah_attr->ah_flags & IB_AH_GRH) == 0)
2268 cmd->params.tclass_sq_psn |=
2269 (ah_attr->grh.traffic_class << OCRDMA_QP_PARAMS_TCLASS_SHIFT);
2270 cmd->params.rnt_rc_sl_fl |=
2271 (ah_attr->grh.flow_label & OCRDMA_QP_PARAMS_FLOW_LABEL_MASK);
2272 cmd->params.rnt_rc_sl_fl |= (ah_attr->sl << OCRDMA_QP_PARAMS_SL_SHIFT);
2273 cmd->params.hop_lmt_rq_psn |=
2274 (ah_attr->grh.hop_limit << OCRDMA_QP_PARAMS_HOP_LMT_SHIFT);
2275 cmd->flags |= OCRDMA_QP_PARA_FLOW_LBL_VALID;
2276 memcpy(&cmd->params.dgid[0], &ah_attr->grh.dgid.raw[0],
2277 sizeof(cmd->params.dgid));
2278 status = ocrdma_query_gid(&qp->dev->ibdev, 1,
2279 ah_attr->grh.sgid_index, &sgid);
2283 memset(&zgid, 0, sizeof(zgid));
2284 if (!memcmp(&sgid, &zgid, sizeof(zgid)))
2287 qp->sgid_idx = ah_attr->grh.sgid_index;
2288 memcpy(&cmd->params.sgid[0], &sgid.raw[0], sizeof(cmd->params.sgid));
2289 ocrdma_resolve_dmac(qp->dev, ah_attr, &mac_addr[0]);
2290 cmd->params.dmac_b0_to_b3 = mac_addr[0] | (mac_addr[1] << 8) |
2291 (mac_addr[2] << 16) | (mac_addr[3] << 24);
2292 /* convert them to LE format. */
2293 ocrdma_cpu_to_le32(&cmd->params.dgid[0], sizeof(cmd->params.dgid));
2294 ocrdma_cpu_to_le32(&cmd->params.sgid[0], sizeof(cmd->params.sgid));
2295 cmd->params.vlan_dmac_b4_to_b5 = mac_addr[4] | (mac_addr[5] << 8);
2296 vlan_id = ah_attr->vlan_id;
2297 if (vlan_id && (vlan_id < 0x1000)) {
2298 cmd->params.vlan_dmac_b4_to_b5 |=
2299 vlan_id << OCRDMA_QP_PARAMS_VLAN_SHIFT;
2300 cmd->flags |= OCRDMA_QP_PARA_VLAN_EN_VALID;
2305 static int ocrdma_set_qp_params(struct ocrdma_qp *qp,
2306 struct ocrdma_modify_qp *cmd,
2307 struct ib_qp_attr *attrs, int attr_mask)
2311 if (attr_mask & IB_QP_PKEY_INDEX) {
2312 cmd->params.path_mtu_pkey_indx |= (attrs->pkey_index &
2313 OCRDMA_QP_PARAMS_PKEY_INDEX_MASK);
2314 cmd->flags |= OCRDMA_QP_PARA_PKEY_VALID;
2316 if (attr_mask & IB_QP_QKEY) {
2317 qp->qkey = attrs->qkey;
2318 cmd->params.qkey = attrs->qkey;
2319 cmd->flags |= OCRDMA_QP_PARA_QKEY_VALID;
2321 if (attr_mask & IB_QP_AV) {
2322 status = ocrdma_set_av_params(qp, cmd, attrs);
2325 } else if (qp->qp_type == IB_QPT_GSI || qp->qp_type == IB_QPT_UD) {
2326 /* set the default mac address for UD, GSI QPs */
2327 cmd->params.dmac_b0_to_b3 = qp->dev->nic_info.mac_addr[0] |
2328 (qp->dev->nic_info.mac_addr[1] << 8) |
2329 (qp->dev->nic_info.mac_addr[2] << 16) |
2330 (qp->dev->nic_info.mac_addr[3] << 24);
2331 cmd->params.vlan_dmac_b4_to_b5 = qp->dev->nic_info.mac_addr[4] |
2332 (qp->dev->nic_info.mac_addr[5] << 8);
2334 if ((attr_mask & IB_QP_EN_SQD_ASYNC_NOTIFY) &&
2335 attrs->en_sqd_async_notify) {
2336 cmd->params.max_sge_recv_flags |=
2337 OCRDMA_QP_PARAMS_FLAGS_SQD_ASYNC;
2338 cmd->flags |= OCRDMA_QP_PARA_DST_QPN_VALID;
2340 if (attr_mask & IB_QP_DEST_QPN) {
2341 cmd->params.ack_to_rnr_rtc_dest_qpn |= (attrs->dest_qp_num &
2342 OCRDMA_QP_PARAMS_DEST_QPN_MASK);
2343 cmd->flags |= OCRDMA_QP_PARA_DST_QPN_VALID;
2345 if (attr_mask & IB_QP_PATH_MTU) {
2346 if (attrs->path_mtu < IB_MTU_256 ||
2347 attrs->path_mtu > IB_MTU_4096) {
2351 cmd->params.path_mtu_pkey_indx |=
2352 (ib_mtu_enum_to_int(attrs->path_mtu) <<
2353 OCRDMA_QP_PARAMS_PATH_MTU_SHIFT) &
2354 OCRDMA_QP_PARAMS_PATH_MTU_MASK;
2355 cmd->flags |= OCRDMA_QP_PARA_PMTU_VALID;
2357 if (attr_mask & IB_QP_TIMEOUT) {
2358 cmd->params.ack_to_rnr_rtc_dest_qpn |= attrs->timeout <<
2359 OCRDMA_QP_PARAMS_ACK_TIMEOUT_SHIFT;
2360 cmd->flags |= OCRDMA_QP_PARA_ACK_TO_VALID;
2362 if (attr_mask & IB_QP_RETRY_CNT) {
2363 cmd->params.rnt_rc_sl_fl |= (attrs->retry_cnt <<
2364 OCRDMA_QP_PARAMS_RETRY_CNT_SHIFT) &
2365 OCRDMA_QP_PARAMS_RETRY_CNT_MASK;
2366 cmd->flags |= OCRDMA_QP_PARA_RETRY_CNT_VALID;
2368 if (attr_mask & IB_QP_MIN_RNR_TIMER) {
2369 cmd->params.rnt_rc_sl_fl |= (attrs->min_rnr_timer <<
2370 OCRDMA_QP_PARAMS_RNR_NAK_TIMER_SHIFT) &
2371 OCRDMA_QP_PARAMS_RNR_NAK_TIMER_MASK;
2372 cmd->flags |= OCRDMA_QP_PARA_RNT_VALID;
2374 if (attr_mask & IB_QP_RNR_RETRY) {
2375 cmd->params.ack_to_rnr_rtc_dest_qpn |= (attrs->rnr_retry <<
2376 OCRDMA_QP_PARAMS_RNR_RETRY_CNT_SHIFT)
2377 & OCRDMA_QP_PARAMS_RNR_RETRY_CNT_MASK;
2378 cmd->flags |= OCRDMA_QP_PARA_RRC_VALID;
2380 if (attr_mask & IB_QP_SQ_PSN) {
2381 cmd->params.tclass_sq_psn |= (attrs->sq_psn & 0x00ffffff);
2382 cmd->flags |= OCRDMA_QP_PARA_SQPSN_VALID;
2384 if (attr_mask & IB_QP_RQ_PSN) {
2385 cmd->params.hop_lmt_rq_psn |= (attrs->rq_psn & 0x00ffffff);
2386 cmd->flags |= OCRDMA_QP_PARA_RQPSN_VALID;
2388 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
2389 if (attrs->max_rd_atomic > qp->dev->attr.max_ord_per_qp) {
2393 qp->max_ord = attrs->max_rd_atomic;
2394 cmd->flags |= OCRDMA_QP_PARA_MAX_ORD_VALID;
2396 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
2397 if (attrs->max_dest_rd_atomic > qp->dev->attr.max_ird_per_qp) {
2401 qp->max_ird = attrs->max_dest_rd_atomic;
2402 cmd->flags |= OCRDMA_QP_PARA_MAX_IRD_VALID;
2404 cmd->params.max_ord_ird = (qp->max_ord <<
2405 OCRDMA_QP_PARAMS_MAX_ORD_SHIFT) |
2406 (qp->max_ird & OCRDMA_QP_PARAMS_MAX_IRD_MASK);
2411 int ocrdma_mbx_modify_qp(struct ocrdma_dev *dev, struct ocrdma_qp *qp,
2412 struct ib_qp_attr *attrs, int attr_mask)
2414 int status = -ENOMEM;
2415 struct ocrdma_modify_qp *cmd;
2417 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_MODIFY_QP, sizeof(*cmd));
2421 cmd->params.id = qp->id;
2423 if (attr_mask & IB_QP_STATE) {
2424 cmd->params.max_sge_recv_flags |=
2425 (get_ocrdma_qp_state(attrs->qp_state) <<
2426 OCRDMA_QP_PARAMS_STATE_SHIFT) &
2427 OCRDMA_QP_PARAMS_STATE_MASK;
2428 cmd->flags |= OCRDMA_QP_PARA_QPS_VALID;
2430 cmd->params.max_sge_recv_flags |=
2431 (qp->state << OCRDMA_QP_PARAMS_STATE_SHIFT) &
2432 OCRDMA_QP_PARAMS_STATE_MASK;
2435 status = ocrdma_set_qp_params(qp, cmd, attrs, attr_mask);
2438 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
2447 int ocrdma_mbx_destroy_qp(struct ocrdma_dev *dev, struct ocrdma_qp *qp)
2449 int status = -ENOMEM;
2450 struct ocrdma_destroy_qp *cmd;
2451 struct pci_dev *pdev = dev->nic_info.pdev;
2453 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_DELETE_QP, sizeof(*cmd));
2456 cmd->qp_id = qp->id;
2457 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
2464 dma_free_coherent(&pdev->dev, qp->sq.len, qp->sq.va, qp->sq.pa);
2465 if (!qp->srq && qp->rq.va)
2466 dma_free_coherent(&pdev->dev, qp->rq.len, qp->rq.va, qp->rq.pa);
2467 if (qp->dpp_enabled)
2468 qp->pd->num_dpp_qp++;
2472 int ocrdma_mbx_create_srq(struct ocrdma_dev *dev, struct ocrdma_srq *srq,
2473 struct ib_srq_init_attr *srq_attr,
2474 struct ocrdma_pd *pd)
2476 int status = -ENOMEM;
2477 int hw_pages, hw_page_size;
2479 struct ocrdma_create_srq_rsp *rsp;
2480 struct ocrdma_create_srq *cmd;
2482 struct pci_dev *pdev = dev->nic_info.pdev;
2483 u32 max_rqe_allocated;
2485 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_CREATE_SRQ, sizeof(*cmd));
2489 cmd->pgsz_pdid = pd->id & OCRDMA_CREATE_SRQ_PD_ID_MASK;
2490 max_rqe_allocated = srq_attr->attr.max_wr + 1;
2491 status = ocrdma_build_q_conf(&max_rqe_allocated,
2493 &hw_pages, &hw_page_size);
2495 pr_err("%s() req. max_wr=0x%x\n", __func__,
2496 srq_attr->attr.max_wr);
2500 len = hw_pages * hw_page_size;
2501 srq->rq.va = dma_alloc_coherent(&pdev->dev, len, &pa, GFP_KERNEL);
2506 ocrdma_build_q_pages(&cmd->rq_addr[0], hw_pages, pa, hw_page_size);
2508 srq->rq.entry_size = dev->attr.rqe_size;
2511 srq->rq.max_cnt = max_rqe_allocated;
2513 cmd->max_sge_rqe = ilog2(max_rqe_allocated);
2514 cmd->max_sge_rqe |= srq_attr->attr.max_sge <<
2515 OCRDMA_CREATE_SRQ_MAX_SGE_RECV_SHIFT;
2517 cmd->pgsz_pdid |= (ilog2(hw_page_size / OCRDMA_MIN_Q_PAGE_SIZE)
2518 << OCRDMA_CREATE_SRQ_PG_SZ_SHIFT);
2519 cmd->pages_rqe_sz |= (dev->attr.rqe_size
2520 << OCRDMA_CREATE_SRQ_RQE_SIZE_SHIFT)
2521 & OCRDMA_CREATE_SRQ_RQE_SIZE_MASK;
2522 cmd->pages_rqe_sz |= hw_pages << OCRDMA_CREATE_SRQ_NUM_RQ_PAGES_SHIFT;
2524 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
2527 rsp = (struct ocrdma_create_srq_rsp *)cmd;
2529 srq->rq.dbid = rsp->id;
2530 max_rqe_allocated = ((rsp->max_sge_rqe_allocated &
2531 OCRDMA_CREATE_SRQ_RSP_MAX_RQE_ALLOCATED_MASK) >>
2532 OCRDMA_CREATE_SRQ_RSP_MAX_RQE_ALLOCATED_SHIFT);
2533 max_rqe_allocated = (1 << max_rqe_allocated);
2534 srq->rq.max_cnt = max_rqe_allocated;
2535 srq->rq.max_wqe_idx = max_rqe_allocated - 1;
2536 srq->rq.max_sges = (rsp->max_sge_rqe_allocated &
2537 OCRDMA_CREATE_SRQ_RSP_MAX_SGE_RECV_ALLOCATED_MASK) >>
2538 OCRDMA_CREATE_SRQ_RSP_MAX_SGE_RECV_ALLOCATED_SHIFT;
2541 dma_free_coherent(&pdev->dev, srq->rq.len, srq->rq.va, pa);
2547 int ocrdma_mbx_modify_srq(struct ocrdma_srq *srq, struct ib_srq_attr *srq_attr)
2549 int status = -ENOMEM;
2550 struct ocrdma_modify_srq *cmd;
2551 struct ocrdma_pd *pd = srq->pd;
2552 struct ocrdma_dev *dev = get_ocrdma_dev(pd->ibpd.device);
2554 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_MODIFY_SRQ, sizeof(*cmd));
2558 cmd->limit_max_rqe |= srq_attr->srq_limit <<
2559 OCRDMA_MODIFY_SRQ_LIMIT_SHIFT;
2560 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
2565 int ocrdma_mbx_query_srq(struct ocrdma_srq *srq, struct ib_srq_attr *srq_attr)
2567 int status = -ENOMEM;
2568 struct ocrdma_query_srq *cmd;
2569 struct ocrdma_dev *dev = get_ocrdma_dev(srq->ibsrq.device);
2571 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_QUERY_SRQ, sizeof(*cmd));
2574 cmd->id = srq->rq.dbid;
2575 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
2577 struct ocrdma_query_srq_rsp *rsp =
2578 (struct ocrdma_query_srq_rsp *)cmd;
2580 rsp->srq_lmt_max_sge &
2581 OCRDMA_QUERY_SRQ_RSP_MAX_SGE_RECV_MASK;
2583 rsp->max_rqe_pdid >> OCRDMA_QUERY_SRQ_RSP_MAX_RQE_SHIFT;
2584 srq_attr->srq_limit = rsp->srq_lmt_max_sge >>
2585 OCRDMA_QUERY_SRQ_RSP_SRQ_LIMIT_SHIFT;
2591 int ocrdma_mbx_destroy_srq(struct ocrdma_dev *dev, struct ocrdma_srq *srq)
2593 int status = -ENOMEM;
2594 struct ocrdma_destroy_srq *cmd;
2595 struct pci_dev *pdev = dev->nic_info.pdev;
2596 cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_DELETE_SRQ, sizeof(*cmd));
2600 status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
2602 dma_free_coherent(&pdev->dev, srq->rq.len,
2603 srq->rq.va, srq->rq.pa);
2608 int ocrdma_alloc_av(struct ocrdma_dev *dev, struct ocrdma_ah *ah)
2611 int status = -EINVAL;
2612 struct ocrdma_av *av;
2613 unsigned long flags;
2615 av = dev->av_tbl.va;
2616 spin_lock_irqsave(&dev->av_tbl.lock, flags);
2617 for (i = 0; i < dev->av_tbl.num_ah; i++) {
2618 if (av->valid == 0) {
2619 av->valid = OCRDMA_AV_VALID;
2627 if (i == dev->av_tbl.num_ah)
2629 spin_unlock_irqrestore(&dev->av_tbl.lock, flags);
2633 int ocrdma_free_av(struct ocrdma_dev *dev, struct ocrdma_ah *ah)
2635 unsigned long flags;
2636 spin_lock_irqsave(&dev->av_tbl.lock, flags);
2638 spin_unlock_irqrestore(&dev->av_tbl.lock, flags);
2642 static int ocrdma_create_eqs(struct ocrdma_dev *dev)
2644 int num_eq, i, status = 0;
2646 unsigned long flags = 0;
2648 num_eq = dev->nic_info.msix.num_vectors -
2649 dev->nic_info.msix.start_vector;
2650 if (dev->nic_info.intr_mode == BE_INTERRUPT_MODE_INTX) {
2652 flags = IRQF_SHARED;
2654 num_eq = min_t(u32, num_eq, num_online_cpus());
2660 dev->eq_tbl = kzalloc(sizeof(struct ocrdma_eq) * num_eq, GFP_KERNEL);
2664 for (i = 0; i < num_eq; i++) {
2665 status = ocrdma_create_eq(dev, &dev->eq_tbl[i],
2671 sprintf(dev->eq_tbl[i].irq_name, "ocrdma%d-%d",
2673 irq = ocrdma_get_irq(dev, &dev->eq_tbl[i]);
2674 status = request_irq(irq, ocrdma_irq_handler, flags,
2675 dev->eq_tbl[i].irq_name,
2681 /* one eq is sufficient for data path to work */
2684 ocrdma_destroy_eqs(dev);
2688 int ocrdma_init_hw(struct ocrdma_dev *dev)
2692 /* create the eqs */
2693 status = ocrdma_create_eqs(dev);
2696 status = ocrdma_create_mq(dev);
2699 status = ocrdma_mbx_query_fw_config(dev);
2702 status = ocrdma_mbx_query_dev(dev);
2705 status = ocrdma_mbx_query_fw_ver(dev);
2708 status = ocrdma_mbx_create_ah_tbl(dev);
2711 status = ocrdma_mbx_get_phy_info(dev);
2714 status = ocrdma_mbx_get_ctrl_attribs(dev);
2721 ocrdma_destroy_mq(dev);
2723 ocrdma_destroy_eqs(dev);
2725 pr_err("%s() status=%d\n", __func__, status);
2729 void ocrdma_cleanup_hw(struct ocrdma_dev *dev)
2731 ocrdma_mbx_delete_ah_tbl(dev);
2733 /* cleanup the eqs */
2734 ocrdma_destroy_eqs(dev);
2736 /* cleanup the control path */
2737 ocrdma_destroy_mq(dev);