ipath_diag.c 15 KB

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