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 <linux/sysfs.h>
28 #include <linux/slab.h>
29 #include <scsi/scsi.h>
30 #include "scsi_priv.h"
31 #include <scsi/scsi_device.h>
32 #include <scsi/scsi_host.h>
33 #include <scsi/scsi_cmnd.h>
34 #include <scsi/scsi_eh.h>
35 #include <scsi/scsi_tcq.h>
36 #include <scsi/scsi_transport.h>
37 #include <scsi/scsi_transport_spi.h>
39 #define SPI_NUM_ATTRS 14 /* increase this if you add attributes */
40 #define SPI_OTHER_ATTRS 1 /* Increase this if you add "always
42 #define SPI_HOST_ATTRS 1
44 #define SPI_MAX_ECHO_BUFFER_SIZE 4096
47 #define DV_TIMEOUT (10*HZ)
48 #define DV_RETRIES 3 /* should only need at most
51 /* Our blacklist flags */
53 SPI_BLIST_NOIUS = (__force blist_flags_t)0x1,
56 /* blacklist table, modelled on scsi_devinfo.c */
61 } spi_static_device_list[] __initdata = {
62 {"HP", "Ultrium 3-SCSI", SPI_BLIST_NOIUS },
63 {"IBM", "ULTRIUM-TD3", SPI_BLIST_NOIUS },
67 /* Private data accessors (keep these out of the header file) */
68 #define spi_dv_in_progress(x) (((struct spi_transport_attrs *)&(x)->starget_data)->dv_in_progress)
69 #define spi_dv_mutex(x) (((struct spi_transport_attrs *)&(x)->starget_data)->dv_mutex)
72 struct scsi_transport_template t;
73 struct spi_function_template *f;
76 #define to_spi_internal(tmpl) container_of(tmpl, struct spi_internal, t)
78 static const int ppr_to_ps[] = {
79 /* The PPR values 0-6 are reserved, fill them in when
80 * the committee defines them */
95 /* The PPR values at which you calculate the period in ns by multiplying
97 #define SPI_STATIC_PPR 0x0c
99 static int sprint_frac(char *dest, int value, int denom)
101 int frac = value % denom;
102 int result = sprintf(dest, "%d", value / denom);
106 dest[result++] = '.';
110 sprintf(dest + result, "%d", frac / denom);
115 dest[result++] = '\0';
119 static int spi_execute(struct scsi_device *sdev, const void *cmd,
120 enum dma_data_direction dir,
121 void *buffer, unsigned bufflen,
122 struct scsi_sense_hdr *sshdr)
125 unsigned char sense[SCSI_SENSE_BUFFERSIZE];
126 struct scsi_sense_hdr sshdr_tmp;
131 for(i = 0; i < DV_RETRIES; i++) {
132 result = scsi_execute(sdev, cmd, dir, buffer, bufflen, sense,
133 sshdr, DV_TIMEOUT, /* retries */ 1,
135 REQ_FAILFAST_TRANSPORT |
138 if (!(driver_byte(result) & DRIVER_SENSE) ||
139 sshdr->sense_key != UNIT_ATTENTION)
146 enum spi_signal_type value;
149 { SPI_SIGNAL_UNKNOWN, "unknown" },
150 { SPI_SIGNAL_SE, "SE" },
151 { SPI_SIGNAL_LVD, "LVD" },
152 { SPI_SIGNAL_HVD, "HVD" },
155 static inline const char *spi_signal_to_string(enum spi_signal_type type)
159 for (i = 0; i < ARRAY_SIZE(signal_types); i++) {
160 if (type == signal_types[i].value)
161 return signal_types[i].name;
165 static inline enum spi_signal_type spi_signal_to_value(const char *name)
169 for (i = 0; i < ARRAY_SIZE(signal_types); i++) {
170 len = strlen(signal_types[i].name);
171 if (strncmp(name, signal_types[i].name, len) == 0 &&
172 (name[len] == '\n' || name[len] == '\0'))
173 return signal_types[i].value;
175 return SPI_SIGNAL_UNKNOWN;
178 static int spi_host_setup(struct transport_container *tc, struct device *dev,
181 struct Scsi_Host *shost = dev_to_shost(dev);
183 spi_signalling(shost) = SPI_SIGNAL_UNKNOWN;
188 static int spi_host_configure(struct transport_container *tc,
190 struct device *cdev);
192 static DECLARE_TRANSPORT_CLASS(spi_host_class,
198 static int spi_host_match(struct attribute_container *cont,
201 struct Scsi_Host *shost;
203 if (!scsi_is_host_device(dev))
206 shost = dev_to_shost(dev);
207 if (!shost->transportt || shost->transportt->host_attrs.ac.class
208 != &spi_host_class.class)
211 return &shost->transportt->host_attrs.ac == cont;
214 static int spi_target_configure(struct transport_container *tc,
216 struct device *cdev);
218 static int spi_device_configure(struct transport_container *tc,
222 struct scsi_device *sdev = to_scsi_device(dev);
223 struct scsi_target *starget = sdev->sdev_target;
224 blist_flags_t bflags;
226 bflags = scsi_get_device_flags_keyed(sdev, &sdev->inquiry[8],
230 /* Populate the target capability fields with the values
231 * gleaned from the device inquiry */
233 spi_support_sync(starget) = scsi_device_sync(sdev);
234 spi_support_wide(starget) = scsi_device_wide(sdev);
235 spi_support_dt(starget) = scsi_device_dt(sdev);
236 spi_support_dt_only(starget) = scsi_device_dt_only(sdev);
237 spi_support_ius(starget) = scsi_device_ius(sdev);
238 if (bflags & SPI_BLIST_NOIUS) {
239 dev_info(dev, "Information Units disabled by blacklist\n");
240 spi_support_ius(starget) = 0;
242 spi_support_qas(starget) = scsi_device_qas(sdev);
247 static int spi_setup_transport_attrs(struct transport_container *tc,
251 struct scsi_target *starget = to_scsi_target(dev);
253 spi_period(starget) = -1; /* illegal value */
254 spi_min_period(starget) = 0;
255 spi_offset(starget) = 0; /* async */
256 spi_max_offset(starget) = 255;
257 spi_width(starget) = 0; /* narrow */
258 spi_max_width(starget) = 1;
259 spi_iu(starget) = 0; /* no IU */
260 spi_max_iu(starget) = 1;
261 spi_dt(starget) = 0; /* ST */
262 spi_qas(starget) = 0;
263 spi_max_qas(starget) = 1;
264 spi_wr_flow(starget) = 0;
265 spi_rd_strm(starget) = 0;
266 spi_rti(starget) = 0;
267 spi_pcomp_en(starget) = 0;
268 spi_hold_mcs(starget) = 0;
269 spi_dv_pending(starget) = 0;
270 spi_dv_in_progress(starget) = 0;
271 spi_initial_dv(starget) = 0;
272 mutex_init(&spi_dv_mutex(starget));
277 #define spi_transport_show_simple(field, format_string) \
280 show_spi_transport_##field(struct device *dev, \
281 struct device_attribute *attr, char *buf) \
283 struct scsi_target *starget = transport_class_to_starget(dev); \
284 struct spi_transport_attrs *tp; \
286 tp = (struct spi_transport_attrs *)&starget->starget_data; \
287 return snprintf(buf, 20, format_string, tp->field); \
290 #define spi_transport_store_simple(field, format_string) \
293 store_spi_transport_##field(struct device *dev, \
294 struct device_attribute *attr, \
295 const char *buf, size_t count) \
298 struct scsi_target *starget = transport_class_to_starget(dev); \
299 struct spi_transport_attrs *tp; \
301 tp = (struct spi_transport_attrs *)&starget->starget_data; \
302 val = simple_strtoul(buf, NULL, 0); \
307 #define spi_transport_show_function(field, format_string) \
310 show_spi_transport_##field(struct device *dev, \
311 struct device_attribute *attr, char *buf) \
313 struct scsi_target *starget = transport_class_to_starget(dev); \
314 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
315 struct spi_transport_attrs *tp; \
316 struct spi_internal *i = to_spi_internal(shost->transportt); \
317 tp = (struct spi_transport_attrs *)&starget->starget_data; \
318 if (i->f->get_##field) \
319 i->f->get_##field(starget); \
320 return snprintf(buf, 20, format_string, tp->field); \
323 #define spi_transport_store_function(field, format_string) \
325 store_spi_transport_##field(struct device *dev, \
326 struct device_attribute *attr, \
327 const char *buf, size_t count) \
330 struct scsi_target *starget = transport_class_to_starget(dev); \
331 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
332 struct spi_internal *i = to_spi_internal(shost->transportt); \
334 if (!i->f->set_##field) \
336 val = simple_strtoul(buf, NULL, 0); \
337 i->f->set_##field(starget, val); \
341 #define spi_transport_store_max(field, format_string) \
343 store_spi_transport_##field(struct device *dev, \
344 struct device_attribute *attr, \
345 const char *buf, size_t count) \
348 struct scsi_target *starget = transport_class_to_starget(dev); \
349 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
350 struct spi_internal *i = to_spi_internal(shost->transportt); \
351 struct spi_transport_attrs *tp \
352 = (struct spi_transport_attrs *)&starget->starget_data; \
354 if (i->f->set_##field) \
356 val = simple_strtoul(buf, NULL, 0); \
357 if (val > tp->max_##field) \
358 val = tp->max_##field; \
359 i->f->set_##field(starget, val); \
363 #define spi_transport_rd_attr(field, format_string) \
364 spi_transport_show_function(field, format_string) \
365 spi_transport_store_function(field, format_string) \
366 static DEVICE_ATTR(field, S_IRUGO, \
367 show_spi_transport_##field, \
368 store_spi_transport_##field);
370 #define spi_transport_simple_attr(field, format_string) \
371 spi_transport_show_simple(field, format_string) \
372 spi_transport_store_simple(field, format_string) \
373 static DEVICE_ATTR(field, S_IRUGO, \
374 show_spi_transport_##field, \
375 store_spi_transport_##field);
377 #define spi_transport_max_attr(field, format_string) \
378 spi_transport_show_function(field, format_string) \
379 spi_transport_store_max(field, format_string) \
380 spi_transport_simple_attr(max_##field, format_string) \
381 static DEVICE_ATTR(field, S_IRUGO, \
382 show_spi_transport_##field, \
383 store_spi_transport_##field);
385 /* The Parallel SCSI Tranport Attributes: */
386 spi_transport_max_attr(offset, "%d\n");
387 spi_transport_max_attr(width, "%d\n");
388 spi_transport_max_attr(iu, "%d\n");
389 spi_transport_rd_attr(dt, "%d\n");
390 spi_transport_max_attr(qas, "%d\n");
391 spi_transport_rd_attr(wr_flow, "%d\n");
392 spi_transport_rd_attr(rd_strm, "%d\n");
393 spi_transport_rd_attr(rti, "%d\n");
394 spi_transport_rd_attr(pcomp_en, "%d\n");
395 spi_transport_rd_attr(hold_mcs, "%d\n");
397 /* we only care about the first child device that's a real SCSI device
398 * so we return 1 to terminate the iteration when we find it */
399 static int child_iter(struct device *dev, void *data)
401 if (!scsi_is_sdev_device(dev))
404 spi_dv_device(to_scsi_device(dev));
409 store_spi_revalidate(struct device *dev, struct device_attribute *attr,
410 const char *buf, size_t count)
412 struct scsi_target *starget = transport_class_to_starget(dev);
414 device_for_each_child(&starget->dev, NULL, child_iter);
417 static DEVICE_ATTR(revalidate, S_IWUSR, NULL, store_spi_revalidate);
419 /* Translate the period into ns according to the current spec
420 * for SDTR/PPR messages */
421 static int period_to_str(char *buf, int period)
425 if (period < 0 || period > 0xff) {
427 } else if (period <= SPI_STATIC_PPR) {
428 picosec = ppr_to_ps[period];
430 picosec = period * 4000;
434 len = sprintf(buf, "reserved");
436 len = sprint_frac(buf, picosec, 1000);
443 show_spi_transport_period_helper(char *buf, int period)
445 int len = period_to_str(buf, period);
452 store_spi_transport_period_helper(struct device *dev, const char *buf,
453 size_t count, int *periodp)
455 int j, picosec, period = -1;
458 picosec = simple_strtoul(buf, &endp, 10) * 1000;
465 picosec += (*endp - '0') * mult;
470 for (j = 0; j <= SPI_STATIC_PPR; j++) {
471 if (ppr_to_ps[j] < picosec)
478 period = picosec / 4000;
489 show_spi_transport_period(struct device *dev,
490 struct device_attribute *attr, char *buf)
492 struct scsi_target *starget = transport_class_to_starget(dev);
493 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
494 struct spi_internal *i = to_spi_internal(shost->transportt);
495 struct spi_transport_attrs *tp =
496 (struct spi_transport_attrs *)&starget->starget_data;
498 if (i->f->get_period)
499 i->f->get_period(starget);
501 return show_spi_transport_period_helper(buf, tp->period);
505 store_spi_transport_period(struct device *cdev, struct device_attribute *attr,
506 const char *buf, size_t count)
508 struct scsi_target *starget = transport_class_to_starget(cdev);
509 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
510 struct spi_internal *i = to_spi_internal(shost->transportt);
511 struct spi_transport_attrs *tp =
512 (struct spi_transport_attrs *)&starget->starget_data;
515 if (!i->f->set_period)
518 retval = store_spi_transport_period_helper(cdev, buf, count, &period);
520 if (period < tp->min_period)
521 period = tp->min_period;
523 i->f->set_period(starget, period);
528 static DEVICE_ATTR(period, S_IRUGO,
529 show_spi_transport_period,
530 store_spi_transport_period);
533 show_spi_transport_min_period(struct device *cdev,
534 struct device_attribute *attr, char *buf)
536 struct scsi_target *starget = transport_class_to_starget(cdev);
537 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
538 struct spi_internal *i = to_spi_internal(shost->transportt);
539 struct spi_transport_attrs *tp =
540 (struct spi_transport_attrs *)&starget->starget_data;
542 if (!i->f->set_period)
545 return show_spi_transport_period_helper(buf, tp->min_period);
549 store_spi_transport_min_period(struct device *cdev,
550 struct device_attribute *attr,
551 const char *buf, size_t count)
553 struct scsi_target *starget = transport_class_to_starget(cdev);
554 struct spi_transport_attrs *tp =
555 (struct spi_transport_attrs *)&starget->starget_data;
557 return store_spi_transport_period_helper(cdev, buf, count,
562 static DEVICE_ATTR(min_period, S_IRUGO,
563 show_spi_transport_min_period,
564 store_spi_transport_min_period);
567 static ssize_t show_spi_host_signalling(struct device *cdev,
568 struct device_attribute *attr,
571 struct Scsi_Host *shost = transport_class_to_shost(cdev);
572 struct spi_internal *i = to_spi_internal(shost->transportt);
574 if (i->f->get_signalling)
575 i->f->get_signalling(shost);
577 return sprintf(buf, "%s\n", spi_signal_to_string(spi_signalling(shost)));
579 static ssize_t store_spi_host_signalling(struct device *dev,
580 struct device_attribute *attr,
581 const char *buf, size_t count)
583 struct Scsi_Host *shost = transport_class_to_shost(dev);
584 struct spi_internal *i = to_spi_internal(shost->transportt);
585 enum spi_signal_type type = spi_signal_to_value(buf);
587 if (!i->f->set_signalling)
590 if (type != SPI_SIGNAL_UNKNOWN)
591 i->f->set_signalling(shost, type);
595 static DEVICE_ATTR(signalling, S_IRUGO,
596 show_spi_host_signalling,
597 store_spi_host_signalling);
599 static ssize_t show_spi_host_width(struct device *cdev,
600 struct device_attribute *attr,
603 struct Scsi_Host *shost = transport_class_to_shost(cdev);
605 return sprintf(buf, "%s\n", shost->max_id == 16 ? "wide" : "narrow");
607 static DEVICE_ATTR(host_width, S_IRUGO,
608 show_spi_host_width, NULL);
610 static ssize_t show_spi_host_hba_id(struct device *cdev,
611 struct device_attribute *attr,
614 struct Scsi_Host *shost = transport_class_to_shost(cdev);
616 return sprintf(buf, "%d\n", shost->this_id);
618 static DEVICE_ATTR(hba_id, S_IRUGO,
619 show_spi_host_hba_id, NULL);
621 #define DV_SET(x, y) \
623 i->f->set_##x(sdev->sdev_target, y)
625 enum spi_compare_returns {
628 SPI_COMPARE_SKIP_TEST,
632 /* This is for read/write Domain Validation: If the device supports
633 * an echo buffer, we do read/write tests to it */
634 static enum spi_compare_returns
635 spi_dv_device_echo_buffer(struct scsi_device *sdev, u8 *buffer,
636 u8 *ptr, const int retries)
638 int len = ptr - buffer;
640 unsigned int pattern = 0x0000ffff;
641 struct scsi_sense_hdr sshdr;
643 const char spi_write_buffer[] = {
644 WRITE_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
646 const char spi_read_buffer[] = {
647 READ_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
650 /* set up the pattern buffer. Doesn't matter if we spill
651 * slightly beyond since that's where the read buffer is */
652 for (j = 0; j < len; ) {
654 /* fill the buffer with counting (test a) */
655 for ( ; j < min(len, 32); j++)
658 /* fill the buffer with alternating words of 0x0 and
660 for ( ; j < min(len, k + 32); j += 2) {
661 u16 *word = (u16 *)&buffer[j];
663 *word = (j & 0x02) ? 0x0000 : 0xffff;
666 /* fill with crosstalk (alternating 0x5555 0xaaa)
668 for ( ; j < min(len, k + 32); j += 2) {
669 u16 *word = (u16 *)&buffer[j];
671 *word = (j & 0x02) ? 0x5555 : 0xaaaa;
674 /* fill with shifting bits (test d) */
675 for ( ; j < min(len, k + 32); j += 4) {
676 u32 *word = (unsigned int *)&buffer[j];
677 u32 roll = (pattern & 0x80000000) ? 1 : 0;
680 pattern = (pattern << 1) | roll;
682 /* don't bother with random data (test e) */
685 for (r = 0; r < retries; r++) {
686 result = spi_execute(sdev, spi_write_buffer, DMA_TO_DEVICE,
687 buffer, len, &sshdr);
688 if(result || !scsi_device_online(sdev)) {
690 scsi_device_set_state(sdev, SDEV_QUIESCE);
691 if (scsi_sense_valid(&sshdr)
692 && sshdr.sense_key == ILLEGAL_REQUEST
693 /* INVALID FIELD IN CDB */
694 && sshdr.asc == 0x24 && sshdr.ascq == 0x00)
695 /* This would mean that the drive lied
696 * to us about supporting an echo
697 * buffer (unfortunately some Western
698 * Digital drives do precisely this)
700 return SPI_COMPARE_SKIP_TEST;
703 sdev_printk(KERN_ERR, sdev, "Write Buffer failure %x\n", result);
704 return SPI_COMPARE_FAILURE;
708 spi_execute(sdev, spi_read_buffer, DMA_FROM_DEVICE,
710 scsi_device_set_state(sdev, SDEV_QUIESCE);
712 if (memcmp(buffer, ptr, len) != 0)
713 return SPI_COMPARE_FAILURE;
715 return SPI_COMPARE_SUCCESS;
718 /* This is for the simplest form of Domain Validation: a read test
719 * on the inquiry data from the device */
720 static enum spi_compare_returns
721 spi_dv_device_compare_inquiry(struct scsi_device *sdev, u8 *buffer,
722 u8 *ptr, const int retries)
725 const int len = sdev->inquiry_len;
726 const char spi_inquiry[] = {
727 INQUIRY, 0, 0, 0, len, 0
730 for (r = 0; r < retries; r++) {
733 result = spi_execute(sdev, spi_inquiry, DMA_FROM_DEVICE,
736 if(result || !scsi_device_online(sdev)) {
737 scsi_device_set_state(sdev, SDEV_QUIESCE);
738 return SPI_COMPARE_FAILURE;
741 /* If we don't have the inquiry data already, the
742 * first read gets it */
749 if (memcmp(buffer, ptr, len) != 0)
751 return SPI_COMPARE_FAILURE;
753 return SPI_COMPARE_SUCCESS;
756 static enum spi_compare_returns
757 spi_dv_retrain(struct scsi_device *sdev, u8 *buffer, u8 *ptr,
758 enum spi_compare_returns
759 (*compare_fn)(struct scsi_device *, u8 *, u8 *, int))
761 struct spi_internal *i = to_spi_internal(sdev->host->transportt);
762 struct scsi_target *starget = sdev->sdev_target;
763 int period = 0, prevperiod = 0;
764 enum spi_compare_returns retval;
769 retval = compare_fn(sdev, buffer, ptr, DV_LOOPS);
771 if (retval == SPI_COMPARE_SUCCESS
772 || retval == SPI_COMPARE_SKIP_TEST)
775 /* OK, retrain, fallback */
777 i->f->get_iu(starget);
779 i->f->get_qas(starget);
780 if (i->f->get_period)
781 i->f->get_period(sdev->sdev_target);
783 /* Here's the fallback sequence; first try turning off
784 * IU, then QAS (if we can control them), then finally
785 * fall down the periods */
786 if (i->f->set_iu && spi_iu(starget)) {
787 starget_printk(KERN_ERR, starget, "Domain Validation Disabling Information Units\n");
789 } else if (i->f->set_qas && spi_qas(starget)) {
790 starget_printk(KERN_ERR, starget, "Domain Validation Disabling Quick Arbitration and Selection\n");
793 newperiod = spi_period(starget);
794 period = newperiod > period ? newperiod : period;
798 period += period >> 1;
800 if (unlikely(period > 0xff || period == prevperiod)) {
801 /* Total failure; set to async and return */
802 starget_printk(KERN_ERR, starget, "Domain Validation Failure, dropping back to Asynchronous\n");
804 return SPI_COMPARE_FAILURE;
806 starget_printk(KERN_ERR, starget, "Domain Validation detected failure, dropping back\n");
807 DV_SET(period, period);
815 spi_dv_device_get_echo_buffer(struct scsi_device *sdev, u8 *buffer)
819 /* first off do a test unit ready. This can error out
820 * because of reservations or some other reason. If it
821 * fails, the device won't let us write to the echo buffer
822 * so just return failure */
824 const char spi_test_unit_ready[] = {
825 TEST_UNIT_READY, 0, 0, 0, 0, 0
828 const char spi_read_buffer_descriptor[] = {
829 READ_BUFFER, 0x0b, 0, 0, 0, 0, 0, 0, 4, 0
833 /* We send a set of three TURs to clear any outstanding
834 * unit attention conditions if they exist (Otherwise the
835 * buffer tests won't be happy). If the TUR still fails
836 * (reservation conflict, device not ready, etc) just
837 * skip the write tests */
839 result = spi_execute(sdev, spi_test_unit_ready, DMA_NONE,
851 result = spi_execute(sdev, spi_read_buffer_descriptor,
852 DMA_FROM_DEVICE, buffer, 4, NULL);
855 /* Device has no echo buffer */
858 return buffer[3] + ((buffer[2] & 0x1f) << 8);
862 spi_dv_device_internal(struct scsi_device *sdev, u8 *buffer)
864 struct spi_internal *i = to_spi_internal(sdev->host->transportt);
865 struct scsi_target *starget = sdev->sdev_target;
866 struct Scsi_Host *shost = sdev->host;
867 int len = sdev->inquiry_len;
868 int min_period = spi_min_period(starget);
869 int max_width = spi_max_width(starget);
870 /* first set us up for narrow async */
874 if (spi_dv_device_compare_inquiry(sdev, buffer, buffer, DV_LOOPS)
875 != SPI_COMPARE_SUCCESS) {
876 starget_printk(KERN_ERR, starget, "Domain Validation Initial Inquiry Failed\n");
877 /* FIXME: should probably offline the device here? */
881 if (!spi_support_wide(starget)) {
882 spi_max_width(starget) = 0;
887 if (i->f->set_width && max_width) {
888 i->f->set_width(starget, 1);
890 if (spi_dv_device_compare_inquiry(sdev, buffer,
893 != SPI_COMPARE_SUCCESS) {
894 starget_printk(KERN_ERR, starget, "Wide Transfers Fail\n");
895 i->f->set_width(starget, 0);
896 /* Make sure we don't force wide back on by asking
897 * for a transfer period that requires it */
904 if (!i->f->set_period)
907 /* device can't handle synchronous */
908 if (!spi_support_sync(starget) && !spi_support_dt(starget))
911 /* len == -1 is the signal that we need to ascertain the
912 * presence of an echo buffer before trying to use it. len ==
913 * 0 means we don't have an echo buffer */
918 /* now set up to the maximum */
919 DV_SET(offset, spi_max_offset(starget));
920 DV_SET(period, min_period);
922 /* try QAS requests; this should be harmless to set if the
923 * target supports it */
924 if (spi_support_qas(starget) && spi_max_qas(starget)) {
930 if (spi_support_ius(starget) && spi_max_iu(starget) &&
932 /* This u320 (or u640). Set IU transfers */
934 /* Then set the optional parameters */
944 /* now that we've done all this, actually check the bus
945 * signal type (if known). Some devices are stupid on
946 * a SE bus and still claim they can try LVD only settings */
947 if (i->f->get_signalling)
948 i->f->get_signalling(shost);
949 if (spi_signalling(shost) == SPI_SIGNAL_SE ||
950 spi_signalling(shost) == SPI_SIGNAL_HVD ||
951 !spi_support_dt(starget)) {
956 /* set width last because it will pull all the other
957 * parameters down to required values */
958 DV_SET(width, max_width);
960 /* Do the read only INQUIRY tests */
961 spi_dv_retrain(sdev, buffer, buffer + sdev->inquiry_len,
962 spi_dv_device_compare_inquiry);
963 /* See if we actually managed to negotiate and sustain DT */
965 i->f->get_dt(starget);
967 /* see if the device has an echo buffer. If it does we can do
968 * the SPI pattern write tests. Because of some broken
969 * devices, we *only* try this on a device that has actually
972 if (len == -1 && spi_dt(starget))
973 len = spi_dv_device_get_echo_buffer(sdev, buffer);
976 starget_printk(KERN_INFO, starget, "Domain Validation skipping write tests\n");
980 if (len > SPI_MAX_ECHO_BUFFER_SIZE) {
981 starget_printk(KERN_WARNING, starget, "Echo buffer size %d is too big, trimming to %d\n", len, SPI_MAX_ECHO_BUFFER_SIZE);
982 len = SPI_MAX_ECHO_BUFFER_SIZE;
985 if (spi_dv_retrain(sdev, buffer, buffer + len,
986 spi_dv_device_echo_buffer)
987 == SPI_COMPARE_SKIP_TEST) {
988 /* OK, the stupid drive can't do a write echo buffer
989 * test after all, fall back to the read tests */
996 /** spi_dv_device - Do Domain Validation on the device
997 * @sdev: scsi device to validate
999 * Performs the domain validation on the given device in the
1000 * current execution thread. Since DV operations may sleep,
1001 * the current thread must have user context. Also no SCSI
1002 * related locks that would deadlock I/O issued by the DV may
1006 spi_dv_device(struct scsi_device *sdev)
1008 struct scsi_target *starget = sdev->sdev_target;
1010 const int len = SPI_MAX_ECHO_BUFFER_SIZE*2;
1012 if (unlikely(spi_dv_in_progress(starget)))
1015 if (unlikely(scsi_device_get(sdev)))
1017 spi_dv_in_progress(starget) = 1;
1019 buffer = kzalloc(len, GFP_KERNEL);
1021 if (unlikely(!buffer))
1024 /* We need to verify that the actual device will quiesce; the
1025 * later target quiesce is just a nice to have */
1026 if (unlikely(scsi_device_quiesce(sdev)))
1029 scsi_target_quiesce(starget);
1031 spi_dv_pending(starget) = 1;
1032 mutex_lock(&spi_dv_mutex(starget));
1034 starget_printk(KERN_INFO, starget, "Beginning Domain Validation\n");
1036 spi_dv_device_internal(sdev, buffer);
1038 starget_printk(KERN_INFO, starget, "Ending Domain Validation\n");
1040 mutex_unlock(&spi_dv_mutex(starget));
1041 spi_dv_pending(starget) = 0;
1043 scsi_target_resume(starget);
1045 spi_initial_dv(starget) = 1;
1050 spi_dv_in_progress(starget) = 0;
1051 scsi_device_put(sdev);
1053 EXPORT_SYMBOL(spi_dv_device);
1055 struct work_queue_wrapper {
1056 struct work_struct work;
1057 struct scsi_device *sdev;
1061 spi_dv_device_work_wrapper(struct work_struct *work)
1063 struct work_queue_wrapper *wqw =
1064 container_of(work, struct work_queue_wrapper, work);
1065 struct scsi_device *sdev = wqw->sdev;
1068 spi_dv_device(sdev);
1069 spi_dv_pending(sdev->sdev_target) = 0;
1070 scsi_device_put(sdev);
1075 * spi_schedule_dv_device - schedule domain validation to occur on the device
1076 * @sdev: The device to validate
1078 * Identical to spi_dv_device() above, except that the DV will be
1079 * scheduled to occur in a workqueue later. All memory allocations
1080 * are atomic, so may be called from any context including those holding
1084 spi_schedule_dv_device(struct scsi_device *sdev)
1086 struct work_queue_wrapper *wqw =
1087 kmalloc(sizeof(struct work_queue_wrapper), GFP_ATOMIC);
1092 if (unlikely(spi_dv_pending(sdev->sdev_target))) {
1096 /* Set pending early (dv_device doesn't check it, only sets it) */
1097 spi_dv_pending(sdev->sdev_target) = 1;
1098 if (unlikely(scsi_device_get(sdev))) {
1100 spi_dv_pending(sdev->sdev_target) = 0;
1104 INIT_WORK(&wqw->work, spi_dv_device_work_wrapper);
1107 schedule_work(&wqw->work);
1109 EXPORT_SYMBOL(spi_schedule_dv_device);
1112 * spi_display_xfer_agreement - Print the current target transfer agreement
1113 * @starget: The target for which to display the agreement
1115 * Each SPI port is required to maintain a transfer agreement for each
1116 * other port on the bus. This function prints a one-line summary of
1117 * the current agreement; more detailed information is available in sysfs.
1119 void spi_display_xfer_agreement(struct scsi_target *starget)
1121 struct spi_transport_attrs *tp;
1122 tp = (struct spi_transport_attrs *)&starget->starget_data;
1124 if (tp->offset > 0 && tp->period > 0) {
1125 unsigned int picosec, kb100;
1126 char *scsi = "FAST-?";
1129 if (tp->period <= SPI_STATIC_PPR) {
1130 picosec = ppr_to_ps[tp->period];
1131 switch (tp->period) {
1132 case 7: scsi = "FAST-320"; break;
1133 case 8: scsi = "FAST-160"; break;
1134 case 9: scsi = "FAST-80"; break;
1136 case 11: scsi = "FAST-40"; break;
1137 case 12: scsi = "FAST-20"; break;
1140 picosec = tp->period * 4000;
1141 if (tp->period < 25)
1143 else if (tp->period < 50)
1149 kb100 = (10000000 + picosec / 2) / picosec;
1152 sprint_frac(tmp, picosec, 1000);
1154 dev_info(&starget->dev,
1155 "%s %sSCSI %d.%d MB/s %s%s%s%s%s%s%s%s (%s ns, offset %d)\n",
1156 scsi, tp->width ? "WIDE " : "", kb100/10, kb100 % 10,
1157 tp->dt ? "DT" : "ST",
1158 tp->iu ? " IU" : "",
1159 tp->qas ? " QAS" : "",
1160 tp->rd_strm ? " RDSTRM" : "",
1161 tp->rti ? " RTI" : "",
1162 tp->wr_flow ? " WRFLOW" : "",
1163 tp->pcomp_en ? " PCOMP" : "",
1164 tp->hold_mcs ? " HMCS" : "",
1167 dev_info(&starget->dev, "%sasynchronous\n",
1168 tp->width ? "wide " : "");
1171 EXPORT_SYMBOL(spi_display_xfer_agreement);
1173 int spi_populate_width_msg(unsigned char *msg, int width)
1175 msg[0] = EXTENDED_MESSAGE;
1177 msg[2] = EXTENDED_WDTR;
1181 EXPORT_SYMBOL_GPL(spi_populate_width_msg);
1183 int spi_populate_sync_msg(unsigned char *msg, int period, int offset)
1185 msg[0] = EXTENDED_MESSAGE;
1187 msg[2] = EXTENDED_SDTR;
1192 EXPORT_SYMBOL_GPL(spi_populate_sync_msg);
1194 int spi_populate_ppr_msg(unsigned char *msg, int period, int offset,
1195 int width, int options)
1197 msg[0] = EXTENDED_MESSAGE;
1199 msg[2] = EXTENDED_PPR;
1207 EXPORT_SYMBOL_GPL(spi_populate_ppr_msg);
1210 * spi_populate_tag_msg - place a tag message in a buffer
1211 * @msg: pointer to the area to place the tag
1212 * @cmd: pointer to the scsi command for the tag
1215 * designed to create the correct type of tag message for the
1216 * particular request. Returns the size of the tag message.
1217 * May return 0 if TCQ is disabled for this device.
1219 int spi_populate_tag_msg(unsigned char *msg, struct scsi_cmnd *cmd)
1221 if (cmd->flags & SCMD_TAGGED) {
1222 *msg++ = SIMPLE_QUEUE_TAG;
1223 *msg++ = cmd->request->tag;
1229 EXPORT_SYMBOL_GPL(spi_populate_tag_msg);
1231 #ifdef CONFIG_SCSI_CONSTANTS
1232 static const char * const one_byte_msgs[] = {
1233 /* 0x00 */ "Task Complete", NULL /* Extended Message */, "Save Pointers",
1234 /* 0x03 */ "Restore Pointers", "Disconnect", "Initiator Error",
1235 /* 0x06 */ "Abort Task Set", "Message Reject", "Nop", "Message Parity Error",
1236 /* 0x0a */ "Linked Command Complete", "Linked Command Complete w/flag",
1237 /* 0x0c */ "Target Reset", "Abort Task", "Clear Task Set",
1238 /* 0x0f */ "Initiate Recovery", "Release Recovery",
1239 /* 0x11 */ "Terminate Process", "Continue Task", "Target Transfer Disable",
1240 /* 0x14 */ NULL, NULL, "Clear ACA", "LUN Reset"
1243 static const char * const two_byte_msgs[] = {
1244 /* 0x20 */ "Simple Queue Tag", "Head of Queue Tag", "Ordered Queue Tag",
1245 /* 0x23 */ "Ignore Wide Residue", "ACA"
1248 static const char * const extended_msgs[] = {
1249 /* 0x00 */ "Modify Data Pointer", "Synchronous Data Transfer Request",
1250 /* 0x02 */ "SCSI-I Extended Identify", "Wide Data Transfer Request",
1251 /* 0x04 */ "Parallel Protocol Request", "Modify Bidirectional Data Pointer"
1254 static void print_nego(const unsigned char *msg, int per, int off, int width)
1258 period_to_str(buf, msg[per]);
1259 printk("period = %s ns ", buf);
1263 printk("offset = %d ", msg[off]);
1265 printk("width = %d ", 8 << msg[width]);
1268 static void print_ptr(const unsigned char *msg, int msb, const char *desc)
1270 int ptr = (msg[msb] << 24) | (msg[msb+1] << 16) | (msg[msb+2] << 8) |
1272 printk("%s = %d ", desc, ptr);
1275 int spi_print_msg(const unsigned char *msg)
1278 if (msg[0] == EXTENDED_MESSAGE) {
1282 if (msg[2] < ARRAY_SIZE(extended_msgs))
1283 printk ("%s ", extended_msgs[msg[2]]);
1285 printk ("Extended Message, reserved code (0x%02x) ",
1288 case EXTENDED_MODIFY_DATA_POINTER:
1289 print_ptr(msg, 3, "pointer");
1292 print_nego(msg, 3, 4, 0);
1295 print_nego(msg, 0, 0, 3);
1298 print_nego(msg, 3, 5, 6);
1300 case EXTENDED_MODIFY_BIDI_DATA_PTR:
1301 print_ptr(msg, 3, "out");
1302 print_ptr(msg, 7, "in");
1305 for (i = 2; i < len; ++i)
1306 printk("%02x ", msg[i]);
1309 } else if (msg[0] & 0x80) {
1310 printk("Identify disconnect %sallowed %s %d ",
1311 (msg[0] & 0x40) ? "" : "not ",
1312 (msg[0] & 0x20) ? "target routine" : "lun",
1314 /* Normal One byte */
1315 } else if (msg[0] < 0x1f) {
1316 if (msg[0] < ARRAY_SIZE(one_byte_msgs) && one_byte_msgs[msg[0]])
1317 printk("%s ", one_byte_msgs[msg[0]]);
1319 printk("reserved (%02x) ", msg[0]);
1320 } else if (msg[0] == 0x55) {
1321 printk("QAS Request ");
1323 } else if (msg[0] <= 0x2f) {
1324 if ((msg[0] - 0x20) < ARRAY_SIZE(two_byte_msgs))
1325 printk("%s %02x ", two_byte_msgs[msg[0] - 0x20],
1328 printk("reserved two byte (%02x %02x) ",
1332 printk("reserved ");
1335 EXPORT_SYMBOL(spi_print_msg);
1337 #else /* ifndef CONFIG_SCSI_CONSTANTS */
1339 int spi_print_msg(const unsigned char *msg)
1343 if (msg[0] == EXTENDED_MESSAGE) {
1347 for (i = 0; i < len; ++i)
1348 printk("%02x ", msg[i]);
1350 } else if (msg[0] & 0x80) {
1351 printk("%02x ", msg[0]);
1352 /* Normal One byte */
1353 } else if ((msg[0] < 0x1f) || (msg[0] == 0x55)) {
1354 printk("%02x ", msg[0]);
1356 } else if (msg[0] <= 0x2f) {
1357 printk("%02x %02x", msg[0], msg[1]);
1360 printk("%02x ", msg[0]);
1363 EXPORT_SYMBOL(spi_print_msg);
1364 #endif /* ! CONFIG_SCSI_CONSTANTS */
1366 static int spi_device_match(struct attribute_container *cont,
1369 struct scsi_device *sdev;
1370 struct Scsi_Host *shost;
1371 struct spi_internal *i;
1373 if (!scsi_is_sdev_device(dev))
1376 sdev = to_scsi_device(dev);
1378 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1379 != &spi_host_class.class)
1381 /* Note: this class has no device attributes, so it has
1382 * no per-HBA allocation and thus we don't need to distinguish
1383 * the attribute containers for the device */
1384 i = to_spi_internal(shost->transportt);
1385 if (i->f->deny_binding && i->f->deny_binding(sdev->sdev_target))
1390 static int spi_target_match(struct attribute_container *cont,
1393 struct Scsi_Host *shost;
1394 struct scsi_target *starget;
1395 struct spi_internal *i;
1397 if (!scsi_is_target_device(dev))
1400 shost = dev_to_shost(dev->parent);
1401 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1402 != &spi_host_class.class)
1405 i = to_spi_internal(shost->transportt);
1406 starget = to_scsi_target(dev);
1408 if (i->f->deny_binding && i->f->deny_binding(starget))
1411 return &i->t.target_attrs.ac == cont;
1414 static DECLARE_TRANSPORT_CLASS(spi_transport_class,
1416 spi_setup_transport_attrs,
1418 spi_target_configure);
1420 static DECLARE_ANON_TRANSPORT_CLASS(spi_device_class,
1422 spi_device_configure);
1424 static struct attribute *host_attributes[] = {
1425 &dev_attr_signalling.attr,
1426 &dev_attr_host_width.attr,
1427 &dev_attr_hba_id.attr,
1431 static struct attribute_group host_attribute_group = {
1432 .attrs = host_attributes,
1435 static int spi_host_configure(struct transport_container *tc,
1437 struct device *cdev)
1439 struct kobject *kobj = &cdev->kobj;
1440 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1441 struct spi_internal *si = to_spi_internal(shost->transportt);
1442 struct attribute *attr = &dev_attr_signalling.attr;
1445 if (si->f->set_signalling)
1446 rc = sysfs_chmod_file(kobj, attr, attr->mode | S_IWUSR);
1451 /* returns true if we should be showing the variable. Also
1452 * overloads the return by setting 1<<1 if the attribute should
1454 #define TARGET_ATTRIBUTE_HELPER(name) \
1455 (si->f->show_##name ? S_IRUGO : 0) | \
1456 (si->f->set_##name ? S_IWUSR : 0)
1458 static umode_t target_attribute_is_visible(struct kobject *kobj,
1459 struct attribute *attr, int i)
1461 struct device *cdev = container_of(kobj, struct device, kobj);
1462 struct scsi_target *starget = transport_class_to_starget(cdev);
1463 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1464 struct spi_internal *si = to_spi_internal(shost->transportt);
1466 if (attr == &dev_attr_period.attr &&
1467 spi_support_sync(starget))
1468 return TARGET_ATTRIBUTE_HELPER(period);
1469 else if (attr == &dev_attr_min_period.attr &&
1470 spi_support_sync(starget))
1471 return TARGET_ATTRIBUTE_HELPER(period);
1472 else if (attr == &dev_attr_offset.attr &&
1473 spi_support_sync(starget))
1474 return TARGET_ATTRIBUTE_HELPER(offset);
1475 else if (attr == &dev_attr_max_offset.attr &&
1476 spi_support_sync(starget))
1477 return TARGET_ATTRIBUTE_HELPER(offset);
1478 else if (attr == &dev_attr_width.attr &&
1479 spi_support_wide(starget))
1480 return TARGET_ATTRIBUTE_HELPER(width);
1481 else if (attr == &dev_attr_max_width.attr &&
1482 spi_support_wide(starget))
1483 return TARGET_ATTRIBUTE_HELPER(width);
1484 else if (attr == &dev_attr_iu.attr &&
1485 spi_support_ius(starget))
1486 return TARGET_ATTRIBUTE_HELPER(iu);
1487 else if (attr == &dev_attr_max_iu.attr &&
1488 spi_support_ius(starget))
1489 return TARGET_ATTRIBUTE_HELPER(iu);
1490 else if (attr == &dev_attr_dt.attr &&
1491 spi_support_dt(starget))
1492 return TARGET_ATTRIBUTE_HELPER(dt);
1493 else if (attr == &dev_attr_qas.attr &&
1494 spi_support_qas(starget))
1495 return TARGET_ATTRIBUTE_HELPER(qas);
1496 else if (attr == &dev_attr_max_qas.attr &&
1497 spi_support_qas(starget))
1498 return TARGET_ATTRIBUTE_HELPER(qas);
1499 else if (attr == &dev_attr_wr_flow.attr &&
1500 spi_support_ius(starget))
1501 return TARGET_ATTRIBUTE_HELPER(wr_flow);
1502 else if (attr == &dev_attr_rd_strm.attr &&
1503 spi_support_ius(starget))
1504 return TARGET_ATTRIBUTE_HELPER(rd_strm);
1505 else if (attr == &dev_attr_rti.attr &&
1506 spi_support_ius(starget))
1507 return TARGET_ATTRIBUTE_HELPER(rti);
1508 else if (attr == &dev_attr_pcomp_en.attr &&
1509 spi_support_ius(starget))
1510 return TARGET_ATTRIBUTE_HELPER(pcomp_en);
1511 else if (attr == &dev_attr_hold_mcs.attr &&
1512 spi_support_ius(starget))
1513 return TARGET_ATTRIBUTE_HELPER(hold_mcs);
1514 else if (attr == &dev_attr_revalidate.attr)
1520 static struct attribute *target_attributes[] = {
1521 &dev_attr_period.attr,
1522 &dev_attr_min_period.attr,
1523 &dev_attr_offset.attr,
1524 &dev_attr_max_offset.attr,
1525 &dev_attr_width.attr,
1526 &dev_attr_max_width.attr,
1528 &dev_attr_max_iu.attr,
1531 &dev_attr_max_qas.attr,
1532 &dev_attr_wr_flow.attr,
1533 &dev_attr_rd_strm.attr,
1535 &dev_attr_pcomp_en.attr,
1536 &dev_attr_hold_mcs.attr,
1537 &dev_attr_revalidate.attr,
1541 static struct attribute_group target_attribute_group = {
1542 .attrs = target_attributes,
1543 .is_visible = target_attribute_is_visible,
1546 static int spi_target_configure(struct transport_container *tc,
1548 struct device *cdev)
1550 struct kobject *kobj = &cdev->kobj;
1552 /* force an update based on parameters read from the device */
1553 sysfs_update_group(kobj, &target_attribute_group);
1558 struct scsi_transport_template *
1559 spi_attach_transport(struct spi_function_template *ft)
1561 struct spi_internal *i = kzalloc(sizeof(struct spi_internal),
1567 i->t.target_attrs.ac.class = &spi_transport_class.class;
1568 i->t.target_attrs.ac.grp = &target_attribute_group;
1569 i->t.target_attrs.ac.match = spi_target_match;
1570 transport_container_register(&i->t.target_attrs);
1571 i->t.target_size = sizeof(struct spi_transport_attrs);
1572 i->t.host_attrs.ac.class = &spi_host_class.class;
1573 i->t.host_attrs.ac.grp = &host_attribute_group;
1574 i->t.host_attrs.ac.match = spi_host_match;
1575 transport_container_register(&i->t.host_attrs);
1576 i->t.host_size = sizeof(struct spi_host_attrs);
1581 EXPORT_SYMBOL(spi_attach_transport);
1583 void spi_release_transport(struct scsi_transport_template *t)
1585 struct spi_internal *i = to_spi_internal(t);
1587 transport_container_unregister(&i->t.target_attrs);
1588 transport_container_unregister(&i->t.host_attrs);
1592 EXPORT_SYMBOL(spi_release_transport);
1594 static __init int spi_transport_init(void)
1596 int error = scsi_dev_info_add_list(SCSI_DEVINFO_SPI,
1597 "SCSI Parallel Transport Class");
1601 for (i = 0; spi_static_device_list[i].vendor; i++)
1602 scsi_dev_info_list_add_keyed(1, /* compatible */
1603 spi_static_device_list[i].vendor,
1604 spi_static_device_list[i].model,
1606 spi_static_device_list[i].flags,
1610 error = transport_class_register(&spi_transport_class);
1613 error = anon_transport_class_register(&spi_device_class);
1614 return transport_class_register(&spi_host_class);
1617 static void __exit spi_transport_exit(void)
1619 transport_class_unregister(&spi_transport_class);
1620 anon_transport_class_unregister(&spi_device_class);
1621 transport_class_unregister(&spi_host_class);
1622 scsi_dev_info_remove_list(SCSI_DEVINFO_SPI);
1625 MODULE_AUTHOR("Martin Hicks");
1626 MODULE_DESCRIPTION("SPI Transport Attributes");
1627 MODULE_LICENSE("GPL");
1629 module_init(spi_transport_init);
1630 module_exit(spi_transport_exit);