Merge branch 'for_paulus' of master.kernel.org:/pub/scm/linux/kernel/git/galak/powerpc
[pandora-kernel.git] / drivers / infiniband / hw / ipath / ipath_diag.c
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 /*
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
43 #include <linux/pci.h>
44 #include <asm/uaccess.h>
45
46 #include "ipath_common.h"
47 #include "ipath_kernel.h"
48 #include "ips_common.h"
49 #include "ipath_layer.h"
50
51 int ipath_diag_inuse;
52 static int diag_set_link;
53
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);
60
61 static struct file_operations diag_file_ops = {
62         .owner = THIS_MODULE,
63         .write = ipath_diag_write,
64         .read = ipath_diag_read,
65         .open = ipath_diag_open,
66         .release = ipath_diag_release
67 };
68
69 static struct cdev *diag_cdev;
70 static struct class_device *diag_class_dev;
71
72 int ipath_diag_init(void)
73 {
74         return ipath_cdev_init(IPATH_DIAG_MINOR, "ipath_diag",
75                                &diag_file_ops, &diag_cdev, &diag_class_dev);
76 }
77
78 void ipath_diag_cleanup(void)
79 {
80         ipath_cdev_cleanup(&diag_cdev, &diag_class_dev);
81 }
82
83 /**
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)
89  *
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
93  *
94  * NOTE:  This assumes the chip address is 64-bit aligned.
95  */
96 static int ipath_read_umem64(struct ipath_devdata *dd, void __user *uaddr,
97                              const void __iomem *caddr, size_t count)
98 {
99         const u64 __iomem *reg_addr = caddr;
100         const u64 __iomem *reg_end = reg_addr + (count / sizeof(u64));
101         int ret;
102
103         /* not very efficient, but it works for now */
104         if (reg_addr < dd->ipath_kregbase ||
105             reg_end > dd->ipath_kregend) {
106                 ret = -EINVAL;
107                 goto bail;
108         }
109         while (reg_addr < reg_end) {
110                 u64 data = readq(reg_addr);
111                 if (copy_to_user(uaddr, &data, sizeof(u64))) {
112                         ret = -EFAULT;
113                         goto bail;
114                 }
115                 reg_addr++;
116                 uaddr++;
117         }
118         ret = 0;
119 bail:
120         return ret;
121 }
122
123 /**
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)
129  *
130  * This is usually used for a single qword
131  * NOTE:  This assumes the chip address is 64-bit aligned.
132  */
133
134 static int ipath_write_umem64(struct ipath_devdata *dd, void __iomem *caddr,
135                               const void __user *uaddr, size_t count)
136 {
137         u64 __iomem *reg_addr = caddr;
138         const u64 __iomem *reg_end = reg_addr + (count / sizeof(u64));
139         int ret;
140
141         /* not very efficient, but it works for now */
142         if (reg_addr < dd->ipath_kregbase ||
143             reg_end > dd->ipath_kregend) {
144                 ret = -EINVAL;
145                 goto bail;
146         }
147         while (reg_addr < reg_end) {
148                 u64 data;
149                 if (copy_from_user(&data, uaddr, sizeof(data))) {
150                         ret = -EFAULT;
151                         goto bail;
152                 }
153                 writeq(data, reg_addr);
154
155                 reg_addr++;
156                 uaddr++;
157         }
158         ret = 0;
159 bail:
160         return ret;
161 }
162
163 /**
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
169  *
170  * read 32 bit values, not 64 bit; for memories that only
171  * support 32 bit reads; usually a single dword.
172  */
173 static int ipath_read_umem32(struct ipath_devdata *dd, void __user *uaddr,
174                              const void __iomem *caddr, size_t count)
175 {
176         const u32 __iomem *reg_addr = caddr;
177         const u32 __iomem *reg_end = reg_addr + (count / sizeof(u32));
178         int ret;
179
180         if (reg_addr < (u32 __iomem *) dd->ipath_kregbase ||
181             reg_end > (u32 __iomem *) dd->ipath_kregend) {
182                 ret = -EINVAL;
183                 goto bail;
184         }
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))) {
189                         ret = -EFAULT;
190                         goto bail;
191                 }
192
193                 reg_addr++;
194                 uaddr++;
195         }
196         ret = 0;
197 bail:
198         return ret;
199 }
200
201 /**
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
207  *
208  * write 32 bit values, not 64 bit; for memories that only
209  * support 32 bit write; usually a single dword.
210  */
211
212 static int ipath_write_umem32(struct ipath_devdata *dd, void __iomem *caddr,
213                               const void __user *uaddr, size_t count)
214 {
215         u32 __iomem *reg_addr = caddr;
216         const u32 __iomem *reg_end = reg_addr + (count / sizeof(u32));
217         int ret;
218
219         if (reg_addr < (u32 __iomem *) dd->ipath_kregbase ||
220             reg_end > (u32 __iomem *) dd->ipath_kregend) {
221                 ret = -EINVAL;
222                 goto bail;
223         }
224         while (reg_addr < reg_end) {
225                 u32 data;
226                 if (copy_from_user(&data, uaddr, sizeof(data))) {
227                         ret = -EFAULT;
228                         goto bail;
229                 }
230                 writel(data, reg_addr);
231
232                 reg_addr++;
233                 uaddr++;
234         }
235         ret = 0;
236 bail:
237         return ret;
238 }
239
240 static int ipath_diag_open(struct inode *in, struct file *fp)
241 {
242         struct ipath_devdata *dd;
243         int unit = 0; /* XXX this is bogus */
244         unsigned long flags;
245         int ret;
246
247         dd = ipath_lookup(unit);
248
249         mutex_lock(&ipath_mutex);
250         spin_lock_irqsave(&ipath_devs_lock, flags);
251
252         if (ipath_diag_inuse) {
253                 ret = -EBUSY;
254                 goto bail;
255         }
256
257         list_for_each_entry(dd, &ipath_dev_list, ipath_list) {
258                 /*
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.
264                  */
265
266                 if (!(dd->ipath_flags & IPATH_PRESENT) ||
267                     !dd->ipath_kregbase)
268                         continue;
269
270                 ipath_diag_inuse = 1;
271                 diag_set_link = 0;
272                 ret = 0;
273                 goto bail;
274         }
275
276         ret = -ENODEV;
277
278 bail:
279         spin_unlock_irqrestore(&ipath_devs_lock, flags);
280         mutex_unlock(&ipath_mutex);
281
282         /* Only expose a way to reset the device if we
283            make it into diag mode. */
284         if (ret == 0)
285                 ipath_expose_reset(&dd->pcidev->dev);
286
287         return ret;
288 }
289
290 static int ipath_diag_release(struct inode *i, struct file *f)
291 {
292         mutex_lock(&ipath_mutex);
293         ipath_diag_inuse = 0;
294         mutex_unlock(&ipath_mutex);
295         return 0;
296 }
297
298 static ssize_t ipath_diag_read(struct file *fp, char __user *data,
299                                size_t count, loff_t *off)
300 {
301         int unit = 0; /* XXX provide for reads on other units some day */
302         struct ipath_devdata *dd;
303         void __iomem *kreg_base;
304         ssize_t ret;
305
306         dd = ipath_lookup(unit);
307         if (!dd) {
308                 ret = -ENODEV;
309                 goto bail;
310         }
311
312         kreg_base = dd->ipath_kregbase;
313
314         if (count == 0)
315                 ret = 0;
316         else if ((count % 4) || (*off % 4))
317                 /* address or length is not 32-bit aligned, hence invalid */
318                 ret = -EINVAL;
319         else if ((count % 8) || (*off % 8))
320                 /* address or length not 64-bit aligned; do 32-bit reads */
321                 ret = ipath_read_umem32(dd, data, kreg_base + *off, count);
322         else
323                 ret = ipath_read_umem64(dd, data, kreg_base + *off, count);
324
325         if (ret >= 0) {
326                 *off += count;
327                 ret = count;
328         }
329
330 bail:
331         return ret;
332 }
333
334 static ssize_t ipath_diag_write(struct file *fp, const char __user *data,
335                                 size_t count, loff_t *off)
336 {
337         int unit = 0; /* XXX this is bogus */
338         struct ipath_devdata *dd;
339         void __iomem *kreg_base;
340         ssize_t ret;
341
342         dd = ipath_lookup(unit);
343         if (!dd) {
344                 ret = -ENODEV;
345                 goto bail;
346         }
347         kreg_base = dd->ipath_kregbase;
348
349         if (count == 0)
350                 ret = 0;
351         else if ((count % 4) || (*off % 4))
352                 /* address or length is not 32-bit aligned, hence invalid */
353                 ret = -EINVAL;
354         else if ((count % 8) || (*off % 8))
355                 /* address or length not 64-bit aligned; do 32-bit writes */
356                 ret = ipath_write_umem32(dd, kreg_base + *off, data, count);
357         else
358                 ret = ipath_write_umem64(dd, kreg_base + *off, data, count);
359
360         if (ret >= 0) {
361                 *off += count;
362                 ret = count;
363         }
364
365 bail:
366         return ret;
367 }