ipath_diag.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. /*
  2. * Copyright (c) 2006, 2007, 2008 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 <linux/fs.h>
  46. #include <asm/uaccess.h>
  47. #include "ipath_kernel.h"
  48. #include "ipath_common.h"
  49. int ipath_diag_inuse;
  50. static int diag_set_link;
  51. static int ipath_diag_open(struct inode *in, struct file *fp);
  52. static int ipath_diag_release(struct inode *in, struct file *fp);
  53. static ssize_t ipath_diag_read(struct file *fp, char __user *data,
  54. size_t count, loff_t *off);
  55. static ssize_t ipath_diag_write(struct file *fp, const char __user *data,
  56. size_t count, loff_t *off);
  57. static const struct file_operations diag_file_ops = {
  58. .owner = THIS_MODULE,
  59. .write = ipath_diag_write,
  60. .read = ipath_diag_read,
  61. .open = ipath_diag_open,
  62. .release = ipath_diag_release
  63. };
  64. static ssize_t ipath_diagpkt_write(struct file *fp,
  65. const char __user *data,
  66. size_t count, loff_t *off);
  67. static const struct file_operations diagpkt_file_ops = {
  68. .owner = THIS_MODULE,
  69. .write = ipath_diagpkt_write,
  70. };
  71. static atomic_t diagpkt_count = ATOMIC_INIT(0);
  72. static struct cdev *diagpkt_cdev;
  73. static struct device *diagpkt_dev;
  74. int ipath_diag_add(struct ipath_devdata *dd)
  75. {
  76. char name[16];
  77. int ret = 0;
  78. if (atomic_inc_return(&diagpkt_count) == 1) {
  79. ret = ipath_cdev_init(IPATH_DIAGPKT_MINOR,
  80. "ipath_diagpkt", &diagpkt_file_ops,
  81. &diagpkt_cdev, &diagpkt_dev);
  82. if (ret) {
  83. ipath_dev_err(dd, "Couldn't create ipath_diagpkt "
  84. "device: %d", ret);
  85. goto done;
  86. }
  87. }
  88. snprintf(name, sizeof(name), "ipath_diag%d", dd->ipath_unit);
  89. ret = ipath_cdev_init(IPATH_DIAG_MINOR_BASE + dd->ipath_unit, name,
  90. &diag_file_ops, &dd->diag_cdev,
  91. &dd->diag_dev);
  92. if (ret)
  93. ipath_dev_err(dd, "Couldn't create %s device: %d",
  94. name, ret);
  95. done:
  96. return ret;
  97. }
  98. void ipath_diag_remove(struct ipath_devdata *dd)
  99. {
  100. if (atomic_dec_and_test(&diagpkt_count))
  101. ipath_cdev_cleanup(&diagpkt_cdev, &diagpkt_dev);
  102. ipath_cdev_cleanup(&dd->diag_cdev, &dd->diag_dev);
  103. }
  104. /**
  105. * ipath_read_umem64 - read a 64-bit quantity from the chip into user space
  106. * @dd: the infinipath device
  107. * @uaddr: the location to store the data in user memory
  108. * @caddr: the source chip address (full pointer, not offset)
  109. * @count: number of bytes to copy (multiple of 32 bits)
  110. *
  111. * This function also localizes all chip memory accesses.
  112. * The copy should be written such that we read full cacheline packets
  113. * from the chip. This is usually used for a single qword
  114. *
  115. * NOTE: This assumes the chip address is 64-bit aligned.
  116. */
  117. static int ipath_read_umem64(struct ipath_devdata *dd, void __user *uaddr,
  118. const void __iomem *caddr, size_t count)
  119. {
  120. const u64 __iomem *reg_addr = caddr;
  121. const u64 __iomem *reg_end = reg_addr + (count / sizeof(u64));
  122. int ret;
  123. /* not very efficient, but it works for now */
  124. if (reg_addr < dd->ipath_kregbase || reg_end > dd->ipath_kregend) {
  125. ret = -EINVAL;
  126. goto bail;
  127. }
  128. while (reg_addr < reg_end) {
  129. u64 data = readq(reg_addr);
  130. if (copy_to_user(uaddr, &data, sizeof(u64))) {
  131. ret = -EFAULT;
  132. goto bail;
  133. }
  134. reg_addr++;
  135. uaddr += sizeof(u64);
  136. }
  137. ret = 0;
  138. bail:
  139. return ret;
  140. }
  141. /**
  142. * ipath_write_umem64 - write a 64-bit quantity to the chip from user space
  143. * @dd: the infinipath device
  144. * @caddr: the destination chip address (full pointer, not offset)
  145. * @uaddr: the source of the data in user memory
  146. * @count: the number of bytes to copy (multiple of 32 bits)
  147. *
  148. * This is usually used for a single qword
  149. * NOTE: This assumes the chip address is 64-bit aligned.
  150. */
  151. static int ipath_write_umem64(struct ipath_devdata *dd, void __iomem *caddr,
  152. const void __user *uaddr, size_t count)
  153. {
  154. u64 __iomem *reg_addr = caddr;
  155. const u64 __iomem *reg_end = reg_addr + (count / sizeof(u64));
  156. int ret;
  157. /* not very efficient, but it works for now */
  158. if (reg_addr < dd->ipath_kregbase || reg_end > dd->ipath_kregend) {
  159. ret = -EINVAL;
  160. goto bail;
  161. }
  162. while (reg_addr < reg_end) {
  163. u64 data;
  164. if (copy_from_user(&data, uaddr, sizeof(data))) {
  165. ret = -EFAULT;
  166. goto bail;
  167. }
  168. writeq(data, reg_addr);
  169. reg_addr++;
  170. uaddr += sizeof(u64);
  171. }
  172. ret = 0;
  173. bail:
  174. return ret;
  175. }
  176. /**
  177. * ipath_read_umem32 - read a 32-bit quantity from the chip into user space
  178. * @dd: the infinipath device
  179. * @uaddr: the location to store the data in user memory
  180. * @caddr: the source chip address (full pointer, not offset)
  181. * @count: number of bytes to copy
  182. *
  183. * read 32 bit values, not 64 bit; for memories that only
  184. * support 32 bit reads; usually a single dword.
  185. */
  186. static int ipath_read_umem32(struct ipath_devdata *dd, void __user *uaddr,
  187. const void __iomem *caddr, size_t count)
  188. {
  189. const u32 __iomem *reg_addr = caddr;
  190. const u32 __iomem *reg_end = reg_addr + (count / sizeof(u32));
  191. int ret;
  192. if (reg_addr < (u32 __iomem *) dd->ipath_kregbase ||
  193. reg_end > (u32 __iomem *) dd->ipath_kregend) {
  194. ret = -EINVAL;
  195. goto bail;
  196. }
  197. /* not very efficient, but it works for now */
  198. while (reg_addr < reg_end) {
  199. u32 data = readl(reg_addr);
  200. if (copy_to_user(uaddr, &data, sizeof(data))) {
  201. ret = -EFAULT;
  202. goto bail;
  203. }
  204. reg_addr++;
  205. uaddr += sizeof(u32);
  206. }
  207. ret = 0;
  208. bail:
  209. return ret;
  210. }
  211. /**
  212. * ipath_write_umem32 - write a 32-bit quantity to the chip from user space
  213. * @dd: the infinipath device
  214. * @caddr: the destination chip address (full pointer, not offset)
  215. * @uaddr: the source of the data in user memory
  216. * @count: number of bytes to copy
  217. *
  218. * write 32 bit values, not 64 bit; for memories that only
  219. * support 32 bit write; usually a single dword.
  220. */
  221. static int ipath_write_umem32(struct ipath_devdata *dd, void __iomem *caddr,
  222. const void __user *uaddr, size_t count)
  223. {
  224. u32 __iomem *reg_addr = caddr;
  225. const u32 __iomem *reg_end = reg_addr + (count / sizeof(u32));
  226. int ret;
  227. if (reg_addr < (u32 __iomem *) dd->ipath_kregbase ||
  228. reg_end > (u32 __iomem *) dd->ipath_kregend) {
  229. ret = -EINVAL;
  230. goto bail;
  231. }
  232. while (reg_addr < reg_end) {
  233. u32 data;
  234. if (copy_from_user(&data, uaddr, sizeof(data))) {
  235. ret = -EFAULT;
  236. goto bail;
  237. }
  238. writel(data, reg_addr);
  239. reg_addr++;
  240. uaddr += sizeof(u32);
  241. }
  242. ret = 0;
  243. bail:
  244. return ret;
  245. }
  246. static int ipath_diag_open(struct inode *in, struct file *fp)
  247. {
  248. int unit = iminor(in) - IPATH_DIAG_MINOR_BASE;
  249. struct ipath_devdata *dd;
  250. int ret;
  251. mutex_lock(&ipath_mutex);
  252. if (ipath_diag_inuse) {
  253. ret = -EBUSY;
  254. goto bail;
  255. }
  256. dd = ipath_lookup(unit);
  257. if (dd == NULL || !(dd->ipath_flags & IPATH_PRESENT) ||
  258. !dd->ipath_kregbase) {
  259. ret = -ENODEV;
  260. goto bail;
  261. }
  262. fp->private_data = dd;
  263. ipath_diag_inuse = -2;
  264. diag_set_link = 0;
  265. ret = 0;
  266. /* Only expose a way to reset the device if we
  267. make it into diag mode. */
  268. ipath_expose_reset(&dd->pcidev->dev);
  269. bail:
  270. mutex_unlock(&ipath_mutex);
  271. return ret;
  272. }
  273. /**
  274. * ipath_diagpkt_write - write an IB packet
  275. * @fp: the diag data device file pointer
  276. * @data: ipath_diag_pkt structure saying where to get the packet
  277. * @count: size of data to write
  278. * @off: unused by this code
  279. */
  280. static ssize_t ipath_diagpkt_write(struct file *fp,
  281. const char __user *data,
  282. size_t count, loff_t *off)
  283. {
  284. u32 __iomem *piobuf;
  285. u32 plen, clen, pbufn;
  286. struct ipath_diag_pkt odp;
  287. struct ipath_diag_xpkt dp;
  288. u32 *tmpbuf = NULL;
  289. struct ipath_devdata *dd;
  290. ssize_t ret = 0;
  291. u64 val;
  292. u32 l_state, lt_state; /* LinkState, LinkTrainingState */
  293. if (count < sizeof(odp)) {
  294. ret = -EINVAL;
  295. goto bail;
  296. }
  297. if (count == sizeof(dp)) {
  298. if (copy_from_user(&dp, data, sizeof(dp))) {
  299. ret = -EFAULT;
  300. goto bail;
  301. }
  302. } else if (copy_from_user(&odp, data, sizeof(odp))) {
  303. ret = -EFAULT;
  304. goto bail;
  305. }
  306. /*
  307. * Due to padding/alignment issues (lessened with new struct)
  308. * the old and new structs are the same length. We need to
  309. * disambiguate them, which we can do because odp.len has never
  310. * been less than the total of LRH+BTH+DETH so far, while
  311. * dp.unit (same offset) unit is unlikely to get that high.
  312. * Similarly, dp.data, the pointer to user at the same offset
  313. * as odp.unit, is almost certainly at least one (512byte)page
  314. * "above" NULL. The if-block below can be omitted if compatibility
  315. * between a new driver and older diagnostic code is unimportant.
  316. * compatibility the other direction (new diags, old driver) is
  317. * handled in the diagnostic code, with a warning.
  318. */
  319. if (dp.unit >= 20 && dp.data < 512) {
  320. /* very probable version mismatch. Fix it up */
  321. memcpy(&odp, &dp, sizeof(odp));
  322. /* We got a legacy dp, copy elements to dp */
  323. dp.unit = odp.unit;
  324. dp.data = odp.data;
  325. dp.len = odp.len;
  326. dp.pbc_wd = 0; /* Indicate we need to compute PBC wd */
  327. }
  328. /* send count must be an exact number of dwords */
  329. if (dp.len & 3) {
  330. ret = -EINVAL;
  331. goto bail;
  332. }
  333. clen = dp.len >> 2;
  334. dd = ipath_lookup(dp.unit);
  335. if (!dd || !(dd->ipath_flags & IPATH_PRESENT) ||
  336. !dd->ipath_kregbase) {
  337. ipath_cdbg(VERBOSE, "illegal unit %u for diag data send\n",
  338. dp.unit);
  339. ret = -ENODEV;
  340. goto bail;
  341. }
  342. if (ipath_diag_inuse && !diag_set_link &&
  343. !(dd->ipath_flags & IPATH_LINKACTIVE)) {
  344. diag_set_link = 1;
  345. ipath_cdbg(VERBOSE, "Trying to set to set link active for "
  346. "diag pkt\n");
  347. ipath_set_linkstate(dd, IPATH_IB_LINKARM);
  348. ipath_set_linkstate(dd, IPATH_IB_LINKACTIVE);
  349. }
  350. if (!(dd->ipath_flags & IPATH_INITTED)) {
  351. /* no hardware, freeze, etc. */
  352. ipath_cdbg(VERBOSE, "unit %u not usable\n", dd->ipath_unit);
  353. ret = -ENODEV;
  354. goto bail;
  355. }
  356. /*
  357. * Want to skip check for l_state if using custom PBC,
  358. * because we might be trying to force an SM packet out.
  359. * first-cut, skip _all_ state checking in that case.
  360. */
  361. val = ipath_ib_state(dd, dd->ipath_lastibcstat);
  362. lt_state = ipath_ib_linktrstate(dd, dd->ipath_lastibcstat);
  363. l_state = ipath_ib_linkstate(dd, dd->ipath_lastibcstat);
  364. if (!dp.pbc_wd && (lt_state != INFINIPATH_IBCS_LT_STATE_LINKUP ||
  365. (val != dd->ib_init && val != dd->ib_arm &&
  366. val != dd->ib_active))) {
  367. ipath_cdbg(VERBOSE, "unit %u not ready (state %llx)\n",
  368. dd->ipath_unit, (unsigned long long) val);
  369. ret = -EINVAL;
  370. goto bail;
  371. }
  372. /* need total length before first word written */
  373. /* +1 word is for the qword padding */
  374. plen = sizeof(u32) + dp.len;
  375. if ((plen + 4) > dd->ipath_ibmaxlen) {
  376. ipath_dbg("Pkt len 0x%x > ibmaxlen %x\n",
  377. plen - 4, dd->ipath_ibmaxlen);
  378. ret = -EINVAL;
  379. goto bail; /* before writing pbc */
  380. }
  381. tmpbuf = vmalloc(plen);
  382. if (!tmpbuf) {
  383. dev_info(&dd->pcidev->dev, "Unable to allocate tmp buffer, "
  384. "failing\n");
  385. ret = -ENOMEM;
  386. goto bail;
  387. }
  388. if (copy_from_user(tmpbuf,
  389. (const void __user *) (unsigned long) dp.data,
  390. dp.len)) {
  391. ret = -EFAULT;
  392. goto bail;
  393. }
  394. plen >>= 2; /* in dwords */
  395. piobuf = ipath_getpiobuf(dd, plen, &pbufn);
  396. if (!piobuf) {
  397. ipath_cdbg(VERBOSE, "No PIO buffers avail unit for %u\n",
  398. dd->ipath_unit);
  399. ret = -EBUSY;
  400. goto bail;
  401. }
  402. /* disarm it just to be extra sure */
  403. ipath_disarm_piobufs(dd, pbufn, 1);
  404. if (ipath_debug & __IPATH_PKTDBG)
  405. ipath_cdbg(VERBOSE, "unit %u 0x%x+1w pio%d\n",
  406. dd->ipath_unit, plen - 1, pbufn);
  407. if (dp.pbc_wd == 0)
  408. dp.pbc_wd = plen;
  409. writeq(dp.pbc_wd, piobuf);
  410. /*
  411. * Copy all by the trigger word, then flush, so it's written
  412. * to chip before trigger word, then write trigger word, then
  413. * flush again, so packet is sent.
  414. */
  415. if (dd->ipath_flags & IPATH_PIO_FLUSH_WC) {
  416. ipath_flush_wc();
  417. __iowrite32_copy(piobuf + 2, tmpbuf, clen - 1);
  418. ipath_flush_wc();
  419. __raw_writel(tmpbuf[clen - 1], piobuf + clen + 1);
  420. } else
  421. __iowrite32_copy(piobuf + 2, tmpbuf, clen);
  422. ipath_flush_wc();
  423. ret = sizeof(dp);
  424. bail:
  425. vfree(tmpbuf);
  426. return ret;
  427. }
  428. static int ipath_diag_release(struct inode *in, struct file *fp)
  429. {
  430. mutex_lock(&ipath_mutex);
  431. ipath_diag_inuse = 0;
  432. fp->private_data = NULL;
  433. mutex_unlock(&ipath_mutex);
  434. return 0;
  435. }
  436. static ssize_t ipath_diag_read(struct file *fp, char __user *data,
  437. size_t count, loff_t *off)
  438. {
  439. struct ipath_devdata *dd = fp->private_data;
  440. void __iomem *kreg_base;
  441. ssize_t ret;
  442. kreg_base = dd->ipath_kregbase;
  443. if (count == 0)
  444. ret = 0;
  445. else if ((count % 4) || (*off % 4))
  446. /* address or length is not 32-bit aligned, hence invalid */
  447. ret = -EINVAL;
  448. else if (ipath_diag_inuse < 1 && (*off || count != 8))
  449. ret = -EINVAL; /* prevent cat /dev/ipath_diag* */
  450. else if ((count % 8) || (*off % 8))
  451. /* address or length not 64-bit aligned; do 32-bit reads */
  452. ret = ipath_read_umem32(dd, data, kreg_base + *off, count);
  453. else
  454. ret = ipath_read_umem64(dd, data, kreg_base + *off, count);
  455. if (ret >= 0) {
  456. *off += count;
  457. ret = count;
  458. if (ipath_diag_inuse == -2)
  459. ipath_diag_inuse++;
  460. }
  461. return ret;
  462. }
  463. static ssize_t ipath_diag_write(struct file *fp, const char __user *data,
  464. size_t count, loff_t *off)
  465. {
  466. struct ipath_devdata *dd = fp->private_data;
  467. void __iomem *kreg_base;
  468. ssize_t ret;
  469. kreg_base = dd->ipath_kregbase;
  470. if (count == 0)
  471. ret = 0;
  472. else if ((count % 4) || (*off % 4))
  473. /* address or length is not 32-bit aligned, hence invalid */
  474. ret = -EINVAL;
  475. else if ((ipath_diag_inuse == -1 && (*off || count != 8)) ||
  476. ipath_diag_inuse == -2) /* read qw off 0, write qw off 0 */
  477. ret = -EINVAL; /* before any other write allowed */
  478. else if ((count % 8) || (*off % 8))
  479. /* address or length not 64-bit aligned; do 32-bit writes */
  480. ret = ipath_write_umem32(dd, kreg_base + *off, data, count);
  481. else
  482. ret = ipath_write_umem64(dd, kreg_base + *off, data, count);
  483. if (ret >= 0) {
  484. *off += count;
  485. ret = count;
  486. if (ipath_diag_inuse == -1)
  487. ipath_diag_inuse = 1; /* all read/write OK now */
  488. }
  489. return ret;
  490. }