blob: d0219e36080c3b79109ac405eb0cd726545585fc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
James Bottomley949bf792005-05-01 18:58:15 -050025#include <linux/blkdev.h>
Jes Sorensend158d262006-01-13 16:05:44 -080026#include <linux/mutex.h>
Randy Dunlap9f9a73b2008-04-23 09:56:14 -070027#include <linux/sysfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <scsi/scsi.h>
30#include "scsi_priv.h"
31#include <scsi/scsi_device.h>
32#include <scsi/scsi_host.h>
James Bottomley33aa6872005-08-28 11:31:14 -050033#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <scsi/scsi_eh.h>
Christoph Hellwig50668632014-10-30 14:30:06 +010035#include <scsi/scsi_tcq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <scsi/scsi_transport.h>
37#include <scsi/scsi_transport_spi.h>
38
James Bottomleyd872ebe2005-08-03 15:43:52 -050039#define SPI_NUM_ATTRS 14 /* increase this if you add attributes */
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#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
James Bottomley949bf792005-05-01 18:58:15 -050046#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
James Bottomleya9e0edb2009-06-17 19:05:05 +000051/* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070067/* Private data accessors (keep these out of the header file) */
James Bottomleydfdc58b2006-09-20 12:00:18 -040068#define spi_dv_in_progress(x) (((struct spi_transport_attrs *)&(x)->starget_data)->dv_in_progress)
Jes Sorensend158d262006-01-13 16:05:44 -080069#define spi_dv_mutex(x) (((struct spi_transport_attrs *)&(x)->starget_data)->dv_mutex)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71struct spi_internal {
72 struct scsi_transport_template t;
73 struct spi_function_template *f;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074};
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
James Bottomley33aa6872005-08-28 11:31:14 -0500119static 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)
James Bottomley949bf792005-05-01 18:58:15 -0500123{
James Bottomley33aa6872005-08-28 11:31:14 -0500124 int i, result;
125 unsigned char sense[SCSI_SENSE_BUFFERSIZE];
Christoph Hellwig76aaf872017-02-23 16:02:36 +0100126 struct scsi_sense_hdr sshdr_tmp;
127
128 if (!sshdr)
129 sshdr = &sshdr_tmp;
James Bottomley949bf792005-05-01 18:58:15 -0500130
131 for(i = 0; i < DV_RETRIES; i++) {
Christoph Hellwig76aaf872017-02-23 16:02:36 +0100132 result = scsi_execute(sdev, cmd, dir, buffer, bufflen, sense,
133 sshdr, DV_TIMEOUT, /* retries */ 1,
Mike Christie6000a362008-08-19 18:45:30 -0500134 REQ_FAILFAST_DEV |
135 REQ_FAILFAST_TRANSPORT |
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +0900136 REQ_FAILFAST_DRIVER,
Christoph Hellwig76aaf872017-02-23 16:02:36 +0100137 0, NULL);
138 if (!(driver_byte(result) & DRIVER_SENSE) ||
139 sshdr->sense_key != UNIT_ATTENTION)
140 break;
James Bottomley949bf792005-05-01 18:58:15 -0500141 }
James Bottomley33aa6872005-08-28 11:31:14 -0500142 return result;
James Bottomley949bf792005-05-01 18:58:15 -0500143}
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145static 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
Tobias Klauser6391a112006-06-08 22:23:48 -0700159 for (i = 0; i < ARRAY_SIZE(signal_types); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 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
Tobias Klauser6391a112006-06-08 22:23:48 -0700169 for (i = 0; i < ARRAY_SIZE(signal_types); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 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
James Bottomleyd0a7e572005-08-14 17:09:01 -0500178static int spi_host_setup(struct transport_container *tc, struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100179 struct device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
181 struct Scsi_Host *shost = dev_to_shost(dev);
182
183 spi_signalling(shost) = SPI_SIGNAL_UNKNOWN;
184
185 return 0;
186}
187
James Bottomley9b161a42008-01-05 10:18:27 -0600188static int spi_host_configure(struct transport_container *tc,
189 struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100190 struct device *cdev);
James Bottomley9b161a42008-01-05 10:18:27 -0600191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192static DECLARE_TRANSPORT_CLASS(spi_host_class,
193 "spi_host",
194 spi_host_setup,
195 NULL,
James Bottomley9b161a42008-01-05 10:18:27 -0600196 spi_host_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198static int spi_host_match(struct attribute_container *cont,
199 struct device *dev)
200{
201 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
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
James Bottomley9b161a42008-01-05 10:18:27 -0600211 return &shost->transportt->host_attrs.ac == cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
James Bottomley9b161a42008-01-05 10:18:27 -0600214static int spi_target_configure(struct transport_container *tc,
215 struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100216 struct device *cdev);
James Bottomley9b161a42008-01-05 10:18:27 -0600217
James Bottomleyd0a7e572005-08-14 17:09:01 -0500218static int spi_device_configure(struct transport_container *tc,
219 struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100220 struct device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
222 struct scsi_device *sdev = to_scsi_device(dev);
223 struct scsi_target *starget = sdev->sdev_target;
James Bottomleya9e0edb2009-06-17 19:05:05 +0000224 unsigned bflags = scsi_get_device_flags_keyed(sdev, &sdev->inquiry[8],
225 &sdev->inquiry[16],
226 SCSI_DEVINFO_SPI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
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);
James Bottomleya9e0edb2009-06-17 19:05:05 +0000236 if (bflags & SPI_BLIST_NOIUS) {
237 dev_info(dev, "Information Units disabled by blacklist\n");
238 spi_support_ius(starget) = 0;
239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 spi_support_qas(starget) = scsi_device_qas(sdev);
241
242 return 0;
243}
244
James Bottomleyd0a7e572005-08-14 17:09:01 -0500245static int spi_setup_transport_attrs(struct transport_container *tc,
246 struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100247 struct device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
249 struct scsi_target *starget = to_scsi_target(dev);
250
251 spi_period(starget) = -1; /* illegal value */
James Bottomley 62a86122005-05-06 18:05:20 -0500252 spi_min_period(starget) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 spi_offset(starget) = 0; /* async */
James Bottomley 62a86122005-05-06 18:05:20 -0500254 spi_max_offset(starget) = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 spi_width(starget) = 0; /* narrow */
James Bottomley 62a86122005-05-06 18:05:20 -0500256 spi_max_width(starget) = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 spi_iu(starget) = 0; /* no IU */
James Bottomleyea443192009-06-13 12:19:05 -0500258 spi_max_iu(starget) = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 spi_dt(starget) = 0; /* ST */
260 spi_qas(starget) = 0;
James Bottomleyea443192009-06-13 12:19:05 -0500261 spi_max_qas(starget) = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 spi_wr_flow(starget) = 0;
263 spi_rd_strm(starget) = 0;
264 spi_rti(starget) = 0;
265 spi_pcomp_en(starget) = 0;
James Bottomleyd872ebe2005-08-03 15:43:52 -0500266 spi_hold_mcs(starget) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 spi_dv_pending(starget) = 0;
James Bottomleydfdc58b2006-09-20 12:00:18 -0400268 spi_dv_in_progress(starget) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 spi_initial_dv(starget) = 0;
Jes Sorensend158d262006-01-13 16:05:44 -0800270 mutex_init(&spi_dv_mutex(starget));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 return 0;
273}
274
James Bottomley 62a86122005-05-06 18:05:20 -0500275#define spi_transport_show_simple(field, format_string) \
276 \
277static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100278show_spi_transport_##field(struct device *dev, \
279 struct device_attribute *attr, char *buf) \
James Bottomley 62a86122005-05-06 18:05:20 -0500280{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100281 struct scsi_target *starget = transport_class_to_starget(dev); \
James Bottomley 62a86122005-05-06 18:05:20 -0500282 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 \
Tony Jonesee959b02008-02-22 00:13:36 +0100291store_spi_transport_##field(struct device *dev, \
292 struct device_attribute *attr, \
293 const char *buf, size_t count) \
James Bottomley 62a86122005-05-06 18:05:20 -0500294{ \
295 int val; \
Tony Jonesee959b02008-02-22 00:13:36 +0100296 struct scsi_target *starget = transport_class_to_starget(dev); \
James Bottomley 62a86122005-05-06 18:05:20 -0500297 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305#define spi_transport_show_function(field, format_string) \
306 \
307static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100308show_spi_transport_##field(struct device *dev, \
309 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100311 struct scsi_target *starget = transport_class_to_starget(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 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 \
Tony Jonesee959b02008-02-22 00:13:36 +0100323store_spi_transport_##field(struct device *dev, \
324 struct device_attribute *attr, \
325 const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{ \
327 int val; \
Tony Jonesee959b02008-02-22 00:13:36 +0100328 struct scsi_target *starget = transport_class_to_starget(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
330 struct spi_internal *i = to_spi_internal(shost->transportt); \
331 \
James Bottomley9b161a42008-01-05 10:18:27 -0600332 if (!i->f->set_##field) \
333 return -EINVAL; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 val = simple_strtoul(buf, NULL, 0); \
James Bottomley9b161a42008-01-05 10:18:27 -0600335 i->f->set_##field(starget, val); \
James Bottomley 62a86122005-05-06 18:05:20 -0500336 return count; \
337}
338
339#define spi_transport_store_max(field, format_string) \
340static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100341store_spi_transport_##field(struct device *dev, \
342 struct device_attribute *attr, \
343 const char *buf, size_t count) \
James Bottomley 62a86122005-05-06 18:05:20 -0500344{ \
345 int val; \
Tony Jonesee959b02008-02-22 00:13:36 +0100346 struct scsi_target *starget = transport_class_to_starget(dev); \
James Bottomley 62a86122005-05-06 18:05:20 -0500347 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 \
James Bottomley9b161a42008-01-05 10:18:27 -0600352 if (i->f->set_##field) \
353 return -EINVAL; \
James Bottomley 62a86122005-05-06 18:05:20 -0500354 val = simple_strtoul(buf, NULL, 0); \
355 if (val > tp->max_##field) \
356 val = tp->max_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 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) \
Tony Jonesee959b02008-02-22 00:13:36 +0100364static DEVICE_ATTR(field, S_IRUGO, \
365 show_spi_transport_##field, \
366 store_spi_transport_##field);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
James Bottomley 62a86122005-05-06 18:05:20 -0500368#define spi_transport_simple_attr(field, format_string) \
369 spi_transport_show_simple(field, format_string) \
370 spi_transport_store_simple(field, format_string) \
Tony Jonesee959b02008-02-22 00:13:36 +0100371static DEVICE_ATTR(field, S_IRUGO, \
372 show_spi_transport_##field, \
373 store_spi_transport_##field);
James Bottomley 62a86122005-05-06 18:05:20 -0500374
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) \
Tony Jonesee959b02008-02-22 00:13:36 +0100379static DEVICE_ATTR(field, S_IRUGO, \
380 show_spi_transport_##field, \
381 store_spi_transport_##field);
James Bottomley 62a86122005-05-06 18:05:20 -0500382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383/* The Parallel SCSI Tranport Attributes: */
James Bottomley 62a86122005-05-06 18:05:20 -0500384spi_transport_max_attr(offset, "%d\n");
385spi_transport_max_attr(width, "%d\n");
James Bottomleyea443192009-06-13 12:19:05 -0500386spi_transport_max_attr(iu, "%d\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387spi_transport_rd_attr(dt, "%d\n");
James Bottomleyea443192009-06-13 12:19:05 -0500388spi_transport_max_attr(qas, "%d\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389spi_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");
James Bottomleyd872ebe2005-08-03 15:43:52 -0500393spi_transport_rd_attr(hold_mcs, "%d\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
James Bottomleye8bac9e2008-07-29 12:52:20 -0500395/* 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 */
gregkh@suse.de9a881f12005-03-25 15:52:00 -0800397static int child_iter(struct device *dev, void *data)
398{
James Bottomleye8bac9e2008-07-29 12:52:20 -0500399 if (!scsi_is_sdev_device(dev))
400 return 0;
gregkh@suse.de9a881f12005-03-25 15:52:00 -0800401
James Bottomleye8bac9e2008-07-29 12:52:20 -0500402 spi_dv_device(to_scsi_device(dev));
gregkh@suse.de9a881f12005-03-25 15:52:00 -0800403 return 1;
404}
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100407store_spi_revalidate(struct device *dev, struct device_attribute *attr,
408 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Tony Jonesee959b02008-02-22 00:13:36 +0100410 struct scsi_target *starget = transport_class_to_starget(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
gregkh@suse.de9a881f12005-03-25 15:52:00 -0800412 device_for_each_child(&starget->dev, NULL, child_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 return count;
414}
Tony Jonesee959b02008-02-22 00:13:36 +0100415static DEVICE_ATTR(revalidate, S_IWUSR, NULL, store_spi_revalidate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417/* Translate the period into ns according to the current spec
418 * for SDTR/PPR messages */
Matthew Wilcoxef725822005-12-15 16:22:01 -0500419static int period_to_str(char *buf, int period)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 int len, picosec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
James Bottomley 62a86122005-05-06 18:05:20 -0500423 if (period < 0 || period > 0xff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 picosec = -1;
James Bottomley 62a86122005-05-06 18:05:20 -0500425 } else if (period <= SPI_STATIC_PPR) {
426 picosec = ppr_to_ps[period];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 } else {
James Bottomley 62a86122005-05-06 18:05:20 -0500428 picosec = period * 4000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
430
431 if (picosec == -1) {
432 len = sprintf(buf, "reserved");
433 } else {
434 len = sprint_frac(buf, picosec, 1000);
435 }
436
Matthew Wilcoxef725822005-12-15 16:22:01 -0500437 return len;
438}
439
440static ssize_t
Matthew Wilcoxea697e42006-02-07 08:01:02 -0700441show_spi_transport_period_helper(char *buf, int period)
Matthew Wilcoxef725822005-12-15 16:22:01 -0500442{
443 int len = period_to_str(buf, period);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 buf[len++] = '\n';
445 buf[len] = '\0';
446 return len;
447}
448
449static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100450store_spi_transport_period_helper(struct device *dev, const char *buf,
James Bottomley 62a86122005-05-06 18:05:20 -0500451 size_t count, int *periodp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 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
James Bottomley 62a86122005-05-06 18:05:20 -0500481 *periodp = period;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 return count;
484}
485
James Bottomley 62a86122005-05-06 18:05:20 -0500486static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100487show_spi_transport_period(struct device *dev,
488 struct device_attribute *attr, char *buf)
James Bottomley 62a86122005-05-06 18:05:20 -0500489{
Tony Jonesee959b02008-02-22 00:13:36 +0100490 struct scsi_target *starget = transport_class_to_starget(dev);
James Bottomley 62a86122005-05-06 18:05:20 -0500491 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
Matthew Wilcoxea697e42006-02-07 08:01:02 -0700499 return show_spi_transport_period_helper(buf, tp->period);
James Bottomley 62a86122005-05-06 18:05:20 -0500500}
501
502static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100503store_spi_transport_period(struct device *cdev, struct device_attribute *attr,
504 const char *buf, size_t count)
James Bottomley 62a86122005-05-06 18:05:20 -0500505{
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
James Bottomley9b161a42008-01-05 10:18:27 -0600513 if (!i->f->set_period)
514 return -EINVAL;
515
James Bottomley 62a86122005-05-06 18:05:20 -0500516 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
Tony Jonesee959b02008-02-22 00:13:36 +0100526static DEVICE_ATTR(period, S_IRUGO,
527 show_spi_transport_period,
528 store_spi_transport_period);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
James Bottomley 62a86122005-05-06 18:05:20 -0500530static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100531show_spi_transport_min_period(struct device *cdev,
532 struct device_attribute *attr, char *buf)
James Bottomley 62a86122005-05-06 18:05:20 -0500533{
534 struct scsi_target *starget = transport_class_to_starget(cdev);
James Bottomley9b161a42008-01-05 10:18:27 -0600535 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
536 struct spi_internal *i = to_spi_internal(shost->transportt);
James Bottomley 62a86122005-05-06 18:05:20 -0500537 struct spi_transport_attrs *tp =
538 (struct spi_transport_attrs *)&starget->starget_data;
539
James Bottomley9b161a42008-01-05 10:18:27 -0600540 if (!i->f->set_period)
541 return -EINVAL;
542
Matthew Wilcoxea697e42006-02-07 08:01:02 -0700543 return show_spi_transport_period_helper(buf, tp->min_period);
James Bottomley 62a86122005-05-06 18:05:20 -0500544}
545
546static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100547store_spi_transport_min_period(struct device *cdev,
548 struct device_attribute *attr,
549 const char *buf, size_t count)
James Bottomley 62a86122005-05-06 18:05:20 -0500550{
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
Tony Jonesee959b02008-02-22 00:13:36 +0100560static DEVICE_ATTR(min_period, S_IRUGO,
561 show_spi_transport_min_period,
562 store_spi_transport_min_period);
James Bottomley 62a86122005-05-06 18:05:20 -0500563
564
Tony Jonesee959b02008-02-22 00:13:36 +0100565static ssize_t show_spi_host_signalling(struct device *cdev,
566 struct device_attribute *attr,
567 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
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}
Tony Jonesee959b02008-02-22 00:13:36 +0100577static ssize_t store_spi_host_signalling(struct device *dev,
578 struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 const char *buf, size_t count)
580{
Tony Jonesee959b02008-02-22 00:13:36 +0100581 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 struct spi_internal *i = to_spi_internal(shost->transportt);
583 enum spi_signal_type type = spi_signal_to_value(buf);
584
James Bottomley9b161a42008-01-05 10:18:27 -0600585 if (!i->f->set_signalling)
586 return -EINVAL;
587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 if (type != SPI_SIGNAL_UNKNOWN)
589 i->f->set_signalling(shost, type);
590
591 return count;
592}
Tony Jonesee959b02008-02-22 00:13:36 +0100593static DEVICE_ATTR(signalling, S_IRUGO,
594 show_spi_host_signalling,
595 store_spi_host_signalling);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Hannes Reinecke0ac23772011-06-30 02:19:57 +0530597static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619#define DV_SET(x, y) \
620 if(i->f->set_##x) \
621 i->f->set_##x(sdev->sdev_target, y)
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623enum 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
James Bottomley33aa6872005-08-28 11:31:14 -0500633spi_dv_device_echo_buffer(struct scsi_device *sdev, u8 *buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 u8 *ptr, const int retries)
635{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 int len = ptr - buffer;
James Bottomley33aa6872005-08-28 11:31:14 -0500637 int j, k, r, result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 unsigned int pattern = 0x0000ffff;
James Bottomley33aa6872005-08-28 11:31:14 -0500639 struct scsi_sense_hdr sshdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
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++) {
James Bottomley33aa6872005-08-28 11:31:14 -0500684 result = spi_execute(sdev, spi_write_buffer, DMA_TO_DEVICE,
685 buffer, len, &sshdr);
686 if(result || !scsi_device_online(sdev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688 scsi_device_set_state(sdev, SDEV_QUIESCE);
James Bottomley33aa6872005-08-28 11:31:14 -0500689 if (scsi_sense_valid(&sshdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 && 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
James Bottomley9ccfc752005-10-02 11:45:08 -0500701 sdev_printk(KERN_ERR, sdev, "Write Buffer failure %x\n", result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return SPI_COMPARE_FAILURE;
703 }
704
705 memset(ptr, 0, len);
James Bottomley33aa6872005-08-28 11:31:14 -0500706 spi_execute(sdev, spi_read_buffer, DMA_FROM_DEVICE,
707 ptr, len, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 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
James Bottomley33aa6872005-08-28 11:31:14 -0500719spi_dv_device_compare_inquiry(struct scsi_device *sdev, u8 *buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 u8 *ptr, const int retries)
721{
James Bottomley33aa6872005-08-28 11:31:14 -0500722 int r, result;
723 const int len = sdev->inquiry_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 const char spi_inquiry[] = {
725 INQUIRY, 0, 0, 0, len, 0
726 };
727
728 for (r = 0; r < retries; r++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 memset(ptr, 0, len);
730
James Bottomley33aa6872005-08-28 11:31:14 -0500731 result = spi_execute(sdev, spi_inquiry, DMA_FROM_DEVICE,
732 ptr, len, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
James Bottomley33aa6872005-08-28 11:31:14 -0500734 if(result || !scsi_device_online(sdev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 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
James Bottomley33aa6872005-08-28 11:31:14 -0500755spi_dv_retrain(struct scsi_device *sdev, u8 *buffer, u8 *ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 enum spi_compare_returns
James Bottomley33aa6872005-08-28 11:31:14 -0500757 (*compare_fn)(struct scsi_device *, u8 *, u8 *, int))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
James Bottomley33aa6872005-08-28 11:31:14 -0500759 struct spi_internal *i = to_spi_internal(sdev->host->transportt);
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500760 struct scsi_target *starget = sdev->sdev_target;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 int period = 0, prevperiod = 0;
762 enum spi_compare_returns retval;
763
764
765 for (;;) {
766 int newperiod;
James Bottomley33aa6872005-08-28 11:31:14 -0500767 retval = compare_fn(sdev, buffer, ptr, DV_LOOPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769 if (retval == SPI_COMPARE_SUCCESS
770 || retval == SPI_COMPARE_SKIP_TEST)
771 break;
772
773 /* OK, retrain, fallback */
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500774 if (i->f->get_iu)
775 i->f->get_iu(starget);
776 if (i->f->get_qas)
777 i->f->get_qas(starget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 if (i->f->get_period)
779 i->f->get_period(sdev->sdev_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500781 /* 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)) {
Masanari Iida804ff602015-05-07 23:21:27 +0900785 starget_printk(KERN_ERR, starget, "Domain Validation Disabling Information Units\n");
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500786 DV_SET(iu, 0);
787 } else if (i->f->set_qas && spi_qas(starget)) {
Masanari Iida804ff602015-05-07 23:21:27 +0900788 starget_printk(KERN_ERR, starget, "Domain Validation Disabling Quick Arbitration and Selection\n");
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500789 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 */
James Bottomley9ccfc752005-10-02 11:45:08 -0500800 starget_printk(KERN_ERR, starget, "Domain Validation Failure, dropping back to Asynchronous\n");
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500801 DV_SET(offset, 0);
802 return SPI_COMPARE_FAILURE;
803 }
James Bottomley9ccfc752005-10-02 11:45:08 -0500804 starget_printk(KERN_ERR, starget, "Domain Validation detected failure, dropping back\n");
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500805 DV_SET(period, period);
806 prevperiod = period;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
809 return retval;
810}
811
812static int
James Bottomley33aa6872005-08-28 11:31:14 -0500813spi_dv_device_get_echo_buffer(struct scsi_device *sdev, u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
James Bottomley33aa6872005-08-28 11:31:14 -0500815 int l, result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 /* 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++) {
James Bottomley33aa6872005-08-28 11:31:14 -0500837 result = spi_execute(sdev, spi_test_unit_ready, DMA_NONE,
838 NULL, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
James Bottomley33aa6872005-08-28 11:31:14 -0500840 if(result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 if(l >= 3)
842 return 0;
843 } else {
844 /* TUR succeeded */
845 break;
846 }
847 }
848
James Bottomley33aa6872005-08-28 11:31:14 -0500849 result = spi_execute(sdev, spi_read_buffer_descriptor,
850 DMA_FROM_DEVICE, buffer, 4, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
James Bottomley33aa6872005-08-28 11:31:14 -0500852 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 /* Device has no echo buffer */
854 return 0;
855
856 return buffer[3] + ((buffer[2] & 0x1f) << 8);
857}
858
859static void
James Bottomley33aa6872005-08-28 11:31:14 -0500860spi_dv_device_internal(struct scsi_device *sdev, u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
James Bottomley33aa6872005-08-28 11:31:14 -0500862 struct spi_internal *i = to_spi_internal(sdev->host->transportt);
James Bottomley 62a86122005-05-06 18:05:20 -0500863 struct scsi_target *starget = sdev->sdev_target;
James Bottomley60eef252006-06-10 10:51:23 -0500864 struct Scsi_Host *shost = sdev->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 int len = sdev->inquiry_len;
James Bottomley23028272007-09-22 08:40:09 -0500866 int min_period = spi_min_period(starget);
867 int max_width = spi_max_width(starget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 /* first set us up for narrow async */
869 DV_SET(offset, 0);
870 DV_SET(width, 0);
James Bottomley23028272007-09-22 08:40:09 -0500871
James Bottomley33aa6872005-08-28 11:31:14 -0500872 if (spi_dv_device_compare_inquiry(sdev, buffer, buffer, DV_LOOPS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 != SPI_COMPARE_SUCCESS) {
James Bottomley9ccfc752005-10-02 11:45:08 -0500874 starget_printk(KERN_ERR, starget, "Domain Validation Initial Inquiry Failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 /* FIXME: should probably offline the device here? */
876 return;
877 }
878
James Bottomley9872b812009-06-17 15:03:41 -0400879 if (!spi_support_wide(starget)) {
James Bottomley23028272007-09-22 08:40:09 -0500880 spi_max_width(starget) = 0;
881 max_width = 0;
882 }
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 /* test width */
James Bottomley23028272007-09-22 08:40:09 -0500885 if (i->f->set_width && max_width) {
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500886 i->f->set_width(starget, 1);
James Bottomley 62a86122005-05-06 18:05:20 -0500887
James Bottomley33aa6872005-08-28 11:31:14 -0500888 if (spi_dv_device_compare_inquiry(sdev, buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 buffer + len,
890 DV_LOOPS)
891 != SPI_COMPARE_SUCCESS) {
James Bottomley9ccfc752005-10-02 11:45:08 -0500892 starget_printk(KERN_ERR, starget, "Wide Transfers Fail\n");
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500893 i->f->set_width(starget, 0);
James Bottomley23028272007-09-22 08:40:09 -0500894 /* 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 }
900 }
901
902 if (!i->f->set_period)
903 return;
904
905 /* device can't handle synchronous */
James Bottomley9872b812009-06-17 15:03:41 -0400906 if (!spi_support_sync(starget) && !spi_support_dt(starget))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 return;
908
James Bottomley349cd7c2005-11-28 15:41:58 -0600909 /* 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 retry:
915
916 /* now set up to the maximum */
James Bottomley 62a86122005-05-06 18:05:20 -0500917 DV_SET(offset, spi_max_offset(starget));
James Bottomley23028272007-09-22 08:40:09 -0500918 DV_SET(period, min_period);
919
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500920 /* try QAS requests; this should be harmless to set if the
921 * target supports it */
James Bottomley9872b812009-06-17 15:03:41 -0400922 if (spi_support_qas(starget) && spi_max_qas(starget)) {
James Bottomleyeb1dd682005-07-02 12:22:01 -0400923 DV_SET(qas, 1);
James Bottomleydfdc58b2006-09-20 12:00:18 -0400924 } else {
925 DV_SET(qas, 0);
926 }
927
James Bottomley9872b812009-06-17 15:03:41 -0400928 if (spi_support_ius(starget) && spi_max_iu(starget) &&
929 min_period < 9) {
James Bottomleydfdc58b2006-09-20 12:00:18 -0400930 /* This u320 (or u640). Set IU transfers */
James Bottomleyeb1dd682005-07-02 12:22:01 -0400931 DV_SET(iu, 1);
James Bottomleydfdc58b2006-09-20 12:00:18 -0400932 /* Then set the optional parameters */
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500933 DV_SET(rd_strm, 1);
934 DV_SET(wr_flow, 1);
935 DV_SET(rti, 1);
James Bottomley23028272007-09-22 08:40:09 -0500936 if (min_period == 8)
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500937 DV_SET(pcomp_en, 1);
James Bottomleydfdc58b2006-09-20 12:00:18 -0400938 } else {
939 DV_SET(iu, 0);
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500940 }
James Bottomleydfdc58b2006-09-20 12:00:18 -0400941
James Bottomley60eef252006-06-10 10:51:23 -0500942 /* 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 ||
James Bottomleydfdc58b2006-09-20 12:00:18 -0400948 spi_signalling(shost) == SPI_SIGNAL_HVD ||
James Bottomley9872b812009-06-17 15:03:41 -0400949 !spi_support_dt(starget)) {
James Bottomley60eef252006-06-10 10:51:23 -0500950 DV_SET(dt, 0);
James Bottomleydfdc58b2006-09-20 12:00:18 -0400951 } else {
952 DV_SET(dt, 1);
953 }
James Bottomley23028272007-09-22 08:40:09 -0500954 /* set width last because it will pull all the other
955 * parameters down to required values */
956 DV_SET(width, max_width);
957
James Bottomley349cd7c2005-11-28 15:41:58 -0600958 /* 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
James Bottomley349cd7c2005-11-28 15:41:58 -0600965 /* 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);
972
973 if (len <= 0) {
James Bottomley9ccfc752005-10-02 11:45:08 -0500974 starget_printk(KERN_INFO, starget, "Domain Validation skipping write tests\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 return;
976 }
977
978 if (len > SPI_MAX_ECHO_BUFFER_SIZE) {
James Bottomley9ccfc752005-10-02 11:45:08 -0500979 starget_printk(KERN_WARNING, starget, "Echo buffer size %d is too big, trimming to %d\n", len, SPI_MAX_ECHO_BUFFER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 len = SPI_MAX_ECHO_BUFFER_SIZE;
981 }
982
James Bottomley33aa6872005-08-28 11:31:14 -0500983 if (spi_dv_retrain(sdev, buffer, buffer + len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 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{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 struct scsi_target *starget = sdev->sdev_target;
1007 u8 *buffer;
1008 const int len = SPI_MAX_ECHO_BUFFER_SIZE*2;
1009
Mike Maslenkin89a342c2012-04-28 05:32:14 +04001010 if (unlikely(spi_dv_in_progress(starget)))
James Bottomley33aa6872005-08-28 11:31:14 -05001011 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Mike Maslenkin89a342c2012-04-28 05:32:14 +04001013 if (unlikely(scsi_device_get(sdev)))
James Bottomleydfdc58b2006-09-20 12:00:18 -04001014 return;
1015 spi_dv_in_progress(starget) = 1;
1016
Jes Sorensen24669f752006-01-16 10:31:18 -05001017 buffer = kzalloc(len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 if (unlikely(!buffer))
1020 goto out_put;
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 /* 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;
Jes Sorensend158d262006-01-13 16:05:44 -08001030 mutex_lock(&spi_dv_mutex(starget));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
James Bottomley9ccfc752005-10-02 11:45:08 -05001032 starget_printk(KERN_INFO, starget, "Beginning Domain Validation\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
James Bottomley33aa6872005-08-28 11:31:14 -05001034 spi_dv_device_internal(sdev, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
James Bottomley9ccfc752005-10-02 11:45:08 -05001036 starget_printk(KERN_INFO, starget, "Ending Domain Validation\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Jes Sorensend158d262006-01-13 16:05:44 -08001038 mutex_unlock(&spi_dv_mutex(starget));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 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:
James Bottomleydfdc58b2006-09-20 12:00:18 -04001048 spi_dv_in_progress(starget) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 scsi_device_put(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050}
1051EXPORT_SYMBOL(spi_dv_device);
1052
1053struct work_queue_wrapper {
1054 struct work_struct work;
1055 struct scsi_device *sdev;
1056};
1057
1058static void
David Howellsc4028952006-11-22 14:57:56 +00001059spi_dv_device_work_wrapper(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
David Howellsc4028952006-11-22 14:57:56 +00001061 struct work_queue_wrapper *wqw =
1062 container_of(work, struct work_queue_wrapper, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 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
David Howellsc4028952006-11-22 14:57:56 +00001102 INIT_WORK(&wqw->work, spi_dv_device_work_wrapper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 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,
James Bottomleyd872ebe2005-08-03 15:43:52 -05001153 "%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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 } else {
Matthew Wilcox493ff4e2005-11-17 11:13:43 -07001165 dev_info(&starget->dev, "%sasynchronous\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 tp->width ? "wide " : "");
1167 }
1168}
1169EXPORT_SYMBOL(spi_display_xfer_agreement);
1170
Matthew Wilcox6ea3c0b2006-02-07 07:54:46 -07001171int 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}
James Bottomley38e14f82006-02-17 14:58:47 -08001179EXPORT_SYMBOL_GPL(spi_populate_width_msg);
Matthew Wilcox6ea3c0b2006-02-07 07:54:46 -07001180
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}
James Bottomley38e14f82006-02-17 14:58:47 -08001190EXPORT_SYMBOL_GPL(spi_populate_sync_msg);
Matthew Wilcox6ea3c0b2006-02-07 07:54:46 -07001191
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}
James Bottomley38e14f82006-02-17 14:58:47 -08001205EXPORT_SYMBOL_GPL(spi_populate_ppr_msg);
Matthew Wilcox6ea3c0b2006-02-07 07:54:46 -07001206
Christoph Hellwig50668632014-10-30 14:30:06 +01001207/**
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) {
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001220 *msg++ = SIMPLE_QUEUE_TAG;
Christoph Hellwig50668632014-10-30 14:30:06 +01001221 *msg++ = cmd->request->tag;
1222 return 2;
1223 }
1224
1225 return 0;
1226}
1227EXPORT_SYMBOL_GPL(spi_populate_tag_msg);
1228
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001229#ifdef CONFIG_SCSI_CONSTANTS
1230static const char * const one_byte_msgs[] = {
Matthew Wilcox72df0eb2006-03-10 07:18:22 -07001231/* 0x00 */ "Task Complete", NULL /* Extended Message */, "Save Pointers",
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001232/* 0x03 */ "Restore Pointers", "Disconnect", "Initiator Error",
Matthew Wilcox72df0eb2006-03-10 07:18:22 -07001233/* 0x06 */ "Abort Task Set", "Message Reject", "Nop", "Message Parity Error",
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001234/* 0x0a */ "Linked Command Complete", "Linked Command Complete w/flag",
Matthew Wilcox72df0eb2006-03-10 07:18:22 -07001235/* 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"
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001239};
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001240
1241static const char * const two_byte_msgs[] = {
Matthew Wilcox47972152005-12-15 16:22:01 -05001242/* 0x20 */ "Simple Queue Tag", "Head of Queue Tag", "Ordered Queue Tag",
Matthew Wilcox72df0eb2006-03-10 07:18:22 -07001243/* 0x23 */ "Ignore Wide Residue", "ACA"
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001244};
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001245
1246static const char * const extended_msgs[] = {
1247/* 0x00 */ "Modify Data Pointer", "Synchronous Data Transfer Request",
Matthew Wilcoxef725822005-12-15 16:22:01 -05001248/* 0x02 */ "SCSI-I Extended Identify", "Wide Data Transfer Request",
Matthew Wilcoxfc253072006-02-18 20:52:31 -07001249/* 0x04 */ "Parallel Protocol Request", "Modify Bidirectional Data Pointer"
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001250};
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001251
Adrian Bunkbdd70f22006-01-05 23:39:18 +01001252static void print_nego(const unsigned char *msg, int per, int off, int width)
Matthew Wilcoxef725822005-12-15 16:22:01 -05001253{
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}
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001265
Matthew Wilcoxfc253072006-02-18 20:52:31 -07001266static 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
Matthew Wilcox1abfd372005-12-15 16:22:01 -05001273int spi_print_msg(const unsigned char *msg)
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001274{
Matthew Wilcox72df0eb2006-03-10 07:18:22 -07001275 int len = 1, i;
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001276 if (msg[0] == EXTENDED_MESSAGE) {
Matthew Wilcoxfc253072006-02-18 20:52:31 -07001277 len = 2 + msg[1];
1278 if (len == 2)
1279 len += 256;
Matthew Wilcoxb32aaff2005-12-15 16:22:01 -05001280 if (msg[2] < ARRAY_SIZE(extended_msgs))
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001281 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:
Matthew Wilcoxfc253072006-02-18 20:52:31 -07001287 print_ptr(msg, 3, "pointer");
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001288 break;
1289 case EXTENDED_SDTR:
Matthew Wilcoxef725822005-12-15 16:22:01 -05001290 print_nego(msg, 3, 4, 0);
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001291 break;
1292 case EXTENDED_WDTR:
Matthew Wilcoxef725822005-12-15 16:22:01 -05001293 print_nego(msg, 0, 0, 3);
1294 break;
1295 case EXTENDED_PPR:
1296 print_nego(msg, 3, 5, 6);
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001297 break;
Matthew Wilcoxfc253072006-02-18 20:52:31 -07001298 case EXTENDED_MODIFY_BIDI_DATA_PTR:
1299 print_ptr(msg, 3, "out");
1300 print_ptr(msg, 7, "in");
1301 break;
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001302 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);
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001312 /* Normal One byte */
1313 } else if (msg[0] < 0x1f) {
Matthew Wilcox72df0eb2006-03-10 07:18:22 -07001314 if (msg[0] < ARRAY_SIZE(one_byte_msgs) && one_byte_msgs[msg[0]])
Matthew Wilcoxe24d8732006-02-07 08:05:26 -07001315 printk("%s ", one_byte_msgs[msg[0]]);
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001316 else
1317 printk("reserved (%02x) ", msg[0]);
Matthew Wilcox72df0eb2006-03-10 07:18:22 -07001318 } else if (msg[0] == 0x55) {
1319 printk("QAS Request ");
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001320 /* Two byte */
1321 } else if (msg[0] <= 0x2f) {
Matthew Wilcoxb32aaff2005-12-15 16:22:01 -05001322 if ((msg[0] - 0x20) < ARRAY_SIZE(two_byte_msgs))
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001323 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
Matthew Wilcoxe24d8732006-02-07 08:05:26 -07001330 printk("reserved ");
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001331 return len;
1332}
Matthew Wilcox1abfd372005-12-15 16:22:01 -05001333EXPORT_SYMBOL(spi_print_msg);
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001334
1335#else /* ifndef CONFIG_SCSI_CONSTANTS */
1336
Matthew Wilcox1abfd372005-12-15 16:22:01 -05001337int spi_print_msg(const unsigned char *msg)
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001338{
Matthew Wilcox72df0eb2006-03-10 07:18:22 -07001339 int len = 1, i;
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001340
1341 if (msg[0] == EXTENDED_MESSAGE) {
Matthew Wilcoxfc253072006-02-18 20:52:31 -07001342 len = 2 + msg[1];
1343 if (len == 2)
1344 len += 256;
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001345 for (i = 0; i < len; ++i)
1346 printk("%02x ", msg[i]);
1347 /* Identify */
1348 } else if (msg[0] & 0x80) {
1349 printk("%02x ", msg[0]);
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001350 /* Normal One byte */
James Bottomley597705a2006-03-12 09:54:19 -06001351 } else if ((msg[0] < 0x1f) || (msg[0] == 0x55)) {
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001352 printk("%02x ", msg[0]);
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001353 /* 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}
Matthew Wilcox1abfd372005-12-15 16:22:01 -05001361EXPORT_SYMBOL(spi_print_msg);
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001362#endif /* ! CONFIG_SCSI_CONSTANTS */
1363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364static int spi_device_match(struct attribute_container *cont,
1365 struct device *dev)
1366{
1367 struct scsi_device *sdev;
1368 struct Scsi_Host *shost;
James Bottomley10c1b882005-08-14 14:34:06 -05001369 struct spi_internal *i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
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 */
James Bottomley10c1b882005-08-14 14:34:06 -05001382 i = to_spi_internal(shost->transportt);
1383 if (i->f->deny_binding && i->f->deny_binding(sdev->sdev_target))
1384 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 return 1;
1386}
1387
1388static int spi_target_match(struct attribute_container *cont,
1389 struct device *dev)
1390{
1391 struct Scsi_Host *shost;
James Bottomley10c1b882005-08-14 14:34:06 -05001392 struct scsi_target *starget;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 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);
James Bottomley10c1b882005-08-14 14:34:06 -05001404 starget = to_scsi_target(dev);
1405
1406 if (i->f->deny_binding && i->f->deny_binding(starget))
1407 return 0;
1408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 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,
James Bottomley9b161a42008-01-05 10:18:27 -06001416 spi_target_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
1418static DECLARE_ANON_TRANSPORT_CLASS(spi_device_class,
1419 spi_device_match,
1420 spi_device_configure);
1421
James Bottomley9b161a42008-01-05 10:18:27 -06001422static struct attribute *host_attributes[] = {
Tony Jonesee959b02008-02-22 00:13:36 +01001423 &dev_attr_signalling.attr,
Hannes Reinecke0ac23772011-06-30 02:19:57 +05301424 &dev_attr_host_width.attr,
1425 &dev_attr_hba_id.attr,
James Bottomley9b161a42008-01-05 10:18:27 -06001426 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,
Tony Jonesee959b02008-02-22 00:13:36 +01001435 struct device *cdev)
James Bottomley9b161a42008-01-05 10:18:27 -06001436{
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);
Tony Jonesee959b02008-02-22 00:13:36 +01001440 struct attribute *attr = &dev_attr_signalling.attr;
James Bottomley9b161a42008-01-05 10:18:27 -06001441 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) \
James Bottomley352f6bb2008-03-20 20:57:02 -05001453 (si->f->show_##name ? S_IRUGO : 0) | \
1454 (si->f->set_##name ? S_IWUSR : 0)
James Bottomley9b161a42008-01-05 10:18:27 -06001455
Al Viro587a1f12011-07-23 23:11:19 -04001456static umode_t target_attribute_is_visible(struct kobject *kobj,
James Bottomley352f6bb2008-03-20 20:57:02 -05001457 struct attribute *attr, int i)
James Bottomley9b161a42008-01-05 10:18:27 -06001458{
Tony Jonesee959b02008-02-22 00:13:36 +01001459 struct device *cdev = container_of(kobj, struct device, kobj);
James Bottomley9b161a42008-01-05 10:18:27 -06001460 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
Tony Jonesee959b02008-02-22 00:13:36 +01001464 if (attr == &dev_attr_period.attr &&
James Bottomley9b161a42008-01-05 10:18:27 -06001465 spi_support_sync(starget))
1466 return TARGET_ATTRIBUTE_HELPER(period);
Tony Jonesee959b02008-02-22 00:13:36 +01001467 else if (attr == &dev_attr_min_period.attr &&
James Bottomley9b161a42008-01-05 10:18:27 -06001468 spi_support_sync(starget))
1469 return TARGET_ATTRIBUTE_HELPER(period);
Tony Jonesee959b02008-02-22 00:13:36 +01001470 else if (attr == &dev_attr_offset.attr &&
James Bottomley9b161a42008-01-05 10:18:27 -06001471 spi_support_sync(starget))
1472 return TARGET_ATTRIBUTE_HELPER(offset);
Tony Jonesee959b02008-02-22 00:13:36 +01001473 else if (attr == &dev_attr_max_offset.attr &&
James Bottomley9b161a42008-01-05 10:18:27 -06001474 spi_support_sync(starget))
1475 return TARGET_ATTRIBUTE_HELPER(offset);
Tony Jonesee959b02008-02-22 00:13:36 +01001476 else if (attr == &dev_attr_width.attr &&
James Bottomley9b161a42008-01-05 10:18:27 -06001477 spi_support_wide(starget))
1478 return TARGET_ATTRIBUTE_HELPER(width);
Tony Jonesee959b02008-02-22 00:13:36 +01001479 else if (attr == &dev_attr_max_width.attr &&
James Bottomley9b161a42008-01-05 10:18:27 -06001480 spi_support_wide(starget))
1481 return TARGET_ATTRIBUTE_HELPER(width);
Tony Jonesee959b02008-02-22 00:13:36 +01001482 else if (attr == &dev_attr_iu.attr &&
James Bottomley9b161a42008-01-05 10:18:27 -06001483 spi_support_ius(starget))
1484 return TARGET_ATTRIBUTE_HELPER(iu);
James Bottomleyea443192009-06-13 12:19:05 -05001485 else if (attr == &dev_attr_max_iu.attr &&
1486 spi_support_ius(starget))
1487 return TARGET_ATTRIBUTE_HELPER(iu);
Tony Jonesee959b02008-02-22 00:13:36 +01001488 else if (attr == &dev_attr_dt.attr &&
James Bottomley9b161a42008-01-05 10:18:27 -06001489 spi_support_dt(starget))
1490 return TARGET_ATTRIBUTE_HELPER(dt);
Tony Jonesee959b02008-02-22 00:13:36 +01001491 else if (attr == &dev_attr_qas.attr &&
James Bottomley9b161a42008-01-05 10:18:27 -06001492 spi_support_qas(starget))
1493 return TARGET_ATTRIBUTE_HELPER(qas);
James Bottomleyea443192009-06-13 12:19:05 -05001494 else if (attr == &dev_attr_max_qas.attr &&
1495 spi_support_qas(starget))
1496 return TARGET_ATTRIBUTE_HELPER(qas);
Tony Jonesee959b02008-02-22 00:13:36 +01001497 else if (attr == &dev_attr_wr_flow.attr &&
James Bottomley9b161a42008-01-05 10:18:27 -06001498 spi_support_ius(starget))
1499 return TARGET_ATTRIBUTE_HELPER(wr_flow);
Tony Jonesee959b02008-02-22 00:13:36 +01001500 else if (attr == &dev_attr_rd_strm.attr &&
James Bottomley9b161a42008-01-05 10:18:27 -06001501 spi_support_ius(starget))
1502 return TARGET_ATTRIBUTE_HELPER(rd_strm);
Tony Jonesee959b02008-02-22 00:13:36 +01001503 else if (attr == &dev_attr_rti.attr &&
James Bottomley9b161a42008-01-05 10:18:27 -06001504 spi_support_ius(starget))
1505 return TARGET_ATTRIBUTE_HELPER(rti);
Tony Jonesee959b02008-02-22 00:13:36 +01001506 else if (attr == &dev_attr_pcomp_en.attr &&
James Bottomley9b161a42008-01-05 10:18:27 -06001507 spi_support_ius(starget))
1508 return TARGET_ATTRIBUTE_HELPER(pcomp_en);
Tony Jonesee959b02008-02-22 00:13:36 +01001509 else if (attr == &dev_attr_hold_mcs.attr &&
James Bottomley9b161a42008-01-05 10:18:27 -06001510 spi_support_ius(starget))
1511 return TARGET_ATTRIBUTE_HELPER(hold_mcs);
Tony Jonesee959b02008-02-22 00:13:36 +01001512 else if (attr == &dev_attr_revalidate.attr)
James Bottomley352f6bb2008-03-20 20:57:02 -05001513 return S_IWUSR;
James Bottomley9b161a42008-01-05 10:18:27 -06001514
1515 return 0;
1516}
1517
1518static struct attribute *target_attributes[] = {
Tony Jonesee959b02008-02-22 00:13:36 +01001519 &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,
James Bottomleyea443192009-06-13 12:19:05 -05001526 &dev_attr_max_iu.attr,
Tony Jonesee959b02008-02-22 00:13:36 +01001527 &dev_attr_dt.attr,
1528 &dev_attr_qas.attr,
James Bottomleyea443192009-06-13 12:19:05 -05001529 &dev_attr_max_qas.attr,
Tony Jonesee959b02008-02-22 00:13:36 +01001530 &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,
James Bottomley9b161a42008-01-05 10:18:27 -06001536 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,
Tony Jonesee959b02008-02-22 00:13:36 +01001546 struct device *cdev)
James Bottomley9b161a42008-01-05 10:18:27 -06001547{
1548 struct kobject *kobj = &cdev->kobj;
James Bottomley9b161a42008-01-05 10:18:27 -06001549
James Bottomley352f6bb2008-03-20 20:57:02 -05001550 /* force an update based on parameters read from the device */
1551 sysfs_update_group(kobj, &target_attribute_group);
James Bottomley9b161a42008-01-05 10:18:27 -06001552
1553 return 0;
1554}
1555
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556struct scsi_transport_template *
1557spi_attach_transport(struct spi_function_template *ft)
1558{
Jes Sorensen24669f752006-01-16 10:31:18 -05001559 struct spi_internal *i = kzalloc(sizeof(struct spi_internal),
1560 GFP_KERNEL);
1561
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 if (unlikely(!i))
1563 return NULL;
1564
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 i->t.target_attrs.ac.class = &spi_transport_class.class;
James Bottomley9b161a42008-01-05 10:18:27 -06001566 i->t.target_attrs.ac.grp = &target_attribute_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 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;
James Bottomley9b161a42008-01-05 10:18:27 -06001571 i->t.host_attrs.ac.grp = &host_attribute_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 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{
James Bottomleya9e0edb2009-06-17 19:05:05 +00001594 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 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);
James Bottomleya9e0edb2009-06-17 19:05:05 +00001620 scsi_dev_info_remove_list(SCSI_DEVINFO_SPI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621}
1622
1623MODULE_AUTHOR("Martin Hicks");
1624MODULE_DESCRIPTION("SPI Transport Attributes");
1625MODULE_LICENSE("GPL");
1626
1627module_init(spi_transport_init);
1628module_exit(spi_transport_exit);