Pull bugzilla-5737 into release branch
[deliverable/linux.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
281 /* Only expose a way to reset the device if we
282 make it into diag mode. */
283 if (ret == 0)
284 ipath_expose_reset(&dd->pcidev->dev);
285
286 mutex_unlock(&ipath_mutex);
287
288 return ret;
289 }
290
291 static int ipath_diag_release(struct inode *i, struct file *f)
292 {
293 mutex_lock(&ipath_mutex);
294 ipath_diag_inuse = 0;
295 mutex_unlock(&ipath_mutex);
296 return 0;
297 }
298
299 static ssize_t ipath_diag_read(struct file *fp, char __user *data,
300 size_t count, loff_t *off)
301 {
302 int unit = 0; /* XXX provide for reads on other units some day */
303 struct ipath_devdata *dd;
304 void __iomem *kreg_base;
305 ssize_t ret;
306
307 dd = ipath_lookup(unit);
308 if (!dd) {
309 ret = -ENODEV;
310 goto bail;
311 }
312
313 kreg_base = dd->ipath_kregbase;
314
315 if (count == 0)
316 ret = 0;
317 else if ((count % 4) || (*off % 4))
318 /* address or length is not 32-bit aligned, hence invalid */
319 ret = -EINVAL;
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);
323 else
324 ret = ipath_read_umem64(dd, data, kreg_base + *off, count);
325
326 if (ret >= 0) {
327 *off += count;
328 ret = count;
329 }
330
331 bail:
332 return ret;
333 }
334
335 static ssize_t ipath_diag_write(struct file *fp, const char __user *data,
336 size_t count, loff_t *off)
337 {
338 int unit = 0; /* XXX this is bogus */
339 struct ipath_devdata *dd;
340 void __iomem *kreg_base;
341 ssize_t ret;
342
343 dd = ipath_lookup(unit);
344 if (!dd) {
345 ret = -ENODEV;
346 goto bail;
347 }
348 kreg_base = dd->ipath_kregbase;
349
350 if (count == 0)
351 ret = 0;
352 else if ((count % 4) || (*off % 4))
353 /* address or length is not 32-bit aligned, hence invalid */
354 ret = -EINVAL;
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);
358 else
359 ret = ipath_write_umem64(dd, kreg_base + *off, data, count);
360
361 if (ret >= 0) {
362 *off += count;
363 ret = count;
364 }
365
366 bail:
367 return ret;
368 }
This page took 0.057826 seconds and 6 git commands to generate.