[PATCH] I2C: Move hwmon drivers (1/3)
[linux-2.6] / drivers / scsi / scsi_transport_spi.c
1 /* 
2  *  Parallel SCSI (SPI) transport specific attributes exported to sysfs.
3  *
4  *  Copyright (c) 2003 Silicon Graphics, Inc.  All rights reserved.
5  *  Copyright (c) 2004, 2005 James Bottomley <James.Bottomley@SteelEye.com>
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21 #include <linux/ctype.h>
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/workqueue.h>
25 #include <linux/blkdev.h>
26 #include <asm/semaphore.h>
27 #include <scsi/scsi.h>
28 #include "scsi_priv.h"
29 #include <scsi/scsi_device.h>
30 #include <scsi/scsi_host.h>
31 #include <scsi/scsi_request.h>
32 #include <scsi/scsi_eh.h>
33 #include <scsi/scsi_transport.h>
34 #include <scsi/scsi_transport_spi.h>
35
36 #define SPI_PRINTK(x, l, f, a...)       dev_printk(l, &(x)->dev, f , ##a)
37
38 #define SPI_NUM_ATTRS 13        /* increase this if you add attributes */
39 #define SPI_OTHER_ATTRS 1       /* Increase this if you add "always
40                                  * on" attributes */
41 #define SPI_HOST_ATTRS  1
42
43 #define SPI_MAX_ECHO_BUFFER_SIZE        4096
44
45 #define DV_LOOPS        3
46 #define DV_TIMEOUT      (10*HZ)
47 #define DV_RETRIES      3       /* should only need at most 
48                                  * two cc/ua clears */
49
50 /* Private data accessors (keep these out of the header file) */
51 #define spi_dv_pending(x) (((struct spi_transport_attrs *)&(x)->starget_data)->dv_pending)
52 #define spi_dv_sem(x) (((struct spi_transport_attrs *)&(x)->starget_data)->dv_sem)
53
54 struct spi_internal {
55         struct scsi_transport_template t;
56         struct spi_function_template *f;
57         /* The actual attributes */
58         struct class_device_attribute private_attrs[SPI_NUM_ATTRS];
59         /* The array of null terminated pointers to attributes 
60          * needed by scsi_sysfs.c */
61         struct class_device_attribute *attrs[SPI_NUM_ATTRS + SPI_OTHER_ATTRS + 1];
62         struct class_device_attribute private_host_attrs[SPI_HOST_ATTRS];
63         struct class_device_attribute *host_attrs[SPI_HOST_ATTRS + 1];
64 };
65
66 #define to_spi_internal(tmpl)   container_of(tmpl, struct spi_internal, t)
67
68 static const int ppr_to_ps[] = {
69         /* The PPR values 0-6 are reserved, fill them in when
70          * the committee defines them */
71         -1,                     /* 0x00 */
72         -1,                     /* 0x01 */
73         -1,                     /* 0x02 */
74         -1,                     /* 0x03 */
75         -1,                     /* 0x04 */
76         -1,                     /* 0x05 */
77         -1,                     /* 0x06 */
78          3125,                  /* 0x07 */
79          6250,                  /* 0x08 */
80         12500,                  /* 0x09 */
81         25000,                  /* 0x0a */
82         30300,                  /* 0x0b */
83         50000,                  /* 0x0c */
84 };
85 /* The PPR values at which you calculate the period in ns by multiplying
86  * by 4 */
87 #define SPI_STATIC_PPR  0x0c
88
89 static int sprint_frac(char *dest, int value, int denom)
90 {
91         int frac = value % denom;
92         int result = sprintf(dest, "%d", value / denom);
93
94         if (frac == 0)
95                 return result;
96         dest[result++] = '.';
97
98         do {
99                 denom /= 10;
100                 sprintf(dest + result, "%d", frac / denom);
101                 result++;
102                 frac %= denom;
103         } while (frac);
104
105         dest[result++] = '\0';
106         return result;
107 }
108
109 /* Modification of scsi_wait_req that will clear UNIT ATTENTION conditions
110  * resulting from (likely) bus and device resets */
111 static void spi_wait_req(struct scsi_request *sreq, const void *cmd,
112                          void *buffer, unsigned bufflen)
113 {
114         int i;
115
116         for(i = 0; i < DV_RETRIES; i++) {
117                 sreq->sr_request->flags |= REQ_FAILFAST;
118
119                 scsi_wait_req(sreq, cmd, buffer, bufflen,
120                               DV_TIMEOUT, /* retries */ 1);
121                 if (sreq->sr_result & DRIVER_SENSE) {
122                         struct scsi_sense_hdr sshdr;
123
124                         if (scsi_request_normalize_sense(sreq, &sshdr)
125                             && sshdr.sense_key == UNIT_ATTENTION)
126                                 continue;
127                 }
128                 break;
129         }
130 }
131
132 static struct {
133         enum spi_signal_type    value;
134         char                    *name;
135 } signal_types[] = {
136         { SPI_SIGNAL_UNKNOWN, "unknown" },
137         { SPI_SIGNAL_SE, "SE" },
138         { SPI_SIGNAL_LVD, "LVD" },
139         { SPI_SIGNAL_HVD, "HVD" },
140 };
141
142 static inline const char *spi_signal_to_string(enum spi_signal_type type)
143 {
144         int i;
145
146         for (i = 0; i < sizeof(signal_types)/sizeof(signal_types[0]); i++) {
147                 if (type == signal_types[i].value)
148                         return signal_types[i].name;
149         }
150         return NULL;
151 }
152 static inline enum spi_signal_type spi_signal_to_value(const char *name)
153 {
154         int i, len;
155
156         for (i = 0; i < sizeof(signal_types)/sizeof(signal_types[0]); i++) {
157                 len =  strlen(signal_types[i].name);
158                 if (strncmp(name, signal_types[i].name, len) == 0 &&
159                     (name[len] == '\n' || name[len] == '\0'))
160                         return signal_types[i].value;
161         }
162         return SPI_SIGNAL_UNKNOWN;
163 }
164
165 static int spi_host_setup(struct device *dev)
166 {
167         struct Scsi_Host *shost = dev_to_shost(dev);
168
169         spi_signalling(shost) = SPI_SIGNAL_UNKNOWN;
170
171         return 0;
172 }
173
174 static DECLARE_TRANSPORT_CLASS(spi_host_class,
175                                "spi_host",
176                                spi_host_setup,
177                                NULL,
178                                NULL);
179
180 static int spi_host_match(struct attribute_container *cont,
181                           struct device *dev)
182 {
183         struct Scsi_Host *shost;
184         struct spi_internal *i;
185
186         if (!scsi_is_host_device(dev))
187                 return 0;
188
189         shost = dev_to_shost(dev);
190         if (!shost->transportt  || shost->transportt->host_attrs.ac.class
191             != &spi_host_class.class)
192                 return 0;
193
194         i = to_spi_internal(shost->transportt);
195         
196         return &i->t.host_attrs.ac == cont;
197 }
198
199 static int spi_device_configure(struct device *dev)
200 {
201         struct scsi_device *sdev = to_scsi_device(dev);
202         struct scsi_target *starget = sdev->sdev_target;
203
204         /* Populate the target capability fields with the values
205          * gleaned from the device inquiry */
206
207         spi_support_sync(starget) = scsi_device_sync(sdev);
208         spi_support_wide(starget) = scsi_device_wide(sdev);
209         spi_support_dt(starget) = scsi_device_dt(sdev);
210         spi_support_dt_only(starget) = scsi_device_dt_only(sdev);
211         spi_support_ius(starget) = scsi_device_ius(sdev);
212         spi_support_qas(starget) = scsi_device_qas(sdev);
213
214         return 0;
215 }
216
217 static int spi_setup_transport_attrs(struct device *dev)
218 {
219         struct scsi_target *starget = to_scsi_target(dev);
220
221         spi_period(starget) = -1;       /* illegal value */
222         spi_min_period(starget) = 0;
223         spi_offset(starget) = 0;        /* async */
224         spi_max_offset(starget) = 255;
225         spi_width(starget) = 0; /* narrow */
226         spi_max_width(starget) = 1;
227         spi_iu(starget) = 0;    /* no IU */
228         spi_dt(starget) = 0;    /* ST */
229         spi_qas(starget) = 0;
230         spi_wr_flow(starget) = 0;
231         spi_rd_strm(starget) = 0;
232         spi_rti(starget) = 0;
233         spi_pcomp_en(starget) = 0;
234         spi_dv_pending(starget) = 0;
235         spi_initial_dv(starget) = 0;
236         init_MUTEX(&spi_dv_sem(starget));
237
238         return 0;
239 }
240
241 #define spi_transport_show_simple(field, format_string)                 \
242                                                                         \
243 static ssize_t                                                          \
244 show_spi_transport_##field(struct class_device *cdev, char *buf)        \
245 {                                                                       \
246         struct scsi_target *starget = transport_class_to_starget(cdev); \
247         struct spi_transport_attrs *tp;                                 \
248                                                                         \
249         tp = (struct spi_transport_attrs *)&starget->starget_data;      \
250         return snprintf(buf, 20, format_string, tp->field);             \
251 }
252
253 #define spi_transport_store_simple(field, format_string)                \
254                                                                         \
255 static ssize_t                                                          \
256 store_spi_transport_##field(struct class_device *cdev, const char *buf, \
257                             size_t count)                               \
258 {                                                                       \
259         int val;                                                        \
260         struct scsi_target *starget = transport_class_to_starget(cdev); \
261         struct spi_transport_attrs *tp;                                 \
262                                                                         \
263         tp = (struct spi_transport_attrs *)&starget->starget_data;      \
264         val = simple_strtoul(buf, NULL, 0);                             \
265         tp->field = val;                                                \
266         return count;                                                   \
267 }
268
269 #define spi_transport_show_function(field, format_string)               \
270                                                                         \
271 static ssize_t                                                          \
272 show_spi_transport_##field(struct class_device *cdev, char *buf)        \
273 {                                                                       \
274         struct scsi_target *starget = transport_class_to_starget(cdev); \
275         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);    \
276         struct spi_transport_attrs *tp;                                 \
277         struct spi_internal *i = to_spi_internal(shost->transportt);    \
278         tp = (struct spi_transport_attrs *)&starget->starget_data;      \
279         if (i->f->get_##field)                                          \
280                 i->f->get_##field(starget);                             \
281         return snprintf(buf, 20, format_string, tp->field);             \
282 }
283
284 #define spi_transport_store_function(field, format_string)              \
285 static ssize_t                                                          \
286 store_spi_transport_##field(struct class_device *cdev, const char *buf, \
287                             size_t count)                               \
288 {                                                                       \
289         int val;                                                        \
290         struct scsi_target *starget = transport_class_to_starget(cdev); \
291         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);    \
292         struct spi_internal *i = to_spi_internal(shost->transportt);    \
293                                                                         \
294         val = simple_strtoul(buf, NULL, 0);                             \
295         i->f->set_##field(starget, val);                        \
296         return count;                                                   \
297 }
298
299 #define spi_transport_store_max(field, format_string)                   \
300 static ssize_t                                                          \
301 store_spi_transport_##field(struct class_device *cdev, const char *buf, \
302                             size_t count)                               \
303 {                                                                       \
304         int val;                                                        \
305         struct scsi_target *starget = transport_class_to_starget(cdev); \
306         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);    \
307         struct spi_internal *i = to_spi_internal(shost->transportt);    \
308         struct spi_transport_attrs *tp                                  \
309                 = (struct spi_transport_attrs *)&starget->starget_data; \
310                                                                         \
311         val = simple_strtoul(buf, NULL, 0);                             \
312         if (val > tp->max_##field)                                      \
313                 val = tp->max_##field;                                  \
314         i->f->set_##field(starget, val);                                \
315         return count;                                                   \
316 }
317
318 #define spi_transport_rd_attr(field, format_string)                     \
319         spi_transport_show_function(field, format_string)               \
320         spi_transport_store_function(field, format_string)              \
321 static CLASS_DEVICE_ATTR(field, S_IRUGO | S_IWUSR,                      \
322                          show_spi_transport_##field,                    \
323                          store_spi_transport_##field);
324
325 #define spi_transport_simple_attr(field, format_string)                 \
326         spi_transport_show_simple(field, format_string)                 \
327         spi_transport_store_simple(field, format_string)                \
328 static CLASS_DEVICE_ATTR(field, S_IRUGO | S_IWUSR,                      \
329                          show_spi_transport_##field,                    \
330                          store_spi_transport_##field);
331
332 #define spi_transport_max_attr(field, format_string)                    \
333         spi_transport_show_function(field, format_string)               \
334         spi_transport_store_max(field, format_string)                   \
335         spi_transport_simple_attr(max_##field, format_string)           \
336 static CLASS_DEVICE_ATTR(field, S_IRUGO | S_IWUSR,                      \
337                          show_spi_transport_##field,                    \
338                          store_spi_transport_##field);
339
340 /* The Parallel SCSI Tranport Attributes: */
341 spi_transport_max_attr(offset, "%d\n");
342 spi_transport_max_attr(width, "%d\n");
343 spi_transport_rd_attr(iu, "%d\n");
344 spi_transport_rd_attr(dt, "%d\n");
345 spi_transport_rd_attr(qas, "%d\n");
346 spi_transport_rd_attr(wr_flow, "%d\n");
347 spi_transport_rd_attr(rd_strm, "%d\n");
348 spi_transport_rd_attr(rti, "%d\n");
349 spi_transport_rd_attr(pcomp_en, "%d\n");
350
351 /* we only care about the first child device so we return 1 */
352 static int child_iter(struct device *dev, void *data)
353 {
354         struct scsi_device *sdev = to_scsi_device(dev);
355
356         spi_dv_device(sdev);
357         return 1;
358 }
359
360 static ssize_t
361 store_spi_revalidate(struct class_device *cdev, const char *buf, size_t count)
362 {
363         struct scsi_target *starget = transport_class_to_starget(cdev);
364
365         device_for_each_child(&starget->dev, NULL, child_iter);
366         return count;
367 }
368 static CLASS_DEVICE_ATTR(revalidate, S_IWUSR, NULL, store_spi_revalidate);
369
370 /* Translate the period into ns according to the current spec
371  * for SDTR/PPR messages */
372 static ssize_t
373 show_spi_transport_period_helper(struct class_device *cdev, char *buf,
374                                  int period)
375 {
376         int len, picosec;
377
378         if (period < 0 || period > 0xff) {
379                 picosec = -1;
380         } else if (period <= SPI_STATIC_PPR) {
381                 picosec = ppr_to_ps[period];
382         } else {
383                 picosec = period * 4000;
384         }
385
386         if (picosec == -1) {
387                 len = sprintf(buf, "reserved");
388         } else {
389                 len = sprint_frac(buf, picosec, 1000);
390         }
391
392         buf[len++] = '\n';
393         buf[len] = '\0';
394         return len;
395 }
396
397 static ssize_t
398 store_spi_transport_period_helper(struct class_device *cdev, const char *buf,
399                                   size_t count, int *periodp)
400 {
401         int j, picosec, period = -1;
402         char *endp;
403
404         picosec = simple_strtoul(buf, &endp, 10) * 1000;
405         if (*endp == '.') {
406                 int mult = 100;
407                 do {
408                         endp++;
409                         if (!isdigit(*endp))
410                                 break;
411                         picosec += (*endp - '0') * mult;
412                         mult /= 10;
413                 } while (mult > 0);
414         }
415
416         for (j = 0; j <= SPI_STATIC_PPR; j++) {
417                 if (ppr_to_ps[j] < picosec)
418                         continue;
419                 period = j;
420                 break;
421         }
422
423         if (period == -1)
424                 period = picosec / 4000;
425
426         if (period > 0xff)
427                 period = 0xff;
428
429         *periodp = period;
430
431         return count;
432 }
433
434 static ssize_t
435 show_spi_transport_period(struct class_device *cdev, char *buf)
436 {
437         struct scsi_target *starget = transport_class_to_starget(cdev);
438         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
439         struct spi_internal *i = to_spi_internal(shost->transportt);
440         struct spi_transport_attrs *tp =
441                 (struct spi_transport_attrs *)&starget->starget_data;
442
443         if (i->f->get_period)
444                 i->f->get_period(starget);
445
446         return show_spi_transport_period_helper(cdev, buf, tp->period);
447 }
448
449 static ssize_t
450 store_spi_transport_period(struct class_device *cdev, const char *buf,
451                             size_t count)
452 {
453         struct scsi_target *starget = transport_class_to_starget(cdev);
454         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
455         struct spi_internal *i = to_spi_internal(shost->transportt);
456         struct spi_transport_attrs *tp =
457                 (struct spi_transport_attrs *)&starget->starget_data;
458         int period, retval;
459
460         retval = store_spi_transport_period_helper(cdev, buf, count, &period);
461
462         if (period < tp->min_period)
463                 period = tp->min_period;
464
465         i->f->set_period(starget, period);
466
467         return retval;
468 }
469
470 static CLASS_DEVICE_ATTR(period, S_IRUGO | S_IWUSR, 
471                          show_spi_transport_period,
472                          store_spi_transport_period);
473
474 static ssize_t
475 show_spi_transport_min_period(struct class_device *cdev, char *buf)
476 {
477         struct scsi_target *starget = transport_class_to_starget(cdev);
478         struct spi_transport_attrs *tp =
479                 (struct spi_transport_attrs *)&starget->starget_data;
480
481         return show_spi_transport_period_helper(cdev, buf, tp->min_period);
482 }
483
484 static ssize_t
485 store_spi_transport_min_period(struct class_device *cdev, const char *buf,
486                             size_t count)
487 {
488         struct scsi_target *starget = transport_class_to_starget(cdev);
489         struct spi_transport_attrs *tp =
490                 (struct spi_transport_attrs *)&starget->starget_data;
491
492         return store_spi_transport_period_helper(cdev, buf, count,
493                                                  &tp->min_period);
494 }
495
496
497 static CLASS_DEVICE_ATTR(min_period, S_IRUGO | S_IWUSR, 
498                          show_spi_transport_min_period,
499                          store_spi_transport_min_period);
500
501
502 static ssize_t show_spi_host_signalling(struct class_device *cdev, char *buf)
503 {
504         struct Scsi_Host *shost = transport_class_to_shost(cdev);
505         struct spi_internal *i = to_spi_internal(shost->transportt);
506
507         if (i->f->get_signalling)
508                 i->f->get_signalling(shost);
509
510         return sprintf(buf, "%s\n", spi_signal_to_string(spi_signalling(shost)));
511 }
512 static ssize_t store_spi_host_signalling(struct class_device *cdev,
513                                          const char *buf, size_t count)
514 {
515         struct Scsi_Host *shost = transport_class_to_shost(cdev);
516         struct spi_internal *i = to_spi_internal(shost->transportt);
517         enum spi_signal_type type = spi_signal_to_value(buf);
518
519         if (type != SPI_SIGNAL_UNKNOWN)
520                 i->f->set_signalling(shost, type);
521
522         return count;
523 }
524 static CLASS_DEVICE_ATTR(signalling, S_IRUGO | S_IWUSR,
525                          show_spi_host_signalling,
526                          store_spi_host_signalling);
527
528 #define DV_SET(x, y)                    \
529         if(i->f->set_##x)               \
530                 i->f->set_##x(sdev->sdev_target, y)
531
532 enum spi_compare_returns {
533         SPI_COMPARE_SUCCESS,
534         SPI_COMPARE_FAILURE,
535         SPI_COMPARE_SKIP_TEST,
536 };
537
538
539 /* This is for read/write Domain Validation:  If the device supports
540  * an echo buffer, we do read/write tests to it */
541 static enum spi_compare_returns
542 spi_dv_device_echo_buffer(struct scsi_request *sreq, u8 *buffer,
543                           u8 *ptr, const int retries)
544 {
545         struct scsi_device *sdev = sreq->sr_device;
546         int len = ptr - buffer;
547         int j, k, r;
548         unsigned int pattern = 0x0000ffff;
549
550         const char spi_write_buffer[] = {
551                 WRITE_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
552         };
553         const char spi_read_buffer[] = {
554                 READ_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
555         };
556
557         /* set up the pattern buffer.  Doesn't matter if we spill
558          * slightly beyond since that's where the read buffer is */
559         for (j = 0; j < len; ) {
560
561                 /* fill the buffer with counting (test a) */
562                 for ( ; j < min(len, 32); j++)
563                         buffer[j] = j;
564                 k = j;
565                 /* fill the buffer with alternating words of 0x0 and
566                  * 0xffff (test b) */
567                 for ( ; j < min(len, k + 32); j += 2) {
568                         u16 *word = (u16 *)&buffer[j];
569                         
570                         *word = (j & 0x02) ? 0x0000 : 0xffff;
571                 }
572                 k = j;
573                 /* fill with crosstalk (alternating 0x5555 0xaaa)
574                  * (test c) */
575                 for ( ; j < min(len, k + 32); j += 2) {
576                         u16 *word = (u16 *)&buffer[j];
577
578                         *word = (j & 0x02) ? 0x5555 : 0xaaaa;
579                 }
580                 k = j;
581                 /* fill with shifting bits (test d) */
582                 for ( ; j < min(len, k + 32); j += 4) {
583                         u32 *word = (unsigned int *)&buffer[j];
584                         u32 roll = (pattern & 0x80000000) ? 1 : 0;
585                         
586                         *word = pattern;
587                         pattern = (pattern << 1) | roll;
588                 }
589                 /* don't bother with random data (test e) */
590         }
591
592         for (r = 0; r < retries; r++) {
593                 sreq->sr_cmd_len = 0;   /* wait_req to fill in */
594                 sreq->sr_data_direction = DMA_TO_DEVICE;
595                 spi_wait_req(sreq, spi_write_buffer, buffer, len);
596                 if(sreq->sr_result || !scsi_device_online(sdev)) {
597                         struct scsi_sense_hdr sshdr;
598
599                         scsi_device_set_state(sdev, SDEV_QUIESCE);
600                         if (scsi_request_normalize_sense(sreq, &sshdr)
601                             && sshdr.sense_key == ILLEGAL_REQUEST
602                             /* INVALID FIELD IN CDB */
603                             && sshdr.asc == 0x24 && sshdr.ascq == 0x00)
604                                 /* This would mean that the drive lied
605                                  * to us about supporting an echo
606                                  * buffer (unfortunately some Western
607                                  * Digital drives do precisely this)
608                                  */
609                                 return SPI_COMPARE_SKIP_TEST;
610
611
612                         SPI_PRINTK(sdev->sdev_target, KERN_ERR, "Write Buffer failure %x\n", sreq->sr_result);
613                         return SPI_COMPARE_FAILURE;
614                 }
615
616                 memset(ptr, 0, len);
617                 sreq->sr_cmd_len = 0;   /* wait_req to fill in */
618                 sreq->sr_data_direction = DMA_FROM_DEVICE;
619                 spi_wait_req(sreq, spi_read_buffer, ptr, len);
620                 scsi_device_set_state(sdev, SDEV_QUIESCE);
621
622                 if (memcmp(buffer, ptr, len) != 0)
623                         return SPI_COMPARE_FAILURE;
624         }
625         return SPI_COMPARE_SUCCESS;
626 }
627
628 /* This is for the simplest form of Domain Validation: a read test
629  * on the inquiry data from the device */
630 static enum spi_compare_returns
631 spi_dv_device_compare_inquiry(struct scsi_request *sreq, u8 *buffer,
632                               u8 *ptr, const int retries)
633 {
634         int r;
635         const int len = sreq->sr_device->inquiry_len;
636         struct scsi_device *sdev = sreq->sr_device;
637         const char spi_inquiry[] = {
638                 INQUIRY, 0, 0, 0, len, 0
639         };
640
641         for (r = 0; r < retries; r++) {
642                 sreq->sr_cmd_len = 0;   /* wait_req to fill in */
643                 sreq->sr_data_direction = DMA_FROM_DEVICE;
644
645                 memset(ptr, 0, len);
646
647                 spi_wait_req(sreq, spi_inquiry, ptr, len);
648                 
649                 if(sreq->sr_result || !scsi_device_online(sdev)) {
650                         scsi_device_set_state(sdev, SDEV_QUIESCE);
651                         return SPI_COMPARE_FAILURE;
652                 }
653
654                 /* If we don't have the inquiry data already, the
655                  * first read gets it */
656                 if (ptr == buffer) {
657                         ptr += len;
658                         --r;
659                         continue;
660                 }
661
662                 if (memcmp(buffer, ptr, len) != 0)
663                         /* failure */
664                         return SPI_COMPARE_FAILURE;
665         }
666         return SPI_COMPARE_SUCCESS;
667 }
668
669 static enum spi_compare_returns
670 spi_dv_retrain(struct scsi_request *sreq, u8 *buffer, u8 *ptr,
671                enum spi_compare_returns 
672                (*compare_fn)(struct scsi_request *, u8 *, u8 *, int))
673 {
674         struct spi_internal *i = to_spi_internal(sreq->sr_host->transportt);
675         struct scsi_device *sdev = sreq->sr_device;
676         struct scsi_target *starget = sdev->sdev_target;
677         int period = 0, prevperiod = 0; 
678         enum spi_compare_returns retval;
679
680
681         for (;;) {
682                 int newperiod;
683                 retval = compare_fn(sreq, buffer, ptr, DV_LOOPS);
684
685                 if (retval == SPI_COMPARE_SUCCESS
686                     || retval == SPI_COMPARE_SKIP_TEST)
687                         break;
688
689                 /* OK, retrain, fallback */
690                 if (i->f->get_iu)
691                         i->f->get_iu(starget);
692                 if (i->f->get_qas)
693                         i->f->get_qas(starget);
694                 if (i->f->get_period)
695                         i->f->get_period(sdev->sdev_target);
696
697                 /* Here's the fallback sequence; first try turning off
698                  * IU, then QAS (if we can control them), then finally
699                  * fall down the periods */
700                 if (i->f->set_iu && spi_iu(starget)) {
701                         SPI_PRINTK(starget, KERN_ERR, "Domain Validation Disabing Information Units\n");
702                         DV_SET(iu, 0);
703                 } else if (i->f->set_qas && spi_qas(starget)) {
704                         SPI_PRINTK(starget, KERN_ERR, "Domain Validation Disabing Quick Arbitration and Selection\n");
705                         DV_SET(qas, 0);
706                 } else {
707                         newperiod = spi_period(starget);
708                         period = newperiod > period ? newperiod : period;
709                         if (period < 0x0d)
710                                 period++;
711                         else
712                                 period += period >> 1;
713
714                         if (unlikely(period > 0xff || period == prevperiod)) {
715                                 /* Total failure; set to async and return */
716                                 SPI_PRINTK(starget, KERN_ERR, "Domain Validation Failure, dropping back to Asynchronous\n");
717                                 DV_SET(offset, 0);
718                                 return SPI_COMPARE_FAILURE;
719                         }
720                         SPI_PRINTK(starget, KERN_ERR, "Domain Validation detected failure, dropping back\n");
721                         DV_SET(period, period);
722                         prevperiod = period;
723                 }
724         }
725         return retval;
726 }
727
728 static int
729 spi_dv_device_get_echo_buffer(struct scsi_request *sreq, u8 *buffer)
730 {
731         int l;
732
733         /* first off do a test unit ready.  This can error out 
734          * because of reservations or some other reason.  If it
735          * fails, the device won't let us write to the echo buffer
736          * so just return failure */
737         
738         const char spi_test_unit_ready[] = {
739                 TEST_UNIT_READY, 0, 0, 0, 0, 0
740         };
741
742         const char spi_read_buffer_descriptor[] = {
743                 READ_BUFFER, 0x0b, 0, 0, 0, 0, 0, 0, 4, 0
744         };
745
746         
747         sreq->sr_cmd_len = 0;
748         sreq->sr_data_direction = DMA_NONE;
749
750         /* We send a set of three TURs to clear any outstanding 
751          * unit attention conditions if they exist (Otherwise the
752          * buffer tests won't be happy).  If the TUR still fails
753          * (reservation conflict, device not ready, etc) just
754          * skip the write tests */
755         for (l = 0; ; l++) {
756                 spi_wait_req(sreq, spi_test_unit_ready, NULL, 0);
757
758                 if(sreq->sr_result) {
759                         if(l >= 3)
760                                 return 0;
761                 } else {
762                         /* TUR succeeded */
763                         break;
764                 }
765         }
766
767         sreq->sr_cmd_len = 0;
768         sreq->sr_data_direction = DMA_FROM_DEVICE;
769
770         spi_wait_req(sreq, spi_read_buffer_descriptor, buffer, 4);
771
772         if (sreq->sr_result)
773                 /* Device has no echo buffer */
774                 return 0;
775
776         return buffer[3] + ((buffer[2] & 0x1f) << 8);
777 }
778
779 static void
780 spi_dv_device_internal(struct scsi_request *sreq, u8 *buffer)
781 {
782         struct spi_internal *i = to_spi_internal(sreq->sr_host->transportt);
783         struct scsi_device *sdev = sreq->sr_device;
784         struct scsi_target *starget = sdev->sdev_target;
785         int len = sdev->inquiry_len;
786         /* first set us up for narrow async */
787         DV_SET(offset, 0);
788         DV_SET(width, 0);
789         
790         if (spi_dv_device_compare_inquiry(sreq, buffer, buffer, DV_LOOPS)
791             != SPI_COMPARE_SUCCESS) {
792                 SPI_PRINTK(starget, KERN_ERR, "Domain Validation Initial Inquiry Failed\n");
793                 /* FIXME: should probably offline the device here? */
794                 return;
795         }
796
797         /* test width */
798         if (i->f->set_width && spi_max_width(starget) && sdev->wdtr) {
799                 i->f->set_width(starget, 1);
800
801                 if (spi_dv_device_compare_inquiry(sreq, buffer,
802                                                    buffer + len,
803                                                    DV_LOOPS)
804                     != SPI_COMPARE_SUCCESS) {
805                         SPI_PRINTK(starget, KERN_ERR, "Wide Transfers Fail\n");
806                         i->f->set_width(starget, 0);
807                 }
808         }
809
810         if (!i->f->set_period)
811                 return;
812
813         /* device can't handle synchronous */
814         if (!sdev->ppr && !sdev->sdtr)
815                 return;
816
817         /* see if the device has an echo buffer.  If it does we can
818          * do the SPI pattern write tests */
819
820         len = 0;
821         if (sdev->ppr)
822                 len = spi_dv_device_get_echo_buffer(sreq, buffer);
823
824  retry:
825
826         /* now set up to the maximum */
827         DV_SET(offset, spi_max_offset(starget));
828         DV_SET(period, spi_min_period(starget));
829         /* try QAS requests; this should be harmless to set if the
830          * target supports it */
831         DV_SET(qas, 1);
832         /* Also try IU transfers */
833         DV_SET(iu, 1);
834         if (spi_min_period(starget) < 9) {
835                 /* This u320 (or u640). Ignore the coupled parameters
836                  * like DT and IU, but set the optional ones */
837                 DV_SET(rd_strm, 1);
838                 DV_SET(wr_flow, 1);
839                 DV_SET(rti, 1);
840                 if (spi_min_period(starget) == 8)
841                         DV_SET(pcomp_en, 1);
842         }
843
844         if (len == 0) {
845                 SPI_PRINTK(starget, KERN_INFO, "Domain Validation skipping write tests\n");
846                 spi_dv_retrain(sreq, buffer, buffer + len,
847                                spi_dv_device_compare_inquiry);
848                 return;
849         }
850
851         if (len > SPI_MAX_ECHO_BUFFER_SIZE) {
852                 SPI_PRINTK(starget, KERN_WARNING, "Echo buffer size %d is too big, trimming to %d\n", len, SPI_MAX_ECHO_BUFFER_SIZE);
853                 len = SPI_MAX_ECHO_BUFFER_SIZE;
854         }
855
856         if (spi_dv_retrain(sreq, buffer, buffer + len,
857                            spi_dv_device_echo_buffer)
858             == SPI_COMPARE_SKIP_TEST) {
859                 /* OK, the stupid drive can't do a write echo buffer
860                  * test after all, fall back to the read tests */
861                 len = 0;
862                 goto retry;
863         }
864 }
865
866
867 /**     spi_dv_device - Do Domain Validation on the device
868  *      @sdev:          scsi device to validate
869  *
870  *      Performs the domain validation on the given device in the
871  *      current execution thread.  Since DV operations may sleep,
872  *      the current thread must have user context.  Also no SCSI
873  *      related locks that would deadlock I/O issued by the DV may
874  *      be held.
875  */
876 void
877 spi_dv_device(struct scsi_device *sdev)
878 {
879         struct scsi_request *sreq = scsi_allocate_request(sdev, GFP_KERNEL);
880         struct scsi_target *starget = sdev->sdev_target;
881         u8 *buffer;
882         const int len = SPI_MAX_ECHO_BUFFER_SIZE*2;
883
884         if (unlikely(!sreq))
885                 return;
886
887         if (unlikely(scsi_device_get(sdev)))
888                 goto out_free_req;
889
890         buffer = kmalloc(len, GFP_KERNEL);
891
892         if (unlikely(!buffer))
893                 goto out_put;
894
895         memset(buffer, 0, len);
896
897         /* We need to verify that the actual device will quiesce; the
898          * later target quiesce is just a nice to have */
899         if (unlikely(scsi_device_quiesce(sdev)))
900                 goto out_free;
901
902         scsi_target_quiesce(starget);
903
904         spi_dv_pending(starget) = 1;
905         down(&spi_dv_sem(starget));
906
907         SPI_PRINTK(starget, KERN_INFO, "Beginning Domain Validation\n");
908
909         spi_dv_device_internal(sreq, buffer);
910
911         SPI_PRINTK(starget, KERN_INFO, "Ending Domain Validation\n");
912
913         up(&spi_dv_sem(starget));
914         spi_dv_pending(starget) = 0;
915
916         scsi_target_resume(starget);
917
918         spi_initial_dv(starget) = 1;
919
920  out_free:
921         kfree(buffer);
922  out_put:
923         scsi_device_put(sdev);
924  out_free_req:
925         scsi_release_request(sreq);
926 }
927 EXPORT_SYMBOL(spi_dv_device);
928
929 struct work_queue_wrapper {
930         struct work_struct      work;
931         struct scsi_device      *sdev;
932 };
933
934 static void
935 spi_dv_device_work_wrapper(void *data)
936 {
937         struct work_queue_wrapper *wqw = (struct work_queue_wrapper *)data;
938         struct scsi_device *sdev = wqw->sdev;
939
940         kfree(wqw);
941         spi_dv_device(sdev);
942         spi_dv_pending(sdev->sdev_target) = 0;
943         scsi_device_put(sdev);
944 }
945
946
947 /**
948  *      spi_schedule_dv_device - schedule domain validation to occur on the device
949  *      @sdev:  The device to validate
950  *
951  *      Identical to spi_dv_device() above, except that the DV will be
952  *      scheduled to occur in a workqueue later.  All memory allocations
953  *      are atomic, so may be called from any context including those holding
954  *      SCSI locks.
955  */
956 void
957 spi_schedule_dv_device(struct scsi_device *sdev)
958 {
959         struct work_queue_wrapper *wqw =
960                 kmalloc(sizeof(struct work_queue_wrapper), GFP_ATOMIC);
961
962         if (unlikely(!wqw))
963                 return;
964
965         if (unlikely(spi_dv_pending(sdev->sdev_target))) {
966                 kfree(wqw);
967                 return;
968         }
969         /* Set pending early (dv_device doesn't check it, only sets it) */
970         spi_dv_pending(sdev->sdev_target) = 1;
971         if (unlikely(scsi_device_get(sdev))) {
972                 kfree(wqw);
973                 spi_dv_pending(sdev->sdev_target) = 0;
974                 return;
975         }
976
977         INIT_WORK(&wqw->work, spi_dv_device_work_wrapper, wqw);
978         wqw->sdev = sdev;
979
980         schedule_work(&wqw->work);
981 }
982 EXPORT_SYMBOL(spi_schedule_dv_device);
983
984 /**
985  * spi_display_xfer_agreement - Print the current target transfer agreement
986  * @starget: The target for which to display the agreement
987  *
988  * Each SPI port is required to maintain a transfer agreement for each
989  * other port on the bus.  This function prints a one-line summary of
990  * the current agreement; more detailed information is available in sysfs.
991  */
992 void spi_display_xfer_agreement(struct scsi_target *starget)
993 {
994         struct spi_transport_attrs *tp;
995         tp = (struct spi_transport_attrs *)&starget->starget_data;
996
997         if (tp->offset > 0 && tp->period > 0) {
998                 unsigned int picosec, kb100;
999                 char *scsi = "FAST-?";
1000                 char tmp[8];
1001
1002                 if (tp->period <= SPI_STATIC_PPR) {
1003                         picosec = ppr_to_ps[tp->period];
1004                         switch (tp->period) {
1005                                 case  7: scsi = "FAST-320"; break;
1006                                 case  8: scsi = "FAST-160"; break;
1007                                 case  9: scsi = "FAST-80"; break;
1008                                 case 10:
1009                                 case 11: scsi = "FAST-40"; break;
1010                                 case 12: scsi = "FAST-20"; break;
1011                         }
1012                 } else {
1013                         picosec = tp->period * 4000;
1014                         if (tp->period < 25)
1015                                 scsi = "FAST-20";
1016                         else if (tp->period < 50)
1017                                 scsi = "FAST-10";
1018                         else
1019                                 scsi = "FAST-5";
1020                 }
1021
1022                 kb100 = (10000000 + picosec / 2) / picosec;
1023                 if (tp->width)
1024                         kb100 *= 2;
1025                 sprint_frac(tmp, picosec, 1000);
1026
1027                 dev_info(&starget->dev,
1028                         "%s %sSCSI %d.%d MB/s %s%s%s (%s ns, offset %d)\n",
1029                         scsi, tp->width ? "WIDE " : "", kb100/10, kb100 % 10,
1030                         tp->dt ? "DT" : "ST", tp->iu ? " IU" : "",
1031                         tp->qas  ? " QAS" : "", tmp, tp->offset);
1032         } else {
1033                 dev_info(&starget->dev, "%sasynchronous.\n",
1034                                 tp->width ? "wide " : "");
1035         }
1036 }
1037 EXPORT_SYMBOL(spi_display_xfer_agreement);
1038
1039 #define SETUP_ATTRIBUTE(field)                                          \
1040         i->private_attrs[count] = class_device_attr_##field;            \
1041         if (!i->f->set_##field) {                                       \
1042                 i->private_attrs[count].attr.mode = S_IRUGO;            \
1043                 i->private_attrs[count].store = NULL;                   \
1044         }                                                               \
1045         i->attrs[count] = &i->private_attrs[count];                     \
1046         if (i->f->show_##field)                                         \
1047                 count++
1048
1049 #define SETUP_RELATED_ATTRIBUTE(field, rel_field)                       \
1050         i->private_attrs[count] = class_device_attr_##field;            \
1051         if (!i->f->set_##rel_field) {                                   \
1052                 i->private_attrs[count].attr.mode = S_IRUGO;            \
1053                 i->private_attrs[count].store = NULL;                   \
1054         }                                                               \
1055         i->attrs[count] = &i->private_attrs[count];                     \
1056         if (i->f->show_##rel_field)                                     \
1057                 count++
1058
1059 #define SETUP_HOST_ATTRIBUTE(field)                                     \
1060         i->private_host_attrs[count] = class_device_attr_##field;       \
1061         if (!i->f->set_##field) {                                       \
1062                 i->private_host_attrs[count].attr.mode = S_IRUGO;       \
1063                 i->private_host_attrs[count].store = NULL;              \
1064         }                                                               \
1065         i->host_attrs[count] = &i->private_host_attrs[count];           \
1066         count++
1067
1068 static int spi_device_match(struct attribute_container *cont,
1069                             struct device *dev)
1070 {
1071         struct scsi_device *sdev;
1072         struct Scsi_Host *shost;
1073
1074         if (!scsi_is_sdev_device(dev))
1075                 return 0;
1076
1077         sdev = to_scsi_device(dev);
1078         shost = sdev->host;
1079         if (!shost->transportt  || shost->transportt->host_attrs.ac.class
1080             != &spi_host_class.class)
1081                 return 0;
1082         /* Note: this class has no device attributes, so it has
1083          * no per-HBA allocation and thus we don't need to distinguish
1084          * the attribute containers for the device */
1085         return 1;
1086 }
1087
1088 static int spi_target_match(struct attribute_container *cont,
1089                             struct device *dev)
1090 {
1091         struct Scsi_Host *shost;
1092         struct spi_internal *i;
1093
1094         if (!scsi_is_target_device(dev))
1095                 return 0;
1096
1097         shost = dev_to_shost(dev->parent);
1098         if (!shost->transportt  || shost->transportt->host_attrs.ac.class
1099             != &spi_host_class.class)
1100                 return 0;
1101
1102         i = to_spi_internal(shost->transportt);
1103         
1104         return &i->t.target_attrs.ac == cont;
1105 }
1106
1107 static DECLARE_TRANSPORT_CLASS(spi_transport_class,
1108                                "spi_transport",
1109                                spi_setup_transport_attrs,
1110                                NULL,
1111                                NULL);
1112
1113 static DECLARE_ANON_TRANSPORT_CLASS(spi_device_class,
1114                                     spi_device_match,
1115                                     spi_device_configure);
1116
1117 struct scsi_transport_template *
1118 spi_attach_transport(struct spi_function_template *ft)
1119 {
1120         struct spi_internal *i = kmalloc(sizeof(struct spi_internal),
1121                                          GFP_KERNEL);
1122         int count = 0;
1123         if (unlikely(!i))
1124                 return NULL;
1125
1126         memset(i, 0, sizeof(struct spi_internal));
1127
1128
1129         i->t.target_attrs.ac.class = &spi_transport_class.class;
1130         i->t.target_attrs.ac.attrs = &i->attrs[0];
1131         i->t.target_attrs.ac.match = spi_target_match;
1132         transport_container_register(&i->t.target_attrs);
1133         i->t.target_size = sizeof(struct spi_transport_attrs);
1134         i->t.host_attrs.ac.class = &spi_host_class.class;
1135         i->t.host_attrs.ac.attrs = &i->host_attrs[0];
1136         i->t.host_attrs.ac.match = spi_host_match;
1137         transport_container_register(&i->t.host_attrs);
1138         i->t.host_size = sizeof(struct spi_host_attrs);
1139         i->f = ft;
1140
1141         SETUP_ATTRIBUTE(period);
1142         SETUP_RELATED_ATTRIBUTE(min_period, period);
1143         SETUP_ATTRIBUTE(offset);
1144         SETUP_RELATED_ATTRIBUTE(max_offset, offset);
1145         SETUP_ATTRIBUTE(width);
1146         SETUP_RELATED_ATTRIBUTE(max_width, width);
1147         SETUP_ATTRIBUTE(iu);
1148         SETUP_ATTRIBUTE(dt);
1149         SETUP_ATTRIBUTE(qas);
1150         SETUP_ATTRIBUTE(wr_flow);
1151         SETUP_ATTRIBUTE(rd_strm);
1152         SETUP_ATTRIBUTE(rti);
1153         SETUP_ATTRIBUTE(pcomp_en);
1154
1155         /* if you add an attribute but forget to increase SPI_NUM_ATTRS
1156          * this bug will trigger */
1157         BUG_ON(count > SPI_NUM_ATTRS);
1158
1159         i->attrs[count++] = &class_device_attr_revalidate;
1160
1161         i->attrs[count] = NULL;
1162
1163         count = 0;
1164         SETUP_HOST_ATTRIBUTE(signalling);
1165
1166         BUG_ON(count > SPI_HOST_ATTRS);
1167
1168         i->host_attrs[count] = NULL;
1169
1170         return &i->t;
1171 }
1172 EXPORT_SYMBOL(spi_attach_transport);
1173
1174 void spi_release_transport(struct scsi_transport_template *t)
1175 {
1176         struct spi_internal *i = to_spi_internal(t);
1177
1178         transport_container_unregister(&i->t.target_attrs);
1179         transport_container_unregister(&i->t.host_attrs);
1180
1181         kfree(i);
1182 }
1183 EXPORT_SYMBOL(spi_release_transport);
1184
1185 static __init int spi_transport_init(void)
1186 {
1187         int error = transport_class_register(&spi_transport_class);
1188         if (error)
1189                 return error;
1190         error = anon_transport_class_register(&spi_device_class);
1191         return transport_class_register(&spi_host_class);
1192 }
1193
1194 static void __exit spi_transport_exit(void)
1195 {
1196         transport_class_unregister(&spi_transport_class);
1197         anon_transport_class_unregister(&spi_device_class);
1198         transport_class_unregister(&spi_host_class);
1199 }
1200
1201 MODULE_AUTHOR("Martin Hicks");
1202 MODULE_DESCRIPTION("SPI Transport Attributes");
1203 MODULE_LICENSE("GPL");
1204
1205 module_init(spi_transport_init);
1206 module_exit(spi_transport_exit);