2 * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
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.
39 * Accesses to the chip through this interface are not similar to going
40 * through the /sys/bus/pci resource mmap interface.
43 #include <linux/pci.h>
44 #include <asm/uaccess.h>
46 #include "ipath_common.h"
47 #include "ipath_kernel.h"
48 #include "ips_common.h"
49 #include "ipath_layer.h"
52 static int diag_set_link;
54 static int ipath_diag_open(struct inode *in, struct file *fp);
55 static int ipath_diag_release(struct inode *in, struct file *fp);
56 static ssize_t ipath_diag_read(struct file *fp, char __user *data,
57 size_t count, loff_t *off);
58 static ssize_t ipath_diag_write(struct file *fp, const char __user *data,
59 size_t count, loff_t *off);
61 static struct file_operations diag_file_ops = {
63 .write = ipath_diag_write,
64 .read = ipath_diag_read,
65 .open = ipath_diag_open,
66 .release = ipath_diag_release
69 static struct cdev *diag_cdev;
70 static struct class_device *diag_class_dev;
72 int ipath_diag_init(void)
74 return ipath_cdev_init(IPATH_DIAG_MINOR, "ipath_diag",
75 &diag_file_ops, &diag_cdev, &diag_class_dev);
78 void ipath_diag_cleanup(void)
80 ipath_cdev_cleanup(&diag_cdev, &diag_class_dev);
84 * ipath_read_umem64 - read a 64-bit quantity from the chip into user space
85 * @dd: the infinipath device
86 * @uaddr: the location to store the data in user memory
87 * @caddr: the source chip address (full pointer, not offset)
88 * @count: number of bytes to copy (multiple of 32 bits)
90 * This function also localizes all chip memory accesses.
91 * The copy should be written such that we read full cacheline packets
92 * from the chip. This is usually used for a single qword
94 * NOTE: This assumes the chip address is 64-bit aligned.
96 static int ipath_read_umem64(struct ipath_devdata *dd, void __user *uaddr,
97 const void __iomem *caddr, size_t count)
99 const u64 __iomem *reg_addr = caddr;
100 const u64 __iomem *reg_end = reg_addr + (count / sizeof(u64));
103 /* not very efficient, but it works for now */
104 if (reg_addr < dd->ipath_kregbase ||
105 reg_end > dd->ipath_kregend) {
109 while (reg_addr < reg_end) {
110 u64 data = readq(reg_addr);
111 if (copy_to_user(uaddr, &data, sizeof(u64))) {
124 * ipath_write_umem64 - write a 64-bit quantity to the chip from user space
125 * @dd: the infinipath device
126 * @caddr: the destination chip address (full pointer, not offset)
127 * @uaddr: the source of the data in user memory
128 * @count: the number of bytes to copy (multiple of 32 bits)
130 * This is usually used for a single qword
131 * NOTE: This assumes the chip address is 64-bit aligned.
134 static int ipath_write_umem64(struct ipath_devdata *dd, void __iomem *caddr,
135 const void __user *uaddr, size_t count)
137 u64 __iomem *reg_addr = caddr;
138 const u64 __iomem *reg_end = reg_addr + (count / sizeof(u64));
141 /* not very efficient, but it works for now */
142 if (reg_addr < dd->ipath_kregbase ||
143 reg_end > dd->ipath_kregend) {
147 while (reg_addr < reg_end) {
149 if (copy_from_user(&data, uaddr, sizeof(data))) {
153 writeq(data, reg_addr);
164 * ipath_read_umem32 - read a 32-bit quantity from the chip into user space
165 * @dd: the infinipath device
166 * @uaddr: the location to store the data in user memory
167 * @caddr: the source chip address (full pointer, not offset)
168 * @count: number of bytes to copy
170 * read 32 bit values, not 64 bit; for memories that only
171 * support 32 bit reads; usually a single dword.
173 static int ipath_read_umem32(struct ipath_devdata *dd, void __user *uaddr,
174 const void __iomem *caddr, size_t count)
176 const u32 __iomem *reg_addr = caddr;
177 const u32 __iomem *reg_end = reg_addr + (count / sizeof(u32));
180 if (reg_addr < (u32 __iomem *) dd->ipath_kregbase ||
181 reg_end > (u32 __iomem *) dd->ipath_kregend) {
185 /* not very efficient, but it works for now */
186 while (reg_addr < reg_end) {
187 u32 data = readl(reg_addr);
188 if (copy_to_user(uaddr, &data, sizeof(data))) {
202 * ipath_write_umem32 - write a 32-bit quantity to the chip from user space
203 * @dd: the infinipath device
204 * @caddr: the destination chip address (full pointer, not offset)
205 * @uaddr: the source of the data in user memory
206 * @count: number of bytes to copy
208 * write 32 bit values, not 64 bit; for memories that only
209 * support 32 bit write; usually a single dword.
212 static int ipath_write_umem32(struct ipath_devdata *dd, void __iomem *caddr,
213 const void __user *uaddr, size_t count)
215 u32 __iomem *reg_addr = caddr;
216 const u32 __iomem *reg_end = reg_addr + (count / sizeof(u32));
219 if (reg_addr < (u32 __iomem *) dd->ipath_kregbase ||
220 reg_end > (u32 __iomem *) dd->ipath_kregend) {
224 while (reg_addr < reg_end) {
226 if (copy_from_user(&data, uaddr, sizeof(data))) {
230 writel(data, reg_addr);
240 static int ipath_diag_open(struct inode *in, struct file *fp)
242 struct ipath_devdata *dd;
243 int unit = 0; /* XXX this is bogus */
247 dd = ipath_lookup(unit);
249 mutex_lock(&ipath_mutex);
250 spin_lock_irqsave(&ipath_devs_lock, flags);
252 if (ipath_diag_inuse) {
257 list_for_each_entry(dd, &ipath_dev_list, ipath_list) {
259 * we need at least one infinipath device to be present
260 * (don't use INITTED, because we want to be able to open
261 * even if device is in freeze mode, which cleared INITTED).
262 * There is a small amount of risk to this, which is why we
263 * also verify kregbase is set.
266 if (!(dd->ipath_flags & IPATH_PRESENT) ||
270 ipath_diag_inuse = 1;
279 spin_unlock_irqrestore(&ipath_devs_lock, flags);
281 /* Only expose a way to reset the device if we
282 make it into diag mode. */
284 ipath_expose_reset(&dd->pcidev->dev);
286 mutex_unlock(&ipath_mutex);
291 static int ipath_diag_release(struct inode *i, struct file *f)
293 mutex_lock(&ipath_mutex);
294 ipath_diag_inuse = 0;
295 mutex_unlock(&ipath_mutex);
299 static ssize_t ipath_diag_read(struct file *fp, char __user *data,
300 size_t count, loff_t *off)
302 int unit = 0; /* XXX provide for reads on other units some day */
303 struct ipath_devdata *dd;
304 void __iomem *kreg_base;
307 dd = ipath_lookup(unit);
313 kreg_base = dd->ipath_kregbase;
317 else if ((count % 4) || (*off % 4))
318 /* address or length is not 32-bit aligned, hence invalid */
320 else if ((count % 8) || (*off % 8))
321 /* address or length not 64-bit aligned; do 32-bit reads */
322 ret = ipath_read_umem32(dd, data, kreg_base + *off, count);
324 ret = ipath_read_umem64(dd, data, kreg_base + *off, count);
335 static ssize_t ipath_diag_write(struct file *fp, const char __user *data,
336 size_t count, loff_t *off)
338 int unit = 0; /* XXX this is bogus */
339 struct ipath_devdata *dd;
340 void __iomem *kreg_base;
343 dd = ipath_lookup(unit);
348 kreg_base = dd->ipath_kregbase;
352 else if ((count % 4) || (*off % 4))
353 /* address or length is not 32-bit aligned, hence invalid */
355 else if ((count % 8) || (*off % 8))
356 /* address or length not 64-bit aligned; do 32-bit writes */
357 ret = ipath_write_umem32(dd, kreg_base + *off, data, count);
359 ret = ipath_write_umem64(dd, kreg_base + *off, data, count);