blob: 10ebb213ddb33e2920e2fe83e60cc712a50c3002 [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 {
Bart Van Assche093b8882017-12-12 10:23:28 -080053 SPI_BLIST_NOIUS = (__force blist_flags_t)0x1,
James Bottomleya9e0edb2009-06-17 19:05:05 +000054};
55
56/* blacklist table, modelled on scsi_devinfo.c */
57static struct {
58 char *vendor;
59 char *model;
Bart Van Assche093b8882017-12-12 10:23:28 -080060 blist_flags_t flags;
James Bottomleya9e0edb2009-06-17 19:05:05 +000061} 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;
Bart Van Assche093b8882017-12-12 10:23:28 -0800224 blist_flags_t bflags;
225
226 bflags = scsi_get_device_flags_keyed(sdev, &sdev->inquiry[8],
227 &sdev->inquiry[16],
228 SCSI_DEVINFO_SPI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 /* Populate the target capability fields with the values
231 * gleaned from the device inquiry */
232
233 spi_support_sync(starget) = scsi_device_sync(sdev);
234 spi_support_wide(starget) = scsi_device_wide(sdev);
235 spi_support_dt(starget) = scsi_device_dt(sdev);
236 spi_support_dt_only(starget) = scsi_device_dt_only(sdev);
237 spi_support_ius(starget) = scsi_device_ius(sdev);
James Bottomleya9e0edb2009-06-17 19:05:05 +0000238 if (bflags & SPI_BLIST_NOIUS) {
239 dev_info(dev, "Information Units disabled by blacklist\n");
240 spi_support_ius(starget) = 0;
241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 spi_support_qas(starget) = scsi_device_qas(sdev);
243
244 return 0;
245}
246
James Bottomleyd0a7e572005-08-14 17:09:01 -0500247static int spi_setup_transport_attrs(struct transport_container *tc,
248 struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100249 struct device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
251 struct scsi_target *starget = to_scsi_target(dev);
252
253 spi_period(starget) = -1; /* illegal value */
James Bottomley 62a86122005-05-06 18:05:20 -0500254 spi_min_period(starget) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 spi_offset(starget) = 0; /* async */
James Bottomley 62a86122005-05-06 18:05:20 -0500256 spi_max_offset(starget) = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 spi_width(starget) = 0; /* narrow */
James Bottomley 62a86122005-05-06 18:05:20 -0500258 spi_max_width(starget) = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 spi_iu(starget) = 0; /* no IU */
James Bottomleyea443192009-06-13 12:19:05 -0500260 spi_max_iu(starget) = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 spi_dt(starget) = 0; /* ST */
262 spi_qas(starget) = 0;
James Bottomleyea443192009-06-13 12:19:05 -0500263 spi_max_qas(starget) = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 spi_wr_flow(starget) = 0;
265 spi_rd_strm(starget) = 0;
266 spi_rti(starget) = 0;
267 spi_pcomp_en(starget) = 0;
James Bottomleyd872ebe2005-08-03 15:43:52 -0500268 spi_hold_mcs(starget) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 spi_dv_pending(starget) = 0;
James Bottomleydfdc58b2006-09-20 12:00:18 -0400270 spi_dv_in_progress(starget) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 spi_initial_dv(starget) = 0;
Jes Sorensend158d262006-01-13 16:05:44 -0800272 mutex_init(&spi_dv_mutex(starget));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 return 0;
275}
276
James Bottomley 62a86122005-05-06 18:05:20 -0500277#define spi_transport_show_simple(field, format_string) \
278 \
279static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100280show_spi_transport_##field(struct device *dev, \
281 struct device_attribute *attr, char *buf) \
James Bottomley 62a86122005-05-06 18:05:20 -0500282{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100283 struct scsi_target *starget = transport_class_to_starget(dev); \
James Bottomley 62a86122005-05-06 18:05:20 -0500284 struct spi_transport_attrs *tp; \
285 \
286 tp = (struct spi_transport_attrs *)&starget->starget_data; \
287 return snprintf(buf, 20, format_string, tp->field); \
288}
289
290#define spi_transport_store_simple(field, format_string) \
291 \
292static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100293store_spi_transport_##field(struct device *dev, \
294 struct device_attribute *attr, \
295 const char *buf, size_t count) \
James Bottomley 62a86122005-05-06 18:05:20 -0500296{ \
297 int val; \
Tony Jonesee959b02008-02-22 00:13:36 +0100298 struct scsi_target *starget = transport_class_to_starget(dev); \
James Bottomley 62a86122005-05-06 18:05:20 -0500299 struct spi_transport_attrs *tp; \
300 \
301 tp = (struct spi_transport_attrs *)&starget->starget_data; \
302 val = simple_strtoul(buf, NULL, 0); \
303 tp->field = val; \
304 return count; \
305}
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307#define spi_transport_show_function(field, format_string) \
308 \
309static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100310show_spi_transport_##field(struct device *dev, \
311 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100313 struct scsi_target *starget = transport_class_to_starget(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
315 struct spi_transport_attrs *tp; \
316 struct spi_internal *i = to_spi_internal(shost->transportt); \
317 tp = (struct spi_transport_attrs *)&starget->starget_data; \
318 if (i->f->get_##field) \
319 i->f->get_##field(starget); \
320 return snprintf(buf, 20, format_string, tp->field); \
321}
322
323#define spi_transport_store_function(field, format_string) \
324static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100325store_spi_transport_##field(struct device *dev, \
326 struct device_attribute *attr, \
327 const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{ \
329 int val; \
Tony Jonesee959b02008-02-22 00:13:36 +0100330 struct scsi_target *starget = transport_class_to_starget(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
332 struct spi_internal *i = to_spi_internal(shost->transportt); \
333 \
James Bottomley9b161a42008-01-05 10:18:27 -0600334 if (!i->f->set_##field) \
335 return -EINVAL; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 val = simple_strtoul(buf, NULL, 0); \
James Bottomley9b161a42008-01-05 10:18:27 -0600337 i->f->set_##field(starget, val); \
James Bottomley 62a86122005-05-06 18:05:20 -0500338 return count; \
339}
340
341#define spi_transport_store_max(field, format_string) \
342static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100343store_spi_transport_##field(struct device *dev, \
344 struct device_attribute *attr, \
345 const char *buf, size_t count) \
James Bottomley 62a86122005-05-06 18:05:20 -0500346{ \
347 int val; \
Tony Jonesee959b02008-02-22 00:13:36 +0100348 struct scsi_target *starget = transport_class_to_starget(dev); \
James Bottomley 62a86122005-05-06 18:05:20 -0500349 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
350 struct spi_internal *i = to_spi_internal(shost->transportt); \
351 struct spi_transport_attrs *tp \
352 = (struct spi_transport_attrs *)&starget->starget_data; \
353 \
James Bottomley9b161a42008-01-05 10:18:27 -0600354 if (i->f->set_##field) \
355 return -EINVAL; \
James Bottomley 62a86122005-05-06 18:05:20 -0500356 val = simple_strtoul(buf, NULL, 0); \
357 if (val > tp->max_##field) \
358 val = tp->max_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 i->f->set_##field(starget, val); \
360 return count; \
361}
362
363#define spi_transport_rd_attr(field, format_string) \
364 spi_transport_show_function(field, format_string) \
365 spi_transport_store_function(field, format_string) \
Tony Jonesee959b02008-02-22 00:13:36 +0100366static DEVICE_ATTR(field, S_IRUGO, \
367 show_spi_transport_##field, \
368 store_spi_transport_##field);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
James Bottomley 62a86122005-05-06 18:05:20 -0500370#define spi_transport_simple_attr(field, format_string) \
371 spi_transport_show_simple(field, format_string) \
372 spi_transport_store_simple(field, format_string) \
Tony Jonesee959b02008-02-22 00:13:36 +0100373static DEVICE_ATTR(field, S_IRUGO, \
374 show_spi_transport_##field, \
375 store_spi_transport_##field);
James Bottomley 62a86122005-05-06 18:05:20 -0500376
377#define spi_transport_max_attr(field, format_string) \
378 spi_transport_show_function(field, format_string) \
379 spi_transport_store_max(field, format_string) \
380 spi_transport_simple_attr(max_##field, format_string) \
Tony Jonesee959b02008-02-22 00:13:36 +0100381static DEVICE_ATTR(field, S_IRUGO, \
382 show_spi_transport_##field, \
383 store_spi_transport_##field);
James Bottomley 62a86122005-05-06 18:05:20 -0500384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385/* The Parallel SCSI Tranport Attributes: */
James Bottomley 62a86122005-05-06 18:05:20 -0500386spi_transport_max_attr(offset, "%d\n");
387spi_transport_max_attr(width, "%d\n");
James Bottomleyea443192009-06-13 12:19:05 -0500388spi_transport_max_attr(iu, "%d\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389spi_transport_rd_attr(dt, "%d\n");
James Bottomleyea443192009-06-13 12:19:05 -0500390spi_transport_max_attr(qas, "%d\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391spi_transport_rd_attr(wr_flow, "%d\n");
392spi_transport_rd_attr(rd_strm, "%d\n");
393spi_transport_rd_attr(rti, "%d\n");
394spi_transport_rd_attr(pcomp_en, "%d\n");
James Bottomleyd872ebe2005-08-03 15:43:52 -0500395spi_transport_rd_attr(hold_mcs, "%d\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
James Bottomleye8bac9e2008-07-29 12:52:20 -0500397/* we only care about the first child device that's a real SCSI device
398 * so we return 1 to terminate the iteration when we find it */
gregkh@suse.de9a881f12005-03-25 15:52:00 -0800399static int child_iter(struct device *dev, void *data)
400{
James Bottomleye8bac9e2008-07-29 12:52:20 -0500401 if (!scsi_is_sdev_device(dev))
402 return 0;
gregkh@suse.de9a881f12005-03-25 15:52:00 -0800403
James Bottomleye8bac9e2008-07-29 12:52:20 -0500404 spi_dv_device(to_scsi_device(dev));
gregkh@suse.de9a881f12005-03-25 15:52:00 -0800405 return 1;
406}
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100409store_spi_revalidate(struct device *dev, struct device_attribute *attr,
410 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
Tony Jonesee959b02008-02-22 00:13:36 +0100412 struct scsi_target *starget = transport_class_to_starget(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
gregkh@suse.de9a881f12005-03-25 15:52:00 -0800414 device_for_each_child(&starget->dev, NULL, child_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 return count;
416}
Tony Jonesee959b02008-02-22 00:13:36 +0100417static DEVICE_ATTR(revalidate, S_IWUSR, NULL, store_spi_revalidate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419/* Translate the period into ns according to the current spec
420 * for SDTR/PPR messages */
Matthew Wilcoxef725822005-12-15 16:22:01 -0500421static int period_to_str(char *buf, int period)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 int len, picosec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
James Bottomley 62a86122005-05-06 18:05:20 -0500425 if (period < 0 || period > 0xff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 picosec = -1;
James Bottomley 62a86122005-05-06 18:05:20 -0500427 } else if (period <= SPI_STATIC_PPR) {
428 picosec = ppr_to_ps[period];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 } else {
James Bottomley 62a86122005-05-06 18:05:20 -0500430 picosec = period * 4000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 }
432
433 if (picosec == -1) {
434 len = sprintf(buf, "reserved");
435 } else {
436 len = sprint_frac(buf, picosec, 1000);
437 }
438
Matthew Wilcoxef725822005-12-15 16:22:01 -0500439 return len;
440}
441
442static ssize_t
Matthew Wilcoxea697e42006-02-07 08:01:02 -0700443show_spi_transport_period_helper(char *buf, int period)
Matthew Wilcoxef725822005-12-15 16:22:01 -0500444{
445 int len = period_to_str(buf, period);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 buf[len++] = '\n';
447 buf[len] = '\0';
448 return len;
449}
450
451static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100452store_spi_transport_period_helper(struct device *dev, const char *buf,
James Bottomley 62a86122005-05-06 18:05:20 -0500453 size_t count, int *periodp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 int j, picosec, period = -1;
456 char *endp;
457
458 picosec = simple_strtoul(buf, &endp, 10) * 1000;
459 if (*endp == '.') {
460 int mult = 100;
461 do {
462 endp++;
463 if (!isdigit(*endp))
464 break;
465 picosec += (*endp - '0') * mult;
466 mult /= 10;
467 } while (mult > 0);
468 }
469
470 for (j = 0; j <= SPI_STATIC_PPR; j++) {
471 if (ppr_to_ps[j] < picosec)
472 continue;
473 period = j;
474 break;
475 }
476
477 if (period == -1)
478 period = picosec / 4000;
479
480 if (period > 0xff)
481 period = 0xff;
482
James Bottomley 62a86122005-05-06 18:05:20 -0500483 *periodp = period;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 return count;
486}
487
James Bottomley 62a86122005-05-06 18:05:20 -0500488static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100489show_spi_transport_period(struct device *dev,
490 struct device_attribute *attr, char *buf)
James Bottomley 62a86122005-05-06 18:05:20 -0500491{
Tony Jonesee959b02008-02-22 00:13:36 +0100492 struct scsi_target *starget = transport_class_to_starget(dev);
James Bottomley 62a86122005-05-06 18:05:20 -0500493 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
494 struct spi_internal *i = to_spi_internal(shost->transportt);
495 struct spi_transport_attrs *tp =
496 (struct spi_transport_attrs *)&starget->starget_data;
497
498 if (i->f->get_period)
499 i->f->get_period(starget);
500
Matthew Wilcoxea697e42006-02-07 08:01:02 -0700501 return show_spi_transport_period_helper(buf, tp->period);
James Bottomley 62a86122005-05-06 18:05:20 -0500502}
503
504static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100505store_spi_transport_period(struct device *cdev, struct device_attribute *attr,
506 const char *buf, size_t count)
James Bottomley 62a86122005-05-06 18:05:20 -0500507{
508 struct scsi_target *starget = transport_class_to_starget(cdev);
509 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
510 struct spi_internal *i = to_spi_internal(shost->transportt);
511 struct spi_transport_attrs *tp =
512 (struct spi_transport_attrs *)&starget->starget_data;
513 int period, retval;
514
James Bottomley9b161a42008-01-05 10:18:27 -0600515 if (!i->f->set_period)
516 return -EINVAL;
517
James Bottomley 62a86122005-05-06 18:05:20 -0500518 retval = store_spi_transport_period_helper(cdev, buf, count, &period);
519
520 if (period < tp->min_period)
521 period = tp->min_period;
522
523 i->f->set_period(starget, period);
524
525 return retval;
526}
527
Tony Jonesee959b02008-02-22 00:13:36 +0100528static DEVICE_ATTR(period, S_IRUGO,
529 show_spi_transport_period,
530 store_spi_transport_period);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
James Bottomley 62a86122005-05-06 18:05:20 -0500532static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100533show_spi_transport_min_period(struct device *cdev,
534 struct device_attribute *attr, char *buf)
James Bottomley 62a86122005-05-06 18:05:20 -0500535{
536 struct scsi_target *starget = transport_class_to_starget(cdev);
James Bottomley9b161a42008-01-05 10:18:27 -0600537 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
538 struct spi_internal *i = to_spi_internal(shost->transportt);
James Bottomley 62a86122005-05-06 18:05:20 -0500539 struct spi_transport_attrs *tp =
540 (struct spi_transport_attrs *)&starget->starget_data;
541
James Bottomley9b161a42008-01-05 10:18:27 -0600542 if (!i->f->set_period)
543 return -EINVAL;
544
Matthew Wilcoxea697e42006-02-07 08:01:02 -0700545 return show_spi_transport_period_helper(buf, tp->min_period);
James Bottomley 62a86122005-05-06 18:05:20 -0500546}
547
548static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100549store_spi_transport_min_period(struct device *cdev,
550 struct device_attribute *attr,
551 const char *buf, size_t count)
James Bottomley 62a86122005-05-06 18:05:20 -0500552{
553 struct scsi_target *starget = transport_class_to_starget(cdev);
554 struct spi_transport_attrs *tp =
555 (struct spi_transport_attrs *)&starget->starget_data;
556
557 return store_spi_transport_period_helper(cdev, buf, count,
558 &tp->min_period);
559}
560
561
Tony Jonesee959b02008-02-22 00:13:36 +0100562static DEVICE_ATTR(min_period, S_IRUGO,
563 show_spi_transport_min_period,
564 store_spi_transport_min_period);
James Bottomley 62a86122005-05-06 18:05:20 -0500565
566
Tony Jonesee959b02008-02-22 00:13:36 +0100567static ssize_t show_spi_host_signalling(struct device *cdev,
568 struct device_attribute *attr,
569 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
571 struct Scsi_Host *shost = transport_class_to_shost(cdev);
572 struct spi_internal *i = to_spi_internal(shost->transportt);
573
574 if (i->f->get_signalling)
575 i->f->get_signalling(shost);
576
577 return sprintf(buf, "%s\n", spi_signal_to_string(spi_signalling(shost)));
578}
Tony Jonesee959b02008-02-22 00:13:36 +0100579static ssize_t store_spi_host_signalling(struct device *dev,
580 struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 const char *buf, size_t count)
582{
Tony Jonesee959b02008-02-22 00:13:36 +0100583 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 struct spi_internal *i = to_spi_internal(shost->transportt);
585 enum spi_signal_type type = spi_signal_to_value(buf);
586
James Bottomley9b161a42008-01-05 10:18:27 -0600587 if (!i->f->set_signalling)
588 return -EINVAL;
589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 if (type != SPI_SIGNAL_UNKNOWN)
591 i->f->set_signalling(shost, type);
592
593 return count;
594}
Tony Jonesee959b02008-02-22 00:13:36 +0100595static DEVICE_ATTR(signalling, S_IRUGO,
596 show_spi_host_signalling,
597 store_spi_host_signalling);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Hannes Reinecke0ac23772011-06-30 02:19:57 +0530599static ssize_t show_spi_host_width(struct device *cdev,
600 struct device_attribute *attr,
601 char *buf)
602{
603 struct Scsi_Host *shost = transport_class_to_shost(cdev);
604
605 return sprintf(buf, "%s\n", shost->max_id == 16 ? "wide" : "narrow");
606}
607static DEVICE_ATTR(host_width, S_IRUGO,
608 show_spi_host_width, NULL);
609
610static ssize_t show_spi_host_hba_id(struct device *cdev,
611 struct device_attribute *attr,
612 char *buf)
613{
614 struct Scsi_Host *shost = transport_class_to_shost(cdev);
615
616 return sprintf(buf, "%d\n", shost->this_id);
617}
618static DEVICE_ATTR(hba_id, S_IRUGO,
619 show_spi_host_hba_id, NULL);
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621#define DV_SET(x, y) \
622 if(i->f->set_##x) \
623 i->f->set_##x(sdev->sdev_target, y)
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625enum spi_compare_returns {
626 SPI_COMPARE_SUCCESS,
627 SPI_COMPARE_FAILURE,
628 SPI_COMPARE_SKIP_TEST,
629};
630
631
632/* This is for read/write Domain Validation: If the device supports
633 * an echo buffer, we do read/write tests to it */
634static enum spi_compare_returns
James Bottomley33aa6872005-08-28 11:31:14 -0500635spi_dv_device_echo_buffer(struct scsi_device *sdev, u8 *buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 u8 *ptr, const int retries)
637{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 int len = ptr - buffer;
James Bottomley33aa6872005-08-28 11:31:14 -0500639 int j, k, r, result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 unsigned int pattern = 0x0000ffff;
James Bottomley33aa6872005-08-28 11:31:14 -0500641 struct scsi_sense_hdr sshdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 const char spi_write_buffer[] = {
644 WRITE_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
645 };
646 const char spi_read_buffer[] = {
647 READ_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
648 };
649
650 /* set up the pattern buffer. Doesn't matter if we spill
651 * slightly beyond since that's where the read buffer is */
652 for (j = 0; j < len; ) {
653
654 /* fill the buffer with counting (test a) */
655 for ( ; j < min(len, 32); j++)
656 buffer[j] = j;
657 k = j;
658 /* fill the buffer with alternating words of 0x0 and
659 * 0xffff (test b) */
660 for ( ; j < min(len, k + 32); j += 2) {
661 u16 *word = (u16 *)&buffer[j];
662
663 *word = (j & 0x02) ? 0x0000 : 0xffff;
664 }
665 k = j;
666 /* fill with crosstalk (alternating 0x5555 0xaaa)
667 * (test c) */
668 for ( ; j < min(len, k + 32); j += 2) {
669 u16 *word = (u16 *)&buffer[j];
670
671 *word = (j & 0x02) ? 0x5555 : 0xaaaa;
672 }
673 k = j;
674 /* fill with shifting bits (test d) */
675 for ( ; j < min(len, k + 32); j += 4) {
676 u32 *word = (unsigned int *)&buffer[j];
677 u32 roll = (pattern & 0x80000000) ? 1 : 0;
678
679 *word = pattern;
680 pattern = (pattern << 1) | roll;
681 }
682 /* don't bother with random data (test e) */
683 }
684
685 for (r = 0; r < retries; r++) {
James Bottomley33aa6872005-08-28 11:31:14 -0500686 result = spi_execute(sdev, spi_write_buffer, DMA_TO_DEVICE,
687 buffer, len, &sshdr);
688 if(result || !scsi_device_online(sdev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 scsi_device_set_state(sdev, SDEV_QUIESCE);
James Bottomley33aa6872005-08-28 11:31:14 -0500691 if (scsi_sense_valid(&sshdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 && sshdr.sense_key == ILLEGAL_REQUEST
693 /* INVALID FIELD IN CDB */
694 && sshdr.asc == 0x24 && sshdr.ascq == 0x00)
695 /* This would mean that the drive lied
696 * to us about supporting an echo
697 * buffer (unfortunately some Western
698 * Digital drives do precisely this)
699 */
700 return SPI_COMPARE_SKIP_TEST;
701
702
James Bottomley9ccfc752005-10-02 11:45:08 -0500703 sdev_printk(KERN_ERR, sdev, "Write Buffer failure %x\n", result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 return SPI_COMPARE_FAILURE;
705 }
706
707 memset(ptr, 0, len);
James Bottomley33aa6872005-08-28 11:31:14 -0500708 spi_execute(sdev, spi_read_buffer, DMA_FROM_DEVICE,
709 ptr, len, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 scsi_device_set_state(sdev, SDEV_QUIESCE);
711
712 if (memcmp(buffer, ptr, len) != 0)
713 return SPI_COMPARE_FAILURE;
714 }
715 return SPI_COMPARE_SUCCESS;
716}
717
718/* This is for the simplest form of Domain Validation: a read test
719 * on the inquiry data from the device */
720static enum spi_compare_returns
James Bottomley33aa6872005-08-28 11:31:14 -0500721spi_dv_device_compare_inquiry(struct scsi_device *sdev, u8 *buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 u8 *ptr, const int retries)
723{
James Bottomley33aa6872005-08-28 11:31:14 -0500724 int r, result;
725 const int len = sdev->inquiry_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 const char spi_inquiry[] = {
727 INQUIRY, 0, 0, 0, len, 0
728 };
729
730 for (r = 0; r < retries; r++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 memset(ptr, 0, len);
732
James Bottomley33aa6872005-08-28 11:31:14 -0500733 result = spi_execute(sdev, spi_inquiry, DMA_FROM_DEVICE,
734 ptr, len, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
James Bottomley33aa6872005-08-28 11:31:14 -0500736 if(result || !scsi_device_online(sdev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 scsi_device_set_state(sdev, SDEV_QUIESCE);
738 return SPI_COMPARE_FAILURE;
739 }
740
741 /* If we don't have the inquiry data already, the
742 * first read gets it */
743 if (ptr == buffer) {
744 ptr += len;
745 --r;
746 continue;
747 }
748
749 if (memcmp(buffer, ptr, len) != 0)
750 /* failure */
751 return SPI_COMPARE_FAILURE;
752 }
753 return SPI_COMPARE_SUCCESS;
754}
755
756static enum spi_compare_returns
James Bottomley33aa6872005-08-28 11:31:14 -0500757spi_dv_retrain(struct scsi_device *sdev, u8 *buffer, u8 *ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 enum spi_compare_returns
James Bottomley33aa6872005-08-28 11:31:14 -0500759 (*compare_fn)(struct scsi_device *, u8 *, u8 *, int))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
James Bottomley33aa6872005-08-28 11:31:14 -0500761 struct spi_internal *i = to_spi_internal(sdev->host->transportt);
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500762 struct scsi_target *starget = sdev->sdev_target;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 int period = 0, prevperiod = 0;
764 enum spi_compare_returns retval;
765
766
767 for (;;) {
768 int newperiod;
James Bottomley33aa6872005-08-28 11:31:14 -0500769 retval = compare_fn(sdev, buffer, ptr, DV_LOOPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 if (retval == SPI_COMPARE_SUCCESS
772 || retval == SPI_COMPARE_SKIP_TEST)
773 break;
774
775 /* OK, retrain, fallback */
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500776 if (i->f->get_iu)
777 i->f->get_iu(starget);
778 if (i->f->get_qas)
779 i->f->get_qas(starget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 if (i->f->get_period)
781 i->f->get_period(sdev->sdev_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500783 /* Here's the fallback sequence; first try turning off
784 * IU, then QAS (if we can control them), then finally
785 * fall down the periods */
786 if (i->f->set_iu && spi_iu(starget)) {
Masanari Iida804ff602015-05-07 23:21:27 +0900787 starget_printk(KERN_ERR, starget, "Domain Validation Disabling Information Units\n");
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500788 DV_SET(iu, 0);
789 } else if (i->f->set_qas && spi_qas(starget)) {
Masanari Iida804ff602015-05-07 23:21:27 +0900790 starget_printk(KERN_ERR, starget, "Domain Validation Disabling Quick Arbitration and Selection\n");
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500791 DV_SET(qas, 0);
792 } else {
793 newperiod = spi_period(starget);
794 period = newperiod > period ? newperiod : period;
795 if (period < 0x0d)
796 period++;
797 else
798 period += period >> 1;
799
800 if (unlikely(period > 0xff || period == prevperiod)) {
801 /* Total failure; set to async and return */
James Bottomley9ccfc752005-10-02 11:45:08 -0500802 starget_printk(KERN_ERR, starget, "Domain Validation Failure, dropping back to Asynchronous\n");
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500803 DV_SET(offset, 0);
804 return SPI_COMPARE_FAILURE;
805 }
James Bottomley9ccfc752005-10-02 11:45:08 -0500806 starget_printk(KERN_ERR, starget, "Domain Validation detected failure, dropping back\n");
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500807 DV_SET(period, period);
808 prevperiod = period;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 }
811 return retval;
812}
813
814static int
James Bottomley33aa6872005-08-28 11:31:14 -0500815spi_dv_device_get_echo_buffer(struct scsi_device *sdev, u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
James Bottomley33aa6872005-08-28 11:31:14 -0500817 int l, result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 /* first off do a test unit ready. This can error out
820 * because of reservations or some other reason. If it
821 * fails, the device won't let us write to the echo buffer
822 * so just return failure */
823
824 const char spi_test_unit_ready[] = {
825 TEST_UNIT_READY, 0, 0, 0, 0, 0
826 };
827
828 const char spi_read_buffer_descriptor[] = {
829 READ_BUFFER, 0x0b, 0, 0, 0, 0, 0, 0, 4, 0
830 };
831
832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 /* We send a set of three TURs to clear any outstanding
834 * unit attention conditions if they exist (Otherwise the
835 * buffer tests won't be happy). If the TUR still fails
836 * (reservation conflict, device not ready, etc) just
837 * skip the write tests */
838 for (l = 0; ; l++) {
James Bottomley33aa6872005-08-28 11:31:14 -0500839 result = spi_execute(sdev, spi_test_unit_ready, DMA_NONE,
840 NULL, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
James Bottomley33aa6872005-08-28 11:31:14 -0500842 if(result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 if(l >= 3)
844 return 0;
845 } else {
846 /* TUR succeeded */
847 break;
848 }
849 }
850
James Bottomley33aa6872005-08-28 11:31:14 -0500851 result = spi_execute(sdev, spi_read_buffer_descriptor,
852 DMA_FROM_DEVICE, buffer, 4, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
James Bottomley33aa6872005-08-28 11:31:14 -0500854 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 /* Device has no echo buffer */
856 return 0;
857
858 return buffer[3] + ((buffer[2] & 0x1f) << 8);
859}
860
861static void
James Bottomley33aa6872005-08-28 11:31:14 -0500862spi_dv_device_internal(struct scsi_device *sdev, u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
James Bottomley33aa6872005-08-28 11:31:14 -0500864 struct spi_internal *i = to_spi_internal(sdev->host->transportt);
James Bottomley 62a86122005-05-06 18:05:20 -0500865 struct scsi_target *starget = sdev->sdev_target;
James Bottomley60eef252006-06-10 10:51:23 -0500866 struct Scsi_Host *shost = sdev->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 int len = sdev->inquiry_len;
James Bottomley23028272007-09-22 08:40:09 -0500868 int min_period = spi_min_period(starget);
869 int max_width = spi_max_width(starget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 /* first set us up for narrow async */
871 DV_SET(offset, 0);
872 DV_SET(width, 0);
James Bottomley23028272007-09-22 08:40:09 -0500873
James Bottomley33aa6872005-08-28 11:31:14 -0500874 if (spi_dv_device_compare_inquiry(sdev, buffer, buffer, DV_LOOPS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 != SPI_COMPARE_SUCCESS) {
James Bottomley9ccfc752005-10-02 11:45:08 -0500876 starget_printk(KERN_ERR, starget, "Domain Validation Initial Inquiry Failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 /* FIXME: should probably offline the device here? */
878 return;
879 }
880
James Bottomley9872b812009-06-17 15:03:41 -0400881 if (!spi_support_wide(starget)) {
James Bottomley23028272007-09-22 08:40:09 -0500882 spi_max_width(starget) = 0;
883 max_width = 0;
884 }
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 /* test width */
James Bottomley23028272007-09-22 08:40:09 -0500887 if (i->f->set_width && max_width) {
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500888 i->f->set_width(starget, 1);
James Bottomley 62a86122005-05-06 18:05:20 -0500889
James Bottomley33aa6872005-08-28 11:31:14 -0500890 if (spi_dv_device_compare_inquiry(sdev, buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 buffer + len,
892 DV_LOOPS)
893 != SPI_COMPARE_SUCCESS) {
James Bottomley9ccfc752005-10-02 11:45:08 -0500894 starget_printk(KERN_ERR, starget, "Wide Transfers Fail\n");
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500895 i->f->set_width(starget, 0);
James Bottomley23028272007-09-22 08:40:09 -0500896 /* Make sure we don't force wide back on by asking
897 * for a transfer period that requires it */
898 max_width = 0;
899 if (min_period < 10)
900 min_period = 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 }
902 }
903
904 if (!i->f->set_period)
905 return;
906
907 /* device can't handle synchronous */
James Bottomley9872b812009-06-17 15:03:41 -0400908 if (!spi_support_sync(starget) && !spi_support_dt(starget))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 return;
910
James Bottomley349cd7c2005-11-28 15:41:58 -0600911 /* len == -1 is the signal that we need to ascertain the
912 * presence of an echo buffer before trying to use it. len ==
913 * 0 means we don't have an echo buffer */
914 len = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 retry:
917
918 /* now set up to the maximum */
James Bottomley 62a86122005-05-06 18:05:20 -0500919 DV_SET(offset, spi_max_offset(starget));
James Bottomley23028272007-09-22 08:40:09 -0500920 DV_SET(period, min_period);
921
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500922 /* try QAS requests; this should be harmless to set if the
923 * target supports it */
James Bottomley9872b812009-06-17 15:03:41 -0400924 if (spi_support_qas(starget) && spi_max_qas(starget)) {
James Bottomleyeb1dd682005-07-02 12:22:01 -0400925 DV_SET(qas, 1);
James Bottomleydfdc58b2006-09-20 12:00:18 -0400926 } else {
927 DV_SET(qas, 0);
928 }
929
James Bottomley9872b812009-06-17 15:03:41 -0400930 if (spi_support_ius(starget) && spi_max_iu(starget) &&
931 min_period < 9) {
James Bottomleydfdc58b2006-09-20 12:00:18 -0400932 /* This u320 (or u640). Set IU transfers */
James Bottomleyeb1dd682005-07-02 12:22:01 -0400933 DV_SET(iu, 1);
James Bottomleydfdc58b2006-09-20 12:00:18 -0400934 /* Then set the optional parameters */
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500935 DV_SET(rd_strm, 1);
936 DV_SET(wr_flow, 1);
937 DV_SET(rti, 1);
James Bottomley23028272007-09-22 08:40:09 -0500938 if (min_period == 8)
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500939 DV_SET(pcomp_en, 1);
James Bottomleydfdc58b2006-09-20 12:00:18 -0400940 } else {
941 DV_SET(iu, 0);
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500942 }
James Bottomleydfdc58b2006-09-20 12:00:18 -0400943
James Bottomley60eef252006-06-10 10:51:23 -0500944 /* now that we've done all this, actually check the bus
945 * signal type (if known). Some devices are stupid on
946 * a SE bus and still claim they can try LVD only settings */
947 if (i->f->get_signalling)
948 i->f->get_signalling(shost);
949 if (spi_signalling(shost) == SPI_SIGNAL_SE ||
James Bottomleydfdc58b2006-09-20 12:00:18 -0400950 spi_signalling(shost) == SPI_SIGNAL_HVD ||
James Bottomley9872b812009-06-17 15:03:41 -0400951 !spi_support_dt(starget)) {
James Bottomley60eef252006-06-10 10:51:23 -0500952 DV_SET(dt, 0);
James Bottomleydfdc58b2006-09-20 12:00:18 -0400953 } else {
954 DV_SET(dt, 1);
955 }
James Bottomley23028272007-09-22 08:40:09 -0500956 /* set width last because it will pull all the other
957 * parameters down to required values */
958 DV_SET(width, max_width);
959
James Bottomley349cd7c2005-11-28 15:41:58 -0600960 /* Do the read only INQUIRY tests */
961 spi_dv_retrain(sdev, buffer, buffer + sdev->inquiry_len,
962 spi_dv_device_compare_inquiry);
963 /* See if we actually managed to negotiate and sustain DT */
964 if (i->f->get_dt)
965 i->f->get_dt(starget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
James Bottomley349cd7c2005-11-28 15:41:58 -0600967 /* see if the device has an echo buffer. If it does we can do
968 * the SPI pattern write tests. Because of some broken
969 * devices, we *only* try this on a device that has actually
970 * negotiated DT */
971
972 if (len == -1 && spi_dt(starget))
973 len = spi_dv_device_get_echo_buffer(sdev, buffer);
974
975 if (len <= 0) {
James Bottomley9ccfc752005-10-02 11:45:08 -0500976 starget_printk(KERN_INFO, starget, "Domain Validation skipping write tests\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 return;
978 }
979
980 if (len > SPI_MAX_ECHO_BUFFER_SIZE) {
James Bottomley9ccfc752005-10-02 11:45:08 -0500981 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 -0700982 len = SPI_MAX_ECHO_BUFFER_SIZE;
983 }
984
James Bottomley33aa6872005-08-28 11:31:14 -0500985 if (spi_dv_retrain(sdev, buffer, buffer + len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 spi_dv_device_echo_buffer)
987 == SPI_COMPARE_SKIP_TEST) {
988 /* OK, the stupid drive can't do a write echo buffer
989 * test after all, fall back to the read tests */
990 len = 0;
991 goto retry;
992 }
993}
994
995
996/** spi_dv_device - Do Domain Validation on the device
997 * @sdev: scsi device to validate
998 *
999 * Performs the domain validation on the given device in the
1000 * current execution thread. Since DV operations may sleep,
1001 * the current thread must have user context. Also no SCSI
1002 * related locks that would deadlock I/O issued by the DV may
1003 * be held.
1004 */
1005void
1006spi_dv_device(struct scsi_device *sdev)
1007{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 struct scsi_target *starget = sdev->sdev_target;
1009 u8 *buffer;
1010 const int len = SPI_MAX_ECHO_BUFFER_SIZE*2;
1011
Mike Maslenkin89a342c2012-04-28 05:32:14 +04001012 if (unlikely(spi_dv_in_progress(starget)))
James Bottomley33aa6872005-08-28 11:31:14 -05001013 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Mike Maslenkin89a342c2012-04-28 05:32:14 +04001015 if (unlikely(scsi_device_get(sdev)))
James Bottomleydfdc58b2006-09-20 12:00:18 -04001016 return;
1017 spi_dv_in_progress(starget) = 1;
1018
Jes Sorensen24669f752006-01-16 10:31:18 -05001019 buffer = kzalloc(len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021 if (unlikely(!buffer))
1022 goto out_put;
1023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 /* We need to verify that the actual device will quiesce; the
1025 * later target quiesce is just a nice to have */
1026 if (unlikely(scsi_device_quiesce(sdev)))
1027 goto out_free;
1028
1029 scsi_target_quiesce(starget);
1030
1031 spi_dv_pending(starget) = 1;
Jes Sorensend158d262006-01-13 16:05:44 -08001032 mutex_lock(&spi_dv_mutex(starget));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
James Bottomley9ccfc752005-10-02 11:45:08 -05001034 starget_printk(KERN_INFO, starget, "Beginning Domain Validation\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
James Bottomley33aa6872005-08-28 11:31:14 -05001036 spi_dv_device_internal(sdev, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
James Bottomley9ccfc752005-10-02 11:45:08 -05001038 starget_printk(KERN_INFO, starget, "Ending Domain Validation\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Jes Sorensend158d262006-01-13 16:05:44 -08001040 mutex_unlock(&spi_dv_mutex(starget));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 spi_dv_pending(starget) = 0;
1042
1043 scsi_target_resume(starget);
1044
1045 spi_initial_dv(starget) = 1;
1046
1047 out_free:
1048 kfree(buffer);
1049 out_put:
James Bottomleydfdc58b2006-09-20 12:00:18 -04001050 spi_dv_in_progress(starget) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 scsi_device_put(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052}
1053EXPORT_SYMBOL(spi_dv_device);
1054
1055struct work_queue_wrapper {
1056 struct work_struct work;
1057 struct scsi_device *sdev;
1058};
1059
1060static void
David Howellsc4028952006-11-22 14:57:56 +00001061spi_dv_device_work_wrapper(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
David Howellsc4028952006-11-22 14:57:56 +00001063 struct work_queue_wrapper *wqw =
1064 container_of(work, struct work_queue_wrapper, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 struct scsi_device *sdev = wqw->sdev;
1066
1067 kfree(wqw);
1068 spi_dv_device(sdev);
1069 spi_dv_pending(sdev->sdev_target) = 0;
1070 scsi_device_put(sdev);
1071}
1072
1073
1074/**
1075 * spi_schedule_dv_device - schedule domain validation to occur on the device
1076 * @sdev: The device to validate
1077 *
1078 * Identical to spi_dv_device() above, except that the DV will be
1079 * scheduled to occur in a workqueue later. All memory allocations
1080 * are atomic, so may be called from any context including those holding
1081 * SCSI locks.
1082 */
1083void
1084spi_schedule_dv_device(struct scsi_device *sdev)
1085{
1086 struct work_queue_wrapper *wqw =
1087 kmalloc(sizeof(struct work_queue_wrapper), GFP_ATOMIC);
1088
1089 if (unlikely(!wqw))
1090 return;
1091
1092 if (unlikely(spi_dv_pending(sdev->sdev_target))) {
1093 kfree(wqw);
1094 return;
1095 }
1096 /* Set pending early (dv_device doesn't check it, only sets it) */
1097 spi_dv_pending(sdev->sdev_target) = 1;
1098 if (unlikely(scsi_device_get(sdev))) {
1099 kfree(wqw);
1100 spi_dv_pending(sdev->sdev_target) = 0;
1101 return;
1102 }
1103
David Howellsc4028952006-11-22 14:57:56 +00001104 INIT_WORK(&wqw->work, spi_dv_device_work_wrapper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 wqw->sdev = sdev;
1106
1107 schedule_work(&wqw->work);
1108}
1109EXPORT_SYMBOL(spi_schedule_dv_device);
1110
1111/**
1112 * spi_display_xfer_agreement - Print the current target transfer agreement
1113 * @starget: The target for which to display the agreement
1114 *
1115 * Each SPI port is required to maintain a transfer agreement for each
1116 * other port on the bus. This function prints a one-line summary of
1117 * the current agreement; more detailed information is available in sysfs.
1118 */
1119void spi_display_xfer_agreement(struct scsi_target *starget)
1120{
1121 struct spi_transport_attrs *tp;
1122 tp = (struct spi_transport_attrs *)&starget->starget_data;
1123
1124 if (tp->offset > 0 && tp->period > 0) {
1125 unsigned int picosec, kb100;
1126 char *scsi = "FAST-?";
1127 char tmp[8];
1128
1129 if (tp->period <= SPI_STATIC_PPR) {
1130 picosec = ppr_to_ps[tp->period];
1131 switch (tp->period) {
1132 case 7: scsi = "FAST-320"; break;
1133 case 8: scsi = "FAST-160"; break;
1134 case 9: scsi = "FAST-80"; break;
1135 case 10:
1136 case 11: scsi = "FAST-40"; break;
1137 case 12: scsi = "FAST-20"; break;
1138 }
1139 } else {
1140 picosec = tp->period * 4000;
1141 if (tp->period < 25)
1142 scsi = "FAST-20";
1143 else if (tp->period < 50)
1144 scsi = "FAST-10";
1145 else
1146 scsi = "FAST-5";
1147 }
1148
1149 kb100 = (10000000 + picosec / 2) / picosec;
1150 if (tp->width)
1151 kb100 *= 2;
1152 sprint_frac(tmp, picosec, 1000);
1153
1154 dev_info(&starget->dev,
James Bottomleyd872ebe2005-08-03 15:43:52 -05001155 "%s %sSCSI %d.%d MB/s %s%s%s%s%s%s%s%s (%s ns, offset %d)\n",
1156 scsi, tp->width ? "WIDE " : "", kb100/10, kb100 % 10,
1157 tp->dt ? "DT" : "ST",
1158 tp->iu ? " IU" : "",
1159 tp->qas ? " QAS" : "",
1160 tp->rd_strm ? " RDSTRM" : "",
1161 tp->rti ? " RTI" : "",
1162 tp->wr_flow ? " WRFLOW" : "",
1163 tp->pcomp_en ? " PCOMP" : "",
1164 tp->hold_mcs ? " HMCS" : "",
1165 tmp, tp->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 } else {
Matthew Wilcox493ff4e2005-11-17 11:13:43 -07001167 dev_info(&starget->dev, "%sasynchronous\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 tp->width ? "wide " : "");
1169 }
1170}
1171EXPORT_SYMBOL(spi_display_xfer_agreement);
1172
Matthew Wilcox6ea3c0b2006-02-07 07:54:46 -07001173int spi_populate_width_msg(unsigned char *msg, int width)
1174{
1175 msg[0] = EXTENDED_MESSAGE;
1176 msg[1] = 2;
1177 msg[2] = EXTENDED_WDTR;
1178 msg[3] = width;
1179 return 4;
1180}
James Bottomley38e14f82006-02-17 14:58:47 -08001181EXPORT_SYMBOL_GPL(spi_populate_width_msg);
Matthew Wilcox6ea3c0b2006-02-07 07:54:46 -07001182
1183int spi_populate_sync_msg(unsigned char *msg, int period, int offset)
1184{
1185 msg[0] = EXTENDED_MESSAGE;
1186 msg[1] = 3;
1187 msg[2] = EXTENDED_SDTR;
1188 msg[3] = period;
1189 msg[4] = offset;
1190 return 5;
1191}
James Bottomley38e14f82006-02-17 14:58:47 -08001192EXPORT_SYMBOL_GPL(spi_populate_sync_msg);
Matthew Wilcox6ea3c0b2006-02-07 07:54:46 -07001193
1194int spi_populate_ppr_msg(unsigned char *msg, int period, int offset,
1195 int width, int options)
1196{
1197 msg[0] = EXTENDED_MESSAGE;
1198 msg[1] = 6;
1199 msg[2] = EXTENDED_PPR;
1200 msg[3] = period;
1201 msg[4] = 0;
1202 msg[5] = offset;
1203 msg[6] = width;
1204 msg[7] = options;
1205 return 8;
1206}
James Bottomley38e14f82006-02-17 14:58:47 -08001207EXPORT_SYMBOL_GPL(spi_populate_ppr_msg);
Matthew Wilcox6ea3c0b2006-02-07 07:54:46 -07001208
Christoph Hellwig50668632014-10-30 14:30:06 +01001209/**
1210 * spi_populate_tag_msg - place a tag message in a buffer
1211 * @msg: pointer to the area to place the tag
1212 * @cmd: pointer to the scsi command for the tag
1213 *
1214 * Notes:
1215 * designed to create the correct type of tag message for the
1216 * particular request. Returns the size of the tag message.
1217 * May return 0 if TCQ is disabled for this device.
1218 **/
1219int spi_populate_tag_msg(unsigned char *msg, struct scsi_cmnd *cmd)
1220{
1221 if (cmd->flags & SCMD_TAGGED) {
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001222 *msg++ = SIMPLE_QUEUE_TAG;
Christoph Hellwig50668632014-10-30 14:30:06 +01001223 *msg++ = cmd->request->tag;
1224 return 2;
1225 }
1226
1227 return 0;
1228}
1229EXPORT_SYMBOL_GPL(spi_populate_tag_msg);
1230
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001231#ifdef CONFIG_SCSI_CONSTANTS
1232static const char * const one_byte_msgs[] = {
Matthew Wilcox72df0eb2006-03-10 07:18:22 -07001233/* 0x00 */ "Task Complete", NULL /* Extended Message */, "Save Pointers",
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001234/* 0x03 */ "Restore Pointers", "Disconnect", "Initiator Error",
Matthew Wilcox72df0eb2006-03-10 07:18:22 -07001235/* 0x06 */ "Abort Task Set", "Message Reject", "Nop", "Message Parity Error",
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001236/* 0x0a */ "Linked Command Complete", "Linked Command Complete w/flag",
Matthew Wilcox72df0eb2006-03-10 07:18:22 -07001237/* 0x0c */ "Target Reset", "Abort Task", "Clear Task Set",
1238/* 0x0f */ "Initiate Recovery", "Release Recovery",
1239/* 0x11 */ "Terminate Process", "Continue Task", "Target Transfer Disable",
1240/* 0x14 */ NULL, NULL, "Clear ACA", "LUN Reset"
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001241};
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001242
1243static const char * const two_byte_msgs[] = {
Matthew Wilcox47972152005-12-15 16:22:01 -05001244/* 0x20 */ "Simple Queue Tag", "Head of Queue Tag", "Ordered Queue Tag",
Matthew Wilcox72df0eb2006-03-10 07:18:22 -07001245/* 0x23 */ "Ignore Wide Residue", "ACA"
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001246};
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001247
1248static const char * const extended_msgs[] = {
1249/* 0x00 */ "Modify Data Pointer", "Synchronous Data Transfer Request",
Matthew Wilcoxef725822005-12-15 16:22:01 -05001250/* 0x02 */ "SCSI-I Extended Identify", "Wide Data Transfer Request",
Matthew Wilcoxfc253072006-02-18 20:52:31 -07001251/* 0x04 */ "Parallel Protocol Request", "Modify Bidirectional Data Pointer"
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001252};
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001253
Adrian Bunkbdd70f22006-01-05 23:39:18 +01001254static void print_nego(const unsigned char *msg, int per, int off, int width)
Matthew Wilcoxef725822005-12-15 16:22:01 -05001255{
1256 if (per) {
1257 char buf[20];
1258 period_to_str(buf, msg[per]);
1259 printk("period = %s ns ", buf);
1260 }
1261
1262 if (off)
1263 printk("offset = %d ", msg[off]);
1264 if (width)
1265 printk("width = %d ", 8 << msg[width]);
1266}
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001267
Matthew Wilcoxfc253072006-02-18 20:52:31 -07001268static void print_ptr(const unsigned char *msg, int msb, const char *desc)
1269{
1270 int ptr = (msg[msb] << 24) | (msg[msb+1] << 16) | (msg[msb+2] << 8) |
1271 msg[msb+3];
1272 printk("%s = %d ", desc, ptr);
1273}
1274
Matthew Wilcox1abfd372005-12-15 16:22:01 -05001275int spi_print_msg(const unsigned char *msg)
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001276{
Matthew Wilcox72df0eb2006-03-10 07:18:22 -07001277 int len = 1, i;
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001278 if (msg[0] == EXTENDED_MESSAGE) {
Matthew Wilcoxfc253072006-02-18 20:52:31 -07001279 len = 2 + msg[1];
1280 if (len == 2)
1281 len += 256;
Matthew Wilcoxb32aaff2005-12-15 16:22:01 -05001282 if (msg[2] < ARRAY_SIZE(extended_msgs))
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001283 printk ("%s ", extended_msgs[msg[2]]);
1284 else
1285 printk ("Extended Message, reserved code (0x%02x) ",
1286 (int) msg[2]);
1287 switch (msg[2]) {
1288 case EXTENDED_MODIFY_DATA_POINTER:
Matthew Wilcoxfc253072006-02-18 20:52:31 -07001289 print_ptr(msg, 3, "pointer");
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001290 break;
1291 case EXTENDED_SDTR:
Matthew Wilcoxef725822005-12-15 16:22:01 -05001292 print_nego(msg, 3, 4, 0);
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001293 break;
1294 case EXTENDED_WDTR:
Matthew Wilcoxef725822005-12-15 16:22:01 -05001295 print_nego(msg, 0, 0, 3);
1296 break;
1297 case EXTENDED_PPR:
1298 print_nego(msg, 3, 5, 6);
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001299 break;
Matthew Wilcoxfc253072006-02-18 20:52:31 -07001300 case EXTENDED_MODIFY_BIDI_DATA_PTR:
1301 print_ptr(msg, 3, "out");
1302 print_ptr(msg, 7, "in");
1303 break;
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001304 default:
1305 for (i = 2; i < len; ++i)
1306 printk("%02x ", msg[i]);
1307 }
1308 /* Identify */
1309 } else if (msg[0] & 0x80) {
1310 printk("Identify disconnect %sallowed %s %d ",
1311 (msg[0] & 0x40) ? "" : "not ",
1312 (msg[0] & 0x20) ? "target routine" : "lun",
1313 msg[0] & 0x7);
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001314 /* Normal One byte */
1315 } else if (msg[0] < 0x1f) {
Matthew Wilcox72df0eb2006-03-10 07:18:22 -07001316 if (msg[0] < ARRAY_SIZE(one_byte_msgs) && one_byte_msgs[msg[0]])
Matthew Wilcoxe24d8732006-02-07 08:05:26 -07001317 printk("%s ", one_byte_msgs[msg[0]]);
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001318 else
1319 printk("reserved (%02x) ", msg[0]);
Matthew Wilcox72df0eb2006-03-10 07:18:22 -07001320 } else if (msg[0] == 0x55) {
1321 printk("QAS Request ");
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001322 /* Two byte */
1323 } else if (msg[0] <= 0x2f) {
Matthew Wilcoxb32aaff2005-12-15 16:22:01 -05001324 if ((msg[0] - 0x20) < ARRAY_SIZE(two_byte_msgs))
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001325 printk("%s %02x ", two_byte_msgs[msg[0] - 0x20],
1326 msg[1]);
1327 else
1328 printk("reserved two byte (%02x %02x) ",
1329 msg[0], msg[1]);
1330 len = 2;
1331 } else
Matthew Wilcoxe24d8732006-02-07 08:05:26 -07001332 printk("reserved ");
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001333 return len;
1334}
Matthew Wilcox1abfd372005-12-15 16:22:01 -05001335EXPORT_SYMBOL(spi_print_msg);
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001336
1337#else /* ifndef CONFIG_SCSI_CONSTANTS */
1338
Matthew Wilcox1abfd372005-12-15 16:22:01 -05001339int spi_print_msg(const unsigned char *msg)
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001340{
Matthew Wilcox72df0eb2006-03-10 07:18:22 -07001341 int len = 1, i;
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001342
1343 if (msg[0] == EXTENDED_MESSAGE) {
Matthew Wilcoxfc253072006-02-18 20:52:31 -07001344 len = 2 + msg[1];
1345 if (len == 2)
1346 len += 256;
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001347 for (i = 0; i < len; ++i)
1348 printk("%02x ", msg[i]);
1349 /* Identify */
1350 } else if (msg[0] & 0x80) {
1351 printk("%02x ", msg[0]);
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001352 /* Normal One byte */
James Bottomley597705a2006-03-12 09:54:19 -06001353 } else if ((msg[0] < 0x1f) || (msg[0] == 0x55)) {
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001354 printk("%02x ", msg[0]);
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001355 /* Two byte */
1356 } else if (msg[0] <= 0x2f) {
1357 printk("%02x %02x", msg[0], msg[1]);
1358 len = 2;
1359 } else
1360 printk("%02x ", msg[0]);
1361 return len;
1362}
Matthew Wilcox1abfd372005-12-15 16:22:01 -05001363EXPORT_SYMBOL(spi_print_msg);
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001364#endif /* ! CONFIG_SCSI_CONSTANTS */
1365
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366static int spi_device_match(struct attribute_container *cont,
1367 struct device *dev)
1368{
1369 struct scsi_device *sdev;
1370 struct Scsi_Host *shost;
James Bottomley10c1b882005-08-14 14:34:06 -05001371 struct spi_internal *i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
1373 if (!scsi_is_sdev_device(dev))
1374 return 0;
1375
1376 sdev = to_scsi_device(dev);
1377 shost = sdev->host;
1378 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1379 != &spi_host_class.class)
1380 return 0;
1381 /* Note: this class has no device attributes, so it has
1382 * no per-HBA allocation and thus we don't need to distinguish
1383 * the attribute containers for the device */
James Bottomley10c1b882005-08-14 14:34:06 -05001384 i = to_spi_internal(shost->transportt);
1385 if (i->f->deny_binding && i->f->deny_binding(sdev->sdev_target))
1386 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 return 1;
1388}
1389
1390static int spi_target_match(struct attribute_container *cont,
1391 struct device *dev)
1392{
1393 struct Scsi_Host *shost;
James Bottomley10c1b882005-08-14 14:34:06 -05001394 struct scsi_target *starget;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 struct spi_internal *i;
1396
1397 if (!scsi_is_target_device(dev))
1398 return 0;
1399
1400 shost = dev_to_shost(dev->parent);
1401 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1402 != &spi_host_class.class)
1403 return 0;
1404
1405 i = to_spi_internal(shost->transportt);
James Bottomley10c1b882005-08-14 14:34:06 -05001406 starget = to_scsi_target(dev);
1407
1408 if (i->f->deny_binding && i->f->deny_binding(starget))
1409 return 0;
1410
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 return &i->t.target_attrs.ac == cont;
1412}
1413
1414static DECLARE_TRANSPORT_CLASS(spi_transport_class,
1415 "spi_transport",
1416 spi_setup_transport_attrs,
1417 NULL,
James Bottomley9b161a42008-01-05 10:18:27 -06001418 spi_target_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
1420static DECLARE_ANON_TRANSPORT_CLASS(spi_device_class,
1421 spi_device_match,
1422 spi_device_configure);
1423
James Bottomley9b161a42008-01-05 10:18:27 -06001424static struct attribute *host_attributes[] = {
Tony Jonesee959b02008-02-22 00:13:36 +01001425 &dev_attr_signalling.attr,
Hannes Reinecke0ac23772011-06-30 02:19:57 +05301426 &dev_attr_host_width.attr,
1427 &dev_attr_hba_id.attr,
James Bottomley9b161a42008-01-05 10:18:27 -06001428 NULL
1429};
1430
1431static struct attribute_group host_attribute_group = {
1432 .attrs = host_attributes,
1433};
1434
1435static int spi_host_configure(struct transport_container *tc,
1436 struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +01001437 struct device *cdev)
James Bottomley9b161a42008-01-05 10:18:27 -06001438{
1439 struct kobject *kobj = &cdev->kobj;
1440 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1441 struct spi_internal *si = to_spi_internal(shost->transportt);
Tony Jonesee959b02008-02-22 00:13:36 +01001442 struct attribute *attr = &dev_attr_signalling.attr;
James Bottomley9b161a42008-01-05 10:18:27 -06001443 int rc = 0;
1444
1445 if (si->f->set_signalling)
1446 rc = sysfs_chmod_file(kobj, attr, attr->mode | S_IWUSR);
1447
1448 return rc;
1449}
1450
1451/* returns true if we should be showing the variable. Also
1452 * overloads the return by setting 1<<1 if the attribute should
1453 * be writeable */
1454#define TARGET_ATTRIBUTE_HELPER(name) \
James Bottomley352f6bb2008-03-20 20:57:02 -05001455 (si->f->show_##name ? S_IRUGO : 0) | \
1456 (si->f->set_##name ? S_IWUSR : 0)
James Bottomley9b161a42008-01-05 10:18:27 -06001457
Al Viro587a1f12011-07-23 23:11:19 -04001458static umode_t target_attribute_is_visible(struct kobject *kobj,
James Bottomley352f6bb2008-03-20 20:57:02 -05001459 struct attribute *attr, int i)
James Bottomley9b161a42008-01-05 10:18:27 -06001460{
Tony Jonesee959b02008-02-22 00:13:36 +01001461 struct device *cdev = container_of(kobj, struct device, kobj);
James Bottomley9b161a42008-01-05 10:18:27 -06001462 struct scsi_target *starget = transport_class_to_starget(cdev);
1463 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1464 struct spi_internal *si = to_spi_internal(shost->transportt);
1465
Tony Jonesee959b02008-02-22 00:13:36 +01001466 if (attr == &dev_attr_period.attr &&
James Bottomley9b161a42008-01-05 10:18:27 -06001467 spi_support_sync(starget))
1468 return TARGET_ATTRIBUTE_HELPER(period);
Tony Jonesee959b02008-02-22 00:13:36 +01001469 else if (attr == &dev_attr_min_period.attr &&
James Bottomley9b161a42008-01-05 10:18:27 -06001470 spi_support_sync(starget))
1471 return TARGET_ATTRIBUTE_HELPER(period);
Tony Jonesee959b02008-02-22 00:13:36 +01001472 else if (attr == &dev_attr_offset.attr &&
James Bottomley9b161a42008-01-05 10:18:27 -06001473 spi_support_sync(starget))
1474 return TARGET_ATTRIBUTE_HELPER(offset);
Tony Jonesee959b02008-02-22 00:13:36 +01001475 else if (attr == &dev_attr_max_offset.attr &&
James Bottomley9b161a42008-01-05 10:18:27 -06001476 spi_support_sync(starget))
1477 return TARGET_ATTRIBUTE_HELPER(offset);
Tony Jonesee959b02008-02-22 00:13:36 +01001478 else if (attr == &dev_attr_width.attr &&
James Bottomley9b161a42008-01-05 10:18:27 -06001479 spi_support_wide(starget))
1480 return TARGET_ATTRIBUTE_HELPER(width);
Tony Jonesee959b02008-02-22 00:13:36 +01001481 else if (attr == &dev_attr_max_width.attr &&
James Bottomley9b161a42008-01-05 10:18:27 -06001482 spi_support_wide(starget))
1483 return TARGET_ATTRIBUTE_HELPER(width);
Tony Jonesee959b02008-02-22 00:13:36 +01001484 else if (attr == &dev_attr_iu.attr &&
James Bottomley9b161a42008-01-05 10:18:27 -06001485 spi_support_ius(starget))
1486 return TARGET_ATTRIBUTE_HELPER(iu);
James Bottomleyea443192009-06-13 12:19:05 -05001487 else if (attr == &dev_attr_max_iu.attr &&
1488 spi_support_ius(starget))
1489 return TARGET_ATTRIBUTE_HELPER(iu);
Tony Jonesee959b02008-02-22 00:13:36 +01001490 else if (attr == &dev_attr_dt.attr &&
James Bottomley9b161a42008-01-05 10:18:27 -06001491 spi_support_dt(starget))
1492 return TARGET_ATTRIBUTE_HELPER(dt);
Tony Jonesee959b02008-02-22 00:13:36 +01001493 else if (attr == &dev_attr_qas.attr &&
James Bottomley9b161a42008-01-05 10:18:27 -06001494 spi_support_qas(starget))
1495 return TARGET_ATTRIBUTE_HELPER(qas);
James Bottomleyea443192009-06-13 12:19:05 -05001496 else if (attr == &dev_attr_max_qas.attr &&
1497 spi_support_qas(starget))
1498 return TARGET_ATTRIBUTE_HELPER(qas);
Tony Jonesee959b02008-02-22 00:13:36 +01001499 else if (attr == &dev_attr_wr_flow.attr &&
James Bottomley9b161a42008-01-05 10:18:27 -06001500 spi_support_ius(starget))
1501 return TARGET_ATTRIBUTE_HELPER(wr_flow);
Tony Jonesee959b02008-02-22 00:13:36 +01001502 else if (attr == &dev_attr_rd_strm.attr &&
James Bottomley9b161a42008-01-05 10:18:27 -06001503 spi_support_ius(starget))
1504 return TARGET_ATTRIBUTE_HELPER(rd_strm);
Tony Jonesee959b02008-02-22 00:13:36 +01001505 else if (attr == &dev_attr_rti.attr &&
James Bottomley9b161a42008-01-05 10:18:27 -06001506 spi_support_ius(starget))
1507 return TARGET_ATTRIBUTE_HELPER(rti);
Tony Jonesee959b02008-02-22 00:13:36 +01001508 else if (attr == &dev_attr_pcomp_en.attr &&
James Bottomley9b161a42008-01-05 10:18:27 -06001509 spi_support_ius(starget))
1510 return TARGET_ATTRIBUTE_HELPER(pcomp_en);
Tony Jonesee959b02008-02-22 00:13:36 +01001511 else if (attr == &dev_attr_hold_mcs.attr &&
James Bottomley9b161a42008-01-05 10:18:27 -06001512 spi_support_ius(starget))
1513 return TARGET_ATTRIBUTE_HELPER(hold_mcs);
Tony Jonesee959b02008-02-22 00:13:36 +01001514 else if (attr == &dev_attr_revalidate.attr)
James Bottomley352f6bb2008-03-20 20:57:02 -05001515 return S_IWUSR;
James Bottomley9b161a42008-01-05 10:18:27 -06001516
1517 return 0;
1518}
1519
1520static struct attribute *target_attributes[] = {
Tony Jonesee959b02008-02-22 00:13:36 +01001521 &dev_attr_period.attr,
1522 &dev_attr_min_period.attr,
1523 &dev_attr_offset.attr,
1524 &dev_attr_max_offset.attr,
1525 &dev_attr_width.attr,
1526 &dev_attr_max_width.attr,
1527 &dev_attr_iu.attr,
James Bottomleyea443192009-06-13 12:19:05 -05001528 &dev_attr_max_iu.attr,
Tony Jonesee959b02008-02-22 00:13:36 +01001529 &dev_attr_dt.attr,
1530 &dev_attr_qas.attr,
James Bottomleyea443192009-06-13 12:19:05 -05001531 &dev_attr_max_qas.attr,
Tony Jonesee959b02008-02-22 00:13:36 +01001532 &dev_attr_wr_flow.attr,
1533 &dev_attr_rd_strm.attr,
1534 &dev_attr_rti.attr,
1535 &dev_attr_pcomp_en.attr,
1536 &dev_attr_hold_mcs.attr,
1537 &dev_attr_revalidate.attr,
James Bottomley9b161a42008-01-05 10:18:27 -06001538 NULL
1539};
1540
1541static struct attribute_group target_attribute_group = {
1542 .attrs = target_attributes,
1543 .is_visible = target_attribute_is_visible,
1544};
1545
1546static int spi_target_configure(struct transport_container *tc,
1547 struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +01001548 struct device *cdev)
James Bottomley9b161a42008-01-05 10:18:27 -06001549{
1550 struct kobject *kobj = &cdev->kobj;
James Bottomley9b161a42008-01-05 10:18:27 -06001551
James Bottomley352f6bb2008-03-20 20:57:02 -05001552 /* force an update based on parameters read from the device */
1553 sysfs_update_group(kobj, &target_attribute_group);
James Bottomley9b161a42008-01-05 10:18:27 -06001554
1555 return 0;
1556}
1557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558struct scsi_transport_template *
1559spi_attach_transport(struct spi_function_template *ft)
1560{
Jes Sorensen24669f752006-01-16 10:31:18 -05001561 struct spi_internal *i = kzalloc(sizeof(struct spi_internal),
1562 GFP_KERNEL);
1563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 if (unlikely(!i))
1565 return NULL;
1566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 i->t.target_attrs.ac.class = &spi_transport_class.class;
James Bottomley9b161a42008-01-05 10:18:27 -06001568 i->t.target_attrs.ac.grp = &target_attribute_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 i->t.target_attrs.ac.match = spi_target_match;
1570 transport_container_register(&i->t.target_attrs);
1571 i->t.target_size = sizeof(struct spi_transport_attrs);
1572 i->t.host_attrs.ac.class = &spi_host_class.class;
James Bottomley9b161a42008-01-05 10:18:27 -06001573 i->t.host_attrs.ac.grp = &host_attribute_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 i->t.host_attrs.ac.match = spi_host_match;
1575 transport_container_register(&i->t.host_attrs);
1576 i->t.host_size = sizeof(struct spi_host_attrs);
1577 i->f = ft;
1578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 return &i->t;
1580}
1581EXPORT_SYMBOL(spi_attach_transport);
1582
1583void spi_release_transport(struct scsi_transport_template *t)
1584{
1585 struct spi_internal *i = to_spi_internal(t);
1586
1587 transport_container_unregister(&i->t.target_attrs);
1588 transport_container_unregister(&i->t.host_attrs);
1589
1590 kfree(i);
1591}
1592EXPORT_SYMBOL(spi_release_transport);
1593
1594static __init int spi_transport_init(void)
1595{
James Bottomleya9e0edb2009-06-17 19:05:05 +00001596 int error = scsi_dev_info_add_list(SCSI_DEVINFO_SPI,
1597 "SCSI Parallel Transport Class");
1598 if (!error) {
1599 int i;
1600
1601 for (i = 0; spi_static_device_list[i].vendor; i++)
1602 scsi_dev_info_list_add_keyed(1, /* compatible */
1603 spi_static_device_list[i].vendor,
1604 spi_static_device_list[i].model,
1605 NULL,
1606 spi_static_device_list[i].flags,
1607 SCSI_DEVINFO_SPI);
1608 }
1609
1610 error = transport_class_register(&spi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 if (error)
1612 return error;
1613 error = anon_transport_class_register(&spi_device_class);
1614 return transport_class_register(&spi_host_class);
1615}
1616
1617static void __exit spi_transport_exit(void)
1618{
1619 transport_class_unregister(&spi_transport_class);
1620 anon_transport_class_unregister(&spi_device_class);
1621 transport_class_unregister(&spi_host_class);
James Bottomleya9e0edb2009-06-17 19:05:05 +00001622 scsi_dev_info_remove_list(SCSI_DEVINFO_SPI);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623}
1624
1625MODULE_AUTHOR("Martin Hicks");
1626MODULE_DESCRIPTION("SPI Transport Attributes");
1627MODULE_LICENSE("GPL");
1628
1629module_init(spi_transport_init);
1630module_exit(spi_transport_exit);