debugfs.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589
  1. /*
  2. * Copyright(c) 2015-2018 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. ssize_t r;
  72. r = debugfs_file_get(d);
  73. if (unlikely(r))
  74. return r;
  75. r = seq_read(file, buf, size, ppos);
  76. debugfs_file_put(d);
  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. loff_t r;
  86. r = debugfs_file_get(d);
  87. if (unlikely(r))
  88. return r;
  89. r = seq_lseek(file, offset, whence);
  90. debugfs_file_put(d);
  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. for_each_possible_cpu(j) {
  1000. struct hfi1_opcode_stats_perctx *sp =
  1001. per_cpu_ptr(dd->tx_opstats, j);
  1002. n_packets += sp->stats[i].n_packets;
  1003. n_bytes += sp->stats[i].n_bytes;
  1004. }
  1005. if (!n_packets && !n_bytes)
  1006. return SEQ_SKIP;
  1007. if (!ibd->fault_opcode->n_rxfaults[i] &&
  1008. !ibd->fault_opcode->n_txfaults[i])
  1009. return SEQ_SKIP;
  1010. seq_printf(s, "%02llx %llu/%llu (faults rx:%llu faults: tx:%llu)\n", i,
  1011. (unsigned long long)n_packets,
  1012. (unsigned long long)n_bytes,
  1013. (unsigned long long)ibd->fault_opcode->n_rxfaults[i],
  1014. (unsigned long long)ibd->fault_opcode->n_txfaults[i]);
  1015. return 0;
  1016. }
  1017. DEBUGFS_SEQ_FILE_OPS(fault_stats);
  1018. DEBUGFS_SEQ_FILE_OPEN(fault_stats);
  1019. DEBUGFS_FILE_OPS(fault_stats);
  1020. static void fault_exit_opcode_debugfs(struct hfi1_ibdev *ibd)
  1021. {
  1022. if (ibd->fault_opcode)
  1023. debugfs_remove_recursive(ibd->fault_opcode->dir);
  1024. kfree(ibd->fault_opcode);
  1025. ibd->fault_opcode = NULL;
  1026. }
  1027. static int fault_init_opcode_debugfs(struct hfi1_ibdev *ibd)
  1028. {
  1029. struct dentry *parent = ibd->hfi1_ibdev_dbg;
  1030. ibd->fault_opcode = kzalloc(sizeof(*ibd->fault_opcode), GFP_KERNEL);
  1031. if (!ibd->fault_opcode)
  1032. return -ENOMEM;
  1033. ibd->fault_opcode->attr.interval = 1;
  1034. ibd->fault_opcode->attr.require_end = ULONG_MAX;
  1035. ibd->fault_opcode->attr.stacktrace_depth = 32;
  1036. ibd->fault_opcode->attr.dname = NULL;
  1037. ibd->fault_opcode->attr.verbose = 0;
  1038. ibd->fault_opcode->fault_by_opcode = false;
  1039. ibd->fault_opcode->opcode = 0;
  1040. ibd->fault_opcode->mask = 0xff;
  1041. ibd->fault_opcode->dir =
  1042. fault_create_debugfs_attr("fault_opcode",
  1043. parent,
  1044. &ibd->fault_opcode->attr);
  1045. if (IS_ERR(ibd->fault_opcode->dir)) {
  1046. kfree(ibd->fault_opcode);
  1047. ibd->fault_opcode = NULL;
  1048. return -ENOENT;
  1049. }
  1050. DEBUGFS_SEQ_FILE_CREATE(fault_stats, ibd->fault_opcode->dir, ibd);
  1051. if (!debugfs_create_bool("fault_by_opcode", 0600,
  1052. ibd->fault_opcode->dir,
  1053. &ibd->fault_opcode->fault_by_opcode))
  1054. goto fail;
  1055. if (!debugfs_create_x8("opcode", 0600, ibd->fault_opcode->dir,
  1056. &ibd->fault_opcode->opcode))
  1057. goto fail;
  1058. if (!debugfs_create_x8("mask", 0600, ibd->fault_opcode->dir,
  1059. &ibd->fault_opcode->mask))
  1060. goto fail;
  1061. return 0;
  1062. fail:
  1063. fault_exit_opcode_debugfs(ibd);
  1064. return -ENOMEM;
  1065. }
  1066. static void fault_exit_packet_debugfs(struct hfi1_ibdev *ibd)
  1067. {
  1068. if (ibd->fault_packet)
  1069. debugfs_remove_recursive(ibd->fault_packet->dir);
  1070. kfree(ibd->fault_packet);
  1071. ibd->fault_packet = NULL;
  1072. }
  1073. static int fault_init_packet_debugfs(struct hfi1_ibdev *ibd)
  1074. {
  1075. struct dentry *parent = ibd->hfi1_ibdev_dbg;
  1076. ibd->fault_packet = kzalloc(sizeof(*ibd->fault_packet), GFP_KERNEL);
  1077. if (!ibd->fault_packet)
  1078. return -ENOMEM;
  1079. ibd->fault_packet->attr.interval = 1;
  1080. ibd->fault_packet->attr.require_end = ULONG_MAX;
  1081. ibd->fault_packet->attr.stacktrace_depth = 32;
  1082. ibd->fault_packet->attr.dname = NULL;
  1083. ibd->fault_packet->attr.verbose = 0;
  1084. ibd->fault_packet->fault_by_packet = false;
  1085. ibd->fault_packet->dir =
  1086. fault_create_debugfs_attr("fault_packet",
  1087. parent,
  1088. &ibd->fault_opcode->attr);
  1089. if (IS_ERR(ibd->fault_packet->dir)) {
  1090. kfree(ibd->fault_packet);
  1091. ibd->fault_packet = NULL;
  1092. return -ENOENT;
  1093. }
  1094. if (!debugfs_create_bool("fault_by_packet", 0600,
  1095. ibd->fault_packet->dir,
  1096. &ibd->fault_packet->fault_by_packet))
  1097. goto fail;
  1098. if (!debugfs_create_u64("fault_stats", 0400,
  1099. ibd->fault_packet->dir,
  1100. &ibd->fault_packet->n_faults))
  1101. goto fail;
  1102. return 0;
  1103. fail:
  1104. fault_exit_packet_debugfs(ibd);
  1105. return -ENOMEM;
  1106. }
  1107. static void fault_exit_debugfs(struct hfi1_ibdev *ibd)
  1108. {
  1109. fault_exit_opcode_debugfs(ibd);
  1110. fault_exit_packet_debugfs(ibd);
  1111. }
  1112. static int fault_init_debugfs(struct hfi1_ibdev *ibd)
  1113. {
  1114. int ret = 0;
  1115. ret = fault_init_opcode_debugfs(ibd);
  1116. if (ret)
  1117. return ret;
  1118. ret = fault_init_packet_debugfs(ibd);
  1119. if (ret)
  1120. fault_exit_opcode_debugfs(ibd);
  1121. return ret;
  1122. }
  1123. bool hfi1_dbg_fault_suppress_err(struct hfi1_ibdev *ibd)
  1124. {
  1125. return ibd->fault_suppress_err;
  1126. }
  1127. bool hfi1_dbg_fault_opcode(struct rvt_qp *qp, u32 opcode, bool rx)
  1128. {
  1129. bool ret = false;
  1130. struct hfi1_ibdev *ibd = to_idev(qp->ibqp.device);
  1131. if (!ibd->fault_opcode || !ibd->fault_opcode->fault_by_opcode)
  1132. return false;
  1133. if (ibd->fault_opcode->opcode != (opcode & ibd->fault_opcode->mask))
  1134. return false;
  1135. ret = should_fail(&ibd->fault_opcode->attr, 1);
  1136. if (ret) {
  1137. trace_hfi1_fault_opcode(qp, opcode);
  1138. if (rx)
  1139. ibd->fault_opcode->n_rxfaults[opcode]++;
  1140. else
  1141. ibd->fault_opcode->n_txfaults[opcode]++;
  1142. }
  1143. return ret;
  1144. }
  1145. bool hfi1_dbg_fault_packet(struct hfi1_packet *packet)
  1146. {
  1147. struct rvt_dev_info *rdi = &packet->rcd->ppd->dd->verbs_dev.rdi;
  1148. struct hfi1_ibdev *ibd = dev_from_rdi(rdi);
  1149. bool ret = false;
  1150. if (!ibd->fault_packet || !ibd->fault_packet->fault_by_packet)
  1151. return false;
  1152. ret = should_fail(&ibd->fault_packet->attr, 1);
  1153. if (ret) {
  1154. ++ibd->fault_packet->n_faults;
  1155. trace_hfi1_fault_packet(packet);
  1156. }
  1157. return ret;
  1158. }
  1159. #endif
  1160. void hfi1_dbg_ibdev_init(struct hfi1_ibdev *ibd)
  1161. {
  1162. char name[sizeof("port0counters") + 1];
  1163. char link[10];
  1164. struct hfi1_devdata *dd = dd_from_dev(ibd);
  1165. struct hfi1_pportdata *ppd;
  1166. int unit = dd->unit;
  1167. int i, j;
  1168. if (!hfi1_dbg_root)
  1169. return;
  1170. snprintf(name, sizeof(name), "%s_%d", class_name(), unit);
  1171. snprintf(link, sizeof(link), "%d", unit);
  1172. ibd->hfi1_ibdev_dbg = debugfs_create_dir(name, hfi1_dbg_root);
  1173. if (!ibd->hfi1_ibdev_dbg) {
  1174. pr_warn("create of %s failed\n", name);
  1175. return;
  1176. }
  1177. ibd->hfi1_ibdev_link =
  1178. debugfs_create_symlink(link, hfi1_dbg_root, name);
  1179. if (!ibd->hfi1_ibdev_link) {
  1180. pr_warn("create of %s symlink failed\n", name);
  1181. return;
  1182. }
  1183. DEBUGFS_SEQ_FILE_CREATE(opcode_stats, ibd->hfi1_ibdev_dbg, ibd);
  1184. DEBUGFS_SEQ_FILE_CREATE(tx_opcode_stats, ibd->hfi1_ibdev_dbg, ibd);
  1185. DEBUGFS_SEQ_FILE_CREATE(ctx_stats, ibd->hfi1_ibdev_dbg, ibd);
  1186. DEBUGFS_SEQ_FILE_CREATE(qp_stats, ibd->hfi1_ibdev_dbg, ibd);
  1187. DEBUGFS_SEQ_FILE_CREATE(sdes, ibd->hfi1_ibdev_dbg, ibd);
  1188. DEBUGFS_SEQ_FILE_CREATE(rcds, ibd->hfi1_ibdev_dbg, ibd);
  1189. DEBUGFS_SEQ_FILE_CREATE(sdma_cpu_list, ibd->hfi1_ibdev_dbg, ibd);
  1190. /* dev counter files */
  1191. for (i = 0; i < ARRAY_SIZE(cntr_ops); i++)
  1192. DEBUGFS_FILE_CREATE(cntr_ops[i].name,
  1193. ibd->hfi1_ibdev_dbg,
  1194. dd,
  1195. &cntr_ops[i].ops, S_IRUGO);
  1196. /* per port files */
  1197. for (ppd = dd->pport, j = 0; j < dd->num_pports; j++, ppd++)
  1198. for (i = 0; i < ARRAY_SIZE(port_cntr_ops); i++) {
  1199. snprintf(name,
  1200. sizeof(name),
  1201. port_cntr_ops[i].name,
  1202. j + 1);
  1203. DEBUGFS_FILE_CREATE(name,
  1204. ibd->hfi1_ibdev_dbg,
  1205. ppd,
  1206. &port_cntr_ops[i].ops,
  1207. !port_cntr_ops[i].ops.write ?
  1208. S_IRUGO : S_IRUGO | S_IWUSR);
  1209. }
  1210. #ifdef CONFIG_FAULT_INJECTION
  1211. debugfs_create_bool("fault_suppress_err", 0600,
  1212. ibd->hfi1_ibdev_dbg,
  1213. &ibd->fault_suppress_err);
  1214. fault_init_debugfs(ibd);
  1215. #endif
  1216. }
  1217. void hfi1_dbg_ibdev_exit(struct hfi1_ibdev *ibd)
  1218. {
  1219. if (!hfi1_dbg_root)
  1220. goto out;
  1221. #ifdef CONFIG_FAULT_INJECTION
  1222. fault_exit_debugfs(ibd);
  1223. #endif
  1224. debugfs_remove(ibd->hfi1_ibdev_link);
  1225. debugfs_remove_recursive(ibd->hfi1_ibdev_dbg);
  1226. out:
  1227. ibd->hfi1_ibdev_dbg = NULL;
  1228. }
  1229. /*
  1230. * driver stats field names, one line per stat, single string. Used by
  1231. * programs like hfistats to print the stats in a way which works for
  1232. * different versions of drivers, without changing program source.
  1233. * if hfi1_ib_stats changes, this needs to change. Names need to be
  1234. * 12 chars or less (w/o newline), for proper display by hfistats utility.
  1235. */
  1236. static const char * const hfi1_statnames[] = {
  1237. /* must be element 0*/
  1238. "KernIntr",
  1239. "ErrorIntr",
  1240. "Tx_Errs",
  1241. "Rcv_Errs",
  1242. "H/W_Errs",
  1243. "NoPIOBufs",
  1244. "CtxtsOpen",
  1245. "RcvLen_Errs",
  1246. "EgrBufFull",
  1247. "EgrHdrFull"
  1248. };
  1249. static void *_driver_stats_names_seq_start(struct seq_file *s, loff_t *pos)
  1250. {
  1251. if (*pos >= ARRAY_SIZE(hfi1_statnames))
  1252. return NULL;
  1253. return pos;
  1254. }
  1255. static void *_driver_stats_names_seq_next(
  1256. struct seq_file *s,
  1257. void *v,
  1258. loff_t *pos)
  1259. {
  1260. ++*pos;
  1261. if (*pos >= ARRAY_SIZE(hfi1_statnames))
  1262. return NULL;
  1263. return pos;
  1264. }
  1265. static void _driver_stats_names_seq_stop(struct seq_file *s, void *v)
  1266. {
  1267. }
  1268. static int _driver_stats_names_seq_show(struct seq_file *s, void *v)
  1269. {
  1270. loff_t *spos = v;
  1271. seq_printf(s, "%s\n", hfi1_statnames[*spos]);
  1272. return 0;
  1273. }
  1274. DEBUGFS_SEQ_FILE_OPS(driver_stats_names);
  1275. DEBUGFS_SEQ_FILE_OPEN(driver_stats_names)
  1276. DEBUGFS_FILE_OPS(driver_stats_names);
  1277. static void *_driver_stats_seq_start(struct seq_file *s, loff_t *pos)
  1278. {
  1279. if (*pos >= ARRAY_SIZE(hfi1_statnames))
  1280. return NULL;
  1281. return pos;
  1282. }
  1283. static void *_driver_stats_seq_next(struct seq_file *s, void *v, loff_t *pos)
  1284. {
  1285. ++*pos;
  1286. if (*pos >= ARRAY_SIZE(hfi1_statnames))
  1287. return NULL;
  1288. return pos;
  1289. }
  1290. static void _driver_stats_seq_stop(struct seq_file *s, void *v)
  1291. {
  1292. }
  1293. static u64 hfi1_sps_ints(void)
  1294. {
  1295. unsigned long flags;
  1296. struct hfi1_devdata *dd;
  1297. u64 sps_ints = 0;
  1298. spin_lock_irqsave(&hfi1_devs_lock, flags);
  1299. list_for_each_entry(dd, &hfi1_dev_list, list) {
  1300. sps_ints += get_all_cpu_total(dd->int_counter);
  1301. }
  1302. spin_unlock_irqrestore(&hfi1_devs_lock, flags);
  1303. return sps_ints;
  1304. }
  1305. static int _driver_stats_seq_show(struct seq_file *s, void *v)
  1306. {
  1307. loff_t *spos = v;
  1308. char *buffer;
  1309. u64 *stats = (u64 *)&hfi1_stats;
  1310. size_t sz = seq_get_buf(s, &buffer);
  1311. if (sz < sizeof(u64))
  1312. return SEQ_SKIP;
  1313. /* special case for interrupts */
  1314. if (*spos == 0)
  1315. *(u64 *)buffer = hfi1_sps_ints();
  1316. else
  1317. *(u64 *)buffer = stats[*spos];
  1318. seq_commit(s, sizeof(u64));
  1319. return 0;
  1320. }
  1321. DEBUGFS_SEQ_FILE_OPS(driver_stats);
  1322. DEBUGFS_SEQ_FILE_OPEN(driver_stats)
  1323. DEBUGFS_FILE_OPS(driver_stats);
  1324. void hfi1_dbg_init(void)
  1325. {
  1326. hfi1_dbg_root = debugfs_create_dir(DRIVER_NAME, NULL);
  1327. if (!hfi1_dbg_root)
  1328. pr_warn("init of debugfs failed\n");
  1329. DEBUGFS_SEQ_FILE_CREATE(driver_stats_names, hfi1_dbg_root, NULL);
  1330. DEBUGFS_SEQ_FILE_CREATE(driver_stats, hfi1_dbg_root, NULL);
  1331. }
  1332. void hfi1_dbg_exit(void)
  1333. {
  1334. debugfs_remove_recursive(hfi1_dbg_root);
  1335. hfi1_dbg_root = NULL;
  1336. }