service-locator.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. /*
  2. * Copyright (c) 2015-2018, 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) "servloc: %s: " fmt, __func__
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/mutex.h>
  18. #include <linux/string.h>
  19. #include <linux/completion.h>
  20. #include <linux/slab.h>
  21. #include <linux/of.h>
  22. #include <linux/device.h>
  23. #include <linux/delay.h>
  24. #include <linux/workqueue.h>
  25. #include <linux/debugfs.h>
  26. #include <soc/qcom/msm_qmi_interface.h>
  27. #include <soc/qcom/service-locator.h>
  28. #include "service-locator-private.h"
  29. #define SERVREG_LOC_SERVICE_INSTANCE_ID 1
  30. #define QMI_SERVREG_LOC_SERVER_INITIAL_TIMEOUT 2000
  31. #define QMI_SERVREG_LOC_SERVER_TIMEOUT 2000
  32. #define INITIAL_TIMEOUT 100000
  33. #define LOCATOR_SERVICE_TIMEOUT 300000
  34. #define LOCATOR_NOT_PRESENT 0
  35. #define LOCATOR_PRESENT 1
  36. static u32 locator_status = LOCATOR_NOT_PRESENT;
  37. static bool service_inited;
  38. module_param_named(enable, locator_status, uint, 0644);
  39. static void service_locator_svc_arrive(struct work_struct *work);
  40. static void service_locator_svc_exit(struct work_struct *work);
  41. static void service_locator_recv_msg(struct work_struct *work);
  42. static void pd_locator_work(struct work_struct *work);
  43. struct workqueue_struct *servloc_wq;
  44. struct pd_qmi_data {
  45. struct work_struct svc_arrive;
  46. struct work_struct svc_exit;
  47. struct work_struct svc_rcv_msg;
  48. struct notifier_block notifier;
  49. struct completion service_available;
  50. struct mutex service_mutex;
  51. struct qmi_handle *clnt_handle;
  52. };
  53. struct pd_qmi_work {
  54. struct work_struct pd_loc_work;
  55. struct pd_qmi_client_data *pdc;
  56. struct notifier_block *notifier;
  57. };
  58. DEFINE_MUTEX(service_init_mutex);
  59. struct pd_qmi_data service_locator;
  60. /* Please refer soc/qcom/service-locator.h for use about APIs defined here */
  61. static int service_locator_svc_event_notify(struct notifier_block *this,
  62. unsigned long code,
  63. void *_cmd)
  64. {
  65. switch (code) {
  66. case QMI_SERVER_ARRIVE:
  67. queue_work(servloc_wq, &service_locator.svc_arrive);
  68. break;
  69. case QMI_SERVER_EXIT:
  70. queue_work(servloc_wq, &service_locator.svc_exit);
  71. break;
  72. default:
  73. break;
  74. }
  75. return 0;
  76. }
  77. static void service_locator_clnt_notify(struct qmi_handle *handle,
  78. enum qmi_event_type event, void *notify_priv)
  79. {
  80. switch (event) {
  81. case QMI_RECV_MSG:
  82. schedule_work(&service_locator.svc_rcv_msg);
  83. break;
  84. default:
  85. break;
  86. }
  87. }
  88. static void service_locator_svc_arrive(struct work_struct *work)
  89. {
  90. int rc = 0;
  91. /* Create a Local client port for QMI communication */
  92. mutex_lock(&service_locator.service_mutex);
  93. service_locator.clnt_handle =
  94. qmi_handle_create(service_locator_clnt_notify, NULL);
  95. if (!service_locator.clnt_handle) {
  96. service_locator.clnt_handle = NULL;
  97. complete_all(&service_locator.service_available);
  98. mutex_unlock(&service_locator.service_mutex);
  99. pr_err("Service locator QMI client handle alloc failed!\n");
  100. return;
  101. }
  102. /* Connect to service */
  103. rc = qmi_connect_to_service(service_locator.clnt_handle,
  104. SERVREG_LOC_SERVICE_ID_V01, SERVREG_LOC_SERVICE_VERS_V01,
  105. SERVREG_LOC_SERVICE_INSTANCE_ID);
  106. if (rc) {
  107. qmi_handle_destroy(service_locator.clnt_handle);
  108. service_locator.clnt_handle = NULL;
  109. complete_all(&service_locator.service_available);
  110. mutex_unlock(&service_locator.service_mutex);
  111. pr_err("Unable to connnect to service rc:%d\n", rc);
  112. return;
  113. }
  114. if (!service_inited)
  115. complete_all(&service_locator.service_available);
  116. mutex_unlock(&service_locator.service_mutex);
  117. pr_info("Connection established with the Service locator\n");
  118. }
  119. static void service_locator_svc_exit(struct work_struct *work)
  120. {
  121. mutex_lock(&service_locator.service_mutex);
  122. qmi_handle_destroy(service_locator.clnt_handle);
  123. service_locator.clnt_handle = NULL;
  124. complete_all(&service_locator.service_available);
  125. mutex_unlock(&service_locator.service_mutex);
  126. pr_info("Connection with service locator lost\n");
  127. }
  128. static void service_locator_recv_msg(struct work_struct *work)
  129. {
  130. int ret;
  131. do {
  132. pr_debug("Notified about a Receive event\n");
  133. } while ((ret = qmi_recv_msg(service_locator.clnt_handle)) == 0);
  134. if (ret != -ENOMSG)
  135. pr_err("Error receiving message rc:%d\n", ret);
  136. }
  137. static void store_get_domain_list_response(struct pd_qmi_client_data *pd,
  138. struct qmi_servreg_loc_get_domain_list_resp_msg_v01 *resp,
  139. int offset)
  140. {
  141. int i;
  142. for (i = offset; i < resp->domain_list_len; i++) {
  143. pd->domain_list[i].instance_id =
  144. resp->domain_list[i].instance_id;
  145. strlcpy(pd->domain_list[i].name, resp->domain_list[i].name,
  146. QMI_SERVREG_LOC_NAME_LENGTH_V01 + 1);
  147. pd->domain_list[i].service_data_valid =
  148. resp->domain_list[i].service_data_valid;
  149. pd->domain_list[i].service_data =
  150. resp->domain_list[i].service_data;
  151. }
  152. }
  153. static int servreg_loc_send_msg(struct msg_desc *req_desc,
  154. struct msg_desc *resp_desc,
  155. struct qmi_servreg_loc_get_domain_list_req_msg_v01 *req,
  156. struct qmi_servreg_loc_get_domain_list_resp_msg_v01 *resp,
  157. struct pd_qmi_client_data *pd)
  158. {
  159. int rc;
  160. /*
  161. * Send msg and get response. There is a chance that the service went
  162. * away since the time we last checked for it to be available and
  163. * actually made this call. In that case the call just fails.
  164. */
  165. rc = qmi_send_req_wait(service_locator.clnt_handle, req_desc, req,
  166. sizeof(*req), resp_desc, resp, sizeof(*resp),
  167. QMI_SERVREG_LOC_SERVER_TIMEOUT);
  168. if (rc < 0) {
  169. pr_err("QMI send req failed for client %s, ret - %d\n",
  170. pd->client_name, rc);
  171. return rc;
  172. }
  173. /* Check the response */
  174. if (resp->resp.result != QMI_RESULT_SUCCESS_V01) {
  175. pr_err("QMI request for client %s failed 0x%x\n",
  176. pd->client_name, resp->resp.error);
  177. return -EREMOTEIO;
  178. }
  179. return rc;
  180. }
  181. static int service_locator_send_msg(struct pd_qmi_client_data *pd)
  182. {
  183. struct msg_desc req_desc, resp_desc;
  184. struct qmi_servreg_loc_get_domain_list_resp_msg_v01 *resp = NULL;
  185. struct qmi_servreg_loc_get_domain_list_req_msg_v01 *req = NULL;
  186. int rc;
  187. int db_rev_count = 0, domains_read = 0;
  188. if (!service_locator.clnt_handle) {
  189. pr_err("Service locator not available!\n");
  190. return -EAGAIN;
  191. }
  192. req = kzalloc(sizeof(
  193. struct qmi_servreg_loc_get_domain_list_req_msg_v01),
  194. GFP_KERNEL);
  195. if (!req) {
  196. pr_err("Unable to allocate memory for req message\n");
  197. rc = -ENOMEM;
  198. goto out;
  199. }
  200. resp = kzalloc(sizeof(
  201. struct qmi_servreg_loc_get_domain_list_resp_msg_v01),
  202. GFP_KERNEL);
  203. if (!resp) {
  204. pr_err("Unable to allocate memory for resp message\n");
  205. rc = -ENOMEM;
  206. goto out;
  207. }
  208. /* Prepare req and response message formats */
  209. req_desc.msg_id = QMI_SERVREG_LOC_GET_DOMAIN_LIST_REQ_V01;
  210. req_desc.max_msg_len =
  211. QMI_SERVREG_LOC_GET_DOMAIN_LIST_REQ_MSG_V01_MAX_MSG_LEN;
  212. req_desc.ei_array = qmi_servreg_loc_get_domain_list_req_msg_v01_ei;
  213. resp_desc.msg_id = QMI_SERVREG_LOC_GET_DOMAIN_LIST_RESP_V01;
  214. resp_desc.max_msg_len =
  215. QMI_SERVREG_LOC_GET_DOMAIN_LIST_RESP_MSG_V01_MAX_MSG_LEN;
  216. resp_desc.ei_array = qmi_servreg_loc_get_domain_list_resp_msg_v01_ei;
  217. /* Prepare req and response message */
  218. strlcpy(req->service_name, pd->service_name,
  219. QMI_SERVREG_LOC_NAME_LENGTH_V01 + 1);
  220. req->domain_offset_valid = true;
  221. req->domain_offset = 0;
  222. pd->domain_list = NULL;
  223. do {
  224. req->domain_offset += domains_read;
  225. rc = servreg_loc_send_msg(&req_desc, &resp_desc, req, resp,
  226. pd);
  227. if (rc < 0) {
  228. pr_err("send msg failed rc:%d\n", rc);
  229. goto out;
  230. }
  231. if (!domains_read) {
  232. db_rev_count = pd->db_rev_count = resp->db_rev_count;
  233. pd->total_domains = resp->total_domains;
  234. if (!resp->total_domains) {
  235. pr_err("No matching domains found\n");
  236. goto out;
  237. }
  238. pd->domain_list = kmalloc(
  239. sizeof(struct servreg_loc_entry_v01) *
  240. resp->total_domains, GFP_KERNEL);
  241. if (!pd->domain_list) {
  242. pr_err("Cannot allocate domain list\n");
  243. rc = -ENOMEM;
  244. goto out;
  245. }
  246. }
  247. if (db_rev_count != resp->db_rev_count) {
  248. pr_err("Service Locator DB updated for client %s\n",
  249. pd->client_name);
  250. kfree(pd->domain_list);
  251. rc = -EAGAIN;
  252. goto out;
  253. }
  254. if (resp->domain_list_len > resp->total_domains) {
  255. /* Always read total_domains from the response msg */
  256. resp->domain_list_len = resp->total_domains;
  257. }
  258. /* Copy the response*/
  259. store_get_domain_list_response(pd, resp, domains_read);
  260. domains_read += resp->domain_list_len;
  261. } while (domains_read < resp->total_domains);
  262. rc = 0;
  263. out:
  264. kfree(req);
  265. kfree(resp);
  266. return rc;
  267. }
  268. static int init_service_locator(void)
  269. {
  270. int rc = 0;
  271. static bool service_timedout;
  272. rc = mutex_lock_interruptible(&service_init_mutex);
  273. if (rc)
  274. return rc;
  275. if (locator_status == LOCATOR_NOT_PRESENT) {
  276. pr_err("Service Locator not enabled\n");
  277. rc = -ENODEV;
  278. goto inited;
  279. }
  280. if (service_timedout) {
  281. rc = -ETIME;
  282. goto inited;
  283. }
  284. if (service_inited)
  285. goto inited;
  286. service_locator.notifier.notifier_call =
  287. service_locator_svc_event_notify;
  288. init_completion(&service_locator.service_available);
  289. mutex_init(&service_locator.service_mutex);
  290. servloc_wq = create_singlethread_workqueue("servloc_wq");
  291. if (!servloc_wq) {
  292. rc = -ENOMEM;
  293. pr_err("Could not create workqueue\n");
  294. goto inited;
  295. }
  296. INIT_WORK(&service_locator.svc_arrive, service_locator_svc_arrive);
  297. INIT_WORK(&service_locator.svc_exit, service_locator_svc_exit);
  298. INIT_WORK(&service_locator.svc_rcv_msg, service_locator_recv_msg);
  299. rc = qmi_svc_event_notifier_register(SERVREG_LOC_SERVICE_ID_V01,
  300. SERVREG_LOC_SERVICE_VERS_V01, SERVREG_LOC_SERVICE_INSTANCE_ID,
  301. &service_locator.notifier);
  302. if (rc < 0) {
  303. pr_err("Notifier register failed rc:%d\n", rc);
  304. goto inited;
  305. }
  306. rc = wait_for_completion_interruptible_timeout(
  307. &service_locator.service_available,
  308. msecs_to_jiffies(LOCATOR_SERVICE_TIMEOUT));
  309. if (rc < 0) {
  310. pr_err("Wait for locator service interrupted by signal\n");
  311. goto inited;
  312. }
  313. if (!rc) {
  314. pr_err("%s: wait for locator service timed out\n", __func__);
  315. service_timedout = true;
  316. rc = -ETIME;
  317. goto inited;
  318. }
  319. service_inited = true;
  320. mutex_unlock(&service_init_mutex);
  321. pr_info("Service locator initialized\n");
  322. return 0;
  323. inited:
  324. mutex_unlock(&service_init_mutex);
  325. return rc;
  326. }
  327. int get_service_location(char *client_name, char *service_name,
  328. struct notifier_block *locator_nb)
  329. {
  330. struct pd_qmi_client_data *pqcd;
  331. struct pd_qmi_work *pqw;
  332. int rc = 0;
  333. if (!locator_nb || !client_name || !service_name) {
  334. rc = -EINVAL;
  335. pr_err("Invalid input!\n");
  336. goto err;
  337. }
  338. pqcd = kmalloc(sizeof(struct pd_qmi_client_data), GFP_KERNEL);
  339. if (!pqcd) {
  340. rc = -ENOMEM;
  341. pr_err("Allocation failed\n");
  342. goto err;
  343. }
  344. strlcpy(pqcd->client_name, client_name, ARRAY_SIZE(pqcd->client_name));
  345. strlcpy(pqcd->service_name, service_name,
  346. ARRAY_SIZE(pqcd->service_name));
  347. pqw = kmalloc(sizeof(struct pd_qmi_work), GFP_KERNEL);
  348. if (!pqw) {
  349. rc = -ENOMEM;
  350. pr_err("Allocation failed\n");
  351. kfree(pqcd);
  352. goto err;
  353. }
  354. pqw->notifier = locator_nb;
  355. pqw->pdc = pqcd;
  356. INIT_WORK(&pqw->pd_loc_work, pd_locator_work);
  357. schedule_work(&pqw->pd_loc_work);
  358. err:
  359. return rc;
  360. }
  361. EXPORT_SYMBOL(get_service_location);
  362. static void pd_locator_work(struct work_struct *work)
  363. {
  364. int rc = 0;
  365. struct pd_qmi_client_data *data;
  366. struct pd_qmi_work *pdqw = container_of(work, struct pd_qmi_work,
  367. pd_loc_work);
  368. data = pdqw->pdc;
  369. rc = init_service_locator();
  370. if (rc) {
  371. pr_err("Unable to connect to service locator!, rc = %d\n", rc);
  372. pdqw->notifier->notifier_call(pdqw->notifier,
  373. LOCATOR_DOWN, NULL);
  374. goto err;
  375. }
  376. rc = service_locator_send_msg(data);
  377. if (rc) {
  378. pr_err("Failed to get process domains for %s for client %s rc:%d\n",
  379. data->service_name, data->client_name, rc);
  380. pdqw->notifier->notifier_call(pdqw->notifier,
  381. LOCATOR_DOWN, NULL);
  382. goto err;
  383. }
  384. pdqw->notifier->notifier_call(pdqw->notifier, LOCATOR_UP, data);
  385. err:
  386. kfree(data);
  387. kfree(pdqw);
  388. }
  389. int find_subsys(const char *pd_path, char *subsys)
  390. {
  391. char *start, *end;
  392. if (!subsys || !pd_path)
  393. return -EINVAL;
  394. start = strnstr(pd_path, "/", QMI_SERVREG_LOC_NAME_LENGTH_V01);
  395. if (!start)
  396. return -EINVAL;
  397. start++;
  398. end = strnstr(start, "/", QMI_SERVREG_LOC_NAME_LENGTH_V01);
  399. if (!end || start == end)
  400. return -EINVAL;
  401. strlcpy(subsys, start, end - start + 1);
  402. return 0;
  403. }
  404. EXPORT_SYMBOL(find_subsys);
  405. static struct pd_qmi_client_data test_data;
  406. static int servloc_test_pdr_cb(struct notifier_block *this,
  407. unsigned long opcode, void *ptr)
  408. {
  409. int i, rc = 0;
  410. char subsys[QMI_SERVREG_LOC_NAME_LENGTH_V01];
  411. struct pd_qmi_client_data *return_data;
  412. return_data = (struct pd_qmi_client_data *)ptr;
  413. if (opcode) {
  414. pr_err("%s: Failed to get process domain!, opcode = %lu\n",
  415. __func__, opcode);
  416. return -EIO;
  417. }
  418. pr_err("Service Name: %s\tTotal Domains: %d\n",
  419. return_data->service_name, return_data->total_domains);
  420. for (i = 0; i < return_data->total_domains; i++) {
  421. pr_err("Instance ID: %d\t ",
  422. return_data->domain_list[i].instance_id);
  423. pr_err("Domain Name: %s\n",
  424. return_data->domain_list[i].name);
  425. rc = find_subsys(return_data->domain_list[i].name,
  426. subsys);
  427. if (rc < 0)
  428. pr_err("No valid subsys found for %s!\n",
  429. return_data->domain_list[i].name);
  430. else
  431. pr_err("Subsys: %s\n", subsys);
  432. }
  433. return 0;
  434. }
  435. static struct notifier_block pdr_service_nb = {
  436. .notifier_call = servloc_test_pdr_cb,
  437. };
  438. static ssize_t servloc_read(struct file *filp, char __user *ubuf,
  439. size_t cnt, loff_t *ppos)
  440. {
  441. int rc = 0;
  442. char *node_name = filp->private_data;
  443. if (!strcmp(node_name, "test_servloc_get"))
  444. rc = get_service_location(test_data.client_name,
  445. test_data.service_name, &pdr_service_nb);
  446. return rc;
  447. }
  448. static ssize_t servloc_write(struct file *fp, const char __user *buf,
  449. size_t count, loff_t *unused)
  450. {
  451. char *node_name = fp->private_data;
  452. if (!buf)
  453. return -EIO;
  454. if (!strcmp(node_name, "service_name")) {
  455. snprintf(test_data.service_name, sizeof(test_data.service_name),
  456. "%.*s", (int) min((size_t)count - 1,
  457. (sizeof(test_data.service_name) - 1)), buf);
  458. } else {
  459. snprintf(test_data.client_name, sizeof(test_data.client_name),
  460. "%.*s", (int) min((size_t)count - 1,
  461. (sizeof(test_data.client_name) - 1)), buf);
  462. }
  463. return count;
  464. }
  465. static const struct file_operations servloc_fops = {
  466. .open = simple_open,
  467. .read = servloc_read,
  468. .write = servloc_write,
  469. };
  470. static struct dentry *servloc_base_dir;
  471. static struct dentry *test_servloc_file;
  472. static int __init servloc_debugfs_init(void)
  473. {
  474. servloc_base_dir = debugfs_create_dir("test_servloc", NULL);
  475. return !servloc_base_dir ? -ENOMEM : 0;
  476. }
  477. static void servloc_debugfs_exit(void)
  478. {
  479. debugfs_remove_recursive(servloc_base_dir);
  480. }
  481. static int servloc_debugfs_add(void)
  482. {
  483. int rc;
  484. if (!servloc_base_dir)
  485. return -ENOMEM;
  486. test_servloc_file = debugfs_create_file("client_name",
  487. 0644, servloc_base_dir,
  488. "client_name", &servloc_fops);
  489. rc = !test_servloc_file ? -ENOMEM : 0;
  490. if (rc == 0) {
  491. test_servloc_file = debugfs_create_file("service_name",
  492. 0644, servloc_base_dir,
  493. "service_name", &servloc_fops);
  494. rc = !test_servloc_file ? -ENOMEM : 0;
  495. }
  496. if (rc == 0) {
  497. test_servloc_file = debugfs_create_file("test_servloc_get",
  498. 0644, servloc_base_dir,
  499. "test_servloc_get", &servloc_fops);
  500. rc = !test_servloc_file ? -ENOMEM : 0;
  501. }
  502. return rc;
  503. }
  504. static int __init service_locator_init(void)
  505. {
  506. pr_debug("service_locator_status = %d\n", locator_status);
  507. if (servloc_debugfs_init())
  508. pr_err("Could not create test_servloc base directory!");
  509. if (servloc_debugfs_add())
  510. pr_err("Could not create test_servloc node entries!");
  511. return 0;
  512. }
  513. static void __exit service_locator_exit(void)
  514. {
  515. servloc_debugfs_exit();
  516. }
  517. module_init(service_locator_init);
  518. module_exit(service_locator_exit);