]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/infiniband/hw/ipath/ipath_diag.c
IB/ipath: simplify layering code
[mv-sheeva.git] / drivers / infiniband / hw / ipath / ipath_diag.c
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 /*
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.
39  *
40  * Accesses to the chip through this interface are not similar to going
41  * through the /sys/bus/pci resource mmap interface.
42  */
43
44 #include <linux/pci.h>
45 #include <asm/uaccess.h>
46
47 #include "ipath_kernel.h"
48 #include "ipath_common.h"
49
50 int ipath_diag_inuse;
51 static int diag_set_link;
52
53 static int ipath_diag_open(struct inode *in, struct file *fp);
54 static int ipath_diag_release(struct inode *in, struct file *fp);
55 static ssize_t ipath_diag_read(struct file *fp, char __user *data,
56                                size_t count, loff_t *off);
57 static ssize_t ipath_diag_write(struct file *fp, const char __user *data,
58                                 size_t count, loff_t *off);
59
60 static struct file_operations diag_file_ops = {
61         .owner = THIS_MODULE,
62         .write = ipath_diag_write,
63         .read = ipath_diag_read,
64         .open = ipath_diag_open,
65         .release = ipath_diag_release
66 };
67
68 int ipath_diag_add(struct ipath_devdata *dd)
69 {
70         char name[16];
71
72         snprintf(name, sizeof(name), "ipath_diag%d", dd->ipath_unit);
73
74         return ipath_cdev_init(IPATH_DIAG_MINOR_BASE + dd->ipath_unit, name,
75                                &diag_file_ops, &dd->diag_cdev,
76                                &dd->diag_class_dev);
77 }
78
79 void ipath_diag_remove(struct ipath_devdata *dd)
80 {
81         ipath_cdev_cleanup(&dd->diag_cdev, &dd->diag_class_dev);
82 }
83
84 /**
85  * ipath_read_umem64 - read a 64-bit quantity from the chip into user space
86  * @dd: the infinipath device
87  * @uaddr: the location to store the data in user memory
88  * @caddr: the source chip address (full pointer, not offset)
89  * @count: number of bytes to copy (multiple of 32 bits)
90  *
91  * This function also localizes all chip memory accesses.
92  * The copy should be written such that we read full cacheline packets
93  * from the chip.  This is usually used for a single qword
94  *
95  * NOTE:  This assumes the chip address is 64-bit aligned.
96  */
97 static int ipath_read_umem64(struct ipath_devdata *dd, void __user *uaddr,
98                              const void __iomem *caddr, size_t count)
99 {
100         const u64 __iomem *reg_addr = caddr;
101         const u64 __iomem *reg_end = reg_addr + (count / sizeof(u64));
102         int ret;
103
104         /* not very efficient, but it works for now */
105         if (reg_addr < dd->ipath_kregbase || 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 += sizeof(u64);
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 || reg_end > dd->ipath_kregend) {
143                 ret = -EINVAL;
144                 goto bail;
145         }
146         while (reg_addr < reg_end) {
147                 u64 data;
148                 if (copy_from_user(&data, uaddr, sizeof(data))) {
149                         ret = -EFAULT;
150                         goto bail;
151                 }
152                 writeq(data, reg_addr);
153
154                 reg_addr++;
155                 uaddr += sizeof(u64);
156         }
157         ret = 0;
158 bail:
159         return ret;
160 }
161
162 /**
163  * ipath_read_umem32 - read a 32-bit quantity from the chip into user space
164  * @dd: the infinipath device
165  * @uaddr: the location to store the data in user memory
166  * @caddr: the source chip address (full pointer, not offset)
167  * @count: number of bytes to copy
168  *
169  * read 32 bit values, not 64 bit; for memories that only
170  * support 32 bit reads; usually a single dword.
171  */
172 static int ipath_read_umem32(struct ipath_devdata *dd, void __user *uaddr,
173                              const void __iomem *caddr, size_t count)
174 {
175         const u32 __iomem *reg_addr = caddr;
176         const u32 __iomem *reg_end = reg_addr + (count / sizeof(u32));
177         int ret;
178
179         if (reg_addr < (u32 __iomem *) dd->ipath_kregbase ||
180             reg_end > (u32 __iomem *) dd->ipath_kregend) {
181                 ret = -EINVAL;
182                 goto bail;
183         }
184         /* not very efficient, but it works for now */
185         while (reg_addr < reg_end) {
186                 u32 data = readl(reg_addr);
187                 if (copy_to_user(uaddr, &data, sizeof(data))) {
188                         ret = -EFAULT;
189                         goto bail;
190                 }
191
192                 reg_addr++;
193                 uaddr += sizeof(u32);
194
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 += sizeof(u32);
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         int unit = iminor(in) - IPATH_DIAG_MINOR_BASE;
243         struct ipath_devdata *dd;
244         int ret;
245
246         mutex_lock(&ipath_mutex);
247
248         if (ipath_diag_inuse) {
249                 ret = -EBUSY;
250                 goto bail;
251         }
252
253         dd = ipath_lookup(unit);
254
255         if (dd == NULL || !(dd->ipath_flags & IPATH_PRESENT) ||
256             !dd->ipath_kregbase) {
257                 ret = -ENODEV;
258                 goto bail;
259         }
260
261         fp->private_data = dd;
262         ipath_diag_inuse = 1;
263         diag_set_link = 0;
264         ret = 0;
265
266         /* Only expose a way to reset the device if we
267            make it into diag mode. */
268         ipath_expose_reset(&dd->pcidev->dev);
269
270 bail:
271         mutex_unlock(&ipath_mutex);
272
273         return ret;
274 }
275
276 static int ipath_diag_release(struct inode *in, struct file *fp)
277 {
278         mutex_lock(&ipath_mutex);
279         ipath_diag_inuse = 0;
280         fp->private_data = NULL;
281         mutex_unlock(&ipath_mutex);
282         return 0;
283 }
284
285 static ssize_t ipath_diag_read(struct file *fp, char __user *data,
286                                size_t count, loff_t *off)
287 {
288         struct ipath_devdata *dd = fp->private_data;
289         void __iomem *kreg_base;
290         ssize_t ret;
291
292         kreg_base = dd->ipath_kregbase;
293
294         if (count == 0)
295                 ret = 0;
296         else if ((count % 4) || (*off % 4))
297                 /* address or length is not 32-bit aligned, hence invalid */
298                 ret = -EINVAL;
299         else if ((count % 8) || (*off % 8))
300                 /* address or length not 64-bit aligned; do 32-bit reads */
301                 ret = ipath_read_umem32(dd, data, kreg_base + *off, count);
302         else
303                 ret = ipath_read_umem64(dd, data, kreg_base + *off, count);
304
305         if (ret >= 0) {
306                 *off += count;
307                 ret = count;
308         }
309
310         return ret;
311 }
312
313 static ssize_t ipath_diag_write(struct file *fp, const char __user *data,
314                                 size_t count, loff_t *off)
315 {
316         struct ipath_devdata *dd = fp->private_data;
317         void __iomem *kreg_base;
318         ssize_t ret;
319
320         kreg_base = dd->ipath_kregbase;
321
322         if (count == 0)
323                 ret = 0;
324         else if ((count % 4) || (*off % 4))
325                 /* address or length is not 32-bit aligned, hence invalid */
326                 ret = -EINVAL;
327         else if ((count % 8) || (*off % 8))
328                 /* address or length not 64-bit aligned; do 32-bit writes */
329                 ret = ipath_write_umem32(dd, kreg_base + *off, data, count);
330         else
331                 ret = ipath_write_umem64(dd, kreg_base + *off, data, count);
332
333         if (ret >= 0) {
334                 *off += count;
335                 ret = count;
336         }
337
338         return ret;
339 }