[SCSI] libsas: Warn if ATA device detected but CONFIG_SCSI_SAS_ATA not set
[linux-2.6] / 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 class_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 class_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 class_device *cdev);
199
200 static int spi_device_configure(struct transport_container *tc,
201                                 struct device *dev,
202                                 struct class_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 class_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 class_device *cdev, char *buf)        \
252 {                                                                       \
253         struct scsi_target *starget = transport_class_to_starget(cdev); \
254         struct spi_transport_attrs *tp;                                 \
255                                                                         \
256         tp = (struct spi_transport_attrs *)&starget->starget_data;      \
257         return snprintf(buf, 20, format_string, tp->field);             \
258 }
259
260 #define spi_transport_store_simple(field, format_string)                \
261                                                                         \
262 static ssize_t                                                          \
263 store_spi_transport_##field(struct class_device *cdev, const char *buf, \
264                             size_t count)                               \
265 {                                                                       \
266         int val;                                                        \
267         struct scsi_target *starget = transport_class_to_starget(cdev); \
268         struct spi_transport_attrs *tp;                                 \
269                                                                         \
270         tp = (struct spi_transport_attrs *)&starget->starget_data;      \
271         val = simple_strtoul(buf, NULL, 0);                             \
272         tp->field = val;                                                \
273         return count;                                                   \
274 }
275
276 #define spi_transport_show_function(field, format_string)               \
277                                                                         \
278 static ssize_t                                                          \
279 show_spi_transport_##field(struct class_device *cdev, char *buf)        \
280 {                                                                       \
281         struct scsi_target *starget = transport_class_to_starget(cdev); \
282         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);    \
283         struct spi_transport_attrs *tp;                                 \
284         struct spi_internal *i = to_spi_internal(shost->transportt);    \
285         tp = (struct spi_transport_attrs *)&starget->starget_data;      \
286         if (i->f->get_##field)                                          \
287                 i->f->get_##field(starget);                             \
288         return snprintf(buf, 20, format_string, tp->field);             \
289 }
290
291 #define spi_transport_store_function(field, format_string)              \
292 static ssize_t                                                          \
293 store_spi_transport_##field(struct class_device *cdev, const char *buf, \
294                             size_t count)                               \
295 {                                                                       \
296         int val;                                                        \
297         struct scsi_target *starget = transport_class_to_starget(cdev); \
298         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);    \
299         struct spi_internal *i = to_spi_internal(shost->transportt);    \
300                                                                         \
301         if (!i->f->set_##field)                                         \
302                 return -EINVAL;                                         \
303         val = simple_strtoul(buf, NULL, 0);                             \
304         i->f->set_##field(starget, val);                                \
305         return count;                                                   \
306 }
307
308 #define spi_transport_store_max(field, format_string)                   \
309 static ssize_t                                                          \
310 store_spi_transport_##field(struct class_device *cdev, const char *buf, \
311                             size_t count)                               \
312 {                                                                       \
313         int val;                                                        \
314         struct scsi_target *starget = transport_class_to_starget(cdev); \
315         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);    \
316         struct spi_internal *i = to_spi_internal(shost->transportt);    \
317         struct spi_transport_attrs *tp                                  \
318                 = (struct spi_transport_attrs *)&starget->starget_data; \
319                                                                         \
320         if (i->f->set_##field)                                          \
321                 return -EINVAL;                                         \
322         val = simple_strtoul(buf, NULL, 0);                             \
323         if (val > tp->max_##field)                                      \
324                 val = tp->max_##field;                                  \
325         i->f->set_##field(starget, val);                                \
326         return count;                                                   \
327 }
328
329 #define spi_transport_rd_attr(field, format_string)                     \
330         spi_transport_show_function(field, format_string)               \
331         spi_transport_store_function(field, format_string)              \
332 static CLASS_DEVICE_ATTR(field, S_IRUGO,                                \
333                          show_spi_transport_##field,                    \
334                          store_spi_transport_##field);
335
336 #define spi_transport_simple_attr(field, format_string)                 \
337         spi_transport_show_simple(field, format_string)                 \
338         spi_transport_store_simple(field, format_string)                \
339 static CLASS_DEVICE_ATTR(field, S_IRUGO,                                \
340                          show_spi_transport_##field,                    \
341                          store_spi_transport_##field);
342
343 #define spi_transport_max_attr(field, format_string)                    \
344         spi_transport_show_function(field, format_string)               \
345         spi_transport_store_max(field, format_string)                   \
346         spi_transport_simple_attr(max_##field, format_string)           \
347 static CLASS_DEVICE_ATTR(field, S_IRUGO,                                \
348                          show_spi_transport_##field,                    \
349                          store_spi_transport_##field);
350
351 /* The Parallel SCSI Tranport Attributes: */
352 spi_transport_max_attr(offset, "%d\n");
353 spi_transport_max_attr(width, "%d\n");
354 spi_transport_rd_attr(iu, "%d\n");
355 spi_transport_rd_attr(dt, "%d\n");
356 spi_transport_rd_attr(qas, "%d\n");
357 spi_transport_rd_attr(wr_flow, "%d\n");
358 spi_transport_rd_attr(rd_strm, "%d\n");
359 spi_transport_rd_attr(rti, "%d\n");
360 spi_transport_rd_attr(pcomp_en, "%d\n");
361 spi_transport_rd_attr(hold_mcs, "%d\n");
362
363 /* we only care about the first child device so we return 1 */
364 static int child_iter(struct device *dev, void *data)
365 {
366         struct scsi_device *sdev = to_scsi_device(dev);
367
368         spi_dv_device(sdev);
369         return 1;
370 }
371
372 static ssize_t
373 store_spi_revalidate(struct class_device *cdev, const char *buf, size_t count)
374 {
375         struct scsi_target *starget = transport_class_to_starget(cdev);
376
377         device_for_each_child(&starget->dev, NULL, child_iter);
378         return count;
379 }
380 static CLASS_DEVICE_ATTR(revalidate, S_IWUSR, NULL, store_spi_revalidate);
381
382 /* Translate the period into ns according to the current spec
383  * for SDTR/PPR messages */
384 static int period_to_str(char *buf, int period)
385 {
386         int len, picosec;
387
388         if (period < 0 || period > 0xff) {
389                 picosec = -1;
390         } else if (period <= SPI_STATIC_PPR) {
391                 picosec = ppr_to_ps[period];
392         } else {
393                 picosec = period * 4000;
394         }
395
396         if (picosec == -1) {
397                 len = sprintf(buf, "reserved");
398         } else {
399                 len = sprint_frac(buf, picosec, 1000);
400         }
401
402         return len;
403 }
404
405 static ssize_t
406 show_spi_transport_period_helper(char *buf, int period)
407 {
408         int len = period_to_str(buf, period);
409         buf[len++] = '\n';
410         buf[len] = '\0';
411         return len;
412 }
413
414 static ssize_t
415 store_spi_transport_period_helper(struct class_device *cdev, const char *buf,
416                                   size_t count, int *periodp)
417 {
418         int j, picosec, period = -1;
419         char *endp;
420
421         picosec = simple_strtoul(buf, &endp, 10) * 1000;
422         if (*endp == '.') {
423                 int mult = 100;
424                 do {
425                         endp++;
426                         if (!isdigit(*endp))
427                                 break;
428                         picosec += (*endp - '0') * mult;
429                         mult /= 10;
430                 } while (mult > 0);
431         }
432
433         for (j = 0; j <= SPI_STATIC_PPR; j++) {
434                 if (ppr_to_ps[j] < picosec)
435                         continue;
436                 period = j;
437                 break;
438         }
439
440         if (period == -1)
441                 period = picosec / 4000;
442
443         if (period > 0xff)
444                 period = 0xff;
445
446         *periodp = period;
447
448         return count;
449 }
450
451 static ssize_t
452 show_spi_transport_period(struct class_device *cdev, char *buf)
453 {
454         struct scsi_target *starget = transport_class_to_starget(cdev);
455         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
456         struct spi_internal *i = to_spi_internal(shost->transportt);
457         struct spi_transport_attrs *tp =
458                 (struct spi_transport_attrs *)&starget->starget_data;
459
460         if (i->f->get_period)
461                 i->f->get_period(starget);
462
463         return show_spi_transport_period_helper(buf, tp->period);
464 }
465
466 static ssize_t
467 store_spi_transport_period(struct class_device *cdev, const char *buf,
468                             size_t count)
469 {
470         struct scsi_target *starget = transport_class_to_starget(cdev);
471         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
472         struct spi_internal *i = to_spi_internal(shost->transportt);
473         struct spi_transport_attrs *tp =
474                 (struct spi_transport_attrs *)&starget->starget_data;
475         int period, retval;
476
477         if (!i->f->set_period)
478                 return -EINVAL;
479
480         retval = store_spi_transport_period_helper(cdev, buf, count, &period);
481
482         if (period < tp->min_period)
483                 period = tp->min_period;
484
485         i->f->set_period(starget, period);
486
487         return retval;
488 }
489
490 static CLASS_DEVICE_ATTR(period, S_IRUGO,
491                          show_spi_transport_period,
492                          store_spi_transport_period);
493
494 static ssize_t
495 show_spi_transport_min_period(struct class_device *cdev, char *buf)
496 {
497         struct scsi_target *starget = transport_class_to_starget(cdev);
498         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
499         struct spi_internal *i = to_spi_internal(shost->transportt);
500         struct spi_transport_attrs *tp =
501                 (struct spi_transport_attrs *)&starget->starget_data;
502
503         if (!i->f->set_period)
504                 return -EINVAL;
505
506         return show_spi_transport_period_helper(buf, tp->min_period);
507 }
508
509 static ssize_t
510 store_spi_transport_min_period(struct class_device *cdev, const char *buf,
511                             size_t count)
512 {
513         struct scsi_target *starget = transport_class_to_starget(cdev);
514         struct spi_transport_attrs *tp =
515                 (struct spi_transport_attrs *)&starget->starget_data;
516
517         return store_spi_transport_period_helper(cdev, buf, count,
518                                                  &tp->min_period);
519 }
520
521
522 static CLASS_DEVICE_ATTR(min_period, S_IRUGO,
523                          show_spi_transport_min_period,
524                          store_spi_transport_min_period);
525
526
527 static ssize_t show_spi_host_signalling(struct class_device *cdev, char *buf)
528 {
529         struct Scsi_Host *shost = transport_class_to_shost(cdev);
530         struct spi_internal *i = to_spi_internal(shost->transportt);
531
532         if (i->f->get_signalling)
533                 i->f->get_signalling(shost);
534
535         return sprintf(buf, "%s\n", spi_signal_to_string(spi_signalling(shost)));
536 }
537 static ssize_t store_spi_host_signalling(struct class_device *cdev,
538                                          const char *buf, size_t count)
539 {
540         struct Scsi_Host *shost = transport_class_to_shost(cdev);
541         struct spi_internal *i = to_spi_internal(shost->transportt);
542         enum spi_signal_type type = spi_signal_to_value(buf);
543
544         if (!i->f->set_signalling)
545                 return -EINVAL;
546
547         if (type != SPI_SIGNAL_UNKNOWN)
548                 i->f->set_signalling(shost, type);
549
550         return count;
551 }
552 static CLASS_DEVICE_ATTR(signalling, S_IRUGO,
553                          show_spi_host_signalling,
554                          store_spi_host_signalling);
555
556 #define DV_SET(x, y)                    \
557         if(i->f->set_##x)               \
558                 i->f->set_##x(sdev->sdev_target, y)
559
560 enum spi_compare_returns {
561         SPI_COMPARE_SUCCESS,
562         SPI_COMPARE_FAILURE,
563         SPI_COMPARE_SKIP_TEST,
564 };
565
566
567 /* This is for read/write Domain Validation:  If the device supports
568  * an echo buffer, we do read/write tests to it */
569 static enum spi_compare_returns
570 spi_dv_device_echo_buffer(struct scsi_device *sdev, u8 *buffer,
571                           u8 *ptr, const int retries)
572 {
573         int len = ptr - buffer;
574         int j, k, r, result;
575         unsigned int pattern = 0x0000ffff;
576         struct scsi_sense_hdr sshdr;
577
578         const char spi_write_buffer[] = {
579                 WRITE_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
580         };
581         const char spi_read_buffer[] = {
582                 READ_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
583         };
584
585         /* set up the pattern buffer.  Doesn't matter if we spill
586          * slightly beyond since that's where the read buffer is */
587         for (j = 0; j < len; ) {
588
589                 /* fill the buffer with counting (test a) */
590                 for ( ; j < min(len, 32); j++)
591                         buffer[j] = j;
592                 k = j;
593                 /* fill the buffer with alternating words of 0x0 and
594                  * 0xffff (test b) */
595                 for ( ; j < min(len, k + 32); j += 2) {
596                         u16 *word = (u16 *)&buffer[j];
597                         
598                         *word = (j & 0x02) ? 0x0000 : 0xffff;
599                 }
600                 k = j;
601                 /* fill with crosstalk (alternating 0x5555 0xaaa)
602                  * (test c) */
603                 for ( ; j < min(len, k + 32); j += 2) {
604                         u16 *word = (u16 *)&buffer[j];
605
606                         *word = (j & 0x02) ? 0x5555 : 0xaaaa;
607                 }
608                 k = j;
609                 /* fill with shifting bits (test d) */
610                 for ( ; j < min(len, k + 32); j += 4) {
611                         u32 *word = (unsigned int *)&buffer[j];
612                         u32 roll = (pattern & 0x80000000) ? 1 : 0;
613                         
614                         *word = pattern;
615                         pattern = (pattern << 1) | roll;
616                 }
617                 /* don't bother with random data (test e) */
618         }
619
620         for (r = 0; r < retries; r++) {
621                 result = spi_execute(sdev, spi_write_buffer, DMA_TO_DEVICE,
622                                      buffer, len, &sshdr);
623                 if(result || !scsi_device_online(sdev)) {
624
625                         scsi_device_set_state(sdev, SDEV_QUIESCE);
626                         if (scsi_sense_valid(&sshdr)
627                             && sshdr.sense_key == ILLEGAL_REQUEST
628                             /* INVALID FIELD IN CDB */
629                             && sshdr.asc == 0x24 && sshdr.ascq == 0x00)
630                                 /* This would mean that the drive lied
631                                  * to us about supporting an echo
632                                  * buffer (unfortunately some Western
633                                  * Digital drives do precisely this)
634                                  */
635                                 return SPI_COMPARE_SKIP_TEST;
636
637
638                         sdev_printk(KERN_ERR, sdev, "Write Buffer failure %x\n", result);
639                         return SPI_COMPARE_FAILURE;
640                 }
641
642                 memset(ptr, 0, len);
643                 spi_execute(sdev, spi_read_buffer, DMA_FROM_DEVICE,
644                             ptr, len, NULL);
645                 scsi_device_set_state(sdev, SDEV_QUIESCE);
646
647                 if (memcmp(buffer, ptr, len) != 0)
648                         return SPI_COMPARE_FAILURE;
649         }
650         return SPI_COMPARE_SUCCESS;
651 }
652
653 /* This is for the simplest form of Domain Validation: a read test
654  * on the inquiry data from the device */
655 static enum spi_compare_returns
656 spi_dv_device_compare_inquiry(struct scsi_device *sdev, u8 *buffer,
657                               u8 *ptr, const int retries)
658 {
659         int r, result;
660         const int len = sdev->inquiry_len;
661         const char spi_inquiry[] = {
662                 INQUIRY, 0, 0, 0, len, 0
663         };
664
665         for (r = 0; r < retries; r++) {
666                 memset(ptr, 0, len);
667
668                 result = spi_execute(sdev, spi_inquiry, DMA_FROM_DEVICE,
669                                      ptr, len, NULL);
670                 
671                 if(result || !scsi_device_online(sdev)) {
672                         scsi_device_set_state(sdev, SDEV_QUIESCE);
673                         return SPI_COMPARE_FAILURE;
674                 }
675
676                 /* If we don't have the inquiry data already, the
677                  * first read gets it */
678                 if (ptr == buffer) {
679                         ptr += len;
680                         --r;
681                         continue;
682                 }
683
684                 if (memcmp(buffer, ptr, len) != 0)
685                         /* failure */
686                         return SPI_COMPARE_FAILURE;
687         }
688         return SPI_COMPARE_SUCCESS;
689 }
690
691 static enum spi_compare_returns
692 spi_dv_retrain(struct scsi_device *sdev, u8 *buffer, u8 *ptr,
693                enum spi_compare_returns 
694                (*compare_fn)(struct scsi_device *, u8 *, u8 *, int))
695 {
696         struct spi_internal *i = to_spi_internal(sdev->host->transportt);
697         struct scsi_target *starget = sdev->sdev_target;
698         int period = 0, prevperiod = 0; 
699         enum spi_compare_returns retval;
700
701
702         for (;;) {
703                 int newperiod;
704                 retval = compare_fn(sdev, buffer, ptr, DV_LOOPS);
705
706                 if (retval == SPI_COMPARE_SUCCESS
707                     || retval == SPI_COMPARE_SKIP_TEST)
708                         break;
709
710                 /* OK, retrain, fallback */
711                 if (i->f->get_iu)
712                         i->f->get_iu(starget);
713                 if (i->f->get_qas)
714                         i->f->get_qas(starget);
715                 if (i->f->get_period)
716                         i->f->get_period(sdev->sdev_target);
717
718                 /* Here's the fallback sequence; first try turning off
719                  * IU, then QAS (if we can control them), then finally
720                  * fall down the periods */
721                 if (i->f->set_iu && spi_iu(starget)) {
722                         starget_printk(KERN_ERR, starget, "Domain Validation Disabing Information Units\n");
723                         DV_SET(iu, 0);
724                 } else if (i->f->set_qas && spi_qas(starget)) {
725                         starget_printk(KERN_ERR, starget, "Domain Validation Disabing Quick Arbitration and Selection\n");
726                         DV_SET(qas, 0);
727                 } else {
728                         newperiod = spi_period(starget);
729                         period = newperiod > period ? newperiod : period;
730                         if (period < 0x0d)
731                                 period++;
732                         else
733                                 period += period >> 1;
734
735                         if (unlikely(period > 0xff || period == prevperiod)) {
736                                 /* Total failure; set to async and return */
737                                 starget_printk(KERN_ERR, starget, "Domain Validation Failure, dropping back to Asynchronous\n");
738                                 DV_SET(offset, 0);
739                                 return SPI_COMPARE_FAILURE;
740                         }
741                         starget_printk(KERN_ERR, starget, "Domain Validation detected failure, dropping back\n");
742                         DV_SET(period, period);
743                         prevperiod = period;
744                 }
745         }
746         return retval;
747 }
748
749 static int
750 spi_dv_device_get_echo_buffer(struct scsi_device *sdev, u8 *buffer)
751 {
752         int l, result;
753
754         /* first off do a test unit ready.  This can error out 
755          * because of reservations or some other reason.  If it
756          * fails, the device won't let us write to the echo buffer
757          * so just return failure */
758         
759         const char spi_test_unit_ready[] = {
760                 TEST_UNIT_READY, 0, 0, 0, 0, 0
761         };
762
763         const char spi_read_buffer_descriptor[] = {
764                 READ_BUFFER, 0x0b, 0, 0, 0, 0, 0, 0, 4, 0
765         };
766
767         
768         /* We send a set of three TURs to clear any outstanding 
769          * unit attention conditions if they exist (Otherwise the
770          * buffer tests won't be happy).  If the TUR still fails
771          * (reservation conflict, device not ready, etc) just
772          * skip the write tests */
773         for (l = 0; ; l++) {
774                 result = spi_execute(sdev, spi_test_unit_ready, DMA_NONE, 
775                                      NULL, 0, NULL);
776
777                 if(result) {
778                         if(l >= 3)
779                                 return 0;
780                 } else {
781                         /* TUR succeeded */
782                         break;
783                 }
784         }
785
786         result = spi_execute(sdev, spi_read_buffer_descriptor, 
787                              DMA_FROM_DEVICE, buffer, 4, NULL);
788
789         if (result)
790                 /* Device has no echo buffer */
791                 return 0;
792
793         return buffer[3] + ((buffer[2] & 0x1f) << 8);
794 }
795
796 static void
797 spi_dv_device_internal(struct scsi_device *sdev, u8 *buffer)
798 {
799         struct spi_internal *i = to_spi_internal(sdev->host->transportt);
800         struct scsi_target *starget = sdev->sdev_target;
801         struct Scsi_Host *shost = sdev->host;
802         int len = sdev->inquiry_len;
803         int min_period = spi_min_period(starget);
804         int max_width = spi_max_width(starget);
805         /* first set us up for narrow async */
806         DV_SET(offset, 0);
807         DV_SET(width, 0);
808
809         if (spi_dv_device_compare_inquiry(sdev, buffer, buffer, DV_LOOPS)
810             != SPI_COMPARE_SUCCESS) {
811                 starget_printk(KERN_ERR, starget, "Domain Validation Initial Inquiry Failed\n");
812                 /* FIXME: should probably offline the device here? */
813                 return;
814         }
815
816         if (!scsi_device_wide(sdev)) {
817                 spi_max_width(starget) = 0;
818                 max_width = 0;
819         }
820
821         /* test width */
822         if (i->f->set_width && max_width) {
823                 i->f->set_width(starget, 1);
824
825                 if (spi_dv_device_compare_inquiry(sdev, buffer,
826                                                    buffer + len,
827                                                    DV_LOOPS)
828                     != SPI_COMPARE_SUCCESS) {
829                         starget_printk(KERN_ERR, starget, "Wide Transfers Fail\n");
830                         i->f->set_width(starget, 0);
831                         /* Make sure we don't force wide back on by asking
832                          * for a transfer period that requires it */
833                         max_width = 0;
834                         if (min_period < 10)
835                                 min_period = 10;
836                 }
837         }
838
839         if (!i->f->set_period)
840                 return;
841
842         /* device can't handle synchronous */
843         if (!scsi_device_sync(sdev) && !scsi_device_dt(sdev))
844                 return;
845
846         /* len == -1 is the signal that we need to ascertain the
847          * presence of an echo buffer before trying to use it.  len ==
848          * 0 means we don't have an echo buffer */
849         len = -1;
850
851  retry:
852
853         /* now set up to the maximum */
854         DV_SET(offset, spi_max_offset(starget));
855         DV_SET(period, min_period);
856
857         /* try QAS requests; this should be harmless to set if the
858          * target supports it */
859         if (scsi_device_qas(sdev)) {
860                 DV_SET(qas, 1);
861         } else {
862                 DV_SET(qas, 0);
863         }
864
865         if (scsi_device_ius(sdev) && min_period < 9) {
866                 /* This u320 (or u640). Set IU transfers */
867                 DV_SET(iu, 1);
868                 /* Then set the optional parameters */
869                 DV_SET(rd_strm, 1);
870                 DV_SET(wr_flow, 1);
871                 DV_SET(rti, 1);
872                 if (min_period == 8)
873                         DV_SET(pcomp_en, 1);
874         } else {
875                 DV_SET(iu, 0);
876         }
877
878         /* now that we've done all this, actually check the bus
879          * signal type (if known).  Some devices are stupid on
880          * a SE bus and still claim they can try LVD only settings */
881         if (i->f->get_signalling)
882                 i->f->get_signalling(shost);
883         if (spi_signalling(shost) == SPI_SIGNAL_SE ||
884             spi_signalling(shost) == SPI_SIGNAL_HVD ||
885             !scsi_device_dt(sdev)) {
886                 DV_SET(dt, 0);
887         } else {
888                 DV_SET(dt, 1);
889         }
890         /* set width last because it will pull all the other
891          * parameters down to required values */
892         DV_SET(width, max_width);
893
894         /* Do the read only INQUIRY tests */
895         spi_dv_retrain(sdev, buffer, buffer + sdev->inquiry_len,
896                        spi_dv_device_compare_inquiry);
897         /* See if we actually managed to negotiate and sustain DT */
898         if (i->f->get_dt)
899                 i->f->get_dt(starget);
900
901         /* see if the device has an echo buffer.  If it does we can do
902          * the SPI pattern write tests.  Because of some broken
903          * devices, we *only* try this on a device that has actually
904          * negotiated DT */
905
906         if (len == -1 && spi_dt(starget))
907                 len = spi_dv_device_get_echo_buffer(sdev, buffer);
908
909         if (len <= 0) {
910                 starget_printk(KERN_INFO, starget, "Domain Validation skipping write tests\n");
911                 return;
912         }
913
914         if (len > SPI_MAX_ECHO_BUFFER_SIZE) {
915                 starget_printk(KERN_WARNING, starget, "Echo buffer size %d is too big, trimming to %d\n", len, SPI_MAX_ECHO_BUFFER_SIZE);
916                 len = SPI_MAX_ECHO_BUFFER_SIZE;
917         }
918
919         if (spi_dv_retrain(sdev, buffer, buffer + len,
920                            spi_dv_device_echo_buffer)
921             == SPI_COMPARE_SKIP_TEST) {
922                 /* OK, the stupid drive can't do a write echo buffer
923                  * test after all, fall back to the read tests */
924                 len = 0;
925                 goto retry;
926         }
927 }
928
929
930 /**     spi_dv_device - Do Domain Validation on the device
931  *      @sdev:          scsi device to validate
932  *
933  *      Performs the domain validation on the given device in the
934  *      current execution thread.  Since DV operations may sleep,
935  *      the current thread must have user context.  Also no SCSI
936  *      related locks that would deadlock I/O issued by the DV may
937  *      be held.
938  */
939 void
940 spi_dv_device(struct scsi_device *sdev)
941 {
942         struct scsi_target *starget = sdev->sdev_target;
943         u8 *buffer;
944         const int len = SPI_MAX_ECHO_BUFFER_SIZE*2;
945
946         if (unlikely(scsi_device_get(sdev)))
947                 return;
948
949         if (unlikely(spi_dv_in_progress(starget)))
950                 return;
951         spi_dv_in_progress(starget) = 1;
952
953         buffer = kzalloc(len, GFP_KERNEL);
954
955         if (unlikely(!buffer))
956                 goto out_put;
957
958         /* We need to verify that the actual device will quiesce; the
959          * later target quiesce is just a nice to have */
960         if (unlikely(scsi_device_quiesce(sdev)))
961                 goto out_free;
962
963         scsi_target_quiesce(starget);
964
965         spi_dv_pending(starget) = 1;
966         mutex_lock(&spi_dv_mutex(starget));
967
968         starget_printk(KERN_INFO, starget, "Beginning Domain Validation\n");
969
970         spi_dv_device_internal(sdev, buffer);
971
972         starget_printk(KERN_INFO, starget, "Ending Domain Validation\n");
973
974         mutex_unlock(&spi_dv_mutex(starget));
975         spi_dv_pending(starget) = 0;
976
977         scsi_target_resume(starget);
978
979         spi_initial_dv(starget) = 1;
980
981  out_free:
982         kfree(buffer);
983  out_put:
984         spi_dv_in_progress(starget) = 0;
985         scsi_device_put(sdev);
986 }
987 EXPORT_SYMBOL(spi_dv_device);
988
989 struct work_queue_wrapper {
990         struct work_struct      work;
991         struct scsi_device      *sdev;
992 };
993
994 static void
995 spi_dv_device_work_wrapper(struct work_struct *work)
996 {
997         struct work_queue_wrapper *wqw =
998                 container_of(work, struct work_queue_wrapper, work);
999         struct scsi_device *sdev = wqw->sdev;
1000
1001         kfree(wqw);
1002         spi_dv_device(sdev);
1003         spi_dv_pending(sdev->sdev_target) = 0;
1004         scsi_device_put(sdev);
1005 }
1006
1007
1008 /**
1009  *      spi_schedule_dv_device - schedule domain validation to occur on the device
1010  *      @sdev:  The device to validate
1011  *
1012  *      Identical to spi_dv_device() above, except that the DV will be
1013  *      scheduled to occur in a workqueue later.  All memory allocations
1014  *      are atomic, so may be called from any context including those holding
1015  *      SCSI locks.
1016  */
1017 void
1018 spi_schedule_dv_device(struct scsi_device *sdev)
1019 {
1020         struct work_queue_wrapper *wqw =
1021                 kmalloc(sizeof(struct work_queue_wrapper), GFP_ATOMIC);
1022
1023         if (unlikely(!wqw))
1024                 return;
1025
1026         if (unlikely(spi_dv_pending(sdev->sdev_target))) {
1027                 kfree(wqw);
1028                 return;
1029         }
1030         /* Set pending early (dv_device doesn't check it, only sets it) */
1031         spi_dv_pending(sdev->sdev_target) = 1;
1032         if (unlikely(scsi_device_get(sdev))) {
1033                 kfree(wqw);
1034                 spi_dv_pending(sdev->sdev_target) = 0;
1035                 return;
1036         }
1037
1038         INIT_WORK(&wqw->work, spi_dv_device_work_wrapper);
1039         wqw->sdev = sdev;
1040
1041         schedule_work(&wqw->work);
1042 }
1043 EXPORT_SYMBOL(spi_schedule_dv_device);
1044
1045 /**
1046  * spi_display_xfer_agreement - Print the current target transfer agreement
1047  * @starget: The target for which to display the agreement
1048  *
1049  * Each SPI port is required to maintain a transfer agreement for each
1050  * other port on the bus.  This function prints a one-line summary of
1051  * the current agreement; more detailed information is available in sysfs.
1052  */
1053 void spi_display_xfer_agreement(struct scsi_target *starget)
1054 {
1055         struct spi_transport_attrs *tp;
1056         tp = (struct spi_transport_attrs *)&starget->starget_data;
1057
1058         if (tp->offset > 0 && tp->period > 0) {
1059                 unsigned int picosec, kb100;
1060                 char *scsi = "FAST-?";
1061                 char tmp[8];
1062
1063                 if (tp->period <= SPI_STATIC_PPR) {
1064                         picosec = ppr_to_ps[tp->period];
1065                         switch (tp->period) {
1066                                 case  7: scsi = "FAST-320"; break;
1067                                 case  8: scsi = "FAST-160"; break;
1068                                 case  9: scsi = "FAST-80"; break;
1069                                 case 10:
1070                                 case 11: scsi = "FAST-40"; break;
1071                                 case 12: scsi = "FAST-20"; break;
1072                         }
1073                 } else {
1074                         picosec = tp->period * 4000;
1075                         if (tp->period < 25)
1076                                 scsi = "FAST-20";
1077                         else if (tp->period < 50)
1078                                 scsi = "FAST-10";
1079                         else
1080                                 scsi = "FAST-5";
1081                 }
1082
1083                 kb100 = (10000000 + picosec / 2) / picosec;
1084                 if (tp->width)
1085                         kb100 *= 2;
1086                 sprint_frac(tmp, picosec, 1000);
1087
1088                 dev_info(&starget->dev,
1089                          "%s %sSCSI %d.%d MB/s %s%s%s%s%s%s%s%s (%s ns, offset %d)\n",
1090                          scsi, tp->width ? "WIDE " : "", kb100/10, kb100 % 10,
1091                          tp->dt ? "DT" : "ST",
1092                          tp->iu ? " IU" : "",
1093                          tp->qas  ? " QAS" : "",
1094                          tp->rd_strm ? " RDSTRM" : "",
1095                          tp->rti ? " RTI" : "",
1096                          tp->wr_flow ? " WRFLOW" : "",
1097                          tp->pcomp_en ? " PCOMP" : "",
1098                          tp->hold_mcs ? " HMCS" : "",
1099                          tmp, tp->offset);
1100         } else {
1101                 dev_info(&starget->dev, "%sasynchronous\n",
1102                                 tp->width ? "wide " : "");
1103         }
1104 }
1105 EXPORT_SYMBOL(spi_display_xfer_agreement);
1106
1107 int spi_populate_width_msg(unsigned char *msg, int width)
1108 {
1109         msg[0] = EXTENDED_MESSAGE;
1110         msg[1] = 2;
1111         msg[2] = EXTENDED_WDTR;
1112         msg[3] = width;
1113         return 4;
1114 }
1115 EXPORT_SYMBOL_GPL(spi_populate_width_msg);
1116
1117 int spi_populate_sync_msg(unsigned char *msg, int period, int offset)
1118 {
1119         msg[0] = EXTENDED_MESSAGE;
1120         msg[1] = 3;
1121         msg[2] = EXTENDED_SDTR;
1122         msg[3] = period;
1123         msg[4] = offset;
1124         return 5;
1125 }
1126 EXPORT_SYMBOL_GPL(spi_populate_sync_msg);
1127
1128 int spi_populate_ppr_msg(unsigned char *msg, int period, int offset,
1129                 int width, int options)
1130 {
1131         msg[0] = EXTENDED_MESSAGE;
1132         msg[1] = 6;
1133         msg[2] = EXTENDED_PPR;
1134         msg[3] = period;
1135         msg[4] = 0;
1136         msg[5] = offset;
1137         msg[6] = width;
1138         msg[7] = options;
1139         return 8;
1140 }
1141 EXPORT_SYMBOL_GPL(spi_populate_ppr_msg);
1142
1143 #ifdef CONFIG_SCSI_CONSTANTS
1144 static const char * const one_byte_msgs[] = {
1145 /* 0x00 */ "Task Complete", NULL /* Extended Message */, "Save Pointers",
1146 /* 0x03 */ "Restore Pointers", "Disconnect", "Initiator Error", 
1147 /* 0x06 */ "Abort Task Set", "Message Reject", "Nop", "Message Parity Error",
1148 /* 0x0a */ "Linked Command Complete", "Linked Command Complete w/flag",
1149 /* 0x0c */ "Target Reset", "Abort Task", "Clear Task Set", 
1150 /* 0x0f */ "Initiate Recovery", "Release Recovery",
1151 /* 0x11 */ "Terminate Process", "Continue Task", "Target Transfer Disable",
1152 /* 0x14 */ NULL, NULL, "Clear ACA", "LUN Reset"
1153 };
1154
1155 static const char * const two_byte_msgs[] = {
1156 /* 0x20 */ "Simple Queue Tag", "Head of Queue Tag", "Ordered Queue Tag",
1157 /* 0x23 */ "Ignore Wide Residue", "ACA"
1158 };
1159
1160 static const char * const extended_msgs[] = {
1161 /* 0x00 */ "Modify Data Pointer", "Synchronous Data Transfer Request",
1162 /* 0x02 */ "SCSI-I Extended Identify", "Wide Data Transfer Request",
1163 /* 0x04 */ "Parallel Protocol Request", "Modify Bidirectional Data Pointer"
1164 };
1165
1166 static void print_nego(const unsigned char *msg, int per, int off, int width)
1167 {
1168         if (per) {
1169                 char buf[20];
1170                 period_to_str(buf, msg[per]);
1171                 printk("period = %s ns ", buf);
1172         }
1173
1174         if (off)
1175                 printk("offset = %d ", msg[off]);
1176         if (width)
1177                 printk("width = %d ", 8 << msg[width]);
1178 }
1179
1180 static void print_ptr(const unsigned char *msg, int msb, const char *desc)
1181 {
1182         int ptr = (msg[msb] << 24) | (msg[msb+1] << 16) | (msg[msb+2] << 8) |
1183                         msg[msb+3];
1184         printk("%s = %d ", desc, ptr);
1185 }
1186
1187 int spi_print_msg(const unsigned char *msg)
1188 {
1189         int len = 1, i;
1190         if (msg[0] == EXTENDED_MESSAGE) {
1191                 len = 2 + msg[1];
1192                 if (len == 2)
1193                         len += 256;
1194                 if (msg[2] < ARRAY_SIZE(extended_msgs))
1195                         printk ("%s ", extended_msgs[msg[2]]); 
1196                 else 
1197                         printk ("Extended Message, reserved code (0x%02x) ",
1198                                 (int) msg[2]);
1199                 switch (msg[2]) {
1200                 case EXTENDED_MODIFY_DATA_POINTER:
1201                         print_ptr(msg, 3, "pointer");
1202                         break;
1203                 case EXTENDED_SDTR:
1204                         print_nego(msg, 3, 4, 0);
1205                         break;
1206                 case EXTENDED_WDTR:
1207                         print_nego(msg, 0, 0, 3);
1208                         break;
1209                 case EXTENDED_PPR:
1210                         print_nego(msg, 3, 5, 6);
1211                         break;
1212                 case EXTENDED_MODIFY_BIDI_DATA_PTR:
1213                         print_ptr(msg, 3, "out");
1214                         print_ptr(msg, 7, "in");
1215                         break;
1216                 default:
1217                 for (i = 2; i < len; ++i) 
1218                         printk("%02x ", msg[i]);
1219                 }
1220         /* Identify */
1221         } else if (msg[0] & 0x80) {
1222                 printk("Identify disconnect %sallowed %s %d ",
1223                         (msg[0] & 0x40) ? "" : "not ",
1224                         (msg[0] & 0x20) ? "target routine" : "lun",
1225                         msg[0] & 0x7);
1226         /* Normal One byte */
1227         } else if (msg[0] < 0x1f) {
1228                 if (msg[0] < ARRAY_SIZE(one_byte_msgs) && one_byte_msgs[msg[0]])
1229                         printk("%s ", one_byte_msgs[msg[0]]);
1230                 else
1231                         printk("reserved (%02x) ", msg[0]);
1232         } else if (msg[0] == 0x55) {
1233                 printk("QAS Request ");
1234         /* Two byte */
1235         } else if (msg[0] <= 0x2f) {
1236                 if ((msg[0] - 0x20) < ARRAY_SIZE(two_byte_msgs))
1237                         printk("%s %02x ", two_byte_msgs[msg[0] - 0x20], 
1238                                 msg[1]);
1239                 else 
1240                         printk("reserved two byte (%02x %02x) ", 
1241                                 msg[0], msg[1]);
1242                 len = 2;
1243         } else 
1244                 printk("reserved ");
1245         return len;
1246 }
1247 EXPORT_SYMBOL(spi_print_msg);
1248
1249 #else  /* ifndef CONFIG_SCSI_CONSTANTS */
1250
1251 int spi_print_msg(const unsigned char *msg)
1252 {
1253         int len = 1, i;
1254
1255         if (msg[0] == EXTENDED_MESSAGE) {
1256                 len = 2 + msg[1];
1257                 if (len == 2)
1258                         len += 256;
1259                 for (i = 0; i < len; ++i)
1260                         printk("%02x ", msg[i]);
1261         /* Identify */
1262         } else if (msg[0] & 0x80) {
1263                 printk("%02x ", msg[0]);
1264         /* Normal One byte */
1265         } else if ((msg[0] < 0x1f) || (msg[0] == 0x55)) {
1266                 printk("%02x ", msg[0]);
1267         /* Two byte */
1268         } else if (msg[0] <= 0x2f) {
1269                 printk("%02x %02x", msg[0], msg[1]);
1270                 len = 2;
1271         } else 
1272                 printk("%02x ", msg[0]);
1273         return len;
1274 }
1275 EXPORT_SYMBOL(spi_print_msg);
1276 #endif /* ! CONFIG_SCSI_CONSTANTS */
1277
1278 static int spi_device_match(struct attribute_container *cont,
1279                             struct device *dev)
1280 {
1281         struct scsi_device *sdev;
1282         struct Scsi_Host *shost;
1283         struct spi_internal *i;
1284
1285         if (!scsi_is_sdev_device(dev))
1286                 return 0;
1287
1288         sdev = to_scsi_device(dev);
1289         shost = sdev->host;
1290         if (!shost->transportt  || shost->transportt->host_attrs.ac.class
1291             != &spi_host_class.class)
1292                 return 0;
1293         /* Note: this class has no device attributes, so it has
1294          * no per-HBA allocation and thus we don't need to distinguish
1295          * the attribute containers for the device */
1296         i = to_spi_internal(shost->transportt);
1297         if (i->f->deny_binding && i->f->deny_binding(sdev->sdev_target))
1298                 return 0;
1299         return 1;
1300 }
1301
1302 static int spi_target_match(struct attribute_container *cont,
1303                             struct device *dev)
1304 {
1305         struct Scsi_Host *shost;
1306         struct scsi_target *starget;
1307         struct spi_internal *i;
1308
1309         if (!scsi_is_target_device(dev))
1310                 return 0;
1311
1312         shost = dev_to_shost(dev->parent);
1313         if (!shost->transportt  || shost->transportt->host_attrs.ac.class
1314             != &spi_host_class.class)
1315                 return 0;
1316
1317         i = to_spi_internal(shost->transportt);
1318         starget = to_scsi_target(dev);
1319
1320         if (i->f->deny_binding && i->f->deny_binding(starget))
1321                 return 0;
1322
1323         return &i->t.target_attrs.ac == cont;
1324 }
1325
1326 static DECLARE_TRANSPORT_CLASS(spi_transport_class,
1327                                "spi_transport",
1328                                spi_setup_transport_attrs,
1329                                NULL,
1330                                spi_target_configure);
1331
1332 static DECLARE_ANON_TRANSPORT_CLASS(spi_device_class,
1333                                     spi_device_match,
1334                                     spi_device_configure);
1335
1336 static struct attribute *host_attributes[] = {
1337         &class_device_attr_signalling.attr,
1338         NULL
1339 };
1340
1341 static struct attribute_group host_attribute_group = {
1342         .attrs = host_attributes,
1343 };
1344
1345 static int spi_host_configure(struct transport_container *tc,
1346                               struct device *dev,
1347                               struct class_device *cdev)
1348 {
1349         struct kobject *kobj = &cdev->kobj;
1350         struct Scsi_Host *shost = transport_class_to_shost(cdev);
1351         struct spi_internal *si = to_spi_internal(shost->transportt);
1352         struct attribute *attr = &class_device_attr_signalling.attr;
1353         int rc = 0;
1354
1355         if (si->f->set_signalling)
1356                 rc = sysfs_chmod_file(kobj, attr, attr->mode | S_IWUSR);
1357
1358         return rc;
1359 }
1360
1361 /* returns true if we should be showing the variable.  Also
1362  * overloads the return by setting 1<<1 if the attribute should
1363  * be writeable */
1364 #define TARGET_ATTRIBUTE_HELPER(name) \
1365         (si->f->show_##name ? 1 : 0) + \
1366         (si->f->set_##name ? 2 : 0)
1367
1368 static int target_attribute_is_visible(struct kobject *kobj,
1369                                        struct attribute *attr, int i)
1370 {
1371         struct class_device *cdev =
1372                 container_of(kobj, struct class_device, kobj);
1373         struct scsi_target *starget = transport_class_to_starget(cdev);
1374         struct Scsi_Host *shost = transport_class_to_shost(cdev);
1375         struct spi_internal *si = to_spi_internal(shost->transportt);
1376
1377         if (attr == &class_device_attr_period.attr &&
1378             spi_support_sync(starget))
1379                 return TARGET_ATTRIBUTE_HELPER(period);
1380         else if (attr == &class_device_attr_min_period.attr &&
1381                  spi_support_sync(starget))
1382                 return TARGET_ATTRIBUTE_HELPER(period);
1383         else if (attr == &class_device_attr_offset.attr &&
1384                  spi_support_sync(starget))
1385                 return TARGET_ATTRIBUTE_HELPER(offset);
1386         else if (attr == &class_device_attr_max_offset.attr &&
1387                  spi_support_sync(starget))
1388                 return TARGET_ATTRIBUTE_HELPER(offset);
1389         else if (attr == &class_device_attr_width.attr &&
1390                  spi_support_wide(starget))
1391                 return TARGET_ATTRIBUTE_HELPER(width);
1392         else if (attr == &class_device_attr_max_width.attr &&
1393                  spi_support_wide(starget))
1394                 return TARGET_ATTRIBUTE_HELPER(width);
1395         else if (attr == &class_device_attr_iu.attr &&
1396                  spi_support_ius(starget))
1397                 return TARGET_ATTRIBUTE_HELPER(iu);
1398         else if (attr == &class_device_attr_dt.attr &&
1399                  spi_support_dt(starget))
1400                 return TARGET_ATTRIBUTE_HELPER(dt);
1401         else if (attr == &class_device_attr_qas.attr &&
1402                  spi_support_qas(starget))
1403                 return TARGET_ATTRIBUTE_HELPER(qas);
1404         else if (attr == &class_device_attr_wr_flow.attr &&
1405                  spi_support_ius(starget))
1406                 return TARGET_ATTRIBUTE_HELPER(wr_flow);
1407         else if (attr == &class_device_attr_rd_strm.attr &&
1408                  spi_support_ius(starget))
1409                 return TARGET_ATTRIBUTE_HELPER(rd_strm);
1410         else if (attr == &class_device_attr_rti.attr &&
1411                  spi_support_ius(starget))
1412                 return TARGET_ATTRIBUTE_HELPER(rti);
1413         else if (attr == &class_device_attr_pcomp_en.attr &&
1414                  spi_support_ius(starget))
1415                 return TARGET_ATTRIBUTE_HELPER(pcomp_en);
1416         else if (attr == &class_device_attr_hold_mcs.attr &&
1417                  spi_support_ius(starget))
1418                 return TARGET_ATTRIBUTE_HELPER(hold_mcs);
1419         else if (attr == &class_device_attr_revalidate.attr)
1420                 return 1;
1421
1422         return 0;
1423 }
1424
1425 static struct attribute *target_attributes[] = {
1426         &class_device_attr_period.attr,
1427         &class_device_attr_min_period.attr,
1428         &class_device_attr_offset.attr,
1429         &class_device_attr_max_offset.attr,
1430         &class_device_attr_width.attr,
1431         &class_device_attr_max_width.attr,
1432         &class_device_attr_iu.attr,
1433         &class_device_attr_dt.attr,
1434         &class_device_attr_qas.attr,
1435         &class_device_attr_wr_flow.attr,
1436         &class_device_attr_rd_strm.attr,
1437         &class_device_attr_rti.attr,
1438         &class_device_attr_pcomp_en.attr,
1439         &class_device_attr_hold_mcs.attr,
1440         &class_device_attr_revalidate.attr,
1441         NULL
1442 };
1443
1444 static struct attribute_group target_attribute_group = {
1445         .attrs = target_attributes,
1446         .is_visible = target_attribute_is_visible,
1447 };
1448
1449 static int spi_target_configure(struct transport_container *tc,
1450                                 struct device *dev,
1451                                 struct class_device *cdev)
1452 {
1453         struct kobject *kobj = &cdev->kobj;
1454         int i;
1455         struct attribute *attr;
1456         int rc;
1457
1458         for (i = 0; (attr = target_attributes[i]) != NULL; i++) {
1459                 int j = target_attribute_group.is_visible(kobj, attr, i);
1460
1461                 /* FIXME: as well as returning -EEXIST, which we'd like
1462                  * to ignore, sysfs also does a WARN_ON and dumps a trace,
1463                  * which is bad, so temporarily, skip attributes that are
1464                  * already visible (the revalidate one) */
1465                 if (j && attr != &class_device_attr_revalidate.attr)
1466                         rc = sysfs_add_file_to_group(kobj, attr,
1467                                                 target_attribute_group.name);
1468                 /* and make the attribute writeable if we have a set
1469                  * function */
1470                 if ((j & 1))
1471                         rc = sysfs_chmod_file(kobj, attr, attr->mode | S_IWUSR);
1472         }
1473
1474         return 0;
1475 }
1476
1477 struct scsi_transport_template *
1478 spi_attach_transport(struct spi_function_template *ft)
1479 {
1480         struct spi_internal *i = kzalloc(sizeof(struct spi_internal),
1481                                          GFP_KERNEL);
1482
1483         if (unlikely(!i))
1484                 return NULL;
1485
1486         i->t.target_attrs.ac.class = &spi_transport_class.class;
1487         i->t.target_attrs.ac.grp = &target_attribute_group;
1488         i->t.target_attrs.ac.match = spi_target_match;
1489         transport_container_register(&i->t.target_attrs);
1490         i->t.target_size = sizeof(struct spi_transport_attrs);
1491         i->t.host_attrs.ac.class = &spi_host_class.class;
1492         i->t.host_attrs.ac.grp = &host_attribute_group;
1493         i->t.host_attrs.ac.match = spi_host_match;
1494         transport_container_register(&i->t.host_attrs);
1495         i->t.host_size = sizeof(struct spi_host_attrs);
1496         i->f = ft;
1497
1498         return &i->t;
1499 }
1500 EXPORT_SYMBOL(spi_attach_transport);
1501
1502 void spi_release_transport(struct scsi_transport_template *t)
1503 {
1504         struct spi_internal *i = to_spi_internal(t);
1505
1506         transport_container_unregister(&i->t.target_attrs);
1507         transport_container_unregister(&i->t.host_attrs);
1508
1509         kfree(i);
1510 }
1511 EXPORT_SYMBOL(spi_release_transport);
1512
1513 static __init int spi_transport_init(void)
1514 {
1515         int error = transport_class_register(&spi_transport_class);
1516         if (error)
1517                 return error;
1518         error = anon_transport_class_register(&spi_device_class);
1519         return transport_class_register(&spi_host_class);
1520 }
1521
1522 static void __exit spi_transport_exit(void)
1523 {
1524         transport_class_unregister(&spi_transport_class);
1525         anon_transport_class_unregister(&spi_device_class);
1526         transport_class_unregister(&spi_host_class);
1527 }
1528
1529 MODULE_AUTHOR("Martin Hicks");
1530 MODULE_DESCRIPTION("SPI Transport Attributes");
1531 MODULE_LICENSE("GPL");
1532
1533 module_init(spi_transport_init);
1534 module_exit(spi_transport_exit);