2 * Parallel SCSI (SPI) transport specific attributes exported to sysfs.
4 * Copyright (c) 2003 Silicon Graphics, Inc. All rights reserved.
5 * Copyright (c) 2004, 2005 James Bottomley <James.Bottomley@SteelEye.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <linux/ctype.h>
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/workqueue.h>
25 #include <linux/blkdev.h>
26 #include <linux/mutex.h>
27 #include <scsi/scsi.h>
28 #include "scsi_priv.h"
29 #include <scsi/scsi_device.h>
30 #include <scsi/scsi_host.h>
31 #include <scsi/scsi_cmnd.h>
32 #include <scsi/scsi_eh.h>
33 #include <scsi/scsi_transport.h>
34 #include <scsi/scsi_transport_spi.h>
36 #define SPI_NUM_ATTRS 14 /* increase this if you add attributes */
37 #define SPI_OTHER_ATTRS 1 /* Increase this if you add "always
39 #define SPI_HOST_ATTRS 1
41 #define SPI_MAX_ECHO_BUFFER_SIZE 4096
44 #define DV_TIMEOUT (10*HZ)
45 #define DV_RETRIES 3 /* should only need at most
48 /* Private data accessors (keep these out of the header file) */
49 #define spi_dv_in_progress(x) (((struct spi_transport_attrs *)&(x)->starget_data)->dv_in_progress)
50 #define spi_dv_mutex(x) (((struct spi_transport_attrs *)&(x)->starget_data)->dv_mutex)
53 struct scsi_transport_template t
;
54 struct spi_function_template
*f
;
57 #define to_spi_internal(tmpl) container_of(tmpl, struct spi_internal, t)
59 static const int ppr_to_ps
[] = {
60 /* The PPR values 0-6 are reserved, fill them in when
61 * the committee defines them */
76 /* The PPR values at which you calculate the period in ns by multiplying
78 #define SPI_STATIC_PPR 0x0c
80 static int sprint_frac(char *dest
, int value
, int denom
)
82 int frac
= value
% denom
;
83 int result
= sprintf(dest
, "%d", value
/ denom
);
91 sprintf(dest
+ result
, "%d", frac
/ denom
);
96 dest
[result
++] = '\0';
100 static int spi_execute(struct scsi_device
*sdev
, const void *cmd
,
101 enum dma_data_direction dir
,
102 void *buffer
, unsigned bufflen
,
103 struct scsi_sense_hdr
*sshdr
)
106 unsigned char sense
[SCSI_SENSE_BUFFERSIZE
];
108 for(i
= 0; i
< DV_RETRIES
; i
++) {
109 result
= scsi_execute(sdev
, cmd
, dir
, buffer
, bufflen
,
110 sense
, DV_TIMEOUT
, /* retries */ 1,
112 if (result
& DRIVER_SENSE
) {
113 struct scsi_sense_hdr sshdr_tmp
;
117 if (scsi_normalize_sense(sense
, SCSI_SENSE_BUFFERSIZE
,
119 && sshdr
->sense_key
== UNIT_ATTENTION
)
128 enum spi_signal_type value
;
131 { SPI_SIGNAL_UNKNOWN
, "unknown" },
132 { SPI_SIGNAL_SE
, "SE" },
133 { SPI_SIGNAL_LVD
, "LVD" },
134 { SPI_SIGNAL_HVD
, "HVD" },
137 static inline const char *spi_signal_to_string(enum spi_signal_type type
)
141 for (i
= 0; i
< ARRAY_SIZE(signal_types
); i
++) {
142 if (type
== signal_types
[i
].value
)
143 return signal_types
[i
].name
;
147 static inline enum spi_signal_type
spi_signal_to_value(const char *name
)
151 for (i
= 0; i
< ARRAY_SIZE(signal_types
); i
++) {
152 len
= strlen(signal_types
[i
].name
);
153 if (strncmp(name
, signal_types
[i
].name
, len
) == 0 &&
154 (name
[len
] == '\n' || name
[len
] == '\0'))
155 return signal_types
[i
].value
;
157 return SPI_SIGNAL_UNKNOWN
;
160 static int spi_host_setup(struct transport_container
*tc
, struct device
*dev
,
161 struct class_device
*cdev
)
163 struct Scsi_Host
*shost
= dev_to_shost(dev
);
165 spi_signalling(shost
) = SPI_SIGNAL_UNKNOWN
;
170 static int spi_host_configure(struct transport_container
*tc
,
172 struct class_device
*cdev
);
174 static DECLARE_TRANSPORT_CLASS(spi_host_class
,
180 static int spi_host_match(struct attribute_container
*cont
,
183 struct Scsi_Host
*shost
;
185 if (!scsi_is_host_device(dev
))
188 shost
= dev_to_shost(dev
);
189 if (!shost
->transportt
|| shost
->transportt
->host_attrs
.ac
.class
190 != &spi_host_class
.class)
193 return &shost
->transportt
->host_attrs
.ac
== cont
;
196 static int spi_target_configure(struct transport_container
*tc
,
198 struct class_device
*cdev
);
200 static int spi_device_configure(struct transport_container
*tc
,
202 struct class_device
*cdev
)
204 struct scsi_device
*sdev
= to_scsi_device(dev
);
205 struct scsi_target
*starget
= sdev
->sdev_target
;
207 /* Populate the target capability fields with the values
208 * gleaned from the device inquiry */
210 spi_support_sync(starget
) = scsi_device_sync(sdev
);
211 spi_support_wide(starget
) = scsi_device_wide(sdev
);
212 spi_support_dt(starget
) = scsi_device_dt(sdev
);
213 spi_support_dt_only(starget
) = scsi_device_dt_only(sdev
);
214 spi_support_ius(starget
) = scsi_device_ius(sdev
);
215 spi_support_qas(starget
) = scsi_device_qas(sdev
);
220 static int spi_setup_transport_attrs(struct transport_container
*tc
,
222 struct class_device
*cdev
)
224 struct scsi_target
*starget
= to_scsi_target(dev
);
226 spi_period(starget
) = -1; /* illegal value */
227 spi_min_period(starget
) = 0;
228 spi_offset(starget
) = 0; /* async */
229 spi_max_offset(starget
) = 255;
230 spi_width(starget
) = 0; /* narrow */
231 spi_max_width(starget
) = 1;
232 spi_iu(starget
) = 0; /* no IU */
233 spi_dt(starget
) = 0; /* ST */
234 spi_qas(starget
) = 0;
235 spi_wr_flow(starget
) = 0;
236 spi_rd_strm(starget
) = 0;
237 spi_rti(starget
) = 0;
238 spi_pcomp_en(starget
) = 0;
239 spi_hold_mcs(starget
) = 0;
240 spi_dv_pending(starget
) = 0;
241 spi_dv_in_progress(starget
) = 0;
242 spi_initial_dv(starget
) = 0;
243 mutex_init(&spi_dv_mutex(starget
));
248 #define spi_transport_show_simple(field, format_string) \
251 show_spi_transport_##field(struct class_device *cdev, char *buf) \
253 struct scsi_target *starget = transport_class_to_starget(cdev); \
254 struct spi_transport_attrs *tp; \
256 tp = (struct spi_transport_attrs *)&starget->starget_data; \
257 return snprintf(buf, 20, format_string, tp->field); \
260 #define spi_transport_store_simple(field, format_string) \
263 store_spi_transport_##field(struct class_device *cdev, const char *buf, \
267 struct scsi_target *starget = transport_class_to_starget(cdev); \
268 struct spi_transport_attrs *tp; \
270 tp = (struct spi_transport_attrs *)&starget->starget_data; \
271 val = simple_strtoul(buf, NULL, 0); \
276 #define spi_transport_show_function(field, format_string) \
279 show_spi_transport_##field(struct class_device *cdev, char *buf) \
281 struct scsi_target *starget = transport_class_to_starget(cdev); \
282 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
283 struct spi_transport_attrs *tp; \
284 struct spi_internal *i = to_spi_internal(shost->transportt); \
285 tp = (struct spi_transport_attrs *)&starget->starget_data; \
286 if (i->f->get_##field) \
287 i->f->get_##field(starget); \
288 return snprintf(buf, 20, format_string, tp->field); \
291 #define spi_transport_store_function(field, format_string) \
293 store_spi_transport_##field(struct class_device *cdev, const char *buf, \
297 struct scsi_target *starget = transport_class_to_starget(cdev); \
298 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
299 struct spi_internal *i = to_spi_internal(shost->transportt); \
301 if (!i->f->set_##field) \
303 val = simple_strtoul(buf, NULL, 0); \
304 i->f->set_##field(starget, val); \
308 #define spi_transport_store_max(field, format_string) \
310 store_spi_transport_##field(struct class_device *cdev, const char *buf, \
314 struct scsi_target *starget = transport_class_to_starget(cdev); \
315 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
316 struct spi_internal *i = to_spi_internal(shost->transportt); \
317 struct spi_transport_attrs *tp \
318 = (struct spi_transport_attrs *)&starget->starget_data; \
320 if (i->f->set_##field) \
322 val = simple_strtoul(buf, NULL, 0); \
323 if (val > tp->max_##field) \
324 val = tp->max_##field; \
325 i->f->set_##field(starget, val); \
329 #define spi_transport_rd_attr(field, format_string) \
330 spi_transport_show_function(field, format_string) \
331 spi_transport_store_function(field, format_string) \
332 static CLASS_DEVICE_ATTR(field, S_IRUGO, \
333 show_spi_transport_##field, \
334 store_spi_transport_##field);
336 #define spi_transport_simple_attr(field, format_string) \
337 spi_transport_show_simple(field, format_string) \
338 spi_transport_store_simple(field, format_string) \
339 static CLASS_DEVICE_ATTR(field, S_IRUGO, \
340 show_spi_transport_##field, \
341 store_spi_transport_##field);
343 #define spi_transport_max_attr(field, format_string) \
344 spi_transport_show_function(field, format_string) \
345 spi_transport_store_max(field, format_string) \
346 spi_transport_simple_attr(max_##field, format_string) \
347 static CLASS_DEVICE_ATTR(field, S_IRUGO, \
348 show_spi_transport_##field, \
349 store_spi_transport_##field);
351 /* The Parallel SCSI Tranport Attributes: */
352 spi_transport_max_attr(offset
, "%d\n");
353 spi_transport_max_attr(width
, "%d\n");
354 spi_transport_rd_attr(iu
, "%d\n");
355 spi_transport_rd_attr(dt
, "%d\n");
356 spi_transport_rd_attr(qas
, "%d\n");
357 spi_transport_rd_attr(wr_flow
, "%d\n");
358 spi_transport_rd_attr(rd_strm
, "%d\n");
359 spi_transport_rd_attr(rti
, "%d\n");
360 spi_transport_rd_attr(pcomp_en
, "%d\n");
361 spi_transport_rd_attr(hold_mcs
, "%d\n");
363 /* we only care about the first child device so we return 1 */
364 static int child_iter(struct device
*dev
, void *data
)
366 struct scsi_device
*sdev
= to_scsi_device(dev
);
373 store_spi_revalidate(struct class_device
*cdev
, const char *buf
, size_t count
)
375 struct scsi_target
*starget
= transport_class_to_starget(cdev
);
377 device_for_each_child(&starget
->dev
, NULL
, child_iter
);
380 static CLASS_DEVICE_ATTR(revalidate
, S_IWUSR
, NULL
, store_spi_revalidate
);
382 /* Translate the period into ns according to the current spec
383 * for SDTR/PPR messages */
384 static int period_to_str(char *buf
, int period
)
388 if (period
< 0 || period
> 0xff) {
390 } else if (period
<= SPI_STATIC_PPR
) {
391 picosec
= ppr_to_ps
[period
];
393 picosec
= period
* 4000;
397 len
= sprintf(buf
, "reserved");
399 len
= sprint_frac(buf
, picosec
, 1000);
406 show_spi_transport_period_helper(char *buf
, int period
)
408 int len
= period_to_str(buf
, period
);
415 store_spi_transport_period_helper(struct class_device
*cdev
, const char *buf
,
416 size_t count
, int *periodp
)
418 int j
, picosec
, period
= -1;
421 picosec
= simple_strtoul(buf
, &endp
, 10) * 1000;
428 picosec
+= (*endp
- '0') * mult
;
433 for (j
= 0; j
<= SPI_STATIC_PPR
; j
++) {
434 if (ppr_to_ps
[j
] < picosec
)
441 period
= picosec
/ 4000;
452 show_spi_transport_period(struct class_device
*cdev
, char *buf
)
454 struct scsi_target
*starget
= transport_class_to_starget(cdev
);
455 struct Scsi_Host
*shost
= dev_to_shost(starget
->dev
.parent
);
456 struct spi_internal
*i
= to_spi_internal(shost
->transportt
);
457 struct spi_transport_attrs
*tp
=
458 (struct spi_transport_attrs
*)&starget
->starget_data
;
460 if (i
->f
->get_period
)
461 i
->f
->get_period(starget
);
463 return show_spi_transport_period_helper(buf
, tp
->period
);
467 store_spi_transport_period(struct class_device
*cdev
, const char *buf
,
470 struct scsi_target
*starget
= transport_class_to_starget(cdev
);
471 struct Scsi_Host
*shost
= dev_to_shost(starget
->dev
.parent
);
472 struct spi_internal
*i
= to_spi_internal(shost
->transportt
);
473 struct spi_transport_attrs
*tp
=
474 (struct spi_transport_attrs
*)&starget
->starget_data
;
477 if (!i
->f
->set_period
)
480 retval
= store_spi_transport_period_helper(cdev
, buf
, count
, &period
);
482 if (period
< tp
->min_period
)
483 period
= tp
->min_period
;
485 i
->f
->set_period(starget
, period
);
490 static CLASS_DEVICE_ATTR(period
, S_IRUGO
,
491 show_spi_transport_period
,
492 store_spi_transport_period
);
495 show_spi_transport_min_period(struct class_device
*cdev
, char *buf
)
497 struct scsi_target
*starget
= transport_class_to_starget(cdev
);
498 struct Scsi_Host
*shost
= dev_to_shost(starget
->dev
.parent
);
499 struct spi_internal
*i
= to_spi_internal(shost
->transportt
);
500 struct spi_transport_attrs
*tp
=
501 (struct spi_transport_attrs
*)&starget
->starget_data
;
503 if (!i
->f
->set_period
)
506 return show_spi_transport_period_helper(buf
, tp
->min_period
);
510 store_spi_transport_min_period(struct class_device
*cdev
, const char *buf
,
513 struct scsi_target
*starget
= transport_class_to_starget(cdev
);
514 struct spi_transport_attrs
*tp
=
515 (struct spi_transport_attrs
*)&starget
->starget_data
;
517 return store_spi_transport_period_helper(cdev
, buf
, count
,
522 static CLASS_DEVICE_ATTR(min_period
, S_IRUGO
,
523 show_spi_transport_min_period
,
524 store_spi_transport_min_period
);
527 static ssize_t
show_spi_host_signalling(struct class_device
*cdev
, char *buf
)
529 struct Scsi_Host
*shost
= transport_class_to_shost(cdev
);
530 struct spi_internal
*i
= to_spi_internal(shost
->transportt
);
532 if (i
->f
->get_signalling
)
533 i
->f
->get_signalling(shost
);
535 return sprintf(buf
, "%s\n", spi_signal_to_string(spi_signalling(shost
)));
537 static ssize_t
store_spi_host_signalling(struct class_device
*cdev
,
538 const char *buf
, size_t count
)
540 struct Scsi_Host
*shost
= transport_class_to_shost(cdev
);
541 struct spi_internal
*i
= to_spi_internal(shost
->transportt
);
542 enum spi_signal_type type
= spi_signal_to_value(buf
);
544 if (!i
->f
->set_signalling
)
547 if (type
!= SPI_SIGNAL_UNKNOWN
)
548 i
->f
->set_signalling(shost
, type
);
552 static CLASS_DEVICE_ATTR(signalling
, S_IRUGO
,
553 show_spi_host_signalling
,
554 store_spi_host_signalling
);
556 #define DV_SET(x, y) \
558 i->f->set_##x(sdev->sdev_target, y)
560 enum spi_compare_returns
{
563 SPI_COMPARE_SKIP_TEST
,
567 /* This is for read/write Domain Validation: If the device supports
568 * an echo buffer, we do read/write tests to it */
569 static enum spi_compare_returns
570 spi_dv_device_echo_buffer(struct scsi_device
*sdev
, u8
*buffer
,
571 u8
*ptr
, const int retries
)
573 int len
= ptr
- buffer
;
575 unsigned int pattern
= 0x0000ffff;
576 struct scsi_sense_hdr sshdr
;
578 const char spi_write_buffer
[] = {
579 WRITE_BUFFER
, 0x0a, 0, 0, 0, 0, 0, len
>> 8, len
& 0xff, 0
581 const char spi_read_buffer
[] = {
582 READ_BUFFER
, 0x0a, 0, 0, 0, 0, 0, len
>> 8, len
& 0xff, 0
585 /* set up the pattern buffer. Doesn't matter if we spill
586 * slightly beyond since that's where the read buffer is */
587 for (j
= 0; j
< len
; ) {
589 /* fill the buffer with counting (test a) */
590 for ( ; j
< min(len
, 32); j
++)
593 /* fill the buffer with alternating words of 0x0 and
595 for ( ; j
< min(len
, k
+ 32); j
+= 2) {
596 u16
*word
= (u16
*)&buffer
[j
];
598 *word
= (j
& 0x02) ? 0x0000 : 0xffff;
601 /* fill with crosstalk (alternating 0x5555 0xaaa)
603 for ( ; j
< min(len
, k
+ 32); j
+= 2) {
604 u16
*word
= (u16
*)&buffer
[j
];
606 *word
= (j
& 0x02) ? 0x5555 : 0xaaaa;
609 /* fill with shifting bits (test d) */
610 for ( ; j
< min(len
, k
+ 32); j
+= 4) {
611 u32
*word
= (unsigned int *)&buffer
[j
];
612 u32 roll
= (pattern
& 0x80000000) ? 1 : 0;
615 pattern
= (pattern
<< 1) | roll
;
617 /* don't bother with random data (test e) */
620 for (r
= 0; r
< retries
; r
++) {
621 result
= spi_execute(sdev
, spi_write_buffer
, DMA_TO_DEVICE
,
622 buffer
, len
, &sshdr
);
623 if(result
|| !scsi_device_online(sdev
)) {
625 scsi_device_set_state(sdev
, SDEV_QUIESCE
);
626 if (scsi_sense_valid(&sshdr
)
627 && sshdr
.sense_key
== ILLEGAL_REQUEST
628 /* INVALID FIELD IN CDB */
629 && sshdr
.asc
== 0x24 && sshdr
.ascq
== 0x00)
630 /* This would mean that the drive lied
631 * to us about supporting an echo
632 * buffer (unfortunately some Western
633 * Digital drives do precisely this)
635 return SPI_COMPARE_SKIP_TEST
;
638 sdev_printk(KERN_ERR
, sdev
, "Write Buffer failure %x\n", result
);
639 return SPI_COMPARE_FAILURE
;
643 spi_execute(sdev
, spi_read_buffer
, DMA_FROM_DEVICE
,
645 scsi_device_set_state(sdev
, SDEV_QUIESCE
);
647 if (memcmp(buffer
, ptr
, len
) != 0)
648 return SPI_COMPARE_FAILURE
;
650 return SPI_COMPARE_SUCCESS
;
653 /* This is for the simplest form of Domain Validation: a read test
654 * on the inquiry data from the device */
655 static enum spi_compare_returns
656 spi_dv_device_compare_inquiry(struct scsi_device
*sdev
, u8
*buffer
,
657 u8
*ptr
, const int retries
)
660 const int len
= sdev
->inquiry_len
;
661 const char spi_inquiry
[] = {
662 INQUIRY
, 0, 0, 0, len
, 0
665 for (r
= 0; r
< retries
; r
++) {
668 result
= spi_execute(sdev
, spi_inquiry
, DMA_FROM_DEVICE
,
671 if(result
|| !scsi_device_online(sdev
)) {
672 scsi_device_set_state(sdev
, SDEV_QUIESCE
);
673 return SPI_COMPARE_FAILURE
;
676 /* If we don't have the inquiry data already, the
677 * first read gets it */
684 if (memcmp(buffer
, ptr
, len
) != 0)
686 return SPI_COMPARE_FAILURE
;
688 return SPI_COMPARE_SUCCESS
;
691 static enum spi_compare_returns
692 spi_dv_retrain(struct scsi_device
*sdev
, u8
*buffer
, u8
*ptr
,
693 enum spi_compare_returns
694 (*compare_fn
)(struct scsi_device
*, u8
*, u8
*, int))
696 struct spi_internal
*i
= to_spi_internal(sdev
->host
->transportt
);
697 struct scsi_target
*starget
= sdev
->sdev_target
;
698 int period
= 0, prevperiod
= 0;
699 enum spi_compare_returns retval
;
704 retval
= compare_fn(sdev
, buffer
, ptr
, DV_LOOPS
);
706 if (retval
== SPI_COMPARE_SUCCESS
707 || retval
== SPI_COMPARE_SKIP_TEST
)
710 /* OK, retrain, fallback */
712 i
->f
->get_iu(starget
);
714 i
->f
->get_qas(starget
);
715 if (i
->f
->get_period
)
716 i
->f
->get_period(sdev
->sdev_target
);
718 /* Here's the fallback sequence; first try turning off
719 * IU, then QAS (if we can control them), then finally
720 * fall down the periods */
721 if (i
->f
->set_iu
&& spi_iu(starget
)) {
722 starget_printk(KERN_ERR
, starget
, "Domain Validation Disabing Information Units\n");
724 } else if (i
->f
->set_qas
&& spi_qas(starget
)) {
725 starget_printk(KERN_ERR
, starget
, "Domain Validation Disabing Quick Arbitration and Selection\n");
728 newperiod
= spi_period(starget
);
729 period
= newperiod
> period
? newperiod
: period
;
733 period
+= period
>> 1;
735 if (unlikely(period
> 0xff || period
== prevperiod
)) {
736 /* Total failure; set to async and return */
737 starget_printk(KERN_ERR
, starget
, "Domain Validation Failure, dropping back to Asynchronous\n");
739 return SPI_COMPARE_FAILURE
;
741 starget_printk(KERN_ERR
, starget
, "Domain Validation detected failure, dropping back\n");
742 DV_SET(period
, period
);
750 spi_dv_device_get_echo_buffer(struct scsi_device
*sdev
, u8
*buffer
)
754 /* first off do a test unit ready. This can error out
755 * because of reservations or some other reason. If it
756 * fails, the device won't let us write to the echo buffer
757 * so just return failure */
759 const char spi_test_unit_ready
[] = {
760 TEST_UNIT_READY
, 0, 0, 0, 0, 0
763 const char spi_read_buffer_descriptor
[] = {
764 READ_BUFFER
, 0x0b, 0, 0, 0, 0, 0, 0, 4, 0
768 /* We send a set of three TURs to clear any outstanding
769 * unit attention conditions if they exist (Otherwise the
770 * buffer tests won't be happy). If the TUR still fails
771 * (reservation conflict, device not ready, etc) just
772 * skip the write tests */
774 result
= spi_execute(sdev
, spi_test_unit_ready
, DMA_NONE
,
786 result
= spi_execute(sdev
, spi_read_buffer_descriptor
,
787 DMA_FROM_DEVICE
, buffer
, 4, NULL
);
790 /* Device has no echo buffer */
793 return buffer
[3] + ((buffer
[2] & 0x1f) << 8);
797 spi_dv_device_internal(struct scsi_device
*sdev
, u8
*buffer
)
799 struct spi_internal
*i
= to_spi_internal(sdev
->host
->transportt
);
800 struct scsi_target
*starget
= sdev
->sdev_target
;
801 struct Scsi_Host
*shost
= sdev
->host
;
802 int len
= sdev
->inquiry_len
;
803 int min_period
= spi_min_period(starget
);
804 int max_width
= spi_max_width(starget
);
805 /* first set us up for narrow async */
809 if (spi_dv_device_compare_inquiry(sdev
, buffer
, buffer
, DV_LOOPS
)
810 != SPI_COMPARE_SUCCESS
) {
811 starget_printk(KERN_ERR
, starget
, "Domain Validation Initial Inquiry Failed\n");
812 /* FIXME: should probably offline the device here? */
816 if (!scsi_device_wide(sdev
)) {
817 spi_max_width(starget
) = 0;
822 if (i
->f
->set_width
&& max_width
) {
823 i
->f
->set_width(starget
, 1);
825 if (spi_dv_device_compare_inquiry(sdev
, buffer
,
828 != SPI_COMPARE_SUCCESS
) {
829 starget_printk(KERN_ERR
, starget
, "Wide Transfers Fail\n");
830 i
->f
->set_width(starget
, 0);
831 /* Make sure we don't force wide back on by asking
832 * for a transfer period that requires it */
839 if (!i
->f
->set_period
)
842 /* device can't handle synchronous */
843 if (!scsi_device_sync(sdev
) && !scsi_device_dt(sdev
))
846 /* len == -1 is the signal that we need to ascertain the
847 * presence of an echo buffer before trying to use it. len ==
848 * 0 means we don't have an echo buffer */
853 /* now set up to the maximum */
854 DV_SET(offset
, spi_max_offset(starget
));
855 DV_SET(period
, min_period
);
857 /* try QAS requests; this should be harmless to set if the
858 * target supports it */
859 if (scsi_device_qas(sdev
)) {
865 if (scsi_device_ius(sdev
) && min_period
< 9) {
866 /* This u320 (or u640). Set IU transfers */
868 /* Then set the optional parameters */
878 /* now that we've done all this, actually check the bus
879 * signal type (if known). Some devices are stupid on
880 * a SE bus and still claim they can try LVD only settings */
881 if (i
->f
->get_signalling
)
882 i
->f
->get_signalling(shost
);
883 if (spi_signalling(shost
) == SPI_SIGNAL_SE
||
884 spi_signalling(shost
) == SPI_SIGNAL_HVD
||
885 !scsi_device_dt(sdev
)) {
890 /* set width last because it will pull all the other
891 * parameters down to required values */
892 DV_SET(width
, max_width
);
894 /* Do the read only INQUIRY tests */
895 spi_dv_retrain(sdev
, buffer
, buffer
+ sdev
->inquiry_len
,
896 spi_dv_device_compare_inquiry
);
897 /* See if we actually managed to negotiate and sustain DT */
899 i
->f
->get_dt(starget
);
901 /* see if the device has an echo buffer. If it does we can do
902 * the SPI pattern write tests. Because of some broken
903 * devices, we *only* try this on a device that has actually
906 if (len
== -1 && spi_dt(starget
))
907 len
= spi_dv_device_get_echo_buffer(sdev
, buffer
);
910 starget_printk(KERN_INFO
, starget
, "Domain Validation skipping write tests\n");
914 if (len
> SPI_MAX_ECHO_BUFFER_SIZE
) {
915 starget_printk(KERN_WARNING
, starget
, "Echo buffer size %d is too big, trimming to %d\n", len
, SPI_MAX_ECHO_BUFFER_SIZE
);
916 len
= SPI_MAX_ECHO_BUFFER_SIZE
;
919 if (spi_dv_retrain(sdev
, buffer
, buffer
+ len
,
920 spi_dv_device_echo_buffer
)
921 == SPI_COMPARE_SKIP_TEST
) {
922 /* OK, the stupid drive can't do a write echo buffer
923 * test after all, fall back to the read tests */
930 /** spi_dv_device - Do Domain Validation on the device
931 * @sdev: scsi device to validate
933 * Performs the domain validation on the given device in the
934 * current execution thread. Since DV operations may sleep,
935 * the current thread must have user context. Also no SCSI
936 * related locks that would deadlock I/O issued by the DV may
940 spi_dv_device(struct scsi_device
*sdev
)
942 struct scsi_target
*starget
= sdev
->sdev_target
;
944 const int len
= SPI_MAX_ECHO_BUFFER_SIZE
*2;
946 if (unlikely(scsi_device_get(sdev
)))
949 if (unlikely(spi_dv_in_progress(starget
)))
951 spi_dv_in_progress(starget
) = 1;
953 buffer
= kzalloc(len
, GFP_KERNEL
);
955 if (unlikely(!buffer
))
958 /* We need to verify that the actual device will quiesce; the
959 * later target quiesce is just a nice to have */
960 if (unlikely(scsi_device_quiesce(sdev
)))
963 scsi_target_quiesce(starget
);
965 spi_dv_pending(starget
) = 1;
966 mutex_lock(&spi_dv_mutex(starget
));
968 starget_printk(KERN_INFO
, starget
, "Beginning Domain Validation\n");
970 spi_dv_device_internal(sdev
, buffer
);
972 starget_printk(KERN_INFO
, starget
, "Ending Domain Validation\n");
974 mutex_unlock(&spi_dv_mutex(starget
));
975 spi_dv_pending(starget
) = 0;
977 scsi_target_resume(starget
);
979 spi_initial_dv(starget
) = 1;
984 spi_dv_in_progress(starget
) = 0;
985 scsi_device_put(sdev
);
987 EXPORT_SYMBOL(spi_dv_device
);
989 struct work_queue_wrapper
{
990 struct work_struct work
;
991 struct scsi_device
*sdev
;
995 spi_dv_device_work_wrapper(struct work_struct
*work
)
997 struct work_queue_wrapper
*wqw
=
998 container_of(work
, struct work_queue_wrapper
, work
);
999 struct scsi_device
*sdev
= wqw
->sdev
;
1002 spi_dv_device(sdev
);
1003 spi_dv_pending(sdev
->sdev_target
) = 0;
1004 scsi_device_put(sdev
);
1009 * spi_schedule_dv_device - schedule domain validation to occur on the device
1010 * @sdev: The device to validate
1012 * Identical to spi_dv_device() above, except that the DV will be
1013 * scheduled to occur in a workqueue later. All memory allocations
1014 * are atomic, so may be called from any context including those holding
1018 spi_schedule_dv_device(struct scsi_device
*sdev
)
1020 struct work_queue_wrapper
*wqw
=
1021 kmalloc(sizeof(struct work_queue_wrapper
), GFP_ATOMIC
);
1026 if (unlikely(spi_dv_pending(sdev
->sdev_target
))) {
1030 /* Set pending early (dv_device doesn't check it, only sets it) */
1031 spi_dv_pending(sdev
->sdev_target
) = 1;
1032 if (unlikely(scsi_device_get(sdev
))) {
1034 spi_dv_pending(sdev
->sdev_target
) = 0;
1038 INIT_WORK(&wqw
->work
, spi_dv_device_work_wrapper
);
1041 schedule_work(&wqw
->work
);
1043 EXPORT_SYMBOL(spi_schedule_dv_device
);
1046 * spi_display_xfer_agreement - Print the current target transfer agreement
1047 * @starget: The target for which to display the agreement
1049 * Each SPI port is required to maintain a transfer agreement for each
1050 * other port on the bus. This function prints a one-line summary of
1051 * the current agreement; more detailed information is available in sysfs.
1053 void spi_display_xfer_agreement(struct scsi_target
*starget
)
1055 struct spi_transport_attrs
*tp
;
1056 tp
= (struct spi_transport_attrs
*)&starget
->starget_data
;
1058 if (tp
->offset
> 0 && tp
->period
> 0) {
1059 unsigned int picosec
, kb100
;
1060 char *scsi
= "FAST-?";
1063 if (tp
->period
<= SPI_STATIC_PPR
) {
1064 picosec
= ppr_to_ps
[tp
->period
];
1065 switch (tp
->period
) {
1066 case 7: scsi
= "FAST-320"; break;
1067 case 8: scsi
= "FAST-160"; break;
1068 case 9: scsi
= "FAST-80"; break;
1070 case 11: scsi
= "FAST-40"; break;
1071 case 12: scsi
= "FAST-20"; break;
1074 picosec
= tp
->period
* 4000;
1075 if (tp
->period
< 25)
1077 else if (tp
->period
< 50)
1083 kb100
= (10000000 + picosec
/ 2) / picosec
;
1086 sprint_frac(tmp
, picosec
, 1000);
1088 dev_info(&starget
->dev
,
1089 "%s %sSCSI %d.%d MB/s %s%s%s%s%s%s%s%s (%s ns, offset %d)\n",
1090 scsi
, tp
->width
? "WIDE " : "", kb100
/10, kb100
% 10,
1091 tp
->dt
? "DT" : "ST",
1092 tp
->iu
? " IU" : "",
1093 tp
->qas
? " QAS" : "",
1094 tp
->rd_strm
? " RDSTRM" : "",
1095 tp
->rti
? " RTI" : "",
1096 tp
->wr_flow
? " WRFLOW" : "",
1097 tp
->pcomp_en
? " PCOMP" : "",
1098 tp
->hold_mcs
? " HMCS" : "",
1101 dev_info(&starget
->dev
, "%sasynchronous\n",
1102 tp
->width
? "wide " : "");
1105 EXPORT_SYMBOL(spi_display_xfer_agreement
);
1107 int spi_populate_width_msg(unsigned char *msg
, int width
)
1109 msg
[0] = EXTENDED_MESSAGE
;
1111 msg
[2] = EXTENDED_WDTR
;
1115 EXPORT_SYMBOL_GPL(spi_populate_width_msg
);
1117 int spi_populate_sync_msg(unsigned char *msg
, int period
, int offset
)
1119 msg
[0] = EXTENDED_MESSAGE
;
1121 msg
[2] = EXTENDED_SDTR
;
1126 EXPORT_SYMBOL_GPL(spi_populate_sync_msg
);
1128 int spi_populate_ppr_msg(unsigned char *msg
, int period
, int offset
,
1129 int width
, int options
)
1131 msg
[0] = EXTENDED_MESSAGE
;
1133 msg
[2] = EXTENDED_PPR
;
1141 EXPORT_SYMBOL_GPL(spi_populate_ppr_msg
);
1143 #ifdef CONFIG_SCSI_CONSTANTS
1144 static const char * const one_byte_msgs
[] = {
1145 /* 0x00 */ "Task Complete", NULL
/* Extended Message */, "Save Pointers",
1146 /* 0x03 */ "Restore Pointers", "Disconnect", "Initiator Error",
1147 /* 0x06 */ "Abort Task Set", "Message Reject", "Nop", "Message Parity Error",
1148 /* 0x0a */ "Linked Command Complete", "Linked Command Complete w/flag",
1149 /* 0x0c */ "Target Reset", "Abort Task", "Clear Task Set",
1150 /* 0x0f */ "Initiate Recovery", "Release Recovery",
1151 /* 0x11 */ "Terminate Process", "Continue Task", "Target Transfer Disable",
1152 /* 0x14 */ NULL
, NULL
, "Clear ACA", "LUN Reset"
1155 static const char * const two_byte_msgs
[] = {
1156 /* 0x20 */ "Simple Queue Tag", "Head of Queue Tag", "Ordered Queue Tag",
1157 /* 0x23 */ "Ignore Wide Residue", "ACA"
1160 static const char * const extended_msgs
[] = {
1161 /* 0x00 */ "Modify Data Pointer", "Synchronous Data Transfer Request",
1162 /* 0x02 */ "SCSI-I Extended Identify", "Wide Data Transfer Request",
1163 /* 0x04 */ "Parallel Protocol Request", "Modify Bidirectional Data Pointer"
1166 static void print_nego(const unsigned char *msg
, int per
, int off
, int width
)
1170 period_to_str(buf
, msg
[per
]);
1171 printk("period = %s ns ", buf
);
1175 printk("offset = %d ", msg
[off
]);
1177 printk("width = %d ", 8 << msg
[width
]);
1180 static void print_ptr(const unsigned char *msg
, int msb
, const char *desc
)
1182 int ptr
= (msg
[msb
] << 24) | (msg
[msb
+1] << 16) | (msg
[msb
+2] << 8) |
1184 printk("%s = %d ", desc
, ptr
);
1187 int spi_print_msg(const unsigned char *msg
)
1190 if (msg
[0] == EXTENDED_MESSAGE
) {
1194 if (msg
[2] < ARRAY_SIZE(extended_msgs
))
1195 printk ("%s ", extended_msgs
[msg
[2]]);
1197 printk ("Extended Message, reserved code (0x%02x) ",
1200 case EXTENDED_MODIFY_DATA_POINTER
:
1201 print_ptr(msg
, 3, "pointer");
1204 print_nego(msg
, 3, 4, 0);
1207 print_nego(msg
, 0, 0, 3);
1210 print_nego(msg
, 3, 5, 6);
1212 case EXTENDED_MODIFY_BIDI_DATA_PTR
:
1213 print_ptr(msg
, 3, "out");
1214 print_ptr(msg
, 7, "in");
1217 for (i
= 2; i
< len
; ++i
)
1218 printk("%02x ", msg
[i
]);
1221 } else if (msg
[0] & 0x80) {
1222 printk("Identify disconnect %sallowed %s %d ",
1223 (msg
[0] & 0x40) ? "" : "not ",
1224 (msg
[0] & 0x20) ? "target routine" : "lun",
1226 /* Normal One byte */
1227 } else if (msg
[0] < 0x1f) {
1228 if (msg
[0] < ARRAY_SIZE(one_byte_msgs
) && one_byte_msgs
[msg
[0]])
1229 printk("%s ", one_byte_msgs
[msg
[0]]);
1231 printk("reserved (%02x) ", msg
[0]);
1232 } else if (msg
[0] == 0x55) {
1233 printk("QAS Request ");
1235 } else if (msg
[0] <= 0x2f) {
1236 if ((msg
[0] - 0x20) < ARRAY_SIZE(two_byte_msgs
))
1237 printk("%s %02x ", two_byte_msgs
[msg
[0] - 0x20],
1240 printk("reserved two byte (%02x %02x) ",
1244 printk("reserved ");
1247 EXPORT_SYMBOL(spi_print_msg
);
1249 #else /* ifndef CONFIG_SCSI_CONSTANTS */
1251 int spi_print_msg(const unsigned char *msg
)
1255 if (msg
[0] == EXTENDED_MESSAGE
) {
1259 for (i
= 0; i
< len
; ++i
)
1260 printk("%02x ", msg
[i
]);
1262 } else if (msg
[0] & 0x80) {
1263 printk("%02x ", msg
[0]);
1264 /* Normal One byte */
1265 } else if ((msg
[0] < 0x1f) || (msg
[0] == 0x55)) {
1266 printk("%02x ", msg
[0]);
1268 } else if (msg
[0] <= 0x2f) {
1269 printk("%02x %02x", msg
[0], msg
[1]);
1272 printk("%02x ", msg
[0]);
1275 EXPORT_SYMBOL(spi_print_msg
);
1276 #endif /* ! CONFIG_SCSI_CONSTANTS */
1278 static int spi_device_match(struct attribute_container
*cont
,
1281 struct scsi_device
*sdev
;
1282 struct Scsi_Host
*shost
;
1283 struct spi_internal
*i
;
1285 if (!scsi_is_sdev_device(dev
))
1288 sdev
= to_scsi_device(dev
);
1290 if (!shost
->transportt
|| shost
->transportt
->host_attrs
.ac
.class
1291 != &spi_host_class
.class)
1293 /* Note: this class has no device attributes, so it has
1294 * no per-HBA allocation and thus we don't need to distinguish
1295 * the attribute containers for the device */
1296 i
= to_spi_internal(shost
->transportt
);
1297 if (i
->f
->deny_binding
&& i
->f
->deny_binding(sdev
->sdev_target
))
1302 static int spi_target_match(struct attribute_container
*cont
,
1305 struct Scsi_Host
*shost
;
1306 struct scsi_target
*starget
;
1307 struct spi_internal
*i
;
1309 if (!scsi_is_target_device(dev
))
1312 shost
= dev_to_shost(dev
->parent
);
1313 if (!shost
->transportt
|| shost
->transportt
->host_attrs
.ac
.class
1314 != &spi_host_class
.class)
1317 i
= to_spi_internal(shost
->transportt
);
1318 starget
= to_scsi_target(dev
);
1320 if (i
->f
->deny_binding
&& i
->f
->deny_binding(starget
))
1323 return &i
->t
.target_attrs
.ac
== cont
;
1326 static DECLARE_TRANSPORT_CLASS(spi_transport_class
,
1328 spi_setup_transport_attrs
,
1330 spi_target_configure
);
1332 static DECLARE_ANON_TRANSPORT_CLASS(spi_device_class
,
1334 spi_device_configure
);
1336 static struct attribute
*host_attributes
[] = {
1337 &class_device_attr_signalling
.attr
,
1341 static struct attribute_group host_attribute_group
= {
1342 .attrs
= host_attributes
,
1345 static int spi_host_configure(struct transport_container
*tc
,
1347 struct class_device
*cdev
)
1349 struct kobject
*kobj
= &cdev
->kobj
;
1350 struct Scsi_Host
*shost
= transport_class_to_shost(cdev
);
1351 struct spi_internal
*si
= to_spi_internal(shost
->transportt
);
1352 struct attribute
*attr
= &class_device_attr_signalling
.attr
;
1355 if (si
->f
->set_signalling
)
1356 rc
= sysfs_chmod_file(kobj
, attr
, attr
->mode
| S_IWUSR
);
1361 /* returns true if we should be showing the variable. Also
1362 * overloads the return by setting 1<<1 if the attribute should
1364 #define TARGET_ATTRIBUTE_HELPER(name) \
1365 (si->f->show_##name ? 1 : 0) + \
1366 (si->f->set_##name ? 2 : 0)
1368 static int target_attribute_is_visible(struct kobject
*kobj
,
1369 struct attribute
*attr
, int i
)
1371 struct class_device
*cdev
=
1372 container_of(kobj
, struct class_device
, kobj
);
1373 struct scsi_target
*starget
= transport_class_to_starget(cdev
);
1374 struct Scsi_Host
*shost
= transport_class_to_shost(cdev
);
1375 struct spi_internal
*si
= to_spi_internal(shost
->transportt
);
1377 if (attr
== &class_device_attr_period
.attr
&&
1378 spi_support_sync(starget
))
1379 return TARGET_ATTRIBUTE_HELPER(period
);
1380 else if (attr
== &class_device_attr_min_period
.attr
&&
1381 spi_support_sync(starget
))
1382 return TARGET_ATTRIBUTE_HELPER(period
);
1383 else if (attr
== &class_device_attr_offset
.attr
&&
1384 spi_support_sync(starget
))
1385 return TARGET_ATTRIBUTE_HELPER(offset
);
1386 else if (attr
== &class_device_attr_max_offset
.attr
&&
1387 spi_support_sync(starget
))
1388 return TARGET_ATTRIBUTE_HELPER(offset
);
1389 else if (attr
== &class_device_attr_width
.attr
&&
1390 spi_support_wide(starget
))
1391 return TARGET_ATTRIBUTE_HELPER(width
);
1392 else if (attr
== &class_device_attr_max_width
.attr
&&
1393 spi_support_wide(starget
))
1394 return TARGET_ATTRIBUTE_HELPER(width
);
1395 else if (attr
== &class_device_attr_iu
.attr
&&
1396 spi_support_ius(starget
))
1397 return TARGET_ATTRIBUTE_HELPER(iu
);
1398 else if (attr
== &class_device_attr_dt
.attr
&&
1399 spi_support_dt(starget
))
1400 return TARGET_ATTRIBUTE_HELPER(dt
);
1401 else if (attr
== &class_device_attr_qas
.attr
&&
1402 spi_support_qas(starget
))
1403 return TARGET_ATTRIBUTE_HELPER(qas
);
1404 else if (attr
== &class_device_attr_wr_flow
.attr
&&
1405 spi_support_ius(starget
))
1406 return TARGET_ATTRIBUTE_HELPER(wr_flow
);
1407 else if (attr
== &class_device_attr_rd_strm
.attr
&&
1408 spi_support_ius(starget
))
1409 return TARGET_ATTRIBUTE_HELPER(rd_strm
);
1410 else if (attr
== &class_device_attr_rti
.attr
&&
1411 spi_support_ius(starget
))
1412 return TARGET_ATTRIBUTE_HELPER(rti
);
1413 else if (attr
== &class_device_attr_pcomp_en
.attr
&&
1414 spi_support_ius(starget
))
1415 return TARGET_ATTRIBUTE_HELPER(pcomp_en
);
1416 else if (attr
== &class_device_attr_hold_mcs
.attr
&&
1417 spi_support_ius(starget
))
1418 return TARGET_ATTRIBUTE_HELPER(hold_mcs
);
1419 else if (attr
== &class_device_attr_revalidate
.attr
)
1425 static struct attribute
*target_attributes
[] = {
1426 &class_device_attr_period
.attr
,
1427 &class_device_attr_min_period
.attr
,
1428 &class_device_attr_offset
.attr
,
1429 &class_device_attr_max_offset
.attr
,
1430 &class_device_attr_width
.attr
,
1431 &class_device_attr_max_width
.attr
,
1432 &class_device_attr_iu
.attr
,
1433 &class_device_attr_dt
.attr
,
1434 &class_device_attr_qas
.attr
,
1435 &class_device_attr_wr_flow
.attr
,
1436 &class_device_attr_rd_strm
.attr
,
1437 &class_device_attr_rti
.attr
,
1438 &class_device_attr_pcomp_en
.attr
,
1439 &class_device_attr_hold_mcs
.attr
,
1440 &class_device_attr_revalidate
.attr
,
1444 static struct attribute_group target_attribute_group
= {
1445 .attrs
= target_attributes
,
1446 .is_visible
= target_attribute_is_visible
,
1449 static int spi_target_configure(struct transport_container
*tc
,
1451 struct class_device
*cdev
)
1453 struct kobject
*kobj
= &cdev
->kobj
;
1455 struct attribute
*attr
;
1458 for (i
= 0; (attr
= target_attributes
[i
]) != NULL
; i
++) {
1459 int j
= target_attribute_group
.is_visible(kobj
, attr
, i
);
1461 /* FIXME: as well as returning -EEXIST, which we'd like
1462 * to ignore, sysfs also does a WARN_ON and dumps a trace,
1463 * which is bad, so temporarily, skip attributes that are
1464 * already visible (the revalidate one) */
1465 if (j
&& attr
!= &class_device_attr_revalidate
.attr
)
1466 rc
= sysfs_add_file_to_group(kobj
, attr
,
1467 target_attribute_group
.name
);
1468 /* and make the attribute writeable if we have a set
1471 rc
= sysfs_chmod_file(kobj
, attr
, attr
->mode
| S_IWUSR
);
1477 struct scsi_transport_template
*
1478 spi_attach_transport(struct spi_function_template
*ft
)
1480 struct spi_internal
*i
= kzalloc(sizeof(struct spi_internal
),
1486 i
->t
.target_attrs
.ac
.class = &spi_transport_class
.class;
1487 i
->t
.target_attrs
.ac
.grp
= &target_attribute_group
;
1488 i
->t
.target_attrs
.ac
.match
= spi_target_match
;
1489 transport_container_register(&i
->t
.target_attrs
);
1490 i
->t
.target_size
= sizeof(struct spi_transport_attrs
);
1491 i
->t
.host_attrs
.ac
.class = &spi_host_class
.class;
1492 i
->t
.host_attrs
.ac
.grp
= &host_attribute_group
;
1493 i
->t
.host_attrs
.ac
.match
= spi_host_match
;
1494 transport_container_register(&i
->t
.host_attrs
);
1495 i
->t
.host_size
= sizeof(struct spi_host_attrs
);
1500 EXPORT_SYMBOL(spi_attach_transport
);
1502 void spi_release_transport(struct scsi_transport_template
*t
)
1504 struct spi_internal
*i
= to_spi_internal(t
);
1506 transport_container_unregister(&i
->t
.target_attrs
);
1507 transport_container_unregister(&i
->t
.host_attrs
);
1511 EXPORT_SYMBOL(spi_release_transport
);
1513 static __init
int spi_transport_init(void)
1515 int error
= transport_class_register(&spi_transport_class
);
1518 error
= anon_transport_class_register(&spi_device_class
);
1519 return transport_class_register(&spi_host_class
);
1522 static void __exit
spi_transport_exit(void)
1524 transport_class_unregister(&spi_transport_class
);
1525 anon_transport_class_unregister(&spi_device_class
);
1526 transport_class_unregister(&spi_host_class
);
1529 MODULE_AUTHOR("Martin Hicks");
1530 MODULE_DESCRIPTION("SPI Transport Attributes");
1531 MODULE_LICENSE("GPL");
1533 module_init(spi_transport_init
);
1534 module_exit(spi_transport_exit
);