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