ipath_diag.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. /*
  33. * This file contains support for diagnostic functions. It is accessed by
  34. * opening the ipath_diag device, normally minor number 129. Diagnostic use
  35. * of the InfiniPath chip may render the chip or board unusable until the
  36. * driver is unloaded, or in some cases, until the system is rebooted.
  37. *
  38. * Accesses to the chip through this interface are not similar to going
  39. * through the /sys/bus/pci resource mmap interface.
  40. */
  41. #include <linux/pci.h>
  42. #include <asm/uaccess.h>
  43. #include "ipath_common.h"
  44. #include "ipath_kernel.h"
  45. #include "ips_common.h"
  46. #include "ipath_layer.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. static struct cdev *diag_cdev;
  63. static struct class_device *diag_class_dev;
  64. int ipath_diag_init(void)
  65. {
  66. return ipath_cdev_init(IPATH_DIAG_MINOR, "ipath_diag",
  67. &diag_file_ops, &diag_cdev, &diag_class_dev);
  68. }
  69. void ipath_diag_cleanup(void)
  70. {
  71. ipath_cdev_cleanup(&diag_cdev, &diag_class_dev);
  72. }
  73. /**
  74. * ipath_read_umem64 - read a 64-bit quantity from the chip into user space
  75. * @dd: the infinipath device
  76. * @uaddr: the location to store the data in user memory
  77. * @caddr: the source chip address (full pointer, not offset)
  78. * @count: number of bytes to copy (multiple of 32 bits)
  79. *
  80. * This function also localizes all chip memory accesses.
  81. * The copy should be written such that we read full cacheline packets
  82. * from the chip. This is usually used for a single qword
  83. *
  84. * NOTE: This assumes the chip address is 64-bit aligned.
  85. */
  86. static int ipath_read_umem64(struct ipath_devdata *dd, void __user *uaddr,
  87. const void __iomem *caddr, size_t count)
  88. {
  89. const u64 __iomem *reg_addr = caddr;
  90. const u64 __iomem *reg_end = reg_addr + (count / sizeof(u64));
  91. int ret;
  92. /* not very efficient, but it works for now */
  93. if (reg_addr < dd->ipath_kregbase ||
  94. 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++;
  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 ||
  129. 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++;
  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++;
  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++;
  212. }
  213. ret = 0;
  214. bail:
  215. return ret;
  216. }
  217. static int ipath_diag_open(struct inode *in, struct file *fp)
  218. {
  219. struct ipath_devdata *dd;
  220. int unit = 0; /* XXX this is bogus */
  221. unsigned long flags;
  222. int ret;
  223. dd = ipath_lookup(unit);
  224. mutex_lock(&ipath_mutex);
  225. spin_lock_irqsave(&ipath_devs_lock, flags);
  226. if (ipath_diag_inuse) {
  227. ret = -EBUSY;
  228. goto bail;
  229. }
  230. list_for_each_entry(dd, &ipath_dev_list, ipath_list) {
  231. /*
  232. * we need at least one infinipath device to be present
  233. * (don't use INITTED, because we want to be able to open
  234. * even if device is in freeze mode, which cleared INITTED).
  235. * There is a small amount of risk to this, which is why we
  236. * also verify kregbase is set.
  237. */
  238. if (!(dd->ipath_flags & IPATH_PRESENT) ||
  239. !dd->ipath_kregbase)
  240. continue;
  241. ipath_diag_inuse = 1;
  242. diag_set_link = 0;
  243. ret = 0;
  244. goto bail;
  245. }
  246. ret = -ENODEV;
  247. bail:
  248. spin_unlock_irqrestore(&ipath_devs_lock, flags);
  249. mutex_unlock(&ipath_mutex);
  250. /* Only expose a way to reset the device if we
  251. make it into diag mode. */
  252. if (ret == 0)
  253. ipath_expose_reset(&dd->pcidev->dev);
  254. return ret;
  255. }
  256. static int ipath_diag_release(struct inode *i, struct file *f)
  257. {
  258. mutex_lock(&ipath_mutex);
  259. ipath_diag_inuse = 0;
  260. mutex_unlock(&ipath_mutex);
  261. return 0;
  262. }
  263. static ssize_t ipath_diag_read(struct file *fp, char __user *data,
  264. size_t count, loff_t *off)
  265. {
  266. int unit = 0; /* XXX provide for reads on other units some day */
  267. struct ipath_devdata *dd;
  268. void __iomem *kreg_base;
  269. ssize_t ret;
  270. dd = ipath_lookup(unit);
  271. if (!dd) {
  272. ret = -ENODEV;
  273. goto bail;
  274. }
  275. kreg_base = dd->ipath_kregbase;
  276. if (count == 0)
  277. ret = 0;
  278. else if ((count % 4) || (*off % 4))
  279. /* address or length is not 32-bit aligned, hence invalid */
  280. ret = -EINVAL;
  281. else if ((count % 8) || (*off % 8))
  282. /* address or length not 64-bit aligned; do 32-bit reads */
  283. ret = ipath_read_umem32(dd, data, kreg_base + *off, count);
  284. else
  285. ret = ipath_read_umem64(dd, data, kreg_base + *off, count);
  286. if (ret >= 0) {
  287. *off += count;
  288. ret = count;
  289. }
  290. bail:
  291. return ret;
  292. }
  293. static ssize_t ipath_diag_write(struct file *fp, const char __user *data,
  294. size_t count, loff_t *off)
  295. {
  296. int unit = 0; /* XXX this is bogus */
  297. struct ipath_devdata *dd;
  298. void __iomem *kreg_base;
  299. ssize_t ret;
  300. dd = ipath_lookup(unit);
  301. if (!dd) {
  302. ret = -ENODEV;
  303. goto bail;
  304. }
  305. kreg_base = dd->ipath_kregbase;
  306. if (count == 0)
  307. ret = 0;
  308. else if ((count % 4) || (*off % 4))
  309. /* address or length is not 32-bit aligned, hence invalid */
  310. ret = -EINVAL;
  311. else if ((count % 8) || (*off % 8))
  312. /* address or length not 64-bit aligned; do 32-bit writes */
  313. ret = ipath_write_umem32(dd, kreg_base + *off, data, count);
  314. else
  315. ret = ipath_write_umem64(dd, kreg_base + *off, data, count);
  316. if (ret >= 0) {
  317. *off += count;
  318. ret = count;
  319. }
  320. bail:
  321. return ret;
  322. }