blob: 29958b6e0214a672905bab6dc61a50e7f8a021d0 [file] [log] [blame]
Bryan O'Sullivan097709f2006-03-29 15:23:28 -08001/*
Bryan O'Sullivan759d5762006-07-01 04:35:49 -07002 * Copyright (c) 2006 QLogic, Inc. All rights reserved.
Bryan O'Sullivan097709f2006-03-29 15:23:28 -08003 * 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
Bryan O'Sullivan98341f22006-08-25 11:24:36 -070044#include <linux/io.h>
Bryan O'Sullivan097709f2006-03-29 15:23:28 -080045#include <linux/pci.h>
Al Virod7b20042006-09-23 16:44:16 +010046#include <linux/vmalloc.h>
Bryan O'Sullivan097709f2006-03-29 15:23:28 -080047#include <asm/uaccess.h>
48
Bryan O'Sullivan097709f2006-03-29 15:23:28 -080049#include "ipath_kernel.h"
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -070050#include "ipath_common.h"
Bryan O'Sullivan097709f2006-03-29 15:23:28 -080051
52int ipath_diag_inuse;
53static int diag_set_link;
54
55static int ipath_diag_open(struct inode *in, struct file *fp);
56static int ipath_diag_release(struct inode *in, struct file *fp);
57static ssize_t ipath_diag_read(struct file *fp, char __user *data,
58 size_t count, loff_t *off);
59static ssize_t ipath_diag_write(struct file *fp, const char __user *data,
60 size_t count, loff_t *off);
61
62static struct file_operations diag_file_ops = {
63 .owner = THIS_MODULE,
64 .write = ipath_diag_write,
65 .read = ipath_diag_read,
66 .open = ipath_diag_open,
67 .release = ipath_diag_release
68};
69
Bryan O'Sullivana2acb2f2006-07-01 04:35:52 -070070int ipath_diag_add(struct ipath_devdata *dd)
Bryan O'Sullivan097709f2006-03-29 15:23:28 -080071{
Bryan O'Sullivana2acb2f2006-07-01 04:35:52 -070072 char name[16];
73
74 snprintf(name, sizeof(name), "ipath_diag%d", dd->ipath_unit);
75
76 return ipath_cdev_init(IPATH_DIAG_MINOR_BASE + dd->ipath_unit, name,
77 &diag_file_ops, &dd->diag_cdev,
78 &dd->diag_class_dev);
Bryan O'Sullivan097709f2006-03-29 15:23:28 -080079}
80
Bryan O'Sullivana2acb2f2006-07-01 04:35:52 -070081void ipath_diag_remove(struct ipath_devdata *dd)
Bryan O'Sullivan097709f2006-03-29 15:23:28 -080082{
Bryan O'Sullivana2acb2f2006-07-01 04:35:52 -070083 ipath_cdev_cleanup(&dd->diag_cdev, &dd->diag_class_dev);
Bryan O'Sullivan097709f2006-03-29 15:23:28 -080084}
85
86/**
87 * ipath_read_umem64 - read a 64-bit quantity from the chip into user space
88 * @dd: the infinipath device
89 * @uaddr: the location to store the data in user memory
90 * @caddr: the source chip address (full pointer, not offset)
91 * @count: number of bytes to copy (multiple of 32 bits)
92 *
93 * This function also localizes all chip memory accesses.
94 * The copy should be written such that we read full cacheline packets
95 * from the chip. This is usually used for a single qword
96 *
97 * NOTE: This assumes the chip address is 64-bit aligned.
98 */
99static int ipath_read_umem64(struct ipath_devdata *dd, void __user *uaddr,
100 const void __iomem *caddr, size_t count)
101{
102 const u64 __iomem *reg_addr = caddr;
103 const u64 __iomem *reg_end = reg_addr + (count / sizeof(u64));
104 int ret;
105
106 /* not very efficient, but it works for now */
Bryan O'Sullivana2acb2f2006-07-01 04:35:52 -0700107 if (reg_addr < dd->ipath_kregbase || reg_end > dd->ipath_kregend) {
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800108 ret = -EINVAL;
109 goto bail;
110 }
111 while (reg_addr < reg_end) {
112 u64 data = readq(reg_addr);
113 if (copy_to_user(uaddr, &data, sizeof(u64))) {
114 ret = -EFAULT;
115 goto bail;
116 }
117 reg_addr++;
Bryan O'Sullivan6d8e9dd2006-07-01 04:36:14 -0700118 uaddr += sizeof(u64);
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800119 }
120 ret = 0;
121bail:
122 return ret;
123}
124
125/**
126 * ipath_write_umem64 - write a 64-bit quantity to the chip from user space
127 * @dd: the infinipath device
128 * @caddr: the destination chip address (full pointer, not offset)
129 * @uaddr: the source of the data in user memory
130 * @count: the number of bytes to copy (multiple of 32 bits)
131 *
132 * This is usually used for a single qword
133 * NOTE: This assumes the chip address is 64-bit aligned.
134 */
135
136static int ipath_write_umem64(struct ipath_devdata *dd, void __iomem *caddr,
137 const void __user *uaddr, size_t count)
138{
139 u64 __iomem *reg_addr = caddr;
140 const u64 __iomem *reg_end = reg_addr + (count / sizeof(u64));
141 int ret;
142
143 /* not very efficient, but it works for now */
Bryan O'Sullivana2acb2f2006-07-01 04:35:52 -0700144 if (reg_addr < dd->ipath_kregbase || reg_end > dd->ipath_kregend) {
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800145 ret = -EINVAL;
146 goto bail;
147 }
148 while (reg_addr < reg_end) {
149 u64 data;
150 if (copy_from_user(&data, uaddr, sizeof(data))) {
151 ret = -EFAULT;
152 goto bail;
153 }
154 writeq(data, reg_addr);
155
156 reg_addr++;
Bryan O'Sullivan6d8e9dd2006-07-01 04:36:14 -0700157 uaddr += sizeof(u64);
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800158 }
159 ret = 0;
160bail:
161 return ret;
162}
163
164/**
165 * ipath_read_umem32 - read a 32-bit quantity from the chip into user space
166 * @dd: the infinipath device
167 * @uaddr: the location to store the data in user memory
168 * @caddr: the source chip address (full pointer, not offset)
169 * @count: number of bytes to copy
170 *
171 * read 32 bit values, not 64 bit; for memories that only
172 * support 32 bit reads; usually a single dword.
173 */
174static int ipath_read_umem32(struct ipath_devdata *dd, void __user *uaddr,
175 const void __iomem *caddr, size_t count)
176{
177 const u32 __iomem *reg_addr = caddr;
178 const u32 __iomem *reg_end = reg_addr + (count / sizeof(u32));
179 int ret;
180
181 if (reg_addr < (u32 __iomem *) dd->ipath_kregbase ||
182 reg_end > (u32 __iomem *) dd->ipath_kregend) {
183 ret = -EINVAL;
184 goto bail;
185 }
186 /* not very efficient, but it works for now */
187 while (reg_addr < reg_end) {
188 u32 data = readl(reg_addr);
189 if (copy_to_user(uaddr, &data, sizeof(data))) {
190 ret = -EFAULT;
191 goto bail;
192 }
193
194 reg_addr++;
Bryan O'Sullivan6d8e9dd2006-07-01 04:36:14 -0700195 uaddr += sizeof(u32);
196
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800197 }
198 ret = 0;
199bail:
200 return ret;
201}
202
203/**
204 * ipath_write_umem32 - write a 32-bit quantity to the chip from user space
205 * @dd: the infinipath device
206 * @caddr: the destination chip address (full pointer, not offset)
207 * @uaddr: the source of the data in user memory
208 * @count: number of bytes to copy
209 *
210 * write 32 bit values, not 64 bit; for memories that only
211 * support 32 bit write; usually a single dword.
212 */
213
214static int ipath_write_umem32(struct ipath_devdata *dd, void __iomem *caddr,
215 const void __user *uaddr, size_t count)
216{
217 u32 __iomem *reg_addr = caddr;
218 const u32 __iomem *reg_end = reg_addr + (count / sizeof(u32));
219 int ret;
220
221 if (reg_addr < (u32 __iomem *) dd->ipath_kregbase ||
222 reg_end > (u32 __iomem *) dd->ipath_kregend) {
223 ret = -EINVAL;
224 goto bail;
225 }
226 while (reg_addr < reg_end) {
227 u32 data;
228 if (copy_from_user(&data, uaddr, sizeof(data))) {
229 ret = -EFAULT;
230 goto bail;
231 }
232 writel(data, reg_addr);
233
234 reg_addr++;
Bryan O'Sullivan6d8e9dd2006-07-01 04:36:14 -0700235 uaddr += sizeof(u32);
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800236 }
237 ret = 0;
238bail:
239 return ret;
240}
241
242static int ipath_diag_open(struct inode *in, struct file *fp)
243{
Bryan O'Sullivana2acb2f2006-07-01 04:35:52 -0700244 int unit = iminor(in) - IPATH_DIAG_MINOR_BASE;
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800245 struct ipath_devdata *dd;
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800246 int ret;
247
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800248 mutex_lock(&ipath_mutex);
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800249
250 if (ipath_diag_inuse) {
251 ret = -EBUSY;
252 goto bail;
253 }
254
Bryan O'Sullivana2acb2f2006-07-01 04:35:52 -0700255 dd = ipath_lookup(unit);
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800256
Bryan O'Sullivana2acb2f2006-07-01 04:35:52 -0700257 if (dd == NULL || !(dd->ipath_flags & IPATH_PRESENT) ||
258 !dd->ipath_kregbase) {
259 ret = -ENODEV;
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800260 goto bail;
261 }
262
Bryan O'Sullivana2acb2f2006-07-01 04:35:52 -0700263 fp->private_data = dd;
264 ipath_diag_inuse = 1;
265 diag_set_link = 0;
266 ret = 0;
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800267
268 /* Only expose a way to reset the device if we
269 make it into diag mode. */
Bryan O'Sullivana2acb2f2006-07-01 04:35:52 -0700270 ipath_expose_reset(&dd->pcidev->dev);
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800271
Bryan O'Sullivana2acb2f2006-07-01 04:35:52 -0700272bail:
Bryan O'Sullivan755e4ca2006-04-24 14:22:57 -0700273 mutex_unlock(&ipath_mutex);
274
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800275 return ret;
276}
277
Bryan O'Sullivan98341f22006-08-25 11:24:36 -0700278static ssize_t ipath_diagpkt_write(struct file *fp,
279 const char __user *data,
280 size_t count, loff_t *off);
281
282static struct file_operations diagpkt_file_ops = {
283 .owner = THIS_MODULE,
284 .write = ipath_diagpkt_write,
285};
286
287static struct cdev *diagpkt_cdev;
288static struct class_device *diagpkt_class_dev;
289
290int __init ipath_diagpkt_add(void)
291{
292 return ipath_cdev_init(IPATH_DIAGPKT_MINOR,
293 "ipath_diagpkt", &diagpkt_file_ops,
294 &diagpkt_cdev, &diagpkt_class_dev);
295}
296
297void __exit ipath_diagpkt_remove(void)
298{
299 ipath_cdev_cleanup(&diagpkt_cdev, &diagpkt_class_dev);
300}
301
302/**
303 * ipath_diagpkt_write - write an IB packet
304 * @fp: the diag data device file pointer
305 * @data: ipath_diag_pkt structure saying where to get the packet
306 * @count: size of data to write
307 * @off: unused by this code
308 */
309static ssize_t ipath_diagpkt_write(struct file *fp,
310 const char __user *data,
311 size_t count, loff_t *off)
312{
313 u32 __iomem *piobuf;
314 u32 plen, clen, pbufn;
315 struct ipath_diag_pkt dp;
316 u32 *tmpbuf = NULL;
317 struct ipath_devdata *dd;
318 ssize_t ret = 0;
319 u64 val;
320
321 if (count < sizeof(dp)) {
322 ret = -EINVAL;
323 goto bail;
324 }
325
326 if (copy_from_user(&dp, data, sizeof(dp))) {
327 ret = -EFAULT;
328 goto bail;
329 }
330
331 /* send count must be an exact number of dwords */
332 if (dp.len & 3) {
333 ret = -EINVAL;
334 goto bail;
335 }
336
337 clen = dp.len >> 2;
338
339 dd = ipath_lookup(dp.unit);
340 if (!dd || !(dd->ipath_flags & IPATH_PRESENT) ||
341 !dd->ipath_kregbase) {
342 ipath_cdbg(VERBOSE, "illegal unit %u for diag data send\n",
343 dp.unit);
344 ret = -ENODEV;
345 goto bail;
346 }
347
348 if (ipath_diag_inuse && !diag_set_link &&
349 !(dd->ipath_flags & IPATH_LINKACTIVE)) {
350 diag_set_link = 1;
351 ipath_cdbg(VERBOSE, "Trying to set to set link active for "
352 "diag pkt\n");
353 ipath_set_linkstate(dd, IPATH_IB_LINKARM);
354 ipath_set_linkstate(dd, IPATH_IB_LINKACTIVE);
355 }
356
357 if (!(dd->ipath_flags & IPATH_INITTED)) {
358 /* no hardware, freeze, etc. */
359 ipath_cdbg(VERBOSE, "unit %u not usable\n", dd->ipath_unit);
360 ret = -ENODEV;
361 goto bail;
362 }
363 val = dd->ipath_lastibcstat & IPATH_IBSTATE_MASK;
364 if (val != IPATH_IBSTATE_INIT && val != IPATH_IBSTATE_ARM &&
365 val != IPATH_IBSTATE_ACTIVE) {
366 ipath_cdbg(VERBOSE, "unit %u not ready (state %llx)\n",
367 dd->ipath_unit, (unsigned long long) val);
368 ret = -EINVAL;
369 goto bail;
370 }
371
372 /* need total length before first word written */
373 /* +1 word is for the qword padding */
374 plen = sizeof(u32) + dp.len;
375
376 if ((plen + 4) > dd->ipath_ibmaxlen) {
377 ipath_dbg("Pkt len 0x%x > ibmaxlen %x\n",
378 plen - 4, dd->ipath_ibmaxlen);
379 ret = -EINVAL;
380 goto bail; /* before writing pbc */
381 }
382 tmpbuf = vmalloc(plen);
383 if (!tmpbuf) {
384 dev_info(&dd->pcidev->dev, "Unable to allocate tmp buffer, "
385 "failing\n");
386 ret = -ENOMEM;
387 goto bail;
388 }
389
390 if (copy_from_user(tmpbuf,
391 (const void __user *) (unsigned long) dp.data,
392 dp.len)) {
393 ret = -EFAULT;
394 goto bail;
395 }
396
397 piobuf = ipath_getpiobuf(dd, &pbufn);
398 if (!piobuf) {
399 ipath_cdbg(VERBOSE, "No PIO buffers avail unit for %u\n",
400 dd->ipath_unit);
401 ret = -EBUSY;
402 goto bail;
403 }
404
405 plen >>= 2; /* in dwords */
406
407 if (ipath_debug & __IPATH_PKTDBG)
408 ipath_cdbg(VERBOSE, "unit %u 0x%x+1w pio%d\n",
409 dd->ipath_unit, plen - 1, pbufn);
410
411 /* we have to flush after the PBC for correctness on some cpus
412 * or WC buffer can be written out of order */
413 writeq(plen, piobuf);
414 ipath_flush_wc();
415 /* copy all by the trigger word, then flush, so it's written
416 * to chip before trigger word, then write trigger word, then
417 * flush again, so packet is sent. */
418 __iowrite32_copy(piobuf + 2, tmpbuf, clen - 1);
419 ipath_flush_wc();
420 __raw_writel(tmpbuf[clen - 1], piobuf + clen + 1);
421 ipath_flush_wc();
422
423 ret = sizeof(dp);
424
425bail:
426 vfree(tmpbuf);
427 return ret;
428}
429
Bryan O'Sullivana2acb2f2006-07-01 04:35:52 -0700430static int ipath_diag_release(struct inode *in, struct file *fp)
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800431{
432 mutex_lock(&ipath_mutex);
433 ipath_diag_inuse = 0;
Bryan O'Sullivana2acb2f2006-07-01 04:35:52 -0700434 fp->private_data = NULL;
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800435 mutex_unlock(&ipath_mutex);
436 return 0;
437}
438
439static ssize_t ipath_diag_read(struct file *fp, char __user *data,
440 size_t count, loff_t *off)
441{
Bryan O'Sullivana2acb2f2006-07-01 04:35:52 -0700442 struct ipath_devdata *dd = fp->private_data;
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800443 void __iomem *kreg_base;
444 ssize_t ret;
445
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800446 kreg_base = dd->ipath_kregbase;
447
448 if (count == 0)
449 ret = 0;
450 else if ((count % 4) || (*off % 4))
451 /* address or length is not 32-bit aligned, hence invalid */
452 ret = -EINVAL;
453 else if ((count % 8) || (*off % 8))
454 /* address or length not 64-bit aligned; do 32-bit reads */
455 ret = ipath_read_umem32(dd, data, kreg_base + *off, count);
456 else
457 ret = ipath_read_umem64(dd, data, kreg_base + *off, count);
458
459 if (ret >= 0) {
460 *off += count;
461 ret = count;
462 }
463
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800464 return ret;
465}
466
467static ssize_t ipath_diag_write(struct file *fp, const char __user *data,
468 size_t count, loff_t *off)
469{
Bryan O'Sullivana2acb2f2006-07-01 04:35:52 -0700470 struct ipath_devdata *dd = fp->private_data;
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800471 void __iomem *kreg_base;
472 ssize_t ret;
473
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800474 kreg_base = dd->ipath_kregbase;
475
476 if (count == 0)
477 ret = 0;
478 else if ((count % 4) || (*off % 4))
479 /* address or length is not 32-bit aligned, hence invalid */
480 ret = -EINVAL;
481 else if ((count % 8) || (*off % 8))
482 /* address or length not 64-bit aligned; do 32-bit writes */
483 ret = ipath_write_umem32(dd, kreg_base + *off, data, count);
484 else
485 ret = ipath_write_umem64(dd, kreg_base + *off, data, count);
486
487 if (ret >= 0) {
488 *off += count;
489 ret = count;
490 }
491
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800492 return ret;
493}