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>
26 #include <linux/workqueue.h>
27 #include <asm/scatterlist.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
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)
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 */
79 /* The PPR values at which you calculate the period in ns by multiplying
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 */
106 spi_wr_flow(sdev) = 0;
107 spi_rd_strm(sdev) = 0;
109 spi_pcomp_en(sdev) = 0;
110 spi_dv_pending(sdev) = 0;
111 init_MUTEX(&spi_dv_sem(sdev));
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) \
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) \
138 store_spi_transport_##field(struct class_device *cdev, const char *buf, \
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); \
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");
169 store_spi_revalidate(struct class_device *cdev, const char *buf, size_t count)
171 struct scsi_device *sdev = transport_class_to_sdev(cdev);
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;
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);
195 case 0x07 ... SPI_STATIC_PPR:
196 str = ppr_to_ns[tp->period];
202 case (SPI_STATIC_PPR+1) ... 0xff:
203 return sprintf(buf, "%d\n", tp->period * 4);
208 return sprintf(buf, "%s\n", str);
212 store_spi_transport_period(struct class_device *cdev, const char *buf,
215 struct scsi_device *sdev = transport_class_to_sdev(cdev);
216 struct spi_internal *i = to_spi_internal(sdev->host->transportt);
219 for (j = 0; j < SPI_STATIC_PPR; j++) {
222 if(ppr_to_ns[j] == NULL)
225 len = strlen(ppr_to_ns[j]);
227 if(strncmp(ppr_to_ns[j], buf, len) != 0)
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 */
249 i->f->set_period(sdev, period);
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) \
260 i->f->set_##x(sdev, y)
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 */
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;
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++)
294 /* fill the buffer with alternating words of 0x0 and
296 for ( ; j < min(len, k + 32); j += 2) {
297 u16 *word = (u16 *)&buffer[j];
299 *word = (j & 0x02) ? 0x0000 : 0xffff;
302 /* fill with crosstalk (alternating 0x5555 0xaaa)
304 for ( ; j < min(len, k + 32); j += 2) {
305 u16 *word = (u16 *)&buffer[j];
307 *word = (j & 0x02) ? 0x5555 : 0xaaaa;
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;
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);
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)
345 /* This is for the simplest form of Domain Validation: a read test
346 * on the inquiry data from the device */
348 spi_dv_device_compare_inquiry(struct scsi_request *sreq, u8 *buffer,
349 u8 *ptr, const int retries)
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;
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);
372 /* If we don't have the inquiry data already, the
373 * first read gets it */
380 if (memcmp(buffer, ptr, len) != 0)
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, prevperiod = 0;
397 if (compare_fn(sreq, buffer, ptr, DV_LOOPS))
401 /* OK, retrain, fallback */
402 if (i->f->get_period)
403 i->f->get_period(sdev);
404 period = spi_period(sdev);
408 period += period >> 1;
410 if (unlikely(period > 0xff || period == prevperiod)) {
411 /* Total failure; set to async and return */
412 SPI_PRINTK(sdev, KERN_ERR, "Domain Validation Failure, dropping back to Asynchronous\n");
416 SPI_PRINTK(sdev, KERN_ERR, "Domain Validation detected failure, dropping back\n");
417 DV_SET(period, period);
424 spi_dv_device_get_echo_buffer(struct scsi_request *sreq, u8 *buffer)
428 /* first off do a test unit ready. This can error out
429 * because of reservations or some other reason. If it
430 * fails, the device won't let us write to the echo buffer
431 * so just return failure */
433 const char spi_test_unit_ready[] = {
434 TEST_UNIT_READY, 0, 0, 0, 0, 0
437 const char spi_read_buffer_descriptor[] = {
438 READ_BUFFER, 0x0b, 0, 0, 0, 0, 0, 0, 4, 0
442 sreq->sr_cmd_len = 0;
443 sreq->sr_data_direction = DMA_NONE;
445 /* We send a set of three TURs to clear any outstanding
446 * unit attention conditions if they exist (Otherwise the
447 * buffer tests won't be happy). If the TUR still fails
448 * (reservation conflict, device not ready, etc) just
449 * skip the write tests */
451 scsi_wait_req(sreq, spi_test_unit_ready, NULL, 0,
452 DV_TIMEOUT, DV_RETRIES);
454 if(sreq->sr_result) {
463 sreq->sr_cmd_len = 0;
464 sreq->sr_data_direction = DMA_FROM_DEVICE;
466 scsi_wait_req(sreq, spi_read_buffer_descriptor, buffer, 4,
467 DV_TIMEOUT, DV_RETRIES);
470 /* Device has no echo buffer */
473 return buffer[3] + ((buffer[2] & 0x1f) << 8);
477 spi_dv_device_internal(struct scsi_request *sreq, u8 *buffer)
479 struct spi_internal *i = to_spi_internal(sreq->sr_host->transportt);
480 struct scsi_device *sdev = sreq->sr_device;
481 int len = sdev->inquiry_len;
482 /* first set us up for narrow async */
486 if (!spi_dv_device_compare_inquiry(sreq, buffer, buffer, DV_LOOPS)) {
487 SPI_PRINTK(sdev, KERN_ERR, "Domain Validation Initial Inquiry Failed\n");
488 /* FIXME: should probably offline the device here? */
493 if (i->f->set_width && sdev->wdtr) {
494 i->f->set_width(sdev, 1);
496 if (!spi_dv_device_compare_inquiry(sreq, buffer,
499 SPI_PRINTK(sdev, KERN_ERR, "Wide Transfers Fail\n");
500 i->f->set_width(sdev, 0);
504 if (!i->f->set_period)
507 /* device can't handle synchronous */
508 if(!sdev->ppr && !sdev->sdtr)
511 /* now set up to the maximum */
514 if (!spi_dv_retrain(sreq, buffer, buffer + len,
515 spi_dv_device_compare_inquiry))
518 /* OK, now we have our initial speed set by the read only inquiry
519 * test, now try an echo buffer test (if the device allows it) */
521 if ((len = spi_dv_device_get_echo_buffer(sreq, buffer)) == 0) {
522 SPI_PRINTK(sdev, KERN_INFO, "Domain Validation skipping write tests\n");
525 if (len > SPI_MAX_ECHO_BUFFER_SIZE) {
526 SPI_PRINTK(sdev, KERN_WARNING, "Echo buffer size %d is too big, trimming to %d\n", len, SPI_MAX_ECHO_BUFFER_SIZE);
527 len = SPI_MAX_ECHO_BUFFER_SIZE;
530 spi_dv_retrain(sreq, buffer, buffer + len,
531 spi_dv_device_echo_buffer);
535 /** spi_dv_device - Do Domain Validation on the device
536 * @sdev: scsi device to validate
538 * Performs the domain validation on the given device in the
539 * current execution thread. Since DV operations may sleep,
540 * the current thread must have user context. Also no SCSI
541 * related locks that would deadlock I/O issued by the DV may
545 spi_dv_device(struct scsi_device *sdev)
547 struct scsi_request *sreq = scsi_allocate_request(sdev, GFP_KERNEL);
549 const int len = SPI_MAX_ECHO_BUFFER_SIZE*2;
554 if (unlikely(scsi_device_get(sdev)))
557 buffer = kmalloc(len, GFP_KERNEL);
559 if (unlikely(!buffer))
562 memset(buffer, 0, len);
564 if (unlikely(scsi_device_quiesce(sdev)))
567 spi_dv_pending(sdev) = 1;
568 down(&spi_dv_sem(sdev));
570 SPI_PRINTK(sdev, KERN_INFO, "Beginning Domain Validation\n");
572 spi_dv_device_internal(sreq, buffer);
574 SPI_PRINTK(sdev, KERN_INFO, "Ending Domain Validation\n");
576 up(&spi_dv_sem(sdev));
577 spi_dv_pending(sdev) = 0;
579 scsi_device_resume(sdev);
584 scsi_device_put(sdev);
586 scsi_release_request(sreq);
588 EXPORT_SYMBOL(spi_dv_device);
590 struct work_queue_wrapper {
591 struct work_struct work;
592 struct scsi_device *sdev;
596 spi_dv_device_work_wrapper(void *data)
598 struct work_queue_wrapper *wqw = (struct work_queue_wrapper *)data;
599 struct scsi_device *sdev = wqw->sdev;
603 spi_dv_pending(sdev) = 0;
604 scsi_device_put(sdev);
609 * spi_schedule_dv_device - schedule domain validation to occur on the device
610 * @sdev: The device to validate
612 * Identical to spi_dv_device() above, except that the DV will be
613 * scheduled to occur in a workqueue later. All memory allocations
614 * are atomic, so may be called from any context including those holding
618 spi_schedule_dv_device(struct scsi_device *sdev)
620 struct work_queue_wrapper *wqw =
621 kmalloc(sizeof(struct work_queue_wrapper), GFP_ATOMIC);
626 if (unlikely(spi_dv_pending(sdev))) {
630 /* Set pending early (dv_device doesn't check it, only sets it) */
631 spi_dv_pending(sdev) = 1;
632 if (unlikely(scsi_device_get(sdev))) {
634 spi_dv_pending(sdev) = 0;
638 INIT_WORK(&wqw->work, spi_dv_device_work_wrapper, wqw);
641 schedule_work(&wqw->work);
643 EXPORT_SYMBOL(spi_schedule_dv_device);
645 #define SETUP_ATTRIBUTE(field) \
646 i->private_attrs[count] = class_device_attr_##field; \
647 if (!i->f->set_##field) { \
648 i->private_attrs[count].attr.mode = S_IRUGO; \
649 i->private_attrs[count].store = NULL; \
651 i->attrs[count] = &i->private_attrs[count]; \
652 if (i->f->show_##field) \
655 struct scsi_transport_template *
656 spi_attach_transport(struct spi_function_template *ft)
658 struct spi_internal *i = kmalloc(sizeof(struct spi_internal),
664 memset(i, 0, sizeof(struct spi_internal));
667 i->t.attrs = &i->attrs[0];
668 i->t.class = &spi_transport_class;
669 i->t.setup = &spi_setup_transport_attrs;
670 i->t.size = sizeof(struct spi_transport_attrs);
673 SETUP_ATTRIBUTE(period);
674 SETUP_ATTRIBUTE(offset);
675 SETUP_ATTRIBUTE(width);
678 SETUP_ATTRIBUTE(qas);
679 SETUP_ATTRIBUTE(wr_flow);
680 SETUP_ATTRIBUTE(rd_strm);
681 SETUP_ATTRIBUTE(rti);
682 SETUP_ATTRIBUTE(pcomp_en);
684 /* if you add an attribute but forget to increase SPI_NUM_ATTRS
685 * this bug will trigger */
686 BUG_ON(count > SPI_NUM_ATTRS);
688 i->attrs[count++] = &class_device_attr_revalidate;
690 i->attrs[count] = NULL;
694 EXPORT_SYMBOL(spi_attach_transport);
696 void spi_release_transport(struct scsi_transport_template *t)
698 struct spi_internal *i = to_spi_internal(t);
702 EXPORT_SYMBOL(spi_release_transport);
705 MODULE_AUTHOR("Martin Hicks");
706 MODULE_DESCRIPTION("SPI Transport Attributes");
707 MODULE_LICENSE("GPL");
709 module_init(spi_transport_init);
710 module_exit(spi_transport_exit);