1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2021, The Linux Foundation. All rights reserved.
4  * Copyright (c) 2021, 2023 Qualcomm Innovation Center, Inc. All rights reserved.
5  */
6 #include <linux/module.h>
7 #include <linux/kernel.h>
8 #include <linux/qrtr.h>
9 #include <linux/soc/qcom/qmi.h>
10 #if IS_ENABLED(CONFIG_IPC_LOGGING)
11 #include <linux/ipc_logging.h>
12 #endif
13 #include <linux/sched.h>
14 #include <asm/current.h>
15 #include <linux/limits.h>
16 #include <linux/slab.h>
17 #include <linux/delay.h>
18 #include <linux/workqueue.h>
19 #include <linux/of.h>
20 #include "cnss_plat_ipc_qmi.h"
21 #include "cnss_plat_ipc_service_v01.h"
22 
23 #define CNSS_MAX_FILE_SIZE (32 * 1024 * 1024)
24 #define CNSS_PLAT_IPC_MAX_USER 1
25 #define CNSS_PLAT_IPC_QMI_FILE_TXN_TIMEOUT 10000
26 #define QMI_INIT_RETRY_MAX_TIMES 240
27 #define QMI_INIT_RETRY_DELAY_MS 250
28 #define NUM_LOG_PAGES			10
29 #define CNSS_DAEMON_CFG_WAIT_RETRY	200
30 #define CNSS_DAEMON_CFG_WAIT_MS	50
31 
32 /**
33  * struct cnss_plat_ipc_file_data: File transfer context data
34  * @name: File name
35  * @buf: Buffer provided for TX/RX file contents
36  * @id: File ID corresponding to file name
37  * @buf_size: Buffer size
38  * @file_fize: File Size
39  * @seg_index: Running index for buffer segments
40  * @seg_len: Total number of segments
41  * @end: End of transaction
42  * @complete: Completion variable for file transfer
43  */
44 struct cnss_plat_ipc_file_data {
45 	char *name;
46 	char *buf;
47 	u32 id;
48 	u32 buf_size;
49 	u32 file_size;
50 	u32 seg_index;
51 	u32 seg_len;
52 	u32 end;
53 	struct completion complete;
54 };
55 
56 /**
57  * struct cnss_plat_ipc_qmi_client_ctx: Context for QMI IPC client
58  * @client_sq: QMI IPC client QRTR socket
59  * @client_connected: QMI IPC client connection status
60  * @connection_update_cb: Registered user callback for QMI connection status
61  * @cb_ctx: Context for registered user
62  * @num_user: Number of registered users
63  */
64 struct cnss_plat_ipc_qmi_client_ctx {
65 	struct sockaddr_qrtr client_sq;
66 	bool client_connected;
67 
68 	cnss_plat_ipc_connection_update
69 		connection_update_cb[CNSS_PLAT_IPC_MAX_USER];
70 	void *cb_ctx[CNSS_PLAT_IPC_MAX_USER];
71 	u32 num_user;
72 };
73 
74 /**
75  * struct cnss_plat_ipc_qmi_svc_ctx: Platform context for QMI IPC service
76  * @svc_hdl: QMI server handle
77  * @file_idr: File ID generator
78  * @flle_idr_lock: File ID generator usage lock
79  * @qmi_client_ctx: ontext for QMI IPC client
80  */
81 struct cnss_plat_ipc_qmi_svc_ctx {
82 	struct qmi_handle *svc_hdl;
83 	struct idr file_idr;
84 	struct mutex file_idr_lock; /* File ID generator usage lock */
85 	struct cnss_plat_ipc_qmi_client_ctx
86 		qmi_client_ctx[CNSS_PLAT_IPC_MAX_QMI_CLIENTS + 1];
87 };
88 
89 static struct cnss_plat_ipc_qmi_svc_ctx plat_ipc_qmi_svc;
90 static struct cnss_plat_ipc_daemon_config daemon_cfg;
91 
92 #if IS_ENABLED(CONFIG_IPC_LOGGING)
93 static void *cnss_plat_ipc_log_context;
94 
cnss_plat_ipc_logging_init(void)95 static void cnss_plat_ipc_logging_init(void)
96 {
97 	cnss_plat_ipc_log_context = ipc_log_context_create(NUM_LOG_PAGES,
98 							   "cnss_plat", 0);
99 	if (!cnss_plat_ipc_log_context)
100 		pr_err("cnss_plat: Unable to create log context\n");
101 }
102 
cnss_plat_ipc_logging_deinit(void)103 static void cnss_plat_ipc_logging_deinit(void)
104 {
105 	if (cnss_plat_ipc_log_context) {
106 		ipc_log_context_destroy(cnss_plat_ipc_log_context);
107 		cnss_plat_ipc_log_context = NULL;
108 	}
109 }
110 
cnss_plat_ipc_debug_log_print(void * log_ctx,char * process,const char * fn,const char * log_level,char * fmt,...)111 void cnss_plat_ipc_debug_log_print(void *log_ctx, char *process, const char *fn,
112 				   const char *log_level, char *fmt, ...)
113 {
114 	struct va_format vaf;
115 	va_list va_args;
116 
117 	va_start(va_args, fmt);
118 	vaf.fmt = fmt;
119 	vaf.va = &va_args;
120 
121 	if (log_level)
122 		printk("%scnss_plat: %pV", log_level, &vaf);
123 
124 	ipc_log_string(log_ctx, "[%s] %s: %pV", process, fn, &vaf);
125 
126 	va_end(va_args);
127 }
128 
129 #define cnss_plat_ipc_log_print(_x...) \
130 		cnss_plat_ipc_debug_log_print(cnss_plat_ipc_log_context, _x)
131 #else
cnss_plat_ipc_logging_init(void)132 static void cnss_plat_ipc_logging_init(void) {};
cnss_plat_ipc_logging_deinit(void)133 static void cnss_plat_ipc_logging_deinit(void) {};
134 
cnss_plat_ipc_debug_log_print(void * log_ctx,char * process,const char * fn,const char * log_level,char * fmt,...)135 void cnss_plat_ipc_debug_log_print(void *log_ctx, char *process, const char *fn,
136 				   const char *log_level, char *fmt, ...)
137 {
138 	struct va_format vaf;
139 	va_list va_args;
140 
141 	va_start(va_args, fmt);
142 	vaf.fmt = fmt;
143 	vaf.va = &va_args;
144 
145 	if (log_level)
146 		printk("%scnss_plat: %pV", log_level, &vaf);
147 
148 	va_end(va_args);
149 }
150 
151 #define cnss_plat_ipc_log_print(_x...) \
152 		cnss_plat_ipc_debug_log_print((void *)NULL, _x)
153 #endif
154 
155 #define proc_name (in_irq() ? "irq" : \
156 		(in_softirq() ? "soft_irq" : current->comm))
157 #define cnss_plat_ipc_err(_fmt, ...) \
158 		cnss_plat_ipc_log_print(proc_name, __func__, \
159 		KERN_ERR, _fmt, ##__VA_ARGS__)
160 
161 #define cnss_plat_ipc_info(_fmt, ...) \
162 		cnss_plat_ipc_log_print(proc_name, __func__, \
163 		KERN_INFO, _fmt, ##__VA_ARGS__)
164 
165 #define cnss_plat_ipc_dbg(_fmt, ...) \
166 		cnss_plat_ipc_log_print(proc_name, __func__, \
167 		KERN_DEBUG, _fmt, ##__VA_ARGS__)
168 /**
169  * cnss_plat_ipc_init_file_data() - Initialize file transfer context data
170  * @name: File name
171  * @buf: Buffer pointer for file contents
172  * @buf_size: Buffer size for download / upload
173  * @file_size: File size for upload
174  *
175  * Return: File data pointer
176  */
177 static
cnss_plat_ipc_init_file_data(char * name,char * buf,u32 buf_size,u32 file_size)178 struct cnss_plat_ipc_file_data *cnss_plat_ipc_init_file_data(char *name,
179 							     char *buf,
180 							     u32 buf_size,
181 							     u32 file_size)
182 {
183 	struct cnss_plat_ipc_qmi_svc_ctx *svc = &plat_ipc_qmi_svc;
184 	struct cnss_plat_ipc_file_data *fd;
185 
186 	fd = kmalloc(sizeof(*fd), GFP_KERNEL);
187 	if (!fd)
188 		goto end;
189 	fd->name = name;
190 	fd->buf = buf;
191 	fd->buf_size = buf_size;
192 	fd->file_size = file_size;
193 	fd->seg_index = 0;
194 	fd->end = 0;
195 	if (file_size)
196 		fd->seg_len =
197 			(file_size / CNSS_PLAT_IPC_QMI_MAX_DATA_SIZE_V01) +
198 			!!(file_size % CNSS_PLAT_IPC_QMI_MAX_DATA_SIZE_V01);
199 	else
200 		fd->seg_len = 0;
201 	init_completion(&fd->complete);
202 	mutex_lock(&svc->file_idr_lock);
203 	fd->id = idr_alloc_cyclic(&svc->file_idr, fd, 0, U32_MAX, GFP_KERNEL);
204 	if (fd->id < 0) {
205 		kfree(fd);
206 		fd = NULL;
207 	}
208 	mutex_unlock(&svc->file_idr_lock);
209 end:
210 	return fd;
211 }
212 
213 /**
214  * cnss_plat_ipc_deinit_file_data() - Release file transfer context data
215  * @fd: File data pointer
216  *
217  * Return: 0 on success, negative error values otherwise
218  */
cnss_plat_ipc_deinit_file_data(struct cnss_plat_ipc_file_data * fd)219 static int cnss_plat_ipc_deinit_file_data(struct cnss_plat_ipc_file_data *fd)
220 {
221 	struct cnss_plat_ipc_qmi_svc_ctx *svc = &plat_ipc_qmi_svc;
222 	int ret = 0;
223 
224 	if (unlikely(!fd))
225 		return -EINVAL;
226 
227 	mutex_lock(&svc->file_idr_lock);
228 	idr_remove(&svc->file_idr, fd->id);
229 	mutex_unlock(&svc->file_idr_lock);
230 
231 	if (!fd->end)
232 		ret = -EINVAL;
233 	kfree(fd);
234 	return ret;
235 }
236 
237 /**
238  * cnss_plat_ipc_qmi_update_user() - Inform registered users about QMI
239  *                                      client status
240  * @client_id: User space QMI IPC client ID. Also works as
241  *		array index for QMI client context
242  *
243  * Return: None
244  */
245 static void
cnss_plat_ipc_qmi_update_user(enum cnss_plat_ipc_qmi_client_id_v01 client_id)246 cnss_plat_ipc_qmi_update_user(enum cnss_plat_ipc_qmi_client_id_v01 client_id)
247 {
248 	struct cnss_plat_ipc_qmi_svc_ctx *svc = &plat_ipc_qmi_svc;
249 	struct cnss_plat_ipc_qmi_client_ctx *qmi_client =
250 			&svc->qmi_client_ctx[client_id];
251 	int i;
252 
253 	for (i = 0; i < qmi_client->num_user; i++) {
254 		if (qmi_client->connection_update_cb[i])
255 			qmi_client->connection_update_cb[i]
256 						(qmi_client->cb_ctx[i],
257 						qmi_client->client_connected);
258 	}
259 }
260 
261 /**
262  * cnss_plat_ipc_qmi_file_upload() - Upload data as platform accessible file
263  * @client_id: User space QMI IPC client ID. Also works as
264  *		array index for QMI client context
265  * @file_mame: File name to store in platform data location
266  * @file_buf: Pointer to buffer with file contents
267  * @file_size: Provides the size of buffer / file size
268  *
269  * Return: 0 on success, negative error values otherwise
270  */
cnss_plat_ipc_qmi_file_upload(enum cnss_plat_ipc_qmi_client_id_v01 client_id,char * file_name,u8 * file_buf,u32 file_size)271 int cnss_plat_ipc_qmi_file_upload(enum cnss_plat_ipc_qmi_client_id_v01
272 				  client_id, char *file_name, u8 *file_buf,
273 				  u32 file_size)
274 {
275 	struct cnss_plat_ipc_qmi_file_upload_ind_msg_v01 ind;
276 	struct cnss_plat_ipc_qmi_svc_ctx *svc = &plat_ipc_qmi_svc;
277 	struct cnss_plat_ipc_qmi_client_ctx *qmi_client;
278 	int ret;
279 	struct cnss_plat_ipc_file_data *fd;
280 
281 	if (client_id > CNSS_PLAT_IPC_MAX_QMI_CLIENTS) {
282 		cnss_plat_ipc_err("Invalid Client ID: %d\n", client_id);
283 		return -EINVAL;
284 	}
285 
286 	qmi_client = &svc->qmi_client_ctx[client_id];
287 
288 	if (!qmi_client->client_connected || !file_name || !file_buf)
289 		return -EINVAL;
290 
291 	cnss_plat_ipc_info("File name: %s Size: %d\n", file_name, file_size);
292 
293 	if (file_size == 0 || file_size > CNSS_MAX_FILE_SIZE)
294 		return -EINVAL;
295 
296 	fd = cnss_plat_ipc_init_file_data(file_name, file_buf, file_size,
297 					  file_size);
298 	if (!fd) {
299 		cnss_plat_ipc_err("Unable to initialize file transfer data\n");
300 		return -EINVAL;
301 	}
302 	scnprintf(ind.file_name, CNSS_PLAT_IPC_QMI_MAX_FILE_NAME_LEN_V01, "%s",
303 		  fd->name);
304 	ind.file_size = fd->file_size;
305 	ind.file_id = fd->id;
306 
307 	ret = qmi_send_indication
308 			(svc->svc_hdl, &qmi_client->client_sq,
309 			 CNSS_PLAT_IPC_QMI_FILE_UPLOAD_IND_V01,
310 			 CNSS_PLAT_IPC_QMI_FILE_UPLOAD_IND_MSG_V01_MAX_MSG_LEN,
311 			 cnss_plat_ipc_qmi_file_upload_ind_msg_v01_ei, &ind);
312 
313 	if (ret < 0) {
314 		cnss_plat_ipc_err("QMI failed: %d\n", ret);
315 		goto end;
316 	}
317 	ret = wait_for_completion_timeout(&fd->complete,
318 					  msecs_to_jiffies
319 					  (CNSS_PLAT_IPC_QMI_FILE_TXN_TIMEOUT));
320 	if (!ret)
321 		cnss_plat_ipc_err("Timeout Uploading file: %s\n", fd->name);
322 
323 end:
324 	ret = cnss_plat_ipc_deinit_file_data(fd);
325 	cnss_plat_ipc_dbg("Status: %d\n", ret);
326 
327 	return ret;
328 }
329 EXPORT_SYMBOL(cnss_plat_ipc_qmi_file_upload);
330 
331 /**
332  * cnss_plat_ipc_qmi_file_upload_req_handler() - QMI Upload data request handler
333  * @handle: Pointer to QMI handle
334  * @sq: QMI socket
335  * @txn: QMI transaction pointer
336  * @decoded_msg: Pointer to decoded QMI message
337  *
338  * Handles the QMI upload sequence from userspace. It uses the file descriptor
339  * ID to upload buffer contents to QMI messages as segments.
340  *
341  * Return: None
342  */
343 static void
cnss_plat_ipc_qmi_file_upload_req_handler(struct qmi_handle * handle,struct sockaddr_qrtr * sq,struct qmi_txn * txn,const void * decoded_msg)344 cnss_plat_ipc_qmi_file_upload_req_handler(struct qmi_handle *handle,
345 					  struct sockaddr_qrtr *sq,
346 					  struct qmi_txn *txn,
347 					  const void *decoded_msg)
348 {
349 	struct cnss_plat_ipc_qmi_file_upload_req_msg_v01 *req_msg;
350 	struct cnss_plat_ipc_qmi_file_upload_resp_msg_v01 *resp;
351 	struct cnss_plat_ipc_qmi_svc_ctx *svc = &plat_ipc_qmi_svc;
352 	int ret = 0;
353 	struct cnss_plat_ipc_file_data *fd;
354 
355 	req_msg = (struct cnss_plat_ipc_qmi_file_upload_req_msg_v01 *)
356 		   decoded_msg;
357 	if (!req_msg)
358 		return;
359 	cnss_plat_ipc_dbg("File ID: %d Seg Index: %d\n", req_msg->file_id,
360 			  req_msg->seg_index);
361 
362 	mutex_lock(&svc->file_idr_lock);
363 	fd = idr_find(&svc->file_idr, req_msg->file_id);
364 	mutex_unlock(&svc->file_idr_lock);
365 	if (!fd) {
366 		cnss_plat_ipc_err("Invalid File ID %d\n", req_msg->file_id);
367 		return;
368 	}
369 
370 	if (req_msg->seg_index != fd->seg_index) {
371 		cnss_plat_ipc_err("File %s transfer segment failure\n", fd->name);
372 		complete(&fd->complete);
373 	}
374 
375 	resp = kzalloc(sizeof(*resp), GFP_KERNEL);
376 	if (!resp)
377 		return;
378 
379 	resp->file_id = fd->id;
380 	resp->seg_index = fd->seg_index++;
381 	resp->seg_buf_len =
382 		(fd->buf_size > CNSS_PLAT_IPC_QMI_MAX_DATA_SIZE_V01 ?
383 		 CNSS_PLAT_IPC_QMI_MAX_DATA_SIZE_V01 : fd->buf_size);
384 	resp->end = (fd->seg_index == fd->seg_len);
385 	memcpy(resp->seg_buf, fd->buf, resp->seg_buf_len);
386 
387 	cnss_plat_ipc_dbg("ID: %d Seg ID: %d Len: %d End: %d\n", resp->file_id,
388 			  resp->seg_index, resp->seg_buf_len, resp->end);
389 
390 	ret = qmi_send_response
391 		(svc->svc_hdl, sq, txn,
392 		CNSS_PLAT_IPC_QMI_FILE_UPLOAD_RESP_V01,
393 		CNSS_PLAT_IPC_QMI_FILE_UPLOAD_RESP_MSG_V01_MAX_MSG_LEN,
394 		cnss_plat_ipc_qmi_file_upload_resp_msg_v01_ei,
395 		resp);
396 
397 	if (ret < 0) {
398 		cnss_plat_ipc_err("QMI failed: %d\n", ret);
399 		goto end;
400 	}
401 
402 	fd->buf_size -= resp->seg_buf_len;
403 	fd->buf += resp->seg_buf_len;
404 	if (resp->end) {
405 		fd->end = true;
406 		complete(&fd->complete);
407 	}
408 end:
409 	kfree(resp);
410 }
411 
412 /**
413  * cnss_plat_ipc_qmi_file_download() - Download platform accessible file
414  * @client_id: User space QMI IPC client ID. Also works as
415  *		array index for QMI client context
416  * @file_mame: File name to get from platform data location
417  * @buf: Pointer of the buffer to store file contents
418  * @size: Provides the size of buffer. It is updated to reflect the file size
419  *        at the end of file download.
420  */
cnss_plat_ipc_qmi_file_download(enum cnss_plat_ipc_qmi_client_id_v01 client_id,char * file_name,char * buf,u32 * size)421 int cnss_plat_ipc_qmi_file_download(enum cnss_plat_ipc_qmi_client_id_v01
422 				    client_id, char *file_name, char *buf,
423 				    u32 *size)
424 {
425 	struct cnss_plat_ipc_qmi_file_download_ind_msg_v01 ind;
426 	struct cnss_plat_ipc_qmi_svc_ctx *svc = &plat_ipc_qmi_svc;
427 	struct cnss_plat_ipc_qmi_client_ctx *qmi_client;
428 	int ret;
429 	struct cnss_plat_ipc_file_data *fd;
430 
431 	if (client_id > CNSS_PLAT_IPC_MAX_QMI_CLIENTS) {
432 		cnss_plat_ipc_err("Invalid Client ID: %d\n", client_id);
433 		return -EINVAL;
434 	}
435 
436 	qmi_client = &svc->qmi_client_ctx[client_id];
437 
438 	if (!qmi_client->client_connected || !file_name || !buf)
439 		return -EINVAL;
440 
441 	fd = cnss_plat_ipc_init_file_data(file_name, buf, *size, 0);
442 	if (!fd) {
443 		cnss_plat_ipc_err("Unable to initialize file transfer data\n");
444 		return -EINVAL;
445 	}
446 
447 	scnprintf(ind.file_name, CNSS_PLAT_IPC_QMI_MAX_FILE_NAME_LEN_V01, "%s",
448 		  file_name);
449 	ind.file_id = fd->id;
450 
451 	ret = qmi_send_indication
452 		(svc->svc_hdl, &qmi_client->client_sq,
453 		 CNSS_PLAT_IPC_QMI_FILE_DOWNLOAD_IND_V01,
454 		 CNSS_PLAT_IPC_QMI_FILE_DOWNLOAD_IND_MSG_V01_MAX_MSG_LEN,
455 		 cnss_plat_ipc_qmi_file_download_ind_msg_v01_ei, &ind);
456 
457 	if (ret < 0) {
458 		cnss_plat_ipc_err("QMI failed: %d\n", ret);
459 		goto end;
460 	}
461 	ret = wait_for_completion_timeout(&fd->complete,
462 					  msecs_to_jiffies
463 					  (CNSS_PLAT_IPC_QMI_FILE_TXN_TIMEOUT));
464 	if (!ret)
465 		cnss_plat_ipc_err("Timeout downloading file:%s\n", fd->name);
466 
467 end:
468 	*size = fd->file_size;
469 	ret = cnss_plat_ipc_deinit_file_data(fd);
470 	cnss_plat_ipc_dbg("Status: %d Size: %d\n", ret, *size);
471 
472 	return ret;
473 }
474 EXPORT_SYMBOL(cnss_plat_ipc_qmi_file_download);
475 
476 /**
477  * cnss_plat_ipc_qmi_file_download_req_handler() - QMI download request handler
478  * @handle: Pointer to QMI handle
479  * @sq: QMI socket
480  * @txn: QMI transaction pointer
481  * @decoded_msg: Pointer to decoded QMI message
482  *
483  * Handles the QMI download request sequence to userspace. It uses the file
484  * descriptor ID to download QMI message buffer segment to file descriptor
485  * buffer.
486  *
487  * Return: None
488  */
489 static void
cnss_plat_ipc_qmi_file_download_req_handler(struct qmi_handle * handle,struct sockaddr_qrtr * sq,struct qmi_txn * txn,const void * decoded_msg)490 cnss_plat_ipc_qmi_file_download_req_handler(struct qmi_handle *handle,
491 					    struct sockaddr_qrtr *sq,
492 					    struct qmi_txn *txn,
493 					    const void *decoded_msg)
494 {
495 	struct cnss_plat_ipc_qmi_file_download_req_msg_v01 *req_msg;
496 	struct cnss_plat_ipc_qmi_file_download_resp_msg_v01 resp = {0};
497 	struct cnss_plat_ipc_qmi_svc_ctx *svc = &plat_ipc_qmi_svc;
498 	int ret = 0;
499 	struct cnss_plat_ipc_file_data *fd;
500 
501 	req_msg = (struct cnss_plat_ipc_qmi_file_download_req_msg_v01 *)
502 		   decoded_msg;
503 	if (!req_msg)
504 		return;
505 	cnss_plat_ipc_dbg("File ID: %d Size: %d Seg Len: %d Index: %d End: %d\n",
506 			  req_msg->file_id, req_msg->file_size,
507 			  req_msg->seg_buf_len, req_msg->seg_index,
508 			  req_msg->end);
509 
510 	mutex_lock(&svc->file_idr_lock);
511 	fd = idr_find(&svc->file_idr, req_msg->file_id);
512 	mutex_unlock(&svc->file_idr_lock);
513 	if (!fd) {
514 		cnss_plat_ipc_err("Invalid File ID: %d\n", req_msg->file_id);
515 		return;
516 	}
517 
518 	if (req_msg->file_size > fd->buf_size) {
519 		cnss_plat_ipc_err("File %s size %d larger than buffer size %d\n",
520 				  fd->name, req_msg->file_size, fd->buf_size);
521 		goto file_error;
522 	}
523 	if (req_msg->seg_buf_len > CNSS_PLAT_IPC_QMI_MAX_DATA_SIZE_V01 ||
524 	    ((req_msg->seg_buf_len + fd->file_size) > fd->buf_size)) {
525 		cnss_plat_ipc_err("Segment buf ID: %d buffer size %d not allowed\n",
526 				  req_msg->seg_index, req_msg->seg_buf_len);
527 		goto file_error;
528 	}
529 	if (req_msg->seg_index != fd->seg_index) {
530 		cnss_plat_ipc_err("File %s transfer segment failure\n",
531 				  fd->name);
532 		goto file_error;
533 	}
534 
535 	memcpy(fd->buf, req_msg->seg_buf, req_msg->seg_buf_len);
536 	fd->seg_index++;
537 	fd->buf += req_msg->seg_buf_len;
538 	fd->file_size += req_msg->seg_buf_len;
539 
540 	resp.file_id = fd->id;
541 	resp.seg_index = fd->seg_index;
542 	ret = qmi_send_response
543 		(svc->svc_hdl, sq, txn,
544 		CNSS_PLAT_IPC_QMI_FILE_DOWNLOAD_RESP_V01,
545 		CNSS_PLAT_IPC_QMI_FILE_DOWNLOAD_RESP_MSG_V01_MAX_MSG_LEN,
546 		cnss_plat_ipc_qmi_file_download_resp_msg_v01_ei,
547 		&resp);
548 
549 	if (ret < 0)
550 		cnss_plat_ipc_err("QMI failed: %d\n", ret);
551 
552 	if (req_msg->end) {
553 		fd->end = true;
554 		complete(&fd->complete);
555 	}
556 
557 	return;
558 file_error:
559 	complete(&fd->complete);
560 }
561 
562 /**
563  * cnss_plat_ipc_qmi_init_setup_req_handler() - Init_Setup QMI message handler
564  * @handle: Pointer to QMI handle
565  * @sq: QMI socket
566  * @txn: QMI transaction pointer
567  * @decoded_msg: Pointer to decoded QMI message
568  *
569  * Handles the QMI Init setup handshake message from userspace.
570  * buffer.
571  *
572  * Return: None
573  */
574 static void
cnss_plat_ipc_qmi_init_setup_req_handler(struct qmi_handle * handle,struct sockaddr_qrtr * sq,struct qmi_txn * txn,const void * decoded_msg)575 cnss_plat_ipc_qmi_init_setup_req_handler(struct qmi_handle *handle,
576 					 struct sockaddr_qrtr *sq,
577 					 struct qmi_txn *txn,
578 					 const void *decoded_msg)
579 {
580 	struct cnss_plat_ipc_qmi_init_setup_req_msg_v01 *req_msg;
581 	struct cnss_plat_ipc_qmi_init_setup_resp_msg_v01 resp = {0};
582 	struct cnss_plat_ipc_qmi_svc_ctx *svc = &plat_ipc_qmi_svc;
583 	struct cnss_plat_ipc_daemon_config *cfg = &daemon_cfg;
584 	int ret = 0;
585 
586 	req_msg =
587 		(struct cnss_plat_ipc_qmi_init_setup_req_msg_v01 *)decoded_msg;
588 	cnss_plat_ipc_dbg("MAC: %d HW_TRC: %d CAL: %d\n",
589 			  req_msg->dms_mac_addr_supported,
590 			  req_msg->qdss_hw_trace_override,
591 			  req_msg->cal_file_available_bitmask);
592 
593 	cfg->dms_mac_addr_supported = req_msg->dms_mac_addr_supported;
594 	cfg->qdss_hw_trace_override = req_msg->qdss_hw_trace_override;
595 	cfg->cal_file_available_bitmask = req_msg->cal_file_available_bitmask;
596 	cfg->initialized = 1;
597 
598 	ret = qmi_send_response
599 		(svc->svc_hdl, sq, txn,
600 		 CNSS_PLAT_IPC_QMI_INIT_SETUP_RESP_V01,
601 		 CNSS_PLAT_IPC_QMI_INIT_SETUP_RESP_MSG_V01_MAX_MSG_LEN,
602 		 cnss_plat_ipc_qmi_init_setup_resp_msg_v01_ei, &resp);
603 	if (ret < 0)
604 		cnss_plat_ipc_err("%s: QMI failed: %d\n", __func__, ret);
605 }
606 
607 /**
608  * cnss_plat_ipc_qmi_reg_client_req_handler() - Register QMI client
609  * @handle: Pointer to QMI handle
610  * @sq: QMI socket
611  * @txn: QMI transaction pointer
612  * @decoded_msg: Pointer to decoded QMI message
613  *
614  * Handles the userspace QMI client registration.
615  *
616  * Return: None
617  */
618 static void
cnss_plat_ipc_qmi_reg_client_req_handler(struct qmi_handle * handle,struct sockaddr_qrtr * sq,struct qmi_txn * txn,const void * decoded_msg)619 cnss_plat_ipc_qmi_reg_client_req_handler(struct qmi_handle *handle,
620 					 struct sockaddr_qrtr *sq,
621 					 struct qmi_txn *txn,
622 					 const void *decoded_msg)
623 {
624 	struct cnss_plat_ipc_qmi_reg_client_req_msg_v01 *req_msg;
625 	struct cnss_plat_ipc_qmi_reg_client_resp_msg_v01 resp = {0};
626 	struct cnss_plat_ipc_qmi_svc_ctx *svc = &plat_ipc_qmi_svc;
627 	struct cnss_plat_ipc_qmi_client_ctx *qmi_client = svc->qmi_client_ctx;
628 	int ret = 0;
629 
630 	req_msg =
631 		(struct cnss_plat_ipc_qmi_reg_client_req_msg_v01 *)decoded_msg;
632 
633 	if (req_msg->client_id_valid) {
634 		if (req_msg->client_id <= CNSS_PLAT_IPC_MAX_QMI_CLIENTS &&
635 		    !qmi_client[req_msg->client_id].client_connected) {
636 			cnss_plat_ipc_info
637 				("%s: QMI Client Connected. QMI Socket Node: %d Port: %d ID: %d\n",
638 				 __func__, sq->sq_node, sq->sq_port,
639 				 req_msg->client_id);
640 			qmi_client[req_msg->client_id].client_sq = *sq;
641 			qmi_client[req_msg->client_id].client_connected = true;
642 			cnss_plat_ipc_qmi_update_user
643 					((enum cnss_plat_ipc_qmi_client_id_v01)
644 					req_msg->client_id);
645 		} else {
646 			cnss_plat_ipc_err("QMI client already connected or Invalid client id\n");
647 			return;
648 		}
649 	}
650 
651 	ret = qmi_send_response
652 		(svc->svc_hdl, sq, txn,
653 		 CNSS_PLAT_IPC_QMI_REG_CLIENT_RESP_V01,
654 		 CNSS_PLAT_IPC_QMI_REG_CLIENT_RESP_MSG_V01_MAX_MSG_LEN,
655 		 cnss_plat_ipc_qmi_reg_client_resp_msg_v01_ei, &resp);
656 
657 	if (ret < 0)
658 		cnss_plat_ipc_err("QMI failed: %d\n", ret);
659 }
660 
661 /**
662  * cnss_plat_ipc_qmi_disconnect_cb() - Handler for QMI node disconnect specific
663  *                                     to node and port
664  * @handle: Pointer to QMI handle
665  * @node: QMI node that is disconnected
666  * @port: QMI port that is disconnected
667  *
668  * Return: None
669  */
cnss_plat_ipc_qmi_disconnect_cb(struct qmi_handle * handle,unsigned int node,unsigned int port)670 static void cnss_plat_ipc_qmi_disconnect_cb(struct qmi_handle *handle,
671 					    unsigned int node,
672 					    unsigned int port)
673 {
674 	struct cnss_plat_ipc_qmi_svc_ctx *svc = &plat_ipc_qmi_svc;
675 	struct cnss_plat_ipc_qmi_client_ctx *qmi_client =
676 					svc->qmi_client_ctx;
677 	struct cnss_plat_ipc_file_data *fd;
678 	u32 file_id;
679 	int i;
680 
681 	if (svc->svc_hdl != handle) {
682 		cnss_plat_ipc_err("Invalid QMI Handle\n");
683 		return;
684 	}
685 
686 	for (i = 0; i <= CNSS_PLAT_IPC_MAX_QMI_CLIENTS; i++) {
687 		if (qmi_client[i].client_connected &&
688 		    qmi_client[i].client_sq.sq_node == node &&
689 		    qmi_client[i].client_sq.sq_port == port) {
690 			cnss_plat_ipc_err
691 				("%s: QMI client disconnect. QMI Socket Node:%d Port:%d ID: %d\n",
692 				 __func__, node, port, i);
693 			qmi_client[i].client_sq.sq_node = 0;
694 			qmi_client[i].client_sq.sq_port = 0;
695 			qmi_client[i].client_sq.sq_family = 0;
696 			qmi_client[i].client_connected = false;
697 
698 			/* Daemon killed. Fail any download / upload in progress. This
699 			 * will also free stale fd
700 			 */
701 			mutex_lock(&svc->file_idr_lock);
702 			idr_for_each_entry(&svc->file_idr, fd, file_id)
703 				complete(&fd->complete);
704 			mutex_unlock(&svc->file_idr_lock);
705 			cnss_plat_ipc_qmi_update_user(i);
706 		}
707 	}
708 }
709 
710 /**
711  * cnss_plat_ipc_qmi_bye_cb() - Handler for QMI node disconnect for all port of
712  *                              the given node.
713  * @handle: Pointer to QMI handle
714  * @node: QMI node that is disconnected
715  *
716  * Return: None
717  */
cnss_plat_ipc_qmi_bye_cb(struct qmi_handle * handle,unsigned int node)718 static void cnss_plat_ipc_qmi_bye_cb(struct qmi_handle *handle,
719 				     unsigned int node)
720 {
721 	struct cnss_plat_ipc_qmi_svc_ctx *svc = &plat_ipc_qmi_svc;
722 	struct cnss_plat_ipc_qmi_client_ctx *qmi_client =
723 					svc->qmi_client_ctx;
724 	int i;
725 
726 	for (i = 0; i <= CNSS_PLAT_IPC_MAX_QMI_CLIENTS; i++) {
727 		cnss_plat_ipc_qmi_disconnect_cb
728 					(handle, node,
729 					 qmi_client[i].client_sq.sq_port);
730 	}
731 }
732 
733 static struct qmi_ops cnss_plat_ipc_qmi_ops = {
734 	/* inform a client that all clients from a node are gone */
735 	.bye = cnss_plat_ipc_qmi_bye_cb,
736 	.del_client = cnss_plat_ipc_qmi_disconnect_cb,
737 };
738 
739 static struct qmi_msg_handler cnss_plat_ipc_qmi_req_handlers[] = {
740 	{
741 		.type = QMI_REQUEST,
742 		.msg_id = CNSS_PLAT_IPC_QMI_REG_CLIENT_REQ_V01,
743 		.ei = cnss_plat_ipc_qmi_reg_client_req_msg_v01_ei,
744 		.decoded_size =
745 		sizeof(struct cnss_plat_ipc_qmi_reg_client_req_msg_v01),
746 		.fn = cnss_plat_ipc_qmi_reg_client_req_handler,
747 	},
748 	{
749 		.type = QMI_REQUEST,
750 		.msg_id = CNSS_PLAT_IPC_QMI_INIT_SETUP_REQ_V01,
751 		.ei = cnss_plat_ipc_qmi_init_setup_req_msg_v01_ei,
752 		.decoded_size =
753 		sizeof(struct cnss_plat_ipc_qmi_init_setup_req_msg_v01),
754 		.fn = cnss_plat_ipc_qmi_init_setup_req_handler,
755 	},
756 	{
757 		.type = QMI_REQUEST,
758 		.msg_id = CNSS_PLAT_IPC_QMI_FILE_DOWNLOAD_REQ_V01,
759 		.ei = cnss_plat_ipc_qmi_file_download_req_msg_v01_ei,
760 		.decoded_size =
761 		sizeof(struct cnss_plat_ipc_qmi_file_download_req_msg_v01),
762 		.fn = cnss_plat_ipc_qmi_file_download_req_handler,
763 	},
764 	{
765 		.type = QMI_REQUEST,
766 		.msg_id = CNSS_PLAT_IPC_QMI_FILE_UPLOAD_REQ_V01,
767 		.ei = cnss_plat_ipc_qmi_file_upload_req_msg_v01_ei,
768 		.decoded_size =
769 		sizeof(struct cnss_plat_ipc_qmi_file_upload_req_msg_v01),
770 		.fn = cnss_plat_ipc_qmi_file_upload_req_handler,
771 	},
772 	{}
773 };
774 
775 /**
776  * cnss_plat_ipc_qmi_daemon_config() - Get daemon config for CNSS platform
777  *
778  * Return: Pointer to daemon client config
779  */
cnss_plat_ipc_qmi_daemon_config(void)780 struct cnss_plat_ipc_daemon_config *cnss_plat_ipc_qmi_daemon_config(void)
781 {
782 	int i;
783 	struct cnss_plat_ipc_qmi_svc_ctx *svc = &plat_ipc_qmi_svc;
784 	struct cnss_plat_ipc_qmi_client_ctx *qmi_client =
785 		&svc->qmi_client_ctx[CNSS_PLAT_IPC_DAEMON_QMI_CLIENT_V01];
786 
787 	if (!qmi_client->client_connected)
788 		return NULL;
789 
790 	for (i = 0; i < CNSS_DAEMON_CFG_WAIT_RETRY; i++) {
791 		if (daemon_cfg.initialized == 1)
792 			break;
793 
794 		msleep(CNSS_DAEMON_CFG_WAIT_MS);
795 	}
796 
797 	return &daemon_cfg;
798 }
799 EXPORT_SYMBOL(cnss_plat_ipc_qmi_daemon_config);
800 
801 /**
802  * cnss_plat_ipc_register() - Register for QMI IPC client status update
803  * @client_id: User space QMI IPC client ID. Also works as
804  *		array index for QMI client context
805  * @connect_update_cb: Function pointer for callback
806  * @cb_ctx: Callback context
807  *
808  * Return: 0 on success, negative error value otherwise
809  */
cnss_plat_ipc_register(enum cnss_plat_ipc_qmi_client_id_v01 client_id,cnss_plat_ipc_connection_update connection_update_cb,void * cb_ctx)810 int cnss_plat_ipc_register(enum cnss_plat_ipc_qmi_client_id_v01 client_id,
811 			   cnss_plat_ipc_connection_update
812 			   connection_update_cb, void *cb_ctx)
813 {
814 	struct cnss_plat_ipc_qmi_svc_ctx *svc = &plat_ipc_qmi_svc;
815 	struct cnss_plat_ipc_qmi_client_ctx *qmi_client;
816 	int num_user;
817 
818 	if (client_id > CNSS_PLAT_IPC_MAX_QMI_CLIENTS) {
819 		cnss_plat_ipc_err("Invalid Client ID: %d\n", client_id);
820 		return -EINVAL;
821 	}
822 
823 	qmi_client = &svc->qmi_client_ctx[client_id];
824 	num_user = qmi_client->num_user;
825 
826 	if (num_user >= CNSS_PLAT_IPC_MAX_USER) {
827 		cnss_plat_ipc_err("Max Service users reached\n");
828 		return -EINVAL;
829 	}
830 
831 	qmi_client->connection_update_cb[num_user] = connection_update_cb;
832 	qmi_client->cb_ctx[num_user] = cb_ctx;
833 	qmi_client->num_user++;
834 
835 	return 0;
836 }
837 EXPORT_SYMBOL(cnss_plat_ipc_register);
838 
839 /**
840  * cnss_plat_ipc_unregister() - Unregister QMI IPC client status callback
841  * @client_id: User space QMI IPC client ID. Also works as
842  *		array index for QMI client context
843  * @cb_cbt: Callback context provided during registration
844  *
845  * Return: None
846  */
cnss_plat_ipc_unregister(enum cnss_plat_ipc_qmi_client_id_v01 client_id,void * cb_ctx)847 void cnss_plat_ipc_unregister(enum cnss_plat_ipc_qmi_client_id_v01 client_id,
848 			      void *cb_ctx)
849 {
850 	struct cnss_plat_ipc_qmi_svc_ctx *svc = &plat_ipc_qmi_svc;
851 	struct cnss_plat_ipc_qmi_client_ctx *qmi_client;
852 	int i;
853 
854 	if (client_id > CNSS_PLAT_IPC_MAX_QMI_CLIENTS) {
855 		cnss_plat_ipc_err("Invalid Client ID: %d\n", client_id);
856 		return;
857 	}
858 
859 	qmi_client = &svc->qmi_client_ctx[client_id];
860 
861 	for (i = 0; i < qmi_client->num_user; i++) {
862 		if (qmi_client->cb_ctx[i] == cb_ctx) {
863 			qmi_client->cb_ctx[i] = NULL;
864 			qmi_client->connection_update_cb[i] = NULL;
865 			qmi_client->num_user--;
866 			break;
867 		}
868 	}
869 }
870 EXPORT_SYMBOL(cnss_plat_ipc_unregister);
871 
872 /**
873  * cnss_plat_ipc_init_fn() - CNSS Platform qmi service init function
874  *
875  * Initialize a QMI client handle and register new QMI service for CNSS Platform
876  *
877  * Return: None
878  */
cnss_plat_ipc_init_fn(struct work_struct * work)879 static void cnss_plat_ipc_init_fn(struct work_struct *work)
880 {
881 	int ret = 0, retry = 0;
882 	struct cnss_plat_ipc_qmi_svc_ctx *svc = &plat_ipc_qmi_svc;
883 
884 	svc->svc_hdl = kzalloc(sizeof(*svc->svc_hdl), GFP_KERNEL);
885 	if (!svc->svc_hdl)
886 		return;
887 
888 retry:
889 	ret = qmi_handle_init(svc->svc_hdl,
890 			      CNSS_PLAT_IPC_QMI_MAX_MSG_SIZE_V01,
891 			      &cnss_plat_ipc_qmi_ops,
892 			      cnss_plat_ipc_qmi_req_handlers);
893 	if (ret < 0) {
894 		/* If QMI fails to init, retry for total
895 		 * QMI_INIT_RETRY_DELAY_MS * QMI_INIT_RETRY_MAX_TIMES ms.
896 		 */
897 		if (retry++ < QMI_INIT_RETRY_MAX_TIMES) {
898 			msleep(QMI_INIT_RETRY_DELAY_MS);
899 			goto retry;
900 		}
901 		cnss_plat_ipc_err("Failed to init QMI handle after %d ms * %d, err = %d\n",
902 				  QMI_INIT_RETRY_DELAY_MS,
903 				  QMI_INIT_RETRY_MAX_TIMES, ret);
904 		goto free_svc_hdl;
905 	}
906 
907 	ret = qmi_add_server(svc->svc_hdl,
908 			     CNSS_PLATFORM_SERVICE_ID_V01,
909 			     CNSS_PLATFORM_SERVICE_VERS_V01, 0);
910 	if (ret < 0) {
911 		cnss_plat_ipc_err("Server add fail: %d\n", ret);
912 		goto release_svc_hdl;
913 	}
914 
915 	cnss_plat_ipc_info("CNSS Platform IPC QMI Service is started\n");
916 	idr_init(&svc->file_idr);
917 	mutex_init(&svc->file_idr_lock);
918 	return;
919 
920 release_svc_hdl:
921 	qmi_handle_release(svc->svc_hdl);
922 free_svc_hdl:
923 	kfree(svc->svc_hdl);
924 }
925 
926 /**
927  * cnss_plat_ipc_is_valid_dt_node_found - Check if valid device tree node
928  *                                        present
929  *
930  * Valid device tree node means a node with "qcom,wlan" property present
931  * and "status" property not disabled.
932  *
933  * Return: true if valid device tree node found, false if not found
934  */
cnss_plat_ipc_is_valid_dt_node_found(void)935 static bool cnss_plat_ipc_is_valid_dt_node_found(void)
936 {
937 	struct device_node *dn = NULL;
938 
939 	for_each_node_with_property(dn, "qcom,wlan") {
940 		if (of_device_is_available(dn))
941 			break;
942 	}
943 
944 	if (dn)
945 		return true;
946 
947 	return false;
948 }
949 
950 static DECLARE_WORK(cnss_plat_ipc_init_work, cnss_plat_ipc_init_fn);
951 
cnss_plat_ipc_qmi_svc_init(void)952 static int __init cnss_plat_ipc_qmi_svc_init(void)
953 {
954 	if (!cnss_plat_ipc_is_valid_dt_node_found())
955 		return -ENODEV;
956 
957 	/* Schedule a work to do real init to avoid blocking here */
958 	cnss_plat_ipc_logging_init();
959 	schedule_work(&cnss_plat_ipc_init_work);
960 	return 0;
961 }
962 
963 /**
964  * cnss_plat_ipc_qmi_svc_exit() - CNSS Platform qmi service exit
965  *
966  * Release all resources during exit
967  *
968  * Return: None
969  */
cnss_plat_ipc_qmi_svc_exit(void)970 static void __exit cnss_plat_ipc_qmi_svc_exit(void)
971 {
972 	struct cnss_plat_ipc_qmi_svc_ctx *svc = &plat_ipc_qmi_svc;
973 
974 	cancel_work_sync(&cnss_plat_ipc_init_work);
975 
976 	if (svc->svc_hdl) {
977 		qmi_handle_release(svc->svc_hdl);
978 		kfree(svc->svc_hdl);
979 		idr_destroy(&svc->file_idr);
980 	}
981 
982 	cnss_plat_ipc_logging_deinit();
983 }
984 
985 module_init(cnss_plat_ipc_qmi_svc_init);
986 module_exit(cnss_plat_ipc_qmi_svc_exit);
987 MODULE_LICENSE("GPL v2");
988 MODULE_DESCRIPTION("CNSS Platform IPC QMI Service");
989