Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / drivers / infiniband / hw / ipath / ipath_diag.c
blob4137c7770f1bce083d7342508005c9497e5a91cd
1 /*
2 * Copyright (c) 2006, 2007 QLogic Corporation. 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/io.h>
45 #include <linux/pci.h>
46 #include <linux/vmalloc.h>
47 #include <linux/fs.h>
48 #include <asm/uaccess.h>
50 #include "ipath_kernel.h"
51 #include "ipath_common.h"
53 int ipath_diag_inuse;
54 static int diag_set_link;
56 static int ipath_diag_open(struct inode *in, struct file *fp);
57 static int ipath_diag_release(struct inode *in, struct file *fp);
58 static ssize_t ipath_diag_read(struct file *fp, char __user *data,
59 size_t count, loff_t *off);
60 static ssize_t ipath_diag_write(struct file *fp, const char __user *data,
61 size_t count, loff_t *off);
63 static const struct file_operations diag_file_ops = {
64 .owner = THIS_MODULE,
65 .write = ipath_diag_write,
66 .read = ipath_diag_read,
67 .open = ipath_diag_open,
68 .release = ipath_diag_release
71 static ssize_t ipath_diagpkt_write(struct file *fp,
72 const char __user *data,
73 size_t count, loff_t *off);
75 static const struct file_operations diagpkt_file_ops = {
76 .owner = THIS_MODULE,
77 .write = ipath_diagpkt_write,
80 static atomic_t diagpkt_count = ATOMIC_INIT(0);
81 static struct cdev *diagpkt_cdev;
82 static struct class_device *diagpkt_class_dev;
84 int ipath_diag_add(struct ipath_devdata *dd)
86 char name[16];
87 int ret = 0;
89 if (atomic_inc_return(&diagpkt_count) == 1) {
90 ret = ipath_cdev_init(IPATH_DIAGPKT_MINOR,
91 "ipath_diagpkt", &diagpkt_file_ops,
92 &diagpkt_cdev, &diagpkt_class_dev);
94 if (ret) {
95 ipath_dev_err(dd, "Couldn't create ipath_diagpkt "
96 "device: %d", ret);
97 goto done;
101 snprintf(name, sizeof(name), "ipath_diag%d", dd->ipath_unit);
103 ret = ipath_cdev_init(IPATH_DIAG_MINOR_BASE + dd->ipath_unit, name,
104 &diag_file_ops, &dd->diag_cdev,
105 &dd->diag_class_dev);
106 if (ret)
107 ipath_dev_err(dd, "Couldn't create %s device: %d",
108 name, ret);
110 done:
111 return ret;
114 void ipath_diag_remove(struct ipath_devdata *dd)
116 if (atomic_dec_and_test(&diagpkt_count))
117 ipath_cdev_cleanup(&diagpkt_cdev, &diagpkt_class_dev);
119 ipath_cdev_cleanup(&dd->diag_cdev, &dd->diag_class_dev);
123 * ipath_read_umem64 - read a 64-bit quantity from the chip into user space
124 * @dd: the infinipath device
125 * @uaddr: the location to store the data in user memory
126 * @caddr: the source chip address (full pointer, not offset)
127 * @count: number of bytes to copy (multiple of 32 bits)
129 * This function also localizes all chip memory accesses.
130 * The copy should be written such that we read full cacheline packets
131 * from the chip. This is usually used for a single qword
133 * NOTE: This assumes the chip address is 64-bit aligned.
135 static int ipath_read_umem64(struct ipath_devdata *dd, void __user *uaddr,
136 const void __iomem *caddr, size_t count)
138 const u64 __iomem *reg_addr = caddr;
139 const u64 __iomem *reg_end = reg_addr + (count / sizeof(u64));
140 int ret;
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;
147 while (reg_addr < reg_end) {
148 u64 data = readq(reg_addr);
149 if (copy_to_user(uaddr, &data, sizeof(u64))) {
150 ret = -EFAULT;
151 goto bail;
153 reg_addr++;
154 uaddr += sizeof(u64);
156 ret = 0;
157 bail:
158 return ret;
162 * ipath_write_umem64 - write a 64-bit quantity to the chip from user space
163 * @dd: the infinipath device
164 * @caddr: the destination chip address (full pointer, not offset)
165 * @uaddr: the source of the data in user memory
166 * @count: the number of bytes to copy (multiple of 32 bits)
168 * This is usually used for a single qword
169 * NOTE: This assumes the chip address is 64-bit aligned.
172 static int ipath_write_umem64(struct ipath_devdata *dd, void __iomem *caddr,
173 const void __user *uaddr, size_t count)
175 u64 __iomem *reg_addr = caddr;
176 const u64 __iomem *reg_end = reg_addr + (count / sizeof(u64));
177 int ret;
179 /* not very efficient, but it works for now */
180 if (reg_addr < dd->ipath_kregbase || reg_end > dd->ipath_kregend) {
181 ret = -EINVAL;
182 goto bail;
184 while (reg_addr < reg_end) {
185 u64 data;
186 if (copy_from_user(&data, uaddr, sizeof(data))) {
187 ret = -EFAULT;
188 goto bail;
190 writeq(data, reg_addr);
192 reg_addr++;
193 uaddr += sizeof(u64);
195 ret = 0;
196 bail:
197 return ret;
201 * ipath_read_umem32 - read a 32-bit quantity from the chip into user space
202 * @dd: the infinipath device
203 * @uaddr: the location to store the data in user memory
204 * @caddr: the source chip address (full pointer, not offset)
205 * @count: number of bytes to copy
207 * read 32 bit values, not 64 bit; for memories that only
208 * support 32 bit reads; usually a single dword.
210 static int ipath_read_umem32(struct ipath_devdata *dd, void __user *uaddr,
211 const void __iomem *caddr, size_t count)
213 const u32 __iomem *reg_addr = caddr;
214 const u32 __iomem *reg_end = reg_addr + (count / sizeof(u32));
215 int ret;
217 if (reg_addr < (u32 __iomem *) dd->ipath_kregbase ||
218 reg_end > (u32 __iomem *) dd->ipath_kregend) {
219 ret = -EINVAL;
220 goto bail;
222 /* not very efficient, but it works for now */
223 while (reg_addr < reg_end) {
224 u32 data = readl(reg_addr);
225 if (copy_to_user(uaddr, &data, sizeof(data))) {
226 ret = -EFAULT;
227 goto bail;
230 reg_addr++;
231 uaddr += sizeof(u32);
234 ret = 0;
235 bail:
236 return ret;
240 * ipath_write_umem32 - write a 32-bit quantity to the chip from user space
241 * @dd: the infinipath device
242 * @caddr: the destination chip address (full pointer, not offset)
243 * @uaddr: the source of the data in user memory
244 * @count: number of bytes to copy
246 * write 32 bit values, not 64 bit; for memories that only
247 * support 32 bit write; usually a single dword.
250 static int ipath_write_umem32(struct ipath_devdata *dd, void __iomem *caddr,
251 const void __user *uaddr, size_t count)
253 u32 __iomem *reg_addr = caddr;
254 const u32 __iomem *reg_end = reg_addr + (count / sizeof(u32));
255 int ret;
257 if (reg_addr < (u32 __iomem *) dd->ipath_kregbase ||
258 reg_end > (u32 __iomem *) dd->ipath_kregend) {
259 ret = -EINVAL;
260 goto bail;
262 while (reg_addr < reg_end) {
263 u32 data;
264 if (copy_from_user(&data, uaddr, sizeof(data))) {
265 ret = -EFAULT;
266 goto bail;
268 writel(data, reg_addr);
270 reg_addr++;
271 uaddr += sizeof(u32);
273 ret = 0;
274 bail:
275 return ret;
278 static int ipath_diag_open(struct inode *in, struct file *fp)
280 int unit = iminor(in) - IPATH_DIAG_MINOR_BASE;
281 struct ipath_devdata *dd;
282 int ret;
284 mutex_lock(&ipath_mutex);
286 if (ipath_diag_inuse) {
287 ret = -EBUSY;
288 goto bail;
291 dd = ipath_lookup(unit);
293 if (dd == NULL || !(dd->ipath_flags & IPATH_PRESENT) ||
294 !dd->ipath_kregbase) {
295 ret = -ENODEV;
296 goto bail;
299 fp->private_data = dd;
300 ipath_diag_inuse = -2;
301 diag_set_link = 0;
302 ret = 0;
304 /* Only expose a way to reset the device if we
305 make it into diag mode. */
306 ipath_expose_reset(&dd->pcidev->dev);
308 bail:
309 mutex_unlock(&ipath_mutex);
311 return ret;
315 * ipath_diagpkt_write - write an IB packet
316 * @fp: the diag data device file pointer
317 * @data: ipath_diag_pkt structure saying where to get the packet
318 * @count: size of data to write
319 * @off: unused by this code
321 static ssize_t ipath_diagpkt_write(struct file *fp,
322 const char __user *data,
323 size_t count, loff_t *off)
325 u32 __iomem *piobuf;
326 u32 plen, clen, pbufn;
327 struct ipath_diag_pkt odp;
328 struct ipath_diag_xpkt dp;
329 u32 *tmpbuf = NULL;
330 struct ipath_devdata *dd;
331 ssize_t ret = 0;
332 u64 val;
334 if (count != sizeof(dp)) {
335 ret = -EINVAL;
336 goto bail;
339 if (copy_from_user(&dp, data, sizeof(dp))) {
340 ret = -EFAULT;
341 goto bail;
345 * Due to padding/alignment issues (lessened with new struct)
346 * the old and new structs are the same length. We need to
347 * disambiguate them, which we can do because odp.len has never
348 * been less than the total of LRH+BTH+DETH so far, while
349 * dp.unit (same offset) unit is unlikely to get that high.
350 * Similarly, dp.data, the pointer to user at the same offset
351 * as odp.unit, is almost certainly at least one (512byte)page
352 * "above" NULL. The if-block below can be omitted if compatibility
353 * between a new driver and older diagnostic code is unimportant.
354 * compatibility the other direction (new diags, old driver) is
355 * handled in the diagnostic code, with a warning.
357 if (dp.unit >= 20 && dp.data < 512) {
358 /* very probable version mismatch. Fix it up */
359 memcpy(&odp, &dp, sizeof(odp));
360 /* We got a legacy dp, copy elements to dp */
361 dp.unit = odp.unit;
362 dp.data = odp.data;
363 dp.len = odp.len;
364 dp.pbc_wd = 0; /* Indicate we need to compute PBC wd */
367 /* send count must be an exact number of dwords */
368 if (dp.len & 3) {
369 ret = -EINVAL;
370 goto bail;
373 clen = dp.len >> 2;
375 dd = ipath_lookup(dp.unit);
376 if (!dd || !(dd->ipath_flags & IPATH_PRESENT) ||
377 !dd->ipath_kregbase) {
378 ipath_cdbg(VERBOSE, "illegal unit %u for diag data send\n",
379 dp.unit);
380 ret = -ENODEV;
381 goto bail;
384 if (ipath_diag_inuse && !diag_set_link &&
385 !(dd->ipath_flags & IPATH_LINKACTIVE)) {
386 diag_set_link = 1;
387 ipath_cdbg(VERBOSE, "Trying to set to set link active for "
388 "diag pkt\n");
389 ipath_set_linkstate(dd, IPATH_IB_LINKARM);
390 ipath_set_linkstate(dd, IPATH_IB_LINKACTIVE);
393 if (!(dd->ipath_flags & IPATH_INITTED)) {
394 /* no hardware, freeze, etc. */
395 ipath_cdbg(VERBOSE, "unit %u not usable\n", dd->ipath_unit);
396 ret = -ENODEV;
397 goto bail;
399 /* Check link state, but not if we have custom PBC */
400 val = dd->ipath_lastibcstat & IPATH_IBSTATE_MASK;
401 if (!dp.pbc_wd && val != IPATH_IBSTATE_INIT &&
402 val != IPATH_IBSTATE_ARM && val != IPATH_IBSTATE_ACTIVE) {
403 ipath_cdbg(VERBOSE, "unit %u not ready (state %llx)\n",
404 dd->ipath_unit, (unsigned long long) val);
405 ret = -EINVAL;
406 goto bail;
409 /* need total length before first word written */
410 /* +1 word is for the qword padding */
411 plen = sizeof(u32) + dp.len;
413 if ((plen + 4) > dd->ipath_ibmaxlen) {
414 ipath_dbg("Pkt len 0x%x > ibmaxlen %x\n",
415 plen - 4, dd->ipath_ibmaxlen);
416 ret = -EINVAL;
417 goto bail; /* before writing pbc */
419 tmpbuf = vmalloc(plen);
420 if (!tmpbuf) {
421 dev_info(&dd->pcidev->dev, "Unable to allocate tmp buffer, "
422 "failing\n");
423 ret = -ENOMEM;
424 goto bail;
427 if (copy_from_user(tmpbuf,
428 (const void __user *) (unsigned long) dp.data,
429 dp.len)) {
430 ret = -EFAULT;
431 goto bail;
434 piobuf = ipath_getpiobuf(dd, &pbufn);
435 if (!piobuf) {
436 ipath_cdbg(VERBOSE, "No PIO buffers avail unit for %u\n",
437 dd->ipath_unit);
438 ret = -EBUSY;
439 goto bail;
442 plen >>= 2; /* in dwords */
444 if (ipath_debug & __IPATH_PKTDBG)
445 ipath_cdbg(VERBOSE, "unit %u 0x%x+1w pio%d\n",
446 dd->ipath_unit, plen - 1, pbufn);
448 if (dp.pbc_wd == 0)
449 dp.pbc_wd = plen;
450 writeq(dp.pbc_wd, piobuf);
452 * Copy all by the trigger word, then flush, so it's written
453 * to chip before trigger word, then write trigger word, then
454 * flush again, so packet is sent.
456 if (dd->ipath_flags & IPATH_PIO_FLUSH_WC) {
457 ipath_flush_wc();
458 __iowrite32_copy(piobuf + 2, tmpbuf, clen - 1);
459 ipath_flush_wc();
460 __raw_writel(tmpbuf[clen - 1], piobuf + clen + 1);
461 } else
462 __iowrite32_copy(piobuf + 2, tmpbuf, clen);
464 ipath_flush_wc();
466 ret = sizeof(dp);
468 bail:
469 vfree(tmpbuf);
470 return ret;
473 static int ipath_diag_release(struct inode *in, struct file *fp)
475 mutex_lock(&ipath_mutex);
476 ipath_diag_inuse = 0;
477 fp->private_data = NULL;
478 mutex_unlock(&ipath_mutex);
479 return 0;
482 static ssize_t ipath_diag_read(struct file *fp, char __user *data,
483 size_t count, loff_t *off)
485 struct ipath_devdata *dd = fp->private_data;
486 void __iomem *kreg_base;
487 ssize_t ret;
489 kreg_base = dd->ipath_kregbase;
491 if (count == 0)
492 ret = 0;
493 else if ((count % 4) || (*off % 4))
494 /* address or length is not 32-bit aligned, hence invalid */
495 ret = -EINVAL;
496 else if (ipath_diag_inuse < 1 && (*off || count != 8))
497 ret = -EINVAL; /* prevent cat /dev/ipath_diag* */
498 else if ((count % 8) || (*off % 8))
499 /* address or length not 64-bit aligned; do 32-bit reads */
500 ret = ipath_read_umem32(dd, data, kreg_base + *off, count);
501 else
502 ret = ipath_read_umem64(dd, data, kreg_base + *off, count);
504 if (ret >= 0) {
505 *off += count;
506 ret = count;
507 if (ipath_diag_inuse == -2)
508 ipath_diag_inuse++;
511 return ret;
514 static ssize_t ipath_diag_write(struct file *fp, const char __user *data,
515 size_t count, loff_t *off)
517 struct ipath_devdata *dd = fp->private_data;
518 void __iomem *kreg_base;
519 ssize_t ret;
521 kreg_base = dd->ipath_kregbase;
523 if (count == 0)
524 ret = 0;
525 else if ((count % 4) || (*off % 4))
526 /* address or length is not 32-bit aligned, hence invalid */
527 ret = -EINVAL;
528 else if ((ipath_diag_inuse == -1 && (*off || count != 8)) ||
529 ipath_diag_inuse == -2) /* read qw off 0, write qw off 0 */
530 ret = -EINVAL; /* before any other write allowed */
531 else if ((count % 8) || (*off % 8))
532 /* address or length not 64-bit aligned; do 32-bit writes */
533 ret = ipath_write_umem32(dd, kreg_base + *off, data, count);
534 else
535 ret = ipath_write_umem64(dd, kreg_base + *off, data, count);
537 if (ret >= 0) {
538 *off += count;
539 ret = count;
540 if (ipath_diag_inuse == -1)
541 ipath_diag_inuse = 1; /* all read/write OK now */
544 return ret;