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