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 <asm/semaphore.h>
26 #include <scsi/scsi.h>
27 #include "scsi_priv.h"
28 #include <scsi/scsi_device.h>
29 #include <scsi/scsi_host.h>
30 #include <scsi/scsi_request.h>
31 #include <scsi/scsi_eh.h>
32 #include <scsi/scsi_transport.h>
33 #include <scsi/scsi_transport_spi.h>
35 #define SPI_PRINTK(x, l, f, a...) dev_printk(l, &(x)->dev, f , ##a)
37 #define SPI_NUM_ATTRS 10 /* increase this if you add attributes */
38 #define SPI_OTHER_ATTRS 1 /* Increase this if you add "always
40 #define SPI_HOST_ATTRS 1
42 #define SPI_MAX_ECHO_BUFFER_SIZE 4096
44 /* Private data accessors (keep these out of the header file) */
45 #define spi_dv_pending(x) (((struct spi_transport_attrs *)&(x)->starget_data)->dv_pending)
46 #define spi_dv_sem(x) (((struct spi_transport_attrs *)&(x)->starget_data)->dv_sem)
49 struct scsi_transport_template t;
50 struct spi_function_template *f;
51 /* The actual attributes */
52 struct class_device_attribute private_attrs[SPI_NUM_ATTRS];
53 /* The array of null terminated pointers to attributes
54 * needed by scsi_sysfs.c */
55 struct class_device_attribute *attrs[SPI_NUM_ATTRS + SPI_OTHER_ATTRS + 1];
56 struct class_device_attribute private_host_attrs[SPI_HOST_ATTRS];
57 struct class_device_attribute *host_attrs[SPI_HOST_ATTRS + 1];
60 #define to_spi_internal(tmpl) container_of(tmpl, struct spi_internal, t)
62 static const int ppr_to_ps[] = {
63 /* The PPR values 0-6 are reserved, fill them in when
64 * the committee defines them */
79 /* The PPR values at which you calculate the period in ns by multiplying
81 #define SPI_STATIC_PPR 0x0c
83 static int sprint_frac(char *dest, int value, int denom)
85 int frac = value % denom;
86 int result = sprintf(dest, "%d", value / denom);
94 sprintf(dest + result, "%d", frac / denom);
99 dest[result++] = '\0';
104 enum spi_signal_type value;
107 { SPI_SIGNAL_UNKNOWN, "unknown" },
108 { SPI_SIGNAL_SE, "SE" },
109 { SPI_SIGNAL_LVD, "LVD" },
110 { SPI_SIGNAL_HVD, "HVD" },
113 static inline const char *spi_signal_to_string(enum spi_signal_type type)
117 for (i = 0; i < sizeof(signal_types)/sizeof(signal_types[0]); i++) {
118 if (type == signal_types[i].value)
119 return signal_types[i].name;
123 static inline enum spi_signal_type spi_signal_to_value(const char *name)
127 for (i = 0; i < sizeof(signal_types)/sizeof(signal_types[0]); i++) {
128 len = strlen(signal_types[i].name);
129 if (strncmp(name, signal_types[i].name, len) == 0 &&
130 (name[len] == '\n' || name[len] == '\0'))
131 return signal_types[i].value;
133 return SPI_SIGNAL_UNKNOWN;
136 static int spi_host_setup(struct device *dev)
138 struct Scsi_Host *shost = dev_to_shost(dev);
140 spi_signalling(shost) = SPI_SIGNAL_UNKNOWN;
145 static DECLARE_TRANSPORT_CLASS(spi_host_class,
151 static int spi_host_match(struct attribute_container *cont,
154 struct Scsi_Host *shost;
155 struct spi_internal *i;
157 if (!scsi_is_host_device(dev))
160 shost = dev_to_shost(dev);
161 if (!shost->transportt || shost->transportt->host_attrs.ac.class
162 != &spi_host_class.class)
165 i = to_spi_internal(shost->transportt);
167 return &i->t.host_attrs.ac == cont;
170 static int spi_device_configure(struct device *dev)
172 struct scsi_device *sdev = to_scsi_device(dev);
173 struct scsi_target *starget = sdev->sdev_target;
175 /* Populate the target capability fields with the values
176 * gleaned from the device inquiry */
178 spi_support_sync(starget) = scsi_device_sync(sdev);
179 spi_support_wide(starget) = scsi_device_wide(sdev);
180 spi_support_dt(starget) = scsi_device_dt(sdev);
181 spi_support_dt_only(starget) = scsi_device_dt_only(sdev);
182 spi_support_ius(starget) = scsi_device_ius(sdev);
183 spi_support_qas(starget) = scsi_device_qas(sdev);
188 static int spi_setup_transport_attrs(struct device *dev)
190 struct scsi_target *starget = to_scsi_target(dev);
192 spi_period(starget) = -1; /* illegal value */
193 spi_offset(starget) = 0; /* async */
194 spi_width(starget) = 0; /* narrow */
195 spi_iu(starget) = 0; /* no IU */
196 spi_dt(starget) = 0; /* ST */
197 spi_qas(starget) = 0;
198 spi_wr_flow(starget) = 0;
199 spi_rd_strm(starget) = 0;
200 spi_rti(starget) = 0;
201 spi_pcomp_en(starget) = 0;
202 spi_dv_pending(starget) = 0;
203 spi_initial_dv(starget) = 0;
204 init_MUTEX(&spi_dv_sem(starget));
209 #define spi_transport_show_function(field, format_string) \
212 show_spi_transport_##field(struct class_device *cdev, char *buf) \
214 struct scsi_target *starget = transport_class_to_starget(cdev); \
215 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
216 struct spi_transport_attrs *tp; \
217 struct spi_internal *i = to_spi_internal(shost->transportt); \
218 tp = (struct spi_transport_attrs *)&starget->starget_data; \
219 if (i->f->get_##field) \
220 i->f->get_##field(starget); \
221 return snprintf(buf, 20, format_string, tp->field); \
224 #define spi_transport_store_function(field, format_string) \
226 store_spi_transport_##field(struct class_device *cdev, const char *buf, \
230 struct scsi_target *starget = transport_class_to_starget(cdev); \
231 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
232 struct spi_internal *i = to_spi_internal(shost->transportt); \
234 val = simple_strtoul(buf, NULL, 0); \
235 i->f->set_##field(starget, val); \
239 #define spi_transport_rd_attr(field, format_string) \
240 spi_transport_show_function(field, format_string) \
241 spi_transport_store_function(field, format_string) \
242 static CLASS_DEVICE_ATTR(field, S_IRUGO | S_IWUSR, \
243 show_spi_transport_##field, \
244 store_spi_transport_##field);
246 /* The Parallel SCSI Tranport Attributes: */
247 spi_transport_rd_attr(offset, "%d\n");
248 spi_transport_rd_attr(width, "%d\n");
249 spi_transport_rd_attr(iu, "%d\n");
250 spi_transport_rd_attr(dt, "%d\n");
251 spi_transport_rd_attr(qas, "%d\n");
252 spi_transport_rd_attr(wr_flow, "%d\n");
253 spi_transport_rd_attr(rd_strm, "%d\n");
254 spi_transport_rd_attr(rti, "%d\n");
255 spi_transport_rd_attr(pcomp_en, "%d\n");
258 store_spi_revalidate(struct class_device *cdev, const char *buf, size_t count)
260 struct scsi_target *starget = transport_class_to_starget(cdev);
262 /* FIXME: we're relying on an awful lot of device internals
263 * here. We really need a function to get the first available
265 struct device *dev = container_of(starget->dev.children.next, struct device, node);
266 struct scsi_device *sdev = to_scsi_device(dev);
270 static CLASS_DEVICE_ATTR(revalidate, S_IWUSR, NULL, store_spi_revalidate);
272 /* Translate the period into ns according to the current spec
273 * for SDTR/PPR messages */
274 static ssize_t show_spi_transport_period(struct class_device *cdev, char *buf)
277 struct scsi_target *starget = transport_class_to_starget(cdev);
278 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
279 struct spi_transport_attrs *tp;
281 struct spi_internal *i = to_spi_internal(shost->transportt);
283 tp = (struct spi_transport_attrs *)&starget->starget_data;
285 if (i->f->get_period)
286 i->f->get_period(starget);
288 if (tp->period < 0 || tp->period > 0xff) {
290 } else if (tp->period <= SPI_STATIC_PPR) {
291 picosec = ppr_to_ps[tp->period];
293 picosec = tp->period * 4000;
297 len = sprintf(buf, "reserved");
299 len = sprint_frac(buf, picosec, 1000);
308 store_spi_transport_period(struct class_device *cdev, const char *buf,
311 struct scsi_target *starget = transport_class_to_starget(cdev);
312 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
313 struct spi_internal *i = to_spi_internal(shost->transportt);
314 int j, picosec, period = -1;
317 picosec = simple_strtoul(buf, &endp, 10) * 1000;
324 picosec += (*endp - '0') * mult;
329 for (j = 0; j <= SPI_STATIC_PPR; j++) {
330 if (ppr_to_ps[j] < picosec)
337 period = picosec / 4000;
342 i->f->set_period(starget, period);
347 static CLASS_DEVICE_ATTR(period, S_IRUGO | S_IWUSR,
348 show_spi_transport_period,
349 store_spi_transport_period);
351 static ssize_t show_spi_host_signalling(struct class_device *cdev, char *buf)
353 struct Scsi_Host *shost = transport_class_to_shost(cdev);
354 struct spi_internal *i = to_spi_internal(shost->transportt);
356 if (i->f->get_signalling)
357 i->f->get_signalling(shost);
359 return sprintf(buf, "%s\n", spi_signal_to_string(spi_signalling(shost)));
361 static ssize_t store_spi_host_signalling(struct class_device *cdev,
362 const char *buf, size_t count)
364 struct Scsi_Host *shost = transport_class_to_shost(cdev);
365 struct spi_internal *i = to_spi_internal(shost->transportt);
366 enum spi_signal_type type = spi_signal_to_value(buf);
368 if (type != SPI_SIGNAL_UNKNOWN)
369 i->f->set_signalling(shost, type);
373 static CLASS_DEVICE_ATTR(signalling, S_IRUGO | S_IWUSR,
374 show_spi_host_signalling,
375 store_spi_host_signalling);
377 #define DV_SET(x, y) \
379 i->f->set_##x(sdev->sdev_target, y)
382 #define DV_TIMEOUT (10*HZ)
383 #define DV_RETRIES 3 /* should only need at most
384 * two cc/ua clears */
386 enum spi_compare_returns {
389 SPI_COMPARE_SKIP_TEST,
393 /* This is for read/write Domain Validation: If the device supports
394 * an echo buffer, we do read/write tests to it */
395 static enum spi_compare_returns
396 spi_dv_device_echo_buffer(struct scsi_request *sreq, u8 *buffer,
397 u8 *ptr, const int retries)
399 struct scsi_device *sdev = sreq->sr_device;
400 int len = ptr - buffer;
402 unsigned int pattern = 0x0000ffff;
404 const char spi_write_buffer[] = {
405 WRITE_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
407 const char spi_read_buffer[] = {
408 READ_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
411 /* set up the pattern buffer. Doesn't matter if we spill
412 * slightly beyond since that's where the read buffer is */
413 for (j = 0; j < len; ) {
415 /* fill the buffer with counting (test a) */
416 for ( ; j < min(len, 32); j++)
419 /* fill the buffer with alternating words of 0x0 and
421 for ( ; j < min(len, k + 32); j += 2) {
422 u16 *word = (u16 *)&buffer[j];
424 *word = (j & 0x02) ? 0x0000 : 0xffff;
427 /* fill with crosstalk (alternating 0x5555 0xaaa)
429 for ( ; j < min(len, k + 32); j += 2) {
430 u16 *word = (u16 *)&buffer[j];
432 *word = (j & 0x02) ? 0x5555 : 0xaaaa;
435 /* fill with shifting bits (test d) */
436 for ( ; j < min(len, k + 32); j += 4) {
437 u32 *word = (unsigned int *)&buffer[j];
438 u32 roll = (pattern & 0x80000000) ? 1 : 0;
441 pattern = (pattern << 1) | roll;
443 /* don't bother with random data (test e) */
446 for (r = 0; r < retries; r++) {
447 sreq->sr_cmd_len = 0; /* wait_req to fill in */
448 sreq->sr_data_direction = DMA_TO_DEVICE;
449 scsi_wait_req(sreq, spi_write_buffer, buffer, len,
450 DV_TIMEOUT, DV_RETRIES);
451 if(sreq->sr_result || !scsi_device_online(sdev)) {
452 struct scsi_sense_hdr sshdr;
454 scsi_device_set_state(sdev, SDEV_QUIESCE);
455 if (scsi_request_normalize_sense(sreq, &sshdr)
456 && sshdr.sense_key == ILLEGAL_REQUEST
457 /* INVALID FIELD IN CDB */
458 && sshdr.asc == 0x24 && sshdr.ascq == 0x00)
459 /* This would mean that the drive lied
460 * to us about supporting an echo
461 * buffer (unfortunately some Western
462 * Digital drives do precisely this)
464 return SPI_COMPARE_SKIP_TEST;
467 SPI_PRINTK(sdev->sdev_target, KERN_ERR, "Write Buffer failure %x\n", sreq->sr_result);
468 return SPI_COMPARE_FAILURE;
472 sreq->sr_cmd_len = 0; /* wait_req to fill in */
473 sreq->sr_data_direction = DMA_FROM_DEVICE;
474 scsi_wait_req(sreq, spi_read_buffer, ptr, len,
475 DV_TIMEOUT, DV_RETRIES);
476 scsi_device_set_state(sdev, SDEV_QUIESCE);
478 if (memcmp(buffer, ptr, len) != 0)
479 return SPI_COMPARE_FAILURE;
481 return SPI_COMPARE_SUCCESS;
484 /* This is for the simplest form of Domain Validation: a read test
485 * on the inquiry data from the device */
486 static enum spi_compare_returns
487 spi_dv_device_compare_inquiry(struct scsi_request *sreq, u8 *buffer,
488 u8 *ptr, const int retries)
491 const int len = sreq->sr_device->inquiry_len;
492 struct scsi_device *sdev = sreq->sr_device;
493 const char spi_inquiry[] = {
494 INQUIRY, 0, 0, 0, len, 0
497 for (r = 0; r < retries; r++) {
498 sreq->sr_cmd_len = 0; /* wait_req to fill in */
499 sreq->sr_data_direction = DMA_FROM_DEVICE;
503 scsi_wait_req(sreq, spi_inquiry, ptr, len,
504 DV_TIMEOUT, DV_RETRIES);
506 if(sreq->sr_result || !scsi_device_online(sdev)) {
507 scsi_device_set_state(sdev, SDEV_QUIESCE);
508 return SPI_COMPARE_FAILURE;
511 /* If we don't have the inquiry data already, the
512 * first read gets it */
519 if (memcmp(buffer, ptr, len) != 0)
521 return SPI_COMPARE_FAILURE;
523 return SPI_COMPARE_SUCCESS;
526 static enum spi_compare_returns
527 spi_dv_retrain(struct scsi_request *sreq, u8 *buffer, u8 *ptr,
528 enum spi_compare_returns
529 (*compare_fn)(struct scsi_request *, u8 *, u8 *, int))
531 struct spi_internal *i = to_spi_internal(sreq->sr_host->transportt);
532 struct scsi_device *sdev = sreq->sr_device;
533 int period = 0, prevperiod = 0;
534 enum spi_compare_returns retval;
539 retval = compare_fn(sreq, buffer, ptr, DV_LOOPS);
541 if (retval == SPI_COMPARE_SUCCESS
542 || retval == SPI_COMPARE_SKIP_TEST)
545 /* OK, retrain, fallback */
546 if (i->f->get_period)
547 i->f->get_period(sdev->sdev_target);
548 newperiod = spi_period(sdev->sdev_target);
549 period = newperiod > period ? newperiod : period;
553 period += period >> 1;
555 if (unlikely(period > 0xff || period == prevperiod)) {
556 /* Total failure; set to async and return */
557 SPI_PRINTK(sdev->sdev_target, KERN_ERR, "Domain Validation Failure, dropping back to Asynchronous\n");
559 return SPI_COMPARE_FAILURE;
561 SPI_PRINTK(sdev->sdev_target, KERN_ERR, "Domain Validation detected failure, dropping back\n");
562 DV_SET(period, period);
569 spi_dv_device_get_echo_buffer(struct scsi_request *sreq, u8 *buffer)
573 /* first off do a test unit ready. This can error out
574 * because of reservations or some other reason. If it
575 * fails, the device won't let us write to the echo buffer
576 * so just return failure */
578 const char spi_test_unit_ready[] = {
579 TEST_UNIT_READY, 0, 0, 0, 0, 0
582 const char spi_read_buffer_descriptor[] = {
583 READ_BUFFER, 0x0b, 0, 0, 0, 0, 0, 0, 4, 0
587 sreq->sr_cmd_len = 0;
588 sreq->sr_data_direction = DMA_NONE;
590 /* We send a set of three TURs to clear any outstanding
591 * unit attention conditions if they exist (Otherwise the
592 * buffer tests won't be happy). If the TUR still fails
593 * (reservation conflict, device not ready, etc) just
594 * skip the write tests */
596 scsi_wait_req(sreq, spi_test_unit_ready, NULL, 0,
597 DV_TIMEOUT, DV_RETRIES);
599 if(sreq->sr_result) {
608 sreq->sr_cmd_len = 0;
609 sreq->sr_data_direction = DMA_FROM_DEVICE;
611 scsi_wait_req(sreq, spi_read_buffer_descriptor, buffer, 4,
612 DV_TIMEOUT, DV_RETRIES);
615 /* Device has no echo buffer */
618 return buffer[3] + ((buffer[2] & 0x1f) << 8);
622 spi_dv_device_internal(struct scsi_request *sreq, u8 *buffer)
624 struct spi_internal *i = to_spi_internal(sreq->sr_host->transportt);
625 struct scsi_device *sdev = sreq->sr_device;
626 int len = sdev->inquiry_len;
627 /* first set us up for narrow async */
631 if (spi_dv_device_compare_inquiry(sreq, buffer, buffer, DV_LOOPS)
632 != SPI_COMPARE_SUCCESS) {
633 SPI_PRINTK(sdev->sdev_target, KERN_ERR, "Domain Validation Initial Inquiry Failed\n");
634 /* FIXME: should probably offline the device here? */
639 if (i->f->set_width && sdev->wdtr) {
640 i->f->set_width(sdev->sdev_target, 1);
642 if (spi_dv_device_compare_inquiry(sreq, buffer,
645 != SPI_COMPARE_SUCCESS) {
646 SPI_PRINTK(sdev->sdev_target, KERN_ERR, "Wide Transfers Fail\n");
647 i->f->set_width(sdev->sdev_target, 0);
651 if (!i->f->set_period)
654 /* device can't handle synchronous */
655 if(!sdev->ppr && !sdev->sdtr)
658 /* see if the device has an echo buffer. If it does we can
659 * do the SPI pattern write tests */
663 len = spi_dv_device_get_echo_buffer(sreq, buffer);
667 /* now set up to the maximum */
672 SPI_PRINTK(sdev->sdev_target, KERN_INFO, "Domain Validation skipping write tests\n");
673 spi_dv_retrain(sreq, buffer, buffer + len,
674 spi_dv_device_compare_inquiry);
678 if (len > SPI_MAX_ECHO_BUFFER_SIZE) {
679 SPI_PRINTK(sdev->sdev_target, KERN_WARNING, "Echo buffer size %d is too big, trimming to %d\n", len, SPI_MAX_ECHO_BUFFER_SIZE);
680 len = SPI_MAX_ECHO_BUFFER_SIZE;
683 if (spi_dv_retrain(sreq, buffer, buffer + len,
684 spi_dv_device_echo_buffer)
685 == SPI_COMPARE_SKIP_TEST) {
686 /* OK, the stupid drive can't do a write echo buffer
687 * test after all, fall back to the read tests */
694 /** spi_dv_device - Do Domain Validation on the device
695 * @sdev: scsi device to validate
697 * Performs the domain validation on the given device in the
698 * current execution thread. Since DV operations may sleep,
699 * the current thread must have user context. Also no SCSI
700 * related locks that would deadlock I/O issued by the DV may
704 spi_dv_device(struct scsi_device *sdev)
706 struct scsi_request *sreq = scsi_allocate_request(sdev, GFP_KERNEL);
707 struct scsi_target *starget = sdev->sdev_target;
709 const int len = SPI_MAX_ECHO_BUFFER_SIZE*2;
714 if (unlikely(scsi_device_get(sdev)))
717 buffer = kmalloc(len, GFP_KERNEL);
719 if (unlikely(!buffer))
722 memset(buffer, 0, len);
724 /* We need to verify that the actual device will quiesce; the
725 * later target quiesce is just a nice to have */
726 if (unlikely(scsi_device_quiesce(sdev)))
729 scsi_target_quiesce(starget);
731 spi_dv_pending(starget) = 1;
732 down(&spi_dv_sem(starget));
734 SPI_PRINTK(starget, KERN_INFO, "Beginning Domain Validation\n");
736 spi_dv_device_internal(sreq, buffer);
738 SPI_PRINTK(starget, KERN_INFO, "Ending Domain Validation\n");
740 up(&spi_dv_sem(starget));
741 spi_dv_pending(starget) = 0;
743 scsi_target_resume(starget);
745 spi_initial_dv(starget) = 1;
750 scsi_device_put(sdev);
752 scsi_release_request(sreq);
754 EXPORT_SYMBOL(spi_dv_device);
756 struct work_queue_wrapper {
757 struct work_struct work;
758 struct scsi_device *sdev;
762 spi_dv_device_work_wrapper(void *data)
764 struct work_queue_wrapper *wqw = (struct work_queue_wrapper *)data;
765 struct scsi_device *sdev = wqw->sdev;
769 spi_dv_pending(sdev->sdev_target) = 0;
770 scsi_device_put(sdev);
775 * spi_schedule_dv_device - schedule domain validation to occur on the device
776 * @sdev: The device to validate
778 * Identical to spi_dv_device() above, except that the DV will be
779 * scheduled to occur in a workqueue later. All memory allocations
780 * are atomic, so may be called from any context including those holding
784 spi_schedule_dv_device(struct scsi_device *sdev)
786 struct work_queue_wrapper *wqw =
787 kmalloc(sizeof(struct work_queue_wrapper), GFP_ATOMIC);
792 if (unlikely(spi_dv_pending(sdev->sdev_target))) {
796 /* Set pending early (dv_device doesn't check it, only sets it) */
797 spi_dv_pending(sdev->sdev_target) = 1;
798 if (unlikely(scsi_device_get(sdev))) {
800 spi_dv_pending(sdev->sdev_target) = 0;
804 INIT_WORK(&wqw->work, spi_dv_device_work_wrapper, wqw);
807 schedule_work(&wqw->work);
809 EXPORT_SYMBOL(spi_schedule_dv_device);
812 * spi_display_xfer_agreement - Print the current target transfer agreement
813 * @starget: The target for which to display the agreement
815 * Each SPI port is required to maintain a transfer agreement for each
816 * other port on the bus. This function prints a one-line summary of
817 * the current agreement; more detailed information is available in sysfs.
819 void spi_display_xfer_agreement(struct scsi_target *starget)
821 struct spi_transport_attrs *tp;
822 tp = (struct spi_transport_attrs *)&starget->starget_data;
824 if (tp->offset > 0 && tp->period > 0) {
825 unsigned int picosec, kb100;
826 char *scsi = "FAST-?";
829 if (tp->period <= SPI_STATIC_PPR) {
830 picosec = ppr_to_ps[tp->period];
831 switch (tp->period) {
832 case 7: scsi = "FAST-320"; break;
833 case 8: scsi = "FAST-160"; break;
834 case 9: scsi = "FAST-80"; break;
836 case 11: scsi = "FAST-40"; break;
837 case 12: scsi = "FAST-20"; break;
840 picosec = tp->period * 4000;
843 else if (tp->period < 50)
849 kb100 = (10000000 + picosec / 2) / picosec;
852 sprint_frac(tmp, picosec, 1000);
854 dev_info(&starget->dev,
855 "%s %sSCSI %d.%d MB/s %s%s%s (%s ns, offset %d)\n",
856 scsi, tp->width ? "WIDE " : "", kb100/10, kb100 % 10,
857 tp->dt ? "DT" : "ST", tp->iu ? " IU" : "",
858 tp->qas ? " QAS" : "", tmp, tp->offset);
860 dev_info(&starget->dev, "%sasynchronous.\n",
861 tp->width ? "wide " : "");
864 EXPORT_SYMBOL(spi_display_xfer_agreement);
866 #define SETUP_ATTRIBUTE(field) \
867 i->private_attrs[count] = class_device_attr_##field; \
868 if (!i->f->set_##field) { \
869 i->private_attrs[count].attr.mode = S_IRUGO; \
870 i->private_attrs[count].store = NULL; \
872 i->attrs[count] = &i->private_attrs[count]; \
873 if (i->f->show_##field) \
876 #define SETUP_HOST_ATTRIBUTE(field) \
877 i->private_host_attrs[count] = class_device_attr_##field; \
878 if (!i->f->set_##field) { \
879 i->private_host_attrs[count].attr.mode = S_IRUGO; \
880 i->private_host_attrs[count].store = NULL; \
882 i->host_attrs[count] = &i->private_host_attrs[count]; \
885 static int spi_device_match(struct attribute_container *cont,
888 struct scsi_device *sdev;
889 struct Scsi_Host *shost;
891 if (!scsi_is_sdev_device(dev))
894 sdev = to_scsi_device(dev);
896 if (!shost->transportt || shost->transportt->host_attrs.ac.class
897 != &spi_host_class.class)
899 /* Note: this class has no device attributes, so it has
900 * no per-HBA allocation and thus we don't need to distinguish
901 * the attribute containers for the device */
905 static int spi_target_match(struct attribute_container *cont,
908 struct Scsi_Host *shost;
909 struct spi_internal *i;
911 if (!scsi_is_target_device(dev))
914 shost = dev_to_shost(dev->parent);
915 if (!shost->transportt || shost->transportt->host_attrs.ac.class
916 != &spi_host_class.class)
919 i = to_spi_internal(shost->transportt);
921 return &i->t.target_attrs.ac == cont;
924 static DECLARE_TRANSPORT_CLASS(spi_transport_class,
926 spi_setup_transport_attrs,
930 static DECLARE_ANON_TRANSPORT_CLASS(spi_device_class,
932 spi_device_configure);
934 struct scsi_transport_template *
935 spi_attach_transport(struct spi_function_template *ft)
937 struct spi_internal *i = kmalloc(sizeof(struct spi_internal),
943 memset(i, 0, sizeof(struct spi_internal));
946 i->t.target_attrs.ac.class = &spi_transport_class.class;
947 i->t.target_attrs.ac.attrs = &i->attrs[0];
948 i->t.target_attrs.ac.match = spi_target_match;
949 transport_container_register(&i->t.target_attrs);
950 i->t.target_size = sizeof(struct spi_transport_attrs);
951 i->t.host_attrs.ac.class = &spi_host_class.class;
952 i->t.host_attrs.ac.attrs = &i->host_attrs[0];
953 i->t.host_attrs.ac.match = spi_host_match;
954 transport_container_register(&i->t.host_attrs);
955 i->t.host_size = sizeof(struct spi_host_attrs);
958 SETUP_ATTRIBUTE(period);
959 SETUP_ATTRIBUTE(offset);
960 SETUP_ATTRIBUTE(width);
963 SETUP_ATTRIBUTE(qas);
964 SETUP_ATTRIBUTE(wr_flow);
965 SETUP_ATTRIBUTE(rd_strm);
966 SETUP_ATTRIBUTE(rti);
967 SETUP_ATTRIBUTE(pcomp_en);
969 /* if you add an attribute but forget to increase SPI_NUM_ATTRS
970 * this bug will trigger */
971 BUG_ON(count > SPI_NUM_ATTRS);
973 i->attrs[count++] = &class_device_attr_revalidate;
975 i->attrs[count] = NULL;
978 SETUP_HOST_ATTRIBUTE(signalling);
980 BUG_ON(count > SPI_HOST_ATTRS);
982 i->host_attrs[count] = NULL;
986 EXPORT_SYMBOL(spi_attach_transport);
988 void spi_release_transport(struct scsi_transport_template *t)
990 struct spi_internal *i = to_spi_internal(t);
992 transport_container_unregister(&i->t.target_attrs);
993 transport_container_unregister(&i->t.host_attrs);
997 EXPORT_SYMBOL(spi_release_transport);
999 static __init int spi_transport_init(void)
1001 int error = transport_class_register(&spi_transport_class);
1004 error = anon_transport_class_register(&spi_device_class);
1005 return transport_class_register(&spi_host_class);
1008 static void __exit spi_transport_exit(void)
1010 transport_class_unregister(&spi_transport_class);
1011 anon_transport_class_unregister(&spi_device_class);
1012 transport_class_unregister(&spi_host_class);
1015 MODULE_AUTHOR("Martin Hicks");
1016 MODULE_DESCRIPTION("SPI Transport Attributes");
1017 MODULE_LICENSE("GPL");
1019 module_init(spi_transport_init);
1020 module_exit(spi_transport_exit);