]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/scsi/scsi_transport_spi.c
bc12b5d5d676df7ab733fd23ea0ef8e929241363
[linux-2.6-omap-h63xx.git] / drivers / scsi / scsi_transport_spi.c
1 /* 
2  *  Parallel SCSI (SPI) transport specific attributes exported to sysfs.
3  *
4  *  Copyright (c) 2003 Silicon Graphics, Inc.  All rights reserved.
5  *  Copyright (c) 2004, 2005 James Bottomley <James.Bottomley@SteelEye.com>
6  *
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.
11  *
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.
16  *
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
20  */
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>
35
36 #define SPI_NUM_ATTRS 14        /* increase this if you add attributes */
37 #define SPI_OTHER_ATTRS 1       /* Increase this if you add "always
38                                  * on" attributes */
39 #define SPI_HOST_ATTRS  1
40
41 #define SPI_MAX_ECHO_BUFFER_SIZE        4096
42
43 #define DV_LOOPS        3
44 #define DV_TIMEOUT      (10*HZ)
45 #define DV_RETRIES      3       /* should only need at most 
46                                  * two cc/ua clears */
47
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)
51
52 struct spi_internal {
53         struct scsi_transport_template t;
54         struct spi_function_template *f;
55 };
56
57 #define to_spi_internal(tmpl)   container_of(tmpl, struct spi_internal, t)
58
59 static const int ppr_to_ps[] = {
60         /* The PPR values 0-6 are reserved, fill them in when
61          * the committee defines them */
62         -1,                     /* 0x00 */
63         -1,                     /* 0x01 */
64         -1,                     /* 0x02 */
65         -1,                     /* 0x03 */
66         -1,                     /* 0x04 */
67         -1,                     /* 0x05 */
68         -1,                     /* 0x06 */
69          3125,                  /* 0x07 */
70          6250,                  /* 0x08 */
71         12500,                  /* 0x09 */
72         25000,                  /* 0x0a */
73         30300,                  /* 0x0b */
74         50000,                  /* 0x0c */
75 };
76 /* The PPR values at which you calculate the period in ns by multiplying
77  * by 4 */
78 #define SPI_STATIC_PPR  0x0c
79
80 static int sprint_frac(char *dest, int value, int denom)
81 {
82         int frac = value % denom;
83         int result = sprintf(dest, "%d", value / denom);
84
85         if (frac == 0)
86                 return result;
87         dest[result++] = '.';
88
89         do {
90                 denom /= 10;
91                 sprintf(dest + result, "%d", frac / denom);
92                 result++;
93                 frac %= denom;
94         } while (frac);
95
96         dest[result++] = '\0';
97         return result;
98 }
99
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)
104 {
105         int i, result;
106         unsigned char sense[SCSI_SENSE_BUFFERSIZE];
107
108         for(i = 0; i < DV_RETRIES; i++) {
109                 result = scsi_execute(sdev, cmd, dir, buffer, bufflen,
110                                       sense, DV_TIMEOUT, /* retries */ 1,
111                                       REQ_FAILFAST);
112                 if (result & DRIVER_SENSE) {
113                         struct scsi_sense_hdr sshdr_tmp;
114                         if (!sshdr)
115                                 sshdr = &sshdr_tmp;
116
117                         if (scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE,
118                                                  sshdr)
119                             && sshdr->sense_key == UNIT_ATTENTION)
120                                 continue;
121                 }
122                 break;
123         }
124         return result;
125 }
126
127 static struct {
128         enum spi_signal_type    value;
129         char                    *name;
130 } signal_types[] = {
131         { SPI_SIGNAL_UNKNOWN, "unknown" },
132         { SPI_SIGNAL_SE, "SE" },
133         { SPI_SIGNAL_LVD, "LVD" },
134         { SPI_SIGNAL_HVD, "HVD" },
135 };
136
137 static inline const char *spi_signal_to_string(enum spi_signal_type type)
138 {
139         int i;
140
141         for (i = 0; i < ARRAY_SIZE(signal_types); i++) {
142                 if (type == signal_types[i].value)
143                         return signal_types[i].name;
144         }
145         return NULL;
146 }
147 static inline enum spi_signal_type spi_signal_to_value(const char *name)
148 {
149         int i, len;
150
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;
156         }
157         return SPI_SIGNAL_UNKNOWN;
158 }
159
160 static int spi_host_setup(struct transport_container *tc, struct device *dev,
161                           struct device *cdev)
162 {
163         struct Scsi_Host *shost = dev_to_shost(dev);
164
165         spi_signalling(shost) = SPI_SIGNAL_UNKNOWN;
166
167         return 0;
168 }
169
170 static int spi_host_configure(struct transport_container *tc,
171                               struct device *dev,
172                               struct device *cdev);
173
174 static DECLARE_TRANSPORT_CLASS(spi_host_class,
175                                "spi_host",
176                                spi_host_setup,
177                                NULL,
178                                spi_host_configure);
179
180 static int spi_host_match(struct attribute_container *cont,
181                           struct device *dev)
182 {
183         struct Scsi_Host *shost;
184
185         if (!scsi_is_host_device(dev))
186                 return 0;
187
188         shost = dev_to_shost(dev);
189         if (!shost->transportt  || shost->transportt->host_attrs.ac.class
190             != &spi_host_class.class)
191                 return 0;
192
193         return &shost->transportt->host_attrs.ac == cont;
194 }
195
196 static int spi_target_configure(struct transport_container *tc,
197                                 struct device *dev,
198                                 struct device *cdev);
199
200 static int spi_device_configure(struct transport_container *tc,
201                                 struct device *dev,
202                                 struct device *cdev)
203 {
204         struct scsi_device *sdev = to_scsi_device(dev);
205         struct scsi_target *starget = sdev->sdev_target;
206
207         /* Populate the target capability fields with the values
208          * gleaned from the device inquiry */
209
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);
216
217         return 0;
218 }
219
220 static int spi_setup_transport_attrs(struct transport_container *tc,
221                                      struct device *dev,
222                                      struct device *cdev)
223 {
224         struct scsi_target *starget = to_scsi_target(dev);
225
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));
244
245         return 0;
246 }
247
248 #define spi_transport_show_simple(field, format_string)                 \
249                                                                         \
250 static ssize_t                                                          \
251 show_spi_transport_##field(struct device *dev,                  \
252                            struct device_attribute *attr, char *buf)    \
253 {                                                                       \
254         struct scsi_target *starget = transport_class_to_starget(dev);  \
255         struct spi_transport_attrs *tp;                                 \
256                                                                         \
257         tp = (struct spi_transport_attrs *)&starget->starget_data;      \
258         return snprintf(buf, 20, format_string, tp->field);             \
259 }
260
261 #define spi_transport_store_simple(field, format_string)                \
262                                                                         \
263 static ssize_t                                                          \
264 store_spi_transport_##field(struct device *dev,                         \
265                             struct device_attribute *attr,              \
266                             const char *buf, size_t count)              \
267 {                                                                       \
268         int val;                                                        \
269         struct scsi_target *starget = transport_class_to_starget(dev);  \
270         struct spi_transport_attrs *tp;                                 \
271                                                                         \
272         tp = (struct spi_transport_attrs *)&starget->starget_data;      \
273         val = simple_strtoul(buf, NULL, 0);                             \
274         tp->field = val;                                                \
275         return count;                                                   \
276 }
277
278 #define spi_transport_show_function(field, format_string)               \
279                                                                         \
280 static ssize_t                                                          \
281 show_spi_transport_##field(struct device *dev,                  \
282                            struct device_attribute *attr, char *buf)    \
283 {                                                                       \
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);             \
292 }
293
294 #define spi_transport_store_function(field, format_string)              \
295 static ssize_t                                                          \
296 store_spi_transport_##field(struct device *dev,                         \
297                             struct device_attribute *attr,              \
298                             const char *buf, size_t count)              \
299 {                                                                       \
300         int val;                                                        \
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);    \
304                                                                         \
305         if (!i->f->set_##field)                                         \
306                 return -EINVAL;                                         \
307         val = simple_strtoul(buf, NULL, 0);                             \
308         i->f->set_##field(starget, val);                                \
309         return count;                                                   \
310 }
311
312 #define spi_transport_store_max(field, format_string)                   \
313 static ssize_t                                                          \
314 store_spi_transport_##field(struct device *dev,                         \
315                             struct device_attribute *attr,              \
316                             const char *buf, size_t count)              \
317 {                                                                       \
318         int val;                                                        \
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; \
324                                                                         \
325         if (i->f->set_##field)                                          \
326                 return -EINVAL;                                         \
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);                                \
331         return count;                                                   \
332 }
333
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);
340
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);
347
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);
355
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");
367
368 /* we only care about the first child device so we return 1 */
369 static int child_iter(struct device *dev, void *data)
370 {
371         struct scsi_device *sdev = to_scsi_device(dev);
372
373         spi_dv_device(sdev);
374         return 1;
375 }
376
377 static ssize_t
378 store_spi_revalidate(struct device *dev, struct device_attribute *attr,
379                      const char *buf, size_t count)
380 {
381         struct scsi_target *starget = transport_class_to_starget(dev);
382
383         device_for_each_child(&starget->dev, NULL, child_iter);
384         return count;
385 }
386 static DEVICE_ATTR(revalidate, S_IWUSR, NULL, store_spi_revalidate);
387
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)
391 {
392         int len, picosec;
393
394         if (period < 0 || period > 0xff) {
395                 picosec = -1;
396         } else if (period <= SPI_STATIC_PPR) {
397                 picosec = ppr_to_ps[period];
398         } else {
399                 picosec = period * 4000;
400         }
401
402         if (picosec == -1) {
403                 len = sprintf(buf, "reserved");
404         } else {
405                 len = sprint_frac(buf, picosec, 1000);
406         }
407
408         return len;
409 }
410
411 static ssize_t
412 show_spi_transport_period_helper(char *buf, int period)
413 {
414         int len = period_to_str(buf, period);
415         buf[len++] = '\n';
416         buf[len] = '\0';
417         return len;
418 }
419
420 static ssize_t
421 store_spi_transport_period_helper(struct device *dev, const char *buf,
422                                   size_t count, int *periodp)
423 {
424         int j, picosec, period = -1;
425         char *endp;
426
427         picosec = simple_strtoul(buf, &endp, 10) * 1000;
428         if (*endp == '.') {
429                 int mult = 100;
430                 do {
431                         endp++;
432                         if (!isdigit(*endp))
433                                 break;
434                         picosec += (*endp - '0') * mult;
435                         mult /= 10;
436                 } while (mult > 0);
437         }
438
439         for (j = 0; j <= SPI_STATIC_PPR; j++) {
440                 if (ppr_to_ps[j] < picosec)
441                         continue;
442                 period = j;
443                 break;
444         }
445
446         if (period == -1)
447                 period = picosec / 4000;
448
449         if (period > 0xff)
450                 period = 0xff;
451
452         *periodp = period;
453
454         return count;
455 }
456
457 static ssize_t
458 show_spi_transport_period(struct device *dev,
459                           struct device_attribute *attr, char *buf)
460 {
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;
466
467         if (i->f->get_period)
468                 i->f->get_period(starget);
469
470         return show_spi_transport_period_helper(buf, tp->period);
471 }
472
473 static ssize_t
474 store_spi_transport_period(struct device *cdev, struct device_attribute *attr,
475                            const char *buf, size_t count)
476 {
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;
482         int period, retval;
483
484         if (!i->f->set_period)
485                 return -EINVAL;
486
487         retval = store_spi_transport_period_helper(cdev, buf, count, &period);
488
489         if (period < tp->min_period)
490                 period = tp->min_period;
491
492         i->f->set_period(starget, period);
493
494         return retval;
495 }
496
497 static DEVICE_ATTR(period, S_IRUGO,
498                    show_spi_transport_period,
499                    store_spi_transport_period);
500
501 static ssize_t
502 show_spi_transport_min_period(struct device *cdev,
503                               struct device_attribute *attr, char *buf)
504 {
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;
510
511         if (!i->f->set_period)
512                 return -EINVAL;
513
514         return show_spi_transport_period_helper(buf, tp->min_period);
515 }
516
517 static ssize_t
518 store_spi_transport_min_period(struct device *cdev,
519                                struct device_attribute *attr,
520                                const char *buf, size_t count)
521 {
522         struct scsi_target *starget = transport_class_to_starget(cdev);
523         struct spi_transport_attrs *tp =
524                 (struct spi_transport_attrs *)&starget->starget_data;
525
526         return store_spi_transport_period_helper(cdev, buf, count,
527                                                  &tp->min_period);
528 }
529
530
531 static DEVICE_ATTR(min_period, S_IRUGO,
532                    show_spi_transport_min_period,
533                    store_spi_transport_min_period);
534
535
536 static ssize_t show_spi_host_signalling(struct device *cdev,
537                                         struct device_attribute *attr,
538                                         char *buf)
539 {
540         struct Scsi_Host *shost = transport_class_to_shost(cdev);
541         struct spi_internal *i = to_spi_internal(shost->transportt);
542
543         if (i->f->get_signalling)
544                 i->f->get_signalling(shost);
545
546         return sprintf(buf, "%s\n", spi_signal_to_string(spi_signalling(shost)));
547 }
548 static ssize_t store_spi_host_signalling(struct device *dev,
549                                          struct device_attribute *attr,
550                                          const char *buf, size_t count)
551 {
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);
555
556         if (!i->f->set_signalling)
557                 return -EINVAL;
558
559         if (type != SPI_SIGNAL_UNKNOWN)
560                 i->f->set_signalling(shost, type);
561
562         return count;
563 }
564 static DEVICE_ATTR(signalling, S_IRUGO,
565                    show_spi_host_signalling,
566                    store_spi_host_signalling);
567
568 #define DV_SET(x, y)                    \
569         if(i->f->set_##x)               \
570                 i->f->set_##x(sdev->sdev_target, y)
571
572 enum spi_compare_returns {
573         SPI_COMPARE_SUCCESS,
574         SPI_COMPARE_FAILURE,
575         SPI_COMPARE_SKIP_TEST,
576 };
577
578
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)
584 {
585         int len = ptr - buffer;
586         int j, k, r, result;
587         unsigned int pattern = 0x0000ffff;
588         struct scsi_sense_hdr sshdr;
589
590         const char spi_write_buffer[] = {
591                 WRITE_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
592         };
593         const char spi_read_buffer[] = {
594                 READ_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
595         };
596
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; ) {
600
601                 /* fill the buffer with counting (test a) */
602                 for ( ; j < min(len, 32); j++)
603                         buffer[j] = j;
604                 k = j;
605                 /* fill the buffer with alternating words of 0x0 and
606                  * 0xffff (test b) */
607                 for ( ; j < min(len, k + 32); j += 2) {
608                         u16 *word = (u16 *)&buffer[j];
609                         
610                         *word = (j & 0x02) ? 0x0000 : 0xffff;
611                 }
612                 k = j;
613                 /* fill with crosstalk (alternating 0x5555 0xaaa)
614                  * (test c) */
615                 for ( ; j < min(len, k + 32); j += 2) {
616                         u16 *word = (u16 *)&buffer[j];
617
618                         *word = (j & 0x02) ? 0x5555 : 0xaaaa;
619                 }
620                 k = j;
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;
625                         
626                         *word = pattern;
627                         pattern = (pattern << 1) | roll;
628                 }
629                 /* don't bother with random data (test e) */
630         }
631
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)) {
636
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)
646                                  */
647                                 return SPI_COMPARE_SKIP_TEST;
648
649
650                         sdev_printk(KERN_ERR, sdev, "Write Buffer failure %x\n", result);
651                         return SPI_COMPARE_FAILURE;
652                 }
653
654                 memset(ptr, 0, len);
655                 spi_execute(sdev, spi_read_buffer, DMA_FROM_DEVICE,
656                             ptr, len, NULL);
657                 scsi_device_set_state(sdev, SDEV_QUIESCE);
658
659                 if (memcmp(buffer, ptr, len) != 0)
660                         return SPI_COMPARE_FAILURE;
661         }
662         return SPI_COMPARE_SUCCESS;
663 }
664
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)
670 {
671         int r, result;
672         const int len = sdev->inquiry_len;
673         const char spi_inquiry[] = {
674                 INQUIRY, 0, 0, 0, len, 0
675         };
676
677         for (r = 0; r < retries; r++) {
678                 memset(ptr, 0, len);
679
680                 result = spi_execute(sdev, spi_inquiry, DMA_FROM_DEVICE,
681                                      ptr, len, NULL);
682                 
683                 if(result || !scsi_device_online(sdev)) {
684                         scsi_device_set_state(sdev, SDEV_QUIESCE);
685                         return SPI_COMPARE_FAILURE;
686                 }
687
688                 /* If we don't have the inquiry data already, the
689                  * first read gets it */
690                 if (ptr == buffer) {
691                         ptr += len;
692                         --r;
693                         continue;
694                 }
695
696                 if (memcmp(buffer, ptr, len) != 0)
697                         /* failure */
698                         return SPI_COMPARE_FAILURE;
699         }
700         return SPI_COMPARE_SUCCESS;
701 }
702
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))
707 {
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;
712
713
714         for (;;) {
715                 int newperiod;
716                 retval = compare_fn(sdev, buffer, ptr, DV_LOOPS);
717
718                 if (retval == SPI_COMPARE_SUCCESS
719                     || retval == SPI_COMPARE_SKIP_TEST)
720                         break;
721
722                 /* OK, retrain, fallback */
723                 if (i->f->get_iu)
724                         i->f->get_iu(starget);
725                 if (i->f->get_qas)
726                         i->f->get_qas(starget);
727                 if (i->f->get_period)
728                         i->f->get_period(sdev->sdev_target);
729
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");
735                         DV_SET(iu, 0);
736                 } else if (i->f->set_qas && spi_qas(starget)) {
737                         starget_printk(KERN_ERR, starget, "Domain Validation Disabing Quick Arbitration and Selection\n");
738                         DV_SET(qas, 0);
739                 } else {
740                         newperiod = spi_period(starget);
741                         period = newperiod > period ? newperiod : period;
742                         if (period < 0x0d)
743                                 period++;
744                         else
745                                 period += period >> 1;
746
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");
750                                 DV_SET(offset, 0);
751                                 return SPI_COMPARE_FAILURE;
752                         }
753                         starget_printk(KERN_ERR, starget, "Domain Validation detected failure, dropping back\n");
754                         DV_SET(period, period);
755                         prevperiod = period;
756                 }
757         }
758         return retval;
759 }
760
761 static int
762 spi_dv_device_get_echo_buffer(struct scsi_device *sdev, u8 *buffer)
763 {
764         int l, result;
765
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 */
770         
771         const char spi_test_unit_ready[] = {
772                 TEST_UNIT_READY, 0, 0, 0, 0, 0
773         };
774
775         const char spi_read_buffer_descriptor[] = {
776                 READ_BUFFER, 0x0b, 0, 0, 0, 0, 0, 0, 4, 0
777         };
778
779         
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 */
785         for (l = 0; ; l++) {
786                 result = spi_execute(sdev, spi_test_unit_ready, DMA_NONE, 
787                                      NULL, 0, NULL);
788
789                 if(result) {
790                         if(l >= 3)
791                                 return 0;
792                 } else {
793                         /* TUR succeeded */
794                         break;
795                 }
796         }
797
798         result = spi_execute(sdev, spi_read_buffer_descriptor, 
799                              DMA_FROM_DEVICE, buffer, 4, NULL);
800
801         if (result)
802                 /* Device has no echo buffer */
803                 return 0;
804
805         return buffer[3] + ((buffer[2] & 0x1f) << 8);
806 }
807
808 static void
809 spi_dv_device_internal(struct scsi_device *sdev, u8 *buffer)
810 {
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 */
818         DV_SET(offset, 0);
819         DV_SET(width, 0);
820
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? */
825                 return;
826         }
827
828         if (!scsi_device_wide(sdev)) {
829                 spi_max_width(starget) = 0;
830                 max_width = 0;
831         }
832
833         /* test width */
834         if (i->f->set_width && max_width) {
835                 i->f->set_width(starget, 1);
836
837                 if (spi_dv_device_compare_inquiry(sdev, buffer,
838                                                    buffer + len,
839                                                    DV_LOOPS)
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 */
845                         max_width = 0;
846                         if (min_period < 10)
847                                 min_period = 10;
848                 }
849         }
850
851         if (!i->f->set_period)
852                 return;
853
854         /* device can't handle synchronous */
855         if (!scsi_device_sync(sdev) && !scsi_device_dt(sdev))
856                 return;
857
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 */
861         len = -1;
862
863  retry:
864
865         /* now set up to the maximum */
866         DV_SET(offset, spi_max_offset(starget));
867         DV_SET(period, min_period);
868
869         /* try QAS requests; this should be harmless to set if the
870          * target supports it */
871         if (scsi_device_qas(sdev)) {
872                 DV_SET(qas, 1);
873         } else {
874                 DV_SET(qas, 0);
875         }
876
877         if (scsi_device_ius(sdev) && min_period < 9) {
878                 /* This u320 (or u640). Set IU transfers */
879                 DV_SET(iu, 1);
880                 /* Then set the optional parameters */
881                 DV_SET(rd_strm, 1);
882                 DV_SET(wr_flow, 1);
883                 DV_SET(rti, 1);
884                 if (min_period == 8)
885                         DV_SET(pcomp_en, 1);
886         } else {
887                 DV_SET(iu, 0);
888         }
889
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)) {
898                 DV_SET(dt, 0);
899         } else {
900                 DV_SET(dt, 1);
901         }
902         /* set width last because it will pull all the other
903          * parameters down to required values */
904         DV_SET(width, max_width);
905
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 */
910         if (i->f->get_dt)
911                 i->f->get_dt(starget);
912
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
916          * negotiated DT */
917
918         if (len == -1 && spi_dt(starget))
919                 len = spi_dv_device_get_echo_buffer(sdev, buffer);
920
921         if (len <= 0) {
922                 starget_printk(KERN_INFO, starget, "Domain Validation skipping write tests\n");
923                 return;
924         }
925
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;
929         }
930
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 */
936                 len = 0;
937                 goto retry;
938         }
939 }
940
941
942 /**     spi_dv_device - Do Domain Validation on the device
943  *      @sdev:          scsi device to validate
944  *
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
949  *      be held.
950  */
951 void
952 spi_dv_device(struct scsi_device *sdev)
953 {
954         struct scsi_target *starget = sdev->sdev_target;
955         u8 *buffer;
956         const int len = SPI_MAX_ECHO_BUFFER_SIZE*2;
957
958         if (unlikely(scsi_device_get(sdev)))
959                 return;
960
961         if (unlikely(spi_dv_in_progress(starget)))
962                 return;
963         spi_dv_in_progress(starget) = 1;
964
965         buffer = kzalloc(len, GFP_KERNEL);
966
967         if (unlikely(!buffer))
968                 goto out_put;
969
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)))
973                 goto out_free;
974
975         scsi_target_quiesce(starget);
976
977         spi_dv_pending(starget) = 1;
978         mutex_lock(&spi_dv_mutex(starget));
979
980         starget_printk(KERN_INFO, starget, "Beginning Domain Validation\n");
981
982         spi_dv_device_internal(sdev, buffer);
983
984         starget_printk(KERN_INFO, starget, "Ending Domain Validation\n");
985
986         mutex_unlock(&spi_dv_mutex(starget));
987         spi_dv_pending(starget) = 0;
988
989         scsi_target_resume(starget);
990
991         spi_initial_dv(starget) = 1;
992
993  out_free:
994         kfree(buffer);
995  out_put:
996         spi_dv_in_progress(starget) = 0;
997         scsi_device_put(sdev);
998 }
999 EXPORT_SYMBOL(spi_dv_device);
1000
1001 struct work_queue_wrapper {
1002         struct work_struct      work;
1003         struct scsi_device      *sdev;
1004 };
1005
1006 static void
1007 spi_dv_device_work_wrapper(struct work_struct *work)
1008 {
1009         struct work_queue_wrapper *wqw =
1010                 container_of(work, struct work_queue_wrapper, work);
1011         struct scsi_device *sdev = wqw->sdev;
1012
1013         kfree(wqw);
1014         spi_dv_device(sdev);
1015         spi_dv_pending(sdev->sdev_target) = 0;
1016         scsi_device_put(sdev);
1017 }
1018
1019
1020 /**
1021  *      spi_schedule_dv_device - schedule domain validation to occur on the device
1022  *      @sdev:  The device to validate
1023  *
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
1027  *      SCSI locks.
1028  */
1029 void
1030 spi_schedule_dv_device(struct scsi_device *sdev)
1031 {
1032         struct work_queue_wrapper *wqw =
1033                 kmalloc(sizeof(struct work_queue_wrapper), GFP_ATOMIC);
1034
1035         if (unlikely(!wqw))
1036                 return;
1037
1038         if (unlikely(spi_dv_pending(sdev->sdev_target))) {
1039                 kfree(wqw);
1040                 return;
1041         }
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))) {
1045                 kfree(wqw);
1046                 spi_dv_pending(sdev->sdev_target) = 0;
1047                 return;
1048         }
1049
1050         INIT_WORK(&wqw->work, spi_dv_device_work_wrapper);
1051         wqw->sdev = sdev;
1052
1053         schedule_work(&wqw->work);
1054 }
1055 EXPORT_SYMBOL(spi_schedule_dv_device);
1056
1057 /**
1058  * spi_display_xfer_agreement - Print the current target transfer agreement
1059  * @starget: The target for which to display the agreement
1060  *
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.
1064  */
1065 void spi_display_xfer_agreement(struct scsi_target *starget)
1066 {
1067         struct spi_transport_attrs *tp;
1068         tp = (struct spi_transport_attrs *)&starget->starget_data;
1069
1070         if (tp->offset > 0 && tp->period > 0) {
1071                 unsigned int picosec, kb100;
1072                 char *scsi = "FAST-?";
1073                 char tmp[8];
1074
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;
1081                                 case 10:
1082                                 case 11: scsi = "FAST-40"; break;
1083                                 case 12: scsi = "FAST-20"; break;
1084                         }
1085                 } else {
1086                         picosec = tp->period * 4000;
1087                         if (tp->period < 25)
1088                                 scsi = "FAST-20";
1089                         else if (tp->period < 50)
1090                                 scsi = "FAST-10";
1091                         else
1092                                 scsi = "FAST-5";
1093                 }
1094
1095                 kb100 = (10000000 + picosec / 2) / picosec;
1096                 if (tp->width)
1097                         kb100 *= 2;
1098                 sprint_frac(tmp, picosec, 1000);
1099
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" : "",
1111                          tmp, tp->offset);
1112         } else {
1113                 dev_info(&starget->dev, "%sasynchronous\n",
1114                                 tp->width ? "wide " : "");
1115         }
1116 }
1117 EXPORT_SYMBOL(spi_display_xfer_agreement);
1118
1119 int spi_populate_width_msg(unsigned char *msg, int width)
1120 {
1121         msg[0] = EXTENDED_MESSAGE;
1122         msg[1] = 2;
1123         msg[2] = EXTENDED_WDTR;
1124         msg[3] = width;
1125         return 4;
1126 }
1127 EXPORT_SYMBOL_GPL(spi_populate_width_msg);
1128
1129 int spi_populate_sync_msg(unsigned char *msg, int period, int offset)
1130 {
1131         msg[0] = EXTENDED_MESSAGE;
1132         msg[1] = 3;
1133         msg[2] = EXTENDED_SDTR;
1134         msg[3] = period;
1135         msg[4] = offset;
1136         return 5;
1137 }
1138 EXPORT_SYMBOL_GPL(spi_populate_sync_msg);
1139
1140 int spi_populate_ppr_msg(unsigned char *msg, int period, int offset,
1141                 int width, int options)
1142 {
1143         msg[0] = EXTENDED_MESSAGE;
1144         msg[1] = 6;
1145         msg[2] = EXTENDED_PPR;
1146         msg[3] = period;
1147         msg[4] = 0;
1148         msg[5] = offset;
1149         msg[6] = width;
1150         msg[7] = options;
1151         return 8;
1152 }
1153 EXPORT_SYMBOL_GPL(spi_populate_ppr_msg);
1154
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"
1165 };
1166
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"
1170 };
1171
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"
1176 };
1177
1178 static void print_nego(const unsigned char *msg, int per, int off, int width)
1179 {
1180         if (per) {
1181                 char buf[20];
1182                 period_to_str(buf, msg[per]);
1183                 printk("period = %s ns ", buf);
1184         }
1185
1186         if (off)
1187                 printk("offset = %d ", msg[off]);
1188         if (width)
1189                 printk("width = %d ", 8 << msg[width]);
1190 }
1191
1192 static void print_ptr(const unsigned char *msg, int msb, const char *desc)
1193 {
1194         int ptr = (msg[msb] << 24) | (msg[msb+1] << 16) | (msg[msb+2] << 8) |
1195                         msg[msb+3];
1196         printk("%s = %d ", desc, ptr);
1197 }
1198
1199 int spi_print_msg(const unsigned char *msg)
1200 {
1201         int len = 1, i;
1202         if (msg[0] == EXTENDED_MESSAGE) {
1203                 len = 2 + msg[1];
1204                 if (len == 2)
1205                         len += 256;
1206                 if (msg[2] < ARRAY_SIZE(extended_msgs))
1207                         printk ("%s ", extended_msgs[msg[2]]); 
1208                 else 
1209                         printk ("Extended Message, reserved code (0x%02x) ",
1210                                 (int) msg[2]);
1211                 switch (msg[2]) {
1212                 case EXTENDED_MODIFY_DATA_POINTER:
1213                         print_ptr(msg, 3, "pointer");
1214                         break;
1215                 case EXTENDED_SDTR:
1216                         print_nego(msg, 3, 4, 0);
1217                         break;
1218                 case EXTENDED_WDTR:
1219                         print_nego(msg, 0, 0, 3);
1220                         break;
1221                 case EXTENDED_PPR:
1222                         print_nego(msg, 3, 5, 6);
1223                         break;
1224                 case EXTENDED_MODIFY_BIDI_DATA_PTR:
1225                         print_ptr(msg, 3, "out");
1226                         print_ptr(msg, 7, "in");
1227                         break;
1228                 default:
1229                 for (i = 2; i < len; ++i) 
1230                         printk("%02x ", msg[i]);
1231                 }
1232         /* Identify */
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",
1237                         msg[0] & 0x7);
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]]);
1242                 else
1243                         printk("reserved (%02x) ", msg[0]);
1244         } else if (msg[0] == 0x55) {
1245                 printk("QAS Request ");
1246         /* Two byte */
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], 
1250                                 msg[1]);
1251                 else 
1252                         printk("reserved two byte (%02x %02x) ", 
1253                                 msg[0], msg[1]);
1254                 len = 2;
1255         } else 
1256                 printk("reserved ");
1257         return len;
1258 }
1259 EXPORT_SYMBOL(spi_print_msg);
1260
1261 #else  /* ifndef CONFIG_SCSI_CONSTANTS */
1262
1263 int spi_print_msg(const unsigned char *msg)
1264 {
1265         int len = 1, i;
1266
1267         if (msg[0] == EXTENDED_MESSAGE) {
1268                 len = 2 + msg[1];
1269                 if (len == 2)
1270                         len += 256;
1271                 for (i = 0; i < len; ++i)
1272                         printk("%02x ", msg[i]);
1273         /* Identify */
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]);
1279         /* Two byte */
1280         } else if (msg[0] <= 0x2f) {
1281                 printk("%02x %02x", msg[0], msg[1]);
1282                 len = 2;
1283         } else 
1284                 printk("%02x ", msg[0]);
1285         return len;
1286 }
1287 EXPORT_SYMBOL(spi_print_msg);
1288 #endif /* ! CONFIG_SCSI_CONSTANTS */
1289
1290 static int spi_device_match(struct attribute_container *cont,
1291                             struct device *dev)
1292 {
1293         struct scsi_device *sdev;
1294         struct Scsi_Host *shost;
1295         struct spi_internal *i;
1296
1297         if (!scsi_is_sdev_device(dev))
1298                 return 0;
1299
1300         sdev = to_scsi_device(dev);
1301         shost = sdev->host;
1302         if (!shost->transportt  || shost->transportt->host_attrs.ac.class
1303             != &spi_host_class.class)
1304                 return 0;
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))
1310                 return 0;
1311         return 1;
1312 }
1313
1314 static int spi_target_match(struct attribute_container *cont,
1315                             struct device *dev)
1316 {
1317         struct Scsi_Host *shost;
1318         struct scsi_target *starget;
1319         struct spi_internal *i;
1320
1321         if (!scsi_is_target_device(dev))
1322                 return 0;
1323
1324         shost = dev_to_shost(dev->parent);
1325         if (!shost->transportt  || shost->transportt->host_attrs.ac.class
1326             != &spi_host_class.class)
1327                 return 0;
1328
1329         i = to_spi_internal(shost->transportt);
1330         starget = to_scsi_target(dev);
1331
1332         if (i->f->deny_binding && i->f->deny_binding(starget))
1333                 return 0;
1334
1335         return &i->t.target_attrs.ac == cont;
1336 }
1337
1338 static DECLARE_TRANSPORT_CLASS(spi_transport_class,
1339                                "spi_transport",
1340                                spi_setup_transport_attrs,
1341                                NULL,
1342                                spi_target_configure);
1343
1344 static DECLARE_ANON_TRANSPORT_CLASS(spi_device_class,
1345                                     spi_device_match,
1346                                     spi_device_configure);
1347
1348 static struct attribute *host_attributes[] = {
1349         &dev_attr_signalling.attr,
1350         NULL
1351 };
1352
1353 static struct attribute_group host_attribute_group = {
1354         .attrs = host_attributes,
1355 };
1356
1357 static int spi_host_configure(struct transport_container *tc,
1358                               struct device *dev,
1359                               struct device *cdev)
1360 {
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;
1365         int rc = 0;
1366
1367         if (si->f->set_signalling)
1368                 rc = sysfs_chmod_file(kobj, attr, attr->mode | S_IWUSR);
1369
1370         return rc;
1371 }
1372
1373 /* returns true if we should be showing the variable.  Also
1374  * overloads the return by setting 1<<1 if the attribute should
1375  * be writeable */
1376 #define TARGET_ATTRIBUTE_HELPER(name) \
1377         (si->f->show_##name ? 1 : 0) + \
1378         (si->f->set_##name ? 2 : 0)
1379
1380 static int target_attribute_is_visible(struct kobject *kobj,
1381                                        struct attribute *attr, int i)
1382 {
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);
1387
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)
1431                 return 1;
1432
1433         return 0;
1434 }
1435
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,
1443         &dev_attr_iu.attr,
1444         &dev_attr_dt.attr,
1445         &dev_attr_qas.attr,
1446         &dev_attr_wr_flow.attr,
1447         &dev_attr_rd_strm.attr,
1448         &dev_attr_rti.attr,
1449         &dev_attr_pcomp_en.attr,
1450         &dev_attr_hold_mcs.attr,
1451         &dev_attr_revalidate.attr,
1452         NULL
1453 };
1454
1455 static struct attribute_group target_attribute_group = {
1456         .attrs = target_attributes,
1457         .is_visible = target_attribute_is_visible,
1458 };
1459
1460 static int spi_target_configure(struct transport_container *tc,
1461                                 struct device *dev,
1462                                 struct device *cdev)
1463 {
1464         struct kobject *kobj = &cdev->kobj;
1465         int i;
1466         struct attribute *attr;
1467         int rc;
1468
1469         for (i = 0; (attr = target_attributes[i]) != NULL; i++) {
1470                 int j = target_attribute_group.is_visible(kobj, attr, i);
1471
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
1480                  * function */
1481                 if ((j & 1))
1482                         rc = sysfs_chmod_file(kobj, attr, attr->mode | S_IWUSR);
1483         }
1484
1485         return 0;
1486 }
1487
1488 struct scsi_transport_template *
1489 spi_attach_transport(struct spi_function_template *ft)
1490 {
1491         struct spi_internal *i = kzalloc(sizeof(struct spi_internal),
1492                                          GFP_KERNEL);
1493
1494         if (unlikely(!i))
1495                 return NULL;
1496
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);
1507         i->f = ft;
1508
1509         return &i->t;
1510 }
1511 EXPORT_SYMBOL(spi_attach_transport);
1512
1513 void spi_release_transport(struct scsi_transport_template *t)
1514 {
1515         struct spi_internal *i = to_spi_internal(t);
1516
1517         transport_container_unregister(&i->t.target_attrs);
1518         transport_container_unregister(&i->t.host_attrs);
1519
1520         kfree(i);
1521 }
1522 EXPORT_SYMBOL(spi_release_transport);
1523
1524 static __init int spi_transport_init(void)
1525 {
1526         int error = transport_class_register(&spi_transport_class);
1527         if (error)
1528                 return error;
1529         error = anon_transport_class_register(&spi_device_class);
1530         return transport_class_register(&spi_host_class);
1531 }
1532
1533 static void __exit spi_transport_exit(void)
1534 {
1535         transport_class_unregister(&spi_transport_class);
1536         anon_transport_class_unregister(&spi_device_class);
1537         transport_class_unregister(&spi_host_class);
1538 }
1539
1540 MODULE_AUTHOR("Martin Hicks");
1541 MODULE_DESCRIPTION("SPI Transport Attributes");
1542 MODULE_LICENSE("GPL");
1543
1544 module_init(spi_transport_init);
1545 module_exit(spi_transport_exit);