ipath_diag.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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/pci.h>
  43. #include <asm/uaccess.h>
  44. #include "ipath_kernel.h"
  45. #include "ipath_common.h"
  46. int ipath_diag_inuse;
  47. static int diag_set_link;
  48. static int ipath_diag_open(struct inode *in, struct file *fp);
  49. static int ipath_diag_release(struct inode *in, struct file *fp);
  50. static ssize_t ipath_diag_read(struct file *fp, char __user *data,
  51. size_t count, loff_t *off);
  52. static ssize_t ipath_diag_write(struct file *fp, const char __user *data,
  53. size_t count, loff_t *off);
  54. static struct file_operations diag_file_ops = {
  55. .owner = THIS_MODULE,
  56. .write = ipath_diag_write,
  57. .read = ipath_diag_read,
  58. .open = ipath_diag_open,
  59. .release = ipath_diag_release
  60. };
  61. int ipath_diag_add(struct ipath_devdata *dd)
  62. {
  63. char name[16];
  64. snprintf(name, sizeof(name), "ipath_diag%d", dd->ipath_unit);
  65. return ipath_cdev_init(IPATH_DIAG_MINOR_BASE + dd->ipath_unit, name,
  66. &diag_file_ops, &dd->diag_cdev,
  67. &dd->diag_class_dev);
  68. }
  69. void ipath_diag_remove(struct ipath_devdata *dd)
  70. {
  71. ipath_cdev_cleanup(&dd->diag_cdev, &dd->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 || reg_end > dd->ipath_kregend) {
  94. ret = -EINVAL;
  95. goto bail;
  96. }
  97. while (reg_addr < reg_end) {
  98. u64 data = readq(reg_addr);
  99. if (copy_to_user(uaddr, &data, sizeof(u64))) {
  100. ret = -EFAULT;
  101. goto bail;
  102. }
  103. reg_addr++;
  104. uaddr += sizeof(u64);
  105. }
  106. ret = 0;
  107. bail:
  108. return ret;
  109. }
  110. /**
  111. * ipath_write_umem64 - write a 64-bit quantity to the chip from user space
  112. * @dd: the infinipath device
  113. * @caddr: the destination chip address (full pointer, not offset)
  114. * @uaddr: the source of the data in user memory
  115. * @count: the number of bytes to copy (multiple of 32 bits)
  116. *
  117. * This is usually used for a single qword
  118. * NOTE: This assumes the chip address is 64-bit aligned.
  119. */
  120. static int ipath_write_umem64(struct ipath_devdata *dd, void __iomem *caddr,
  121. const void __user *uaddr, size_t count)
  122. {
  123. 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;
  133. if (copy_from_user(&data, uaddr, sizeof(data))) {
  134. ret = -EFAULT;
  135. goto bail;
  136. }
  137. writeq(data, reg_addr);
  138. reg_addr++;
  139. uaddr += sizeof(u64);
  140. }
  141. ret = 0;
  142. bail:
  143. return ret;
  144. }
  145. /**
  146. * ipath_read_umem32 - read a 32-bit quantity from the chip into user space
  147. * @dd: the infinipath device
  148. * @uaddr: the location to store the data in user memory
  149. * @caddr: the source chip address (full pointer, not offset)
  150. * @count: number of bytes to copy
  151. *
  152. * read 32 bit values, not 64 bit; for memories that only
  153. * support 32 bit reads; usually a single dword.
  154. */
  155. static int ipath_read_umem32(struct ipath_devdata *dd, void __user *uaddr,
  156. const void __iomem *caddr, size_t count)
  157. {
  158. const u32 __iomem *reg_addr = caddr;
  159. const u32 __iomem *reg_end = reg_addr + (count / sizeof(u32));
  160. int ret;
  161. if (reg_addr < (u32 __iomem *) dd->ipath_kregbase ||
  162. reg_end > (u32 __iomem *) dd->ipath_kregend) {
  163. ret = -EINVAL;
  164. goto bail;
  165. }
  166. /* not very efficient, but it works for now */
  167. while (reg_addr < reg_end) {
  168. u32 data = readl(reg_addr);
  169. if (copy_to_user(uaddr, &data, sizeof(data))) {
  170. ret = -EFAULT;
  171. goto bail;
  172. }
  173. reg_addr++;
  174. uaddr += sizeof(u32);
  175. }
  176. ret = 0;
  177. bail:
  178. return ret;
  179. }
  180. /**
  181. * ipath_write_umem32 - write a 32-bit quantity to the chip from user space
  182. * @dd: the infinipath device
  183. * @caddr: the destination chip address (full pointer, not offset)
  184. * @uaddr: the source of the data in user memory
  185. * @count: number of bytes to copy
  186. *
  187. * write 32 bit values, not 64 bit; for memories that only
  188. * support 32 bit write; usually a single dword.
  189. */
  190. static int ipath_write_umem32(struct ipath_devdata *dd, void __iomem *caddr,
  191. const void __user *uaddr, size_t count)
  192. {
  193. u32 __iomem *reg_addr = caddr;
  194. const u32 __iomem *reg_end = reg_addr + (count / sizeof(u32));
  195. int ret;
  196. if (reg_addr < (u32 __iomem *) dd->ipath_kregbase ||
  197. reg_end > (u32 __iomem *) dd->ipath_kregend) {
  198. ret = -EINVAL;
  199. goto bail;
  200. }
  201. while (reg_addr < reg_end) {
  202. u32 data;
  203. if (copy_from_user(&data, uaddr, sizeof(data))) {
  204. ret = -EFAULT;
  205. goto bail;
  206. }
  207. writel(data, reg_addr);
  208. reg_addr++;
  209. uaddr += sizeof(u32);
  210. }
  211. ret = 0;
  212. bail:
  213. return ret;
  214. }
  215. static int ipath_diag_open(struct inode *in, struct file *fp)
  216. {
  217. int unit = iminor(in) - IPATH_DIAG_MINOR_BASE;
  218. struct ipath_devdata *dd;
  219. int ret;
  220. mutex_lock(&ipath_mutex);
  221. if (ipath_diag_inuse) {
  222. ret = -EBUSY;
  223. goto bail;
  224. }
  225. dd = ipath_lookup(unit);
  226. if (dd == NULL || !(dd->ipath_flags & IPATH_PRESENT) ||
  227. !dd->ipath_kregbase) {
  228. ret = -ENODEV;
  229. goto bail;
  230. }
  231. fp->private_data = dd;
  232. ipath_diag_inuse = 1;
  233. diag_set_link = 0;
  234. ret = 0;
  235. /* Only expose a way to reset the device if we
  236. make it into diag mode. */
  237. ipath_expose_reset(&dd->pcidev->dev);
  238. bail:
  239. mutex_unlock(&ipath_mutex);
  240. return ret;
  241. }
  242. static int ipath_diag_release(struct inode *in, struct file *fp)
  243. {
  244. mutex_lock(&ipath_mutex);
  245. ipath_diag_inuse = 0;
  246. fp->private_data = NULL;
  247. mutex_unlock(&ipath_mutex);
  248. return 0;
  249. }
  250. static ssize_t ipath_diag_read(struct file *fp, char __user *data,
  251. size_t count, loff_t *off)
  252. {
  253. struct ipath_devdata *dd = fp->private_data;
  254. void __iomem *kreg_base;
  255. ssize_t ret;
  256. kreg_base = dd->ipath_kregbase;
  257. if (count == 0)
  258. ret = 0;
  259. else if ((count % 4) || (*off % 4))
  260. /* address or length is not 32-bit aligned, hence invalid */
  261. ret = -EINVAL;
  262. else if ((count % 8) || (*off % 8))
  263. /* address or length not 64-bit aligned; do 32-bit reads */
  264. ret = ipath_read_umem32(dd, data, kreg_base + *off, count);
  265. else
  266. ret = ipath_read_umem64(dd, data, kreg_base + *off, count);
  267. if (ret >= 0) {
  268. *off += count;
  269. ret = count;
  270. }
  271. return ret;
  272. }
  273. static ssize_t ipath_diag_write(struct file *fp, const char __user *data,
  274. size_t count, loff_t *off)
  275. {
  276. struct ipath_devdata *dd = fp->private_data;
  277. void __iomem *kreg_base;
  278. ssize_t ret;
  279. kreg_base = dd->ipath_kregbase;
  280. if (count == 0)
  281. ret = 0;
  282. else if ((count % 4) || (*off % 4))
  283. /* address or length is not 32-bit aligned, hence invalid */
  284. ret = -EINVAL;
  285. else if ((count % 8) || (*off % 8))
  286. /* address or length not 64-bit aligned; do 32-bit writes */
  287. ret = ipath_write_umem32(dd, kreg_base + *off, data, count);
  288. else
  289. ret = ipath_write_umem64(dd, kreg_base + *off, data, count);
  290. if (ret >= 0) {
  291. *off += count;
  292. ret = count;
  293. }
  294. return ret;
  295. }