Merge git://git.kernel.org/pub/scm/linux/kernel/git/sfrench/cifs-2.6
[deliverable/linux.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/io.h>
45 #include <linux/pci.h>
46 #include <asm/uaccess.h>
47
48 #include "ipath_kernel.h"
49 #include "ipath_common.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 int ipath_diag_add(struct ipath_devdata *dd)
70 {
71 char name[16];
72
73 snprintf(name, sizeof(name), "ipath_diag%d", dd->ipath_unit);
74
75 return ipath_cdev_init(IPATH_DIAG_MINOR_BASE + dd->ipath_unit, name,
76 &diag_file_ops, &dd->diag_cdev,
77 &dd->diag_class_dev);
78 }
79
80 void ipath_diag_remove(struct ipath_devdata *dd)
81 {
82 ipath_cdev_cleanup(&dd->diag_cdev, &dd->diag_class_dev);
83 }
84
85 /**
86 * ipath_read_umem64 - read a 64-bit quantity from the chip into user space
87 * @dd: the infinipath device
88 * @uaddr: the location to store the data in user memory
89 * @caddr: the source chip address (full pointer, not offset)
90 * @count: number of bytes to copy (multiple of 32 bits)
91 *
92 * This function also localizes all chip memory accesses.
93 * The copy should be written such that we read full cacheline packets
94 * from the chip. This is usually used for a single qword
95 *
96 * NOTE: This assumes the chip address is 64-bit aligned.
97 */
98 static int ipath_read_umem64(struct ipath_devdata *dd, void __user *uaddr,
99 const void __iomem *caddr, size_t count)
100 {
101 const u64 __iomem *reg_addr = caddr;
102 const u64 __iomem *reg_end = reg_addr + (count / sizeof(u64));
103 int ret;
104
105 /* not very efficient, but it works for now */
106 if (reg_addr < dd->ipath_kregbase || reg_end > dd->ipath_kregend) {
107 ret = -EINVAL;
108 goto bail;
109 }
110 while (reg_addr < reg_end) {
111 u64 data = readq(reg_addr);
112 if (copy_to_user(uaddr, &data, sizeof(u64))) {
113 ret = -EFAULT;
114 goto bail;
115 }
116 reg_addr++;
117 uaddr += sizeof(u64);
118 }
119 ret = 0;
120 bail:
121 return ret;
122 }
123
124 /**
125 * ipath_write_umem64 - write a 64-bit quantity to the chip from user space
126 * @dd: the infinipath device
127 * @caddr: the destination chip address (full pointer, not offset)
128 * @uaddr: the source of the data in user memory
129 * @count: the number of bytes to copy (multiple of 32 bits)
130 *
131 * This is usually used for a single qword
132 * NOTE: This assumes the chip address is 64-bit aligned.
133 */
134
135 static int ipath_write_umem64(struct ipath_devdata *dd, void __iomem *caddr,
136 const void __user *uaddr, size_t count)
137 {
138 u64 __iomem *reg_addr = caddr;
139 const u64 __iomem *reg_end = reg_addr + (count / sizeof(u64));
140 int ret;
141
142 /* not very efficient, but it works for now */
143 if (reg_addr < dd->ipath_kregbase || 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 += sizeof(u64);
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 += sizeof(u32);
195
196 }
197 ret = 0;
198 bail:
199 return ret;
200 }
201
202 /**
203 * ipath_write_umem32 - write a 32-bit quantity to the chip from user space
204 * @dd: the infinipath device
205 * @caddr: the destination chip address (full pointer, not offset)
206 * @uaddr: the source of the data in user memory
207 * @count: number of bytes to copy
208 *
209 * write 32 bit values, not 64 bit; for memories that only
210 * support 32 bit write; usually a single dword.
211 */
212
213 static int ipath_write_umem32(struct ipath_devdata *dd, void __iomem *caddr,
214 const void __user *uaddr, size_t count)
215 {
216 u32 __iomem *reg_addr = caddr;
217 const u32 __iomem *reg_end = reg_addr + (count / sizeof(u32));
218 int ret;
219
220 if (reg_addr < (u32 __iomem *) dd->ipath_kregbase ||
221 reg_end > (u32 __iomem *) dd->ipath_kregend) {
222 ret = -EINVAL;
223 goto bail;
224 }
225 while (reg_addr < reg_end) {
226 u32 data;
227 if (copy_from_user(&data, uaddr, sizeof(data))) {
228 ret = -EFAULT;
229 goto bail;
230 }
231 writel(data, reg_addr);
232
233 reg_addr++;
234 uaddr += sizeof(u32);
235 }
236 ret = 0;
237 bail:
238 return ret;
239 }
240
241 static int ipath_diag_open(struct inode *in, struct file *fp)
242 {
243 int unit = iminor(in) - IPATH_DIAG_MINOR_BASE;
244 struct ipath_devdata *dd;
245 int ret;
246
247 mutex_lock(&ipath_mutex);
248
249 if (ipath_diag_inuse) {
250 ret = -EBUSY;
251 goto bail;
252 }
253
254 dd = ipath_lookup(unit);
255
256 if (dd == NULL || !(dd->ipath_flags & IPATH_PRESENT) ||
257 !dd->ipath_kregbase) {
258 ret = -ENODEV;
259 goto bail;
260 }
261
262 fp->private_data = dd;
263 ipath_diag_inuse = 1;
264 diag_set_link = 0;
265 ret = 0;
266
267 /* Only expose a way to reset the device if we
268 make it into diag mode. */
269 ipath_expose_reset(&dd->pcidev->dev);
270
271 bail:
272 mutex_unlock(&ipath_mutex);
273
274 return ret;
275 }
276
277 static ssize_t ipath_diagpkt_write(struct file *fp,
278 const char __user *data,
279 size_t count, loff_t *off);
280
281 static struct file_operations diagpkt_file_ops = {
282 .owner = THIS_MODULE,
283 .write = ipath_diagpkt_write,
284 };
285
286 static struct cdev *diagpkt_cdev;
287 static struct class_device *diagpkt_class_dev;
288
289 int __init ipath_diagpkt_add(void)
290 {
291 return ipath_cdev_init(IPATH_DIAGPKT_MINOR,
292 "ipath_diagpkt", &diagpkt_file_ops,
293 &diagpkt_cdev, &diagpkt_class_dev);
294 }
295
296 void __exit ipath_diagpkt_remove(void)
297 {
298 ipath_cdev_cleanup(&diagpkt_cdev, &diagpkt_class_dev);
299 }
300
301 /**
302 * ipath_diagpkt_write - write an IB packet
303 * @fp: the diag data device file pointer
304 * @data: ipath_diag_pkt structure saying where to get the packet
305 * @count: size of data to write
306 * @off: unused by this code
307 */
308 static ssize_t ipath_diagpkt_write(struct file *fp,
309 const char __user *data,
310 size_t count, loff_t *off)
311 {
312 u32 __iomem *piobuf;
313 u32 plen, clen, pbufn;
314 struct ipath_diag_pkt dp;
315 u32 *tmpbuf = NULL;
316 struct ipath_devdata *dd;
317 ssize_t ret = 0;
318 u64 val;
319
320 if (count < sizeof(dp)) {
321 ret = -EINVAL;
322 goto bail;
323 }
324
325 if (copy_from_user(&dp, data, sizeof(dp))) {
326 ret = -EFAULT;
327 goto bail;
328 }
329
330 /* send count must be an exact number of dwords */
331 if (dp.len & 3) {
332 ret = -EINVAL;
333 goto bail;
334 }
335
336 clen = dp.len >> 2;
337
338 dd = ipath_lookup(dp.unit);
339 if (!dd || !(dd->ipath_flags & IPATH_PRESENT) ||
340 !dd->ipath_kregbase) {
341 ipath_cdbg(VERBOSE, "illegal unit %u for diag data send\n",
342 dp.unit);
343 ret = -ENODEV;
344 goto bail;
345 }
346
347 if (ipath_diag_inuse && !diag_set_link &&
348 !(dd->ipath_flags & IPATH_LINKACTIVE)) {
349 diag_set_link = 1;
350 ipath_cdbg(VERBOSE, "Trying to set to set link active for "
351 "diag pkt\n");
352 ipath_set_linkstate(dd, IPATH_IB_LINKARM);
353 ipath_set_linkstate(dd, IPATH_IB_LINKACTIVE);
354 }
355
356 if (!(dd->ipath_flags & IPATH_INITTED)) {
357 /* no hardware, freeze, etc. */
358 ipath_cdbg(VERBOSE, "unit %u not usable\n", dd->ipath_unit);
359 ret = -ENODEV;
360 goto bail;
361 }
362 val = dd->ipath_lastibcstat & IPATH_IBSTATE_MASK;
363 if (val != IPATH_IBSTATE_INIT && val != IPATH_IBSTATE_ARM &&
364 val != IPATH_IBSTATE_ACTIVE) {
365 ipath_cdbg(VERBOSE, "unit %u not ready (state %llx)\n",
366 dd->ipath_unit, (unsigned long long) val);
367 ret = -EINVAL;
368 goto bail;
369 }
370
371 /* need total length before first word written */
372 /* +1 word is for the qword padding */
373 plen = sizeof(u32) + dp.len;
374
375 if ((plen + 4) > dd->ipath_ibmaxlen) {
376 ipath_dbg("Pkt len 0x%x > ibmaxlen %x\n",
377 plen - 4, dd->ipath_ibmaxlen);
378 ret = -EINVAL;
379 goto bail; /* before writing pbc */
380 }
381 tmpbuf = vmalloc(plen);
382 if (!tmpbuf) {
383 dev_info(&dd->pcidev->dev, "Unable to allocate tmp buffer, "
384 "failing\n");
385 ret = -ENOMEM;
386 goto bail;
387 }
388
389 if (copy_from_user(tmpbuf,
390 (const void __user *) (unsigned long) dp.data,
391 dp.len)) {
392 ret = -EFAULT;
393 goto bail;
394 }
395
396 piobuf = ipath_getpiobuf(dd, &pbufn);
397 if (!piobuf) {
398 ipath_cdbg(VERBOSE, "No PIO buffers avail unit for %u\n",
399 dd->ipath_unit);
400 ret = -EBUSY;
401 goto bail;
402 }
403
404 plen >>= 2; /* in dwords */
405
406 if (ipath_debug & __IPATH_PKTDBG)
407 ipath_cdbg(VERBOSE, "unit %u 0x%x+1w pio%d\n",
408 dd->ipath_unit, plen - 1, pbufn);
409
410 /* we have to flush after the PBC for correctness on some cpus
411 * or WC buffer can be written out of order */
412 writeq(plen, piobuf);
413 ipath_flush_wc();
414 /* copy all by the trigger word, then flush, so it's written
415 * to chip before trigger word, then write trigger word, then
416 * flush again, so packet is sent. */
417 __iowrite32_copy(piobuf + 2, tmpbuf, clen - 1);
418 ipath_flush_wc();
419 __raw_writel(tmpbuf[clen - 1], piobuf + clen + 1);
420 ipath_flush_wc();
421
422 ret = sizeof(dp);
423
424 bail:
425 vfree(tmpbuf);
426 return ret;
427 }
428
429 static int ipath_diag_release(struct inode *in, struct file *fp)
430 {
431 mutex_lock(&ipath_mutex);
432 ipath_diag_inuse = 0;
433 fp->private_data = NULL;
434 mutex_unlock(&ipath_mutex);
435 return 0;
436 }
437
438 static ssize_t ipath_diag_read(struct file *fp, char __user *data,
439 size_t count, loff_t *off)
440 {
441 struct ipath_devdata *dd = fp->private_data;
442 void __iomem *kreg_base;
443 ssize_t ret;
444
445 kreg_base = dd->ipath_kregbase;
446
447 if (count == 0)
448 ret = 0;
449 else if ((count % 4) || (*off % 4))
450 /* address or length is not 32-bit aligned, hence invalid */
451 ret = -EINVAL;
452 else if ((count % 8) || (*off % 8))
453 /* address or length not 64-bit aligned; do 32-bit reads */
454 ret = ipath_read_umem32(dd, data, kreg_base + *off, count);
455 else
456 ret = ipath_read_umem64(dd, data, kreg_base + *off, count);
457
458 if (ret >= 0) {
459 *off += count;
460 ret = count;
461 }
462
463 return ret;
464 }
465
466 static ssize_t ipath_diag_write(struct file *fp, const char __user *data,
467 size_t count, loff_t *off)
468 {
469 struct ipath_devdata *dd = fp->private_data;
470 void __iomem *kreg_base;
471 ssize_t ret;
472
473 kreg_base = dd->ipath_kregbase;
474
475 if (count == 0)
476 ret = 0;
477 else if ((count % 4) || (*off % 4))
478 /* address or length is not 32-bit aligned, hence invalid */
479 ret = -EINVAL;
480 else if ((count % 8) || (*off % 8))
481 /* address or length not 64-bit aligned; do 32-bit writes */
482 ret = ipath_write_umem32(dd, kreg_base + *off, data, count);
483 else
484 ret = ipath_write_umem64(dd, kreg_base + *off, data, count);
485
486 if (ret >= 0) {
487 *off += count;
488 ret = count;
489 }
490
491 return ret;
492 }
This page took 0.041977 seconds and 6 git commands to generate.