Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (c) 2012 Intel Corporation. All rights reserved.
0003  * Copyright (c) 2006 - 2012 QLogic Corporation. All rights reserved.
0004  * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
0005  *
0006  * This software is available to you under a choice of one of two
0007  * licenses.  You may choose to be licensed under the terms of the GNU
0008  * General Public License (GPL) Version 2, available from the file
0009  * COPYING in the main directory of this source tree, or the
0010  * OpenIB.org BSD license below:
0011  *
0012  *     Redistribution and use in source and binary forms, with or
0013  *     without modification, are permitted provided that the following
0014  *     conditions are met:
0015  *
0016  *      - Redistributions of source code must retain the above
0017  *        copyright notice, this list of conditions and the following
0018  *        disclaimer.
0019  *
0020  *      - Redistributions in binary form must reproduce the above
0021  *        copyright notice, this list of conditions and the following
0022  *        disclaimer in the documentation and/or other materials
0023  *        provided with the distribution.
0024  *
0025  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
0026  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0027  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
0028  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
0029  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
0030  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
0031  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
0032  * SOFTWARE.
0033  */
0034 
0035 /*
0036  * This file contains support for diagnostic functions.  It is accessed by
0037  * opening the qib_diag device, normally minor number 129.  Diagnostic use
0038  * of the QLogic_IB chip may render the chip or board unusable until the
0039  * driver is unloaded, or in some cases, until the system is rebooted.
0040  *
0041  * Accesses to the chip through this interface are not similar to going
0042  * through the /sys/bus/pci resource mmap interface.
0043  */
0044 
0045 #include <linux/io.h>
0046 #include <linux/pci.h>
0047 #include <linux/poll.h>
0048 #include <linux/vmalloc.h>
0049 #include <linux/export.h>
0050 #include <linux/fs.h>
0051 #include <linux/uaccess.h>
0052 
0053 #include "qib.h"
0054 #include "qib_common.h"
0055 
0056 #undef pr_fmt
0057 #define pr_fmt(fmt) QIB_DRV_NAME ": " fmt
0058 
0059 /*
0060  * Each client that opens the diag device must read then write
0061  * offset 0, to prevent lossage from random cat or od. diag_state
0062  * sequences this "handshake".
0063  */
0064 enum diag_state { UNUSED = 0, OPENED, INIT, READY };
0065 
0066 /* State for an individual client. PID so children cannot abuse handshake */
0067 static struct qib_diag_client {
0068     struct qib_diag_client *next;
0069     struct qib_devdata *dd;
0070     pid_t pid;
0071     enum diag_state state;
0072 } *client_pool;
0073 
0074 /*
0075  * Get a client struct. Recycled if possible, else kmalloc.
0076  * Must be called with qib_mutex held
0077  */
0078 static struct qib_diag_client *get_client(struct qib_devdata *dd)
0079 {
0080     struct qib_diag_client *dc;
0081 
0082     dc = client_pool;
0083     if (dc)
0084         /* got from pool remove it and use */
0085         client_pool = dc->next;
0086     else
0087         /* None in pool, alloc and init */
0088         dc = kmalloc(sizeof(*dc), GFP_KERNEL);
0089 
0090     if (dc) {
0091         dc->next = NULL;
0092         dc->dd = dd;
0093         dc->pid = current->pid;
0094         dc->state = OPENED;
0095     }
0096     return dc;
0097 }
0098 
0099 /*
0100  * Return to pool. Must be called with qib_mutex held
0101  */
0102 static void return_client(struct qib_diag_client *dc)
0103 {
0104     struct qib_devdata *dd = dc->dd;
0105     struct qib_diag_client *tdc, *rdc;
0106 
0107     rdc = NULL;
0108     if (dc == dd->diag_client) {
0109         dd->diag_client = dc->next;
0110         rdc = dc;
0111     } else {
0112         tdc = dc->dd->diag_client;
0113         while (tdc) {
0114             if (dc == tdc->next) {
0115                 tdc->next = dc->next;
0116                 rdc = dc;
0117                 break;
0118             }
0119             tdc = tdc->next;
0120         }
0121     }
0122     if (rdc) {
0123         rdc->state = UNUSED;
0124         rdc->dd = NULL;
0125         rdc->pid = 0;
0126         rdc->next = client_pool;
0127         client_pool = rdc;
0128     }
0129 }
0130 
0131 static int qib_diag_open(struct inode *in, struct file *fp);
0132 static int qib_diag_release(struct inode *in, struct file *fp);
0133 static ssize_t qib_diag_read(struct file *fp, char __user *data,
0134                  size_t count, loff_t *off);
0135 static ssize_t qib_diag_write(struct file *fp, const char __user *data,
0136                   size_t count, loff_t *off);
0137 
0138 static const struct file_operations diag_file_ops = {
0139     .owner = THIS_MODULE,
0140     .write = qib_diag_write,
0141     .read = qib_diag_read,
0142     .open = qib_diag_open,
0143     .release = qib_diag_release,
0144     .llseek = default_llseek,
0145 };
0146 
0147 static atomic_t diagpkt_count = ATOMIC_INIT(0);
0148 static struct cdev *diagpkt_cdev;
0149 static struct device *diagpkt_device;
0150 
0151 static ssize_t qib_diagpkt_write(struct file *fp, const char __user *data,
0152                  size_t count, loff_t *off);
0153 
0154 static const struct file_operations diagpkt_file_ops = {
0155     .owner = THIS_MODULE,
0156     .write = qib_diagpkt_write,
0157     .llseek = noop_llseek,
0158 };
0159 
0160 int qib_diag_add(struct qib_devdata *dd)
0161 {
0162     char name[16];
0163     int ret = 0;
0164 
0165     if (atomic_inc_return(&diagpkt_count) == 1) {
0166         ret = qib_cdev_init(QIB_DIAGPKT_MINOR, "ipath_diagpkt",
0167                     &diagpkt_file_ops, &diagpkt_cdev,
0168                     &diagpkt_device);
0169         if (ret)
0170             goto done;
0171     }
0172 
0173     snprintf(name, sizeof(name), "ipath_diag%d", dd->unit);
0174     ret = qib_cdev_init(QIB_DIAG_MINOR_BASE + dd->unit, name,
0175                 &diag_file_ops, &dd->diag_cdev,
0176                 &dd->diag_device);
0177 done:
0178     return ret;
0179 }
0180 
0181 static void qib_unregister_observers(struct qib_devdata *dd);
0182 
0183 void qib_diag_remove(struct qib_devdata *dd)
0184 {
0185     struct qib_diag_client *dc;
0186 
0187     if (atomic_dec_and_test(&diagpkt_count))
0188         qib_cdev_cleanup(&diagpkt_cdev, &diagpkt_device);
0189 
0190     qib_cdev_cleanup(&dd->diag_cdev, &dd->diag_device);
0191 
0192     /*
0193      * Return all diag_clients of this device. There should be none,
0194      * as we are "guaranteed" that no clients are still open
0195      */
0196     while (dd->diag_client)
0197         return_client(dd->diag_client);
0198 
0199     /* Now clean up all unused client structs */
0200     while (client_pool) {
0201         dc = client_pool;
0202         client_pool = dc->next;
0203         kfree(dc);
0204     }
0205     /* Clean up observer list */
0206     qib_unregister_observers(dd);
0207 }
0208 
0209 /* qib_remap_ioaddr32 - remap an offset into chip address space to __iomem *
0210  *
0211  * @dd: the qlogic_ib device
0212  * @offs: the offset in chip-space
0213  * @cntp: Pointer to max (byte) count for transfer starting at offset
0214  * This returns a u32 __iomem * so it can be used for both 64 and 32-bit
0215  * mapping. It is needed because with the use of PAT for control of
0216  * write-combining, the logically contiguous address-space of the chip
0217  * may be split into virtually non-contiguous spaces, with different
0218  * attributes, which are them mapped to contiguous physical space
0219  * based from the first BAR.
0220  *
0221  * The code below makes the same assumptions as were made in
0222  * init_chip_wc_pat() (qib_init.c), copied here:
0223  * Assumes chip address space looks like:
0224  *      - kregs + sregs + cregs + uregs (in any order)
0225  *      - piobufs (2K and 4K bufs in either order)
0226  *  or:
0227  *      - kregs + sregs + cregs (in any order)
0228  *      - piobufs (2K and 4K bufs in either order)
0229  *      - uregs
0230  *
0231  * If cntp is non-NULL, returns how many bytes from offset can be accessed
0232  * Returns 0 if the offset is not mapped.
0233  */
0234 static u32 __iomem *qib_remap_ioaddr32(struct qib_devdata *dd, u32 offset,
0235                        u32 *cntp)
0236 {
0237     u32 kreglen;
0238     u32 snd_bottom, snd_lim = 0;
0239     u32 __iomem *krb32 = (u32 __iomem *)dd->kregbase;
0240     u32 __iomem *map = NULL;
0241     u32 cnt = 0;
0242     u32 tot4k, offs4k;
0243 
0244     /* First, simplest case, offset is within the first map. */
0245     kreglen = (dd->kregend - dd->kregbase) * sizeof(u64);
0246     if (offset < kreglen) {
0247         map = krb32 + (offset / sizeof(u32));
0248         cnt = kreglen - offset;
0249         goto mapped;
0250     }
0251 
0252     /*
0253      * Next check for user regs, the next most common case,
0254      * and a cheap check because if they are not in the first map
0255      * they are last in chip.
0256      */
0257     if (dd->userbase) {
0258         /* If user regs mapped, they are after send, so set limit. */
0259         u32 ulim = (dd->cfgctxts * dd->ureg_align) + dd->uregbase;
0260 
0261         if (!dd->piovl15base)
0262             snd_lim = dd->uregbase;
0263         krb32 = (u32 __iomem *)dd->userbase;
0264         if (offset >= dd->uregbase && offset < ulim) {
0265             map = krb32 + (offset - dd->uregbase) / sizeof(u32);
0266             cnt = ulim - offset;
0267             goto mapped;
0268         }
0269     }
0270 
0271     /*
0272      * Lastly, check for offset within Send Buffers.
0273      * This is gnarly because struct devdata is deliberately vague
0274      * about things like 7322 VL15 buffers, and we are not in
0275      * chip-specific code here, so should not make many assumptions.
0276      * The one we _do_ make is that the only chip that has more sndbufs
0277      * than we admit is the 7322, and it has userregs above that, so
0278      * we know the snd_lim.
0279      */
0280     /* Assume 2K buffers are first. */
0281     snd_bottom = dd->pio2k_bufbase;
0282     if (snd_lim == 0) {
0283         u32 tot2k = dd->piobcnt2k * ALIGN(dd->piosize2k, dd->palign);
0284 
0285         snd_lim = snd_bottom + tot2k;
0286     }
0287     /* If 4k buffers exist, account for them by bumping
0288      * appropriate limit.
0289      */
0290     tot4k = dd->piobcnt4k * dd->align4k;
0291     offs4k = dd->piobufbase >> 32;
0292     if (dd->piobcnt4k) {
0293         if (snd_bottom > offs4k)
0294             snd_bottom = offs4k;
0295         else {
0296             /* 4k above 2k. Bump snd_lim, if needed*/
0297             if (!dd->userbase || dd->piovl15base)
0298                 snd_lim = offs4k + tot4k;
0299         }
0300     }
0301     /*
0302      * Judgement call: can we ignore the space between SendBuffs and
0303      * UserRegs, where we would like to see vl15 buffs, but not more?
0304      */
0305     if (offset >= snd_bottom && offset < snd_lim) {
0306         offset -= snd_bottom;
0307         map = (u32 __iomem *)dd->piobase + (offset / sizeof(u32));
0308         cnt = snd_lim - offset;
0309     }
0310 
0311     if (!map && offs4k && dd->piovl15base) {
0312         snd_lim = offs4k + tot4k + 2 * dd->align4k;
0313         if (offset >= (offs4k + tot4k) && offset < snd_lim) {
0314             map = (u32 __iomem *)dd->piovl15base +
0315                 ((offset - (offs4k + tot4k)) / sizeof(u32));
0316             cnt = snd_lim - offset;
0317         }
0318     }
0319 
0320 mapped:
0321     if (cntp)
0322         *cntp = cnt;
0323     return map;
0324 }
0325 
0326 /*
0327  * qib_read_umem64 - read a 64-bit quantity from the chip into user space
0328  * @dd: the qlogic_ib device
0329  * @uaddr: the location to store the data in user memory
0330  * @regoffs: the offset from BAR0 (_NOT_ full pointer, anymore)
0331  * @count: number of bytes to copy (multiple of 32 bits)
0332  *
0333  * This function also localizes all chip memory accesses.
0334  * The copy should be written such that we read full cacheline packets
0335  * from the chip.  This is usually used for a single qword
0336  *
0337  * NOTE:  This assumes the chip address is 64-bit aligned.
0338  */
0339 static int qib_read_umem64(struct qib_devdata *dd, void __user *uaddr,
0340                u32 regoffs, size_t count)
0341 {
0342     const u64 __iomem *reg_addr;
0343     const u64 __iomem *reg_end;
0344     u32 limit;
0345     int ret;
0346 
0347     reg_addr = (const u64 __iomem *)qib_remap_ioaddr32(dd, regoffs, &limit);
0348     if (reg_addr == NULL || limit == 0 || !(dd->flags & QIB_PRESENT)) {
0349         ret = -EINVAL;
0350         goto bail;
0351     }
0352     if (count >= limit)
0353         count = limit;
0354     reg_end = reg_addr + (count / sizeof(u64));
0355 
0356     /* not very efficient, but it works for now */
0357     while (reg_addr < reg_end) {
0358         u64 data = readq(reg_addr);
0359 
0360         if (copy_to_user(uaddr, &data, sizeof(u64))) {
0361             ret = -EFAULT;
0362             goto bail;
0363         }
0364         reg_addr++;
0365         uaddr += sizeof(u64);
0366     }
0367     ret = 0;
0368 bail:
0369     return ret;
0370 }
0371 
0372 /*
0373  * qib_write_umem64 - write a 64-bit quantity to the chip from user space
0374  * @dd: the qlogic_ib device
0375  * @regoffs: the offset from BAR0 (_NOT_ full pointer, anymore)
0376  * @uaddr: the source of the data in user memory
0377  * @count: the number of bytes to copy (multiple of 32 bits)
0378  *
0379  * This is usually used for a single qword
0380  * NOTE:  This assumes the chip address is 64-bit aligned.
0381  */
0382 
0383 static int qib_write_umem64(struct qib_devdata *dd, u32 regoffs,
0384                 const void __user *uaddr, size_t count)
0385 {
0386     u64 __iomem *reg_addr;
0387     const u64 __iomem *reg_end;
0388     u32 limit;
0389     int ret;
0390 
0391     reg_addr = (u64 __iomem *)qib_remap_ioaddr32(dd, regoffs, &limit);
0392     if (reg_addr == NULL || limit == 0 || !(dd->flags & QIB_PRESENT)) {
0393         ret = -EINVAL;
0394         goto bail;
0395     }
0396     if (count >= limit)
0397         count = limit;
0398     reg_end = reg_addr + (count / sizeof(u64));
0399 
0400     /* not very efficient, but it works for now */
0401     while (reg_addr < reg_end) {
0402         u64 data;
0403 
0404         if (copy_from_user(&data, uaddr, sizeof(data))) {
0405             ret = -EFAULT;
0406             goto bail;
0407         }
0408         writeq(data, reg_addr);
0409 
0410         reg_addr++;
0411         uaddr += sizeof(u64);
0412     }
0413     ret = 0;
0414 bail:
0415     return ret;
0416 }
0417 
0418 /*
0419  * qib_read_umem32 - read a 32-bit quantity from the chip into user space
0420  * @dd: the qlogic_ib device
0421  * @uaddr: the location to store the data in user memory
0422  * @regoffs: the offset from BAR0 (_NOT_ full pointer, anymore)
0423  * @count: number of bytes to copy
0424  *
0425  * read 32 bit values, not 64 bit; for memories that only
0426  * support 32 bit reads; usually a single dword.
0427  */
0428 static int qib_read_umem32(struct qib_devdata *dd, void __user *uaddr,
0429                u32 regoffs, size_t count)
0430 {
0431     const u32 __iomem *reg_addr;
0432     const u32 __iomem *reg_end;
0433     u32 limit;
0434     int ret;
0435 
0436     reg_addr = qib_remap_ioaddr32(dd, regoffs, &limit);
0437     if (reg_addr == NULL || limit == 0 || !(dd->flags & QIB_PRESENT)) {
0438         ret = -EINVAL;
0439         goto bail;
0440     }
0441     if (count >= limit)
0442         count = limit;
0443     reg_end = reg_addr + (count / sizeof(u32));
0444 
0445     /* not very efficient, but it works for now */
0446     while (reg_addr < reg_end) {
0447         u32 data = readl(reg_addr);
0448 
0449         if (copy_to_user(uaddr, &data, sizeof(data))) {
0450             ret = -EFAULT;
0451             goto bail;
0452         }
0453 
0454         reg_addr++;
0455         uaddr += sizeof(u32);
0456 
0457     }
0458     ret = 0;
0459 bail:
0460     return ret;
0461 }
0462 
0463 /*
0464  * qib_write_umem32 - write a 32-bit quantity to the chip from user space
0465  * @dd: the qlogic_ib device
0466  * @regoffs: the offset from BAR0 (_NOT_ full pointer, anymore)
0467  * @uaddr: the source of the data in user memory
0468  * @count: number of bytes to copy
0469  *
0470  * write 32 bit values, not 64 bit; for memories that only
0471  * support 32 bit write; usually a single dword.
0472  */
0473 
0474 static int qib_write_umem32(struct qib_devdata *dd, u32 regoffs,
0475                 const void __user *uaddr, size_t count)
0476 {
0477     u32 __iomem *reg_addr;
0478     const u32 __iomem *reg_end;
0479     u32 limit;
0480     int ret;
0481 
0482     reg_addr = qib_remap_ioaddr32(dd, regoffs, &limit);
0483     if (reg_addr == NULL || limit == 0 || !(dd->flags & QIB_PRESENT)) {
0484         ret = -EINVAL;
0485         goto bail;
0486     }
0487     if (count >= limit)
0488         count = limit;
0489     reg_end = reg_addr + (count / sizeof(u32));
0490 
0491     while (reg_addr < reg_end) {
0492         u32 data;
0493 
0494         if (copy_from_user(&data, uaddr, sizeof(data))) {
0495             ret = -EFAULT;
0496             goto bail;
0497         }
0498         writel(data, reg_addr);
0499 
0500         reg_addr++;
0501         uaddr += sizeof(u32);
0502     }
0503     ret = 0;
0504 bail:
0505     return ret;
0506 }
0507 
0508 static int qib_diag_open(struct inode *in, struct file *fp)
0509 {
0510     int unit = iminor(in) - QIB_DIAG_MINOR_BASE;
0511     struct qib_devdata *dd;
0512     struct qib_diag_client *dc;
0513     int ret;
0514 
0515     mutex_lock(&qib_mutex);
0516 
0517     dd = qib_lookup(unit);
0518 
0519     if (dd == NULL || !(dd->flags & QIB_PRESENT) ||
0520         !dd->kregbase) {
0521         ret = -ENODEV;
0522         goto bail;
0523     }
0524 
0525     dc = get_client(dd);
0526     if (!dc) {
0527         ret = -ENOMEM;
0528         goto bail;
0529     }
0530     dc->next = dd->diag_client;
0531     dd->diag_client = dc;
0532     fp->private_data = dc;
0533     ret = 0;
0534 bail:
0535     mutex_unlock(&qib_mutex);
0536 
0537     return ret;
0538 }
0539 
0540 /**
0541  * qib_diagpkt_write - write an IB packet
0542  * @fp: the diag data device file pointer
0543  * @data: qib_diag_pkt structure saying where to get the packet
0544  * @count: size of data to write
0545  * @off: unused by this code
0546  */
0547 static ssize_t qib_diagpkt_write(struct file *fp,
0548                  const char __user *data,
0549                  size_t count, loff_t *off)
0550 {
0551     u32 __iomem *piobuf;
0552     u32 plen, pbufn, maxlen_reserve;
0553     struct qib_diag_xpkt dp;
0554     u32 *tmpbuf = NULL;
0555     struct qib_devdata *dd;
0556     struct qib_pportdata *ppd;
0557     ssize_t ret = 0;
0558 
0559     if (count != sizeof(dp)) {
0560         ret = -EINVAL;
0561         goto bail;
0562     }
0563     if (copy_from_user(&dp, data, sizeof(dp))) {
0564         ret = -EFAULT;
0565         goto bail;
0566     }
0567 
0568     dd = qib_lookup(dp.unit);
0569     if (!dd || !(dd->flags & QIB_PRESENT) || !dd->kregbase) {
0570         ret = -ENODEV;
0571         goto bail;
0572     }
0573     if (!(dd->flags & QIB_INITTED)) {
0574         /* no hardware, freeze, etc. */
0575         ret = -ENODEV;
0576         goto bail;
0577     }
0578 
0579     if (dp.version != _DIAG_XPKT_VERS) {
0580         qib_dev_err(dd, "Invalid version %u for diagpkt_write\n",
0581                 dp.version);
0582         ret = -EINVAL;
0583         goto bail;
0584     }
0585     /* send count must be an exact number of dwords */
0586     if (dp.len & 3) {
0587         ret = -EINVAL;
0588         goto bail;
0589     }
0590     if (!dp.port || dp.port > dd->num_pports) {
0591         ret = -EINVAL;
0592         goto bail;
0593     }
0594     ppd = &dd->pport[dp.port - 1];
0595 
0596     /*
0597      * need total length before first word written, plus 2 Dwords. One Dword
0598      * is for padding so we get the full user data when not aligned on
0599      * a word boundary. The other Dword is to make sure we have room for the
0600      * ICRC which gets tacked on later.
0601      */
0602     maxlen_reserve = 2 * sizeof(u32);
0603     if (dp.len > ppd->ibmaxlen - maxlen_reserve) {
0604         ret = -EINVAL;
0605         goto bail;
0606     }
0607 
0608     plen = sizeof(u32) + dp.len;
0609 
0610     tmpbuf = vmalloc(plen);
0611     if (!tmpbuf) {
0612         ret = -ENOMEM;
0613         goto bail;
0614     }
0615 
0616     if (copy_from_user(tmpbuf,
0617                u64_to_user_ptr(dp.data),
0618                dp.len)) {
0619         ret = -EFAULT;
0620         goto bail;
0621     }
0622 
0623     plen >>= 2;             /* in dwords */
0624 
0625     if (dp.pbc_wd == 0)
0626         dp.pbc_wd = plen;
0627 
0628     piobuf = dd->f_getsendbuf(ppd, dp.pbc_wd, &pbufn);
0629     if (!piobuf) {
0630         ret = -EBUSY;
0631         goto bail;
0632     }
0633     /* disarm it just to be extra sure */
0634     dd->f_sendctrl(dd->pport, QIB_SENDCTRL_DISARM_BUF(pbufn));
0635 
0636     /* disable header check on pbufn for this packet */
0637     dd->f_txchk_change(dd, pbufn, 1, TXCHK_CHG_TYPE_DIS1, NULL);
0638 
0639     writeq(dp.pbc_wd, piobuf);
0640     /*
0641      * Copy all but the trigger word, then flush, so it's written
0642      * to chip before trigger word, then write trigger word, then
0643      * flush again, so packet is sent.
0644      */
0645     if (dd->flags & QIB_PIO_FLUSH_WC) {
0646         qib_flush_wc();
0647         qib_pio_copy(piobuf + 2, tmpbuf, plen - 1);
0648         qib_flush_wc();
0649         __raw_writel(tmpbuf[plen - 1], piobuf + plen + 1);
0650     } else
0651         qib_pio_copy(piobuf + 2, tmpbuf, plen);
0652 
0653     if (dd->flags & QIB_USE_SPCL_TRIG) {
0654         u32 spcl_off = (pbufn >= dd->piobcnt2k) ? 2047 : 1023;
0655 
0656         qib_flush_wc();
0657         __raw_writel(0xaebecede, piobuf + spcl_off);
0658     }
0659 
0660     /*
0661      * Ensure buffer is written to the chip, then re-enable
0662      * header checks (if supported by chip).  The txchk
0663      * code will ensure seen by chip before returning.
0664      */
0665     qib_flush_wc();
0666     qib_sendbuf_done(dd, pbufn);
0667     dd->f_txchk_change(dd, pbufn, 1, TXCHK_CHG_TYPE_ENAB1, NULL);
0668 
0669     ret = sizeof(dp);
0670 
0671 bail:
0672     vfree(tmpbuf);
0673     return ret;
0674 }
0675 
0676 static int qib_diag_release(struct inode *in, struct file *fp)
0677 {
0678     mutex_lock(&qib_mutex);
0679     return_client(fp->private_data);
0680     fp->private_data = NULL;
0681     mutex_unlock(&qib_mutex);
0682     return 0;
0683 }
0684 
0685 /*
0686  * Chip-specific code calls to register its interest in
0687  * a specific range.
0688  */
0689 struct diag_observer_list_elt {
0690     struct diag_observer_list_elt *next;
0691     const struct diag_observer *op;
0692 };
0693 
0694 int qib_register_observer(struct qib_devdata *dd,
0695               const struct diag_observer *op)
0696 {
0697     struct diag_observer_list_elt *olp;
0698     unsigned long flags;
0699 
0700     if (!dd || !op)
0701         return -EINVAL;
0702     olp = vmalloc(sizeof(*olp));
0703     if (!olp)
0704         return -ENOMEM;
0705 
0706     spin_lock_irqsave(&dd->qib_diag_trans_lock, flags);
0707     olp->op = op;
0708     olp->next = dd->diag_observer_list;
0709     dd->diag_observer_list = olp;
0710     spin_unlock_irqrestore(&dd->qib_diag_trans_lock, flags);
0711 
0712     return 0;
0713 }
0714 
0715 /* Remove all registered observers when device is closed */
0716 static void qib_unregister_observers(struct qib_devdata *dd)
0717 {
0718     struct diag_observer_list_elt *olp;
0719     unsigned long flags;
0720 
0721     spin_lock_irqsave(&dd->qib_diag_trans_lock, flags);
0722     olp = dd->diag_observer_list;
0723     while (olp) {
0724         /* Pop one observer, let go of lock */
0725         dd->diag_observer_list = olp->next;
0726         spin_unlock_irqrestore(&dd->qib_diag_trans_lock, flags);
0727         vfree(olp);
0728         /* try again. */
0729         spin_lock_irqsave(&dd->qib_diag_trans_lock, flags);
0730         olp = dd->diag_observer_list;
0731     }
0732     spin_unlock_irqrestore(&dd->qib_diag_trans_lock, flags);
0733 }
0734 
0735 /*
0736  * Find the observer, if any, for the specified address. Initial implementation
0737  * is simple stack of observers. This must be called with diag transaction
0738  * lock held.
0739  */
0740 static const struct diag_observer *diag_get_observer(struct qib_devdata *dd,
0741                              u32 addr)
0742 {
0743     struct diag_observer_list_elt *olp;
0744     const struct diag_observer *op = NULL;
0745 
0746     olp = dd->diag_observer_list;
0747     while (olp) {
0748         op = olp->op;
0749         if (addr >= op->bottom && addr <= op->top)
0750             break;
0751         olp = olp->next;
0752     }
0753     if (!olp)
0754         op = NULL;
0755 
0756     return op;
0757 }
0758 
0759 static ssize_t qib_diag_read(struct file *fp, char __user *data,
0760                  size_t count, loff_t *off)
0761 {
0762     struct qib_diag_client *dc = fp->private_data;
0763     struct qib_devdata *dd = dc->dd;
0764     ssize_t ret;
0765 
0766     if (dc->pid != current->pid) {
0767         ret = -EPERM;
0768         goto bail;
0769     }
0770 
0771     if (count == 0)
0772         ret = 0;
0773     else if ((count % 4) || (*off % 4))
0774         /* address or length is not 32-bit aligned, hence invalid */
0775         ret = -EINVAL;
0776     else if (dc->state < READY && (*off || count != 8))
0777         ret = -EINVAL;  /* prevent cat /dev/qib_diag* */
0778     else {
0779         unsigned long flags;
0780         u64 data64 = 0;
0781         int use_32;
0782         const struct diag_observer *op;
0783 
0784         use_32 = (count % 8) || (*off % 8);
0785         ret = -1;
0786         spin_lock_irqsave(&dd->qib_diag_trans_lock, flags);
0787         /*
0788          * Check for observer on this address range.
0789          * we only support a single 32 or 64-bit read
0790          * via observer, currently.
0791          */
0792         op = diag_get_observer(dd, *off);
0793         if (op) {
0794             u32 offset = *off;
0795 
0796             ret = op->hook(dd, op, offset, &data64, 0, use_32);
0797         }
0798         /*
0799          * We need to release lock before any copy_to_user(),
0800          * whether implicit in qib_read_umem* or explicit below.
0801          */
0802         spin_unlock_irqrestore(&dd->qib_diag_trans_lock, flags);
0803         if (!op) {
0804             if (use_32)
0805                 /*
0806                  * Address or length is not 64-bit aligned;
0807                  * do 32-bit rd
0808                  */
0809                 ret = qib_read_umem32(dd, data, (u32) *off,
0810                               count);
0811             else
0812                 ret = qib_read_umem64(dd, data, (u32) *off,
0813                               count);
0814         } else if (ret == count) {
0815             /* Below finishes case where observer existed */
0816             ret = copy_to_user(data, &data64, use_32 ?
0817                        sizeof(u32) : sizeof(u64));
0818             if (ret)
0819                 ret = -EFAULT;
0820         }
0821     }
0822 
0823     if (ret >= 0) {
0824         *off += count;
0825         ret = count;
0826         if (dc->state == OPENED)
0827             dc->state = INIT;
0828     }
0829 bail:
0830     return ret;
0831 }
0832 
0833 static ssize_t qib_diag_write(struct file *fp, const char __user *data,
0834                   size_t count, loff_t *off)
0835 {
0836     struct qib_diag_client *dc = fp->private_data;
0837     struct qib_devdata *dd = dc->dd;
0838     ssize_t ret;
0839 
0840     if (dc->pid != current->pid) {
0841         ret = -EPERM;
0842         goto bail;
0843     }
0844 
0845     if (count == 0)
0846         ret = 0;
0847     else if ((count % 4) || (*off % 4))
0848         /* address or length is not 32-bit aligned, hence invalid */
0849         ret = -EINVAL;
0850     else if (dc->state < READY &&
0851         ((*off || count != 8) || dc->state != INIT))
0852         /* No writes except second-step of init seq */
0853         ret = -EINVAL;  /* before any other write allowed */
0854     else {
0855         unsigned long flags;
0856         const struct diag_observer *op = NULL;
0857         int use_32 =  (count % 8) || (*off % 8);
0858 
0859         /*
0860          * Check for observer on this address range.
0861          * We only support a single 32 or 64-bit write
0862          * via observer, currently. This helps, because
0863          * we would otherwise have to jump through hoops
0864          * to make "diag transaction" meaningful when we
0865          * cannot do a copy_from_user while holding the lock.
0866          */
0867         if (count == 4 || count == 8) {
0868             u64 data64;
0869             u32 offset = *off;
0870 
0871             ret = copy_from_user(&data64, data, count);
0872             if (ret) {
0873                 ret = -EFAULT;
0874                 goto bail;
0875             }
0876             spin_lock_irqsave(&dd->qib_diag_trans_lock, flags);
0877             op = diag_get_observer(dd, *off);
0878             if (op)
0879                 ret = op->hook(dd, op, offset, &data64, ~0Ull,
0880                            use_32);
0881             spin_unlock_irqrestore(&dd->qib_diag_trans_lock, flags);
0882         }
0883 
0884         if (!op) {
0885             if (use_32)
0886                 /*
0887                  * Address or length is not 64-bit aligned;
0888                  * do 32-bit write
0889                  */
0890                 ret = qib_write_umem32(dd, (u32) *off, data,
0891                                count);
0892             else
0893                 ret = qib_write_umem64(dd, (u32) *off, data,
0894                                count);
0895         }
0896     }
0897 
0898     if (ret >= 0) {
0899         *off += count;
0900         ret = count;
0901         if (dc->state == INIT)
0902             dc->state = READY; /* all read/write OK now */
0903     }
0904 bail:
0905     return ret;
0906 }