blob: aab21c1b822d48d427061a42e833ca9c27a074a7 [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
Arjan van de Ven2b8693c2007-02-12 00:55:32 -080062static const struct file_operations diag_file_ops = {
Bryan O'Sullivan097709f2006-03-29 15:23:28 -080063 .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
Robert Walsh6ef93dd2006-10-10 14:55:45 -070070static ssize_t ipath_diagpkt_write(struct file *fp,
71 const char __user *data,
72 size_t count, loff_t *off);
73
Arjan van de Ven2b8693c2007-02-12 00:55:32 -080074static const struct file_operations diagpkt_file_ops = {
Robert Walsh6ef93dd2006-10-10 14:55:45 -070075 .owner = THIS_MODULE,
76 .write = ipath_diagpkt_write,
77};
78
79static atomic_t diagpkt_count = ATOMIC_INIT(0);
80static struct cdev *diagpkt_cdev;
81static struct class_device *diagpkt_class_dev;
82
Bryan O'Sullivana2acb2f2006-07-01 04:35:52 -070083int ipath_diag_add(struct ipath_devdata *dd)
Bryan O'Sullivan097709f2006-03-29 15:23:28 -080084{
Bryan O'Sullivana2acb2f2006-07-01 04:35:52 -070085 char name[16];
Robert Walsh6ef93dd2006-10-10 14:55:45 -070086 int ret = 0;
87
88 if (atomic_inc_return(&diagpkt_count) == 1) {
89 ret = ipath_cdev_init(IPATH_DIAGPKT_MINOR,
90 "ipath_diagpkt", &diagpkt_file_ops,
91 &diagpkt_cdev, &diagpkt_class_dev);
92
93 if (ret) {
94 ipath_dev_err(dd, "Couldn't create ipath_diagpkt "
95 "device: %d", ret);
96 goto done;
97 }
98 }
Bryan O'Sullivana2acb2f2006-07-01 04:35:52 -070099
100 snprintf(name, sizeof(name), "ipath_diag%d", dd->ipath_unit);
101
Robert Walsh6ef93dd2006-10-10 14:55:45 -0700102 ret = ipath_cdev_init(IPATH_DIAG_MINOR_BASE + dd->ipath_unit, name,
103 &diag_file_ops, &dd->diag_cdev,
104 &dd->diag_class_dev);
105 if (ret)
106 ipath_dev_err(dd, "Couldn't create %s device: %d",
107 name, ret);
108
109done:
110 return ret;
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800111}
112
Bryan O'Sullivana2acb2f2006-07-01 04:35:52 -0700113void ipath_diag_remove(struct ipath_devdata *dd)
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800114{
Robert Walsh6ef93dd2006-10-10 14:55:45 -0700115 if (atomic_dec_and_test(&diagpkt_count))
116 ipath_cdev_cleanup(&diagpkt_cdev, &diagpkt_class_dev);
117
Bryan O'Sullivana2acb2f2006-07-01 04:35:52 -0700118 ipath_cdev_cleanup(&dd->diag_cdev, &dd->diag_class_dev);
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800119}
120
121/**
122 * ipath_read_umem64 - read a 64-bit quantity from the chip into user space
123 * @dd: the infinipath device
124 * @uaddr: the location to store the data in user memory
125 * @caddr: the source chip address (full pointer, not offset)
126 * @count: number of bytes to copy (multiple of 32 bits)
127 *
128 * This function also localizes all chip memory accesses.
129 * The copy should be written such that we read full cacheline packets
130 * from the chip. This is usually used for a single qword
131 *
132 * NOTE: This assumes the chip address is 64-bit aligned.
133 */
134static int ipath_read_umem64(struct ipath_devdata *dd, void __user *uaddr,
135 const void __iomem *caddr, size_t count)
136{
137 const u64 __iomem *reg_addr = caddr;
138 const u64 __iomem *reg_end = reg_addr + (count / sizeof(u64));
139 int ret;
140
141 /* not very efficient, but it works for now */
Bryan O'Sullivana2acb2f2006-07-01 04:35:52 -0700142 if (reg_addr < dd->ipath_kregbase || reg_end > dd->ipath_kregend) {
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800143 ret = -EINVAL;
144 goto bail;
145 }
146 while (reg_addr < reg_end) {
147 u64 data = readq(reg_addr);
148 if (copy_to_user(uaddr, &data, sizeof(u64))) {
149 ret = -EFAULT;
150 goto bail;
151 }
152 reg_addr++;
Bryan O'Sullivan6d8e9dd2006-07-01 04:36:14 -0700153 uaddr += sizeof(u64);
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800154 }
155 ret = 0;
156bail:
157 return ret;
158}
159
160/**
161 * ipath_write_umem64 - write a 64-bit quantity to the chip from user space
162 * @dd: the infinipath device
163 * @caddr: the destination chip address (full pointer, not offset)
164 * @uaddr: the source of the data in user memory
165 * @count: the number of bytes to copy (multiple of 32 bits)
166 *
167 * This is usually used for a single qword
168 * NOTE: This assumes the chip address is 64-bit aligned.
169 */
170
171static int ipath_write_umem64(struct ipath_devdata *dd, void __iomem *caddr,
172 const void __user *uaddr, size_t count)
173{
174 u64 __iomem *reg_addr = caddr;
175 const u64 __iomem *reg_end = reg_addr + (count / sizeof(u64));
176 int ret;
177
178 /* not very efficient, but it works for now */
Bryan O'Sullivana2acb2f2006-07-01 04:35:52 -0700179 if (reg_addr < dd->ipath_kregbase || reg_end > dd->ipath_kregend) {
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800180 ret = -EINVAL;
181 goto bail;
182 }
183 while (reg_addr < reg_end) {
184 u64 data;
185 if (copy_from_user(&data, uaddr, sizeof(data))) {
186 ret = -EFAULT;
187 goto bail;
188 }
189 writeq(data, reg_addr);
190
191 reg_addr++;
Bryan O'Sullivan6d8e9dd2006-07-01 04:36:14 -0700192 uaddr += sizeof(u64);
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800193 }
194 ret = 0;
195bail:
196 return ret;
197}
198
199/**
200 * ipath_read_umem32 - read a 32-bit quantity from the chip into user space
201 * @dd: the infinipath device
202 * @uaddr: the location to store the data in user memory
203 * @caddr: the source chip address (full pointer, not offset)
204 * @count: number of bytes to copy
205 *
206 * read 32 bit values, not 64 bit; for memories that only
207 * support 32 bit reads; usually a single dword.
208 */
209static int ipath_read_umem32(struct ipath_devdata *dd, void __user *uaddr,
210 const void __iomem *caddr, size_t count)
211{
212 const u32 __iomem *reg_addr = caddr;
213 const u32 __iomem *reg_end = reg_addr + (count / sizeof(u32));
214 int ret;
215
216 if (reg_addr < (u32 __iomem *) dd->ipath_kregbase ||
217 reg_end > (u32 __iomem *) dd->ipath_kregend) {
218 ret = -EINVAL;
219 goto bail;
220 }
221 /* not very efficient, but it works for now */
222 while (reg_addr < reg_end) {
223 u32 data = readl(reg_addr);
224 if (copy_to_user(uaddr, &data, sizeof(data))) {
225 ret = -EFAULT;
226 goto bail;
227 }
228
229 reg_addr++;
Bryan O'Sullivan6d8e9dd2006-07-01 04:36:14 -0700230 uaddr += sizeof(u32);
231
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800232 }
233 ret = 0;
234bail:
235 return ret;
236}
237
238/**
239 * ipath_write_umem32 - write a 32-bit quantity to the chip from user space
240 * @dd: the infinipath device
241 * @caddr: the destination chip address (full pointer, not offset)
242 * @uaddr: the source of the data in user memory
243 * @count: number of bytes to copy
244 *
245 * write 32 bit values, not 64 bit; for memories that only
246 * support 32 bit write; usually a single dword.
247 */
248
249static int ipath_write_umem32(struct ipath_devdata *dd, void __iomem *caddr,
250 const void __user *uaddr, size_t count)
251{
252 u32 __iomem *reg_addr = caddr;
253 const u32 __iomem *reg_end = reg_addr + (count / sizeof(u32));
254 int ret;
255
256 if (reg_addr < (u32 __iomem *) dd->ipath_kregbase ||
257 reg_end > (u32 __iomem *) dd->ipath_kregend) {
258 ret = -EINVAL;
259 goto bail;
260 }
261 while (reg_addr < reg_end) {
262 u32 data;
263 if (copy_from_user(&data, uaddr, sizeof(data))) {
264 ret = -EFAULT;
265 goto bail;
266 }
267 writel(data, reg_addr);
268
269 reg_addr++;
Bryan O'Sullivan6d8e9dd2006-07-01 04:36:14 -0700270 uaddr += sizeof(u32);
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800271 }
272 ret = 0;
273bail:
274 return ret;
275}
276
277static int ipath_diag_open(struct inode *in, struct file *fp)
278{
Bryan O'Sullivana2acb2f2006-07-01 04:35:52 -0700279 int unit = iminor(in) - IPATH_DIAG_MINOR_BASE;
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800280 struct ipath_devdata *dd;
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800281 int ret;
282
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800283 mutex_lock(&ipath_mutex);
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800284
285 if (ipath_diag_inuse) {
286 ret = -EBUSY;
287 goto bail;
288 }
289
Bryan O'Sullivana2acb2f2006-07-01 04:35:52 -0700290 dd = ipath_lookup(unit);
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800291
Bryan O'Sullivana2acb2f2006-07-01 04:35:52 -0700292 if (dd == NULL || !(dd->ipath_flags & IPATH_PRESENT) ||
293 !dd->ipath_kregbase) {
294 ret = -ENODEV;
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800295 goto bail;
296 }
297
Bryan O'Sullivana2acb2f2006-07-01 04:35:52 -0700298 fp->private_data = dd;
Bryan O'Sullivan490462c2007-03-15 14:45:10 -0700299 ipath_diag_inuse = -2;
Bryan O'Sullivana2acb2f2006-07-01 04:35:52 -0700300 diag_set_link = 0;
301 ret = 0;
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800302
303 /* Only expose a way to reset the device if we
304 make it into diag mode. */
Bryan O'Sullivana2acb2f2006-07-01 04:35:52 -0700305 ipath_expose_reset(&dd->pcidev->dev);
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800306
Bryan O'Sullivana2acb2f2006-07-01 04:35:52 -0700307bail:
Bryan O'Sullivan755e4ca2006-04-24 14:22:57 -0700308 mutex_unlock(&ipath_mutex);
309
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800310 return ret;
311}
312
Bryan O'Sullivan98341f22006-08-25 11:24:36 -0700313/**
314 * ipath_diagpkt_write - write an IB packet
315 * @fp: the diag data device file pointer
316 * @data: ipath_diag_pkt structure saying where to get the packet
317 * @count: size of data to write
318 * @off: unused by this code
319 */
320static ssize_t ipath_diagpkt_write(struct file *fp,
321 const char __user *data,
322 size_t count, loff_t *off)
323{
324 u32 __iomem *piobuf;
325 u32 plen, clen, pbufn;
Michael Albaughe8e7ad72007-06-18 14:24:47 -0700326 struct ipath_diag_pkt odp;
327 struct ipath_diag_xpkt dp;
Bryan O'Sullivan98341f22006-08-25 11:24:36 -0700328 u32 *tmpbuf = NULL;
329 struct ipath_devdata *dd;
330 ssize_t ret = 0;
331 u64 val;
332
Michael Albaughe8e7ad72007-06-18 14:24:47 -0700333 if (count != sizeof(dp)) {
Bryan O'Sullivan98341f22006-08-25 11:24:36 -0700334 ret = -EINVAL;
335 goto bail;
336 }
337
338 if (copy_from_user(&dp, data, sizeof(dp))) {
339 ret = -EFAULT;
340 goto bail;
341 }
342
Michael Albaughe8e7ad72007-06-18 14:24:47 -0700343 /*
344 * Due to padding/alignment issues (lessened with new struct)
345 * the old and new structs are the same length. We need to
346 * disambiguate them, which we can do because odp.len has never
347 * been less than the total of LRH+BTH+DETH so far, while
348 * dp.unit (same offset) unit is unlikely to get that high.
349 * Similarly, dp.data, the pointer to user at the same offset
350 * as odp.unit, is almost certainly at least one (512byte)page
351 * "above" NULL. The if-block below can be omitted if compatibility
352 * between a new driver and older diagnostic code is unimportant.
353 * compatibility the other direction (new diags, old driver) is
354 * handled in the diagnostic code, with a warning.
355 */
356 if (dp.unit >= 20 && dp.data < 512) {
357 /* very probable version mismatch. Fix it up */
358 memcpy(&odp, &dp, sizeof(odp));
359 /* We got a legacy dp, copy elements to dp */
360 dp.unit = odp.unit;
361 dp.data = odp.data;
362 dp.len = odp.len;
363 dp.pbc_wd = 0; /* Indicate we need to compute PBC wd */
364 }
365
Bryan O'Sullivan98341f22006-08-25 11:24:36 -0700366 /* send count must be an exact number of dwords */
367 if (dp.len & 3) {
368 ret = -EINVAL;
369 goto bail;
370 }
371
372 clen = dp.len >> 2;
373
374 dd = ipath_lookup(dp.unit);
375 if (!dd || !(dd->ipath_flags & IPATH_PRESENT) ||
376 !dd->ipath_kregbase) {
377 ipath_cdbg(VERBOSE, "illegal unit %u for diag data send\n",
378 dp.unit);
379 ret = -ENODEV;
380 goto bail;
381 }
382
383 if (ipath_diag_inuse && !diag_set_link &&
384 !(dd->ipath_flags & IPATH_LINKACTIVE)) {
385 diag_set_link = 1;
386 ipath_cdbg(VERBOSE, "Trying to set to set link active for "
387 "diag pkt\n");
388 ipath_set_linkstate(dd, IPATH_IB_LINKARM);
389 ipath_set_linkstate(dd, IPATH_IB_LINKACTIVE);
390 }
391
392 if (!(dd->ipath_flags & IPATH_INITTED)) {
393 /* no hardware, freeze, etc. */
394 ipath_cdbg(VERBOSE, "unit %u not usable\n", dd->ipath_unit);
395 ret = -ENODEV;
396 goto bail;
397 }
Michael Albaughe8e7ad72007-06-18 14:24:47 -0700398 /* Check link state, but not if we have custom PBC */
Bryan O'Sullivan98341f22006-08-25 11:24:36 -0700399 val = dd->ipath_lastibcstat & IPATH_IBSTATE_MASK;
Michael Albaughe8e7ad72007-06-18 14:24:47 -0700400 if (!dp.pbc_wd && val != IPATH_IBSTATE_INIT &&
401 val != IPATH_IBSTATE_ARM && val != IPATH_IBSTATE_ACTIVE) {
Bryan O'Sullivan98341f22006-08-25 11:24:36 -0700402 ipath_cdbg(VERBOSE, "unit %u not ready (state %llx)\n",
403 dd->ipath_unit, (unsigned long long) val);
404 ret = -EINVAL;
405 goto bail;
406 }
407
408 /* need total length before first word written */
409 /* +1 word is for the qword padding */
410 plen = sizeof(u32) + dp.len;
411
412 if ((plen + 4) > dd->ipath_ibmaxlen) {
413 ipath_dbg("Pkt len 0x%x > ibmaxlen %x\n",
414 plen - 4, dd->ipath_ibmaxlen);
415 ret = -EINVAL;
416 goto bail; /* before writing pbc */
417 }
418 tmpbuf = vmalloc(plen);
419 if (!tmpbuf) {
420 dev_info(&dd->pcidev->dev, "Unable to allocate tmp buffer, "
421 "failing\n");
422 ret = -ENOMEM;
423 goto bail;
424 }
425
426 if (copy_from_user(tmpbuf,
427 (const void __user *) (unsigned long) dp.data,
428 dp.len)) {
429 ret = -EFAULT;
430 goto bail;
431 }
432
433 piobuf = ipath_getpiobuf(dd, &pbufn);
434 if (!piobuf) {
435 ipath_cdbg(VERBOSE, "No PIO buffers avail unit for %u\n",
436 dd->ipath_unit);
437 ret = -EBUSY;
438 goto bail;
439 }
440
441 plen >>= 2; /* in dwords */
442
443 if (ipath_debug & __IPATH_PKTDBG)
444 ipath_cdbg(VERBOSE, "unit %u 0x%x+1w pio%d\n",
445 dd->ipath_unit, plen - 1, pbufn);
446
Michael Albaughe8e7ad72007-06-18 14:24:47 -0700447 if (dp.pbc_wd == 0)
448 /* Legacy operation, use computed pbc_wd */
449 dp.pbc_wd = plen;
450
Bryan O'Sullivan98341f22006-08-25 11:24:36 -0700451 /* we have to flush after the PBC for correctness on some cpus
452 * or WC buffer can be written out of order */
Michael Albaughe8e7ad72007-06-18 14:24:47 -0700453 writeq(dp.pbc_wd, piobuf);
Bryan O'Sullivan98341f22006-08-25 11:24:36 -0700454 ipath_flush_wc();
455 /* copy all by the trigger word, then flush, so it's written
456 * to chip before trigger word, then write trigger word, then
457 * flush again, so packet is sent. */
458 __iowrite32_copy(piobuf + 2, tmpbuf, clen - 1);
459 ipath_flush_wc();
460 __raw_writel(tmpbuf[clen - 1], piobuf + clen + 1);
461 ipath_flush_wc();
462
463 ret = sizeof(dp);
464
465bail:
466 vfree(tmpbuf);
467 return ret;
468}
469
Bryan O'Sullivana2acb2f2006-07-01 04:35:52 -0700470static int ipath_diag_release(struct inode *in, struct file *fp)
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800471{
472 mutex_lock(&ipath_mutex);
473 ipath_diag_inuse = 0;
Bryan O'Sullivana2acb2f2006-07-01 04:35:52 -0700474 fp->private_data = NULL;
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800475 mutex_unlock(&ipath_mutex);
476 return 0;
477}
478
479static ssize_t ipath_diag_read(struct file *fp, char __user *data,
480 size_t count, loff_t *off)
481{
Bryan O'Sullivana2acb2f2006-07-01 04:35:52 -0700482 struct ipath_devdata *dd = fp->private_data;
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800483 void __iomem *kreg_base;
484 ssize_t ret;
485
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800486 kreg_base = dd->ipath_kregbase;
487
488 if (count == 0)
489 ret = 0;
490 else if ((count % 4) || (*off % 4))
491 /* address or length is not 32-bit aligned, hence invalid */
492 ret = -EINVAL;
Bryan O'Sullivan490462c2007-03-15 14:45:10 -0700493 else if (ipath_diag_inuse < 1 && (*off || count != 8))
494 ret = -EINVAL; /* prevent cat /dev/ipath_diag* */
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800495 else if ((count % 8) || (*off % 8))
496 /* address or length not 64-bit aligned; do 32-bit reads */
497 ret = ipath_read_umem32(dd, data, kreg_base + *off, count);
498 else
499 ret = ipath_read_umem64(dd, data, kreg_base + *off, count);
500
501 if (ret >= 0) {
502 *off += count;
503 ret = count;
Bryan O'Sullivan490462c2007-03-15 14:45:10 -0700504 if (ipath_diag_inuse == -2)
505 ipath_diag_inuse++;
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800506 }
507
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800508 return ret;
509}
510
511static ssize_t ipath_diag_write(struct file *fp, const char __user *data,
512 size_t count, loff_t *off)
513{
Bryan O'Sullivana2acb2f2006-07-01 04:35:52 -0700514 struct ipath_devdata *dd = fp->private_data;
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800515 void __iomem *kreg_base;
516 ssize_t ret;
517
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800518 kreg_base = dd->ipath_kregbase;
519
520 if (count == 0)
521 ret = 0;
522 else if ((count % 4) || (*off % 4))
523 /* address or length is not 32-bit aligned, hence invalid */
524 ret = -EINVAL;
Bryan O'Sullivan490462c2007-03-15 14:45:10 -0700525 else if ((ipath_diag_inuse == -1 && (*off || count != 8)) ||
526 ipath_diag_inuse == -2) /* read qw off 0, write qw off 0 */
527 ret = -EINVAL; /* before any other write allowed */
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800528 else if ((count % 8) || (*off % 8))
529 /* address or length not 64-bit aligned; do 32-bit writes */
530 ret = ipath_write_umem32(dd, kreg_base + *off, data, count);
531 else
532 ret = ipath_write_umem64(dd, kreg_base + *off, data, count);
533
534 if (ret >= 0) {
535 *off += count;
536 ret = count;
Bryan O'Sullivan490462c2007-03-15 14:45:10 -0700537 if (ipath_diag_inuse == -1)
538 ipath_diag_inuse = 1; /* all read/write OK now */
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800539 }
540
Bryan O'Sullivan097709f2006-03-29 15:23:28 -0800541 return ret;
542}