debugfs.c 27 KB

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