ipath_diag.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /*
  2. * Copyright (c) 2006 QLogic, Inc. 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 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. int ipath_diag_add(struct ipath_devdata *dd)
  64. {
  65. char name[16];
  66. snprintf(name, sizeof(name), "ipath_diag%d", dd->ipath_unit);
  67. return ipath_cdev_init(IPATH_DIAG_MINOR_BASE + dd->ipath_unit, name,
  68. &diag_file_ops, &dd->diag_cdev,
  69. &dd->diag_class_dev);
  70. }
  71. void ipath_diag_remove(struct ipath_devdata *dd)
  72. {
  73. ipath_cdev_cleanup(&dd->diag_cdev, &dd->diag_class_dev);
  74. }
  75. /**
  76. * ipath_read_umem64 - read a 64-bit quantity from the chip into user space
  77. * @dd: the infinipath device
  78. * @uaddr: the location to store the data in user memory
  79. * @caddr: the source chip address (full pointer, not offset)
  80. * @count: number of bytes to copy (multiple of 32 bits)
  81. *
  82. * This function also localizes all chip memory accesses.
  83. * The copy should be written such that we read full cacheline packets
  84. * from the chip. This is usually used for a single qword
  85. *
  86. * NOTE: This assumes the chip address is 64-bit aligned.
  87. */
  88. static int ipath_read_umem64(struct ipath_devdata *dd, void __user *uaddr,
  89. const void __iomem *caddr, size_t count)
  90. {
  91. const u64 __iomem *reg_addr = caddr;
  92. const u64 __iomem *reg_end = reg_addr + (count / sizeof(u64));
  93. int ret;
  94. /* not very efficient, but it works for now */
  95. if (reg_addr < dd->ipath_kregbase || reg_end > dd->ipath_kregend) {
  96. ret = -EINVAL;
  97. goto bail;
  98. }
  99. while (reg_addr < reg_end) {
  100. u64 data = readq(reg_addr);
  101. if (copy_to_user(uaddr, &data, sizeof(u64))) {
  102. ret = -EFAULT;
  103. goto bail;
  104. }
  105. reg_addr++;
  106. uaddr += sizeof(u64);
  107. }
  108. ret = 0;
  109. bail:
  110. return ret;
  111. }
  112. /**
  113. * ipath_write_umem64 - write a 64-bit quantity to the chip from user space
  114. * @dd: the infinipath device
  115. * @caddr: the destination chip address (full pointer, not offset)
  116. * @uaddr: the source of the data in user memory
  117. * @count: the number of bytes to copy (multiple of 32 bits)
  118. *
  119. * This is usually used for a single qword
  120. * NOTE: This assumes the chip address is 64-bit aligned.
  121. */
  122. static int ipath_write_umem64(struct ipath_devdata *dd, void __iomem *caddr,
  123. const void __user *uaddr, size_t count)
  124. {
  125. u64 __iomem *reg_addr = caddr;
  126. const u64 __iomem *reg_end = reg_addr + (count / sizeof(u64));
  127. int ret;
  128. /* not very efficient, but it works for now */
  129. if (reg_addr < dd->ipath_kregbase || reg_end > dd->ipath_kregend) {
  130. ret = -EINVAL;
  131. goto bail;
  132. }
  133. while (reg_addr < reg_end) {
  134. u64 data;
  135. if (copy_from_user(&data, uaddr, sizeof(data))) {
  136. ret = -EFAULT;
  137. goto bail;
  138. }
  139. writeq(data, reg_addr);
  140. reg_addr++;
  141. uaddr += sizeof(u64);
  142. }
  143. ret = 0;
  144. bail:
  145. return ret;
  146. }
  147. /**
  148. * ipath_read_umem32 - read a 32-bit quantity from the chip into user space
  149. * @dd: the infinipath device
  150. * @uaddr: the location to store the data in user memory
  151. * @caddr: the source chip address (full pointer, not offset)
  152. * @count: number of bytes to copy
  153. *
  154. * read 32 bit values, not 64 bit; for memories that only
  155. * support 32 bit reads; usually a single dword.
  156. */
  157. static int ipath_read_umem32(struct ipath_devdata *dd, void __user *uaddr,
  158. const void __iomem *caddr, size_t count)
  159. {
  160. const u32 __iomem *reg_addr = caddr;
  161. const u32 __iomem *reg_end = reg_addr + (count / sizeof(u32));
  162. int ret;
  163. if (reg_addr < (u32 __iomem *) dd->ipath_kregbase ||
  164. reg_end > (u32 __iomem *) dd->ipath_kregend) {
  165. ret = -EINVAL;
  166. goto bail;
  167. }
  168. /* not very efficient, but it works for now */
  169. while (reg_addr < reg_end) {
  170. u32 data = readl(reg_addr);
  171. if (copy_to_user(uaddr, &data, sizeof(data))) {
  172. ret = -EFAULT;
  173. goto bail;
  174. }
  175. reg_addr++;
  176. uaddr += sizeof(u32);
  177. }
  178. ret = 0;
  179. bail:
  180. return ret;
  181. }
  182. /**
  183. * ipath_write_umem32 - write a 32-bit quantity to the chip from user space
  184. * @dd: the infinipath device
  185. * @caddr: the destination chip address (full pointer, not offset)
  186. * @uaddr: the source of the data in user memory
  187. * @count: number of bytes to copy
  188. *
  189. * write 32 bit values, not 64 bit; for memories that only
  190. * support 32 bit write; usually a single dword.
  191. */
  192. static int ipath_write_umem32(struct ipath_devdata *dd, void __iomem *caddr,
  193. const void __user *uaddr, size_t count)
  194. {
  195. u32 __iomem *reg_addr = caddr;
  196. const u32 __iomem *reg_end = reg_addr + (count / sizeof(u32));
  197. int ret;
  198. if (reg_addr < (u32 __iomem *) dd->ipath_kregbase ||
  199. reg_end > (u32 __iomem *) dd->ipath_kregend) {
  200. ret = -EINVAL;
  201. goto bail;
  202. }
  203. while (reg_addr < reg_end) {
  204. u32 data;
  205. if (copy_from_user(&data, uaddr, sizeof(data))) {
  206. ret = -EFAULT;
  207. goto bail;
  208. }
  209. writel(data, reg_addr);
  210. reg_addr++;
  211. uaddr += sizeof(u32);
  212. }
  213. ret = 0;
  214. bail:
  215. return ret;
  216. }
  217. static int ipath_diag_open(struct inode *in, struct file *fp)
  218. {
  219. int unit = iminor(in) - IPATH_DIAG_MINOR_BASE;
  220. struct ipath_devdata *dd;
  221. int ret;
  222. mutex_lock(&ipath_mutex);
  223. if (ipath_diag_inuse) {
  224. ret = -EBUSY;
  225. goto bail;
  226. }
  227. dd = ipath_lookup(unit);
  228. if (dd == NULL || !(dd->ipath_flags & IPATH_PRESENT) ||
  229. !dd->ipath_kregbase) {
  230. ret = -ENODEV;
  231. goto bail;
  232. }
  233. fp->private_data = dd;
  234. ipath_diag_inuse = 1;
  235. diag_set_link = 0;
  236. ret = 0;
  237. /* Only expose a way to reset the device if we
  238. make it into diag mode. */
  239. ipath_expose_reset(&dd->pcidev->dev);
  240. bail:
  241. mutex_unlock(&ipath_mutex);
  242. return ret;
  243. }
  244. static ssize_t ipath_diagpkt_write(struct file *fp,
  245. const char __user *data,
  246. size_t count, loff_t *off);
  247. static struct file_operations diagpkt_file_ops = {
  248. .owner = THIS_MODULE,
  249. .write = ipath_diagpkt_write,
  250. };
  251. static struct cdev *diagpkt_cdev;
  252. static struct class_device *diagpkt_class_dev;
  253. int __init ipath_diagpkt_add(void)
  254. {
  255. return ipath_cdev_init(IPATH_DIAGPKT_MINOR,
  256. "ipath_diagpkt", &diagpkt_file_ops,
  257. &diagpkt_cdev, &diagpkt_class_dev);
  258. }
  259. void __exit ipath_diagpkt_remove(void)
  260. {
  261. ipath_cdev_cleanup(&diagpkt_cdev, &diagpkt_class_dev);
  262. }
  263. /**
  264. * ipath_diagpkt_write - write an IB packet
  265. * @fp: the diag data device file pointer
  266. * @data: ipath_diag_pkt structure saying where to get the packet
  267. * @count: size of data to write
  268. * @off: unused by this code
  269. */
  270. static ssize_t ipath_diagpkt_write(struct file *fp,
  271. const char __user *data,
  272. size_t count, loff_t *off)
  273. {
  274. u32 __iomem *piobuf;
  275. u32 plen, clen, pbufn;
  276. struct ipath_diag_pkt dp;
  277. u32 *tmpbuf = NULL;
  278. struct ipath_devdata *dd;
  279. ssize_t ret = 0;
  280. u64 val;
  281. if (count < sizeof(dp)) {
  282. ret = -EINVAL;
  283. goto bail;
  284. }
  285. if (copy_from_user(&dp, data, sizeof(dp))) {
  286. ret = -EFAULT;
  287. goto bail;
  288. }
  289. /* send count must be an exact number of dwords */
  290. if (dp.len & 3) {
  291. ret = -EINVAL;
  292. goto bail;
  293. }
  294. clen = dp.len >> 2;
  295. dd = ipath_lookup(dp.unit);
  296. if (!dd || !(dd->ipath_flags & IPATH_PRESENT) ||
  297. !dd->ipath_kregbase) {
  298. ipath_cdbg(VERBOSE, "illegal unit %u for diag data send\n",
  299. dp.unit);
  300. ret = -ENODEV;
  301. goto bail;
  302. }
  303. if (ipath_diag_inuse && !diag_set_link &&
  304. !(dd->ipath_flags & IPATH_LINKACTIVE)) {
  305. diag_set_link = 1;
  306. ipath_cdbg(VERBOSE, "Trying to set to set link active for "
  307. "diag pkt\n");
  308. ipath_set_linkstate(dd, IPATH_IB_LINKARM);
  309. ipath_set_linkstate(dd, IPATH_IB_LINKACTIVE);
  310. }
  311. if (!(dd->ipath_flags & IPATH_INITTED)) {
  312. /* no hardware, freeze, etc. */
  313. ipath_cdbg(VERBOSE, "unit %u not usable\n", dd->ipath_unit);
  314. ret = -ENODEV;
  315. goto bail;
  316. }
  317. val = dd->ipath_lastibcstat & IPATH_IBSTATE_MASK;
  318. if (val != IPATH_IBSTATE_INIT && val != IPATH_IBSTATE_ARM &&
  319. val != IPATH_IBSTATE_ACTIVE) {
  320. ipath_cdbg(VERBOSE, "unit %u not ready (state %llx)\n",
  321. dd->ipath_unit, (unsigned long long) val);
  322. ret = -EINVAL;
  323. goto bail;
  324. }
  325. /* need total length before first word written */
  326. /* +1 word is for the qword padding */
  327. plen = sizeof(u32) + dp.len;
  328. if ((plen + 4) > dd->ipath_ibmaxlen) {
  329. ipath_dbg("Pkt len 0x%x > ibmaxlen %x\n",
  330. plen - 4, dd->ipath_ibmaxlen);
  331. ret = -EINVAL;
  332. goto bail; /* before writing pbc */
  333. }
  334. tmpbuf = vmalloc(plen);
  335. if (!tmpbuf) {
  336. dev_info(&dd->pcidev->dev, "Unable to allocate tmp buffer, "
  337. "failing\n");
  338. ret = -ENOMEM;
  339. goto bail;
  340. }
  341. if (copy_from_user(tmpbuf,
  342. (const void __user *) (unsigned long) dp.data,
  343. dp.len)) {
  344. ret = -EFAULT;
  345. goto bail;
  346. }
  347. piobuf = ipath_getpiobuf(dd, &pbufn);
  348. if (!piobuf) {
  349. ipath_cdbg(VERBOSE, "No PIO buffers avail unit for %u\n",
  350. dd->ipath_unit);
  351. ret = -EBUSY;
  352. goto bail;
  353. }
  354. plen >>= 2; /* in dwords */
  355. if (ipath_debug & __IPATH_PKTDBG)
  356. ipath_cdbg(VERBOSE, "unit %u 0x%x+1w pio%d\n",
  357. dd->ipath_unit, plen - 1, pbufn);
  358. /* we have to flush after the PBC for correctness on some cpus
  359. * or WC buffer can be written out of order */
  360. writeq(plen, piobuf);
  361. ipath_flush_wc();
  362. /* copy all by the trigger word, then flush, so it's written
  363. * to chip before trigger word, then write trigger word, then
  364. * flush again, so packet is sent. */
  365. __iowrite32_copy(piobuf + 2, tmpbuf, clen - 1);
  366. ipath_flush_wc();
  367. __raw_writel(tmpbuf[clen - 1], piobuf + clen + 1);
  368. ipath_flush_wc();
  369. ret = sizeof(dp);
  370. bail:
  371. vfree(tmpbuf);
  372. return ret;
  373. }
  374. static int ipath_diag_release(struct inode *in, struct file *fp)
  375. {
  376. mutex_lock(&ipath_mutex);
  377. ipath_diag_inuse = 0;
  378. fp->private_data = NULL;
  379. mutex_unlock(&ipath_mutex);
  380. return 0;
  381. }
  382. static ssize_t ipath_diag_read(struct file *fp, char __user *data,
  383. size_t count, loff_t *off)
  384. {
  385. struct ipath_devdata *dd = fp->private_data;
  386. void __iomem *kreg_base;
  387. ssize_t ret;
  388. kreg_base = dd->ipath_kregbase;
  389. if (count == 0)
  390. ret = 0;
  391. else if ((count % 4) || (*off % 4))
  392. /* address or length is not 32-bit aligned, hence invalid */
  393. ret = -EINVAL;
  394. else if ((count % 8) || (*off % 8))
  395. /* address or length not 64-bit aligned; do 32-bit reads */
  396. ret = ipath_read_umem32(dd, data, kreg_base + *off, count);
  397. else
  398. ret = ipath_read_umem64(dd, data, kreg_base + *off, count);
  399. if (ret >= 0) {
  400. *off += count;
  401. ret = count;
  402. }
  403. return ret;
  404. }
  405. static ssize_t ipath_diag_write(struct file *fp, const char __user *data,
  406. size_t count, loff_t *off)
  407. {
  408. struct ipath_devdata *dd = fp->private_data;
  409. void __iomem *kreg_base;
  410. ssize_t ret;
  411. kreg_base = dd->ipath_kregbase;
  412. if (count == 0)
  413. ret = 0;
  414. else if ((count % 4) || (*off % 4))
  415. /* address or length is not 32-bit aligned, hence invalid */
  416. ret = -EINVAL;
  417. else if ((count % 8) || (*off % 8))
  418. /* address or length not 64-bit aligned; do 32-bit writes */
  419. ret = ipath_write_umem32(dd, kreg_base + *off, data, count);
  420. else
  421. ret = ipath_write_umem64(dd, kreg_base + *off, data, count);
  422. if (ret >= 0) {
  423. *off += count;
  424. ret = count;
  425. }
  426. return ret;
  427. }