Merge tag 'trace-v4.14-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[linux-2.6-microblaze.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 <linux/sysfs.h>
28 #include <linux/slab.h>
29 #include <scsi/scsi.h>
30 #include "scsi_priv.h"
31 #include <scsi/scsi_device.h>
32 #include <scsi/scsi_host.h>
33 #include <scsi/scsi_cmnd.h>
34 #include <scsi/scsi_eh.h>
35 #include <scsi/scsi_tcq.h>
36 #include <scsi/scsi_transport.h>
37 #include <scsi/scsi_transport_spi.h>
38
39 #define SPI_NUM_ATTRS 14        /* increase this if you add attributes */
40 #define SPI_OTHER_ATTRS 1       /* Increase this if you add "always
41                                  * on" attributes */
42 #define SPI_HOST_ATTRS  1
43
44 #define SPI_MAX_ECHO_BUFFER_SIZE        4096
45
46 #define DV_LOOPS        3
47 #define DV_TIMEOUT      (10*HZ)
48 #define DV_RETRIES      3       /* should only need at most 
49                                  * two cc/ua clears */
50
51 /* Our blacklist flags */
52 enum {
53         SPI_BLIST_NOIUS = 0x1,
54 };
55
56 /* blacklist table, modelled on scsi_devinfo.c */
57 static struct {
58         char *vendor;
59         char *model;
60         unsigned flags;
61 } spi_static_device_list[] __initdata = {
62         {"HP", "Ultrium 3-SCSI", SPI_BLIST_NOIUS },
63         {"IBM", "ULTRIUM-TD3", SPI_BLIST_NOIUS },
64         {NULL, NULL, 0}
65 };
66
67 /* Private data accessors (keep these out of the header file) */
68 #define spi_dv_in_progress(x) (((struct spi_transport_attrs *)&(x)->starget_data)->dv_in_progress)
69 #define spi_dv_mutex(x) (((struct spi_transport_attrs *)&(x)->starget_data)->dv_mutex)
70
71 struct spi_internal {
72         struct scsi_transport_template t;
73         struct spi_function_template *f;
74 };
75
76 #define to_spi_internal(tmpl)   container_of(tmpl, struct spi_internal, t)
77
78 static const int ppr_to_ps[] = {
79         /* The PPR values 0-6 are reserved, fill them in when
80          * the committee defines them */
81         -1,                     /* 0x00 */
82         -1,                     /* 0x01 */
83         -1,                     /* 0x02 */
84         -1,                     /* 0x03 */
85         -1,                     /* 0x04 */
86         -1,                     /* 0x05 */
87         -1,                     /* 0x06 */
88          3125,                  /* 0x07 */
89          6250,                  /* 0x08 */
90         12500,                  /* 0x09 */
91         25000,                  /* 0x0a */
92         30300,                  /* 0x0b */
93         50000,                  /* 0x0c */
94 };
95 /* The PPR values at which you calculate the period in ns by multiplying
96  * by 4 */
97 #define SPI_STATIC_PPR  0x0c
98
99 static int sprint_frac(char *dest, int value, int denom)
100 {
101         int frac = value % denom;
102         int result = sprintf(dest, "%d", value / denom);
103
104         if (frac == 0)
105                 return result;
106         dest[result++] = '.';
107
108         do {
109                 denom /= 10;
110                 sprintf(dest + result, "%d", frac / denom);
111                 result++;
112                 frac %= denom;
113         } while (frac);
114
115         dest[result++] = '\0';
116         return result;
117 }
118
119 static int spi_execute(struct scsi_device *sdev, const void *cmd,
120                        enum dma_data_direction dir,
121                        void *buffer, unsigned bufflen,
122                        struct scsi_sense_hdr *sshdr)
123 {
124         int i, result;
125         unsigned char sense[SCSI_SENSE_BUFFERSIZE];
126         struct scsi_sense_hdr sshdr_tmp;
127
128         if (!sshdr)
129                 sshdr = &sshdr_tmp;
130
131         for(i = 0; i < DV_RETRIES; i++) {
132                 result = scsi_execute(sdev, cmd, dir, buffer, bufflen, sense,
133                                       sshdr, DV_TIMEOUT, /* retries */ 1,
134                                       REQ_FAILFAST_DEV |
135                                       REQ_FAILFAST_TRANSPORT |
136                                       REQ_FAILFAST_DRIVER,
137                                       0, NULL);
138                 if (!(driver_byte(result) & DRIVER_SENSE) ||
139                     sshdr->sense_key != UNIT_ATTENTION)
140                         break;
141         }
142         return result;
143 }
144
145 static struct {
146         enum spi_signal_type    value;
147         char                    *name;
148 } signal_types[] = {
149         { SPI_SIGNAL_UNKNOWN, "unknown" },
150         { SPI_SIGNAL_SE, "SE" },
151         { SPI_SIGNAL_LVD, "LVD" },
152         { SPI_SIGNAL_HVD, "HVD" },
153 };
154
155 static inline const char *spi_signal_to_string(enum spi_signal_type type)
156 {
157         int i;
158
159         for (i = 0; i < ARRAY_SIZE(signal_types); i++) {
160                 if (type == signal_types[i].value)
161                         return signal_types[i].name;
162         }
163         return NULL;
164 }
165 static inline enum spi_signal_type spi_signal_to_value(const char *name)
166 {
167         int i, len;
168
169         for (i = 0; i < ARRAY_SIZE(signal_types); i++) {
170                 len =  strlen(signal_types[i].name);
171                 if (strncmp(name, signal_types[i].name, len) == 0 &&
172                     (name[len] == '\n' || name[len] == '\0'))
173                         return signal_types[i].value;
174         }
175         return SPI_SIGNAL_UNKNOWN;
176 }
177
178 static int spi_host_setup(struct transport_container *tc, struct device *dev,
179                           struct device *cdev)
180 {
181         struct Scsi_Host *shost = dev_to_shost(dev);
182
183         spi_signalling(shost) = SPI_SIGNAL_UNKNOWN;
184
185         return 0;
186 }
187
188 static int spi_host_configure(struct transport_container *tc,
189                               struct device *dev,
190                               struct device *cdev);
191
192 static DECLARE_TRANSPORT_CLASS(spi_host_class,
193                                "spi_host",
194                                spi_host_setup,
195                                NULL,
196                                spi_host_configure);
197
198 static int spi_host_match(struct attribute_container *cont,
199                           struct device *dev)
200 {
201         struct Scsi_Host *shost;
202
203         if (!scsi_is_host_device(dev))
204                 return 0;
205
206         shost = dev_to_shost(dev);
207         if (!shost->transportt  || shost->transportt->host_attrs.ac.class
208             != &spi_host_class.class)
209                 return 0;
210
211         return &shost->transportt->host_attrs.ac == cont;
212 }
213
214 static int spi_target_configure(struct transport_container *tc,
215                                 struct device *dev,
216                                 struct device *cdev);
217
218 static int spi_device_configure(struct transport_container *tc,
219                                 struct device *dev,
220                                 struct device *cdev)
221 {
222         struct scsi_device *sdev = to_scsi_device(dev);
223         struct scsi_target *starget = sdev->sdev_target;
224         unsigned bflags = scsi_get_device_flags_keyed(sdev, &sdev->inquiry[8],
225                                                       &sdev->inquiry[16],
226                                                       SCSI_DEVINFO_SPI);
227
228         /* Populate the target capability fields with the values
229          * gleaned from the device inquiry */
230
231         spi_support_sync(starget) = scsi_device_sync(sdev);
232         spi_support_wide(starget) = scsi_device_wide(sdev);
233         spi_support_dt(starget) = scsi_device_dt(sdev);
234         spi_support_dt_only(starget) = scsi_device_dt_only(sdev);
235         spi_support_ius(starget) = scsi_device_ius(sdev);
236         if (bflags & SPI_BLIST_NOIUS) {
237                 dev_info(dev, "Information Units disabled by blacklist\n");
238                 spi_support_ius(starget) = 0;
239         }
240         spi_support_qas(starget) = scsi_device_qas(sdev);
241
242         return 0;
243 }
244
245 static int spi_setup_transport_attrs(struct transport_container *tc,
246                                      struct device *dev,
247                                      struct device *cdev)
248 {
249         struct scsi_target *starget = to_scsi_target(dev);
250
251         spi_period(starget) = -1;       /* illegal value */
252         spi_min_period(starget) = 0;
253         spi_offset(starget) = 0;        /* async */
254         spi_max_offset(starget) = 255;
255         spi_width(starget) = 0; /* narrow */
256         spi_max_width(starget) = 1;
257         spi_iu(starget) = 0;    /* no IU */
258         spi_max_iu(starget) = 1;
259         spi_dt(starget) = 0;    /* ST */
260         spi_qas(starget) = 0;
261         spi_max_qas(starget) = 1;
262         spi_wr_flow(starget) = 0;
263         spi_rd_strm(starget) = 0;
264         spi_rti(starget) = 0;
265         spi_pcomp_en(starget) = 0;
266         spi_hold_mcs(starget) = 0;
267         spi_dv_pending(starget) = 0;
268         spi_dv_in_progress(starget) = 0;
269         spi_initial_dv(starget) = 0;
270         mutex_init(&spi_dv_mutex(starget));
271
272         return 0;
273 }
274
275 #define spi_transport_show_simple(field, format_string)                 \
276                                                                         \
277 static ssize_t                                                          \
278 show_spi_transport_##field(struct device *dev,                  \
279                            struct device_attribute *attr, char *buf)    \
280 {                                                                       \
281         struct scsi_target *starget = transport_class_to_starget(dev);  \
282         struct spi_transport_attrs *tp;                                 \
283                                                                         \
284         tp = (struct spi_transport_attrs *)&starget->starget_data;      \
285         return snprintf(buf, 20, format_string, tp->field);             \
286 }
287
288 #define spi_transport_store_simple(field, format_string)                \
289                                                                         \
290 static ssize_t                                                          \
291 store_spi_transport_##field(struct device *dev,                         \
292                             struct device_attribute *attr,              \
293                             const char *buf, size_t count)              \
294 {                                                                       \
295         int val;                                                        \
296         struct scsi_target *starget = transport_class_to_starget(dev);  \
297         struct spi_transport_attrs *tp;                                 \
298                                                                         \
299         tp = (struct spi_transport_attrs *)&starget->starget_data;      \
300         val = simple_strtoul(buf, NULL, 0);                             \
301         tp->field = val;                                                \
302         return count;                                                   \
303 }
304
305 #define spi_transport_show_function(field, format_string)               \
306                                                                         \
307 static ssize_t                                                          \
308 show_spi_transport_##field(struct device *dev,                  \
309                            struct device_attribute *attr, char *buf)    \
310 {                                                                       \
311         struct scsi_target *starget = transport_class_to_starget(dev);  \
312         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);    \
313         struct spi_transport_attrs *tp;                                 \
314         struct spi_internal *i = to_spi_internal(shost->transportt);    \
315         tp = (struct spi_transport_attrs *)&starget->starget_data;      \
316         if (i->f->get_##field)                                          \
317                 i->f->get_##field(starget);                             \
318         return snprintf(buf, 20, format_string, tp->field);             \
319 }
320
321 #define spi_transport_store_function(field, format_string)              \
322 static ssize_t                                                          \
323 store_spi_transport_##field(struct device *dev,                         \
324                             struct device_attribute *attr,              \
325                             const char *buf, size_t count)              \
326 {                                                                       \
327         int val;                                                        \
328         struct scsi_target *starget = transport_class_to_starget(dev);  \
329         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);    \
330         struct spi_internal *i = to_spi_internal(shost->transportt);    \
331                                                                         \
332         if (!i->f->set_##field)                                         \
333                 return -EINVAL;                                         \
334         val = simple_strtoul(buf, NULL, 0);                             \
335         i->f->set_##field(starget, val);                                \
336         return count;                                                   \
337 }
338
339 #define spi_transport_store_max(field, format_string)                   \
340 static ssize_t                                                          \
341 store_spi_transport_##field(struct device *dev,                         \
342                             struct device_attribute *attr,              \
343                             const char *buf, size_t count)              \
344 {                                                                       \
345         int val;                                                        \
346         struct scsi_target *starget = transport_class_to_starget(dev);  \
347         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);    \
348         struct spi_internal *i = to_spi_internal(shost->transportt);    \
349         struct spi_transport_attrs *tp                                  \
350                 = (struct spi_transport_attrs *)&starget->starget_data; \
351                                                                         \
352         if (i->f->set_##field)                                          \
353                 return -EINVAL;                                         \
354         val = simple_strtoul(buf, NULL, 0);                             \
355         if (val > tp->max_##field)                                      \
356                 val = tp->max_##field;                                  \
357         i->f->set_##field(starget, val);                                \
358         return count;                                                   \
359 }
360
361 #define spi_transport_rd_attr(field, format_string)                     \
362         spi_transport_show_function(field, format_string)               \
363         spi_transport_store_function(field, format_string)              \
364 static DEVICE_ATTR(field, S_IRUGO,                              \
365                    show_spi_transport_##field,                  \
366                    store_spi_transport_##field);
367
368 #define spi_transport_simple_attr(field, format_string)                 \
369         spi_transport_show_simple(field, format_string)                 \
370         spi_transport_store_simple(field, format_string)                \
371 static DEVICE_ATTR(field, S_IRUGO,                              \
372                    show_spi_transport_##field,                  \
373                    store_spi_transport_##field);
374
375 #define spi_transport_max_attr(field, format_string)                    \
376         spi_transport_show_function(field, format_string)               \
377         spi_transport_store_max(field, format_string)                   \
378         spi_transport_simple_attr(max_##field, format_string)           \
379 static DEVICE_ATTR(field, S_IRUGO,                              \
380                    show_spi_transport_##field,                  \
381                    store_spi_transport_##field);
382
383 /* The Parallel SCSI Tranport Attributes: */
384 spi_transport_max_attr(offset, "%d\n");
385 spi_transport_max_attr(width, "%d\n");
386 spi_transport_max_attr(iu, "%d\n");
387 spi_transport_rd_attr(dt, "%d\n");
388 spi_transport_max_attr(qas, "%d\n");
389 spi_transport_rd_attr(wr_flow, "%d\n");
390 spi_transport_rd_attr(rd_strm, "%d\n");
391 spi_transport_rd_attr(rti, "%d\n");
392 spi_transport_rd_attr(pcomp_en, "%d\n");
393 spi_transport_rd_attr(hold_mcs, "%d\n");
394
395 /* we only care about the first child device that's a real SCSI device
396  * so we return 1 to terminate the iteration when we find it */
397 static int child_iter(struct device *dev, void *data)
398 {
399         if (!scsi_is_sdev_device(dev))
400                 return 0;
401
402         spi_dv_device(to_scsi_device(dev));
403         return 1;
404 }
405
406 static ssize_t
407 store_spi_revalidate(struct device *dev, struct device_attribute *attr,
408                      const char *buf, size_t count)
409 {
410         struct scsi_target *starget = transport_class_to_starget(dev);
411
412         device_for_each_child(&starget->dev, NULL, child_iter);
413         return count;
414 }
415 static DEVICE_ATTR(revalidate, S_IWUSR, NULL, store_spi_revalidate);
416
417 /* Translate the period into ns according to the current spec
418  * for SDTR/PPR messages */
419 static int period_to_str(char *buf, int period)
420 {
421         int len, picosec;
422
423         if (period < 0 || period > 0xff) {
424                 picosec = -1;
425         } else if (period <= SPI_STATIC_PPR) {
426                 picosec = ppr_to_ps[period];
427         } else {
428                 picosec = period * 4000;
429         }
430
431         if (picosec == -1) {
432                 len = sprintf(buf, "reserved");
433         } else {
434                 len = sprint_frac(buf, picosec, 1000);
435         }
436
437         return len;
438 }
439
440 static ssize_t
441 show_spi_transport_period_helper(char *buf, int period)
442 {
443         int len = period_to_str(buf, period);
444         buf[len++] = '\n';
445         buf[len] = '\0';
446         return len;
447 }
448
449 static ssize_t
450 store_spi_transport_period_helper(struct device *dev, const char *buf,
451                                   size_t count, int *periodp)
452 {
453         int j, picosec, period = -1;
454         char *endp;
455
456         picosec = simple_strtoul(buf, &endp, 10) * 1000;
457         if (*endp == '.') {
458                 int mult = 100;
459                 do {
460                         endp++;
461                         if (!isdigit(*endp))
462                                 break;
463                         picosec += (*endp - '0') * mult;
464                         mult /= 10;
465                 } while (mult > 0);
466         }
467
468         for (j = 0; j <= SPI_STATIC_PPR; j++) {
469                 if (ppr_to_ps[j] < picosec)
470                         continue;
471                 period = j;
472                 break;
473         }
474
475         if (period == -1)
476                 period = picosec / 4000;
477
478         if (period > 0xff)
479                 period = 0xff;
480
481         *periodp = period;
482
483         return count;
484 }
485
486 static ssize_t
487 show_spi_transport_period(struct device *dev,
488                           struct device_attribute *attr, char *buf)
489 {
490         struct scsi_target *starget = transport_class_to_starget(dev);
491         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
492         struct spi_internal *i = to_spi_internal(shost->transportt);
493         struct spi_transport_attrs *tp =
494                 (struct spi_transport_attrs *)&starget->starget_data;
495
496         if (i->f->get_period)
497                 i->f->get_period(starget);
498
499         return show_spi_transport_period_helper(buf, tp->period);
500 }
501
502 static ssize_t
503 store_spi_transport_period(struct device *cdev, struct device_attribute *attr,
504                            const char *buf, size_t count)
505 {
506         struct scsi_target *starget = transport_class_to_starget(cdev);
507         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
508         struct spi_internal *i = to_spi_internal(shost->transportt);
509         struct spi_transport_attrs *tp =
510                 (struct spi_transport_attrs *)&starget->starget_data;
511         int period, retval;
512
513         if (!i->f->set_period)
514                 return -EINVAL;
515
516         retval = store_spi_transport_period_helper(cdev, buf, count, &period);
517
518         if (period < tp->min_period)
519                 period = tp->min_period;
520
521         i->f->set_period(starget, period);
522
523         return retval;
524 }
525
526 static DEVICE_ATTR(period, S_IRUGO,
527                    show_spi_transport_period,
528                    store_spi_transport_period);
529
530 static ssize_t
531 show_spi_transport_min_period(struct device *cdev,
532                               struct device_attribute *attr, char *buf)
533 {
534         struct scsi_target *starget = transport_class_to_starget(cdev);
535         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
536         struct spi_internal *i = to_spi_internal(shost->transportt);
537         struct spi_transport_attrs *tp =
538                 (struct spi_transport_attrs *)&starget->starget_data;
539
540         if (!i->f->set_period)
541                 return -EINVAL;
542
543         return show_spi_transport_period_helper(buf, tp->min_period);
544 }
545
546 static ssize_t
547 store_spi_transport_min_period(struct device *cdev,
548                                struct device_attribute *attr,
549                                const char *buf, size_t count)
550 {
551         struct scsi_target *starget = transport_class_to_starget(cdev);
552         struct spi_transport_attrs *tp =
553                 (struct spi_transport_attrs *)&starget->starget_data;
554
555         return store_spi_transport_period_helper(cdev, buf, count,
556                                                  &tp->min_period);
557 }
558
559
560 static DEVICE_ATTR(min_period, S_IRUGO,
561                    show_spi_transport_min_period,
562                    store_spi_transport_min_period);
563
564
565 static ssize_t show_spi_host_signalling(struct device *cdev,
566                                         struct device_attribute *attr,
567                                         char *buf)
568 {
569         struct Scsi_Host *shost = transport_class_to_shost(cdev);
570         struct spi_internal *i = to_spi_internal(shost->transportt);
571
572         if (i->f->get_signalling)
573                 i->f->get_signalling(shost);
574
575         return sprintf(buf, "%s\n", spi_signal_to_string(spi_signalling(shost)));
576 }
577 static ssize_t store_spi_host_signalling(struct device *dev,
578                                          struct device_attribute *attr,
579                                          const char *buf, size_t count)
580 {
581         struct Scsi_Host *shost = transport_class_to_shost(dev);
582         struct spi_internal *i = to_spi_internal(shost->transportt);
583         enum spi_signal_type type = spi_signal_to_value(buf);
584
585         if (!i->f->set_signalling)
586                 return -EINVAL;
587
588         if (type != SPI_SIGNAL_UNKNOWN)
589                 i->f->set_signalling(shost, type);
590
591         return count;
592 }
593 static DEVICE_ATTR(signalling, S_IRUGO,
594                    show_spi_host_signalling,
595                    store_spi_host_signalling);
596
597 static ssize_t show_spi_host_width(struct device *cdev,
598                                       struct device_attribute *attr,
599                                       char *buf)
600 {
601         struct Scsi_Host *shost = transport_class_to_shost(cdev);
602
603         return sprintf(buf, "%s\n", shost->max_id == 16 ? "wide" : "narrow");
604 }
605 static DEVICE_ATTR(host_width, S_IRUGO,
606                    show_spi_host_width, NULL);
607
608 static ssize_t show_spi_host_hba_id(struct device *cdev,
609                                     struct device_attribute *attr,
610                                     char *buf)
611 {
612         struct Scsi_Host *shost = transport_class_to_shost(cdev);
613
614         return sprintf(buf, "%d\n", shost->this_id);
615 }
616 static DEVICE_ATTR(hba_id, S_IRUGO,
617                    show_spi_host_hba_id, NULL);
618
619 #define DV_SET(x, y)                    \
620         if(i->f->set_##x)               \
621                 i->f->set_##x(sdev->sdev_target, y)
622
623 enum spi_compare_returns {
624         SPI_COMPARE_SUCCESS,
625         SPI_COMPARE_FAILURE,
626         SPI_COMPARE_SKIP_TEST,
627 };
628
629
630 /* This is for read/write Domain Validation:  If the device supports
631  * an echo buffer, we do read/write tests to it */
632 static enum spi_compare_returns
633 spi_dv_device_echo_buffer(struct scsi_device *sdev, u8 *buffer,
634                           u8 *ptr, const int retries)
635 {
636         int len = ptr - buffer;
637         int j, k, r, result;
638         unsigned int pattern = 0x0000ffff;
639         struct scsi_sense_hdr sshdr;
640
641         const char spi_write_buffer[] = {
642                 WRITE_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
643         };
644         const char spi_read_buffer[] = {
645                 READ_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
646         };
647
648         /* set up the pattern buffer.  Doesn't matter if we spill
649          * slightly beyond since that's where the read buffer is */
650         for (j = 0; j < len; ) {
651
652                 /* fill the buffer with counting (test a) */
653                 for ( ; j < min(len, 32); j++)
654                         buffer[j] = j;
655                 k = j;
656                 /* fill the buffer with alternating words of 0x0 and
657                  * 0xffff (test b) */
658                 for ( ; j < min(len, k + 32); j += 2) {
659                         u16 *word = (u16 *)&buffer[j];
660                         
661                         *word = (j & 0x02) ? 0x0000 : 0xffff;
662                 }
663                 k = j;
664                 /* fill with crosstalk (alternating 0x5555 0xaaa)
665                  * (test c) */
666                 for ( ; j < min(len, k + 32); j += 2) {
667                         u16 *word = (u16 *)&buffer[j];
668
669                         *word = (j & 0x02) ? 0x5555 : 0xaaaa;
670                 }
671                 k = j;
672                 /* fill with shifting bits (test d) */
673                 for ( ; j < min(len, k + 32); j += 4) {
674                         u32 *word = (unsigned int *)&buffer[j];
675                         u32 roll = (pattern & 0x80000000) ? 1 : 0;
676                         
677                         *word = pattern;
678                         pattern = (pattern << 1) | roll;
679                 }
680                 /* don't bother with random data (test e) */
681         }
682
683         for (r = 0; r < retries; r++) {
684                 result = spi_execute(sdev, spi_write_buffer, DMA_TO_DEVICE,
685                                      buffer, len, &sshdr);
686                 if(result || !scsi_device_online(sdev)) {
687
688                         scsi_device_set_state(sdev, SDEV_QUIESCE);
689                         if (scsi_sense_valid(&sshdr)
690                             && sshdr.sense_key == ILLEGAL_REQUEST
691                             /* INVALID FIELD IN CDB */
692                             && sshdr.asc == 0x24 && sshdr.ascq == 0x00)
693                                 /* This would mean that the drive lied
694                                  * to us about supporting an echo
695                                  * buffer (unfortunately some Western
696                                  * Digital drives do precisely this)
697                                  */
698                                 return SPI_COMPARE_SKIP_TEST;
699
700
701                         sdev_printk(KERN_ERR, sdev, "Write Buffer failure %x\n", result);
702                         return SPI_COMPARE_FAILURE;
703                 }
704
705                 memset(ptr, 0, len);
706                 spi_execute(sdev, spi_read_buffer, DMA_FROM_DEVICE,
707                             ptr, len, NULL);
708                 scsi_device_set_state(sdev, SDEV_QUIESCE);
709
710                 if (memcmp(buffer, ptr, len) != 0)
711                         return SPI_COMPARE_FAILURE;
712         }
713         return SPI_COMPARE_SUCCESS;
714 }
715
716 /* This is for the simplest form of Domain Validation: a read test
717  * on the inquiry data from the device */
718 static enum spi_compare_returns
719 spi_dv_device_compare_inquiry(struct scsi_device *sdev, u8 *buffer,
720                               u8 *ptr, const int retries)
721 {
722         int r, result;
723         const int len = sdev->inquiry_len;
724         const char spi_inquiry[] = {
725                 INQUIRY, 0, 0, 0, len, 0
726         };
727
728         for (r = 0; r < retries; r++) {
729                 memset(ptr, 0, len);
730
731                 result = spi_execute(sdev, spi_inquiry, DMA_FROM_DEVICE,
732                                      ptr, len, NULL);
733                 
734                 if(result || !scsi_device_online(sdev)) {
735                         scsi_device_set_state(sdev, SDEV_QUIESCE);
736                         return SPI_COMPARE_FAILURE;
737                 }
738
739                 /* If we don't have the inquiry data already, the
740                  * first read gets it */
741                 if (ptr == buffer) {
742                         ptr += len;
743                         --r;
744                         continue;
745                 }
746
747                 if (memcmp(buffer, ptr, len) != 0)
748                         /* failure */
749                         return SPI_COMPARE_FAILURE;
750         }
751         return SPI_COMPARE_SUCCESS;
752 }
753
754 static enum spi_compare_returns
755 spi_dv_retrain(struct scsi_device *sdev, u8 *buffer, u8 *ptr,
756                enum spi_compare_returns 
757                (*compare_fn)(struct scsi_device *, u8 *, u8 *, int))
758 {
759         struct spi_internal *i = to_spi_internal(sdev->host->transportt);
760         struct scsi_target *starget = sdev->sdev_target;
761         int period = 0, prevperiod = 0; 
762         enum spi_compare_returns retval;
763
764
765         for (;;) {
766                 int newperiod;
767                 retval = compare_fn(sdev, buffer, ptr, DV_LOOPS);
768
769                 if (retval == SPI_COMPARE_SUCCESS
770                     || retval == SPI_COMPARE_SKIP_TEST)
771                         break;
772
773                 /* OK, retrain, fallback */
774                 if (i->f->get_iu)
775                         i->f->get_iu(starget);
776                 if (i->f->get_qas)
777                         i->f->get_qas(starget);
778                 if (i->f->get_period)
779                         i->f->get_period(sdev->sdev_target);
780
781                 /* Here's the fallback sequence; first try turning off
782                  * IU, then QAS (if we can control them), then finally
783                  * fall down the periods */
784                 if (i->f->set_iu && spi_iu(starget)) {
785                         starget_printk(KERN_ERR, starget, "Domain Validation Disabling Information Units\n");
786                         DV_SET(iu, 0);
787                 } else if (i->f->set_qas && spi_qas(starget)) {
788                         starget_printk(KERN_ERR, starget, "Domain Validation Disabling Quick Arbitration and Selection\n");
789                         DV_SET(qas, 0);
790                 } else {
791                         newperiod = spi_period(starget);
792                         period = newperiod > period ? newperiod : period;
793                         if (period < 0x0d)
794                                 period++;
795                         else
796                                 period += period >> 1;
797
798                         if (unlikely(period > 0xff || period == prevperiod)) {
799                                 /* Total failure; set to async and return */
800                                 starget_printk(KERN_ERR, starget, "Domain Validation Failure, dropping back to Asynchronous\n");
801                                 DV_SET(offset, 0);
802                                 return SPI_COMPARE_FAILURE;
803                         }
804                         starget_printk(KERN_ERR, starget, "Domain Validation detected failure, dropping back\n");
805                         DV_SET(period, period);
806                         prevperiod = period;
807                 }
808         }
809         return retval;
810 }
811
812 static int
813 spi_dv_device_get_echo_buffer(struct scsi_device *sdev, u8 *buffer)
814 {
815         int l, result;
816
817         /* first off do a test unit ready.  This can error out 
818          * because of reservations or some other reason.  If it
819          * fails, the device won't let us write to the echo buffer
820          * so just return failure */
821         
822         const char spi_test_unit_ready[] = {
823                 TEST_UNIT_READY, 0, 0, 0, 0, 0
824         };
825
826         const char spi_read_buffer_descriptor[] = {
827                 READ_BUFFER, 0x0b, 0, 0, 0, 0, 0, 0, 4, 0
828         };
829
830         
831         /* We send a set of three TURs to clear any outstanding 
832          * unit attention conditions if they exist (Otherwise the
833          * buffer tests won't be happy).  If the TUR still fails
834          * (reservation conflict, device not ready, etc) just
835          * skip the write tests */
836         for (l = 0; ; l++) {
837                 result = spi_execute(sdev, spi_test_unit_ready, DMA_NONE, 
838                                      NULL, 0, NULL);
839
840                 if(result) {
841                         if(l >= 3)
842                                 return 0;
843                 } else {
844                         /* TUR succeeded */
845                         break;
846                 }
847         }
848
849         result = spi_execute(sdev, spi_read_buffer_descriptor, 
850                              DMA_FROM_DEVICE, buffer, 4, NULL);
851
852         if (result)
853                 /* Device has no echo buffer */
854                 return 0;
855
856         return buffer[3] + ((buffer[2] & 0x1f) << 8);
857 }
858
859 static void
860 spi_dv_device_internal(struct scsi_device *sdev, u8 *buffer)
861 {
862         struct spi_internal *i = to_spi_internal(sdev->host->transportt);
863         struct scsi_target *starget = sdev->sdev_target;
864         struct Scsi_Host *shost = sdev->host;
865         int len = sdev->inquiry_len;
866         int min_period = spi_min_period(starget);
867         int max_width = spi_max_width(starget);
868         /* first set us up for narrow async */
869         DV_SET(offset, 0);
870         DV_SET(width, 0);
871
872         if (spi_dv_device_compare_inquiry(sdev, buffer, buffer, DV_LOOPS)
873             != SPI_COMPARE_SUCCESS) {
874                 starget_printk(KERN_ERR, starget, "Domain Validation Initial Inquiry Failed\n");
875                 /* FIXME: should probably offline the device here? */
876                 return;
877         }
878
879         if (!spi_support_wide(starget)) {
880                 spi_max_width(starget) = 0;
881                 max_width = 0;
882         }
883
884         /* test width */
885         if (i->f->set_width && max_width) {
886                 i->f->set_width(starget, 1);
887
888                 if (spi_dv_device_compare_inquiry(sdev, buffer,
889                                                    buffer + len,
890                                                    DV_LOOPS)
891                     != SPI_COMPARE_SUCCESS) {
892                         starget_printk(KERN_ERR, starget, "Wide Transfers Fail\n");
893                         i->f->set_width(starget, 0);
894                         /* Make sure we don't force wide back on by asking
895                          * for a transfer period that requires it */
896                         max_width = 0;
897                         if (min_period < 10)
898                                 min_period = 10;
899                 }
900         }
901
902         if (!i->f->set_period)
903                 return;
904
905         /* device can't handle synchronous */
906         if (!spi_support_sync(starget) && !spi_support_dt(starget))
907                 return;
908
909         /* len == -1 is the signal that we need to ascertain the
910          * presence of an echo buffer before trying to use it.  len ==
911          * 0 means we don't have an echo buffer */
912         len = -1;
913
914  retry:
915
916         /* now set up to the maximum */
917         DV_SET(offset, spi_max_offset(starget));
918         DV_SET(period, min_period);
919
920         /* try QAS requests; this should be harmless to set if the
921          * target supports it */
922         if (spi_support_qas(starget) && spi_max_qas(starget)) {
923                 DV_SET(qas, 1);
924         } else {
925                 DV_SET(qas, 0);
926         }
927
928         if (spi_support_ius(starget) && spi_max_iu(starget) &&
929             min_period < 9) {
930                 /* This u320 (or u640). Set IU transfers */
931                 DV_SET(iu, 1);
932                 /* Then set the optional parameters */
933                 DV_SET(rd_strm, 1);
934                 DV_SET(wr_flow, 1);
935                 DV_SET(rti, 1);
936                 if (min_period == 8)
937                         DV_SET(pcomp_en, 1);
938         } else {
939                 DV_SET(iu, 0);
940         }
941
942         /* now that we've done all this, actually check the bus
943          * signal type (if known).  Some devices are stupid on
944          * a SE bus and still claim they can try LVD only settings */
945         if (i->f->get_signalling)
946                 i->f->get_signalling(shost);
947         if (spi_signalling(shost) == SPI_SIGNAL_SE ||
948             spi_signalling(shost) == SPI_SIGNAL_HVD ||
949             !spi_support_dt(starget)) {
950                 DV_SET(dt, 0);
951         } else {
952                 DV_SET(dt, 1);
953         }
954         /* set width last because it will pull all the other
955          * parameters down to required values */
956         DV_SET(width, max_width);
957
958         /* Do the read only INQUIRY tests */
959         spi_dv_retrain(sdev, buffer, buffer + sdev->inquiry_len,
960                        spi_dv_device_compare_inquiry);
961         /* See if we actually managed to negotiate and sustain DT */
962         if (i->f->get_dt)
963                 i->f->get_dt(starget);
964
965         /* see if the device has an echo buffer.  If it does we can do
966          * the SPI pattern write tests.  Because of some broken
967          * devices, we *only* try this on a device that has actually
968          * negotiated DT */
969
970         if (len == -1 && spi_dt(starget))
971                 len = spi_dv_device_get_echo_buffer(sdev, buffer);
972
973         if (len <= 0) {
974                 starget_printk(KERN_INFO, starget, "Domain Validation skipping write tests\n");
975                 return;
976         }
977
978         if (len > SPI_MAX_ECHO_BUFFER_SIZE) {
979                 starget_printk(KERN_WARNING, starget, "Echo buffer size %d is too big, trimming to %d\n", len, SPI_MAX_ECHO_BUFFER_SIZE);
980                 len = SPI_MAX_ECHO_BUFFER_SIZE;
981         }
982
983         if (spi_dv_retrain(sdev, buffer, buffer + len,
984                            spi_dv_device_echo_buffer)
985             == SPI_COMPARE_SKIP_TEST) {
986                 /* OK, the stupid drive can't do a write echo buffer
987                  * test after all, fall back to the read tests */
988                 len = 0;
989                 goto retry;
990         }
991 }
992
993
994 /**     spi_dv_device - Do Domain Validation on the device
995  *      @sdev:          scsi device to validate
996  *
997  *      Performs the domain validation on the given device in the
998  *      current execution thread.  Since DV operations may sleep,
999  *      the current thread must have user context.  Also no SCSI
1000  *      related locks that would deadlock I/O issued by the DV may
1001  *      be held.
1002  */
1003 void
1004 spi_dv_device(struct scsi_device *sdev)
1005 {
1006         struct scsi_target *starget = sdev->sdev_target;
1007         u8 *buffer;
1008         const int len = SPI_MAX_ECHO_BUFFER_SIZE*2;
1009
1010         if (unlikely(spi_dv_in_progress(starget)))
1011                 return;
1012
1013         if (unlikely(scsi_device_get(sdev)))
1014                 return;
1015         spi_dv_in_progress(starget) = 1;
1016
1017         buffer = kzalloc(len, GFP_KERNEL);
1018
1019         if (unlikely(!buffer))
1020                 goto out_put;
1021
1022         /* We need to verify that the actual device will quiesce; the
1023          * later target quiesce is just a nice to have */
1024         if (unlikely(scsi_device_quiesce(sdev)))
1025                 goto out_free;
1026
1027         scsi_target_quiesce(starget);
1028
1029         spi_dv_pending(starget) = 1;
1030         mutex_lock(&spi_dv_mutex(starget));
1031
1032         starget_printk(KERN_INFO, starget, "Beginning Domain Validation\n");
1033
1034         spi_dv_device_internal(sdev, buffer);
1035
1036         starget_printk(KERN_INFO, starget, "Ending Domain Validation\n");
1037
1038         mutex_unlock(&spi_dv_mutex(starget));
1039         spi_dv_pending(starget) = 0;
1040
1041         scsi_target_resume(starget);
1042
1043         spi_initial_dv(starget) = 1;
1044
1045  out_free:
1046         kfree(buffer);
1047  out_put:
1048         spi_dv_in_progress(starget) = 0;
1049         scsi_device_put(sdev);
1050 }
1051 EXPORT_SYMBOL(spi_dv_device);
1052
1053 struct work_queue_wrapper {
1054         struct work_struct      work;
1055         struct scsi_device      *sdev;
1056 };
1057
1058 static void
1059 spi_dv_device_work_wrapper(struct work_struct *work)
1060 {
1061         struct work_queue_wrapper *wqw =
1062                 container_of(work, struct work_queue_wrapper, work);
1063         struct scsi_device *sdev = wqw->sdev;
1064
1065         kfree(wqw);
1066         spi_dv_device(sdev);
1067         spi_dv_pending(sdev->sdev_target) = 0;
1068         scsi_device_put(sdev);
1069 }
1070
1071
1072 /**
1073  *      spi_schedule_dv_device - schedule domain validation to occur on the device
1074  *      @sdev:  The device to validate
1075  *
1076  *      Identical to spi_dv_device() above, except that the DV will be
1077  *      scheduled to occur in a workqueue later.  All memory allocations
1078  *      are atomic, so may be called from any context including those holding
1079  *      SCSI locks.
1080  */
1081 void
1082 spi_schedule_dv_device(struct scsi_device *sdev)
1083 {
1084         struct work_queue_wrapper *wqw =
1085                 kmalloc(sizeof(struct work_queue_wrapper), GFP_ATOMIC);
1086
1087         if (unlikely(!wqw))
1088                 return;
1089
1090         if (unlikely(spi_dv_pending(sdev->sdev_target))) {
1091                 kfree(wqw);
1092                 return;
1093         }
1094         /* Set pending early (dv_device doesn't check it, only sets it) */
1095         spi_dv_pending(sdev->sdev_target) = 1;
1096         if (unlikely(scsi_device_get(sdev))) {
1097                 kfree(wqw);
1098                 spi_dv_pending(sdev->sdev_target) = 0;
1099                 return;
1100         }
1101
1102         INIT_WORK(&wqw->work, spi_dv_device_work_wrapper);
1103         wqw->sdev = sdev;
1104
1105         schedule_work(&wqw->work);
1106 }
1107 EXPORT_SYMBOL(spi_schedule_dv_device);
1108
1109 /**
1110  * spi_display_xfer_agreement - Print the current target transfer agreement
1111  * @starget: The target for which to display the agreement
1112  *
1113  * Each SPI port is required to maintain a transfer agreement for each
1114  * other port on the bus.  This function prints a one-line summary of
1115  * the current agreement; more detailed information is available in sysfs.
1116  */
1117 void spi_display_xfer_agreement(struct scsi_target *starget)
1118 {
1119         struct spi_transport_attrs *tp;
1120         tp = (struct spi_transport_attrs *)&starget->starget_data;
1121
1122         if (tp->offset > 0 && tp->period > 0) {
1123                 unsigned int picosec, kb100;
1124                 char *scsi = "FAST-?";
1125                 char tmp[8];
1126
1127                 if (tp->period <= SPI_STATIC_PPR) {
1128                         picosec = ppr_to_ps[tp->period];
1129                         switch (tp->period) {
1130                                 case  7: scsi = "FAST-320"; break;
1131                                 case  8: scsi = "FAST-160"; break;
1132                                 case  9: scsi = "FAST-80"; break;
1133                                 case 10:
1134                                 case 11: scsi = "FAST-40"; break;
1135                                 case 12: scsi = "FAST-20"; break;
1136                         }
1137                 } else {
1138                         picosec = tp->period * 4000;
1139                         if (tp->period < 25)
1140                                 scsi = "FAST-20";
1141                         else if (tp->period < 50)
1142                                 scsi = "FAST-10";
1143                         else
1144                                 scsi = "FAST-5";
1145                 }
1146
1147                 kb100 = (10000000 + picosec / 2) / picosec;
1148                 if (tp->width)
1149                         kb100 *= 2;
1150                 sprint_frac(tmp, picosec, 1000);
1151
1152                 dev_info(&starget->dev,
1153                          "%s %sSCSI %d.%d MB/s %s%s%s%s%s%s%s%s (%s ns, offset %d)\n",
1154                          scsi, tp->width ? "WIDE " : "", kb100/10, kb100 % 10,
1155                          tp->dt ? "DT" : "ST",
1156                          tp->iu ? " IU" : "",
1157                          tp->qas  ? " QAS" : "",
1158                          tp->rd_strm ? " RDSTRM" : "",
1159                          tp->rti ? " RTI" : "",
1160                          tp->wr_flow ? " WRFLOW" : "",
1161                          tp->pcomp_en ? " PCOMP" : "",
1162                          tp->hold_mcs ? " HMCS" : "",
1163                          tmp, tp->offset);
1164         } else {
1165                 dev_info(&starget->dev, "%sasynchronous\n",
1166                                 tp->width ? "wide " : "");
1167         }
1168 }
1169 EXPORT_SYMBOL(spi_display_xfer_agreement);
1170
1171 int spi_populate_width_msg(unsigned char *msg, int width)
1172 {
1173         msg[0] = EXTENDED_MESSAGE;
1174         msg[1] = 2;
1175         msg[2] = EXTENDED_WDTR;
1176         msg[3] = width;
1177         return 4;
1178 }
1179 EXPORT_SYMBOL_GPL(spi_populate_width_msg);
1180
1181 int spi_populate_sync_msg(unsigned char *msg, int period, int offset)
1182 {
1183         msg[0] = EXTENDED_MESSAGE;
1184         msg[1] = 3;
1185         msg[2] = EXTENDED_SDTR;
1186         msg[3] = period;
1187         msg[4] = offset;
1188         return 5;
1189 }
1190 EXPORT_SYMBOL_GPL(spi_populate_sync_msg);
1191
1192 int spi_populate_ppr_msg(unsigned char *msg, int period, int offset,
1193                 int width, int options)
1194 {
1195         msg[0] = EXTENDED_MESSAGE;
1196         msg[1] = 6;
1197         msg[2] = EXTENDED_PPR;
1198         msg[3] = period;
1199         msg[4] = 0;
1200         msg[5] = offset;
1201         msg[6] = width;
1202         msg[7] = options;
1203         return 8;
1204 }
1205 EXPORT_SYMBOL_GPL(spi_populate_ppr_msg);
1206
1207 /**
1208  * spi_populate_tag_msg - place a tag message in a buffer
1209  * @msg:        pointer to the area to place the tag
1210  * @cmd:        pointer to the scsi command for the tag
1211  *
1212  * Notes:
1213  *      designed to create the correct type of tag message for the 
1214  *      particular request.  Returns the size of the tag message.
1215  *      May return 0 if TCQ is disabled for this device.
1216  **/
1217 int spi_populate_tag_msg(unsigned char *msg, struct scsi_cmnd *cmd)
1218 {
1219         if (cmd->flags & SCMD_TAGGED) {
1220                 *msg++ = SIMPLE_QUEUE_TAG;
1221                 *msg++ = cmd->request->tag;
1222                 return 2;
1223         }
1224
1225         return 0;
1226 }
1227 EXPORT_SYMBOL_GPL(spi_populate_tag_msg);
1228
1229 #ifdef CONFIG_SCSI_CONSTANTS
1230 static const char * const one_byte_msgs[] = {
1231 /* 0x00 */ "Task Complete", NULL /* Extended Message */, "Save Pointers",
1232 /* 0x03 */ "Restore Pointers", "Disconnect", "Initiator Error", 
1233 /* 0x06 */ "Abort Task Set", "Message Reject", "Nop", "Message Parity Error",
1234 /* 0x0a */ "Linked Command Complete", "Linked Command Complete w/flag",
1235 /* 0x0c */ "Target Reset", "Abort Task", "Clear Task Set", 
1236 /* 0x0f */ "Initiate Recovery", "Release Recovery",
1237 /* 0x11 */ "Terminate Process", "Continue Task", "Target Transfer Disable",
1238 /* 0x14 */ NULL, NULL, "Clear ACA", "LUN Reset"
1239 };
1240
1241 static const char * const two_byte_msgs[] = {
1242 /* 0x20 */ "Simple Queue Tag", "Head of Queue Tag", "Ordered Queue Tag",
1243 /* 0x23 */ "Ignore Wide Residue", "ACA"
1244 };
1245
1246 static const char * const extended_msgs[] = {
1247 /* 0x00 */ "Modify Data Pointer", "Synchronous Data Transfer Request",
1248 /* 0x02 */ "SCSI-I Extended Identify", "Wide Data Transfer Request",
1249 /* 0x04 */ "Parallel Protocol Request", "Modify Bidirectional Data Pointer"
1250 };
1251
1252 static void print_nego(const unsigned char *msg, int per, int off, int width)
1253 {
1254         if (per) {
1255                 char buf[20];
1256                 period_to_str(buf, msg[per]);
1257                 printk("period = %s ns ", buf);
1258         }
1259
1260         if (off)
1261                 printk("offset = %d ", msg[off]);
1262         if (width)
1263                 printk("width = %d ", 8 << msg[width]);
1264 }
1265
1266 static void print_ptr(const unsigned char *msg, int msb, const char *desc)
1267 {
1268         int ptr = (msg[msb] << 24) | (msg[msb+1] << 16) | (msg[msb+2] << 8) |
1269                         msg[msb+3];
1270         printk("%s = %d ", desc, ptr);
1271 }
1272
1273 int spi_print_msg(const unsigned char *msg)
1274 {
1275         int len = 1, i;
1276         if (msg[0] == EXTENDED_MESSAGE) {
1277                 len = 2 + msg[1];
1278                 if (len == 2)
1279                         len += 256;
1280                 if (msg[2] < ARRAY_SIZE(extended_msgs))
1281                         printk ("%s ", extended_msgs[msg[2]]); 
1282                 else 
1283                         printk ("Extended Message, reserved code (0x%02x) ",
1284                                 (int) msg[2]);
1285                 switch (msg[2]) {
1286                 case EXTENDED_MODIFY_DATA_POINTER:
1287                         print_ptr(msg, 3, "pointer");
1288                         break;
1289                 case EXTENDED_SDTR:
1290                         print_nego(msg, 3, 4, 0);
1291                         break;
1292                 case EXTENDED_WDTR:
1293                         print_nego(msg, 0, 0, 3);
1294                         break;
1295                 case EXTENDED_PPR:
1296                         print_nego(msg, 3, 5, 6);
1297                         break;
1298                 case EXTENDED_MODIFY_BIDI_DATA_PTR:
1299                         print_ptr(msg, 3, "out");
1300                         print_ptr(msg, 7, "in");
1301                         break;
1302                 default:
1303                 for (i = 2; i < len; ++i) 
1304                         printk("%02x ", msg[i]);
1305                 }
1306         /* Identify */
1307         } else if (msg[0] & 0x80) {
1308                 printk("Identify disconnect %sallowed %s %d ",
1309                         (msg[0] & 0x40) ? "" : "not ",
1310                         (msg[0] & 0x20) ? "target routine" : "lun",
1311                         msg[0] & 0x7);
1312         /* Normal One byte */
1313         } else if (msg[0] < 0x1f) {
1314                 if (msg[0] < ARRAY_SIZE(one_byte_msgs) && one_byte_msgs[msg[0]])
1315                         printk("%s ", one_byte_msgs[msg[0]]);
1316                 else
1317                         printk("reserved (%02x) ", msg[0]);
1318         } else if (msg[0] == 0x55) {
1319                 printk("QAS Request ");
1320         /* Two byte */
1321         } else if (msg[0] <= 0x2f) {
1322                 if ((msg[0] - 0x20) < ARRAY_SIZE(two_byte_msgs))
1323                         printk("%s %02x ", two_byte_msgs[msg[0] - 0x20], 
1324                                 msg[1]);
1325                 else 
1326                         printk("reserved two byte (%02x %02x) ", 
1327                                 msg[0], msg[1]);
1328                 len = 2;
1329         } else 
1330                 printk("reserved ");
1331         return len;
1332 }
1333 EXPORT_SYMBOL(spi_print_msg);
1334
1335 #else  /* ifndef CONFIG_SCSI_CONSTANTS */
1336
1337 int spi_print_msg(const unsigned char *msg)
1338 {
1339         int len = 1, i;
1340
1341         if (msg[0] == EXTENDED_MESSAGE) {
1342                 len = 2 + msg[1];
1343                 if (len == 2)
1344                         len += 256;
1345                 for (i = 0; i < len; ++i)
1346                         printk("%02x ", msg[i]);
1347         /* Identify */
1348         } else if (msg[0] & 0x80) {
1349                 printk("%02x ", msg[0]);
1350         /* Normal One byte */
1351         } else if ((msg[0] < 0x1f) || (msg[0] == 0x55)) {
1352                 printk("%02x ", msg[0]);
1353         /* Two byte */
1354         } else if (msg[0] <= 0x2f) {
1355                 printk("%02x %02x", msg[0], msg[1]);
1356                 len = 2;
1357         } else 
1358                 printk("%02x ", msg[0]);
1359         return len;
1360 }
1361 EXPORT_SYMBOL(spi_print_msg);
1362 #endif /* ! CONFIG_SCSI_CONSTANTS */
1363
1364 static int spi_device_match(struct attribute_container *cont,
1365                             struct device *dev)
1366 {
1367         struct scsi_device *sdev;
1368         struct Scsi_Host *shost;
1369         struct spi_internal *i;
1370
1371         if (!scsi_is_sdev_device(dev))
1372                 return 0;
1373
1374         sdev = to_scsi_device(dev);
1375         shost = sdev->host;
1376         if (!shost->transportt  || shost->transportt->host_attrs.ac.class
1377             != &spi_host_class.class)
1378                 return 0;
1379         /* Note: this class has no device attributes, so it has
1380          * no per-HBA allocation and thus we don't need to distinguish
1381          * the attribute containers for the device */
1382         i = to_spi_internal(shost->transportt);
1383         if (i->f->deny_binding && i->f->deny_binding(sdev->sdev_target))
1384                 return 0;
1385         return 1;
1386 }
1387
1388 static int spi_target_match(struct attribute_container *cont,
1389                             struct device *dev)
1390 {
1391         struct Scsi_Host *shost;
1392         struct scsi_target *starget;
1393         struct spi_internal *i;
1394
1395         if (!scsi_is_target_device(dev))
1396                 return 0;
1397
1398         shost = dev_to_shost(dev->parent);
1399         if (!shost->transportt  || shost->transportt->host_attrs.ac.class
1400             != &spi_host_class.class)
1401                 return 0;
1402
1403         i = to_spi_internal(shost->transportt);
1404         starget = to_scsi_target(dev);
1405
1406         if (i->f->deny_binding && i->f->deny_binding(starget))
1407                 return 0;
1408
1409         return &i->t.target_attrs.ac == cont;
1410 }
1411
1412 static DECLARE_TRANSPORT_CLASS(spi_transport_class,
1413                                "spi_transport",
1414                                spi_setup_transport_attrs,
1415                                NULL,
1416                                spi_target_configure);
1417
1418 static DECLARE_ANON_TRANSPORT_CLASS(spi_device_class,
1419                                     spi_device_match,
1420                                     spi_device_configure);
1421
1422 static struct attribute *host_attributes[] = {
1423         &dev_attr_signalling.attr,
1424         &dev_attr_host_width.attr,
1425         &dev_attr_hba_id.attr,
1426         NULL
1427 };
1428
1429 static struct attribute_group host_attribute_group = {
1430         .attrs = host_attributes,
1431 };
1432
1433 static int spi_host_configure(struct transport_container *tc,
1434                               struct device *dev,
1435                               struct device *cdev)
1436 {
1437         struct kobject *kobj = &cdev->kobj;
1438         struct Scsi_Host *shost = transport_class_to_shost(cdev);
1439         struct spi_internal *si = to_spi_internal(shost->transportt);
1440         struct attribute *attr = &dev_attr_signalling.attr;
1441         int rc = 0;
1442
1443         if (si->f->set_signalling)
1444                 rc = sysfs_chmod_file(kobj, attr, attr->mode | S_IWUSR);
1445
1446         return rc;
1447 }
1448
1449 /* returns true if we should be showing the variable.  Also
1450  * overloads the return by setting 1<<1 if the attribute should
1451  * be writeable */
1452 #define TARGET_ATTRIBUTE_HELPER(name) \
1453         (si->f->show_##name ? S_IRUGO : 0) | \
1454         (si->f->set_##name ? S_IWUSR : 0)
1455
1456 static umode_t target_attribute_is_visible(struct kobject *kobj,
1457                                           struct attribute *attr, int i)
1458 {
1459         struct device *cdev = container_of(kobj, struct device, kobj);
1460         struct scsi_target *starget = transport_class_to_starget(cdev);
1461         struct Scsi_Host *shost = transport_class_to_shost(cdev);
1462         struct spi_internal *si = to_spi_internal(shost->transportt);
1463
1464         if (attr == &dev_attr_period.attr &&
1465             spi_support_sync(starget))
1466                 return TARGET_ATTRIBUTE_HELPER(period);
1467         else if (attr == &dev_attr_min_period.attr &&
1468                  spi_support_sync(starget))
1469                 return TARGET_ATTRIBUTE_HELPER(period);
1470         else if (attr == &dev_attr_offset.attr &&
1471                  spi_support_sync(starget))
1472                 return TARGET_ATTRIBUTE_HELPER(offset);
1473         else if (attr == &dev_attr_max_offset.attr &&
1474                  spi_support_sync(starget))
1475                 return TARGET_ATTRIBUTE_HELPER(offset);
1476         else if (attr == &dev_attr_width.attr &&
1477                  spi_support_wide(starget))
1478                 return TARGET_ATTRIBUTE_HELPER(width);
1479         else if (attr == &dev_attr_max_width.attr &&
1480                  spi_support_wide(starget))
1481                 return TARGET_ATTRIBUTE_HELPER(width);
1482         else if (attr == &dev_attr_iu.attr &&
1483                  spi_support_ius(starget))
1484                 return TARGET_ATTRIBUTE_HELPER(iu);
1485         else if (attr == &dev_attr_max_iu.attr &&
1486                  spi_support_ius(starget))
1487                 return TARGET_ATTRIBUTE_HELPER(iu);
1488         else if (attr == &dev_attr_dt.attr &&
1489                  spi_support_dt(starget))
1490                 return TARGET_ATTRIBUTE_HELPER(dt);
1491         else if (attr == &dev_attr_qas.attr &&
1492                  spi_support_qas(starget))
1493                 return TARGET_ATTRIBUTE_HELPER(qas);
1494         else if (attr == &dev_attr_max_qas.attr &&
1495                  spi_support_qas(starget))
1496                 return TARGET_ATTRIBUTE_HELPER(qas);
1497         else if (attr == &dev_attr_wr_flow.attr &&
1498                  spi_support_ius(starget))
1499                 return TARGET_ATTRIBUTE_HELPER(wr_flow);
1500         else if (attr == &dev_attr_rd_strm.attr &&
1501                  spi_support_ius(starget))
1502                 return TARGET_ATTRIBUTE_HELPER(rd_strm);
1503         else if (attr == &dev_attr_rti.attr &&
1504                  spi_support_ius(starget))
1505                 return TARGET_ATTRIBUTE_HELPER(rti);
1506         else if (attr == &dev_attr_pcomp_en.attr &&
1507                  spi_support_ius(starget))
1508                 return TARGET_ATTRIBUTE_HELPER(pcomp_en);
1509         else if (attr == &dev_attr_hold_mcs.attr &&
1510                  spi_support_ius(starget))
1511                 return TARGET_ATTRIBUTE_HELPER(hold_mcs);
1512         else if (attr == &dev_attr_revalidate.attr)
1513                 return S_IWUSR;
1514
1515         return 0;
1516 }
1517
1518 static struct attribute *target_attributes[] = {
1519         &dev_attr_period.attr,
1520         &dev_attr_min_period.attr,
1521         &dev_attr_offset.attr,
1522         &dev_attr_max_offset.attr,
1523         &dev_attr_width.attr,
1524         &dev_attr_max_width.attr,
1525         &dev_attr_iu.attr,
1526         &dev_attr_max_iu.attr,
1527         &dev_attr_dt.attr,
1528         &dev_attr_qas.attr,
1529         &dev_attr_max_qas.attr,
1530         &dev_attr_wr_flow.attr,
1531         &dev_attr_rd_strm.attr,
1532         &dev_attr_rti.attr,
1533         &dev_attr_pcomp_en.attr,
1534         &dev_attr_hold_mcs.attr,
1535         &dev_attr_revalidate.attr,
1536         NULL
1537 };
1538
1539 static struct attribute_group target_attribute_group = {
1540         .attrs = target_attributes,
1541         .is_visible = target_attribute_is_visible,
1542 };
1543
1544 static int spi_target_configure(struct transport_container *tc,
1545                                 struct device *dev,
1546                                 struct device *cdev)
1547 {
1548         struct kobject *kobj = &cdev->kobj;
1549
1550         /* force an update based on parameters read from the device */
1551         sysfs_update_group(kobj, &target_attribute_group);
1552
1553         return 0;
1554 }
1555
1556 struct scsi_transport_template *
1557 spi_attach_transport(struct spi_function_template *ft)
1558 {
1559         struct spi_internal *i = kzalloc(sizeof(struct spi_internal),
1560                                          GFP_KERNEL);
1561
1562         if (unlikely(!i))
1563                 return NULL;
1564
1565         i->t.target_attrs.ac.class = &spi_transport_class.class;
1566         i->t.target_attrs.ac.grp = &target_attribute_group;
1567         i->t.target_attrs.ac.match = spi_target_match;
1568         transport_container_register(&i->t.target_attrs);
1569         i->t.target_size = sizeof(struct spi_transport_attrs);
1570         i->t.host_attrs.ac.class = &spi_host_class.class;
1571         i->t.host_attrs.ac.grp = &host_attribute_group;
1572         i->t.host_attrs.ac.match = spi_host_match;
1573         transport_container_register(&i->t.host_attrs);
1574         i->t.host_size = sizeof(struct spi_host_attrs);
1575         i->f = ft;
1576
1577         return &i->t;
1578 }
1579 EXPORT_SYMBOL(spi_attach_transport);
1580
1581 void spi_release_transport(struct scsi_transport_template *t)
1582 {
1583         struct spi_internal *i = to_spi_internal(t);
1584
1585         transport_container_unregister(&i->t.target_attrs);
1586         transport_container_unregister(&i->t.host_attrs);
1587
1588         kfree(i);
1589 }
1590 EXPORT_SYMBOL(spi_release_transport);
1591
1592 static __init int spi_transport_init(void)
1593 {
1594         int error = scsi_dev_info_add_list(SCSI_DEVINFO_SPI,
1595                                            "SCSI Parallel Transport Class");
1596         if (!error) {
1597                 int i;
1598
1599                 for (i = 0; spi_static_device_list[i].vendor; i++)
1600                         scsi_dev_info_list_add_keyed(1, /* compatible */
1601                                                      spi_static_device_list[i].vendor,
1602                                                      spi_static_device_list[i].model,
1603                                                      NULL,
1604                                                      spi_static_device_list[i].flags,
1605                                                      SCSI_DEVINFO_SPI);
1606         }
1607
1608         error = transport_class_register(&spi_transport_class);
1609         if (error)
1610                 return error;
1611         error = anon_transport_class_register(&spi_device_class);
1612         return transport_class_register(&spi_host_class);
1613 }
1614
1615 static void __exit spi_transport_exit(void)
1616 {
1617         transport_class_unregister(&spi_transport_class);
1618         anon_transport_class_unregister(&spi_device_class);
1619         transport_class_unregister(&spi_host_class);
1620         scsi_dev_info_remove_list(SCSI_DEVINFO_SPI);
1621 }
1622
1623 MODULE_AUTHOR("Martin Hicks");
1624 MODULE_DESCRIPTION("SPI Transport Attributes");
1625 MODULE_LICENSE("GPL");
1626
1627 module_init(spi_transport_init);
1628 module_exit(spi_transport_exit);