ipath_diag.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. /*
  2. * Copyright (c) 2006, 2007 QLogic Corporation. All rights reserved.
  3. * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. /*
  34. * This file contains support for diagnostic functions. It is accessed by
  35. * opening the ipath_diag device, normally minor number 129. Diagnostic use
  36. * of the InfiniPath chip may render the chip or board unusable until the
  37. * driver is unloaded, or in some cases, until the system is rebooted.
  38. *
  39. * Accesses to the chip through this interface are not similar to going
  40. * through the /sys/bus/pci resource mmap interface.
  41. */
  42. #include <linux/io.h>
  43. #include <linux/pci.h>
  44. #include <linux/vmalloc.h>
  45. #include <asm/uaccess.h>
  46. #include "ipath_kernel.h"
  47. #include "ipath_common.h"
  48. int ipath_diag_inuse;
  49. static int diag_set_link;
  50. static int ipath_diag_open(struct inode *in, struct file *fp);
  51. static int ipath_diag_release(struct inode *in, struct file *fp);
  52. static ssize_t ipath_diag_read(struct file *fp, char __user *data,
  53. size_t count, loff_t *off);
  54. static ssize_t ipath_diag_write(struct file *fp, const char __user *data,
  55. size_t count, loff_t *off);
  56. static const struct file_operations diag_file_ops = {
  57. .owner = THIS_MODULE,
  58. .write = ipath_diag_write,
  59. .read = ipath_diag_read,
  60. .open = ipath_diag_open,
  61. .release = ipath_diag_release
  62. };
  63. static ssize_t ipath_diagpkt_write(struct file *fp,
  64. const char __user *data,
  65. size_t count, loff_t *off);
  66. static const struct file_operations diagpkt_file_ops = {
  67. .owner = THIS_MODULE,
  68. .write = ipath_diagpkt_write,
  69. };
  70. static atomic_t diagpkt_count = ATOMIC_INIT(0);
  71. static struct cdev *diagpkt_cdev;
  72. static struct class_device *diagpkt_class_dev;
  73. int ipath_diag_add(struct ipath_devdata *dd)
  74. {
  75. char name[16];
  76. int ret = 0;
  77. if (atomic_inc_return(&diagpkt_count) == 1) {
  78. ret = ipath_cdev_init(IPATH_DIAGPKT_MINOR,
  79. "ipath_diagpkt", &diagpkt_file_ops,
  80. &diagpkt_cdev, &diagpkt_class_dev);
  81. if (ret) {
  82. ipath_dev_err(dd, "Couldn't create ipath_diagpkt "
  83. "device: %d", ret);
  84. goto done;
  85. }
  86. }
  87. snprintf(name, sizeof(name), "ipath_diag%d", dd->ipath_unit);
  88. ret = ipath_cdev_init(IPATH_DIAG_MINOR_BASE + dd->ipath_unit, name,
  89. &diag_file_ops, &dd->diag_cdev,
  90. &dd->diag_class_dev);
  91. if (ret)
  92. ipath_dev_err(dd, "Couldn't create %s device: %d",
  93. name, ret);
  94. done:
  95. return ret;
  96. }
  97. void ipath_diag_remove(struct ipath_devdata *dd)
  98. {
  99. if (atomic_dec_and_test(&diagpkt_count))
  100. ipath_cdev_cleanup(&diagpkt_cdev, &diagpkt_class_dev);
  101. ipath_cdev_cleanup(&dd->diag_cdev, &dd->diag_class_dev);
  102. }
  103. /**
  104. * ipath_read_umem64 - read a 64-bit quantity from the chip into user space
  105. * @dd: the infinipath device
  106. * @uaddr: the location to store the data in user memory
  107. * @caddr: the source chip address (full pointer, not offset)
  108. * @count: number of bytes to copy (multiple of 32 bits)
  109. *
  110. * This function also localizes all chip memory accesses.
  111. * The copy should be written such that we read full cacheline packets
  112. * from the chip. This is usually used for a single qword
  113. *
  114. * NOTE: This assumes the chip address is 64-bit aligned.
  115. */
  116. static int ipath_read_umem64(struct ipath_devdata *dd, void __user *uaddr,
  117. const void __iomem *caddr, size_t count)
  118. {
  119. const u64 __iomem *reg_addr = caddr;
  120. const u64 __iomem *reg_end = reg_addr + (count / sizeof(u64));
  121. int ret;
  122. /* not very efficient, but it works for now */
  123. if (reg_addr < dd->ipath_kregbase || reg_end > dd->ipath_kregend) {
  124. ret = -EINVAL;
  125. goto bail;
  126. }
  127. while (reg_addr < reg_end) {
  128. u64 data = readq(reg_addr);
  129. if (copy_to_user(uaddr, &data, sizeof(u64))) {
  130. ret = -EFAULT;
  131. goto bail;
  132. }
  133. reg_addr++;
  134. uaddr += sizeof(u64);
  135. }
  136. ret = 0;
  137. bail:
  138. return ret;
  139. }
  140. /**
  141. * ipath_write_umem64 - write a 64-bit quantity to the chip from user space
  142. * @dd: the infinipath device
  143. * @caddr: the destination chip address (full pointer, not offset)
  144. * @uaddr: the source of the data in user memory
  145. * @count: the number of bytes to copy (multiple of 32 bits)
  146. *
  147. * This is usually used for a single qword
  148. * NOTE: This assumes the chip address is 64-bit aligned.
  149. */
  150. static int ipath_write_umem64(struct ipath_devdata *dd, void __iomem *caddr,
  151. const void __user *uaddr, size_t count)
  152. {
  153. u64 __iomem *reg_addr = caddr;
  154. const u64 __iomem *reg_end = reg_addr + (count / sizeof(u64));
  155. int ret;
  156. /* not very efficient, but it works for now */
  157. if (reg_addr < dd->ipath_kregbase || reg_end > dd->ipath_kregend) {
  158. ret = -EINVAL;
  159. goto bail;
  160. }
  161. while (reg_addr < reg_end) {
  162. u64 data;
  163. if (copy_from_user(&data, uaddr, sizeof(data))) {
  164. ret = -EFAULT;
  165. goto bail;
  166. }
  167. writeq(data, reg_addr);
  168. reg_addr++;
  169. uaddr += sizeof(u64);
  170. }
  171. ret = 0;
  172. bail:
  173. return ret;
  174. }
  175. /**
  176. * ipath_read_umem32 - read a 32-bit quantity from the chip into user space
  177. * @dd: the infinipath device
  178. * @uaddr: the location to store the data in user memory
  179. * @caddr: the source chip address (full pointer, not offset)
  180. * @count: number of bytes to copy
  181. *
  182. * read 32 bit values, not 64 bit; for memories that only
  183. * support 32 bit reads; usually a single dword.
  184. */
  185. static int ipath_read_umem32(struct ipath_devdata *dd, void __user *uaddr,
  186. const void __iomem *caddr, size_t count)
  187. {
  188. const u32 __iomem *reg_addr = caddr;
  189. const u32 __iomem *reg_end = reg_addr + (count / sizeof(u32));
  190. int ret;
  191. if (reg_addr < (u32 __iomem *) dd->ipath_kregbase ||
  192. reg_end > (u32 __iomem *) dd->ipath_kregend) {
  193. ret = -EINVAL;
  194. goto bail;
  195. }
  196. /* not very efficient, but it works for now */
  197. while (reg_addr < reg_end) {
  198. u32 data = readl(reg_addr);
  199. if (copy_to_user(uaddr, &data, sizeof(data))) {
  200. ret = -EFAULT;
  201. goto bail;
  202. }
  203. reg_addr++;
  204. uaddr += sizeof(u32);
  205. }
  206. ret = 0;
  207. bail:
  208. return ret;
  209. }
  210. /**
  211. * ipath_write_umem32 - write a 32-bit quantity to the chip from user space
  212. * @dd: the infinipath device
  213. * @caddr: the destination chip address (full pointer, not offset)
  214. * @uaddr: the source of the data in user memory
  215. * @count: number of bytes to copy
  216. *
  217. * write 32 bit values, not 64 bit; for memories that only
  218. * support 32 bit write; usually a single dword.
  219. */
  220. static int ipath_write_umem32(struct ipath_devdata *dd, void __iomem *caddr,
  221. const void __user *uaddr, size_t count)
  222. {
  223. u32 __iomem *reg_addr = caddr;
  224. const u32 __iomem *reg_end = reg_addr + (count / sizeof(u32));
  225. int ret;
  226. if (reg_addr < (u32 __iomem *) dd->ipath_kregbase ||
  227. reg_end > (u32 __iomem *) dd->ipath_kregend) {
  228. ret = -EINVAL;
  229. goto bail;
  230. }
  231. while (reg_addr < reg_end) {
  232. u32 data;
  233. if (copy_from_user(&data, uaddr, sizeof(data))) {
  234. ret = -EFAULT;
  235. goto bail;
  236. }
  237. writel(data, reg_addr);
  238. reg_addr++;
  239. uaddr += sizeof(u32);
  240. }
  241. ret = 0;
  242. bail:
  243. return ret;
  244. }
  245. static int ipath_diag_open(struct inode *in, struct file *fp)
  246. {
  247. int unit = iminor(in) - IPATH_DIAG_MINOR_BASE;
  248. struct ipath_devdata *dd;
  249. int ret;
  250. mutex_lock(&ipath_mutex);
  251. if (ipath_diag_inuse) {
  252. ret = -EBUSY;
  253. goto bail;
  254. }
  255. dd = ipath_lookup(unit);
  256. if (dd == NULL || !(dd->ipath_flags & IPATH_PRESENT) ||
  257. !dd->ipath_kregbase) {
  258. ret = -ENODEV;
  259. goto bail;
  260. }
  261. fp->private_data = dd;
  262. ipath_diag_inuse = -2;
  263. diag_set_link = 0;
  264. ret = 0;
  265. /* Only expose a way to reset the device if we
  266. make it into diag mode. */
  267. ipath_expose_reset(&dd->pcidev->dev);
  268. bail:
  269. mutex_unlock(&ipath_mutex);
  270. return ret;
  271. }
  272. /**
  273. * ipath_diagpkt_write - write an IB packet
  274. * @fp: the diag data device file pointer
  275. * @data: ipath_diag_pkt structure saying where to get the packet
  276. * @count: size of data to write
  277. * @off: unused by this code
  278. */
  279. static ssize_t ipath_diagpkt_write(struct file *fp,
  280. const char __user *data,
  281. size_t count, loff_t *off)
  282. {
  283. u32 __iomem *piobuf;
  284. u32 plen, clen, pbufn;
  285. struct ipath_diag_pkt odp;
  286. struct ipath_diag_xpkt dp;
  287. u32 *tmpbuf = NULL;
  288. struct ipath_devdata *dd;
  289. ssize_t ret = 0;
  290. u64 val;
  291. if (count != sizeof(dp)) {
  292. ret = -EINVAL;
  293. goto bail;
  294. }
  295. if (copy_from_user(&dp, data, sizeof(dp))) {
  296. ret = -EFAULT;
  297. goto bail;
  298. }
  299. /*
  300. * Due to padding/alignment issues (lessened with new struct)
  301. * the old and new structs are the same length. We need to
  302. * disambiguate them, which we can do because odp.len has never
  303. * been less than the total of LRH+BTH+DETH so far, while
  304. * dp.unit (same offset) unit is unlikely to get that high.
  305. * Similarly, dp.data, the pointer to user at the same offset
  306. * as odp.unit, is almost certainly at least one (512byte)page
  307. * "above" NULL. The if-block below can be omitted if compatibility
  308. * between a new driver and older diagnostic code is unimportant.
  309. * compatibility the other direction (new diags, old driver) is
  310. * handled in the diagnostic code, with a warning.
  311. */
  312. if (dp.unit >= 20 && dp.data < 512) {
  313. /* very probable version mismatch. Fix it up */
  314. memcpy(&odp, &dp, sizeof(odp));
  315. /* We got a legacy dp, copy elements to dp */
  316. dp.unit = odp.unit;
  317. dp.data = odp.data;
  318. dp.len = odp.len;
  319. dp.pbc_wd = 0; /* Indicate we need to compute PBC wd */
  320. }
  321. /* send count must be an exact number of dwords */
  322. if (dp.len & 3) {
  323. ret = -EINVAL;
  324. goto bail;
  325. }
  326. clen = dp.len >> 2;
  327. dd = ipath_lookup(dp.unit);
  328. if (!dd || !(dd->ipath_flags & IPATH_PRESENT) ||
  329. !dd->ipath_kregbase) {
  330. ipath_cdbg(VERBOSE, "illegal unit %u for diag data send\n",
  331. dp.unit);
  332. ret = -ENODEV;
  333. goto bail;
  334. }
  335. if (ipath_diag_inuse && !diag_set_link &&
  336. !(dd->ipath_flags & IPATH_LINKACTIVE)) {
  337. diag_set_link = 1;
  338. ipath_cdbg(VERBOSE, "Trying to set to set link active for "
  339. "diag pkt\n");
  340. ipath_set_linkstate(dd, IPATH_IB_LINKARM);
  341. ipath_set_linkstate(dd, IPATH_IB_LINKACTIVE);
  342. }
  343. if (!(dd->ipath_flags & IPATH_INITTED)) {
  344. /* no hardware, freeze, etc. */
  345. ipath_cdbg(VERBOSE, "unit %u not usable\n", dd->ipath_unit);
  346. ret = -ENODEV;
  347. goto bail;
  348. }
  349. /* Check link state, but not if we have custom PBC */
  350. val = dd->ipath_lastibcstat & IPATH_IBSTATE_MASK;
  351. if (!dp.pbc_wd && val != IPATH_IBSTATE_INIT &&
  352. val != IPATH_IBSTATE_ARM && val != IPATH_IBSTATE_ACTIVE) {
  353. ipath_cdbg(VERBOSE, "unit %u not ready (state %llx)\n",
  354. dd->ipath_unit, (unsigned long long) val);
  355. ret = -EINVAL;
  356. goto bail;
  357. }
  358. /* need total length before first word written */
  359. /* +1 word is for the qword padding */
  360. plen = sizeof(u32) + dp.len;
  361. if ((plen + 4) > dd->ipath_ibmaxlen) {
  362. ipath_dbg("Pkt len 0x%x > ibmaxlen %x\n",
  363. plen - 4, dd->ipath_ibmaxlen);
  364. ret = -EINVAL;
  365. goto bail; /* before writing pbc */
  366. }
  367. tmpbuf = vmalloc(plen);
  368. if (!tmpbuf) {
  369. dev_info(&dd->pcidev->dev, "Unable to allocate tmp buffer, "
  370. "failing\n");
  371. ret = -ENOMEM;
  372. goto bail;
  373. }
  374. if (copy_from_user(tmpbuf,
  375. (const void __user *) (unsigned long) dp.data,
  376. dp.len)) {
  377. ret = -EFAULT;
  378. goto bail;
  379. }
  380. piobuf = ipath_getpiobuf(dd, &pbufn);
  381. if (!piobuf) {
  382. ipath_cdbg(VERBOSE, "No PIO buffers avail unit for %u\n",
  383. dd->ipath_unit);
  384. ret = -EBUSY;
  385. goto bail;
  386. }
  387. plen >>= 2; /* in dwords */
  388. if (ipath_debug & __IPATH_PKTDBG)
  389. ipath_cdbg(VERBOSE, "unit %u 0x%x+1w pio%d\n",
  390. dd->ipath_unit, plen - 1, pbufn);
  391. if (dp.pbc_wd == 0)
  392. /* Legacy operation, use computed pbc_wd */
  393. dp.pbc_wd = plen;
  394. /* we have to flush after the PBC for correctness on some cpus
  395. * or WC buffer can be written out of order */
  396. writeq(dp.pbc_wd, piobuf);
  397. ipath_flush_wc();
  398. /* copy all by the trigger word, then flush, so it's written
  399. * to chip before trigger word, then write trigger word, then
  400. * flush again, so packet is sent. */
  401. __iowrite32_copy(piobuf + 2, tmpbuf, clen - 1);
  402. ipath_flush_wc();
  403. __raw_writel(tmpbuf[clen - 1], piobuf + clen + 1);
  404. ipath_flush_wc();
  405. ret = sizeof(dp);
  406. bail:
  407. vfree(tmpbuf);
  408. return ret;
  409. }
  410. static int ipath_diag_release(struct inode *in, struct file *fp)
  411. {
  412. mutex_lock(&ipath_mutex);
  413. ipath_diag_inuse = 0;
  414. fp->private_data = NULL;
  415. mutex_unlock(&ipath_mutex);
  416. return 0;
  417. }
  418. static ssize_t ipath_diag_read(struct file *fp, char __user *data,
  419. size_t count, loff_t *off)
  420. {
  421. struct ipath_devdata *dd = fp->private_data;
  422. void __iomem *kreg_base;
  423. ssize_t ret;
  424. kreg_base = dd->ipath_kregbase;
  425. if (count == 0)
  426. ret = 0;
  427. else if ((count % 4) || (*off % 4))
  428. /* address or length is not 32-bit aligned, hence invalid */
  429. ret = -EINVAL;
  430. else if (ipath_diag_inuse < 1 && (*off || count != 8))
  431. ret = -EINVAL; /* prevent cat /dev/ipath_diag* */
  432. else if ((count % 8) || (*off % 8))
  433. /* address or length not 64-bit aligned; do 32-bit reads */
  434. ret = ipath_read_umem32(dd, data, kreg_base + *off, count);
  435. else
  436. ret = ipath_read_umem64(dd, data, kreg_base + *off, count);
  437. if (ret >= 0) {
  438. *off += count;
  439. ret = count;
  440. if (ipath_diag_inuse == -2)
  441. ipath_diag_inuse++;
  442. }
  443. return ret;
  444. }
  445. static ssize_t ipath_diag_write(struct file *fp, const char __user *data,
  446. size_t count, loff_t *off)
  447. {
  448. struct ipath_devdata *dd = fp->private_data;
  449. void __iomem *kreg_base;
  450. ssize_t ret;
  451. kreg_base = dd->ipath_kregbase;
  452. if (count == 0)
  453. ret = 0;
  454. else if ((count % 4) || (*off % 4))
  455. /* address or length is not 32-bit aligned, hence invalid */
  456. ret = -EINVAL;
  457. else if ((ipath_diag_inuse == -1 && (*off || count != 8)) ||
  458. ipath_diag_inuse == -2) /* read qw off 0, write qw off 0 */
  459. ret = -EINVAL; /* before any other write allowed */
  460. else if ((count % 8) || (*off % 8))
  461. /* address or length not 64-bit aligned; do 32-bit writes */
  462. ret = ipath_write_umem32(dd, kreg_base + *off, data, count);
  463. else
  464. ret = ipath_write_umem64(dd, kreg_base + *off, data, count);
  465. if (ret >= 0) {
  466. *off += count;
  467. ret = count;
  468. if (ipath_diag_inuse == -1)
  469. ipath_diag_inuse = 1; /* all read/write OK now */
  470. }
  471. return ret;
  472. }