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