debugfs.c 38 KB

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