blob: bc12b5d5d676df7ab733fd23ea0ef8e929241363 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <scsi/scsi.h>
28#include "scsi_priv.h"
29#include <scsi/scsi_device.h>
30#include <scsi/scsi_host.h>
James Bottomley33aa6872005-08-28 11:31:14 -050031#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <scsi/scsi_eh.h>
33#include <scsi/scsi_transport.h>
34#include <scsi/scsi_transport_spi.h>
35
James Bottomleyd872ebe2005-08-03 15:43:52 -050036#define SPI_NUM_ATTRS 14 /* increase this if you add attributes */
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define SPI_OTHER_ATTRS 1 /* Increase this if you add "always
38 * on" attributes */
39#define SPI_HOST_ATTRS 1
40
41#define SPI_MAX_ECHO_BUFFER_SIZE 4096
42
James Bottomley949bf792005-05-01 18:58:15 -050043#define DV_LOOPS 3
44#define DV_TIMEOUT (10*HZ)
45#define DV_RETRIES 3 /* should only need at most
46 * two cc/ua clears */
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048/* Private data accessors (keep these out of the header file) */
James Bottomleydfdc58b2006-09-20 12:00:18 -040049#define spi_dv_in_progress(x) (((struct spi_transport_attrs *)&(x)->starget_data)->dv_in_progress)
Jes Sorensend158d262006-01-13 16:05:44 -080050#define spi_dv_mutex(x) (((struct spi_transport_attrs *)&(x)->starget_data)->dv_mutex)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52struct spi_internal {
53 struct scsi_transport_template t;
54 struct spi_function_template *f;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055};
56
57#define to_spi_internal(tmpl) container_of(tmpl, struct spi_internal, t)
58
59static const int ppr_to_ps[] = {
60 /* The PPR values 0-6 are reserved, fill them in when
61 * the committee defines them */
62 -1, /* 0x00 */
63 -1, /* 0x01 */
64 -1, /* 0x02 */
65 -1, /* 0x03 */
66 -1, /* 0x04 */
67 -1, /* 0x05 */
68 -1, /* 0x06 */
69 3125, /* 0x07 */
70 6250, /* 0x08 */
71 12500, /* 0x09 */
72 25000, /* 0x0a */
73 30300, /* 0x0b */
74 50000, /* 0x0c */
75};
76/* The PPR values at which you calculate the period in ns by multiplying
77 * by 4 */
78#define SPI_STATIC_PPR 0x0c
79
80static int sprint_frac(char *dest, int value, int denom)
81{
82 int frac = value % denom;
83 int result = sprintf(dest, "%d", value / denom);
84
85 if (frac == 0)
86 return result;
87 dest[result++] = '.';
88
89 do {
90 denom /= 10;
91 sprintf(dest + result, "%d", frac / denom);
92 result++;
93 frac %= denom;
94 } while (frac);
95
96 dest[result++] = '\0';
97 return result;
98}
99
James Bottomley33aa6872005-08-28 11:31:14 -0500100static int spi_execute(struct scsi_device *sdev, const void *cmd,
101 enum dma_data_direction dir,
102 void *buffer, unsigned bufflen,
103 struct scsi_sense_hdr *sshdr)
James Bottomley949bf792005-05-01 18:58:15 -0500104{
James Bottomley33aa6872005-08-28 11:31:14 -0500105 int i, result;
106 unsigned char sense[SCSI_SENSE_BUFFERSIZE];
James Bottomley949bf792005-05-01 18:58:15 -0500107
108 for(i = 0; i < DV_RETRIES; i++) {
James Bottomley33aa6872005-08-28 11:31:14 -0500109 result = scsi_execute(sdev, cmd, dir, buffer, bufflen,
110 sense, DV_TIMEOUT, /* retries */ 1,
111 REQ_FAILFAST);
112 if (result & DRIVER_SENSE) {
113 struct scsi_sense_hdr sshdr_tmp;
114 if (!sshdr)
115 sshdr = &sshdr_tmp;
James Bottomley949bf792005-05-01 18:58:15 -0500116
James Bottomley4ed381e2006-12-11 09:47:06 -0600117 if (scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE,
James Bottomley33aa6872005-08-28 11:31:14 -0500118 sshdr)
119 && sshdr->sense_key == UNIT_ATTENTION)
James Bottomley949bf792005-05-01 18:58:15 -0500120 continue;
121 }
122 break;
123 }
James Bottomley33aa6872005-08-28 11:31:14 -0500124 return result;
James Bottomley949bf792005-05-01 18:58:15 -0500125}
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127static struct {
128 enum spi_signal_type value;
129 char *name;
130} signal_types[] = {
131 { SPI_SIGNAL_UNKNOWN, "unknown" },
132 { SPI_SIGNAL_SE, "SE" },
133 { SPI_SIGNAL_LVD, "LVD" },
134 { SPI_SIGNAL_HVD, "HVD" },
135};
136
137static inline const char *spi_signal_to_string(enum spi_signal_type type)
138{
139 int i;
140
Tobias Klauser6391a112006-06-08 22:23:48 -0700141 for (i = 0; i < ARRAY_SIZE(signal_types); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 if (type == signal_types[i].value)
143 return signal_types[i].name;
144 }
145 return NULL;
146}
147static inline enum spi_signal_type spi_signal_to_value(const char *name)
148{
149 int i, len;
150
Tobias Klauser6391a112006-06-08 22:23:48 -0700151 for (i = 0; i < ARRAY_SIZE(signal_types); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 len = strlen(signal_types[i].name);
153 if (strncmp(name, signal_types[i].name, len) == 0 &&
154 (name[len] == '\n' || name[len] == '\0'))
155 return signal_types[i].value;
156 }
157 return SPI_SIGNAL_UNKNOWN;
158}
159
James Bottomleyd0a7e572005-08-14 17:09:01 -0500160static int spi_host_setup(struct transport_container *tc, struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100161 struct device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
163 struct Scsi_Host *shost = dev_to_shost(dev);
164
165 spi_signalling(shost) = SPI_SIGNAL_UNKNOWN;
166
167 return 0;
168}
169
James Bottomley9b161a42008-01-05 10:18:27 -0600170static int spi_host_configure(struct transport_container *tc,
171 struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100172 struct device *cdev);
James Bottomley9b161a42008-01-05 10:18:27 -0600173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174static DECLARE_TRANSPORT_CLASS(spi_host_class,
175 "spi_host",
176 spi_host_setup,
177 NULL,
James Bottomley9b161a42008-01-05 10:18:27 -0600178 spi_host_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180static int spi_host_match(struct attribute_container *cont,
181 struct device *dev)
182{
183 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 if (!scsi_is_host_device(dev))
186 return 0;
187
188 shost = dev_to_shost(dev);
189 if (!shost->transportt || shost->transportt->host_attrs.ac.class
190 != &spi_host_class.class)
191 return 0;
192
James Bottomley9b161a42008-01-05 10:18:27 -0600193 return &shost->transportt->host_attrs.ac == cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
James Bottomley9b161a42008-01-05 10:18:27 -0600196static int spi_target_configure(struct transport_container *tc,
197 struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100198 struct device *cdev);
James Bottomley9b161a42008-01-05 10:18:27 -0600199
James Bottomleyd0a7e572005-08-14 17:09:01 -0500200static int spi_device_configure(struct transport_container *tc,
201 struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100202 struct device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
204 struct scsi_device *sdev = to_scsi_device(dev);
205 struct scsi_target *starget = sdev->sdev_target;
206
207 /* Populate the target capability fields with the values
208 * gleaned from the device inquiry */
209
210 spi_support_sync(starget) = scsi_device_sync(sdev);
211 spi_support_wide(starget) = scsi_device_wide(sdev);
212 spi_support_dt(starget) = scsi_device_dt(sdev);
213 spi_support_dt_only(starget) = scsi_device_dt_only(sdev);
214 spi_support_ius(starget) = scsi_device_ius(sdev);
215 spi_support_qas(starget) = scsi_device_qas(sdev);
216
217 return 0;
218}
219
James Bottomleyd0a7e572005-08-14 17:09:01 -0500220static int spi_setup_transport_attrs(struct transport_container *tc,
221 struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100222 struct device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
224 struct scsi_target *starget = to_scsi_target(dev);
225
226 spi_period(starget) = -1; /* illegal value */
James Bottomley 62a86122005-05-06 18:05:20 -0500227 spi_min_period(starget) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 spi_offset(starget) = 0; /* async */
James Bottomley 62a86122005-05-06 18:05:20 -0500229 spi_max_offset(starget) = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 spi_width(starget) = 0; /* narrow */
James Bottomley 62a86122005-05-06 18:05:20 -0500231 spi_max_width(starget) = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 spi_iu(starget) = 0; /* no IU */
233 spi_dt(starget) = 0; /* ST */
234 spi_qas(starget) = 0;
235 spi_wr_flow(starget) = 0;
236 spi_rd_strm(starget) = 0;
237 spi_rti(starget) = 0;
238 spi_pcomp_en(starget) = 0;
James Bottomleyd872ebe2005-08-03 15:43:52 -0500239 spi_hold_mcs(starget) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 spi_dv_pending(starget) = 0;
James Bottomleydfdc58b2006-09-20 12:00:18 -0400241 spi_dv_in_progress(starget) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 spi_initial_dv(starget) = 0;
Jes Sorensend158d262006-01-13 16:05:44 -0800243 mutex_init(&spi_dv_mutex(starget));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245 return 0;
246}
247
James Bottomley 62a86122005-05-06 18:05:20 -0500248#define spi_transport_show_simple(field, format_string) \
249 \
250static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100251show_spi_transport_##field(struct device *dev, \
252 struct device_attribute *attr, char *buf) \
James Bottomley 62a86122005-05-06 18:05:20 -0500253{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100254 struct scsi_target *starget = transport_class_to_starget(dev); \
James Bottomley 62a86122005-05-06 18:05:20 -0500255 struct spi_transport_attrs *tp; \
256 \
257 tp = (struct spi_transport_attrs *)&starget->starget_data; \
258 return snprintf(buf, 20, format_string, tp->field); \
259}
260
261#define spi_transport_store_simple(field, format_string) \
262 \
263static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100264store_spi_transport_##field(struct device *dev, \
265 struct device_attribute *attr, \
266 const char *buf, size_t count) \
James Bottomley 62a86122005-05-06 18:05:20 -0500267{ \
268 int val; \
Tony Jonesee959b02008-02-22 00:13:36 +0100269 struct scsi_target *starget = transport_class_to_starget(dev); \
James Bottomley 62a86122005-05-06 18:05:20 -0500270 struct spi_transport_attrs *tp; \
271 \
272 tp = (struct spi_transport_attrs *)&starget->starget_data; \
273 val = simple_strtoul(buf, NULL, 0); \
274 tp->field = val; \
275 return count; \
276}
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278#define spi_transport_show_function(field, format_string) \
279 \
280static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100281show_spi_transport_##field(struct device *dev, \
282 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100284 struct scsi_target *starget = transport_class_to_starget(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
286 struct spi_transport_attrs *tp; \
287 struct spi_internal *i = to_spi_internal(shost->transportt); \
288 tp = (struct spi_transport_attrs *)&starget->starget_data; \
289 if (i->f->get_##field) \
290 i->f->get_##field(starget); \
291 return snprintf(buf, 20, format_string, tp->field); \
292}
293
294#define spi_transport_store_function(field, format_string) \
295static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100296store_spi_transport_##field(struct device *dev, \
297 struct device_attribute *attr, \
298 const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{ \
300 int val; \
Tony Jonesee959b02008-02-22 00:13:36 +0100301 struct scsi_target *starget = transport_class_to_starget(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
303 struct spi_internal *i = to_spi_internal(shost->transportt); \
304 \
James Bottomley9b161a42008-01-05 10:18:27 -0600305 if (!i->f->set_##field) \
306 return -EINVAL; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 val = simple_strtoul(buf, NULL, 0); \
James Bottomley9b161a42008-01-05 10:18:27 -0600308 i->f->set_##field(starget, val); \
James Bottomley 62a86122005-05-06 18:05:20 -0500309 return count; \
310}
311
312#define spi_transport_store_max(field, format_string) \
313static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +0100314store_spi_transport_##field(struct device *dev, \
315 struct device_attribute *attr, \
316 const char *buf, size_t count) \
James Bottomley 62a86122005-05-06 18:05:20 -0500317{ \
318 int val; \
Tony Jonesee959b02008-02-22 00:13:36 +0100319 struct scsi_target *starget = transport_class_to_starget(dev); \
James Bottomley 62a86122005-05-06 18:05:20 -0500320 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
321 struct spi_internal *i = to_spi_internal(shost->transportt); \
322 struct spi_transport_attrs *tp \
323 = (struct spi_transport_attrs *)&starget->starget_data; \
324 \
James Bottomley9b161a42008-01-05 10:18:27 -0600325 if (i->f->set_##field) \
326 return -EINVAL; \
James Bottomley 62a86122005-05-06 18:05:20 -0500327 val = simple_strtoul(buf, NULL, 0); \
328 if (val > tp->max_##field) \
329 val = tp->max_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 i->f->set_##field(starget, val); \
331 return count; \
332}
333
334#define spi_transport_rd_attr(field, format_string) \
335 spi_transport_show_function(field, format_string) \
336 spi_transport_store_function(field, format_string) \
Tony Jonesee959b02008-02-22 00:13:36 +0100337static DEVICE_ATTR(field, S_IRUGO, \
338 show_spi_transport_##field, \
339 store_spi_transport_##field);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
James Bottomley 62a86122005-05-06 18:05:20 -0500341#define spi_transport_simple_attr(field, format_string) \
342 spi_transport_show_simple(field, format_string) \
343 spi_transport_store_simple(field, format_string) \
Tony Jonesee959b02008-02-22 00:13:36 +0100344static DEVICE_ATTR(field, S_IRUGO, \
345 show_spi_transport_##field, \
346 store_spi_transport_##field);
James Bottomley 62a86122005-05-06 18:05:20 -0500347
348#define spi_transport_max_attr(field, format_string) \
349 spi_transport_show_function(field, format_string) \
350 spi_transport_store_max(field, format_string) \
351 spi_transport_simple_attr(max_##field, format_string) \
Tony Jonesee959b02008-02-22 00:13:36 +0100352static DEVICE_ATTR(field, S_IRUGO, \
353 show_spi_transport_##field, \
354 store_spi_transport_##field);
James Bottomley 62a86122005-05-06 18:05:20 -0500355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356/* The Parallel SCSI Tranport Attributes: */
James Bottomley 62a86122005-05-06 18:05:20 -0500357spi_transport_max_attr(offset, "%d\n");
358spi_transport_max_attr(width, "%d\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359spi_transport_rd_attr(iu, "%d\n");
360spi_transport_rd_attr(dt, "%d\n");
361spi_transport_rd_attr(qas, "%d\n");
362spi_transport_rd_attr(wr_flow, "%d\n");
363spi_transport_rd_attr(rd_strm, "%d\n");
364spi_transport_rd_attr(rti, "%d\n");
365spi_transport_rd_attr(pcomp_en, "%d\n");
James Bottomleyd872ebe2005-08-03 15:43:52 -0500366spi_transport_rd_attr(hold_mcs, "%d\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
gregkh@suse.de9a881f12005-03-25 15:52:00 -0800368/* we only care about the first child device so we return 1 */
369static int child_iter(struct device *dev, void *data)
370{
371 struct scsi_device *sdev = to_scsi_device(dev);
372
373 spi_dv_device(sdev);
374 return 1;
375}
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100378store_spi_revalidate(struct device *dev, struct device_attribute *attr,
379 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Tony Jonesee959b02008-02-22 00:13:36 +0100381 struct scsi_target *starget = transport_class_to_starget(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
gregkh@suse.de9a881f12005-03-25 15:52:00 -0800383 device_for_each_child(&starget->dev, NULL, child_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return count;
385}
Tony Jonesee959b02008-02-22 00:13:36 +0100386static DEVICE_ATTR(revalidate, S_IWUSR, NULL, store_spi_revalidate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388/* Translate the period into ns according to the current spec
389 * for SDTR/PPR messages */
Matthew Wilcoxef725822005-12-15 16:22:01 -0500390static int period_to_str(char *buf, int period)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 int len, picosec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
James Bottomley 62a86122005-05-06 18:05:20 -0500394 if (period < 0 || period > 0xff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 picosec = -1;
James Bottomley 62a86122005-05-06 18:05:20 -0500396 } else if (period <= SPI_STATIC_PPR) {
397 picosec = ppr_to_ps[period];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 } else {
James Bottomley 62a86122005-05-06 18:05:20 -0500399 picosec = period * 4000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
401
402 if (picosec == -1) {
403 len = sprintf(buf, "reserved");
404 } else {
405 len = sprint_frac(buf, picosec, 1000);
406 }
407
Matthew Wilcoxef725822005-12-15 16:22:01 -0500408 return len;
409}
410
411static ssize_t
Matthew Wilcoxea697e42006-02-07 08:01:02 -0700412show_spi_transport_period_helper(char *buf, int period)
Matthew Wilcoxef725822005-12-15 16:22:01 -0500413{
414 int len = period_to_str(buf, period);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 buf[len++] = '\n';
416 buf[len] = '\0';
417 return len;
418}
419
420static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100421store_spi_transport_period_helper(struct device *dev, const char *buf,
James Bottomley 62a86122005-05-06 18:05:20 -0500422 size_t count, int *periodp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 int j, picosec, period = -1;
425 char *endp;
426
427 picosec = simple_strtoul(buf, &endp, 10) * 1000;
428 if (*endp == '.') {
429 int mult = 100;
430 do {
431 endp++;
432 if (!isdigit(*endp))
433 break;
434 picosec += (*endp - '0') * mult;
435 mult /= 10;
436 } while (mult > 0);
437 }
438
439 for (j = 0; j <= SPI_STATIC_PPR; j++) {
440 if (ppr_to_ps[j] < picosec)
441 continue;
442 period = j;
443 break;
444 }
445
446 if (period == -1)
447 period = picosec / 4000;
448
449 if (period > 0xff)
450 period = 0xff;
451
James Bottomley 62a86122005-05-06 18:05:20 -0500452 *periodp = period;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 return count;
455}
456
James Bottomley 62a86122005-05-06 18:05:20 -0500457static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100458show_spi_transport_period(struct device *dev,
459 struct device_attribute *attr, char *buf)
James Bottomley 62a86122005-05-06 18:05:20 -0500460{
Tony Jonesee959b02008-02-22 00:13:36 +0100461 struct scsi_target *starget = transport_class_to_starget(dev);
James Bottomley 62a86122005-05-06 18:05:20 -0500462 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
463 struct spi_internal *i = to_spi_internal(shost->transportt);
464 struct spi_transport_attrs *tp =
465 (struct spi_transport_attrs *)&starget->starget_data;
466
467 if (i->f->get_period)
468 i->f->get_period(starget);
469
Matthew Wilcoxea697e42006-02-07 08:01:02 -0700470 return show_spi_transport_period_helper(buf, tp->period);
James Bottomley 62a86122005-05-06 18:05:20 -0500471}
472
473static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100474store_spi_transport_period(struct device *cdev, struct device_attribute *attr,
475 const char *buf, size_t count)
James Bottomley 62a86122005-05-06 18:05:20 -0500476{
477 struct scsi_target *starget = transport_class_to_starget(cdev);
478 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
479 struct spi_internal *i = to_spi_internal(shost->transportt);
480 struct spi_transport_attrs *tp =
481 (struct spi_transport_attrs *)&starget->starget_data;
482 int period, retval;
483
James Bottomley9b161a42008-01-05 10:18:27 -0600484 if (!i->f->set_period)
485 return -EINVAL;
486
James Bottomley 62a86122005-05-06 18:05:20 -0500487 retval = store_spi_transport_period_helper(cdev, buf, count, &period);
488
489 if (period < tp->min_period)
490 period = tp->min_period;
491
492 i->f->set_period(starget, period);
493
494 return retval;
495}
496
Tony Jonesee959b02008-02-22 00:13:36 +0100497static DEVICE_ATTR(period, S_IRUGO,
498 show_spi_transport_period,
499 store_spi_transport_period);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
James Bottomley 62a86122005-05-06 18:05:20 -0500501static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100502show_spi_transport_min_period(struct device *cdev,
503 struct device_attribute *attr, char *buf)
James Bottomley 62a86122005-05-06 18:05:20 -0500504{
505 struct scsi_target *starget = transport_class_to_starget(cdev);
James Bottomley9b161a42008-01-05 10:18:27 -0600506 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
507 struct spi_internal *i = to_spi_internal(shost->transportt);
James Bottomley 62a86122005-05-06 18:05:20 -0500508 struct spi_transport_attrs *tp =
509 (struct spi_transport_attrs *)&starget->starget_data;
510
James Bottomley9b161a42008-01-05 10:18:27 -0600511 if (!i->f->set_period)
512 return -EINVAL;
513
Matthew Wilcoxea697e42006-02-07 08:01:02 -0700514 return show_spi_transport_period_helper(buf, tp->min_period);
James Bottomley 62a86122005-05-06 18:05:20 -0500515}
516
517static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100518store_spi_transport_min_period(struct device *cdev,
519 struct device_attribute *attr,
520 const char *buf, size_t count)
James Bottomley 62a86122005-05-06 18:05:20 -0500521{
522 struct scsi_target *starget = transport_class_to_starget(cdev);
523 struct spi_transport_attrs *tp =
524 (struct spi_transport_attrs *)&starget->starget_data;
525
526 return store_spi_transport_period_helper(cdev, buf, count,
527 &tp->min_period);
528}
529
530
Tony Jonesee959b02008-02-22 00:13:36 +0100531static DEVICE_ATTR(min_period, S_IRUGO,
532 show_spi_transport_min_period,
533 store_spi_transport_min_period);
James Bottomley 62a86122005-05-06 18:05:20 -0500534
535
Tony Jonesee959b02008-02-22 00:13:36 +0100536static ssize_t show_spi_host_signalling(struct device *cdev,
537 struct device_attribute *attr,
538 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
540 struct Scsi_Host *shost = transport_class_to_shost(cdev);
541 struct spi_internal *i = to_spi_internal(shost->transportt);
542
543 if (i->f->get_signalling)
544 i->f->get_signalling(shost);
545
546 return sprintf(buf, "%s\n", spi_signal_to_string(spi_signalling(shost)));
547}
Tony Jonesee959b02008-02-22 00:13:36 +0100548static ssize_t store_spi_host_signalling(struct device *dev,
549 struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 const char *buf, size_t count)
551{
Tony Jonesee959b02008-02-22 00:13:36 +0100552 struct Scsi_Host *shost = transport_class_to_shost(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 struct spi_internal *i = to_spi_internal(shost->transportt);
554 enum spi_signal_type type = spi_signal_to_value(buf);
555
James Bottomley9b161a42008-01-05 10:18:27 -0600556 if (!i->f->set_signalling)
557 return -EINVAL;
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if (type != SPI_SIGNAL_UNKNOWN)
560 i->f->set_signalling(shost, type);
561
562 return count;
563}
Tony Jonesee959b02008-02-22 00:13:36 +0100564static DEVICE_ATTR(signalling, S_IRUGO,
565 show_spi_host_signalling,
566 store_spi_host_signalling);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568#define DV_SET(x, y) \
569 if(i->f->set_##x) \
570 i->f->set_##x(sdev->sdev_target, y)
571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572enum spi_compare_returns {
573 SPI_COMPARE_SUCCESS,
574 SPI_COMPARE_FAILURE,
575 SPI_COMPARE_SKIP_TEST,
576};
577
578
579/* This is for read/write Domain Validation: If the device supports
580 * an echo buffer, we do read/write tests to it */
581static enum spi_compare_returns
James Bottomley33aa6872005-08-28 11:31:14 -0500582spi_dv_device_echo_buffer(struct scsi_device *sdev, u8 *buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 u8 *ptr, const int retries)
584{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 int len = ptr - buffer;
James Bottomley33aa6872005-08-28 11:31:14 -0500586 int j, k, r, result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 unsigned int pattern = 0x0000ffff;
James Bottomley33aa6872005-08-28 11:31:14 -0500588 struct scsi_sense_hdr sshdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 const char spi_write_buffer[] = {
591 WRITE_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
592 };
593 const char spi_read_buffer[] = {
594 READ_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
595 };
596
597 /* set up the pattern buffer. Doesn't matter if we spill
598 * slightly beyond since that's where the read buffer is */
599 for (j = 0; j < len; ) {
600
601 /* fill the buffer with counting (test a) */
602 for ( ; j < min(len, 32); j++)
603 buffer[j] = j;
604 k = j;
605 /* fill the buffer with alternating words of 0x0 and
606 * 0xffff (test b) */
607 for ( ; j < min(len, k + 32); j += 2) {
608 u16 *word = (u16 *)&buffer[j];
609
610 *word = (j & 0x02) ? 0x0000 : 0xffff;
611 }
612 k = j;
613 /* fill with crosstalk (alternating 0x5555 0xaaa)
614 * (test c) */
615 for ( ; j < min(len, k + 32); j += 2) {
616 u16 *word = (u16 *)&buffer[j];
617
618 *word = (j & 0x02) ? 0x5555 : 0xaaaa;
619 }
620 k = j;
621 /* fill with shifting bits (test d) */
622 for ( ; j < min(len, k + 32); j += 4) {
623 u32 *word = (unsigned int *)&buffer[j];
624 u32 roll = (pattern & 0x80000000) ? 1 : 0;
625
626 *word = pattern;
627 pattern = (pattern << 1) | roll;
628 }
629 /* don't bother with random data (test e) */
630 }
631
632 for (r = 0; r < retries; r++) {
James Bottomley33aa6872005-08-28 11:31:14 -0500633 result = spi_execute(sdev, spi_write_buffer, DMA_TO_DEVICE,
634 buffer, len, &sshdr);
635 if(result || !scsi_device_online(sdev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 scsi_device_set_state(sdev, SDEV_QUIESCE);
James Bottomley33aa6872005-08-28 11:31:14 -0500638 if (scsi_sense_valid(&sshdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 && sshdr.sense_key == ILLEGAL_REQUEST
640 /* INVALID FIELD IN CDB */
641 && sshdr.asc == 0x24 && sshdr.ascq == 0x00)
642 /* This would mean that the drive lied
643 * to us about supporting an echo
644 * buffer (unfortunately some Western
645 * Digital drives do precisely this)
646 */
647 return SPI_COMPARE_SKIP_TEST;
648
649
James Bottomley9ccfc752005-10-02 11:45:08 -0500650 sdev_printk(KERN_ERR, sdev, "Write Buffer failure %x\n", result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return SPI_COMPARE_FAILURE;
652 }
653
654 memset(ptr, 0, len);
James Bottomley33aa6872005-08-28 11:31:14 -0500655 spi_execute(sdev, spi_read_buffer, DMA_FROM_DEVICE,
656 ptr, len, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 scsi_device_set_state(sdev, SDEV_QUIESCE);
658
659 if (memcmp(buffer, ptr, len) != 0)
660 return SPI_COMPARE_FAILURE;
661 }
662 return SPI_COMPARE_SUCCESS;
663}
664
665/* This is for the simplest form of Domain Validation: a read test
666 * on the inquiry data from the device */
667static enum spi_compare_returns
James Bottomley33aa6872005-08-28 11:31:14 -0500668spi_dv_device_compare_inquiry(struct scsi_device *sdev, u8 *buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 u8 *ptr, const int retries)
670{
James Bottomley33aa6872005-08-28 11:31:14 -0500671 int r, result;
672 const int len = sdev->inquiry_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 const char spi_inquiry[] = {
674 INQUIRY, 0, 0, 0, len, 0
675 };
676
677 for (r = 0; r < retries; r++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 memset(ptr, 0, len);
679
James Bottomley33aa6872005-08-28 11:31:14 -0500680 result = spi_execute(sdev, spi_inquiry, DMA_FROM_DEVICE,
681 ptr, len, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
James Bottomley33aa6872005-08-28 11:31:14 -0500683 if(result || !scsi_device_online(sdev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 scsi_device_set_state(sdev, SDEV_QUIESCE);
685 return SPI_COMPARE_FAILURE;
686 }
687
688 /* If we don't have the inquiry data already, the
689 * first read gets it */
690 if (ptr == buffer) {
691 ptr += len;
692 --r;
693 continue;
694 }
695
696 if (memcmp(buffer, ptr, len) != 0)
697 /* failure */
698 return SPI_COMPARE_FAILURE;
699 }
700 return SPI_COMPARE_SUCCESS;
701}
702
703static enum spi_compare_returns
James Bottomley33aa6872005-08-28 11:31:14 -0500704spi_dv_retrain(struct scsi_device *sdev, u8 *buffer, u8 *ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 enum spi_compare_returns
James Bottomley33aa6872005-08-28 11:31:14 -0500706 (*compare_fn)(struct scsi_device *, u8 *, u8 *, int))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
James Bottomley33aa6872005-08-28 11:31:14 -0500708 struct spi_internal *i = to_spi_internal(sdev->host->transportt);
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500709 struct scsi_target *starget = sdev->sdev_target;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 int period = 0, prevperiod = 0;
711 enum spi_compare_returns retval;
712
713
714 for (;;) {
715 int newperiod;
James Bottomley33aa6872005-08-28 11:31:14 -0500716 retval = compare_fn(sdev, buffer, ptr, DV_LOOPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 if (retval == SPI_COMPARE_SUCCESS
719 || retval == SPI_COMPARE_SKIP_TEST)
720 break;
721
722 /* OK, retrain, fallback */
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500723 if (i->f->get_iu)
724 i->f->get_iu(starget);
725 if (i->f->get_qas)
726 i->f->get_qas(starget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (i->f->get_period)
728 i->f->get_period(sdev->sdev_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500730 /* Here's the fallback sequence; first try turning off
731 * IU, then QAS (if we can control them), then finally
732 * fall down the periods */
733 if (i->f->set_iu && spi_iu(starget)) {
James Bottomley9ccfc752005-10-02 11:45:08 -0500734 starget_printk(KERN_ERR, starget, "Domain Validation Disabing Information Units\n");
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500735 DV_SET(iu, 0);
736 } else if (i->f->set_qas && spi_qas(starget)) {
James Bottomley9ccfc752005-10-02 11:45:08 -0500737 starget_printk(KERN_ERR, starget, "Domain Validation Disabing Quick Arbitration and Selection\n");
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500738 DV_SET(qas, 0);
739 } else {
740 newperiod = spi_period(starget);
741 period = newperiod > period ? newperiod : period;
742 if (period < 0x0d)
743 period++;
744 else
745 period += period >> 1;
746
747 if (unlikely(period > 0xff || period == prevperiod)) {
748 /* Total failure; set to async and return */
James Bottomley9ccfc752005-10-02 11:45:08 -0500749 starget_printk(KERN_ERR, starget, "Domain Validation Failure, dropping back to Asynchronous\n");
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500750 DV_SET(offset, 0);
751 return SPI_COMPARE_FAILURE;
752 }
James Bottomley9ccfc752005-10-02 11:45:08 -0500753 starget_printk(KERN_ERR, starget, "Domain Validation detected failure, dropping back\n");
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500754 DV_SET(period, period);
755 prevperiod = period;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
758 return retval;
759}
760
761static int
James Bottomley33aa6872005-08-28 11:31:14 -0500762spi_dv_device_get_echo_buffer(struct scsi_device *sdev, u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
James Bottomley33aa6872005-08-28 11:31:14 -0500764 int l, result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 /* first off do a test unit ready. This can error out
767 * because of reservations or some other reason. If it
768 * fails, the device won't let us write to the echo buffer
769 * so just return failure */
770
771 const char spi_test_unit_ready[] = {
772 TEST_UNIT_READY, 0, 0, 0, 0, 0
773 };
774
775 const char spi_read_buffer_descriptor[] = {
776 READ_BUFFER, 0x0b, 0, 0, 0, 0, 0, 0, 4, 0
777 };
778
779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 /* We send a set of three TURs to clear any outstanding
781 * unit attention conditions if they exist (Otherwise the
782 * buffer tests won't be happy). If the TUR still fails
783 * (reservation conflict, device not ready, etc) just
784 * skip the write tests */
785 for (l = 0; ; l++) {
James Bottomley33aa6872005-08-28 11:31:14 -0500786 result = spi_execute(sdev, spi_test_unit_ready, DMA_NONE,
787 NULL, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
James Bottomley33aa6872005-08-28 11:31:14 -0500789 if(result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 if(l >= 3)
791 return 0;
792 } else {
793 /* TUR succeeded */
794 break;
795 }
796 }
797
James Bottomley33aa6872005-08-28 11:31:14 -0500798 result = spi_execute(sdev, spi_read_buffer_descriptor,
799 DMA_FROM_DEVICE, buffer, 4, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
James Bottomley33aa6872005-08-28 11:31:14 -0500801 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 /* Device has no echo buffer */
803 return 0;
804
805 return buffer[3] + ((buffer[2] & 0x1f) << 8);
806}
807
808static void
James Bottomley33aa6872005-08-28 11:31:14 -0500809spi_dv_device_internal(struct scsi_device *sdev, u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
James Bottomley33aa6872005-08-28 11:31:14 -0500811 struct spi_internal *i = to_spi_internal(sdev->host->transportt);
James Bottomley 62a86122005-05-06 18:05:20 -0500812 struct scsi_target *starget = sdev->sdev_target;
James Bottomley60eef252006-06-10 10:51:23 -0500813 struct Scsi_Host *shost = sdev->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 int len = sdev->inquiry_len;
James Bottomley23028272007-09-22 08:40:09 -0500815 int min_period = spi_min_period(starget);
816 int max_width = spi_max_width(starget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 /* first set us up for narrow async */
818 DV_SET(offset, 0);
819 DV_SET(width, 0);
James Bottomley23028272007-09-22 08:40:09 -0500820
James Bottomley33aa6872005-08-28 11:31:14 -0500821 if (spi_dv_device_compare_inquiry(sdev, buffer, buffer, DV_LOOPS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 != SPI_COMPARE_SUCCESS) {
James Bottomley9ccfc752005-10-02 11:45:08 -0500823 starget_printk(KERN_ERR, starget, "Domain Validation Initial Inquiry Failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 /* FIXME: should probably offline the device here? */
825 return;
826 }
827
James Bottomley23028272007-09-22 08:40:09 -0500828 if (!scsi_device_wide(sdev)) {
829 spi_max_width(starget) = 0;
830 max_width = 0;
831 }
832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 /* test width */
James Bottomley23028272007-09-22 08:40:09 -0500834 if (i->f->set_width && max_width) {
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500835 i->f->set_width(starget, 1);
James Bottomley 62a86122005-05-06 18:05:20 -0500836
James Bottomley33aa6872005-08-28 11:31:14 -0500837 if (spi_dv_device_compare_inquiry(sdev, buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 buffer + len,
839 DV_LOOPS)
840 != SPI_COMPARE_SUCCESS) {
James Bottomley9ccfc752005-10-02 11:45:08 -0500841 starget_printk(KERN_ERR, starget, "Wide Transfers Fail\n");
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500842 i->f->set_width(starget, 0);
James Bottomley23028272007-09-22 08:40:09 -0500843 /* Make sure we don't force wide back on by asking
844 * for a transfer period that requires it */
845 max_width = 0;
846 if (min_period < 10)
847 min_period = 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 }
849 }
850
851 if (!i->f->set_period)
852 return;
853
854 /* device can't handle synchronous */
James Bottomleyeb1dd682005-07-02 12:22:01 -0400855 if (!scsi_device_sync(sdev) && !scsi_device_dt(sdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 return;
857
James Bottomley349cd7c2005-11-28 15:41:58 -0600858 /* len == -1 is the signal that we need to ascertain the
859 * presence of an echo buffer before trying to use it. len ==
860 * 0 means we don't have an echo buffer */
861 len = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863 retry:
864
865 /* now set up to the maximum */
James Bottomley 62a86122005-05-06 18:05:20 -0500866 DV_SET(offset, spi_max_offset(starget));
James Bottomley23028272007-09-22 08:40:09 -0500867 DV_SET(period, min_period);
868
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500869 /* try QAS requests; this should be harmless to set if the
870 * target supports it */
James Bottomleydfdc58b2006-09-20 12:00:18 -0400871 if (scsi_device_qas(sdev)) {
James Bottomleyeb1dd682005-07-02 12:22:01 -0400872 DV_SET(qas, 1);
James Bottomleydfdc58b2006-09-20 12:00:18 -0400873 } else {
874 DV_SET(qas, 0);
875 }
876
James Bottomley23028272007-09-22 08:40:09 -0500877 if (scsi_device_ius(sdev) && min_period < 9) {
James Bottomleydfdc58b2006-09-20 12:00:18 -0400878 /* This u320 (or u640). Set IU transfers */
James Bottomleyeb1dd682005-07-02 12:22:01 -0400879 DV_SET(iu, 1);
James Bottomleydfdc58b2006-09-20 12:00:18 -0400880 /* Then set the optional parameters */
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500881 DV_SET(rd_strm, 1);
882 DV_SET(wr_flow, 1);
883 DV_SET(rti, 1);
James Bottomley23028272007-09-22 08:40:09 -0500884 if (min_period == 8)
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500885 DV_SET(pcomp_en, 1);
James Bottomleydfdc58b2006-09-20 12:00:18 -0400886 } else {
887 DV_SET(iu, 0);
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500888 }
James Bottomleydfdc58b2006-09-20 12:00:18 -0400889
James Bottomley60eef252006-06-10 10:51:23 -0500890 /* now that we've done all this, actually check the bus
891 * signal type (if known). Some devices are stupid on
892 * a SE bus and still claim they can try LVD only settings */
893 if (i->f->get_signalling)
894 i->f->get_signalling(shost);
895 if (spi_signalling(shost) == SPI_SIGNAL_SE ||
James Bottomleydfdc58b2006-09-20 12:00:18 -0400896 spi_signalling(shost) == SPI_SIGNAL_HVD ||
897 !scsi_device_dt(sdev)) {
James Bottomley60eef252006-06-10 10:51:23 -0500898 DV_SET(dt, 0);
James Bottomleydfdc58b2006-09-20 12:00:18 -0400899 } else {
900 DV_SET(dt, 1);
901 }
James Bottomley23028272007-09-22 08:40:09 -0500902 /* set width last because it will pull all the other
903 * parameters down to required values */
904 DV_SET(width, max_width);
905
James Bottomley349cd7c2005-11-28 15:41:58 -0600906 /* Do the read only INQUIRY tests */
907 spi_dv_retrain(sdev, buffer, buffer + sdev->inquiry_len,
908 spi_dv_device_compare_inquiry);
909 /* See if we actually managed to negotiate and sustain DT */
910 if (i->f->get_dt)
911 i->f->get_dt(starget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
James Bottomley349cd7c2005-11-28 15:41:58 -0600913 /* see if the device has an echo buffer. If it does we can do
914 * the SPI pattern write tests. Because of some broken
915 * devices, we *only* try this on a device that has actually
916 * negotiated DT */
917
918 if (len == -1 && spi_dt(starget))
919 len = spi_dv_device_get_echo_buffer(sdev, buffer);
920
921 if (len <= 0) {
James Bottomley9ccfc752005-10-02 11:45:08 -0500922 starget_printk(KERN_INFO, starget, "Domain Validation skipping write tests\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 return;
924 }
925
926 if (len > SPI_MAX_ECHO_BUFFER_SIZE) {
James Bottomley9ccfc752005-10-02 11:45:08 -0500927 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 -0700928 len = SPI_MAX_ECHO_BUFFER_SIZE;
929 }
930
James Bottomley33aa6872005-08-28 11:31:14 -0500931 if (spi_dv_retrain(sdev, buffer, buffer + len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 spi_dv_device_echo_buffer)
933 == SPI_COMPARE_SKIP_TEST) {
934 /* OK, the stupid drive can't do a write echo buffer
935 * test after all, fall back to the read tests */
936 len = 0;
937 goto retry;
938 }
939}
940
941
942/** spi_dv_device - Do Domain Validation on the device
943 * @sdev: scsi device to validate
944 *
945 * Performs the domain validation on the given device in the
946 * current execution thread. Since DV operations may sleep,
947 * the current thread must have user context. Also no SCSI
948 * related locks that would deadlock I/O issued by the DV may
949 * be held.
950 */
951void
952spi_dv_device(struct scsi_device *sdev)
953{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 struct scsi_target *starget = sdev->sdev_target;
955 u8 *buffer;
956 const int len = SPI_MAX_ECHO_BUFFER_SIZE*2;
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 if (unlikely(scsi_device_get(sdev)))
James Bottomley33aa6872005-08-28 11:31:14 -0500959 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
James Bottomleydfdc58b2006-09-20 12:00:18 -0400961 if (unlikely(spi_dv_in_progress(starget)))
962 return;
963 spi_dv_in_progress(starget) = 1;
964
Jes Sorensen24669f752006-01-16 10:31:18 -0500965 buffer = kzalloc(len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967 if (unlikely(!buffer))
968 goto out_put;
969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 /* We need to verify that the actual device will quiesce; the
971 * later target quiesce is just a nice to have */
972 if (unlikely(scsi_device_quiesce(sdev)))
973 goto out_free;
974
975 scsi_target_quiesce(starget);
976
977 spi_dv_pending(starget) = 1;
Jes Sorensend158d262006-01-13 16:05:44 -0800978 mutex_lock(&spi_dv_mutex(starget));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
James Bottomley9ccfc752005-10-02 11:45:08 -0500980 starget_printk(KERN_INFO, starget, "Beginning Domain Validation\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
James Bottomley33aa6872005-08-28 11:31:14 -0500982 spi_dv_device_internal(sdev, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
James Bottomley9ccfc752005-10-02 11:45:08 -0500984 starget_printk(KERN_INFO, starget, "Ending Domain Validation\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Jes Sorensend158d262006-01-13 16:05:44 -0800986 mutex_unlock(&spi_dv_mutex(starget));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 spi_dv_pending(starget) = 0;
988
989 scsi_target_resume(starget);
990
991 spi_initial_dv(starget) = 1;
992
993 out_free:
994 kfree(buffer);
995 out_put:
James Bottomleydfdc58b2006-09-20 12:00:18 -0400996 spi_dv_in_progress(starget) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 scsi_device_put(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998}
999EXPORT_SYMBOL(spi_dv_device);
1000
1001struct work_queue_wrapper {
1002 struct work_struct work;
1003 struct scsi_device *sdev;
1004};
1005
1006static void
David Howellsc4028952006-11-22 14:57:56 +00001007spi_dv_device_work_wrapper(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
David Howellsc4028952006-11-22 14:57:56 +00001009 struct work_queue_wrapper *wqw =
1010 container_of(work, struct work_queue_wrapper, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 struct scsi_device *sdev = wqw->sdev;
1012
1013 kfree(wqw);
1014 spi_dv_device(sdev);
1015 spi_dv_pending(sdev->sdev_target) = 0;
1016 scsi_device_put(sdev);
1017}
1018
1019
1020/**
1021 * spi_schedule_dv_device - schedule domain validation to occur on the device
1022 * @sdev: The device to validate
1023 *
1024 * Identical to spi_dv_device() above, except that the DV will be
1025 * scheduled to occur in a workqueue later. All memory allocations
1026 * are atomic, so may be called from any context including those holding
1027 * SCSI locks.
1028 */
1029void
1030spi_schedule_dv_device(struct scsi_device *sdev)
1031{
1032 struct work_queue_wrapper *wqw =
1033 kmalloc(sizeof(struct work_queue_wrapper), GFP_ATOMIC);
1034
1035 if (unlikely(!wqw))
1036 return;
1037
1038 if (unlikely(spi_dv_pending(sdev->sdev_target))) {
1039 kfree(wqw);
1040 return;
1041 }
1042 /* Set pending early (dv_device doesn't check it, only sets it) */
1043 spi_dv_pending(sdev->sdev_target) = 1;
1044 if (unlikely(scsi_device_get(sdev))) {
1045 kfree(wqw);
1046 spi_dv_pending(sdev->sdev_target) = 0;
1047 return;
1048 }
1049
David Howellsc4028952006-11-22 14:57:56 +00001050 INIT_WORK(&wqw->work, spi_dv_device_work_wrapper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 wqw->sdev = sdev;
1052
1053 schedule_work(&wqw->work);
1054}
1055EXPORT_SYMBOL(spi_schedule_dv_device);
1056
1057/**
1058 * spi_display_xfer_agreement - Print the current target transfer agreement
1059 * @starget: The target for which to display the agreement
1060 *
1061 * Each SPI port is required to maintain a transfer agreement for each
1062 * other port on the bus. This function prints a one-line summary of
1063 * the current agreement; more detailed information is available in sysfs.
1064 */
1065void spi_display_xfer_agreement(struct scsi_target *starget)
1066{
1067 struct spi_transport_attrs *tp;
1068 tp = (struct spi_transport_attrs *)&starget->starget_data;
1069
1070 if (tp->offset > 0 && tp->period > 0) {
1071 unsigned int picosec, kb100;
1072 char *scsi = "FAST-?";
1073 char tmp[8];
1074
1075 if (tp->period <= SPI_STATIC_PPR) {
1076 picosec = ppr_to_ps[tp->period];
1077 switch (tp->period) {
1078 case 7: scsi = "FAST-320"; break;
1079 case 8: scsi = "FAST-160"; break;
1080 case 9: scsi = "FAST-80"; break;
1081 case 10:
1082 case 11: scsi = "FAST-40"; break;
1083 case 12: scsi = "FAST-20"; break;
1084 }
1085 } else {
1086 picosec = tp->period * 4000;
1087 if (tp->period < 25)
1088 scsi = "FAST-20";
1089 else if (tp->period < 50)
1090 scsi = "FAST-10";
1091 else
1092 scsi = "FAST-5";
1093 }
1094
1095 kb100 = (10000000 + picosec / 2) / picosec;
1096 if (tp->width)
1097 kb100 *= 2;
1098 sprint_frac(tmp, picosec, 1000);
1099
1100 dev_info(&starget->dev,
James Bottomleyd872ebe2005-08-03 15:43:52 -05001101 "%s %sSCSI %d.%d MB/s %s%s%s%s%s%s%s%s (%s ns, offset %d)\n",
1102 scsi, tp->width ? "WIDE " : "", kb100/10, kb100 % 10,
1103 tp->dt ? "DT" : "ST",
1104 tp->iu ? " IU" : "",
1105 tp->qas ? " QAS" : "",
1106 tp->rd_strm ? " RDSTRM" : "",
1107 tp->rti ? " RTI" : "",
1108 tp->wr_flow ? " WRFLOW" : "",
1109 tp->pcomp_en ? " PCOMP" : "",
1110 tp->hold_mcs ? " HMCS" : "",
1111 tmp, tp->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 } else {
Matthew Wilcox493ff4e2005-11-17 11:13:43 -07001113 dev_info(&starget->dev, "%sasynchronous\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 tp->width ? "wide " : "");
1115 }
1116}
1117EXPORT_SYMBOL(spi_display_xfer_agreement);
1118
Matthew Wilcox6ea3c0b2006-02-07 07:54:46 -07001119int spi_populate_width_msg(unsigned char *msg, int width)
1120{
1121 msg[0] = EXTENDED_MESSAGE;
1122 msg[1] = 2;
1123 msg[2] = EXTENDED_WDTR;
1124 msg[3] = width;
1125 return 4;
1126}
James Bottomley38e14f82006-02-17 14:58:47 -08001127EXPORT_SYMBOL_GPL(spi_populate_width_msg);
Matthew Wilcox6ea3c0b2006-02-07 07:54:46 -07001128
1129int spi_populate_sync_msg(unsigned char *msg, int period, int offset)
1130{
1131 msg[0] = EXTENDED_MESSAGE;
1132 msg[1] = 3;
1133 msg[2] = EXTENDED_SDTR;
1134 msg[3] = period;
1135 msg[4] = offset;
1136 return 5;
1137}
James Bottomley38e14f82006-02-17 14:58:47 -08001138EXPORT_SYMBOL_GPL(spi_populate_sync_msg);
Matthew Wilcox6ea3c0b2006-02-07 07:54:46 -07001139
1140int spi_populate_ppr_msg(unsigned char *msg, int period, int offset,
1141 int width, int options)
1142{
1143 msg[0] = EXTENDED_MESSAGE;
1144 msg[1] = 6;
1145 msg[2] = EXTENDED_PPR;
1146 msg[3] = period;
1147 msg[4] = 0;
1148 msg[5] = offset;
1149 msg[6] = width;
1150 msg[7] = options;
1151 return 8;
1152}
James Bottomley38e14f82006-02-17 14:58:47 -08001153EXPORT_SYMBOL_GPL(spi_populate_ppr_msg);
Matthew Wilcox6ea3c0b2006-02-07 07:54:46 -07001154
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001155#ifdef CONFIG_SCSI_CONSTANTS
1156static const char * const one_byte_msgs[] = {
Matthew Wilcox72df0eb2006-03-10 07:18:22 -07001157/* 0x00 */ "Task Complete", NULL /* Extended Message */, "Save Pointers",
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001158/* 0x03 */ "Restore Pointers", "Disconnect", "Initiator Error",
Matthew Wilcox72df0eb2006-03-10 07:18:22 -07001159/* 0x06 */ "Abort Task Set", "Message Reject", "Nop", "Message Parity Error",
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001160/* 0x0a */ "Linked Command Complete", "Linked Command Complete w/flag",
Matthew Wilcox72df0eb2006-03-10 07:18:22 -07001161/* 0x0c */ "Target Reset", "Abort Task", "Clear Task Set",
1162/* 0x0f */ "Initiate Recovery", "Release Recovery",
1163/* 0x11 */ "Terminate Process", "Continue Task", "Target Transfer Disable",
1164/* 0x14 */ NULL, NULL, "Clear ACA", "LUN Reset"
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001165};
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001166
1167static const char * const two_byte_msgs[] = {
Matthew Wilcox47972152005-12-15 16:22:01 -05001168/* 0x20 */ "Simple Queue Tag", "Head of Queue Tag", "Ordered Queue Tag",
Matthew Wilcox72df0eb2006-03-10 07:18:22 -07001169/* 0x23 */ "Ignore Wide Residue", "ACA"
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001170};
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001171
1172static const char * const extended_msgs[] = {
1173/* 0x00 */ "Modify Data Pointer", "Synchronous Data Transfer Request",
Matthew Wilcoxef725822005-12-15 16:22:01 -05001174/* 0x02 */ "SCSI-I Extended Identify", "Wide Data Transfer Request",
Matthew Wilcoxfc253072006-02-18 20:52:31 -07001175/* 0x04 */ "Parallel Protocol Request", "Modify Bidirectional Data Pointer"
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001176};
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001177
Adrian Bunkbdd70f22006-01-05 23:39:18 +01001178static void print_nego(const unsigned char *msg, int per, int off, int width)
Matthew Wilcoxef725822005-12-15 16:22:01 -05001179{
1180 if (per) {
1181 char buf[20];
1182 period_to_str(buf, msg[per]);
1183 printk("period = %s ns ", buf);
1184 }
1185
1186 if (off)
1187 printk("offset = %d ", msg[off]);
1188 if (width)
1189 printk("width = %d ", 8 << msg[width]);
1190}
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001191
Matthew Wilcoxfc253072006-02-18 20:52:31 -07001192static void print_ptr(const unsigned char *msg, int msb, const char *desc)
1193{
1194 int ptr = (msg[msb] << 24) | (msg[msb+1] << 16) | (msg[msb+2] << 8) |
1195 msg[msb+3];
1196 printk("%s = %d ", desc, ptr);
1197}
1198
Matthew Wilcox1abfd372005-12-15 16:22:01 -05001199int spi_print_msg(const unsigned char *msg)
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001200{
Matthew Wilcox72df0eb2006-03-10 07:18:22 -07001201 int len = 1, i;
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001202 if (msg[0] == EXTENDED_MESSAGE) {
Matthew Wilcoxfc253072006-02-18 20:52:31 -07001203 len = 2 + msg[1];
1204 if (len == 2)
1205 len += 256;
Matthew Wilcoxb32aaff2005-12-15 16:22:01 -05001206 if (msg[2] < ARRAY_SIZE(extended_msgs))
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001207 printk ("%s ", extended_msgs[msg[2]]);
1208 else
1209 printk ("Extended Message, reserved code (0x%02x) ",
1210 (int) msg[2]);
1211 switch (msg[2]) {
1212 case EXTENDED_MODIFY_DATA_POINTER:
Matthew Wilcoxfc253072006-02-18 20:52:31 -07001213 print_ptr(msg, 3, "pointer");
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001214 break;
1215 case EXTENDED_SDTR:
Matthew Wilcoxef725822005-12-15 16:22:01 -05001216 print_nego(msg, 3, 4, 0);
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001217 break;
1218 case EXTENDED_WDTR:
Matthew Wilcoxef725822005-12-15 16:22:01 -05001219 print_nego(msg, 0, 0, 3);
1220 break;
1221 case EXTENDED_PPR:
1222 print_nego(msg, 3, 5, 6);
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001223 break;
Matthew Wilcoxfc253072006-02-18 20:52:31 -07001224 case EXTENDED_MODIFY_BIDI_DATA_PTR:
1225 print_ptr(msg, 3, "out");
1226 print_ptr(msg, 7, "in");
1227 break;
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001228 default:
1229 for (i = 2; i < len; ++i)
1230 printk("%02x ", msg[i]);
1231 }
1232 /* Identify */
1233 } else if (msg[0] & 0x80) {
1234 printk("Identify disconnect %sallowed %s %d ",
1235 (msg[0] & 0x40) ? "" : "not ",
1236 (msg[0] & 0x20) ? "target routine" : "lun",
1237 msg[0] & 0x7);
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001238 /* Normal One byte */
1239 } else if (msg[0] < 0x1f) {
Matthew Wilcox72df0eb2006-03-10 07:18:22 -07001240 if (msg[0] < ARRAY_SIZE(one_byte_msgs) && one_byte_msgs[msg[0]])
Matthew Wilcoxe24d8732006-02-07 08:05:26 -07001241 printk("%s ", one_byte_msgs[msg[0]]);
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001242 else
1243 printk("reserved (%02x) ", msg[0]);
Matthew Wilcox72df0eb2006-03-10 07:18:22 -07001244 } else if (msg[0] == 0x55) {
1245 printk("QAS Request ");
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001246 /* Two byte */
1247 } else if (msg[0] <= 0x2f) {
Matthew Wilcoxb32aaff2005-12-15 16:22:01 -05001248 if ((msg[0] - 0x20) < ARRAY_SIZE(two_byte_msgs))
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001249 printk("%s %02x ", two_byte_msgs[msg[0] - 0x20],
1250 msg[1]);
1251 else
1252 printk("reserved two byte (%02x %02x) ",
1253 msg[0], msg[1]);
1254 len = 2;
1255 } else
Matthew Wilcoxe24d8732006-02-07 08:05:26 -07001256 printk("reserved ");
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001257 return len;
1258}
Matthew Wilcox1abfd372005-12-15 16:22:01 -05001259EXPORT_SYMBOL(spi_print_msg);
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001260
1261#else /* ifndef CONFIG_SCSI_CONSTANTS */
1262
Matthew Wilcox1abfd372005-12-15 16:22:01 -05001263int spi_print_msg(const unsigned char *msg)
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001264{
Matthew Wilcox72df0eb2006-03-10 07:18:22 -07001265 int len = 1, i;
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001266
1267 if (msg[0] == EXTENDED_MESSAGE) {
Matthew Wilcoxfc253072006-02-18 20:52:31 -07001268 len = 2 + msg[1];
1269 if (len == 2)
1270 len += 256;
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001271 for (i = 0; i < len; ++i)
1272 printk("%02x ", msg[i]);
1273 /* Identify */
1274 } else if (msg[0] & 0x80) {
1275 printk("%02x ", msg[0]);
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001276 /* Normal One byte */
James Bottomley597705a2006-03-12 09:54:19 -06001277 } else if ((msg[0] < 0x1f) || (msg[0] == 0x55)) {
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001278 printk("%02x ", msg[0]);
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001279 /* Two byte */
1280 } else if (msg[0] <= 0x2f) {
1281 printk("%02x %02x", msg[0], msg[1]);
1282 len = 2;
1283 } else
1284 printk("%02x ", msg[0]);
1285 return len;
1286}
Matthew Wilcox1abfd372005-12-15 16:22:01 -05001287EXPORT_SYMBOL(spi_print_msg);
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001288#endif /* ! CONFIG_SCSI_CONSTANTS */
1289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290static int spi_device_match(struct attribute_container *cont,
1291 struct device *dev)
1292{
1293 struct scsi_device *sdev;
1294 struct Scsi_Host *shost;
James Bottomley10c1b882005-08-14 14:34:06 -05001295 struct spi_internal *i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 if (!scsi_is_sdev_device(dev))
1298 return 0;
1299
1300 sdev = to_scsi_device(dev);
1301 shost = sdev->host;
1302 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1303 != &spi_host_class.class)
1304 return 0;
1305 /* Note: this class has no device attributes, so it has
1306 * no per-HBA allocation and thus we don't need to distinguish
1307 * the attribute containers for the device */
James Bottomley10c1b882005-08-14 14:34:06 -05001308 i = to_spi_internal(shost->transportt);
1309 if (i->f->deny_binding && i->f->deny_binding(sdev->sdev_target))
1310 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 return 1;
1312}
1313
1314static int spi_target_match(struct attribute_container *cont,
1315 struct device *dev)
1316{
1317 struct Scsi_Host *shost;
James Bottomley10c1b882005-08-14 14:34:06 -05001318 struct scsi_target *starget;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 struct spi_internal *i;
1320
1321 if (!scsi_is_target_device(dev))
1322 return 0;
1323
1324 shost = dev_to_shost(dev->parent);
1325 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1326 != &spi_host_class.class)
1327 return 0;
1328
1329 i = to_spi_internal(shost->transportt);
James Bottomley10c1b882005-08-14 14:34:06 -05001330 starget = to_scsi_target(dev);
1331
1332 if (i->f->deny_binding && i->f->deny_binding(starget))
1333 return 0;
1334
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 return &i->t.target_attrs.ac == cont;
1336}
1337
1338static DECLARE_TRANSPORT_CLASS(spi_transport_class,
1339 "spi_transport",
1340 spi_setup_transport_attrs,
1341 NULL,
James Bottomley9b161a42008-01-05 10:18:27 -06001342 spi_target_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
1344static DECLARE_ANON_TRANSPORT_CLASS(spi_device_class,
1345 spi_device_match,
1346 spi_device_configure);
1347
James Bottomley9b161a42008-01-05 10:18:27 -06001348static struct attribute *host_attributes[] = {
Tony Jonesee959b02008-02-22 00:13:36 +01001349 &dev_attr_signalling.attr,
James Bottomley9b161a42008-01-05 10:18:27 -06001350 NULL
1351};
1352
1353static struct attribute_group host_attribute_group = {
1354 .attrs = host_attributes,
1355};
1356
1357static int spi_host_configure(struct transport_container *tc,
1358 struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +01001359 struct device *cdev)
James Bottomley9b161a42008-01-05 10:18:27 -06001360{
1361 struct kobject *kobj = &cdev->kobj;
1362 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1363 struct spi_internal *si = to_spi_internal(shost->transportt);
Tony Jonesee959b02008-02-22 00:13:36 +01001364 struct attribute *attr = &dev_attr_signalling.attr;
James Bottomley9b161a42008-01-05 10:18:27 -06001365 int rc = 0;
1366
1367 if (si->f->set_signalling)
1368 rc = sysfs_chmod_file(kobj, attr, attr->mode | S_IWUSR);
1369
1370 return rc;
1371}
1372
1373/* returns true if we should be showing the variable. Also
1374 * overloads the return by setting 1<<1 if the attribute should
1375 * be writeable */
1376#define TARGET_ATTRIBUTE_HELPER(name) \
1377 (si->f->show_##name ? 1 : 0) + \
1378 (si->f->set_##name ? 2 : 0)
1379
1380static int target_attribute_is_visible(struct kobject *kobj,
1381 struct attribute *attr, int i)
1382{
Tony Jonesee959b02008-02-22 00:13:36 +01001383 struct device *cdev = container_of(kobj, struct device, kobj);
James Bottomley9b161a42008-01-05 10:18:27 -06001384 struct scsi_target *starget = transport_class_to_starget(cdev);
1385 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1386 struct spi_internal *si = to_spi_internal(shost->transportt);
1387
Tony Jonesee959b02008-02-22 00:13:36 +01001388 if (attr == &dev_attr_period.attr &&
James Bottomley9b161a42008-01-05 10:18:27 -06001389 spi_support_sync(starget))
1390 return TARGET_ATTRIBUTE_HELPER(period);
Tony Jonesee959b02008-02-22 00:13:36 +01001391 else if (attr == &dev_attr_min_period.attr &&
James Bottomley9b161a42008-01-05 10:18:27 -06001392 spi_support_sync(starget))
1393 return TARGET_ATTRIBUTE_HELPER(period);
Tony Jonesee959b02008-02-22 00:13:36 +01001394 else if (attr == &dev_attr_offset.attr &&
James Bottomley9b161a42008-01-05 10:18:27 -06001395 spi_support_sync(starget))
1396 return TARGET_ATTRIBUTE_HELPER(offset);
Tony Jonesee959b02008-02-22 00:13:36 +01001397 else if (attr == &dev_attr_max_offset.attr &&
James Bottomley9b161a42008-01-05 10:18:27 -06001398 spi_support_sync(starget))
1399 return TARGET_ATTRIBUTE_HELPER(offset);
Tony Jonesee959b02008-02-22 00:13:36 +01001400 else if (attr == &dev_attr_width.attr &&
James Bottomley9b161a42008-01-05 10:18:27 -06001401 spi_support_wide(starget))
1402 return TARGET_ATTRIBUTE_HELPER(width);
Tony Jonesee959b02008-02-22 00:13:36 +01001403 else if (attr == &dev_attr_max_width.attr &&
James Bottomley9b161a42008-01-05 10:18:27 -06001404 spi_support_wide(starget))
1405 return TARGET_ATTRIBUTE_HELPER(width);
Tony Jonesee959b02008-02-22 00:13:36 +01001406 else if (attr == &dev_attr_iu.attr &&
James Bottomley9b161a42008-01-05 10:18:27 -06001407 spi_support_ius(starget))
1408 return TARGET_ATTRIBUTE_HELPER(iu);
Tony Jonesee959b02008-02-22 00:13:36 +01001409 else if (attr == &dev_attr_dt.attr &&
James Bottomley9b161a42008-01-05 10:18:27 -06001410 spi_support_dt(starget))
1411 return TARGET_ATTRIBUTE_HELPER(dt);
Tony Jonesee959b02008-02-22 00:13:36 +01001412 else if (attr == &dev_attr_qas.attr &&
James Bottomley9b161a42008-01-05 10:18:27 -06001413 spi_support_qas(starget))
1414 return TARGET_ATTRIBUTE_HELPER(qas);
Tony Jonesee959b02008-02-22 00:13:36 +01001415 else if (attr == &dev_attr_wr_flow.attr &&
James Bottomley9b161a42008-01-05 10:18:27 -06001416 spi_support_ius(starget))
1417 return TARGET_ATTRIBUTE_HELPER(wr_flow);
Tony Jonesee959b02008-02-22 00:13:36 +01001418 else if (attr == &dev_attr_rd_strm.attr &&
James Bottomley9b161a42008-01-05 10:18:27 -06001419 spi_support_ius(starget))
1420 return TARGET_ATTRIBUTE_HELPER(rd_strm);
Tony Jonesee959b02008-02-22 00:13:36 +01001421 else if (attr == &dev_attr_rti.attr &&
James Bottomley9b161a42008-01-05 10:18:27 -06001422 spi_support_ius(starget))
1423 return TARGET_ATTRIBUTE_HELPER(rti);
Tony Jonesee959b02008-02-22 00:13:36 +01001424 else if (attr == &dev_attr_pcomp_en.attr &&
James Bottomley9b161a42008-01-05 10:18:27 -06001425 spi_support_ius(starget))
1426 return TARGET_ATTRIBUTE_HELPER(pcomp_en);
Tony Jonesee959b02008-02-22 00:13:36 +01001427 else if (attr == &dev_attr_hold_mcs.attr &&
James Bottomley9b161a42008-01-05 10:18:27 -06001428 spi_support_ius(starget))
1429 return TARGET_ATTRIBUTE_HELPER(hold_mcs);
Tony Jonesee959b02008-02-22 00:13:36 +01001430 else if (attr == &dev_attr_revalidate.attr)
James Bottomley9b161a42008-01-05 10:18:27 -06001431 return 1;
1432
1433 return 0;
1434}
1435
1436static struct attribute *target_attributes[] = {
Tony Jonesee959b02008-02-22 00:13:36 +01001437 &dev_attr_period.attr,
1438 &dev_attr_min_period.attr,
1439 &dev_attr_offset.attr,
1440 &dev_attr_max_offset.attr,
1441 &dev_attr_width.attr,
1442 &dev_attr_max_width.attr,
1443 &dev_attr_iu.attr,
1444 &dev_attr_dt.attr,
1445 &dev_attr_qas.attr,
1446 &dev_attr_wr_flow.attr,
1447 &dev_attr_rd_strm.attr,
1448 &dev_attr_rti.attr,
1449 &dev_attr_pcomp_en.attr,
1450 &dev_attr_hold_mcs.attr,
1451 &dev_attr_revalidate.attr,
James Bottomley9b161a42008-01-05 10:18:27 -06001452 NULL
1453};
1454
1455static struct attribute_group target_attribute_group = {
1456 .attrs = target_attributes,
1457 .is_visible = target_attribute_is_visible,
1458};
1459
1460static int spi_target_configure(struct transport_container *tc,
1461 struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +01001462 struct device *cdev)
James Bottomley9b161a42008-01-05 10:18:27 -06001463{
1464 struct kobject *kobj = &cdev->kobj;
1465 int i;
1466 struct attribute *attr;
1467 int rc;
1468
1469 for (i = 0; (attr = target_attributes[i]) != NULL; i++) {
1470 int j = target_attribute_group.is_visible(kobj, attr, i);
1471
1472 /* FIXME: as well as returning -EEXIST, which we'd like
1473 * to ignore, sysfs also does a WARN_ON and dumps a trace,
1474 * which is bad, so temporarily, skip attributes that are
1475 * already visible (the revalidate one) */
Tony Jonesee959b02008-02-22 00:13:36 +01001476 if (j && attr != &dev_attr_revalidate.attr)
James Bottomley9b161a42008-01-05 10:18:27 -06001477 rc = sysfs_add_file_to_group(kobj, attr,
1478 target_attribute_group.name);
1479 /* and make the attribute writeable if we have a set
1480 * function */
1481 if ((j & 1))
1482 rc = sysfs_chmod_file(kobj, attr, attr->mode | S_IWUSR);
1483 }
1484
1485 return 0;
1486}
1487
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488struct scsi_transport_template *
1489spi_attach_transport(struct spi_function_template *ft)
1490{
Jes Sorensen24669f752006-01-16 10:31:18 -05001491 struct spi_internal *i = kzalloc(sizeof(struct spi_internal),
1492 GFP_KERNEL);
1493
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 if (unlikely(!i))
1495 return NULL;
1496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 i->t.target_attrs.ac.class = &spi_transport_class.class;
James Bottomley9b161a42008-01-05 10:18:27 -06001498 i->t.target_attrs.ac.grp = &target_attribute_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 i->t.target_attrs.ac.match = spi_target_match;
1500 transport_container_register(&i->t.target_attrs);
1501 i->t.target_size = sizeof(struct spi_transport_attrs);
1502 i->t.host_attrs.ac.class = &spi_host_class.class;
James Bottomley9b161a42008-01-05 10:18:27 -06001503 i->t.host_attrs.ac.grp = &host_attribute_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 i->t.host_attrs.ac.match = spi_host_match;
1505 transport_container_register(&i->t.host_attrs);
1506 i->t.host_size = sizeof(struct spi_host_attrs);
1507 i->f = ft;
1508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 return &i->t;
1510}
1511EXPORT_SYMBOL(spi_attach_transport);
1512
1513void spi_release_transport(struct scsi_transport_template *t)
1514{
1515 struct spi_internal *i = to_spi_internal(t);
1516
1517 transport_container_unregister(&i->t.target_attrs);
1518 transport_container_unregister(&i->t.host_attrs);
1519
1520 kfree(i);
1521}
1522EXPORT_SYMBOL(spi_release_transport);
1523
1524static __init int spi_transport_init(void)
1525{
1526 int error = transport_class_register(&spi_transport_class);
1527 if (error)
1528 return error;
1529 error = anon_transport_class_register(&spi_device_class);
1530 return transport_class_register(&spi_host_class);
1531}
1532
1533static void __exit spi_transport_exit(void)
1534{
1535 transport_class_unregister(&spi_transport_class);
1536 anon_transport_class_unregister(&spi_device_class);
1537 transport_class_unregister(&spi_host_class);
1538}
1539
1540MODULE_AUTHOR("Martin Hicks");
1541MODULE_DESCRIPTION("SPI Transport Attributes");
1542MODULE_LICENSE("GPL");
1543
1544module_init(spi_transport_init);
1545module_exit(spi_transport_exit);