sysmon-qmi.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. /*
  2. * Copyright (c) 2014-2017, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. */
  14. #define pr_fmt(fmt) "sysmon-qmi: %s: " fmt, __func__
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/string.h>
  18. #include <linux/completion.h>
  19. #include <linux/slab.h>
  20. #include <linux/of.h>
  21. #include <soc/qcom/subsystem_restart.h>
  22. #include <soc/qcom/subsystem_notif.h>
  23. #include <soc/qcom/msm_qmi_interface.h>
  24. #include <soc/qcom/sysmon.h>
  25. #define QMI_RESP_BIT_SHIFT(x) (x << 16)
  26. #define QMI_SSCTL_RESTART_REQ_V02 0x0020
  27. #define QMI_SSCTL_RESTART_RESP_V02 0x0020
  28. #define QMI_SSCTL_RESTART_READY_IND_V02 0x0020
  29. #define QMI_SSCTL_SHUTDOWN_REQ_V02 0x0021
  30. #define QMI_SSCTL_SHUTDOWN_RESP_V02 0x0021
  31. #define QMI_SSCTL_SHUTDOWN_READY_IND_V02 0x0021
  32. #define QMI_SSCTL_GET_FAILURE_REASON_REQ_V02 0x0022
  33. #define QMI_SSCTL_GET_FAILURE_REASON_RESP_V02 0x0022
  34. #define QMI_SSCTL_SUBSYS_EVENT_REQ_V02 0x0023
  35. #define QMI_SSCTL_SUBSYS_EVENT_RESP_V02 0x0023
  36. #define QMI_SSCTL_SUBSYS_EVENT_READY_IND_V02 0x0023
  37. #define QMI_SSCTL_ERROR_MSG_LENGTH 90
  38. #define QMI_SSCTL_SUBSYS_NAME_LENGTH 15
  39. #define QMI_SSCTL_SUBSYS_EVENT_REQ_LENGTH 40
  40. #define QMI_SSCTL_RESP_MSG_LENGTH 7
  41. #define QMI_SSCTL_EMPTY_MSG_LENGTH 0
  42. #define SSCTL_SERVICE_ID 0x2B
  43. #define SSCTL_VER_2 2
  44. #define SERVER_TIMEOUT 500
  45. #define SHUTDOWN_TIMEOUT 10000
  46. #define QMI_EOTI_DATA_TYPE \
  47. { \
  48. .data_type = QMI_EOTI, \
  49. .elem_len = 0, \
  50. .elem_size = 0, \
  51. .is_array = NO_ARRAY, \
  52. .tlv_type = 0x00, \
  53. .offset = 0, \
  54. .ei_array = NULL, \
  55. },
  56. struct sysmon_qmi_data {
  57. const char *name;
  58. int instance_id;
  59. struct work_struct svc_arrive;
  60. struct work_struct svc_exit;
  61. struct work_struct svc_rcv_msg;
  62. struct qmi_handle *clnt_handle;
  63. struct notifier_block notifier;
  64. void *notif_handle;
  65. bool legacy_version;
  66. struct completion server_connect;
  67. struct completion ind_recv;
  68. struct list_head list;
  69. };
  70. static struct workqueue_struct *sysmon_wq;
  71. static LIST_HEAD(sysmon_list);
  72. static DEFINE_MUTEX(sysmon_list_lock);
  73. static DEFINE_MUTEX(sysmon_lock);
  74. static void sysmon_clnt_recv_msg(struct work_struct *work);
  75. static void sysmon_clnt_svc_arrive(struct work_struct *work);
  76. static void sysmon_clnt_svc_exit(struct work_struct *work);
  77. static const int notif_map[SUBSYS_NOTIF_TYPE_COUNT] = {
  78. [SUBSYS_BEFORE_POWERUP] = SSCTL_SSR_EVENT_BEFORE_POWERUP,
  79. [SUBSYS_AFTER_POWERUP] = SSCTL_SSR_EVENT_AFTER_POWERUP,
  80. [SUBSYS_BEFORE_SHUTDOWN] = SSCTL_SSR_EVENT_BEFORE_SHUTDOWN,
  81. [SUBSYS_AFTER_SHUTDOWN] = SSCTL_SSR_EVENT_AFTER_SHUTDOWN,
  82. };
  83. static void sysmon_ind_cb(struct qmi_handle *handle, unsigned int msg_id,
  84. void *msg, unsigned int msg_len, void *ind_cb_priv)
  85. {
  86. struct sysmon_qmi_data *data = NULL, *temp;
  87. mutex_lock(&sysmon_list_lock);
  88. list_for_each_entry(temp, &sysmon_list, list)
  89. if (!strcmp(temp->name, (char *)ind_cb_priv))
  90. data = temp;
  91. mutex_unlock(&sysmon_list_lock);
  92. if (!data)
  93. return;
  94. pr_debug("%s: Indication received from subsystem\n", data->name);
  95. complete(&data->ind_recv);
  96. }
  97. static int sysmon_svc_event_notify(struct notifier_block *this,
  98. unsigned long code,
  99. void *_cmd)
  100. {
  101. struct sysmon_qmi_data *data = container_of(this,
  102. struct sysmon_qmi_data, notifier);
  103. switch (code) {
  104. case QMI_SERVER_ARRIVE:
  105. queue_work(sysmon_wq, &data->svc_arrive);
  106. break;
  107. case QMI_SERVER_EXIT:
  108. queue_work(sysmon_wq, &data->svc_exit);
  109. break;
  110. default:
  111. break;
  112. }
  113. return 0;
  114. }
  115. static void sysmon_clnt_notify(struct qmi_handle *handle,
  116. enum qmi_event_type event, void *notify_priv)
  117. {
  118. struct sysmon_qmi_data *data = container_of(notify_priv,
  119. struct sysmon_qmi_data, svc_arrive);
  120. switch (event) {
  121. case QMI_RECV_MSG:
  122. schedule_work(&data->svc_rcv_msg);
  123. break;
  124. default:
  125. break;
  126. }
  127. }
  128. static void sysmon_clnt_svc_arrive(struct work_struct *work)
  129. {
  130. int rc;
  131. struct sysmon_qmi_data *data = container_of(work,
  132. struct sysmon_qmi_data, svc_arrive);
  133. mutex_lock(&sysmon_lock);
  134. /* Create a Local client port for QMI communication */
  135. data->clnt_handle = qmi_handle_create(sysmon_clnt_notify, work);
  136. if (!data->clnt_handle) {
  137. pr_err("QMI client handle alloc failed for %s\n", data->name);
  138. mutex_unlock(&sysmon_lock);
  139. return;
  140. }
  141. rc = qmi_connect_to_service(data->clnt_handle, SSCTL_SERVICE_ID,
  142. SSCTL_VER_2, data->instance_id);
  143. if (rc < 0) {
  144. pr_err("%s: Could not connect handle to service\n",
  145. data->name);
  146. qmi_handle_destroy(data->clnt_handle);
  147. data->clnt_handle = NULL;
  148. mutex_unlock(&sysmon_lock);
  149. return;
  150. }
  151. pr_info("Connection established between QMI handle and %s's SSCTL service\n"
  152. , data->name);
  153. rc = qmi_register_ind_cb(data->clnt_handle, sysmon_ind_cb,
  154. (void *)data->name);
  155. if (rc < 0)
  156. pr_warn("%s: Could not register the indication callback\n",
  157. data->name);
  158. mutex_unlock(&sysmon_lock);
  159. }
  160. static void sysmon_clnt_svc_exit(struct work_struct *work)
  161. {
  162. struct sysmon_qmi_data *data = container_of(work,
  163. struct sysmon_qmi_data, svc_exit);
  164. mutex_lock(&sysmon_lock);
  165. qmi_handle_destroy(data->clnt_handle);
  166. data->clnt_handle = NULL;
  167. mutex_unlock(&sysmon_lock);
  168. }
  169. static void sysmon_clnt_recv_msg(struct work_struct *work)
  170. {
  171. int ret;
  172. struct sysmon_qmi_data *data = container_of(work,
  173. struct sysmon_qmi_data, svc_rcv_msg);
  174. do {
  175. pr_debug("%s: Notified about a Receive event\n", data->name);
  176. } while ((ret = qmi_recv_msg(data->clnt_handle)) == 0);
  177. if (ret != -ENOMSG)
  178. pr_err("%s: Error receiving message\n", data->name);
  179. }
  180. struct qmi_ssctl_subsys_event_req_msg {
  181. uint8_t subsys_name_len;
  182. char subsys_name[QMI_SSCTL_SUBSYS_NAME_LENGTH];
  183. enum ssctl_ssr_event_enum_type event;
  184. uint8_t evt_driven_valid;
  185. enum ssctl_ssr_event_driven_enum_type evt_driven;
  186. };
  187. struct qmi_ssctl_subsys_event_resp_msg {
  188. struct qmi_response_type_v01 resp;
  189. };
  190. static struct elem_info qmi_ssctl_subsys_event_req_msg_ei[] = {
  191. {
  192. .data_type = QMI_DATA_LEN,
  193. .elem_len = 1,
  194. .elem_size = sizeof(uint8_t),
  195. .is_array = NO_ARRAY,
  196. .tlv_type = 0x01,
  197. .offset = offsetof(struct qmi_ssctl_subsys_event_req_msg,
  198. subsys_name_len),
  199. .ei_array = NULL,
  200. },
  201. {
  202. .data_type = QMI_UNSIGNED_1_BYTE,
  203. .elem_len = QMI_SSCTL_SUBSYS_NAME_LENGTH,
  204. .elem_size = sizeof(char),
  205. .is_array = VAR_LEN_ARRAY,
  206. .tlv_type = 0x01,
  207. .offset = offsetof(struct qmi_ssctl_subsys_event_req_msg,
  208. subsys_name),
  209. .ei_array = NULL,
  210. },
  211. {
  212. .data_type = QMI_SIGNED_4_BYTE_ENUM,
  213. .elem_len = 1,
  214. .elem_size = sizeof(uint32_t),
  215. .is_array = NO_ARRAY,
  216. .tlv_type = 0x02,
  217. .offset = offsetof(struct qmi_ssctl_subsys_event_req_msg,
  218. event),
  219. .ei_array = NULL,
  220. },
  221. {
  222. .data_type = QMI_OPT_FLAG,
  223. .elem_len = 1,
  224. .elem_size = sizeof(uint8_t),
  225. .is_array = NO_ARRAY,
  226. .tlv_type = 0x10,
  227. .offset = offsetof(struct qmi_ssctl_subsys_event_req_msg,
  228. evt_driven_valid),
  229. .ei_array = NULL,
  230. },
  231. {
  232. .data_type = QMI_SIGNED_4_BYTE_ENUM,
  233. .elem_len = 1,
  234. .elem_size = sizeof(uint32_t),
  235. .is_array = NO_ARRAY,
  236. .tlv_type = 0x10,
  237. .offset = offsetof(struct qmi_ssctl_subsys_event_req_msg,
  238. evt_driven),
  239. .ei_array = NULL,
  240. },
  241. QMI_EOTI_DATA_TYPE
  242. };
  243. static struct elem_info qmi_ssctl_subsys_event_resp_msg_ei[] = {
  244. {
  245. .data_type = QMI_STRUCT,
  246. .elem_len = 1,
  247. .elem_size = sizeof(struct qmi_response_type_v01),
  248. .is_array = NO_ARRAY,
  249. .tlv_type = 0x02,
  250. .offset = offsetof(struct qmi_ssctl_subsys_event_resp_msg,
  251. resp),
  252. .ei_array = get_qmi_response_type_v01_ei(),
  253. },
  254. QMI_EOTI_DATA_TYPE
  255. };
  256. /**
  257. * sysmon_send_event() - Notify a subsystem of another's state change
  258. * @dest_desc: Subsystem descriptor of the subsystem the notification
  259. * should be sent to
  260. * @event_desc: Subsystem descriptor of the subsystem that generated the
  261. * notification
  262. * @notif: ID of the notification type (ex. SUBSYS_BEFORE_SHUTDOWN)
  263. *
  264. * Reverts to using legacy sysmon API (sysmon_send_event_no_qmi()) if
  265. * client handle is not set.
  266. *
  267. * Returns 0 for success, -EINVAL for invalid destination or notification IDs,
  268. * -ENODEV if the transport channel is not open, -ETIMEDOUT if the destination
  269. * subsystem does not respond, and -EPROTO if the destination subsystem
  270. * responds, but with something other than an acknowledgment.
  271. *
  272. * If CONFIG_MSM_SYSMON_COMM is not defined, always return success (0).
  273. */
  274. int sysmon_send_event(struct subsys_desc *dest_desc,
  275. struct subsys_desc *event_desc,
  276. enum subsys_notif_type notif)
  277. {
  278. struct qmi_ssctl_subsys_event_req_msg req;
  279. struct msg_desc req_desc, resp_desc;
  280. struct qmi_ssctl_subsys_event_resp_msg resp = { { 0, 0 } };
  281. struct sysmon_qmi_data *data = NULL, *temp;
  282. const char *event_ss = event_desc->name;
  283. const char *dest_ss = dest_desc->name;
  284. int ret;
  285. if (notif < 0 || notif >= SUBSYS_NOTIF_TYPE_COUNT || event_ss == NULL
  286. || dest_ss == NULL)
  287. return -EINVAL;
  288. mutex_lock(&sysmon_list_lock);
  289. list_for_each_entry(temp, &sysmon_list, list)
  290. if (!strcmp(temp->name, dest_desc->name))
  291. data = temp;
  292. mutex_unlock(&sysmon_list_lock);
  293. if (!data)
  294. return -EINVAL;
  295. if (!data->clnt_handle) {
  296. pr_debug("No SSCTL_V2 support for %s. Revert to SSCTL_V0\n",
  297. dest_ss);
  298. ret = sysmon_send_event_no_qmi(dest_desc, event_desc, notif);
  299. if (ret)
  300. pr_debug("SSCTL_V0 implementation failed - %d\n", ret);
  301. return ret;
  302. }
  303. snprintf(req.subsys_name, ARRAY_SIZE(req.subsys_name), "%s", event_ss);
  304. req.subsys_name_len = strlen(req.subsys_name);
  305. req.event = notif_map[notif];
  306. req.evt_driven_valid = 1;
  307. req.evt_driven = SSCTL_SSR_EVENT_FORCED;
  308. req_desc.msg_id = QMI_SSCTL_SUBSYS_EVENT_REQ_V02;
  309. req_desc.max_msg_len = QMI_SSCTL_SUBSYS_EVENT_REQ_LENGTH;
  310. req_desc.ei_array = qmi_ssctl_subsys_event_req_msg_ei;
  311. resp_desc.msg_id = QMI_SSCTL_SUBSYS_EVENT_RESP_V02;
  312. resp_desc.max_msg_len = QMI_SSCTL_RESP_MSG_LENGTH;
  313. resp_desc.ei_array = qmi_ssctl_subsys_event_resp_msg_ei;
  314. mutex_lock(&sysmon_lock);
  315. ret = qmi_send_req_wait(data->clnt_handle, &req_desc, &req,
  316. sizeof(req), &resp_desc, &resp, sizeof(resp), SERVER_TIMEOUT);
  317. if (ret < 0) {
  318. pr_err("QMI send req to %s failed, ret - %d\n", dest_ss, ret);
  319. goto out;
  320. }
  321. /* Check the response */
  322. if (QMI_RESP_BIT_SHIFT(resp.resp.result) != QMI_RESULT_SUCCESS_V01) {
  323. pr_debug("QMI request failed 0x%x\n",
  324. QMI_RESP_BIT_SHIFT(resp.resp.error));
  325. ret = -EREMOTEIO;
  326. }
  327. out:
  328. mutex_unlock(&sysmon_lock);
  329. return ret;
  330. }
  331. EXPORT_SYMBOL(sysmon_send_event);
  332. struct qmi_ssctl_shutdown_req_msg {
  333. };
  334. struct qmi_ssctl_shutdown_resp_msg {
  335. struct qmi_response_type_v01 resp;
  336. };
  337. static struct elem_info qmi_ssctl_shutdown_req_msg_ei[] = {
  338. QMI_EOTI_DATA_TYPE
  339. };
  340. static struct elem_info qmi_ssctl_shutdown_resp_msg_ei[] = {
  341. {
  342. .data_type = QMI_STRUCT,
  343. .elem_len = 1,
  344. .elem_size = sizeof(struct qmi_response_type_v01),
  345. .is_array = NO_ARRAY,
  346. .tlv_type = 0x02,
  347. .offset = offsetof(struct qmi_ssctl_shutdown_resp_msg,
  348. resp),
  349. .ei_array = get_qmi_response_type_v01_ei(),
  350. },
  351. QMI_EOTI_DATA_TYPE
  352. };
  353. /**
  354. * sysmon_send_shutdown() - send shutdown command to a
  355. * subsystem.
  356. * @dest_desc: Subsystem descriptor of the subsystem to send to
  357. *
  358. * Reverts to using legacy sysmon API (sysmon_send_shutdown_no_qmi()) if
  359. * client handle is not set.
  360. *
  361. * Returns 0 for success, -EINVAL for an invalid destination, -ENODEV if
  362. * the SMD transport channel is not open, -ETIMEDOUT if the destination
  363. * subsystem does not respond, and -EPROTO if the destination subsystem
  364. * responds with something unexpected.
  365. *
  366. * If CONFIG_MSM_SYSMON_COMM is not defined, always return success (0).
  367. */
  368. int sysmon_send_shutdown(struct subsys_desc *dest_desc)
  369. {
  370. struct msg_desc req_desc, resp_desc;
  371. struct qmi_ssctl_shutdown_resp_msg resp = { { 0, 0 } };
  372. struct sysmon_qmi_data *data = NULL, *temp;
  373. const char *dest_ss = dest_desc->name;
  374. char req = 0;
  375. int ret, shutdown_ack_ret;
  376. if (dest_ss == NULL)
  377. return -EINVAL;
  378. mutex_lock(&sysmon_list_lock);
  379. list_for_each_entry(temp, &sysmon_list, list)
  380. if (!strcmp(temp->name, dest_desc->name))
  381. data = temp;
  382. mutex_unlock(&sysmon_list_lock);
  383. if (!data)
  384. return -EINVAL;
  385. if (!data->clnt_handle) {
  386. pr_debug("No SSCTL_V2 support for %s. Revert to SSCTL_V0\n",
  387. dest_ss);
  388. ret = sysmon_send_shutdown_no_qmi(dest_desc);
  389. if (ret)
  390. pr_debug("SSCTL_V0 implementation failed - %d\n", ret);
  391. return ret;
  392. }
  393. req_desc.msg_id = QMI_SSCTL_SHUTDOWN_REQ_V02;
  394. req_desc.max_msg_len = QMI_SSCTL_EMPTY_MSG_LENGTH;
  395. req_desc.ei_array = qmi_ssctl_shutdown_req_msg_ei;
  396. resp_desc.msg_id = QMI_SSCTL_SHUTDOWN_RESP_V02;
  397. resp_desc.max_msg_len = QMI_SSCTL_RESP_MSG_LENGTH;
  398. resp_desc.ei_array = qmi_ssctl_shutdown_resp_msg_ei;
  399. reinit_completion(&data->ind_recv);
  400. mutex_lock(&sysmon_lock);
  401. ret = qmi_send_req_wait(data->clnt_handle, &req_desc, &req,
  402. sizeof(req), &resp_desc, &resp, sizeof(resp), SERVER_TIMEOUT);
  403. if (ret < 0) {
  404. pr_err("QMI send req to %s failed, ret - %d\n", dest_ss, ret);
  405. goto out;
  406. }
  407. /* Check the response */
  408. if (QMI_RESP_BIT_SHIFT(resp.resp.result) != QMI_RESULT_SUCCESS_V01) {
  409. pr_err("QMI request failed 0x%x\n",
  410. QMI_RESP_BIT_SHIFT(resp.resp.error));
  411. ret = -EREMOTEIO;
  412. goto out;
  413. }
  414. shutdown_ack_ret = wait_for_shutdown_ack(dest_desc);
  415. if (shutdown_ack_ret < 0) {
  416. pr_err("shutdown_ack SMP2P bit for %s not set\n", data->name);
  417. if (!&data->ind_recv.done) {
  418. pr_err("QMI shutdown indication not received\n");
  419. ret = shutdown_ack_ret;
  420. }
  421. goto out;
  422. } else if (shutdown_ack_ret > 0)
  423. goto out;
  424. if (!wait_for_completion_timeout(&data->ind_recv,
  425. msecs_to_jiffies(SHUTDOWN_TIMEOUT))) {
  426. pr_err("Timed out waiting for shutdown indication from %s\n",
  427. data->name);
  428. ret = -ETIMEDOUT;
  429. }
  430. out:
  431. mutex_unlock(&sysmon_lock);
  432. return ret;
  433. }
  434. EXPORT_SYMBOL(sysmon_send_shutdown);
  435. struct qmi_ssctl_get_failure_reason_req_msg {
  436. };
  437. struct qmi_ssctl_get_failure_reason_resp_msg {
  438. struct qmi_response_type_v01 resp;
  439. uint8_t error_message_valid;
  440. uint32_t error_message_len;
  441. char error_message[QMI_SSCTL_ERROR_MSG_LENGTH];
  442. };
  443. static struct elem_info qmi_ssctl_get_failure_reason_req_msg_ei[] = {
  444. QMI_EOTI_DATA_TYPE
  445. };
  446. static struct elem_info qmi_ssctl_get_failure_reason_resp_msg_ei[] = {
  447. {
  448. .data_type = QMI_STRUCT,
  449. .elem_len = 1,
  450. .elem_size = sizeof(struct qmi_response_type_v01),
  451. .is_array = NO_ARRAY,
  452. .tlv_type = 0x02,
  453. .offset = offsetof(
  454. struct qmi_ssctl_get_failure_reason_resp_msg,
  455. resp),
  456. .ei_array = get_qmi_response_type_v01_ei(),
  457. },
  458. {
  459. .data_type = QMI_OPT_FLAG,
  460. .elem_len = 1,
  461. .elem_size = sizeof(uint8_t),
  462. .is_array = NO_ARRAY,
  463. .tlv_type = 0x10,
  464. .offset = offsetof(
  465. struct qmi_ssctl_get_failure_reason_resp_msg,
  466. error_message_valid),
  467. .ei_array = NULL,
  468. },
  469. {
  470. .data_type = QMI_DATA_LEN,
  471. .elem_len = 1,
  472. .elem_size = sizeof(uint8_t),
  473. .is_array = NO_ARRAY,
  474. .tlv_type = 0x10,
  475. .offset = offsetof(
  476. struct qmi_ssctl_get_failure_reason_resp_msg,
  477. error_message_len),
  478. .ei_array = NULL,
  479. },
  480. {
  481. .data_type = QMI_UNSIGNED_1_BYTE,
  482. .elem_len = QMI_SSCTL_ERROR_MSG_LENGTH,
  483. .elem_size = sizeof(char),
  484. .is_array = VAR_LEN_ARRAY,
  485. .tlv_type = 0x10,
  486. .offset = offsetof(
  487. struct qmi_ssctl_get_failure_reason_resp_msg,
  488. error_message),
  489. .ei_array = NULL,
  490. },
  491. QMI_EOTI_DATA_TYPE
  492. };
  493. /**
  494. * sysmon_get_reason() - Retrieve failure reason from a subsystem.
  495. * @dest_desc: Subsystem descriptor of the subsystem to query
  496. * @buf: Caller-allocated buffer for the returned NUL-terminated reason
  497. * @len: Length of @buf
  498. *
  499. * Reverts to using legacy sysmon API (sysmon_get_reason_no_qmi()) if client
  500. * handle is not set.
  501. *
  502. * Returns 0 for success, -EINVAL for an invalid destination, -ENODEV if
  503. * the SMD transport channel is not open, -ETIMEDOUT if the destination
  504. * subsystem does not respond, and -EPROTO if the destination subsystem
  505. * responds with something unexpected.
  506. *
  507. * If CONFIG_MSM_SYSMON_COMM is not defined, always return success (0).
  508. */
  509. int sysmon_get_reason(struct subsys_desc *dest_desc, char *buf, size_t len)
  510. {
  511. struct msg_desc req_desc, resp_desc;
  512. struct qmi_ssctl_get_failure_reason_resp_msg resp;
  513. struct sysmon_qmi_data *data = NULL, *temp;
  514. const char *dest_ss = dest_desc->name;
  515. const char expect[] = "ssr:return:";
  516. char req = 0;
  517. int ret;
  518. if (dest_ss == NULL || buf == NULL || len == 0)
  519. return -EINVAL;
  520. mutex_lock(&sysmon_list_lock);
  521. list_for_each_entry(temp, &sysmon_list, list)
  522. if (!strcmp(temp->name, dest_desc->name))
  523. data = temp;
  524. mutex_unlock(&sysmon_list_lock);
  525. if (!data)
  526. return -EINVAL;
  527. if (!data->clnt_handle) {
  528. pr_debug("No SSCTL_V2 support for %s. Revert to SSCTL_V0\n",
  529. dest_ss);
  530. ret = sysmon_get_reason_no_qmi(dest_desc, buf, len);
  531. if (ret)
  532. pr_debug("SSCTL_V0 implementation failed - %d\n", ret);
  533. return ret;
  534. }
  535. req_desc.msg_id = QMI_SSCTL_GET_FAILURE_REASON_REQ_V02;
  536. req_desc.max_msg_len = QMI_SSCTL_EMPTY_MSG_LENGTH;
  537. req_desc.ei_array = qmi_ssctl_get_failure_reason_req_msg_ei;
  538. resp_desc.msg_id = QMI_SSCTL_GET_FAILURE_REASON_RESP_V02;
  539. resp_desc.max_msg_len = QMI_SSCTL_ERROR_MSG_LENGTH;
  540. resp_desc.ei_array = qmi_ssctl_get_failure_reason_resp_msg_ei;
  541. mutex_lock(&sysmon_lock);
  542. ret = qmi_send_req_wait(data->clnt_handle, &req_desc, &req,
  543. sizeof(req), &resp_desc, &resp, sizeof(resp), SERVER_TIMEOUT);
  544. if (ret < 0) {
  545. pr_err("QMI send req to %s failed, ret - %d\n", dest_ss, ret);
  546. goto out;
  547. }
  548. /* Check the response */
  549. if (QMI_RESP_BIT_SHIFT(resp.resp.result) != QMI_RESULT_SUCCESS_V01) {
  550. pr_err("QMI request failed 0x%x\n",
  551. QMI_RESP_BIT_SHIFT(resp.resp.error));
  552. ret = -EREMOTEIO;
  553. goto out;
  554. }
  555. if (!strcmp(resp.error_message, expect)) {
  556. pr_err("Unexpected response %s\n", resp.error_message);
  557. ret = -EPROTO;
  558. goto out;
  559. }
  560. strlcpy(buf, resp.error_message, resp.error_message_len);
  561. out:
  562. mutex_unlock(&sysmon_lock);
  563. return ret;
  564. }
  565. EXPORT_SYMBOL(sysmon_get_reason);
  566. /**
  567. * sysmon_notifier_register() - Initialize sysmon data for a subsystem.
  568. * @dest_desc: Subsystem descriptor of the subsystem
  569. *
  570. * Returns 0 for success. If the subsystem does not support SSCTL v2, a
  571. * value of 0 is returned after adding the subsystem entry to the sysmon_list.
  572. * In addition, if the SSCTL v2 support exists, the notifier block to receive
  573. * events from the SSCTL service on the subsystem is registered.
  574. *
  575. * If CONFIG_MSM_SYSMON_COMM is not defined, always return success (0).
  576. */
  577. int sysmon_notifier_register(struct subsys_desc *desc)
  578. {
  579. struct sysmon_qmi_data *data;
  580. int rc = 0;
  581. data = kmalloc(sizeof(*data), GFP_KERNEL);
  582. if (!data)
  583. return -ENOMEM;
  584. data->name = desc->name;
  585. data->instance_id = desc->ssctl_instance_id;
  586. data->clnt_handle = NULL;
  587. data->legacy_version = false;
  588. mutex_lock(&sysmon_list_lock);
  589. if (data->instance_id <= 0) {
  590. pr_debug("SSCTL instance id not defined\n");
  591. goto add_list;
  592. }
  593. if (sysmon_wq)
  594. goto notif_register;
  595. sysmon_wq = create_singlethread_workqueue("sysmon_wq");
  596. if (!sysmon_wq) {
  597. mutex_unlock(&sysmon_list_lock);
  598. pr_err("Could not create workqueue\n");
  599. kfree(data);
  600. return -ENOMEM;
  601. }
  602. notif_register:
  603. data->notifier.notifier_call = sysmon_svc_event_notify;
  604. init_completion(&data->ind_recv);
  605. INIT_WORK(&data->svc_arrive, sysmon_clnt_svc_arrive);
  606. INIT_WORK(&data->svc_exit, sysmon_clnt_svc_exit);
  607. INIT_WORK(&data->svc_rcv_msg, sysmon_clnt_recv_msg);
  608. rc = qmi_svc_event_notifier_register(SSCTL_SERVICE_ID, SSCTL_VER_2,
  609. data->instance_id, &data->notifier);
  610. if (rc < 0)
  611. pr_err("Notifier register failed for %s\n", data->name);
  612. add_list:
  613. INIT_LIST_HEAD(&data->list);
  614. list_add_tail(&data->list, &sysmon_list);
  615. mutex_unlock(&sysmon_list_lock);
  616. return rc;
  617. }
  618. EXPORT_SYMBOL(sysmon_notifier_register);
  619. /**
  620. * sysmon_notifier_unregister() - Cleanup the subsystem's sysmon data.
  621. * @dest_desc: Subsystem descriptor of the subsystem
  622. *
  623. * If the subsystem does not support SSCTL v2, its entry is simply removed from
  624. * the sysmon_list. In addition, if the SSCTL v2 support exists, the notifier
  625. * block to receive events from the SSCTL service is unregistered.
  626. */
  627. void sysmon_notifier_unregister(struct subsys_desc *desc)
  628. {
  629. struct sysmon_qmi_data *data = NULL, *sysmon_data, *tmp;
  630. mutex_lock(&sysmon_list_lock);
  631. list_for_each_entry_safe(sysmon_data, tmp, &sysmon_list, list)
  632. if (!strcmp(sysmon_data->name, desc->name)) {
  633. data = sysmon_data;
  634. list_del(&data->list);
  635. }
  636. if (data == NULL)
  637. goto exit;
  638. if (data->instance_id > 0)
  639. qmi_svc_event_notifier_unregister(SSCTL_SERVICE_ID,
  640. SSCTL_VER_2, data->instance_id, &data->notifier);
  641. if (sysmon_wq && list_empty(&sysmon_list))
  642. destroy_workqueue(sysmon_wq);
  643. exit:
  644. mutex_unlock(&sysmon_list_lock);
  645. kfree(data);
  646. }
  647. EXPORT_SYMBOL(sysmon_notifier_unregister);