VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[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/scsi_device.h>
31 #include <scsi/scsi_host.h>
32 #include <scsi/scsi_request.h>
33 #include <scsi/scsi_transport.h>
34 #include <scsi/scsi_transport_spi.h>
35
36 #define SPI_PRINTK(x, l, f, a...)       printk(l "scsi(%d:%d:%d:%d): " f, (x)->host->host_no, (x)->channel, (x)->id, (x)->lun , ##a)
37
38 static void transport_class_release(struct class_device *class_dev);
39
40 #define SPI_NUM_ATTRS 10        /* increase this if you add attributes */
41 #define SPI_OTHER_ATTRS 1       /* Increase this if you add "always
42                                  * on" attributes */
43
44 #define SPI_MAX_ECHO_BUFFER_SIZE        4096
45
46 /* Private data accessors (keep these out of the header file) */
47 #define spi_dv_pending(x) (((struct spi_transport_attrs *)&(x)->transport_data)->dv_pending)
48 #define spi_dv_sem(x) (((struct spi_transport_attrs *)&(x)->transport_data)->dv_sem)
49
50 struct spi_internal {
51         struct scsi_transport_template t;
52         struct spi_function_template *f;
53         /* The actual attributes */
54         struct class_device_attribute private_attrs[SPI_NUM_ATTRS];
55         /* The array of null terminated pointers to attributes 
56          * needed by scsi_sysfs.c */
57         struct class_device_attribute *attrs[SPI_NUM_ATTRS + SPI_OTHER_ATTRS + 1];
58 };
59
60 #define to_spi_internal(tmpl)   container_of(tmpl, struct spi_internal, t)
61
62 static const char *const ppr_to_ns[] = {
63         /* The PPR values 0-6 are reserved, fill them in when
64          * the committee defines them */
65         NULL,                   /* 0x00 */
66         NULL,                   /* 0x01 */
67         NULL,                   /* 0x02 */
68         NULL,                   /* 0x03 */
69         NULL,                   /* 0x04 */
70         NULL,                   /* 0x05 */
71         NULL,                   /* 0x06 */
72         "3.125",                /* 0x07 */
73         "6.25",                 /* 0x08 */
74         "12.5",                 /* 0x09 */
75         "25",                   /* 0x0a */
76         "30.3",                 /* 0x0b */
77         "50",                   /* 0x0c */
78 };
79 /* The PPR values at which you calculate the period in ns by multiplying
80  * by 4 */
81 #define SPI_STATIC_PPR  0x0c
82
83 struct class spi_transport_class = {
84         .name = "spi_transport",
85         .release = transport_class_release,
86 };
87
88 static __init int spi_transport_init(void)
89 {
90         return class_register(&spi_transport_class);
91 }
92
93 static void __exit spi_transport_exit(void)
94 {
95         class_unregister(&spi_transport_class);
96 }
97
98 static int spi_setup_transport_attrs(struct scsi_device *sdev)
99 {
100         spi_period(sdev) = -1;  /* illegal value */
101         spi_offset(sdev) = 0;   /* async */
102         spi_width(sdev) = 0;    /* narrow */
103         spi_iu(sdev) = 0;       /* no IU */
104         spi_dt(sdev) = 0;       /* ST */
105         spi_qas(sdev) = 0;
106         spi_wr_flow(sdev) = 0;
107         spi_rd_strm(sdev) = 0;
108         spi_rti(sdev) = 0;
109         spi_pcomp_en(sdev) = 0;
110         spi_dv_pending(sdev) = 0;
111         init_MUTEX(&spi_dv_sem(sdev));
112
113         return 0;
114 }
115
116 static void transport_class_release(struct class_device *class_dev)
117 {
118         struct scsi_device *sdev = transport_class_to_sdev(class_dev);
119         put_device(&sdev->sdev_gendev);
120 }
121
122 #define spi_transport_show_function(field, format_string)               \
123                                                                         \
124 static ssize_t                                                          \
125 show_spi_transport_##field(struct class_device *cdev, char *buf)        \
126 {                                                                       \
127         struct scsi_device *sdev = transport_class_to_sdev(cdev);       \
128         struct spi_transport_attrs *tp;                                 \
129         struct spi_internal *i = to_spi_internal(sdev->host->transportt); \
130         tp = (struct spi_transport_attrs *)&sdev->transport_data;       \
131         if (i->f->get_##field)                                          \
132                 i->f->get_##field(sdev);                                \
133         return snprintf(buf, 20, format_string, tp->field);             \
134 }
135
136 #define spi_transport_store_function(field, format_string)              \
137 static ssize_t                                                          \
138 store_spi_transport_##field(struct class_device *cdev, const char *buf, \
139                             size_t count)                               \
140 {                                                                       \
141         int val;                                                        \
142         struct scsi_device *sdev = transport_class_to_sdev(cdev);       \
143         struct spi_internal *i = to_spi_internal(sdev->host->transportt); \
144                                                                         \
145         val = simple_strtoul(buf, NULL, 0);                             \
146         i->f->set_##field(sdev, val);                                   \
147         return count;                                                   \
148 }
149
150 #define spi_transport_rd_attr(field, format_string)                     \
151         spi_transport_show_function(field, format_string)               \
152         spi_transport_store_function(field, format_string)              \
153 static CLASS_DEVICE_ATTR(field, S_IRUGO | S_IWUSR,                      \
154                          show_spi_transport_##field,                    \
155                          store_spi_transport_##field);
156
157 /* The Parallel SCSI Tranport Attributes: */
158 spi_transport_rd_attr(offset, "%d\n");
159 spi_transport_rd_attr(width, "%d\n");
160 spi_transport_rd_attr(iu, "%d\n");
161 spi_transport_rd_attr(dt, "%d\n");
162 spi_transport_rd_attr(qas, "%d\n");
163 spi_transport_rd_attr(wr_flow, "%d\n");
164 spi_transport_rd_attr(rd_strm, "%d\n");
165 spi_transport_rd_attr(rti, "%d\n");
166 spi_transport_rd_attr(pcomp_en, "%d\n");
167
168 static ssize_t
169 store_spi_revalidate(struct class_device *cdev, const char *buf, size_t count)
170 {
171         struct scsi_device *sdev = transport_class_to_sdev(cdev);
172
173         spi_dv_device(sdev);
174         return count;
175 }
176 static CLASS_DEVICE_ATTR(revalidate, S_IWUSR, NULL, store_spi_revalidate);
177
178 /* Translate the period into ns according to the current spec
179  * for SDTR/PPR messages */
180 static ssize_t show_spi_transport_period(struct class_device *cdev, char *buf)
181
182 {
183         struct scsi_device *sdev = transport_class_to_sdev(cdev);
184         struct spi_transport_attrs *tp;
185         const char *str;
186         struct spi_internal *i = to_spi_internal(sdev->host->transportt);
187
188         tp = (struct spi_transport_attrs *)&sdev->transport_data;
189
190         if (i->f->get_period)
191                 i->f->get_period(sdev);
192
193         switch(tp->period) {
194
195         case 0x07 ... SPI_STATIC_PPR:
196                 str = ppr_to_ns[tp->period];
197                 if(!str)
198                         str = "reserved";
199                 break;
200
201
202         case (SPI_STATIC_PPR+1) ... 0xff:
203                 return sprintf(buf, "%d\n", tp->period * 4);
204
205         default:
206                 str = "unknown";
207         }
208         return sprintf(buf, "%s\n", str);
209 }
210
211 static ssize_t
212 store_spi_transport_period(struct class_device *cdev, const char *buf,
213                             size_t count)
214 {
215         struct scsi_device *sdev = transport_class_to_sdev(cdev);
216         struct spi_internal *i = to_spi_internal(sdev->host->transportt);
217         int j, period = -1;
218
219         for (j = 0; j < SPI_STATIC_PPR; j++) {
220                 int len;
221
222                 if(ppr_to_ns[j] == NULL)
223                         continue;
224
225                 len = strlen(ppr_to_ns[j]);
226
227                 if(strncmp(ppr_to_ns[j], buf, len) != 0)
228                         continue;
229
230                 if(buf[len] != '\n')
231                         continue;
232                 
233                 period = j;
234                 break;
235         }
236
237         if (period == -1) {
238                 int val = simple_strtoul(buf, NULL, 0);
239
240
241                 /* Should probably check limits here, but this
242                  * gets reasonably close to OK for most things */
243                 period = val/4;
244         }
245
246         if (period > 0xff)
247                 period = 0xff;
248
249         i->f->set_period(sdev, period);
250
251         return count;
252 }
253         
254 static CLASS_DEVICE_ATTR(period, S_IRUGO | S_IWUSR, 
255                          show_spi_transport_period,
256                          store_spi_transport_period);
257
258 #define DV_SET(x, y)                    \
259         if(i->f->set_##x)               \
260                 i->f->set_##x(sdev, y)
261
262 #define DV_LOOPS        3
263 #define DV_TIMEOUT      (10*HZ)
264 #define DV_RETRIES      3       /* should only need at most 
265                                  * two cc/ua clears */
266
267
268 /* This is for read/write Domain Validation:  If the device supports
269  * an echo buffer, we do read/write tests to it */
270 static int
271 spi_dv_device_echo_buffer(struct scsi_request *sreq, u8 *buffer,
272                           u8 *ptr, const int retries)
273 {
274         struct scsi_device *sdev = sreq->sr_device;
275         int len = ptr - buffer;
276         int j, k, r;
277         unsigned int pattern = 0x0000ffff;
278
279         const char spi_write_buffer[] = {
280                 WRITE_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
281         };
282         const char spi_read_buffer[] = {
283                 READ_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
284         };
285
286         /* set up the pattern buffer.  Doesn't matter if we spill
287          * slightly beyond since that's where the read buffer is */
288         for (j = 0; j < len; ) {
289
290                 /* fill the buffer with counting (test a) */
291                 for ( ; j < min(len, 32); j++)
292                         buffer[j] = j;
293                 k = j;
294                 /* fill the buffer with alternating words of 0x0 and
295                  * 0xffff (test b) */
296                 for ( ; j < min(len, k + 32); j += 2) {
297                         u16 *word = (u16 *)&buffer[j];
298                         
299                         *word = (j & 0x02) ? 0x0000 : 0xffff;
300                 }
301                 k = j;
302                 /* fill with crosstalk (alternating 0x5555 0xaaa)
303                  * (test c) */
304                 for ( ; j < min(len, k + 32); j += 2) {
305                         u16 *word = (u16 *)&buffer[j];
306
307                         *word = (j & 0x02) ? 0x5555 : 0xaaaa;
308                 }
309                 k = j;
310                 /* fill with shifting bits (test d) */
311                 for ( ; j < min(len, k + 32); j += 4) {
312                         u32 *word = (unsigned int *)&buffer[j];
313                         u32 roll = (pattern & 0x80000000) ? 1 : 0;
314                         
315                         *word = pattern;
316                         pattern = (pattern << 1) | roll;
317                 }
318                 /* don't bother with random data (test e) */
319         }
320
321         for (r = 0; r < retries; r++) {
322                 sreq->sr_cmd_len = 0;   /* wait_req to fill in */
323                 sreq->sr_data_direction = DMA_TO_DEVICE;
324                 scsi_wait_req(sreq, spi_write_buffer, buffer, len,
325                               DV_TIMEOUT, DV_RETRIES);
326                 if(sreq->sr_result || !scsi_device_online(sdev)) {
327                         scsi_device_set_state(sdev, SDEV_QUIESCE);
328                         SPI_PRINTK(sdev, KERN_ERR, "Write Buffer failure %x\n", sreq->sr_result);
329                         return 0;
330                 }
331
332                 memset(ptr, 0, len);
333                 sreq->sr_cmd_len = 0;   /* wait_req to fill in */
334                 sreq->sr_data_direction = DMA_FROM_DEVICE;
335                 scsi_wait_req(sreq, spi_read_buffer, ptr, len,
336                               DV_TIMEOUT, DV_RETRIES);
337                 scsi_device_set_state(sdev, SDEV_QUIESCE);
338
339                 if (memcmp(buffer, ptr, len) != 0)
340                         return 0;
341         }
342         return 1;
343 }
344
345 /* This is for the simplest form of Domain Validation: a read test
346  * on the inquiry data from the device */
347 static int
348 spi_dv_device_compare_inquiry(struct scsi_request *sreq, u8 *buffer,
349                               u8 *ptr, const int retries)
350 {
351         int r;
352         const int len = sreq->sr_device->inquiry_len;
353         struct scsi_device *sdev = sreq->sr_device;
354         const char spi_inquiry[] = {
355                 INQUIRY, 0, 0, 0, len, 0
356         };
357
358         for (r = 0; r < retries; r++) {
359                 sreq->sr_cmd_len = 0;   /* wait_req to fill in */
360                 sreq->sr_data_direction = DMA_FROM_DEVICE;
361
362                 memset(ptr, 0, len);
363
364                 scsi_wait_req(sreq, spi_inquiry, ptr, len,
365                               DV_TIMEOUT, DV_RETRIES);
366                 
367                 if(sreq->sr_result || !scsi_device_online(sdev)) {
368                         scsi_device_set_state(sdev, SDEV_QUIESCE);
369                         return 0;
370                 }
371
372                 /* If we don't have the inquiry data already, the
373                  * first read gets it */
374                 if (ptr == buffer) {
375                         ptr += len;
376                         --r;
377                         continue;
378                 }
379
380                 if (memcmp(buffer, ptr, len) != 0)
381                         /* failure */
382                         return 0;
383         }
384         return 1;
385 }
386
387 static int
388 spi_dv_retrain(struct scsi_request *sreq, u8 *buffer, u8 *ptr,
389                int (*compare_fn)(struct scsi_request *, u8 *, u8 *, int))
390 {
391         struct spi_internal *i = to_spi_internal(sreq->sr_host->transportt);
392         struct scsi_device *sdev = sreq->sr_device;
393         int period = 0, prevperiod = 0; 
394
395
396         for (;;) {
397                 int newperiod;
398                 if (compare_fn(sreq, buffer, ptr, DV_LOOPS))
399                         /* Successful DV */
400                         break;
401
402                 /* OK, retrain, fallback */
403                 if (i->f->get_period)
404                         i->f->get_period(sdev);
405                 newperiod = spi_period(sdev);
406                 period = newperiod > period ? newperiod : period;
407                 if (period < 0x0d)
408                         period++;
409                 else
410                         period += period >> 1;
411
412                 if (unlikely(period > 0xff || period == prevperiod)) {
413                         /* Total failure; set to async and return */
414                         SPI_PRINTK(sdev, KERN_ERR, "Domain Validation Failure, dropping back to Asynchronous\n");
415                         DV_SET(offset, 0);
416                         return 0;
417                 }
418                 SPI_PRINTK(sdev, KERN_ERR, "Domain Validation detected failure, dropping back\n");
419                 DV_SET(period, period);
420                 prevperiod = period;
421         }
422         return 1;
423 }
424
425 static int
426 spi_dv_device_get_echo_buffer(struct scsi_request *sreq, u8 *buffer)
427 {
428         int l;
429
430         /* first off do a test unit ready.  This can error out 
431          * because of reservations or some other reason.  If it
432          * fails, the device won't let us write to the echo buffer
433          * so just return failure */
434         
435         const char spi_test_unit_ready[] = {
436                 TEST_UNIT_READY, 0, 0, 0, 0, 0
437         };
438
439         const char spi_read_buffer_descriptor[] = {
440                 READ_BUFFER, 0x0b, 0, 0, 0, 0, 0, 0, 4, 0
441         };
442
443         
444         sreq->sr_cmd_len = 0;
445         sreq->sr_data_direction = DMA_NONE;
446
447         /* We send a set of three TURs to clear any outstanding 
448          * unit attention conditions if they exist (Otherwise the
449          * buffer tests won't be happy).  If the TUR still fails
450          * (reservation conflict, device not ready, etc) just
451          * skip the write tests */
452         for (l = 0; ; l++) {
453                 scsi_wait_req(sreq, spi_test_unit_ready, NULL, 0,
454                               DV_TIMEOUT, DV_RETRIES);
455
456                 if(sreq->sr_result) {
457                         if(l >= 3)
458                                 return 0;
459                 } else {
460                         /* TUR succeeded */
461                         break;
462                 }
463         }
464
465         sreq->sr_cmd_len = 0;
466         sreq->sr_data_direction = DMA_FROM_DEVICE;
467
468         scsi_wait_req(sreq, spi_read_buffer_descriptor, buffer, 4,
469                       DV_TIMEOUT, DV_RETRIES);
470
471         if (sreq->sr_result)
472                 /* Device has no echo buffer */
473                 return 0;
474
475         return buffer[3] + ((buffer[2] & 0x1f) << 8);
476 }
477
478 static void
479 spi_dv_device_internal(struct scsi_request *sreq, u8 *buffer)
480 {
481         struct spi_internal *i = to_spi_internal(sreq->sr_host->transportt);
482         struct scsi_device *sdev = sreq->sr_device;
483         int len = sdev->inquiry_len;
484         /* first set us up for narrow async */
485         DV_SET(offset, 0);
486         DV_SET(width, 0);
487         
488         if (!spi_dv_device_compare_inquiry(sreq, buffer, buffer, DV_LOOPS)) {
489                 SPI_PRINTK(sdev, KERN_ERR, "Domain Validation Initial Inquiry Failed\n");
490                 /* FIXME: should probably offline the device here? */
491                 return;
492         }
493
494         /* test width */
495         if (i->f->set_width && sdev->wdtr) {
496                 i->f->set_width(sdev, 1);
497
498                 if (!spi_dv_device_compare_inquiry(sreq, buffer,
499                                                    buffer + len,
500                                                    DV_LOOPS)) {
501                         SPI_PRINTK(sdev, KERN_ERR, "Wide Transfers Fail\n");
502                         i->f->set_width(sdev, 0);
503                 }
504         }
505
506         if (!i->f->set_period)
507                 return;
508
509         /* device can't handle synchronous */
510         if(!sdev->ppr && !sdev->sdtr)
511                 return;
512
513         /* now set up to the maximum */
514         DV_SET(offset, 255);
515         DV_SET(period, 1);
516         if (!spi_dv_retrain(sreq, buffer, buffer + len,
517                             spi_dv_device_compare_inquiry))
518                 return;
519
520         /* OK, now we have our initial speed set by the read only inquiry
521          * test, now try an echo buffer test (if the device allows it) */
522
523         if ((len = spi_dv_device_get_echo_buffer(sreq, buffer)) == 0) {
524                 SPI_PRINTK(sdev, KERN_INFO, "Domain Validation skipping write tests\n");
525                 return;
526         }
527         if (len > SPI_MAX_ECHO_BUFFER_SIZE) {
528                 SPI_PRINTK(sdev, KERN_WARNING, "Echo buffer size %d is too big, trimming to %d\n", len, SPI_MAX_ECHO_BUFFER_SIZE);
529                 len = SPI_MAX_ECHO_BUFFER_SIZE;
530         }
531
532         spi_dv_retrain(sreq, buffer, buffer + len,
533                        spi_dv_device_echo_buffer);
534 }
535
536
537 /**     spi_dv_device - Do Domain Validation on the device
538  *      @sdev:          scsi device to validate
539  *
540  *      Performs the domain validation on the given device in the
541  *      current execution thread.  Since DV operations may sleep,
542  *      the current thread must have user context.  Also no SCSI
543  *      related locks that would deadlock I/O issued by the DV may
544  *      be held.
545  */
546 void
547 spi_dv_device(struct scsi_device *sdev)
548 {
549         struct scsi_request *sreq = scsi_allocate_request(sdev, GFP_KERNEL);
550         u8 *buffer;
551         const int len = SPI_MAX_ECHO_BUFFER_SIZE*2;
552
553         if (unlikely(!sreq))
554                 return;
555
556         if (unlikely(scsi_device_get(sdev)))
557                 goto out_free_req;
558
559         buffer = kmalloc(len, GFP_KERNEL);
560
561         if (unlikely(!buffer))
562                 goto out_put;
563
564         memset(buffer, 0, len);
565
566         if (unlikely(scsi_device_quiesce(sdev)))
567                 goto out_free;
568
569         spi_dv_pending(sdev) = 1;
570         down(&spi_dv_sem(sdev));
571
572         SPI_PRINTK(sdev, KERN_INFO, "Beginning Domain Validation\n");
573
574         spi_dv_device_internal(sreq, buffer);
575
576         SPI_PRINTK(sdev, KERN_INFO, "Ending Domain Validation\n");
577
578         up(&spi_dv_sem(sdev));
579         spi_dv_pending(sdev) = 0;
580
581         scsi_device_resume(sdev);
582
583  out_free:
584         kfree(buffer);
585  out_put:
586         scsi_device_put(sdev);
587  out_free_req:
588         scsi_release_request(sreq);
589 }
590 EXPORT_SYMBOL(spi_dv_device);
591
592 struct work_queue_wrapper {
593         struct work_struct      work;
594         struct scsi_device      *sdev;
595 };
596
597 static void
598 spi_dv_device_work_wrapper(void *data)
599 {
600         struct work_queue_wrapper *wqw = (struct work_queue_wrapper *)data;
601         struct scsi_device *sdev = wqw->sdev;
602
603         kfree(wqw);
604         spi_dv_device(sdev);
605         spi_dv_pending(sdev) = 0;
606         scsi_device_put(sdev);
607 }
608
609
610 /**
611  *      spi_schedule_dv_device - schedule domain validation to occur on the device
612  *      @sdev:  The device to validate
613  *
614  *      Identical to spi_dv_device() above, except that the DV will be
615  *      scheduled to occur in a workqueue later.  All memory allocations
616  *      are atomic, so may be called from any context including those holding
617  *      SCSI locks.
618  */
619 void
620 spi_schedule_dv_device(struct scsi_device *sdev)
621 {
622         struct work_queue_wrapper *wqw =
623                 kmalloc(sizeof(struct work_queue_wrapper), GFP_ATOMIC);
624
625         if (unlikely(!wqw))
626                 return;
627
628         if (unlikely(spi_dv_pending(sdev))) {
629                 kfree(wqw);
630                 return;
631         }
632         /* Set pending early (dv_device doesn't check it, only sets it) */
633         spi_dv_pending(sdev) = 1;
634         if (unlikely(scsi_device_get(sdev))) {
635                 kfree(wqw);
636                 spi_dv_pending(sdev) = 0;
637                 return;
638         }
639
640         INIT_WORK(&wqw->work, spi_dv_device_work_wrapper, wqw);
641         wqw->sdev = sdev;
642
643         schedule_work(&wqw->work);
644 }
645 EXPORT_SYMBOL(spi_schedule_dv_device);
646
647 #define SETUP_ATTRIBUTE(field)                                          \
648         i->private_attrs[count] = class_device_attr_##field;            \
649         if (!i->f->set_##field) {                                       \
650                 i->private_attrs[count].attr.mode = S_IRUGO;            \
651                 i->private_attrs[count].store = NULL;                   \
652         }                                                               \
653         i->attrs[count] = &i->private_attrs[count];                     \
654         if (i->f->show_##field)                                         \
655                 count++
656
657 struct scsi_transport_template *
658 spi_attach_transport(struct spi_function_template *ft)
659 {
660         struct spi_internal *i = kmalloc(sizeof(struct spi_internal),
661                                          GFP_KERNEL);
662         int count = 0;
663         if (unlikely(!i))
664                 return NULL;
665
666         memset(i, 0, sizeof(struct spi_internal));
667
668
669         i->t.attrs = &i->attrs[0];
670         i->t.class = &spi_transport_class;
671         i->t.setup = &spi_setup_transport_attrs;
672         i->t.size = sizeof(struct spi_transport_attrs);
673         i->f = ft;
674
675         SETUP_ATTRIBUTE(period);
676         SETUP_ATTRIBUTE(offset);
677         SETUP_ATTRIBUTE(width);
678         SETUP_ATTRIBUTE(iu);
679         SETUP_ATTRIBUTE(dt);
680         SETUP_ATTRIBUTE(qas);
681         SETUP_ATTRIBUTE(wr_flow);
682         SETUP_ATTRIBUTE(rd_strm);
683         SETUP_ATTRIBUTE(rti);
684         SETUP_ATTRIBUTE(pcomp_en);
685
686         /* if you add an attribute but forget to increase SPI_NUM_ATTRS
687          * this bug will trigger */
688         BUG_ON(count > SPI_NUM_ATTRS);
689
690         i->attrs[count++] = &class_device_attr_revalidate;
691
692         i->attrs[count] = NULL;
693
694         return &i->t;
695 }
696 EXPORT_SYMBOL(spi_attach_transport);
697
698 void spi_release_transport(struct scsi_transport_template *t)
699 {
700         struct spi_internal *i = to_spi_internal(t);
701
702         kfree(i);
703 }
704 EXPORT_SYMBOL(spi_release_transport);
705
706
707 MODULE_AUTHOR("Martin Hicks");
708 MODULE_DESCRIPTION("SPI Transport Attributes");
709 MODULE_LICENSE("GPL");
710
711 module_init(spi_transport_init);
712 module_exit(spi_transport_exit);