debugfs.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  1. #ifdef CONFIG_DEBUG_FS
  2. /*
  3. * Copyright(c) 2015, 2016 Intel Corporation.
  4. *
  5. * This file is provided under a dual BSD/GPLv2 license. When using or
  6. * redistributing this file, you may do so under either license.
  7. *
  8. * GPL LICENSE SUMMARY
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of version 2 of the GNU General Public License as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * BSD LICENSE
  20. *
  21. * Redistribution and use in source and binary forms, with or without
  22. * modification, are permitted provided that the following conditions
  23. * are met:
  24. *
  25. * - Redistributions of source code must retain the above copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * - Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in
  29. * the documentation and/or other materials provided with the
  30. * distribution.
  31. * - Neither the name of Intel Corporation nor the names of its
  32. * contributors may be used to endorse or promote products derived
  33. * from this software without specific prior written permission.
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  36. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  37. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  38. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  39. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  42. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  43. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  44. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  45. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  46. *
  47. */
  48. #include <linux/debugfs.h>
  49. #include <linux/seq_file.h>
  50. #include <linux/kernel.h>
  51. #include <linux/export.h>
  52. #include <linux/module.h>
  53. #include <linux/string.h>
  54. #include "hfi.h"
  55. #include "debugfs.h"
  56. #include "device.h"
  57. #include "qp.h"
  58. #include "sdma.h"
  59. static struct dentry *hfi1_dbg_root;
  60. /* wrappers to enforce srcu in seq file */
  61. static ssize_t hfi1_seq_read(
  62. struct file *file,
  63. char __user *buf,
  64. size_t size,
  65. loff_t *ppos)
  66. {
  67. struct dentry *d = file->f_path.dentry;
  68. int srcu_idx;
  69. ssize_t r;
  70. r = debugfs_use_file_start(d, &srcu_idx);
  71. if (likely(!r))
  72. r = seq_read(file, buf, size, ppos);
  73. debugfs_use_file_finish(srcu_idx);
  74. return r;
  75. }
  76. static loff_t hfi1_seq_lseek(
  77. struct file *file,
  78. loff_t offset,
  79. int whence)
  80. {
  81. struct dentry *d = file->f_path.dentry;
  82. int srcu_idx;
  83. loff_t r;
  84. r = debugfs_use_file_start(d, &srcu_idx);
  85. if (likely(!r))
  86. r = seq_lseek(file, offset, whence);
  87. debugfs_use_file_finish(srcu_idx);
  88. return r;
  89. }
  90. #define private2dd(file) (file_inode(file)->i_private)
  91. #define private2ppd(file) (file_inode(file)->i_private)
  92. #define DEBUGFS_SEQ_FILE_OPS(name) \
  93. static const struct seq_operations _##name##_seq_ops = { \
  94. .start = _##name##_seq_start, \
  95. .next = _##name##_seq_next, \
  96. .stop = _##name##_seq_stop, \
  97. .show = _##name##_seq_show \
  98. }
  99. #define DEBUGFS_SEQ_FILE_OPEN(name) \
  100. static int _##name##_open(struct inode *inode, struct file *s) \
  101. { \
  102. struct seq_file *seq; \
  103. int ret; \
  104. ret = seq_open(s, &_##name##_seq_ops); \
  105. if (ret) \
  106. return ret; \
  107. seq = s->private_data; \
  108. seq->private = inode->i_private; \
  109. return 0; \
  110. }
  111. #define DEBUGFS_FILE_OPS(name) \
  112. static const struct file_operations _##name##_file_ops = { \
  113. .owner = THIS_MODULE, \
  114. .open = _##name##_open, \
  115. .read = hfi1_seq_read, \
  116. .llseek = hfi1_seq_lseek, \
  117. .release = seq_release \
  118. }
  119. #define DEBUGFS_FILE_CREATE(name, parent, data, ops, mode) \
  120. do { \
  121. struct dentry *ent; \
  122. ent = debugfs_create_file(name, mode, parent, \
  123. data, ops); \
  124. if (!ent) \
  125. pr_warn("create of %s failed\n", name); \
  126. } while (0)
  127. #define DEBUGFS_SEQ_FILE_CREATE(name, parent, data) \
  128. DEBUGFS_FILE_CREATE(#name, parent, data, &_##name##_file_ops, S_IRUGO)
  129. static void *_opcode_stats_seq_start(struct seq_file *s, loff_t *pos)
  130. {
  131. struct hfi1_opcode_stats_perctx *opstats;
  132. if (*pos >= ARRAY_SIZE(opstats->stats))
  133. return NULL;
  134. return pos;
  135. }
  136. static void *_opcode_stats_seq_next(struct seq_file *s, void *v, loff_t *pos)
  137. {
  138. struct hfi1_opcode_stats_perctx *opstats;
  139. ++*pos;
  140. if (*pos >= ARRAY_SIZE(opstats->stats))
  141. return NULL;
  142. return pos;
  143. }
  144. static void _opcode_stats_seq_stop(struct seq_file *s, void *v)
  145. {
  146. }
  147. static int _opcode_stats_seq_show(struct seq_file *s, void *v)
  148. {
  149. loff_t *spos = v;
  150. loff_t i = *spos, j;
  151. u64 n_packets = 0, n_bytes = 0;
  152. struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
  153. struct hfi1_devdata *dd = dd_from_dev(ibd);
  154. for (j = 0; j < dd->first_user_ctxt; j++) {
  155. if (!dd->rcd[j])
  156. continue;
  157. n_packets += dd->rcd[j]->opstats->stats[i].n_packets;
  158. n_bytes += dd->rcd[j]->opstats->stats[i].n_bytes;
  159. }
  160. if (!n_packets && !n_bytes)
  161. return SEQ_SKIP;
  162. seq_printf(s, "%02llx %llu/%llu\n", i,
  163. (unsigned long long)n_packets,
  164. (unsigned long long)n_bytes);
  165. return 0;
  166. }
  167. DEBUGFS_SEQ_FILE_OPS(opcode_stats);
  168. DEBUGFS_SEQ_FILE_OPEN(opcode_stats)
  169. DEBUGFS_FILE_OPS(opcode_stats);
  170. static void *_ctx_stats_seq_start(struct seq_file *s, loff_t *pos)
  171. {
  172. struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
  173. struct hfi1_devdata *dd = dd_from_dev(ibd);
  174. if (!*pos)
  175. return SEQ_START_TOKEN;
  176. if (*pos >= dd->first_user_ctxt)
  177. return NULL;
  178. return pos;
  179. }
  180. static void *_ctx_stats_seq_next(struct seq_file *s, void *v, loff_t *pos)
  181. {
  182. struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
  183. struct hfi1_devdata *dd = dd_from_dev(ibd);
  184. if (v == SEQ_START_TOKEN)
  185. return pos;
  186. ++*pos;
  187. if (*pos >= dd->first_user_ctxt)
  188. return NULL;
  189. return pos;
  190. }
  191. static void _ctx_stats_seq_stop(struct seq_file *s, void *v)
  192. {
  193. /* nothing allocated */
  194. }
  195. static int _ctx_stats_seq_show(struct seq_file *s, void *v)
  196. {
  197. loff_t *spos;
  198. loff_t i, j;
  199. u64 n_packets = 0;
  200. struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
  201. struct hfi1_devdata *dd = dd_from_dev(ibd);
  202. if (v == SEQ_START_TOKEN) {
  203. seq_puts(s, "Ctx:npkts\n");
  204. return 0;
  205. }
  206. spos = v;
  207. i = *spos;
  208. if (!dd->rcd[i])
  209. return SEQ_SKIP;
  210. for (j = 0; j < ARRAY_SIZE(dd->rcd[i]->opstats->stats); j++)
  211. n_packets += dd->rcd[i]->opstats->stats[j].n_packets;
  212. if (!n_packets)
  213. return SEQ_SKIP;
  214. seq_printf(s, " %llu:%llu\n", i, n_packets);
  215. return 0;
  216. }
  217. DEBUGFS_SEQ_FILE_OPS(ctx_stats);
  218. DEBUGFS_SEQ_FILE_OPEN(ctx_stats)
  219. DEBUGFS_FILE_OPS(ctx_stats);
  220. static void *_qp_stats_seq_start(struct seq_file *s, loff_t *pos)
  221. __acquires(RCU)
  222. {
  223. struct qp_iter *iter;
  224. loff_t n = *pos;
  225. iter = qp_iter_init(s->private);
  226. /* stop calls rcu_read_unlock */
  227. rcu_read_lock();
  228. if (!iter)
  229. return NULL;
  230. do {
  231. if (qp_iter_next(iter)) {
  232. kfree(iter);
  233. return NULL;
  234. }
  235. } while (n--);
  236. return iter;
  237. }
  238. static void *_qp_stats_seq_next(struct seq_file *s, void *iter_ptr,
  239. loff_t *pos)
  240. __must_hold(RCU)
  241. {
  242. struct qp_iter *iter = iter_ptr;
  243. (*pos)++;
  244. if (qp_iter_next(iter)) {
  245. kfree(iter);
  246. return NULL;
  247. }
  248. return iter;
  249. }
  250. static void _qp_stats_seq_stop(struct seq_file *s, void *iter_ptr)
  251. __releases(RCU)
  252. {
  253. rcu_read_unlock();
  254. }
  255. static int _qp_stats_seq_show(struct seq_file *s, void *iter_ptr)
  256. {
  257. struct qp_iter *iter = iter_ptr;
  258. if (!iter)
  259. return 0;
  260. qp_iter_print(s, iter);
  261. return 0;
  262. }
  263. DEBUGFS_SEQ_FILE_OPS(qp_stats);
  264. DEBUGFS_SEQ_FILE_OPEN(qp_stats)
  265. DEBUGFS_FILE_OPS(qp_stats);
  266. static void *_sdes_seq_start(struct seq_file *s, loff_t *pos)
  267. {
  268. struct hfi1_ibdev *ibd;
  269. struct hfi1_devdata *dd;
  270. ibd = (struct hfi1_ibdev *)s->private;
  271. dd = dd_from_dev(ibd);
  272. if (!dd->per_sdma || *pos >= dd->num_sdma)
  273. return NULL;
  274. return pos;
  275. }
  276. static void *_sdes_seq_next(struct seq_file *s, void *v, loff_t *pos)
  277. {
  278. struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
  279. struct hfi1_devdata *dd = dd_from_dev(ibd);
  280. ++*pos;
  281. if (!dd->per_sdma || *pos >= dd->num_sdma)
  282. return NULL;
  283. return pos;
  284. }
  285. static void _sdes_seq_stop(struct seq_file *s, void *v)
  286. {
  287. }
  288. static int _sdes_seq_show(struct seq_file *s, void *v)
  289. {
  290. struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
  291. struct hfi1_devdata *dd = dd_from_dev(ibd);
  292. loff_t *spos = v;
  293. loff_t i = *spos;
  294. sdma_seqfile_dump_sde(s, &dd->per_sdma[i]);
  295. return 0;
  296. }
  297. DEBUGFS_SEQ_FILE_OPS(sdes);
  298. DEBUGFS_SEQ_FILE_OPEN(sdes)
  299. DEBUGFS_FILE_OPS(sdes);
  300. /* read the per-device counters */
  301. static ssize_t dev_counters_read(struct file *file, char __user *buf,
  302. size_t count, loff_t *ppos)
  303. {
  304. u64 *counters;
  305. size_t avail;
  306. struct hfi1_devdata *dd;
  307. ssize_t rval;
  308. dd = private2dd(file);
  309. avail = hfi1_read_cntrs(dd, NULL, &counters);
  310. rval = simple_read_from_buffer(buf, count, ppos, counters, avail);
  311. return rval;
  312. }
  313. /* read the per-device counters */
  314. static ssize_t dev_names_read(struct file *file, char __user *buf,
  315. size_t count, loff_t *ppos)
  316. {
  317. char *names;
  318. size_t avail;
  319. struct hfi1_devdata *dd;
  320. ssize_t rval;
  321. dd = private2dd(file);
  322. avail = hfi1_read_cntrs(dd, &names, NULL);
  323. rval = simple_read_from_buffer(buf, count, ppos, names, avail);
  324. return rval;
  325. }
  326. struct counter_info {
  327. char *name;
  328. const struct file_operations ops;
  329. };
  330. /*
  331. * Could use file_inode(file)->i_ino to figure out which file,
  332. * instead of separate routine for each, but for now, this works...
  333. */
  334. /* read the per-port names (same for each port) */
  335. static ssize_t portnames_read(struct file *file, char __user *buf,
  336. size_t count, loff_t *ppos)
  337. {
  338. char *names;
  339. size_t avail;
  340. struct hfi1_devdata *dd;
  341. ssize_t rval;
  342. dd = private2dd(file);
  343. avail = hfi1_read_portcntrs(dd->pport, &names, NULL);
  344. rval = simple_read_from_buffer(buf, count, ppos, names, avail);
  345. return rval;
  346. }
  347. /* read the per-port counters */
  348. static ssize_t portcntrs_debugfs_read(struct file *file, char __user *buf,
  349. size_t count, loff_t *ppos)
  350. {
  351. u64 *counters;
  352. size_t avail;
  353. struct hfi1_pportdata *ppd;
  354. ssize_t rval;
  355. ppd = private2ppd(file);
  356. avail = hfi1_read_portcntrs(ppd, NULL, &counters);
  357. rval = simple_read_from_buffer(buf, count, ppos, counters, avail);
  358. return rval;
  359. }
  360. static void check_dyn_flag(u64 scratch0, char *p, int size, int *used,
  361. int this_hfi, int hfi, u32 flag, const char *what)
  362. {
  363. u32 mask;
  364. mask = flag << (hfi ? CR_DYN_SHIFT : 0);
  365. if (scratch0 & mask) {
  366. *used += scnprintf(p + *used, size - *used,
  367. " 0x%08x - HFI%d %s in use, %s device\n",
  368. mask, hfi, what,
  369. this_hfi == hfi ? "this" : "other");
  370. }
  371. }
  372. static ssize_t asic_flags_read(struct file *file, char __user *buf,
  373. size_t count, loff_t *ppos)
  374. {
  375. struct hfi1_pportdata *ppd;
  376. struct hfi1_devdata *dd;
  377. u64 scratch0;
  378. char *tmp;
  379. int ret = 0;
  380. int size;
  381. int used;
  382. int i;
  383. ppd = private2ppd(file);
  384. dd = ppd->dd;
  385. size = PAGE_SIZE;
  386. used = 0;
  387. tmp = kmalloc(size, GFP_KERNEL);
  388. if (!tmp)
  389. return -ENOMEM;
  390. scratch0 = read_csr(dd, ASIC_CFG_SCRATCH);
  391. used += scnprintf(tmp + used, size - used,
  392. "Resource flags: 0x%016llx\n", scratch0);
  393. /* check permanent flag */
  394. if (scratch0 & CR_THERM_INIT) {
  395. used += scnprintf(tmp + used, size - used,
  396. " 0x%08x - thermal monitoring initialized\n",
  397. (u32)CR_THERM_INIT);
  398. }
  399. /* check each dynamic flag on each HFI */
  400. for (i = 0; i < 2; i++) {
  401. check_dyn_flag(scratch0, tmp, size, &used, dd->hfi1_id, i,
  402. CR_SBUS, "SBus");
  403. check_dyn_flag(scratch0, tmp, size, &used, dd->hfi1_id, i,
  404. CR_EPROM, "EPROM");
  405. check_dyn_flag(scratch0, tmp, size, &used, dd->hfi1_id, i,
  406. CR_I2C1, "i2c chain 1");
  407. check_dyn_flag(scratch0, tmp, size, &used, dd->hfi1_id, i,
  408. CR_I2C2, "i2c chain 2");
  409. }
  410. used += scnprintf(tmp + used, size - used, "Write bits to clear\n");
  411. ret = simple_read_from_buffer(buf, count, ppos, tmp, used);
  412. kfree(tmp);
  413. return ret;
  414. }
  415. static ssize_t asic_flags_write(struct file *file, const char __user *buf,
  416. size_t count, loff_t *ppos)
  417. {
  418. struct hfi1_pportdata *ppd;
  419. struct hfi1_devdata *dd;
  420. char *buff;
  421. int ret;
  422. unsigned long long value;
  423. u64 scratch0;
  424. u64 clear;
  425. ppd = private2ppd(file);
  426. dd = ppd->dd;
  427. /* zero terminate and read the expected integer */
  428. buff = memdup_user_nul(buf, count);
  429. if (IS_ERR(buff))
  430. return PTR_ERR(buff);
  431. ret = kstrtoull(buff, 0, &value);
  432. if (ret)
  433. goto do_free;
  434. clear = value;
  435. /* obtain exclusive access */
  436. mutex_lock(&dd->asic_data->asic_resource_mutex);
  437. acquire_hw_mutex(dd);
  438. scratch0 = read_csr(dd, ASIC_CFG_SCRATCH);
  439. scratch0 &= ~clear;
  440. write_csr(dd, ASIC_CFG_SCRATCH, scratch0);
  441. /* force write to be visible to other HFI on another OS */
  442. (void)read_csr(dd, ASIC_CFG_SCRATCH);
  443. release_hw_mutex(dd);
  444. mutex_unlock(&dd->asic_data->asic_resource_mutex);
  445. /* return the number of bytes written */
  446. ret = count;
  447. do_free:
  448. kfree(buff);
  449. return ret;
  450. }
  451. /* read the dc8051 memory */
  452. static ssize_t dc8051_memory_read(struct file *file, char __user *buf,
  453. size_t count, loff_t *ppos)
  454. {
  455. struct hfi1_pportdata *ppd = private2ppd(file);
  456. ssize_t rval;
  457. void *tmp;
  458. loff_t start, end;
  459. /* the checks below expect the position to be positive */
  460. if (*ppos < 0)
  461. return -EINVAL;
  462. tmp = kzalloc(DC8051_DATA_MEM_SIZE, GFP_KERNEL);
  463. if (!tmp)
  464. return -ENOMEM;
  465. /*
  466. * Fill in the requested portion of the temporary buffer from the
  467. * 8051 memory. The 8051 memory read is done in terms of 8 bytes.
  468. * Adjust start and end to fit. Skip reading anything if out of
  469. * range.
  470. */
  471. start = *ppos & ~0x7; /* round down */
  472. if (start < DC8051_DATA_MEM_SIZE) {
  473. end = (*ppos + count + 7) & ~0x7; /* round up */
  474. if (end > DC8051_DATA_MEM_SIZE)
  475. end = DC8051_DATA_MEM_SIZE;
  476. rval = read_8051_data(ppd->dd, start, end - start,
  477. (u64 *)(tmp + start));
  478. if (rval)
  479. goto done;
  480. }
  481. rval = simple_read_from_buffer(buf, count, ppos, tmp,
  482. DC8051_DATA_MEM_SIZE);
  483. done:
  484. kfree(tmp);
  485. return rval;
  486. }
  487. static ssize_t debugfs_lcb_read(struct file *file, char __user *buf,
  488. size_t count, loff_t *ppos)
  489. {
  490. struct hfi1_pportdata *ppd = private2ppd(file);
  491. struct hfi1_devdata *dd = ppd->dd;
  492. unsigned long total, csr_off;
  493. u64 data;
  494. if (*ppos < 0)
  495. return -EINVAL;
  496. /* only read 8 byte quantities */
  497. if ((count % 8) != 0)
  498. return -EINVAL;
  499. /* offset must be 8-byte aligned */
  500. if ((*ppos % 8) != 0)
  501. return -EINVAL;
  502. /* do nothing if out of range or zero count */
  503. if (*ppos >= (LCB_END - LCB_START) || !count)
  504. return 0;
  505. /* reduce count if needed */
  506. if (*ppos + count > LCB_END - LCB_START)
  507. count = (LCB_END - LCB_START) - *ppos;
  508. csr_off = LCB_START + *ppos;
  509. for (total = 0; total < count; total += 8, csr_off += 8) {
  510. if (read_lcb_csr(dd, csr_off, (u64 *)&data))
  511. break; /* failed */
  512. if (put_user(data, (unsigned long __user *)(buf + total)))
  513. break;
  514. }
  515. *ppos += total;
  516. return total;
  517. }
  518. static ssize_t debugfs_lcb_write(struct file *file, const char __user *buf,
  519. size_t count, loff_t *ppos)
  520. {
  521. struct hfi1_pportdata *ppd = private2ppd(file);
  522. struct hfi1_devdata *dd = ppd->dd;
  523. unsigned long total, csr_off, data;
  524. if (*ppos < 0)
  525. return -EINVAL;
  526. /* only write 8 byte quantities */
  527. if ((count % 8) != 0)
  528. return -EINVAL;
  529. /* offset must be 8-byte aligned */
  530. if ((*ppos % 8) != 0)
  531. return -EINVAL;
  532. /* do nothing if out of range or zero count */
  533. if (*ppos >= (LCB_END - LCB_START) || !count)
  534. return 0;
  535. /* reduce count if needed */
  536. if (*ppos + count > LCB_END - LCB_START)
  537. count = (LCB_END - LCB_START) - *ppos;
  538. csr_off = LCB_START + *ppos;
  539. for (total = 0; total < count; total += 8, csr_off += 8) {
  540. if (get_user(data, (unsigned long __user *)(buf + total)))
  541. break;
  542. if (write_lcb_csr(dd, csr_off, data))
  543. break; /* failed */
  544. }
  545. *ppos += total;
  546. return total;
  547. }
  548. /*
  549. * read the per-port QSFP data for ppd
  550. */
  551. static ssize_t qsfp_debugfs_dump(struct file *file, char __user *buf,
  552. size_t count, loff_t *ppos)
  553. {
  554. struct hfi1_pportdata *ppd;
  555. char *tmp;
  556. int ret;
  557. ppd = private2ppd(file);
  558. tmp = kmalloc(PAGE_SIZE, GFP_KERNEL);
  559. if (!tmp)
  560. return -ENOMEM;
  561. ret = qsfp_dump(ppd, tmp, PAGE_SIZE);
  562. if (ret > 0)
  563. ret = simple_read_from_buffer(buf, count, ppos, tmp, ret);
  564. kfree(tmp);
  565. return ret;
  566. }
  567. /* Do an i2c write operation on the chain for the given HFI. */
  568. static ssize_t __i2c_debugfs_write(struct file *file, const char __user *buf,
  569. size_t count, loff_t *ppos, u32 target)
  570. {
  571. struct hfi1_pportdata *ppd;
  572. char *buff;
  573. int ret;
  574. int i2c_addr;
  575. int offset;
  576. int total_written;
  577. ppd = private2ppd(file);
  578. /* byte offset format: [offsetSize][i2cAddr][offsetHigh][offsetLow] */
  579. i2c_addr = (*ppos >> 16) & 0xffff;
  580. offset = *ppos & 0xffff;
  581. /* explicitly reject invalid address 0 to catch cp and cat */
  582. if (i2c_addr == 0)
  583. return -EINVAL;
  584. buff = memdup_user(buf, count);
  585. if (IS_ERR(buff))
  586. return PTR_ERR(buff);
  587. total_written = i2c_write(ppd, target, i2c_addr, offset, buff, count);
  588. if (total_written < 0) {
  589. ret = total_written;
  590. goto _free;
  591. }
  592. *ppos += total_written;
  593. ret = total_written;
  594. _free:
  595. kfree(buff);
  596. return ret;
  597. }
  598. /* Do an i2c write operation on chain for HFI 0. */
  599. static ssize_t i2c1_debugfs_write(struct file *file, const char __user *buf,
  600. size_t count, loff_t *ppos)
  601. {
  602. return __i2c_debugfs_write(file, buf, count, ppos, 0);
  603. }
  604. /* Do an i2c write operation on chain for HFI 1. */
  605. static ssize_t i2c2_debugfs_write(struct file *file, const char __user *buf,
  606. size_t count, loff_t *ppos)
  607. {
  608. return __i2c_debugfs_write(file, buf, count, ppos, 1);
  609. }
  610. /* Do an i2c read operation on the chain for the given HFI. */
  611. static ssize_t __i2c_debugfs_read(struct file *file, char __user *buf,
  612. size_t count, loff_t *ppos, u32 target)
  613. {
  614. struct hfi1_pportdata *ppd;
  615. char *buff;
  616. int ret;
  617. int i2c_addr;
  618. int offset;
  619. int total_read;
  620. ppd = private2ppd(file);
  621. /* byte offset format: [offsetSize][i2cAddr][offsetHigh][offsetLow] */
  622. i2c_addr = (*ppos >> 16) & 0xffff;
  623. offset = *ppos & 0xffff;
  624. /* explicitly reject invalid address 0 to catch cp and cat */
  625. if (i2c_addr == 0)
  626. return -EINVAL;
  627. buff = kmalloc(count, GFP_KERNEL);
  628. if (!buff)
  629. return -ENOMEM;
  630. total_read = i2c_read(ppd, target, i2c_addr, offset, buff, count);
  631. if (total_read < 0) {
  632. ret = total_read;
  633. goto _free;
  634. }
  635. *ppos += total_read;
  636. ret = copy_to_user(buf, buff, total_read);
  637. if (ret > 0) {
  638. ret = -EFAULT;
  639. goto _free;
  640. }
  641. ret = total_read;
  642. _free:
  643. kfree(buff);
  644. return ret;
  645. }
  646. /* Do an i2c read operation on chain for HFI 0. */
  647. static ssize_t i2c1_debugfs_read(struct file *file, char __user *buf,
  648. size_t count, loff_t *ppos)
  649. {
  650. return __i2c_debugfs_read(file, buf, count, ppos, 0);
  651. }
  652. /* Do an i2c read operation on chain for HFI 1. */
  653. static ssize_t i2c2_debugfs_read(struct file *file, char __user *buf,
  654. size_t count, loff_t *ppos)
  655. {
  656. return __i2c_debugfs_read(file, buf, count, ppos, 1);
  657. }
  658. /* Do a QSFP write operation on the i2c chain for the given HFI. */
  659. static ssize_t __qsfp_debugfs_write(struct file *file, const char __user *buf,
  660. size_t count, loff_t *ppos, u32 target)
  661. {
  662. struct hfi1_pportdata *ppd;
  663. char *buff;
  664. int ret;
  665. int total_written;
  666. if (*ppos + count > QSFP_PAGESIZE * 4) /* base page + page00-page03 */
  667. return -EINVAL;
  668. ppd = private2ppd(file);
  669. buff = memdup_user(buf, count);
  670. if (IS_ERR(buff))
  671. return PTR_ERR(buff);
  672. total_written = qsfp_write(ppd, target, *ppos, buff, count);
  673. if (total_written < 0) {
  674. ret = total_written;
  675. goto _free;
  676. }
  677. *ppos += total_written;
  678. ret = total_written;
  679. _free:
  680. kfree(buff);
  681. return ret;
  682. }
  683. /* Do a QSFP write operation on i2c chain for HFI 0. */
  684. static ssize_t qsfp1_debugfs_write(struct file *file, const char __user *buf,
  685. size_t count, loff_t *ppos)
  686. {
  687. return __qsfp_debugfs_write(file, buf, count, ppos, 0);
  688. }
  689. /* Do a QSFP write operation on i2c chain for HFI 1. */
  690. static ssize_t qsfp2_debugfs_write(struct file *file, const char __user *buf,
  691. size_t count, loff_t *ppos)
  692. {
  693. return __qsfp_debugfs_write(file, buf, count, ppos, 1);
  694. }
  695. /* Do a QSFP read operation on the i2c chain for the given HFI. */
  696. static ssize_t __qsfp_debugfs_read(struct file *file, char __user *buf,
  697. size_t count, loff_t *ppos, u32 target)
  698. {
  699. struct hfi1_pportdata *ppd;
  700. char *buff;
  701. int ret;
  702. int total_read;
  703. if (*ppos + count > QSFP_PAGESIZE * 4) { /* base page + page00-page03 */
  704. ret = -EINVAL;
  705. goto _return;
  706. }
  707. ppd = private2ppd(file);
  708. buff = kmalloc(count, GFP_KERNEL);
  709. if (!buff) {
  710. ret = -ENOMEM;
  711. goto _return;
  712. }
  713. total_read = qsfp_read(ppd, target, *ppos, buff, count);
  714. if (total_read < 0) {
  715. ret = total_read;
  716. goto _free;
  717. }
  718. *ppos += total_read;
  719. ret = copy_to_user(buf, buff, total_read);
  720. if (ret > 0) {
  721. ret = -EFAULT;
  722. goto _free;
  723. }
  724. ret = total_read;
  725. _free:
  726. kfree(buff);
  727. _return:
  728. return ret;
  729. }
  730. /* Do a QSFP read operation on i2c chain for HFI 0. */
  731. static ssize_t qsfp1_debugfs_read(struct file *file, char __user *buf,
  732. size_t count, loff_t *ppos)
  733. {
  734. return __qsfp_debugfs_read(file, buf, count, ppos, 0);
  735. }
  736. /* Do a QSFP read operation on i2c chain for HFI 1. */
  737. static ssize_t qsfp2_debugfs_read(struct file *file, char __user *buf,
  738. size_t count, loff_t *ppos)
  739. {
  740. return __qsfp_debugfs_read(file, buf, count, ppos, 1);
  741. }
  742. static int __i2c_debugfs_open(struct inode *in, struct file *fp, u32 target)
  743. {
  744. struct hfi1_pportdata *ppd;
  745. int ret;
  746. if (!try_module_get(THIS_MODULE))
  747. return -ENODEV;
  748. ppd = private2ppd(fp);
  749. ret = acquire_chip_resource(ppd->dd, i2c_target(target), 0);
  750. if (ret) /* failed - release the module */
  751. module_put(THIS_MODULE);
  752. return ret;
  753. }
  754. static int i2c1_debugfs_open(struct inode *in, struct file *fp)
  755. {
  756. return __i2c_debugfs_open(in, fp, 0);
  757. }
  758. static int i2c2_debugfs_open(struct inode *in, struct file *fp)
  759. {
  760. return __i2c_debugfs_open(in, fp, 1);
  761. }
  762. static int __i2c_debugfs_release(struct inode *in, struct file *fp, u32 target)
  763. {
  764. struct hfi1_pportdata *ppd;
  765. ppd = private2ppd(fp);
  766. release_chip_resource(ppd->dd, i2c_target(target));
  767. module_put(THIS_MODULE);
  768. return 0;
  769. }
  770. static int i2c1_debugfs_release(struct inode *in, struct file *fp)
  771. {
  772. return __i2c_debugfs_release(in, fp, 0);
  773. }
  774. static int i2c2_debugfs_release(struct inode *in, struct file *fp)
  775. {
  776. return __i2c_debugfs_release(in, fp, 1);
  777. }
  778. static int __qsfp_debugfs_open(struct inode *in, struct file *fp, u32 target)
  779. {
  780. struct hfi1_pportdata *ppd;
  781. int ret;
  782. if (!try_module_get(THIS_MODULE))
  783. return -ENODEV;
  784. ppd = private2ppd(fp);
  785. ret = acquire_chip_resource(ppd->dd, i2c_target(target), 0);
  786. if (ret) /* failed - release the module */
  787. module_put(THIS_MODULE);
  788. return ret;
  789. }
  790. static int qsfp1_debugfs_open(struct inode *in, struct file *fp)
  791. {
  792. return __qsfp_debugfs_open(in, fp, 0);
  793. }
  794. static int qsfp2_debugfs_open(struct inode *in, struct file *fp)
  795. {
  796. return __qsfp_debugfs_open(in, fp, 1);
  797. }
  798. static int __qsfp_debugfs_release(struct inode *in, struct file *fp, u32 target)
  799. {
  800. struct hfi1_pportdata *ppd;
  801. ppd = private2ppd(fp);
  802. release_chip_resource(ppd->dd, i2c_target(target));
  803. module_put(THIS_MODULE);
  804. return 0;
  805. }
  806. static int qsfp1_debugfs_release(struct inode *in, struct file *fp)
  807. {
  808. return __qsfp_debugfs_release(in, fp, 0);
  809. }
  810. static int qsfp2_debugfs_release(struct inode *in, struct file *fp)
  811. {
  812. return __qsfp_debugfs_release(in, fp, 1);
  813. }
  814. #define DEBUGFS_OPS(nm, readroutine, writeroutine) \
  815. { \
  816. .name = nm, \
  817. .ops = { \
  818. .read = readroutine, \
  819. .write = writeroutine, \
  820. .llseek = generic_file_llseek, \
  821. }, \
  822. }
  823. #define DEBUGFS_XOPS(nm, readf, writef, openf, releasef) \
  824. { \
  825. .name = nm, \
  826. .ops = { \
  827. .read = readf, \
  828. .write = writef, \
  829. .llseek = generic_file_llseek, \
  830. .open = openf, \
  831. .release = releasef \
  832. }, \
  833. }
  834. static const struct counter_info cntr_ops[] = {
  835. DEBUGFS_OPS("counter_names", dev_names_read, NULL),
  836. DEBUGFS_OPS("counters", dev_counters_read, NULL),
  837. DEBUGFS_OPS("portcounter_names", portnames_read, NULL),
  838. };
  839. static const struct counter_info port_cntr_ops[] = {
  840. DEBUGFS_OPS("port%dcounters", portcntrs_debugfs_read, NULL),
  841. DEBUGFS_XOPS("i2c1", i2c1_debugfs_read, i2c1_debugfs_write,
  842. i2c1_debugfs_open, i2c1_debugfs_release),
  843. DEBUGFS_XOPS("i2c2", i2c2_debugfs_read, i2c2_debugfs_write,
  844. i2c2_debugfs_open, i2c2_debugfs_release),
  845. DEBUGFS_OPS("qsfp_dump%d", qsfp_debugfs_dump, NULL),
  846. DEBUGFS_XOPS("qsfp1", qsfp1_debugfs_read, qsfp1_debugfs_write,
  847. qsfp1_debugfs_open, qsfp1_debugfs_release),
  848. DEBUGFS_XOPS("qsfp2", qsfp2_debugfs_read, qsfp2_debugfs_write,
  849. qsfp2_debugfs_open, qsfp2_debugfs_release),
  850. DEBUGFS_OPS("asic_flags", asic_flags_read, asic_flags_write),
  851. DEBUGFS_OPS("dc8051_memory", dc8051_memory_read, NULL),
  852. DEBUGFS_OPS("lcb", debugfs_lcb_read, debugfs_lcb_write),
  853. };
  854. static void *_sdma_cpu_list_seq_start(struct seq_file *s, loff_t *pos)
  855. {
  856. if (*pos >= num_online_cpus())
  857. return NULL;
  858. return pos;
  859. }
  860. static void *_sdma_cpu_list_seq_next(struct seq_file *s, void *v, loff_t *pos)
  861. {
  862. ++*pos;
  863. if (*pos >= num_online_cpus())
  864. return NULL;
  865. return pos;
  866. }
  867. static void _sdma_cpu_list_seq_stop(struct seq_file *s, void *v)
  868. {
  869. /* nothing allocated */
  870. }
  871. static int _sdma_cpu_list_seq_show(struct seq_file *s, void *v)
  872. {
  873. struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
  874. struct hfi1_devdata *dd = dd_from_dev(ibd);
  875. loff_t *spos = v;
  876. loff_t i = *spos;
  877. sdma_seqfile_dump_cpu_list(s, dd, (unsigned long)i);
  878. return 0;
  879. }
  880. DEBUGFS_SEQ_FILE_OPS(sdma_cpu_list);
  881. DEBUGFS_SEQ_FILE_OPEN(sdma_cpu_list)
  882. DEBUGFS_FILE_OPS(sdma_cpu_list);
  883. void hfi1_dbg_ibdev_init(struct hfi1_ibdev *ibd)
  884. {
  885. char name[sizeof("port0counters") + 1];
  886. char link[10];
  887. struct hfi1_devdata *dd = dd_from_dev(ibd);
  888. struct hfi1_pportdata *ppd;
  889. int unit = dd->unit;
  890. int i, j;
  891. if (!hfi1_dbg_root)
  892. return;
  893. snprintf(name, sizeof(name), "%s_%d", class_name(), unit);
  894. snprintf(link, sizeof(link), "%d", unit);
  895. ibd->hfi1_ibdev_dbg = debugfs_create_dir(name, hfi1_dbg_root);
  896. if (!ibd->hfi1_ibdev_dbg) {
  897. pr_warn("create of %s failed\n", name);
  898. return;
  899. }
  900. ibd->hfi1_ibdev_link =
  901. debugfs_create_symlink(link, hfi1_dbg_root, name);
  902. if (!ibd->hfi1_ibdev_link) {
  903. pr_warn("create of %s symlink failed\n", name);
  904. return;
  905. }
  906. DEBUGFS_SEQ_FILE_CREATE(opcode_stats, ibd->hfi1_ibdev_dbg, ibd);
  907. DEBUGFS_SEQ_FILE_CREATE(ctx_stats, ibd->hfi1_ibdev_dbg, ibd);
  908. DEBUGFS_SEQ_FILE_CREATE(qp_stats, ibd->hfi1_ibdev_dbg, ibd);
  909. DEBUGFS_SEQ_FILE_CREATE(sdes, ibd->hfi1_ibdev_dbg, ibd);
  910. DEBUGFS_SEQ_FILE_CREATE(sdma_cpu_list, ibd->hfi1_ibdev_dbg, ibd);
  911. /* dev counter files */
  912. for (i = 0; i < ARRAY_SIZE(cntr_ops); i++)
  913. DEBUGFS_FILE_CREATE(cntr_ops[i].name,
  914. ibd->hfi1_ibdev_dbg,
  915. dd,
  916. &cntr_ops[i].ops, S_IRUGO);
  917. /* per port files */
  918. for (ppd = dd->pport, j = 0; j < dd->num_pports; j++, ppd++)
  919. for (i = 0; i < ARRAY_SIZE(port_cntr_ops); i++) {
  920. snprintf(name,
  921. sizeof(name),
  922. port_cntr_ops[i].name,
  923. j + 1);
  924. DEBUGFS_FILE_CREATE(name,
  925. ibd->hfi1_ibdev_dbg,
  926. ppd,
  927. &port_cntr_ops[i].ops,
  928. !port_cntr_ops[i].ops.write ?
  929. S_IRUGO : S_IRUGO | S_IWUSR);
  930. }
  931. }
  932. void hfi1_dbg_ibdev_exit(struct hfi1_ibdev *ibd)
  933. {
  934. if (!hfi1_dbg_root)
  935. goto out;
  936. debugfs_remove(ibd->hfi1_ibdev_link);
  937. debugfs_remove_recursive(ibd->hfi1_ibdev_dbg);
  938. out:
  939. ibd->hfi1_ibdev_dbg = NULL;
  940. }
  941. /*
  942. * driver stats field names, one line per stat, single string. Used by
  943. * programs like hfistats to print the stats in a way which works for
  944. * different versions of drivers, without changing program source.
  945. * if hfi1_ib_stats changes, this needs to change. Names need to be
  946. * 12 chars or less (w/o newline), for proper display by hfistats utility.
  947. */
  948. static const char * const hfi1_statnames[] = {
  949. /* must be element 0*/
  950. "KernIntr",
  951. "ErrorIntr",
  952. "Tx_Errs",
  953. "Rcv_Errs",
  954. "H/W_Errs",
  955. "NoPIOBufs",
  956. "CtxtsOpen",
  957. "RcvLen_Errs",
  958. "EgrBufFull",
  959. "EgrHdrFull"
  960. };
  961. static void *_driver_stats_names_seq_start(struct seq_file *s, loff_t *pos)
  962. {
  963. if (*pos >= ARRAY_SIZE(hfi1_statnames))
  964. return NULL;
  965. return pos;
  966. }
  967. static void *_driver_stats_names_seq_next(
  968. struct seq_file *s,
  969. void *v,
  970. loff_t *pos)
  971. {
  972. ++*pos;
  973. if (*pos >= ARRAY_SIZE(hfi1_statnames))
  974. return NULL;
  975. return pos;
  976. }
  977. static void _driver_stats_names_seq_stop(struct seq_file *s, void *v)
  978. {
  979. }
  980. static int _driver_stats_names_seq_show(struct seq_file *s, void *v)
  981. {
  982. loff_t *spos = v;
  983. seq_printf(s, "%s\n", hfi1_statnames[*spos]);
  984. return 0;
  985. }
  986. DEBUGFS_SEQ_FILE_OPS(driver_stats_names);
  987. DEBUGFS_SEQ_FILE_OPEN(driver_stats_names)
  988. DEBUGFS_FILE_OPS(driver_stats_names);
  989. static void *_driver_stats_seq_start(struct seq_file *s, loff_t *pos)
  990. {
  991. if (*pos >= ARRAY_SIZE(hfi1_statnames))
  992. return NULL;
  993. return pos;
  994. }
  995. static void *_driver_stats_seq_next(struct seq_file *s, void *v, loff_t *pos)
  996. {
  997. ++*pos;
  998. if (*pos >= ARRAY_SIZE(hfi1_statnames))
  999. return NULL;
  1000. return pos;
  1001. }
  1002. static void _driver_stats_seq_stop(struct seq_file *s, void *v)
  1003. {
  1004. }
  1005. static u64 hfi1_sps_ints(void)
  1006. {
  1007. unsigned long flags;
  1008. struct hfi1_devdata *dd;
  1009. u64 sps_ints = 0;
  1010. spin_lock_irqsave(&hfi1_devs_lock, flags);
  1011. list_for_each_entry(dd, &hfi1_dev_list, list) {
  1012. sps_ints += get_all_cpu_total(dd->int_counter);
  1013. }
  1014. spin_unlock_irqrestore(&hfi1_devs_lock, flags);
  1015. return sps_ints;
  1016. }
  1017. static int _driver_stats_seq_show(struct seq_file *s, void *v)
  1018. {
  1019. loff_t *spos = v;
  1020. char *buffer;
  1021. u64 *stats = (u64 *)&hfi1_stats;
  1022. size_t sz = seq_get_buf(s, &buffer);
  1023. if (sz < sizeof(u64))
  1024. return SEQ_SKIP;
  1025. /* special case for interrupts */
  1026. if (*spos == 0)
  1027. *(u64 *)buffer = hfi1_sps_ints();
  1028. else
  1029. *(u64 *)buffer = stats[*spos];
  1030. seq_commit(s, sizeof(u64));
  1031. return 0;
  1032. }
  1033. DEBUGFS_SEQ_FILE_OPS(driver_stats);
  1034. DEBUGFS_SEQ_FILE_OPEN(driver_stats)
  1035. DEBUGFS_FILE_OPS(driver_stats);
  1036. void hfi1_dbg_init(void)
  1037. {
  1038. hfi1_dbg_root = debugfs_create_dir(DRIVER_NAME, NULL);
  1039. if (!hfi1_dbg_root)
  1040. pr_warn("init of debugfs failed\n");
  1041. DEBUGFS_SEQ_FILE_CREATE(driver_stats_names, hfi1_dbg_root, NULL);
  1042. DEBUGFS_SEQ_FILE_CREATE(driver_stats, hfi1_dbg_root, NULL);
  1043. }
  1044. void hfi1_dbg_exit(void)
  1045. {
  1046. debugfs_remove_recursive(hfi1_dbg_root);
  1047. hfi1_dbg_root = NULL;
  1048. }
  1049. #endif