initial commit with v2.6.9
[linux-2.6.9-moxart.git] / drivers / scsi / scsi_transport_spi.c
blob9c41eefb9640e56dac3df75a53bc90d7d71f4c66
1 /*
2 * Parallel SCSI (SPI) transport specific attributes exported to sysfs.
4 * Copyright (c) 2003 Silicon Graphics, Inc. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/smp_lock.h>
23 #include <linux/list.h>
24 #include <linux/spinlock.h>
25 #include <linux/mm.h>
26 #include <linux/workqueue.h>
27 #include <asm/scatterlist.h>
28 #include <asm/io.h>
29 #include <scsi/scsi.h>
30 #include <scsi/scsi_device.h>
31 #include <scsi/scsi_host.h>
32 #include <scsi/scsi_request.h>
33 #include <scsi/scsi_transport.h>
34 #include <scsi/scsi_transport_spi.h>
36 #define SPI_PRINTK(x, l, f, a...) printk(l "scsi(%d:%d:%d:%d): " f, (x)->host->host_no, (x)->channel, (x)->id, (x)->lun , ##a)
38 static void transport_class_release(struct class_device *class_dev);
40 #define SPI_NUM_ATTRS 10 /* increase this if you add attributes */
41 #define SPI_OTHER_ATTRS 1 /* Increase this if you add "always
42 * on" attributes */
44 #define SPI_MAX_ECHO_BUFFER_SIZE 4096
46 /* Private data accessors (keep these out of the header file) */
47 #define spi_dv_pending(x) (((struct spi_transport_attrs *)&(x)->transport_data)->dv_pending)
48 #define spi_dv_sem(x) (((struct spi_transport_attrs *)&(x)->transport_data)->dv_sem)
50 struct spi_internal {
51 struct scsi_transport_template t;
52 struct spi_function_template *f;
53 /* The actual attributes */
54 struct class_device_attribute private_attrs[SPI_NUM_ATTRS];
55 /* The array of null terminated pointers to attributes
56 * needed by scsi_sysfs.c */
57 struct class_device_attribute *attrs[SPI_NUM_ATTRS + SPI_OTHER_ATTRS + 1];
60 #define to_spi_internal(tmpl) container_of(tmpl, struct spi_internal, t)
62 static const char *const ppr_to_ns[] = {
63 /* The PPR values 0-6 are reserved, fill them in when
64 * the committee defines them */
65 NULL, /* 0x00 */
66 NULL, /* 0x01 */
67 NULL, /* 0x02 */
68 NULL, /* 0x03 */
69 NULL, /* 0x04 */
70 NULL, /* 0x05 */
71 NULL, /* 0x06 */
72 "3.125", /* 0x07 */
73 "6.25", /* 0x08 */
74 "12.5", /* 0x09 */
75 "25", /* 0x0a */
76 "30.3", /* 0x0b */
77 "50", /* 0x0c */
79 /* The PPR values at which you calculate the period in ns by multiplying
80 * by 4 */
81 #define SPI_STATIC_PPR 0x0c
83 struct class spi_transport_class = {
84 .name = "spi_transport",
85 .release = transport_class_release,
88 static __init int spi_transport_init(void)
90 return class_register(&spi_transport_class);
93 static void __exit spi_transport_exit(void)
95 class_unregister(&spi_transport_class);
98 static int spi_setup_transport_attrs(struct scsi_device *sdev)
100 spi_period(sdev) = -1; /* illegal value */
101 spi_offset(sdev) = 0; /* async */
102 spi_width(sdev) = 0; /* narrow */
103 spi_iu(sdev) = 0; /* no IU */
104 spi_dt(sdev) = 0; /* ST */
105 spi_qas(sdev) = 0;
106 spi_wr_flow(sdev) = 0;
107 spi_rd_strm(sdev) = 0;
108 spi_rti(sdev) = 0;
109 spi_pcomp_en(sdev) = 0;
110 spi_dv_pending(sdev) = 0;
111 init_MUTEX(&spi_dv_sem(sdev));
113 return 0;
116 static void transport_class_release(struct class_device *class_dev)
118 struct scsi_device *sdev = transport_class_to_sdev(class_dev);
119 put_device(&sdev->sdev_gendev);
122 #define spi_transport_show_function(field, format_string) \
124 static ssize_t \
125 show_spi_transport_##field(struct class_device *cdev, char *buf) \
127 struct scsi_device *sdev = transport_class_to_sdev(cdev); \
128 struct spi_transport_attrs *tp; \
129 struct spi_internal *i = to_spi_internal(sdev->host->transportt); \
130 tp = (struct spi_transport_attrs *)&sdev->transport_data; \
131 if (i->f->get_##field) \
132 i->f->get_##field(sdev); \
133 return snprintf(buf, 20, format_string, tp->field); \
136 #define spi_transport_store_function(field, format_string) \
137 static ssize_t \
138 store_spi_transport_##field(struct class_device *cdev, const char *buf, \
139 size_t count) \
141 int val; \
142 struct scsi_device *sdev = transport_class_to_sdev(cdev); \
143 struct spi_internal *i = to_spi_internal(sdev->host->transportt); \
145 val = simple_strtoul(buf, NULL, 0); \
146 i->f->set_##field(sdev, val); \
147 return count; \
150 #define spi_transport_rd_attr(field, format_string) \
151 spi_transport_show_function(field, format_string) \
152 spi_transport_store_function(field, format_string) \
153 static CLASS_DEVICE_ATTR(field, S_IRUGO | S_IWUSR, \
154 show_spi_transport_##field, \
155 store_spi_transport_##field);
157 /* The Parallel SCSI Tranport Attributes: */
158 spi_transport_rd_attr(offset, "%d\n");
159 spi_transport_rd_attr(width, "%d\n");
160 spi_transport_rd_attr(iu, "%d\n");
161 spi_transport_rd_attr(dt, "%d\n");
162 spi_transport_rd_attr(qas, "%d\n");
163 spi_transport_rd_attr(wr_flow, "%d\n");
164 spi_transport_rd_attr(rd_strm, "%d\n");
165 spi_transport_rd_attr(rti, "%d\n");
166 spi_transport_rd_attr(pcomp_en, "%d\n");
168 static ssize_t
169 store_spi_revalidate(struct class_device *cdev, const char *buf, size_t count)
171 struct scsi_device *sdev = transport_class_to_sdev(cdev);
173 spi_dv_device(sdev);
174 return count;
176 static CLASS_DEVICE_ATTR(revalidate, S_IWUSR, NULL, store_spi_revalidate);
178 /* Translate the period into ns according to the current spec
179 * for SDTR/PPR messages */
180 static ssize_t show_spi_transport_period(struct class_device *cdev, char *buf)
183 struct scsi_device *sdev = transport_class_to_sdev(cdev);
184 struct spi_transport_attrs *tp;
185 const char *str;
186 struct spi_internal *i = to_spi_internal(sdev->host->transportt);
188 tp = (struct spi_transport_attrs *)&sdev->transport_data;
190 if (i->f->get_period)
191 i->f->get_period(sdev);
193 switch(tp->period) {
195 case 0x07 ... SPI_STATIC_PPR:
196 str = ppr_to_ns[tp->period];
197 if(!str)
198 str = "reserved";
199 break;
202 case (SPI_STATIC_PPR+1) ... 0xff:
203 return sprintf(buf, "%d\n", tp->period * 4);
205 default:
206 str = "unknown";
208 return sprintf(buf, "%s\n", str);
211 static ssize_t
212 store_spi_transport_period(struct class_device *cdev, const char *buf,
213 size_t count)
215 struct scsi_device *sdev = transport_class_to_sdev(cdev);
216 struct spi_internal *i = to_spi_internal(sdev->host->transportt);
217 int j, period = -1;
219 for (j = 0; j < SPI_STATIC_PPR; j++) {
220 int len;
222 if(ppr_to_ns[j] == NULL)
223 continue;
225 len = strlen(ppr_to_ns[j]);
227 if(strncmp(ppr_to_ns[j], buf, len) != 0)
228 continue;
230 if(buf[len] != '\n')
231 continue;
233 period = j;
234 break;
237 if (period == -1) {
238 int val = simple_strtoul(buf, NULL, 0);
241 /* Should probably check limits here, but this
242 * gets reasonably close to OK for most things */
243 period = val/4;
246 if (period > 0xff)
247 period = 0xff;
249 i->f->set_period(sdev, period);
251 return count;
254 static CLASS_DEVICE_ATTR(period, S_IRUGO | S_IWUSR,
255 show_spi_transport_period,
256 store_spi_transport_period);
258 #define DV_SET(x, y) \
259 if(i->f->set_##x) \
260 i->f->set_##x(sdev, y)
262 #define DV_LOOPS 3
263 #define DV_TIMEOUT (10*HZ)
264 #define DV_RETRIES 3 /* should only need at most
265 * two cc/ua clears */
268 /* This is for read/write Domain Validation: If the device supports
269 * an echo buffer, we do read/write tests to it */
270 static int
271 spi_dv_device_echo_buffer(struct scsi_request *sreq, u8 *buffer,
272 u8 *ptr, const int retries)
274 struct scsi_device *sdev = sreq->sr_device;
275 int len = ptr - buffer;
276 int j, k, r;
277 unsigned int pattern = 0x0000ffff;
279 const char spi_write_buffer[] = {
280 WRITE_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
282 const char spi_read_buffer[] = {
283 READ_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
286 /* set up the pattern buffer. Doesn't matter if we spill
287 * slightly beyond since that's where the read buffer is */
288 for (j = 0; j < len; ) {
290 /* fill the buffer with counting (test a) */
291 for ( ; j < min(len, 32); j++)
292 buffer[j] = j;
293 k = j;
294 /* fill the buffer with alternating words of 0x0 and
295 * 0xffff (test b) */
296 for ( ; j < min(len, k + 32); j += 2) {
297 u16 *word = (u16 *)&buffer[j];
299 *word = (j & 0x02) ? 0x0000 : 0xffff;
301 k = j;
302 /* fill with crosstalk (alternating 0x5555 0xaaa)
303 * (test c) */
304 for ( ; j < min(len, k + 32); j += 2) {
305 u16 *word = (u16 *)&buffer[j];
307 *word = (j & 0x02) ? 0x5555 : 0xaaaa;
309 k = j;
310 /* fill with shifting bits (test d) */
311 for ( ; j < min(len, k + 32); j += 4) {
312 u32 *word = (unsigned int *)&buffer[j];
313 u32 roll = (pattern & 0x80000000) ? 1 : 0;
315 *word = pattern;
316 pattern = (pattern << 1) | roll;
318 /* don't bother with random data (test e) */
321 for (r = 0; r < retries; r++) {
322 sreq->sr_cmd_len = 0; /* wait_req to fill in */
323 sreq->sr_data_direction = DMA_TO_DEVICE;
324 scsi_wait_req(sreq, spi_write_buffer, buffer, len,
325 DV_TIMEOUT, DV_RETRIES);
326 if(sreq->sr_result || !scsi_device_online(sdev)) {
327 scsi_device_set_state(sdev, SDEV_QUIESCE);
328 SPI_PRINTK(sdev, KERN_ERR, "Write Buffer failure %x\n", sreq->sr_result);
329 return 0;
332 memset(ptr, 0, len);
333 sreq->sr_cmd_len = 0; /* wait_req to fill in */
334 sreq->sr_data_direction = DMA_FROM_DEVICE;
335 scsi_wait_req(sreq, spi_read_buffer, ptr, len,
336 DV_TIMEOUT, DV_RETRIES);
337 scsi_device_set_state(sdev, SDEV_QUIESCE);
339 if (memcmp(buffer, ptr, len) != 0)
340 return 0;
342 return 1;
345 /* This is for the simplest form of Domain Validation: a read test
346 * on the inquiry data from the device */
347 static int
348 spi_dv_device_compare_inquiry(struct scsi_request *sreq, u8 *buffer,
349 u8 *ptr, const int retries)
351 int r;
352 const int len = sreq->sr_device->inquiry_len;
353 struct scsi_device *sdev = sreq->sr_device;
354 const char spi_inquiry[] = {
355 INQUIRY, 0, 0, 0, len, 0
358 for (r = 0; r < retries; r++) {
359 sreq->sr_cmd_len = 0; /* wait_req to fill in */
360 sreq->sr_data_direction = DMA_FROM_DEVICE;
362 memset(ptr, 0, len);
364 scsi_wait_req(sreq, spi_inquiry, ptr, len,
365 DV_TIMEOUT, DV_RETRIES);
367 if(sreq->sr_result || !scsi_device_online(sdev)) {
368 scsi_device_set_state(sdev, SDEV_QUIESCE);
369 return 0;
372 /* If we don't have the inquiry data already, the
373 * first read gets it */
374 if (ptr == buffer) {
375 ptr += len;
376 --r;
377 continue;
380 if (memcmp(buffer, ptr, len) != 0)
381 /* failure */
382 return 0;
384 return 1;
387 static int
388 spi_dv_retrain(struct scsi_request *sreq, u8 *buffer, u8 *ptr,
389 int (*compare_fn)(struct scsi_request *, u8 *, u8 *, int))
391 struct spi_internal *i = to_spi_internal(sreq->sr_host->transportt);
392 struct scsi_device *sdev = sreq->sr_device;
393 int period = 0, prevperiod = 0;
396 for (;;) {
397 int newperiod;
398 if (compare_fn(sreq, buffer, ptr, DV_LOOPS))
399 /* Successful DV */
400 break;
402 /* OK, retrain, fallback */
403 if (i->f->get_period)
404 i->f->get_period(sdev);
405 newperiod = spi_period(sdev);
406 period = newperiod > period ? newperiod : period;
407 if (period < 0x0d)
408 period++;
409 else
410 period += period >> 1;
412 if (unlikely(period > 0xff || period == prevperiod)) {
413 /* Total failure; set to async and return */
414 SPI_PRINTK(sdev, KERN_ERR, "Domain Validation Failure, dropping back to Asynchronous\n");
415 DV_SET(offset, 0);
416 return 0;
418 SPI_PRINTK(sdev, KERN_ERR, "Domain Validation detected failure, dropping back\n");
419 DV_SET(period, period);
420 prevperiod = period;
422 return 1;
425 static int
426 spi_dv_device_get_echo_buffer(struct scsi_request *sreq, u8 *buffer)
428 int l;
430 /* first off do a test unit ready. This can error out
431 * because of reservations or some other reason. If it
432 * fails, the device won't let us write to the echo buffer
433 * so just return failure */
435 const char spi_test_unit_ready[] = {
436 TEST_UNIT_READY, 0, 0, 0, 0, 0
439 const char spi_read_buffer_descriptor[] = {
440 READ_BUFFER, 0x0b, 0, 0, 0, 0, 0, 0, 4, 0
444 sreq->sr_cmd_len = 0;
445 sreq->sr_data_direction = DMA_NONE;
447 /* We send a set of three TURs to clear any outstanding
448 * unit attention conditions if they exist (Otherwise the
449 * buffer tests won't be happy). If the TUR still fails
450 * (reservation conflict, device not ready, etc) just
451 * skip the write tests */
452 for (l = 0; ; l++) {
453 scsi_wait_req(sreq, spi_test_unit_ready, NULL, 0,
454 DV_TIMEOUT, DV_RETRIES);
456 if(sreq->sr_result) {
457 if(l >= 3)
458 return 0;
459 } else {
460 /* TUR succeeded */
461 break;
465 sreq->sr_cmd_len = 0;
466 sreq->sr_data_direction = DMA_FROM_DEVICE;
468 scsi_wait_req(sreq, spi_read_buffer_descriptor, buffer, 4,
469 DV_TIMEOUT, DV_RETRIES);
471 if (sreq->sr_result)
472 /* Device has no echo buffer */
473 return 0;
475 return buffer[3] + ((buffer[2] & 0x1f) << 8);
478 static void
479 spi_dv_device_internal(struct scsi_request *sreq, u8 *buffer)
481 struct spi_internal *i = to_spi_internal(sreq->sr_host->transportt);
482 struct scsi_device *sdev = sreq->sr_device;
483 int len = sdev->inquiry_len;
484 /* first set us up for narrow async */
485 DV_SET(offset, 0);
486 DV_SET(width, 0);
488 if (!spi_dv_device_compare_inquiry(sreq, buffer, buffer, DV_LOOPS)) {
489 SPI_PRINTK(sdev, KERN_ERR, "Domain Validation Initial Inquiry Failed\n");
490 /* FIXME: should probably offline the device here? */
491 return;
494 /* test width */
495 if (i->f->set_width && sdev->wdtr) {
496 i->f->set_width(sdev, 1);
498 if (!spi_dv_device_compare_inquiry(sreq, buffer,
499 buffer + len,
500 DV_LOOPS)) {
501 SPI_PRINTK(sdev, KERN_ERR, "Wide Transfers Fail\n");
502 i->f->set_width(sdev, 0);
506 if (!i->f->set_period)
507 return;
509 /* device can't handle synchronous */
510 if(!sdev->ppr && !sdev->sdtr)
511 return;
513 /* now set up to the maximum */
514 DV_SET(offset, 255);
515 DV_SET(period, 1);
516 if (!spi_dv_retrain(sreq, buffer, buffer + len,
517 spi_dv_device_compare_inquiry))
518 return;
520 /* OK, now we have our initial speed set by the read only inquiry
521 * test, now try an echo buffer test (if the device allows it) */
523 if ((len = spi_dv_device_get_echo_buffer(sreq, buffer)) == 0) {
524 SPI_PRINTK(sdev, KERN_INFO, "Domain Validation skipping write tests\n");
525 return;
527 if (len > SPI_MAX_ECHO_BUFFER_SIZE) {
528 SPI_PRINTK(sdev, KERN_WARNING, "Echo buffer size %d is too big, trimming to %d\n", len, SPI_MAX_ECHO_BUFFER_SIZE);
529 len = SPI_MAX_ECHO_BUFFER_SIZE;
532 spi_dv_retrain(sreq, buffer, buffer + len,
533 spi_dv_device_echo_buffer);
537 /** spi_dv_device - Do Domain Validation on the device
538 * @sdev: scsi device to validate
540 * Performs the domain validation on the given device in the
541 * current execution thread. Since DV operations may sleep,
542 * the current thread must have user context. Also no SCSI
543 * related locks that would deadlock I/O issued by the DV may
544 * be held.
546 void
547 spi_dv_device(struct scsi_device *sdev)
549 struct scsi_request *sreq = scsi_allocate_request(sdev, GFP_KERNEL);
550 u8 *buffer;
551 const int len = SPI_MAX_ECHO_BUFFER_SIZE*2;
553 if (unlikely(!sreq))
554 return;
556 if (unlikely(scsi_device_get(sdev)))
557 goto out_free_req;
559 buffer = kmalloc(len, GFP_KERNEL);
561 if (unlikely(!buffer))
562 goto out_put;
564 memset(buffer, 0, len);
566 if (unlikely(scsi_device_quiesce(sdev)))
567 goto out_free;
569 spi_dv_pending(sdev) = 1;
570 down(&spi_dv_sem(sdev));
572 SPI_PRINTK(sdev, KERN_INFO, "Beginning Domain Validation\n");
574 spi_dv_device_internal(sreq, buffer);
576 SPI_PRINTK(sdev, KERN_INFO, "Ending Domain Validation\n");
578 up(&spi_dv_sem(sdev));
579 spi_dv_pending(sdev) = 0;
581 scsi_device_resume(sdev);
583 out_free:
584 kfree(buffer);
585 out_put:
586 scsi_device_put(sdev);
587 out_free_req:
588 scsi_release_request(sreq);
590 EXPORT_SYMBOL(spi_dv_device);
592 struct work_queue_wrapper {
593 struct work_struct work;
594 struct scsi_device *sdev;
597 static void
598 spi_dv_device_work_wrapper(void *data)
600 struct work_queue_wrapper *wqw = (struct work_queue_wrapper *)data;
601 struct scsi_device *sdev = wqw->sdev;
603 kfree(wqw);
604 spi_dv_device(sdev);
605 spi_dv_pending(sdev) = 0;
606 scsi_device_put(sdev);
611 * spi_schedule_dv_device - schedule domain validation to occur on the device
612 * @sdev: The device to validate
614 * Identical to spi_dv_device() above, except that the DV will be
615 * scheduled to occur in a workqueue later. All memory allocations
616 * are atomic, so may be called from any context including those holding
617 * SCSI locks.
619 void
620 spi_schedule_dv_device(struct scsi_device *sdev)
622 struct work_queue_wrapper *wqw =
623 kmalloc(sizeof(struct work_queue_wrapper), GFP_ATOMIC);
625 if (unlikely(!wqw))
626 return;
628 if (unlikely(spi_dv_pending(sdev))) {
629 kfree(wqw);
630 return;
632 /* Set pending early (dv_device doesn't check it, only sets it) */
633 spi_dv_pending(sdev) = 1;
634 if (unlikely(scsi_device_get(sdev))) {
635 kfree(wqw);
636 spi_dv_pending(sdev) = 0;
637 return;
640 INIT_WORK(&wqw->work, spi_dv_device_work_wrapper, wqw);
641 wqw->sdev = sdev;
643 schedule_work(&wqw->work);
645 EXPORT_SYMBOL(spi_schedule_dv_device);
647 #define SETUP_ATTRIBUTE(field) \
648 i->private_attrs[count] = class_device_attr_##field; \
649 if (!i->f->set_##field) { \
650 i->private_attrs[count].attr.mode = S_IRUGO; \
651 i->private_attrs[count].store = NULL; \
653 i->attrs[count] = &i->private_attrs[count]; \
654 if (i->f->show_##field) \
655 count++
657 struct scsi_transport_template *
658 spi_attach_transport(struct spi_function_template *ft)
660 struct spi_internal *i = kmalloc(sizeof(struct spi_internal),
661 GFP_KERNEL);
662 int count = 0;
663 if (unlikely(!i))
664 return NULL;
666 memset(i, 0, sizeof(struct spi_internal));
669 i->t.attrs = &i->attrs[0];
670 i->t.class = &spi_transport_class;
671 i->t.setup = &spi_setup_transport_attrs;
672 i->t.size = sizeof(struct spi_transport_attrs);
673 i->f = ft;
675 SETUP_ATTRIBUTE(period);
676 SETUP_ATTRIBUTE(offset);
677 SETUP_ATTRIBUTE(width);
678 SETUP_ATTRIBUTE(iu);
679 SETUP_ATTRIBUTE(dt);
680 SETUP_ATTRIBUTE(qas);
681 SETUP_ATTRIBUTE(wr_flow);
682 SETUP_ATTRIBUTE(rd_strm);
683 SETUP_ATTRIBUTE(rti);
684 SETUP_ATTRIBUTE(pcomp_en);
686 /* if you add an attribute but forget to increase SPI_NUM_ATTRS
687 * this bug will trigger */
688 BUG_ON(count > SPI_NUM_ATTRS);
690 i->attrs[count++] = &class_device_attr_revalidate;
692 i->attrs[count] = NULL;
694 return &i->t;
696 EXPORT_SYMBOL(spi_attach_transport);
698 void spi_release_transport(struct scsi_transport_template *t)
700 struct spi_internal *i = to_spi_internal(t);
702 kfree(i);
704 EXPORT_SYMBOL(spi_release_transport);
707 MODULE_AUTHOR("Martin Hicks");
708 MODULE_DESCRIPTION("SPI Transport Attributes");
709 MODULE_LICENSE("GPL");
711 module_init(spi_transport_init);
712 module_exit(spi_transport_exit);