Bryan O'Sullivan | 097709f | 2006-03-29 15:23:28 -0800 | [diff] [blame] | 1 | /* |
Bryan O'Sullivan | 759d576 | 2006-07-01 04:35:49 -0700 | [diff] [blame] | 2 | * Copyright (c) 2006 QLogic, Inc. All rights reserved. |
Bryan O'Sullivan | 097709f | 2006-03-29 15:23:28 -0800 | [diff] [blame] | 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/pci.h> |
| 45 | #include <asm/uaccess.h> |
| 46 | |
| 47 | #include "ipath_common.h" |
| 48 | #include "ipath_kernel.h" |
| 49 | #include "ips_common.h" |
| 50 | #include "ipath_layer.h" |
| 51 | |
| 52 | int ipath_diag_inuse; |
| 53 | static int diag_set_link; |
| 54 | |
| 55 | static int ipath_diag_open(struct inode *in, struct file *fp); |
| 56 | static int ipath_diag_release(struct inode *in, struct file *fp); |
| 57 | static ssize_t ipath_diag_read(struct file *fp, char __user *data, |
| 58 | size_t count, loff_t *off); |
| 59 | static ssize_t ipath_diag_write(struct file *fp, const char __user *data, |
| 60 | size_t count, loff_t *off); |
| 61 | |
| 62 | static 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 | |
| 70 | static struct cdev *diag_cdev; |
| 71 | static struct class_device *diag_class_dev; |
| 72 | |
| 73 | int ipath_diag_init(void) |
| 74 | { |
| 75 | return ipath_cdev_init(IPATH_DIAG_MINOR, "ipath_diag", |
| 76 | &diag_file_ops, &diag_cdev, &diag_class_dev); |
| 77 | } |
| 78 | |
| 79 | void ipath_diag_cleanup(void) |
| 80 | { |
| 81 | ipath_cdev_cleanup(&diag_cdev, &diag_class_dev); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * ipath_read_umem64 - read a 64-bit quantity from the chip into user space |
| 86 | * @dd: the infinipath device |
| 87 | * @uaddr: the location to store the data in user memory |
| 88 | * @caddr: the source chip address (full pointer, not offset) |
| 89 | * @count: number of bytes to copy (multiple of 32 bits) |
| 90 | * |
| 91 | * This function also localizes all chip memory accesses. |
| 92 | * The copy should be written such that we read full cacheline packets |
| 93 | * from the chip. This is usually used for a single qword |
| 94 | * |
| 95 | * NOTE: This assumes the chip address is 64-bit aligned. |
| 96 | */ |
| 97 | static int ipath_read_umem64(struct ipath_devdata *dd, void __user *uaddr, |
| 98 | const void __iomem *caddr, size_t count) |
| 99 | { |
| 100 | const u64 __iomem *reg_addr = caddr; |
| 101 | const u64 __iomem *reg_end = reg_addr + (count / sizeof(u64)); |
| 102 | int ret; |
| 103 | |
| 104 | /* not very efficient, but it works for now */ |
| 105 | if (reg_addr < dd->ipath_kregbase || |
| 106 | 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++; |
| 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 || |
| 144 | reg_end > dd->ipath_kregend) { |
| 145 | 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++; |
| 157 | uaddr++; |
| 158 | } |
| 159 | ret = 0; |
| 160 | bail: |
| 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 | */ |
| 174 | static 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++; |
| 195 | uaddr++; |
| 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++; |
| 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 | struct ipath_devdata *dd; |
| 244 | int unit = 0; /* XXX this is bogus */ |
| 245 | unsigned long flags; |
| 246 | int ret; |
| 247 | |
| 248 | dd = ipath_lookup(unit); |
| 249 | |
| 250 | mutex_lock(&ipath_mutex); |
| 251 | spin_lock_irqsave(&ipath_devs_lock, flags); |
| 252 | |
| 253 | if (ipath_diag_inuse) { |
| 254 | ret = -EBUSY; |
| 255 | goto bail; |
| 256 | } |
| 257 | |
| 258 | list_for_each_entry(dd, &ipath_dev_list, ipath_list) { |
| 259 | /* |
| 260 | * we need at least one infinipath device to be present |
| 261 | * (don't use INITTED, because we want to be able to open |
| 262 | * even if device is in freeze mode, which cleared INITTED). |
| 263 | * There is a small amount of risk to this, which is why we |
| 264 | * also verify kregbase is set. |
| 265 | */ |
| 266 | |
| 267 | if (!(dd->ipath_flags & IPATH_PRESENT) || |
| 268 | !dd->ipath_kregbase) |
| 269 | continue; |
| 270 | |
| 271 | ipath_diag_inuse = 1; |
| 272 | diag_set_link = 0; |
| 273 | ret = 0; |
| 274 | goto bail; |
| 275 | } |
| 276 | |
| 277 | ret = -ENODEV; |
| 278 | |
| 279 | bail: |
| 280 | spin_unlock_irqrestore(&ipath_devs_lock, flags); |
Bryan O'Sullivan | 097709f | 2006-03-29 15:23:28 -0800 | [diff] [blame] | 281 | |
| 282 | /* Only expose a way to reset the device if we |
| 283 | make it into diag mode. */ |
| 284 | if (ret == 0) |
| 285 | ipath_expose_reset(&dd->pcidev->dev); |
| 286 | |
Bryan O'Sullivan | 755e4ca | 2006-04-24 14:22:57 -0700 | [diff] [blame] | 287 | mutex_unlock(&ipath_mutex); |
| 288 | |
Bryan O'Sullivan | 097709f | 2006-03-29 15:23:28 -0800 | [diff] [blame] | 289 | return ret; |
| 290 | } |
| 291 | |
| 292 | static int ipath_diag_release(struct inode *i, struct file *f) |
| 293 | { |
| 294 | mutex_lock(&ipath_mutex); |
| 295 | ipath_diag_inuse = 0; |
| 296 | mutex_unlock(&ipath_mutex); |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | static ssize_t ipath_diag_read(struct file *fp, char __user *data, |
| 301 | size_t count, loff_t *off) |
| 302 | { |
| 303 | int unit = 0; /* XXX provide for reads on other units some day */ |
| 304 | struct ipath_devdata *dd; |
| 305 | void __iomem *kreg_base; |
| 306 | ssize_t ret; |
| 307 | |
| 308 | dd = ipath_lookup(unit); |
| 309 | if (!dd) { |
| 310 | ret = -ENODEV; |
| 311 | goto bail; |
| 312 | } |
| 313 | |
| 314 | kreg_base = dd->ipath_kregbase; |
| 315 | |
| 316 | if (count == 0) |
| 317 | ret = 0; |
| 318 | else if ((count % 4) || (*off % 4)) |
| 319 | /* address or length is not 32-bit aligned, hence invalid */ |
| 320 | ret = -EINVAL; |
| 321 | else if ((count % 8) || (*off % 8)) |
| 322 | /* address or length not 64-bit aligned; do 32-bit reads */ |
| 323 | ret = ipath_read_umem32(dd, data, kreg_base + *off, count); |
| 324 | else |
| 325 | ret = ipath_read_umem64(dd, data, kreg_base + *off, count); |
| 326 | |
| 327 | if (ret >= 0) { |
| 328 | *off += count; |
| 329 | ret = count; |
| 330 | } |
| 331 | |
| 332 | bail: |
| 333 | return ret; |
| 334 | } |
| 335 | |
| 336 | static ssize_t ipath_diag_write(struct file *fp, const char __user *data, |
| 337 | size_t count, loff_t *off) |
| 338 | { |
| 339 | int unit = 0; /* XXX this is bogus */ |
| 340 | struct ipath_devdata *dd; |
| 341 | void __iomem *kreg_base; |
| 342 | ssize_t ret; |
| 343 | |
| 344 | dd = ipath_lookup(unit); |
| 345 | if (!dd) { |
| 346 | ret = -ENODEV; |
| 347 | goto bail; |
| 348 | } |
| 349 | kreg_base = dd->ipath_kregbase; |
| 350 | |
| 351 | if (count == 0) |
| 352 | ret = 0; |
| 353 | else if ((count % 4) || (*off % 4)) |
| 354 | /* address or length is not 32-bit aligned, hence invalid */ |
| 355 | ret = -EINVAL; |
| 356 | else if ((count % 8) || (*off % 8)) |
| 357 | /* address or length not 64-bit aligned; do 32-bit writes */ |
| 358 | ret = ipath_write_umem32(dd, kreg_base + *off, data, count); |
| 359 | else |
| 360 | ret = ipath_write_umem64(dd, kreg_base + *off, data, count); |
| 361 | |
| 362 | if (ret >= 0) { |
| 363 | *off += count; |
| 364 | ret = count; |
| 365 | } |
| 366 | |
| 367 | bail: |
| 368 | return ret; |
| 369 | } |