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
,
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 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 device
*cdev
);
200 static int spi_device_configure(struct transport_container
*tc
,
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
,
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 device *dev, \
252 struct device_attribute *attr, char *buf) \
254 struct scsi_target *starget = transport_class_to_starget(dev); \
255 struct spi_transport_attrs *tp; \
257 tp = (struct spi_transport_attrs *)&starget->starget_data; \
258 return snprintf(buf, 20, format_string, tp->field); \
261 #define spi_transport_store_simple(field, format_string) \
264 store_spi_transport_##field(struct device *dev, \
265 struct device_attribute *attr, \
266 const char *buf, size_t count) \
269 struct scsi_target *starget = transport_class_to_starget(dev); \
270 struct spi_transport_attrs *tp; \
272 tp = (struct spi_transport_attrs *)&starget->starget_data; \
273 val = simple_strtoul(buf, NULL, 0); \
278 #define spi_transport_show_function(field, format_string) \
281 show_spi_transport_##field(struct device *dev, \
282 struct device_attribute *attr, char *buf) \
284 struct scsi_target *starget = transport_class_to_starget(dev); \
285 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
286 struct spi_transport_attrs *tp; \
287 struct spi_internal *i = to_spi_internal(shost->transportt); \
288 tp = (struct spi_transport_attrs *)&starget->starget_data; \
289 if (i->f->get_##field) \
290 i->f->get_##field(starget); \
291 return snprintf(buf, 20, format_string, tp->field); \
294 #define spi_transport_store_function(field, format_string) \
296 store_spi_transport_##field(struct device *dev, \
297 struct device_attribute *attr, \
298 const char *buf, size_t count) \
301 struct scsi_target *starget = transport_class_to_starget(dev); \
302 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
303 struct spi_internal *i = to_spi_internal(shost->transportt); \
305 if (!i->f->set_##field) \
307 val = simple_strtoul(buf, NULL, 0); \
308 i->f->set_##field(starget, val); \
312 #define spi_transport_store_max(field, format_string) \
314 store_spi_transport_##field(struct device *dev, \
315 struct device_attribute *attr, \
316 const char *buf, size_t count) \
319 struct scsi_target *starget = transport_class_to_starget(dev); \
320 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
321 struct spi_internal *i = to_spi_internal(shost->transportt); \
322 struct spi_transport_attrs *tp \
323 = (struct spi_transport_attrs *)&starget->starget_data; \
325 if (i->f->set_##field) \
327 val = simple_strtoul(buf, NULL, 0); \
328 if (val > tp->max_##field) \
329 val = tp->max_##field; \
330 i->f->set_##field(starget, val); \
334 #define spi_transport_rd_attr(field, format_string) \
335 spi_transport_show_function(field, format_string) \
336 spi_transport_store_function(field, format_string) \
337 static DEVICE_ATTR(field, S_IRUGO, \
338 show_spi_transport_##field, \
339 store_spi_transport_##field);
341 #define spi_transport_simple_attr(field, format_string) \
342 spi_transport_show_simple(field, format_string) \
343 spi_transport_store_simple(field, format_string) \
344 static DEVICE_ATTR(field, S_IRUGO, \
345 show_spi_transport_##field, \
346 store_spi_transport_##field);
348 #define spi_transport_max_attr(field, format_string) \
349 spi_transport_show_function(field, format_string) \
350 spi_transport_store_max(field, format_string) \
351 spi_transport_simple_attr(max_##field, format_string) \
352 static DEVICE_ATTR(field, S_IRUGO, \
353 show_spi_transport_##field, \
354 store_spi_transport_##field);
356 /* The Parallel SCSI Tranport Attributes: */
357 spi_transport_max_attr(offset
, "%d\n");
358 spi_transport_max_attr(width
, "%d\n");
359 spi_transport_rd_attr(iu
, "%d\n");
360 spi_transport_rd_attr(dt
, "%d\n");
361 spi_transport_rd_attr(qas
, "%d\n");
362 spi_transport_rd_attr(wr_flow
, "%d\n");
363 spi_transport_rd_attr(rd_strm
, "%d\n");
364 spi_transport_rd_attr(rti
, "%d\n");
365 spi_transport_rd_attr(pcomp_en
, "%d\n");
366 spi_transport_rd_attr(hold_mcs
, "%d\n");
368 /* we only care about the first child device so we return 1 */
369 static int child_iter(struct device
*dev
, void *data
)
371 struct scsi_device
*sdev
= to_scsi_device(dev
);
378 store_spi_revalidate(struct device
*dev
, struct device_attribute
*attr
,
379 const char *buf
, size_t count
)
381 struct scsi_target
*starget
= transport_class_to_starget(dev
);
383 device_for_each_child(&starget
->dev
, NULL
, child_iter
);
386 static DEVICE_ATTR(revalidate
, S_IWUSR
, NULL
, store_spi_revalidate
);
388 /* Translate the period into ns according to the current spec
389 * for SDTR/PPR messages */
390 static int period_to_str(char *buf
, int period
)
394 if (period
< 0 || period
> 0xff) {
396 } else if (period
<= SPI_STATIC_PPR
) {
397 picosec
= ppr_to_ps
[period
];
399 picosec
= period
* 4000;
403 len
= sprintf(buf
, "reserved");
405 len
= sprint_frac(buf
, picosec
, 1000);
412 show_spi_transport_period_helper(char *buf
, int period
)
414 int len
= period_to_str(buf
, period
);
421 store_spi_transport_period_helper(struct device
*dev
, const char *buf
,
422 size_t count
, int *periodp
)
424 int j
, picosec
, period
= -1;
427 picosec
= simple_strtoul(buf
, &endp
, 10) * 1000;
434 picosec
+= (*endp
- '0') * mult
;
439 for (j
= 0; j
<= SPI_STATIC_PPR
; j
++) {
440 if (ppr_to_ps
[j
] < picosec
)
447 period
= picosec
/ 4000;
458 show_spi_transport_period(struct device
*dev
,
459 struct device_attribute
*attr
, char *buf
)
461 struct scsi_target
*starget
= transport_class_to_starget(dev
);
462 struct Scsi_Host
*shost
= dev_to_shost(starget
->dev
.parent
);
463 struct spi_internal
*i
= to_spi_internal(shost
->transportt
);
464 struct spi_transport_attrs
*tp
=
465 (struct spi_transport_attrs
*)&starget
->starget_data
;
467 if (i
->f
->get_period
)
468 i
->f
->get_period(starget
);
470 return show_spi_transport_period_helper(buf
, tp
->period
);
474 store_spi_transport_period(struct device
*cdev
, struct device_attribute
*attr
,
475 const char *buf
, size_t count
)
477 struct scsi_target
*starget
= transport_class_to_starget(cdev
);
478 struct Scsi_Host
*shost
= dev_to_shost(starget
->dev
.parent
);
479 struct spi_internal
*i
= to_spi_internal(shost
->transportt
);
480 struct spi_transport_attrs
*tp
=
481 (struct spi_transport_attrs
*)&starget
->starget_data
;
484 if (!i
->f
->set_period
)
487 retval
= store_spi_transport_period_helper(cdev
, buf
, count
, &period
);
489 if (period
< tp
->min_period
)
490 period
= tp
->min_period
;
492 i
->f
->set_period(starget
, period
);
497 static DEVICE_ATTR(period
, S_IRUGO
,
498 show_spi_transport_period
,
499 store_spi_transport_period
);
502 show_spi_transport_min_period(struct device
*cdev
,
503 struct device_attribute
*attr
, char *buf
)
505 struct scsi_target
*starget
= transport_class_to_starget(cdev
);
506 struct Scsi_Host
*shost
= dev_to_shost(starget
->dev
.parent
);
507 struct spi_internal
*i
= to_spi_internal(shost
->transportt
);
508 struct spi_transport_attrs
*tp
=
509 (struct spi_transport_attrs
*)&starget
->starget_data
;
511 if (!i
->f
->set_period
)
514 return show_spi_transport_period_helper(buf
, tp
->min_period
);
518 store_spi_transport_min_period(struct device
*cdev
,
519 struct device_attribute
*attr
,
520 const char *buf
, size_t count
)
522 struct scsi_target
*starget
= transport_class_to_starget(cdev
);
523 struct spi_transport_attrs
*tp
=
524 (struct spi_transport_attrs
*)&starget
->starget_data
;
526 return store_spi_transport_period_helper(cdev
, buf
, count
,
531 static DEVICE_ATTR(min_period
, S_IRUGO
,
532 show_spi_transport_min_period
,
533 store_spi_transport_min_period
);
536 static ssize_t
show_spi_host_signalling(struct device
*cdev
,
537 struct device_attribute
*attr
,
540 struct Scsi_Host
*shost
= transport_class_to_shost(cdev
);
541 struct spi_internal
*i
= to_spi_internal(shost
->transportt
);
543 if (i
->f
->get_signalling
)
544 i
->f
->get_signalling(shost
);
546 return sprintf(buf
, "%s\n", spi_signal_to_string(spi_signalling(shost
)));
548 static ssize_t
store_spi_host_signalling(struct device
*dev
,
549 struct device_attribute
*attr
,
550 const char *buf
, size_t count
)
552 struct Scsi_Host
*shost
= transport_class_to_shost(dev
);
553 struct spi_internal
*i
= to_spi_internal(shost
->transportt
);
554 enum spi_signal_type type
= spi_signal_to_value(buf
);
556 if (!i
->f
->set_signalling
)
559 if (type
!= SPI_SIGNAL_UNKNOWN
)
560 i
->f
->set_signalling(shost
, type
);
564 static DEVICE_ATTR(signalling
, S_IRUGO
,
565 show_spi_host_signalling
,
566 store_spi_host_signalling
);
568 #define DV_SET(x, y) \
570 i->f->set_##x(sdev->sdev_target, y)
572 enum spi_compare_returns
{
575 SPI_COMPARE_SKIP_TEST
,
579 /* This is for read/write Domain Validation: If the device supports
580 * an echo buffer, we do read/write tests to it */
581 static enum spi_compare_returns
582 spi_dv_device_echo_buffer(struct scsi_device
*sdev
, u8
*buffer
,
583 u8
*ptr
, const int retries
)
585 int len
= ptr
- buffer
;
587 unsigned int pattern
= 0x0000ffff;
588 struct scsi_sense_hdr sshdr
;
590 const char spi_write_buffer
[] = {
591 WRITE_BUFFER
, 0x0a, 0, 0, 0, 0, 0, len
>> 8, len
& 0xff, 0
593 const char spi_read_buffer
[] = {
594 READ_BUFFER
, 0x0a, 0, 0, 0, 0, 0, len
>> 8, len
& 0xff, 0
597 /* set up the pattern buffer. Doesn't matter if we spill
598 * slightly beyond since that's where the read buffer is */
599 for (j
= 0; j
< len
; ) {
601 /* fill the buffer with counting (test a) */
602 for ( ; j
< min(len
, 32); j
++)
605 /* fill the buffer with alternating words of 0x0 and
607 for ( ; j
< min(len
, k
+ 32); j
+= 2) {
608 u16
*word
= (u16
*)&buffer
[j
];
610 *word
= (j
& 0x02) ? 0x0000 : 0xffff;
613 /* fill with crosstalk (alternating 0x5555 0xaaa)
615 for ( ; j
< min(len
, k
+ 32); j
+= 2) {
616 u16
*word
= (u16
*)&buffer
[j
];
618 *word
= (j
& 0x02) ? 0x5555 : 0xaaaa;
621 /* fill with shifting bits (test d) */
622 for ( ; j
< min(len
, k
+ 32); j
+= 4) {
623 u32
*word
= (unsigned int *)&buffer
[j
];
624 u32 roll
= (pattern
& 0x80000000) ? 1 : 0;
627 pattern
= (pattern
<< 1) | roll
;
629 /* don't bother with random data (test e) */
632 for (r
= 0; r
< retries
; r
++) {
633 result
= spi_execute(sdev
, spi_write_buffer
, DMA_TO_DEVICE
,
634 buffer
, len
, &sshdr
);
635 if(result
|| !scsi_device_online(sdev
)) {
637 scsi_device_set_state(sdev
, SDEV_QUIESCE
);
638 if (scsi_sense_valid(&sshdr
)
639 && sshdr
.sense_key
== ILLEGAL_REQUEST
640 /* INVALID FIELD IN CDB */
641 && sshdr
.asc
== 0x24 && sshdr
.ascq
== 0x00)
642 /* This would mean that the drive lied
643 * to us about supporting an echo
644 * buffer (unfortunately some Western
645 * Digital drives do precisely this)
647 return SPI_COMPARE_SKIP_TEST
;
650 sdev_printk(KERN_ERR
, sdev
, "Write Buffer failure %x\n", result
);
651 return SPI_COMPARE_FAILURE
;
655 spi_execute(sdev
, spi_read_buffer
, DMA_FROM_DEVICE
,
657 scsi_device_set_state(sdev
, SDEV_QUIESCE
);
659 if (memcmp(buffer
, ptr
, len
) != 0)
660 return SPI_COMPARE_FAILURE
;
662 return SPI_COMPARE_SUCCESS
;
665 /* This is for the simplest form of Domain Validation: a read test
666 * on the inquiry data from the device */
667 static enum spi_compare_returns
668 spi_dv_device_compare_inquiry(struct scsi_device
*sdev
, u8
*buffer
,
669 u8
*ptr
, const int retries
)
672 const int len
= sdev
->inquiry_len
;
673 const char spi_inquiry
[] = {
674 INQUIRY
, 0, 0, 0, len
, 0
677 for (r
= 0; r
< retries
; r
++) {
680 result
= spi_execute(sdev
, spi_inquiry
, DMA_FROM_DEVICE
,
683 if(result
|| !scsi_device_online(sdev
)) {
684 scsi_device_set_state(sdev
, SDEV_QUIESCE
);
685 return SPI_COMPARE_FAILURE
;
688 /* If we don't have the inquiry data already, the
689 * first read gets it */
696 if (memcmp(buffer
, ptr
, len
) != 0)
698 return SPI_COMPARE_FAILURE
;
700 return SPI_COMPARE_SUCCESS
;
703 static enum spi_compare_returns
704 spi_dv_retrain(struct scsi_device
*sdev
, u8
*buffer
, u8
*ptr
,
705 enum spi_compare_returns
706 (*compare_fn
)(struct scsi_device
*, u8
*, u8
*, int))
708 struct spi_internal
*i
= to_spi_internal(sdev
->host
->transportt
);
709 struct scsi_target
*starget
= sdev
->sdev_target
;
710 int period
= 0, prevperiod
= 0;
711 enum spi_compare_returns retval
;
716 retval
= compare_fn(sdev
, buffer
, ptr
, DV_LOOPS
);
718 if (retval
== SPI_COMPARE_SUCCESS
719 || retval
== SPI_COMPARE_SKIP_TEST
)
722 /* OK, retrain, fallback */
724 i
->f
->get_iu(starget
);
726 i
->f
->get_qas(starget
);
727 if (i
->f
->get_period
)
728 i
->f
->get_period(sdev
->sdev_target
);
730 /* Here's the fallback sequence; first try turning off
731 * IU, then QAS (if we can control them), then finally
732 * fall down the periods */
733 if (i
->f
->set_iu
&& spi_iu(starget
)) {
734 starget_printk(KERN_ERR
, starget
, "Domain Validation Disabing Information Units\n");
736 } else if (i
->f
->set_qas
&& spi_qas(starget
)) {
737 starget_printk(KERN_ERR
, starget
, "Domain Validation Disabing Quick Arbitration and Selection\n");
740 newperiod
= spi_period(starget
);
741 period
= newperiod
> period
? newperiod
: period
;
745 period
+= period
>> 1;
747 if (unlikely(period
> 0xff || period
== prevperiod
)) {
748 /* Total failure; set to async and return */
749 starget_printk(KERN_ERR
, starget
, "Domain Validation Failure, dropping back to Asynchronous\n");
751 return SPI_COMPARE_FAILURE
;
753 starget_printk(KERN_ERR
, starget
, "Domain Validation detected failure, dropping back\n");
754 DV_SET(period
, period
);
762 spi_dv_device_get_echo_buffer(struct scsi_device
*sdev
, u8
*buffer
)
766 /* first off do a test unit ready. This can error out
767 * because of reservations or some other reason. If it
768 * fails, the device won't let us write to the echo buffer
769 * so just return failure */
771 const char spi_test_unit_ready
[] = {
772 TEST_UNIT_READY
, 0, 0, 0, 0, 0
775 const char spi_read_buffer_descriptor
[] = {
776 READ_BUFFER
, 0x0b, 0, 0, 0, 0, 0, 0, 4, 0
780 /* We send a set of three TURs to clear any outstanding
781 * unit attention conditions if they exist (Otherwise the
782 * buffer tests won't be happy). If the TUR still fails
783 * (reservation conflict, device not ready, etc) just
784 * skip the write tests */
786 result
= spi_execute(sdev
, spi_test_unit_ready
, DMA_NONE
,
798 result
= spi_execute(sdev
, spi_read_buffer_descriptor
,
799 DMA_FROM_DEVICE
, buffer
, 4, NULL
);
802 /* Device has no echo buffer */
805 return buffer
[3] + ((buffer
[2] & 0x1f) << 8);
809 spi_dv_device_internal(struct scsi_device
*sdev
, u8
*buffer
)
811 struct spi_internal
*i
= to_spi_internal(sdev
->host
->transportt
);
812 struct scsi_target
*starget
= sdev
->sdev_target
;
813 struct Scsi_Host
*shost
= sdev
->host
;
814 int len
= sdev
->inquiry_len
;
815 int min_period
= spi_min_period(starget
);
816 int max_width
= spi_max_width(starget
);
817 /* first set us up for narrow async */
821 if (spi_dv_device_compare_inquiry(sdev
, buffer
, buffer
, DV_LOOPS
)
822 != SPI_COMPARE_SUCCESS
) {
823 starget_printk(KERN_ERR
, starget
, "Domain Validation Initial Inquiry Failed\n");
824 /* FIXME: should probably offline the device here? */
828 if (!scsi_device_wide(sdev
)) {
829 spi_max_width(starget
) = 0;
834 if (i
->f
->set_width
&& max_width
) {
835 i
->f
->set_width(starget
, 1);
837 if (spi_dv_device_compare_inquiry(sdev
, buffer
,
840 != SPI_COMPARE_SUCCESS
) {
841 starget_printk(KERN_ERR
, starget
, "Wide Transfers Fail\n");
842 i
->f
->set_width(starget
, 0);
843 /* Make sure we don't force wide back on by asking
844 * for a transfer period that requires it */
851 if (!i
->f
->set_period
)
854 /* device can't handle synchronous */
855 if (!scsi_device_sync(sdev
) && !scsi_device_dt(sdev
))
858 /* len == -1 is the signal that we need to ascertain the
859 * presence of an echo buffer before trying to use it. len ==
860 * 0 means we don't have an echo buffer */
865 /* now set up to the maximum */
866 DV_SET(offset
, spi_max_offset(starget
));
867 DV_SET(period
, min_period
);
869 /* try QAS requests; this should be harmless to set if the
870 * target supports it */
871 if (scsi_device_qas(sdev
)) {
877 if (scsi_device_ius(sdev
) && min_period
< 9) {
878 /* This u320 (or u640). Set IU transfers */
880 /* Then set the optional parameters */
890 /* now that we've done all this, actually check the bus
891 * signal type (if known). Some devices are stupid on
892 * a SE bus and still claim they can try LVD only settings */
893 if (i
->f
->get_signalling
)
894 i
->f
->get_signalling(shost
);
895 if (spi_signalling(shost
) == SPI_SIGNAL_SE
||
896 spi_signalling(shost
) == SPI_SIGNAL_HVD
||
897 !scsi_device_dt(sdev
)) {
902 /* set width last because it will pull all the other
903 * parameters down to required values */
904 DV_SET(width
, max_width
);
906 /* Do the read only INQUIRY tests */
907 spi_dv_retrain(sdev
, buffer
, buffer
+ sdev
->inquiry_len
,
908 spi_dv_device_compare_inquiry
);
909 /* See if we actually managed to negotiate and sustain DT */
911 i
->f
->get_dt(starget
);
913 /* see if the device has an echo buffer. If it does we can do
914 * the SPI pattern write tests. Because of some broken
915 * devices, we *only* try this on a device that has actually
918 if (len
== -1 && spi_dt(starget
))
919 len
= spi_dv_device_get_echo_buffer(sdev
, buffer
);
922 starget_printk(KERN_INFO
, starget
, "Domain Validation skipping write tests\n");
926 if (len
> SPI_MAX_ECHO_BUFFER_SIZE
) {
927 starget_printk(KERN_WARNING
, starget
, "Echo buffer size %d is too big, trimming to %d\n", len
, SPI_MAX_ECHO_BUFFER_SIZE
);
928 len
= SPI_MAX_ECHO_BUFFER_SIZE
;
931 if (spi_dv_retrain(sdev
, buffer
, buffer
+ len
,
932 spi_dv_device_echo_buffer
)
933 == SPI_COMPARE_SKIP_TEST
) {
934 /* OK, the stupid drive can't do a write echo buffer
935 * test after all, fall back to the read tests */
942 /** spi_dv_device - Do Domain Validation on the device
943 * @sdev: scsi device to validate
945 * Performs the domain validation on the given device in the
946 * current execution thread. Since DV operations may sleep,
947 * the current thread must have user context. Also no SCSI
948 * related locks that would deadlock I/O issued by the DV may
952 spi_dv_device(struct scsi_device
*sdev
)
954 struct scsi_target
*starget
= sdev
->sdev_target
;
956 const int len
= SPI_MAX_ECHO_BUFFER_SIZE
*2;
958 if (unlikely(scsi_device_get(sdev
)))
961 if (unlikely(spi_dv_in_progress(starget
)))
963 spi_dv_in_progress(starget
) = 1;
965 buffer
= kzalloc(len
, GFP_KERNEL
);
967 if (unlikely(!buffer
))
970 /* We need to verify that the actual device will quiesce; the
971 * later target quiesce is just a nice to have */
972 if (unlikely(scsi_device_quiesce(sdev
)))
975 scsi_target_quiesce(starget
);
977 spi_dv_pending(starget
) = 1;
978 mutex_lock(&spi_dv_mutex(starget
));
980 starget_printk(KERN_INFO
, starget
, "Beginning Domain Validation\n");
982 spi_dv_device_internal(sdev
, buffer
);
984 starget_printk(KERN_INFO
, starget
, "Ending Domain Validation\n");
986 mutex_unlock(&spi_dv_mutex(starget
));
987 spi_dv_pending(starget
) = 0;
989 scsi_target_resume(starget
);
991 spi_initial_dv(starget
) = 1;
996 spi_dv_in_progress(starget
) = 0;
997 scsi_device_put(sdev
);
999 EXPORT_SYMBOL(spi_dv_device
);
1001 struct work_queue_wrapper
{
1002 struct work_struct work
;
1003 struct scsi_device
*sdev
;
1007 spi_dv_device_work_wrapper(struct work_struct
*work
)
1009 struct work_queue_wrapper
*wqw
=
1010 container_of(work
, struct work_queue_wrapper
, work
);
1011 struct scsi_device
*sdev
= wqw
->sdev
;
1014 spi_dv_device(sdev
);
1015 spi_dv_pending(sdev
->sdev_target
) = 0;
1016 scsi_device_put(sdev
);
1021 * spi_schedule_dv_device - schedule domain validation to occur on the device
1022 * @sdev: The device to validate
1024 * Identical to spi_dv_device() above, except that the DV will be
1025 * scheduled to occur in a workqueue later. All memory allocations
1026 * are atomic, so may be called from any context including those holding
1030 spi_schedule_dv_device(struct scsi_device
*sdev
)
1032 struct work_queue_wrapper
*wqw
=
1033 kmalloc(sizeof(struct work_queue_wrapper
), GFP_ATOMIC
);
1038 if (unlikely(spi_dv_pending(sdev
->sdev_target
))) {
1042 /* Set pending early (dv_device doesn't check it, only sets it) */
1043 spi_dv_pending(sdev
->sdev_target
) = 1;
1044 if (unlikely(scsi_device_get(sdev
))) {
1046 spi_dv_pending(sdev
->sdev_target
) = 0;
1050 INIT_WORK(&wqw
->work
, spi_dv_device_work_wrapper
);
1053 schedule_work(&wqw
->work
);
1055 EXPORT_SYMBOL(spi_schedule_dv_device
);
1058 * spi_display_xfer_agreement - Print the current target transfer agreement
1059 * @starget: The target for which to display the agreement
1061 * Each SPI port is required to maintain a transfer agreement for each
1062 * other port on the bus. This function prints a one-line summary of
1063 * the current agreement; more detailed information is available in sysfs.
1065 void spi_display_xfer_agreement(struct scsi_target
*starget
)
1067 struct spi_transport_attrs
*tp
;
1068 tp
= (struct spi_transport_attrs
*)&starget
->starget_data
;
1070 if (tp
->offset
> 0 && tp
->period
> 0) {
1071 unsigned int picosec
, kb100
;
1072 char *scsi
= "FAST-?";
1075 if (tp
->period
<= SPI_STATIC_PPR
) {
1076 picosec
= ppr_to_ps
[tp
->period
];
1077 switch (tp
->period
) {
1078 case 7: scsi
= "FAST-320"; break;
1079 case 8: scsi
= "FAST-160"; break;
1080 case 9: scsi
= "FAST-80"; break;
1082 case 11: scsi
= "FAST-40"; break;
1083 case 12: scsi
= "FAST-20"; break;
1086 picosec
= tp
->period
* 4000;
1087 if (tp
->period
< 25)
1089 else if (tp
->period
< 50)
1095 kb100
= (10000000 + picosec
/ 2) / picosec
;
1098 sprint_frac(tmp
, picosec
, 1000);
1100 dev_info(&starget
->dev
,
1101 "%s %sSCSI %d.%d MB/s %s%s%s%s%s%s%s%s (%s ns, offset %d)\n",
1102 scsi
, tp
->width
? "WIDE " : "", kb100
/10, kb100
% 10,
1103 tp
->dt
? "DT" : "ST",
1104 tp
->iu
? " IU" : "",
1105 tp
->qas
? " QAS" : "",
1106 tp
->rd_strm
? " RDSTRM" : "",
1107 tp
->rti
? " RTI" : "",
1108 tp
->wr_flow
? " WRFLOW" : "",
1109 tp
->pcomp_en
? " PCOMP" : "",
1110 tp
->hold_mcs
? " HMCS" : "",
1113 dev_info(&starget
->dev
, "%sasynchronous\n",
1114 tp
->width
? "wide " : "");
1117 EXPORT_SYMBOL(spi_display_xfer_agreement
);
1119 int spi_populate_width_msg(unsigned char *msg
, int width
)
1121 msg
[0] = EXTENDED_MESSAGE
;
1123 msg
[2] = EXTENDED_WDTR
;
1127 EXPORT_SYMBOL_GPL(spi_populate_width_msg
);
1129 int spi_populate_sync_msg(unsigned char *msg
, int period
, int offset
)
1131 msg
[0] = EXTENDED_MESSAGE
;
1133 msg
[2] = EXTENDED_SDTR
;
1138 EXPORT_SYMBOL_GPL(spi_populate_sync_msg
);
1140 int spi_populate_ppr_msg(unsigned char *msg
, int period
, int offset
,
1141 int width
, int options
)
1143 msg
[0] = EXTENDED_MESSAGE
;
1145 msg
[2] = EXTENDED_PPR
;
1153 EXPORT_SYMBOL_GPL(spi_populate_ppr_msg
);
1155 #ifdef CONFIG_SCSI_CONSTANTS
1156 static const char * const one_byte_msgs
[] = {
1157 /* 0x00 */ "Task Complete", NULL
/* Extended Message */, "Save Pointers",
1158 /* 0x03 */ "Restore Pointers", "Disconnect", "Initiator Error",
1159 /* 0x06 */ "Abort Task Set", "Message Reject", "Nop", "Message Parity Error",
1160 /* 0x0a */ "Linked Command Complete", "Linked Command Complete w/flag",
1161 /* 0x0c */ "Target Reset", "Abort Task", "Clear Task Set",
1162 /* 0x0f */ "Initiate Recovery", "Release Recovery",
1163 /* 0x11 */ "Terminate Process", "Continue Task", "Target Transfer Disable",
1164 /* 0x14 */ NULL
, NULL
, "Clear ACA", "LUN Reset"
1167 static const char * const two_byte_msgs
[] = {
1168 /* 0x20 */ "Simple Queue Tag", "Head of Queue Tag", "Ordered Queue Tag",
1169 /* 0x23 */ "Ignore Wide Residue", "ACA"
1172 static const char * const extended_msgs
[] = {
1173 /* 0x00 */ "Modify Data Pointer", "Synchronous Data Transfer Request",
1174 /* 0x02 */ "SCSI-I Extended Identify", "Wide Data Transfer Request",
1175 /* 0x04 */ "Parallel Protocol Request", "Modify Bidirectional Data Pointer"
1178 static void print_nego(const unsigned char *msg
, int per
, int off
, int width
)
1182 period_to_str(buf
, msg
[per
]);
1183 printk("period = %s ns ", buf
);
1187 printk("offset = %d ", msg
[off
]);
1189 printk("width = %d ", 8 << msg
[width
]);
1192 static void print_ptr(const unsigned char *msg
, int msb
, const char *desc
)
1194 int ptr
= (msg
[msb
] << 24) | (msg
[msb
+1] << 16) | (msg
[msb
+2] << 8) |
1196 printk("%s = %d ", desc
, ptr
);
1199 int spi_print_msg(const unsigned char *msg
)
1202 if (msg
[0] == EXTENDED_MESSAGE
) {
1206 if (msg
[2] < ARRAY_SIZE(extended_msgs
))
1207 printk ("%s ", extended_msgs
[msg
[2]]);
1209 printk ("Extended Message, reserved code (0x%02x) ",
1212 case EXTENDED_MODIFY_DATA_POINTER
:
1213 print_ptr(msg
, 3, "pointer");
1216 print_nego(msg
, 3, 4, 0);
1219 print_nego(msg
, 0, 0, 3);
1222 print_nego(msg
, 3, 5, 6);
1224 case EXTENDED_MODIFY_BIDI_DATA_PTR
:
1225 print_ptr(msg
, 3, "out");
1226 print_ptr(msg
, 7, "in");
1229 for (i
= 2; i
< len
; ++i
)
1230 printk("%02x ", msg
[i
]);
1233 } else if (msg
[0] & 0x80) {
1234 printk("Identify disconnect %sallowed %s %d ",
1235 (msg
[0] & 0x40) ? "" : "not ",
1236 (msg
[0] & 0x20) ? "target routine" : "lun",
1238 /* Normal One byte */
1239 } else if (msg
[0] < 0x1f) {
1240 if (msg
[0] < ARRAY_SIZE(one_byte_msgs
) && one_byte_msgs
[msg
[0]])
1241 printk("%s ", one_byte_msgs
[msg
[0]]);
1243 printk("reserved (%02x) ", msg
[0]);
1244 } else if (msg
[0] == 0x55) {
1245 printk("QAS Request ");
1247 } else if (msg
[0] <= 0x2f) {
1248 if ((msg
[0] - 0x20) < ARRAY_SIZE(two_byte_msgs
))
1249 printk("%s %02x ", two_byte_msgs
[msg
[0] - 0x20],
1252 printk("reserved two byte (%02x %02x) ",
1256 printk("reserved ");
1259 EXPORT_SYMBOL(spi_print_msg
);
1261 #else /* ifndef CONFIG_SCSI_CONSTANTS */
1263 int spi_print_msg(const unsigned char *msg
)
1267 if (msg
[0] == EXTENDED_MESSAGE
) {
1271 for (i
= 0; i
< len
; ++i
)
1272 printk("%02x ", msg
[i
]);
1274 } else if (msg
[0] & 0x80) {
1275 printk("%02x ", msg
[0]);
1276 /* Normal One byte */
1277 } else if ((msg
[0] < 0x1f) || (msg
[0] == 0x55)) {
1278 printk("%02x ", msg
[0]);
1280 } else if (msg
[0] <= 0x2f) {
1281 printk("%02x %02x", msg
[0], msg
[1]);
1284 printk("%02x ", msg
[0]);
1287 EXPORT_SYMBOL(spi_print_msg
);
1288 #endif /* ! CONFIG_SCSI_CONSTANTS */
1290 static int spi_device_match(struct attribute_container
*cont
,
1293 struct scsi_device
*sdev
;
1294 struct Scsi_Host
*shost
;
1295 struct spi_internal
*i
;
1297 if (!scsi_is_sdev_device(dev
))
1300 sdev
= to_scsi_device(dev
);
1302 if (!shost
->transportt
|| shost
->transportt
->host_attrs
.ac
.class
1303 != &spi_host_class
.class)
1305 /* Note: this class has no device attributes, so it has
1306 * no per-HBA allocation and thus we don't need to distinguish
1307 * the attribute containers for the device */
1308 i
= to_spi_internal(shost
->transportt
);
1309 if (i
->f
->deny_binding
&& i
->f
->deny_binding(sdev
->sdev_target
))
1314 static int spi_target_match(struct attribute_container
*cont
,
1317 struct Scsi_Host
*shost
;
1318 struct scsi_target
*starget
;
1319 struct spi_internal
*i
;
1321 if (!scsi_is_target_device(dev
))
1324 shost
= dev_to_shost(dev
->parent
);
1325 if (!shost
->transportt
|| shost
->transportt
->host_attrs
.ac
.class
1326 != &spi_host_class
.class)
1329 i
= to_spi_internal(shost
->transportt
);
1330 starget
= to_scsi_target(dev
);
1332 if (i
->f
->deny_binding
&& i
->f
->deny_binding(starget
))
1335 return &i
->t
.target_attrs
.ac
== cont
;
1338 static DECLARE_TRANSPORT_CLASS(spi_transport_class
,
1340 spi_setup_transport_attrs
,
1342 spi_target_configure
);
1344 static DECLARE_ANON_TRANSPORT_CLASS(spi_device_class
,
1346 spi_device_configure
);
1348 static struct attribute
*host_attributes
[] = {
1349 &dev_attr_signalling
.attr
,
1353 static struct attribute_group host_attribute_group
= {
1354 .attrs
= host_attributes
,
1357 static int spi_host_configure(struct transport_container
*tc
,
1359 struct device
*cdev
)
1361 struct kobject
*kobj
= &cdev
->kobj
;
1362 struct Scsi_Host
*shost
= transport_class_to_shost(cdev
);
1363 struct spi_internal
*si
= to_spi_internal(shost
->transportt
);
1364 struct attribute
*attr
= &dev_attr_signalling
.attr
;
1367 if (si
->f
->set_signalling
)
1368 rc
= sysfs_chmod_file(kobj
, attr
, attr
->mode
| S_IWUSR
);
1373 /* returns true if we should be showing the variable. Also
1374 * overloads the return by setting 1<<1 if the attribute should
1376 #define TARGET_ATTRIBUTE_HELPER(name) \
1377 (si->f->show_##name ? 1 : 0) + \
1378 (si->f->set_##name ? 2 : 0)
1380 static int target_attribute_is_visible(struct kobject
*kobj
,
1381 struct attribute
*attr
, int i
)
1383 struct device
*cdev
= container_of(kobj
, struct device
, kobj
);
1384 struct scsi_target
*starget
= transport_class_to_starget(cdev
);
1385 struct Scsi_Host
*shost
= transport_class_to_shost(cdev
);
1386 struct spi_internal
*si
= to_spi_internal(shost
->transportt
);
1388 if (attr
== &dev_attr_period
.attr
&&
1389 spi_support_sync(starget
))
1390 return TARGET_ATTRIBUTE_HELPER(period
);
1391 else if (attr
== &dev_attr_min_period
.attr
&&
1392 spi_support_sync(starget
))
1393 return TARGET_ATTRIBUTE_HELPER(period
);
1394 else if (attr
== &dev_attr_offset
.attr
&&
1395 spi_support_sync(starget
))
1396 return TARGET_ATTRIBUTE_HELPER(offset
);
1397 else if (attr
== &dev_attr_max_offset
.attr
&&
1398 spi_support_sync(starget
))
1399 return TARGET_ATTRIBUTE_HELPER(offset
);
1400 else if (attr
== &dev_attr_width
.attr
&&
1401 spi_support_wide(starget
))
1402 return TARGET_ATTRIBUTE_HELPER(width
);
1403 else if (attr
== &dev_attr_max_width
.attr
&&
1404 spi_support_wide(starget
))
1405 return TARGET_ATTRIBUTE_HELPER(width
);
1406 else if (attr
== &dev_attr_iu
.attr
&&
1407 spi_support_ius(starget
))
1408 return TARGET_ATTRIBUTE_HELPER(iu
);
1409 else if (attr
== &dev_attr_dt
.attr
&&
1410 spi_support_dt(starget
))
1411 return TARGET_ATTRIBUTE_HELPER(dt
);
1412 else if (attr
== &dev_attr_qas
.attr
&&
1413 spi_support_qas(starget
))
1414 return TARGET_ATTRIBUTE_HELPER(qas
);
1415 else if (attr
== &dev_attr_wr_flow
.attr
&&
1416 spi_support_ius(starget
))
1417 return TARGET_ATTRIBUTE_HELPER(wr_flow
);
1418 else if (attr
== &dev_attr_rd_strm
.attr
&&
1419 spi_support_ius(starget
))
1420 return TARGET_ATTRIBUTE_HELPER(rd_strm
);
1421 else if (attr
== &dev_attr_rti
.attr
&&
1422 spi_support_ius(starget
))
1423 return TARGET_ATTRIBUTE_HELPER(rti
);
1424 else if (attr
== &dev_attr_pcomp_en
.attr
&&
1425 spi_support_ius(starget
))
1426 return TARGET_ATTRIBUTE_HELPER(pcomp_en
);
1427 else if (attr
== &dev_attr_hold_mcs
.attr
&&
1428 spi_support_ius(starget
))
1429 return TARGET_ATTRIBUTE_HELPER(hold_mcs
);
1430 else if (attr
== &dev_attr_revalidate
.attr
)
1436 static struct attribute
*target_attributes
[] = {
1437 &dev_attr_period
.attr
,
1438 &dev_attr_min_period
.attr
,
1439 &dev_attr_offset
.attr
,
1440 &dev_attr_max_offset
.attr
,
1441 &dev_attr_width
.attr
,
1442 &dev_attr_max_width
.attr
,
1446 &dev_attr_wr_flow
.attr
,
1447 &dev_attr_rd_strm
.attr
,
1449 &dev_attr_pcomp_en
.attr
,
1450 &dev_attr_hold_mcs
.attr
,
1451 &dev_attr_revalidate
.attr
,
1455 static struct attribute_group target_attribute_group
= {
1456 .attrs
= target_attributes
,
1457 .is_visible
= target_attribute_is_visible
,
1460 static int spi_target_configure(struct transport_container
*tc
,
1462 struct device
*cdev
)
1464 struct kobject
*kobj
= &cdev
->kobj
;
1466 struct attribute
*attr
;
1469 for (i
= 0; (attr
= target_attributes
[i
]) != NULL
; i
++) {
1470 int j
= target_attribute_group
.is_visible(kobj
, attr
, i
);
1472 /* FIXME: as well as returning -EEXIST, which we'd like
1473 * to ignore, sysfs also does a WARN_ON and dumps a trace,
1474 * which is bad, so temporarily, skip attributes that are
1475 * already visible (the revalidate one) */
1476 if (j
&& attr
!= &dev_attr_revalidate
.attr
)
1477 rc
= sysfs_add_file_to_group(kobj
, attr
,
1478 target_attribute_group
.name
);
1479 /* and make the attribute writeable if we have a set
1482 rc
= sysfs_chmod_file(kobj
, attr
, attr
->mode
| S_IWUSR
);
1488 struct scsi_transport_template
*
1489 spi_attach_transport(struct spi_function_template
*ft
)
1491 struct spi_internal
*i
= kzalloc(sizeof(struct spi_internal
),
1497 i
->t
.target_attrs
.ac
.class = &spi_transport_class
.class;
1498 i
->t
.target_attrs
.ac
.grp
= &target_attribute_group
;
1499 i
->t
.target_attrs
.ac
.match
= spi_target_match
;
1500 transport_container_register(&i
->t
.target_attrs
);
1501 i
->t
.target_size
= sizeof(struct spi_transport_attrs
);
1502 i
->t
.host_attrs
.ac
.class = &spi_host_class
.class;
1503 i
->t
.host_attrs
.ac
.grp
= &host_attribute_group
;
1504 i
->t
.host_attrs
.ac
.match
= spi_host_match
;
1505 transport_container_register(&i
->t
.host_attrs
);
1506 i
->t
.host_size
= sizeof(struct spi_host_attrs
);
1511 EXPORT_SYMBOL(spi_attach_transport
);
1513 void spi_release_transport(struct scsi_transport_template
*t
)
1515 struct spi_internal
*i
= to_spi_internal(t
);
1517 transport_container_unregister(&i
->t
.target_attrs
);
1518 transport_container_unregister(&i
->t
.host_attrs
);
1522 EXPORT_SYMBOL(spi_release_transport
);
1524 static __init
int spi_transport_init(void)
1526 int error
= transport_class_register(&spi_transport_class
);
1529 error
= anon_transport_class_register(&spi_device_class
);
1530 return transport_class_register(&spi_host_class
);
1533 static void __exit
spi_transport_exit(void)
1535 transport_class_unregister(&spi_transport_class
);
1536 anon_transport_class_unregister(&spi_device_class
);
1537 transport_class_unregister(&spi_host_class
);
1540 MODULE_AUTHOR("Martin Hicks");
1541 MODULE_DESCRIPTION("SPI Transport Attributes");
1542 MODULE_LICENSE("GPL");
1544 module_init(spi_transport_init
);
1545 module_exit(spi_transport_exit
);