debugfs.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  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. /* read the dc8051 memory */
  457. static ssize_t dc8051_memory_read(struct file *file, char __user *buf,
  458. size_t count, loff_t *ppos)
  459. {
  460. struct hfi1_pportdata *ppd = private2ppd(file);
  461. ssize_t rval;
  462. void *tmp;
  463. loff_t start, end;
  464. /* the checks below expect the position to be positive */
  465. if (*ppos < 0)
  466. return -EINVAL;
  467. tmp = kzalloc(DC8051_DATA_MEM_SIZE, GFP_KERNEL);
  468. if (!tmp)
  469. return -ENOMEM;
  470. /*
  471. * Fill in the requested portion of the temporary buffer from the
  472. * 8051 memory. The 8051 memory read is done in terms of 8 bytes.
  473. * Adjust start and end to fit. Skip reading anything if out of
  474. * range.
  475. */
  476. start = *ppos & ~0x7; /* round down */
  477. if (start < DC8051_DATA_MEM_SIZE) {
  478. end = (*ppos + count + 7) & ~0x7; /* round up */
  479. if (end > DC8051_DATA_MEM_SIZE)
  480. end = DC8051_DATA_MEM_SIZE;
  481. rval = read_8051_data(ppd->dd, start, end - start,
  482. (u64 *)(tmp + start));
  483. if (rval)
  484. goto done;
  485. }
  486. rval = simple_read_from_buffer(buf, count, ppos, tmp,
  487. DC8051_DATA_MEM_SIZE);
  488. done:
  489. kfree(tmp);
  490. return rval;
  491. }
  492. static ssize_t debugfs_lcb_read(struct file *file, char __user *buf,
  493. size_t count, loff_t *ppos)
  494. {
  495. struct hfi1_pportdata *ppd = private2ppd(file);
  496. struct hfi1_devdata *dd = ppd->dd;
  497. unsigned long total, csr_off;
  498. u64 data;
  499. if (*ppos < 0)
  500. return -EINVAL;
  501. /* only read 8 byte quantities */
  502. if ((count % 8) != 0)
  503. return -EINVAL;
  504. /* offset must be 8-byte aligned */
  505. if ((*ppos % 8) != 0)
  506. return -EINVAL;
  507. /* do nothing if out of range or zero count */
  508. if (*ppos >= (LCB_END - LCB_START) || !count)
  509. return 0;
  510. /* reduce count if needed */
  511. if (*ppos + count > LCB_END - LCB_START)
  512. count = (LCB_END - LCB_START) - *ppos;
  513. csr_off = LCB_START + *ppos;
  514. for (total = 0; total < count; total += 8, csr_off += 8) {
  515. if (read_lcb_csr(dd, csr_off, (u64 *)&data))
  516. break; /* failed */
  517. if (put_user(data, (unsigned long __user *)(buf + total)))
  518. break;
  519. }
  520. *ppos += total;
  521. return total;
  522. }
  523. static ssize_t debugfs_lcb_write(struct file *file, const char __user *buf,
  524. size_t count, loff_t *ppos)
  525. {
  526. struct hfi1_pportdata *ppd = private2ppd(file);
  527. struct hfi1_devdata *dd = ppd->dd;
  528. unsigned long total, csr_off, data;
  529. if (*ppos < 0)
  530. return -EINVAL;
  531. /* only write 8 byte quantities */
  532. if ((count % 8) != 0)
  533. return -EINVAL;
  534. /* offset must be 8-byte aligned */
  535. if ((*ppos % 8) != 0)
  536. return -EINVAL;
  537. /* do nothing if out of range or zero count */
  538. if (*ppos >= (LCB_END - LCB_START) || !count)
  539. return 0;
  540. /* reduce count if needed */
  541. if (*ppos + count > LCB_END - LCB_START)
  542. count = (LCB_END - LCB_START) - *ppos;
  543. csr_off = LCB_START + *ppos;
  544. for (total = 0; total < count; total += 8, csr_off += 8) {
  545. if (get_user(data, (unsigned long __user *)(buf + total)))
  546. break;
  547. if (write_lcb_csr(dd, csr_off, data))
  548. break; /* failed */
  549. }
  550. *ppos += total;
  551. return total;
  552. }
  553. /*
  554. * read the per-port QSFP data for ppd
  555. */
  556. static ssize_t qsfp_debugfs_dump(struct file *file, char __user *buf,
  557. size_t count, loff_t *ppos)
  558. {
  559. struct hfi1_pportdata *ppd;
  560. char *tmp;
  561. int ret;
  562. ppd = private2ppd(file);
  563. tmp = kmalloc(PAGE_SIZE, GFP_KERNEL);
  564. if (!tmp)
  565. return -ENOMEM;
  566. ret = qsfp_dump(ppd, tmp, PAGE_SIZE);
  567. if (ret > 0)
  568. ret = simple_read_from_buffer(buf, count, ppos, tmp, ret);
  569. kfree(tmp);
  570. return ret;
  571. }
  572. /* Do an i2c write operation on the chain for the given HFI. */
  573. static ssize_t __i2c_debugfs_write(struct file *file, const char __user *buf,
  574. size_t count, loff_t *ppos, u32 target)
  575. {
  576. struct hfi1_pportdata *ppd;
  577. char *buff;
  578. int ret;
  579. int i2c_addr;
  580. int offset;
  581. int total_written;
  582. ppd = private2ppd(file);
  583. /* byte offset format: [offsetSize][i2cAddr][offsetHigh][offsetLow] */
  584. i2c_addr = (*ppos >> 16) & 0xffff;
  585. offset = *ppos & 0xffff;
  586. /* explicitly reject invalid address 0 to catch cp and cat */
  587. if (i2c_addr == 0)
  588. return -EINVAL;
  589. buff = kmalloc(count, GFP_KERNEL);
  590. if (!buff)
  591. return -ENOMEM;
  592. ret = copy_from_user(buff, buf, count);
  593. if (ret > 0) {
  594. ret = -EFAULT;
  595. goto _free;
  596. }
  597. total_written = i2c_write(ppd, target, i2c_addr, offset, buff, count);
  598. if (total_written < 0) {
  599. ret = total_written;
  600. goto _free;
  601. }
  602. *ppos += total_written;
  603. ret = total_written;
  604. _free:
  605. kfree(buff);
  606. return ret;
  607. }
  608. /* Do an i2c write operation on chain for HFI 0. */
  609. static ssize_t i2c1_debugfs_write(struct file *file, const char __user *buf,
  610. size_t count, loff_t *ppos)
  611. {
  612. return __i2c_debugfs_write(file, buf, count, ppos, 0);
  613. }
  614. /* Do an i2c write operation on chain for HFI 1. */
  615. static ssize_t i2c2_debugfs_write(struct file *file, const char __user *buf,
  616. size_t count, loff_t *ppos)
  617. {
  618. return __i2c_debugfs_write(file, buf, count, ppos, 1);
  619. }
  620. /* Do an i2c read operation on the chain for the given HFI. */
  621. static ssize_t __i2c_debugfs_read(struct file *file, char __user *buf,
  622. size_t count, loff_t *ppos, u32 target)
  623. {
  624. struct hfi1_pportdata *ppd;
  625. char *buff;
  626. int ret;
  627. int i2c_addr;
  628. int offset;
  629. int total_read;
  630. ppd = private2ppd(file);
  631. /* byte offset format: [offsetSize][i2cAddr][offsetHigh][offsetLow] */
  632. i2c_addr = (*ppos >> 16) & 0xffff;
  633. offset = *ppos & 0xffff;
  634. /* explicitly reject invalid address 0 to catch cp and cat */
  635. if (i2c_addr == 0)
  636. return -EINVAL;
  637. buff = kmalloc(count, GFP_KERNEL);
  638. if (!buff)
  639. return -ENOMEM;
  640. total_read = i2c_read(ppd, target, i2c_addr, offset, buff, count);
  641. if (total_read < 0) {
  642. ret = total_read;
  643. goto _free;
  644. }
  645. *ppos += total_read;
  646. ret = copy_to_user(buf, buff, total_read);
  647. if (ret > 0) {
  648. ret = -EFAULT;
  649. goto _free;
  650. }
  651. ret = total_read;
  652. _free:
  653. kfree(buff);
  654. return ret;
  655. }
  656. /* Do an i2c read operation on chain for HFI 0. */
  657. static ssize_t i2c1_debugfs_read(struct file *file, char __user *buf,
  658. size_t count, loff_t *ppos)
  659. {
  660. return __i2c_debugfs_read(file, buf, count, ppos, 0);
  661. }
  662. /* Do an i2c read operation on chain for HFI 1. */
  663. static ssize_t i2c2_debugfs_read(struct file *file, char __user *buf,
  664. size_t count, loff_t *ppos)
  665. {
  666. return __i2c_debugfs_read(file, buf, count, ppos, 1);
  667. }
  668. /* Do a QSFP write operation on the i2c chain for the given HFI. */
  669. static ssize_t __qsfp_debugfs_write(struct file *file, const char __user *buf,
  670. size_t count, loff_t *ppos, u32 target)
  671. {
  672. struct hfi1_pportdata *ppd;
  673. char *buff;
  674. int ret;
  675. int total_written;
  676. if (*ppos + count > QSFP_PAGESIZE * 4) /* base page + page00-page03 */
  677. return -EINVAL;
  678. ppd = private2ppd(file);
  679. buff = kmalloc(count, GFP_KERNEL);
  680. if (!buff)
  681. return -ENOMEM;
  682. ret = copy_from_user(buff, buf, count);
  683. if (ret > 0) {
  684. ret = -EFAULT;
  685. goto _free;
  686. }
  687. total_written = qsfp_write(ppd, target, *ppos, buff, count);
  688. if (total_written < 0) {
  689. ret = total_written;
  690. goto _free;
  691. }
  692. *ppos += total_written;
  693. ret = total_written;
  694. _free:
  695. kfree(buff);
  696. return ret;
  697. }
  698. /* Do a QSFP write operation on i2c chain for HFI 0. */
  699. static ssize_t qsfp1_debugfs_write(struct file *file, const char __user *buf,
  700. size_t count, loff_t *ppos)
  701. {
  702. return __qsfp_debugfs_write(file, buf, count, ppos, 0);
  703. }
  704. /* Do a QSFP write operation on i2c chain for HFI 1. */
  705. static ssize_t qsfp2_debugfs_write(struct file *file, const char __user *buf,
  706. size_t count, loff_t *ppos)
  707. {
  708. return __qsfp_debugfs_write(file, buf, count, ppos, 1);
  709. }
  710. /* Do a QSFP read operation on the i2c chain for the given HFI. */
  711. static ssize_t __qsfp_debugfs_read(struct file *file, char __user *buf,
  712. size_t count, loff_t *ppos, u32 target)
  713. {
  714. struct hfi1_pportdata *ppd;
  715. char *buff;
  716. int ret;
  717. int total_read;
  718. if (*ppos + count > QSFP_PAGESIZE * 4) { /* base page + page00-page03 */
  719. ret = -EINVAL;
  720. goto _return;
  721. }
  722. ppd = private2ppd(file);
  723. buff = kmalloc(count, GFP_KERNEL);
  724. if (!buff) {
  725. ret = -ENOMEM;
  726. goto _return;
  727. }
  728. total_read = qsfp_read(ppd, target, *ppos, buff, count);
  729. if (total_read < 0) {
  730. ret = total_read;
  731. goto _free;
  732. }
  733. *ppos += total_read;
  734. ret = copy_to_user(buf, buff, total_read);
  735. if (ret > 0) {
  736. ret = -EFAULT;
  737. goto _free;
  738. }
  739. ret = total_read;
  740. _free:
  741. kfree(buff);
  742. _return:
  743. return ret;
  744. }
  745. /* Do a QSFP read operation on i2c chain for HFI 0. */
  746. static ssize_t qsfp1_debugfs_read(struct file *file, char __user *buf,
  747. size_t count, loff_t *ppos)
  748. {
  749. return __qsfp_debugfs_read(file, buf, count, ppos, 0);
  750. }
  751. /* Do a QSFP read operation on i2c chain for HFI 1. */
  752. static ssize_t qsfp2_debugfs_read(struct file *file, char __user *buf,
  753. size_t count, loff_t *ppos)
  754. {
  755. return __qsfp_debugfs_read(file, buf, count, ppos, 1);
  756. }
  757. static int __i2c_debugfs_open(struct inode *in, struct file *fp, u32 target)
  758. {
  759. struct hfi1_pportdata *ppd;
  760. int ret;
  761. if (!try_module_get(THIS_MODULE))
  762. return -ENODEV;
  763. ppd = private2ppd(fp);
  764. ret = acquire_chip_resource(ppd->dd, i2c_target(target), 0);
  765. if (ret) /* failed - release the module */
  766. module_put(THIS_MODULE);
  767. return ret;
  768. }
  769. static int i2c1_debugfs_open(struct inode *in, struct file *fp)
  770. {
  771. return __i2c_debugfs_open(in, fp, 0);
  772. }
  773. static int i2c2_debugfs_open(struct inode *in, struct file *fp)
  774. {
  775. return __i2c_debugfs_open(in, fp, 1);
  776. }
  777. static int __i2c_debugfs_release(struct inode *in, struct file *fp, u32 target)
  778. {
  779. struct hfi1_pportdata *ppd;
  780. ppd = private2ppd(fp);
  781. release_chip_resource(ppd->dd, i2c_target(target));
  782. module_put(THIS_MODULE);
  783. return 0;
  784. }
  785. static int i2c1_debugfs_release(struct inode *in, struct file *fp)
  786. {
  787. return __i2c_debugfs_release(in, fp, 0);
  788. }
  789. static int i2c2_debugfs_release(struct inode *in, struct file *fp)
  790. {
  791. return __i2c_debugfs_release(in, fp, 1);
  792. }
  793. static int __qsfp_debugfs_open(struct inode *in, struct file *fp, u32 target)
  794. {
  795. struct hfi1_pportdata *ppd;
  796. int ret;
  797. if (!try_module_get(THIS_MODULE))
  798. return -ENODEV;
  799. ppd = private2ppd(fp);
  800. ret = acquire_chip_resource(ppd->dd, i2c_target(target), 0);
  801. if (ret) /* failed - release the module */
  802. module_put(THIS_MODULE);
  803. return ret;
  804. }
  805. static int qsfp1_debugfs_open(struct inode *in, struct file *fp)
  806. {
  807. return __qsfp_debugfs_open(in, fp, 0);
  808. }
  809. static int qsfp2_debugfs_open(struct inode *in, struct file *fp)
  810. {
  811. return __qsfp_debugfs_open(in, fp, 1);
  812. }
  813. static int __qsfp_debugfs_release(struct inode *in, struct file *fp, u32 target)
  814. {
  815. struct hfi1_pportdata *ppd;
  816. ppd = private2ppd(fp);
  817. release_chip_resource(ppd->dd, i2c_target(target));
  818. module_put(THIS_MODULE);
  819. return 0;
  820. }
  821. static int qsfp1_debugfs_release(struct inode *in, struct file *fp)
  822. {
  823. return __qsfp_debugfs_release(in, fp, 0);
  824. }
  825. static int qsfp2_debugfs_release(struct inode *in, struct file *fp)
  826. {
  827. return __qsfp_debugfs_release(in, fp, 1);
  828. }
  829. #define DEBUGFS_OPS(nm, readroutine, writeroutine) \
  830. { \
  831. .name = nm, \
  832. .ops = { \
  833. .read = readroutine, \
  834. .write = writeroutine, \
  835. .llseek = generic_file_llseek, \
  836. }, \
  837. }
  838. #define DEBUGFS_XOPS(nm, readf, writef, openf, releasef) \
  839. { \
  840. .name = nm, \
  841. .ops = { \
  842. .read = readf, \
  843. .write = writef, \
  844. .llseek = generic_file_llseek, \
  845. .open = openf, \
  846. .release = releasef \
  847. }, \
  848. }
  849. static const struct counter_info cntr_ops[] = {
  850. DEBUGFS_OPS("counter_names", dev_names_read, NULL),
  851. DEBUGFS_OPS("counters", dev_counters_read, NULL),
  852. DEBUGFS_OPS("portcounter_names", portnames_read, NULL),
  853. };
  854. static const struct counter_info port_cntr_ops[] = {
  855. DEBUGFS_OPS("port%dcounters", portcntrs_debugfs_read, NULL),
  856. DEBUGFS_XOPS("i2c1", i2c1_debugfs_read, i2c1_debugfs_write,
  857. i2c1_debugfs_open, i2c1_debugfs_release),
  858. DEBUGFS_XOPS("i2c2", i2c2_debugfs_read, i2c2_debugfs_write,
  859. i2c2_debugfs_open, i2c2_debugfs_release),
  860. DEBUGFS_OPS("qsfp_dump%d", qsfp_debugfs_dump, NULL),
  861. DEBUGFS_XOPS("qsfp1", qsfp1_debugfs_read, qsfp1_debugfs_write,
  862. qsfp1_debugfs_open, qsfp1_debugfs_release),
  863. DEBUGFS_XOPS("qsfp2", qsfp2_debugfs_read, qsfp2_debugfs_write,
  864. qsfp2_debugfs_open, qsfp2_debugfs_release),
  865. DEBUGFS_OPS("asic_flags", asic_flags_read, asic_flags_write),
  866. DEBUGFS_OPS("dc8051_memory", dc8051_memory_read, NULL),
  867. DEBUGFS_OPS("lcb", debugfs_lcb_read, debugfs_lcb_write),
  868. };
  869. static void *_sdma_cpu_list_seq_start(struct seq_file *s, loff_t *pos)
  870. {
  871. if (*pos >= num_online_cpus())
  872. return NULL;
  873. return pos;
  874. }
  875. static void *_sdma_cpu_list_seq_next(struct seq_file *s, void *v, loff_t *pos)
  876. {
  877. ++*pos;
  878. if (*pos >= num_online_cpus())
  879. return NULL;
  880. return pos;
  881. }
  882. static void _sdma_cpu_list_seq_stop(struct seq_file *s, void *v)
  883. {
  884. /* nothing allocated */
  885. }
  886. static int _sdma_cpu_list_seq_show(struct seq_file *s, void *v)
  887. {
  888. struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
  889. struct hfi1_devdata *dd = dd_from_dev(ibd);
  890. loff_t *spos = v;
  891. loff_t i = *spos;
  892. sdma_seqfile_dump_cpu_list(s, dd, (unsigned long)i);
  893. return 0;
  894. }
  895. DEBUGFS_SEQ_FILE_OPS(sdma_cpu_list);
  896. DEBUGFS_SEQ_FILE_OPEN(sdma_cpu_list)
  897. DEBUGFS_FILE_OPS(sdma_cpu_list);
  898. void hfi1_dbg_ibdev_init(struct hfi1_ibdev *ibd)
  899. {
  900. char name[sizeof("port0counters") + 1];
  901. char link[10];
  902. struct hfi1_devdata *dd = dd_from_dev(ibd);
  903. struct hfi1_pportdata *ppd;
  904. int unit = dd->unit;
  905. int i, j;
  906. if (!hfi1_dbg_root)
  907. return;
  908. snprintf(name, sizeof(name), "%s_%d", class_name(), unit);
  909. snprintf(link, sizeof(link), "%d", unit);
  910. ibd->hfi1_ibdev_dbg = debugfs_create_dir(name, hfi1_dbg_root);
  911. if (!ibd->hfi1_ibdev_dbg) {
  912. pr_warn("create of %s failed\n", name);
  913. return;
  914. }
  915. ibd->hfi1_ibdev_link =
  916. debugfs_create_symlink(link, hfi1_dbg_root, name);
  917. if (!ibd->hfi1_ibdev_link) {
  918. pr_warn("create of %s symlink failed\n", name);
  919. return;
  920. }
  921. DEBUGFS_SEQ_FILE_CREATE(opcode_stats, ibd->hfi1_ibdev_dbg, ibd);
  922. DEBUGFS_SEQ_FILE_CREATE(ctx_stats, ibd->hfi1_ibdev_dbg, ibd);
  923. DEBUGFS_SEQ_FILE_CREATE(qp_stats, ibd->hfi1_ibdev_dbg, ibd);
  924. DEBUGFS_SEQ_FILE_CREATE(sdes, ibd->hfi1_ibdev_dbg, ibd);
  925. DEBUGFS_SEQ_FILE_CREATE(sdma_cpu_list, ibd->hfi1_ibdev_dbg, ibd);
  926. /* dev counter files */
  927. for (i = 0; i < ARRAY_SIZE(cntr_ops); i++)
  928. DEBUGFS_FILE_CREATE(cntr_ops[i].name,
  929. ibd->hfi1_ibdev_dbg,
  930. dd,
  931. &cntr_ops[i].ops, S_IRUGO);
  932. /* per port files */
  933. for (ppd = dd->pport, j = 0; j < dd->num_pports; j++, ppd++)
  934. for (i = 0; i < ARRAY_SIZE(port_cntr_ops); i++) {
  935. snprintf(name,
  936. sizeof(name),
  937. port_cntr_ops[i].name,
  938. j + 1);
  939. DEBUGFS_FILE_CREATE(name,
  940. ibd->hfi1_ibdev_dbg,
  941. ppd,
  942. &port_cntr_ops[i].ops,
  943. !port_cntr_ops[i].ops.write ?
  944. S_IRUGO : S_IRUGO | S_IWUSR);
  945. }
  946. }
  947. void hfi1_dbg_ibdev_exit(struct hfi1_ibdev *ibd)
  948. {
  949. if (!hfi1_dbg_root)
  950. goto out;
  951. debugfs_remove(ibd->hfi1_ibdev_link);
  952. debugfs_remove_recursive(ibd->hfi1_ibdev_dbg);
  953. out:
  954. ibd->hfi1_ibdev_dbg = NULL;
  955. }
  956. /*
  957. * driver stats field names, one line per stat, single string. Used by
  958. * programs like hfistats to print the stats in a way which works for
  959. * different versions of drivers, without changing program source.
  960. * if hfi1_ib_stats changes, this needs to change. Names need to be
  961. * 12 chars or less (w/o newline), for proper display by hfistats utility.
  962. */
  963. static const char * const hfi1_statnames[] = {
  964. /* must be element 0*/
  965. "KernIntr",
  966. "ErrorIntr",
  967. "Tx_Errs",
  968. "Rcv_Errs",
  969. "H/W_Errs",
  970. "NoPIOBufs",
  971. "CtxtsOpen",
  972. "RcvLen_Errs",
  973. "EgrBufFull",
  974. "EgrHdrFull"
  975. };
  976. static void *_driver_stats_names_seq_start(struct seq_file *s, loff_t *pos)
  977. {
  978. if (*pos >= ARRAY_SIZE(hfi1_statnames))
  979. return NULL;
  980. return pos;
  981. }
  982. static void *_driver_stats_names_seq_next(
  983. struct seq_file *s,
  984. void *v,
  985. loff_t *pos)
  986. {
  987. ++*pos;
  988. if (*pos >= ARRAY_SIZE(hfi1_statnames))
  989. return NULL;
  990. return pos;
  991. }
  992. static void _driver_stats_names_seq_stop(struct seq_file *s, void *v)
  993. {
  994. }
  995. static int _driver_stats_names_seq_show(struct seq_file *s, void *v)
  996. {
  997. loff_t *spos = v;
  998. seq_printf(s, "%s\n", hfi1_statnames[*spos]);
  999. return 0;
  1000. }
  1001. DEBUGFS_SEQ_FILE_OPS(driver_stats_names);
  1002. DEBUGFS_SEQ_FILE_OPEN(driver_stats_names)
  1003. DEBUGFS_FILE_OPS(driver_stats_names);
  1004. static void *_driver_stats_seq_start(struct seq_file *s, loff_t *pos)
  1005. {
  1006. if (*pos >= ARRAY_SIZE(hfi1_statnames))
  1007. return NULL;
  1008. return pos;
  1009. }
  1010. static void *_driver_stats_seq_next(struct seq_file *s, void *v, loff_t *pos)
  1011. {
  1012. ++*pos;
  1013. if (*pos >= ARRAY_SIZE(hfi1_statnames))
  1014. return NULL;
  1015. return pos;
  1016. }
  1017. static void _driver_stats_seq_stop(struct seq_file *s, void *v)
  1018. {
  1019. }
  1020. static u64 hfi1_sps_ints(void)
  1021. {
  1022. unsigned long flags;
  1023. struct hfi1_devdata *dd;
  1024. u64 sps_ints = 0;
  1025. spin_lock_irqsave(&hfi1_devs_lock, flags);
  1026. list_for_each_entry(dd, &hfi1_dev_list, list) {
  1027. sps_ints += get_all_cpu_total(dd->int_counter);
  1028. }
  1029. spin_unlock_irqrestore(&hfi1_devs_lock, flags);
  1030. return sps_ints;
  1031. }
  1032. static int _driver_stats_seq_show(struct seq_file *s, void *v)
  1033. {
  1034. loff_t *spos = v;
  1035. char *buffer;
  1036. u64 *stats = (u64 *)&hfi1_stats;
  1037. size_t sz = seq_get_buf(s, &buffer);
  1038. if (sz < sizeof(u64))
  1039. return SEQ_SKIP;
  1040. /* special case for interrupts */
  1041. if (*spos == 0)
  1042. *(u64 *)buffer = hfi1_sps_ints();
  1043. else
  1044. *(u64 *)buffer = stats[*spos];
  1045. seq_commit(s, sizeof(u64));
  1046. return 0;
  1047. }
  1048. DEBUGFS_SEQ_FILE_OPS(driver_stats);
  1049. DEBUGFS_SEQ_FILE_OPEN(driver_stats)
  1050. DEBUGFS_FILE_OPS(driver_stats);
  1051. void hfi1_dbg_init(void)
  1052. {
  1053. hfi1_dbg_root = debugfs_create_dir(DRIVER_NAME, NULL);
  1054. if (!hfi1_dbg_root)
  1055. pr_warn("init of debugfs failed\n");
  1056. DEBUGFS_SEQ_FILE_CREATE(driver_stats_names, hfi1_dbg_root, NULL);
  1057. DEBUGFS_SEQ_FILE_CREATE(driver_stats, hfi1_dbg_root, NULL);
  1058. }
  1059. void hfi1_dbg_exit(void)
  1060. {
  1061. debugfs_remove_recursive(hfi1_dbg_root);
  1062. hfi1_dbg_root = NULL;
  1063. }
  1064. #endif