glink_debugfs.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. /* Copyright (c) 2014-2016, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/debugfs.h>
  13. #include <linux/err.h>
  14. #include <linux/ipc_logging.h>
  15. #include <linux/list.h>
  16. #include <linux/slab.h>
  17. #include <soc/qcom/glink.h>
  18. #include "glink_private.h"
  19. #include "glink_core_if.h"
  20. static const char * const ss_string[] = {
  21. [GLINK_DBGFS_MPSS] = "mpss",
  22. [GLINK_DBGFS_APSS] = "apss",
  23. [GLINK_DBGFS_LPASS] = "lpass",
  24. [GLINK_DBGFS_DSPS] = "dsps",
  25. [GLINK_DBGFS_RPM] = "rpm",
  26. [GLINK_DBGFS_WCNSS] = "wcnss",
  27. [GLINK_DBGFS_LLOOP] = "lloop",
  28. [GLINK_DBGFS_MOCK] = "mock"
  29. };
  30. static const char * const xprt_string[] = {
  31. [GLINK_DBGFS_SMEM] = "smem",
  32. [GLINK_DBGFS_SMD] = "smd",
  33. [GLINK_DBGFS_XLLOOP] = "lloop",
  34. [GLINK_DBGFS_XMOCK] = "mock",
  35. [GLINK_DBGFS_XMOCK_LOW] = "mock_low",
  36. [GLINK_DBGFS_XMOCK_HIGH] = "mock_high"
  37. };
  38. static const char * const ch_st_string[] = {
  39. [GLINK_CHANNEL_CLOSED] = "CLOSED",
  40. [GLINK_CHANNEL_OPENING] = "OPENING",
  41. [GLINK_CHANNEL_OPENED] = "OPENED",
  42. [GLINK_CHANNEL_CLOSING] = "CLOSING",
  43. };
  44. static const char * const xprt_st_string[] = {
  45. [GLINK_XPRT_DOWN] = "DOWN",
  46. [GLINK_XPRT_NEGOTIATING] = "NEGOT",
  47. [GLINK_XPRT_OPENED] = "OPENED",
  48. [GLINK_XPRT_FAILED] = "FAILED"
  49. };
  50. #if defined(CONFIG_DEBUG_FS)
  51. #define GLINK_DBGFS_NAME_SIZE (2 * GLINK_NAME_SIZE + 1)
  52. struct glink_dbgfs_dent {
  53. struct list_head list_node;
  54. char par_name[GLINK_DBGFS_NAME_SIZE];
  55. char self_name[GLINK_DBGFS_NAME_SIZE];
  56. struct dentry *parent;
  57. struct dentry *self;
  58. spinlock_t file_list_lock_lhb0;
  59. struct list_head file_list;
  60. bool rm_debugfs;
  61. struct work_struct rm_work;
  62. };
  63. static struct dentry *dent;
  64. static LIST_HEAD(dent_list);
  65. static DEFINE_MUTEX(dent_list_lock_lha0);
  66. static int debugfs_show(struct seq_file *s, void *data)
  67. {
  68. struct glink_dbgfs_data *dfs_d;
  69. dfs_d = s->private;
  70. dfs_d->o_func(s);
  71. return 0;
  72. }
  73. static int debug_open(struct inode *inode, struct file *file)
  74. {
  75. return single_open(file, debugfs_show, inode->i_private);
  76. }
  77. static const struct file_operations debug_ops = {
  78. .open = debug_open,
  79. .release = single_release,
  80. .read = seq_read,
  81. .llseek = seq_lseek,
  82. };
  83. #endif
  84. static void glink_dfs_dent_rm_worker(struct work_struct *work);
  85. /**
  86. * glink_get_ss_enum_string() - get the name of the subsystem based on enum
  87. * value
  88. * @enum_id: enum id of a specific subsystem.
  89. *
  90. * Return: name of the subsystem, NULL in case of invalid input
  91. */
  92. const char *glink_get_ss_enum_string(unsigned int enum_id)
  93. {
  94. if (enum_id >= ARRAY_SIZE(ss_string))
  95. return NULL;
  96. return ss_string[enum_id];
  97. }
  98. EXPORT_SYMBOL(glink_get_ss_enum_string);
  99. /**
  100. * glink_get_xprt_enum_string() - get the name of the transport based on enum
  101. * value
  102. * @enum_id: enum id of a specific transport.
  103. *
  104. * Return: name of the transport, NULL in case of invalid input
  105. */
  106. const char *glink_get_xprt_enum_string(unsigned int enum_id)
  107. {
  108. if (enum_id >= ARRAY_SIZE(xprt_string))
  109. return NULL;
  110. return xprt_string[enum_id];
  111. }
  112. EXPORT_SYMBOL(glink_get_xprt_enum_string);
  113. /**
  114. * glink_get_xprt_state_string() - get the name of the transport based on enum
  115. * value
  116. * @enum_id: enum id of the state of the transport.
  117. *
  118. * Return: name of the transport state, NULL in case of invalid input
  119. */
  120. const char *glink_get_xprt_state_string(
  121. enum transport_state_e enum_id)
  122. {
  123. if (enum_id >= ARRAY_SIZE(xprt_st_string))
  124. return NULL;
  125. return xprt_st_string[enum_id];
  126. }
  127. EXPORT_SYMBOL(glink_get_xprt_state_string);
  128. /**
  129. * glink_get_ch_state_string() - get the name of the transport based on enum
  130. * value
  131. * @enum_id: enum id of a specific state of the channel.
  132. *
  133. * Return: name of the channel state, NULL in case of invalid input
  134. */
  135. const char *glink_get_ch_state_string(
  136. enum local_channel_state_e enum_id)
  137. {
  138. if (enum_id >= ARRAY_SIZE(ch_st_string))
  139. return NULL;
  140. return ch_st_string[enum_id];
  141. }
  142. EXPORT_SYMBOL(glink_get_ch_state_string);
  143. #if defined(CONFIG_DEBUG_FS)
  144. /**
  145. * glink_dfs_create_file() - create the debugfs file
  146. * @name: debugfs file name
  147. * @parent: pointer to the parent dentry structure
  148. * @show: pointer to the actual function which will be invoked upon
  149. * opening this file.
  150. *
  151. * Return: pointer to the allocated glink_dbgfs_data structure or
  152. * NULL in case of an error.
  153. *
  154. * This function actually create a debugfs file under the parent directory
  155. */
  156. static struct glink_dbgfs_data *glink_dfs_create_file(const char *name,
  157. struct dentry *parent, void (*show)(struct seq_file *s),
  158. void *dbgfs_data, bool b_free_req)
  159. {
  160. struct dentry *file;
  161. struct glink_dbgfs_data *dfs_d;
  162. dfs_d = kzalloc(sizeof(struct glink_dbgfs_data), GFP_KERNEL);
  163. if (dfs_d == NULL)
  164. return NULL;
  165. dfs_d->o_func = show;
  166. if (dbgfs_data != NULL) {
  167. dfs_d->priv_data = dbgfs_data;
  168. dfs_d->b_priv_free_req = b_free_req;
  169. }
  170. file = debugfs_create_file(name, 0400, parent, dfs_d, &debug_ops);
  171. if (!file)
  172. GLINK_DBG("%s: unable to create file '%s'\n", __func__,
  173. name);
  174. dfs_d->dent = file;
  175. return dfs_d;
  176. }
  177. /**
  178. * write_ch_intent() - write channel intent details
  179. * @s: pointer to the sequential file
  180. * @intent: pointer glink core intent structure
  181. * @i_type: type of intent
  182. * @count: serial number of the intent.
  183. *
  184. * This function is a helper function of glink_dfs_update_ch_intents()
  185. * that prints out details of any specific intent.
  186. */
  187. static void write_ch_intent(struct seq_file *s,
  188. struct glink_core_rx_intent *intent,
  189. char *i_type, unsigned int count)
  190. {
  191. char *intent_type;
  192. /*
  193. * formatted, human readable channel state output, ie:
  194. * TYPE |SN |ID |PKT_SIZE|W_OFFSET|INT_SIZE|
  195. * --------------------------------------------------------------
  196. * LOCAL_LIST|#2 |1 |0 |0 |8 |
  197. */
  198. if (count == 1) {
  199. intent_type = i_type;
  200. seq_puts(s,
  201. "\n--------------------------------------------------------\n");
  202. } else {
  203. intent_type = "";
  204. }
  205. seq_printf(s, "%-20s|#%-5d|%-6u|%-10zu|%-10zu|%-10zu|\n",
  206. intent_type,
  207. count,
  208. intent->id,
  209. intent->pkt_size,
  210. intent->write_offset,
  211. intent->intent_size);
  212. }
  213. /**
  214. * glink_dfs_update_ch_intent() - writes the intent details of a specific
  215. * channel to the corresponding debugfs file
  216. * @s: pointer to the sequential file
  217. *
  218. * This function extracts the intent details of a channel & prints them to
  219. * corrseponding debugfs file of that channel.
  220. */
  221. static void glink_dfs_update_ch_intent(struct seq_file *s)
  222. {
  223. struct glink_dbgfs_data *dfs_d;
  224. struct channel_ctx *ch_ctx;
  225. struct glink_core_rx_intent *intent;
  226. struct glink_core_rx_intent *intent_temp;
  227. struct glink_ch_intent_info ch_intent_info;
  228. unsigned long flags;
  229. unsigned int count = 0;
  230. dfs_d = s->private;
  231. ch_ctx = dfs_d->priv_data;
  232. if (ch_ctx != NULL) {
  233. glink_get_ch_intent_info(ch_ctx, &ch_intent_info);
  234. seq_puts(s,
  235. "---------------------------------------------------------------\n");
  236. seq_printf(s, "%-20s|%-6s|%-6s|%-10s|%-10s|%-10s|\n",
  237. "INTENT TYPE",
  238. "SN",
  239. "ID",
  240. "PKT_SIZE",
  241. "W_OFFSET",
  242. "INT_SIZE");
  243. seq_puts(s,
  244. "---------------------------------------------------------------\n");
  245. spin_lock_irqsave(ch_intent_info.li_lst_lock, flags);
  246. list_for_each_entry_safe(intent, intent_temp,
  247. ch_intent_info.li_avail_list, list) {
  248. count++;
  249. write_ch_intent(s, intent, "LOCAL_AVAIL_LIST", count);
  250. }
  251. count = 0;
  252. list_for_each_entry_safe(intent, intent_temp,
  253. ch_intent_info.li_used_list, list) {
  254. count++;
  255. write_ch_intent(s, intent, "LOCAL_USED_LIST", count);
  256. }
  257. spin_unlock_irqrestore(ch_intent_info.li_lst_lock, flags);
  258. count = 0;
  259. spin_lock_irqsave(ch_intent_info.ri_lst_lock, flags);
  260. list_for_each_entry_safe(intent, intent_temp,
  261. ch_intent_info.ri_list, list) {
  262. count++;
  263. write_ch_intent(s, intent, "REMOTE_LIST", count);
  264. }
  265. spin_unlock_irqrestore(ch_intent_info.ri_lst_lock,
  266. flags);
  267. seq_puts(s,
  268. "---------------------------------------------------------------\n");
  269. }
  270. }
  271. /**
  272. * glink_dfs_update_ch_stats() - writes statistics of a specific
  273. * channel to the corresponding debugfs file
  274. * @s: pointer to the sequential file
  275. *
  276. * This function extracts other statistics of a channel & prints them to
  277. * corrseponding debugfs file of that channel
  278. */
  279. static void glink_dfs_update_ch_stats(struct seq_file *s)
  280. {
  281. /* FUTURE: add channel statistics */
  282. seq_puts(s, "not yet implemented\n");
  283. }
  284. /**
  285. * glink_debugfs_remove_channel() - remove all channel specific files & folder
  286. * in debugfs when channel is fully closed
  287. * @ch_ctx: pointer to the channel_contenxt
  288. * @xprt_ctx: pointer to the transport_context
  289. *
  290. * This function is invoked when any channel is fully closed. It removes the
  291. * folders & other files in debugfs for that channel.
  292. */
  293. void glink_debugfs_remove_channel(struct channel_ctx *ch_ctx,
  294. struct glink_core_xprt_ctx *xprt_ctx){
  295. struct glink_dbgfs ch_rm_dbgfs;
  296. char *edge_name;
  297. char curr_dir_name[GLINK_DBGFS_NAME_SIZE];
  298. char *xprt_name;
  299. ch_rm_dbgfs.curr_name = glink_get_ch_name(ch_ctx);
  300. edge_name = glink_get_xprt_edge_name(xprt_ctx);
  301. xprt_name = glink_get_xprt_name(xprt_ctx);
  302. if (!xprt_name || !edge_name) {
  303. GLINK_ERR("%s: Invalid xprt_name or edge_name for ch '%s'\n",
  304. __func__, ch_rm_dbgfs.curr_name);
  305. return;
  306. }
  307. snprintf(curr_dir_name, sizeof(curr_dir_name), "%s_%s",
  308. edge_name, xprt_name);
  309. ch_rm_dbgfs.par_name = curr_dir_name;
  310. glink_debugfs_remove_recur(&ch_rm_dbgfs);
  311. }
  312. EXPORT_SYMBOL(glink_debugfs_remove_channel);
  313. /**
  314. * glink_debugfs_add_channel() - create channel specific files & folder in
  315. * debugfs when channel is added
  316. * @ch_ctx: pointer to the channel_contenxt
  317. * @xprt_ctx: pointer to the transport_context
  318. *
  319. * This function is invoked when a new channel is created. It creates the
  320. * folders & other files in debugfs for that channel
  321. */
  322. void glink_debugfs_add_channel(struct channel_ctx *ch_ctx,
  323. struct glink_core_xprt_ctx *xprt_ctx)
  324. {
  325. struct glink_dbgfs ch_dbgfs;
  326. char *ch_name;
  327. char *edge_name;
  328. char *xprt_name;
  329. char curr_dir_name[GLINK_DBGFS_NAME_SIZE];
  330. if (ch_ctx == NULL) {
  331. GLINK_ERR("%s: Channel Context is NULL\n", __func__);
  332. return;
  333. }
  334. ch_name = glink_get_ch_name(ch_ctx);
  335. edge_name = glink_get_xprt_edge_name(xprt_ctx);
  336. xprt_name = glink_get_xprt_name(xprt_ctx);
  337. if (!xprt_name || !edge_name) {
  338. GLINK_ERR("%s: Invalid xprt_name or edge_name for ch '%s'\n",
  339. __func__, ch_name);
  340. return;
  341. }
  342. snprintf(curr_dir_name, sizeof(curr_dir_name), "%s_%s",
  343. edge_name, xprt_name);
  344. ch_dbgfs.curr_name = curr_dir_name;
  345. ch_dbgfs.par_name = "channel";
  346. ch_dbgfs.b_dir_create = true;
  347. glink_debugfs_create(ch_name, NULL, &ch_dbgfs, NULL, false);
  348. ch_dbgfs.par_name = ch_dbgfs.curr_name;
  349. ch_dbgfs.curr_name = ch_name;
  350. ch_dbgfs.b_dir_create = false;
  351. glink_debugfs_create("stats", glink_dfs_update_ch_stats,
  352. &ch_dbgfs, (void *)ch_ctx, false);
  353. glink_debugfs_create("intents", glink_dfs_update_ch_intent,
  354. &ch_dbgfs, (void *)ch_ctx, false);
  355. }
  356. EXPORT_SYMBOL(glink_debugfs_add_channel);
  357. /**
  358. * glink_debugfs_add_xprt() - create transport specific files & folder in
  359. * debugfs when new transport is registered
  360. * @xprt_ctx: pointer to the transport_context
  361. *
  362. * This function is invoked when a new transport is registered. It creates the
  363. * folders & other files in debugfs for that transport
  364. */
  365. void glink_debugfs_add_xprt(struct glink_core_xprt_ctx *xprt_ctx)
  366. {
  367. struct glink_dbgfs xprt_dbgfs;
  368. char *xprt_name;
  369. char *edge_name;
  370. char curr_dir_name[GLINK_DBGFS_NAME_SIZE];
  371. if (xprt_ctx == NULL)
  372. GLINK_ERR("%s: Transport Context is NULL\n", __func__);
  373. xprt_name = glink_get_xprt_name(xprt_ctx);
  374. edge_name = glink_get_xprt_edge_name(xprt_ctx);
  375. if (!xprt_name || !edge_name) {
  376. GLINK_ERR("%s: xprt name or edge name is NULL\n", __func__);
  377. return;
  378. }
  379. snprintf(curr_dir_name, sizeof(curr_dir_name), "%s_%s",
  380. edge_name, xprt_name);
  381. xprt_dbgfs.par_name = "glink";
  382. xprt_dbgfs.curr_name = "xprt";
  383. xprt_dbgfs.b_dir_create = true;
  384. glink_debugfs_create(curr_dir_name, NULL, &xprt_dbgfs, NULL, false);
  385. xprt_dbgfs.curr_name = "channel";
  386. glink_debugfs_create(curr_dir_name, NULL, &xprt_dbgfs, NULL, false);
  387. }
  388. EXPORT_SYMBOL(glink_debugfs_add_xprt);
  389. /**
  390. * glink_dfs_create_channel_list() - create & update the channel details
  391. * s: pointer to seq_file
  392. *
  393. * This function updates channel details in debugfs
  394. * file present in /glink/channel/channels
  395. */
  396. static void glink_dfs_create_channel_list(struct seq_file *s)
  397. {
  398. struct xprt_ctx_iterator xprt_iter;
  399. struct ch_ctx_iterator ch_iter;
  400. struct glink_core_xprt_ctx *xprt_ctx;
  401. struct channel_ctx *ch_ctx;
  402. int count = 0;
  403. /*
  404. * formatted, human readable channel state output, ie:
  405. * NAME |LCID|RCID|XPRT|EDGE|LSTATE |RSTATE|LINT-Q|RINT-Q|
  406. * --------------------------------------------------------------------
  407. * LOCAL_LOOPBACK_CLNT|2 |1 |lloop|local|OPENED|OPENED|5 |6 |
  408. * N.B. Number of TX & RX Packets not implemented yet. -ENOSYS is printed
  409. */
  410. seq_printf(s, "%-20s|%-4s|%-4s|%-10s|%-6s|%-7s|%-7s|%-5s|%-5s|\n",
  411. "NAME",
  412. "LCID",
  413. "RCID",
  414. "XPRT",
  415. "EDGE",
  416. "LSTATE",
  417. "RSTATE",
  418. "LINTQ",
  419. "RINTQ");
  420. seq_puts(s,
  421. "-------------------------------------------------------------------------------\n");
  422. glink_xprt_ctx_iterator_init(&xprt_iter);
  423. xprt_ctx = glink_xprt_ctx_iterator_next(&xprt_iter);
  424. while (xprt_ctx != NULL) {
  425. glink_ch_ctx_iterator_init(&ch_iter, xprt_ctx);
  426. ch_ctx = glink_ch_ctx_iterator_next(&ch_iter);
  427. while (ch_ctx != NULL) {
  428. count++;
  429. seq_printf(s, "%-20s|%-4i|%-4i|%-10s|%-6s|%-7s|",
  430. glink_get_ch_name(ch_ctx),
  431. glink_get_ch_lcid(ch_ctx),
  432. glink_get_ch_rcid(ch_ctx),
  433. glink_get_ch_xprt_name(ch_ctx),
  434. glink_get_ch_edge_name(ch_ctx),
  435. glink_get_ch_lstate(ch_ctx));
  436. seq_printf(s, "%-7s|%-5i|%-5i|\n",
  437. (glink_get_ch_rstate(ch_ctx) ? "OPENED" : "CLOSED"),
  438. glink_get_ch_lintents_queued(ch_ctx),
  439. glink_get_ch_rintents_queued(ch_ctx));
  440. ch_ctx = glink_ch_ctx_iterator_next(&ch_iter);
  441. }
  442. glink_ch_ctx_iterator_end(&ch_iter, xprt_ctx);
  443. xprt_ctx = glink_xprt_ctx_iterator_next(&xprt_iter);
  444. }
  445. glink_xprt_ctx_iterator_end(&xprt_iter);
  446. }
  447. /**
  448. * glink_dfs_create_xprt_list() - create & update the transport details
  449. * @s: pointer to seq_file
  450. *
  451. * This function updates channel details in debugfs file present
  452. * in /glink/xprt/xprts
  453. */
  454. static void glink_dfs_create_xprt_list(struct seq_file *s)
  455. {
  456. struct xprt_ctx_iterator xprt_iter;
  457. struct glink_core_xprt_ctx *xprt_ctx;
  458. const struct glink_core_version *gver;
  459. uint32_t version;
  460. uint32_t features;
  461. int count = 0;
  462. /*
  463. * formatted, human readable channel state output, ie:
  464. * XPRT_NAME|REMOTE |STATE|VERSION |FEATURES|
  465. * ---------------------------------------------
  466. * smd_trans|lpass |2 |0 |1 |
  467. * smem |mpss |0 |0 |0 |
  468. */
  469. seq_printf(s, "%-20s|%-20s|%-6s|%-8s|%-8s|\n",
  470. "XPRT_NAME",
  471. "REMOTE",
  472. "STATE",
  473. "VERSION",
  474. "FEATURES");
  475. seq_puts(s,
  476. "-------------------------------------------------------------------------------\n");
  477. glink_xprt_ctx_iterator_init(&xprt_iter);
  478. xprt_ctx = glink_xprt_ctx_iterator_next(&xprt_iter);
  479. while (xprt_ctx != NULL) {
  480. count++;
  481. seq_printf(s, "%-20s|%-20s|",
  482. glink_get_xprt_name(xprt_ctx),
  483. glink_get_xprt_edge_name(xprt_ctx));
  484. gver = glink_get_xprt_version_features(xprt_ctx);
  485. if (gver != NULL) {
  486. version = gver->version;
  487. features = gver->features;
  488. seq_printf(s, "%-6s|%-8i|%-8i|\n",
  489. glink_get_xprt_state(xprt_ctx),
  490. version,
  491. features);
  492. } else {
  493. seq_printf(s, "%-6s|%-8i|%-8i|\n",
  494. glink_get_xprt_state(xprt_ctx),
  495. -ENODATA,
  496. -ENODATA);
  497. }
  498. xprt_ctx = glink_xprt_ctx_iterator_next(&xprt_iter);
  499. }
  500. glink_xprt_ctx_iterator_end(&xprt_iter);
  501. }
  502. /**
  503. * glink_dfs_update_list() - update the internally maintained dentry linked list
  504. * @curr_dent: pointer to the current dentry object
  505. * @parent: pointer to the parent dentry object
  506. * @curr: current directory name
  507. * @par_dir: parent directory name
  508. */
  509. void glink_dfs_update_list(struct dentry *curr_dent, struct dentry *parent,
  510. const char *curr, const char *par_dir)
  511. {
  512. struct glink_dbgfs_dent *dbgfs_dent_s;
  513. if (curr_dent != NULL) {
  514. dbgfs_dent_s = kzalloc(sizeof(struct glink_dbgfs_dent),
  515. GFP_KERNEL);
  516. if (dbgfs_dent_s != NULL) {
  517. INIT_LIST_HEAD(&dbgfs_dent_s->file_list);
  518. spin_lock_init(&dbgfs_dent_s->file_list_lock_lhb0);
  519. dbgfs_dent_s->parent = parent;
  520. dbgfs_dent_s->self = curr_dent;
  521. strlcpy(dbgfs_dent_s->self_name,
  522. curr, strlen(curr) + 1);
  523. strlcpy(dbgfs_dent_s->par_name, par_dir,
  524. strlen(par_dir) + 1);
  525. INIT_WORK(&dbgfs_dent_s->rm_work,
  526. glink_dfs_dent_rm_worker);
  527. mutex_lock(&dent_list_lock_lha0);
  528. list_add_tail(&dbgfs_dent_s->list_node, &dent_list);
  529. mutex_unlock(&dent_list_lock_lha0);
  530. }
  531. } else {
  532. GLINK_DBG("%s:create directory failed for par:curr [%s:%s]\n",
  533. __func__, par_dir, curr);
  534. }
  535. }
  536. /**
  537. * glink_remove_dfs_entry() - remove the the entries from dent_list
  538. * @entry: pointer to the glink_dbgfs_dent structure
  539. *
  540. * This function removes the removes the entries from internally maintained
  541. * linked list of dentries. It also deletes the file list and associated memory
  542. * if present.
  543. */
  544. void glink_remove_dfs_entry(struct glink_dbgfs_dent *entry)
  545. {
  546. struct glink_dbgfs_data *fentry, *fentry_temp;
  547. unsigned long flags;
  548. if (entry == NULL)
  549. return;
  550. if (!list_empty(&entry->file_list)) {
  551. spin_lock_irqsave(&entry->file_list_lock_lhb0, flags);
  552. list_for_each_entry_safe(fentry, fentry_temp,
  553. &entry->file_list, flist) {
  554. if (fentry->b_priv_free_req)
  555. kfree(fentry->priv_data);
  556. list_del(&fentry->flist);
  557. kfree(fentry);
  558. fentry = NULL;
  559. }
  560. spin_unlock_irqrestore(&entry->file_list_lock_lhb0, flags);
  561. }
  562. list_del(&entry->list_node);
  563. schedule_work(&entry->rm_work);
  564. }
  565. /**
  566. * glink_dfs_dent_rm_worker() - Remove the debugfs entry recursively
  567. * @work: Deferred work whose entry needs to be removed.
  568. */
  569. static void glink_dfs_dent_rm_worker(struct work_struct *work)
  570. {
  571. struct glink_dbgfs_dent *entry =
  572. container_of(work, struct glink_dbgfs_dent, rm_work);
  573. if (entry->rm_debugfs)
  574. debugfs_remove_recursive(entry->self);
  575. kfree(entry);
  576. }
  577. /**
  578. * glink_debugfs_remove_recur() - remove the the directory & files recursively
  579. * @rm_dfs: pointer to the structure glink_dbgfs
  580. *
  581. * This function removes the files & directories below the given directory.
  582. * This also takes care of freeing any memory associated with the debugfs file.
  583. */
  584. void glink_debugfs_remove_recur(struct glink_dbgfs *rm_dfs)
  585. {
  586. const char *c_dir_name;
  587. const char *p_dir_name;
  588. struct glink_dbgfs_dent *entry, *entry_temp;
  589. if (rm_dfs == NULL)
  590. return;
  591. c_dir_name = rm_dfs->curr_name;
  592. p_dir_name = rm_dfs->par_name;
  593. mutex_lock(&dent_list_lock_lha0);
  594. list_for_each_entry_safe(entry, entry_temp, &dent_list, list_node) {
  595. if (!strcmp(entry->par_name, c_dir_name)) {
  596. glink_remove_dfs_entry(entry);
  597. } else if (!strcmp(entry->self_name, c_dir_name)
  598. && !strcmp(entry->par_name, p_dir_name)) {
  599. entry->rm_debugfs = true;
  600. glink_remove_dfs_entry(entry);
  601. }
  602. }
  603. mutex_unlock(&dent_list_lock_lha0);
  604. }
  605. EXPORT_SYMBOL(glink_debugfs_remove_recur);
  606. /**
  607. * glink_debugfs_create() - create the debugfs file
  608. * @name: debugfs file name
  609. * @show: pointer to the actual function which will be invoked upon
  610. * opening this file.
  611. * @dir: pointer to a structure debugfs_dir
  612. * dbgfs_data: pointer to any private data need to be associated with debugfs
  613. * b_free_req: boolean value to decide to free the memory associated with
  614. * @dbgfs_data during deletion of the file
  615. *
  616. * Return: pointer to the file/directory created, NULL in case of error
  617. *
  618. * This function checks which directory will be used to create the debugfs file
  619. * and calls glink_dfs_create_file. Anybody who intend to allocate some memory
  620. * for the dbgfs_data and required to free it in deletion, need to set
  621. * b_free_req to true. Otherwise, there will be a memory leak.
  622. */
  623. struct dentry *glink_debugfs_create(const char *name,
  624. void (*show)(struct seq_file *),
  625. struct glink_dbgfs *dir, void *dbgfs_data, bool b_free_req)
  626. {
  627. struct dentry *parent = NULL;
  628. struct dentry *dent = NULL;
  629. struct glink_dbgfs_dent *entry;
  630. struct glink_dbgfs_data *file_data;
  631. const char *c_dir_name;
  632. const char *p_dir_name;
  633. unsigned long flags;
  634. if (dir == NULL) {
  635. GLINK_ERR("%s: debugfs_dir strucutre is null\n", __func__);
  636. return NULL;
  637. }
  638. c_dir_name = dir->curr_name;
  639. p_dir_name = dir->par_name;
  640. mutex_lock(&dent_list_lock_lha0);
  641. list_for_each_entry(entry, &dent_list, list_node)
  642. if (!strcmp(entry->par_name, p_dir_name)
  643. && !strcmp(entry->self_name, c_dir_name)) {
  644. parent = entry->self;
  645. break;
  646. }
  647. mutex_unlock(&dent_list_lock_lha0);
  648. p_dir_name = c_dir_name;
  649. c_dir_name = name;
  650. if (parent != NULL) {
  651. if (dir->b_dir_create) {
  652. dent = debugfs_create_dir(name, parent);
  653. if (dent != NULL)
  654. glink_dfs_update_list(dent, parent,
  655. c_dir_name, p_dir_name);
  656. } else {
  657. file_data = glink_dfs_create_file(name, parent, show,
  658. dbgfs_data, b_free_req);
  659. spin_lock_irqsave(&entry->file_list_lock_lhb0, flags);
  660. if (file_data != NULL)
  661. list_add_tail(&file_data->flist,
  662. &entry->file_list);
  663. spin_unlock_irqrestore(&entry->file_list_lock_lhb0,
  664. flags);
  665. }
  666. } else {
  667. GLINK_DBG("%s: parent dentry is null for [%s]\n",
  668. __func__, name);
  669. }
  670. return dent;
  671. }
  672. EXPORT_SYMBOL(glink_debugfs_create);
  673. /**
  674. * glink_debugfs_init() - initialize the glink debugfs directory structure
  675. *
  676. * Return: 0 in success otherwise appropriate error code
  677. *
  678. * This function initializes the debugfs directory for glink
  679. */
  680. int glink_debugfs_init(void)
  681. {
  682. struct glink_dbgfs dbgfs;
  683. /* fake parent name */
  684. dent = debugfs_create_dir("glink", NULL);
  685. if (IS_ERR_OR_NULL(dent))
  686. return PTR_ERR(dent);
  687. glink_dfs_update_list(dent, NULL, "glink", "root");
  688. dbgfs.b_dir_create = true;
  689. dbgfs.curr_name = "glink";
  690. dbgfs.par_name = "root";
  691. glink_debugfs_create("xprt", NULL, &dbgfs, NULL, false);
  692. glink_debugfs_create("channel", NULL, &dbgfs, NULL, false);
  693. dbgfs.curr_name = "channel";
  694. dbgfs.par_name = "glink";
  695. dbgfs.b_dir_create = false;
  696. glink_debugfs_create("channels", glink_dfs_create_channel_list,
  697. &dbgfs, NULL, false);
  698. dbgfs.curr_name = "xprt";
  699. glink_debugfs_create("xprts", glink_dfs_create_xprt_list,
  700. &dbgfs, NULL, false);
  701. return 0;
  702. }
  703. EXPORT_SYMBOL(glink_debugfs_init);
  704. /**
  705. * glink_debugfs_exit() - removes the glink debugfs directory
  706. *
  707. * This function recursively remove all the debugfs directories
  708. * starting from dent
  709. */
  710. void glink_debugfs_exit(void)
  711. {
  712. if (dent != NULL)
  713. debugfs_remove_recursive(dent);
  714. }
  715. EXPORT_SYMBOL(glink_debugfs_exit);
  716. #else
  717. void glink_debugfs_remove_recur(struct glink_dbgfs *dfs) { }
  718. EXPORT_SYMBOL(glink_debugfs_remove_recur);
  719. void glink_debugfs_remove_channel(struct channel_ctx *ch_ctx,
  720. struct glink_core_xprt_ctx *xprt_ctx) { }
  721. EXPORT_SYMBOL(glink_debugfs_remove_channel);
  722. void glink_debugfs_add_channel(struct channel_ctx *ch_ctx,
  723. struct glink_core_xprt_ctx *xprt_ctx) { }
  724. EXPORT_SYMBOL(glink_debugfs_add_channel);
  725. void glink_debugfs_add_xprt(struct glink_core_xprt_ctx *xprt_ctx) { }
  726. EXPORT_SYMBOL(glink_debugfs_add_xprt);
  727. int glink_debugfs_init(void) { return 0; }
  728. EXPORT_SYMBOL(glink_debugfs_init);
  729. void glink_debugfs_exit(void) { }
  730. EXPORT_SYMBOL(glink_debugfs_exit);
  731. #endif /* CONFIG_DEBUG_FS */