[PATCH] IB/ipath: read/write correct sizes through diag interface
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / infiniband / hw / ipath / ipath_diag.c
blob8a0425e98b5f6d201c6921e7da59ff43530781d7
1 /*
2 * Copyright (c) 2006 QLogic, Inc. All rights reserved.
3 * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
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:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
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.
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.
35 * This file contains support for diagnostic functions. It is accessed by
36 * opening the ipath_diag device, normally minor number 129. Diagnostic use
37 * of the InfiniPath chip may render the chip or board unusable until the
38 * driver is unloaded, or in some cases, until the system is rebooted.
40 * Accesses to the chip through this interface are not similar to going
41 * through the /sys/bus/pci resource mmap interface.
44 #include <linux/pci.h>
45 #include <asm/uaccess.h>
47 #include "ipath_common.h"
48 #include "ipath_kernel.h"
49 #include "ips_common.h"
50 #include "ipath_layer.h"
52 int ipath_diag_inuse;
53 static int diag_set_link;
55 static int ipath_diag_open(struct inode *in, struct file *fp);
56 static int ipath_diag_release(struct inode *in, struct file *fp);
57 static ssize_t ipath_diag_read(struct file *fp, char __user *data,
58 size_t count, loff_t *off);
59 static ssize_t ipath_diag_write(struct file *fp, const char __user *data,
60 size_t count, loff_t *off);
62 static struct file_operations diag_file_ops = {
63 .owner = THIS_MODULE,
64 .write = ipath_diag_write,
65 .read = ipath_diag_read,
66 .open = ipath_diag_open,
67 .release = ipath_diag_release
70 int ipath_diag_add(struct ipath_devdata *dd)
72 char name[16];
74 snprintf(name, sizeof(name), "ipath_diag%d", dd->ipath_unit);
76 return ipath_cdev_init(IPATH_DIAG_MINOR_BASE + dd->ipath_unit, name,
77 &diag_file_ops, &dd->diag_cdev,
78 &dd->diag_class_dev);
81 void ipath_diag_remove(struct ipath_devdata *dd)
83 ipath_cdev_cleanup(&dd->diag_cdev, &dd->diag_class_dev);
86 /**
87 * ipath_read_umem64 - read a 64-bit quantity from the chip into user space
88 * @dd: the infinipath device
89 * @uaddr: the location to store the data in user memory
90 * @caddr: the source chip address (full pointer, not offset)
91 * @count: number of bytes to copy (multiple of 32 bits)
93 * This function also localizes all chip memory accesses.
94 * The copy should be written such that we read full cacheline packets
95 * from the chip. This is usually used for a single qword
97 * NOTE: This assumes the chip address is 64-bit aligned.
99 static int ipath_read_umem64(struct ipath_devdata *dd, void __user *uaddr,
100 const void __iomem *caddr, size_t count)
102 const u64 __iomem *reg_addr = caddr;
103 const u64 __iomem *reg_end = reg_addr + (count / sizeof(u64));
104 int ret;
106 /* not very efficient, but it works for now */
107 if (reg_addr < dd->ipath_kregbase || reg_end > dd->ipath_kregend) {
108 ret = -EINVAL;
109 goto bail;
111 while (reg_addr < reg_end) {
112 u64 data = readq(reg_addr);
113 if (copy_to_user(uaddr, &data, sizeof(u64))) {
114 ret = -EFAULT;
115 goto bail;
117 reg_addr++;
118 uaddr += sizeof(u64);
120 ret = 0;
121 bail:
122 return ret;
126 * ipath_write_umem64 - write a 64-bit quantity to the chip from user space
127 * @dd: the infinipath device
128 * @caddr: the destination chip address (full pointer, not offset)
129 * @uaddr: the source of the data in user memory
130 * @count: the number of bytes to copy (multiple of 32 bits)
132 * This is usually used for a single qword
133 * NOTE: This assumes the chip address is 64-bit aligned.
136 static int ipath_write_umem64(struct ipath_devdata *dd, void __iomem *caddr,
137 const void __user *uaddr, size_t count)
139 u64 __iomem *reg_addr = caddr;
140 const u64 __iomem *reg_end = reg_addr + (count / sizeof(u64));
141 int ret;
143 /* not very efficient, but it works for now */
144 if (reg_addr < dd->ipath_kregbase || reg_end > dd->ipath_kregend) {
145 ret = -EINVAL;
146 goto bail;
148 while (reg_addr < reg_end) {
149 u64 data;
150 if (copy_from_user(&data, uaddr, sizeof(data))) {
151 ret = -EFAULT;
152 goto bail;
154 writeq(data, reg_addr);
156 reg_addr++;
157 uaddr += sizeof(u64);
159 ret = 0;
160 bail:
161 return ret;
165 * ipath_read_umem32 - read a 32-bit quantity from the chip into user space
166 * @dd: the infinipath device
167 * @uaddr: the location to store the data in user memory
168 * @caddr: the source chip address (full pointer, not offset)
169 * @count: number of bytes to copy
171 * read 32 bit values, not 64 bit; for memories that only
172 * support 32 bit reads; usually a single dword.
174 static int ipath_read_umem32(struct ipath_devdata *dd, void __user *uaddr,
175 const void __iomem *caddr, size_t count)
177 const u32 __iomem *reg_addr = caddr;
178 const u32 __iomem *reg_end = reg_addr + (count / sizeof(u32));
179 int ret;
181 if (reg_addr < (u32 __iomem *) dd->ipath_kregbase ||
182 reg_end > (u32 __iomem *) dd->ipath_kregend) {
183 ret = -EINVAL;
184 goto bail;
186 /* not very efficient, but it works for now */
187 while (reg_addr < reg_end) {
188 u32 data = readl(reg_addr);
189 if (copy_to_user(uaddr, &data, sizeof(data))) {
190 ret = -EFAULT;
191 goto bail;
194 reg_addr++;
195 uaddr += sizeof(u32);
198 ret = 0;
199 bail:
200 return ret;
204 * ipath_write_umem32 - write a 32-bit quantity to the chip from user space
205 * @dd: the infinipath device
206 * @caddr: the destination chip address (full pointer, not offset)
207 * @uaddr: the source of the data in user memory
208 * @count: number of bytes to copy
210 * write 32 bit values, not 64 bit; for memories that only
211 * support 32 bit write; usually a single dword.
214 static int ipath_write_umem32(struct ipath_devdata *dd, void __iomem *caddr,
215 const void __user *uaddr, size_t count)
217 u32 __iomem *reg_addr = caddr;
218 const u32 __iomem *reg_end = reg_addr + (count / sizeof(u32));
219 int ret;
221 if (reg_addr < (u32 __iomem *) dd->ipath_kregbase ||
222 reg_end > (u32 __iomem *) dd->ipath_kregend) {
223 ret = -EINVAL;
224 goto bail;
226 while (reg_addr < reg_end) {
227 u32 data;
228 if (copy_from_user(&data, uaddr, sizeof(data))) {
229 ret = -EFAULT;
230 goto bail;
232 writel(data, reg_addr);
234 reg_addr++;
235 uaddr += sizeof(u32);
237 ret = 0;
238 bail:
239 return ret;
242 static int ipath_diag_open(struct inode *in, struct file *fp)
244 int unit = iminor(in) - IPATH_DIAG_MINOR_BASE;
245 struct ipath_devdata *dd;
246 int ret;
248 mutex_lock(&ipath_mutex);
250 if (ipath_diag_inuse) {
251 ret = -EBUSY;
252 goto bail;
255 dd = ipath_lookup(unit);
257 if (dd == NULL || !(dd->ipath_flags & IPATH_PRESENT) ||
258 !dd->ipath_kregbase) {
259 ret = -ENODEV;
260 goto bail;
263 fp->private_data = dd;
264 ipath_diag_inuse = 1;
265 diag_set_link = 0;
266 ret = 0;
268 /* Only expose a way to reset the device if we
269 make it into diag mode. */
270 ipath_expose_reset(&dd->pcidev->dev);
272 bail:
273 mutex_unlock(&ipath_mutex);
275 return ret;
278 static int ipath_diag_release(struct inode *in, struct file *fp)
280 mutex_lock(&ipath_mutex);
281 ipath_diag_inuse = 0;
282 fp->private_data = NULL;
283 mutex_unlock(&ipath_mutex);
284 return 0;
287 static ssize_t ipath_diag_read(struct file *fp, char __user *data,
288 size_t count, loff_t *off)
290 struct ipath_devdata *dd = fp->private_data;
291 void __iomem *kreg_base;
292 ssize_t ret;
294 kreg_base = dd->ipath_kregbase;
296 if (count == 0)
297 ret = 0;
298 else if ((count % 4) || (*off % 4))
299 /* address or length is not 32-bit aligned, hence invalid */
300 ret = -EINVAL;
301 else if ((count % 8) || (*off % 8))
302 /* address or length not 64-bit aligned; do 32-bit reads */
303 ret = ipath_read_umem32(dd, data, kreg_base + *off, count);
304 else
305 ret = ipath_read_umem64(dd, data, kreg_base + *off, count);
307 if (ret >= 0) {
308 *off += count;
309 ret = count;
312 return ret;
315 static ssize_t ipath_diag_write(struct file *fp, const char __user *data,
316 size_t count, loff_t *off)
318 struct ipath_devdata *dd = fp->private_data;
319 void __iomem *kreg_base;
320 ssize_t ret;
322 kreg_base = dd->ipath_kregbase;
324 if (count == 0)
325 ret = 0;
326 else if ((count % 4) || (*off % 4))
327 /* address or length is not 32-bit aligned, hence invalid */
328 ret = -EINVAL;
329 else if ((count % 8) || (*off % 8))
330 /* address or length not 64-bit aligned; do 32-bit writes */
331 ret = ipath_write_umem32(dd, kreg_base + *off, data, count);
332 else
333 ret = ipath_write_umem64(dd, kreg_base + *off, data, count);
335 if (ret >= 0) {
336 *off += count;
337 ret = count;
340 return ret;