ipath_diag.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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_layer.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 int ipath_diag_release(struct inode *in, struct file *fp)
  244. {
  245. mutex_lock(&ipath_mutex);
  246. ipath_diag_inuse = 0;
  247. fp->private_data = NULL;
  248. mutex_unlock(&ipath_mutex);
  249. return 0;
  250. }
  251. static ssize_t ipath_diag_read(struct file *fp, char __user *data,
  252. size_t count, loff_t *off)
  253. {
  254. struct ipath_devdata *dd = fp->private_data;
  255. void __iomem *kreg_base;
  256. ssize_t ret;
  257. kreg_base = dd->ipath_kregbase;
  258. if (count == 0)
  259. ret = 0;
  260. else if ((count % 4) || (*off % 4))
  261. /* address or length is not 32-bit aligned, hence invalid */
  262. ret = -EINVAL;
  263. else if ((count % 8) || (*off % 8))
  264. /* address or length not 64-bit aligned; do 32-bit reads */
  265. ret = ipath_read_umem32(dd, data, kreg_base + *off, count);
  266. else
  267. ret = ipath_read_umem64(dd, data, kreg_base + *off, count);
  268. if (ret >= 0) {
  269. *off += count;
  270. ret = count;
  271. }
  272. return ret;
  273. }
  274. static ssize_t ipath_diag_write(struct file *fp, const char __user *data,
  275. size_t count, loff_t *off)
  276. {
  277. struct ipath_devdata *dd = fp->private_data;
  278. void __iomem *kreg_base;
  279. ssize_t ret;
  280. kreg_base = dd->ipath_kregbase;
  281. if (count == 0)
  282. ret = 0;
  283. else if ((count % 4) || (*off % 4))
  284. /* address or length is not 32-bit aligned, hence invalid */
  285. ret = -EINVAL;
  286. else if ((count % 8) || (*off % 8))
  287. /* address or length not 64-bit aligned; do 32-bit writes */
  288. ret = ipath_write_umem32(dd, kreg_base + *off, data, count);
  289. else
  290. ret = ipath_write_umem64(dd, kreg_base + *off, data, count);
  291. if (ret >= 0) {
  292. *off += count;
  293. ret = count;
  294. }
  295. return ret;
  296. }