1
2
3
4
5
6
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>
12
13#include "ipa.h"
14#include "ipa_endpoint.h"
15#include "ipa_mem.h"
16#include "ipa_table.h"
17#include "ipa_modem.h"
18#include "ipa_qmi_msg.h"
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73#define IPA_HOST_SERVICE_SVC_ID 0x31
74#define IPA_HOST_SVC_VERS 1
75#define IPA_HOST_SERVICE_INS_ID 1
76
77#define IPA_MODEM_SERVICE_SVC_ID 0x31
78#define IPA_MODEM_SERVICE_INS_ID 2
79#define IPA_MODEM_SVC_VERS 1
80
81#define QMI_INIT_DRIVER_TIMEOUT 60000
82
83
84static void ipa_server_init_complete(struct ipa_qmi *ipa_qmi)
85{
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 = { };
90 int ret;
91
92 ind.status.result = QMI_RESULT_SUCCESS_V01;
93 ind.status.error = QMI_ERR_NONE_V01;
94
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);
98 if (ret)
99 dev_err(&ipa->pdev->dev,
100 "error %d sending init complete indication\n", ret);
101 else
102 ipa_qmi->indication_sent = true;
103}
104
105
106static void ipa_qmi_indication(struct ipa_qmi *ipa_qmi)
107{
108 if (!ipa_qmi->indication_requested)
109 return;
110
111 if (ipa_qmi->indication_sent)
112 return;
113
114 ipa_server_init_complete(ipa_qmi);
115}
116
117
118
119
120
121
122
123
124
125
126static void ipa_qmi_ready(struct ipa_qmi *ipa_qmi)
127{
128 struct ipa *ipa = container_of(ipa_qmi, struct ipa, qmi);
129 int ret;
130
131
132 if (!ipa_qmi->modem_ready || !ipa_qmi->uc_ready)
133 return;
134
135
136 ipa_qmi_indication(ipa_qmi);
137
138
139 if (ipa_qmi->initial_boot) {
140 if (!ipa_qmi->indication_sent)
141 return;
142
143
144 ipa_qmi->initial_boot = false;
145 }
146
147
148 ipa = container_of(ipa_qmi, struct ipa, qmi);
149 ret = ipa_modem_start(ipa);
150 if (ret)
151 dev_err(&ipa->pdev->dev, "error %d starting modem\n", ret);
152}
153
154
155static void ipa_server_bye(struct qmi_handle *qmi, unsigned int node)
156{
157 struct ipa_qmi *ipa_qmi;
158
159 ipa_qmi = container_of(qmi, struct ipa_qmi, server_handle);
160
161
162 memset(&ipa_qmi->modem_sq, 0, sizeof(ipa_qmi->modem_sq));
163
164
165
166 ipa_qmi->modem_ready = false;
167 ipa_qmi->indication_requested = false;
168 ipa_qmi->indication_sent = false;
169}
170
171static struct qmi_ops ipa_server_ops = {
172 .bye = ipa_server_bye,
173};
174
175
176
177
178
179static void ipa_server_indication_register(struct qmi_handle *qmi,
180 struct sockaddr_qrtr *sq,
181 struct qmi_txn *txn,
182 const void *decoded)
183{
184 struct ipa_indication_register_rsp rsp = { };
185 struct ipa_qmi *ipa_qmi;
186 struct ipa *ipa;
187 int ret;
188
189 ipa_qmi = container_of(qmi, struct ipa_qmi, server_handle);
190 ipa = container_of(ipa_qmi, struct ipa, qmi);
191
192 rsp.rsp.result = QMI_RESULT_SUCCESS_V01;
193 rsp.rsp.error = QMI_ERR_NONE_V01;
194
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);
198 if (!ret) {
199 ipa_qmi->indication_requested = true;
200 ipa_qmi_ready(ipa_qmi);
201 } else {
202 dev_err(&ipa->pdev->dev,
203 "error %d sending register indication response\n", ret);
204 }
205}
206
207
208static void ipa_server_driver_init_complete(struct qmi_handle *qmi,
209 struct sockaddr_qrtr *sq,
210 struct qmi_txn *txn,
211 const void *decoded)
212{
213 struct ipa_driver_init_complete_rsp rsp = { };
214 struct ipa_qmi *ipa_qmi;
215 struct ipa *ipa;
216 int ret;
217
218 ipa_qmi = container_of(qmi, struct ipa_qmi, server_handle);
219 ipa = container_of(ipa_qmi, struct ipa, qmi);
220
221 rsp.rsp.result = QMI_RESULT_SUCCESS_V01;
222 rsp.rsp.error = QMI_ERR_NONE_V01;
223
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);
227 if (!ret) {
228 ipa_qmi->uc_ready = true;
229 ipa_qmi_ready(ipa_qmi);
230 } else {
231 dev_err(&ipa->pdev->dev,
232 "error %d sending init complete response\n", ret);
233 }
234}
235
236
237static struct qmi_msg_handler ipa_server_msg_handlers[] = {
238 {
239 .type = QMI_REQUEST,
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,
244 },
245 {
246 .type = QMI_REQUEST,
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,
251 },
252};
253
254
255static void ipa_client_init_driver(struct qmi_handle *qmi,
256 struct sockaddr_qrtr *sq,
257 struct qmi_txn *txn, const void *decoded)
258{
259 txn->result = 0;
260 complete(&txn->completion);
261}
262
263
264static struct qmi_msg_handler ipa_client_msg_handlers[] = {
265 {
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,
271 },
272};
273
274
275
276
277
278
279
280
281static const struct ipa_init_modem_driver_req *
282init_modem_driver_req(struct ipa_qmi *ipa_qmi)
283{
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;
287
288
289 req.skip_uc_load_valid = 1;
290 req.skip_uc_load = ipa->uc_loaded ? 1 : 0;
291
292
293 if (req.platform_type_valid)
294 return &req;
295
296 req.platform_type_valid = 1;
297 req.platform_type = IPA_QMI_PLATFORM_TYPE_MSM_ANDROID;
298
299 mem = &ipa->mem[IPA_MEM_MODEM_HEADER];
300 if (mem->size) {
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;
304 }
305
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;
310
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;
315
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;
319
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;
323
324 mem = &ipa->mem[IPA_MEM_MODEM];
325 if (mem->size) {
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;
329 }
330
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;
334
335
336
337 mem = &ipa->mem[IPA_MEM_MODEM_PROC_CTX];
338 if (mem->size) {
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;
344 }
345
346
347
348 mem = &ipa->mem[IPA_MEM_V4_ROUTE_HASHED];
349 if (mem->size) {
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;
355 }
356
357 mem = &ipa->mem[IPA_MEM_V6_ROUTE_HASHED];
358 if (mem->size) {
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;
364 }
365
366 mem = &ipa->mem[IPA_MEM_V4_FILTER_HASHED];
367 if (mem->size) {
368 req.v4_hash_filter_tbl_start_valid = 1;
369 req.v4_hash_filter_tbl_start = ipa->mem_offset + mem->offset;
370 }
371
372 mem = &ipa->mem[IPA_MEM_V6_FILTER_HASHED];
373 if (mem->size) {
374 req.v6_hash_filter_tbl_start_valid = 1;
375 req.v6_hash_filter_tbl_start = ipa->mem_offset + mem->offset;
376 }
377
378
379
380 if (ipa->version != IPA_VERSION_3_5_1) {
381 mem = &ipa->mem[IPA_MEM_STATS_QUOTA];
382 if (mem->size) {
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;
388 }
389
390 mem = &ipa->mem[IPA_MEM_STATS_DROP];
391 if (mem->size) {
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;
397 }
398 }
399
400 return &req;
401}
402
403
404static void ipa_client_init_driver_work(struct work_struct *work)
405{
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;
410 struct qmi_txn txn;
411 struct device *dev;
412 struct ipa *ipa;
413 int ret;
414
415 ipa_qmi = container_of(work, struct ipa_qmi, init_driver_work);
416 qmi = &ipa_qmi->client_handle,
417
418 ipa = container_of(ipa_qmi, struct ipa, qmi);
419 dev = &ipa->pdev->dev;
420
421 ret = qmi_txn_init(qmi, &txn, NULL, NULL);
422 if (ret < 0) {
423 dev_err(dev, "error %d preparing init driver request\n", ret);
424 return;
425 }
426
427
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);
432 if (ret)
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);
436
437 if (!ret) {
438 ipa_qmi->modem_ready = true;
439 ipa_qmi_ready(ipa_qmi);
440 } else {
441
442 qmi_txn_cancel(&txn);
443 }
444}
445
446
447
448
449
450static int
451ipa_client_new_server(struct qmi_handle *qmi, struct qmi_service *svc)
452{
453 struct ipa_qmi *ipa_qmi;
454
455 ipa_qmi = container_of(qmi, struct ipa_qmi, client_handle);
456
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;
460
461 schedule_work(&ipa_qmi->init_driver_work);
462
463 return 0;
464}
465
466static struct qmi_ops ipa_client_ops = {
467 .new_server = ipa_client_new_server,
468};
469
470
471
472
473
474int ipa_qmi_setup(struct ipa *ipa)
475{
476 struct ipa_qmi *ipa_qmi = &ipa->qmi;
477 int ret;
478
479 ipa_qmi->initial_boot = true;
480
481
482
483
484
485
486
487 ret = qmi_handle_init(&ipa_qmi->server_handle,
488 IPA_QMI_SERVER_MAX_RCV_SZ, &ipa_server_ops,
489 ipa_server_msg_handlers);
490 if (ret)
491 return ret;
492
493 ret = qmi_add_server(&ipa_qmi->server_handle, IPA_HOST_SERVICE_SVC_ID,
494 IPA_HOST_SVC_VERS, IPA_HOST_SERVICE_INS_ID);
495 if (ret)
496 goto err_server_handle_release;
497
498
499
500
501 ret = qmi_handle_init(&ipa_qmi->client_handle,
502 IPA_QMI_CLIENT_MAX_RCV_SZ, &ipa_client_ops,
503 ipa_client_msg_handlers);
504 if (ret)
505 goto err_server_handle_release;
506
507
508 INIT_WORK(&ipa_qmi->init_driver_work, ipa_client_init_driver_work);
509
510 ret = qmi_add_lookup(&ipa_qmi->client_handle, IPA_MODEM_SERVICE_SVC_ID,
511 IPA_MODEM_SVC_VERS, IPA_MODEM_SERVICE_INS_ID);
512 if (ret)
513 goto err_client_handle_release;
514
515 return 0;
516
517err_client_handle_release:
518
519 qmi_handle_release(&ipa_qmi->client_handle);
520 memset(&ipa_qmi->client_handle, 0, sizeof(ipa_qmi->client_handle));
521err_server_handle_release:
522
523 qmi_handle_release(&ipa_qmi->server_handle);
524 memset(&ipa_qmi->server_handle, 0, sizeof(ipa_qmi->server_handle));
525
526 return ret;
527}
528
529void ipa_qmi_teardown(struct ipa *ipa)
530{
531 cancel_work_sync(&ipa->qmi.init_driver_work);
532
533 qmi_handle_release(&ipa->qmi.client_handle);
534 memset(&ipa->qmi.client_handle, 0, sizeof(ipa->qmi.client_handle));
535
536 qmi_handle_release(&ipa->qmi.server_handle);
537 memset(&ipa->qmi.server_handle, 0, sizeof(ipa->qmi.server_handle));
538}
539