1 // SPDX-License-Identifier: GPL-2.0
3 /* Copyright (c) 2013-2018, The Linux Foundation. All rights reserved.
4 * Copyright (C) 2018-2020 Linaro Ltd.
7 #include <linux/types.h>
8 #include <linux/string.h>
9 #include <linux/slab.h>
10 #include <linux/qrtr.h>
11 #include <linux/soc/qcom/qmi.h>
14 #include "ipa_endpoint.h"
16 #include "ipa_table.h"
17 #include "ipa_modem.h"
18 #include "ipa_qmi_msg.h"
21 * DOC: AP/Modem QMI Handshake
23 * The AP and modem perform a "handshake" at initialization time to ensure
24 * both sides know when everything is ready to begin operating. The AP
25 * driver (this code) uses two QMI handles (endpoints) for this; a client
26 * using a service on the modem, and server to service modem requests (and
27 * to supply an indication message from the AP). Once the handshake is
28 * complete, the AP and modem may begin IPA operation. This occurs
29 * only when the AP IPA driver, modem IPA driver, and IPA microcontroller
32 * The QMI service on the modem expects to receive an INIT_DRIVER request from
33 * the AP, which contains parameters used by the modem during initialization.
34 * The AP sends this request as soon as it is knows the modem side service
35 * is available. The modem responds to this request, and if this response
36 * contains a success result, the AP knows the modem IPA driver is ready.
38 * The modem is responsible for loading firmware on the IPA microcontroller.
39 * This occurs only during the initial modem boot. The modem sends a
40 * separate DRIVER_INIT_COMPLETE request to the AP to report that the
41 * microcontroller is ready. The AP may assume the microcontroller is
42 * ready and remain so (even if the modem reboots) once it has received
43 * and responded to this request.
45 * There is one final exchange involved in the handshake. It is required
46 * on the initial modem boot, but optional (but in practice does occur) on
47 * subsequent boots. The modem expects to receive a final INIT_COMPLETE
48 * indication message from the AP when it is about to begin its normal
49 * operation. The AP will only send this message after it has received
50 * and responded to an INDICATION_REGISTER request from the modem.
53 * - Whenever the AP learns the modem has booted and its IPA QMI service
54 * is available, it sends an INIT_DRIVER request to the modem. The
55 * modem supplies a success response when it is ready to operate.
56 * - On the initial boot, the modem sets up the IPA microcontroller, and
57 * sends a DRIVER_INIT_COMPLETE request to the AP when this is done.
58 * - When the modem is ready to receive an INIT_COMPLETE indication from
59 * the AP, it sends an INDICATION_REGISTER request to the AP.
60 * - On the initial modem boot, everything is ready when:
61 * - AP has received a success response from its INIT_DRIVER request
62 * - AP has responded to a DRIVER_INIT_COMPLETE request
63 * - AP has responded to an INDICATION_REGISTER request from the modem
64 * - AP has sent an INIT_COMPLETE indication to the modem
65 * - On subsequent modem boots, everything is ready when:
66 * - AP has received a success response from its INIT_DRIVER request
67 * - AP has responded to a DRIVER_INIT_COMPLETE request
68 * - The INDICATION_REGISTER request and INIT_COMPLETE indication are
69 * optional for non-initial modem boots, and have no bearing on the
70 * determination of when things are "ready"
73 #define IPA_HOST_SERVICE_SVC_ID 0x31
74 #define IPA_HOST_SVC_VERS 1
75 #define IPA_HOST_SERVICE_INS_ID 1
77 #define IPA_MODEM_SERVICE_SVC_ID 0x31
78 #define IPA_MODEM_SERVICE_INS_ID 2
79 #define IPA_MODEM_SVC_VERS 1
81 #define QMI_INIT_DRIVER_TIMEOUT 60000 /* A minute in milliseconds */
83 /* Send an INIT_COMPLETE indication message to the modem */
84 static void ipa_server_init_complete(struct ipa_qmi
*ipa_qmi
)
86 struct ipa
*ipa
= container_of(ipa_qmi
, struct ipa
, qmi
);
87 struct qmi_handle
*qmi
= &ipa_qmi
->server_handle
;
88 struct sockaddr_qrtr
*sq
= &ipa_qmi
->modem_sq
;
89 struct ipa_init_complete_ind ind
= { };
92 ind
.status
.result
= QMI_RESULT_SUCCESS_V01
;
93 ind
.status
.error
= QMI_ERR_NONE_V01
;
95 ret
= qmi_send_indication(qmi
, sq
, IPA_QMI_INIT_COMPLETE
,
96 IPA_QMI_INIT_COMPLETE_IND_SZ
,
97 ipa_init_complete_ind_ei
, &ind
);
99 dev_err(&ipa
->pdev
->dev
,
100 "error %d sending init complete indication\n", ret
);
102 ipa_qmi
->indication_sent
= true;
105 /* If requested (and not already sent) send the INIT_COMPLETE indication */
106 static void ipa_qmi_indication(struct ipa_qmi
*ipa_qmi
)
108 if (!ipa_qmi
->indication_requested
)
111 if (ipa_qmi
->indication_sent
)
114 ipa_server_init_complete(ipa_qmi
);
117 /* Determine whether everything is ready to start normal operation.
118 * We know everything (else) is ready when we know the IPA driver on
119 * the modem is ready, and the microcontroller is ready.
121 * When the modem boots (or reboots), the handshake sequence starts
122 * with the AP sending the modem an INIT_DRIVER request. Within
123 * that request, the uc_loaded flag will be zero (false) for an
124 * initial boot, non-zero (true) for a subsequent (SSR) boot.
126 static void ipa_qmi_ready(struct ipa_qmi
*ipa_qmi
)
128 struct ipa
*ipa
= container_of(ipa_qmi
, struct ipa
, qmi
);
131 /* We aren't ready until the modem and microcontroller are */
132 if (!ipa_qmi
->modem_ready
|| !ipa_qmi
->uc_ready
)
135 /* Send the indication message if it was requested */
136 ipa_qmi_indication(ipa_qmi
);
138 /* The initial boot requires us to send the indication. */
139 if (ipa_qmi
->initial_boot
) {
140 if (!ipa_qmi
->indication_sent
)
143 /* The initial modem boot completed successfully */
144 ipa_qmi
->initial_boot
= false;
147 /* We're ready. Start up normal operation */
148 ipa
= container_of(ipa_qmi
, struct ipa
, qmi
);
149 ret
= ipa_modem_start(ipa
);
151 dev_err(&ipa
->pdev
->dev
, "error %d starting modem\n", ret
);
154 /* All QMI clients from the modem node are gone (modem shut down or crashed). */
155 static void ipa_server_bye(struct qmi_handle
*qmi
, unsigned int node
)
157 struct ipa_qmi
*ipa_qmi
;
159 ipa_qmi
= container_of(qmi
, struct ipa_qmi
, server_handle
);
161 /* The modem client and server go away at the same time */
162 memset(&ipa_qmi
->modem_sq
, 0, sizeof(ipa_qmi
->modem_sq
));
164 /* initial_boot doesn't change when modem reboots */
165 /* uc_ready doesn't change when modem reboots */
166 ipa_qmi
->modem_ready
= false;
167 ipa_qmi
->indication_requested
= false;
168 ipa_qmi
->indication_sent
= false;
171 static struct qmi_ops ipa_server_ops
= {
172 .bye
= ipa_server_bye
,
175 /* Callback function to handle an INDICATION_REGISTER request message from the
176 * modem. This informs the AP that the modem is now ready to receive the
177 * INIT_COMPLETE indication message.
179 static void ipa_server_indication_register(struct qmi_handle
*qmi
,
180 struct sockaddr_qrtr
*sq
,
184 struct ipa_indication_register_rsp rsp
= { };
185 struct ipa_qmi
*ipa_qmi
;
189 ipa_qmi
= container_of(qmi
, struct ipa_qmi
, server_handle
);
190 ipa
= container_of(ipa_qmi
, struct ipa
, qmi
);
192 rsp
.rsp
.result
= QMI_RESULT_SUCCESS_V01
;
193 rsp
.rsp
.error
= QMI_ERR_NONE_V01
;
195 ret
= qmi_send_response(qmi
, sq
, txn
, IPA_QMI_INDICATION_REGISTER
,
196 IPA_QMI_INDICATION_REGISTER_RSP_SZ
,
197 ipa_indication_register_rsp_ei
, &rsp
);
199 ipa_qmi
->indication_requested
= true;
200 ipa_qmi_ready(ipa_qmi
); /* We might be ready now */
202 dev_err(&ipa
->pdev
->dev
,
203 "error %d sending register indication response\n", ret
);
207 /* Respond to a DRIVER_INIT_COMPLETE request message from the modem. */
208 static void ipa_server_driver_init_complete(struct qmi_handle
*qmi
,
209 struct sockaddr_qrtr
*sq
,
213 struct ipa_driver_init_complete_rsp rsp
= { };
214 struct ipa_qmi
*ipa_qmi
;
218 ipa_qmi
= container_of(qmi
, struct ipa_qmi
, server_handle
);
219 ipa
= container_of(ipa_qmi
, struct ipa
, qmi
);
221 rsp
.rsp
.result
= QMI_RESULT_SUCCESS_V01
;
222 rsp
.rsp
.error
= QMI_ERR_NONE_V01
;
224 ret
= qmi_send_response(qmi
, sq
, txn
, IPA_QMI_DRIVER_INIT_COMPLETE
,
225 IPA_QMI_DRIVER_INIT_COMPLETE_RSP_SZ
,
226 ipa_driver_init_complete_rsp_ei
, &rsp
);
228 ipa_qmi
->uc_ready
= true;
229 ipa_qmi_ready(ipa_qmi
); /* We might be ready now */
231 dev_err(&ipa
->pdev
->dev
,
232 "error %d sending init complete response\n", ret
);
236 /* The server handles two request message types sent by the modem. */
237 static struct qmi_msg_handler ipa_server_msg_handlers
[] = {
240 .msg_id
= IPA_QMI_INDICATION_REGISTER
,
241 .ei
= ipa_indication_register_req_ei
,
242 .decoded_size
= IPA_QMI_INDICATION_REGISTER_REQ_SZ
,
243 .fn
= ipa_server_indication_register
,
247 .msg_id
= IPA_QMI_DRIVER_INIT_COMPLETE
,
248 .ei
= ipa_driver_init_complete_req_ei
,
249 .decoded_size
= IPA_QMI_DRIVER_INIT_COMPLETE_REQ_SZ
,
250 .fn
= ipa_server_driver_init_complete
,
254 /* Handle an INIT_DRIVER response message from the modem. */
255 static void ipa_client_init_driver(struct qmi_handle
*qmi
,
256 struct sockaddr_qrtr
*sq
,
257 struct qmi_txn
*txn
, const void *decoded
)
259 txn
->result
= 0; /* IPA_QMI_INIT_DRIVER request was successful */
260 complete(&txn
->completion
);
263 /* The client handles one response message type sent by the modem. */
264 static struct qmi_msg_handler ipa_client_msg_handlers
[] = {
266 .type
= QMI_RESPONSE
,
267 .msg_id
= IPA_QMI_INIT_DRIVER
,
268 .ei
= ipa_init_modem_driver_rsp_ei
,
269 .decoded_size
= IPA_QMI_INIT_DRIVER_RSP_SZ
,
270 .fn
= ipa_client_init_driver
,
274 /* Return a pointer to an init modem driver request structure, which contains
275 * configuration parameters for the modem. The modem may be started multiple
276 * times, but generally these parameters don't change so we can reuse the
277 * request structure once it's initialized. The only exception is the
278 * skip_uc_load field, which will be set only after the microcontroller has
279 * reported it has completed its initialization.
281 static const struct ipa_init_modem_driver_req
*
282 init_modem_driver_req(struct ipa_qmi
*ipa_qmi
)
284 struct ipa
*ipa
= container_of(ipa_qmi
, struct ipa
, qmi
);
285 static struct ipa_init_modem_driver_req req
;
286 const struct ipa_mem
*mem
;
288 /* The microcontroller is initialized on the first boot */
289 req
.skip_uc_load_valid
= 1;
290 req
.skip_uc_load
= ipa
->uc_loaded
? 1 : 0;
292 /* We only have to initialize most of it once */
293 if (req
.platform_type_valid
)
296 req
.platform_type_valid
= 1;
297 req
.platform_type
= IPA_QMI_PLATFORM_TYPE_MSM_ANDROID
;
299 mem
= &ipa
->mem
[IPA_MEM_MODEM_HEADER
];
301 req
.hdr_tbl_info_valid
= 1;
302 req
.hdr_tbl_info
.start
= ipa
->mem_offset
+ mem
->offset
;
303 req
.hdr_tbl_info
.end
= req
.hdr_tbl_info
.start
+ mem
->size
- 1;
306 mem
= &ipa
->mem
[IPA_MEM_V4_ROUTE
];
307 req
.v4_route_tbl_info_valid
= 1;
308 req
.v4_route_tbl_info
.start
= ipa
->mem_offset
+ mem
->offset
;
309 req
.v4_route_tbl_info
.count
= mem
->size
/ IPA_TABLE_ENTRY_SIZE
;
311 mem
= &ipa
->mem
[IPA_MEM_V6_ROUTE
];
312 req
.v6_route_tbl_info_valid
= 1;
313 req
.v6_route_tbl_info
.start
= ipa
->mem_offset
+ mem
->offset
;
314 req
.v6_route_tbl_info
.count
= mem
->size
/ IPA_TABLE_ENTRY_SIZE
;
316 mem
= &ipa
->mem
[IPA_MEM_V4_FILTER
];
317 req
.v4_filter_tbl_start_valid
= 1;
318 req
.v4_filter_tbl_start
= ipa
->mem_offset
+ mem
->offset
;
320 mem
= &ipa
->mem
[IPA_MEM_V6_FILTER
];
321 req
.v6_filter_tbl_start_valid
= 1;
322 req
.v6_filter_tbl_start
= ipa
->mem_offset
+ mem
->offset
;
324 mem
= &ipa
->mem
[IPA_MEM_MODEM
];
326 req
.modem_mem_info_valid
= 1;
327 req
.modem_mem_info
.start
= ipa
->mem_offset
+ mem
->offset
;
328 req
.modem_mem_info
.size
= mem
->size
;
331 req
.ctrl_comm_dest_end_pt_valid
= 1;
332 req
.ctrl_comm_dest_end_pt
=
333 ipa
->name_map
[IPA_ENDPOINT_AP_MODEM_RX
]->endpoint_id
;
335 /* skip_uc_load_valid and skip_uc_load are set above */
337 mem
= &ipa
->mem
[IPA_MEM_MODEM_PROC_CTX
];
339 req
.hdr_proc_ctx_tbl_info_valid
= 1;
340 req
.hdr_proc_ctx_tbl_info
.start
=
341 ipa
->mem_offset
+ mem
->offset
;
342 req
.hdr_proc_ctx_tbl_info
.end
=
343 req
.hdr_proc_ctx_tbl_info
.start
+ mem
->size
- 1;
346 /* Nothing to report for the compression table (zip_tbl_info) */
348 mem
= &ipa
->mem
[IPA_MEM_V4_ROUTE_HASHED
];
350 req
.v4_hash_route_tbl_info_valid
= 1;
351 req
.v4_hash_route_tbl_info
.start
=
352 ipa
->mem_offset
+ mem
->offset
;
353 req
.v4_hash_route_tbl_info
.count
=
354 mem
->size
/ IPA_TABLE_ENTRY_SIZE
;
357 mem
= &ipa
->mem
[IPA_MEM_V6_ROUTE_HASHED
];
359 req
.v6_hash_route_tbl_info_valid
= 1;
360 req
.v6_hash_route_tbl_info
.start
=
361 ipa
->mem_offset
+ mem
->offset
;
362 req
.v6_hash_route_tbl_info
.count
=
363 mem
->size
/ IPA_TABLE_ENTRY_SIZE
;
366 mem
= &ipa
->mem
[IPA_MEM_V4_FILTER_HASHED
];
368 req
.v4_hash_filter_tbl_start_valid
= 1;
369 req
.v4_hash_filter_tbl_start
= ipa
->mem_offset
+ mem
->offset
;
372 mem
= &ipa
->mem
[IPA_MEM_V6_FILTER_HASHED
];
374 req
.v6_hash_filter_tbl_start_valid
= 1;
375 req
.v6_hash_filter_tbl_start
= ipa
->mem_offset
+ mem
->offset
;
378 /* None of the stats fields are valid (IPA v4.0 and above) */
380 if (ipa
->version
!= IPA_VERSION_3_5_1
) {
381 mem
= &ipa
->mem
[IPA_MEM_STATS_QUOTA
];
383 req
.hw_stats_quota_base_addr_valid
= 1;
384 req
.hw_stats_quota_base_addr
=
385 ipa
->mem_offset
+ mem
->offset
;
386 req
.hw_stats_quota_size_valid
= 1;
387 req
.hw_stats_quota_size
= ipa
->mem_offset
+ mem
->size
;
390 mem
= &ipa
->mem
[IPA_MEM_STATS_DROP
];
392 req
.hw_stats_drop_base_addr_valid
= 1;
393 req
.hw_stats_drop_base_addr
=
394 ipa
->mem_offset
+ mem
->offset
;
395 req
.hw_stats_drop_size_valid
= 1;
396 req
.hw_stats_drop_size
= ipa
->mem_offset
+ mem
->size
;
403 /* Send an INIT_DRIVER request to the modem, and wait for it to complete. */
404 static void ipa_client_init_driver_work(struct work_struct
*work
)
406 unsigned long timeout
= msecs_to_jiffies(QMI_INIT_DRIVER_TIMEOUT
);
407 const struct ipa_init_modem_driver_req
*req
;
408 struct ipa_qmi
*ipa_qmi
;
409 struct qmi_handle
*qmi
;
415 ipa_qmi
= container_of(work
, struct ipa_qmi
, init_driver_work
);
416 qmi
= &ipa_qmi
->client_handle
,
418 ipa
= container_of(ipa_qmi
, struct ipa
, qmi
);
419 dev
= &ipa
->pdev
->dev
;
421 ret
= qmi_txn_init(qmi
, &txn
, NULL
, NULL
);
423 dev_err(dev
, "error %d preparing init driver request\n", ret
);
427 /* Send the request, and if successful wait for its response */
428 req
= init_modem_driver_req(ipa_qmi
);
429 ret
= qmi_send_request(qmi
, &ipa_qmi
->modem_sq
, &txn
,
430 IPA_QMI_INIT_DRIVER
, IPA_QMI_INIT_DRIVER_REQ_SZ
,
431 ipa_init_modem_driver_req_ei
, req
);
433 dev_err(dev
, "error %d sending init driver request\n", ret
);
434 else if ((ret
= qmi_txn_wait(&txn
, timeout
)))
435 dev_err(dev
, "error %d awaiting init driver response\n", ret
);
438 ipa_qmi
->modem_ready
= true;
439 ipa_qmi_ready(ipa_qmi
); /* We might be ready now */
441 /* If any error occurs we need to cancel the transaction */
442 qmi_txn_cancel(&txn
);
446 /* The modem server is now available. We will send an INIT_DRIVER request
447 * to the modem, but can't wait for it to complete in this callback thread.
448 * Schedule a worker on the global workqueue to do that for us.
451 ipa_client_new_server(struct qmi_handle
*qmi
, struct qmi_service
*svc
)
453 struct ipa_qmi
*ipa_qmi
;
455 ipa_qmi
= container_of(qmi
, struct ipa_qmi
, client_handle
);
457 ipa_qmi
->modem_sq
.sq_family
= AF_QIPCRTR
;
458 ipa_qmi
->modem_sq
.sq_node
= svc
->node
;
459 ipa_qmi
->modem_sq
.sq_port
= svc
->port
;
461 schedule_work(&ipa_qmi
->init_driver_work
);
466 static struct qmi_ops ipa_client_ops
= {
467 .new_server
= ipa_client_new_server
,
470 /* This is called by ipa_setup(). We can be informed via remoteproc that
471 * the modem has shut down, in which case this function will be called
472 * again to prepare for it coming back up again.
474 int ipa_qmi_setup(struct ipa
*ipa
)
476 struct ipa_qmi
*ipa_qmi
= &ipa
->qmi
;
479 ipa_qmi
->initial_boot
= true;
481 /* The server handle is used to handle the DRIVER_INIT_COMPLETE
482 * request on the first modem boot. It also receives the
483 * INDICATION_REGISTER request on the first boot and (optionally)
484 * subsequent boots. The INIT_COMPLETE indication message is
485 * sent over the server handle if requested.
487 ret
= qmi_handle_init(&ipa_qmi
->server_handle
,
488 IPA_QMI_SERVER_MAX_RCV_SZ
, &ipa_server_ops
,
489 ipa_server_msg_handlers
);
493 ret
= qmi_add_server(&ipa_qmi
->server_handle
, IPA_HOST_SERVICE_SVC_ID
,
494 IPA_HOST_SVC_VERS
, IPA_HOST_SERVICE_INS_ID
);
496 goto err_server_handle_release
;
498 /* The client handle is only used for sending an INIT_DRIVER request
499 * to the modem, and receiving its response message.
501 ret
= qmi_handle_init(&ipa_qmi
->client_handle
,
502 IPA_QMI_CLIENT_MAX_RCV_SZ
, &ipa_client_ops
,
503 ipa_client_msg_handlers
);
505 goto err_server_handle_release
;
507 /* We need this ready before the service lookup is added */
508 INIT_WORK(&ipa_qmi
->init_driver_work
, ipa_client_init_driver_work
);
510 ret
= qmi_add_lookup(&ipa_qmi
->client_handle
, IPA_MODEM_SERVICE_SVC_ID
,
511 IPA_MODEM_SVC_VERS
, IPA_MODEM_SERVICE_INS_ID
);
513 goto err_client_handle_release
;
517 err_client_handle_release
:
518 /* Releasing the handle also removes registered lookups */
519 qmi_handle_release(&ipa_qmi
->client_handle
);
520 memset(&ipa_qmi
->client_handle
, 0, sizeof(ipa_qmi
->client_handle
));
521 err_server_handle_release
:
522 /* Releasing the handle also removes registered services */
523 qmi_handle_release(&ipa_qmi
->server_handle
);
524 memset(&ipa_qmi
->server_handle
, 0, sizeof(ipa_qmi
->server_handle
));
529 void ipa_qmi_teardown(struct ipa
*ipa
)
531 cancel_work_sync(&ipa
->qmi
.init_driver_work
);
533 qmi_handle_release(&ipa
->qmi
.client_handle
);
534 memset(&ipa
->qmi
.client_handle
, 0, sizeof(ipa
->qmi
.client_handle
));
536 qmi_handle_release(&ipa
->qmi
.server_handle
);
537 memset(&ipa
->qmi
.server_handle
, 0, sizeof(ipa
->qmi
.server_handle
));