xref: /freebsd/sys/dev/bnxt/bnxt_re/qplib_rcfw.c (revision acd884dec99adcf8c4cdd0aa8a50be79c216f8e8)
1*acd884deSSumit Saxena /*
2*acd884deSSumit Saxena  * Copyright (c) 2015-2024, Broadcom. All rights reserved.  The term
3*acd884deSSumit Saxena  * Broadcom refers to Broadcom Limited and/or its subsidiaries.
4*acd884deSSumit Saxena  *
5*acd884deSSumit Saxena  * Redistribution and use in source and binary forms, with or without
6*acd884deSSumit Saxena  * modification, are permitted provided that the following conditions
7*acd884deSSumit Saxena  * are met:
8*acd884deSSumit Saxena  *
9*acd884deSSumit Saxena  * 1. Redistributions of source code must retain the above copyright
10*acd884deSSumit Saxena  *    notice, this list of conditions and the following disclaimer.
11*acd884deSSumit Saxena  * 2. Redistributions in binary form must reproduce the above copyright
12*acd884deSSumit Saxena  *    notice, this list of conditions and the following disclaimer in
13*acd884deSSumit Saxena  *    the documentation and/or other materials provided with the
14*acd884deSSumit Saxena  *    distribution.
15*acd884deSSumit Saxena  *
16*acd884deSSumit Saxena  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
17*acd884deSSumit Saxena  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18*acd884deSSumit Saxena  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19*acd884deSSumit Saxena  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
20*acd884deSSumit Saxena  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*acd884deSSumit Saxena  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*acd884deSSumit Saxena  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23*acd884deSSumit Saxena  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24*acd884deSSumit Saxena  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25*acd884deSSumit Saxena  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26*acd884deSSumit Saxena  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*acd884deSSumit Saxena  *
28*acd884deSSumit Saxena  * Description: RDMA Controller HW interface
29*acd884deSSumit Saxena  */
30*acd884deSSumit Saxena 
31*acd884deSSumit Saxena #include <linux/interrupt.h>
32*acd884deSSumit Saxena #include <linux/spinlock.h>
33*acd884deSSumit Saxena #include <linux/dma-mapping.h>
34*acd884deSSumit Saxena #include <linux/sched.h>
35*acd884deSSumit Saxena #include <linux/pci.h>
36*acd884deSSumit Saxena #include <linux/delay.h>
37*acd884deSSumit Saxena #include <linux/hardirq.h>
38*acd884deSSumit Saxena #include <linux/device.h>
39*acd884deSSumit Saxena 
40*acd884deSSumit Saxena #include "hsi_struct_def.h"
41*acd884deSSumit Saxena #include "qplib_tlv.h"
42*acd884deSSumit Saxena #include "qplib_res.h"
43*acd884deSSumit Saxena #include "qplib_sp.h"
44*acd884deSSumit Saxena #include "qplib_rcfw.h"
45*acd884deSSumit Saxena #include "bnxt_re.h"
46*acd884deSSumit Saxena 
47*acd884deSSumit Saxena static void bnxt_qplib_service_creq(unsigned long data);
48*acd884deSSumit Saxena 
__check_cmdq_stall(struct bnxt_qplib_rcfw * rcfw,u32 * cur_prod,u32 * cur_cons)49*acd884deSSumit Saxena int __check_cmdq_stall(struct bnxt_qplib_rcfw *rcfw,
50*acd884deSSumit Saxena 			      u32 *cur_prod, u32 *cur_cons)
51*acd884deSSumit Saxena {
52*acd884deSSumit Saxena 	struct bnxt_qplib_cmdq_ctx *cmdq;
53*acd884deSSumit Saxena 
54*acd884deSSumit Saxena 	cmdq = &rcfw->cmdq;
55*acd884deSSumit Saxena 
56*acd884deSSumit Saxena 	if (*cur_prod == cmdq->hwq.prod &&
57*acd884deSSumit Saxena 	    *cur_cons == cmdq->hwq.cons)
58*acd884deSSumit Saxena 		/* No activity on CMDQ or CREQ. FW down */
59*acd884deSSumit Saxena 		return -ETIMEDOUT;
60*acd884deSSumit Saxena 
61*acd884deSSumit Saxena 	*cur_prod = cmdq->hwq.prod;
62*acd884deSSumit Saxena 	*cur_cons = cmdq->hwq.cons;
63*acd884deSSumit Saxena 	return 0;
64*acd884deSSumit Saxena }
65*acd884deSSumit Saxena 
bnxt_qplib_map_rc(u8 opcode)66*acd884deSSumit Saxena static int bnxt_qplib_map_rc(u8 opcode)
67*acd884deSSumit Saxena {
68*acd884deSSumit Saxena 	switch (opcode) {
69*acd884deSSumit Saxena 	case CMDQ_BASE_OPCODE_DESTROY_QP:
70*acd884deSSumit Saxena 	case CMDQ_BASE_OPCODE_DESTROY_SRQ:
71*acd884deSSumit Saxena 	case CMDQ_BASE_OPCODE_DESTROY_CQ:
72*acd884deSSumit Saxena 	case CMDQ_BASE_OPCODE_DEALLOCATE_KEY:
73*acd884deSSumit Saxena 	case CMDQ_BASE_OPCODE_DEREGISTER_MR:
74*acd884deSSumit Saxena 	case CMDQ_BASE_OPCODE_DELETE_GID:
75*acd884deSSumit Saxena 	case CMDQ_BASE_OPCODE_DESTROY_QP1:
76*acd884deSSumit Saxena 	case CMDQ_BASE_OPCODE_DESTROY_AH:
77*acd884deSSumit Saxena 	case CMDQ_BASE_OPCODE_DEINITIALIZE_FW:
78*acd884deSSumit Saxena 	case CMDQ_BASE_OPCODE_MODIFY_ROCE_CC:
79*acd884deSSumit Saxena 	case CMDQ_BASE_OPCODE_SET_LINK_AGGR_MODE:
80*acd884deSSumit Saxena 		return 0;
81*acd884deSSumit Saxena 	default:
82*acd884deSSumit Saxena 		return -ETIMEDOUT;
83*acd884deSSumit Saxena 	}
84*acd884deSSumit Saxena }
85*acd884deSSumit Saxena 
86*acd884deSSumit Saxena /**
87*acd884deSSumit Saxena  * bnxt_re_is_fw_stalled   -	Check firmware health
88*acd884deSSumit Saxena  * @rcfw      -   rcfw channel instance of rdev
89*acd884deSSumit Saxena  * @cookie    -   cookie to track the command
90*acd884deSSumit Saxena  *
91*acd884deSSumit Saxena  * If firmware has not responded any rcfw command within
92*acd884deSSumit Saxena  * rcfw->max_timeout, consider firmware as stalled.
93*acd884deSSumit Saxena  *
94*acd884deSSumit Saxena  * Returns:
95*acd884deSSumit Saxena  * 0 if firmware is responding
96*acd884deSSumit Saxena  * -ENODEV if firmware is not responding
97*acd884deSSumit Saxena  */
bnxt_re_is_fw_stalled(struct bnxt_qplib_rcfw * rcfw,u16 cookie)98*acd884deSSumit Saxena static int bnxt_re_is_fw_stalled(struct bnxt_qplib_rcfw *rcfw, u16 cookie)
99*acd884deSSumit Saxena {
100*acd884deSSumit Saxena 	struct bnxt_qplib_cmdq_ctx *cmdq;
101*acd884deSSumit Saxena 	struct bnxt_qplib_crsqe *crsqe;
102*acd884deSSumit Saxena 
103*acd884deSSumit Saxena 	crsqe = &rcfw->crsqe_tbl[cookie];
104*acd884deSSumit Saxena 	cmdq = &rcfw->cmdq;
105*acd884deSSumit Saxena 
106*acd884deSSumit Saxena 	if (time_after(jiffies, cmdq->last_seen +
107*acd884deSSumit Saxena 		      (rcfw->max_timeout * HZ))) {
108*acd884deSSumit Saxena 		dev_warn_ratelimited(&rcfw->pdev->dev,
109*acd884deSSumit Saxena 				     "%s: FW STALL Detected. cmdq[%#x]=%#x waited (%ld > %d) msec active %d\n",
110*acd884deSSumit Saxena 				     __func__, cookie, crsqe->opcode,
111*acd884deSSumit Saxena 				     (long)jiffies_to_msecs(jiffies - cmdq->last_seen),
112*acd884deSSumit Saxena 				     rcfw->max_timeout * 1000,
113*acd884deSSumit Saxena 				     crsqe->is_in_used);
114*acd884deSSumit Saxena 		return -ENODEV;
115*acd884deSSumit Saxena 	}
116*acd884deSSumit Saxena 
117*acd884deSSumit Saxena 	return 0;
118*acd884deSSumit Saxena }
119*acd884deSSumit Saxena /**
120*acd884deSSumit Saxena  * __wait_for_resp   -	Don't hold the cpu context and wait for response
121*acd884deSSumit Saxena  * @rcfw      -   rcfw channel instance of rdev
122*acd884deSSumit Saxena  * @cookie    -   cookie to track the command
123*acd884deSSumit Saxena  *
124*acd884deSSumit Saxena  * Wait for command completion in sleepable context.
125*acd884deSSumit Saxena  *
126*acd884deSSumit Saxena  * Returns:
127*acd884deSSumit Saxena  * 0 if command is completed by firmware.
128*acd884deSSumit Saxena  * Non zero error code for rest of the case.
129*acd884deSSumit Saxena  */
__wait_for_resp(struct bnxt_qplib_rcfw * rcfw,u16 cookie)130*acd884deSSumit Saxena static int __wait_for_resp(struct bnxt_qplib_rcfw *rcfw, u16 cookie)
131*acd884deSSumit Saxena {
132*acd884deSSumit Saxena 	struct bnxt_qplib_cmdq_ctx *cmdq;
133*acd884deSSumit Saxena 	struct bnxt_qplib_crsqe *crsqe;
134*acd884deSSumit Saxena 	unsigned long issue_time;
135*acd884deSSumit Saxena 	int ret;
136*acd884deSSumit Saxena 
137*acd884deSSumit Saxena 	cmdq = &rcfw->cmdq;
138*acd884deSSumit Saxena 	issue_time = jiffies;
139*acd884deSSumit Saxena 	crsqe = &rcfw->crsqe_tbl[cookie];
140*acd884deSSumit Saxena 
141*acd884deSSumit Saxena 	do {
142*acd884deSSumit Saxena 		if (RCFW_NO_FW_ACCESS(rcfw))
143*acd884deSSumit Saxena 			return bnxt_qplib_map_rc(crsqe->opcode);
144*acd884deSSumit Saxena 		if (test_bit(FIRMWARE_STALL_DETECTED, &cmdq->flags))
145*acd884deSSumit Saxena 			return -ETIMEDOUT;
146*acd884deSSumit Saxena 
147*acd884deSSumit Saxena 		/* Non zero means command completed */
148*acd884deSSumit Saxena 		ret = wait_event_timeout(cmdq->waitq,
149*acd884deSSumit Saxena 					 !crsqe->is_in_used ||
150*acd884deSSumit Saxena 					 RCFW_NO_FW_ACCESS(rcfw),
151*acd884deSSumit Saxena 					 msecs_to_jiffies(rcfw->max_timeout * 1000));
152*acd884deSSumit Saxena 
153*acd884deSSumit Saxena 		if (!crsqe->is_in_used)
154*acd884deSSumit Saxena 			return 0;
155*acd884deSSumit Saxena 		/*
156*acd884deSSumit Saxena 		 * Take care if interrupt miss or other cases like DBR drop
157*acd884deSSumit Saxena 		 */
158*acd884deSSumit Saxena 		bnxt_qplib_service_creq((unsigned long)rcfw);
159*acd884deSSumit Saxena 		dev_warn_ratelimited(&rcfw->pdev->dev,
160*acd884deSSumit Saxena 			"Non-Blocking QPLIB: cmdq[%#x]=%#x waited (%lu) msec bit %d\n",
161*acd884deSSumit Saxena 			cookie, crsqe->opcode,
162*acd884deSSumit Saxena 			(long)jiffies_to_msecs(jiffies - issue_time),
163*acd884deSSumit Saxena 			crsqe->is_in_used);
164*acd884deSSumit Saxena 
165*acd884deSSumit Saxena 		if (!crsqe->is_in_used)
166*acd884deSSumit Saxena 			return 0;
167*acd884deSSumit Saxena 
168*acd884deSSumit Saxena 		ret = bnxt_re_is_fw_stalled(rcfw, cookie);
169*acd884deSSumit Saxena 		if (ret)
170*acd884deSSumit Saxena 			return ret;
171*acd884deSSumit Saxena 
172*acd884deSSumit Saxena 	} while (true);
173*acd884deSSumit Saxena };
174*acd884deSSumit Saxena 
175*acd884deSSumit Saxena /**
176*acd884deSSumit Saxena  * __block_for_resp   -	hold the cpu context and wait for response
177*acd884deSSumit Saxena  * @rcfw      -   rcfw channel instance of rdev
178*acd884deSSumit Saxena  * @cookie    -   cookie to track the command
179*acd884deSSumit Saxena  *
180*acd884deSSumit Saxena  * This function will hold the cpu (non-sleepable context) and
181*acd884deSSumit Saxena  * wait for command completion. Maximum holding interval is 8 second.
182*acd884deSSumit Saxena  *
183*acd884deSSumit Saxena  * Returns:
184*acd884deSSumit Saxena  * -ETIMEOUT if command is not completed in specific time interval.
185*acd884deSSumit Saxena  * 0 if command is completed by firmware.
186*acd884deSSumit Saxena  */
__block_for_resp(struct bnxt_qplib_rcfw * rcfw,u16 cookie)187*acd884deSSumit Saxena static int __block_for_resp(struct bnxt_qplib_rcfw *rcfw, u16 cookie)
188*acd884deSSumit Saxena {
189*acd884deSSumit Saxena 	struct bnxt_qplib_cmdq_ctx *cmdq = &rcfw->cmdq;
190*acd884deSSumit Saxena 	struct bnxt_qplib_crsqe *crsqe;
191*acd884deSSumit Saxena 	unsigned long issue_time = 0;
192*acd884deSSumit Saxena 
193*acd884deSSumit Saxena 	issue_time = jiffies;
194*acd884deSSumit Saxena 	crsqe = &rcfw->crsqe_tbl[cookie];
195*acd884deSSumit Saxena 
196*acd884deSSumit Saxena 	do {
197*acd884deSSumit Saxena 		if (RCFW_NO_FW_ACCESS(rcfw))
198*acd884deSSumit Saxena 			return bnxt_qplib_map_rc(crsqe->opcode);
199*acd884deSSumit Saxena 		if (test_bit(FIRMWARE_STALL_DETECTED, &cmdq->flags))
200*acd884deSSumit Saxena 			return -ETIMEDOUT;
201*acd884deSSumit Saxena 
202*acd884deSSumit Saxena 		udelay(1);
203*acd884deSSumit Saxena 
204*acd884deSSumit Saxena 		/* Below call is must since there can be a deadlock
205*acd884deSSumit Saxena 		 * if interrupt is mapped to the same cpu
206*acd884deSSumit Saxena 		 */
207*acd884deSSumit Saxena 		bnxt_qplib_service_creq((unsigned long)rcfw);
208*acd884deSSumit Saxena 		if (!crsqe->is_in_used)
209*acd884deSSumit Saxena 			return 0;
210*acd884deSSumit Saxena 
211*acd884deSSumit Saxena 	} while (time_before(jiffies, issue_time + (8 * HZ)));
212*acd884deSSumit Saxena 
213*acd884deSSumit Saxena 	dev_warn_ratelimited(&rcfw->pdev->dev,
214*acd884deSSumit Saxena 		"Blocking QPLIB: cmdq[%#x]=%#x taken (%lu) msec",
215*acd884deSSumit Saxena 		cookie, crsqe->opcode,
216*acd884deSSumit Saxena 		(long)jiffies_to_msecs(jiffies - issue_time));
217*acd884deSSumit Saxena 
218*acd884deSSumit Saxena 	return -ETIMEDOUT;
219*acd884deSSumit Saxena };
220*acd884deSSumit Saxena 
221*acd884deSSumit Saxena /*  __send_message_no_waiter -	get cookie and post the message.
222*acd884deSSumit Saxena  * @rcfw      -   rcfw channel instance of rdev
223*acd884deSSumit Saxena  * @msg      -    qplib message internal
224*acd884deSSumit Saxena  *
225*acd884deSSumit Saxena  * This function will just post and don't bother about completion.
226*acd884deSSumit Saxena  * Current design of this function is -
227*acd884deSSumit Saxena  * user must hold the completion queue hwq->lock.
228*acd884deSSumit Saxena  * user must have used existing completion and free the resources.
229*acd884deSSumit Saxena  * this function will not check queue full condition.
230*acd884deSSumit Saxena  * this function will explicitly set is_waiter_alive=false.
231*acd884deSSumit Saxena  * current use case is - send destroy_ah if create_ah is return
232*acd884deSSumit Saxena  * after waiter of create_ah is lost. It can be extended for other
233*acd884deSSumit Saxena  * use case as well.
234*acd884deSSumit Saxena  *
235*acd884deSSumit Saxena  * Returns: Nothing
236*acd884deSSumit Saxena  *
237*acd884deSSumit Saxena  */
__send_message_no_waiter(struct bnxt_qplib_rcfw * rcfw,struct bnxt_qplib_cmdqmsg * msg)238*acd884deSSumit Saxena static  void __send_message_no_waiter(struct bnxt_qplib_rcfw *rcfw,
239*acd884deSSumit Saxena 			  struct bnxt_qplib_cmdqmsg *msg)
240*acd884deSSumit Saxena {
241*acd884deSSumit Saxena 	struct bnxt_qplib_cmdq_ctx *cmdq = &rcfw->cmdq;
242*acd884deSSumit Saxena 	struct bnxt_qplib_hwq *cmdq_hwq = &cmdq->hwq;
243*acd884deSSumit Saxena 	struct bnxt_qplib_crsqe *crsqe;
244*acd884deSSumit Saxena 	struct bnxt_qplib_cmdqe *cmdqe;
245*acd884deSSumit Saxena 	u32 sw_prod, cmdq_prod, bsize;
246*acd884deSSumit Saxena 	u16 cookie;
247*acd884deSSumit Saxena 	u8 *preq;
248*acd884deSSumit Saxena 
249*acd884deSSumit Saxena 	cookie = cmdq->seq_num & RCFW_MAX_COOKIE_VALUE;
250*acd884deSSumit Saxena 	__set_cmdq_base_cookie(msg->req, msg->req_sz, cpu_to_le16(cookie));
251*acd884deSSumit Saxena 	crsqe = &rcfw->crsqe_tbl[cookie];
252*acd884deSSumit Saxena 
253*acd884deSSumit Saxena 	/* Set cmd_size in terms of 16B slots in req. */
254*acd884deSSumit Saxena 	bsize = bnxt_qplib_set_cmd_slots(msg->req);
255*acd884deSSumit Saxena 	/* GET_CMD_SIZE would return number of slots in either case of tlv
256*acd884deSSumit Saxena 	 * and non-tlv commands after call to bnxt_qplib_set_cmd_slots()
257*acd884deSSumit Saxena 	 */
258*acd884deSSumit Saxena 	crsqe->send_timestamp = jiffies;
259*acd884deSSumit Saxena 	crsqe->is_internal_cmd = true;
260*acd884deSSumit Saxena 	crsqe->is_waiter_alive = false;
261*acd884deSSumit Saxena 	crsqe->is_in_used = true;
262*acd884deSSumit Saxena 	crsqe->req_size = __get_cmdq_base_cmd_size(msg->req, msg->req_sz);
263*acd884deSSumit Saxena 
264*acd884deSSumit Saxena 	preq = (u8 *)msg->req;
265*acd884deSSumit Saxena 	do {
266*acd884deSSumit Saxena 		/* Locate the next cmdq slot */
267*acd884deSSumit Saxena 		sw_prod = HWQ_CMP(cmdq_hwq->prod, cmdq_hwq);
268*acd884deSSumit Saxena 		cmdqe = bnxt_qplib_get_qe(cmdq_hwq, sw_prod, NULL);
269*acd884deSSumit Saxena 		/* Copy a segment of the req cmd to the cmdq */
270*acd884deSSumit Saxena 		memset(cmdqe, 0, sizeof(*cmdqe));
271*acd884deSSumit Saxena 		memcpy(cmdqe, preq, min_t(u32, bsize, sizeof(*cmdqe)));
272*acd884deSSumit Saxena 		preq += min_t(u32, bsize, sizeof(*cmdqe));
273*acd884deSSumit Saxena 		bsize -= min_t(u32, bsize, sizeof(*cmdqe));
274*acd884deSSumit Saxena 		cmdq_hwq->prod++;
275*acd884deSSumit Saxena 	} while (bsize > 0);
276*acd884deSSumit Saxena 	cmdq->seq_num++;
277*acd884deSSumit Saxena 
278*acd884deSSumit Saxena 	cmdq_prod = cmdq_hwq->prod & 0xFFFF;
279*acd884deSSumit Saxena 	atomic_inc(&rcfw->timeout_send);
280*acd884deSSumit Saxena 	/* ring CMDQ DB */
281*acd884deSSumit Saxena 	wmb();
282*acd884deSSumit Saxena 	writel(cmdq_prod, cmdq->cmdq_mbox.prod);
283*acd884deSSumit Saxena 	writel(RCFW_CMDQ_TRIG_VAL, cmdq->cmdq_mbox.db);
284*acd884deSSumit Saxena }
285*acd884deSSumit Saxena 
__send_message(struct bnxt_qplib_rcfw * rcfw,struct bnxt_qplib_cmdqmsg * msg)286*acd884deSSumit Saxena static int __send_message(struct bnxt_qplib_rcfw *rcfw,
287*acd884deSSumit Saxena 			  struct bnxt_qplib_cmdqmsg *msg)
288*acd884deSSumit Saxena {
289*acd884deSSumit Saxena 	u32 bsize, free_slots, required_slots;
290*acd884deSSumit Saxena 	struct bnxt_qplib_cmdq_ctx *cmdq;
291*acd884deSSumit Saxena 	struct bnxt_qplib_crsqe *crsqe;
292*acd884deSSumit Saxena 	struct bnxt_qplib_cmdqe *cmdqe;
293*acd884deSSumit Saxena 	struct bnxt_qplib_hwq *cmdq_hwq;
294*acd884deSSumit Saxena 	u32 sw_prod, cmdq_prod;
295*acd884deSSumit Saxena 	struct pci_dev *pdev;
296*acd884deSSumit Saxena 	unsigned long flags;
297*acd884deSSumit Saxena 	u16 cookie;
298*acd884deSSumit Saxena 	u8 opcode;
299*acd884deSSumit Saxena 	u8 *preq;
300*acd884deSSumit Saxena 
301*acd884deSSumit Saxena 	cmdq = &rcfw->cmdq;
302*acd884deSSumit Saxena 	cmdq_hwq = &cmdq->hwq;
303*acd884deSSumit Saxena 	pdev = rcfw->pdev;
304*acd884deSSumit Saxena 	opcode = __get_cmdq_base_opcode(msg->req, msg->req_sz);
305*acd884deSSumit Saxena 
306*acd884deSSumit Saxena 	/* Cmdq are in 16-byte units, each request can consume 1 or more
307*acd884deSSumit Saxena 	   cmdqe */
308*acd884deSSumit Saxena 	spin_lock_irqsave(&cmdq_hwq->lock, flags);
309*acd884deSSumit Saxena 	required_slots = bnxt_qplib_get_cmd_slots(msg->req);
310*acd884deSSumit Saxena 	free_slots = HWQ_FREE_SLOTS(cmdq_hwq);
311*acd884deSSumit Saxena 	cookie = cmdq->seq_num & RCFW_MAX_COOKIE_VALUE;
312*acd884deSSumit Saxena 	crsqe = &rcfw->crsqe_tbl[cookie];
313*acd884deSSumit Saxena 
314*acd884deSSumit Saxena 	if (required_slots >= free_slots) {
315*acd884deSSumit Saxena 		dev_warn_ratelimited(&pdev->dev,
316*acd884deSSumit Saxena 				"QPLIB: RCFW: CMDQ is full req/free %d/%d!\n",
317*acd884deSSumit Saxena 				required_slots, free_slots);
318*acd884deSSumit Saxena 		rcfw->cmdq_full_dbg++;
319*acd884deSSumit Saxena 		spin_unlock_irqrestore(&cmdq_hwq->lock, flags);
320*acd884deSSumit Saxena 		return -EAGAIN;
321*acd884deSSumit Saxena 	}
322*acd884deSSumit Saxena 
323*acd884deSSumit Saxena 	if (crsqe->is_in_used)
324*acd884deSSumit Saxena 		panic("QPLIB: Cookie was not requested %d\n",
325*acd884deSSumit Saxena 				cookie);
326*acd884deSSumit Saxena 
327*acd884deSSumit Saxena 	if (msg->block)
328*acd884deSSumit Saxena 		cookie |= RCFW_CMD_IS_BLOCKING;
329*acd884deSSumit Saxena 	__set_cmdq_base_cookie(msg->req, msg->req_sz, cpu_to_le16(cookie));
330*acd884deSSumit Saxena 
331*acd884deSSumit Saxena 	/* Set cmd_size in terms of 16B slots in req. */
332*acd884deSSumit Saxena 	bsize = bnxt_qplib_set_cmd_slots(msg->req);
333*acd884deSSumit Saxena 	/* GET_CMD_SIZE would return number of slots in either case of tlv
334*acd884deSSumit Saxena 	 * and non-tlv commands after call to bnxt_qplib_set_cmd_slots()
335*acd884deSSumit Saxena 	 */
336*acd884deSSumit Saxena 	crsqe->send_timestamp = jiffies;
337*acd884deSSumit Saxena 	crsqe->free_slots = free_slots;
338*acd884deSSumit Saxena 	crsqe->resp = (struct creq_qp_event *)msg->resp;
339*acd884deSSumit Saxena 	crsqe->resp->cookie = cpu_to_le16(cookie);
340*acd884deSSumit Saxena 	crsqe->is_internal_cmd = false;
341*acd884deSSumit Saxena 	crsqe->is_waiter_alive = true;
342*acd884deSSumit Saxena 	crsqe->is_in_used = true;
343*acd884deSSumit Saxena 	crsqe->opcode = opcode;
344*acd884deSSumit Saxena 	crsqe->requested_qp_state = msg->qp_state;
345*acd884deSSumit Saxena 
346*acd884deSSumit Saxena 	crsqe->req_size = __get_cmdq_base_cmd_size(msg->req, msg->req_sz);
347*acd884deSSumit Saxena 	if (__get_cmdq_base_resp_size(msg->req, msg->req_sz) && msg->sb) {
348*acd884deSSumit Saxena 		struct bnxt_qplib_rcfw_sbuf *sbuf = msg->sb;
349*acd884deSSumit Saxena 
350*acd884deSSumit Saxena 		__set_cmdq_base_resp_addr(msg->req, msg->req_sz,
351*acd884deSSumit Saxena 					  cpu_to_le64(sbuf->dma_addr));
352*acd884deSSumit Saxena 		__set_cmdq_base_resp_size(msg->req, msg->req_sz,
353*acd884deSSumit Saxena 					  ALIGN(sbuf->size, BNXT_QPLIB_CMDQE_UNITS) /
354*acd884deSSumit Saxena 					   BNXT_QPLIB_CMDQE_UNITS);
355*acd884deSSumit Saxena 	}
356*acd884deSSumit Saxena 
357*acd884deSSumit Saxena 	preq = (u8 *)msg->req;
358*acd884deSSumit Saxena 	do {
359*acd884deSSumit Saxena 		/* Locate the next cmdq slot */
360*acd884deSSumit Saxena 		sw_prod = HWQ_CMP(cmdq_hwq->prod, cmdq_hwq);
361*acd884deSSumit Saxena 		cmdqe = bnxt_qplib_get_qe(cmdq_hwq, sw_prod, NULL);
362*acd884deSSumit Saxena 		/* Copy a segment of the req cmd to the cmdq */
363*acd884deSSumit Saxena 		memset(cmdqe, 0, sizeof(*cmdqe));
364*acd884deSSumit Saxena 		memcpy(cmdqe, preq, min_t(u32, bsize, sizeof(*cmdqe)));
365*acd884deSSumit Saxena 		preq += min_t(u32, bsize, sizeof(*cmdqe));
366*acd884deSSumit Saxena 		bsize -= min_t(u32, bsize, sizeof(*cmdqe));
367*acd884deSSumit Saxena 		cmdq_hwq->prod++;
368*acd884deSSumit Saxena 	} while (bsize > 0);
369*acd884deSSumit Saxena 	cmdq->seq_num++;
370*acd884deSSumit Saxena 
371*acd884deSSumit Saxena 	cmdq_prod = cmdq_hwq->prod & 0xFFFF;
372*acd884deSSumit Saxena 	if (test_bit(FIRMWARE_FIRST_FLAG, &cmdq->flags)) {
373*acd884deSSumit Saxena 		/* The very first doorbell write
374*acd884deSSumit Saxena 		 * is required to set this flag
375*acd884deSSumit Saxena 		 * which prompts the FW to reset
376*acd884deSSumit Saxena 		 * its internal pointers
377*acd884deSSumit Saxena 		 */
378*acd884deSSumit Saxena 		cmdq_prod |= BIT(FIRMWARE_FIRST_FLAG);
379*acd884deSSumit Saxena 		clear_bit(FIRMWARE_FIRST_FLAG, &cmdq->flags);
380*acd884deSSumit Saxena 	}
381*acd884deSSumit Saxena 	/* ring CMDQ DB */
382*acd884deSSumit Saxena 	wmb();
383*acd884deSSumit Saxena 	writel(cmdq_prod, cmdq->cmdq_mbox.prod);
384*acd884deSSumit Saxena 	writel(RCFW_CMDQ_TRIG_VAL, cmdq->cmdq_mbox.db);
385*acd884deSSumit Saxena 
386*acd884deSSumit Saxena 	dev_dbg(&pdev->dev, "QPLIB: RCFW sent request with 0x%x 0x%x 0x%x\n",
387*acd884deSSumit Saxena 			cmdq_prod, cmdq_hwq->prod, crsqe->req_size);
388*acd884deSSumit Saxena 	dev_dbg(&pdev->dev,
389*acd884deSSumit Saxena 		"QPLIB: opcode 0x%x with cookie 0x%x at cmdq/crsq 0x%p/0x%p\n",
390*acd884deSSumit Saxena 		opcode,
391*acd884deSSumit Saxena 		__get_cmdq_base_cookie(msg->req, msg->req_sz),
392*acd884deSSumit Saxena 		cmdqe, crsqe);
393*acd884deSSumit Saxena 	spin_unlock_irqrestore(&cmdq_hwq->lock, flags);
394*acd884deSSumit Saxena 	/* Return the CREQ response pointer */
395*acd884deSSumit Saxena 	return 0;
396*acd884deSSumit Saxena }
397*acd884deSSumit Saxena 
398*acd884deSSumit Saxena /**
399*acd884deSSumit Saxena  * __poll_for_resp   -	self poll completion for rcfw command
400*acd884deSSumit Saxena  * @rcfw      -   rcfw channel instance of rdev
401*acd884deSSumit Saxena  * @cookie    -   cookie to track the command
402*acd884deSSumit Saxena  *
403*acd884deSSumit Saxena  * It works same as __wait_for_resp except this function will
404*acd884deSSumit Saxena  * do self polling in sort interval since interrupt is disabled.
405*acd884deSSumit Saxena  * This function can not be called from non-sleepable context.
406*acd884deSSumit Saxena  *
407*acd884deSSumit Saxena  * Returns:
408*acd884deSSumit Saxena  * -ETIMEOUT if command is not completed in specific time interval.
409*acd884deSSumit Saxena  * 0 if command is completed by firmware.
410*acd884deSSumit Saxena  */
__poll_for_resp(struct bnxt_qplib_rcfw * rcfw,u16 cookie)411*acd884deSSumit Saxena static int __poll_for_resp(struct bnxt_qplib_rcfw *rcfw, u16 cookie)
412*acd884deSSumit Saxena {
413*acd884deSSumit Saxena 	struct bnxt_qplib_cmdq_ctx *cmdq = &rcfw->cmdq;
414*acd884deSSumit Saxena 	struct bnxt_qplib_crsqe *crsqe;
415*acd884deSSumit Saxena 	unsigned long issue_time;
416*acd884deSSumit Saxena 	int ret;
417*acd884deSSumit Saxena 
418*acd884deSSumit Saxena 	issue_time = jiffies;
419*acd884deSSumit Saxena 	crsqe = &rcfw->crsqe_tbl[cookie];
420*acd884deSSumit Saxena 
421*acd884deSSumit Saxena 	do {
422*acd884deSSumit Saxena 		if (RCFW_NO_FW_ACCESS(rcfw))
423*acd884deSSumit Saxena 			return bnxt_qplib_map_rc(crsqe->opcode);
424*acd884deSSumit Saxena 		if (test_bit(FIRMWARE_STALL_DETECTED, &cmdq->flags))
425*acd884deSSumit Saxena 			return -ETIMEDOUT;
426*acd884deSSumit Saxena 
427*acd884deSSumit Saxena 		usleep_range(1000, 1001);
428*acd884deSSumit Saxena 
429*acd884deSSumit Saxena 		bnxt_qplib_service_creq((unsigned long)rcfw);
430*acd884deSSumit Saxena 		if (!crsqe->is_in_used)
431*acd884deSSumit Saxena 			return 0;
432*acd884deSSumit Saxena 
433*acd884deSSumit Saxena 		if (jiffies_to_msecs(jiffies - issue_time) >
434*acd884deSSumit Saxena 		    (rcfw->max_timeout * 1000)) {
435*acd884deSSumit Saxena 			dev_warn_ratelimited(&rcfw->pdev->dev,
436*acd884deSSumit Saxena 				"Self Polling QPLIB: cmdq[%#x]=%#x taken (%lu) msec",
437*acd884deSSumit Saxena 				cookie, crsqe->opcode,
438*acd884deSSumit Saxena 				(long)jiffies_to_msecs(jiffies - issue_time));
439*acd884deSSumit Saxena 			ret = bnxt_re_is_fw_stalled(rcfw, cookie);
440*acd884deSSumit Saxena 			if (ret)
441*acd884deSSumit Saxena 				return ret;
442*acd884deSSumit Saxena 		}
443*acd884deSSumit Saxena 	} while (true);
444*acd884deSSumit Saxena 
445*acd884deSSumit Saxena };
446*acd884deSSumit Saxena 
__send_message_basic_sanity(struct bnxt_qplib_rcfw * rcfw,struct bnxt_qplib_cmdqmsg * msg,u8 opcode)447*acd884deSSumit Saxena static int __send_message_basic_sanity(struct bnxt_qplib_rcfw *rcfw,
448*acd884deSSumit Saxena 			  struct bnxt_qplib_cmdqmsg *msg, u8 opcode)
449*acd884deSSumit Saxena {
450*acd884deSSumit Saxena 	struct bnxt_qplib_cmdq_ctx *cmdq;
451*acd884deSSumit Saxena 
452*acd884deSSumit Saxena 	cmdq = &rcfw->cmdq;
453*acd884deSSumit Saxena 
454*acd884deSSumit Saxena 	/* Prevent posting if f/w is not in a state to process */
455*acd884deSSumit Saxena 	if (RCFW_NO_FW_ACCESS(rcfw))
456*acd884deSSumit Saxena 		return -ENXIO;
457*acd884deSSumit Saxena 
458*acd884deSSumit Saxena 	if (test_bit(FIRMWARE_STALL_DETECTED, &cmdq->flags))
459*acd884deSSumit Saxena 		return -ETIMEDOUT;
460*acd884deSSumit Saxena 
461*acd884deSSumit Saxena 	if (test_bit(FIRMWARE_INITIALIZED_FLAG, &cmdq->flags) &&
462*acd884deSSumit Saxena 	    opcode == CMDQ_BASE_OPCODE_INITIALIZE_FW) {
463*acd884deSSumit Saxena 		dev_err(&rcfw->pdev->dev, "QPLIB: RCFW already initialized!\n");
464*acd884deSSumit Saxena 		return -EINVAL;
465*acd884deSSumit Saxena 	}
466*acd884deSSumit Saxena 
467*acd884deSSumit Saxena 	if (!test_bit(FIRMWARE_INITIALIZED_FLAG, &cmdq->flags) &&
468*acd884deSSumit Saxena 	    (opcode != CMDQ_BASE_OPCODE_QUERY_FUNC &&
469*acd884deSSumit Saxena 	     opcode != CMDQ_BASE_OPCODE_INITIALIZE_FW &&
470*acd884deSSumit Saxena 	     opcode != CMDQ_BASE_OPCODE_QUERY_VERSION)) {
471*acd884deSSumit Saxena 		dev_err(&rcfw->pdev->dev,
472*acd884deSSumit Saxena 			"QPLIB: RCFW not initialized, reject opcode 0x%x\n",
473*acd884deSSumit Saxena 			opcode);
474*acd884deSSumit Saxena 		return -ENOTSUPP;
475*acd884deSSumit Saxena 	}
476*acd884deSSumit Saxena 
477*acd884deSSumit Saxena 	return 0;
478*acd884deSSumit Saxena }
479*acd884deSSumit Saxena 
480*acd884deSSumit Saxena /* This function will just post and do not bother about completion */
__destroy_timedout_ah(struct bnxt_qplib_rcfw * rcfw,struct creq_create_ah_resp * create_ah_resp)481*acd884deSSumit Saxena static  void __destroy_timedout_ah(struct bnxt_qplib_rcfw *rcfw,
482*acd884deSSumit Saxena 			  struct creq_create_ah_resp *create_ah_resp)
483*acd884deSSumit Saxena {
484*acd884deSSumit Saxena 	struct bnxt_qplib_cmdqmsg msg = {};
485*acd884deSSumit Saxena 	struct cmdq_destroy_ah req = {};
486*acd884deSSumit Saxena 
487*acd884deSSumit Saxena 	bnxt_qplib_rcfw_cmd_prep(&req, CMDQ_BASE_OPCODE_DESTROY_AH,
488*acd884deSSumit Saxena 				 sizeof(req));
489*acd884deSSumit Saxena 	req.ah_cid = create_ah_resp->xid;
490*acd884deSSumit Saxena 	msg.req = (struct cmdq_base *)&req;
491*acd884deSSumit Saxena 	msg.req_sz = sizeof(req);
492*acd884deSSumit Saxena 	__send_message_no_waiter(rcfw, &msg);
493*acd884deSSumit Saxena 	dev_warn_ratelimited(&rcfw->pdev->dev,
494*acd884deSSumit Saxena 		"From %s: ah_cid = %d timeout_send %d\n", __func__,
495*acd884deSSumit Saxena 		req.ah_cid,
496*acd884deSSumit Saxena 		atomic_read(&rcfw->timeout_send));
497*acd884deSSumit Saxena }
498*acd884deSSumit Saxena 
499*acd884deSSumit Saxena /**
500*acd884deSSumit Saxena  * __bnxt_qplib_rcfw_send_message   -	qplib interface to send
501*acd884deSSumit Saxena  * and complete rcfw command.
502*acd884deSSumit Saxena  * @rcfw      -   rcfw channel instance of rdev
503*acd884deSSumit Saxena  * @msg      -    qplib message internal
504*acd884deSSumit Saxena  *
505*acd884deSSumit Saxena  * This function does not account shadow queue depth. It will send
506*acd884deSSumit Saxena  * all the command unconditionally as long as send queue is not full.
507*acd884deSSumit Saxena  *
508*acd884deSSumit Saxena  * Returns:
509*acd884deSSumit Saxena  * 0 if command completed by firmware.
510*acd884deSSumit Saxena  * Non zero if the command is not completed by firmware.
511*acd884deSSumit Saxena  */
__bnxt_qplib_rcfw_send_message(struct bnxt_qplib_rcfw * rcfw,struct bnxt_qplib_cmdqmsg * msg)512*acd884deSSumit Saxena static int __bnxt_qplib_rcfw_send_message(struct bnxt_qplib_rcfw *rcfw,
513*acd884deSSumit Saxena 				   struct bnxt_qplib_cmdqmsg *msg)
514*acd884deSSumit Saxena {
515*acd884deSSumit Saxena 	struct bnxt_qplib_crsqe *crsqe;
516*acd884deSSumit Saxena 	struct creq_qp_event *event;
517*acd884deSSumit Saxena 	unsigned long flags;
518*acd884deSSumit Saxena 	u16 cookie;
519*acd884deSSumit Saxena 	int rc = 0;
520*acd884deSSumit Saxena 	u8 opcode;
521*acd884deSSumit Saxena 
522*acd884deSSumit Saxena 	opcode = __get_cmdq_base_opcode(msg->req, msg->req_sz);
523*acd884deSSumit Saxena 
524*acd884deSSumit Saxena 	rc = __send_message_basic_sanity(rcfw, msg, opcode);
525*acd884deSSumit Saxena 	if (rc)
526*acd884deSSumit Saxena 		return rc == -ENXIO ? bnxt_qplib_map_rc(opcode) : rc;
527*acd884deSSumit Saxena 
528*acd884deSSumit Saxena 	rc = __send_message(rcfw, msg);
529*acd884deSSumit Saxena 	if (rc)
530*acd884deSSumit Saxena 		return rc;
531*acd884deSSumit Saxena 
532*acd884deSSumit Saxena 	cookie = le16_to_cpu(__get_cmdq_base_cookie(msg->req,
533*acd884deSSumit Saxena 				msg->req_sz)) & RCFW_MAX_COOKIE_VALUE;
534*acd884deSSumit Saxena 
535*acd884deSSumit Saxena 
536*acd884deSSumit Saxena 	if (msg->block)
537*acd884deSSumit Saxena 		rc = __block_for_resp(rcfw, cookie);
538*acd884deSSumit Saxena 	else if (atomic_read(&rcfw->rcfw_intr_enabled))
539*acd884deSSumit Saxena 		rc = __wait_for_resp(rcfw, cookie);
540*acd884deSSumit Saxena 	else
541*acd884deSSumit Saxena 		rc = __poll_for_resp(rcfw, cookie);
542*acd884deSSumit Saxena 
543*acd884deSSumit Saxena 	if (rc) {
544*acd884deSSumit Saxena 		/* First check if it is FW stall.
545*acd884deSSumit Saxena 		 * Use hwq.lock to avoid race with actual completion.
546*acd884deSSumit Saxena 		 */
547*acd884deSSumit Saxena 		spin_lock_irqsave(&rcfw->cmdq.hwq.lock, flags);
548*acd884deSSumit Saxena 		crsqe = &rcfw->crsqe_tbl[cookie];
549*acd884deSSumit Saxena 		crsqe->is_waiter_alive = false;
550*acd884deSSumit Saxena 		if (rc == -ENODEV)
551*acd884deSSumit Saxena 			set_bit(FIRMWARE_STALL_DETECTED, &rcfw->cmdq.flags);
552*acd884deSSumit Saxena 		spin_unlock_irqrestore(&rcfw->cmdq.hwq.lock, flags);
553*acd884deSSumit Saxena 
554*acd884deSSumit Saxena 		return -ETIMEDOUT;
555*acd884deSSumit Saxena 	}
556*acd884deSSumit Saxena 
557*acd884deSSumit Saxena 	event = (struct creq_qp_event *)msg->resp;
558*acd884deSSumit Saxena 	if (event->status) {
559*acd884deSSumit Saxena 		/* failed with status */
560*acd884deSSumit Saxena 		dev_err(&rcfw->pdev->dev, "QPLIB: cmdq[%#x]=%#x (%s) status %d\n",
561*acd884deSSumit Saxena 			cookie, opcode, GET_OPCODE_TYPE(opcode), event->status);
562*acd884deSSumit Saxena 		rc = -EFAULT;
563*acd884deSSumit Saxena 		/*
564*acd884deSSumit Saxena 		 * Workaround to avoid errors in the stack during bond
565*acd884deSSumit Saxena 		 * creation and deletion.
566*acd884deSSumit Saxena 		 * Disable error returned for  ADD_GID/DEL_GID
567*acd884deSSumit Saxena 		 */
568*acd884deSSumit Saxena 		if (opcode == CMDQ_BASE_OPCODE_ADD_GID ||
569*acd884deSSumit Saxena 		    opcode == CMDQ_BASE_OPCODE_DELETE_GID)
570*acd884deSSumit Saxena 			rc = 0;
571*acd884deSSumit Saxena 	}
572*acd884deSSumit Saxena 
573*acd884deSSumit Saxena 	dev_dbg(&pdev->dev, "QPLIB: %s:%d - op 0x%x (%s), cookie 0x%x -- Return: e->status 0x%x, rc = 0x%x\n",
574*acd884deSSumit Saxena 		__func__, __LINE__, opcode, GET_OPCODE_TYPE(opcode), cookie, event->status, rc);
575*acd884deSSumit Saxena 	return rc;
576*acd884deSSumit Saxena }
577*acd884deSSumit Saxena 
578*acd884deSSumit Saxena /**
579*acd884deSSumit Saxena  * bnxt_qplib_rcfw_send_message   -	qplib interface to send
580*acd884deSSumit Saxena  * and complete rcfw command.
581*acd884deSSumit Saxena  * @rcfw      -   rcfw channel instance of rdev
582*acd884deSSumit Saxena  * @msg      -    qplib message internal
583*acd884deSSumit Saxena  *
584*acd884deSSumit Saxena  * Driver interact with Firmware through rcfw channel/slow path in two ways.
585*acd884deSSumit Saxena  * a. Blocking rcfw command send. In this path, driver cannot hold
586*acd884deSSumit Saxena  * the context for longer period since it is holding cpu until
587*acd884deSSumit Saxena  * command is not completed.
588*acd884deSSumit Saxena  * b. Non-blocking rcfw command send. In this path, driver can hold the
589*acd884deSSumit Saxena  * context for longer period. There may be many pending command waiting
590*acd884deSSumit Saxena  * for completion because of non-blocking nature.
591*acd884deSSumit Saxena  *
592*acd884deSSumit Saxena  * Driver will use shadow queue depth. Current queue depth of 8K
593*acd884deSSumit Saxena  * (due to size of rcfw message it can be actual ~4K rcfw outstanding)
594*acd884deSSumit Saxena  * is not optimal for rcfw command processing in firmware.
595*acd884deSSumit Saxena  * RCFW_CMD_NON_BLOCKING_SHADOW_QD is defined as 64.
596*acd884deSSumit Saxena  * Restrict at max 64 Non-Blocking rcfw commands.
597*acd884deSSumit Saxena  * Do not allow more than 64 non-blocking command to the Firmware.
598*acd884deSSumit Saxena  * Allow all blocking commands until there is no queue full.
599*acd884deSSumit Saxena  *
600*acd884deSSumit Saxena  * Returns:
601*acd884deSSumit Saxena  * 0 if command completed by firmware.
602*acd884deSSumit Saxena  * Non zero if the command is not completed by firmware.
603*acd884deSSumit Saxena  */
bnxt_qplib_rcfw_send_message(struct bnxt_qplib_rcfw * rcfw,struct bnxt_qplib_cmdqmsg * msg)604*acd884deSSumit Saxena int bnxt_qplib_rcfw_send_message(struct bnxt_qplib_rcfw *rcfw,
605*acd884deSSumit Saxena 				 struct bnxt_qplib_cmdqmsg *msg)
606*acd884deSSumit Saxena {
607*acd884deSSumit Saxena 	int ret;
608*acd884deSSumit Saxena 
609*acd884deSSumit Saxena 	if (!msg->block) {
610*acd884deSSumit Saxena 		down(&rcfw->rcfw_inflight);
611*acd884deSSumit Saxena 		ret = __bnxt_qplib_rcfw_send_message(rcfw, msg);
612*acd884deSSumit Saxena 		up(&rcfw->rcfw_inflight);
613*acd884deSSumit Saxena 	} else {
614*acd884deSSumit Saxena 		ret = __bnxt_qplib_rcfw_send_message(rcfw, msg);
615*acd884deSSumit Saxena 	}
616*acd884deSSumit Saxena 
617*acd884deSSumit Saxena 	return ret;
618*acd884deSSumit Saxena }
619*acd884deSSumit Saxena 
bnxt_re_add_perf_stats(struct bnxt_qplib_rcfw * rcfw,struct bnxt_qplib_crsqe * crsqe)620*acd884deSSumit Saxena static void bnxt_re_add_perf_stats(struct bnxt_qplib_rcfw *rcfw,
621*acd884deSSumit Saxena 		struct bnxt_qplib_crsqe *crsqe)
622*acd884deSSumit Saxena {
623*acd884deSSumit Saxena 	u32 latency_msec, dest_stats_id;
624*acd884deSSumit Saxena 	u64 *dest_stats_ptr = NULL;
625*acd884deSSumit Saxena 
626*acd884deSSumit Saxena 	latency_msec = jiffies_to_msecs(rcfw->cmdq.last_seen -
627*acd884deSSumit Saxena 				crsqe->send_timestamp);
628*acd884deSSumit Saxena 	if (latency_msec/1000 < RCFW_MAX_LATENCY_SEC_SLAB_INDEX)
629*acd884deSSumit Saxena 		rcfw->rcfw_lat_slab_sec[latency_msec/1000]++;
630*acd884deSSumit Saxena 
631*acd884deSSumit Saxena 	if (!rcfw->sp_perf_stats_enabled)
632*acd884deSSumit Saxena 		return;
633*acd884deSSumit Saxena 
634*acd884deSSumit Saxena 	if (latency_msec < RCFW_MAX_LATENCY_MSEC_SLAB_INDEX)
635*acd884deSSumit Saxena 		rcfw->rcfw_lat_slab_msec[latency_msec]++;
636*acd884deSSumit Saxena 
637*acd884deSSumit Saxena 	switch (crsqe->opcode) {
638*acd884deSSumit Saxena 	case CMDQ_BASE_OPCODE_CREATE_QP:
639*acd884deSSumit Saxena 		dest_stats_id = rcfw->qp_create_stats_id++;
640*acd884deSSumit Saxena 		dest_stats_id = dest_stats_id % RCFW_MAX_STAT_INDEX;
641*acd884deSSumit Saxena 		dest_stats_ptr = &rcfw->qp_create_stats[dest_stats_id];
642*acd884deSSumit Saxena 		break;
643*acd884deSSumit Saxena 	case CMDQ_BASE_OPCODE_DESTROY_QP:
644*acd884deSSumit Saxena 		dest_stats_id = rcfw->qp_destroy_stats_id++;
645*acd884deSSumit Saxena 		dest_stats_id = dest_stats_id % RCFW_MAX_STAT_INDEX;
646*acd884deSSumit Saxena 		dest_stats_ptr = &rcfw->qp_destroy_stats[dest_stats_id];
647*acd884deSSumit Saxena 		break;
648*acd884deSSumit Saxena 	case CMDQ_BASE_OPCODE_REGISTER_MR:
649*acd884deSSumit Saxena 		dest_stats_id = rcfw->mr_create_stats_id++;
650*acd884deSSumit Saxena 		dest_stats_id = dest_stats_id % RCFW_MAX_STAT_INDEX;
651*acd884deSSumit Saxena 		dest_stats_ptr = &rcfw->mr_create_stats[dest_stats_id];
652*acd884deSSumit Saxena 		break;
653*acd884deSSumit Saxena 	case CMDQ_BASE_OPCODE_DEREGISTER_MR:
654*acd884deSSumit Saxena 	case CMDQ_BASE_OPCODE_DEALLOCATE_KEY:
655*acd884deSSumit Saxena 		dest_stats_id = rcfw->mr_destroy_stats_id++;
656*acd884deSSumit Saxena 		dest_stats_id = dest_stats_id % RCFW_MAX_STAT_INDEX;
657*acd884deSSumit Saxena 		dest_stats_ptr = &rcfw->mr_destroy_stats[dest_stats_id];
658*acd884deSSumit Saxena 		break;
659*acd884deSSumit Saxena 	case CMDQ_BASE_OPCODE_MODIFY_QP:
660*acd884deSSumit Saxena 		if (crsqe->requested_qp_state != IB_QPS_ERR)
661*acd884deSSumit Saxena 			break;
662*acd884deSSumit Saxena 		dest_stats_id = rcfw->qp_modify_stats_id++;
663*acd884deSSumit Saxena 		dest_stats_id = dest_stats_id % RCFW_MAX_STAT_INDEX;
664*acd884deSSumit Saxena 		dest_stats_ptr = &rcfw->qp_modify_stats[dest_stats_id];
665*acd884deSSumit Saxena 		break;
666*acd884deSSumit Saxena 	default:
667*acd884deSSumit Saxena 		break;
668*acd884deSSumit Saxena 	}
669*acd884deSSumit Saxena 	if (dest_stats_ptr)
670*acd884deSSumit Saxena 		*dest_stats_ptr = max_t(unsigned long,
671*acd884deSSumit Saxena 				(rcfw->cmdq.last_seen - crsqe->send_timestamp), 1);
672*acd884deSSumit Saxena 
673*acd884deSSumit Saxena }
674*acd884deSSumit Saxena 
675*acd884deSSumit Saxena /* Completions */
bnxt_qplib_process_qp_event(struct bnxt_qplib_rcfw * rcfw,struct creq_qp_event * event,u32 * num_wait)676*acd884deSSumit Saxena static int bnxt_qplib_process_qp_event(struct bnxt_qplib_rcfw *rcfw,
677*acd884deSSumit Saxena 				       struct creq_qp_event *event,
678*acd884deSSumit Saxena 				       u32 *num_wait)
679*acd884deSSumit Saxena {
680*acd884deSSumit Saxena 	struct bnxt_qplib_hwq *cmdq_hwq = &rcfw->cmdq.hwq;
681*acd884deSSumit Saxena 	struct creq_cq_error_notification *cqerr;
682*acd884deSSumit Saxena 	struct creq_qp_error_notification *qperr;
683*acd884deSSumit Saxena 	struct bnxt_qplib_crsqe *crsqe;
684*acd884deSSumit Saxena 	struct bnxt_qplib_reftbl *tbl;
685*acd884deSSumit Saxena 	struct bnxt_qplib_qp *qp;
686*acd884deSSumit Saxena 	struct bnxt_qplib_cq *cq;
687*acd884deSSumit Saxena 	u16 cookie, blocked = 0;
688*acd884deSSumit Saxena 	struct pci_dev *pdev;
689*acd884deSSumit Saxena 	bool is_waiter_alive;
690*acd884deSSumit Saxena 	unsigned long flags;
691*acd884deSSumit Saxena 	u32 wait_cmds = 0;
692*acd884deSSumit Saxena 	u32 xid, qp_idx;
693*acd884deSSumit Saxena 	u32 req_size;
694*acd884deSSumit Saxena 	int rc = 0;
695*acd884deSSumit Saxena 
696*acd884deSSumit Saxena 	pdev = rcfw->pdev;
697*acd884deSSumit Saxena 	switch (event->event) {
698*acd884deSSumit Saxena 	case CREQ_QP_EVENT_EVENT_QP_ERROR_NOTIFICATION:
699*acd884deSSumit Saxena 		tbl = &rcfw->res->reftbl.qpref;
700*acd884deSSumit Saxena 		qperr = (struct creq_qp_error_notification *)event;
701*acd884deSSumit Saxena 		xid = le32_to_cpu(qperr->xid);
702*acd884deSSumit Saxena 		qp_idx = map_qp_id_to_tbl_indx(xid, tbl);
703*acd884deSSumit Saxena 		spin_lock(&tbl->lock);
704*acd884deSSumit Saxena 		qp = tbl->rec[qp_idx].handle;
705*acd884deSSumit Saxena 		if (!qp) {
706*acd884deSSumit Saxena 			spin_unlock(&tbl->lock);
707*acd884deSSumit Saxena 			break;
708*acd884deSSumit Saxena 		}
709*acd884deSSumit Saxena 		bnxt_qplib_mark_qp_error(qp);
710*acd884deSSumit Saxena 		rc = rcfw->creq.aeq_handler(rcfw, event, qp);
711*acd884deSSumit Saxena 		spin_unlock(&tbl->lock);
712*acd884deSSumit Saxena 		/*
713*acd884deSSumit Saxena 		 * Keeping these prints as debug to avoid flooding of log
714*acd884deSSumit Saxena 		 * messages during modify QP to error state by applications
715*acd884deSSumit Saxena 		 */
716*acd884deSSumit Saxena 		dev_dbg(&pdev->dev, "QPLIB: QP Error encountered!\n");
717*acd884deSSumit Saxena 		dev_dbg(&pdev->dev,
718*acd884deSSumit Saxena 			"QPLIB: qpid 0x%x, req_err=0x%x, resp_err=0x%x\n",
719*acd884deSSumit Saxena 			xid, qperr->req_err_state_reason,
720*acd884deSSumit Saxena 			qperr->res_err_state_reason);
721*acd884deSSumit Saxena 		break;
722*acd884deSSumit Saxena 	case CREQ_QP_EVENT_EVENT_CQ_ERROR_NOTIFICATION:
723*acd884deSSumit Saxena 		tbl = &rcfw->res->reftbl.cqref;
724*acd884deSSumit Saxena 		cqerr = (struct creq_cq_error_notification *)event;
725*acd884deSSumit Saxena 		xid = le32_to_cpu(cqerr->xid);
726*acd884deSSumit Saxena 		spin_lock(&tbl->lock);
727*acd884deSSumit Saxena 		cq = tbl->rec[GET_TBL_INDEX(xid, tbl)].handle;
728*acd884deSSumit Saxena 		if (!cq) {
729*acd884deSSumit Saxena 			spin_unlock(&tbl->lock);
730*acd884deSSumit Saxena 			break;
731*acd884deSSumit Saxena 		}
732*acd884deSSumit Saxena 		rc = rcfw->creq.aeq_handler(rcfw, event, cq);
733*acd884deSSumit Saxena 		spin_unlock(&tbl->lock);
734*acd884deSSumit Saxena 		dev_dbg(&pdev->dev, "QPLIB: CQ error encountered!\n");
735*acd884deSSumit Saxena 		break;
736*acd884deSSumit Saxena 	default:
737*acd884deSSumit Saxena 		/*
738*acd884deSSumit Saxena 		 * Command Response
739*acd884deSSumit Saxena 		 * cmdq hwq lock needs to be acquired to synchronize
740*acd884deSSumit Saxena 		 * the command send and completion reaping. This function
741*acd884deSSumit Saxena 		 * is always called with creq hwq lock held. So there is no
742*acd884deSSumit Saxena 		 * chance of deadlock here as the locking is in correct sequence.
743*acd884deSSumit Saxena 		 * Using  the nested variant of spin_lock to annotate
744*acd884deSSumit Saxena 		 */
745*acd884deSSumit Saxena 		spin_lock_irqsave_nested(&cmdq_hwq->lock, flags,
746*acd884deSSumit Saxena 					 SINGLE_DEPTH_NESTING);
747*acd884deSSumit Saxena 		cookie = le16_to_cpu(event->cookie);
748*acd884deSSumit Saxena 		blocked = cookie & RCFW_CMD_IS_BLOCKING;
749*acd884deSSumit Saxena 		cookie &= RCFW_MAX_COOKIE_VALUE;
750*acd884deSSumit Saxena 
751*acd884deSSumit Saxena 		crsqe = &rcfw->crsqe_tbl[cookie];
752*acd884deSSumit Saxena 
753*acd884deSSumit Saxena 		bnxt_re_add_perf_stats(rcfw, crsqe);
754*acd884deSSumit Saxena 
755*acd884deSSumit Saxena 		if (WARN_ONCE(test_bit(FIRMWARE_STALL_DETECTED,
756*acd884deSSumit Saxena 				       &rcfw->cmdq.flags),
757*acd884deSSumit Saxena 		    "QPLIB: Unreponsive rcfw channel detected.!!")) {
758*acd884deSSumit Saxena 			dev_info(&pdev->dev, "rcfw timedout: cookie = %#x,"
759*acd884deSSumit Saxena 				" latency_msec = %ld free_slots = %d\n", cookie,
760*acd884deSSumit Saxena 				(long)jiffies_to_msecs(rcfw->cmdq.last_seen -
761*acd884deSSumit Saxena 						 crsqe->send_timestamp),
762*acd884deSSumit Saxena 				crsqe->free_slots);
763*acd884deSSumit Saxena 			spin_unlock_irqrestore(&cmdq_hwq->lock, flags);
764*acd884deSSumit Saxena 			return rc;
765*acd884deSSumit Saxena 		}
766*acd884deSSumit Saxena 
767*acd884deSSumit Saxena 		if (crsqe->is_internal_cmd && !event->status)
768*acd884deSSumit Saxena 			atomic_dec(&rcfw->timeout_send);
769*acd884deSSumit Saxena 
770*acd884deSSumit Saxena 		if (crsqe->is_waiter_alive) {
771*acd884deSSumit Saxena 			if (crsqe->resp)
772*acd884deSSumit Saxena 				memcpy(crsqe->resp, event, sizeof(*event));
773*acd884deSSumit Saxena 			if (!blocked)
774*acd884deSSumit Saxena 				wait_cmds++;
775*acd884deSSumit Saxena 		}
776*acd884deSSumit Saxena 
777*acd884deSSumit Saxena 		req_size = crsqe->req_size;
778*acd884deSSumit Saxena 		is_waiter_alive = crsqe->is_waiter_alive;
779*acd884deSSumit Saxena 
780*acd884deSSumit Saxena 		crsqe->req_size = 0;
781*acd884deSSumit Saxena 		if (!crsqe->is_waiter_alive)
782*acd884deSSumit Saxena 			crsqe->resp = NULL;
783*acd884deSSumit Saxena 		crsqe->is_in_used = false;
784*acd884deSSumit Saxena 		/* Consumer is updated so that __send_message_no_waiter
785*acd884deSSumit Saxena 		 * can never see queue full.
786*acd884deSSumit Saxena 		 * It is safe since we are still holding cmdq_hwq->lock.
787*acd884deSSumit Saxena 		 */
788*acd884deSSumit Saxena 		cmdq_hwq->cons += req_size;
789*acd884deSSumit Saxena 
790*acd884deSSumit Saxena 		/* This is a case to handle below scenario -
791*acd884deSSumit Saxena 		 * Create AH is completed successfully by firmware,
792*acd884deSSumit Saxena 		 * but completion took more time and driver already lost
793*acd884deSSumit Saxena 		 * the context of create_ah from caller.
794*acd884deSSumit Saxena 		 * We have already return failure for create_ah verbs,
795*acd884deSSumit Saxena 		 * so let's destroy the same address vector since it is
796*acd884deSSumit Saxena 		 * no more used in stack. We don't care about completion
797*acd884deSSumit Saxena 		 * in __send_message_no_waiter.
798*acd884deSSumit Saxena 		 * If destroy_ah is failued by firmware, there will be AH
799*acd884deSSumit Saxena 		 * resource leak and relatively not critical +  unlikely
800*acd884deSSumit Saxena 		 * scenario. Current design is not to handle such case.
801*acd884deSSumit Saxena 		 */
802*acd884deSSumit Saxena 		if (!is_waiter_alive && !event->status &&
803*acd884deSSumit Saxena 		    event->event == CREQ_QP_EVENT_EVENT_CREATE_AH)
804*acd884deSSumit Saxena 			__destroy_timedout_ah(rcfw,
805*acd884deSSumit Saxena 					      (struct creq_create_ah_resp *)
806*acd884deSSumit Saxena 					      event);
807*acd884deSSumit Saxena 
808*acd884deSSumit Saxena 		spin_unlock_irqrestore(&cmdq_hwq->lock, flags);
809*acd884deSSumit Saxena 	}
810*acd884deSSumit Saxena 	*num_wait += wait_cmds;
811*acd884deSSumit Saxena 	return rc;
812*acd884deSSumit Saxena }
813*acd884deSSumit Saxena 
814*acd884deSSumit Saxena /* SP - CREQ Completion handlers */
bnxt_qplib_service_creq(unsigned long data)815*acd884deSSumit Saxena static void bnxt_qplib_service_creq(unsigned long data)
816*acd884deSSumit Saxena {
817*acd884deSSumit Saxena 	struct bnxt_qplib_rcfw *rcfw = (struct bnxt_qplib_rcfw *)data;
818*acd884deSSumit Saxena 	struct bnxt_qplib_creq_ctx *creq = &rcfw->creq;
819*acd884deSSumit Saxena 	struct bnxt_qplib_res *res;
820*acd884deSSumit Saxena 	u32 type, budget = CREQ_ENTRY_POLL_BUDGET;
821*acd884deSSumit Saxena 	struct bnxt_qplib_hwq *creq_hwq = &creq->hwq;
822*acd884deSSumit Saxena 	struct creq_base *creqe;
823*acd884deSSumit Saxena 	struct pci_dev *pdev;
824*acd884deSSumit Saxena 	unsigned long flags;
825*acd884deSSumit Saxena 	u32 num_wakeup = 0;
826*acd884deSSumit Saxena 	int rc;
827*acd884deSSumit Saxena 
828*acd884deSSumit Saxena 	pdev = rcfw->pdev;
829*acd884deSSumit Saxena 	res = rcfw->res;
830*acd884deSSumit Saxena 	/* Service the CREQ until empty */
831*acd884deSSumit Saxena 	spin_lock_irqsave(&creq_hwq->lock, flags);
832*acd884deSSumit Saxena 	while (budget > 0) {
833*acd884deSSumit Saxena 		if (RCFW_NO_FW_ACCESS(rcfw)) {
834*acd884deSSumit Saxena 			spin_unlock_irqrestore(&creq_hwq->lock, flags);
835*acd884deSSumit Saxena 			return;
836*acd884deSSumit Saxena 		}
837*acd884deSSumit Saxena 		creqe = bnxt_qplib_get_qe(creq_hwq, creq_hwq->cons, NULL);
838*acd884deSSumit Saxena 		if (!CREQ_CMP_VALID(creqe, creq->creq_db.dbinfo.flags))
839*acd884deSSumit Saxena 			break;
840*acd884deSSumit Saxena 		/* The valid test of the entry must be done first before
841*acd884deSSumit Saxena 		 * reading any further.
842*acd884deSSumit Saxena 		 */
843*acd884deSSumit Saxena 		dma_rmb();
844*acd884deSSumit Saxena 		type = creqe->type & CREQ_BASE_TYPE_MASK;
845*acd884deSSumit Saxena 		rcfw->cmdq.last_seen = jiffies;
846*acd884deSSumit Saxena 
847*acd884deSSumit Saxena 		switch (type) {
848*acd884deSSumit Saxena 		case CREQ_BASE_TYPE_QP_EVENT:
849*acd884deSSumit Saxena 			bnxt_qplib_process_qp_event
850*acd884deSSumit Saxena 				(rcfw,(struct creq_qp_event *)creqe,
851*acd884deSSumit Saxena 				 &num_wakeup);
852*acd884deSSumit Saxena 			creq->stats.creq_qp_event_processed++;
853*acd884deSSumit Saxena 			break;
854*acd884deSSumit Saxena 		case CREQ_BASE_TYPE_FUNC_EVENT:
855*acd884deSSumit Saxena 			rc = rcfw->creq.aeq_handler(rcfw, creqe, NULL);
856*acd884deSSumit Saxena 			if (rc)
857*acd884deSSumit Saxena 				dev_warn(&pdev->dev,
858*acd884deSSumit Saxena 					 "QPLIB: async event type = 0x%x not handled",
859*acd884deSSumit Saxena 					 type);
860*acd884deSSumit Saxena 			creq->stats.creq_func_event_processed++;
861*acd884deSSumit Saxena 			break;
862*acd884deSSumit Saxena 		default:
863*acd884deSSumit Saxena 			if (type != HWRM_ASYNC_EVENT_CMPL_TYPE_HWRM_ASYNC_EVENT) {
864*acd884deSSumit Saxena 				dev_warn(&pdev->dev,
865*acd884deSSumit Saxena 					 "QPLIB: op_event = 0x%x not handled\n",
866*acd884deSSumit Saxena 					 type);
867*acd884deSSumit Saxena 			}
868*acd884deSSumit Saxena 			break;
869*acd884deSSumit Saxena 		}
870*acd884deSSumit Saxena 		budget--;
871*acd884deSSumit Saxena 		bnxt_qplib_hwq_incr_cons(creq_hwq->max_elements, &creq_hwq->cons,
872*acd884deSSumit Saxena 					 1, &creq->creq_db.dbinfo.flags);
873*acd884deSSumit Saxena 	}
874*acd884deSSumit Saxena 	if (budget == CREQ_ENTRY_POLL_BUDGET &&
875*acd884deSSumit Saxena 	    !CREQ_CMP_VALID(creqe, creq->creq_db.dbinfo.flags)) {
876*acd884deSSumit Saxena 		/* No completions received during this poll. Enable interrupt now */
877*acd884deSSumit Saxena 		bnxt_qplib_ring_nq_db(&creq->creq_db.dbinfo, res->cctx, true);
878*acd884deSSumit Saxena 		creq->stats.creq_arm_count++;
879*acd884deSSumit Saxena 		dev_dbg(&pdev->dev, "QPLIB: Num of Func (0x%llx) \n",
880*acd884deSSumit Saxena 			creq->stats.creq_func_event_processed);
881*acd884deSSumit Saxena 		dev_dbg(&pdev->dev, "QPLIB: QP (0x%llx) events processed\n",
882*acd884deSSumit Saxena 			creq->stats.creq_qp_event_processed);
883*acd884deSSumit Saxena 		dev_dbg(&pdev->dev, "QPLIB: Armed:%#llx resched:%#llx \n",
884*acd884deSSumit Saxena 			creq->stats.creq_arm_count,
885*acd884deSSumit Saxena 			creq->stats.creq_tasklet_schedule_count);
886*acd884deSSumit Saxena 	} else if (creq->requested) {
887*acd884deSSumit Saxena 		/*
888*acd884deSSumit Saxena 		 * Currently there is no bottom half implementation to process
889*acd884deSSumit Saxena 		 * completions, all completions are processed in interrupt context
890*acd884deSSumit Saxena 		 * only. So enable interrupts.
891*acd884deSSumit Saxena 		 */
892*acd884deSSumit Saxena 		bnxt_qplib_ring_nq_db(&creq->creq_db.dbinfo, res->cctx, true);
893*acd884deSSumit Saxena 		creq->stats.creq_tasklet_schedule_count++;
894*acd884deSSumit Saxena 	}
895*acd884deSSumit Saxena 	spin_unlock_irqrestore(&creq_hwq->lock, flags);
896*acd884deSSumit Saxena 	if (num_wakeup)
897*acd884deSSumit Saxena 		wake_up_all(&rcfw->cmdq.waitq);
898*acd884deSSumit Saxena }
899*acd884deSSumit Saxena 
bnxt_qplib_creq_irq(int irq,void * dev_instance)900*acd884deSSumit Saxena static irqreturn_t bnxt_qplib_creq_irq(int irq, void *dev_instance)
901*acd884deSSumit Saxena {
902*acd884deSSumit Saxena 	struct bnxt_qplib_rcfw *rcfw = dev_instance;
903*acd884deSSumit Saxena 
904*acd884deSSumit Saxena 	bnxt_qplib_service_creq((unsigned long)rcfw);
905*acd884deSSumit Saxena 	return IRQ_HANDLED;
906*acd884deSSumit Saxena }
907*acd884deSSumit Saxena 
908*acd884deSSumit Saxena /* RCFW */
bnxt_qplib_deinit_rcfw(struct bnxt_qplib_rcfw * rcfw)909*acd884deSSumit Saxena int bnxt_qplib_deinit_rcfw(struct bnxt_qplib_rcfw *rcfw)
910*acd884deSSumit Saxena {
911*acd884deSSumit Saxena 	struct creq_deinitialize_fw_resp resp = {};
912*acd884deSSumit Saxena 	struct cmdq_deinitialize_fw req = {};
913*acd884deSSumit Saxena 	struct bnxt_qplib_cmdqmsg msg = {};
914*acd884deSSumit Saxena 	int rc;
915*acd884deSSumit Saxena 
916*acd884deSSumit Saxena 	bnxt_qplib_rcfw_cmd_prep(&req, CMDQ_BASE_OPCODE_DEINITIALIZE_FW,
917*acd884deSSumit Saxena 				 sizeof(req));
918*acd884deSSumit Saxena 	bnxt_qplib_fill_cmdqmsg(&msg, &req, &resp, NULL,
919*acd884deSSumit Saxena 				sizeof(req), sizeof(resp), 0);
920*acd884deSSumit Saxena 	rc = bnxt_qplib_rcfw_send_message(rcfw, &msg);
921*acd884deSSumit Saxena 	if (rc)
922*acd884deSSumit Saxena 		return rc;
923*acd884deSSumit Saxena 	clear_bit(FIRMWARE_INITIALIZED_FLAG, &rcfw->cmdq.flags);
924*acd884deSSumit Saxena 	return 0;
925*acd884deSSumit Saxena }
926*acd884deSSumit Saxena 
bnxt_qplib_init_rcfw(struct bnxt_qplib_rcfw * rcfw,int is_virtfn)927*acd884deSSumit Saxena int bnxt_qplib_init_rcfw(struct bnxt_qplib_rcfw *rcfw, int is_virtfn)
928*acd884deSSumit Saxena {
929*acd884deSSumit Saxena 	struct creq_initialize_fw_resp resp = {};
930*acd884deSSumit Saxena 	struct cmdq_initialize_fw req = {};
931*acd884deSSumit Saxena 	struct bnxt_qplib_cmdqmsg msg = {};
932*acd884deSSumit Saxena 	struct bnxt_qplib_chip_ctx *cctx;
933*acd884deSSumit Saxena 	struct bnxt_qplib_ctx *hctx;
934*acd884deSSumit Saxena 	struct bnxt_qplib_res *res;
935*acd884deSSumit Saxena 	struct bnxt_qplib_hwq *hwq;
936*acd884deSSumit Saxena 	int rc;
937*acd884deSSumit Saxena 
938*acd884deSSumit Saxena 	res = rcfw->res;
939*acd884deSSumit Saxena 	cctx = res->cctx;
940*acd884deSSumit Saxena 	hctx = res->hctx;
941*acd884deSSumit Saxena 
942*acd884deSSumit Saxena 	bnxt_qplib_rcfw_cmd_prep(&req, CMDQ_BASE_OPCODE_INITIALIZE_FW,
943*acd884deSSumit Saxena 				 sizeof(req));
944*acd884deSSumit Saxena 	/* Supply (log-base-2-of-host-page-size - base-page-shift)
945*acd884deSSumit Saxena 	 * to bono to adjust the doorbell page sizes.
946*acd884deSSumit Saxena 	 */
947*acd884deSSumit Saxena 	req.log2_dbr_pg_size = cpu_to_le16(PAGE_SHIFT -
948*acd884deSSumit Saxena 					   RCFW_DBR_BASE_PAGE_SHIFT);
949*acd884deSSumit Saxena 	/*
950*acd884deSSumit Saxena 	 * VFs need not setup the HW context area, PF
951*acd884deSSumit Saxena 	 * shall setup this area for VF. Skipping the
952*acd884deSSumit Saxena 	 * HW programming
953*acd884deSSumit Saxena 	 */
954*acd884deSSumit Saxena 	if (is_virtfn || _is_chip_gen_p5_p7(cctx))
955*acd884deSSumit Saxena 		goto skip_ctx_setup;
956*acd884deSSumit Saxena 
957*acd884deSSumit Saxena 	hwq = &hctx->qp_ctx.hwq;
958*acd884deSSumit Saxena 	req.qpc_page_dir = cpu_to_le64(_get_base_addr(hwq));
959*acd884deSSumit Saxena 	req.number_of_qp = cpu_to_le32(hwq->max_elements);
960*acd884deSSumit Saxena 	req.qpc_pg_size_qpc_lvl = (_get_pte_pg_size(hwq) <<
961*acd884deSSumit Saxena 				   CMDQ_INITIALIZE_FW_QPC_PG_SIZE_SFT) |
962*acd884deSSumit Saxena 				   (u8)hwq->level;
963*acd884deSSumit Saxena 
964*acd884deSSumit Saxena 	hwq = &hctx->mrw_ctx.hwq;
965*acd884deSSumit Saxena 	req.mrw_page_dir = cpu_to_le64(_get_base_addr(hwq));
966*acd884deSSumit Saxena 	req.number_of_mrw = cpu_to_le32(hwq->max_elements);
967*acd884deSSumit Saxena 	req.mrw_pg_size_mrw_lvl = (_get_pte_pg_size(hwq) <<
968*acd884deSSumit Saxena 				   CMDQ_INITIALIZE_FW_MRW_PG_SIZE_SFT) |
969*acd884deSSumit Saxena 				   (u8)hwq->level;
970*acd884deSSumit Saxena 
971*acd884deSSumit Saxena 	hwq = &hctx->srq_ctx.hwq;
972*acd884deSSumit Saxena 	req.srq_page_dir = cpu_to_le64(_get_base_addr(hwq));
973*acd884deSSumit Saxena 	req.number_of_srq = cpu_to_le32(hwq->max_elements);
974*acd884deSSumit Saxena 	req.srq_pg_size_srq_lvl = (_get_pte_pg_size(hwq) <<
975*acd884deSSumit Saxena 				   CMDQ_INITIALIZE_FW_SRQ_PG_SIZE_SFT) |
976*acd884deSSumit Saxena 				   (u8)hwq->level;
977*acd884deSSumit Saxena 
978*acd884deSSumit Saxena 	hwq = &hctx->cq_ctx.hwq;
979*acd884deSSumit Saxena 	req.cq_page_dir = cpu_to_le64(_get_base_addr(hwq));
980*acd884deSSumit Saxena 	req.number_of_cq = cpu_to_le32(hwq->max_elements);
981*acd884deSSumit Saxena 	req.cq_pg_size_cq_lvl = (_get_pte_pg_size(hwq) <<
982*acd884deSSumit Saxena 				 CMDQ_INITIALIZE_FW_CQ_PG_SIZE_SFT) |
983*acd884deSSumit Saxena 				 (u8)hwq->level;
984*acd884deSSumit Saxena 
985*acd884deSSumit Saxena 	hwq = &hctx->tim_ctx.hwq;
986*acd884deSSumit Saxena 	req.tim_page_dir = cpu_to_le64(_get_base_addr(hwq));
987*acd884deSSumit Saxena 	req.tim_pg_size_tim_lvl = (_get_pte_pg_size(hwq) <<
988*acd884deSSumit Saxena 				   CMDQ_INITIALIZE_FW_TIM_PG_SIZE_SFT) |
989*acd884deSSumit Saxena 				   (u8)hwq->level;
990*acd884deSSumit Saxena 	hwq = &hctx->tqm_ctx.pde;
991*acd884deSSumit Saxena 	req.tqm_page_dir = cpu_to_le64(_get_base_addr(hwq));
992*acd884deSSumit Saxena 	req.tqm_pg_size_tqm_lvl = (_get_pte_pg_size(hwq) <<
993*acd884deSSumit Saxena 				   CMDQ_INITIALIZE_FW_TQM_PG_SIZE_SFT) |
994*acd884deSSumit Saxena 				   (u8)hwq->level;
995*acd884deSSumit Saxena skip_ctx_setup:
996*acd884deSSumit Saxena 	if (BNXT_RE_HW_RETX(res->dattr->dev_cap_flags))
997*acd884deSSumit Saxena 		req.flags |= CMDQ_INITIALIZE_FW_FLAGS_HW_REQUESTER_RETX_SUPPORTED;
998*acd884deSSumit Saxena 	req.stat_ctx_id = cpu_to_le32(hctx->stats.fw_id);
999*acd884deSSumit Saxena 	bnxt_qplib_fill_cmdqmsg(&msg, &req, &resp, NULL,
1000*acd884deSSumit Saxena 				sizeof(req), sizeof(resp), 0);
1001*acd884deSSumit Saxena 	rc = bnxt_qplib_rcfw_send_message(rcfw, &msg);
1002*acd884deSSumit Saxena 	if (rc)
1003*acd884deSSumit Saxena 		return rc;
1004*acd884deSSumit Saxena 	set_bit(FIRMWARE_INITIALIZED_FLAG, &rcfw->cmdq.flags);
1005*acd884deSSumit Saxena 
1006*acd884deSSumit Saxena 	return 0;
1007*acd884deSSumit Saxena }
1008*acd884deSSumit Saxena 
bnxt_qplib_free_rcfw_channel(struct bnxt_qplib_res * res)1009*acd884deSSumit Saxena void bnxt_qplib_free_rcfw_channel(struct bnxt_qplib_res *res)
1010*acd884deSSumit Saxena {
1011*acd884deSSumit Saxena 	struct bnxt_qplib_rcfw *rcfw = res->rcfw;
1012*acd884deSSumit Saxena 
1013*acd884deSSumit Saxena 	vfree(rcfw->rcfw_lat_slab_msec);
1014*acd884deSSumit Saxena 	rcfw->rcfw_lat_slab_msec = NULL;
1015*acd884deSSumit Saxena 	vfree(rcfw->qp_create_stats);
1016*acd884deSSumit Saxena 	rcfw->qp_create_stats = NULL;
1017*acd884deSSumit Saxena 	vfree(rcfw->qp_destroy_stats);
1018*acd884deSSumit Saxena 	rcfw->qp_destroy_stats = NULL;
1019*acd884deSSumit Saxena 	vfree(rcfw->mr_create_stats);
1020*acd884deSSumit Saxena 	rcfw->mr_create_stats = NULL;
1021*acd884deSSumit Saxena 	vfree(rcfw->mr_destroy_stats);
1022*acd884deSSumit Saxena 	rcfw->mr_destroy_stats = NULL;
1023*acd884deSSumit Saxena 	vfree(rcfw->qp_modify_stats);
1024*acd884deSSumit Saxena 	rcfw->qp_modify_stats = NULL;
1025*acd884deSSumit Saxena 	rcfw->sp_perf_stats_enabled = false;
1026*acd884deSSumit Saxena 
1027*acd884deSSumit Saxena 	kfree(rcfw->crsqe_tbl);
1028*acd884deSSumit Saxena 	rcfw->crsqe_tbl = NULL;
1029*acd884deSSumit Saxena 
1030*acd884deSSumit Saxena 	bnxt_qplib_free_hwq(res, &rcfw->cmdq.hwq);
1031*acd884deSSumit Saxena 	bnxt_qplib_free_hwq(res, &rcfw->creq.hwq);
1032*acd884deSSumit Saxena 	rcfw->pdev = NULL;
1033*acd884deSSumit Saxena }
1034*acd884deSSumit Saxena 
bnxt_qplib_alloc_rcfw_channel(struct bnxt_qplib_res * res)1035*acd884deSSumit Saxena int bnxt_qplib_alloc_rcfw_channel(struct bnxt_qplib_res *res)
1036*acd884deSSumit Saxena {
1037*acd884deSSumit Saxena 	struct bnxt_qplib_hwq_attr hwq_attr = {};
1038*acd884deSSumit Saxena 	struct bnxt_qplib_rcfw *rcfw = res->rcfw;
1039*acd884deSSumit Saxena 	struct bnxt_qplib_sg_info sginfo = {};
1040*acd884deSSumit Saxena 	struct bnxt_qplib_cmdq_ctx *cmdq;
1041*acd884deSSumit Saxena 	struct bnxt_qplib_creq_ctx *creq;
1042*acd884deSSumit Saxena 
1043*acd884deSSumit Saxena 	rcfw->pdev = res->pdev;
1044*acd884deSSumit Saxena 	rcfw->res = res;
1045*acd884deSSumit Saxena 	cmdq = &rcfw->cmdq;
1046*acd884deSSumit Saxena 	creq = &rcfw->creq;
1047*acd884deSSumit Saxena 
1048*acd884deSSumit Saxena 	sginfo.pgsize = PAGE_SIZE;
1049*acd884deSSumit Saxena 	sginfo.pgshft = PAGE_SHIFT;
1050*acd884deSSumit Saxena 
1051*acd884deSSumit Saxena 	hwq_attr.sginfo = &sginfo;
1052*acd884deSSumit Saxena 	hwq_attr.res = rcfw->res;
1053*acd884deSSumit Saxena 	hwq_attr.depth = BNXT_QPLIB_CREQE_MAX_CNT;
1054*acd884deSSumit Saxena 	hwq_attr.stride = BNXT_QPLIB_CREQE_UNITS;
1055*acd884deSSumit Saxena 	hwq_attr.type = _get_hwq_type(res);
1056*acd884deSSumit Saxena 
1057*acd884deSSumit Saxena 	if (bnxt_qplib_alloc_init_hwq(&creq->hwq, &hwq_attr)) {
1058*acd884deSSumit Saxena 		dev_err(&rcfw->pdev->dev,
1059*acd884deSSumit Saxena 			"QPLIB: HW channel CREQ allocation failed\n");
1060*acd884deSSumit Saxena 		return -ENOMEM;
1061*acd884deSSumit Saxena 	}
1062*acd884deSSumit Saxena 
1063*acd884deSSumit Saxena 	sginfo.pgsize = BNXT_QPLIB_CMDQE_PAGE_SIZE;
1064*acd884deSSumit Saxena 	hwq_attr.depth = BNXT_QPLIB_CMDQE_MAX_CNT & 0x7FFFFFFF;
1065*acd884deSSumit Saxena 	hwq_attr.stride = BNXT_QPLIB_CMDQE_UNITS;
1066*acd884deSSumit Saxena 	hwq_attr.type = HWQ_TYPE_CTX;
1067*acd884deSSumit Saxena 	if (bnxt_qplib_alloc_init_hwq(&cmdq->hwq, &hwq_attr)) {
1068*acd884deSSumit Saxena 		dev_err(&rcfw->pdev->dev,
1069*acd884deSSumit Saxena 			"QPLIB: HW channel CMDQ allocation failed\n");
1070*acd884deSSumit Saxena 		goto fail_free_creq_hwq;
1071*acd884deSSumit Saxena 	}
1072*acd884deSSumit Saxena 
1073*acd884deSSumit Saxena 	rcfw->crsqe_tbl = kcalloc(cmdq->hwq.max_elements,
1074*acd884deSSumit Saxena 			sizeof(*rcfw->crsqe_tbl), GFP_KERNEL);
1075*acd884deSSumit Saxena 	if (!rcfw->crsqe_tbl) {
1076*acd884deSSumit Saxena 		dev_err(&rcfw->pdev->dev,
1077*acd884deSSumit Saxena 			"QPLIB: HW channel CRSQ allocation failed\n");
1078*acd884deSSumit Saxena 		goto fail_free_cmdq_hwq;
1079*acd884deSSumit Saxena 	}
1080*acd884deSSumit Saxena 
1081*acd884deSSumit Saxena 	rcfw->max_timeout = res->cctx->hwrm_cmd_max_timeout;
1082*acd884deSSumit Saxena 
1083*acd884deSSumit Saxena 	rcfw->sp_perf_stats_enabled = false;
1084*acd884deSSumit Saxena 	rcfw->rcfw_lat_slab_msec = vzalloc(sizeof(u32) *
1085*acd884deSSumit Saxena 					   RCFW_MAX_LATENCY_MSEC_SLAB_INDEX);
1086*acd884deSSumit Saxena 	rcfw->qp_create_stats = vzalloc(sizeof(u64) * RCFW_MAX_STAT_INDEX);
1087*acd884deSSumit Saxena 	rcfw->qp_destroy_stats = vzalloc(sizeof(u64) * RCFW_MAX_STAT_INDEX);
1088*acd884deSSumit Saxena 	rcfw->mr_create_stats = vzalloc(sizeof(u64) * RCFW_MAX_STAT_INDEX);
1089*acd884deSSumit Saxena 	rcfw->mr_destroy_stats = vzalloc(sizeof(u64) * RCFW_MAX_STAT_INDEX);
1090*acd884deSSumit Saxena 	rcfw->qp_modify_stats = vzalloc(sizeof(u64) * RCFW_MAX_STAT_INDEX);
1091*acd884deSSumit Saxena 
1092*acd884deSSumit Saxena 	if (rcfw->rcfw_lat_slab_msec &&
1093*acd884deSSumit Saxena 	    rcfw->qp_create_stats &&
1094*acd884deSSumit Saxena 	    rcfw->qp_destroy_stats &&
1095*acd884deSSumit Saxena 	    rcfw->mr_create_stats &&
1096*acd884deSSumit Saxena 	    rcfw->mr_destroy_stats &&
1097*acd884deSSumit Saxena 	    rcfw->qp_modify_stats)
1098*acd884deSSumit Saxena 		rcfw->sp_perf_stats_enabled = true;
1099*acd884deSSumit Saxena 
1100*acd884deSSumit Saxena 	return 0;
1101*acd884deSSumit Saxena fail_free_cmdq_hwq:
1102*acd884deSSumit Saxena 	bnxt_qplib_free_hwq(res, &rcfw->cmdq.hwq);
1103*acd884deSSumit Saxena fail_free_creq_hwq:
1104*acd884deSSumit Saxena 	bnxt_qplib_free_hwq(res, &rcfw->creq.hwq);
1105*acd884deSSumit Saxena 	return -ENOMEM;
1106*acd884deSSumit Saxena }
1107*acd884deSSumit Saxena 
bnxt_qplib_rcfw_stop_irq(struct bnxt_qplib_rcfw * rcfw,bool kill)1108*acd884deSSumit Saxena void bnxt_qplib_rcfw_stop_irq(struct bnxt_qplib_rcfw *rcfw, bool kill)
1109*acd884deSSumit Saxena {
1110*acd884deSSumit Saxena 	struct bnxt_qplib_creq_ctx *creq;
1111*acd884deSSumit Saxena 	struct bnxt_qplib_res *res;
1112*acd884deSSumit Saxena 
1113*acd884deSSumit Saxena 	creq = &rcfw->creq;
1114*acd884deSSumit Saxena 	res = rcfw->res;
1115*acd884deSSumit Saxena 
1116*acd884deSSumit Saxena 	if (!creq->requested)
1117*acd884deSSumit Saxena 		return;
1118*acd884deSSumit Saxena 
1119*acd884deSSumit Saxena 	creq->requested = false;
1120*acd884deSSumit Saxena 	/* Mask h/w interrupts */
1121*acd884deSSumit Saxena 	bnxt_qplib_ring_nq_db(&creq->creq_db.dbinfo, res->cctx, false);
1122*acd884deSSumit Saxena 	/* Sync with last running IRQ-handler */
1123*acd884deSSumit Saxena 	synchronize_irq(creq->msix_vec);
1124*acd884deSSumit Saxena 	free_irq(creq->msix_vec, rcfw);
1125*acd884deSSumit Saxena 	kfree(creq->irq_name);
1126*acd884deSSumit Saxena 	creq->irq_name = NULL;
1127*acd884deSSumit Saxena 	/* rcfw_intr_enabled should not be greater than 1. Debug
1128*acd884deSSumit Saxena 	 * print to check if that is the case
1129*acd884deSSumit Saxena 	 */
1130*acd884deSSumit Saxena 	if (atomic_read(&rcfw->rcfw_intr_enabled) > 1) {
1131*acd884deSSumit Saxena 		dev_err(&rcfw->pdev->dev,
1132*acd884deSSumit Saxena 			"%s: rcfw->rcfw_intr_enabled = 0x%x\n", __func__,
1133*acd884deSSumit Saxena 			atomic_read(&rcfw->rcfw_intr_enabled));
1134*acd884deSSumit Saxena 	}
1135*acd884deSSumit Saxena 	atomic_set(&rcfw->rcfw_intr_enabled, 0);
1136*acd884deSSumit Saxena 	rcfw->num_irq_stopped++;
1137*acd884deSSumit Saxena }
1138*acd884deSSumit Saxena 
bnxt_qplib_disable_rcfw_channel(struct bnxt_qplib_rcfw * rcfw)1139*acd884deSSumit Saxena void bnxt_qplib_disable_rcfw_channel(struct bnxt_qplib_rcfw *rcfw)
1140*acd884deSSumit Saxena {
1141*acd884deSSumit Saxena 	struct bnxt_qplib_creq_ctx *creq;
1142*acd884deSSumit Saxena 	struct bnxt_qplib_cmdq_ctx *cmdq;
1143*acd884deSSumit Saxena 
1144*acd884deSSumit Saxena 	creq = &rcfw->creq;
1145*acd884deSSumit Saxena 	cmdq = &rcfw->cmdq;
1146*acd884deSSumit Saxena 	/* Make sure the HW channel is stopped! */
1147*acd884deSSumit Saxena 	bnxt_qplib_rcfw_stop_irq(rcfw, true);
1148*acd884deSSumit Saxena 
1149*acd884deSSumit Saxena 	creq->creq_db.reg.bar_reg = NULL;
1150*acd884deSSumit Saxena 	creq->creq_db.db = NULL;
1151*acd884deSSumit Saxena 
1152*acd884deSSumit Saxena 	if (cmdq->cmdq_mbox.reg.bar_reg) {
1153*acd884deSSumit Saxena 		iounmap(cmdq->cmdq_mbox.reg.bar_reg);
1154*acd884deSSumit Saxena 		cmdq->cmdq_mbox.reg.bar_reg = NULL;
1155*acd884deSSumit Saxena 		cmdq->cmdq_mbox.prod = NULL;
1156*acd884deSSumit Saxena 		cmdq->cmdq_mbox.db = NULL;
1157*acd884deSSumit Saxena 	}
1158*acd884deSSumit Saxena 
1159*acd884deSSumit Saxena 	creq->aeq_handler = NULL;
1160*acd884deSSumit Saxena 	creq->msix_vec = 0;
1161*acd884deSSumit Saxena }
1162*acd884deSSumit Saxena 
bnxt_qplib_rcfw_start_irq(struct bnxt_qplib_rcfw * rcfw,int msix_vector,bool need_init)1163*acd884deSSumit Saxena int bnxt_qplib_rcfw_start_irq(struct bnxt_qplib_rcfw *rcfw, int msix_vector,
1164*acd884deSSumit Saxena 			      bool need_init)
1165*acd884deSSumit Saxena {
1166*acd884deSSumit Saxena 	struct bnxt_qplib_creq_ctx *creq;
1167*acd884deSSumit Saxena 	struct bnxt_qplib_res *res;
1168*acd884deSSumit Saxena 	int rc;
1169*acd884deSSumit Saxena 
1170*acd884deSSumit Saxena 	creq = &rcfw->creq;
1171*acd884deSSumit Saxena 	res = rcfw->res;
1172*acd884deSSumit Saxena 
1173*acd884deSSumit Saxena 	if (creq->requested)
1174*acd884deSSumit Saxena 		return -EFAULT;
1175*acd884deSSumit Saxena 
1176*acd884deSSumit Saxena 	creq->msix_vec = msix_vector;
1177*acd884deSSumit Saxena 
1178*acd884deSSumit Saxena 	creq->irq_name = kasprintf(GFP_KERNEL, "bnxt_re-creq@pci:%s\n",
1179*acd884deSSumit Saxena 				   pci_name(res->pdev));
1180*acd884deSSumit Saxena 	if (!creq->irq_name)
1181*acd884deSSumit Saxena 		return -ENOMEM;
1182*acd884deSSumit Saxena 
1183*acd884deSSumit Saxena 	rc = request_irq(creq->msix_vec, bnxt_qplib_creq_irq, 0,
1184*acd884deSSumit Saxena 			 creq->irq_name, rcfw);
1185*acd884deSSumit Saxena 	if (rc) {
1186*acd884deSSumit Saxena 		kfree(creq->irq_name);
1187*acd884deSSumit Saxena 		creq->irq_name = NULL;
1188*acd884deSSumit Saxena 		return rc;
1189*acd884deSSumit Saxena 	}
1190*acd884deSSumit Saxena 	creq->requested = true;
1191*acd884deSSumit Saxena 
1192*acd884deSSumit Saxena 	bnxt_qplib_ring_nq_db(&creq->creq_db.dbinfo, res->cctx, true);
1193*acd884deSSumit Saxena 
1194*acd884deSSumit Saxena 	rcfw->num_irq_started++;
1195*acd884deSSumit Saxena 	/* Debug print to check rcfw interrupt enable/disable is invoked
1196*acd884deSSumit Saxena 	 * out of sequence
1197*acd884deSSumit Saxena 	 */
1198*acd884deSSumit Saxena 	if (atomic_read(&rcfw->rcfw_intr_enabled) > 0) {
1199*acd884deSSumit Saxena 		dev_err(&rcfw->pdev->dev,
1200*acd884deSSumit Saxena 			"%s: rcfw->rcfw_intr_enabled = 0x%x\n", __func__,
1201*acd884deSSumit Saxena 			atomic_read(&rcfw->rcfw_intr_enabled));
1202*acd884deSSumit Saxena 	}
1203*acd884deSSumit Saxena 	atomic_inc(&rcfw->rcfw_intr_enabled);
1204*acd884deSSumit Saxena 	return 0;
1205*acd884deSSumit Saxena }
1206*acd884deSSumit Saxena 
bnxt_qplib_map_cmdq_mbox(struct bnxt_qplib_rcfw * rcfw)1207*acd884deSSumit Saxena static int bnxt_qplib_map_cmdq_mbox(struct bnxt_qplib_rcfw *rcfw)
1208*acd884deSSumit Saxena {
1209*acd884deSSumit Saxena 	struct bnxt_qplib_cmdq_mbox *mbox;
1210*acd884deSSumit Saxena 	resource_size_t bar_reg;
1211*acd884deSSumit Saxena 	struct pci_dev *pdev;
1212*acd884deSSumit Saxena 
1213*acd884deSSumit Saxena 	pdev = rcfw->pdev;
1214*acd884deSSumit Saxena 	mbox = &rcfw->cmdq.cmdq_mbox;
1215*acd884deSSumit Saxena 
1216*acd884deSSumit Saxena 	mbox->reg.bar_id = RCFW_COMM_PCI_BAR_REGION;
1217*acd884deSSumit Saxena 	mbox->reg.len = RCFW_COMM_SIZE;
1218*acd884deSSumit Saxena 	mbox->reg.bar_base = pci_resource_start(pdev, mbox->reg.bar_id);
1219*acd884deSSumit Saxena 	if (!mbox->reg.bar_base) {
1220*acd884deSSumit Saxena 		dev_err(&pdev->dev,
1221*acd884deSSumit Saxena 			"QPLIB: CMDQ BAR region %d resc start is 0!\n",
1222*acd884deSSumit Saxena 			mbox->reg.bar_id);
1223*acd884deSSumit Saxena 		return -ENOMEM;
1224*acd884deSSumit Saxena 	}
1225*acd884deSSumit Saxena 
1226*acd884deSSumit Saxena 	bar_reg = mbox->reg.bar_base + RCFW_COMM_BASE_OFFSET;
1227*acd884deSSumit Saxena 	mbox->reg.len = RCFW_COMM_SIZE;
1228*acd884deSSumit Saxena 	mbox->reg.bar_reg = ioremap(bar_reg, mbox->reg.len);
1229*acd884deSSumit Saxena 	if (!mbox->reg.bar_reg) {
1230*acd884deSSumit Saxena 		dev_err(&pdev->dev,
1231*acd884deSSumit Saxena 			"QPLIB: CMDQ BAR region %d mapping failed\n",
1232*acd884deSSumit Saxena 			mbox->reg.bar_id);
1233*acd884deSSumit Saxena 		return -ENOMEM;
1234*acd884deSSumit Saxena 	}
1235*acd884deSSumit Saxena 
1236*acd884deSSumit Saxena 	mbox->prod = (void  __iomem *)((char *)mbox->reg.bar_reg +
1237*acd884deSSumit Saxena 					RCFW_PF_VF_COMM_PROD_OFFSET);
1238*acd884deSSumit Saxena 	mbox->db = (void __iomem *)((char *)mbox->reg.bar_reg +
1239*acd884deSSumit Saxena 				     RCFW_COMM_TRIG_OFFSET);
1240*acd884deSSumit Saxena 	return 0;
1241*acd884deSSumit Saxena }
1242*acd884deSSumit Saxena 
bnxt_qplib_map_creq_db(struct bnxt_qplib_rcfw * rcfw,u32 reg_offt)1243*acd884deSSumit Saxena static int bnxt_qplib_map_creq_db(struct bnxt_qplib_rcfw *rcfw, u32 reg_offt)
1244*acd884deSSumit Saxena {
1245*acd884deSSumit Saxena 	struct bnxt_qplib_creq_db *creq_db;
1246*acd884deSSumit Saxena 	struct bnxt_qplib_reg_desc *dbreg;
1247*acd884deSSumit Saxena 	struct bnxt_qplib_res *res;
1248*acd884deSSumit Saxena 
1249*acd884deSSumit Saxena 	res = rcfw->res;
1250*acd884deSSumit Saxena 	creq_db = &rcfw->creq.creq_db;
1251*acd884deSSumit Saxena 	dbreg = &res->dpi_tbl.ucreg;
1252*acd884deSSumit Saxena 
1253*acd884deSSumit Saxena 	creq_db->reg.bar_id = dbreg->bar_id;
1254*acd884deSSumit Saxena 	creq_db->reg.bar_base = dbreg->bar_base;
1255*acd884deSSumit Saxena 	creq_db->reg.bar_reg = dbreg->bar_reg + reg_offt;
1256*acd884deSSumit Saxena 	creq_db->reg.len = _is_chip_gen_p5_p7(res->cctx) ? sizeof(u64) :
1257*acd884deSSumit Saxena 							sizeof(u32);
1258*acd884deSSumit Saxena 
1259*acd884deSSumit Saxena 	creq_db->dbinfo.db = creq_db->reg.bar_reg;
1260*acd884deSSumit Saxena 	creq_db->dbinfo.hwq = &rcfw->creq.hwq;
1261*acd884deSSumit Saxena 	creq_db->dbinfo.xid = rcfw->creq.ring_id;
1262*acd884deSSumit Saxena 	creq_db->dbinfo.seed = rcfw->creq.ring_id;
1263*acd884deSSumit Saxena 	creq_db->dbinfo.flags = 0;
1264*acd884deSSumit Saxena 	spin_lock_init(&creq_db->dbinfo.lock);
1265*acd884deSSumit Saxena 	creq_db->dbinfo.shadow_key = BNXT_QPLIB_DBR_KEY_INVALID;
1266*acd884deSSumit Saxena 	creq_db->dbinfo.res = rcfw->res;
1267*acd884deSSumit Saxena 
1268*acd884deSSumit Saxena 	return 0;
1269*acd884deSSumit Saxena }
1270*acd884deSSumit Saxena 
bnxt_qplib_start_rcfw(struct bnxt_qplib_rcfw * rcfw)1271*acd884deSSumit Saxena static void bnxt_qplib_start_rcfw(struct bnxt_qplib_rcfw *rcfw)
1272*acd884deSSumit Saxena {
1273*acd884deSSumit Saxena 	struct bnxt_qplib_cmdq_ctx *cmdq;
1274*acd884deSSumit Saxena 	struct bnxt_qplib_creq_ctx *creq;
1275*acd884deSSumit Saxena 	struct bnxt_qplib_cmdq_mbox *mbox;
1276*acd884deSSumit Saxena 	struct cmdq_init init = {0};
1277*acd884deSSumit Saxena 
1278*acd884deSSumit Saxena 	cmdq = &rcfw->cmdq;
1279*acd884deSSumit Saxena 	creq = &rcfw->creq;
1280*acd884deSSumit Saxena 	mbox = &cmdq->cmdq_mbox;
1281*acd884deSSumit Saxena 
1282*acd884deSSumit Saxena 	init.cmdq_pbl = cpu_to_le64(cmdq->hwq.pbl[PBL_LVL_0].pg_map_arr[0]);
1283*acd884deSSumit Saxena 	init.cmdq_size_cmdq_lvl = cpu_to_le16(
1284*acd884deSSumit Saxena 			((BNXT_QPLIB_CMDQE_MAX_CNT << CMDQ_INIT_CMDQ_SIZE_SFT) &
1285*acd884deSSumit Saxena 			 CMDQ_INIT_CMDQ_SIZE_MASK) |
1286*acd884deSSumit Saxena 			((cmdq->hwq.level << CMDQ_INIT_CMDQ_LVL_SFT) &
1287*acd884deSSumit Saxena 			 CMDQ_INIT_CMDQ_LVL_MASK));
1288*acd884deSSumit Saxena 	init.creq_ring_id = cpu_to_le16(creq->ring_id);
1289*acd884deSSumit Saxena 	/* Write to the Bono mailbox register */
1290*acd884deSSumit Saxena 	__iowrite32_copy(mbox->reg.bar_reg, &init, sizeof(init) / 4);
1291*acd884deSSumit Saxena }
1292*acd884deSSumit Saxena 
bnxt_qplib_enable_rcfw_channel(struct bnxt_qplib_rcfw * rcfw,int msix_vector,int cp_bar_reg_off,aeq_handler_t aeq_handler)1293*acd884deSSumit Saxena int bnxt_qplib_enable_rcfw_channel(struct bnxt_qplib_rcfw *rcfw,
1294*acd884deSSumit Saxena 				   int msix_vector,
1295*acd884deSSumit Saxena 				   int cp_bar_reg_off,
1296*acd884deSSumit Saxena 				   aeq_handler_t aeq_handler)
1297*acd884deSSumit Saxena {
1298*acd884deSSumit Saxena 	struct bnxt_qplib_cmdq_ctx *cmdq;
1299*acd884deSSumit Saxena 	struct bnxt_qplib_creq_ctx *creq;
1300*acd884deSSumit Saxena 	int rc;
1301*acd884deSSumit Saxena 
1302*acd884deSSumit Saxena 	cmdq = &rcfw->cmdq;
1303*acd884deSSumit Saxena 	creq = &rcfw->creq;
1304*acd884deSSumit Saxena 
1305*acd884deSSumit Saxena 	/* Clear to defaults */
1306*acd884deSSumit Saxena 	cmdq->seq_num = 0;
1307*acd884deSSumit Saxena 	set_bit(FIRMWARE_FIRST_FLAG, &cmdq->flags);
1308*acd884deSSumit Saxena 	init_waitqueue_head(&cmdq->waitq);
1309*acd884deSSumit Saxena 
1310*acd884deSSumit Saxena 	creq->stats.creq_qp_event_processed = 0;
1311*acd884deSSumit Saxena 	creq->stats.creq_func_event_processed = 0;
1312*acd884deSSumit Saxena 	creq->aeq_handler = aeq_handler;
1313*acd884deSSumit Saxena 
1314*acd884deSSumit Saxena 	rc = bnxt_qplib_map_cmdq_mbox(rcfw);
1315*acd884deSSumit Saxena 	if (rc)
1316*acd884deSSumit Saxena 		return rc;
1317*acd884deSSumit Saxena 
1318*acd884deSSumit Saxena 	rc = bnxt_qplib_map_creq_db(rcfw, cp_bar_reg_off);
1319*acd884deSSumit Saxena 	if (rc)
1320*acd884deSSumit Saxena 		return rc;
1321*acd884deSSumit Saxena 
1322*acd884deSSumit Saxena 	rc = bnxt_qplib_rcfw_start_irq(rcfw, msix_vector, true);
1323*acd884deSSumit Saxena 	if (rc) {
1324*acd884deSSumit Saxena 		dev_err(&rcfw->pdev->dev,
1325*acd884deSSumit Saxena 			"QPLIB: Failed to request IRQ for CREQ rc = 0x%x\n", rc);
1326*acd884deSSumit Saxena 		bnxt_qplib_disable_rcfw_channel(rcfw);
1327*acd884deSSumit Saxena 		return rc;
1328*acd884deSSumit Saxena 	}
1329*acd884deSSumit Saxena 
1330*acd884deSSumit Saxena 	rcfw->curr_shadow_qd = min_not_zero(cmdq_shadow_qd,
1331*acd884deSSumit Saxena 					    (unsigned int)RCFW_CMD_NON_BLOCKING_SHADOW_QD);
1332*acd884deSSumit Saxena 	sema_init(&rcfw->rcfw_inflight, rcfw->curr_shadow_qd);
1333*acd884deSSumit Saxena 	dev_dbg(&rcfw->pdev->dev,
1334*acd884deSSumit Saxena 		"Perf Debug: shadow qd %d\n", rcfw->curr_shadow_qd);
1335*acd884deSSumit Saxena 	bnxt_qplib_start_rcfw(rcfw);
1336*acd884deSSumit Saxena 
1337*acd884deSSumit Saxena 	return 0;
1338*acd884deSSumit Saxena }
1339