xref: /OK3568_Linux_fs/kernel/drivers/net/ipa/ipa_qmi.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun 
3*4882a593Smuzhiyun /* Copyright (c) 2013-2018, The Linux Foundation. All rights reserved.
4*4882a593Smuzhiyun  * Copyright (C) 2018-2020 Linaro Ltd.
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #include <linux/types.h>
8*4882a593Smuzhiyun #include <linux/string.h>
9*4882a593Smuzhiyun #include <linux/slab.h>
10*4882a593Smuzhiyun #include <linux/qrtr.h>
11*4882a593Smuzhiyun #include <linux/soc/qcom/qmi.h>
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #include "ipa.h"
14*4882a593Smuzhiyun #include "ipa_endpoint.h"
15*4882a593Smuzhiyun #include "ipa_mem.h"
16*4882a593Smuzhiyun #include "ipa_table.h"
17*4882a593Smuzhiyun #include "ipa_modem.h"
18*4882a593Smuzhiyun #include "ipa_qmi_msg.h"
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun /**
21*4882a593Smuzhiyun  * DOC: AP/Modem QMI Handshake
22*4882a593Smuzhiyun  *
23*4882a593Smuzhiyun  * The AP and modem perform a "handshake" at initialization time to ensure
24*4882a593Smuzhiyun  * both sides know when everything is ready to begin operating.  The AP
25*4882a593Smuzhiyun  * driver (this code) uses two QMI handles (endpoints) for this; a client
26*4882a593Smuzhiyun  * using a service on the modem, and server to service modem requests (and
27*4882a593Smuzhiyun  * to supply an indication message from the AP).  Once the handshake is
28*4882a593Smuzhiyun  * complete, the AP and modem may begin IPA operation.  This occurs
29*4882a593Smuzhiyun  * only when the AP IPA driver, modem IPA driver, and IPA microcontroller
30*4882a593Smuzhiyun  * are ready.
31*4882a593Smuzhiyun  *
32*4882a593Smuzhiyun  * The QMI service on the modem expects to receive an INIT_DRIVER request from
33*4882a593Smuzhiyun  * the AP, which contains parameters used by the modem during initialization.
34*4882a593Smuzhiyun  * The AP sends this request as soon as it is knows the modem side service
35*4882a593Smuzhiyun  * is available.  The modem responds to this request, and if this response
36*4882a593Smuzhiyun  * contains a success result, the AP knows the modem IPA driver is ready.
37*4882a593Smuzhiyun  *
38*4882a593Smuzhiyun  * The modem is responsible for loading firmware on the IPA microcontroller.
39*4882a593Smuzhiyun  * This occurs only during the initial modem boot.  The modem sends a
40*4882a593Smuzhiyun  * separate DRIVER_INIT_COMPLETE request to the AP to report that the
41*4882a593Smuzhiyun  * microcontroller is ready.  The AP may assume the microcontroller is
42*4882a593Smuzhiyun  * ready and remain so (even if the modem reboots) once it has received
43*4882a593Smuzhiyun  * and responded to this request.
44*4882a593Smuzhiyun  *
45*4882a593Smuzhiyun  * There is one final exchange involved in the handshake.  It is required
46*4882a593Smuzhiyun  * on the initial modem boot, but optional (but in practice does occur) on
47*4882a593Smuzhiyun  * subsequent boots.  The modem expects to receive a final INIT_COMPLETE
48*4882a593Smuzhiyun  * indication message from the AP when it is about to begin its normal
49*4882a593Smuzhiyun  * operation.  The AP will only send this message after it has received
50*4882a593Smuzhiyun  * and responded to an INDICATION_REGISTER request from the modem.
51*4882a593Smuzhiyun  *
52*4882a593Smuzhiyun  * So in summary:
53*4882a593Smuzhiyun  * - Whenever the AP learns the modem has booted and its IPA QMI service
54*4882a593Smuzhiyun  *   is available, it sends an INIT_DRIVER request to the modem.  The
55*4882a593Smuzhiyun  *   modem supplies a success response when it is ready to operate.
56*4882a593Smuzhiyun  * - On the initial boot, the modem sets up the IPA microcontroller, and
57*4882a593Smuzhiyun  *   sends a DRIVER_INIT_COMPLETE request to the AP when this is done.
58*4882a593Smuzhiyun  * - When the modem is ready to receive an INIT_COMPLETE indication from
59*4882a593Smuzhiyun  *   the AP, it sends an INDICATION_REGISTER request to the AP.
60*4882a593Smuzhiyun  * - On the initial modem boot, everything is ready when:
61*4882a593Smuzhiyun  *	- AP has received a success response from its INIT_DRIVER request
62*4882a593Smuzhiyun  *	- AP has responded to a DRIVER_INIT_COMPLETE request
63*4882a593Smuzhiyun  *	- AP has responded to an INDICATION_REGISTER request from the modem
64*4882a593Smuzhiyun  *	- AP has sent an INIT_COMPLETE indication to the modem
65*4882a593Smuzhiyun  * - On subsequent modem boots, everything is ready when:
66*4882a593Smuzhiyun  *	- AP has received a success response from its INIT_DRIVER request
67*4882a593Smuzhiyun  *	- AP has responded to a DRIVER_INIT_COMPLETE request
68*4882a593Smuzhiyun  * - The INDICATION_REGISTER request and INIT_COMPLETE indication are
69*4882a593Smuzhiyun  *   optional for non-initial modem boots, and have no bearing on the
70*4882a593Smuzhiyun  *   determination of when things are "ready"
71*4882a593Smuzhiyun  */
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun #define IPA_HOST_SERVICE_SVC_ID		0x31
74*4882a593Smuzhiyun #define IPA_HOST_SVC_VERS		1
75*4882a593Smuzhiyun #define IPA_HOST_SERVICE_INS_ID		1
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun #define IPA_MODEM_SERVICE_SVC_ID	0x31
78*4882a593Smuzhiyun #define IPA_MODEM_SERVICE_INS_ID	2
79*4882a593Smuzhiyun #define IPA_MODEM_SVC_VERS		1
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun #define QMI_INIT_DRIVER_TIMEOUT		60000	/* A minute in milliseconds */
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun /* Send an INIT_COMPLETE indication message to the modem */
ipa_server_init_complete(struct ipa_qmi * ipa_qmi)84*4882a593Smuzhiyun static void ipa_server_init_complete(struct ipa_qmi *ipa_qmi)
85*4882a593Smuzhiyun {
86*4882a593Smuzhiyun 	struct ipa *ipa = container_of(ipa_qmi, struct ipa, qmi);
87*4882a593Smuzhiyun 	struct qmi_handle *qmi = &ipa_qmi->server_handle;
88*4882a593Smuzhiyun 	struct sockaddr_qrtr *sq = &ipa_qmi->modem_sq;
89*4882a593Smuzhiyun 	struct ipa_init_complete_ind ind = { };
90*4882a593Smuzhiyun 	int ret;
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 	ind.status.result = QMI_RESULT_SUCCESS_V01;
93*4882a593Smuzhiyun 	ind.status.error = QMI_ERR_NONE_V01;
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	ret = qmi_send_indication(qmi, sq, IPA_QMI_INIT_COMPLETE,
96*4882a593Smuzhiyun 				   IPA_QMI_INIT_COMPLETE_IND_SZ,
97*4882a593Smuzhiyun 				   ipa_init_complete_ind_ei, &ind);
98*4882a593Smuzhiyun 	if (ret)
99*4882a593Smuzhiyun 		dev_err(&ipa->pdev->dev,
100*4882a593Smuzhiyun 			"error %d sending init complete indication\n", ret);
101*4882a593Smuzhiyun 	else
102*4882a593Smuzhiyun 		ipa_qmi->indication_sent = true;
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun /* If requested (and not already sent) send the INIT_COMPLETE indication */
ipa_qmi_indication(struct ipa_qmi * ipa_qmi)106*4882a593Smuzhiyun static void ipa_qmi_indication(struct ipa_qmi *ipa_qmi)
107*4882a593Smuzhiyun {
108*4882a593Smuzhiyun 	if (!ipa_qmi->indication_requested)
109*4882a593Smuzhiyun 		return;
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun 	if (ipa_qmi->indication_sent)
112*4882a593Smuzhiyun 		return;
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun 	ipa_server_init_complete(ipa_qmi);
115*4882a593Smuzhiyun }
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun /* Determine whether everything is ready to start normal operation.
118*4882a593Smuzhiyun  * We know everything (else) is ready when we know the IPA driver on
119*4882a593Smuzhiyun  * the modem is ready, and the microcontroller is ready.
120*4882a593Smuzhiyun  *
121*4882a593Smuzhiyun  * When the modem boots (or reboots), the handshake sequence starts
122*4882a593Smuzhiyun  * with the AP sending the modem an INIT_DRIVER request.  Within
123*4882a593Smuzhiyun  * that request, the uc_loaded flag will be zero (false) for an
124*4882a593Smuzhiyun  * initial boot, non-zero (true) for a subsequent (SSR) boot.
125*4882a593Smuzhiyun  */
ipa_qmi_ready(struct ipa_qmi * ipa_qmi)126*4882a593Smuzhiyun static void ipa_qmi_ready(struct ipa_qmi *ipa_qmi)
127*4882a593Smuzhiyun {
128*4882a593Smuzhiyun 	struct ipa *ipa = container_of(ipa_qmi, struct ipa, qmi);
129*4882a593Smuzhiyun 	int ret;
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 	/* We aren't ready until the modem and microcontroller are */
132*4882a593Smuzhiyun 	if (!ipa_qmi->modem_ready || !ipa_qmi->uc_ready)
133*4882a593Smuzhiyun 		return;
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 	/* Send the indication message if it was requested */
136*4882a593Smuzhiyun 	ipa_qmi_indication(ipa_qmi);
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 	/* The initial boot requires us to send the indication. */
139*4882a593Smuzhiyun 	if (ipa_qmi->initial_boot) {
140*4882a593Smuzhiyun 		if (!ipa_qmi->indication_sent)
141*4882a593Smuzhiyun 			return;
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 		/* The initial modem boot completed successfully */
144*4882a593Smuzhiyun 		ipa_qmi->initial_boot = false;
145*4882a593Smuzhiyun 	}
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 	/* We're ready.  Start up normal operation */
148*4882a593Smuzhiyun 	ipa = container_of(ipa_qmi, struct ipa, qmi);
149*4882a593Smuzhiyun 	ret = ipa_modem_start(ipa);
150*4882a593Smuzhiyun 	if (ret)
151*4882a593Smuzhiyun 		dev_err(&ipa->pdev->dev, "error %d starting modem\n", ret);
152*4882a593Smuzhiyun }
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun /* All QMI clients from the modem node are gone (modem shut down or crashed). */
ipa_server_bye(struct qmi_handle * qmi,unsigned int node)155*4882a593Smuzhiyun static void ipa_server_bye(struct qmi_handle *qmi, unsigned int node)
156*4882a593Smuzhiyun {
157*4882a593Smuzhiyun 	struct ipa_qmi *ipa_qmi;
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	ipa_qmi = container_of(qmi, struct ipa_qmi, server_handle);
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun 	/* The modem client and server go away at the same time */
162*4882a593Smuzhiyun 	memset(&ipa_qmi->modem_sq, 0, sizeof(ipa_qmi->modem_sq));
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun 	/* initial_boot doesn't change when modem reboots */
165*4882a593Smuzhiyun 	/* uc_ready doesn't change when modem reboots */
166*4882a593Smuzhiyun 	ipa_qmi->modem_ready = false;
167*4882a593Smuzhiyun 	ipa_qmi->indication_requested = false;
168*4882a593Smuzhiyun 	ipa_qmi->indication_sent = false;
169*4882a593Smuzhiyun }
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun static struct qmi_ops ipa_server_ops = {
172*4882a593Smuzhiyun 	.bye		= ipa_server_bye,
173*4882a593Smuzhiyun };
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun /* Callback function to handle an INDICATION_REGISTER request message from the
176*4882a593Smuzhiyun  * modem.  This informs the AP that the modem is now ready to receive the
177*4882a593Smuzhiyun  * INIT_COMPLETE indication message.
178*4882a593Smuzhiyun  */
ipa_server_indication_register(struct qmi_handle * qmi,struct sockaddr_qrtr * sq,struct qmi_txn * txn,const void * decoded)179*4882a593Smuzhiyun static void ipa_server_indication_register(struct qmi_handle *qmi,
180*4882a593Smuzhiyun 					   struct sockaddr_qrtr *sq,
181*4882a593Smuzhiyun 					   struct qmi_txn *txn,
182*4882a593Smuzhiyun 					   const void *decoded)
183*4882a593Smuzhiyun {
184*4882a593Smuzhiyun 	struct ipa_indication_register_rsp rsp = { };
185*4882a593Smuzhiyun 	struct ipa_qmi *ipa_qmi;
186*4882a593Smuzhiyun 	struct ipa *ipa;
187*4882a593Smuzhiyun 	int ret;
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 	ipa_qmi = container_of(qmi, struct ipa_qmi, server_handle);
190*4882a593Smuzhiyun 	ipa = container_of(ipa_qmi, struct ipa, qmi);
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun 	rsp.rsp.result = QMI_RESULT_SUCCESS_V01;
193*4882a593Smuzhiyun 	rsp.rsp.error = QMI_ERR_NONE_V01;
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun 	ret = qmi_send_response(qmi, sq, txn, IPA_QMI_INDICATION_REGISTER,
196*4882a593Smuzhiyun 				IPA_QMI_INDICATION_REGISTER_RSP_SZ,
197*4882a593Smuzhiyun 				ipa_indication_register_rsp_ei, &rsp);
198*4882a593Smuzhiyun 	if (!ret) {
199*4882a593Smuzhiyun 		ipa_qmi->indication_requested = true;
200*4882a593Smuzhiyun 		ipa_qmi_ready(ipa_qmi);		/* We might be ready now */
201*4882a593Smuzhiyun 	} else {
202*4882a593Smuzhiyun 		dev_err(&ipa->pdev->dev,
203*4882a593Smuzhiyun 			"error %d sending register indication response\n", ret);
204*4882a593Smuzhiyun 	}
205*4882a593Smuzhiyun }
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun /* Respond to a DRIVER_INIT_COMPLETE request message from the modem. */
ipa_server_driver_init_complete(struct qmi_handle * qmi,struct sockaddr_qrtr * sq,struct qmi_txn * txn,const void * decoded)208*4882a593Smuzhiyun static void ipa_server_driver_init_complete(struct qmi_handle *qmi,
209*4882a593Smuzhiyun 					    struct sockaddr_qrtr *sq,
210*4882a593Smuzhiyun 					    struct qmi_txn *txn,
211*4882a593Smuzhiyun 					    const void *decoded)
212*4882a593Smuzhiyun {
213*4882a593Smuzhiyun 	struct ipa_driver_init_complete_rsp rsp = { };
214*4882a593Smuzhiyun 	struct ipa_qmi *ipa_qmi;
215*4882a593Smuzhiyun 	struct ipa *ipa;
216*4882a593Smuzhiyun 	int ret;
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 	ipa_qmi = container_of(qmi, struct ipa_qmi, server_handle);
219*4882a593Smuzhiyun 	ipa = container_of(ipa_qmi, struct ipa, qmi);
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun 	rsp.rsp.result = QMI_RESULT_SUCCESS_V01;
222*4882a593Smuzhiyun 	rsp.rsp.error = QMI_ERR_NONE_V01;
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun 	ret = qmi_send_response(qmi, sq, txn, IPA_QMI_DRIVER_INIT_COMPLETE,
225*4882a593Smuzhiyun 				IPA_QMI_DRIVER_INIT_COMPLETE_RSP_SZ,
226*4882a593Smuzhiyun 				ipa_driver_init_complete_rsp_ei, &rsp);
227*4882a593Smuzhiyun 	if (!ret) {
228*4882a593Smuzhiyun 		ipa_qmi->uc_ready = true;
229*4882a593Smuzhiyun 		ipa_qmi_ready(ipa_qmi);		/* We might be ready now */
230*4882a593Smuzhiyun 	} else {
231*4882a593Smuzhiyun 		dev_err(&ipa->pdev->dev,
232*4882a593Smuzhiyun 			"error %d sending init complete response\n", ret);
233*4882a593Smuzhiyun 	}
234*4882a593Smuzhiyun }
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun /* The server handles two request message types sent by the modem. */
237*4882a593Smuzhiyun static struct qmi_msg_handler ipa_server_msg_handlers[] = {
238*4882a593Smuzhiyun 	{
239*4882a593Smuzhiyun 		.type		= QMI_REQUEST,
240*4882a593Smuzhiyun 		.msg_id		= IPA_QMI_INDICATION_REGISTER,
241*4882a593Smuzhiyun 		.ei		= ipa_indication_register_req_ei,
242*4882a593Smuzhiyun 		.decoded_size	= IPA_QMI_INDICATION_REGISTER_REQ_SZ,
243*4882a593Smuzhiyun 		.fn		= ipa_server_indication_register,
244*4882a593Smuzhiyun 	},
245*4882a593Smuzhiyun 	{
246*4882a593Smuzhiyun 		.type		= QMI_REQUEST,
247*4882a593Smuzhiyun 		.msg_id		= IPA_QMI_DRIVER_INIT_COMPLETE,
248*4882a593Smuzhiyun 		.ei		= ipa_driver_init_complete_req_ei,
249*4882a593Smuzhiyun 		.decoded_size	= IPA_QMI_DRIVER_INIT_COMPLETE_REQ_SZ,
250*4882a593Smuzhiyun 		.fn		= ipa_server_driver_init_complete,
251*4882a593Smuzhiyun 	},
252*4882a593Smuzhiyun 	{ },
253*4882a593Smuzhiyun };
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun /* Handle an INIT_DRIVER response message from the modem. */
ipa_client_init_driver(struct qmi_handle * qmi,struct sockaddr_qrtr * sq,struct qmi_txn * txn,const void * decoded)256*4882a593Smuzhiyun static void ipa_client_init_driver(struct qmi_handle *qmi,
257*4882a593Smuzhiyun 				   struct sockaddr_qrtr *sq,
258*4882a593Smuzhiyun 				   struct qmi_txn *txn, const void *decoded)
259*4882a593Smuzhiyun {
260*4882a593Smuzhiyun 	txn->result = 0;	/* IPA_QMI_INIT_DRIVER request was successful */
261*4882a593Smuzhiyun 	complete(&txn->completion);
262*4882a593Smuzhiyun }
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun /* The client handles one response message type sent by the modem. */
265*4882a593Smuzhiyun static struct qmi_msg_handler ipa_client_msg_handlers[] = {
266*4882a593Smuzhiyun 	{
267*4882a593Smuzhiyun 		.type		= QMI_RESPONSE,
268*4882a593Smuzhiyun 		.msg_id		= IPA_QMI_INIT_DRIVER,
269*4882a593Smuzhiyun 		.ei		= ipa_init_modem_driver_rsp_ei,
270*4882a593Smuzhiyun 		.decoded_size	= IPA_QMI_INIT_DRIVER_RSP_SZ,
271*4882a593Smuzhiyun 		.fn		= ipa_client_init_driver,
272*4882a593Smuzhiyun 	},
273*4882a593Smuzhiyun 	{ },
274*4882a593Smuzhiyun };
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun /* Return a pointer to an init modem driver request structure, which contains
277*4882a593Smuzhiyun  * configuration parameters for the modem.  The modem may be started multiple
278*4882a593Smuzhiyun  * times, but generally these parameters don't change so we can reuse the
279*4882a593Smuzhiyun  * request structure once it's initialized.  The only exception is the
280*4882a593Smuzhiyun  * skip_uc_load field, which will be set only after the microcontroller has
281*4882a593Smuzhiyun  * reported it has completed its initialization.
282*4882a593Smuzhiyun  */
283*4882a593Smuzhiyun static const struct ipa_init_modem_driver_req *
init_modem_driver_req(struct ipa_qmi * ipa_qmi)284*4882a593Smuzhiyun init_modem_driver_req(struct ipa_qmi *ipa_qmi)
285*4882a593Smuzhiyun {
286*4882a593Smuzhiyun 	struct ipa *ipa = container_of(ipa_qmi, struct ipa, qmi);
287*4882a593Smuzhiyun 	static struct ipa_init_modem_driver_req req;
288*4882a593Smuzhiyun 	const struct ipa_mem *mem;
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun 	/* The microcontroller is initialized on the first boot */
291*4882a593Smuzhiyun 	req.skip_uc_load_valid = 1;
292*4882a593Smuzhiyun 	req.skip_uc_load = ipa->uc_loaded ? 1 : 0;
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun 	/* We only have to initialize most of it once */
295*4882a593Smuzhiyun 	if (req.platform_type_valid)
296*4882a593Smuzhiyun 		return &req;
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 	req.platform_type_valid = 1;
299*4882a593Smuzhiyun 	req.platform_type = IPA_QMI_PLATFORM_TYPE_MSM_ANDROID;
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun 	mem = &ipa->mem[IPA_MEM_MODEM_HEADER];
302*4882a593Smuzhiyun 	if (mem->size) {
303*4882a593Smuzhiyun 		req.hdr_tbl_info_valid = 1;
304*4882a593Smuzhiyun 		req.hdr_tbl_info.start = ipa->mem_offset + mem->offset;
305*4882a593Smuzhiyun 		req.hdr_tbl_info.end = req.hdr_tbl_info.start + mem->size - 1;
306*4882a593Smuzhiyun 	}
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun 	mem = &ipa->mem[IPA_MEM_V4_ROUTE];
309*4882a593Smuzhiyun 	req.v4_route_tbl_info_valid = 1;
310*4882a593Smuzhiyun 	req.v4_route_tbl_info.start = ipa->mem_offset + mem->offset;
311*4882a593Smuzhiyun 	req.v4_route_tbl_info.end = IPA_ROUTE_MODEM_COUNT - 1;
312*4882a593Smuzhiyun 
313*4882a593Smuzhiyun 	mem = &ipa->mem[IPA_MEM_V6_ROUTE];
314*4882a593Smuzhiyun 	req.v6_route_tbl_info_valid = 1;
315*4882a593Smuzhiyun 	req.v6_route_tbl_info.start = ipa->mem_offset + mem->offset;
316*4882a593Smuzhiyun 	req.v6_route_tbl_info.end = IPA_ROUTE_MODEM_COUNT - 1;
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun 	mem = &ipa->mem[IPA_MEM_V4_FILTER];
319*4882a593Smuzhiyun 	req.v4_filter_tbl_start_valid = 1;
320*4882a593Smuzhiyun 	req.v4_filter_tbl_start = ipa->mem_offset + mem->offset;
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun 	mem = &ipa->mem[IPA_MEM_V6_FILTER];
323*4882a593Smuzhiyun 	req.v6_filter_tbl_start_valid = 1;
324*4882a593Smuzhiyun 	req.v6_filter_tbl_start = ipa->mem_offset + mem->offset;
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun 	mem = &ipa->mem[IPA_MEM_MODEM];
327*4882a593Smuzhiyun 	if (mem->size) {
328*4882a593Smuzhiyun 		req.modem_mem_info_valid = 1;
329*4882a593Smuzhiyun 		req.modem_mem_info.start = ipa->mem_offset + mem->offset;
330*4882a593Smuzhiyun 		req.modem_mem_info.size = mem->size;
331*4882a593Smuzhiyun 	}
332*4882a593Smuzhiyun 
333*4882a593Smuzhiyun 	req.ctrl_comm_dest_end_pt_valid = 1;
334*4882a593Smuzhiyun 	req.ctrl_comm_dest_end_pt =
335*4882a593Smuzhiyun 		ipa->name_map[IPA_ENDPOINT_AP_MODEM_RX]->endpoint_id;
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun 	/* skip_uc_load_valid and skip_uc_load are set above */
338*4882a593Smuzhiyun 
339*4882a593Smuzhiyun 	mem = &ipa->mem[IPA_MEM_MODEM_PROC_CTX];
340*4882a593Smuzhiyun 	if (mem->size) {
341*4882a593Smuzhiyun 		req.hdr_proc_ctx_tbl_info_valid = 1;
342*4882a593Smuzhiyun 		req.hdr_proc_ctx_tbl_info.start =
343*4882a593Smuzhiyun 			ipa->mem_offset + mem->offset;
344*4882a593Smuzhiyun 		req.hdr_proc_ctx_tbl_info.end =
345*4882a593Smuzhiyun 			req.hdr_proc_ctx_tbl_info.start + mem->size - 1;
346*4882a593Smuzhiyun 	}
347*4882a593Smuzhiyun 
348*4882a593Smuzhiyun 	/* Nothing to report for the compression table (zip_tbl_info) */
349*4882a593Smuzhiyun 
350*4882a593Smuzhiyun 	mem = &ipa->mem[IPA_MEM_V4_ROUTE_HASHED];
351*4882a593Smuzhiyun 	if (mem->size) {
352*4882a593Smuzhiyun 		req.v4_hash_route_tbl_info_valid = 1;
353*4882a593Smuzhiyun 		req.v4_hash_route_tbl_info.start =
354*4882a593Smuzhiyun 				ipa->mem_offset + mem->offset;
355*4882a593Smuzhiyun 		req.v4_hash_route_tbl_info.end = IPA_ROUTE_MODEM_COUNT - 1;
356*4882a593Smuzhiyun 	}
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun 	mem = &ipa->mem[IPA_MEM_V6_ROUTE_HASHED];
359*4882a593Smuzhiyun 	if (mem->size) {
360*4882a593Smuzhiyun 		req.v6_hash_route_tbl_info_valid = 1;
361*4882a593Smuzhiyun 		req.v6_hash_route_tbl_info.start =
362*4882a593Smuzhiyun 			ipa->mem_offset + mem->offset;
363*4882a593Smuzhiyun 		req.v6_hash_route_tbl_info.end = IPA_ROUTE_MODEM_COUNT - 1;
364*4882a593Smuzhiyun 	}
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun 	mem = &ipa->mem[IPA_MEM_V4_FILTER_HASHED];
367*4882a593Smuzhiyun 	if (mem->size) {
368*4882a593Smuzhiyun 		req.v4_hash_filter_tbl_start_valid = 1;
369*4882a593Smuzhiyun 		req.v4_hash_filter_tbl_start = ipa->mem_offset + mem->offset;
370*4882a593Smuzhiyun 	}
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun 	mem = &ipa->mem[IPA_MEM_V6_FILTER_HASHED];
373*4882a593Smuzhiyun 	if (mem->size) {
374*4882a593Smuzhiyun 		req.v6_hash_filter_tbl_start_valid = 1;
375*4882a593Smuzhiyun 		req.v6_hash_filter_tbl_start = ipa->mem_offset + mem->offset;
376*4882a593Smuzhiyun 	}
377*4882a593Smuzhiyun 
378*4882a593Smuzhiyun 	/* None of the stats fields are valid (IPA v4.0 and above) */
379*4882a593Smuzhiyun 
380*4882a593Smuzhiyun 	if (ipa->version != IPA_VERSION_3_5_1) {
381*4882a593Smuzhiyun 		mem = &ipa->mem[IPA_MEM_STATS_QUOTA];
382*4882a593Smuzhiyun 		if (mem->size) {
383*4882a593Smuzhiyun 			req.hw_stats_quota_base_addr_valid = 1;
384*4882a593Smuzhiyun 			req.hw_stats_quota_base_addr =
385*4882a593Smuzhiyun 				ipa->mem_offset + mem->offset;
386*4882a593Smuzhiyun 			req.hw_stats_quota_size_valid = 1;
387*4882a593Smuzhiyun 			req.hw_stats_quota_size = ipa->mem_offset + mem->size;
388*4882a593Smuzhiyun 		}
389*4882a593Smuzhiyun 
390*4882a593Smuzhiyun 		mem = &ipa->mem[IPA_MEM_STATS_DROP];
391*4882a593Smuzhiyun 		if (mem->size) {
392*4882a593Smuzhiyun 			req.hw_stats_drop_base_addr_valid = 1;
393*4882a593Smuzhiyun 			req.hw_stats_drop_base_addr =
394*4882a593Smuzhiyun 				ipa->mem_offset + mem->offset;
395*4882a593Smuzhiyun 			req.hw_stats_drop_size_valid = 1;
396*4882a593Smuzhiyun 			req.hw_stats_drop_size = ipa->mem_offset + mem->size;
397*4882a593Smuzhiyun 		}
398*4882a593Smuzhiyun 	}
399*4882a593Smuzhiyun 
400*4882a593Smuzhiyun 	return &req;
401*4882a593Smuzhiyun }
402*4882a593Smuzhiyun 
403*4882a593Smuzhiyun /* Send an INIT_DRIVER request to the modem, and wait for it to complete. */
ipa_client_init_driver_work(struct work_struct * work)404*4882a593Smuzhiyun static void ipa_client_init_driver_work(struct work_struct *work)
405*4882a593Smuzhiyun {
406*4882a593Smuzhiyun 	unsigned long timeout = msecs_to_jiffies(QMI_INIT_DRIVER_TIMEOUT);
407*4882a593Smuzhiyun 	const struct ipa_init_modem_driver_req *req;
408*4882a593Smuzhiyun 	struct ipa_qmi *ipa_qmi;
409*4882a593Smuzhiyun 	struct qmi_handle *qmi;
410*4882a593Smuzhiyun 	struct qmi_txn txn;
411*4882a593Smuzhiyun 	struct device *dev;
412*4882a593Smuzhiyun 	struct ipa *ipa;
413*4882a593Smuzhiyun 	int ret;
414*4882a593Smuzhiyun 
415*4882a593Smuzhiyun 	ipa_qmi = container_of(work, struct ipa_qmi, init_driver_work);
416*4882a593Smuzhiyun 	qmi = &ipa_qmi->client_handle,
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun 	ipa = container_of(ipa_qmi, struct ipa, qmi);
419*4882a593Smuzhiyun 	dev = &ipa->pdev->dev;
420*4882a593Smuzhiyun 
421*4882a593Smuzhiyun 	ret = qmi_txn_init(qmi, &txn, NULL, NULL);
422*4882a593Smuzhiyun 	if (ret < 0) {
423*4882a593Smuzhiyun 		dev_err(dev, "error %d preparing init driver request\n", ret);
424*4882a593Smuzhiyun 		return;
425*4882a593Smuzhiyun 	}
426*4882a593Smuzhiyun 
427*4882a593Smuzhiyun 	/* Send the request, and if successful wait for its response */
428*4882a593Smuzhiyun 	req = init_modem_driver_req(ipa_qmi);
429*4882a593Smuzhiyun 	ret = qmi_send_request(qmi, &ipa_qmi->modem_sq, &txn,
430*4882a593Smuzhiyun 			       IPA_QMI_INIT_DRIVER, IPA_QMI_INIT_DRIVER_REQ_SZ,
431*4882a593Smuzhiyun 			       ipa_init_modem_driver_req_ei, req);
432*4882a593Smuzhiyun 	if (ret)
433*4882a593Smuzhiyun 		dev_err(dev, "error %d sending init driver request\n", ret);
434*4882a593Smuzhiyun 	else if ((ret = qmi_txn_wait(&txn, timeout)))
435*4882a593Smuzhiyun 		dev_err(dev, "error %d awaiting init driver response\n", ret);
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun 	if (!ret) {
438*4882a593Smuzhiyun 		ipa_qmi->modem_ready = true;
439*4882a593Smuzhiyun 		ipa_qmi_ready(ipa_qmi);		/* We might be ready now */
440*4882a593Smuzhiyun 	} else {
441*4882a593Smuzhiyun 		/* If any error occurs we need to cancel the transaction */
442*4882a593Smuzhiyun 		qmi_txn_cancel(&txn);
443*4882a593Smuzhiyun 	}
444*4882a593Smuzhiyun }
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun /* The modem server is now available.  We will send an INIT_DRIVER request
447*4882a593Smuzhiyun  * to the modem, but can't wait for it to complete in this callback thread.
448*4882a593Smuzhiyun  * Schedule a worker on the global workqueue to do that for us.
449*4882a593Smuzhiyun  */
450*4882a593Smuzhiyun static int
ipa_client_new_server(struct qmi_handle * qmi,struct qmi_service * svc)451*4882a593Smuzhiyun ipa_client_new_server(struct qmi_handle *qmi, struct qmi_service *svc)
452*4882a593Smuzhiyun {
453*4882a593Smuzhiyun 	struct ipa_qmi *ipa_qmi;
454*4882a593Smuzhiyun 
455*4882a593Smuzhiyun 	ipa_qmi = container_of(qmi, struct ipa_qmi, client_handle);
456*4882a593Smuzhiyun 
457*4882a593Smuzhiyun 	ipa_qmi->modem_sq.sq_family = AF_QIPCRTR;
458*4882a593Smuzhiyun 	ipa_qmi->modem_sq.sq_node = svc->node;
459*4882a593Smuzhiyun 	ipa_qmi->modem_sq.sq_port = svc->port;
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun 	schedule_work(&ipa_qmi->init_driver_work);
462*4882a593Smuzhiyun 
463*4882a593Smuzhiyun 	return 0;
464*4882a593Smuzhiyun }
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun static struct qmi_ops ipa_client_ops = {
467*4882a593Smuzhiyun 	.new_server	= ipa_client_new_server,
468*4882a593Smuzhiyun };
469*4882a593Smuzhiyun 
470*4882a593Smuzhiyun /* This is called by ipa_setup().  We can be informed via remoteproc that
471*4882a593Smuzhiyun  * the modem has shut down, in which case this function will be called
472*4882a593Smuzhiyun  * again to prepare for it coming back up again.
473*4882a593Smuzhiyun  */
ipa_qmi_setup(struct ipa * ipa)474*4882a593Smuzhiyun int ipa_qmi_setup(struct ipa *ipa)
475*4882a593Smuzhiyun {
476*4882a593Smuzhiyun 	struct ipa_qmi *ipa_qmi = &ipa->qmi;
477*4882a593Smuzhiyun 	int ret;
478*4882a593Smuzhiyun 
479*4882a593Smuzhiyun 	ipa_qmi->initial_boot = true;
480*4882a593Smuzhiyun 
481*4882a593Smuzhiyun 	/* The server handle is used to handle the DRIVER_INIT_COMPLETE
482*4882a593Smuzhiyun 	 * request on the first modem boot.  It also receives the
483*4882a593Smuzhiyun 	 * INDICATION_REGISTER request on the first boot and (optionally)
484*4882a593Smuzhiyun 	 * subsequent boots.  The INIT_COMPLETE indication message is
485*4882a593Smuzhiyun 	 * sent over the server handle if requested.
486*4882a593Smuzhiyun 	 */
487*4882a593Smuzhiyun 	ret = qmi_handle_init(&ipa_qmi->server_handle,
488*4882a593Smuzhiyun 			      IPA_QMI_SERVER_MAX_RCV_SZ, &ipa_server_ops,
489*4882a593Smuzhiyun 			      ipa_server_msg_handlers);
490*4882a593Smuzhiyun 	if (ret)
491*4882a593Smuzhiyun 		return ret;
492*4882a593Smuzhiyun 
493*4882a593Smuzhiyun 	ret = qmi_add_server(&ipa_qmi->server_handle, IPA_HOST_SERVICE_SVC_ID,
494*4882a593Smuzhiyun 			     IPA_HOST_SVC_VERS, IPA_HOST_SERVICE_INS_ID);
495*4882a593Smuzhiyun 	if (ret)
496*4882a593Smuzhiyun 		goto err_server_handle_release;
497*4882a593Smuzhiyun 
498*4882a593Smuzhiyun 	/* The client handle is only used for sending an INIT_DRIVER request
499*4882a593Smuzhiyun 	 * to the modem, and receiving its response message.
500*4882a593Smuzhiyun 	 */
501*4882a593Smuzhiyun 	ret = qmi_handle_init(&ipa_qmi->client_handle,
502*4882a593Smuzhiyun 			      IPA_QMI_CLIENT_MAX_RCV_SZ, &ipa_client_ops,
503*4882a593Smuzhiyun 			      ipa_client_msg_handlers);
504*4882a593Smuzhiyun 	if (ret)
505*4882a593Smuzhiyun 		goto err_server_handle_release;
506*4882a593Smuzhiyun 
507*4882a593Smuzhiyun 	/* We need this ready before the service lookup is added */
508*4882a593Smuzhiyun 	INIT_WORK(&ipa_qmi->init_driver_work, ipa_client_init_driver_work);
509*4882a593Smuzhiyun 
510*4882a593Smuzhiyun 	ret = qmi_add_lookup(&ipa_qmi->client_handle, IPA_MODEM_SERVICE_SVC_ID,
511*4882a593Smuzhiyun 			     IPA_MODEM_SVC_VERS, IPA_MODEM_SERVICE_INS_ID);
512*4882a593Smuzhiyun 	if (ret)
513*4882a593Smuzhiyun 		goto err_client_handle_release;
514*4882a593Smuzhiyun 
515*4882a593Smuzhiyun 	return 0;
516*4882a593Smuzhiyun 
517*4882a593Smuzhiyun err_client_handle_release:
518*4882a593Smuzhiyun 	/* Releasing the handle also removes registered lookups */
519*4882a593Smuzhiyun 	qmi_handle_release(&ipa_qmi->client_handle);
520*4882a593Smuzhiyun 	memset(&ipa_qmi->client_handle, 0, sizeof(ipa_qmi->client_handle));
521*4882a593Smuzhiyun err_server_handle_release:
522*4882a593Smuzhiyun 	/* Releasing the handle also removes registered services */
523*4882a593Smuzhiyun 	qmi_handle_release(&ipa_qmi->server_handle);
524*4882a593Smuzhiyun 	memset(&ipa_qmi->server_handle, 0, sizeof(ipa_qmi->server_handle));
525*4882a593Smuzhiyun 
526*4882a593Smuzhiyun 	return ret;
527*4882a593Smuzhiyun }
528*4882a593Smuzhiyun 
ipa_qmi_teardown(struct ipa * ipa)529*4882a593Smuzhiyun void ipa_qmi_teardown(struct ipa *ipa)
530*4882a593Smuzhiyun {
531*4882a593Smuzhiyun 	cancel_work_sync(&ipa->qmi.init_driver_work);
532*4882a593Smuzhiyun 
533*4882a593Smuzhiyun 	qmi_handle_release(&ipa->qmi.client_handle);
534*4882a593Smuzhiyun 	memset(&ipa->qmi.client_handle, 0, sizeof(ipa->qmi.client_handle));
535*4882a593Smuzhiyun 
536*4882a593Smuzhiyun 	qmi_handle_release(&ipa->qmi.server_handle);
537*4882a593Smuzhiyun 	memset(&ipa->qmi.server_handle, 0, sizeof(ipa->qmi.server_handle));
538*4882a593Smuzhiyun }
539