xref: /linux/drivers/net/ipa/ipa_qmi.c (revision 88412277)
1530f9216SAlex Elder // SPDX-License-Identifier: GPL-2.0
2530f9216SAlex Elder 
3530f9216SAlex Elder /* Copyright (c) 2013-2018, The Linux Foundation. All rights reserved.
48c044024SAlex Elder  * Copyright (C) 2018-2024 Linaro Ltd.
5530f9216SAlex Elder  */
6530f9216SAlex Elder 
7530f9216SAlex Elder #include <linux/qrtr.h>
8*88412277SAlex Elder #include <linux/string.h>
9*88412277SAlex Elder #include <linux/types.h>
10530f9216SAlex Elder 
11530f9216SAlex Elder #include "ipa.h"
12530f9216SAlex Elder #include "ipa_mem.h"
13530f9216SAlex Elder #include "ipa_modem.h"
14530f9216SAlex Elder #include "ipa_qmi_msg.h"
15530f9216SAlex Elder 
16530f9216SAlex Elder /**
17530f9216SAlex Elder  * DOC: AP/Modem QMI Handshake
18530f9216SAlex Elder  *
19530f9216SAlex Elder  * The AP and modem perform a "handshake" at initialization time to ensure
20530f9216SAlex Elder  * both sides know when everything is ready to begin operating.  The AP
21530f9216SAlex Elder  * driver (this code) uses two QMI handles (endpoints) for this; a client
22530f9216SAlex Elder  * using a service on the modem, and server to service modem requests (and
23530f9216SAlex Elder  * to supply an indication message from the AP).  Once the handshake is
24530f9216SAlex Elder  * complete, the AP and modem may begin IPA operation.  This occurs
25530f9216SAlex Elder  * only when the AP IPA driver, modem IPA driver, and IPA microcontroller
26530f9216SAlex Elder  * are ready.
27530f9216SAlex Elder  *
28530f9216SAlex Elder  * The QMI service on the modem expects to receive an INIT_DRIVER request from
29530f9216SAlex Elder  * the AP, which contains parameters used by the modem during initialization.
30530f9216SAlex Elder  * The AP sends this request as soon as it is knows the modem side service
31530f9216SAlex Elder  * is available.  The modem responds to this request, and if this response
32530f9216SAlex Elder  * contains a success result, the AP knows the modem IPA driver is ready.
33530f9216SAlex Elder  *
34530f9216SAlex Elder  * The modem is responsible for loading firmware on the IPA microcontroller.
35530f9216SAlex Elder  * This occurs only during the initial modem boot.  The modem sends a
36530f9216SAlex Elder  * separate DRIVER_INIT_COMPLETE request to the AP to report that the
37530f9216SAlex Elder  * microcontroller is ready.  The AP may assume the microcontroller is
38530f9216SAlex Elder  * ready and remain so (even if the modem reboots) once it has received
39530f9216SAlex Elder  * and responded to this request.
40530f9216SAlex Elder  *
41530f9216SAlex Elder  * There is one final exchange involved in the handshake.  It is required
42530f9216SAlex Elder  * on the initial modem boot, but optional (but in practice does occur) on
43530f9216SAlex Elder  * subsequent boots.  The modem expects to receive a final INIT_COMPLETE
44530f9216SAlex Elder  * indication message from the AP when it is about to begin its normal
45530f9216SAlex Elder  * operation.  The AP will only send this message after it has received
46530f9216SAlex Elder  * and responded to an INDICATION_REGISTER request from the modem.
47530f9216SAlex Elder  *
48530f9216SAlex Elder  * So in summary:
49530f9216SAlex Elder  * - Whenever the AP learns the modem has booted and its IPA QMI service
50530f9216SAlex Elder  *   is available, it sends an INIT_DRIVER request to the modem.  The
51530f9216SAlex Elder  *   modem supplies a success response when it is ready to operate.
52530f9216SAlex Elder  * - On the initial boot, the modem sets up the IPA microcontroller, and
53530f9216SAlex Elder  *   sends a DRIVER_INIT_COMPLETE request to the AP when this is done.
54530f9216SAlex Elder  * - When the modem is ready to receive an INIT_COMPLETE indication from
55530f9216SAlex Elder  *   the AP, it sends an INDICATION_REGISTER request to the AP.
56530f9216SAlex Elder  * - On the initial modem boot, everything is ready when:
57530f9216SAlex Elder  *	- AP has received a success response from its INIT_DRIVER request
58530f9216SAlex Elder  *	- AP has responded to a DRIVER_INIT_COMPLETE request
59530f9216SAlex Elder  *	- AP has responded to an INDICATION_REGISTER request from the modem
60530f9216SAlex Elder  *	- AP has sent an INIT_COMPLETE indication to the modem
61530f9216SAlex Elder  * - On subsequent modem boots, everything is ready when:
62530f9216SAlex Elder  *	- AP has received a success response from its INIT_DRIVER request
63530f9216SAlex Elder  *	- AP has responded to a DRIVER_INIT_COMPLETE request
64530f9216SAlex Elder  * - The INDICATION_REGISTER request and INIT_COMPLETE indication are
65530f9216SAlex Elder  *   optional for non-initial modem boots, and have no bearing on the
66530f9216SAlex Elder  *   determination of when things are "ready"
67530f9216SAlex Elder  */
68530f9216SAlex Elder 
69530f9216SAlex Elder #define IPA_HOST_SERVICE_SVC_ID		0x31
70530f9216SAlex Elder #define IPA_HOST_SVC_VERS		1
71530f9216SAlex Elder #define IPA_HOST_SERVICE_INS_ID		1
72530f9216SAlex Elder 
73530f9216SAlex Elder #define IPA_MODEM_SERVICE_SVC_ID	0x31
74530f9216SAlex Elder #define IPA_MODEM_SERVICE_INS_ID	2
75530f9216SAlex Elder #define IPA_MODEM_SVC_VERS		1
76530f9216SAlex Elder 
77530f9216SAlex Elder #define QMI_INIT_DRIVER_TIMEOUT		60000	/* A minute in milliseconds */
78530f9216SAlex Elder 
79530f9216SAlex Elder /* Send an INIT_COMPLETE indication message to the modem */
ipa_server_init_complete(struct ipa_qmi * ipa_qmi)80530f9216SAlex Elder static void ipa_server_init_complete(struct ipa_qmi *ipa_qmi)
81530f9216SAlex Elder {
82530f9216SAlex Elder 	struct ipa *ipa = container_of(ipa_qmi, struct ipa, qmi);
83530f9216SAlex Elder 	struct qmi_handle *qmi = &ipa_qmi->server_handle;
84530f9216SAlex Elder 	struct sockaddr_qrtr *sq = &ipa_qmi->modem_sq;
85530f9216SAlex Elder 	struct ipa_init_complete_ind ind = { };
86530f9216SAlex Elder 	int ret;
87530f9216SAlex Elder 
88530f9216SAlex Elder 	ind.status.result = QMI_RESULT_SUCCESS_V01;
89530f9216SAlex Elder 	ind.status.error = QMI_ERR_NONE_V01;
90530f9216SAlex Elder 
91530f9216SAlex Elder 	ret = qmi_send_indication(qmi, sq, IPA_QMI_INIT_COMPLETE,
92530f9216SAlex Elder 				   IPA_QMI_INIT_COMPLETE_IND_SZ,
93530f9216SAlex Elder 				   ipa_init_complete_ind_ei, &ind);
94530f9216SAlex Elder 	if (ret)
955245f4fdSAlex Elder 		dev_err(ipa->dev,
96530f9216SAlex Elder 			"error %d sending init complete indication\n", ret);
97530f9216SAlex Elder 	else
98530f9216SAlex Elder 		ipa_qmi->indication_sent = true;
99530f9216SAlex Elder }
100530f9216SAlex Elder 
101530f9216SAlex Elder /* If requested (and not already sent) send the INIT_COMPLETE indication */
ipa_qmi_indication(struct ipa_qmi * ipa_qmi)102530f9216SAlex Elder static void ipa_qmi_indication(struct ipa_qmi *ipa_qmi)
103530f9216SAlex Elder {
104530f9216SAlex Elder 	if (!ipa_qmi->indication_requested)
105530f9216SAlex Elder 		return;
106530f9216SAlex Elder 
107530f9216SAlex Elder 	if (ipa_qmi->indication_sent)
108530f9216SAlex Elder 		return;
109530f9216SAlex Elder 
110530f9216SAlex Elder 	ipa_server_init_complete(ipa_qmi);
111530f9216SAlex Elder }
112530f9216SAlex Elder 
113530f9216SAlex Elder /* Determine whether everything is ready to start normal operation.
114530f9216SAlex Elder  * We know everything (else) is ready when we know the IPA driver on
115530f9216SAlex Elder  * the modem is ready, and the microcontroller is ready.
116530f9216SAlex Elder  *
117530f9216SAlex Elder  * When the modem boots (or reboots), the handshake sequence starts
118530f9216SAlex Elder  * with the AP sending the modem an INIT_DRIVER request.  Within
119530f9216SAlex Elder  * that request, the uc_loaded flag will be zero (false) for an
120530f9216SAlex Elder  * initial boot, non-zero (true) for a subsequent (SSR) boot.
121530f9216SAlex Elder  */
ipa_qmi_ready(struct ipa_qmi * ipa_qmi)122530f9216SAlex Elder static void ipa_qmi_ready(struct ipa_qmi *ipa_qmi)
123530f9216SAlex Elder {
1248d017efbSAlex Elder 	struct ipa *ipa;
125530f9216SAlex Elder 	int ret;
126530f9216SAlex Elder 
127530f9216SAlex Elder 	/* We aren't ready until the modem and microcontroller are */
128530f9216SAlex Elder 	if (!ipa_qmi->modem_ready || !ipa_qmi->uc_ready)
129530f9216SAlex Elder 		return;
130530f9216SAlex Elder 
131530f9216SAlex Elder 	/* Send the indication message if it was requested */
132530f9216SAlex Elder 	ipa_qmi_indication(ipa_qmi);
133530f9216SAlex Elder 
134530f9216SAlex Elder 	/* The initial boot requires us to send the indication. */
135530f9216SAlex Elder 	if (ipa_qmi->initial_boot) {
136530f9216SAlex Elder 		if (!ipa_qmi->indication_sent)
137530f9216SAlex Elder 			return;
138530f9216SAlex Elder 
139530f9216SAlex Elder 		/* The initial modem boot completed successfully */
140530f9216SAlex Elder 		ipa_qmi->initial_boot = false;
141530f9216SAlex Elder 	}
142530f9216SAlex Elder 
143530f9216SAlex Elder 	/* We're ready.  Start up normal operation */
144530f9216SAlex Elder 	ipa = container_of(ipa_qmi, struct ipa, qmi);
145530f9216SAlex Elder 	ret = ipa_modem_start(ipa);
146530f9216SAlex Elder 	if (ret)
1475245f4fdSAlex Elder 		dev_err(ipa->dev, "error %d starting modem\n", ret);
148530f9216SAlex Elder }
149530f9216SAlex Elder 
150530f9216SAlex Elder /* All QMI clients from the modem node are gone (modem shut down or crashed). */
ipa_server_bye(struct qmi_handle * qmi,unsigned int node)151530f9216SAlex Elder static void ipa_server_bye(struct qmi_handle *qmi, unsigned int node)
152530f9216SAlex Elder {
153530f9216SAlex Elder 	struct ipa_qmi *ipa_qmi;
154530f9216SAlex Elder 
155530f9216SAlex Elder 	ipa_qmi = container_of(qmi, struct ipa_qmi, server_handle);
156530f9216SAlex Elder 
157530f9216SAlex Elder 	/* The modem client and server go away at the same time */
158530f9216SAlex Elder 	memset(&ipa_qmi->modem_sq, 0, sizeof(ipa_qmi->modem_sq));
159530f9216SAlex Elder 
160530f9216SAlex Elder 	/* initial_boot doesn't change when modem reboots */
161530f9216SAlex Elder 	/* uc_ready doesn't change when modem reboots */
162530f9216SAlex Elder 	ipa_qmi->modem_ready = false;
163530f9216SAlex Elder 	ipa_qmi->indication_requested = false;
164530f9216SAlex Elder 	ipa_qmi->indication_sent = false;
165530f9216SAlex Elder }
166530f9216SAlex Elder 
167b5094a3bSRikard Falkeborn static const struct qmi_ops ipa_server_ops = {
168530f9216SAlex Elder 	.bye		= ipa_server_bye,
169530f9216SAlex Elder };
170530f9216SAlex Elder 
171530f9216SAlex Elder /* Callback function to handle an INDICATION_REGISTER request message from the
172530f9216SAlex Elder  * modem.  This informs the AP that the modem is now ready to receive the
173530f9216SAlex Elder  * INIT_COMPLETE indication message.
174530f9216SAlex Elder  */
ipa_server_indication_register(struct qmi_handle * qmi,struct sockaddr_qrtr * sq,struct qmi_txn * txn,const void * decoded)175530f9216SAlex Elder static void ipa_server_indication_register(struct qmi_handle *qmi,
176530f9216SAlex Elder 					   struct sockaddr_qrtr *sq,
177530f9216SAlex Elder 					   struct qmi_txn *txn,
178530f9216SAlex Elder 					   const void *decoded)
179530f9216SAlex Elder {
180530f9216SAlex Elder 	struct ipa_indication_register_rsp rsp = { };
181530f9216SAlex Elder 	struct ipa_qmi *ipa_qmi;
182530f9216SAlex Elder 	struct ipa *ipa;
183530f9216SAlex Elder 	int ret;
184530f9216SAlex Elder 
185530f9216SAlex Elder 	ipa_qmi = container_of(qmi, struct ipa_qmi, server_handle);
186530f9216SAlex Elder 	ipa = container_of(ipa_qmi, struct ipa, qmi);
187530f9216SAlex Elder 
188530f9216SAlex Elder 	rsp.rsp.result = QMI_RESULT_SUCCESS_V01;
189530f9216SAlex Elder 	rsp.rsp.error = QMI_ERR_NONE_V01;
190530f9216SAlex Elder 
191530f9216SAlex Elder 	ret = qmi_send_response(qmi, sq, txn, IPA_QMI_INDICATION_REGISTER,
192530f9216SAlex Elder 				IPA_QMI_INDICATION_REGISTER_RSP_SZ,
193530f9216SAlex Elder 				ipa_indication_register_rsp_ei, &rsp);
194530f9216SAlex Elder 	if (!ret) {
195530f9216SAlex Elder 		ipa_qmi->indication_requested = true;
196530f9216SAlex Elder 		ipa_qmi_ready(ipa_qmi);		/* We might be ready now */
197530f9216SAlex Elder 	} else {
1985245f4fdSAlex Elder 		dev_err(ipa->dev,
199530f9216SAlex Elder 			"error %d sending register indication response\n", ret);
200530f9216SAlex Elder 	}
201530f9216SAlex Elder }
202530f9216SAlex Elder 
203530f9216SAlex Elder /* 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)204530f9216SAlex Elder static void ipa_server_driver_init_complete(struct qmi_handle *qmi,
205530f9216SAlex Elder 					    struct sockaddr_qrtr *sq,
206530f9216SAlex Elder 					    struct qmi_txn *txn,
207530f9216SAlex Elder 					    const void *decoded)
208530f9216SAlex Elder {
209530f9216SAlex Elder 	struct ipa_driver_init_complete_rsp rsp = { };
210530f9216SAlex Elder 	struct ipa_qmi *ipa_qmi;
211530f9216SAlex Elder 	struct ipa *ipa;
212530f9216SAlex Elder 	int ret;
213530f9216SAlex Elder 
214530f9216SAlex Elder 	ipa_qmi = container_of(qmi, struct ipa_qmi, server_handle);
215530f9216SAlex Elder 	ipa = container_of(ipa_qmi, struct ipa, qmi);
216530f9216SAlex Elder 
217530f9216SAlex Elder 	rsp.rsp.result = QMI_RESULT_SUCCESS_V01;
218530f9216SAlex Elder 	rsp.rsp.error = QMI_ERR_NONE_V01;
219530f9216SAlex Elder 
220530f9216SAlex Elder 	ret = qmi_send_response(qmi, sq, txn, IPA_QMI_DRIVER_INIT_COMPLETE,
221530f9216SAlex Elder 				IPA_QMI_DRIVER_INIT_COMPLETE_RSP_SZ,
222530f9216SAlex Elder 				ipa_driver_init_complete_rsp_ei, &rsp);
223530f9216SAlex Elder 	if (!ret) {
224530f9216SAlex Elder 		ipa_qmi->uc_ready = true;
225530f9216SAlex Elder 		ipa_qmi_ready(ipa_qmi);		/* We might be ready now */
226530f9216SAlex Elder 	} else {
2275245f4fdSAlex Elder 		dev_err(ipa->dev,
228530f9216SAlex Elder 			"error %d sending init complete response\n", ret);
229530f9216SAlex Elder 	}
230530f9216SAlex Elder }
231530f9216SAlex Elder 
232530f9216SAlex Elder /* The server handles two request message types sent by the modem. */
233b5094a3bSRikard Falkeborn static const struct qmi_msg_handler ipa_server_msg_handlers[] = {
234530f9216SAlex Elder 	{
235530f9216SAlex Elder 		.type		= QMI_REQUEST,
236530f9216SAlex Elder 		.msg_id		= IPA_QMI_INDICATION_REGISTER,
237530f9216SAlex Elder 		.ei		= ipa_indication_register_req_ei,
238530f9216SAlex Elder 		.decoded_size	= IPA_QMI_INDICATION_REGISTER_REQ_SZ,
239530f9216SAlex Elder 		.fn		= ipa_server_indication_register,
240530f9216SAlex Elder 	},
241530f9216SAlex Elder 	{
242530f9216SAlex Elder 		.type		= QMI_REQUEST,
243530f9216SAlex Elder 		.msg_id		= IPA_QMI_DRIVER_INIT_COMPLETE,
244530f9216SAlex Elder 		.ei		= ipa_driver_init_complete_req_ei,
245530f9216SAlex Elder 		.decoded_size	= IPA_QMI_DRIVER_INIT_COMPLETE_REQ_SZ,
246530f9216SAlex Elder 		.fn		= ipa_server_driver_init_complete,
247530f9216SAlex Elder 	},
2483a9ef3e1SAlex Elder 	{ },
249530f9216SAlex Elder };
250530f9216SAlex Elder 
251530f9216SAlex Elder /* 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)252530f9216SAlex Elder static void ipa_client_init_driver(struct qmi_handle *qmi,
253530f9216SAlex Elder 				   struct sockaddr_qrtr *sq,
254530f9216SAlex Elder 				   struct qmi_txn *txn, const void *decoded)
255530f9216SAlex Elder {
256530f9216SAlex Elder 	txn->result = 0;	/* IPA_QMI_INIT_DRIVER request was successful */
257530f9216SAlex Elder 	complete(&txn->completion);
258530f9216SAlex Elder }
259530f9216SAlex Elder 
260530f9216SAlex Elder /* The client handles one response message type sent by the modem. */
261b5094a3bSRikard Falkeborn static const struct qmi_msg_handler ipa_client_msg_handlers[] = {
262530f9216SAlex Elder 	{
263530f9216SAlex Elder 		.type		= QMI_RESPONSE,
264530f9216SAlex Elder 		.msg_id		= IPA_QMI_INIT_DRIVER,
265530f9216SAlex Elder 		.ei		= ipa_init_modem_driver_rsp_ei,
266530f9216SAlex Elder 		.decoded_size	= IPA_QMI_INIT_DRIVER_RSP_SZ,
267530f9216SAlex Elder 		.fn		= ipa_client_init_driver,
268530f9216SAlex Elder 	},
2693a9ef3e1SAlex Elder 	{ },
270530f9216SAlex Elder };
271530f9216SAlex Elder 
272530f9216SAlex Elder /* Return a pointer to an init modem driver request structure, which contains
273530f9216SAlex Elder  * configuration parameters for the modem.  The modem may be started multiple
274530f9216SAlex Elder  * times, but generally these parameters don't change so we can reuse the
275530f9216SAlex Elder  * request structure once it's initialized.  The only exception is the
276530f9216SAlex Elder  * skip_uc_load field, which will be set only after the microcontroller has
277530f9216SAlex Elder  * reported it has completed its initialization.
278530f9216SAlex Elder  */
279530f9216SAlex Elder static const struct ipa_init_modem_driver_req *
init_modem_driver_req(struct ipa_qmi * ipa_qmi)280530f9216SAlex Elder init_modem_driver_req(struct ipa_qmi *ipa_qmi)
281530f9216SAlex Elder {
282530f9216SAlex Elder 	struct ipa *ipa = container_of(ipa_qmi, struct ipa, qmi);
2838defab8bSAlex Elder 	u32 modem_route_count = ipa->modem_route_count;
284530f9216SAlex Elder 	static struct ipa_init_modem_driver_req req;
285530f9216SAlex Elder 	const struct ipa_mem *mem;
286530f9216SAlex Elder 
287530f9216SAlex Elder 	/* The microcontroller is initialized on the first boot */
288530f9216SAlex Elder 	req.skip_uc_load_valid = 1;
289530f9216SAlex Elder 	req.skip_uc_load = ipa->uc_loaded ? 1 : 0;
290530f9216SAlex Elder 
291530f9216SAlex Elder 	/* We only have to initialize most of it once */
292530f9216SAlex Elder 	if (req.platform_type_valid)
293530f9216SAlex Elder 		return &req;
294530f9216SAlex Elder 
295530f9216SAlex Elder 	req.platform_type_valid = 1;
296530f9216SAlex Elder 	req.platform_type = IPA_QMI_PLATFORM_TYPE_MSM_ANDROID;
297530f9216SAlex Elder 
2985e3bc1e5SAlex Elder 	mem = ipa_mem_find(ipa, IPA_MEM_MODEM_HEADER);
299530f9216SAlex Elder 	if (mem->size) {
300530f9216SAlex Elder 		req.hdr_tbl_info_valid = 1;
301530f9216SAlex Elder 		req.hdr_tbl_info.start = ipa->mem_offset + mem->offset;
302530f9216SAlex Elder 		req.hdr_tbl_info.end = req.hdr_tbl_info.start + mem->size - 1;
303530f9216SAlex Elder 	}
304530f9216SAlex Elder 
3055e3bc1e5SAlex Elder 	mem = ipa_mem_find(ipa, IPA_MEM_V4_ROUTE);
306530f9216SAlex Elder 	req.v4_route_tbl_info_valid = 1;
307530f9216SAlex Elder 	req.v4_route_tbl_info.start = ipa->mem_offset + mem->offset;
3088defab8bSAlex Elder 	req.v4_route_tbl_info.end = modem_route_count - 1;
309530f9216SAlex Elder 
3105e3bc1e5SAlex Elder 	mem = ipa_mem_find(ipa, IPA_MEM_V6_ROUTE);
311530f9216SAlex Elder 	req.v6_route_tbl_info_valid = 1;
312530f9216SAlex Elder 	req.v6_route_tbl_info.start = ipa->mem_offset + mem->offset;
3138defab8bSAlex Elder 	req.v6_route_tbl_info.end = modem_route_count - 1;
314530f9216SAlex Elder 
3155e3bc1e5SAlex Elder 	mem = ipa_mem_find(ipa, IPA_MEM_V4_FILTER);
316530f9216SAlex Elder 	req.v4_filter_tbl_start_valid = 1;
317530f9216SAlex Elder 	req.v4_filter_tbl_start = ipa->mem_offset + mem->offset;
318530f9216SAlex Elder 
3195e3bc1e5SAlex Elder 	mem = ipa_mem_find(ipa, IPA_MEM_V6_FILTER);
320530f9216SAlex Elder 	req.v6_filter_tbl_start_valid = 1;
321530f9216SAlex Elder 	req.v6_filter_tbl_start = ipa->mem_offset + mem->offset;
322530f9216SAlex Elder 
3235e3bc1e5SAlex Elder 	mem = ipa_mem_find(ipa, IPA_MEM_MODEM);
324530f9216SAlex Elder 	if (mem->size) {
325530f9216SAlex Elder 		req.modem_mem_info_valid = 1;
326530f9216SAlex Elder 		req.modem_mem_info.start = ipa->mem_offset + mem->offset;
327530f9216SAlex Elder 		req.modem_mem_info.size = mem->size;
328530f9216SAlex Elder 	}
329530f9216SAlex Elder 
330530f9216SAlex Elder 	req.ctrl_comm_dest_end_pt_valid = 1;
331530f9216SAlex Elder 	req.ctrl_comm_dest_end_pt =
332530f9216SAlex Elder 		ipa->name_map[IPA_ENDPOINT_AP_MODEM_RX]->endpoint_id;
333530f9216SAlex Elder 
334530f9216SAlex Elder 	/* skip_uc_load_valid and skip_uc_load are set above */
335530f9216SAlex Elder 
3365e3bc1e5SAlex Elder 	mem = ipa_mem_find(ipa, IPA_MEM_MODEM_PROC_CTX);
337530f9216SAlex Elder 	if (mem->size) {
338530f9216SAlex Elder 		req.hdr_proc_ctx_tbl_info_valid = 1;
339530f9216SAlex Elder 		req.hdr_proc_ctx_tbl_info.start =
340530f9216SAlex Elder 			ipa->mem_offset + mem->offset;
341530f9216SAlex Elder 		req.hdr_proc_ctx_tbl_info.end =
342530f9216SAlex Elder 			req.hdr_proc_ctx_tbl_info.start + mem->size - 1;
343530f9216SAlex Elder 	}
344530f9216SAlex Elder 
345530f9216SAlex Elder 	/* Nothing to report for the compression table (zip_tbl_info) */
346530f9216SAlex Elder 
3475e3bc1e5SAlex Elder 	mem = ipa_mem_find(ipa, IPA_MEM_V4_ROUTE_HASHED);
348530f9216SAlex Elder 	if (mem->size) {
349530f9216SAlex Elder 		req.v4_hash_route_tbl_info_valid = 1;
350530f9216SAlex Elder 		req.v4_hash_route_tbl_info.start =
351530f9216SAlex Elder 				ipa->mem_offset + mem->offset;
3528defab8bSAlex Elder 		req.v4_hash_route_tbl_info.end = modem_route_count - 1;
353530f9216SAlex Elder 	}
354530f9216SAlex Elder 
3555e3bc1e5SAlex Elder 	mem = ipa_mem_find(ipa, IPA_MEM_V6_ROUTE_HASHED);
356530f9216SAlex Elder 	if (mem->size) {
357530f9216SAlex Elder 		req.v6_hash_route_tbl_info_valid = 1;
358530f9216SAlex Elder 		req.v6_hash_route_tbl_info.start =
359530f9216SAlex Elder 			ipa->mem_offset + mem->offset;
3608defab8bSAlex Elder 		req.v6_hash_route_tbl_info.end = modem_route_count - 1;
361530f9216SAlex Elder 	}
362530f9216SAlex Elder 
3635e3bc1e5SAlex Elder 	mem = ipa_mem_find(ipa, IPA_MEM_V4_FILTER_HASHED);
364530f9216SAlex Elder 	if (mem->size) {
365530f9216SAlex Elder 		req.v4_hash_filter_tbl_start_valid = 1;
366530f9216SAlex Elder 		req.v4_hash_filter_tbl_start = ipa->mem_offset + mem->offset;
367530f9216SAlex Elder 	}
368530f9216SAlex Elder 
3695e3bc1e5SAlex Elder 	mem = ipa_mem_find(ipa, IPA_MEM_V6_FILTER_HASHED);
370530f9216SAlex Elder 	if (mem->size) {
371530f9216SAlex Elder 		req.v6_hash_filter_tbl_start_valid = 1;
372530f9216SAlex Elder 		req.v6_hash_filter_tbl_start = ipa->mem_offset + mem->offset;
373530f9216SAlex Elder 	}
374530f9216SAlex Elder 
3755e3bc1e5SAlex Elder 	/* The stats fields are only valid for IPA v4.0+ */
376d7f3087bSAlex Elder 	if (ipa->version >= IPA_VERSION_4_0) {
3775e3bc1e5SAlex Elder 		mem = ipa_mem_find(ipa, IPA_MEM_STATS_QUOTA_MODEM);
378530f9216SAlex Elder 		if (mem->size) {
379530f9216SAlex Elder 			req.hw_stats_quota_base_addr_valid = 1;
380530f9216SAlex Elder 			req.hw_stats_quota_base_addr =
381530f9216SAlex Elder 				ipa->mem_offset + mem->offset;
382530f9216SAlex Elder 			req.hw_stats_quota_size_valid = 1;
383530f9216SAlex Elder 			req.hw_stats_quota_size = ipa->mem_offset + mem->size;
384530f9216SAlex Elder 		}
385530f9216SAlex Elder 
3865e3bc1e5SAlex Elder 		/* If the DROP stats region is defined, include it */
3875e3bc1e5SAlex Elder 		mem = ipa_mem_find(ipa, IPA_MEM_STATS_DROP);
3885e3bc1e5SAlex Elder 		if (mem && mem->size) {
389530f9216SAlex Elder 			req.hw_stats_drop_base_addr_valid = 1;
390530f9216SAlex Elder 			req.hw_stats_drop_base_addr =
391530f9216SAlex Elder 				ipa->mem_offset + mem->offset;
392530f9216SAlex Elder 			req.hw_stats_drop_size_valid = 1;
393530f9216SAlex Elder 			req.hw_stats_drop_size = ipa->mem_offset + mem->size;
394530f9216SAlex Elder 		}
395530f9216SAlex Elder 	}
396530f9216SAlex Elder 
397530f9216SAlex Elder 	return &req;
398530f9216SAlex Elder }
399530f9216SAlex Elder 
400530f9216SAlex Elder /* Send an INIT_DRIVER request to the modem, and wait for it to complete. */
ipa_client_init_driver_work(struct work_struct * work)401530f9216SAlex Elder static void ipa_client_init_driver_work(struct work_struct *work)
402530f9216SAlex Elder {
403530f9216SAlex Elder 	unsigned long timeout = msecs_to_jiffies(QMI_INIT_DRIVER_TIMEOUT);
404530f9216SAlex Elder 	const struct ipa_init_modem_driver_req *req;
405530f9216SAlex Elder 	struct ipa_qmi *ipa_qmi;
406530f9216SAlex Elder 	struct qmi_handle *qmi;
407530f9216SAlex Elder 	struct qmi_txn txn;
408530f9216SAlex Elder 	struct device *dev;
409530f9216SAlex Elder 	struct ipa *ipa;
410530f9216SAlex Elder 	int ret;
411530f9216SAlex Elder 
412530f9216SAlex Elder 	ipa_qmi = container_of(work, struct ipa_qmi, init_driver_work);
413e65f3df5SZheng Yongjun 	qmi = &ipa_qmi->client_handle;
414530f9216SAlex Elder 
415530f9216SAlex Elder 	ipa = container_of(ipa_qmi, struct ipa, qmi);
4165245f4fdSAlex Elder 	dev = ipa->dev;
417530f9216SAlex Elder 
418530f9216SAlex Elder 	ret = qmi_txn_init(qmi, &txn, NULL, NULL);
419530f9216SAlex Elder 	if (ret < 0) {
420530f9216SAlex Elder 		dev_err(dev, "error %d preparing init driver request\n", ret);
421530f9216SAlex Elder 		return;
422530f9216SAlex Elder 	}
423530f9216SAlex Elder 
424530f9216SAlex Elder 	/* Send the request, and if successful wait for its response */
425530f9216SAlex Elder 	req = init_modem_driver_req(ipa_qmi);
426530f9216SAlex Elder 	ret = qmi_send_request(qmi, &ipa_qmi->modem_sq, &txn,
427530f9216SAlex Elder 			       IPA_QMI_INIT_DRIVER, IPA_QMI_INIT_DRIVER_REQ_SZ,
428530f9216SAlex Elder 			       ipa_init_modem_driver_req_ei, req);
429530f9216SAlex Elder 	if (ret)
430530f9216SAlex Elder 		dev_err(dev, "error %d sending init driver request\n", ret);
431530f9216SAlex Elder 	else if ((ret = qmi_txn_wait(&txn, timeout)))
432530f9216SAlex Elder 		dev_err(dev, "error %d awaiting init driver response\n", ret);
433530f9216SAlex Elder 
434530f9216SAlex Elder 	if (!ret) {
435530f9216SAlex Elder 		ipa_qmi->modem_ready = true;
436530f9216SAlex Elder 		ipa_qmi_ready(ipa_qmi);		/* We might be ready now */
437530f9216SAlex Elder 	} else {
438530f9216SAlex Elder 		/* If any error occurs we need to cancel the transaction */
439530f9216SAlex Elder 		qmi_txn_cancel(&txn);
440530f9216SAlex Elder 	}
441530f9216SAlex Elder }
442530f9216SAlex Elder 
443530f9216SAlex Elder /* The modem server is now available.  We will send an INIT_DRIVER request
444530f9216SAlex Elder  * to the modem, but can't wait for it to complete in this callback thread.
445530f9216SAlex Elder  * Schedule a worker on the global workqueue to do that for us.
446530f9216SAlex Elder  */
447530f9216SAlex Elder static int
ipa_client_new_server(struct qmi_handle * qmi,struct qmi_service * svc)448530f9216SAlex Elder ipa_client_new_server(struct qmi_handle *qmi, struct qmi_service *svc)
449530f9216SAlex Elder {
450530f9216SAlex Elder 	struct ipa_qmi *ipa_qmi;
451530f9216SAlex Elder 
452530f9216SAlex Elder 	ipa_qmi = container_of(qmi, struct ipa_qmi, client_handle);
453530f9216SAlex Elder 
454530f9216SAlex Elder 	ipa_qmi->modem_sq.sq_family = AF_QIPCRTR;
455530f9216SAlex Elder 	ipa_qmi->modem_sq.sq_node = svc->node;
456530f9216SAlex Elder 	ipa_qmi->modem_sq.sq_port = svc->port;
457530f9216SAlex Elder 
458530f9216SAlex Elder 	schedule_work(&ipa_qmi->init_driver_work);
459530f9216SAlex Elder 
460530f9216SAlex Elder 	return 0;
461530f9216SAlex Elder }
462530f9216SAlex Elder 
463b5094a3bSRikard Falkeborn static const struct qmi_ops ipa_client_ops = {
464530f9216SAlex Elder 	.new_server	= ipa_client_new_server,
465530f9216SAlex Elder };
466530f9216SAlex Elder 
46763961f54SAlex Elder /* Set up for QMI message exchange */
ipa_qmi_setup(struct ipa * ipa)468530f9216SAlex Elder int ipa_qmi_setup(struct ipa *ipa)
469530f9216SAlex Elder {
470530f9216SAlex Elder 	struct ipa_qmi *ipa_qmi = &ipa->qmi;
471530f9216SAlex Elder 	int ret;
472530f9216SAlex Elder 
473530f9216SAlex Elder 	ipa_qmi->initial_boot = true;
474530f9216SAlex Elder 
475530f9216SAlex Elder 	/* The server handle is used to handle the DRIVER_INIT_COMPLETE
476530f9216SAlex Elder 	 * request on the first modem boot.  It also receives the
477530f9216SAlex Elder 	 * INDICATION_REGISTER request on the first boot and (optionally)
478530f9216SAlex Elder 	 * subsequent boots.  The INIT_COMPLETE indication message is
479530f9216SAlex Elder 	 * sent over the server handle if requested.
480530f9216SAlex Elder 	 */
481530f9216SAlex Elder 	ret = qmi_handle_init(&ipa_qmi->server_handle,
482530f9216SAlex Elder 			      IPA_QMI_SERVER_MAX_RCV_SZ, &ipa_server_ops,
483530f9216SAlex Elder 			      ipa_server_msg_handlers);
484530f9216SAlex Elder 	if (ret)
485530f9216SAlex Elder 		return ret;
486530f9216SAlex Elder 
487530f9216SAlex Elder 	ret = qmi_add_server(&ipa_qmi->server_handle, IPA_HOST_SERVICE_SVC_ID,
488530f9216SAlex Elder 			     IPA_HOST_SVC_VERS, IPA_HOST_SERVICE_INS_ID);
489530f9216SAlex Elder 	if (ret)
490530f9216SAlex Elder 		goto err_server_handle_release;
491530f9216SAlex Elder 
492530f9216SAlex Elder 	/* The client handle is only used for sending an INIT_DRIVER request
493530f9216SAlex Elder 	 * to the modem, and receiving its response message.
494530f9216SAlex Elder 	 */
495530f9216SAlex Elder 	ret = qmi_handle_init(&ipa_qmi->client_handle,
496530f9216SAlex Elder 			      IPA_QMI_CLIENT_MAX_RCV_SZ, &ipa_client_ops,
497530f9216SAlex Elder 			      ipa_client_msg_handlers);
498530f9216SAlex Elder 	if (ret)
499530f9216SAlex Elder 		goto err_server_handle_release;
500530f9216SAlex Elder 
501530f9216SAlex Elder 	/* We need this ready before the service lookup is added */
502530f9216SAlex Elder 	INIT_WORK(&ipa_qmi->init_driver_work, ipa_client_init_driver_work);
503530f9216SAlex Elder 
504530f9216SAlex Elder 	ret = qmi_add_lookup(&ipa_qmi->client_handle, IPA_MODEM_SERVICE_SVC_ID,
505530f9216SAlex Elder 			     IPA_MODEM_SVC_VERS, IPA_MODEM_SERVICE_INS_ID);
506530f9216SAlex Elder 	if (ret)
507530f9216SAlex Elder 		goto err_client_handle_release;
508530f9216SAlex Elder 
509530f9216SAlex Elder 	return 0;
510530f9216SAlex Elder 
511530f9216SAlex Elder err_client_handle_release:
512530f9216SAlex Elder 	/* Releasing the handle also removes registered lookups */
513530f9216SAlex Elder 	qmi_handle_release(&ipa_qmi->client_handle);
514530f9216SAlex Elder 	memset(&ipa_qmi->client_handle, 0, sizeof(ipa_qmi->client_handle));
515530f9216SAlex Elder err_server_handle_release:
516530f9216SAlex Elder 	/* Releasing the handle also removes registered services */
517530f9216SAlex Elder 	qmi_handle_release(&ipa_qmi->server_handle);
518530f9216SAlex Elder 	memset(&ipa_qmi->server_handle, 0, sizeof(ipa_qmi->server_handle));
519530f9216SAlex Elder 
520530f9216SAlex Elder 	return ret;
521530f9216SAlex Elder }
522530f9216SAlex Elder 
52363961f54SAlex Elder /* Tear down IPA QMI handles */
ipa_qmi_teardown(struct ipa * ipa)524530f9216SAlex Elder void ipa_qmi_teardown(struct ipa *ipa)
525530f9216SAlex Elder {
526530f9216SAlex Elder 	cancel_work_sync(&ipa->qmi.init_driver_work);
527530f9216SAlex Elder 
528530f9216SAlex Elder 	qmi_handle_release(&ipa->qmi.client_handle);
529530f9216SAlex Elder 	memset(&ipa->qmi.client_handle, 0, sizeof(ipa->qmi.client_handle));
530530f9216SAlex Elder 
531530f9216SAlex Elder 	qmi_handle_release(&ipa->qmi.server_handle);
532530f9216SAlex Elder 	memset(&ipa->qmi.server_handle, 0, sizeof(ipa->qmi.server_handle));
533530f9216SAlex Elder }
534