This commit was manufactured by cvs2svn to create tag
[linux-2.6.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  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/smp_lock.h>
23 #include <linux/list.h>
24 #include <linux/spinlock.h>
25 #include <linux/mm.h>
26 #include <linux/workqueue.h>
27 #include <asm/scatterlist.h>
28 #include <asm/io.h>
29 #include <scsi/scsi.h>
30 #include "scsi_priv.h"
31 #include <scsi/scsi_device.h>
32 #include <scsi/scsi_host.h>
33 #include <scsi/scsi_request.h>
34 #include <scsi/scsi_transport.h>
35 #include <scsi/scsi_transport_spi.h>
36
37 #define SPI_PRINTK(x, l, f, a...)       dev_printk(l, &(x)->dev, f , ##a)
38
39 static void transport_class_release(struct class_device *class_dev);
40 static void host_class_release(struct class_device *class_dev);
41
42 #define SPI_NUM_ATTRS 10        /* increase this if you add attributes */
43 #define SPI_OTHER_ATTRS 1       /* Increase this if you add "always
44                                  * on" attributes */
45 #define SPI_HOST_ATTRS  1
46
47 #define SPI_MAX_ECHO_BUFFER_SIZE        4096
48
49 /* Private data accessors (keep these out of the header file) */
50 #define spi_dv_pending(x) (((struct spi_transport_attrs *)&(x)->starget_data)->dv_pending)
51 #define spi_dv_sem(x) (((struct spi_transport_attrs *)&(x)->starget_data)->dv_sem)
52
53 struct spi_internal {
54         struct scsi_transport_template t;
55         struct spi_function_template *f;
56         /* The actual attributes */
57         struct class_device_attribute private_attrs[SPI_NUM_ATTRS];
58         /* The array of null terminated pointers to attributes 
59          * needed by scsi_sysfs.c */
60         struct class_device_attribute *attrs[SPI_NUM_ATTRS + SPI_OTHER_ATTRS + 1];
61         struct class_device_attribute private_host_attrs[SPI_HOST_ATTRS];
62         struct class_device_attribute *host_attrs[SPI_HOST_ATTRS + 1];
63 };
64
65 #define to_spi_internal(tmpl)   container_of(tmpl, struct spi_internal, t)
66
67 static const char *const ppr_to_ns[] = {
68         /* The PPR values 0-6 are reserved, fill them in when
69          * the committee defines them */
70         NULL,                   /* 0x00 */
71         NULL,                   /* 0x01 */
72         NULL,                   /* 0x02 */
73         NULL,                   /* 0x03 */
74         NULL,                   /* 0x04 */
75         NULL,                   /* 0x05 */
76         NULL,                   /* 0x06 */
77         "3.125",                /* 0x07 */
78         "6.25",                 /* 0x08 */
79         "12.5",                 /* 0x09 */
80         "25",                   /* 0x0a */
81         "30.3",                 /* 0x0b */
82         "50",                   /* 0x0c */
83 };
84 /* The PPR values at which you calculate the period in ns by multiplying
85  * by 4 */
86 #define SPI_STATIC_PPR  0x0c
87
88 static struct {
89         enum spi_signal_type    value;
90         char                    *name;
91 } signal_types[] = {
92         { SPI_SIGNAL_UNKNOWN, "unknown" },
93         { SPI_SIGNAL_SE, "SE" },
94         { SPI_SIGNAL_LVD, "LVD" },
95         { SPI_SIGNAL_HVD, "HVD" },
96 };
97
98 static inline const char *spi_signal_to_string(enum spi_signal_type type)
99 {
100         int i;
101
102         for (i = 0; i < sizeof(signal_types)/sizeof(signal_types[0]); i++) {
103                 if (type == signal_types[i].value)
104                         return signal_types[i].name;
105         }
106         return NULL;
107 }
108 static inline enum spi_signal_type spi_signal_to_value(const char *name)
109 {
110         int i, len;
111
112         for (i = 0; i < sizeof(signal_types)/sizeof(signal_types[0]); i++) {
113                 len =  strlen(signal_types[i].name);
114                 if (strncmp(name, signal_types[i].name, len) == 0 &&
115                     (name[len] == '\n' || name[len] == '\0'))
116                         return signal_types[i].value;
117         }
118         return SPI_SIGNAL_UNKNOWN;
119 }
120
121
122 struct class spi_transport_class = {
123         .name = "spi_transport",
124         .release = transport_class_release,
125 };
126
127 struct class spi_host_class = {
128         .name = "spi_host",
129         .release = host_class_release,
130 };
131
132 static __init int spi_transport_init(void)
133 {
134         int error = class_register(&spi_host_class);
135         if (error)
136                 return error;
137         return class_register(&spi_transport_class);
138 }
139
140 static void __exit spi_transport_exit(void)
141 {
142         class_unregister(&spi_transport_class);
143         class_unregister(&spi_host_class);
144 }
145
146 static int spi_setup_host_attrs(struct Scsi_Host *shost)
147 {
148         spi_signalling(shost) = SPI_SIGNAL_UNKNOWN;
149
150         return 0;
151 }
152
153 static int spi_configure_device(struct scsi_device *sdev)
154 {
155         struct scsi_target *starget = sdev->sdev_target;
156
157         /* Populate the target capability fields with the values
158          * gleaned from the device inquiry */
159
160         spi_support_sync(starget) = scsi_device_sync(sdev);
161         spi_support_wide(starget) = scsi_device_wide(sdev);
162         spi_support_dt(starget) = scsi_device_dt(sdev);
163         spi_support_dt_only(starget) = scsi_device_dt_only(sdev);
164         spi_support_ius(starget) = scsi_device_ius(sdev);
165         spi_support_qas(starget) = scsi_device_qas(sdev);
166
167         return 0;
168 }
169
170 static int spi_setup_transport_attrs(struct scsi_target *starget)
171 {
172         spi_period(starget) = -1;       /* illegal value */
173         spi_offset(starget) = 0;        /* async */
174         spi_width(starget) = 0; /* narrow */
175         spi_iu(starget) = 0;    /* no IU */
176         spi_dt(starget) = 0;    /* ST */
177         spi_qas(starget) = 0;
178         spi_wr_flow(starget) = 0;
179         spi_rd_strm(starget) = 0;
180         spi_rti(starget) = 0;
181         spi_pcomp_en(starget) = 0;
182         spi_dv_pending(starget) = 0;
183         spi_initial_dv(starget) = 0;
184         init_MUTEX(&spi_dv_sem(starget));
185
186         return 0;
187 }
188
189 static void transport_class_release(struct class_device *class_dev)
190 {
191         struct scsi_target *starget = transport_class_to_starget(class_dev);
192         put_device(&starget->dev);
193 }
194
195 static void host_class_release(struct class_device *class_dev)
196 {
197         struct Scsi_Host *shost = transport_class_to_shost(class_dev);
198         put_device(&shost->shost_gendev);
199 }
200
201 #define spi_transport_show_function(field, format_string)               \
202                                                                         \
203 static ssize_t                                                          \
204 show_spi_transport_##field(struct class_device *cdev, char *buf)        \
205 {                                                                       \
206         struct scsi_target *starget = transport_class_to_starget(cdev); \
207         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);    \
208         struct spi_transport_attrs *tp;                                 \
209         struct spi_internal *i = to_spi_internal(shost->transportt);    \
210         tp = (struct spi_transport_attrs *)&starget->starget_data;      \
211         if (i->f->get_##field)                                          \
212                 i->f->get_##field(starget);                             \
213         return snprintf(buf, 20, format_string, tp->field);             \
214 }
215
216 #define spi_transport_store_function(field, format_string)              \
217 static ssize_t                                                          \
218 store_spi_transport_##field(struct class_device *cdev, const char *buf, \
219                             size_t count)                               \
220 {                                                                       \
221         int val;                                                        \
222         struct scsi_target *starget = transport_class_to_starget(cdev); \
223         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);    \
224         struct spi_internal *i = to_spi_internal(shost->transportt);    \
225                                                                         \
226         val = simple_strtoul(buf, NULL, 0);                             \
227         i->f->set_##field(starget, val);                                \
228         return count;                                                   \
229 }
230
231 #define spi_transport_rd_attr(field, format_string)                     \
232         spi_transport_show_function(field, format_string)               \
233         spi_transport_store_function(field, format_string)              \
234 static CLASS_DEVICE_ATTR(field, S_IRUGO | S_IWUSR,                      \
235                          show_spi_transport_##field,                    \
236                          store_spi_transport_##field);
237
238 /* The Parallel SCSI Tranport Attributes: */
239 spi_transport_rd_attr(offset, "%d\n");
240 spi_transport_rd_attr(width, "%d\n");
241 spi_transport_rd_attr(iu, "%d\n");
242 spi_transport_rd_attr(dt, "%d\n");
243 spi_transport_rd_attr(qas, "%d\n");
244 spi_transport_rd_attr(wr_flow, "%d\n");
245 spi_transport_rd_attr(rd_strm, "%d\n");
246 spi_transport_rd_attr(rti, "%d\n");
247 spi_transport_rd_attr(pcomp_en, "%d\n");
248
249 static ssize_t
250 store_spi_revalidate(struct class_device *cdev, const char *buf, size_t count)
251 {
252         struct scsi_target *starget = transport_class_to_starget(cdev);
253
254         /* FIXME: we're relying on an awful lot of device internals
255          * here.  We really need a function to get the first available
256          * child */
257         struct device *dev = container_of(starget->dev.children.next, struct device, node);
258         struct scsi_device *sdev = to_scsi_device(dev);
259         spi_dv_device(sdev);
260         return count;
261 }
262 static CLASS_DEVICE_ATTR(revalidate, S_IWUSR, NULL, store_spi_revalidate);
263
264 /* Translate the period into ns according to the current spec
265  * for SDTR/PPR messages */
266 static ssize_t show_spi_transport_period(struct class_device *cdev, char *buf)
267
268 {
269         struct scsi_target *starget = transport_class_to_starget(cdev);
270         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
271         struct spi_transport_attrs *tp;
272         const char *str;
273         struct spi_internal *i = to_spi_internal(shost->transportt);
274
275         tp = (struct spi_transport_attrs *)&starget->starget_data;
276
277         if (i->f->get_period)
278                 i->f->get_period(starget);
279
280         switch(tp->period) {
281
282         case 0x07 ... SPI_STATIC_PPR:
283                 str = ppr_to_ns[tp->period];
284                 if(!str)
285                         str = "reserved";
286                 break;
287
288
289         case (SPI_STATIC_PPR+1) ... 0xff:
290                 return sprintf(buf, "%d\n", tp->period * 4);
291
292         default:
293                 str = "unknown";
294         }
295         return sprintf(buf, "%s\n", str);
296 }
297
298 static ssize_t
299 store_spi_transport_period(struct class_device *cdev, const char *buf,
300                             size_t count)
301 {
302         struct scsi_target *starget = transport_class_to_starget(cdev);
303         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
304         struct spi_internal *i = to_spi_internal(shost->transportt);
305         int j, period = -1;
306
307         for (j = 0; j < SPI_STATIC_PPR; j++) {
308                 int len;
309
310                 if(ppr_to_ns[j] == NULL)
311                         continue;
312
313                 len = strlen(ppr_to_ns[j]);
314
315                 if(strncmp(ppr_to_ns[j], buf, len) != 0)
316                         continue;
317
318                 if(buf[len] != '\n')
319                         continue;
320                 
321                 period = j;
322                 break;
323         }
324
325         if (period == -1) {
326                 int val = simple_strtoul(buf, NULL, 0);
327
328
329                 /* Should probably check limits here, but this
330                  * gets reasonably close to OK for most things */
331                 period = val/4;
332         }
333
334         if (period > 0xff)
335                 period = 0xff;
336
337         i->f->set_period(starget, period);
338
339         return count;
340 }
341         
342 static CLASS_DEVICE_ATTR(period, S_IRUGO | S_IWUSR, 
343                          show_spi_transport_period,
344                          store_spi_transport_period);
345
346 static ssize_t show_spi_host_signalling(struct class_device *cdev, char *buf)
347 {
348         struct Scsi_Host *shost = transport_class_to_shost(cdev);
349         struct spi_internal *i = to_spi_internal(shost->transportt);
350
351         if (i->f->get_signalling)
352                 i->f->get_signalling(shost);
353
354         return sprintf(buf, "%s\n", spi_signal_to_string(spi_signalling(shost)));
355 }
356 static ssize_t store_spi_host_signalling(struct class_device *cdev,
357                                          const char *buf, size_t count)
358 {
359         struct Scsi_Host *shost = transport_class_to_shost(cdev);
360         struct spi_internal *i = to_spi_internal(shost->transportt);
361         enum spi_signal_type type = spi_signal_to_value(buf);
362
363         if (type != SPI_SIGNAL_UNKNOWN)
364                 return count;
365
366         i->f->set_signalling(shost, type);
367         return count;
368 }
369 static CLASS_DEVICE_ATTR(signalling, S_IRUGO | S_IWUSR,
370                          show_spi_host_signalling,
371                          store_spi_host_signalling);
372
373 #define DV_SET(x, y)                    \
374         if(i->f->set_##x)               \
375                 i->f->set_##x(sdev->sdev_target, y)
376
377 #define DV_LOOPS        3
378 #define DV_TIMEOUT      (10*HZ)
379 #define DV_RETRIES      3       /* should only need at most 
380                                  * two cc/ua clears */
381
382
383 /* This is for read/write Domain Validation:  If the device supports
384  * an echo buffer, we do read/write tests to it */
385 static int
386 spi_dv_device_echo_buffer(struct scsi_request *sreq, u8 *buffer,
387                           u8 *ptr, const int retries)
388 {
389         struct scsi_device *sdev = sreq->sr_device;
390         int len = ptr - buffer;
391         int j, k, r;
392         unsigned int pattern = 0x0000ffff;
393
394         const char spi_write_buffer[] = {
395                 WRITE_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
396         };
397         const char spi_read_buffer[] = {
398                 READ_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
399         };
400
401         /* set up the pattern buffer.  Doesn't matter if we spill
402          * slightly beyond since that's where the read buffer is */
403         for (j = 0; j < len; ) {
404
405                 /* fill the buffer with counting (test a) */
406                 for ( ; j < min(len, 32); j++)
407                         buffer[j] = j;
408                 k = j;
409                 /* fill the buffer with alternating words of 0x0 and
410                  * 0xffff (test b) */
411                 for ( ; j < min(len, k + 32); j += 2) {
412                         u16 *word = (u16 *)&buffer[j];
413                         
414                         *word = (j & 0x02) ? 0x0000 : 0xffff;
415                 }
416                 k = j;
417                 /* fill with crosstalk (alternating 0x5555 0xaaa)
418                  * (test c) */
419                 for ( ; j < min(len, k + 32); j += 2) {
420                         u16 *word = (u16 *)&buffer[j];
421
422                         *word = (j & 0x02) ? 0x5555 : 0xaaaa;
423                 }
424                 k = j;
425                 /* fill with shifting bits (test d) */
426                 for ( ; j < min(len, k + 32); j += 4) {
427                         u32 *word = (unsigned int *)&buffer[j];
428                         u32 roll = (pattern & 0x80000000) ? 1 : 0;
429                         
430                         *word = pattern;
431                         pattern = (pattern << 1) | roll;
432                 }
433                 /* don't bother with random data (test e) */
434         }
435
436         for (r = 0; r < retries; r++) {
437                 sreq->sr_cmd_len = 0;   /* wait_req to fill in */
438                 sreq->sr_data_direction = DMA_TO_DEVICE;
439                 scsi_wait_req(sreq, spi_write_buffer, buffer, len,
440                               DV_TIMEOUT, DV_RETRIES);
441                 if(sreq->sr_result || !scsi_device_online(sdev)) {
442                         scsi_device_set_state(sdev, SDEV_QUIESCE);
443                         SPI_PRINTK(sdev->sdev_target, KERN_ERR, "Write Buffer failure %x\n", sreq->sr_result);
444                         return 0;
445                 }
446
447                 memset(ptr, 0, len);
448                 sreq->sr_cmd_len = 0;   /* wait_req to fill in */
449                 sreq->sr_data_direction = DMA_FROM_DEVICE;
450                 scsi_wait_req(sreq, spi_read_buffer, ptr, len,
451                               DV_TIMEOUT, DV_RETRIES);
452                 scsi_device_set_state(sdev, SDEV_QUIESCE);
453
454                 if (memcmp(buffer, ptr, len) != 0)
455                         return 0;
456         }
457         return 1;
458 }
459
460 /* This is for the simplest form of Domain Validation: a read test
461  * on the inquiry data from the device */
462 static int
463 spi_dv_device_compare_inquiry(struct scsi_request *sreq, u8 *buffer,
464                               u8 *ptr, const int retries)
465 {
466         int r;
467         const int len = sreq->sr_device->inquiry_len;
468         struct scsi_device *sdev = sreq->sr_device;
469         const char spi_inquiry[] = {
470                 INQUIRY, 0, 0, 0, len, 0
471         };
472
473         for (r = 0; r < retries; r++) {
474                 sreq->sr_cmd_len = 0;   /* wait_req to fill in */
475                 sreq->sr_data_direction = DMA_FROM_DEVICE;
476
477                 memset(ptr, 0, len);
478
479                 scsi_wait_req(sreq, spi_inquiry, ptr, len,
480                               DV_TIMEOUT, DV_RETRIES);
481                 
482                 if(sreq->sr_result || !scsi_device_online(sdev)) {
483                         scsi_device_set_state(sdev, SDEV_QUIESCE);
484                         return 0;
485                 }
486
487                 /* If we don't have the inquiry data already, the
488                  * first read gets it */
489                 if (ptr == buffer) {
490                         ptr += len;
491                         --r;
492                         continue;
493                 }
494
495                 if (memcmp(buffer, ptr, len) != 0)
496                         /* failure */
497                         return 0;
498         }
499         return 1;
500 }
501
502 static int
503 spi_dv_retrain(struct scsi_request *sreq, u8 *buffer, u8 *ptr,
504                int (*compare_fn)(struct scsi_request *, u8 *, u8 *, int))
505 {
506         struct spi_internal *i = to_spi_internal(sreq->sr_host->transportt);
507         struct scsi_device *sdev = sreq->sr_device;
508         int period = 0, prevperiod = 0; 
509
510
511         for (;;) {
512                 int newperiod;
513                 if (compare_fn(sreq, buffer, ptr, DV_LOOPS))
514                         /* Successful DV */
515                         break;
516
517                 /* OK, retrain, fallback */
518                 if (i->f->get_period)
519                         i->f->get_period(sdev->sdev_target);
520                 newperiod = spi_period(sdev->sdev_target);
521                 period = newperiod > period ? newperiod : period;
522                 if (period < 0x0d)
523                         period++;
524                 else
525                         period += period >> 1;
526
527                 if (unlikely(period > 0xff || period == prevperiod)) {
528                         /* Total failure; set to async and return */
529                         SPI_PRINTK(sdev->sdev_target, KERN_ERR, "Domain Validation Failure, dropping back to Asynchronous\n");
530                         DV_SET(offset, 0);
531                         return 0;
532                 }
533                 SPI_PRINTK(sdev->sdev_target, KERN_ERR, "Domain Validation detected failure, dropping back\n");
534                 DV_SET(period, period);
535                 prevperiod = period;
536         }
537         return 1;
538 }
539
540 static int
541 spi_dv_device_get_echo_buffer(struct scsi_request *sreq, u8 *buffer)
542 {
543         int l;
544
545         /* first off do a test unit ready.  This can error out 
546          * because of reservations or some other reason.  If it
547          * fails, the device won't let us write to the echo buffer
548          * so just return failure */
549         
550         const char spi_test_unit_ready[] = {
551                 TEST_UNIT_READY, 0, 0, 0, 0, 0
552         };
553
554         const char spi_read_buffer_descriptor[] = {
555                 READ_BUFFER, 0x0b, 0, 0, 0, 0, 0, 0, 4, 0
556         };
557
558         
559         sreq->sr_cmd_len = 0;
560         sreq->sr_data_direction = DMA_NONE;
561
562         /* We send a set of three TURs to clear any outstanding 
563          * unit attention conditions if they exist (Otherwise the
564          * buffer tests won't be happy).  If the TUR still fails
565          * (reservation conflict, device not ready, etc) just
566          * skip the write tests */
567         for (l = 0; ; l++) {
568                 scsi_wait_req(sreq, spi_test_unit_ready, NULL, 0,
569                               DV_TIMEOUT, DV_RETRIES);
570
571                 if(sreq->sr_result) {
572                         if(l >= 3)
573                                 return 0;
574                 } else {
575                         /* TUR succeeded */
576                         break;
577                 }
578         }
579
580         sreq->sr_cmd_len = 0;
581         sreq->sr_data_direction = DMA_FROM_DEVICE;
582
583         scsi_wait_req(sreq, spi_read_buffer_descriptor, buffer, 4,
584                       DV_TIMEOUT, DV_RETRIES);
585
586         if (sreq->sr_result)
587                 /* Device has no echo buffer */
588                 return 0;
589
590         return buffer[3] + ((buffer[2] & 0x1f) << 8);
591 }
592
593 static void
594 spi_dv_device_internal(struct scsi_request *sreq, u8 *buffer)
595 {
596         struct spi_internal *i = to_spi_internal(sreq->sr_host->transportt);
597         struct scsi_device *sdev = sreq->sr_device;
598         int len = sdev->inquiry_len;
599         /* first set us up for narrow async */
600         DV_SET(offset, 0);
601         DV_SET(width, 0);
602         
603         if (!spi_dv_device_compare_inquiry(sreq, buffer, buffer, DV_LOOPS)) {
604                 SPI_PRINTK(sdev->sdev_target, KERN_ERR, "Domain Validation Initial Inquiry Failed\n");
605                 /* FIXME: should probably offline the device here? */
606                 return;
607         }
608
609         /* test width */
610         if (i->f->set_width && sdev->wdtr) {
611                 i->f->set_width(sdev->sdev_target, 1);
612
613                 if (!spi_dv_device_compare_inquiry(sreq, buffer,
614                                                    buffer + len,
615                                                    DV_LOOPS)) {
616                         SPI_PRINTK(sdev->sdev_target, KERN_ERR, "Wide Transfers Fail\n");
617                         i->f->set_width(sdev->sdev_target, 0);
618                 }
619         }
620
621         if (!i->f->set_period)
622                 return;
623
624         /* device can't handle synchronous */
625         if(!sdev->ppr && !sdev->sdtr)
626                 return;
627
628         /* now set up to the maximum */
629         DV_SET(offset, 255);
630         DV_SET(period, 1);
631         if (!spi_dv_retrain(sreq, buffer, buffer + len,
632                             spi_dv_device_compare_inquiry))
633                 return;
634
635         /* OK, now we have our initial speed set by the read only inquiry
636          * test, now try an echo buffer test (if the device allows it) */
637
638         if ((len = spi_dv_device_get_echo_buffer(sreq, buffer)) == 0) {
639                 SPI_PRINTK(sdev->sdev_target, KERN_INFO, "Domain Validation skipping write tests\n");
640                 return;
641         }
642         if (len > SPI_MAX_ECHO_BUFFER_SIZE) {
643                 SPI_PRINTK(sdev->sdev_target, KERN_WARNING, "Echo buffer size %d is too big, trimming to %d\n", len, SPI_MAX_ECHO_BUFFER_SIZE);
644                 len = SPI_MAX_ECHO_BUFFER_SIZE;
645         }
646
647         spi_dv_retrain(sreq, buffer, buffer + len,
648                        spi_dv_device_echo_buffer);
649 }
650
651
652 /**     spi_dv_device - Do Domain Validation on the device
653  *      @sdev:          scsi device to validate
654  *
655  *      Performs the domain validation on the given device in the
656  *      current execution thread.  Since DV operations may sleep,
657  *      the current thread must have user context.  Also no SCSI
658  *      related locks that would deadlock I/O issued by the DV may
659  *      be held.
660  */
661 void
662 spi_dv_device(struct scsi_device *sdev)
663 {
664         struct scsi_request *sreq = scsi_allocate_request(sdev, GFP_KERNEL);
665         struct scsi_target *starget = sdev->sdev_target;
666         u8 *buffer;
667         const int len = SPI_MAX_ECHO_BUFFER_SIZE*2;
668
669         if (unlikely(!sreq))
670                 return;
671
672         if (unlikely(scsi_device_get(sdev)))
673                 goto out_free_req;
674
675         buffer = kmalloc(len, GFP_KERNEL);
676
677         if (unlikely(!buffer))
678                 goto out_put;
679
680         memset(buffer, 0, len);
681
682         /* We need to verify that the actual device will quiesce; the
683          * later target quiesce is just a nice to have */
684         if (unlikely(scsi_device_quiesce(sdev)))
685                 goto out_free;
686
687         scsi_target_quiesce(starget);
688
689         spi_dv_pending(starget) = 1;
690         down(&spi_dv_sem(starget));
691
692         SPI_PRINTK(starget, KERN_INFO, "Beginning Domain Validation\n");
693
694         spi_dv_device_internal(sreq, buffer);
695
696         SPI_PRINTK(starget, KERN_INFO, "Ending Domain Validation\n");
697
698         up(&spi_dv_sem(starget));
699         spi_dv_pending(starget) = 0;
700
701         scsi_target_resume(starget);
702
703         spi_initial_dv(starget) = 1;
704
705  out_free:
706         kfree(buffer);
707  out_put:
708         scsi_device_put(sdev);
709  out_free_req:
710         scsi_release_request(sreq);
711 }
712 EXPORT_SYMBOL(spi_dv_device);
713
714 struct work_queue_wrapper {
715         struct work_struct      work;
716         struct scsi_device      *sdev;
717 };
718
719 static void
720 spi_dv_device_work_wrapper(void *data)
721 {
722         struct work_queue_wrapper *wqw = (struct work_queue_wrapper *)data;
723         struct scsi_device *sdev = wqw->sdev;
724
725         kfree(wqw);
726         spi_dv_device(sdev);
727         spi_dv_pending(sdev->sdev_target) = 0;
728         scsi_device_put(sdev);
729 }
730
731
732 /**
733  *      spi_schedule_dv_device - schedule domain validation to occur on the device
734  *      @sdev:  The device to validate
735  *
736  *      Identical to spi_dv_device() above, except that the DV will be
737  *      scheduled to occur in a workqueue later.  All memory allocations
738  *      are atomic, so may be called from any context including those holding
739  *      SCSI locks.
740  */
741 void
742 spi_schedule_dv_device(struct scsi_device *sdev)
743 {
744         struct work_queue_wrapper *wqw =
745                 kmalloc(sizeof(struct work_queue_wrapper), GFP_ATOMIC);
746
747         if (unlikely(!wqw))
748                 return;
749
750         if (unlikely(spi_dv_pending(sdev->sdev_target))) {
751                 kfree(wqw);
752                 return;
753         }
754         /* Set pending early (dv_device doesn't check it, only sets it) */
755         spi_dv_pending(sdev->sdev_target) = 1;
756         if (unlikely(scsi_device_get(sdev))) {
757                 kfree(wqw);
758                 spi_dv_pending(sdev->sdev_target) = 0;
759                 return;
760         }
761
762         INIT_WORK(&wqw->work, spi_dv_device_work_wrapper, wqw);
763         wqw->sdev = sdev;
764
765         schedule_work(&wqw->work);
766 }
767 EXPORT_SYMBOL(spi_schedule_dv_device);
768
769 #define SETUP_ATTRIBUTE(field)                                          \
770         i->private_attrs[count] = class_device_attr_##field;            \
771         if (!i->f->set_##field) {                                       \
772                 i->private_attrs[count].attr.mode = S_IRUGO;            \
773                 i->private_attrs[count].store = NULL;                   \
774         }                                                               \
775         i->attrs[count] = &i->private_attrs[count];                     \
776         if (i->f->show_##field)                                         \
777                 count++
778
779 #define SETUP_HOST_ATTRIBUTE(field)                                     \
780         i->private_host_attrs[count] = class_device_attr_##field;       \
781         if (!i->f->set_##field) {                                       \
782                 i->private_host_attrs[count].attr.mode = S_IRUGO;       \
783                 i->private_host_attrs[count].store = NULL;              \
784         }                                                               \
785         i->host_attrs[count] = &i->private_host_attrs[count];           \
786         count++
787
788 struct scsi_transport_template *
789 spi_attach_transport(struct spi_function_template *ft)
790 {
791         struct spi_internal *i = kmalloc(sizeof(struct spi_internal),
792                                          GFP_KERNEL);
793         int count = 0;
794         if (unlikely(!i))
795                 return NULL;
796
797         memset(i, 0, sizeof(struct spi_internal));
798
799
800         i->t.target_attrs = &i->attrs[0];
801         i->t.target_class = &spi_transport_class;
802         i->t.target_setup = &spi_setup_transport_attrs;
803         i->t.device_configure = &spi_configure_device;
804         i->t.target_size = sizeof(struct spi_transport_attrs);
805         i->t.host_attrs = &i->host_attrs[0];
806         i->t.host_class = &spi_host_class;
807         i->t.host_setup = &spi_setup_host_attrs;
808         i->t.host_size = sizeof(struct spi_host_attrs);
809         i->f = ft;
810
811         SETUP_ATTRIBUTE(period);
812         SETUP_ATTRIBUTE(offset);
813         SETUP_ATTRIBUTE(width);
814         SETUP_ATTRIBUTE(iu);
815         SETUP_ATTRIBUTE(dt);
816         SETUP_ATTRIBUTE(qas);
817         SETUP_ATTRIBUTE(wr_flow);
818         SETUP_ATTRIBUTE(rd_strm);
819         SETUP_ATTRIBUTE(rti);
820         SETUP_ATTRIBUTE(pcomp_en);
821
822         /* if you add an attribute but forget to increase SPI_NUM_ATTRS
823          * this bug will trigger */
824         BUG_ON(count > SPI_NUM_ATTRS);
825
826         i->attrs[count++] = &class_device_attr_revalidate;
827
828         i->attrs[count] = NULL;
829
830         count = 0;
831         SETUP_HOST_ATTRIBUTE(signalling);
832
833         BUG_ON(count > SPI_HOST_ATTRS);
834
835         i->host_attrs[count] = NULL;
836
837         return &i->t;
838 }
839 EXPORT_SYMBOL(spi_attach_transport);
840
841 void spi_release_transport(struct scsi_transport_template *t)
842 {
843         struct spi_internal *i = to_spi_internal(t);
844
845         kfree(i);
846 }
847 EXPORT_SYMBOL(spi_release_transport);
848
849
850 MODULE_AUTHOR("Martin Hicks");
851 MODULE_DESCRIPTION("SPI Transport Attributes");
852 MODULE_LICENSE("GPL");
853
854 module_init(spi_transport_init);
855 module_exit(spi_transport_exit);