ipath_diag.c 13 KB

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