debugfs.c 26 KB

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