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