ipath_diag.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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. static ssize_t ipath_diagpkt_write(struct file *fp,
  64. const char __user *data,
  65. size_t count, loff_t *off);
  66. static 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 = 1;
  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 dp;
  286. u32 *tmpbuf = NULL;
  287. struct ipath_devdata *dd;
  288. ssize_t ret = 0;
  289. u64 val;
  290. if (count < sizeof(dp)) {
  291. ret = -EINVAL;
  292. goto bail;
  293. }
  294. if (copy_from_user(&dp, data, sizeof(dp))) {
  295. ret = -EFAULT;
  296. goto bail;
  297. }
  298. /* send count must be an exact number of dwords */
  299. if (dp.len & 3) {
  300. ret = -EINVAL;
  301. goto bail;
  302. }
  303. clen = dp.len >> 2;
  304. dd = ipath_lookup(dp.unit);
  305. if (!dd || !(dd->ipath_flags & IPATH_PRESENT) ||
  306. !dd->ipath_kregbase) {
  307. ipath_cdbg(VERBOSE, "illegal unit %u for diag data send\n",
  308. dp.unit);
  309. ret = -ENODEV;
  310. goto bail;
  311. }
  312. if (ipath_diag_inuse && !diag_set_link &&
  313. !(dd->ipath_flags & IPATH_LINKACTIVE)) {
  314. diag_set_link = 1;
  315. ipath_cdbg(VERBOSE, "Trying to set to set link active for "
  316. "diag pkt\n");
  317. ipath_set_linkstate(dd, IPATH_IB_LINKARM);
  318. ipath_set_linkstate(dd, IPATH_IB_LINKACTIVE);
  319. }
  320. if (!(dd->ipath_flags & IPATH_INITTED)) {
  321. /* no hardware, freeze, etc. */
  322. ipath_cdbg(VERBOSE, "unit %u not usable\n", dd->ipath_unit);
  323. ret = -ENODEV;
  324. goto bail;
  325. }
  326. val = dd->ipath_lastibcstat & IPATH_IBSTATE_MASK;
  327. if (val != IPATH_IBSTATE_INIT && val != IPATH_IBSTATE_ARM &&
  328. val != IPATH_IBSTATE_ACTIVE) {
  329. ipath_cdbg(VERBOSE, "unit %u not ready (state %llx)\n",
  330. dd->ipath_unit, (unsigned long long) val);
  331. ret = -EINVAL;
  332. goto bail;
  333. }
  334. /* need total length before first word written */
  335. /* +1 word is for the qword padding */
  336. plen = sizeof(u32) + dp.len;
  337. if ((plen + 4) > dd->ipath_ibmaxlen) {
  338. ipath_dbg("Pkt len 0x%x > ibmaxlen %x\n",
  339. plen - 4, dd->ipath_ibmaxlen);
  340. ret = -EINVAL;
  341. goto bail; /* before writing pbc */
  342. }
  343. tmpbuf = vmalloc(plen);
  344. if (!tmpbuf) {
  345. dev_info(&dd->pcidev->dev, "Unable to allocate tmp buffer, "
  346. "failing\n");
  347. ret = -ENOMEM;
  348. goto bail;
  349. }
  350. if (copy_from_user(tmpbuf,
  351. (const void __user *) (unsigned long) dp.data,
  352. dp.len)) {
  353. ret = -EFAULT;
  354. goto bail;
  355. }
  356. piobuf = ipath_getpiobuf(dd, &pbufn);
  357. if (!piobuf) {
  358. ipath_cdbg(VERBOSE, "No PIO buffers avail unit for %u\n",
  359. dd->ipath_unit);
  360. ret = -EBUSY;
  361. goto bail;
  362. }
  363. plen >>= 2; /* in dwords */
  364. if (ipath_debug & __IPATH_PKTDBG)
  365. ipath_cdbg(VERBOSE, "unit %u 0x%x+1w pio%d\n",
  366. dd->ipath_unit, plen - 1, pbufn);
  367. /* we have to flush after the PBC for correctness on some cpus
  368. * or WC buffer can be written out of order */
  369. writeq(plen, piobuf);
  370. ipath_flush_wc();
  371. /* copy all by the trigger word, then flush, so it's written
  372. * to chip before trigger word, then write trigger word, then
  373. * flush again, so packet is sent. */
  374. __iowrite32_copy(piobuf + 2, tmpbuf, clen - 1);
  375. ipath_flush_wc();
  376. __raw_writel(tmpbuf[clen - 1], piobuf + clen + 1);
  377. ipath_flush_wc();
  378. ret = sizeof(dp);
  379. bail:
  380. vfree(tmpbuf);
  381. return ret;
  382. }
  383. static int ipath_diag_release(struct inode *in, struct file *fp)
  384. {
  385. mutex_lock(&ipath_mutex);
  386. ipath_diag_inuse = 0;
  387. fp->private_data = NULL;
  388. mutex_unlock(&ipath_mutex);
  389. return 0;
  390. }
  391. static ssize_t ipath_diag_read(struct file *fp, char __user *data,
  392. size_t count, loff_t *off)
  393. {
  394. struct ipath_devdata *dd = fp->private_data;
  395. void __iomem *kreg_base;
  396. ssize_t ret;
  397. kreg_base = dd->ipath_kregbase;
  398. if (count == 0)
  399. ret = 0;
  400. else if ((count % 4) || (*off % 4))
  401. /* address or length is not 32-bit aligned, hence invalid */
  402. ret = -EINVAL;
  403. else if ((count % 8) || (*off % 8))
  404. /* address or length not 64-bit aligned; do 32-bit reads */
  405. ret = ipath_read_umem32(dd, data, kreg_base + *off, count);
  406. else
  407. ret = ipath_read_umem64(dd, data, kreg_base + *off, count);
  408. if (ret >= 0) {
  409. *off += count;
  410. ret = count;
  411. }
  412. return ret;
  413. }
  414. static ssize_t ipath_diag_write(struct file *fp, const char __user *data,
  415. size_t count, loff_t *off)
  416. {
  417. struct ipath_devdata *dd = fp->private_data;
  418. void __iomem *kreg_base;
  419. ssize_t ret;
  420. kreg_base = dd->ipath_kregbase;
  421. if (count == 0)
  422. ret = 0;
  423. else if ((count % 4) || (*off % 4))
  424. /* address or length is not 32-bit aligned, hence invalid */
  425. ret = -EINVAL;
  426. else if ((count % 8) || (*off % 8))
  427. /* address or length not 64-bit aligned; do 32-bit writes */
  428. ret = ipath_write_umem32(dd, kreg_base + *off, data, count);
  429. else
  430. ret = ipath_write_umem64(dd, kreg_base + *off, data, count);
  431. if (ret >= 0) {
  432. *off += count;
  433. ret = count;
  434. }
  435. return ret;
  436. }