2 * Copyright (c) 2010 QLogic Corporation. All rights reserved.
3 * Copyright (c) 2006, 2007, 2008, 2009 QLogic Corporation. All rights reserved.
4 * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36 * This file contains support for diagnostic functions. It is accessed by
37 * opening the qib_diag device, normally minor number 129. Diagnostic use
38 * of the QLogic_IB chip may render the chip or board unusable until the
39 * driver is unloaded, or in some cases, until the system is rebooted.
41 * Accesses to the chip through this interface are not similar to going
42 * through the /sys/bus/pci resource mmap interface.
46 #include <linux/pci.h>
47 #include <linux/poll.h>
48 #include <linux/vmalloc.h>
50 #include <linux/uaccess.h>
53 #include "qib_common.h"
56 * Each client that opens the diag device must read then write
57 * offset 0, to prevent lossage from random cat or od. diag_state
58 * sequences this "handshake".
60 enum diag_state
{ UNUSED
= 0, OPENED
, INIT
, READY
};
62 /* State for an individual client. PID so children cannot abuse handshake */
63 static struct qib_diag_client
{
64 struct qib_diag_client
*next
;
65 struct qib_devdata
*dd
;
67 enum diag_state state
;
71 * Get a client struct. Recycled if possible, else kmalloc.
72 * Must be called with qib_mutex held
74 static struct qib_diag_client
*get_client(struct qib_devdata
*dd
)
76 struct qib_diag_client
*dc
;
80 /* got from pool remove it and use */
81 client_pool
= dc
->next
;
83 /* None in pool, alloc and init */
84 dc
= kmalloc(sizeof *dc
, GFP_KERNEL
);
89 dc
->pid
= current
->pid
;
96 * Return to pool. Must be called with qib_mutex held
98 static void return_client(struct qib_diag_client
*dc
)
100 struct qib_devdata
*dd
= dc
->dd
;
101 struct qib_diag_client
*tdc
, *rdc
;
104 if (dc
== dd
->diag_client
) {
105 dd
->diag_client
= dc
->next
;
108 tdc
= dc
->dd
->diag_client
;
110 if (dc
== tdc
->next
) {
111 tdc
->next
= dc
->next
;
122 rdc
->next
= client_pool
;
127 static int qib_diag_open(struct inode
*in
, struct file
*fp
);
128 static int qib_diag_release(struct inode
*in
, struct file
*fp
);
129 static ssize_t
qib_diag_read(struct file
*fp
, char __user
*data
,
130 size_t count
, loff_t
*off
);
131 static ssize_t
qib_diag_write(struct file
*fp
, const char __user
*data
,
132 size_t count
, loff_t
*off
);
134 static const struct file_operations diag_file_ops
= {
135 .owner
= THIS_MODULE
,
136 .write
= qib_diag_write
,
137 .read
= qib_diag_read
,
138 .open
= qib_diag_open
,
139 .release
= qib_diag_release
142 static atomic_t diagpkt_count
= ATOMIC_INIT(0);
143 static struct cdev
*diagpkt_cdev
;
144 static struct device
*diagpkt_device
;
146 static ssize_t
qib_diagpkt_write(struct file
*fp
, const char __user
*data
,
147 size_t count
, loff_t
*off
);
149 static const struct file_operations diagpkt_file_ops
= {
150 .owner
= THIS_MODULE
,
151 .write
= qib_diagpkt_write
,
154 int qib_diag_add(struct qib_devdata
*dd
)
159 if (atomic_inc_return(&diagpkt_count
) == 1) {
160 ret
= qib_cdev_init(QIB_DIAGPKT_MINOR
, "ipath_diagpkt",
161 &diagpkt_file_ops
, &diagpkt_cdev
,
167 snprintf(name
, sizeof(name
), "ipath_diag%d", dd
->unit
);
168 ret
= qib_cdev_init(QIB_DIAG_MINOR_BASE
+ dd
->unit
, name
,
169 &diag_file_ops
, &dd
->diag_cdev
,
175 static void qib_unregister_observers(struct qib_devdata
*dd
);
177 void qib_diag_remove(struct qib_devdata
*dd
)
179 struct qib_diag_client
*dc
;
181 if (atomic_dec_and_test(&diagpkt_count
))
182 qib_cdev_cleanup(&diagpkt_cdev
, &diagpkt_device
);
184 qib_cdev_cleanup(&dd
->diag_cdev
, &dd
->diag_device
);
187 * Return all diag_clients of this device. There should be none,
188 * as we are "guaranteed" that no clients are still open
190 while (dd
->diag_client
)
191 return_client(dd
->diag_client
);
193 /* Now clean up all unused client structs */
194 while (client_pool
) {
196 client_pool
= dc
->next
;
199 /* Clean up observer list */
200 qib_unregister_observers(dd
);
203 /* qib_remap_ioaddr32 - remap an offset into chip address space to __iomem *
205 * @dd: the qlogic_ib device
206 * @offs: the offset in chip-space
207 * @cntp: Pointer to max (byte) count for transfer starting at offset
208 * This returns a u32 __iomem * so it can be used for both 64 and 32-bit
209 * mapping. It is needed because with the use of PAT for control of
210 * write-combining, the logically contiguous address-space of the chip
211 * may be split into virtually non-contiguous spaces, with different
212 * attributes, which are them mapped to contiguous physical space
213 * based from the first BAR.
215 * The code below makes the same assumptions as were made in
216 * init_chip_wc_pat() (qib_init.c), copied here:
217 * Assumes chip address space looks like:
218 * - kregs + sregs + cregs + uregs (in any order)
219 * - piobufs (2K and 4K bufs in either order)
221 * - kregs + sregs + cregs (in any order)
222 * - piobufs (2K and 4K bufs in either order)
225 * If cntp is non-NULL, returns how many bytes from offset can be accessed
226 * Returns 0 if the offset is not mapped.
228 static u32 __iomem
*qib_remap_ioaddr32(struct qib_devdata
*dd
, u32 offset
,
232 u32 snd_bottom
, snd_lim
= 0;
233 u32 __iomem
*krb32
= (u32 __iomem
*)dd
->kregbase
;
234 u32 __iomem
*map
= NULL
;
237 /* First, simplest case, offset is within the first map. */
238 kreglen
= (dd
->kregend
- dd
->kregbase
) * sizeof(u64
);
239 if (offset
< kreglen
) {
240 map
= krb32
+ (offset
/ sizeof(u32
));
241 cnt
= kreglen
- offset
;
246 * Next check for user regs, the next most common case,
247 * and a cheap check because if they are not in the first map
248 * they are last in chip.
251 /* If user regs mapped, they are after send, so set limit. */
252 u32 ulim
= (dd
->cfgctxts
* dd
->ureg_align
) + dd
->uregbase
;
253 snd_lim
= dd
->uregbase
;
254 krb32
= (u32 __iomem
*)dd
->userbase
;
255 if (offset
>= dd
->uregbase
&& offset
< ulim
) {
256 map
= krb32
+ (offset
- dd
->uregbase
) / sizeof(u32
);
263 * Lastly, check for offset within Send Buffers.
264 * This is gnarly because struct devdata is deliberately vague
265 * about things like 7322 VL15 buffers, and we are not in
266 * chip-specific code here, so should not make many assumptions.
267 * The one we _do_ make is that the only chip that has more sndbufs
268 * than we admit is the 7322, and it has userregs above that, so
269 * we know the snd_lim.
271 /* Assume 2K buffers are first. */
272 snd_bottom
= dd
->pio2k_bufbase
;
274 u32 tot2k
= dd
->piobcnt2k
* ALIGN(dd
->piosize2k
, dd
->palign
);
275 snd_lim
= snd_bottom
+ tot2k
;
277 /* If 4k buffers exist, account for them by bumping
281 u32 tot4k
= dd
->piobcnt4k
* dd
->align4k
;
282 u32 offs4k
= dd
->piobufbase
>> 32;
283 if (snd_bottom
> offs4k
)
286 /* 4k above 2k. Bump snd_lim, if needed*/
288 snd_lim
= offs4k
+ tot4k
;
292 * Judgement call: can we ignore the space between SendBuffs and
293 * UserRegs, where we would like to see vl15 buffs, but not more?
295 if (offset
>= snd_bottom
&& offset
< snd_lim
) {
296 offset
-= snd_bottom
;
297 map
= (u32 __iomem
*)dd
->piobase
+ (offset
/ sizeof(u32
));
298 cnt
= snd_lim
- offset
;
308 * qib_read_umem64 - read a 64-bit quantity from the chip into user space
309 * @dd: the qlogic_ib device
310 * @uaddr: the location to store the data in user memory
311 * @regoffs: the offset from BAR0 (_NOT_ full pointer, anymore)
312 * @count: number of bytes to copy (multiple of 32 bits)
314 * This function also localizes all chip memory accesses.
315 * The copy should be written such that we read full cacheline packets
316 * from the chip. This is usually used for a single qword
318 * NOTE: This assumes the chip address is 64-bit aligned.
320 static int qib_read_umem64(struct qib_devdata
*dd
, void __user
*uaddr
,
321 u32 regoffs
, size_t count
)
323 const u64 __iomem
*reg_addr
;
324 const u64 __iomem
*reg_end
;
328 reg_addr
= (const u64 __iomem
*)qib_remap_ioaddr32(dd
, regoffs
, &limit
);
329 if (reg_addr
== NULL
|| limit
== 0 || !(dd
->flags
& QIB_PRESENT
)) {
335 reg_end
= reg_addr
+ (count
/ sizeof(u64
));
337 /* not very efficient, but it works for now */
338 while (reg_addr
< reg_end
) {
339 u64 data
= readq(reg_addr
);
341 if (copy_to_user(uaddr
, &data
, sizeof(u64
))) {
346 uaddr
+= sizeof(u64
);
354 * qib_write_umem64 - write a 64-bit quantity to the chip from user space
355 * @dd: the qlogic_ib device
356 * @regoffs: the offset from BAR0 (_NOT_ full pointer, anymore)
357 * @uaddr: the source of the data in user memory
358 * @count: the number of bytes to copy (multiple of 32 bits)
360 * This is usually used for a single qword
361 * NOTE: This assumes the chip address is 64-bit aligned.
364 static int qib_write_umem64(struct qib_devdata
*dd
, u32 regoffs
,
365 const void __user
*uaddr
, size_t count
)
367 u64 __iomem
*reg_addr
;
368 const u64 __iomem
*reg_end
;
372 reg_addr
= (u64 __iomem
*)qib_remap_ioaddr32(dd
, regoffs
, &limit
);
373 if (reg_addr
== NULL
|| limit
== 0 || !(dd
->flags
& QIB_PRESENT
)) {
379 reg_end
= reg_addr
+ (count
/ sizeof(u64
));
381 /* not very efficient, but it works for now */
382 while (reg_addr
< reg_end
) {
384 if (copy_from_user(&data
, uaddr
, sizeof(data
))) {
388 writeq(data
, reg_addr
);
391 uaddr
+= sizeof(u64
);
399 * qib_read_umem32 - read a 32-bit quantity from the chip into user space
400 * @dd: the qlogic_ib device
401 * @uaddr: the location to store the data in user memory
402 * @regoffs: the offset from BAR0 (_NOT_ full pointer, anymore)
403 * @count: number of bytes to copy
405 * read 32 bit values, not 64 bit; for memories that only
406 * support 32 bit reads; usually a single dword.
408 static int qib_read_umem32(struct qib_devdata
*dd
, void __user
*uaddr
,
409 u32 regoffs
, size_t count
)
411 const u32 __iomem
*reg_addr
;
412 const u32 __iomem
*reg_end
;
416 reg_addr
= qib_remap_ioaddr32(dd
, regoffs
, &limit
);
417 if (reg_addr
== NULL
|| limit
== 0 || !(dd
->flags
& QIB_PRESENT
)) {
423 reg_end
= reg_addr
+ (count
/ sizeof(u32
));
425 /* not very efficient, but it works for now */
426 while (reg_addr
< reg_end
) {
427 u32 data
= readl(reg_addr
);
429 if (copy_to_user(uaddr
, &data
, sizeof(data
))) {
435 uaddr
+= sizeof(u32
);
444 * qib_write_umem32 - write a 32-bit quantity to the chip from user space
445 * @dd: the qlogic_ib device
446 * @regoffs: the offset from BAR0 (_NOT_ full pointer, anymore)
447 * @uaddr: the source of the data in user memory
448 * @count: number of bytes to copy
450 * write 32 bit values, not 64 bit; for memories that only
451 * support 32 bit write; usually a single dword.
454 static int qib_write_umem32(struct qib_devdata
*dd
, u32 regoffs
,
455 const void __user
*uaddr
, size_t count
)
457 u32 __iomem
*reg_addr
;
458 const u32 __iomem
*reg_end
;
462 reg_addr
= qib_remap_ioaddr32(dd
, regoffs
, &limit
);
463 if (reg_addr
== NULL
|| limit
== 0 || !(dd
->flags
& QIB_PRESENT
)) {
469 reg_end
= reg_addr
+ (count
/ sizeof(u32
));
471 while (reg_addr
< reg_end
) {
474 if (copy_from_user(&data
, uaddr
, sizeof(data
))) {
478 writel(data
, reg_addr
);
481 uaddr
+= sizeof(u32
);
488 static int qib_diag_open(struct inode
*in
, struct file
*fp
)
490 int unit
= iminor(in
) - QIB_DIAG_MINOR_BASE
;
491 struct qib_devdata
*dd
;
492 struct qib_diag_client
*dc
;
495 mutex_lock(&qib_mutex
);
497 dd
= qib_lookup(unit
);
499 if (dd
== NULL
|| !(dd
->flags
& QIB_PRESENT
) ||
510 dc
->next
= dd
->diag_client
;
511 dd
->diag_client
= dc
;
512 fp
->private_data
= dc
;
515 mutex_unlock(&qib_mutex
);
521 * qib_diagpkt_write - write an IB packet
522 * @fp: the diag data device file pointer
523 * @data: qib_diag_pkt structure saying where to get the packet
524 * @count: size of data to write
525 * @off: unused by this code
527 static ssize_t
qib_diagpkt_write(struct file
*fp
,
528 const char __user
*data
,
529 size_t count
, loff_t
*off
)
532 u32 plen
, clen
, pbufn
;
533 struct qib_diag_xpkt dp
;
535 struct qib_devdata
*dd
;
536 struct qib_pportdata
*ppd
;
539 if (count
!= sizeof(dp
)) {
543 if (copy_from_user(&dp
, data
, sizeof(dp
))) {
548 dd
= qib_lookup(dp
.unit
);
549 if (!dd
|| !(dd
->flags
& QIB_PRESENT
) || !dd
->kregbase
) {
553 if (!(dd
->flags
& QIB_INITTED
)) {
554 /* no hardware, freeze, etc. */
559 if (dp
.version
!= _DIAG_XPKT_VERS
) {
560 qib_dev_err(dd
, "Invalid version %u for diagpkt_write\n",
565 /* send count must be an exact number of dwords */
570 if (!dp
.port
|| dp
.port
> dd
->num_pports
) {
574 ppd
= &dd
->pport
[dp
.port
- 1];
576 /* need total length before first word written */
577 /* +1 word is for the qword padding */
578 plen
= sizeof(u32
) + dp
.len
;
581 if ((plen
+ 4) > ppd
->ibmaxlen
) {
583 goto bail
; /* before writing pbc */
585 tmpbuf
= vmalloc(plen
);
587 qib_devinfo(dd
->pcidev
, "Unable to allocate tmp buffer, "
593 if (copy_from_user(tmpbuf
,
594 (const void __user
*) (unsigned long) dp
.data
,
600 plen
>>= 2; /* in dwords */
605 piobuf
= dd
->f_getsendbuf(ppd
, dp
.pbc_wd
, &pbufn
);
610 /* disarm it just to be extra sure */
611 dd
->f_sendctrl(dd
->pport
, QIB_SENDCTRL_DISARM_BUF(pbufn
));
613 /* disable header check on pbufn for this packet */
614 dd
->f_txchk_change(dd
, pbufn
, 1, TXCHK_CHG_TYPE_DIS1
, NULL
);
616 writeq(dp
.pbc_wd
, piobuf
);
618 * Copy all but the trigger word, then flush, so it's written
619 * to chip before trigger word, then write trigger word, then
620 * flush again, so packet is sent.
622 if (dd
->flags
& QIB_PIO_FLUSH_WC
) {
624 qib_pio_copy(piobuf
+ 2, tmpbuf
, clen
- 1);
626 __raw_writel(tmpbuf
[clen
- 1], piobuf
+ clen
+ 1);
628 qib_pio_copy(piobuf
+ 2, tmpbuf
, clen
);
630 if (dd
->flags
& QIB_USE_SPCL_TRIG
) {
631 u32 spcl_off
= (pbufn
>= dd
->piobcnt2k
) ? 2047 : 1023;
634 __raw_writel(0xaebecede, piobuf
+ spcl_off
);
638 * Ensure buffer is written to the chip, then re-enable
639 * header checks (if supported by chip). The txchk
640 * code will ensure seen by chip before returning.
643 qib_sendbuf_done(dd
, pbufn
);
644 dd
->f_txchk_change(dd
, pbufn
, 1, TXCHK_CHG_TYPE_ENAB1
, NULL
);
653 static int qib_diag_release(struct inode
*in
, struct file
*fp
)
655 mutex_lock(&qib_mutex
);
656 return_client(fp
->private_data
);
657 fp
->private_data
= NULL
;
658 mutex_unlock(&qib_mutex
);
663 * Chip-specific code calls to register its interest in
666 struct diag_observer_list_elt
{
667 struct diag_observer_list_elt
*next
;
668 const struct diag_observer
*op
;
671 int qib_register_observer(struct qib_devdata
*dd
,
672 const struct diag_observer
*op
)
674 struct diag_observer_list_elt
*olp
;
680 olp
= vmalloc(sizeof *olp
);
682 printk(KERN_ERR QIB_DRV_NAME
": vmalloc for observer failed\n");
688 spin_lock_irqsave(&dd
->qib_diag_trans_lock
, flags
);
690 olp
->next
= dd
->diag_observer_list
;
691 dd
->diag_observer_list
= olp
;
692 spin_unlock_irqrestore(&dd
->qib_diag_trans_lock
, flags
);
699 /* Remove all registered observers when device is closed */
700 static void qib_unregister_observers(struct qib_devdata
*dd
)
702 struct diag_observer_list_elt
*olp
;
705 spin_lock_irqsave(&dd
->qib_diag_trans_lock
, flags
);
706 olp
= dd
->diag_observer_list
;
708 /* Pop one observer, let go of lock */
709 dd
->diag_observer_list
= olp
->next
;
710 spin_unlock_irqrestore(&dd
->qib_diag_trans_lock
, flags
);
713 spin_lock_irqsave(&dd
->qib_diag_trans_lock
, flags
);
714 olp
= dd
->diag_observer_list
;
716 spin_unlock_irqrestore(&dd
->qib_diag_trans_lock
, flags
);
720 * Find the observer, if any, for the specified address. Initial implementation
721 * is simple stack of observers. This must be called with diag transaction
724 static const struct diag_observer
*diag_get_observer(struct qib_devdata
*dd
,
727 struct diag_observer_list_elt
*olp
;
728 const struct diag_observer
*op
= NULL
;
730 olp
= dd
->diag_observer_list
;
733 if (addr
>= op
->bottom
&& addr
<= op
->top
)
743 static ssize_t
qib_diag_read(struct file
*fp
, char __user
*data
,
744 size_t count
, loff_t
*off
)
746 struct qib_diag_client
*dc
= fp
->private_data
;
747 struct qib_devdata
*dd
= dc
->dd
;
748 void __iomem
*kreg_base
;
751 if (dc
->pid
!= current
->pid
) {
756 kreg_base
= dd
->kregbase
;
760 else if ((count
% 4) || (*off
% 4))
761 /* address or length is not 32-bit aligned, hence invalid */
763 else if (dc
->state
< READY
&& (*off
|| count
!= 8))
764 ret
= -EINVAL
; /* prevent cat /dev/qib_diag* */
769 const struct diag_observer
*op
;
771 use_32
= (count
% 8) || (*off
% 8);
773 spin_lock_irqsave(&dd
->qib_diag_trans_lock
, flags
);
775 * Check for observer on this address range.
776 * we only support a single 32 or 64-bit read
777 * via observer, currently.
779 op
= diag_get_observer(dd
, *off
);
782 ret
= op
->hook(dd
, op
, offset
, &data64
, 0, use_32
);
785 * We need to release lock before any copy_to_user(),
786 * whether implicit in qib_read_umem* or explicit below.
788 spin_unlock_irqrestore(&dd
->qib_diag_trans_lock
, flags
);
792 * Address or length is not 64-bit aligned;
795 ret
= qib_read_umem32(dd
, data
, (u32
) *off
,
798 ret
= qib_read_umem64(dd
, data
, (u32
) *off
,
800 } else if (ret
== count
) {
801 /* Below finishes case where observer existed */
802 ret
= copy_to_user(data
, &data64
, use_32
?
803 sizeof(u32
) : sizeof(u64
));
812 if (dc
->state
== OPENED
)
819 static ssize_t
qib_diag_write(struct file
*fp
, const char __user
*data
,
820 size_t count
, loff_t
*off
)
822 struct qib_diag_client
*dc
= fp
->private_data
;
823 struct qib_devdata
*dd
= dc
->dd
;
824 void __iomem
*kreg_base
;
827 if (dc
->pid
!= current
->pid
) {
832 kreg_base
= dd
->kregbase
;
836 else if ((count
% 4) || (*off
% 4))
837 /* address or length is not 32-bit aligned, hence invalid */
839 else if (dc
->state
< READY
&&
840 ((*off
|| count
!= 8) || dc
->state
!= INIT
))
841 /* No writes except second-step of init seq */
842 ret
= -EINVAL
; /* before any other write allowed */
845 const struct diag_observer
*op
= NULL
;
846 int use_32
= (count
% 8) || (*off
% 8);
849 * Check for observer on this address range.
850 * We only support a single 32 or 64-bit write
851 * via observer, currently. This helps, because
852 * we would otherwise have to jump through hoops
853 * to make "diag transaction" meaningful when we
854 * cannot do a copy_from_user while holding the lock.
856 if (count
== 4 || count
== 8) {
859 ret
= copy_from_user(&data64
, data
, count
);
864 spin_lock_irqsave(&dd
->qib_diag_trans_lock
, flags
);
865 op
= diag_get_observer(dd
, *off
);
867 ret
= op
->hook(dd
, op
, offset
, &data64
, ~0Ull,
869 spin_unlock_irqrestore(&dd
->qib_diag_trans_lock
, flags
);
875 * Address or length is not 64-bit aligned;
878 ret
= qib_write_umem32(dd
, (u32
) *off
, data
,
881 ret
= qib_write_umem64(dd
, (u32
) *off
, data
,
889 if (dc
->state
== INIT
)
890 dc
->state
= READY
; /* all read/write OK now */