xref: /openbmc/linux/drivers/infiniband/hw/ocrdma/ocrdma_hw.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
171ee6730SDevesh Sharma /* This file is part of the Emulex RoCE Device Driver for
271ee6730SDevesh Sharma  * RoCE (RDMA over Converged Ethernet) adapters.
371ee6730SDevesh Sharma  * Copyright (C) 2012-2015 Emulex. All rights reserved.
471ee6730SDevesh Sharma  * EMULEX and SLI are trademarks of Emulex.
571ee6730SDevesh Sharma  * www.emulex.com
671ee6730SDevesh Sharma  *
771ee6730SDevesh Sharma  * This software is available to you under a choice of one of two licenses.
871ee6730SDevesh Sharma  * You may choose to be licensed under the terms of the GNU General Public
971ee6730SDevesh Sharma  * License (GPL) Version 2, available from the file COPYING in the main
1071ee6730SDevesh Sharma  * directory of this source tree, or the BSD license below:
1171ee6730SDevesh Sharma  *
1271ee6730SDevesh Sharma  * Redistribution and use in source and binary forms, with or without
1371ee6730SDevesh Sharma  * modification, are permitted provided that the following conditions
1471ee6730SDevesh Sharma  * are met:
1571ee6730SDevesh Sharma  *
1671ee6730SDevesh Sharma  * - Redistributions of source code must retain the above copyright notice,
1771ee6730SDevesh Sharma  *   this list of conditions and the following disclaimer.
1871ee6730SDevesh Sharma  *
1971ee6730SDevesh Sharma  * - Redistributions in binary form must reproduce the above copyright
2071ee6730SDevesh Sharma  *   notice, this list of conditions and the following disclaimer in
2171ee6730SDevesh Sharma  *   the documentation and/or other materials provided with the distribution.
2271ee6730SDevesh Sharma  *
2371ee6730SDevesh Sharma  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
2471ee6730SDevesh Sharma  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,THE
2571ee6730SDevesh Sharma  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2671ee6730SDevesh Sharma  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
2771ee6730SDevesh Sharma  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2871ee6730SDevesh Sharma  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2971ee6730SDevesh Sharma  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
3071ee6730SDevesh Sharma  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
3171ee6730SDevesh Sharma  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
3271ee6730SDevesh Sharma  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
3371ee6730SDevesh Sharma  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34fe2caefcSParav Pandit  *
35fe2caefcSParav Pandit  * Contact Information:
36fe2caefcSParav Pandit  * linux-drivers@emulex.com
37fe2caefcSParav Pandit  *
38fe2caefcSParav Pandit  * Emulex
39fe2caefcSParav Pandit  * 3333 Susan Street
40fe2caefcSParav Pandit  * Costa Mesa, CA 92626
4171ee6730SDevesh Sharma  */
42fe2caefcSParav Pandit 
43fe2caefcSParav Pandit #include <linux/sched.h>
44fe2caefcSParav Pandit #include <linux/interrupt.h>
45fe2caefcSParav Pandit #include <linux/log2.h>
46fe2caefcSParav Pandit #include <linux/dma-mapping.h>
4769ae5439SSelvin Xavier #include <linux/if_ether.h>
48fe2caefcSParav Pandit 
49fe2caefcSParav Pandit #include <rdma/ib_verbs.h>
50fe2caefcSParav Pandit #include <rdma/ib_user_verbs.h>
51dbf727deSMatan Barak #include <rdma/ib_cache.h>
52fe2caefcSParav Pandit 
53fe2caefcSParav Pandit #include "ocrdma.h"
54fe2caefcSParav Pandit #include "ocrdma_hw.h"
55fe2caefcSParav Pandit #include "ocrdma_verbs.h"
56fe2caefcSParav Pandit #include "ocrdma_ah.h"
57fe2caefcSParav Pandit 
58fe2caefcSParav Pandit enum mbx_status {
59fe2caefcSParav Pandit 	OCRDMA_MBX_STATUS_FAILED		= 1,
60fe2caefcSParav Pandit 	OCRDMA_MBX_STATUS_ILLEGAL_FIELD		= 3,
61fe2caefcSParav Pandit 	OCRDMA_MBX_STATUS_OOR			= 100,
62fe2caefcSParav Pandit 	OCRDMA_MBX_STATUS_INVALID_PD		= 101,
63fe2caefcSParav Pandit 	OCRDMA_MBX_STATUS_PD_INUSE		= 102,
64fe2caefcSParav Pandit 	OCRDMA_MBX_STATUS_INVALID_CQ		= 103,
65fe2caefcSParav Pandit 	OCRDMA_MBX_STATUS_INVALID_QP		= 104,
66fe2caefcSParav Pandit 	OCRDMA_MBX_STATUS_INVALID_LKEY		= 105,
67fe2caefcSParav Pandit 	OCRDMA_MBX_STATUS_ORD_EXCEEDS		= 106,
68fe2caefcSParav Pandit 	OCRDMA_MBX_STATUS_IRD_EXCEEDS		= 107,
69fe2caefcSParav Pandit 	OCRDMA_MBX_STATUS_SENDQ_WQE_EXCEEDS	= 108,
70fe2caefcSParav Pandit 	OCRDMA_MBX_STATUS_RECVQ_RQE_EXCEEDS	= 109,
71fe2caefcSParav Pandit 	OCRDMA_MBX_STATUS_SGE_SEND_EXCEEDS	= 110,
72fe2caefcSParav Pandit 	OCRDMA_MBX_STATUS_SGE_WRITE_EXCEEDS	= 111,
73fe2caefcSParav Pandit 	OCRDMA_MBX_STATUS_SGE_RECV_EXCEEDS	= 112,
74fe2caefcSParav Pandit 	OCRDMA_MBX_STATUS_INVALID_STATE_CHANGE	= 113,
75fe2caefcSParav Pandit 	OCRDMA_MBX_STATUS_MW_BOUND		= 114,
76fe2caefcSParav Pandit 	OCRDMA_MBX_STATUS_INVALID_VA		= 115,
77fe2caefcSParav Pandit 	OCRDMA_MBX_STATUS_INVALID_LENGTH	= 116,
78fe2caefcSParav Pandit 	OCRDMA_MBX_STATUS_INVALID_FBO		= 117,
79fe2caefcSParav Pandit 	OCRDMA_MBX_STATUS_INVALID_ACC_RIGHTS	= 118,
80fe2caefcSParav Pandit 	OCRDMA_MBX_STATUS_INVALID_PBE_SIZE	= 119,
81fe2caefcSParav Pandit 	OCRDMA_MBX_STATUS_INVALID_PBL_ENTRY	= 120,
82fe2caefcSParav Pandit 	OCRDMA_MBX_STATUS_INVALID_PBL_SHIFT	= 121,
83fe2caefcSParav Pandit 	OCRDMA_MBX_STATUS_INVALID_SRQ_ID	= 129,
84fe2caefcSParav Pandit 	OCRDMA_MBX_STATUS_SRQ_ERROR		= 133,
85fe2caefcSParav Pandit 	OCRDMA_MBX_STATUS_RQE_EXCEEDS		= 134,
86fe2caefcSParav Pandit 	OCRDMA_MBX_STATUS_MTU_EXCEEDS		= 135,
87fe2caefcSParav Pandit 	OCRDMA_MBX_STATUS_MAX_QP_EXCEEDS	= 136,
88fe2caefcSParav Pandit 	OCRDMA_MBX_STATUS_SRQ_LIMIT_EXCEEDS	= 137,
89fe2caefcSParav Pandit 	OCRDMA_MBX_STATUS_SRQ_SIZE_UNDERUNS	= 138,
90fe2caefcSParav Pandit 	OCRDMA_MBX_STATUS_QP_BOUND		= 130,
91fe2caefcSParav Pandit 	OCRDMA_MBX_STATUS_INVALID_CHANGE	= 139,
92fe2caefcSParav Pandit 	OCRDMA_MBX_STATUS_ATOMIC_OPS_UNSUP	= 140,
93fe2caefcSParav Pandit 	OCRDMA_MBX_STATUS_INVALID_RNR_NAK_TIMER	= 141,
94fe2caefcSParav Pandit 	OCRDMA_MBX_STATUS_MW_STILL_BOUND	= 142,
95fe2caefcSParav Pandit 	OCRDMA_MBX_STATUS_PKEY_INDEX_INVALID	= 143,
96fe2caefcSParav Pandit 	OCRDMA_MBX_STATUS_PKEY_INDEX_EXCEEDS	= 144
97fe2caefcSParav Pandit };
98fe2caefcSParav Pandit 
99fe2caefcSParav Pandit enum additional_status {
100fe2caefcSParav Pandit 	OCRDMA_MBX_ADDI_STATUS_INSUFFICIENT_RESOURCES = 22
101fe2caefcSParav Pandit };
102fe2caefcSParav Pandit 
103fe2caefcSParav Pandit enum cqe_status {
104fe2caefcSParav Pandit 	OCRDMA_MBX_CQE_STATUS_INSUFFICIENT_PRIVILEDGES	= 1,
105fe2caefcSParav Pandit 	OCRDMA_MBX_CQE_STATUS_INVALID_PARAMETER		= 2,
106fe2caefcSParav Pandit 	OCRDMA_MBX_CQE_STATUS_INSUFFICIENT_RESOURCES	= 3,
107fe2caefcSParav Pandit 	OCRDMA_MBX_CQE_STATUS_QUEUE_FLUSHING		= 4,
108fe2caefcSParav Pandit 	OCRDMA_MBX_CQE_STATUS_DMA_FAILED		= 5
109fe2caefcSParav Pandit };
110fe2caefcSParav Pandit 
ocrdma_get_eqe(struct ocrdma_eq * eq)111fe2caefcSParav Pandit static inline void *ocrdma_get_eqe(struct ocrdma_eq *eq)
112fe2caefcSParav Pandit {
113f99b1649SNaresh Gottumukkala 	return eq->q.va + (eq->q.tail * sizeof(struct ocrdma_eqe));
114fe2caefcSParav Pandit }
115fe2caefcSParav Pandit 
ocrdma_eq_inc_tail(struct ocrdma_eq * eq)116fe2caefcSParav Pandit static inline void ocrdma_eq_inc_tail(struct ocrdma_eq *eq)
117fe2caefcSParav Pandit {
118fe2caefcSParav Pandit 	eq->q.tail = (eq->q.tail + 1) & (OCRDMA_EQ_LEN - 1);
119fe2caefcSParav Pandit }
120fe2caefcSParav Pandit 
ocrdma_get_mcqe(struct ocrdma_dev * dev)121fe2caefcSParav Pandit static inline void *ocrdma_get_mcqe(struct ocrdma_dev *dev)
122fe2caefcSParav Pandit {
123fe2caefcSParav Pandit 	struct ocrdma_mcqe *cqe = (struct ocrdma_mcqe *)
124f99b1649SNaresh Gottumukkala 	    (dev->mq.cq.va + (dev->mq.cq.tail * sizeof(struct ocrdma_mcqe)));
125fe2caefcSParav Pandit 
126fe2caefcSParav Pandit 	if (!(le32_to_cpu(cqe->valid_ae_cmpl_cons) & OCRDMA_MCQE_VALID_MASK))
127fe2caefcSParav Pandit 		return NULL;
128fe2caefcSParav Pandit 	return cqe;
129fe2caefcSParav Pandit }
130fe2caefcSParav Pandit 
ocrdma_mcq_inc_tail(struct ocrdma_dev * dev)131fe2caefcSParav Pandit static inline void ocrdma_mcq_inc_tail(struct ocrdma_dev *dev)
132fe2caefcSParav Pandit {
133fe2caefcSParav Pandit 	dev->mq.cq.tail = (dev->mq.cq.tail + 1) & (OCRDMA_MQ_CQ_LEN - 1);
134fe2caefcSParav Pandit }
135fe2caefcSParav Pandit 
ocrdma_get_mqe(struct ocrdma_dev * dev)136fe2caefcSParav Pandit static inline struct ocrdma_mqe *ocrdma_get_mqe(struct ocrdma_dev *dev)
137fe2caefcSParav Pandit {
138f99b1649SNaresh Gottumukkala 	return dev->mq.sq.va + (dev->mq.sq.head * sizeof(struct ocrdma_mqe));
139fe2caefcSParav Pandit }
140fe2caefcSParav Pandit 
ocrdma_mq_inc_head(struct ocrdma_dev * dev)141fe2caefcSParav Pandit static inline void ocrdma_mq_inc_head(struct ocrdma_dev *dev)
142fe2caefcSParav Pandit {
143fe2caefcSParav Pandit 	dev->mq.sq.head = (dev->mq.sq.head + 1) & (OCRDMA_MQ_LEN - 1);
144fe2caefcSParav Pandit }
145fe2caefcSParav Pandit 
ocrdma_get_mqe_rsp(struct ocrdma_dev * dev)146fe2caefcSParav Pandit static inline void *ocrdma_get_mqe_rsp(struct ocrdma_dev *dev)
147fe2caefcSParav Pandit {
148f99b1649SNaresh Gottumukkala 	return dev->mq.sq.va + (dev->mqe_ctx.tag * sizeof(struct ocrdma_mqe));
149fe2caefcSParav Pandit }
150fe2caefcSParav Pandit 
get_ibqp_state(enum ocrdma_qp_state qps)151fe2caefcSParav Pandit enum ib_qp_state get_ibqp_state(enum ocrdma_qp_state qps)
152fe2caefcSParav Pandit {
153fe2caefcSParav Pandit 	switch (qps) {
154fe2caefcSParav Pandit 	case OCRDMA_QPS_RST:
155fe2caefcSParav Pandit 		return IB_QPS_RESET;
156fe2caefcSParav Pandit 	case OCRDMA_QPS_INIT:
157fe2caefcSParav Pandit 		return IB_QPS_INIT;
158fe2caefcSParav Pandit 	case OCRDMA_QPS_RTR:
159fe2caefcSParav Pandit 		return IB_QPS_RTR;
160fe2caefcSParav Pandit 	case OCRDMA_QPS_RTS:
161fe2caefcSParav Pandit 		return IB_QPS_RTS;
162fe2caefcSParav Pandit 	case OCRDMA_QPS_SQD:
163fe2caefcSParav Pandit 	case OCRDMA_QPS_SQ_DRAINING:
164fe2caefcSParav Pandit 		return IB_QPS_SQD;
165fe2caefcSParav Pandit 	case OCRDMA_QPS_SQE:
166fe2caefcSParav Pandit 		return IB_QPS_SQE;
167fe2caefcSParav Pandit 	case OCRDMA_QPS_ERR:
168fe2caefcSParav Pandit 		return IB_QPS_ERR;
1692b50176dSJoe Perches 	}
170fe2caefcSParav Pandit 	return IB_QPS_ERR;
171fe2caefcSParav Pandit }
172fe2caefcSParav Pandit 
get_ocrdma_qp_state(enum ib_qp_state qps)173abe3afacSRoland Dreier static enum ocrdma_qp_state get_ocrdma_qp_state(enum ib_qp_state qps)
174fe2caefcSParav Pandit {
175fe2caefcSParav Pandit 	switch (qps) {
176fe2caefcSParav Pandit 	case IB_QPS_RESET:
177fe2caefcSParav Pandit 		return OCRDMA_QPS_RST;
178fe2caefcSParav Pandit 	case IB_QPS_INIT:
179fe2caefcSParav Pandit 		return OCRDMA_QPS_INIT;
180fe2caefcSParav Pandit 	case IB_QPS_RTR:
181fe2caefcSParav Pandit 		return OCRDMA_QPS_RTR;
182fe2caefcSParav Pandit 	case IB_QPS_RTS:
183fe2caefcSParav Pandit 		return OCRDMA_QPS_RTS;
184fe2caefcSParav Pandit 	case IB_QPS_SQD:
185fe2caefcSParav Pandit 		return OCRDMA_QPS_SQD;
186fe2caefcSParav Pandit 	case IB_QPS_SQE:
187fe2caefcSParav Pandit 		return OCRDMA_QPS_SQE;
188fe2caefcSParav Pandit 	case IB_QPS_ERR:
189fe2caefcSParav Pandit 		return OCRDMA_QPS_ERR;
1902b50176dSJoe Perches 	}
191fe2caefcSParav Pandit 	return OCRDMA_QPS_ERR;
192fe2caefcSParav Pandit }
193fe2caefcSParav Pandit 
ocrdma_get_mbx_errno(u32 status)194fe2caefcSParav Pandit static int ocrdma_get_mbx_errno(u32 status)
195fe2caefcSParav Pandit {
196f99b1649SNaresh Gottumukkala 	int err_num;
197fe2caefcSParav Pandit 	u8 mbox_status = (status & OCRDMA_MBX_RSP_STATUS_MASK) >>
198fe2caefcSParav Pandit 					OCRDMA_MBX_RSP_STATUS_SHIFT;
199fe2caefcSParav Pandit 	u8 add_status = (status & OCRDMA_MBX_RSP_ASTATUS_MASK) >>
200fe2caefcSParav Pandit 					OCRDMA_MBX_RSP_ASTATUS_SHIFT;
201fe2caefcSParav Pandit 
202fe2caefcSParav Pandit 	switch (mbox_status) {
203fe2caefcSParav Pandit 	case OCRDMA_MBX_STATUS_OOR:
204fe2caefcSParav Pandit 	case OCRDMA_MBX_STATUS_MAX_QP_EXCEEDS:
205fe2caefcSParav Pandit 		err_num = -EAGAIN;
206fe2caefcSParav Pandit 		break;
207fe2caefcSParav Pandit 
208fe2caefcSParav Pandit 	case OCRDMA_MBX_STATUS_INVALID_PD:
209fe2caefcSParav Pandit 	case OCRDMA_MBX_STATUS_INVALID_CQ:
210fe2caefcSParav Pandit 	case OCRDMA_MBX_STATUS_INVALID_SRQ_ID:
211fe2caefcSParav Pandit 	case OCRDMA_MBX_STATUS_INVALID_QP:
212fe2caefcSParav Pandit 	case OCRDMA_MBX_STATUS_INVALID_CHANGE:
213fe2caefcSParav Pandit 	case OCRDMA_MBX_STATUS_MTU_EXCEEDS:
214fe2caefcSParav Pandit 	case OCRDMA_MBX_STATUS_INVALID_RNR_NAK_TIMER:
215fe2caefcSParav Pandit 	case OCRDMA_MBX_STATUS_PKEY_INDEX_INVALID:
216fe2caefcSParav Pandit 	case OCRDMA_MBX_STATUS_PKEY_INDEX_EXCEEDS:
217fe2caefcSParav Pandit 	case OCRDMA_MBX_STATUS_ILLEGAL_FIELD:
218fe2caefcSParav Pandit 	case OCRDMA_MBX_STATUS_INVALID_PBL_ENTRY:
219fe2caefcSParav Pandit 	case OCRDMA_MBX_STATUS_INVALID_LKEY:
220fe2caefcSParav Pandit 	case OCRDMA_MBX_STATUS_INVALID_VA:
221fe2caefcSParav Pandit 	case OCRDMA_MBX_STATUS_INVALID_LENGTH:
222fe2caefcSParav Pandit 	case OCRDMA_MBX_STATUS_INVALID_FBO:
223fe2caefcSParav Pandit 	case OCRDMA_MBX_STATUS_INVALID_ACC_RIGHTS:
224fe2caefcSParav Pandit 	case OCRDMA_MBX_STATUS_INVALID_PBE_SIZE:
225fe2caefcSParav Pandit 	case OCRDMA_MBX_STATUS_ATOMIC_OPS_UNSUP:
226fe2caefcSParav Pandit 	case OCRDMA_MBX_STATUS_SRQ_ERROR:
227fe2caefcSParav Pandit 	case OCRDMA_MBX_STATUS_SRQ_SIZE_UNDERUNS:
228fe2caefcSParav Pandit 		err_num = -EINVAL;
229fe2caefcSParav Pandit 		break;
230fe2caefcSParav Pandit 
231fe2caefcSParav Pandit 	case OCRDMA_MBX_STATUS_PD_INUSE:
232fe2caefcSParav Pandit 	case OCRDMA_MBX_STATUS_QP_BOUND:
233fe2caefcSParav Pandit 	case OCRDMA_MBX_STATUS_MW_STILL_BOUND:
234fe2caefcSParav Pandit 	case OCRDMA_MBX_STATUS_MW_BOUND:
235fe2caefcSParav Pandit 		err_num = -EBUSY;
236fe2caefcSParav Pandit 		break;
237fe2caefcSParav Pandit 
238fe2caefcSParav Pandit 	case OCRDMA_MBX_STATUS_RECVQ_RQE_EXCEEDS:
239fe2caefcSParav Pandit 	case OCRDMA_MBX_STATUS_SGE_RECV_EXCEEDS:
240fe2caefcSParav Pandit 	case OCRDMA_MBX_STATUS_RQE_EXCEEDS:
241fe2caefcSParav Pandit 	case OCRDMA_MBX_STATUS_SRQ_LIMIT_EXCEEDS:
242fe2caefcSParav Pandit 	case OCRDMA_MBX_STATUS_ORD_EXCEEDS:
243fe2caefcSParav Pandit 	case OCRDMA_MBX_STATUS_IRD_EXCEEDS:
244fe2caefcSParav Pandit 	case OCRDMA_MBX_STATUS_SENDQ_WQE_EXCEEDS:
245fe2caefcSParav Pandit 	case OCRDMA_MBX_STATUS_SGE_SEND_EXCEEDS:
246fe2caefcSParav Pandit 	case OCRDMA_MBX_STATUS_SGE_WRITE_EXCEEDS:
247fe2caefcSParav Pandit 		err_num = -ENOBUFS;
248fe2caefcSParav Pandit 		break;
249fe2caefcSParav Pandit 
250fe2caefcSParav Pandit 	case OCRDMA_MBX_STATUS_FAILED:
251fe2caefcSParav Pandit 		switch (add_status) {
252fe2caefcSParav Pandit 		case OCRDMA_MBX_ADDI_STATUS_INSUFFICIENT_RESOURCES:
253fe2caefcSParav Pandit 			err_num = -EAGAIN;
254fe2caefcSParav Pandit 			break;
25506564f60SColin Ian King 		default:
25606564f60SColin Ian King 			err_num = -EFAULT;
257fe2caefcSParav Pandit 		}
25806564f60SColin Ian King 		break;
259fe2caefcSParav Pandit 	default:
260fe2caefcSParav Pandit 		err_num = -EFAULT;
261fe2caefcSParav Pandit 	}
262fe2caefcSParav Pandit 	return err_num;
263fe2caefcSParav Pandit }
264fe2caefcSParav Pandit 
port_speed_string(struct ocrdma_dev * dev)265a51f06e1SSelvin Xavier char *port_speed_string(struct ocrdma_dev *dev)
266a51f06e1SSelvin Xavier {
267a51f06e1SSelvin Xavier 	char *str = "";
268a51f06e1SSelvin Xavier 	u16 speeds_supported;
269a51f06e1SSelvin Xavier 
270a51f06e1SSelvin Xavier 	speeds_supported = dev->phy.fixed_speeds_supported |
271a51f06e1SSelvin Xavier 				dev->phy.auto_speeds_supported;
272a51f06e1SSelvin Xavier 	if (speeds_supported & OCRDMA_PHY_SPEED_40GBPS)
273a51f06e1SSelvin Xavier 		str = "40Gbps ";
274a51f06e1SSelvin Xavier 	else if (speeds_supported & OCRDMA_PHY_SPEED_10GBPS)
275a51f06e1SSelvin Xavier 		str = "10Gbps ";
276a51f06e1SSelvin Xavier 	else if (speeds_supported & OCRDMA_PHY_SPEED_1GBPS)
277a51f06e1SSelvin Xavier 		str = "1Gbps ";
278a51f06e1SSelvin Xavier 
279a51f06e1SSelvin Xavier 	return str;
280a51f06e1SSelvin Xavier }
281a51f06e1SSelvin Xavier 
ocrdma_get_mbx_cqe_errno(u16 cqe_status)282fe2caefcSParav Pandit static int ocrdma_get_mbx_cqe_errno(u16 cqe_status)
283fe2caefcSParav Pandit {
284fe2caefcSParav Pandit 	int err_num = -EINVAL;
285fe2caefcSParav Pandit 
286fe2caefcSParav Pandit 	switch (cqe_status) {
287fe2caefcSParav Pandit 	case OCRDMA_MBX_CQE_STATUS_INSUFFICIENT_PRIVILEDGES:
288fe2caefcSParav Pandit 		err_num = -EPERM;
289fe2caefcSParav Pandit 		break;
290fe2caefcSParav Pandit 	case OCRDMA_MBX_CQE_STATUS_INVALID_PARAMETER:
291fe2caefcSParav Pandit 		err_num = -EINVAL;
292fe2caefcSParav Pandit 		break;
293fe2caefcSParav Pandit 	case OCRDMA_MBX_CQE_STATUS_INSUFFICIENT_RESOURCES:
294fe2caefcSParav Pandit 	case OCRDMA_MBX_CQE_STATUS_QUEUE_FLUSHING:
295f11220eeSNaresh Gottumukkala 		err_num = -EINVAL;
296fe2caefcSParav Pandit 		break;
297fe2caefcSParav Pandit 	case OCRDMA_MBX_CQE_STATUS_DMA_FAILED:
29843a6b402SNaresh Gottumukkala 	default:
299f11220eeSNaresh Gottumukkala 		err_num = -EINVAL;
300fe2caefcSParav Pandit 		break;
301fe2caefcSParav Pandit 	}
302fe2caefcSParav Pandit 	return err_num;
303fe2caefcSParav Pandit }
304fe2caefcSParav Pandit 
ocrdma_ring_cq_db(struct ocrdma_dev * dev,u16 cq_id,bool armed,bool solicited,u16 cqe_popped)305fe2caefcSParav Pandit void ocrdma_ring_cq_db(struct ocrdma_dev *dev, u16 cq_id, bool armed,
306fe2caefcSParav Pandit 		       bool solicited, u16 cqe_popped)
307fe2caefcSParav Pandit {
308fe2caefcSParav Pandit 	u32 val = cq_id & OCRDMA_DB_CQ_RING_ID_MASK;
309fe2caefcSParav Pandit 
310fe2caefcSParav Pandit 	val |= ((cq_id & OCRDMA_DB_CQ_RING_ID_EXT_MASK) <<
311fe2caefcSParav Pandit 	     OCRDMA_DB_CQ_RING_ID_EXT_MASK_SHIFT);
312fe2caefcSParav Pandit 
313fe2caefcSParav Pandit 	if (armed)
314fe2caefcSParav Pandit 		val |= (1 << OCRDMA_DB_CQ_REARM_SHIFT);
315fe2caefcSParav Pandit 	if (solicited)
316fe2caefcSParav Pandit 		val |= (1 << OCRDMA_DB_CQ_SOLICIT_SHIFT);
317fe2caefcSParav Pandit 	val |= (cqe_popped << OCRDMA_DB_CQ_NUM_POPPED_SHIFT);
318fe2caefcSParav Pandit 	iowrite32(val, dev->nic_info.db + OCRDMA_DB_CQ_OFFSET);
319fe2caefcSParav Pandit }
320fe2caefcSParav Pandit 
ocrdma_ring_mq_db(struct ocrdma_dev * dev)321fe2caefcSParav Pandit static void ocrdma_ring_mq_db(struct ocrdma_dev *dev)
322fe2caefcSParav Pandit {
323fe2caefcSParav Pandit 	u32 val = 0;
324fe2caefcSParav Pandit 
325fe2caefcSParav Pandit 	val |= dev->mq.sq.id & OCRDMA_MQ_ID_MASK;
326fe2caefcSParav Pandit 	val |= 1 << OCRDMA_MQ_NUM_MQE_SHIFT;
327fe2caefcSParav Pandit 	iowrite32(val, dev->nic_info.db + OCRDMA_DB_MQ_OFFSET);
328fe2caefcSParav Pandit }
329fe2caefcSParav Pandit 
ocrdma_ring_eq_db(struct ocrdma_dev * dev,u16 eq_id,bool arm,bool clear_int,u16 num_eqe)330fe2caefcSParav Pandit static void ocrdma_ring_eq_db(struct ocrdma_dev *dev, u16 eq_id,
331fe2caefcSParav Pandit 			      bool arm, bool clear_int, u16 num_eqe)
332fe2caefcSParav Pandit {
333fe2caefcSParav Pandit 	u32 val = 0;
334fe2caefcSParav Pandit 
335fe2caefcSParav Pandit 	val |= eq_id & OCRDMA_EQ_ID_MASK;
336fe2caefcSParav Pandit 	val |= ((eq_id & OCRDMA_EQ_ID_EXT_MASK) << OCRDMA_EQ_ID_EXT_MASK_SHIFT);
337fe2caefcSParav Pandit 	if (arm)
338fe2caefcSParav Pandit 		val |= (1 << OCRDMA_REARM_SHIFT);
339fe2caefcSParav Pandit 	if (clear_int)
340fe2caefcSParav Pandit 		val |= (1 << OCRDMA_EQ_CLR_SHIFT);
341fe2caefcSParav Pandit 	val |= (1 << OCRDMA_EQ_TYPE_SHIFT);
342fe2caefcSParav Pandit 	val |= (num_eqe << OCRDMA_NUM_EQE_SHIFT);
343fe2caefcSParav Pandit 	iowrite32(val, dev->nic_info.db + OCRDMA_DB_EQ_OFFSET);
344fe2caefcSParav Pandit }
345fe2caefcSParav Pandit 
ocrdma_init_mch(struct ocrdma_mbx_hdr * cmd_hdr,u8 opcode,u8 subsys,u32 cmd_len)346fe2caefcSParav Pandit static void ocrdma_init_mch(struct ocrdma_mbx_hdr *cmd_hdr,
347fe2caefcSParav Pandit 			    u8 opcode, u8 subsys, u32 cmd_len)
348fe2caefcSParav Pandit {
349fe2caefcSParav Pandit 	cmd_hdr->subsys_op = (opcode | (subsys << OCRDMA_MCH_SUBSYS_SHIFT));
350fe2caefcSParav Pandit 	cmd_hdr->timeout = 20; /* seconds */
351fe2caefcSParav Pandit 	cmd_hdr->cmd_len = cmd_len - sizeof(struct ocrdma_mbx_hdr);
352fe2caefcSParav Pandit }
353fe2caefcSParav Pandit 
ocrdma_init_emb_mqe(u8 opcode,u32 cmd_len)354fe2caefcSParav Pandit static void *ocrdma_init_emb_mqe(u8 opcode, u32 cmd_len)
355fe2caefcSParav Pandit {
356fe2caefcSParav Pandit 	struct ocrdma_mqe *mqe;
357fe2caefcSParav Pandit 
358fe2caefcSParav Pandit 	mqe = kzalloc(sizeof(struct ocrdma_mqe), GFP_KERNEL);
359fe2caefcSParav Pandit 	if (!mqe)
360fe2caefcSParav Pandit 		return NULL;
361fe2caefcSParav Pandit 	mqe->hdr.spcl_sge_cnt_emb |=
362fe2caefcSParav Pandit 		(OCRDMA_MQE_EMBEDDED << OCRDMA_MQE_HDR_EMB_SHIFT) &
363fe2caefcSParav Pandit 					OCRDMA_MQE_HDR_EMB_MASK;
364fe2caefcSParav Pandit 	mqe->hdr.pyld_len = cmd_len - sizeof(struct ocrdma_mqe_hdr);
365fe2caefcSParav Pandit 
366fe2caefcSParav Pandit 	ocrdma_init_mch(&mqe->u.emb_req.mch, opcode, OCRDMA_SUBSYS_ROCE,
367fe2caefcSParav Pandit 			mqe->hdr.pyld_len);
368fe2caefcSParav Pandit 	return mqe;
369fe2caefcSParav Pandit }
370fe2caefcSParav Pandit 
ocrdma_free_q(struct ocrdma_dev * dev,struct ocrdma_queue_info * q)371fe2caefcSParav Pandit static void ocrdma_free_q(struct ocrdma_dev *dev, struct ocrdma_queue_info *q)
372fe2caefcSParav Pandit {
373fe2caefcSParav Pandit 	dma_free_coherent(&dev->nic_info.pdev->dev, q->size, q->va, q->dma);
374fe2caefcSParav Pandit }
375fe2caefcSParav Pandit 
ocrdma_alloc_q(struct ocrdma_dev * dev,struct ocrdma_queue_info * q,u16 len,u16 entry_size)376fe2caefcSParav Pandit static int ocrdma_alloc_q(struct ocrdma_dev *dev,
377fe2caefcSParav Pandit 			  struct ocrdma_queue_info *q, u16 len, u16 entry_size)
378fe2caefcSParav Pandit {
379fe2caefcSParav Pandit 	memset(q, 0, sizeof(*q));
380fe2caefcSParav Pandit 	q->len = len;
381fe2caefcSParav Pandit 	q->entry_size = entry_size;
382fe2caefcSParav Pandit 	q->size = len * entry_size;
383750afb08SLuis Chamberlain 	q->va = dma_alloc_coherent(&dev->nic_info.pdev->dev, q->size, &q->dma,
384750afb08SLuis Chamberlain 				   GFP_KERNEL);
385fe2caefcSParav Pandit 	if (!q->va)
386fe2caefcSParav Pandit 		return -ENOMEM;
387fe2caefcSParav Pandit 	return 0;
388fe2caefcSParav Pandit }
389fe2caefcSParav Pandit 
ocrdma_build_q_pages(struct ocrdma_pa * q_pa,int cnt,dma_addr_t host_pa,int hw_page_size)390fe2caefcSParav Pandit static void ocrdma_build_q_pages(struct ocrdma_pa *q_pa, int cnt,
391fe2caefcSParav Pandit 					dma_addr_t host_pa, int hw_page_size)
392fe2caefcSParav Pandit {
393fe2caefcSParav Pandit 	int i;
394fe2caefcSParav Pandit 
395fe2caefcSParav Pandit 	for (i = 0; i < cnt; i++) {
396fe2caefcSParav Pandit 		q_pa[i].lo = (u32) (host_pa & 0xffffffff);
397fe2caefcSParav Pandit 		q_pa[i].hi = (u32) upper_32_bits(host_pa);
398fe2caefcSParav Pandit 		host_pa += hw_page_size;
399fe2caefcSParav Pandit 	}
400fe2caefcSParav Pandit }
401fe2caefcSParav Pandit 
ocrdma_mbx_delete_q(struct ocrdma_dev * dev,struct ocrdma_queue_info * q,int queue_type)402fad51b7dSDevesh Sharma static int ocrdma_mbx_delete_q(struct ocrdma_dev *dev,
403fad51b7dSDevesh Sharma 			       struct ocrdma_queue_info *q, int queue_type)
404fe2caefcSParav Pandit {
405fe2caefcSParav Pandit 	u8 opcode = 0;
406fe2caefcSParav Pandit 	int status;
407fe2caefcSParav Pandit 	struct ocrdma_delete_q_req *cmd = dev->mbx_cmd;
408fe2caefcSParav Pandit 
409fe2caefcSParav Pandit 	switch (queue_type) {
410fe2caefcSParav Pandit 	case QTYPE_MCCQ:
411fe2caefcSParav Pandit 		opcode = OCRDMA_CMD_DELETE_MQ;
412fe2caefcSParav Pandit 		break;
413fe2caefcSParav Pandit 	case QTYPE_CQ:
414fe2caefcSParav Pandit 		opcode = OCRDMA_CMD_DELETE_CQ;
415fe2caefcSParav Pandit 		break;
416fe2caefcSParav Pandit 	case QTYPE_EQ:
417fe2caefcSParav Pandit 		opcode = OCRDMA_CMD_DELETE_EQ;
418fe2caefcSParav Pandit 		break;
419fe2caefcSParav Pandit 	default:
420fe2caefcSParav Pandit 		BUG();
421fe2caefcSParav Pandit 	}
422fe2caefcSParav Pandit 	memset(cmd, 0, sizeof(*cmd));
423fe2caefcSParav Pandit 	ocrdma_init_mch(&cmd->req, opcode, OCRDMA_SUBSYS_COMMON, sizeof(*cmd));
424fe2caefcSParav Pandit 	cmd->id = q->id;
425fe2caefcSParav Pandit 
426fe2caefcSParav Pandit 	status = be_roce_mcc_cmd(dev->nic_info.netdev,
427fe2caefcSParav Pandit 				 cmd, sizeof(*cmd), NULL, NULL);
428fe2caefcSParav Pandit 	if (!status)
429fe2caefcSParav Pandit 		q->created = false;
430fe2caefcSParav Pandit 	return status;
431fe2caefcSParav Pandit }
432fe2caefcSParav Pandit 
ocrdma_mbx_create_eq(struct ocrdma_dev * dev,struct ocrdma_eq * eq)433fe2caefcSParav Pandit static int ocrdma_mbx_create_eq(struct ocrdma_dev *dev, struct ocrdma_eq *eq)
434fe2caefcSParav Pandit {
435fe2caefcSParav Pandit 	int status;
436fe2caefcSParav Pandit 	struct ocrdma_create_eq_req *cmd = dev->mbx_cmd;
437fe2caefcSParav Pandit 	struct ocrdma_create_eq_rsp *rsp = dev->mbx_cmd;
438fe2caefcSParav Pandit 
439fe2caefcSParav Pandit 	memset(cmd, 0, sizeof(*cmd));
440fe2caefcSParav Pandit 	ocrdma_init_mch(&cmd->req, OCRDMA_CMD_CREATE_EQ, OCRDMA_SUBSYS_COMMON,
441fe2caefcSParav Pandit 			sizeof(*cmd));
442fe2caefcSParav Pandit 
443c88bd03fSNaresh Gottumukkala 	cmd->req.rsvd_version = 2;
444fe2caefcSParav Pandit 	cmd->num_pages = 4;
445fe2caefcSParav Pandit 	cmd->valid = OCRDMA_CREATE_EQ_VALID;
446fe2caefcSParav Pandit 	cmd->cnt = 4 << OCRDMA_CREATE_EQ_CNT_SHIFT;
447fe2caefcSParav Pandit 
448fe2caefcSParav Pandit 	ocrdma_build_q_pages(&cmd->pa[0], cmd->num_pages, eq->q.dma,
449fe2caefcSParav Pandit 			     PAGE_SIZE_4K);
450fe2caefcSParav Pandit 	status = be_roce_mcc_cmd(dev->nic_info.netdev, cmd, sizeof(*cmd), NULL,
451fe2caefcSParav Pandit 				 NULL);
452fe2caefcSParav Pandit 	if (!status) {
453fe2caefcSParav Pandit 		eq->q.id = rsp->vector_eqid & 0xffff;
454fe2caefcSParav Pandit 		eq->vector = (rsp->vector_eqid >> 16) & 0xffff;
455fe2caefcSParav Pandit 		eq->q.created = true;
456fe2caefcSParav Pandit 	}
457fe2caefcSParav Pandit 	return status;
458fe2caefcSParav Pandit }
459fe2caefcSParav Pandit 
ocrdma_create_eq(struct ocrdma_dev * dev,struct ocrdma_eq * eq,u16 q_len)460fe2caefcSParav Pandit static int ocrdma_create_eq(struct ocrdma_dev *dev,
461fe2caefcSParav Pandit 			    struct ocrdma_eq *eq, u16 q_len)
462fe2caefcSParav Pandit {
463fe2caefcSParav Pandit 	int status;
464fe2caefcSParav Pandit 
465fe2caefcSParav Pandit 	status = ocrdma_alloc_q(dev, &eq->q, OCRDMA_EQ_LEN,
466fe2caefcSParav Pandit 				sizeof(struct ocrdma_eqe));
467fe2caefcSParav Pandit 	if (status)
468fe2caefcSParav Pandit 		return status;
469fe2caefcSParav Pandit 
470fe2caefcSParav Pandit 	status = ocrdma_mbx_create_eq(dev, eq);
471fe2caefcSParav Pandit 	if (status)
472fe2caefcSParav Pandit 		goto mbx_err;
473fe2caefcSParav Pandit 	eq->dev = dev;
474fe2caefcSParav Pandit 	ocrdma_ring_eq_db(dev, eq->q.id, true, true, 0);
475fe2caefcSParav Pandit 
476fe2caefcSParav Pandit 	return 0;
477fe2caefcSParav Pandit mbx_err:
478fe2caefcSParav Pandit 	ocrdma_free_q(dev, &eq->q);
479fe2caefcSParav Pandit 	return status;
480fe2caefcSParav Pandit }
481fe2caefcSParav Pandit 
ocrdma_get_irq(struct ocrdma_dev * dev,struct ocrdma_eq * eq)482ea617626SDevesh Sharma int ocrdma_get_irq(struct ocrdma_dev *dev, struct ocrdma_eq *eq)
483fe2caefcSParav Pandit {
484fe2caefcSParav Pandit 	int irq;
485fe2caefcSParav Pandit 
486fe2caefcSParav Pandit 	if (dev->nic_info.intr_mode == BE_INTERRUPT_MODE_INTX)
487fe2caefcSParav Pandit 		irq = dev->nic_info.pdev->irq;
488fe2caefcSParav Pandit 	else
489fe2caefcSParav Pandit 		irq = dev->nic_info.msix.vector_list[eq->vector];
490fe2caefcSParav Pandit 	return irq;
491fe2caefcSParav Pandit }
492fe2caefcSParav Pandit 
_ocrdma_destroy_eq(struct ocrdma_dev * dev,struct ocrdma_eq * eq)493fe2caefcSParav Pandit static void _ocrdma_destroy_eq(struct ocrdma_dev *dev, struct ocrdma_eq *eq)
494fe2caefcSParav Pandit {
495fe2caefcSParav Pandit 	if (eq->q.created) {
496fe2caefcSParav Pandit 		ocrdma_mbx_delete_q(dev, &eq->q, QTYPE_EQ);
497fe2caefcSParav Pandit 		ocrdma_free_q(dev, &eq->q);
498fe2caefcSParav Pandit 	}
499fe2caefcSParav Pandit }
500fe2caefcSParav Pandit 
ocrdma_destroy_eq(struct ocrdma_dev * dev,struct ocrdma_eq * eq)501fe2caefcSParav Pandit static void ocrdma_destroy_eq(struct ocrdma_dev *dev, struct ocrdma_eq *eq)
502fe2caefcSParav Pandit {
503fe2caefcSParav Pandit 	int irq;
504fe2caefcSParav Pandit 
505fe2caefcSParav Pandit 	/* disarm EQ so that interrupts are not generated
506fe2caefcSParav Pandit 	 * during freeing and EQ delete is in progress.
507fe2caefcSParav Pandit 	 */
508fe2caefcSParav Pandit 	ocrdma_ring_eq_db(dev, eq->q.id, false, false, 0);
509fe2caefcSParav Pandit 
510fe2caefcSParav Pandit 	irq = ocrdma_get_irq(dev, eq);
511fe2caefcSParav Pandit 	free_irq(irq, eq);
512fe2caefcSParav Pandit 	_ocrdma_destroy_eq(dev, eq);
513fe2caefcSParav Pandit }
514fe2caefcSParav Pandit 
ocrdma_destroy_eqs(struct ocrdma_dev * dev)515c88bd03fSNaresh Gottumukkala static void ocrdma_destroy_eqs(struct ocrdma_dev *dev)
516fe2caefcSParav Pandit {
517fe2caefcSParav Pandit 	int i;
518fe2caefcSParav Pandit 
519fe2caefcSParav Pandit 	for (i = 0; i < dev->eq_cnt; i++)
520c88bd03fSNaresh Gottumukkala 		ocrdma_destroy_eq(dev, &dev->eq_tbl[i]);
521fe2caefcSParav Pandit }
522fe2caefcSParav Pandit 
ocrdma_mbx_mq_cq_create(struct ocrdma_dev * dev,struct ocrdma_queue_info * cq,struct ocrdma_queue_info * eq)523abe3afacSRoland Dreier static int ocrdma_mbx_mq_cq_create(struct ocrdma_dev *dev,
524fe2caefcSParav Pandit 				   struct ocrdma_queue_info *cq,
525fe2caefcSParav Pandit 				   struct ocrdma_queue_info *eq)
526fe2caefcSParav Pandit {
527fe2caefcSParav Pandit 	struct ocrdma_create_cq_cmd *cmd = dev->mbx_cmd;
528fe2caefcSParav Pandit 	struct ocrdma_create_cq_cmd_rsp *rsp = dev->mbx_cmd;
529fe2caefcSParav Pandit 	int status;
530fe2caefcSParav Pandit 
531fe2caefcSParav Pandit 	memset(cmd, 0, sizeof(*cmd));
532fe2caefcSParav Pandit 	ocrdma_init_mch(&cmd->req, OCRDMA_CMD_CREATE_CQ,
533fe2caefcSParav Pandit 			OCRDMA_SUBSYS_COMMON, sizeof(*cmd));
534fe2caefcSParav Pandit 
5351afc0454SNaresh Gottumukkala 	cmd->req.rsvd_version = OCRDMA_CREATE_CQ_VER2;
5361afc0454SNaresh Gottumukkala 	cmd->pgsz_pgcnt = (cq->size / OCRDMA_MIN_Q_PAGE_SIZE) <<
5371afc0454SNaresh Gottumukkala 		OCRDMA_CREATE_CQ_PAGE_SIZE_SHIFT;
5381afc0454SNaresh Gottumukkala 	cmd->pgsz_pgcnt |= PAGES_4K_SPANNED(cq->va, cq->size);
539fe2caefcSParav Pandit 
5401afc0454SNaresh Gottumukkala 	cmd->ev_cnt_flags = OCRDMA_CREATE_CQ_DEF_FLAGS;
5411afc0454SNaresh Gottumukkala 	cmd->eqn = eq->id;
5428ac0c7c7SDevesh Sharma 	cmd->pdid_cqecnt = cq->size / sizeof(struct ocrdma_mcqe);
5431afc0454SNaresh Gottumukkala 
5441afc0454SNaresh Gottumukkala 	ocrdma_build_q_pages(&cmd->pa[0], cq->size / OCRDMA_MIN_Q_PAGE_SIZE,
545fe2caefcSParav Pandit 			     cq->dma, PAGE_SIZE_4K);
546fe2caefcSParav Pandit 	status = be_roce_mcc_cmd(dev->nic_info.netdev,
547fe2caefcSParav Pandit 				 cmd, sizeof(*cmd), NULL, NULL);
548fe2caefcSParav Pandit 	if (!status) {
5491afc0454SNaresh Gottumukkala 		cq->id = (u16) (rsp->cq_id & OCRDMA_CREATE_CQ_RSP_CQ_ID_MASK);
550fe2caefcSParav Pandit 		cq->created = true;
551fe2caefcSParav Pandit 	}
552fe2caefcSParav Pandit 	return status;
553fe2caefcSParav Pandit }
554fe2caefcSParav Pandit 
ocrdma_encoded_q_len(int q_len)555fe2caefcSParav Pandit static u32 ocrdma_encoded_q_len(int q_len)
556fe2caefcSParav Pandit {
557fe2caefcSParav Pandit 	u32 len_encoded = fls(q_len);	/* log2(len) + 1 */
558fe2caefcSParav Pandit 
559fe2caefcSParav Pandit 	if (len_encoded == 16)
560fe2caefcSParav Pandit 		len_encoded = 0;
561fe2caefcSParav Pandit 	return len_encoded;
562fe2caefcSParav Pandit }
563fe2caefcSParav Pandit 
ocrdma_mbx_create_mq(struct ocrdma_dev * dev,struct ocrdma_queue_info * mq,struct ocrdma_queue_info * cq)564fe2caefcSParav Pandit static int ocrdma_mbx_create_mq(struct ocrdma_dev *dev,
565fe2caefcSParav Pandit 				struct ocrdma_queue_info *mq,
566fe2caefcSParav Pandit 				struct ocrdma_queue_info *cq)
567fe2caefcSParav Pandit {
568fe2caefcSParav Pandit 	int num_pages, status;
569fe2caefcSParav Pandit 	struct ocrdma_create_mq_req *cmd = dev->mbx_cmd;
570fe2caefcSParav Pandit 	struct ocrdma_create_mq_rsp *rsp = dev->mbx_cmd;
571fe2caefcSParav Pandit 	struct ocrdma_pa *pa;
572fe2caefcSParav Pandit 
573fe2caefcSParav Pandit 	memset(cmd, 0, sizeof(*cmd));
574fe2caefcSParav Pandit 	num_pages = PAGES_4K_SPANNED(mq->va, mq->size);
575fe2caefcSParav Pandit 
576fe2caefcSParav Pandit 	ocrdma_init_mch(&cmd->req, OCRDMA_CMD_CREATE_MQ_EXT,
577fe2caefcSParav Pandit 			OCRDMA_SUBSYS_COMMON, sizeof(*cmd));
578fe2caefcSParav Pandit 	cmd->req.rsvd_version = 1;
579b1d58b99SNaresh Gottumukkala 	cmd->cqid_pages = num_pages;
580b1d58b99SNaresh Gottumukkala 	cmd->cqid_pages |= (cq->id << OCRDMA_CREATE_MQ_CQ_ID_SHIFT);
581b1d58b99SNaresh Gottumukkala 	cmd->async_cqid_valid = OCRDMA_CREATE_MQ_ASYNC_CQ_VALID;
58284b105dbSNaresh Gottumukkala 
583de123485SJes Sorensen 	cmd->async_event_bitmap = BIT(OCRDMA_ASYNC_GRP5_EVE_CODE);
584de123485SJes Sorensen 	cmd->async_event_bitmap |= BIT(OCRDMA_ASYNC_RDMA_EVE_CODE);
58510a214dcSDevesh Sharma 	/* Request link events on this  MQ. */
58610a214dcSDevesh Sharma 	cmd->async_event_bitmap |= BIT(OCRDMA_ASYNC_LINK_EVE_CODE);
58784b105dbSNaresh Gottumukkala 
588b1d58b99SNaresh Gottumukkala 	cmd->async_cqid_ringsize = cq->id;
589b1d58b99SNaresh Gottumukkala 	cmd->async_cqid_ringsize |= (ocrdma_encoded_q_len(mq->len) <<
590fe2caefcSParav Pandit 				OCRDMA_CREATE_MQ_RING_SIZE_SHIFT);
591b1d58b99SNaresh Gottumukkala 	cmd->valid = OCRDMA_CREATE_MQ_VALID;
592b1d58b99SNaresh Gottumukkala 	pa = &cmd->pa[0];
593b1d58b99SNaresh Gottumukkala 
594fe2caefcSParav Pandit 	ocrdma_build_q_pages(pa, num_pages, mq->dma, PAGE_SIZE_4K);
595fe2caefcSParav Pandit 	status = be_roce_mcc_cmd(dev->nic_info.netdev,
596fe2caefcSParav Pandit 				 cmd, sizeof(*cmd), NULL, NULL);
597fe2caefcSParav Pandit 	if (!status) {
598fe2caefcSParav Pandit 		mq->id = rsp->id;
599fe2caefcSParav Pandit 		mq->created = true;
600fe2caefcSParav Pandit 	}
601fe2caefcSParav Pandit 	return status;
602fe2caefcSParav Pandit }
603fe2caefcSParav Pandit 
ocrdma_create_mq(struct ocrdma_dev * dev)604fe2caefcSParav Pandit static int ocrdma_create_mq(struct ocrdma_dev *dev)
605fe2caefcSParav Pandit {
606fe2caefcSParav Pandit 	int status;
607fe2caefcSParav Pandit 
608fe2caefcSParav Pandit 	/* Alloc completion queue for Mailbox queue */
609fe2caefcSParav Pandit 	status = ocrdma_alloc_q(dev, &dev->mq.cq, OCRDMA_MQ_CQ_LEN,
610fe2caefcSParav Pandit 				sizeof(struct ocrdma_mcqe));
611fe2caefcSParav Pandit 	if (status)
612fe2caefcSParav Pandit 		goto alloc_err;
613fe2caefcSParav Pandit 
614ea617626SDevesh Sharma 	dev->eq_tbl[0].cq_cnt++;
615c88bd03fSNaresh Gottumukkala 	status = ocrdma_mbx_mq_cq_create(dev, &dev->mq.cq, &dev->eq_tbl[0].q);
616fe2caefcSParav Pandit 	if (status)
617fe2caefcSParav Pandit 		goto mbx_cq_free;
618fe2caefcSParav Pandit 
619fe2caefcSParav Pandit 	memset(&dev->mqe_ctx, 0, sizeof(dev->mqe_ctx));
620fe2caefcSParav Pandit 	init_waitqueue_head(&dev->mqe_ctx.cmd_wait);
621fe2caefcSParav Pandit 	mutex_init(&dev->mqe_ctx.lock);
622fe2caefcSParav Pandit 
623fe2caefcSParav Pandit 	/* Alloc Mailbox queue */
624fe2caefcSParav Pandit 	status = ocrdma_alloc_q(dev, &dev->mq.sq, OCRDMA_MQ_LEN,
625fe2caefcSParav Pandit 				sizeof(struct ocrdma_mqe));
626fe2caefcSParav Pandit 	if (status)
627fe2caefcSParav Pandit 		goto mbx_cq_destroy;
628fe2caefcSParav Pandit 	status = ocrdma_mbx_create_mq(dev, &dev->mq.sq, &dev->mq.cq);
629fe2caefcSParav Pandit 	if (status)
630fe2caefcSParav Pandit 		goto mbx_q_free;
631fe2caefcSParav Pandit 	ocrdma_ring_cq_db(dev, dev->mq.cq.id, true, false, 0);
632fe2caefcSParav Pandit 	return 0;
633fe2caefcSParav Pandit 
634fe2caefcSParav Pandit mbx_q_free:
635fe2caefcSParav Pandit 	ocrdma_free_q(dev, &dev->mq.sq);
636fe2caefcSParav Pandit mbx_cq_destroy:
637fe2caefcSParav Pandit 	ocrdma_mbx_delete_q(dev, &dev->mq.cq, QTYPE_CQ);
638fe2caefcSParav Pandit mbx_cq_free:
639fe2caefcSParav Pandit 	ocrdma_free_q(dev, &dev->mq.cq);
640fe2caefcSParav Pandit alloc_err:
641fe2caefcSParav Pandit 	return status;
642fe2caefcSParav Pandit }
643fe2caefcSParav Pandit 
ocrdma_destroy_mq(struct ocrdma_dev * dev)644fe2caefcSParav Pandit static void ocrdma_destroy_mq(struct ocrdma_dev *dev)
645fe2caefcSParav Pandit {
646fe2caefcSParav Pandit 	struct ocrdma_queue_info *mbxq, *cq;
647fe2caefcSParav Pandit 
648fe2caefcSParav Pandit 	/* mqe_ctx lock synchronizes with any other pending cmds. */
649fe2caefcSParav Pandit 	mutex_lock(&dev->mqe_ctx.lock);
650fe2caefcSParav Pandit 	mbxq = &dev->mq.sq;
651fe2caefcSParav Pandit 	if (mbxq->created) {
652fe2caefcSParav Pandit 		ocrdma_mbx_delete_q(dev, mbxq, QTYPE_MCCQ);
653fe2caefcSParav Pandit 		ocrdma_free_q(dev, mbxq);
654fe2caefcSParav Pandit 	}
655fe2caefcSParav Pandit 	mutex_unlock(&dev->mqe_ctx.lock);
656fe2caefcSParav Pandit 
657fe2caefcSParav Pandit 	cq = &dev->mq.cq;
658fe2caefcSParav Pandit 	if (cq->created) {
659fe2caefcSParav Pandit 		ocrdma_mbx_delete_q(dev, cq, QTYPE_CQ);
660fe2caefcSParav Pandit 		ocrdma_free_q(dev, cq);
661fe2caefcSParav Pandit 	}
662fe2caefcSParav Pandit }
663fe2caefcSParav Pandit 
ocrdma_process_qpcat_error(struct ocrdma_dev * dev,struct ocrdma_qp * qp)664fe2caefcSParav Pandit static void ocrdma_process_qpcat_error(struct ocrdma_dev *dev,
665fe2caefcSParav Pandit 				       struct ocrdma_qp *qp)
666fe2caefcSParav Pandit {
667fe2caefcSParav Pandit 	enum ib_qp_state new_ib_qps = IB_QPS_ERR;
668fe2caefcSParav Pandit 	enum ib_qp_state old_ib_qps;
669fe2caefcSParav Pandit 
670fe2caefcSParav Pandit 	if (qp == NULL)
671fe2caefcSParav Pandit 		BUG();
672057729cbSNaresh Gottumukkala 	ocrdma_qp_state_change(qp, new_ib_qps, &old_ib_qps);
673fe2caefcSParav Pandit }
674fe2caefcSParav Pandit 
ocrdma_dispatch_ibevent(struct ocrdma_dev * dev,struct ocrdma_ae_mcqe * cqe)675fe2caefcSParav Pandit static void ocrdma_dispatch_ibevent(struct ocrdma_dev *dev,
676fe2caefcSParav Pandit 				    struct ocrdma_ae_mcqe *cqe)
677fe2caefcSParav Pandit {
678fe2caefcSParav Pandit 	struct ocrdma_qp *qp = NULL;
679fe2caefcSParav Pandit 	struct ocrdma_cq *cq = NULL;
6801b09a0c2SSelvin Xavier 	struct ib_event ib_evt;
681fe2caefcSParav Pandit 	int cq_event = 0;
682fe2caefcSParav Pandit 	int qp_event = 1;
683fe2caefcSParav Pandit 	int srq_event = 0;
684fe2caefcSParav Pandit 	int dev_event = 0;
685fe2caefcSParav Pandit 	int type = (cqe->valid_ae_event & OCRDMA_AE_MCQE_EVENT_TYPE_MASK) >>
686fe2caefcSParav Pandit 	    OCRDMA_AE_MCQE_EVENT_TYPE_SHIFT;
687aeb922dfSNaga Irrinki 	u16 qpid = cqe->qpvalid_qpid & OCRDMA_AE_MCQE_QPID_MASK;
688aeb922dfSNaga Irrinki 	u16 cqid = cqe->cqvalid_cqid & OCRDMA_AE_MCQE_CQID_MASK;
689fe2caefcSParav Pandit 
690aeb922dfSNaga Irrinki 	/*
691aeb922dfSNaga Irrinki 	 * Some FW version returns wrong qp or cq ids in CQEs.
692aeb922dfSNaga Irrinki 	 * Checking whether the IDs are valid
693aeb922dfSNaga Irrinki 	 */
694aeb922dfSNaga Irrinki 
695aeb922dfSNaga Irrinki 	if (cqe->qpvalid_qpid & OCRDMA_AE_MCQE_QPVALID) {
696aeb922dfSNaga Irrinki 		if (qpid < dev->attr.max_qp)
697aeb922dfSNaga Irrinki 			qp = dev->qp_tbl[qpid];
698aeb922dfSNaga Irrinki 		if (qp == NULL) {
699aeb922dfSNaga Irrinki 			pr_err("ocrdma%d:Async event - qpid %u is not valid\n",
700aeb922dfSNaga Irrinki 			       dev->id, qpid);
701aeb922dfSNaga Irrinki 			return;
702aeb922dfSNaga Irrinki 		}
703aeb922dfSNaga Irrinki 	}
704aeb922dfSNaga Irrinki 
705aeb922dfSNaga Irrinki 	if (cqe->cqvalid_cqid & OCRDMA_AE_MCQE_CQVALID) {
706aeb922dfSNaga Irrinki 		if (cqid < dev->attr.max_cq)
707aeb922dfSNaga Irrinki 			cq = dev->cq_tbl[cqid];
708aeb922dfSNaga Irrinki 		if (cq == NULL) {
709aeb922dfSNaga Irrinki 			pr_err("ocrdma%d:Async event - cqid %u is not valid\n",
710aeb922dfSNaga Irrinki 			       dev->id, cqid);
711aeb922dfSNaga Irrinki 			return;
712aeb922dfSNaga Irrinki 		}
713aeb922dfSNaga Irrinki 	}
714fe2caefcSParav Pandit 
7151b09a0c2SSelvin Xavier 	memset(&ib_evt, 0, sizeof(ib_evt));
7161b09a0c2SSelvin Xavier 
717e9db2953SRoland Dreier 	ib_evt.device = &dev->ibdev;
718e9db2953SRoland Dreier 
719fe2caefcSParav Pandit 	switch (type) {
720fe2caefcSParav Pandit 	case OCRDMA_CQ_ERROR:
721fe2caefcSParav Pandit 		ib_evt.element.cq = &cq->ibcq;
722fe2caefcSParav Pandit 		ib_evt.event = IB_EVENT_CQ_ERR;
723fe2caefcSParav Pandit 		cq_event = 1;
724fe2caefcSParav Pandit 		qp_event = 0;
725fe2caefcSParav Pandit 		break;
726fe2caefcSParav Pandit 	case OCRDMA_CQ_OVERRUN_ERROR:
727fe2caefcSParav Pandit 		ib_evt.element.cq = &cq->ibcq;
728fe2caefcSParav Pandit 		ib_evt.event = IB_EVENT_CQ_ERR;
7291228056bSSelvin Xavier 		cq_event = 1;
7301228056bSSelvin Xavier 		qp_event = 0;
731fe2caefcSParav Pandit 		break;
732fe2caefcSParav Pandit 	case OCRDMA_CQ_QPCAT_ERROR:
733fe2caefcSParav Pandit 		ib_evt.element.qp = &qp->ibqp;
734fe2caefcSParav Pandit 		ib_evt.event = IB_EVENT_QP_FATAL;
735fe2caefcSParav Pandit 		ocrdma_process_qpcat_error(dev, qp);
736fe2caefcSParav Pandit 		break;
737fe2caefcSParav Pandit 	case OCRDMA_QP_ACCESS_ERROR:
738fe2caefcSParav Pandit 		ib_evt.element.qp = &qp->ibqp;
739fe2caefcSParav Pandit 		ib_evt.event = IB_EVENT_QP_ACCESS_ERR;
740fe2caefcSParav Pandit 		break;
741fe2caefcSParav Pandit 	case OCRDMA_QP_COMM_EST_EVENT:
742fe2caefcSParav Pandit 		ib_evt.element.qp = &qp->ibqp;
743fe2caefcSParav Pandit 		ib_evt.event = IB_EVENT_COMM_EST;
744fe2caefcSParav Pandit 		break;
745fe2caefcSParav Pandit 	case OCRDMA_SQ_DRAINED_EVENT:
746fe2caefcSParav Pandit 		ib_evt.element.qp = &qp->ibqp;
747fe2caefcSParav Pandit 		ib_evt.event = IB_EVENT_SQ_DRAINED;
748fe2caefcSParav Pandit 		break;
749fe2caefcSParav Pandit 	case OCRDMA_DEVICE_FATAL_EVENT:
750fe2caefcSParav Pandit 		ib_evt.element.port_num = 1;
751fe2caefcSParav Pandit 		ib_evt.event = IB_EVENT_DEVICE_FATAL;
752fe2caefcSParav Pandit 		qp_event = 0;
753fe2caefcSParav Pandit 		dev_event = 1;
754fe2caefcSParav Pandit 		break;
755fe2caefcSParav Pandit 	case OCRDMA_SRQCAT_ERROR:
756fe2caefcSParav Pandit 		ib_evt.element.srq = &qp->srq->ibsrq;
757fe2caefcSParav Pandit 		ib_evt.event = IB_EVENT_SRQ_ERR;
758fe2caefcSParav Pandit 		srq_event = 1;
759fe2caefcSParav Pandit 		qp_event = 0;
760fe2caefcSParav Pandit 		break;
761fe2caefcSParav Pandit 	case OCRDMA_SRQ_LIMIT_EVENT:
762fe2caefcSParav Pandit 		ib_evt.element.srq = &qp->srq->ibsrq;
763804eaf29SParav Pandit 		ib_evt.event = IB_EVENT_SRQ_LIMIT_REACHED;
764fe2caefcSParav Pandit 		srq_event = 1;
765fe2caefcSParav Pandit 		qp_event = 0;
766fe2caefcSParav Pandit 		break;
767fe2caefcSParav Pandit 	case OCRDMA_QP_LAST_WQE_EVENT:
768fe2caefcSParav Pandit 		ib_evt.element.qp = &qp->ibqp;
769fe2caefcSParav Pandit 		ib_evt.event = IB_EVENT_QP_LAST_WQE_REACHED;
770fe2caefcSParav Pandit 		break;
771fe2caefcSParav Pandit 	default:
772fe2caefcSParav Pandit 		cq_event = 0;
773fe2caefcSParav Pandit 		qp_event = 0;
774fe2caefcSParav Pandit 		srq_event = 0;
775fe2caefcSParav Pandit 		dev_event = 0;
776ef99c4c2SNaresh Gottumukkala 		pr_err("%s() unknown type=0x%x\n", __func__, type);
777fe2caefcSParav Pandit 		break;
778fe2caefcSParav Pandit 	}
779fe2caefcSParav Pandit 
780ad56ebb4SSelvin Xavier 	if (type < OCRDMA_MAX_ASYNC_ERRORS)
781ad56ebb4SSelvin Xavier 		atomic_inc(&dev->async_err_stats[type]);
782ad56ebb4SSelvin Xavier 
783fe2caefcSParav Pandit 	if (qp_event) {
784fe2caefcSParav Pandit 		if (qp->ibqp.event_handler)
785fe2caefcSParav Pandit 			qp->ibqp.event_handler(&ib_evt, qp->ibqp.qp_context);
786fe2caefcSParav Pandit 	} else if (cq_event) {
787fe2caefcSParav Pandit 		if (cq->ibcq.event_handler)
788fe2caefcSParav Pandit 			cq->ibcq.event_handler(&ib_evt, cq->ibcq.cq_context);
789fe2caefcSParav Pandit 	} else if (srq_event) {
790fe2caefcSParav Pandit 		if (qp->srq->ibsrq.event_handler)
791fe2caefcSParav Pandit 			qp->srq->ibsrq.event_handler(&ib_evt,
792fe2caefcSParav Pandit 						     qp->srq->ibsrq.
793fe2caefcSParav Pandit 						     srq_context);
794f99b1649SNaresh Gottumukkala 	} else if (dev_event) {
7955a738b5dSJason Gunthorpe 		dev_err(&dev->ibdev.dev, "Fatal event received\n");
796fe2caefcSParav Pandit 		ib_dispatch_event(&ib_evt);
797f99b1649SNaresh Gottumukkala 	}
798fe2caefcSParav Pandit 
799fe2caefcSParav Pandit }
800fe2caefcSParav Pandit 
ocrdma_process_grp5_aync(struct ocrdma_dev * dev,struct ocrdma_ae_mcqe * cqe)80184b105dbSNaresh Gottumukkala static void ocrdma_process_grp5_aync(struct ocrdma_dev *dev,
80284b105dbSNaresh Gottumukkala 					struct ocrdma_ae_mcqe *cqe)
80384b105dbSNaresh Gottumukkala {
80484b105dbSNaresh Gottumukkala 	struct ocrdma_ae_pvid_mcqe *evt;
80584b105dbSNaresh Gottumukkala 	int type = (cqe->valid_ae_event & OCRDMA_AE_MCQE_EVENT_TYPE_MASK) >>
80684b105dbSNaresh Gottumukkala 			OCRDMA_AE_MCQE_EVENT_TYPE_SHIFT;
80784b105dbSNaresh Gottumukkala 
80884b105dbSNaresh Gottumukkala 	switch (type) {
80984b105dbSNaresh Gottumukkala 	case OCRDMA_ASYNC_EVENT_PVID_STATE:
81084b105dbSNaresh Gottumukkala 		evt = (struct ocrdma_ae_pvid_mcqe *)cqe;
81184b105dbSNaresh Gottumukkala 		if ((evt->tag_enabled & OCRDMA_AE_PVID_MCQE_ENABLED_MASK) >>
81284b105dbSNaresh Gottumukkala 			OCRDMA_AE_PVID_MCQE_ENABLED_SHIFT)
81384b105dbSNaresh Gottumukkala 			dev->pvid = ((evt->tag_enabled &
81484b105dbSNaresh Gottumukkala 					OCRDMA_AE_PVID_MCQE_TAG_MASK) >>
81584b105dbSNaresh Gottumukkala 					OCRDMA_AE_PVID_MCQE_TAG_SHIFT);
81684b105dbSNaresh Gottumukkala 		break;
81731dbdd9aSSelvin Xavier 
81831dbdd9aSSelvin Xavier 	case OCRDMA_ASYNC_EVENT_COS_VALUE:
81931dbdd9aSSelvin Xavier 		atomic_set(&dev->update_sl, 1);
82031dbdd9aSSelvin Xavier 		break;
82184b105dbSNaresh Gottumukkala 	default:
82284b105dbSNaresh Gottumukkala 		/* Not interested evts. */
82384b105dbSNaresh Gottumukkala 		break;
82484b105dbSNaresh Gottumukkala 	}
82584b105dbSNaresh Gottumukkala }
82684b105dbSNaresh Gottumukkala 
ocrdma_process_link_state(struct ocrdma_dev * dev,struct ocrdma_ae_mcqe * cqe)82710a214dcSDevesh Sharma static void ocrdma_process_link_state(struct ocrdma_dev *dev,
82810a214dcSDevesh Sharma 				      struct ocrdma_ae_mcqe *cqe)
82910a214dcSDevesh Sharma {
83010a214dcSDevesh Sharma 	struct ocrdma_ae_lnkst_mcqe *evt;
83110a214dcSDevesh Sharma 	u8 lstate;
83210a214dcSDevesh Sharma 
83310a214dcSDevesh Sharma 	evt = (struct ocrdma_ae_lnkst_mcqe *)cqe;
83410a214dcSDevesh Sharma 	lstate = ocrdma_get_ae_link_state(evt->speed_state_ptn);
83510a214dcSDevesh Sharma 
83610a214dcSDevesh Sharma 	if (!(lstate & OCRDMA_AE_LSC_LLINK_MASK))
83710a214dcSDevesh Sharma 		return;
83810a214dcSDevesh Sharma 
83910a214dcSDevesh Sharma 	if (dev->flags & OCRDMA_FLAGS_LINK_STATUS_INIT)
84010a214dcSDevesh Sharma 		ocrdma_update_link_state(dev, (lstate & OCRDMA_LINK_ST_MASK));
84110a214dcSDevesh Sharma }
84210a214dcSDevesh Sharma 
ocrdma_process_acqe(struct ocrdma_dev * dev,void * ae_cqe)843fe2caefcSParav Pandit static void ocrdma_process_acqe(struct ocrdma_dev *dev, void *ae_cqe)
844fe2caefcSParav Pandit {
845fe2caefcSParav Pandit 	/* async CQE processing */
846fe2caefcSParav Pandit 	struct ocrdma_ae_mcqe *cqe = ae_cqe;
847fe2caefcSParav Pandit 	u32 evt_code = (cqe->valid_ae_event & OCRDMA_AE_MCQE_EVENT_CODE_MASK) >>
848fe2caefcSParav Pandit 			OCRDMA_AE_MCQE_EVENT_CODE_SHIFT;
84910a214dcSDevesh Sharma 	switch (evt_code) {
85010a214dcSDevesh Sharma 	case OCRDMA_ASYNC_LINK_EVE_CODE:
85110a214dcSDevesh Sharma 		ocrdma_process_link_state(dev, cqe);
85210a214dcSDevesh Sharma 		break;
85310a214dcSDevesh Sharma 	case OCRDMA_ASYNC_RDMA_EVE_CODE:
854fe2caefcSParav Pandit 		ocrdma_dispatch_ibevent(dev, cqe);
85510a214dcSDevesh Sharma 		break;
85610a214dcSDevesh Sharma 	case OCRDMA_ASYNC_GRP5_EVE_CODE:
85784b105dbSNaresh Gottumukkala 		ocrdma_process_grp5_aync(dev, cqe);
85810a214dcSDevesh Sharma 		break;
85910a214dcSDevesh Sharma 	default:
860ef99c4c2SNaresh Gottumukkala 		pr_err("%s(%d) invalid evt code=0x%x\n", __func__,
861ef99c4c2SNaresh Gottumukkala 		       dev->id, evt_code);
862fe2caefcSParav Pandit 	}
86310a214dcSDevesh Sharma }
864fe2caefcSParav Pandit 
ocrdma_process_mcqe(struct ocrdma_dev * dev,struct ocrdma_mcqe * cqe)865fe2caefcSParav Pandit static void ocrdma_process_mcqe(struct ocrdma_dev *dev, struct ocrdma_mcqe *cqe)
866fe2caefcSParav Pandit {
867fe2caefcSParav Pandit 	if (dev->mqe_ctx.tag == cqe->tag_lo && dev->mqe_ctx.cmd_done == false) {
868fe2caefcSParav Pandit 		dev->mqe_ctx.cqe_status = (cqe->status &
869fe2caefcSParav Pandit 		     OCRDMA_MCQE_STATUS_MASK) >> OCRDMA_MCQE_STATUS_SHIFT;
870fe2caefcSParav Pandit 		dev->mqe_ctx.ext_status =
871fe2caefcSParav Pandit 		    (cqe->status & OCRDMA_MCQE_ESTATUS_MASK)
872fe2caefcSParav Pandit 		    >> OCRDMA_MCQE_ESTATUS_SHIFT;
873fe2caefcSParav Pandit 		dev->mqe_ctx.cmd_done = true;
874fe2caefcSParav Pandit 		wake_up(&dev->mqe_ctx.cmd_wait);
875fe2caefcSParav Pandit 	} else
876ef99c4c2SNaresh Gottumukkala 		pr_err("%s() cqe for invalid tag0x%x.expected=0x%x\n",
877fe2caefcSParav Pandit 		       __func__, cqe->tag_lo, dev->mqe_ctx.tag);
878fe2caefcSParav Pandit }
879fe2caefcSParav Pandit 
ocrdma_mq_cq_handler(struct ocrdma_dev * dev,u16 cq_id)880fe2caefcSParav Pandit static int ocrdma_mq_cq_handler(struct ocrdma_dev *dev, u16 cq_id)
881fe2caefcSParav Pandit {
882fe2caefcSParav Pandit 	u16 cqe_popped = 0;
883fe2caefcSParav Pandit 	struct ocrdma_mcqe *cqe;
884fe2caefcSParav Pandit 
885fe2caefcSParav Pandit 	while (1) {
886fe2caefcSParav Pandit 		cqe = ocrdma_get_mcqe(dev);
887fe2caefcSParav Pandit 		if (cqe == NULL)
888fe2caefcSParav Pandit 			break;
889fe2caefcSParav Pandit 		ocrdma_le32_to_cpu(cqe, sizeof(*cqe));
890fe2caefcSParav Pandit 		cqe_popped += 1;
891fe2caefcSParav Pandit 		if (cqe->valid_ae_cmpl_cons & OCRDMA_MCQE_AE_MASK)
892fe2caefcSParav Pandit 			ocrdma_process_acqe(dev, cqe);
893fe2caefcSParav Pandit 		else if (cqe->valid_ae_cmpl_cons & OCRDMA_MCQE_CMPL_MASK)
894fe2caefcSParav Pandit 			ocrdma_process_mcqe(dev, cqe);
895fe2caefcSParav Pandit 		memset(cqe, 0, sizeof(struct ocrdma_mcqe));
896fe2caefcSParav Pandit 		ocrdma_mcq_inc_tail(dev);
897fe2caefcSParav Pandit 	}
898fe2caefcSParav Pandit 	ocrdma_ring_cq_db(dev, dev->mq.cq.id, true, false, cqe_popped);
899fe2caefcSParav Pandit 	return 0;
900fe2caefcSParav Pandit }
901fe2caefcSParav Pandit 
_ocrdma_qp_buddy_cq_handler(struct ocrdma_dev * dev,struct ocrdma_cq * cq,bool sq)902043e9deeSSelvin Xavier static struct ocrdma_cq *_ocrdma_qp_buddy_cq_handler(struct ocrdma_dev *dev,
903043e9deeSSelvin Xavier 				struct ocrdma_cq *cq, bool sq)
904fe2caefcSParav Pandit {
905fe2caefcSParav Pandit 	struct ocrdma_qp *qp;
906043e9deeSSelvin Xavier 	struct list_head *cur;
907043e9deeSSelvin Xavier 	struct ocrdma_cq *bcq = NULL;
908043e9deeSSelvin Xavier 	struct list_head *head = sq?(&cq->sq_head):(&cq->rq_head);
909043e9deeSSelvin Xavier 
910043e9deeSSelvin Xavier 	list_for_each(cur, head) {
911043e9deeSSelvin Xavier 		if (sq)
912043e9deeSSelvin Xavier 			qp = list_entry(cur, struct ocrdma_qp, sq_entry);
913043e9deeSSelvin Xavier 		else
914043e9deeSSelvin Xavier 			qp = list_entry(cur, struct ocrdma_qp, rq_entry);
915043e9deeSSelvin Xavier 
916fe2caefcSParav Pandit 		if (qp->srq)
917fe2caefcSParav Pandit 			continue;
918fe2caefcSParav Pandit 		/* if wq and rq share the same cq, than comp_handler
919fe2caefcSParav Pandit 		 * is already invoked.
920fe2caefcSParav Pandit 		 */
921fe2caefcSParav Pandit 		if (qp->sq_cq == qp->rq_cq)
922fe2caefcSParav Pandit 			continue;
923fe2caefcSParav Pandit 		/* if completion came on sq, rq's cq is buddy cq.
924fe2caefcSParav Pandit 		 * if completion came on rq, sq's cq is buddy cq.
925fe2caefcSParav Pandit 		 */
926fe2caefcSParav Pandit 		if (qp->sq_cq == cq)
927043e9deeSSelvin Xavier 			bcq = qp->rq_cq;
928fe2caefcSParav Pandit 		else
929043e9deeSSelvin Xavier 			bcq = qp->sq_cq;
930043e9deeSSelvin Xavier 		return bcq;
931fe2caefcSParav Pandit 	}
932043e9deeSSelvin Xavier 	return NULL;
933043e9deeSSelvin Xavier }
934043e9deeSSelvin Xavier 
ocrdma_qp_buddy_cq_handler(struct ocrdma_dev * dev,struct ocrdma_cq * cq)935043e9deeSSelvin Xavier static void ocrdma_qp_buddy_cq_handler(struct ocrdma_dev *dev,
936043e9deeSSelvin Xavier 				       struct ocrdma_cq *cq)
937043e9deeSSelvin Xavier {
938043e9deeSSelvin Xavier 	unsigned long flags;
939043e9deeSSelvin Xavier 	struct ocrdma_cq *bcq = NULL;
940043e9deeSSelvin Xavier 
941043e9deeSSelvin Xavier 	/* Go through list of QPs in error state which are using this CQ
942043e9deeSSelvin Xavier 	 * and invoke its callback handler to trigger CQE processing for
943043e9deeSSelvin Xavier 	 * error/flushed CQE. It is rare to find more than few entries in
944043e9deeSSelvin Xavier 	 * this list as most consumers stops after getting error CQE.
945043e9deeSSelvin Xavier 	 * List is traversed only once when a matching buddy cq found for a QP.
946043e9deeSSelvin Xavier 	 */
947043e9deeSSelvin Xavier 	spin_lock_irqsave(&dev->flush_q_lock, flags);
948043e9deeSSelvin Xavier 	/* Check if buddy CQ is present.
949043e9deeSSelvin Xavier 	 * true - Check for  SQ CQ
950043e9deeSSelvin Xavier 	 * false - Check for RQ CQ
951043e9deeSSelvin Xavier 	 */
952043e9deeSSelvin Xavier 	bcq = _ocrdma_qp_buddy_cq_handler(dev, cq, true);
953043e9deeSSelvin Xavier 	if (bcq == NULL)
954043e9deeSSelvin Xavier 		bcq = _ocrdma_qp_buddy_cq_handler(dev, cq, false);
955fe2caefcSParav Pandit 	spin_unlock_irqrestore(&dev->flush_q_lock, flags);
956043e9deeSSelvin Xavier 
957043e9deeSSelvin Xavier 	/* if there is valid buddy cq, look for its completion handler */
958043e9deeSSelvin Xavier 	if (bcq && bcq->ibcq.comp_handler) {
959043e9deeSSelvin Xavier 		spin_lock_irqsave(&bcq->comp_handler_lock, flags);
960043e9deeSSelvin Xavier 		(*bcq->ibcq.comp_handler) (&bcq->ibcq, bcq->ibcq.cq_context);
961043e9deeSSelvin Xavier 		spin_unlock_irqrestore(&bcq->comp_handler_lock, flags);
962fe2caefcSParav Pandit 	}
963fe2caefcSParav Pandit }
964fe2caefcSParav Pandit 
ocrdma_qp_cq_handler(struct ocrdma_dev * dev,u16 cq_idx)965fe2caefcSParav Pandit static void ocrdma_qp_cq_handler(struct ocrdma_dev *dev, u16 cq_idx)
966fe2caefcSParav Pandit {
967fe2caefcSParav Pandit 	unsigned long flags;
968fe2caefcSParav Pandit 	struct ocrdma_cq *cq;
969fe2caefcSParav Pandit 
970fe2caefcSParav Pandit 	if (cq_idx >= OCRDMA_MAX_CQ)
971fe2caefcSParav Pandit 		BUG();
972fe2caefcSParav Pandit 
973fe2caefcSParav Pandit 	cq = dev->cq_tbl[cq_idx];
974ea617626SDevesh Sharma 	if (cq == NULL)
975fe2caefcSParav Pandit 		return;
976fe2caefcSParav Pandit 
977fe2caefcSParav Pandit 	if (cq->ibcq.comp_handler) {
978fe2caefcSParav Pandit 		spin_lock_irqsave(&cq->comp_handler_lock, flags);
979fe2caefcSParav Pandit 		(*cq->ibcq.comp_handler) (&cq->ibcq, cq->ibcq.cq_context);
980fe2caefcSParav Pandit 		spin_unlock_irqrestore(&cq->comp_handler_lock, flags);
981fe2caefcSParav Pandit 	}
982fe2caefcSParav Pandit 	ocrdma_qp_buddy_cq_handler(dev, cq);
983fe2caefcSParav Pandit }
984fe2caefcSParav Pandit 
ocrdma_cq_handler(struct ocrdma_dev * dev,u16 cq_id)985fe2caefcSParav Pandit static void ocrdma_cq_handler(struct ocrdma_dev *dev, u16 cq_id)
986fe2caefcSParav Pandit {
987fe2caefcSParav Pandit 	/* process the MQ-CQE. */
988fe2caefcSParav Pandit 	if (cq_id == dev->mq.cq.id)
989fe2caefcSParav Pandit 		ocrdma_mq_cq_handler(dev, cq_id);
990fe2caefcSParav Pandit 	else
991fe2caefcSParav Pandit 		ocrdma_qp_cq_handler(dev, cq_id);
992fe2caefcSParav Pandit }
993fe2caefcSParav Pandit 
ocrdma_irq_handler(int irq,void * handle)994fe2caefcSParav Pandit static irqreturn_t ocrdma_irq_handler(int irq, void *handle)
995fe2caefcSParav Pandit {
996fe2caefcSParav Pandit 	struct ocrdma_eq *eq = handle;
997fe2caefcSParav Pandit 	struct ocrdma_dev *dev = eq->dev;
998fe2caefcSParav Pandit 	struct ocrdma_eqe eqe;
999fe2caefcSParav Pandit 	struct ocrdma_eqe *ptr;
1000fe2caefcSParav Pandit 	u16 cq_id;
10015e6f9237SDevesh Sharma 	u8 mcode;
1002ea617626SDevesh Sharma 	int budget = eq->cq_cnt;
1003ea617626SDevesh Sharma 
1004ea617626SDevesh Sharma 	do {
1005fe2caefcSParav Pandit 		ptr = ocrdma_get_eqe(eq);
1006fe2caefcSParav Pandit 		eqe = *ptr;
1007fe2caefcSParav Pandit 		ocrdma_le32_to_cpu(&eqe, sizeof(eqe));
10085e6f9237SDevesh Sharma 		mcode = (eqe.id_valid & OCRDMA_EQE_MAJOR_CODE_MASK)
10095e6f9237SDevesh Sharma 				>> OCRDMA_EQE_MAJOR_CODE_SHIFT;
10105e6f9237SDevesh Sharma 		if (mcode == OCRDMA_MAJOR_CODE_SENTINAL)
10115e6f9237SDevesh Sharma 			pr_err("EQ full on eqid = 0x%x, eqe = 0x%x\n",
10125e6f9237SDevesh Sharma 			       eq->q.id, eqe.id_valid);
1013fe2caefcSParav Pandit 		if ((eqe.id_valid & OCRDMA_EQE_VALID_MASK) == 0)
1014fe2caefcSParav Pandit 			break;
1015ea617626SDevesh Sharma 
1016fe2caefcSParav Pandit 		ptr->id_valid = 0;
1017ea617626SDevesh Sharma 		/* ring eq doorbell as soon as its consumed. */
1018ea617626SDevesh Sharma 		ocrdma_ring_eq_db(dev, eq->q.id, false, true, 1);
1019fe2caefcSParav Pandit 		/* check whether its CQE or not. */
1020fe2caefcSParav Pandit 		if ((eqe.id_valid & OCRDMA_EQE_FOR_CQE_MASK) == 0) {
1021fe2caefcSParav Pandit 			cq_id = eqe.id_valid >> OCRDMA_EQE_RESOURCE_ID_SHIFT;
1022fe2caefcSParav Pandit 			ocrdma_cq_handler(dev, cq_id);
1023fe2caefcSParav Pandit 		}
1024fe2caefcSParav Pandit 		ocrdma_eq_inc_tail(eq);
1025ea617626SDevesh Sharma 
1026ea617626SDevesh Sharma 		/* There can be a stale EQE after the last bound CQ is
1027ea617626SDevesh Sharma 		 * destroyed. EQE valid and budget == 0 implies this.
1028ea617626SDevesh Sharma 		 */
1029ea617626SDevesh Sharma 		if (budget)
1030ea617626SDevesh Sharma 			budget--;
1031ea617626SDevesh Sharma 
1032ea617626SDevesh Sharma 	} while (budget);
1033ea617626SDevesh Sharma 
1034b4dbe8d5SMitesh Ahuja 	eq->aic_obj.eq_intr_cnt++;
1035fe2caefcSParav Pandit 	ocrdma_ring_eq_db(dev, eq->q.id, true, true, 0);
1036fe2caefcSParav Pandit 	return IRQ_HANDLED;
1037fe2caefcSParav Pandit }
1038fe2caefcSParav Pandit 
ocrdma_post_mqe(struct ocrdma_dev * dev,struct ocrdma_mqe * cmd)1039fe2caefcSParav Pandit static void ocrdma_post_mqe(struct ocrdma_dev *dev, struct ocrdma_mqe *cmd)
1040fe2caefcSParav Pandit {
1041fe2caefcSParav Pandit 	struct ocrdma_mqe *mqe;
1042fe2caefcSParav Pandit 
1043fe2caefcSParav Pandit 	dev->mqe_ctx.tag = dev->mq.sq.head;
1044fe2caefcSParav Pandit 	dev->mqe_ctx.cmd_done = false;
1045fe2caefcSParav Pandit 	mqe = ocrdma_get_mqe(dev);
1046fe2caefcSParav Pandit 	cmd->hdr.tag_lo = dev->mq.sq.head;
1047fe2caefcSParav Pandit 	ocrdma_copy_cpu_to_le32(mqe, cmd, sizeof(*mqe));
1048fe2caefcSParav Pandit 	/* make sure descriptor is written before ringing doorbell */
1049fe2caefcSParav Pandit 	wmb();
1050fe2caefcSParav Pandit 	ocrdma_mq_inc_head(dev);
1051fe2caefcSParav Pandit 	ocrdma_ring_mq_db(dev);
1052fe2caefcSParav Pandit }
1053fe2caefcSParav Pandit 
ocrdma_wait_mqe_cmpl(struct ocrdma_dev * dev)1054fe2caefcSParav Pandit static int ocrdma_wait_mqe_cmpl(struct ocrdma_dev *dev)
1055fe2caefcSParav Pandit {
1056fe2caefcSParav Pandit 	long status;
1057fe2caefcSParav Pandit 	/* 30 sec timeout */
1058fe2caefcSParav Pandit 	status = wait_event_timeout(dev->mqe_ctx.cmd_wait,
1059fe2caefcSParav Pandit 				    (dev->mqe_ctx.cmd_done != false),
1060fe2caefcSParav Pandit 				    msecs_to_jiffies(30000));
1061fe2caefcSParav Pandit 	if (status)
1062fe2caefcSParav Pandit 		return 0;
10636dab0264SMitesh Ahuja 	else {
10646dab0264SMitesh Ahuja 		dev->mqe_ctx.fw_error_state = true;
10656dab0264SMitesh Ahuja 		pr_err("%s(%d) mailbox timeout: fw not responding\n",
10666dab0264SMitesh Ahuja 		       __func__, dev->id);
1067fe2caefcSParav Pandit 		return -1;
1068fe2caefcSParav Pandit 	}
10696dab0264SMitesh Ahuja }
1070fe2caefcSParav Pandit 
1071fe2caefcSParav Pandit /* issue a mailbox command on the MQ */
ocrdma_mbx_cmd(struct ocrdma_dev * dev,struct ocrdma_mqe * mqe)1072fe2caefcSParav Pandit static int ocrdma_mbx_cmd(struct ocrdma_dev *dev, struct ocrdma_mqe *mqe)
1073fe2caefcSParav Pandit {
1074fe2caefcSParav Pandit 	int status = 0;
1075fe2caefcSParav Pandit 	u16 cqe_status, ext_status;
1076bbc5ec52SSelvin Xavier 	struct ocrdma_mqe *rsp_mqe;
1077bbc5ec52SSelvin Xavier 	struct ocrdma_mbx_rsp *rsp = NULL;
1078fe2caefcSParav Pandit 
1079fe2caefcSParav Pandit 	mutex_lock(&dev->mqe_ctx.lock);
10806dab0264SMitesh Ahuja 	if (dev->mqe_ctx.fw_error_state)
10816dab0264SMitesh Ahuja 		goto mbx_err;
1082fe2caefcSParav Pandit 	ocrdma_post_mqe(dev, mqe);
1083fe2caefcSParav Pandit 	status = ocrdma_wait_mqe_cmpl(dev);
1084fe2caefcSParav Pandit 	if (status)
1085fe2caefcSParav Pandit 		goto mbx_err;
1086fe2caefcSParav Pandit 	cqe_status = dev->mqe_ctx.cqe_status;
1087fe2caefcSParav Pandit 	ext_status = dev->mqe_ctx.ext_status;
1088bbc5ec52SSelvin Xavier 	rsp_mqe = ocrdma_get_mqe_rsp(dev);
1089bbc5ec52SSelvin Xavier 	ocrdma_copy_le32_to_cpu(mqe, rsp_mqe, (sizeof(*mqe)));
1090bbc5ec52SSelvin Xavier 	if ((mqe->hdr.spcl_sge_cnt_emb & OCRDMA_MQE_HDR_EMB_MASK) >>
1091bbc5ec52SSelvin Xavier 				OCRDMA_MQE_HDR_EMB_SHIFT)
1092bbc5ec52SSelvin Xavier 		rsp = &mqe->u.rsp;
1093bbc5ec52SSelvin Xavier 
1094fe2caefcSParav Pandit 	if (cqe_status || ext_status) {
109544596ebfSArvind Yadav 		pr_err("%s() cqe_status=0x%x, ext_status=0x%x,\n",
1096bbc5ec52SSelvin Xavier 		       __func__, cqe_status, ext_status);
1097bbc5ec52SSelvin Xavier 		if (rsp) {
1098bbc5ec52SSelvin Xavier 			/* This is for embedded cmds. */
1099bbc5ec52SSelvin Xavier 			pr_err("opcode=0x%x, subsystem=0x%x\n",
1100bbc5ec52SSelvin Xavier 			       (rsp->subsys_op & OCRDMA_MBX_RSP_OPCODE_MASK) >>
1101bbc5ec52SSelvin Xavier 				OCRDMA_MBX_RSP_OPCODE_SHIFT,
1102bbc5ec52SSelvin Xavier 				(rsp->subsys_op & OCRDMA_MBX_RSP_SUBSYS_MASK) >>
1103bbc5ec52SSelvin Xavier 				OCRDMA_MBX_RSP_SUBSYS_SHIFT);
1104bbc5ec52SSelvin Xavier 		}
1105fe2caefcSParav Pandit 		status = ocrdma_get_mbx_cqe_errno(cqe_status);
1106fe2caefcSParav Pandit 		goto mbx_err;
1107fe2caefcSParav Pandit 	}
1108bbc5ec52SSelvin Xavier 	/* For non embedded, rsp errors are handled in ocrdma_nonemb_mbx_cmd */
1109bbc5ec52SSelvin Xavier 	if (rsp && (mqe->u.rsp.status & OCRDMA_MBX_RSP_STATUS_MASK))
1110fe2caefcSParav Pandit 		status = ocrdma_get_mbx_errno(mqe->u.rsp.status);
1111fe2caefcSParav Pandit mbx_err:
1112fe2caefcSParav Pandit 	mutex_unlock(&dev->mqe_ctx.lock);
1113fe2caefcSParav Pandit 	return status;
1114fe2caefcSParav Pandit }
1115fe2caefcSParav Pandit 
ocrdma_nonemb_mbx_cmd(struct ocrdma_dev * dev,struct ocrdma_mqe * mqe,void * payload_va)1116bbc5ec52SSelvin Xavier static int ocrdma_nonemb_mbx_cmd(struct ocrdma_dev *dev, struct ocrdma_mqe *mqe,
1117bbc5ec52SSelvin Xavier 				 void *payload_va)
1118bbc5ec52SSelvin Xavier {
11190ca4c39fSMarkus Elfring 	int status;
1120bbc5ec52SSelvin Xavier 	struct ocrdma_mbx_rsp *rsp = payload_va;
1121bbc5ec52SSelvin Xavier 
1122bbc5ec52SSelvin Xavier 	if ((mqe->hdr.spcl_sge_cnt_emb & OCRDMA_MQE_HDR_EMB_MASK) >>
1123bbc5ec52SSelvin Xavier 				OCRDMA_MQE_HDR_EMB_SHIFT)
1124bbc5ec52SSelvin Xavier 		BUG();
1125bbc5ec52SSelvin Xavier 
1126bbc5ec52SSelvin Xavier 	status = ocrdma_mbx_cmd(dev, mqe);
1127bbc5ec52SSelvin Xavier 	if (!status)
1128bbc5ec52SSelvin Xavier 		/* For non embedded, only CQE failures are handled in
1129bbc5ec52SSelvin Xavier 		 * ocrdma_mbx_cmd. We need to check for RSP errors.
1130bbc5ec52SSelvin Xavier 		 */
1131bbc5ec52SSelvin Xavier 		if (rsp->status & OCRDMA_MBX_RSP_STATUS_MASK)
1132bbc5ec52SSelvin Xavier 			status = ocrdma_get_mbx_errno(rsp->status);
1133bbc5ec52SSelvin Xavier 
1134bbc5ec52SSelvin Xavier 	if (status)
1135bbc5ec52SSelvin Xavier 		pr_err("opcode=0x%x, subsystem=0x%x\n",
1136bbc5ec52SSelvin Xavier 		       (rsp->subsys_op & OCRDMA_MBX_RSP_OPCODE_MASK) >>
1137bbc5ec52SSelvin Xavier 			OCRDMA_MBX_RSP_OPCODE_SHIFT,
1138bbc5ec52SSelvin Xavier 			(rsp->subsys_op & OCRDMA_MBX_RSP_SUBSYS_MASK) >>
1139bbc5ec52SSelvin Xavier 			OCRDMA_MBX_RSP_SUBSYS_SHIFT);
1140bbc5ec52SSelvin Xavier 	return status;
1141bbc5ec52SSelvin Xavier }
1142bbc5ec52SSelvin Xavier 
ocrdma_get_attr(struct ocrdma_dev * dev,struct ocrdma_dev_attr * attr,struct ocrdma_mbx_query_config * rsp)1143fe2caefcSParav Pandit static void ocrdma_get_attr(struct ocrdma_dev *dev,
1144fe2caefcSParav Pandit 			      struct ocrdma_dev_attr *attr,
1145fe2caefcSParav Pandit 			      struct ocrdma_mbx_query_config *rsp)
1146fe2caefcSParav Pandit {
1147fe2caefcSParav Pandit 	attr->max_pd =
1148fe2caefcSParav Pandit 	    (rsp->max_pd_ca_ack_delay & OCRDMA_MBX_QUERY_CFG_MAX_PD_MASK) >>
1149fe2caefcSParav Pandit 	    OCRDMA_MBX_QUERY_CFG_MAX_PD_SHIFT;
1150e1614869SSomnath Kotur 	attr->udp_encap = (rsp->max_pd_ca_ack_delay &
1151e1614869SSomnath Kotur 			   OCRDMA_MBX_QUERY_CFG_L3_TYPE_MASK) >>
1152e1614869SSomnath Kotur 			   OCRDMA_MBX_QUERY_CFG_L3_TYPE_SHIFT;
11539ba1377dSMitesh Ahuja 	attr->max_dpp_pds =
11549ba1377dSMitesh Ahuja 	   (rsp->max_dpp_pds_credits & OCRDMA_MBX_QUERY_CFG_MAX_DPP_PDS_MASK) >>
11559ba1377dSMitesh Ahuja 	    OCRDMA_MBX_QUERY_CFG_MAX_DPP_PDS_OFFSET;
1156fe2caefcSParav Pandit 	attr->max_qp =
1157fe2caefcSParav Pandit 	    (rsp->qp_srq_cq_ird_ord & OCRDMA_MBX_QUERY_CFG_MAX_QP_MASK) >>
1158fe2caefcSParav Pandit 	    OCRDMA_MBX_QUERY_CFG_MAX_QP_SHIFT;
1159fad51b7dSDevesh Sharma 	attr->max_srq =
1160fad51b7dSDevesh Sharma 		(rsp->max_srq_rpir_qps & OCRDMA_MBX_QUERY_CFG_MAX_SRQ_MASK) >>
1161fad51b7dSDevesh Sharma 		OCRDMA_MBX_QUERY_CFG_MAX_SRQ_OFFSET;
11623c199b45SSelvin Xavier 	attr->max_send_sge = ((rsp->max_recv_send_sge &
1163fe2caefcSParav Pandit 			       OCRDMA_MBX_QUERY_CFG_MAX_SEND_SGE_MASK) >>
1164fe2caefcSParav Pandit 			      OCRDMA_MBX_QUERY_CFG_MAX_SEND_SGE_SHIFT);
11653c199b45SSelvin Xavier 	attr->max_recv_sge = (rsp->max_recv_send_sge &
11663c199b45SSelvin Xavier 			      OCRDMA_MBX_QUERY_CFG_MAX_RECV_SGE_MASK) >>
11673c199b45SSelvin Xavier 	    OCRDMA_MBX_QUERY_CFG_MAX_RECV_SGE_SHIFT;
1168634c5796SMahesh Vardhamanaiah 	attr->max_srq_sge = (rsp->max_srq_rqe_sge &
1169634c5796SMahesh Vardhamanaiah 			      OCRDMA_MBX_QUERY_CFG_MAX_SRQ_SGE_MASK) >>
1170634c5796SMahesh Vardhamanaiah 	    OCRDMA_MBX_QUERY_CFG_MAX_SRQ_SGE_OFFSET;
11713c199b45SSelvin Xavier 	attr->max_rdma_sge = (rsp->max_wr_rd_sge &
11723c199b45SSelvin Xavier 			      OCRDMA_MBX_QUERY_CFG_MAX_RD_SGE_MASK) >>
11733c199b45SSelvin Xavier 	    OCRDMA_MBX_QUERY_CFG_MAX_RD_SGE_SHIFT;
1174fe2caefcSParav Pandit 	attr->max_ord_per_qp = (rsp->max_ird_ord_per_qp &
1175fe2caefcSParav Pandit 				OCRDMA_MBX_QUERY_CFG_MAX_ORD_PER_QP_MASK) >>
1176fe2caefcSParav Pandit 	    OCRDMA_MBX_QUERY_CFG_MAX_ORD_PER_QP_SHIFT;
1177fe2caefcSParav Pandit 	attr->max_ird_per_qp = (rsp->max_ird_ord_per_qp &
1178fe2caefcSParav Pandit 				OCRDMA_MBX_QUERY_CFG_MAX_IRD_PER_QP_MASK) >>
1179fe2caefcSParav Pandit 	    OCRDMA_MBX_QUERY_CFG_MAX_IRD_PER_QP_SHIFT;
1180fe2caefcSParav Pandit 	attr->cq_overflow_detect = (rsp->qp_srq_cq_ird_ord &
1181fe2caefcSParav Pandit 				    OCRDMA_MBX_QUERY_CFG_CQ_OVERFLOW_MASK) >>
1182fe2caefcSParav Pandit 	    OCRDMA_MBX_QUERY_CFG_CQ_OVERFLOW_SHIFT;
1183fe2caefcSParav Pandit 	attr->srq_supported = (rsp->qp_srq_cq_ird_ord &
1184fe2caefcSParav Pandit 			       OCRDMA_MBX_QUERY_CFG_SRQ_SUPPORTED_MASK) >>
1185fe2caefcSParav Pandit 	    OCRDMA_MBX_QUERY_CFG_SRQ_SUPPORTED_SHIFT;
1186fe2caefcSParav Pandit 	attr->local_ca_ack_delay = (rsp->max_pd_ca_ack_delay &
1187fe2caefcSParav Pandit 				    OCRDMA_MBX_QUERY_CFG_CA_ACK_DELAY_MASK) >>
1188fe2caefcSParav Pandit 	    OCRDMA_MBX_QUERY_CFG_CA_ACK_DELAY_SHIFT;
1189ac578aefSSelvin Xavier 	attr->max_mw = rsp->max_mw;
1190fe2caefcSParav Pandit 	attr->max_mr = rsp->max_mr;
1191033edd4dSMitesh Ahuja 	attr->max_mr_size = ((u64)rsp->max_mr_size_hi << 32) |
1192033edd4dSMitesh Ahuja 			      rsp->max_mr_size_lo;
1193fe2caefcSParav Pandit 	attr->max_pages_per_frmr = rsp->max_pages_per_frmr;
1194fe2caefcSParav Pandit 	attr->max_num_mr_pbl = rsp->max_num_mr_pbl;
1195fe2caefcSParav Pandit 	attr->max_cqe = rsp->max_cq_cqes_per_cq &
1196fe2caefcSParav Pandit 			OCRDMA_MBX_QUERY_CFG_MAX_CQES_PER_CQ_MASK;
1197c43e9ab8SNaresh Gottumukkala 	attr->max_cq = (rsp->max_cq_cqes_per_cq &
1198c43e9ab8SNaresh Gottumukkala 			OCRDMA_MBX_QUERY_CFG_MAX_CQ_MASK) >>
1199c43e9ab8SNaresh Gottumukkala 			OCRDMA_MBX_QUERY_CFG_MAX_CQ_OFFSET;
1200fe2caefcSParav Pandit 	attr->wqe_size = ((rsp->wqe_rqe_stride_max_dpp_cqs &
1201fe2caefcSParav Pandit 		OCRDMA_MBX_QUERY_CFG_MAX_WQE_SIZE_MASK) >>
1202fe2caefcSParav Pandit 		OCRDMA_MBX_QUERY_CFG_MAX_WQE_SIZE_OFFSET) *
1203fe2caefcSParav Pandit 		OCRDMA_WQE_STRIDE;
1204fe2caefcSParav Pandit 	attr->rqe_size = ((rsp->wqe_rqe_stride_max_dpp_cqs &
1205fe2caefcSParav Pandit 		OCRDMA_MBX_QUERY_CFG_MAX_RQE_SIZE_MASK) >>
1206fe2caefcSParav Pandit 		OCRDMA_MBX_QUERY_CFG_MAX_RQE_SIZE_OFFSET) *
1207fe2caefcSParav Pandit 		OCRDMA_WQE_STRIDE;
1208fe2caefcSParav Pandit 	attr->max_inline_data =
1209fe2caefcSParav Pandit 	    attr->wqe_size - (sizeof(struct ocrdma_hdr_wqe) +
1210fe2caefcSParav Pandit 			      sizeof(struct ocrdma_sge));
121121c3391aSDevesh Sharma 	if (ocrdma_get_asic_type(dev) == OCRDMA_ASIC_GEN_SKH_R) {
1212fe2caefcSParav Pandit 		attr->ird = 1;
1213fe2caefcSParav Pandit 		attr->ird_page_size = OCRDMA_MIN_Q_PAGE_SIZE;
1214fe2caefcSParav Pandit 		attr->num_ird_pages = MAX_OCRDMA_IRD_PAGES;
121507bb5424SMahesh Vardhamanaiah 	}
121607bb5424SMahesh Vardhamanaiah 	dev->attr.max_wqe = rsp->max_wqes_rqes_per_q >>
121707bb5424SMahesh Vardhamanaiah 		 OCRDMA_MBX_QUERY_CFG_MAX_WQES_PER_WQ_OFFSET;
121807bb5424SMahesh Vardhamanaiah 	dev->attr.max_rqe = rsp->max_wqes_rqes_per_q &
121907bb5424SMahesh Vardhamanaiah 		OCRDMA_MBX_QUERY_CFG_MAX_RQES_PER_RQ_MASK;
1220fe2caefcSParav Pandit }
1221fe2caefcSParav Pandit 
ocrdma_check_fw_config(struct ocrdma_dev * dev,struct ocrdma_fw_conf_rsp * conf)1222fe2caefcSParav Pandit static int ocrdma_check_fw_config(struct ocrdma_dev *dev,
1223fe2caefcSParav Pandit 				   struct ocrdma_fw_conf_rsp *conf)
1224fe2caefcSParav Pandit {
1225fe2caefcSParav Pandit 	u32 fn_mode;
1226fe2caefcSParav Pandit 
1227fe2caefcSParav Pandit 	fn_mode = conf->fn_mode & OCRDMA_FN_MODE_RDMA;
1228fe2caefcSParav Pandit 	if (fn_mode != OCRDMA_FN_MODE_RDMA)
1229fe2caefcSParav Pandit 		return -EINVAL;
1230fe2caefcSParav Pandit 	dev->base_eqid = conf->base_eqid;
1231fe2caefcSParav Pandit 	dev->max_eq = conf->max_eq;
1232fe2caefcSParav Pandit 	return 0;
1233fe2caefcSParav Pandit }
1234fe2caefcSParav Pandit 
1235fe2caefcSParav Pandit /* can be issued only during init time. */
ocrdma_mbx_query_fw_ver(struct ocrdma_dev * dev)1236fe2caefcSParav Pandit static int ocrdma_mbx_query_fw_ver(struct ocrdma_dev *dev)
1237fe2caefcSParav Pandit {
1238fe2caefcSParav Pandit 	int status = -ENOMEM;
1239fe2caefcSParav Pandit 	struct ocrdma_mqe *cmd;
1240fe2caefcSParav Pandit 	struct ocrdma_fw_ver_rsp *rsp;
1241fe2caefcSParav Pandit 
1242fe2caefcSParav Pandit 	cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_GET_FW_VER, sizeof(*cmd));
1243fe2caefcSParav Pandit 	if (!cmd)
1244fe2caefcSParav Pandit 		return -ENOMEM;
1245fe2caefcSParav Pandit 	ocrdma_init_mch((struct ocrdma_mbx_hdr *)&cmd->u.cmd[0],
1246fe2caefcSParav Pandit 			OCRDMA_CMD_GET_FW_VER,
1247fe2caefcSParav Pandit 			OCRDMA_SUBSYS_COMMON, sizeof(*cmd));
1248fe2caefcSParav Pandit 
1249fe2caefcSParav Pandit 	status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1250fe2caefcSParav Pandit 	if (status)
1251fe2caefcSParav Pandit 		goto mbx_err;
1252fe2caefcSParav Pandit 	rsp = (struct ocrdma_fw_ver_rsp *)cmd;
1253fe2caefcSParav Pandit 	memset(&dev->attr.fw_ver[0], 0, sizeof(dev->attr.fw_ver));
1254fe2caefcSParav Pandit 	memcpy(&dev->attr.fw_ver[0], &rsp->running_ver[0],
1255fe2caefcSParav Pandit 	       sizeof(rsp->running_ver));
1256fe2caefcSParav Pandit 	ocrdma_le32_to_cpu(dev->attr.fw_ver, sizeof(rsp->running_ver));
1257fe2caefcSParav Pandit mbx_err:
1258fe2caefcSParav Pandit 	kfree(cmd);
1259fe2caefcSParav Pandit 	return status;
1260fe2caefcSParav Pandit }
1261fe2caefcSParav Pandit 
1262fe2caefcSParav Pandit /* can be issued only during init time. */
ocrdma_mbx_query_fw_config(struct ocrdma_dev * dev)1263fe2caefcSParav Pandit static int ocrdma_mbx_query_fw_config(struct ocrdma_dev *dev)
1264fe2caefcSParav Pandit {
1265fe2caefcSParav Pandit 	int status = -ENOMEM;
1266fe2caefcSParav Pandit 	struct ocrdma_mqe *cmd;
1267fe2caefcSParav Pandit 	struct ocrdma_fw_conf_rsp *rsp;
1268fe2caefcSParav Pandit 
1269fe2caefcSParav Pandit 	cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_GET_FW_CONFIG, sizeof(*cmd));
1270fe2caefcSParav Pandit 	if (!cmd)
1271fe2caefcSParav Pandit 		return -ENOMEM;
1272fe2caefcSParav Pandit 	ocrdma_init_mch((struct ocrdma_mbx_hdr *)&cmd->u.cmd[0],
1273fe2caefcSParav Pandit 			OCRDMA_CMD_GET_FW_CONFIG,
1274fe2caefcSParav Pandit 			OCRDMA_SUBSYS_COMMON, sizeof(*cmd));
1275fe2caefcSParav Pandit 	status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1276fe2caefcSParav Pandit 	if (status)
1277fe2caefcSParav Pandit 		goto mbx_err;
1278fe2caefcSParav Pandit 	rsp = (struct ocrdma_fw_conf_rsp *)cmd;
1279fe2caefcSParav Pandit 	status = ocrdma_check_fw_config(dev, rsp);
1280fe2caefcSParav Pandit mbx_err:
1281fe2caefcSParav Pandit 	kfree(cmd);
1282fe2caefcSParav Pandit 	return status;
1283fe2caefcSParav Pandit }
1284fe2caefcSParav Pandit 
ocrdma_mbx_rdma_stats(struct ocrdma_dev * dev,bool reset)1285a51f06e1SSelvin Xavier int ocrdma_mbx_rdma_stats(struct ocrdma_dev *dev, bool reset)
1286a51f06e1SSelvin Xavier {
1287a51f06e1SSelvin Xavier 	struct ocrdma_rdma_stats_req *req = dev->stats_mem.va;
1288a51f06e1SSelvin Xavier 	struct ocrdma_mqe *mqe = &dev->stats_mem.mqe;
1289beb9b703SJes Sorensen 	struct ocrdma_rdma_stats_resp *old_stats;
1290a51f06e1SSelvin Xavier 	int status;
1291a51f06e1SSelvin Xavier 
1292beb9b703SJes Sorensen 	old_stats = kmalloc(sizeof(*old_stats), GFP_KERNEL);
1293a51f06e1SSelvin Xavier 	if (old_stats == NULL)
1294a51f06e1SSelvin Xavier 		return -ENOMEM;
1295a51f06e1SSelvin Xavier 
1296a51f06e1SSelvin Xavier 	memset(mqe, 0, sizeof(*mqe));
1297a51f06e1SSelvin Xavier 	mqe->hdr.pyld_len = dev->stats_mem.size;
1298a51f06e1SSelvin Xavier 	mqe->hdr.spcl_sge_cnt_emb |=
1299a51f06e1SSelvin Xavier 			(1 << OCRDMA_MQE_HDR_SGE_CNT_SHIFT) &
1300a51f06e1SSelvin Xavier 				OCRDMA_MQE_HDR_SGE_CNT_MASK;
1301a51f06e1SSelvin Xavier 	mqe->u.nonemb_req.sge[0].pa_lo = (u32) (dev->stats_mem.pa & 0xffffffff);
1302a51f06e1SSelvin Xavier 	mqe->u.nonemb_req.sge[0].pa_hi = (u32) upper_32_bits(dev->stats_mem.pa);
1303a51f06e1SSelvin Xavier 	mqe->u.nonemb_req.sge[0].len = dev->stats_mem.size;
1304a51f06e1SSelvin Xavier 
1305a51f06e1SSelvin Xavier 	/* Cache the old stats */
1306a51f06e1SSelvin Xavier 	memcpy(old_stats, req, sizeof(struct ocrdma_rdma_stats_resp));
1307a51f06e1SSelvin Xavier 	memset(req, 0, dev->stats_mem.size);
1308a51f06e1SSelvin Xavier 
1309a51f06e1SSelvin Xavier 	ocrdma_init_mch((struct ocrdma_mbx_hdr *)req,
1310a51f06e1SSelvin Xavier 			OCRDMA_CMD_GET_RDMA_STATS,
1311a51f06e1SSelvin Xavier 			OCRDMA_SUBSYS_ROCE,
1312a51f06e1SSelvin Xavier 			dev->stats_mem.size);
1313a51f06e1SSelvin Xavier 	if (reset)
1314a51f06e1SSelvin Xavier 		req->reset_stats = reset;
1315a51f06e1SSelvin Xavier 
1316a51f06e1SSelvin Xavier 	status = ocrdma_nonemb_mbx_cmd(dev, mqe, dev->stats_mem.va);
1317a51f06e1SSelvin Xavier 	if (status)
1318a51f06e1SSelvin Xavier 		/* Copy from cache, if mbox fails */
1319a51f06e1SSelvin Xavier 		memcpy(req, old_stats, sizeof(struct ocrdma_rdma_stats_resp));
1320a51f06e1SSelvin Xavier 	else
1321a51f06e1SSelvin Xavier 		ocrdma_le32_to_cpu(req, dev->stats_mem.size);
1322a51f06e1SSelvin Xavier 
1323a51f06e1SSelvin Xavier 	kfree(old_stats);
1324a51f06e1SSelvin Xavier 	return status;
1325a51f06e1SSelvin Xavier }
1326a51f06e1SSelvin Xavier 
ocrdma_mbx_get_ctrl_attribs(struct ocrdma_dev * dev)1327a51f06e1SSelvin Xavier static int ocrdma_mbx_get_ctrl_attribs(struct ocrdma_dev *dev)
1328a51f06e1SSelvin Xavier {
1329a51f06e1SSelvin Xavier 	int status = -ENOMEM;
1330a51f06e1SSelvin Xavier 	struct ocrdma_dma_mem dma;
1331a51f06e1SSelvin Xavier 	struct ocrdma_mqe *mqe;
1332a51f06e1SSelvin Xavier 	struct ocrdma_get_ctrl_attribs_rsp *ctrl_attr_rsp;
1333a51f06e1SSelvin Xavier 	struct mgmt_hba_attribs *hba_attribs;
1334a51f06e1SSelvin Xavier 
1335beb9b703SJes Sorensen 	mqe = kzalloc(sizeof(struct ocrdma_mqe), GFP_KERNEL);
1336a51f06e1SSelvin Xavier 	if (!mqe)
1337a51f06e1SSelvin Xavier 		return status;
1338a51f06e1SSelvin Xavier 
1339a51f06e1SSelvin Xavier 	dma.size = sizeof(struct ocrdma_get_ctrl_attribs_rsp);
1340a51f06e1SSelvin Xavier 	dma.va	 = dma_alloc_coherent(&dev->nic_info.pdev->dev,
1341a51f06e1SSelvin Xavier 					dma.size, &dma.pa, GFP_KERNEL);
1342a51f06e1SSelvin Xavier 	if (!dma.va)
1343a51f06e1SSelvin Xavier 		goto free_mqe;
1344a51f06e1SSelvin Xavier 
1345a51f06e1SSelvin Xavier 	mqe->hdr.pyld_len = dma.size;
1346a51f06e1SSelvin Xavier 	mqe->hdr.spcl_sge_cnt_emb |=
1347a51f06e1SSelvin Xavier 			(1 << OCRDMA_MQE_HDR_SGE_CNT_SHIFT) &
1348a51f06e1SSelvin Xavier 			OCRDMA_MQE_HDR_SGE_CNT_MASK;
1349a51f06e1SSelvin Xavier 	mqe->u.nonemb_req.sge[0].pa_lo = (u32) (dma.pa & 0xffffffff);
1350a51f06e1SSelvin Xavier 	mqe->u.nonemb_req.sge[0].pa_hi = (u32) upper_32_bits(dma.pa);
1351a51f06e1SSelvin Xavier 	mqe->u.nonemb_req.sge[0].len = dma.size;
1352a51f06e1SSelvin Xavier 
1353a51f06e1SSelvin Xavier 	ocrdma_init_mch((struct ocrdma_mbx_hdr *)dma.va,
1354a51f06e1SSelvin Xavier 			OCRDMA_CMD_GET_CTRL_ATTRIBUTES,
1355a51f06e1SSelvin Xavier 			OCRDMA_SUBSYS_COMMON,
1356a51f06e1SSelvin Xavier 			dma.size);
1357a51f06e1SSelvin Xavier 
1358a51f06e1SSelvin Xavier 	status = ocrdma_nonemb_mbx_cmd(dev, mqe, dma.va);
1359a51f06e1SSelvin Xavier 	if (!status) {
1360a51f06e1SSelvin Xavier 		ctrl_attr_rsp = (struct ocrdma_get_ctrl_attribs_rsp *)dma.va;
1361a51f06e1SSelvin Xavier 		hba_attribs = &ctrl_attr_rsp->ctrl_attribs.hba_attribs;
1362a51f06e1SSelvin Xavier 
13638ac0c7c7SDevesh Sharma 		dev->hba_port_num = (hba_attribs->ptpnum_maxdoms_hbast_cv &
13648ac0c7c7SDevesh Sharma 					OCRDMA_HBA_ATTRB_PTNUM_MASK)
13658ac0c7c7SDevesh Sharma 					>> OCRDMA_HBA_ATTRB_PTNUM_SHIFT;
1366*2c34bb6dSWolfram Sang 		strscpy(dev->model_number,
13679491a1edSBart Van Assche 			hba_attribs->controller_model_number,
13689491a1edSBart Van Assche 			sizeof(dev->model_number));
1369a51f06e1SSelvin Xavier 	}
1370a51f06e1SSelvin Xavier 	dma_free_coherent(&dev->nic_info.pdev->dev, dma.size, dma.va, dma.pa);
1371a51f06e1SSelvin Xavier free_mqe:
1372a51f06e1SSelvin Xavier 	kfree(mqe);
1373a51f06e1SSelvin Xavier 	return status;
1374a51f06e1SSelvin Xavier }
1375a51f06e1SSelvin Xavier 
ocrdma_mbx_query_dev(struct ocrdma_dev * dev)1376fe2caefcSParav Pandit static int ocrdma_mbx_query_dev(struct ocrdma_dev *dev)
1377fe2caefcSParav Pandit {
1378fe2caefcSParav Pandit 	int status = -ENOMEM;
1379fe2caefcSParav Pandit 	struct ocrdma_mbx_query_config *rsp;
1380fe2caefcSParav Pandit 	struct ocrdma_mqe *cmd;
1381fe2caefcSParav Pandit 
1382fe2caefcSParav Pandit 	cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_QUERY_CONFIG, sizeof(*cmd));
1383fe2caefcSParav Pandit 	if (!cmd)
1384fe2caefcSParav Pandit 		return status;
1385fe2caefcSParav Pandit 	status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1386fe2caefcSParav Pandit 	if (status)
1387fe2caefcSParav Pandit 		goto mbx_err;
1388fe2caefcSParav Pandit 	rsp = (struct ocrdma_mbx_query_config *)cmd;
1389fe2caefcSParav Pandit 	ocrdma_get_attr(dev, &dev->attr, rsp);
1390fe2caefcSParav Pandit mbx_err:
1391fe2caefcSParav Pandit 	kfree(cmd);
1392fe2caefcSParav Pandit 	return status;
1393fe2caefcSParav Pandit }
1394fe2caefcSParav Pandit 
ocrdma_mbx_get_link_speed(struct ocrdma_dev * dev,u8 * lnk_speed,u8 * lnk_state)139510a214dcSDevesh Sharma int ocrdma_mbx_get_link_speed(struct ocrdma_dev *dev, u8 *lnk_speed,
139610a214dcSDevesh Sharma 			      u8 *lnk_state)
1397f24ceba6SNaresh Gottumukkala {
1398f24ceba6SNaresh Gottumukkala 	int status = -ENOMEM;
1399f24ceba6SNaresh Gottumukkala 	struct ocrdma_get_link_speed_rsp *rsp;
1400f24ceba6SNaresh Gottumukkala 	struct ocrdma_mqe *cmd;
1401f24ceba6SNaresh Gottumukkala 
1402f24ceba6SNaresh Gottumukkala 	cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_QUERY_NTWK_LINK_CONFIG_V1,
1403f24ceba6SNaresh Gottumukkala 				  sizeof(*cmd));
1404f24ceba6SNaresh Gottumukkala 	if (!cmd)
1405f24ceba6SNaresh Gottumukkala 		return status;
1406f24ceba6SNaresh Gottumukkala 	ocrdma_init_mch((struct ocrdma_mbx_hdr *)&cmd->u.cmd[0],
1407f24ceba6SNaresh Gottumukkala 			OCRDMA_CMD_QUERY_NTWK_LINK_CONFIG_V1,
1408f24ceba6SNaresh Gottumukkala 			OCRDMA_SUBSYS_COMMON, sizeof(*cmd));
1409f24ceba6SNaresh Gottumukkala 
1410f24ceba6SNaresh Gottumukkala 	((struct ocrdma_mbx_hdr *)cmd->u.cmd)->rsvd_version = 0x1;
1411f24ceba6SNaresh Gottumukkala 
1412f24ceba6SNaresh Gottumukkala 	status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1413f24ceba6SNaresh Gottumukkala 	if (status)
1414f24ceba6SNaresh Gottumukkala 		goto mbx_err;
1415f24ceba6SNaresh Gottumukkala 
1416f24ceba6SNaresh Gottumukkala 	rsp = (struct ocrdma_get_link_speed_rsp *)cmd;
141710a214dcSDevesh Sharma 	if (lnk_speed)
14188ac0c7c7SDevesh Sharma 		*lnk_speed = (rsp->pflt_pps_ld_pnum & OCRDMA_PHY_PS_MASK)
14198ac0c7c7SDevesh Sharma 			      >> OCRDMA_PHY_PS_SHIFT;
142010a214dcSDevesh Sharma 	if (lnk_state)
142110a214dcSDevesh Sharma 		*lnk_state = (rsp->res_lnk_st & OCRDMA_LINK_ST_MASK);
1422f24ceba6SNaresh Gottumukkala 
1423f24ceba6SNaresh Gottumukkala mbx_err:
1424f24ceba6SNaresh Gottumukkala 	kfree(cmd);
1425f24ceba6SNaresh Gottumukkala 	return status;
1426f24ceba6SNaresh Gottumukkala }
1427f24ceba6SNaresh Gottumukkala 
ocrdma_mbx_get_phy_info(struct ocrdma_dev * dev)1428a51f06e1SSelvin Xavier static int ocrdma_mbx_get_phy_info(struct ocrdma_dev *dev)
1429a51f06e1SSelvin Xavier {
1430a51f06e1SSelvin Xavier 	int status = -ENOMEM;
1431a51f06e1SSelvin Xavier 	struct ocrdma_mqe *cmd;
1432a51f06e1SSelvin Xavier 	struct ocrdma_get_phy_info_rsp *rsp;
1433a51f06e1SSelvin Xavier 
1434a51f06e1SSelvin Xavier 	cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_PHY_DETAILS, sizeof(*cmd));
1435a51f06e1SSelvin Xavier 	if (!cmd)
1436a51f06e1SSelvin Xavier 		return status;
1437a51f06e1SSelvin Xavier 
1438a51f06e1SSelvin Xavier 	ocrdma_init_mch((struct ocrdma_mbx_hdr *)&cmd->u.cmd[0],
1439a51f06e1SSelvin Xavier 			OCRDMA_CMD_PHY_DETAILS, OCRDMA_SUBSYS_COMMON,
1440a51f06e1SSelvin Xavier 			sizeof(*cmd));
1441a51f06e1SSelvin Xavier 
1442a51f06e1SSelvin Xavier 	status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1443a51f06e1SSelvin Xavier 	if (status)
1444a51f06e1SSelvin Xavier 		goto mbx_err;
1445a51f06e1SSelvin Xavier 
1446a51f06e1SSelvin Xavier 	rsp = (struct ocrdma_get_phy_info_rsp *)cmd;
14478ac0c7c7SDevesh Sharma 	dev->phy.phy_type =
14488ac0c7c7SDevesh Sharma 			(rsp->ityp_ptyp & OCRDMA_PHY_TYPE_MASK);
14498ac0c7c7SDevesh Sharma 	dev->phy.interface_type =
14508ac0c7c7SDevesh Sharma 			(rsp->ityp_ptyp & OCRDMA_IF_TYPE_MASK)
14518ac0c7c7SDevesh Sharma 				>> OCRDMA_IF_TYPE_SHIFT;
1452a51f06e1SSelvin Xavier 	dev->phy.auto_speeds_supported  =
14538ac0c7c7SDevesh Sharma 			(rsp->fspeed_aspeed & OCRDMA_ASPEED_SUPP_MASK);
1454a51f06e1SSelvin Xavier 	dev->phy.fixed_speeds_supported =
14558ac0c7c7SDevesh Sharma 			(rsp->fspeed_aspeed & OCRDMA_FSPEED_SUPP_MASK)
14568ac0c7c7SDevesh Sharma 				>> OCRDMA_FSPEED_SUPP_SHIFT;
1457a51f06e1SSelvin Xavier mbx_err:
1458a51f06e1SSelvin Xavier 	kfree(cmd);
1459a51f06e1SSelvin Xavier 	return status;
1460a51f06e1SSelvin Xavier }
1461a51f06e1SSelvin Xavier 
ocrdma_mbx_alloc_pd(struct ocrdma_dev * dev,struct ocrdma_pd * pd)1462fe2caefcSParav Pandit int ocrdma_mbx_alloc_pd(struct ocrdma_dev *dev, struct ocrdma_pd *pd)
1463fe2caefcSParav Pandit {
1464fe2caefcSParav Pandit 	int status = -ENOMEM;
1465fe2caefcSParav Pandit 	struct ocrdma_alloc_pd *cmd;
1466fe2caefcSParav Pandit 	struct ocrdma_alloc_pd_rsp *rsp;
1467fe2caefcSParav Pandit 
1468fe2caefcSParav Pandit 	cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_ALLOC_PD, sizeof(*cmd));
1469fe2caefcSParav Pandit 	if (!cmd)
1470fe2caefcSParav Pandit 		return status;
1471fe2caefcSParav Pandit 	if (pd->dpp_enabled)
1472fe2caefcSParav Pandit 		cmd->enable_dpp_rsvd |= OCRDMA_ALLOC_PD_ENABLE_DPP;
1473fe2caefcSParav Pandit 	status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1474fe2caefcSParav Pandit 	if (status)
1475fe2caefcSParav Pandit 		goto mbx_err;
1476fe2caefcSParav Pandit 	rsp = (struct ocrdma_alloc_pd_rsp *)cmd;
1477fe2caefcSParav Pandit 	pd->id = rsp->dpp_page_pdid & OCRDMA_ALLOC_PD_RSP_PDID_MASK;
1478fe2caefcSParav Pandit 	if (rsp->dpp_page_pdid & OCRDMA_ALLOC_PD_RSP_DPP) {
1479fe2caefcSParav Pandit 		pd->dpp_enabled = true;
1480fe2caefcSParav Pandit 		pd->dpp_page = rsp->dpp_page_pdid >>
1481fe2caefcSParav Pandit 				OCRDMA_ALLOC_PD_RSP_DPP_PAGE_SHIFT;
1482fe2caefcSParav Pandit 	} else {
1483fe2caefcSParav Pandit 		pd->dpp_enabled = false;
1484fe2caefcSParav Pandit 		pd->num_dpp_qp = 0;
1485fe2caefcSParav Pandit 	}
1486fe2caefcSParav Pandit mbx_err:
1487fe2caefcSParav Pandit 	kfree(cmd);
1488fe2caefcSParav Pandit 	return status;
1489fe2caefcSParav Pandit }
1490fe2caefcSParav Pandit 
ocrdma_mbx_dealloc_pd(struct ocrdma_dev * dev,struct ocrdma_pd * pd)1491fe2caefcSParav Pandit int ocrdma_mbx_dealloc_pd(struct ocrdma_dev *dev, struct ocrdma_pd *pd)
1492fe2caefcSParav Pandit {
1493fe2caefcSParav Pandit 	int status = -ENOMEM;
1494fe2caefcSParav Pandit 	struct ocrdma_dealloc_pd *cmd;
1495fe2caefcSParav Pandit 
1496fe2caefcSParav Pandit 	cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_DEALLOC_PD, sizeof(*cmd));
1497fe2caefcSParav Pandit 	if (!cmd)
1498fe2caefcSParav Pandit 		return status;
1499fe2caefcSParav Pandit 	cmd->id = pd->id;
1500fe2caefcSParav Pandit 	status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1501fe2caefcSParav Pandit 	kfree(cmd);
1502fe2caefcSParav Pandit 	return status;
1503fe2caefcSParav Pandit }
1504fe2caefcSParav Pandit 
15059ba1377dSMitesh Ahuja 
ocrdma_mbx_alloc_pd_range(struct ocrdma_dev * dev)15069ba1377dSMitesh Ahuja static int ocrdma_mbx_alloc_pd_range(struct ocrdma_dev *dev)
15079ba1377dSMitesh Ahuja {
15089ba1377dSMitesh Ahuja 	int status = -ENOMEM;
15099ba1377dSMitesh Ahuja 	struct ocrdma_alloc_pd_range *cmd;
15109ba1377dSMitesh Ahuja 	struct ocrdma_alloc_pd_range_rsp *rsp;
15119ba1377dSMitesh Ahuja 
15129ba1377dSMitesh Ahuja 	/* Pre allocate the DPP PDs */
151359582d86SMitesh Ahuja 	if (dev->attr.max_dpp_pds) {
151459582d86SMitesh Ahuja 		cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_ALLOC_PD_RANGE,
151559582d86SMitesh Ahuja 					  sizeof(*cmd));
15169ba1377dSMitesh Ahuja 		if (!cmd)
15179ba1377dSMitesh Ahuja 			return -ENOMEM;
15189ba1377dSMitesh Ahuja 		cmd->pd_count = dev->attr.max_dpp_pds;
15199ba1377dSMitesh Ahuja 		cmd->enable_dpp_rsvd |= OCRDMA_ALLOC_PD_ENABLE_DPP;
15209ba1377dSMitesh Ahuja 		status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
15219ba1377dSMitesh Ahuja 		rsp = (struct ocrdma_alloc_pd_range_rsp *)cmd;
15229ba1377dSMitesh Ahuja 
152359582d86SMitesh Ahuja 		if (!status && (rsp->dpp_page_pdid & OCRDMA_ALLOC_PD_RSP_DPP) &&
152459582d86SMitesh Ahuja 		    rsp->pd_count) {
15259ba1377dSMitesh Ahuja 			dev->pd_mgr->dpp_page_index = rsp->dpp_page_pdid >>
15269ba1377dSMitesh Ahuja 					OCRDMA_ALLOC_PD_RSP_DPP_PAGE_SHIFT;
15279ba1377dSMitesh Ahuja 			dev->pd_mgr->pd_dpp_start = rsp->dpp_page_pdid &
15289ba1377dSMitesh Ahuja 					OCRDMA_ALLOC_PD_RNG_RSP_START_PDID_MASK;
15299ba1377dSMitesh Ahuja 			dev->pd_mgr->max_dpp_pd = rsp->pd_count;
153027c2f502SChristophe JAILLET 			dev->pd_mgr->pd_dpp_bitmap = bitmap_zalloc(rsp->pd_count,
15319ba1377dSMitesh Ahuja 								   GFP_KERNEL);
15329ba1377dSMitesh Ahuja 		}
15339ba1377dSMitesh Ahuja 		kfree(cmd);
153459582d86SMitesh Ahuja 	}
15359ba1377dSMitesh Ahuja 
15369ba1377dSMitesh Ahuja 	cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_ALLOC_PD_RANGE, sizeof(*cmd));
15379ba1377dSMitesh Ahuja 	if (!cmd)
15389ba1377dSMitesh Ahuja 		return -ENOMEM;
15399ba1377dSMitesh Ahuja 
15409ba1377dSMitesh Ahuja 	cmd->pd_count = dev->attr.max_pd - dev->attr.max_dpp_pds;
15419ba1377dSMitesh Ahuja 	status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
15429ba1377dSMitesh Ahuja 	rsp = (struct ocrdma_alloc_pd_range_rsp *)cmd;
154359582d86SMitesh Ahuja 	if (!status && rsp->pd_count) {
15449ba1377dSMitesh Ahuja 		dev->pd_mgr->pd_norm_start = rsp->dpp_page_pdid &
15459ba1377dSMitesh Ahuja 					OCRDMA_ALLOC_PD_RNG_RSP_START_PDID_MASK;
15469ba1377dSMitesh Ahuja 		dev->pd_mgr->max_normal_pd = rsp->pd_count;
154727c2f502SChristophe JAILLET 		dev->pd_mgr->pd_norm_bitmap = bitmap_zalloc(rsp->pd_count,
15489ba1377dSMitesh Ahuja 							    GFP_KERNEL);
15499ba1377dSMitesh Ahuja 	}
155059582d86SMitesh Ahuja 	kfree(cmd);
15519ba1377dSMitesh Ahuja 
15529ba1377dSMitesh Ahuja 	if (dev->pd_mgr->pd_norm_bitmap || dev->pd_mgr->pd_dpp_bitmap) {
15539ba1377dSMitesh Ahuja 		/* Enable PD resource manager */
15549ba1377dSMitesh Ahuja 		dev->pd_mgr->pd_prealloc_valid = true;
155559582d86SMitesh Ahuja 		return 0;
15569ba1377dSMitesh Ahuja 	}
15579ba1377dSMitesh Ahuja 	return status;
15589ba1377dSMitesh Ahuja }
15599ba1377dSMitesh Ahuja 
ocrdma_mbx_dealloc_pd_range(struct ocrdma_dev * dev)15609ba1377dSMitesh Ahuja static void ocrdma_mbx_dealloc_pd_range(struct ocrdma_dev *dev)
15619ba1377dSMitesh Ahuja {
15629ba1377dSMitesh Ahuja 	struct ocrdma_dealloc_pd_range *cmd;
15639ba1377dSMitesh Ahuja 
15649ba1377dSMitesh Ahuja 	/* return normal PDs to firmware */
15659ba1377dSMitesh Ahuja 	cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_DEALLOC_PD_RANGE, sizeof(*cmd));
15669ba1377dSMitesh Ahuja 	if (!cmd)
15679ba1377dSMitesh Ahuja 		goto mbx_err;
15689ba1377dSMitesh Ahuja 
15699ba1377dSMitesh Ahuja 	if (dev->pd_mgr->max_normal_pd) {
15709ba1377dSMitesh Ahuja 		cmd->start_pd_id = dev->pd_mgr->pd_norm_start;
15719ba1377dSMitesh Ahuja 		cmd->pd_count = dev->pd_mgr->max_normal_pd;
15729ba1377dSMitesh Ahuja 		ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
15739ba1377dSMitesh Ahuja 	}
15749ba1377dSMitesh Ahuja 
15759ba1377dSMitesh Ahuja 	if (dev->pd_mgr->max_dpp_pd) {
15769ba1377dSMitesh Ahuja 		kfree(cmd);
15779ba1377dSMitesh Ahuja 		/* return DPP PDs to firmware */
15789ba1377dSMitesh Ahuja 		cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_DEALLOC_PD_RANGE,
15799ba1377dSMitesh Ahuja 					  sizeof(*cmd));
15809ba1377dSMitesh Ahuja 		if (!cmd)
15819ba1377dSMitesh Ahuja 			goto mbx_err;
15829ba1377dSMitesh Ahuja 
15839ba1377dSMitesh Ahuja 		cmd->start_pd_id = dev->pd_mgr->pd_dpp_start;
15849ba1377dSMitesh Ahuja 		cmd->pd_count = dev->pd_mgr->max_dpp_pd;
15859ba1377dSMitesh Ahuja 		ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
15869ba1377dSMitesh Ahuja 	}
15879ba1377dSMitesh Ahuja mbx_err:
15889ba1377dSMitesh Ahuja 	kfree(cmd);
15899ba1377dSMitesh Ahuja }
15909ba1377dSMitesh Ahuja 
ocrdma_alloc_pd_pool(struct ocrdma_dev * dev)15919ba1377dSMitesh Ahuja void ocrdma_alloc_pd_pool(struct ocrdma_dev *dev)
15929ba1377dSMitesh Ahuja {
15939ba1377dSMitesh Ahuja 	int status;
15949ba1377dSMitesh Ahuja 
15959ba1377dSMitesh Ahuja 	dev->pd_mgr = kzalloc(sizeof(struct ocrdma_pd_resource_mgr),
15969ba1377dSMitesh Ahuja 			      GFP_KERNEL);
1597740c330eSLeon Romanovsky 	if (!dev->pd_mgr)
15989ba1377dSMitesh Ahuja 		return;
1599740c330eSLeon Romanovsky 
16009ba1377dSMitesh Ahuja 	status = ocrdma_mbx_alloc_pd_range(dev);
16019ba1377dSMitesh Ahuja 	if (status) {
16029ba1377dSMitesh Ahuja 		pr_err("%s(%d) Unable to initialize PD pool, using default.\n",
16039ba1377dSMitesh Ahuja 			 __func__, dev->id);
16049ba1377dSMitesh Ahuja 	}
16059ba1377dSMitesh Ahuja }
16069ba1377dSMitesh Ahuja 
ocrdma_free_pd_pool(struct ocrdma_dev * dev)16079ba1377dSMitesh Ahuja static void ocrdma_free_pd_pool(struct ocrdma_dev *dev)
16089ba1377dSMitesh Ahuja {
16099ba1377dSMitesh Ahuja 	ocrdma_mbx_dealloc_pd_range(dev);
161027c2f502SChristophe JAILLET 	bitmap_free(dev->pd_mgr->pd_norm_bitmap);
161127c2f502SChristophe JAILLET 	bitmap_free(dev->pd_mgr->pd_dpp_bitmap);
16129ba1377dSMitesh Ahuja 	kfree(dev->pd_mgr);
16139ba1377dSMitesh Ahuja }
16149ba1377dSMitesh Ahuja 
ocrdma_build_q_conf(u32 * num_entries,int entry_size,int * num_pages,int * page_size)1615fe2caefcSParav Pandit static int ocrdma_build_q_conf(u32 *num_entries, int entry_size,
1616fe2caefcSParav Pandit 			       int *num_pages, int *page_size)
1617fe2caefcSParav Pandit {
1618fe2caefcSParav Pandit 	int i;
1619fe2caefcSParav Pandit 	int mem_size;
1620fe2caefcSParav Pandit 
1621fe2caefcSParav Pandit 	*num_entries = roundup_pow_of_two(*num_entries);
1622fe2caefcSParav Pandit 	mem_size = *num_entries * entry_size;
1623fe2caefcSParav Pandit 	/* find the possible lowest possible multiplier */
1624fe2caefcSParav Pandit 	for (i = 0; i < OCRDMA_MAX_Q_PAGE_SIZE_CNT; i++) {
1625fe2caefcSParav Pandit 		if (mem_size <= (OCRDMA_Q_PAGE_BASE_SIZE << i))
1626fe2caefcSParav Pandit 			break;
1627fe2caefcSParav Pandit 	}
1628fe2caefcSParav Pandit 	if (i >= OCRDMA_MAX_Q_PAGE_SIZE_CNT)
1629fe2caefcSParav Pandit 		return -EINVAL;
1630fe2caefcSParav Pandit 	mem_size = roundup(mem_size,
1631fe2caefcSParav Pandit 		       ((OCRDMA_Q_PAGE_BASE_SIZE << i) / OCRDMA_MAX_Q_PAGES));
1632fe2caefcSParav Pandit 	*num_pages =
1633fe2caefcSParav Pandit 	    mem_size / ((OCRDMA_Q_PAGE_BASE_SIZE << i) / OCRDMA_MAX_Q_PAGES);
1634fe2caefcSParav Pandit 	*page_size = ((OCRDMA_Q_PAGE_BASE_SIZE << i) / OCRDMA_MAX_Q_PAGES);
1635fe2caefcSParav Pandit 	*num_entries = mem_size / entry_size;
1636fe2caefcSParav Pandit 	return 0;
1637fe2caefcSParav Pandit }
1638fe2caefcSParav Pandit 
ocrdma_mbx_create_ah_tbl(struct ocrdma_dev * dev)1639fe2caefcSParav Pandit static int ocrdma_mbx_create_ah_tbl(struct ocrdma_dev *dev)
1640fe2caefcSParav Pandit {
1641fe2caefcSParav Pandit 	int i;
16425b4c9cd7SPan Bian 	int status = -ENOMEM;
1643fe2caefcSParav Pandit 	int max_ah;
1644fe2caefcSParav Pandit 	struct ocrdma_create_ah_tbl *cmd;
1645fe2caefcSParav Pandit 	struct ocrdma_create_ah_tbl_rsp *rsp;
1646fe2caefcSParav Pandit 	struct pci_dev *pdev = dev->nic_info.pdev;
1647fe2caefcSParav Pandit 	dma_addr_t pa;
1648fe2caefcSParav Pandit 	struct ocrdma_pbe *pbes;
1649fe2caefcSParav Pandit 
1650fe2caefcSParav Pandit 	cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_CREATE_AH_TBL, sizeof(*cmd));
1651fe2caefcSParav Pandit 	if (!cmd)
1652fe2caefcSParav Pandit 		return status;
1653fe2caefcSParav Pandit 
1654fe2caefcSParav Pandit 	max_ah = OCRDMA_MAX_AH;
1655fe2caefcSParav Pandit 	dev->av_tbl.size = sizeof(struct ocrdma_av) * max_ah;
1656fe2caefcSParav Pandit 
1657fe2caefcSParav Pandit 	/* number of PBEs in PBL */
1658fe2caefcSParav Pandit 	cmd->ah_conf = (OCRDMA_AH_TBL_PAGES <<
1659fe2caefcSParav Pandit 				OCRDMA_CREATE_AH_NUM_PAGES_SHIFT) &
1660fe2caefcSParav Pandit 				OCRDMA_CREATE_AH_NUM_PAGES_MASK;
1661fe2caefcSParav Pandit 
1662fe2caefcSParav Pandit 	/* page size */
1663fe2caefcSParav Pandit 	for (i = 0; i < OCRDMA_MAX_Q_PAGE_SIZE_CNT; i++) {
1664fe2caefcSParav Pandit 		if (PAGE_SIZE == (OCRDMA_MIN_Q_PAGE_SIZE << i))
1665fe2caefcSParav Pandit 			break;
1666fe2caefcSParav Pandit 	}
1667fe2caefcSParav Pandit 	cmd->ah_conf |= (i << OCRDMA_CREATE_AH_PAGE_SIZE_SHIFT) &
1668fe2caefcSParav Pandit 				OCRDMA_CREATE_AH_PAGE_SIZE_MASK;
1669fe2caefcSParav Pandit 
1670fe2caefcSParav Pandit 	/* ah_entry size */
1671fe2caefcSParav Pandit 	cmd->ah_conf |= (sizeof(struct ocrdma_av) <<
1672fe2caefcSParav Pandit 				OCRDMA_CREATE_AH_ENTRY_SIZE_SHIFT) &
1673fe2caefcSParav Pandit 				OCRDMA_CREATE_AH_ENTRY_SIZE_MASK;
1674fe2caefcSParav Pandit 
1675fe2caefcSParav Pandit 	dev->av_tbl.pbl.va = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
1676fe2caefcSParav Pandit 						&dev->av_tbl.pbl.pa,
1677fe2caefcSParav Pandit 						GFP_KERNEL);
1678fe2caefcSParav Pandit 	if (dev->av_tbl.pbl.va == NULL)
1679fe2caefcSParav Pandit 		goto mem_err;
1680fe2caefcSParav Pandit 
1681fe2caefcSParav Pandit 	dev->av_tbl.va = dma_alloc_coherent(&pdev->dev, dev->av_tbl.size,
1682fe2caefcSParav Pandit 					    &pa, GFP_KERNEL);
1683fe2caefcSParav Pandit 	if (dev->av_tbl.va == NULL)
1684fe2caefcSParav Pandit 		goto mem_err_ah;
1685fe2caefcSParav Pandit 	dev->av_tbl.pa = pa;
1686fe2caefcSParav Pandit 	dev->av_tbl.num_ah = max_ah;
1687fe2caefcSParav Pandit 
1688fe2caefcSParav Pandit 	pbes = (struct ocrdma_pbe *)dev->av_tbl.pbl.va;
1689fe2caefcSParav Pandit 	for (i = 0; i < dev->av_tbl.size / OCRDMA_MIN_Q_PAGE_SIZE; i++) {
16908ac0c7c7SDevesh Sharma 		pbes[i].pa_lo = (u32)cpu_to_le32(pa & 0xffffffff);
16918ac0c7c7SDevesh Sharma 		pbes[i].pa_hi = (u32)cpu_to_le32(upper_32_bits(pa));
1692fe2caefcSParav Pandit 		pa += PAGE_SIZE;
1693fe2caefcSParav Pandit 	}
1694fe2caefcSParav Pandit 	cmd->tbl_addr[0].lo = (u32)(dev->av_tbl.pbl.pa & 0xFFFFFFFF);
1695fe2caefcSParav Pandit 	cmd->tbl_addr[0].hi = (u32)upper_32_bits(dev->av_tbl.pbl.pa);
1696fe2caefcSParav Pandit 	status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1697fe2caefcSParav Pandit 	if (status)
1698fe2caefcSParav Pandit 		goto mbx_err;
1699fe2caefcSParav Pandit 	rsp = (struct ocrdma_create_ah_tbl_rsp *)cmd;
1700fe2caefcSParav Pandit 	dev->av_tbl.ahid = rsp->ahid & 0xFFFF;
1701fe2caefcSParav Pandit 	kfree(cmd);
1702fe2caefcSParav Pandit 	return 0;
1703fe2caefcSParav Pandit 
1704fe2caefcSParav Pandit mbx_err:
1705fe2caefcSParav Pandit 	dma_free_coherent(&pdev->dev, dev->av_tbl.size, dev->av_tbl.va,
1706fe2caefcSParav Pandit 			  dev->av_tbl.pa);
1707fe2caefcSParav Pandit 	dev->av_tbl.va = NULL;
1708fe2caefcSParav Pandit mem_err_ah:
1709fe2caefcSParav Pandit 	dma_free_coherent(&pdev->dev, PAGE_SIZE, dev->av_tbl.pbl.va,
1710fe2caefcSParav Pandit 			  dev->av_tbl.pbl.pa);
1711fe2caefcSParav Pandit 	dev->av_tbl.pbl.va = NULL;
1712fe2caefcSParav Pandit 	dev->av_tbl.size = 0;
1713fe2caefcSParav Pandit mem_err:
1714fe2caefcSParav Pandit 	kfree(cmd);
1715fe2caefcSParav Pandit 	return status;
1716fe2caefcSParav Pandit }
1717fe2caefcSParav Pandit 
ocrdma_mbx_delete_ah_tbl(struct ocrdma_dev * dev)1718fe2caefcSParav Pandit static void ocrdma_mbx_delete_ah_tbl(struct ocrdma_dev *dev)
1719fe2caefcSParav Pandit {
1720fe2caefcSParav Pandit 	struct ocrdma_delete_ah_tbl *cmd;
1721fe2caefcSParav Pandit 	struct pci_dev *pdev = dev->nic_info.pdev;
1722fe2caefcSParav Pandit 
1723fe2caefcSParav Pandit 	if (dev->av_tbl.va == NULL)
1724fe2caefcSParav Pandit 		return;
1725fe2caefcSParav Pandit 
1726fe2caefcSParav Pandit 	cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_DELETE_AH_TBL, sizeof(*cmd));
1727fe2caefcSParav Pandit 	if (!cmd)
1728fe2caefcSParav Pandit 		return;
1729fe2caefcSParav Pandit 	cmd->ahid = dev->av_tbl.ahid;
1730fe2caefcSParav Pandit 
1731fe2caefcSParav Pandit 	ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1732fe2caefcSParav Pandit 	dma_free_coherent(&pdev->dev, dev->av_tbl.size, dev->av_tbl.va,
1733fe2caefcSParav Pandit 			  dev->av_tbl.pa);
1734daac9681SDevesh Sharma 	dev->av_tbl.va = NULL;
1735fe2caefcSParav Pandit 	dma_free_coherent(&pdev->dev, PAGE_SIZE, dev->av_tbl.pbl.va,
1736fe2caefcSParav Pandit 			  dev->av_tbl.pbl.pa);
1737fe2caefcSParav Pandit 	kfree(cmd);
1738fe2caefcSParav Pandit }
1739fe2caefcSParav Pandit 
1740fe2caefcSParav Pandit /* Multiple CQs uses the EQ. This routine returns least used
1741fe2caefcSParav Pandit  * EQ to associate with CQ. This will distributes the interrupt
1742fe2caefcSParav Pandit  * processing and CPU load to associated EQ, vector and so to that CPU.
1743fe2caefcSParav Pandit  */
ocrdma_bind_eq(struct ocrdma_dev * dev)1744fe2caefcSParav Pandit static u16 ocrdma_bind_eq(struct ocrdma_dev *dev)
1745fe2caefcSParav Pandit {
1746fe2caefcSParav Pandit 	int i, selected_eq = 0, cq_cnt = 0;
1747fe2caefcSParav Pandit 	u16 eq_id;
1748fe2caefcSParav Pandit 
1749fe2caefcSParav Pandit 	mutex_lock(&dev->dev_lock);
1750c88bd03fSNaresh Gottumukkala 	cq_cnt = dev->eq_tbl[0].cq_cnt;
1751c88bd03fSNaresh Gottumukkala 	eq_id = dev->eq_tbl[0].q.id;
1752fe2caefcSParav Pandit 	/* find the EQ which is has the least number of
1753fe2caefcSParav Pandit 	 * CQs associated with it.
1754fe2caefcSParav Pandit 	 */
1755fe2caefcSParav Pandit 	for (i = 0; i < dev->eq_cnt; i++) {
1756c88bd03fSNaresh Gottumukkala 		if (dev->eq_tbl[i].cq_cnt < cq_cnt) {
1757c88bd03fSNaresh Gottumukkala 			cq_cnt = dev->eq_tbl[i].cq_cnt;
1758c88bd03fSNaresh Gottumukkala 			eq_id = dev->eq_tbl[i].q.id;
1759fe2caefcSParav Pandit 			selected_eq = i;
1760fe2caefcSParav Pandit 		}
1761fe2caefcSParav Pandit 	}
1762c88bd03fSNaresh Gottumukkala 	dev->eq_tbl[selected_eq].cq_cnt += 1;
1763fe2caefcSParav Pandit 	mutex_unlock(&dev->dev_lock);
1764fe2caefcSParav Pandit 	return eq_id;
1765fe2caefcSParav Pandit }
1766fe2caefcSParav Pandit 
ocrdma_unbind_eq(struct ocrdma_dev * dev,u16 eq_id)1767fe2caefcSParav Pandit static void ocrdma_unbind_eq(struct ocrdma_dev *dev, u16 eq_id)
1768fe2caefcSParav Pandit {
1769fe2caefcSParav Pandit 	int i;
1770fe2caefcSParav Pandit 
1771fe2caefcSParav Pandit 	mutex_lock(&dev->dev_lock);
1772ea617626SDevesh Sharma 	i = ocrdma_get_eq_table_index(dev, eq_id);
1773ea617626SDevesh Sharma 	if (i == -EINVAL)
1774ea617626SDevesh Sharma 		BUG();
1775c88bd03fSNaresh Gottumukkala 	dev->eq_tbl[i].cq_cnt -= 1;
1776fe2caefcSParav Pandit 	mutex_unlock(&dev->dev_lock);
1777fe2caefcSParav Pandit }
1778fe2caefcSParav Pandit 
ocrdma_mbx_create_cq(struct ocrdma_dev * dev,struct ocrdma_cq * cq,int entries,int dpp_cq,u16 pd_id)1779fe2caefcSParav Pandit int ocrdma_mbx_create_cq(struct ocrdma_dev *dev, struct ocrdma_cq *cq,
1780cffce990SNaresh Gottumukkala 			 int entries, int dpp_cq, u16 pd_id)
1781fe2caefcSParav Pandit {
1782fe2caefcSParav Pandit 	int status = -ENOMEM; int max_hw_cqe;
1783fe2caefcSParav Pandit 	struct pci_dev *pdev = dev->nic_info.pdev;
1784fe2caefcSParav Pandit 	struct ocrdma_create_cq *cmd;
1785fe2caefcSParav Pandit 	struct ocrdma_create_cq_rsp *rsp;
1786fe2caefcSParav Pandit 	u32 hw_pages, cqe_size, page_size, cqe_count;
1787fe2caefcSParav Pandit 
1788fe2caefcSParav Pandit 	if (entries > dev->attr.max_cqe) {
1789ef99c4c2SNaresh Gottumukkala 		pr_err("%s(%d) max_cqe=0x%x, requester_cqe=0x%x\n",
1790fe2caefcSParav Pandit 		       __func__, dev->id, dev->attr.max_cqe, entries);
1791fe2caefcSParav Pandit 		return -EINVAL;
1792fe2caefcSParav Pandit 	}
179321c3391aSDevesh Sharma 	if (dpp_cq && (ocrdma_get_asic_type(dev) != OCRDMA_ASIC_GEN_SKH_R))
1794fe2caefcSParav Pandit 		return -EINVAL;
1795fe2caefcSParav Pandit 
1796fe2caefcSParav Pandit 	if (dpp_cq) {
1797fe2caefcSParav Pandit 		cq->max_hw_cqe = 1;
1798fe2caefcSParav Pandit 		max_hw_cqe = 1;
1799fe2caefcSParav Pandit 		cqe_size = OCRDMA_DPP_CQE_SIZE;
1800fe2caefcSParav Pandit 		hw_pages = 1;
1801fe2caefcSParav Pandit 	} else {
1802fe2caefcSParav Pandit 		cq->max_hw_cqe = dev->attr.max_cqe;
1803fe2caefcSParav Pandit 		max_hw_cqe = dev->attr.max_cqe;
1804fe2caefcSParav Pandit 		cqe_size = sizeof(struct ocrdma_cqe);
1805fe2caefcSParav Pandit 		hw_pages = OCRDMA_CREATE_CQ_MAX_PAGES;
1806fe2caefcSParav Pandit 	}
1807fe2caefcSParav Pandit 
1808fe2caefcSParav Pandit 	cq->len = roundup(max_hw_cqe * cqe_size, OCRDMA_MIN_Q_PAGE_SIZE);
1809fe2caefcSParav Pandit 
1810fe2caefcSParav Pandit 	cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_CREATE_CQ, sizeof(*cmd));
1811fe2caefcSParav Pandit 	if (!cmd)
1812fe2caefcSParav Pandit 		return -ENOMEM;
1813fe2caefcSParav Pandit 	ocrdma_init_mch(&cmd->cmd.req, OCRDMA_CMD_CREATE_CQ,
1814fe2caefcSParav Pandit 			OCRDMA_SUBSYS_COMMON, sizeof(*cmd));
1815750afb08SLuis Chamberlain 	cq->va = dma_alloc_coherent(&pdev->dev, cq->len, &cq->pa, GFP_KERNEL);
1816fe2caefcSParav Pandit 	if (!cq->va) {
1817fe2caefcSParav Pandit 		status = -ENOMEM;
1818fe2caefcSParav Pandit 		goto mem_err;
1819fe2caefcSParav Pandit 	}
1820fe2caefcSParav Pandit 	page_size = cq->len / hw_pages;
1821fe2caefcSParav Pandit 	cmd->cmd.pgsz_pgcnt = (page_size / OCRDMA_MIN_Q_PAGE_SIZE) <<
1822fe2caefcSParav Pandit 					OCRDMA_CREATE_CQ_PAGE_SIZE_SHIFT;
1823fe2caefcSParav Pandit 	cmd->cmd.pgsz_pgcnt |= hw_pages;
1824fe2caefcSParav Pandit 	cmd->cmd.ev_cnt_flags = OCRDMA_CREATE_CQ_DEF_FLAGS;
1825fe2caefcSParav Pandit 
1826fe2caefcSParav Pandit 	cq->eqn = ocrdma_bind_eq(dev);
1827cffce990SNaresh Gottumukkala 	cmd->cmd.req.rsvd_version = OCRDMA_CREATE_CQ_VER3;
1828fe2caefcSParav Pandit 	cqe_count = cq->len / cqe_size;
1829ea617626SDevesh Sharma 	cq->cqe_cnt = cqe_count;
1830f99b1649SNaresh Gottumukkala 	if (cqe_count > 1024) {
1831fe2caefcSParav Pandit 		/* Set cnt to 3 to indicate more than 1024 cq entries */
1832fe2caefcSParav Pandit 		cmd->cmd.ev_cnt_flags |= (0x3 << OCRDMA_CREATE_CQ_CNT_SHIFT);
1833f99b1649SNaresh Gottumukkala 	} else {
1834fe2caefcSParav Pandit 		u8 count = 0;
1835fe2caefcSParav Pandit 		switch (cqe_count) {
1836fe2caefcSParav Pandit 		case 256:
1837fe2caefcSParav Pandit 			count = 0;
1838fe2caefcSParav Pandit 			break;
1839fe2caefcSParav Pandit 		case 512:
1840fe2caefcSParav Pandit 			count = 1;
1841fe2caefcSParav Pandit 			break;
1842fe2caefcSParav Pandit 		case 1024:
1843fe2caefcSParav Pandit 			count = 2;
1844fe2caefcSParav Pandit 			break;
1845fe2caefcSParav Pandit 		default:
1846fe2caefcSParav Pandit 			goto mbx_err;
1847fe2caefcSParav Pandit 		}
1848fe2caefcSParav Pandit 		cmd->cmd.ev_cnt_flags |= (count << OCRDMA_CREATE_CQ_CNT_SHIFT);
1849fe2caefcSParav Pandit 	}
1850fe2caefcSParav Pandit 	/* shared eq between all the consumer cqs. */
1851fe2caefcSParav Pandit 	cmd->cmd.eqn = cq->eqn;
185221c3391aSDevesh Sharma 	if (ocrdma_get_asic_type(dev) == OCRDMA_ASIC_GEN_SKH_R) {
1853fe2caefcSParav Pandit 		if (dpp_cq)
1854fe2caefcSParav Pandit 			cmd->cmd.pgsz_pgcnt |= OCRDMA_CREATE_CQ_DPP <<
1855fe2caefcSParav Pandit 				OCRDMA_CREATE_CQ_TYPE_SHIFT;
1856fe2caefcSParav Pandit 		cq->phase_change = false;
18578ac0c7c7SDevesh Sharma 		cmd->cmd.pdid_cqecnt = (cq->len / cqe_size);
1858fe2caefcSParav Pandit 	} else {
18598ac0c7c7SDevesh Sharma 		cmd->cmd.pdid_cqecnt = (cq->len / cqe_size) - 1;
1860fe2caefcSParav Pandit 		cmd->cmd.ev_cnt_flags |= OCRDMA_CREATE_CQ_FLAGS_AUTO_VALID;
1861fe2caefcSParav Pandit 		cq->phase_change = true;
1862fe2caefcSParav Pandit 	}
1863fe2caefcSParav Pandit 
18648ac0c7c7SDevesh Sharma 	/* pd_id valid only for v3 */
18658ac0c7c7SDevesh Sharma 	cmd->cmd.pdid_cqecnt |= (pd_id <<
18668ac0c7c7SDevesh Sharma 		OCRDMA_CREATE_CQ_CMD_PDID_SHIFT);
1867fe2caefcSParav Pandit 	ocrdma_build_q_pages(&cmd->cmd.pa[0], hw_pages, cq->pa, page_size);
1868fe2caefcSParav Pandit 	status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1869fe2caefcSParav Pandit 	if (status)
1870fe2caefcSParav Pandit 		goto mbx_err;
1871fe2caefcSParav Pandit 
1872fe2caefcSParav Pandit 	rsp = (struct ocrdma_create_cq_rsp *)cmd;
1873fe2caefcSParav Pandit 	cq->id = (u16) (rsp->rsp.cq_id & OCRDMA_CREATE_CQ_RSP_CQ_ID_MASK);
1874fe2caefcSParav Pandit 	kfree(cmd);
1875fe2caefcSParav Pandit 	return 0;
1876fe2caefcSParav Pandit mbx_err:
1877fe2caefcSParav Pandit 	ocrdma_unbind_eq(dev, cq->eqn);
1878fe2caefcSParav Pandit 	dma_free_coherent(&pdev->dev, cq->len, cq->va, cq->pa);
1879fe2caefcSParav Pandit mem_err:
1880fe2caefcSParav Pandit 	kfree(cmd);
1881fe2caefcSParav Pandit 	return status;
1882fe2caefcSParav Pandit }
1883fe2caefcSParav Pandit 
ocrdma_mbx_destroy_cq(struct ocrdma_dev * dev,struct ocrdma_cq * cq)1884a52c8e24SLeon Romanovsky void ocrdma_mbx_destroy_cq(struct ocrdma_dev *dev, struct ocrdma_cq *cq)
1885fe2caefcSParav Pandit {
1886fe2caefcSParav Pandit 	struct ocrdma_destroy_cq *cmd;
1887fe2caefcSParav Pandit 
1888fe2caefcSParav Pandit 	cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_DELETE_CQ, sizeof(*cmd));
1889fe2caefcSParav Pandit 	if (!cmd)
1890a52c8e24SLeon Romanovsky 		return;
1891fe2caefcSParav Pandit 	ocrdma_init_mch(&cmd->req, OCRDMA_CMD_DELETE_CQ,
1892fe2caefcSParav Pandit 			OCRDMA_SUBSYS_COMMON, sizeof(*cmd));
1893fe2caefcSParav Pandit 
1894fe2caefcSParav Pandit 	cmd->bypass_flush_qid |=
1895fe2caefcSParav Pandit 	    (cq->id << OCRDMA_DESTROY_CQ_QID_SHIFT) &
1896fe2caefcSParav Pandit 	    OCRDMA_DESTROY_CQ_QID_MASK;
1897fe2caefcSParav Pandit 
1898a52c8e24SLeon Romanovsky 	ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1899ea617626SDevesh Sharma 	ocrdma_unbind_eq(dev, cq->eqn);
1900fe2caefcSParav Pandit 	dma_free_coherent(&dev->nic_info.pdev->dev, cq->len, cq->va, cq->pa);
1901fe2caefcSParav Pandit 	kfree(cmd);
1902fe2caefcSParav Pandit }
1903fe2caefcSParav Pandit 
ocrdma_mbx_alloc_lkey(struct ocrdma_dev * dev,struct ocrdma_hw_mr * hwmr,u32 pdid,int addr_check)1904fe2caefcSParav Pandit int ocrdma_mbx_alloc_lkey(struct ocrdma_dev *dev, struct ocrdma_hw_mr *hwmr,
1905fe2caefcSParav Pandit 			  u32 pdid, int addr_check)
1906fe2caefcSParav Pandit {
1907fe2caefcSParav Pandit 	int status = -ENOMEM;
1908fe2caefcSParav Pandit 	struct ocrdma_alloc_lkey *cmd;
1909fe2caefcSParav Pandit 	struct ocrdma_alloc_lkey_rsp *rsp;
1910fe2caefcSParav Pandit 
1911fe2caefcSParav Pandit 	cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_ALLOC_LKEY, sizeof(*cmd));
1912fe2caefcSParav Pandit 	if (!cmd)
1913fe2caefcSParav Pandit 		return status;
1914fe2caefcSParav Pandit 	cmd->pdid = pdid;
1915fe2caefcSParav Pandit 	cmd->pbl_sz_flags |= addr_check;
1916fe2caefcSParav Pandit 	cmd->pbl_sz_flags |= (hwmr->fr_mr << OCRDMA_ALLOC_LKEY_FMR_SHIFT);
1917fe2caefcSParav Pandit 	cmd->pbl_sz_flags |=
1918fe2caefcSParav Pandit 	    (hwmr->remote_wr << OCRDMA_ALLOC_LKEY_REMOTE_WR_SHIFT);
1919fe2caefcSParav Pandit 	cmd->pbl_sz_flags |=
1920fe2caefcSParav Pandit 	    (hwmr->remote_rd << OCRDMA_ALLOC_LKEY_REMOTE_RD_SHIFT);
1921fe2caefcSParav Pandit 	cmd->pbl_sz_flags |=
1922fe2caefcSParav Pandit 	    (hwmr->local_wr << OCRDMA_ALLOC_LKEY_LOCAL_WR_SHIFT);
1923fe2caefcSParav Pandit 	cmd->pbl_sz_flags |=
1924fe2caefcSParav Pandit 	    (hwmr->remote_atomic << OCRDMA_ALLOC_LKEY_REMOTE_ATOMIC_SHIFT);
1925fe2caefcSParav Pandit 	cmd->pbl_sz_flags |=
1926fe2caefcSParav Pandit 	    (hwmr->num_pbls << OCRDMA_ALLOC_LKEY_PBL_SIZE_SHIFT);
1927fe2caefcSParav Pandit 
1928fe2caefcSParav Pandit 	status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1929fe2caefcSParav Pandit 	if (status)
1930fe2caefcSParav Pandit 		goto mbx_err;
1931fe2caefcSParav Pandit 	rsp = (struct ocrdma_alloc_lkey_rsp *)cmd;
1932fe2caefcSParav Pandit 	hwmr->lkey = rsp->lrkey;
1933fe2caefcSParav Pandit mbx_err:
1934fe2caefcSParav Pandit 	kfree(cmd);
1935fe2caefcSParav Pandit 	return status;
1936fe2caefcSParav Pandit }
1937fe2caefcSParav Pandit 
ocrdma_mbx_dealloc_lkey(struct ocrdma_dev * dev,int fr_mr,u32 lkey)1938fe2caefcSParav Pandit int ocrdma_mbx_dealloc_lkey(struct ocrdma_dev *dev, int fr_mr, u32 lkey)
1939fe2caefcSParav Pandit {
1940f4e96c1aSGustavo A. R. Silva 	int status;
1941fe2caefcSParav Pandit 	struct ocrdma_dealloc_lkey *cmd;
1942fe2caefcSParav Pandit 
1943fe2caefcSParav Pandit 	cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_DEALLOC_LKEY, sizeof(*cmd));
1944fe2caefcSParav Pandit 	if (!cmd)
1945fe2caefcSParav Pandit 		return -ENOMEM;
1946fe2caefcSParav Pandit 	cmd->lkey = lkey;
1947fe2caefcSParav Pandit 	cmd->rsvd_frmr = fr_mr ? 1 : 0;
1948fe2caefcSParav Pandit 	status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1949f4e96c1aSGustavo A. R. Silva 
1950fe2caefcSParav Pandit 	kfree(cmd);
1951fe2caefcSParav Pandit 	return status;
1952fe2caefcSParav Pandit }
1953fe2caefcSParav Pandit 
ocrdma_mbx_reg_mr(struct ocrdma_dev * dev,struct ocrdma_hw_mr * hwmr,u32 pdid,u32 pbl_cnt,u32 pbe_size,u32 last)1954fe2caefcSParav Pandit static int ocrdma_mbx_reg_mr(struct ocrdma_dev *dev, struct ocrdma_hw_mr *hwmr,
1955fe2caefcSParav Pandit 			     u32 pdid, u32 pbl_cnt, u32 pbe_size, u32 last)
1956fe2caefcSParav Pandit {
1957fe2caefcSParav Pandit 	int status = -ENOMEM;
1958fe2caefcSParav Pandit 	int i;
1959fe2caefcSParav Pandit 	struct ocrdma_reg_nsmr *cmd;
1960fe2caefcSParav Pandit 	struct ocrdma_reg_nsmr_rsp *rsp;
19611d4299edSJason Gunthorpe 	u64 fbo = hwmr->va & (hwmr->pbe_size - 1);
1962fe2caefcSParav Pandit 
1963fe2caefcSParav Pandit 	cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_REGISTER_NSMR, sizeof(*cmd));
1964fe2caefcSParav Pandit 	if (!cmd)
1965fe2caefcSParav Pandit 		return -ENOMEM;
1966fe2caefcSParav Pandit 	cmd->num_pbl_pdid =
1967fe2caefcSParav Pandit 	    pdid | (hwmr->num_pbls << OCRDMA_REG_NSMR_NUM_PBL_SHIFT);
19682b51a9b9SNaresh Gottumukkala 	cmd->fr_mr = hwmr->fr_mr;
1969fe2caefcSParav Pandit 
1970fe2caefcSParav Pandit 	cmd->flags_hpage_pbe_sz |= (hwmr->remote_wr <<
1971fe2caefcSParav Pandit 				    OCRDMA_REG_NSMR_REMOTE_WR_SHIFT);
1972fe2caefcSParav Pandit 	cmd->flags_hpage_pbe_sz |= (hwmr->remote_rd <<
1973fe2caefcSParav Pandit 				    OCRDMA_REG_NSMR_REMOTE_RD_SHIFT);
1974fe2caefcSParav Pandit 	cmd->flags_hpage_pbe_sz |= (hwmr->local_wr <<
1975fe2caefcSParav Pandit 				    OCRDMA_REG_NSMR_LOCAL_WR_SHIFT);
1976fe2caefcSParav Pandit 	cmd->flags_hpage_pbe_sz |= (hwmr->remote_atomic <<
1977fe2caefcSParav Pandit 				    OCRDMA_REG_NSMR_REMOTE_ATOMIC_SHIFT);
1978fe2caefcSParav Pandit 	cmd->flags_hpage_pbe_sz |= (hwmr->mw_bind <<
1979fe2caefcSParav Pandit 				    OCRDMA_REG_NSMR_BIND_MEMWIN_SHIFT);
1980fe2caefcSParav Pandit 	cmd->flags_hpage_pbe_sz |= (last << OCRDMA_REG_NSMR_LAST_SHIFT);
1981fe2caefcSParav Pandit 
1982fe2caefcSParav Pandit 	cmd->flags_hpage_pbe_sz |= (hwmr->pbe_size / OCRDMA_MIN_HPAGE_SIZE);
1983fe2caefcSParav Pandit 	cmd->flags_hpage_pbe_sz |= (hwmr->pbl_size / OCRDMA_MIN_HPAGE_SIZE) <<
1984fe2caefcSParav Pandit 					OCRDMA_REG_NSMR_HPAGE_SIZE_SHIFT;
1985fe2caefcSParav Pandit 	cmd->totlen_low = hwmr->len;
1986fe2caefcSParav Pandit 	cmd->totlen_high = upper_32_bits(hwmr->len);
19871d4299edSJason Gunthorpe 	cmd->fbo_low = lower_32_bits(fbo);
19881d4299edSJason Gunthorpe 	cmd->fbo_high = upper_32_bits(fbo);
1989fe2caefcSParav Pandit 	cmd->va_loaddr = (u32) hwmr->va;
1990fe2caefcSParav Pandit 	cmd->va_hiaddr = (u32) upper_32_bits(hwmr->va);
1991fe2caefcSParav Pandit 
1992fe2caefcSParav Pandit 	for (i = 0; i < pbl_cnt; i++) {
1993fe2caefcSParav Pandit 		cmd->pbl[i].lo = (u32) (hwmr->pbl_table[i].pa & 0xffffffff);
1994fe2caefcSParav Pandit 		cmd->pbl[i].hi = upper_32_bits(hwmr->pbl_table[i].pa);
1995fe2caefcSParav Pandit 	}
1996fe2caefcSParav Pandit 	status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
1997fe2caefcSParav Pandit 	if (status)
1998fe2caefcSParav Pandit 		goto mbx_err;
1999fe2caefcSParav Pandit 	rsp = (struct ocrdma_reg_nsmr_rsp *)cmd;
2000fe2caefcSParav Pandit 	hwmr->lkey = rsp->lrkey;
2001fe2caefcSParav Pandit mbx_err:
2002fe2caefcSParav Pandit 	kfree(cmd);
2003fe2caefcSParav Pandit 	return status;
2004fe2caefcSParav Pandit }
2005fe2caefcSParav Pandit 
ocrdma_mbx_reg_mr_cont(struct ocrdma_dev * dev,struct ocrdma_hw_mr * hwmr,u32 pbl_cnt,u32 pbl_offset,u32 last)2006fe2caefcSParav Pandit static int ocrdma_mbx_reg_mr_cont(struct ocrdma_dev *dev,
2007fe2caefcSParav Pandit 				  struct ocrdma_hw_mr *hwmr, u32 pbl_cnt,
2008fe2caefcSParav Pandit 				  u32 pbl_offset, u32 last)
2009fe2caefcSParav Pandit {
2010689a8e31SGustavo A. R. Silva 	int status;
2011fe2caefcSParav Pandit 	int i;
2012fe2caefcSParav Pandit 	struct ocrdma_reg_nsmr_cont *cmd;
2013fe2caefcSParav Pandit 
2014fe2caefcSParav Pandit 	cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_REGISTER_NSMR_CONT, sizeof(*cmd));
2015fe2caefcSParav Pandit 	if (!cmd)
2016fe2caefcSParav Pandit 		return -ENOMEM;
2017fe2caefcSParav Pandit 	cmd->lrkey = hwmr->lkey;
2018fe2caefcSParav Pandit 	cmd->num_pbl_offset = (pbl_cnt << OCRDMA_REG_NSMR_CONT_NUM_PBL_SHIFT) |
2019fe2caefcSParav Pandit 	    (pbl_offset & OCRDMA_REG_NSMR_CONT_PBL_SHIFT_MASK);
2020fe2caefcSParav Pandit 	cmd->last = last << OCRDMA_REG_NSMR_CONT_LAST_SHIFT;
2021fe2caefcSParav Pandit 
2022fe2caefcSParav Pandit 	for (i = 0; i < pbl_cnt; i++) {
2023fe2caefcSParav Pandit 		cmd->pbl[i].lo =
2024fe2caefcSParav Pandit 		    (u32) (hwmr->pbl_table[i + pbl_offset].pa & 0xffffffff);
2025fe2caefcSParav Pandit 		cmd->pbl[i].hi =
2026fe2caefcSParav Pandit 		    upper_32_bits(hwmr->pbl_table[i + pbl_offset].pa);
2027fe2caefcSParav Pandit 	}
2028fe2caefcSParav Pandit 	status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
2029689a8e31SGustavo A. R. Silva 
2030fe2caefcSParav Pandit 	kfree(cmd);
2031fe2caefcSParav Pandit 	return status;
2032fe2caefcSParav Pandit }
2033fe2caefcSParav Pandit 
ocrdma_reg_mr(struct ocrdma_dev * dev,struct ocrdma_hw_mr * hwmr,u32 pdid,int acc)2034fe2caefcSParav Pandit int ocrdma_reg_mr(struct ocrdma_dev *dev,
2035fe2caefcSParav Pandit 		  struct ocrdma_hw_mr *hwmr, u32 pdid, int acc)
2036fe2caefcSParav Pandit {
2037fe2caefcSParav Pandit 	int status;
2038fe2caefcSParav Pandit 	u32 last = 0;
2039fe2caefcSParav Pandit 	u32 cur_pbl_cnt, pbl_offset;
2040fe2caefcSParav Pandit 	u32 pending_pbl_cnt = hwmr->num_pbls;
2041fe2caefcSParav Pandit 
2042fe2caefcSParav Pandit 	pbl_offset = 0;
2043fe2caefcSParav Pandit 	cur_pbl_cnt = min(pending_pbl_cnt, MAX_OCRDMA_NSMR_PBL);
2044fe2caefcSParav Pandit 	if (cur_pbl_cnt == pending_pbl_cnt)
2045fe2caefcSParav Pandit 		last = 1;
2046fe2caefcSParav Pandit 
2047fe2caefcSParav Pandit 	status = ocrdma_mbx_reg_mr(dev, hwmr, pdid,
2048fe2caefcSParav Pandit 				   cur_pbl_cnt, hwmr->pbe_size, last);
2049fe2caefcSParav Pandit 	if (status) {
2050ef99c4c2SNaresh Gottumukkala 		pr_err("%s() status=%d\n", __func__, status);
2051fe2caefcSParav Pandit 		return status;
2052fe2caefcSParav Pandit 	}
2053fe2caefcSParav Pandit 	/* if there is no more pbls to register then exit. */
2054fe2caefcSParav Pandit 	if (last)
2055fe2caefcSParav Pandit 		return 0;
2056fe2caefcSParav Pandit 
2057fe2caefcSParav Pandit 	while (!last) {
2058fe2caefcSParav Pandit 		pbl_offset += cur_pbl_cnt;
2059fe2caefcSParav Pandit 		pending_pbl_cnt -= cur_pbl_cnt;
2060fe2caefcSParav Pandit 		cur_pbl_cnt = min(pending_pbl_cnt, MAX_OCRDMA_NSMR_PBL);
2061fe2caefcSParav Pandit 		/* if we reach the end of the pbls, then need to set the last
2062fe2caefcSParav Pandit 		 * bit, indicating no more pbls to register for this memory key.
2063fe2caefcSParav Pandit 		 */
2064fe2caefcSParav Pandit 		if (cur_pbl_cnt == pending_pbl_cnt)
2065fe2caefcSParav Pandit 			last = 1;
2066fe2caefcSParav Pandit 
2067fe2caefcSParav Pandit 		status = ocrdma_mbx_reg_mr_cont(dev, hwmr, cur_pbl_cnt,
2068fe2caefcSParav Pandit 						pbl_offset, last);
2069fe2caefcSParav Pandit 		if (status)
2070fe2caefcSParav Pandit 			break;
2071fe2caefcSParav Pandit 	}
2072fe2caefcSParav Pandit 	if (status)
2073ef99c4c2SNaresh Gottumukkala 		pr_err("%s() err. status=%d\n", __func__, status);
2074fe2caefcSParav Pandit 
2075fe2caefcSParav Pandit 	return status;
2076fe2caefcSParav Pandit }
2077fe2caefcSParav Pandit 
ocrdma_is_qp_in_sq_flushlist(struct ocrdma_cq * cq,struct ocrdma_qp * qp)2078fe2caefcSParav Pandit bool ocrdma_is_qp_in_sq_flushlist(struct ocrdma_cq *cq, struct ocrdma_qp *qp)
2079fe2caefcSParav Pandit {
2080fe2caefcSParav Pandit 	struct ocrdma_qp *tmp;
2081fe2caefcSParav Pandit 	bool found = false;
2082fe2caefcSParav Pandit 	list_for_each_entry(tmp, &cq->sq_head, sq_entry) {
2083fe2caefcSParav Pandit 		if (qp == tmp) {
2084fe2caefcSParav Pandit 			found = true;
2085fe2caefcSParav Pandit 			break;
2086fe2caefcSParav Pandit 		}
2087fe2caefcSParav Pandit 	}
2088fe2caefcSParav Pandit 	return found;
2089fe2caefcSParav Pandit }
2090fe2caefcSParav Pandit 
ocrdma_is_qp_in_rq_flushlist(struct ocrdma_cq * cq,struct ocrdma_qp * qp)2091fe2caefcSParav Pandit bool ocrdma_is_qp_in_rq_flushlist(struct ocrdma_cq *cq, struct ocrdma_qp *qp)
2092fe2caefcSParav Pandit {
2093fe2caefcSParav Pandit 	struct ocrdma_qp *tmp;
2094fe2caefcSParav Pandit 	bool found = false;
2095fe2caefcSParav Pandit 	list_for_each_entry(tmp, &cq->rq_head, rq_entry) {
2096fe2caefcSParav Pandit 		if (qp == tmp) {
2097fe2caefcSParav Pandit 			found = true;
2098fe2caefcSParav Pandit 			break;
2099fe2caefcSParav Pandit 		}
2100fe2caefcSParav Pandit 	}
2101fe2caefcSParav Pandit 	return found;
2102fe2caefcSParav Pandit }
2103fe2caefcSParav Pandit 
ocrdma_flush_qp(struct ocrdma_qp * qp)2104fe2caefcSParav Pandit void ocrdma_flush_qp(struct ocrdma_qp *qp)
2105fe2caefcSParav Pandit {
2106fe2caefcSParav Pandit 	bool found;
2107fe2caefcSParav Pandit 	unsigned long flags;
2108d2b8f7b1SMitesh Ahuja 	struct ocrdma_dev *dev = get_ocrdma_dev(qp->ibqp.device);
2109fe2caefcSParav Pandit 
2110d2b8f7b1SMitesh Ahuja 	spin_lock_irqsave(&dev->flush_q_lock, flags);
2111fe2caefcSParav Pandit 	found = ocrdma_is_qp_in_sq_flushlist(qp->sq_cq, qp);
2112fe2caefcSParav Pandit 	if (!found)
2113fe2caefcSParav Pandit 		list_add_tail(&qp->sq_entry, &qp->sq_cq->sq_head);
2114fe2caefcSParav Pandit 	if (!qp->srq) {
2115fe2caefcSParav Pandit 		found = ocrdma_is_qp_in_rq_flushlist(qp->rq_cq, qp);
2116fe2caefcSParav Pandit 		if (!found)
2117fe2caefcSParav Pandit 			list_add_tail(&qp->rq_entry, &qp->rq_cq->rq_head);
2118fe2caefcSParav Pandit 	}
2119d2b8f7b1SMitesh Ahuja 	spin_unlock_irqrestore(&dev->flush_q_lock, flags);
2120fe2caefcSParav Pandit }
2121fe2caefcSParav Pandit 
ocrdma_init_hwq_ptr(struct ocrdma_qp * qp)2122f11220eeSNaresh Gottumukkala static void ocrdma_init_hwq_ptr(struct ocrdma_qp *qp)
2123f11220eeSNaresh Gottumukkala {
2124f11220eeSNaresh Gottumukkala 	qp->sq.head = 0;
2125f11220eeSNaresh Gottumukkala 	qp->sq.tail = 0;
2126f11220eeSNaresh Gottumukkala 	qp->rq.head = 0;
2127f11220eeSNaresh Gottumukkala 	qp->rq.tail = 0;
2128f11220eeSNaresh Gottumukkala }
2129f11220eeSNaresh Gottumukkala 
ocrdma_qp_state_change(struct ocrdma_qp * qp,enum ib_qp_state new_ib_state,enum ib_qp_state * old_ib_state)2130057729cbSNaresh Gottumukkala int ocrdma_qp_state_change(struct ocrdma_qp *qp, enum ib_qp_state new_ib_state,
2131fe2caefcSParav Pandit 			   enum ib_qp_state *old_ib_state)
2132fe2caefcSParav Pandit {
2133fe2caefcSParav Pandit 	unsigned long flags;
2134fe2caefcSParav Pandit 	enum ocrdma_qp_state new_state;
2135fe2caefcSParav Pandit 	new_state = get_ocrdma_qp_state(new_ib_state);
2136fe2caefcSParav Pandit 
2137fe2caefcSParav Pandit 	/* sync with wqe and rqe posting */
2138fe2caefcSParav Pandit 	spin_lock_irqsave(&qp->q_lock, flags);
2139fe2caefcSParav Pandit 
2140fe2caefcSParav Pandit 	if (old_ib_state)
2141fe2caefcSParav Pandit 		*old_ib_state = get_ibqp_state(qp->state);
2142fe2caefcSParav Pandit 	if (new_state == qp->state) {
2143fe2caefcSParav Pandit 		spin_unlock_irqrestore(&qp->q_lock, flags);
2144fe2caefcSParav Pandit 		return 1;
2145fe2caefcSParav Pandit 	}
2146fe2caefcSParav Pandit 
2147057729cbSNaresh Gottumukkala 
2148f11220eeSNaresh Gottumukkala 	if (new_state == OCRDMA_QPS_INIT) {
2149f11220eeSNaresh Gottumukkala 		ocrdma_init_hwq_ptr(qp);
2150f11220eeSNaresh Gottumukkala 		ocrdma_del_flush_qp(qp);
2151f11220eeSNaresh Gottumukkala 	} else if (new_state == OCRDMA_QPS_ERR) {
2152fe2caefcSParav Pandit 		ocrdma_flush_qp(qp);
2153f11220eeSNaresh Gottumukkala 	}
2154057729cbSNaresh Gottumukkala 
2155fe2caefcSParav Pandit 	qp->state = new_state;
2156fe2caefcSParav Pandit 
2157fe2caefcSParav Pandit 	spin_unlock_irqrestore(&qp->q_lock, flags);
2158d1c95b0eSMarkus Elfring 	return 0;
2159fe2caefcSParav Pandit }
2160fe2caefcSParav Pandit 
ocrdma_set_create_qp_mbx_access_flags(struct ocrdma_qp * qp)2161fe2caefcSParav Pandit static u32 ocrdma_set_create_qp_mbx_access_flags(struct ocrdma_qp *qp)
2162fe2caefcSParav Pandit {
2163fe2caefcSParav Pandit 	u32 flags = 0;
2164fe2caefcSParav Pandit 	if (qp->cap_flags & OCRDMA_QP_INB_RD)
2165fe2caefcSParav Pandit 		flags |= OCRDMA_CREATE_QP_REQ_INB_RDEN_MASK;
2166fe2caefcSParav Pandit 	if (qp->cap_flags & OCRDMA_QP_INB_WR)
2167fe2caefcSParav Pandit 		flags |= OCRDMA_CREATE_QP_REQ_INB_WREN_MASK;
2168fe2caefcSParav Pandit 	if (qp->cap_flags & OCRDMA_QP_MW_BIND)
2169fe2caefcSParav Pandit 		flags |= OCRDMA_CREATE_QP_REQ_BIND_MEMWIN_MASK;
2170fe2caefcSParav Pandit 	if (qp->cap_flags & OCRDMA_QP_LKEY0)
2171fe2caefcSParav Pandit 		flags |= OCRDMA_CREATE_QP_REQ_ZERO_LKEYEN_MASK;
2172fe2caefcSParav Pandit 	if (qp->cap_flags & OCRDMA_QP_FAST_REG)
2173fe2caefcSParav Pandit 		flags |= OCRDMA_CREATE_QP_REQ_FMR_EN_MASK;
2174fe2caefcSParav Pandit 	return flags;
2175fe2caefcSParav Pandit }
2176fe2caefcSParav Pandit 
ocrdma_set_create_qp_sq_cmd(struct ocrdma_create_qp_req * cmd,struct ib_qp_init_attr * attrs,struct ocrdma_qp * qp)2177fe2caefcSParav Pandit static int ocrdma_set_create_qp_sq_cmd(struct ocrdma_create_qp_req *cmd,
2178fe2caefcSParav Pandit 					struct ib_qp_init_attr *attrs,
2179fe2caefcSParav Pandit 					struct ocrdma_qp *qp)
2180fe2caefcSParav Pandit {
2181fe2caefcSParav Pandit 	int status;
2182fe2caefcSParav Pandit 	u32 len, hw_pages, hw_page_size;
2183fe2caefcSParav Pandit 	dma_addr_t pa;
2184d2b8f7b1SMitesh Ahuja 	struct ocrdma_pd *pd = qp->pd;
2185d2b8f7b1SMitesh Ahuja 	struct ocrdma_dev *dev = get_ocrdma_dev(pd->ibpd.device);
2186fe2caefcSParav Pandit 	struct pci_dev *pdev = dev->nic_info.pdev;
2187fe2caefcSParav Pandit 	u32 max_wqe_allocated;
2188fe2caefcSParav Pandit 	u32 max_sges = attrs->cap.max_send_sge;
2189fe2caefcSParav Pandit 
219043a6b402SNaresh Gottumukkala 	/* QP1 may exceed 127 */
21916ebacdfcSDan Carpenter 	max_wqe_allocated = min_t(u32, attrs->cap.max_send_wr + 1,
219243a6b402SNaresh Gottumukkala 				dev->attr.max_wqe);
2193fe2caefcSParav Pandit 
2194fe2caefcSParav Pandit 	status = ocrdma_build_q_conf(&max_wqe_allocated,
2195fe2caefcSParav Pandit 		dev->attr.wqe_size, &hw_pages, &hw_page_size);
2196fe2caefcSParav Pandit 	if (status) {
2197ef99c4c2SNaresh Gottumukkala 		pr_err("%s() req. max_send_wr=0x%x\n", __func__,
2198fe2caefcSParav Pandit 		       max_wqe_allocated);
2199fe2caefcSParav Pandit 		return -EINVAL;
2200fe2caefcSParav Pandit 	}
2201fe2caefcSParav Pandit 	qp->sq.max_cnt = max_wqe_allocated;
2202fe2caefcSParav Pandit 	len = (hw_pages * hw_page_size);
2203fe2caefcSParav Pandit 
2204750afb08SLuis Chamberlain 	qp->sq.va = dma_alloc_coherent(&pdev->dev, len, &pa, GFP_KERNEL);
2205fe2caefcSParav Pandit 	if (!qp->sq.va)
2206fe2caefcSParav Pandit 		return -EINVAL;
2207fe2caefcSParav Pandit 	qp->sq.len = len;
2208fe2caefcSParav Pandit 	qp->sq.pa = pa;
2209fe2caefcSParav Pandit 	qp->sq.entry_size = dev->attr.wqe_size;
2210fe2caefcSParav Pandit 	ocrdma_build_q_pages(&cmd->wq_addr[0], hw_pages, pa, hw_page_size);
2211fe2caefcSParav Pandit 
2212fe2caefcSParav Pandit 	cmd->type_pgsz_pdn |= (ilog2(hw_page_size / OCRDMA_MIN_Q_PAGE_SIZE)
2213fe2caefcSParav Pandit 				<< OCRDMA_CREATE_QP_REQ_SQ_PAGE_SIZE_SHIFT);
2214fe2caefcSParav Pandit 	cmd->num_wq_rq_pages |= (hw_pages <<
2215fe2caefcSParav Pandit 				 OCRDMA_CREATE_QP_REQ_NUM_WQ_PAGES_SHIFT) &
2216fe2caefcSParav Pandit 	    OCRDMA_CREATE_QP_REQ_NUM_WQ_PAGES_MASK;
2217fe2caefcSParav Pandit 	cmd->max_sge_send_write |= (max_sges <<
2218fe2caefcSParav Pandit 				    OCRDMA_CREATE_QP_REQ_MAX_SGE_SEND_SHIFT) &
2219fe2caefcSParav Pandit 	    OCRDMA_CREATE_QP_REQ_MAX_SGE_SEND_MASK;
2220fe2caefcSParav Pandit 	cmd->max_sge_send_write |= (max_sges <<
2221fe2caefcSParav Pandit 				    OCRDMA_CREATE_QP_REQ_MAX_SGE_WRITE_SHIFT) &
2222fe2caefcSParav Pandit 					OCRDMA_CREATE_QP_REQ_MAX_SGE_WRITE_MASK;
2223fe2caefcSParav Pandit 	cmd->max_wqe_rqe |= (ilog2(qp->sq.max_cnt) <<
2224fe2caefcSParav Pandit 			     OCRDMA_CREATE_QP_REQ_MAX_WQE_SHIFT) &
2225fe2caefcSParav Pandit 				OCRDMA_CREATE_QP_REQ_MAX_WQE_MASK;
2226fe2caefcSParav Pandit 	cmd->wqe_rqe_size |= (dev->attr.wqe_size <<
2227fe2caefcSParav Pandit 			      OCRDMA_CREATE_QP_REQ_WQE_SIZE_SHIFT) &
2228fe2caefcSParav Pandit 				OCRDMA_CREATE_QP_REQ_WQE_SIZE_MASK;
2229fe2caefcSParav Pandit 	return 0;
2230fe2caefcSParav Pandit }
2231fe2caefcSParav Pandit 
ocrdma_set_create_qp_rq_cmd(struct ocrdma_create_qp_req * cmd,struct ib_qp_init_attr * attrs,struct ocrdma_qp * qp)2232fe2caefcSParav Pandit static int ocrdma_set_create_qp_rq_cmd(struct ocrdma_create_qp_req *cmd,
2233fe2caefcSParav Pandit 					struct ib_qp_init_attr *attrs,
2234fe2caefcSParav Pandit 					struct ocrdma_qp *qp)
2235fe2caefcSParav Pandit {
2236fe2caefcSParav Pandit 	int status;
2237fe2caefcSParav Pandit 	u32 len, hw_pages, hw_page_size;
2238fe2caefcSParav Pandit 	dma_addr_t pa = 0;
2239d2b8f7b1SMitesh Ahuja 	struct ocrdma_pd *pd = qp->pd;
2240d2b8f7b1SMitesh Ahuja 	struct ocrdma_dev *dev = get_ocrdma_dev(pd->ibpd.device);
2241fe2caefcSParav Pandit 	struct pci_dev *pdev = dev->nic_info.pdev;
2242fe2caefcSParav Pandit 	u32 max_rqe_allocated = attrs->cap.max_recv_wr + 1;
2243fe2caefcSParav Pandit 
2244fe2caefcSParav Pandit 	status = ocrdma_build_q_conf(&max_rqe_allocated, dev->attr.rqe_size,
2245fe2caefcSParav Pandit 				     &hw_pages, &hw_page_size);
2246fe2caefcSParav Pandit 	if (status) {
2247ef99c4c2SNaresh Gottumukkala 		pr_err("%s() req. max_recv_wr=0x%x\n", __func__,
2248fe2caefcSParav Pandit 		       attrs->cap.max_recv_wr + 1);
2249fe2caefcSParav Pandit 		return status;
2250fe2caefcSParav Pandit 	}
2251fe2caefcSParav Pandit 	qp->rq.max_cnt = max_rqe_allocated;
2252fe2caefcSParav Pandit 	len = (hw_pages * hw_page_size);
2253fe2caefcSParav Pandit 
2254750afb08SLuis Chamberlain 	qp->rq.va = dma_alloc_coherent(&pdev->dev, len, &pa, GFP_KERNEL);
2255fe2caefcSParav Pandit 	if (!qp->rq.va)
2256c94e15c5SWei Yongjun 		return -ENOMEM;
2257fe2caefcSParav Pandit 	qp->rq.pa = pa;
2258fe2caefcSParav Pandit 	qp->rq.len = len;
2259fe2caefcSParav Pandit 	qp->rq.entry_size = dev->attr.rqe_size;
2260fe2caefcSParav Pandit 
2261fe2caefcSParav Pandit 	ocrdma_build_q_pages(&cmd->rq_addr[0], hw_pages, pa, hw_page_size);
2262fe2caefcSParav Pandit 	cmd->type_pgsz_pdn |= (ilog2(hw_page_size / OCRDMA_MIN_Q_PAGE_SIZE) <<
2263fe2caefcSParav Pandit 		OCRDMA_CREATE_QP_REQ_RQ_PAGE_SIZE_SHIFT);
2264fe2caefcSParav Pandit 	cmd->num_wq_rq_pages |=
2265fe2caefcSParav Pandit 	    (hw_pages << OCRDMA_CREATE_QP_REQ_NUM_RQ_PAGES_SHIFT) &
2266fe2caefcSParav Pandit 	    OCRDMA_CREATE_QP_REQ_NUM_RQ_PAGES_MASK;
2267fe2caefcSParav Pandit 	cmd->max_sge_recv_flags |= (attrs->cap.max_recv_sge <<
2268fe2caefcSParav Pandit 				OCRDMA_CREATE_QP_REQ_MAX_SGE_RECV_SHIFT) &
2269fe2caefcSParav Pandit 				OCRDMA_CREATE_QP_REQ_MAX_SGE_RECV_MASK;
2270fe2caefcSParav Pandit 	cmd->max_wqe_rqe |= (ilog2(qp->rq.max_cnt) <<
2271fe2caefcSParav Pandit 				OCRDMA_CREATE_QP_REQ_MAX_RQE_SHIFT) &
2272fe2caefcSParav Pandit 				OCRDMA_CREATE_QP_REQ_MAX_RQE_MASK;
2273fe2caefcSParav Pandit 	cmd->wqe_rqe_size |= (dev->attr.rqe_size <<
2274fe2caefcSParav Pandit 			OCRDMA_CREATE_QP_REQ_RQE_SIZE_SHIFT) &
2275fe2caefcSParav Pandit 			OCRDMA_CREATE_QP_REQ_RQE_SIZE_MASK;
2276fe2caefcSParav Pandit 	return 0;
2277fe2caefcSParav Pandit }
2278fe2caefcSParav Pandit 
ocrdma_set_create_qp_dpp_cmd(struct ocrdma_create_qp_req * cmd,struct ocrdma_pd * pd,struct ocrdma_qp * qp,u8 enable_dpp_cq,u16 dpp_cq_id)2279fe2caefcSParav Pandit static void ocrdma_set_create_qp_dpp_cmd(struct ocrdma_create_qp_req *cmd,
2280fe2caefcSParav Pandit 					 struct ocrdma_pd *pd,
2281fe2caefcSParav Pandit 					 struct ocrdma_qp *qp,
2282fe2caefcSParav Pandit 					 u8 enable_dpp_cq, u16 dpp_cq_id)
2283fe2caefcSParav Pandit {
2284fe2caefcSParav Pandit 	pd->num_dpp_qp--;
2285fe2caefcSParav Pandit 	qp->dpp_enabled = true;
2286fe2caefcSParav Pandit 	cmd->max_sge_recv_flags |= OCRDMA_CREATE_QP_REQ_ENABLE_DPP_MASK;
2287fe2caefcSParav Pandit 	if (!enable_dpp_cq)
2288fe2caefcSParav Pandit 		return;
2289fe2caefcSParav Pandit 	cmd->max_sge_recv_flags |= OCRDMA_CREATE_QP_REQ_ENABLE_DPP_MASK;
2290fe2caefcSParav Pandit 	cmd->dpp_credits_cqid = dpp_cq_id;
2291fe2caefcSParav Pandit 	cmd->dpp_credits_cqid |= OCRDMA_CREATE_QP_REQ_DPP_CREDIT_LIMIT <<
2292fe2caefcSParav Pandit 					OCRDMA_CREATE_QP_REQ_DPP_CREDIT_SHIFT;
2293fe2caefcSParav Pandit }
2294fe2caefcSParav Pandit 
ocrdma_set_create_qp_ird_cmd(struct ocrdma_create_qp_req * cmd,struct ocrdma_qp * qp)2295fe2caefcSParav Pandit static int ocrdma_set_create_qp_ird_cmd(struct ocrdma_create_qp_req *cmd,
2296fe2caefcSParav Pandit 					struct ocrdma_qp *qp)
2297fe2caefcSParav Pandit {
2298d2b8f7b1SMitesh Ahuja 	struct ocrdma_pd *pd = qp->pd;
2299d2b8f7b1SMitesh Ahuja 	struct ocrdma_dev *dev = get_ocrdma_dev(pd->ibpd.device);
2300fe2caefcSParav Pandit 	struct pci_dev *pdev = dev->nic_info.pdev;
2301fe2caefcSParav Pandit 	dma_addr_t pa = 0;
2302fe2caefcSParav Pandit 	int ird_page_size = dev->attr.ird_page_size;
2303fe2caefcSParav Pandit 	int ird_q_len = dev->attr.num_ird_pages * ird_page_size;
230443a6b402SNaresh Gottumukkala 	struct ocrdma_hdr_wqe *rqe;
230543a6b402SNaresh Gottumukkala 	int i  = 0;
2306fe2caefcSParav Pandit 
2307fe2caefcSParav Pandit 	if (dev->attr.ird == 0)
2308fe2caefcSParav Pandit 		return 0;
2309fe2caefcSParav Pandit 
2310750afb08SLuis Chamberlain 	qp->ird_q_va = dma_alloc_coherent(&pdev->dev, ird_q_len, &pa,
2311d78756d8SHimanshu Jha 					  GFP_KERNEL);
2312fe2caefcSParav Pandit 	if (!qp->ird_q_va)
2313fe2caefcSParav Pandit 		return -ENOMEM;
2314fe2caefcSParav Pandit 	ocrdma_build_q_pages(&cmd->ird_addr[0], dev->attr.num_ird_pages,
2315fe2caefcSParav Pandit 			     pa, ird_page_size);
231643a6b402SNaresh Gottumukkala 	for (; i < ird_q_len / dev->attr.rqe_size; i++) {
231743a6b402SNaresh Gottumukkala 		rqe = (struct ocrdma_hdr_wqe *)(qp->ird_q_va +
231843a6b402SNaresh Gottumukkala 			(i * dev->attr.rqe_size));
231943a6b402SNaresh Gottumukkala 		rqe->cw = 0;
232043a6b402SNaresh Gottumukkala 		rqe->cw |= 2;
232143a6b402SNaresh Gottumukkala 		rqe->cw |= (OCRDMA_TYPE_LKEY << OCRDMA_WQE_TYPE_SHIFT);
232243a6b402SNaresh Gottumukkala 		rqe->cw |= (8 << OCRDMA_WQE_SIZE_SHIFT);
232343a6b402SNaresh Gottumukkala 		rqe->cw |= (8 << OCRDMA_WQE_NXT_WQE_SIZE_SHIFT);
232443a6b402SNaresh Gottumukkala 	}
2325fe2caefcSParav Pandit 	return 0;
2326fe2caefcSParav Pandit }
2327fe2caefcSParav Pandit 
ocrdma_get_create_qp_rsp(struct ocrdma_create_qp_rsp * rsp,struct ocrdma_qp * qp,struct ib_qp_init_attr * attrs,u16 * dpp_offset,u16 * dpp_credit_lmt)2328fe2caefcSParav Pandit static void ocrdma_get_create_qp_rsp(struct ocrdma_create_qp_rsp *rsp,
2329fe2caefcSParav Pandit 				     struct ocrdma_qp *qp,
2330fe2caefcSParav Pandit 				     struct ib_qp_init_attr *attrs,
2331fe2caefcSParav Pandit 				     u16 *dpp_offset, u16 *dpp_credit_lmt)
2332fe2caefcSParav Pandit {
2333fe2caefcSParav Pandit 	u32 max_wqe_allocated, max_rqe_allocated;
2334fe2caefcSParav Pandit 	qp->id = rsp->qp_id & OCRDMA_CREATE_QP_RSP_QP_ID_MASK;
2335fe2caefcSParav Pandit 	qp->rq.dbid = rsp->sq_rq_id & OCRDMA_CREATE_QP_RSP_RQ_ID_MASK;
2336fe2caefcSParav Pandit 	qp->sq.dbid = rsp->sq_rq_id >> OCRDMA_CREATE_QP_RSP_SQ_ID_SHIFT;
2337fe2caefcSParav Pandit 	qp->max_ird = rsp->max_ord_ird & OCRDMA_CREATE_QP_RSP_MAX_IRD_MASK;
2338fe2caefcSParav Pandit 	qp->max_ord = (rsp->max_ord_ird >> OCRDMA_CREATE_QP_RSP_MAX_ORD_SHIFT);
2339fe2caefcSParav Pandit 	qp->dpp_enabled = false;
2340fe2caefcSParav Pandit 	if (rsp->dpp_response & OCRDMA_CREATE_QP_RSP_DPP_ENABLED_MASK) {
2341fe2caefcSParav Pandit 		qp->dpp_enabled = true;
2342fe2caefcSParav Pandit 		*dpp_credit_lmt = (rsp->dpp_response &
2343fe2caefcSParav Pandit 				OCRDMA_CREATE_QP_RSP_DPP_CREDITS_MASK) >>
2344fe2caefcSParav Pandit 				OCRDMA_CREATE_QP_RSP_DPP_CREDITS_SHIFT;
2345fe2caefcSParav Pandit 		*dpp_offset = (rsp->dpp_response &
2346fe2caefcSParav Pandit 				OCRDMA_CREATE_QP_RSP_DPP_PAGE_OFFSET_MASK) >>
2347fe2caefcSParav Pandit 				OCRDMA_CREATE_QP_RSP_DPP_PAGE_OFFSET_SHIFT;
2348fe2caefcSParav Pandit 	}
2349fe2caefcSParav Pandit 	max_wqe_allocated =
2350fe2caefcSParav Pandit 		rsp->max_wqe_rqe >> OCRDMA_CREATE_QP_RSP_MAX_WQE_SHIFT;
2351fe2caefcSParav Pandit 	max_wqe_allocated = 1 << max_wqe_allocated;
2352fe2caefcSParav Pandit 	max_rqe_allocated = 1 << ((u16)rsp->max_wqe_rqe);
2353fe2caefcSParav Pandit 
2354fe2caefcSParav Pandit 	qp->sq.max_cnt = max_wqe_allocated;
2355fe2caefcSParav Pandit 	qp->sq.max_wqe_idx = max_wqe_allocated - 1;
2356fe2caefcSParav Pandit 
2357fe2caefcSParav Pandit 	if (!attrs->srq) {
2358fe2caefcSParav Pandit 		qp->rq.max_cnt = max_rqe_allocated;
2359fe2caefcSParav Pandit 		qp->rq.max_wqe_idx = max_rqe_allocated - 1;
2360fe2caefcSParav Pandit 	}
2361fe2caefcSParav Pandit }
2362fe2caefcSParav Pandit 
ocrdma_mbx_create_qp(struct ocrdma_qp * qp,struct ib_qp_init_attr * attrs,u8 enable_dpp_cq,u16 dpp_cq_id,u16 * dpp_offset,u16 * dpp_credit_lmt)2363fe2caefcSParav Pandit int ocrdma_mbx_create_qp(struct ocrdma_qp *qp, struct ib_qp_init_attr *attrs,
2364fe2caefcSParav Pandit 			 u8 enable_dpp_cq, u16 dpp_cq_id, u16 *dpp_offset,
2365fe2caefcSParav Pandit 			 u16 *dpp_credit_lmt)
2366fe2caefcSParav Pandit {
2367fe2caefcSParav Pandit 	int status = -ENOMEM;
2368fe2caefcSParav Pandit 	u32 flags = 0;
2369fe2caefcSParav Pandit 	struct ocrdma_pd *pd = qp->pd;
2370d2b8f7b1SMitesh Ahuja 	struct ocrdma_dev *dev = get_ocrdma_dev(pd->ibpd.device);
2371fe2caefcSParav Pandit 	struct pci_dev *pdev = dev->nic_info.pdev;
2372fe2caefcSParav Pandit 	struct ocrdma_cq *cq;
2373fe2caefcSParav Pandit 	struct ocrdma_create_qp_req *cmd;
2374fe2caefcSParav Pandit 	struct ocrdma_create_qp_rsp *rsp;
2375fe2caefcSParav Pandit 	int qptype;
2376fe2caefcSParav Pandit 
2377fe2caefcSParav Pandit 	switch (attrs->qp_type) {
2378fe2caefcSParav Pandit 	case IB_QPT_GSI:
2379fe2caefcSParav Pandit 		qptype = OCRDMA_QPT_GSI;
2380fe2caefcSParav Pandit 		break;
2381fe2caefcSParav Pandit 	case IB_QPT_RC:
2382fe2caefcSParav Pandit 		qptype = OCRDMA_QPT_RC;
2383fe2caefcSParav Pandit 		break;
2384fe2caefcSParav Pandit 	case IB_QPT_UD:
2385fe2caefcSParav Pandit 		qptype = OCRDMA_QPT_UD;
2386fe2caefcSParav Pandit 		break;
2387fe2caefcSParav Pandit 	default:
2388fe2caefcSParav Pandit 		return -EINVAL;
23892b50176dSJoe Perches 	}
2390fe2caefcSParav Pandit 
2391fe2caefcSParav Pandit 	cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_CREATE_QP, sizeof(*cmd));
2392fe2caefcSParav Pandit 	if (!cmd)
2393fe2caefcSParav Pandit 		return status;
2394fe2caefcSParav Pandit 	cmd->type_pgsz_pdn |= (qptype << OCRDMA_CREATE_QP_REQ_QPT_SHIFT) &
2395fe2caefcSParav Pandit 						OCRDMA_CREATE_QP_REQ_QPT_MASK;
2396fe2caefcSParav Pandit 	status = ocrdma_set_create_qp_sq_cmd(cmd, attrs, qp);
2397fe2caefcSParav Pandit 	if (status)
2398fe2caefcSParav Pandit 		goto sq_err;
2399fe2caefcSParav Pandit 
2400fe2caefcSParav Pandit 	if (attrs->srq) {
2401fe2caefcSParav Pandit 		struct ocrdma_srq *srq = get_ocrdma_srq(attrs->srq);
2402fe2caefcSParav Pandit 		cmd->max_sge_recv_flags |= OCRDMA_CREATE_QP_REQ_USE_SRQ_MASK;
2403fe2caefcSParav Pandit 		cmd->rq_addr[0].lo = srq->id;
2404fe2caefcSParav Pandit 		qp->srq = srq;
2405fe2caefcSParav Pandit 	} else {
2406fe2caefcSParav Pandit 		status = ocrdma_set_create_qp_rq_cmd(cmd, attrs, qp);
2407fe2caefcSParav Pandit 		if (status)
2408fe2caefcSParav Pandit 			goto rq_err;
2409fe2caefcSParav Pandit 	}
2410fe2caefcSParav Pandit 
2411fe2caefcSParav Pandit 	status = ocrdma_set_create_qp_ird_cmd(cmd, qp);
2412fe2caefcSParav Pandit 	if (status)
2413fe2caefcSParav Pandit 		goto mbx_err;
2414fe2caefcSParav Pandit 
2415fe2caefcSParav Pandit 	cmd->type_pgsz_pdn |= (pd->id << OCRDMA_CREATE_QP_REQ_PD_ID_SHIFT) &
2416fe2caefcSParav Pandit 				OCRDMA_CREATE_QP_REQ_PD_ID_MASK;
2417fe2caefcSParav Pandit 
2418fe2caefcSParav Pandit 	flags = ocrdma_set_create_qp_mbx_access_flags(qp);
2419fe2caefcSParav Pandit 
2420fe2caefcSParav Pandit 	cmd->max_sge_recv_flags |= flags;
2421fe2caefcSParav Pandit 	cmd->max_ord_ird |= (dev->attr.max_ord_per_qp <<
2422fe2caefcSParav Pandit 			     OCRDMA_CREATE_QP_REQ_MAX_ORD_SHIFT) &
2423fe2caefcSParav Pandit 				OCRDMA_CREATE_QP_REQ_MAX_ORD_MASK;
2424fe2caefcSParav Pandit 	cmd->max_ord_ird |= (dev->attr.max_ird_per_qp <<
2425fe2caefcSParav Pandit 			     OCRDMA_CREATE_QP_REQ_MAX_IRD_SHIFT) &
2426fe2caefcSParav Pandit 				OCRDMA_CREATE_QP_REQ_MAX_IRD_MASK;
2427fe2caefcSParav Pandit 	cq = get_ocrdma_cq(attrs->send_cq);
2428fe2caefcSParav Pandit 	cmd->wq_rq_cqid |= (cq->id << OCRDMA_CREATE_QP_REQ_WQ_CQID_SHIFT) &
2429fe2caefcSParav Pandit 				OCRDMA_CREATE_QP_REQ_WQ_CQID_MASK;
2430fe2caefcSParav Pandit 	qp->sq_cq = cq;
2431fe2caefcSParav Pandit 	cq = get_ocrdma_cq(attrs->recv_cq);
2432fe2caefcSParav Pandit 	cmd->wq_rq_cqid |= (cq->id << OCRDMA_CREATE_QP_REQ_RQ_CQID_SHIFT) &
2433fe2caefcSParav Pandit 				OCRDMA_CREATE_QP_REQ_RQ_CQID_MASK;
2434fe2caefcSParav Pandit 	qp->rq_cq = cq;
2435fe2caefcSParav Pandit 
2436f50f31e4SDevesh Sharma 	if (pd->dpp_enabled && attrs->cap.max_inline_data && pd->num_dpp_qp &&
2437f50f31e4SDevesh Sharma 	    (attrs->cap.max_inline_data <= dev->attr.max_inline_data)) {
2438fe2caefcSParav Pandit 		ocrdma_set_create_qp_dpp_cmd(cmd, pd, qp, enable_dpp_cq,
2439fe2caefcSParav Pandit 					     dpp_cq_id);
2440f99b1649SNaresh Gottumukkala 	}
2441fe2caefcSParav Pandit 
2442fe2caefcSParav Pandit 	status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
2443fe2caefcSParav Pandit 	if (status)
2444fe2caefcSParav Pandit 		goto mbx_err;
2445fe2caefcSParav Pandit 	rsp = (struct ocrdma_create_qp_rsp *)cmd;
2446fe2caefcSParav Pandit 	ocrdma_get_create_qp_rsp(rsp, qp, attrs, dpp_offset, dpp_credit_lmt);
2447fe2caefcSParav Pandit 	qp->state = OCRDMA_QPS_RST;
2448fe2caefcSParav Pandit 	kfree(cmd);
2449fe2caefcSParav Pandit 	return 0;
2450fe2caefcSParav Pandit mbx_err:
2451fe2caefcSParav Pandit 	if (qp->rq.va)
2452fe2caefcSParav Pandit 		dma_free_coherent(&pdev->dev, qp->rq.len, qp->rq.va, qp->rq.pa);
2453fe2caefcSParav Pandit rq_err:
2454ef99c4c2SNaresh Gottumukkala 	pr_err("%s(%d) rq_err\n", __func__, dev->id);
2455fe2caefcSParav Pandit 	dma_free_coherent(&pdev->dev, qp->sq.len, qp->sq.va, qp->sq.pa);
2456fe2caefcSParav Pandit sq_err:
2457ef99c4c2SNaresh Gottumukkala 	pr_err("%s(%d) sq_err\n", __func__, dev->id);
2458fe2caefcSParav Pandit 	kfree(cmd);
2459fe2caefcSParav Pandit 	return status;
2460fe2caefcSParav Pandit }
2461fe2caefcSParav Pandit 
ocrdma_mbx_query_qp(struct ocrdma_dev * dev,struct ocrdma_qp * qp,struct ocrdma_qp_params * param)2462fe2caefcSParav Pandit int ocrdma_mbx_query_qp(struct ocrdma_dev *dev, struct ocrdma_qp *qp,
2463fe2caefcSParav Pandit 			struct ocrdma_qp_params *param)
2464fe2caefcSParav Pandit {
2465fe2caefcSParav Pandit 	int status = -ENOMEM;
2466fe2caefcSParav Pandit 	struct ocrdma_query_qp *cmd;
2467fe2caefcSParav Pandit 	struct ocrdma_query_qp_rsp *rsp;
2468fe2caefcSParav Pandit 
2469038ab8b7SMitesh Ahuja 	cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_QUERY_QP, sizeof(*rsp));
2470fe2caefcSParav Pandit 	if (!cmd)
2471fe2caefcSParav Pandit 		return status;
2472fe2caefcSParav Pandit 	cmd->qp_id = qp->id;
2473fe2caefcSParav Pandit 	status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
2474fe2caefcSParav Pandit 	if (status)
2475fe2caefcSParav Pandit 		goto mbx_err;
2476fe2caefcSParav Pandit 	rsp = (struct ocrdma_query_qp_rsp *)cmd;
2477fe2caefcSParav Pandit 	memcpy(param, &rsp->params, sizeof(struct ocrdma_qp_params));
2478fe2caefcSParav Pandit mbx_err:
2479fe2caefcSParav Pandit 	kfree(cmd);
2480fe2caefcSParav Pandit 	return status;
2481fe2caefcSParav Pandit }
2482fe2caefcSParav Pandit 
ocrdma_set_av_params(struct ocrdma_qp * qp,struct ocrdma_modify_qp * cmd,struct ib_qp_attr * attrs,int attr_mask)2483f99b1649SNaresh Gottumukkala static int ocrdma_set_av_params(struct ocrdma_qp *qp,
2484fe2caefcSParav Pandit 				struct ocrdma_modify_qp *cmd,
2485bf67472cSSelvin Xavier 				struct ib_qp_attr *attrs,
2486bf67472cSSelvin Xavier 				int attr_mask)
2487fe2caefcSParav Pandit {
2488f99b1649SNaresh Gottumukkala 	int status;
248990898850SDasaratharaman Chandramouli 	struct rdma_ah_attr *ah_attr = &attrs->ah_attr;
249047ec3866SParav Pandit 	const struct ib_gid_attr *sgid_attr;
2491a70c0739SParav Pandit 	u16 vlan_id = 0xFFFF;
2492bcf117e2SDevesh Sharma 	u8 mac_addr[6], hdr_type;
2493bcf117e2SDevesh Sharma 	union {
2494bcf117e2SDevesh Sharma 		struct sockaddr_in  _sockaddr_in;
2495bcf117e2SDevesh Sharma 		struct sockaddr_in6 _sockaddr_in6;
2496bcf117e2SDevesh Sharma 	} sgid_addr, dgid_addr;
2497d2b8f7b1SMitesh Ahuja 	struct ocrdma_dev *dev = get_ocrdma_dev(qp->ibqp.device);
2498d8966fcdSDasaratharaman Chandramouli 	const struct ib_global_route *grh;
24999c58726bSNaresh Gottumukkala 
2500d8966fcdSDasaratharaman Chandramouli 	if ((rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) == 0)
2501f99b1649SNaresh Gottumukkala 		return -EINVAL;
2502d8966fcdSDasaratharaman Chandramouli 	grh = rdma_ah_read_grh(ah_attr);
2503d2b8f7b1SMitesh Ahuja 	if (atomic_cmpxchg(&dev->update_sl, 1, 0))
2504d2b8f7b1SMitesh Ahuja 		ocrdma_init_service_level(dev);
2505fe2caefcSParav Pandit 	cmd->params.tclass_sq_psn |=
2506d8966fcdSDasaratharaman Chandramouli 	    (grh->traffic_class << OCRDMA_QP_PARAMS_TCLASS_SHIFT);
2507fe2caefcSParav Pandit 	cmd->params.rnt_rc_sl_fl |=
2508d8966fcdSDasaratharaman Chandramouli 	    (grh->flow_label & OCRDMA_QP_PARAMS_FLOW_LABEL_MASK);
2509d8966fcdSDasaratharaman Chandramouli 	cmd->params.rnt_rc_sl_fl |= (rdma_ah_get_sl(ah_attr) <<
2510d8966fcdSDasaratharaman Chandramouli 				     OCRDMA_QP_PARAMS_SL_SHIFT);
2511fe2caefcSParav Pandit 	cmd->params.hop_lmt_rq_psn |=
2512d8966fcdSDasaratharaman Chandramouli 	    (grh->hop_limit << OCRDMA_QP_PARAMS_HOP_LMT_SHIFT);
2513fe2caefcSParav Pandit 	cmd->flags |= OCRDMA_QP_PARA_FLOW_LBL_VALID;
2514bcf117e2SDevesh Sharma 
2515bcf117e2SDevesh Sharma 	/* GIDs */
2516d8966fcdSDasaratharaman Chandramouli 	memcpy(&cmd->params.dgid[0], &grh->dgid.raw[0],
2517fe2caefcSParav Pandit 	       sizeof(cmd->params.dgid));
2518dbf727deSMatan Barak 
251947ec3866SParav Pandit 	sgid_attr = ah_attr->grh.sgid_attr;
2520a70c0739SParav Pandit 	status = rdma_read_gid_l2_fields(sgid_attr, &vlan_id, &mac_addr[0]);
2521a70c0739SParav Pandit 	if (status)
2522a70c0739SParav Pandit 		return status;
25239c58726bSNaresh Gottumukkala 
2524d8966fcdSDasaratharaman Chandramouli 	qp->sgid_idx = grh->sgid_index;
252547ec3866SParav Pandit 	memcpy(&cmd->params.sgid[0], &sgid_attr->gid.raw[0],
252647ec3866SParav Pandit 	       sizeof(cmd->params.sgid));
2527d2b8f7b1SMitesh Ahuja 	status = ocrdma_resolve_dmac(dev, ah_attr, &mac_addr[0]);
2528a601dc77SDevesh Sharma 	if (status)
2529a601dc77SDevesh Sharma 		return status;
253047ec3866SParav Pandit 
2531fe2caefcSParav Pandit 	cmd->params.dmac_b0_to_b3 = mac_addr[0] | (mac_addr[1] << 8) |
2532fe2caefcSParav Pandit 				(mac_addr[2] << 16) | (mac_addr[3] << 24);
2533bcf117e2SDevesh Sharma 
253447ec3866SParav Pandit 	hdr_type = rdma_gid_attr_network_type(sgid_attr);
2535bcf117e2SDevesh Sharma 	if (hdr_type == RDMA_NETWORK_IPV4) {
2536641114d2SJason Gunthorpe 		rdma_gid2ip((struct sockaddr *)&sgid_addr, &sgid_attr->gid);
2537641114d2SJason Gunthorpe 		rdma_gid2ip((struct sockaddr *)&dgid_addr, &grh->dgid);
2538bcf117e2SDevesh Sharma 		memcpy(&cmd->params.dgid[0],
2539bcf117e2SDevesh Sharma 		       &dgid_addr._sockaddr_in.sin_addr.s_addr, 4);
2540bcf117e2SDevesh Sharma 		memcpy(&cmd->params.sgid[0],
2541bcf117e2SDevesh Sharma 		       &sgid_addr._sockaddr_in.sin_addr.s_addr, 4);
2542bcf117e2SDevesh Sharma 	}
2543fe2caefcSParav Pandit 	/* convert them to LE format. */
2544fe2caefcSParav Pandit 	ocrdma_cpu_to_le32(&cmd->params.dgid[0], sizeof(cmd->params.dgid));
2545fe2caefcSParav Pandit 	ocrdma_cpu_to_le32(&cmd->params.sgid[0], sizeof(cmd->params.sgid));
2546fe2caefcSParav Pandit 	cmd->params.vlan_dmac_b4_to_b5 = mac_addr[4] | (mac_addr[5] << 8);
2547dbf727deSMatan Barak 
2548c6002d56SDevesh Sharma 	if (vlan_id == 0xFFFF)
25496f5deab0SDevesh Sharma 		vlan_id = 0;
2550c6002d56SDevesh Sharma 	if (vlan_id || dev->pfc_state) {
2551c6002d56SDevesh Sharma 		if (!vlan_id) {
25526f5deab0SDevesh Sharma 			pr_err("ocrdma%d:Using VLAN with PFC is recommended\n",
25536f5deab0SDevesh Sharma 			       dev->id);
25546f5deab0SDevesh Sharma 			pr_err("ocrdma%d:Using VLAN 0 for this connection\n",
25556f5deab0SDevesh Sharma 			       dev->id);
25566f5deab0SDevesh Sharma 		}
2557fe2caefcSParav Pandit 		cmd->params.vlan_dmac_b4_to_b5 |=
2558fe2caefcSParav Pandit 		    vlan_id << OCRDMA_QP_PARAMS_VLAN_SHIFT;
2559fe2caefcSParav Pandit 		cmd->flags |= OCRDMA_QP_PARA_VLAN_EN_VALID;
256031dbdd9aSSelvin Xavier 		cmd->params.rnt_rc_sl_fl |=
2561d2b8f7b1SMitesh Ahuja 			(dev->sl & 0x07) << OCRDMA_QP_PARAMS_SL_SHIFT;
2562fe2caefcSParav Pandit 	}
2563bcf117e2SDevesh Sharma 	cmd->params.max_sge_recv_flags |= ((hdr_type <<
2564bcf117e2SDevesh Sharma 					OCRDMA_QP_PARAMS_FLAGS_L3_TYPE_SHIFT) &
2565bcf117e2SDevesh Sharma 					OCRDMA_QP_PARAMS_FLAGS_L3_TYPE_MASK);
2566f99b1649SNaresh Gottumukkala 	return 0;
2567fe2caefcSParav Pandit }
2568fe2caefcSParav Pandit 
ocrdma_set_qp_params(struct ocrdma_qp * qp,struct ocrdma_modify_qp * cmd,struct ib_qp_attr * attrs,int attr_mask)2569fe2caefcSParav Pandit static int ocrdma_set_qp_params(struct ocrdma_qp *qp,
2570fe2caefcSParav Pandit 				struct ocrdma_modify_qp *cmd,
2571bc1b04abSPrarit Bhargava 				struct ib_qp_attr *attrs, int attr_mask)
2572fe2caefcSParav Pandit {
2573fe2caefcSParav Pandit 	int status = 0;
2574d2b8f7b1SMitesh Ahuja 	struct ocrdma_dev *dev = get_ocrdma_dev(qp->ibqp.device);
2575fe2caefcSParav Pandit 
2576fe2caefcSParav Pandit 	if (attr_mask & IB_QP_PKEY_INDEX) {
2577fe2caefcSParav Pandit 		cmd->params.path_mtu_pkey_indx |= (attrs->pkey_index &
2578fe2caefcSParav Pandit 					    OCRDMA_QP_PARAMS_PKEY_INDEX_MASK);
2579fe2caefcSParav Pandit 		cmd->flags |= OCRDMA_QP_PARA_PKEY_VALID;
2580fe2caefcSParav Pandit 	}
2581fe2caefcSParav Pandit 	if (attr_mask & IB_QP_QKEY) {
2582fe2caefcSParav Pandit 		qp->qkey = attrs->qkey;
2583fe2caefcSParav Pandit 		cmd->params.qkey = attrs->qkey;
2584fe2caefcSParav Pandit 		cmd->flags |= OCRDMA_QP_PARA_QKEY_VALID;
2585fe2caefcSParav Pandit 	}
2586f99b1649SNaresh Gottumukkala 	if (attr_mask & IB_QP_AV) {
2587bf67472cSSelvin Xavier 		status = ocrdma_set_av_params(qp, cmd, attrs, attr_mask);
2588f99b1649SNaresh Gottumukkala 		if (status)
2589f99b1649SNaresh Gottumukkala 			return status;
2590f99b1649SNaresh Gottumukkala 	} else if (qp->qp_type == IB_QPT_GSI || qp->qp_type == IB_QPT_UD) {
2591fe2caefcSParav Pandit 		/* set the default mac address for UD, GSI QPs */
2592d2b8f7b1SMitesh Ahuja 		cmd->params.dmac_b0_to_b3 = dev->nic_info.mac_addr[0] |
2593d2b8f7b1SMitesh Ahuja 			(dev->nic_info.mac_addr[1] << 8) |
2594d2b8f7b1SMitesh Ahuja 			(dev->nic_info.mac_addr[2] << 16) |
2595d2b8f7b1SMitesh Ahuja 			(dev->nic_info.mac_addr[3] << 24);
2596d2b8f7b1SMitesh Ahuja 		cmd->params.vlan_dmac_b4_to_b5 = dev->nic_info.mac_addr[4] |
2597d2b8f7b1SMitesh Ahuja 					(dev->nic_info.mac_addr[5] << 8);
2598fe2caefcSParav Pandit 	}
2599fe2caefcSParav Pandit 	if ((attr_mask & IB_QP_EN_SQD_ASYNC_NOTIFY) &&
2600fe2caefcSParav Pandit 	    attrs->en_sqd_async_notify) {
2601fe2caefcSParav Pandit 		cmd->params.max_sge_recv_flags |=
2602fe2caefcSParav Pandit 			OCRDMA_QP_PARAMS_FLAGS_SQD_ASYNC;
2603fe2caefcSParav Pandit 		cmd->flags |= OCRDMA_QP_PARA_DST_QPN_VALID;
2604fe2caefcSParav Pandit 	}
2605fe2caefcSParav Pandit 	if (attr_mask & IB_QP_DEST_QPN) {
2606fe2caefcSParav Pandit 		cmd->params.ack_to_rnr_rtc_dest_qpn |= (attrs->dest_qp_num &
2607fe2caefcSParav Pandit 				OCRDMA_QP_PARAMS_DEST_QPN_MASK);
2608fe2caefcSParav Pandit 		cmd->flags |= OCRDMA_QP_PARA_DST_QPN_VALID;
2609fe2caefcSParav Pandit 	}
2610fe2caefcSParav Pandit 	if (attr_mask & IB_QP_PATH_MTU) {
261172d8a013SNaga Irrinki 		if (attrs->path_mtu < IB_MTU_512 ||
2612d3cb6c0bSNaresh Gottumukkala 		    attrs->path_mtu > IB_MTU_4096) {
261372d8a013SNaga Irrinki 			pr_err("ocrdma%d: IB MTU %d is not supported\n",
261472d8a013SNaga Irrinki 			       dev->id, ib_mtu_enum_to_int(attrs->path_mtu));
2615fe2caefcSParav Pandit 			status = -EINVAL;
2616fe2caefcSParav Pandit 			goto pmtu_err;
2617fe2caefcSParav Pandit 		}
2618fe2caefcSParav Pandit 		cmd->params.path_mtu_pkey_indx |=
2619fe2caefcSParav Pandit 		    (ib_mtu_enum_to_int(attrs->path_mtu) <<
2620fe2caefcSParav Pandit 		     OCRDMA_QP_PARAMS_PATH_MTU_SHIFT) &
2621fe2caefcSParav Pandit 		    OCRDMA_QP_PARAMS_PATH_MTU_MASK;
2622fe2caefcSParav Pandit 		cmd->flags |= OCRDMA_QP_PARA_PMTU_VALID;
2623fe2caefcSParav Pandit 	}
2624fe2caefcSParav Pandit 	if (attr_mask & IB_QP_TIMEOUT) {
2625fe2caefcSParav Pandit 		cmd->params.ack_to_rnr_rtc_dest_qpn |= attrs->timeout <<
2626fe2caefcSParav Pandit 		    OCRDMA_QP_PARAMS_ACK_TIMEOUT_SHIFT;
2627fe2caefcSParav Pandit 		cmd->flags |= OCRDMA_QP_PARA_ACK_TO_VALID;
2628fe2caefcSParav Pandit 	}
2629fe2caefcSParav Pandit 	if (attr_mask & IB_QP_RETRY_CNT) {
2630fe2caefcSParav Pandit 		cmd->params.rnt_rc_sl_fl |= (attrs->retry_cnt <<
2631fe2caefcSParav Pandit 				      OCRDMA_QP_PARAMS_RETRY_CNT_SHIFT) &
2632fe2caefcSParav Pandit 		    OCRDMA_QP_PARAMS_RETRY_CNT_MASK;
2633fe2caefcSParav Pandit 		cmd->flags |= OCRDMA_QP_PARA_RETRY_CNT_VALID;
2634fe2caefcSParav Pandit 	}
2635fe2caefcSParav Pandit 	if (attr_mask & IB_QP_MIN_RNR_TIMER) {
2636fe2caefcSParav Pandit 		cmd->params.rnt_rc_sl_fl |= (attrs->min_rnr_timer <<
2637fe2caefcSParav Pandit 				      OCRDMA_QP_PARAMS_RNR_NAK_TIMER_SHIFT) &
2638fe2caefcSParav Pandit 		    OCRDMA_QP_PARAMS_RNR_NAK_TIMER_MASK;
2639fe2caefcSParav Pandit 		cmd->flags |= OCRDMA_QP_PARA_RNT_VALID;
2640fe2caefcSParav Pandit 	}
2641fe2caefcSParav Pandit 	if (attr_mask & IB_QP_RNR_RETRY) {
2642fe2caefcSParav Pandit 		cmd->params.ack_to_rnr_rtc_dest_qpn |= (attrs->rnr_retry <<
2643fe2caefcSParav Pandit 			OCRDMA_QP_PARAMS_RNR_RETRY_CNT_SHIFT)
2644fe2caefcSParav Pandit 			& OCRDMA_QP_PARAMS_RNR_RETRY_CNT_MASK;
2645fe2caefcSParav Pandit 		cmd->flags |= OCRDMA_QP_PARA_RRC_VALID;
2646fe2caefcSParav Pandit 	}
2647fe2caefcSParav Pandit 	if (attr_mask & IB_QP_SQ_PSN) {
2648fe2caefcSParav Pandit 		cmd->params.tclass_sq_psn |= (attrs->sq_psn & 0x00ffffff);
2649fe2caefcSParav Pandit 		cmd->flags |= OCRDMA_QP_PARA_SQPSN_VALID;
2650fe2caefcSParav Pandit 	}
2651fe2caefcSParav Pandit 	if (attr_mask & IB_QP_RQ_PSN) {
2652fe2caefcSParav Pandit 		cmd->params.hop_lmt_rq_psn |= (attrs->rq_psn & 0x00ffffff);
2653fe2caefcSParav Pandit 		cmd->flags |= OCRDMA_QP_PARA_RQPSN_VALID;
2654fe2caefcSParav Pandit 	}
2655fe2caefcSParav Pandit 	if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
2656d2b8f7b1SMitesh Ahuja 		if (attrs->max_rd_atomic > dev->attr.max_ord_per_qp) {
2657fe2caefcSParav Pandit 			status = -EINVAL;
2658fe2caefcSParav Pandit 			goto pmtu_err;
2659fe2caefcSParav Pandit 		}
2660fe2caefcSParav Pandit 		qp->max_ord = attrs->max_rd_atomic;
2661fe2caefcSParav Pandit 		cmd->flags |= OCRDMA_QP_PARA_MAX_ORD_VALID;
2662fe2caefcSParav Pandit 	}
2663fe2caefcSParav Pandit 	if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
2664d2b8f7b1SMitesh Ahuja 		if (attrs->max_dest_rd_atomic > dev->attr.max_ird_per_qp) {
2665fe2caefcSParav Pandit 			status = -EINVAL;
2666fe2caefcSParav Pandit 			goto pmtu_err;
2667fe2caefcSParav Pandit 		}
2668fe2caefcSParav Pandit 		qp->max_ird = attrs->max_dest_rd_atomic;
2669fe2caefcSParav Pandit 		cmd->flags |= OCRDMA_QP_PARA_MAX_IRD_VALID;
2670fe2caefcSParav Pandit 	}
2671fe2caefcSParav Pandit 	cmd->params.max_ord_ird = (qp->max_ord <<
2672fe2caefcSParav Pandit 				OCRDMA_QP_PARAMS_MAX_ORD_SHIFT) |
2673fe2caefcSParav Pandit 				(qp->max_ird & OCRDMA_QP_PARAMS_MAX_IRD_MASK);
2674fe2caefcSParav Pandit pmtu_err:
2675fe2caefcSParav Pandit 	return status;
2676fe2caefcSParav Pandit }
2677fe2caefcSParav Pandit 
ocrdma_mbx_modify_qp(struct ocrdma_dev * dev,struct ocrdma_qp * qp,struct ib_qp_attr * attrs,int attr_mask)2678fe2caefcSParav Pandit int ocrdma_mbx_modify_qp(struct ocrdma_dev *dev, struct ocrdma_qp *qp,
2679bc1b04abSPrarit Bhargava 			 struct ib_qp_attr *attrs, int attr_mask)
2680fe2caefcSParav Pandit {
2681fe2caefcSParav Pandit 	int status = -ENOMEM;
2682fe2caefcSParav Pandit 	struct ocrdma_modify_qp *cmd;
2683fe2caefcSParav Pandit 
2684fe2caefcSParav Pandit 	cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_MODIFY_QP, sizeof(*cmd));
2685fe2caefcSParav Pandit 	if (!cmd)
2686fe2caefcSParav Pandit 		return status;
2687fe2caefcSParav Pandit 
2688fe2caefcSParav Pandit 	cmd->params.id = qp->id;
2689fe2caefcSParav Pandit 	cmd->flags = 0;
2690fe2caefcSParav Pandit 	if (attr_mask & IB_QP_STATE) {
2691fe2caefcSParav Pandit 		cmd->params.max_sge_recv_flags |=
2692fe2caefcSParav Pandit 		    (get_ocrdma_qp_state(attrs->qp_state) <<
2693fe2caefcSParav Pandit 		     OCRDMA_QP_PARAMS_STATE_SHIFT) &
2694fe2caefcSParav Pandit 		    OCRDMA_QP_PARAMS_STATE_MASK;
2695fe2caefcSParav Pandit 		cmd->flags |= OCRDMA_QP_PARA_QPS_VALID;
2696f99b1649SNaresh Gottumukkala 	} else {
2697fe2caefcSParav Pandit 		cmd->params.max_sge_recv_flags |=
2698fe2caefcSParav Pandit 		    (qp->state << OCRDMA_QP_PARAMS_STATE_SHIFT) &
2699fe2caefcSParav Pandit 		    OCRDMA_QP_PARAMS_STATE_MASK;
2700f99b1649SNaresh Gottumukkala 	}
2701f99b1649SNaresh Gottumukkala 
2702bc1b04abSPrarit Bhargava 	status = ocrdma_set_qp_params(qp, cmd, attrs, attr_mask);
2703fe2caefcSParav Pandit 	if (status)
2704fe2caefcSParav Pandit 		goto mbx_err;
2705fe2caefcSParav Pandit 	status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
2706fe2caefcSParav Pandit 	if (status)
2707fe2caefcSParav Pandit 		goto mbx_err;
2708c592c423SRoland Dreier 
2709fe2caefcSParav Pandit mbx_err:
2710fe2caefcSParav Pandit 	kfree(cmd);
2711fe2caefcSParav Pandit 	return status;
2712fe2caefcSParav Pandit }
2713fe2caefcSParav Pandit 
ocrdma_mbx_destroy_qp(struct ocrdma_dev * dev,struct ocrdma_qp * qp)2714fe2caefcSParav Pandit int ocrdma_mbx_destroy_qp(struct ocrdma_dev *dev, struct ocrdma_qp *qp)
2715fe2caefcSParav Pandit {
2716fe2caefcSParav Pandit 	int status = -ENOMEM;
2717fe2caefcSParav Pandit 	struct ocrdma_destroy_qp *cmd;
2718fe2caefcSParav Pandit 	struct pci_dev *pdev = dev->nic_info.pdev;
2719fe2caefcSParav Pandit 
2720fe2caefcSParav Pandit 	cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_DELETE_QP, sizeof(*cmd));
2721fe2caefcSParav Pandit 	if (!cmd)
2722fe2caefcSParav Pandit 		return status;
2723fe2caefcSParav Pandit 	cmd->qp_id = qp->id;
2724fe2caefcSParav Pandit 	status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
2725fe2caefcSParav Pandit 	if (status)
2726fe2caefcSParav Pandit 		goto mbx_err;
2727c592c423SRoland Dreier 
2728fe2caefcSParav Pandit mbx_err:
2729fe2caefcSParav Pandit 	kfree(cmd);
2730fe2caefcSParav Pandit 	if (qp->sq.va)
2731fe2caefcSParav Pandit 		dma_free_coherent(&pdev->dev, qp->sq.len, qp->sq.va, qp->sq.pa);
2732fe2caefcSParav Pandit 	if (!qp->srq && qp->rq.va)
2733fe2caefcSParav Pandit 		dma_free_coherent(&pdev->dev, qp->rq.len, qp->rq.va, qp->rq.pa);
2734fe2caefcSParav Pandit 	if (qp->dpp_enabled)
2735fe2caefcSParav Pandit 		qp->pd->num_dpp_qp++;
2736fe2caefcSParav Pandit 	return status;
2737fe2caefcSParav Pandit }
2738fe2caefcSParav Pandit 
ocrdma_mbx_create_srq(struct ocrdma_dev * dev,struct ocrdma_srq * srq,struct ib_srq_init_attr * srq_attr,struct ocrdma_pd * pd)27391afc0454SNaresh Gottumukkala int ocrdma_mbx_create_srq(struct ocrdma_dev *dev, struct ocrdma_srq *srq,
2740fe2caefcSParav Pandit 			  struct ib_srq_init_attr *srq_attr,
2741fe2caefcSParav Pandit 			  struct ocrdma_pd *pd)
2742fe2caefcSParav Pandit {
2743fe2caefcSParav Pandit 	int status = -ENOMEM;
2744fe2caefcSParav Pandit 	int hw_pages, hw_page_size;
2745fe2caefcSParav Pandit 	int len;
2746fe2caefcSParav Pandit 	struct ocrdma_create_srq_rsp *rsp;
2747fe2caefcSParav Pandit 	struct ocrdma_create_srq *cmd;
2748fe2caefcSParav Pandit 	dma_addr_t pa;
2749fe2caefcSParav Pandit 	struct pci_dev *pdev = dev->nic_info.pdev;
2750fe2caefcSParav Pandit 	u32 max_rqe_allocated;
2751fe2caefcSParav Pandit 
2752fe2caefcSParav Pandit 	cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_CREATE_SRQ, sizeof(*cmd));
2753fe2caefcSParav Pandit 	if (!cmd)
2754fe2caefcSParav Pandit 		return status;
2755fe2caefcSParav Pandit 
2756fe2caefcSParav Pandit 	cmd->pgsz_pdid = pd->id & OCRDMA_CREATE_SRQ_PD_ID_MASK;
2757fe2caefcSParav Pandit 	max_rqe_allocated = srq_attr->attr.max_wr + 1;
2758fe2caefcSParav Pandit 	status = ocrdma_build_q_conf(&max_rqe_allocated,
2759fe2caefcSParav Pandit 				dev->attr.rqe_size,
2760fe2caefcSParav Pandit 				&hw_pages, &hw_page_size);
2761fe2caefcSParav Pandit 	if (status) {
2762ef99c4c2SNaresh Gottumukkala 		pr_err("%s() req. max_wr=0x%x\n", __func__,
2763fe2caefcSParav Pandit 		       srq_attr->attr.max_wr);
2764fe2caefcSParav Pandit 		status = -EINVAL;
2765fe2caefcSParav Pandit 		goto ret;
2766fe2caefcSParav Pandit 	}
2767fe2caefcSParav Pandit 	len = hw_pages * hw_page_size;
2768fe2caefcSParav Pandit 	srq->rq.va = dma_alloc_coherent(&pdev->dev, len, &pa, GFP_KERNEL);
2769fe2caefcSParav Pandit 	if (!srq->rq.va) {
2770fe2caefcSParav Pandit 		status = -ENOMEM;
2771fe2caefcSParav Pandit 		goto ret;
2772fe2caefcSParav Pandit 	}
2773fe2caefcSParav Pandit 	ocrdma_build_q_pages(&cmd->rq_addr[0], hw_pages, pa, hw_page_size);
2774fe2caefcSParav Pandit 
2775fe2caefcSParav Pandit 	srq->rq.entry_size = dev->attr.rqe_size;
2776fe2caefcSParav Pandit 	srq->rq.pa = pa;
2777fe2caefcSParav Pandit 	srq->rq.len = len;
2778fe2caefcSParav Pandit 	srq->rq.max_cnt = max_rqe_allocated;
2779fe2caefcSParav Pandit 
2780fe2caefcSParav Pandit 	cmd->max_sge_rqe = ilog2(max_rqe_allocated);
2781fe2caefcSParav Pandit 	cmd->max_sge_rqe |= srq_attr->attr.max_sge <<
2782fe2caefcSParav Pandit 				OCRDMA_CREATE_SRQ_MAX_SGE_RECV_SHIFT;
2783fe2caefcSParav Pandit 
2784fe2caefcSParav Pandit 	cmd->pgsz_pdid |= (ilog2(hw_page_size / OCRDMA_MIN_Q_PAGE_SIZE)
2785fe2caefcSParav Pandit 		<< OCRDMA_CREATE_SRQ_PG_SZ_SHIFT);
2786fe2caefcSParav Pandit 	cmd->pages_rqe_sz |= (dev->attr.rqe_size
2787fe2caefcSParav Pandit 		<< OCRDMA_CREATE_SRQ_RQE_SIZE_SHIFT)
2788fe2caefcSParav Pandit 		& OCRDMA_CREATE_SRQ_RQE_SIZE_MASK;
2789fe2caefcSParav Pandit 	cmd->pages_rqe_sz |= hw_pages << OCRDMA_CREATE_SRQ_NUM_RQ_PAGES_SHIFT;
2790fe2caefcSParav Pandit 
2791fe2caefcSParav Pandit 	status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
2792fe2caefcSParav Pandit 	if (status)
2793fe2caefcSParav Pandit 		goto mbx_err;
2794fe2caefcSParav Pandit 	rsp = (struct ocrdma_create_srq_rsp *)cmd;
2795fe2caefcSParav Pandit 	srq->id = rsp->id;
2796fe2caefcSParav Pandit 	srq->rq.dbid = rsp->id;
2797fe2caefcSParav Pandit 	max_rqe_allocated = ((rsp->max_sge_rqe_allocated &
2798fe2caefcSParav Pandit 		OCRDMA_CREATE_SRQ_RSP_MAX_RQE_ALLOCATED_MASK) >>
2799fe2caefcSParav Pandit 		OCRDMA_CREATE_SRQ_RSP_MAX_RQE_ALLOCATED_SHIFT);
2800fe2caefcSParav Pandit 	max_rqe_allocated = (1 << max_rqe_allocated);
2801fe2caefcSParav Pandit 	srq->rq.max_cnt = max_rqe_allocated;
2802fe2caefcSParav Pandit 	srq->rq.max_wqe_idx = max_rqe_allocated - 1;
2803fe2caefcSParav Pandit 	srq->rq.max_sges = (rsp->max_sge_rqe_allocated &
2804fe2caefcSParav Pandit 		OCRDMA_CREATE_SRQ_RSP_MAX_SGE_RECV_ALLOCATED_MASK) >>
2805fe2caefcSParav Pandit 		OCRDMA_CREATE_SRQ_RSP_MAX_SGE_RECV_ALLOCATED_SHIFT;
2806fe2caefcSParav Pandit 	goto ret;
2807fe2caefcSParav Pandit mbx_err:
2808fe2caefcSParav Pandit 	dma_free_coherent(&pdev->dev, srq->rq.len, srq->rq.va, pa);
2809fe2caefcSParav Pandit ret:
2810fe2caefcSParav Pandit 	kfree(cmd);
2811fe2caefcSParav Pandit 	return status;
2812fe2caefcSParav Pandit }
2813fe2caefcSParav Pandit 
ocrdma_mbx_modify_srq(struct ocrdma_srq * srq,struct ib_srq_attr * srq_attr)2814fe2caefcSParav Pandit int ocrdma_mbx_modify_srq(struct ocrdma_srq *srq, struct ib_srq_attr *srq_attr)
2815fe2caefcSParav Pandit {
2816fe2caefcSParav Pandit 	int status = -ENOMEM;
2817fe2caefcSParav Pandit 	struct ocrdma_modify_srq *cmd;
2818f11220eeSNaresh Gottumukkala 	struct ocrdma_pd *pd = srq->pd;
2819f11220eeSNaresh Gottumukkala 	struct ocrdma_dev *dev = get_ocrdma_dev(pd->ibpd.device);
28201afc0454SNaresh Gottumukkala 
2821d7e19c0aSNaresh Gottumukkala 	cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_MODIFY_SRQ, sizeof(*cmd));
2822fe2caefcSParav Pandit 	if (!cmd)
2823fe2caefcSParav Pandit 		return status;
2824fe2caefcSParav Pandit 	cmd->id = srq->id;
2825fe2caefcSParav Pandit 	cmd->limit_max_rqe |= srq_attr->srq_limit <<
2826fe2caefcSParav Pandit 	    OCRDMA_MODIFY_SRQ_LIMIT_SHIFT;
28271afc0454SNaresh Gottumukkala 	status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
2828fe2caefcSParav Pandit 	kfree(cmd);
2829fe2caefcSParav Pandit 	return status;
2830fe2caefcSParav Pandit }
2831fe2caefcSParav Pandit 
ocrdma_mbx_query_srq(struct ocrdma_srq * srq,struct ib_srq_attr * srq_attr)2832fe2caefcSParav Pandit int ocrdma_mbx_query_srq(struct ocrdma_srq *srq, struct ib_srq_attr *srq_attr)
2833fe2caefcSParav Pandit {
2834fe2caefcSParav Pandit 	int status = -ENOMEM;
2835fe2caefcSParav Pandit 	struct ocrdma_query_srq *cmd;
28361afc0454SNaresh Gottumukkala 	struct ocrdma_dev *dev = get_ocrdma_dev(srq->ibsrq.device);
28371afc0454SNaresh Gottumukkala 
2838d7e19c0aSNaresh Gottumukkala 	cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_QUERY_SRQ, sizeof(*cmd));
2839fe2caefcSParav Pandit 	if (!cmd)
2840fe2caefcSParav Pandit 		return status;
2841fe2caefcSParav Pandit 	cmd->id = srq->rq.dbid;
28421afc0454SNaresh Gottumukkala 	status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
2843fe2caefcSParav Pandit 	if (status == 0) {
2844fe2caefcSParav Pandit 		struct ocrdma_query_srq_rsp *rsp =
2845fe2caefcSParav Pandit 		    (struct ocrdma_query_srq_rsp *)cmd;
2846fe2caefcSParav Pandit 		srq_attr->max_sge =
2847fe2caefcSParav Pandit 		    rsp->srq_lmt_max_sge &
2848fe2caefcSParav Pandit 		    OCRDMA_QUERY_SRQ_RSP_MAX_SGE_RECV_MASK;
2849fe2caefcSParav Pandit 		srq_attr->max_wr =
2850fe2caefcSParav Pandit 		    rsp->max_rqe_pdid >> OCRDMA_QUERY_SRQ_RSP_MAX_RQE_SHIFT;
2851fe2caefcSParav Pandit 		srq_attr->srq_limit = rsp->srq_lmt_max_sge >>
2852fe2caefcSParav Pandit 		    OCRDMA_QUERY_SRQ_RSP_SRQ_LIMIT_SHIFT;
2853fe2caefcSParav Pandit 	}
2854fe2caefcSParav Pandit 	kfree(cmd);
2855fe2caefcSParav Pandit 	return status;
2856fe2caefcSParav Pandit }
2857fe2caefcSParav Pandit 
ocrdma_mbx_destroy_srq(struct ocrdma_dev * dev,struct ocrdma_srq * srq)285868e326deSLeon Romanovsky void ocrdma_mbx_destroy_srq(struct ocrdma_dev *dev, struct ocrdma_srq *srq)
2859fe2caefcSParav Pandit {
2860fe2caefcSParav Pandit 	struct ocrdma_destroy_srq *cmd;
2861fe2caefcSParav Pandit 	struct pci_dev *pdev = dev->nic_info.pdev;
2862fe2caefcSParav Pandit 	cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_DELETE_SRQ, sizeof(*cmd));
2863fe2caefcSParav Pandit 	if (!cmd)
286468e326deSLeon Romanovsky 		return;
2865fe2caefcSParav Pandit 	cmd->id = srq->id;
286668e326deSLeon Romanovsky 	ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
2867fe2caefcSParav Pandit 	if (srq->rq.va)
2868fe2caefcSParav Pandit 		dma_free_coherent(&pdev->dev, srq->rq.len,
2869fe2caefcSParav Pandit 				  srq->rq.va, srq->rq.pa);
2870fe2caefcSParav Pandit 	kfree(cmd);
2871fe2caefcSParav Pandit }
2872fe2caefcSParav Pandit 
ocrdma_mbx_get_dcbx_config(struct ocrdma_dev * dev,u32 ptype,struct ocrdma_dcbx_cfg * dcbxcfg)287331dbdd9aSSelvin Xavier static int ocrdma_mbx_get_dcbx_config(struct ocrdma_dev *dev, u32 ptype,
287431dbdd9aSSelvin Xavier 				      struct ocrdma_dcbx_cfg *dcbxcfg)
287531dbdd9aSSelvin Xavier {
28760ca4c39fSMarkus Elfring 	int status;
287731dbdd9aSSelvin Xavier 	dma_addr_t pa;
287831dbdd9aSSelvin Xavier 	struct ocrdma_mqe cmd;
287931dbdd9aSSelvin Xavier 
288031dbdd9aSSelvin Xavier 	struct ocrdma_get_dcbx_cfg_req *req = NULL;
288131dbdd9aSSelvin Xavier 	struct ocrdma_get_dcbx_cfg_rsp *rsp = NULL;
288231dbdd9aSSelvin Xavier 	struct pci_dev *pdev = dev->nic_info.pdev;
288331dbdd9aSSelvin Xavier 	struct ocrdma_mqe_sge *mqe_sge = cmd.u.nonemb_req.sge;
288431dbdd9aSSelvin Xavier 
288531dbdd9aSSelvin Xavier 	memset(&cmd, 0, sizeof(struct ocrdma_mqe));
288631dbdd9aSSelvin Xavier 	cmd.hdr.pyld_len = max_t (u32, sizeof(struct ocrdma_get_dcbx_cfg_rsp),
288731dbdd9aSSelvin Xavier 					sizeof(struct ocrdma_get_dcbx_cfg_req));
288831dbdd9aSSelvin Xavier 	req = dma_alloc_coherent(&pdev->dev, cmd.hdr.pyld_len, &pa, GFP_KERNEL);
288931dbdd9aSSelvin Xavier 	if (!req) {
289031dbdd9aSSelvin Xavier 		status = -ENOMEM;
289131dbdd9aSSelvin Xavier 		goto mem_err;
289231dbdd9aSSelvin Xavier 	}
289331dbdd9aSSelvin Xavier 
289431dbdd9aSSelvin Xavier 	cmd.hdr.spcl_sge_cnt_emb |= (1 << OCRDMA_MQE_HDR_SGE_CNT_SHIFT) &
289531dbdd9aSSelvin Xavier 					OCRDMA_MQE_HDR_SGE_CNT_MASK;
289631dbdd9aSSelvin Xavier 	mqe_sge->pa_lo = (u32) (pa & 0xFFFFFFFFUL);
289731dbdd9aSSelvin Xavier 	mqe_sge->pa_hi = (u32) upper_32_bits(pa);
289831dbdd9aSSelvin Xavier 	mqe_sge->len = cmd.hdr.pyld_len;
289931dbdd9aSSelvin Xavier 
290031dbdd9aSSelvin Xavier 	ocrdma_init_mch(&req->hdr, OCRDMA_CMD_GET_DCBX_CONFIG,
290131dbdd9aSSelvin Xavier 			OCRDMA_SUBSYS_DCBX, cmd.hdr.pyld_len);
290231dbdd9aSSelvin Xavier 	req->param_type = ptype;
290331dbdd9aSSelvin Xavier 
290431dbdd9aSSelvin Xavier 	status = ocrdma_mbx_cmd(dev, &cmd);
290531dbdd9aSSelvin Xavier 	if (status)
290631dbdd9aSSelvin Xavier 		goto mbx_err;
290731dbdd9aSSelvin Xavier 
290831dbdd9aSSelvin Xavier 	rsp = (struct ocrdma_get_dcbx_cfg_rsp *)req;
290931dbdd9aSSelvin Xavier 	ocrdma_le32_to_cpu(rsp, sizeof(struct ocrdma_get_dcbx_cfg_rsp));
291031dbdd9aSSelvin Xavier 	memcpy(dcbxcfg, &rsp->cfg, sizeof(struct ocrdma_dcbx_cfg));
291131dbdd9aSSelvin Xavier 
291231dbdd9aSSelvin Xavier mbx_err:
291331dbdd9aSSelvin Xavier 	dma_free_coherent(&pdev->dev, cmd.hdr.pyld_len, req, pa);
291431dbdd9aSSelvin Xavier mem_err:
291531dbdd9aSSelvin Xavier 	return status;
291631dbdd9aSSelvin Xavier }
291731dbdd9aSSelvin Xavier 
291831dbdd9aSSelvin Xavier #define OCRDMA_MAX_SERVICE_LEVEL_INDEX	0x08
291931dbdd9aSSelvin Xavier #define OCRDMA_DEFAULT_SERVICE_LEVEL	0x05
292031dbdd9aSSelvin Xavier 
ocrdma_parse_dcbxcfg_rsp(struct ocrdma_dev * dev,int ptype,struct ocrdma_dcbx_cfg * dcbxcfg,u8 * srvc_lvl)292131dbdd9aSSelvin Xavier static int ocrdma_parse_dcbxcfg_rsp(struct ocrdma_dev *dev, int ptype,
292231dbdd9aSSelvin Xavier 				    struct ocrdma_dcbx_cfg *dcbxcfg,
292331dbdd9aSSelvin Xavier 				    u8 *srvc_lvl)
292431dbdd9aSSelvin Xavier {
292531dbdd9aSSelvin Xavier 	int status = -EINVAL, indx, slindx;
292631dbdd9aSSelvin Xavier 	int ventry_cnt;
292731dbdd9aSSelvin Xavier 	struct ocrdma_app_parameter *app_param;
292831dbdd9aSSelvin Xavier 	u8 valid, proto_sel;
292931dbdd9aSSelvin Xavier 	u8 app_prio, pfc_prio;
293031dbdd9aSSelvin Xavier 	u16 proto;
293131dbdd9aSSelvin Xavier 
293231dbdd9aSSelvin Xavier 	if (!(dcbxcfg->tcv_aev_opv_st & OCRDMA_DCBX_STATE_MASK)) {
293331dbdd9aSSelvin Xavier 		pr_info("%s ocrdma%d DCBX is disabled\n",
293431dbdd9aSSelvin Xavier 			dev_name(&dev->nic_info.pdev->dev), dev->id);
293531dbdd9aSSelvin Xavier 		goto out;
293631dbdd9aSSelvin Xavier 	}
293731dbdd9aSSelvin Xavier 
293831dbdd9aSSelvin Xavier 	if (!ocrdma_is_enabled_and_synced(dcbxcfg->pfc_state)) {
293931dbdd9aSSelvin Xavier 		pr_info("%s ocrdma%d priority flow control(%s) is %s%s\n",
294031dbdd9aSSelvin Xavier 			dev_name(&dev->nic_info.pdev->dev), dev->id,
294131dbdd9aSSelvin Xavier 			(ptype > 0 ? "operational" : "admin"),
294231dbdd9aSSelvin Xavier 			(dcbxcfg->pfc_state & OCRDMA_STATE_FLAG_ENABLED) ?
294331dbdd9aSSelvin Xavier 			"enabled" : "disabled",
294431dbdd9aSSelvin Xavier 			(dcbxcfg->pfc_state & OCRDMA_STATE_FLAG_SYNC) ?
294531dbdd9aSSelvin Xavier 			"" : ", not sync'ed");
294631dbdd9aSSelvin Xavier 		goto out;
294731dbdd9aSSelvin Xavier 	} else {
294831dbdd9aSSelvin Xavier 		pr_info("%s ocrdma%d priority flow control is enabled and sync'ed\n",
294931dbdd9aSSelvin Xavier 			dev_name(&dev->nic_info.pdev->dev), dev->id);
295031dbdd9aSSelvin Xavier 	}
295131dbdd9aSSelvin Xavier 
295231dbdd9aSSelvin Xavier 	ventry_cnt = (dcbxcfg->tcv_aev_opv_st >>
295331dbdd9aSSelvin Xavier 				OCRDMA_DCBX_APP_ENTRY_SHIFT)
295431dbdd9aSSelvin Xavier 				& OCRDMA_DCBX_STATE_MASK;
295531dbdd9aSSelvin Xavier 
295631dbdd9aSSelvin Xavier 	for (indx = 0; indx < ventry_cnt; indx++) {
295731dbdd9aSSelvin Xavier 		app_param = &dcbxcfg->app_param[indx];
295831dbdd9aSSelvin Xavier 		valid = (app_param->valid_proto_app >>
295931dbdd9aSSelvin Xavier 				OCRDMA_APP_PARAM_VALID_SHIFT)
296031dbdd9aSSelvin Xavier 				& OCRDMA_APP_PARAM_VALID_MASK;
296131dbdd9aSSelvin Xavier 		proto_sel = (app_param->valid_proto_app
296231dbdd9aSSelvin Xavier 				>>  OCRDMA_APP_PARAM_PROTO_SEL_SHIFT)
296331dbdd9aSSelvin Xavier 				& OCRDMA_APP_PARAM_PROTO_SEL_MASK;
296431dbdd9aSSelvin Xavier 		proto = app_param->valid_proto_app &
296531dbdd9aSSelvin Xavier 				OCRDMA_APP_PARAM_APP_PROTO_MASK;
296631dbdd9aSSelvin Xavier 
296731dbdd9aSSelvin Xavier 		if (
296869ae5439SSelvin Xavier 			valid && proto == ETH_P_IBOE &&
296931dbdd9aSSelvin Xavier 			proto_sel == OCRDMA_PROTO_SELECT_L2) {
297031dbdd9aSSelvin Xavier 			for (slindx = 0; slindx <
297131dbdd9aSSelvin Xavier 				OCRDMA_MAX_SERVICE_LEVEL_INDEX; slindx++) {
297231dbdd9aSSelvin Xavier 				app_prio = ocrdma_get_app_prio(
297331dbdd9aSSelvin Xavier 						(u8 *)app_param->app_prio,
297431dbdd9aSSelvin Xavier 						slindx);
297531dbdd9aSSelvin Xavier 				pfc_prio = ocrdma_get_pfc_prio(
297631dbdd9aSSelvin Xavier 						(u8 *)dcbxcfg->pfc_prio,
297731dbdd9aSSelvin Xavier 						slindx);
297831dbdd9aSSelvin Xavier 
297931dbdd9aSSelvin Xavier 				if (app_prio && pfc_prio) {
298031dbdd9aSSelvin Xavier 					*srvc_lvl = slindx;
298131dbdd9aSSelvin Xavier 					status = 0;
298231dbdd9aSSelvin Xavier 					goto out;
298331dbdd9aSSelvin Xavier 				}
298431dbdd9aSSelvin Xavier 			}
298531dbdd9aSSelvin Xavier 			if (slindx == OCRDMA_MAX_SERVICE_LEVEL_INDEX) {
298631dbdd9aSSelvin Xavier 				pr_info("%s ocrdma%d application priority not set for 0x%x protocol\n",
298731dbdd9aSSelvin Xavier 					dev_name(&dev->nic_info.pdev->dev),
298831dbdd9aSSelvin Xavier 					dev->id, proto);
298931dbdd9aSSelvin Xavier 			}
299031dbdd9aSSelvin Xavier 		}
299131dbdd9aSSelvin Xavier 	}
299231dbdd9aSSelvin Xavier 
299331dbdd9aSSelvin Xavier out:
299431dbdd9aSSelvin Xavier 	return status;
299531dbdd9aSSelvin Xavier }
299631dbdd9aSSelvin Xavier 
ocrdma_init_service_level(struct ocrdma_dev * dev)299731dbdd9aSSelvin Xavier void ocrdma_init_service_level(struct ocrdma_dev *dev)
299831dbdd9aSSelvin Xavier {
299931dbdd9aSSelvin Xavier 	int status = 0, indx;
300031dbdd9aSSelvin Xavier 	struct ocrdma_dcbx_cfg dcbxcfg;
300131dbdd9aSSelvin Xavier 	u8 srvc_lvl = OCRDMA_DEFAULT_SERVICE_LEVEL;
300231dbdd9aSSelvin Xavier 	int ptype = OCRDMA_PARAMETER_TYPE_OPER;
300331dbdd9aSSelvin Xavier 
300431dbdd9aSSelvin Xavier 	for (indx = 0; indx < 2; indx++) {
300531dbdd9aSSelvin Xavier 		status = ocrdma_mbx_get_dcbx_config(dev, ptype, &dcbxcfg);
300631dbdd9aSSelvin Xavier 		if (status) {
300731dbdd9aSSelvin Xavier 			pr_err("%s(): status=%d\n", __func__, status);
300831dbdd9aSSelvin Xavier 			ptype = OCRDMA_PARAMETER_TYPE_ADMIN;
300931dbdd9aSSelvin Xavier 			continue;
301031dbdd9aSSelvin Xavier 		}
301131dbdd9aSSelvin Xavier 
301231dbdd9aSSelvin Xavier 		status = ocrdma_parse_dcbxcfg_rsp(dev, ptype,
301331dbdd9aSSelvin Xavier 						  &dcbxcfg, &srvc_lvl);
301431dbdd9aSSelvin Xavier 		if (status) {
301531dbdd9aSSelvin Xavier 			ptype = OCRDMA_PARAMETER_TYPE_ADMIN;
301631dbdd9aSSelvin Xavier 			continue;
301731dbdd9aSSelvin Xavier 		}
301831dbdd9aSSelvin Xavier 
301931dbdd9aSSelvin Xavier 		break;
302031dbdd9aSSelvin Xavier 	}
302131dbdd9aSSelvin Xavier 
302231dbdd9aSSelvin Xavier 	if (status)
302331dbdd9aSSelvin Xavier 		pr_info("%s ocrdma%d service level default\n",
302431dbdd9aSSelvin Xavier 			dev_name(&dev->nic_info.pdev->dev), dev->id);
302531dbdd9aSSelvin Xavier 	else
302631dbdd9aSSelvin Xavier 		pr_info("%s ocrdma%d service level %d\n",
302731dbdd9aSSelvin Xavier 			dev_name(&dev->nic_info.pdev->dev), dev->id,
302831dbdd9aSSelvin Xavier 			srvc_lvl);
302931dbdd9aSSelvin Xavier 
303031dbdd9aSSelvin Xavier 	dev->pfc_state = ocrdma_is_enabled_and_synced(dcbxcfg.pfc_state);
303131dbdd9aSSelvin Xavier 	dev->sl = srvc_lvl;
303231dbdd9aSSelvin Xavier }
303331dbdd9aSSelvin Xavier 
ocrdma_alloc_av(struct ocrdma_dev * dev,struct ocrdma_ah * ah)3034fe2caefcSParav Pandit int ocrdma_alloc_av(struct ocrdma_dev *dev, struct ocrdma_ah *ah)
3035fe2caefcSParav Pandit {
3036fe2caefcSParav Pandit 	int i;
3037fe2caefcSParav Pandit 	int status = -EINVAL;
3038fe2caefcSParav Pandit 	struct ocrdma_av *av;
3039fe2caefcSParav Pandit 	unsigned long flags;
3040fe2caefcSParav Pandit 
3041fe2caefcSParav Pandit 	av = dev->av_tbl.va;
3042fe2caefcSParav Pandit 	spin_lock_irqsave(&dev->av_tbl.lock, flags);
3043fe2caefcSParav Pandit 	for (i = 0; i < dev->av_tbl.num_ah; i++) {
3044fe2caefcSParav Pandit 		if (av->valid == 0) {
3045fe2caefcSParav Pandit 			av->valid = OCRDMA_AV_VALID;
3046fe2caefcSParav Pandit 			ah->av = av;
3047fe2caefcSParav Pandit 			ah->id = i;
3048fe2caefcSParav Pandit 			status = 0;
3049fe2caefcSParav Pandit 			break;
3050fe2caefcSParav Pandit 		}
3051fe2caefcSParav Pandit 		av++;
3052fe2caefcSParav Pandit 	}
3053fe2caefcSParav Pandit 	if (i == dev->av_tbl.num_ah)
3054fe2caefcSParav Pandit 		status = -EAGAIN;
3055fe2caefcSParav Pandit 	spin_unlock_irqrestore(&dev->av_tbl.lock, flags);
3056fe2caefcSParav Pandit 	return status;
3057fe2caefcSParav Pandit }
3058fe2caefcSParav Pandit 
ocrdma_free_av(struct ocrdma_dev * dev,struct ocrdma_ah * ah)3059d3456914SLeon Romanovsky void ocrdma_free_av(struct ocrdma_dev *dev, struct ocrdma_ah *ah)
3060fe2caefcSParav Pandit {
3061fe2caefcSParav Pandit 	unsigned long flags;
3062fe2caefcSParav Pandit 	spin_lock_irqsave(&dev->av_tbl.lock, flags);
3063fe2caefcSParav Pandit 	ah->av->valid = 0;
3064fe2caefcSParav Pandit 	spin_unlock_irqrestore(&dev->av_tbl.lock, flags);
3065fe2caefcSParav Pandit }
3066fe2caefcSParav Pandit 
ocrdma_create_eqs(struct ocrdma_dev * dev)3067c88bd03fSNaresh Gottumukkala static int ocrdma_create_eqs(struct ocrdma_dev *dev)
3068fe2caefcSParav Pandit {
3069da496438SRoland Dreier 	int num_eq, i, status = 0;
3070fe2caefcSParav Pandit 	int irq;
3071fe2caefcSParav Pandit 	unsigned long flags = 0;
3072fe2caefcSParav Pandit 
3073fe2caefcSParav Pandit 	num_eq = dev->nic_info.msix.num_vectors -
3074fe2caefcSParav Pandit 			dev->nic_info.msix.start_vector;
3075fe2caefcSParav Pandit 	if (dev->nic_info.intr_mode == BE_INTERRUPT_MODE_INTX) {
3076fe2caefcSParav Pandit 		num_eq = 1;
3077fe2caefcSParav Pandit 		flags = IRQF_SHARED;
3078f99b1649SNaresh Gottumukkala 	} else {
3079fe2caefcSParav Pandit 		num_eq = min_t(u32, num_eq, num_online_cpus());
3080f99b1649SNaresh Gottumukkala 	}
3081f99b1649SNaresh Gottumukkala 
3082c88bd03fSNaresh Gottumukkala 	if (!num_eq)
3083c88bd03fSNaresh Gottumukkala 		return -EINVAL;
3084c88bd03fSNaresh Gottumukkala 
30856396bb22SKees Cook 	dev->eq_tbl = kcalloc(num_eq, sizeof(struct ocrdma_eq), GFP_KERNEL);
3086c88bd03fSNaresh Gottumukkala 	if (!dev->eq_tbl)
3087fe2caefcSParav Pandit 		return -ENOMEM;
3088fe2caefcSParav Pandit 
3089fe2caefcSParav Pandit 	for (i = 0; i < num_eq; i++) {
3090c88bd03fSNaresh Gottumukkala 		status = ocrdma_create_eq(dev, &dev->eq_tbl[i],
3091fe2caefcSParav Pandit 					OCRDMA_EQ_LEN);
3092fe2caefcSParav Pandit 		if (status) {
3093fe2caefcSParav Pandit 			status = -EINVAL;
3094fe2caefcSParav Pandit 			break;
3095fe2caefcSParav Pandit 		}
3096c88bd03fSNaresh Gottumukkala 		sprintf(dev->eq_tbl[i].irq_name, "ocrdma%d-%d",
3097fe2caefcSParav Pandit 			dev->id, i);
3098c88bd03fSNaresh Gottumukkala 		irq = ocrdma_get_irq(dev, &dev->eq_tbl[i]);
3099fe2caefcSParav Pandit 		status = request_irq(irq, ocrdma_irq_handler, flags,
3100c88bd03fSNaresh Gottumukkala 				     dev->eq_tbl[i].irq_name,
3101c88bd03fSNaresh Gottumukkala 				     &dev->eq_tbl[i]);
3102c88bd03fSNaresh Gottumukkala 		if (status)
3103c88bd03fSNaresh Gottumukkala 			goto done;
3104fe2caefcSParav Pandit 		dev->eq_cnt += 1;
3105fe2caefcSParav Pandit 	}
3106fe2caefcSParav Pandit 	/* one eq is sufficient for data path to work */
3107fe2caefcSParav Pandit 	return 0;
3108c88bd03fSNaresh Gottumukkala done:
3109c88bd03fSNaresh Gottumukkala 	ocrdma_destroy_eqs(dev);
3110fe2caefcSParav Pandit 	return status;
3111fe2caefcSParav Pandit }
3112fe2caefcSParav Pandit 
ocrdma_mbx_modify_eqd(struct ocrdma_dev * dev,struct ocrdma_eq * eq,int num)3113b4dbe8d5SMitesh Ahuja static int ocrdma_mbx_modify_eqd(struct ocrdma_dev *dev, struct ocrdma_eq *eq,
3114b4dbe8d5SMitesh Ahuja 				 int num)
3115b4dbe8d5SMitesh Ahuja {
3116689a8e31SGustavo A. R. Silva 	int i, status;
3117b4dbe8d5SMitesh Ahuja 	struct ocrdma_modify_eqd_req *cmd;
3118b4dbe8d5SMitesh Ahuja 
3119b4dbe8d5SMitesh Ahuja 	cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_MODIFY_EQ_DELAY, sizeof(*cmd));
3120b4dbe8d5SMitesh Ahuja 	if (!cmd)
3121689a8e31SGustavo A. R. Silva 		return -ENOMEM;
3122b4dbe8d5SMitesh Ahuja 
3123b4dbe8d5SMitesh Ahuja 	ocrdma_init_mch(&cmd->cmd.req, OCRDMA_CMD_MODIFY_EQ_DELAY,
3124b4dbe8d5SMitesh Ahuja 			OCRDMA_SUBSYS_COMMON, sizeof(*cmd));
3125b4dbe8d5SMitesh Ahuja 
3126b4dbe8d5SMitesh Ahuja 	cmd->cmd.num_eq = num;
3127b4dbe8d5SMitesh Ahuja 	for (i = 0; i < num; i++) {
3128b4dbe8d5SMitesh Ahuja 		cmd->cmd.set_eqd[i].eq_id = eq[i].q.id;
3129b4dbe8d5SMitesh Ahuja 		cmd->cmd.set_eqd[i].phase = 0;
3130b4dbe8d5SMitesh Ahuja 		cmd->cmd.set_eqd[i].delay_multiplier =
3131b4dbe8d5SMitesh Ahuja 				(eq[i].aic_obj.prev_eqd * 65)/100;
3132b4dbe8d5SMitesh Ahuja 	}
3133b4dbe8d5SMitesh Ahuja 	status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
3134689a8e31SGustavo A. R. Silva 
3135b4dbe8d5SMitesh Ahuja 	kfree(cmd);
3136b4dbe8d5SMitesh Ahuja 	return status;
3137b4dbe8d5SMitesh Ahuja }
3138b4dbe8d5SMitesh Ahuja 
ocrdma_modify_eqd(struct ocrdma_dev * dev,struct ocrdma_eq * eq,int num)3139b4dbe8d5SMitesh Ahuja static int ocrdma_modify_eqd(struct ocrdma_dev *dev, struct ocrdma_eq *eq,
3140b4dbe8d5SMitesh Ahuja 			     int num)
3141b4dbe8d5SMitesh Ahuja {
3142b4dbe8d5SMitesh Ahuja 	int num_eqs, i = 0;
3143b4dbe8d5SMitesh Ahuja 	if (num > 8) {
3144b4dbe8d5SMitesh Ahuja 		while (num) {
3145b4dbe8d5SMitesh Ahuja 			num_eqs = min(num, 8);
3146b4dbe8d5SMitesh Ahuja 			ocrdma_mbx_modify_eqd(dev, &eq[i], num_eqs);
3147b4dbe8d5SMitesh Ahuja 			i += num_eqs;
3148b4dbe8d5SMitesh Ahuja 			num -= num_eqs;
3149b4dbe8d5SMitesh Ahuja 		}
3150b4dbe8d5SMitesh Ahuja 	} else {
3151b4dbe8d5SMitesh Ahuja 		ocrdma_mbx_modify_eqd(dev, eq, num);
3152b4dbe8d5SMitesh Ahuja 	}
3153b4dbe8d5SMitesh Ahuja 	return 0;
3154b4dbe8d5SMitesh Ahuja }
3155b4dbe8d5SMitesh Ahuja 
ocrdma_eqd_set_task(struct work_struct * work)3156b4dbe8d5SMitesh Ahuja void ocrdma_eqd_set_task(struct work_struct *work)
3157b4dbe8d5SMitesh Ahuja {
3158b4dbe8d5SMitesh Ahuja 	struct ocrdma_dev *dev =
3159b4dbe8d5SMitesh Ahuja 		container_of(work, struct ocrdma_dev, eqd_work.work);
316069a84035SBart Van Assche 	struct ocrdma_eq *eq = NULL;
316105a8a1cbSBart Van Assche 	int i, num = 0;
3162b4dbe8d5SMitesh Ahuja 	u64 eq_intr;
3163b4dbe8d5SMitesh Ahuja 
3164b4dbe8d5SMitesh Ahuja 	for (i = 0; i < dev->eq_cnt; i++) {
3165b4dbe8d5SMitesh Ahuja 		eq = &dev->eq_tbl[i];
3166b4dbe8d5SMitesh Ahuja 		if (eq->aic_obj.eq_intr_cnt > eq->aic_obj.prev_eq_intr_cnt) {
3167b4dbe8d5SMitesh Ahuja 			eq_intr = eq->aic_obj.eq_intr_cnt -
3168b4dbe8d5SMitesh Ahuja 				  eq->aic_obj.prev_eq_intr_cnt;
3169b4dbe8d5SMitesh Ahuja 			if ((eq_intr > EQ_INTR_PER_SEC_THRSH_HI) &&
3170b4dbe8d5SMitesh Ahuja 			    (eq->aic_obj.prev_eqd == EQ_AIC_MIN_EQD)) {
3171b4dbe8d5SMitesh Ahuja 				eq->aic_obj.prev_eqd = EQ_AIC_MAX_EQD;
3172b4dbe8d5SMitesh Ahuja 				num++;
3173b4dbe8d5SMitesh Ahuja 			} else if ((eq_intr < EQ_INTR_PER_SEC_THRSH_LOW) &&
3174b4dbe8d5SMitesh Ahuja 				   (eq->aic_obj.prev_eqd == EQ_AIC_MAX_EQD)) {
3175b4dbe8d5SMitesh Ahuja 				eq->aic_obj.prev_eqd = EQ_AIC_MIN_EQD;
3176b4dbe8d5SMitesh Ahuja 				num++;
3177b4dbe8d5SMitesh Ahuja 			}
3178b4dbe8d5SMitesh Ahuja 		}
3179b4dbe8d5SMitesh Ahuja 		eq->aic_obj.prev_eq_intr_cnt = eq->aic_obj.eq_intr_cnt;
3180b4dbe8d5SMitesh Ahuja 	}
3181b4dbe8d5SMitesh Ahuja 
3182b4dbe8d5SMitesh Ahuja 	if (num)
318305a8a1cbSBart Van Assche 		ocrdma_modify_eqd(dev, &dev->eq_tbl[0], num);
3184b4dbe8d5SMitesh Ahuja 	schedule_delayed_work(&dev->eqd_work, msecs_to_jiffies(1000));
3185b4dbe8d5SMitesh Ahuja }
3186b4dbe8d5SMitesh Ahuja 
ocrdma_init_hw(struct ocrdma_dev * dev)3187fe2caefcSParav Pandit int ocrdma_init_hw(struct ocrdma_dev *dev)
3188fe2caefcSParav Pandit {
3189fe2caefcSParav Pandit 	int status;
3190c88bd03fSNaresh Gottumukkala 
3191c88bd03fSNaresh Gottumukkala 	/* create the eqs  */
3192c88bd03fSNaresh Gottumukkala 	status = ocrdma_create_eqs(dev);
3193fe2caefcSParav Pandit 	if (status)
3194fe2caefcSParav Pandit 		goto qpeq_err;
3195fe2caefcSParav Pandit 	status = ocrdma_create_mq(dev);
3196fe2caefcSParav Pandit 	if (status)
3197fe2caefcSParav Pandit 		goto mq_err;
3198fe2caefcSParav Pandit 	status = ocrdma_mbx_query_fw_config(dev);
3199fe2caefcSParav Pandit 	if (status)
3200fe2caefcSParav Pandit 		goto conf_err;
3201fe2caefcSParav Pandit 	status = ocrdma_mbx_query_dev(dev);
3202fe2caefcSParav Pandit 	if (status)
3203fe2caefcSParav Pandit 		goto conf_err;
3204fe2caefcSParav Pandit 	status = ocrdma_mbx_query_fw_ver(dev);
3205fe2caefcSParav Pandit 	if (status)
3206fe2caefcSParav Pandit 		goto conf_err;
3207fe2caefcSParav Pandit 	status = ocrdma_mbx_create_ah_tbl(dev);
3208fe2caefcSParav Pandit 	if (status)
3209fe2caefcSParav Pandit 		goto conf_err;
3210a51f06e1SSelvin Xavier 	status = ocrdma_mbx_get_phy_info(dev);
3211a51f06e1SSelvin Xavier 	if (status)
3212daac9681SDevesh Sharma 		goto info_attrb_err;
3213a51f06e1SSelvin Xavier 	status = ocrdma_mbx_get_ctrl_attribs(dev);
3214a51f06e1SSelvin Xavier 	if (status)
3215daac9681SDevesh Sharma 		goto info_attrb_err;
3216a51f06e1SSelvin Xavier 
3217fe2caefcSParav Pandit 	return 0;
3218fe2caefcSParav Pandit 
3219daac9681SDevesh Sharma info_attrb_err:
3220daac9681SDevesh Sharma 	ocrdma_mbx_delete_ah_tbl(dev);
3221fe2caefcSParav Pandit conf_err:
3222fe2caefcSParav Pandit 	ocrdma_destroy_mq(dev);
3223fe2caefcSParav Pandit mq_err:
3224c88bd03fSNaresh Gottumukkala 	ocrdma_destroy_eqs(dev);
3225fe2caefcSParav Pandit qpeq_err:
3226ef99c4c2SNaresh Gottumukkala 	pr_err("%s() status=%d\n", __func__, status);
3227fe2caefcSParav Pandit 	return status;
3228fe2caefcSParav Pandit }
3229fe2caefcSParav Pandit 
ocrdma_cleanup_hw(struct ocrdma_dev * dev)3230fe2caefcSParav Pandit void ocrdma_cleanup_hw(struct ocrdma_dev *dev)
3231fe2caefcSParav Pandit {
32329ba1377dSMitesh Ahuja 	ocrdma_free_pd_pool(dev);
3233fe2caefcSParav Pandit 	ocrdma_mbx_delete_ah_tbl(dev);
3234fe2caefcSParav Pandit 
3235fe2caefcSParav Pandit 	/* cleanup the control path */
3236fe2caefcSParav Pandit 	ocrdma_destroy_mq(dev);
3237314fdf44SSelvin Xavier 
3238314fdf44SSelvin Xavier 	/* cleanup the eqs */
3239314fdf44SSelvin Xavier 	ocrdma_destroy_eqs(dev);
3240fe2caefcSParav Pandit }
3241