cnss_plat_ipc_qmi.c 27 KB

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