blob: 1fb60313a516fcd971538b0ebf17271357d5ed48 [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,
161 struct class_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,
172 struct class_device *cdev);
173
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,
198 struct class_device *cdev);
199
James Bottomleyd0a7e572005-08-14 17:09:01 -0500200static int spi_device_configure(struct transport_container *tc,
201 struct device *dev,
202 struct class_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,
222 struct class_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 \
251show_spi_transport_##field(struct class_device *cdev, char *buf) \
252{ \
253 struct scsi_target *starget = transport_class_to_starget(cdev); \
254 struct spi_transport_attrs *tp; \
255 \
256 tp = (struct spi_transport_attrs *)&starget->starget_data; \
257 return snprintf(buf, 20, format_string, tp->field); \
258}
259
260#define spi_transport_store_simple(field, format_string) \
261 \
262static ssize_t \
263store_spi_transport_##field(struct class_device *cdev, const char *buf, \
264 size_t count) \
265{ \
266 int val; \
267 struct scsi_target *starget = transport_class_to_starget(cdev); \
268 struct spi_transport_attrs *tp; \
269 \
270 tp = (struct spi_transport_attrs *)&starget->starget_data; \
271 val = simple_strtoul(buf, NULL, 0); \
272 tp->field = val; \
273 return count; \
274}
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276#define spi_transport_show_function(field, format_string) \
277 \
278static ssize_t \
279show_spi_transport_##field(struct class_device *cdev, char *buf) \
280{ \
281 struct scsi_target *starget = transport_class_to_starget(cdev); \
282 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
283 struct spi_transport_attrs *tp; \
284 struct spi_internal *i = to_spi_internal(shost->transportt); \
285 tp = (struct spi_transport_attrs *)&starget->starget_data; \
286 if (i->f->get_##field) \
287 i->f->get_##field(starget); \
288 return snprintf(buf, 20, format_string, tp->field); \
289}
290
291#define spi_transport_store_function(field, format_string) \
292static ssize_t \
293store_spi_transport_##field(struct class_device *cdev, const char *buf, \
294 size_t count) \
295{ \
296 int val; \
297 struct scsi_target *starget = transport_class_to_starget(cdev); \
298 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
299 struct spi_internal *i = to_spi_internal(shost->transportt); \
300 \
James Bottomley9b161a42008-01-05 10:18:27 -0600301 if (!i->f->set_##field) \
302 return -EINVAL; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 val = simple_strtoul(buf, NULL, 0); \
James Bottomley9b161a42008-01-05 10:18:27 -0600304 i->f->set_##field(starget, val); \
James Bottomley 62a86122005-05-06 18:05:20 -0500305 return count; \
306}
307
308#define spi_transport_store_max(field, format_string) \
309static ssize_t \
310store_spi_transport_##field(struct class_device *cdev, const char *buf, \
311 size_t count) \
312{ \
313 int val; \
314 struct scsi_target *starget = transport_class_to_starget(cdev); \
315 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
316 struct spi_internal *i = to_spi_internal(shost->transportt); \
317 struct spi_transport_attrs *tp \
318 = (struct spi_transport_attrs *)&starget->starget_data; \
319 \
James Bottomley9b161a42008-01-05 10:18:27 -0600320 if (i->f->set_##field) \
321 return -EINVAL; \
James Bottomley 62a86122005-05-06 18:05:20 -0500322 val = simple_strtoul(buf, NULL, 0); \
323 if (val > tp->max_##field) \
324 val = tp->max_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 i->f->set_##field(starget, val); \
326 return count; \
327}
328
329#define spi_transport_rd_attr(field, format_string) \
330 spi_transport_show_function(field, format_string) \
331 spi_transport_store_function(field, format_string) \
James Bottomley9b161a42008-01-05 10:18:27 -0600332static CLASS_DEVICE_ATTR(field, S_IRUGO, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 show_spi_transport_##field, \
334 store_spi_transport_##field);
335
James Bottomley 62a86122005-05-06 18:05:20 -0500336#define spi_transport_simple_attr(field, format_string) \
337 spi_transport_show_simple(field, format_string) \
338 spi_transport_store_simple(field, format_string) \
James Bottomley9b161a42008-01-05 10:18:27 -0600339static CLASS_DEVICE_ATTR(field, S_IRUGO, \
James Bottomley 62a86122005-05-06 18:05:20 -0500340 show_spi_transport_##field, \
341 store_spi_transport_##field);
342
343#define spi_transport_max_attr(field, format_string) \
344 spi_transport_show_function(field, format_string) \
345 spi_transport_store_max(field, format_string) \
346 spi_transport_simple_attr(max_##field, format_string) \
James Bottomley9b161a42008-01-05 10:18:27 -0600347static CLASS_DEVICE_ATTR(field, S_IRUGO, \
James Bottomley 62a86122005-05-06 18:05:20 -0500348 show_spi_transport_##field, \
349 store_spi_transport_##field);
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351/* The Parallel SCSI Tranport Attributes: */
James Bottomley 62a86122005-05-06 18:05:20 -0500352spi_transport_max_attr(offset, "%d\n");
353spi_transport_max_attr(width, "%d\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354spi_transport_rd_attr(iu, "%d\n");
355spi_transport_rd_attr(dt, "%d\n");
356spi_transport_rd_attr(qas, "%d\n");
357spi_transport_rd_attr(wr_flow, "%d\n");
358spi_transport_rd_attr(rd_strm, "%d\n");
359spi_transport_rd_attr(rti, "%d\n");
360spi_transport_rd_attr(pcomp_en, "%d\n");
James Bottomleyd872ebe2005-08-03 15:43:52 -0500361spi_transport_rd_attr(hold_mcs, "%d\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
gregkh@suse.de9a881f12005-03-25 15:52:00 -0800363/* we only care about the first child device so we return 1 */
364static int child_iter(struct device *dev, void *data)
365{
366 struct scsi_device *sdev = to_scsi_device(dev);
367
368 spi_dv_device(sdev);
369 return 1;
370}
371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372static ssize_t
373store_spi_revalidate(struct class_device *cdev, const char *buf, size_t count)
374{
375 struct scsi_target *starget = transport_class_to_starget(cdev);
376
gregkh@suse.de9a881f12005-03-25 15:52:00 -0800377 device_for_each_child(&starget->dev, NULL, child_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return count;
379}
380static CLASS_DEVICE_ATTR(revalidate, S_IWUSR, NULL, store_spi_revalidate);
381
382/* Translate the period into ns according to the current spec
383 * for SDTR/PPR messages */
Matthew Wilcoxef725822005-12-15 16:22:01 -0500384static int period_to_str(char *buf, int period)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 int len, picosec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
James Bottomley 62a86122005-05-06 18:05:20 -0500388 if (period < 0 || period > 0xff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 picosec = -1;
James Bottomley 62a86122005-05-06 18:05:20 -0500390 } else if (period <= SPI_STATIC_PPR) {
391 picosec = ppr_to_ps[period];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 } else {
James Bottomley 62a86122005-05-06 18:05:20 -0500393 picosec = period * 4000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
395
396 if (picosec == -1) {
397 len = sprintf(buf, "reserved");
398 } else {
399 len = sprint_frac(buf, picosec, 1000);
400 }
401
Matthew Wilcoxef725822005-12-15 16:22:01 -0500402 return len;
403}
404
405static ssize_t
Matthew Wilcoxea697e42006-02-07 08:01:02 -0700406show_spi_transport_period_helper(char *buf, int period)
Matthew Wilcoxef725822005-12-15 16:22:01 -0500407{
408 int len = period_to_str(buf, period);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 buf[len++] = '\n';
410 buf[len] = '\0';
411 return len;
412}
413
414static ssize_t
James Bottomley 62a86122005-05-06 18:05:20 -0500415store_spi_transport_period_helper(struct class_device *cdev, const char *buf,
416 size_t count, int *periodp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 int j, picosec, period = -1;
419 char *endp;
420
421 picosec = simple_strtoul(buf, &endp, 10) * 1000;
422 if (*endp == '.') {
423 int mult = 100;
424 do {
425 endp++;
426 if (!isdigit(*endp))
427 break;
428 picosec += (*endp - '0') * mult;
429 mult /= 10;
430 } while (mult > 0);
431 }
432
433 for (j = 0; j <= SPI_STATIC_PPR; j++) {
434 if (ppr_to_ps[j] < picosec)
435 continue;
436 period = j;
437 break;
438 }
439
440 if (period == -1)
441 period = picosec / 4000;
442
443 if (period > 0xff)
444 period = 0xff;
445
James Bottomley 62a86122005-05-06 18:05:20 -0500446 *periodp = period;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 return count;
449}
450
James Bottomley 62a86122005-05-06 18:05:20 -0500451static ssize_t
452show_spi_transport_period(struct class_device *cdev, char *buf)
453{
454 struct scsi_target *starget = transport_class_to_starget(cdev);
455 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
456 struct spi_internal *i = to_spi_internal(shost->transportt);
457 struct spi_transport_attrs *tp =
458 (struct spi_transport_attrs *)&starget->starget_data;
459
460 if (i->f->get_period)
461 i->f->get_period(starget);
462
Matthew Wilcoxea697e42006-02-07 08:01:02 -0700463 return show_spi_transport_period_helper(buf, tp->period);
James Bottomley 62a86122005-05-06 18:05:20 -0500464}
465
466static ssize_t
467store_spi_transport_period(struct class_device *cdev, const char *buf,
468 size_t count)
469{
470 struct scsi_target *starget = transport_class_to_starget(cdev);
471 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
472 struct spi_internal *i = to_spi_internal(shost->transportt);
473 struct spi_transport_attrs *tp =
474 (struct spi_transport_attrs *)&starget->starget_data;
475 int period, retval;
476
James Bottomley9b161a42008-01-05 10:18:27 -0600477 if (!i->f->set_period)
478 return -EINVAL;
479
James Bottomley 62a86122005-05-06 18:05:20 -0500480 retval = store_spi_transport_period_helper(cdev, buf, count, &period);
481
482 if (period < tp->min_period)
483 period = tp->min_period;
484
485 i->f->set_period(starget, period);
486
487 return retval;
488}
489
James Bottomley9b161a42008-01-05 10:18:27 -0600490static CLASS_DEVICE_ATTR(period, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 show_spi_transport_period,
492 store_spi_transport_period);
493
James Bottomley 62a86122005-05-06 18:05:20 -0500494static ssize_t
495show_spi_transport_min_period(struct class_device *cdev, char *buf)
496{
497 struct scsi_target *starget = transport_class_to_starget(cdev);
James Bottomley9b161a42008-01-05 10:18:27 -0600498 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
499 struct spi_internal *i = to_spi_internal(shost->transportt);
James Bottomley 62a86122005-05-06 18:05:20 -0500500 struct spi_transport_attrs *tp =
501 (struct spi_transport_attrs *)&starget->starget_data;
502
James Bottomley9b161a42008-01-05 10:18:27 -0600503 if (!i->f->set_period)
504 return -EINVAL;
505
Matthew Wilcoxea697e42006-02-07 08:01:02 -0700506 return show_spi_transport_period_helper(buf, tp->min_period);
James Bottomley 62a86122005-05-06 18:05:20 -0500507}
508
509static ssize_t
510store_spi_transport_min_period(struct class_device *cdev, const char *buf,
511 size_t count)
512{
513 struct scsi_target *starget = transport_class_to_starget(cdev);
514 struct spi_transport_attrs *tp =
515 (struct spi_transport_attrs *)&starget->starget_data;
516
517 return store_spi_transport_period_helper(cdev, buf, count,
518 &tp->min_period);
519}
520
521
James Bottomley9b161a42008-01-05 10:18:27 -0600522static CLASS_DEVICE_ATTR(min_period, S_IRUGO,
James Bottomley 62a86122005-05-06 18:05:20 -0500523 show_spi_transport_min_period,
524 store_spi_transport_min_period);
525
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527static ssize_t show_spi_host_signalling(struct class_device *cdev, char *buf)
528{
529 struct Scsi_Host *shost = transport_class_to_shost(cdev);
530 struct spi_internal *i = to_spi_internal(shost->transportt);
531
532 if (i->f->get_signalling)
533 i->f->get_signalling(shost);
534
535 return sprintf(buf, "%s\n", spi_signal_to_string(spi_signalling(shost)));
536}
537static ssize_t store_spi_host_signalling(struct class_device *cdev,
538 const char *buf, size_t count)
539{
540 struct Scsi_Host *shost = transport_class_to_shost(cdev);
541 struct spi_internal *i = to_spi_internal(shost->transportt);
542 enum spi_signal_type type = spi_signal_to_value(buf);
543
James Bottomley9b161a42008-01-05 10:18:27 -0600544 if (!i->f->set_signalling)
545 return -EINVAL;
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (type != SPI_SIGNAL_UNKNOWN)
548 i->f->set_signalling(shost, type);
549
550 return count;
551}
James Bottomley9b161a42008-01-05 10:18:27 -0600552static CLASS_DEVICE_ATTR(signalling, S_IRUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 show_spi_host_signalling,
554 store_spi_host_signalling);
555
556#define DV_SET(x, y) \
557 if(i->f->set_##x) \
558 i->f->set_##x(sdev->sdev_target, y)
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560enum spi_compare_returns {
561 SPI_COMPARE_SUCCESS,
562 SPI_COMPARE_FAILURE,
563 SPI_COMPARE_SKIP_TEST,
564};
565
566
567/* This is for read/write Domain Validation: If the device supports
568 * an echo buffer, we do read/write tests to it */
569static enum spi_compare_returns
James Bottomley33aa6872005-08-28 11:31:14 -0500570spi_dv_device_echo_buffer(struct scsi_device *sdev, u8 *buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 u8 *ptr, const int retries)
572{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 int len = ptr - buffer;
James Bottomley33aa6872005-08-28 11:31:14 -0500574 int j, k, r, result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 unsigned int pattern = 0x0000ffff;
James Bottomley33aa6872005-08-28 11:31:14 -0500576 struct scsi_sense_hdr sshdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578 const char spi_write_buffer[] = {
579 WRITE_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
580 };
581 const char spi_read_buffer[] = {
582 READ_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
583 };
584
585 /* set up the pattern buffer. Doesn't matter if we spill
586 * slightly beyond since that's where the read buffer is */
587 for (j = 0; j < len; ) {
588
589 /* fill the buffer with counting (test a) */
590 for ( ; j < min(len, 32); j++)
591 buffer[j] = j;
592 k = j;
593 /* fill the buffer with alternating words of 0x0 and
594 * 0xffff (test b) */
595 for ( ; j < min(len, k + 32); j += 2) {
596 u16 *word = (u16 *)&buffer[j];
597
598 *word = (j & 0x02) ? 0x0000 : 0xffff;
599 }
600 k = j;
601 /* fill with crosstalk (alternating 0x5555 0xaaa)
602 * (test c) */
603 for ( ; j < min(len, k + 32); j += 2) {
604 u16 *word = (u16 *)&buffer[j];
605
606 *word = (j & 0x02) ? 0x5555 : 0xaaaa;
607 }
608 k = j;
609 /* fill with shifting bits (test d) */
610 for ( ; j < min(len, k + 32); j += 4) {
611 u32 *word = (unsigned int *)&buffer[j];
612 u32 roll = (pattern & 0x80000000) ? 1 : 0;
613
614 *word = pattern;
615 pattern = (pattern << 1) | roll;
616 }
617 /* don't bother with random data (test e) */
618 }
619
620 for (r = 0; r < retries; r++) {
James Bottomley33aa6872005-08-28 11:31:14 -0500621 result = spi_execute(sdev, spi_write_buffer, DMA_TO_DEVICE,
622 buffer, len, &sshdr);
623 if(result || !scsi_device_online(sdev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 scsi_device_set_state(sdev, SDEV_QUIESCE);
James Bottomley33aa6872005-08-28 11:31:14 -0500626 if (scsi_sense_valid(&sshdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 && sshdr.sense_key == ILLEGAL_REQUEST
628 /* INVALID FIELD IN CDB */
629 && sshdr.asc == 0x24 && sshdr.ascq == 0x00)
630 /* This would mean that the drive lied
631 * to us about supporting an echo
632 * buffer (unfortunately some Western
633 * Digital drives do precisely this)
634 */
635 return SPI_COMPARE_SKIP_TEST;
636
637
James Bottomley9ccfc752005-10-02 11:45:08 -0500638 sdev_printk(KERN_ERR, sdev, "Write Buffer failure %x\n", result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 return SPI_COMPARE_FAILURE;
640 }
641
642 memset(ptr, 0, len);
James Bottomley33aa6872005-08-28 11:31:14 -0500643 spi_execute(sdev, spi_read_buffer, DMA_FROM_DEVICE,
644 ptr, len, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 scsi_device_set_state(sdev, SDEV_QUIESCE);
646
647 if (memcmp(buffer, ptr, len) != 0)
648 return SPI_COMPARE_FAILURE;
649 }
650 return SPI_COMPARE_SUCCESS;
651}
652
653/* This is for the simplest form of Domain Validation: a read test
654 * on the inquiry data from the device */
655static enum spi_compare_returns
James Bottomley33aa6872005-08-28 11:31:14 -0500656spi_dv_device_compare_inquiry(struct scsi_device *sdev, u8 *buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 u8 *ptr, const int retries)
658{
James Bottomley33aa6872005-08-28 11:31:14 -0500659 int r, result;
660 const int len = sdev->inquiry_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 const char spi_inquiry[] = {
662 INQUIRY, 0, 0, 0, len, 0
663 };
664
665 for (r = 0; r < retries; r++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 memset(ptr, 0, len);
667
James Bottomley33aa6872005-08-28 11:31:14 -0500668 result = spi_execute(sdev, spi_inquiry, DMA_FROM_DEVICE,
669 ptr, len, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
James Bottomley33aa6872005-08-28 11:31:14 -0500671 if(result || !scsi_device_online(sdev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 scsi_device_set_state(sdev, SDEV_QUIESCE);
673 return SPI_COMPARE_FAILURE;
674 }
675
676 /* If we don't have the inquiry data already, the
677 * first read gets it */
678 if (ptr == buffer) {
679 ptr += len;
680 --r;
681 continue;
682 }
683
684 if (memcmp(buffer, ptr, len) != 0)
685 /* failure */
686 return SPI_COMPARE_FAILURE;
687 }
688 return SPI_COMPARE_SUCCESS;
689}
690
691static enum spi_compare_returns
James Bottomley33aa6872005-08-28 11:31:14 -0500692spi_dv_retrain(struct scsi_device *sdev, u8 *buffer, u8 *ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 enum spi_compare_returns
James Bottomley33aa6872005-08-28 11:31:14 -0500694 (*compare_fn)(struct scsi_device *, u8 *, u8 *, int))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
James Bottomley33aa6872005-08-28 11:31:14 -0500696 struct spi_internal *i = to_spi_internal(sdev->host->transportt);
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500697 struct scsi_target *starget = sdev->sdev_target;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 int period = 0, prevperiod = 0;
699 enum spi_compare_returns retval;
700
701
702 for (;;) {
703 int newperiod;
James Bottomley33aa6872005-08-28 11:31:14 -0500704 retval = compare_fn(sdev, buffer, ptr, DV_LOOPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706 if (retval == SPI_COMPARE_SUCCESS
707 || retval == SPI_COMPARE_SKIP_TEST)
708 break;
709
710 /* OK, retrain, fallback */
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500711 if (i->f->get_iu)
712 i->f->get_iu(starget);
713 if (i->f->get_qas)
714 i->f->get_qas(starget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if (i->f->get_period)
716 i->f->get_period(sdev->sdev_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500718 /* Here's the fallback sequence; first try turning off
719 * IU, then QAS (if we can control them), then finally
720 * fall down the periods */
721 if (i->f->set_iu && spi_iu(starget)) {
James Bottomley9ccfc752005-10-02 11:45:08 -0500722 starget_printk(KERN_ERR, starget, "Domain Validation Disabing Information Units\n");
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500723 DV_SET(iu, 0);
724 } else if (i->f->set_qas && spi_qas(starget)) {
James Bottomley9ccfc752005-10-02 11:45:08 -0500725 starget_printk(KERN_ERR, starget, "Domain Validation Disabing Quick Arbitration and Selection\n");
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500726 DV_SET(qas, 0);
727 } else {
728 newperiod = spi_period(starget);
729 period = newperiod > period ? newperiod : period;
730 if (period < 0x0d)
731 period++;
732 else
733 period += period >> 1;
734
735 if (unlikely(period > 0xff || period == prevperiod)) {
736 /* Total failure; set to async and return */
James Bottomley9ccfc752005-10-02 11:45:08 -0500737 starget_printk(KERN_ERR, starget, "Domain Validation Failure, dropping back to Asynchronous\n");
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500738 DV_SET(offset, 0);
739 return SPI_COMPARE_FAILURE;
740 }
James Bottomley9ccfc752005-10-02 11:45:08 -0500741 starget_printk(KERN_ERR, starget, "Domain Validation detected failure, dropping back\n");
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500742 DV_SET(period, period);
743 prevperiod = period;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
746 return retval;
747}
748
749static int
James Bottomley33aa6872005-08-28 11:31:14 -0500750spi_dv_device_get_echo_buffer(struct scsi_device *sdev, u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
James Bottomley33aa6872005-08-28 11:31:14 -0500752 int l, result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 /* first off do a test unit ready. This can error out
755 * because of reservations or some other reason. If it
756 * fails, the device won't let us write to the echo buffer
757 * so just return failure */
758
759 const char spi_test_unit_ready[] = {
760 TEST_UNIT_READY, 0, 0, 0, 0, 0
761 };
762
763 const char spi_read_buffer_descriptor[] = {
764 READ_BUFFER, 0x0b, 0, 0, 0, 0, 0, 0, 4, 0
765 };
766
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 /* We send a set of three TURs to clear any outstanding
769 * unit attention conditions if they exist (Otherwise the
770 * buffer tests won't be happy). If the TUR still fails
771 * (reservation conflict, device not ready, etc) just
772 * skip the write tests */
773 for (l = 0; ; l++) {
James Bottomley33aa6872005-08-28 11:31:14 -0500774 result = spi_execute(sdev, spi_test_unit_ready, DMA_NONE,
775 NULL, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
James Bottomley33aa6872005-08-28 11:31:14 -0500777 if(result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 if(l >= 3)
779 return 0;
780 } else {
781 /* TUR succeeded */
782 break;
783 }
784 }
785
James Bottomley33aa6872005-08-28 11:31:14 -0500786 result = spi_execute(sdev, spi_read_buffer_descriptor,
787 DMA_FROM_DEVICE, buffer, 4, 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 /* Device has no echo buffer */
791 return 0;
792
793 return buffer[3] + ((buffer[2] & 0x1f) << 8);
794}
795
796static void
James Bottomley33aa6872005-08-28 11:31:14 -0500797spi_dv_device_internal(struct scsi_device *sdev, u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
James Bottomley33aa6872005-08-28 11:31:14 -0500799 struct spi_internal *i = to_spi_internal(sdev->host->transportt);
James Bottomley 62a86122005-05-06 18:05:20 -0500800 struct scsi_target *starget = sdev->sdev_target;
James Bottomley60eef252006-06-10 10:51:23 -0500801 struct Scsi_Host *shost = sdev->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 int len = sdev->inquiry_len;
James Bottomley23028272007-09-22 08:40:09 -0500803 int min_period = spi_min_period(starget);
804 int max_width = spi_max_width(starget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 /* first set us up for narrow async */
806 DV_SET(offset, 0);
807 DV_SET(width, 0);
James Bottomley23028272007-09-22 08:40:09 -0500808
James Bottomley33aa6872005-08-28 11:31:14 -0500809 if (spi_dv_device_compare_inquiry(sdev, buffer, buffer, DV_LOOPS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 != SPI_COMPARE_SUCCESS) {
James Bottomley9ccfc752005-10-02 11:45:08 -0500811 starget_printk(KERN_ERR, starget, "Domain Validation Initial Inquiry Failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 /* FIXME: should probably offline the device here? */
813 return;
814 }
815
James Bottomley23028272007-09-22 08:40:09 -0500816 if (!scsi_device_wide(sdev)) {
817 spi_max_width(starget) = 0;
818 max_width = 0;
819 }
820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 /* test width */
James Bottomley23028272007-09-22 08:40:09 -0500822 if (i->f->set_width && max_width) {
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500823 i->f->set_width(starget, 1);
James Bottomley 62a86122005-05-06 18:05:20 -0500824
James Bottomley33aa6872005-08-28 11:31:14 -0500825 if (spi_dv_device_compare_inquiry(sdev, buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 buffer + len,
827 DV_LOOPS)
828 != SPI_COMPARE_SUCCESS) {
James Bottomley9ccfc752005-10-02 11:45:08 -0500829 starget_printk(KERN_ERR, starget, "Wide Transfers Fail\n");
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500830 i->f->set_width(starget, 0);
James Bottomley23028272007-09-22 08:40:09 -0500831 /* Make sure we don't force wide back on by asking
832 * for a transfer period that requires it */
833 max_width = 0;
834 if (min_period < 10)
835 min_period = 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 }
837 }
838
839 if (!i->f->set_period)
840 return;
841
842 /* device can't handle synchronous */
James Bottomleyeb1dd682005-07-02 12:22:01 -0400843 if (!scsi_device_sync(sdev) && !scsi_device_dt(sdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return;
845
James Bottomley349cd7c2005-11-28 15:41:58 -0600846 /* len == -1 is the signal that we need to ascertain the
847 * presence of an echo buffer before trying to use it. len ==
848 * 0 means we don't have an echo buffer */
849 len = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 retry:
852
853 /* now set up to the maximum */
James Bottomley 62a86122005-05-06 18:05:20 -0500854 DV_SET(offset, spi_max_offset(starget));
James Bottomley23028272007-09-22 08:40:09 -0500855 DV_SET(period, min_period);
856
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500857 /* try QAS requests; this should be harmless to set if the
858 * target supports it */
James Bottomleydfdc58b2006-09-20 12:00:18 -0400859 if (scsi_device_qas(sdev)) {
James Bottomleyeb1dd682005-07-02 12:22:01 -0400860 DV_SET(qas, 1);
James Bottomleydfdc58b2006-09-20 12:00:18 -0400861 } else {
862 DV_SET(qas, 0);
863 }
864
James Bottomley23028272007-09-22 08:40:09 -0500865 if (scsi_device_ius(sdev) && min_period < 9) {
James Bottomleydfdc58b2006-09-20 12:00:18 -0400866 /* This u320 (or u640). Set IU transfers */
James Bottomleyeb1dd682005-07-02 12:22:01 -0400867 DV_SET(iu, 1);
James Bottomleydfdc58b2006-09-20 12:00:18 -0400868 /* Then set the optional parameters */
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500869 DV_SET(rd_strm, 1);
870 DV_SET(wr_flow, 1);
871 DV_SET(rti, 1);
James Bottomley23028272007-09-22 08:40:09 -0500872 if (min_period == 8)
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500873 DV_SET(pcomp_en, 1);
James Bottomleydfdc58b2006-09-20 12:00:18 -0400874 } else {
875 DV_SET(iu, 0);
James Bottomley9a8bc9b2005-05-31 18:35:39 -0500876 }
James Bottomleydfdc58b2006-09-20 12:00:18 -0400877
James Bottomley60eef252006-06-10 10:51:23 -0500878 /* now that we've done all this, actually check the bus
879 * signal type (if known). Some devices are stupid on
880 * a SE bus and still claim they can try LVD only settings */
881 if (i->f->get_signalling)
882 i->f->get_signalling(shost);
883 if (spi_signalling(shost) == SPI_SIGNAL_SE ||
James Bottomleydfdc58b2006-09-20 12:00:18 -0400884 spi_signalling(shost) == SPI_SIGNAL_HVD ||
885 !scsi_device_dt(sdev)) {
James Bottomley60eef252006-06-10 10:51:23 -0500886 DV_SET(dt, 0);
James Bottomleydfdc58b2006-09-20 12:00:18 -0400887 } else {
888 DV_SET(dt, 1);
889 }
James Bottomley23028272007-09-22 08:40:09 -0500890 /* set width last because it will pull all the other
891 * parameters down to required values */
892 DV_SET(width, max_width);
893
James Bottomley349cd7c2005-11-28 15:41:58 -0600894 /* Do the read only INQUIRY tests */
895 spi_dv_retrain(sdev, buffer, buffer + sdev->inquiry_len,
896 spi_dv_device_compare_inquiry);
897 /* See if we actually managed to negotiate and sustain DT */
898 if (i->f->get_dt)
899 i->f->get_dt(starget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
James Bottomley349cd7c2005-11-28 15:41:58 -0600901 /* see if the device has an echo buffer. If it does we can do
902 * the SPI pattern write tests. Because of some broken
903 * devices, we *only* try this on a device that has actually
904 * negotiated DT */
905
906 if (len == -1 && spi_dt(starget))
907 len = spi_dv_device_get_echo_buffer(sdev, buffer);
908
909 if (len <= 0) {
James Bottomley9ccfc752005-10-02 11:45:08 -0500910 starget_printk(KERN_INFO, starget, "Domain Validation skipping write tests\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 return;
912 }
913
914 if (len > SPI_MAX_ECHO_BUFFER_SIZE) {
James Bottomley9ccfc752005-10-02 11:45:08 -0500915 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 -0700916 len = SPI_MAX_ECHO_BUFFER_SIZE;
917 }
918
James Bottomley33aa6872005-08-28 11:31:14 -0500919 if (spi_dv_retrain(sdev, buffer, buffer + len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 spi_dv_device_echo_buffer)
921 == SPI_COMPARE_SKIP_TEST) {
922 /* OK, the stupid drive can't do a write echo buffer
923 * test after all, fall back to the read tests */
924 len = 0;
925 goto retry;
926 }
927}
928
929
930/** spi_dv_device - Do Domain Validation on the device
931 * @sdev: scsi device to validate
932 *
933 * Performs the domain validation on the given device in the
934 * current execution thread. Since DV operations may sleep,
935 * the current thread must have user context. Also no SCSI
936 * related locks that would deadlock I/O issued by the DV may
937 * be held.
938 */
939void
940spi_dv_device(struct scsi_device *sdev)
941{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 struct scsi_target *starget = sdev->sdev_target;
943 u8 *buffer;
944 const int len = SPI_MAX_ECHO_BUFFER_SIZE*2;
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 if (unlikely(scsi_device_get(sdev)))
James Bottomley33aa6872005-08-28 11:31:14 -0500947 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
James Bottomleydfdc58b2006-09-20 12:00:18 -0400949 if (unlikely(spi_dv_in_progress(starget)))
950 return;
951 spi_dv_in_progress(starget) = 1;
952
Jes Sorensen24669f752006-01-16 10:31:18 -0500953 buffer = kzalloc(len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955 if (unlikely(!buffer))
956 goto out_put;
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 /* We need to verify that the actual device will quiesce; the
959 * later target quiesce is just a nice to have */
960 if (unlikely(scsi_device_quiesce(sdev)))
961 goto out_free;
962
963 scsi_target_quiesce(starget);
964
965 spi_dv_pending(starget) = 1;
Jes Sorensend158d262006-01-13 16:05:44 -0800966 mutex_lock(&spi_dv_mutex(starget));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
James Bottomley9ccfc752005-10-02 11:45:08 -0500968 starget_printk(KERN_INFO, starget, "Beginning Domain Validation\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
James Bottomley33aa6872005-08-28 11:31:14 -0500970 spi_dv_device_internal(sdev, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
James Bottomley9ccfc752005-10-02 11:45:08 -0500972 starget_printk(KERN_INFO, starget, "Ending Domain Validation\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Jes Sorensend158d262006-01-13 16:05:44 -0800974 mutex_unlock(&spi_dv_mutex(starget));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 spi_dv_pending(starget) = 0;
976
977 scsi_target_resume(starget);
978
979 spi_initial_dv(starget) = 1;
980
981 out_free:
982 kfree(buffer);
983 out_put:
James Bottomleydfdc58b2006-09-20 12:00:18 -0400984 spi_dv_in_progress(starget) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 scsi_device_put(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986}
987EXPORT_SYMBOL(spi_dv_device);
988
989struct work_queue_wrapper {
990 struct work_struct work;
991 struct scsi_device *sdev;
992};
993
994static void
David Howellsc4028952006-11-22 14:57:56 +0000995spi_dv_device_work_wrapper(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
David Howellsc4028952006-11-22 14:57:56 +0000997 struct work_queue_wrapper *wqw =
998 container_of(work, struct work_queue_wrapper, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 struct scsi_device *sdev = wqw->sdev;
1000
1001 kfree(wqw);
1002 spi_dv_device(sdev);
1003 spi_dv_pending(sdev->sdev_target) = 0;
1004 scsi_device_put(sdev);
1005}
1006
1007
1008/**
1009 * spi_schedule_dv_device - schedule domain validation to occur on the device
1010 * @sdev: The device to validate
1011 *
1012 * Identical to spi_dv_device() above, except that the DV will be
1013 * scheduled to occur in a workqueue later. All memory allocations
1014 * are atomic, so may be called from any context including those holding
1015 * SCSI locks.
1016 */
1017void
1018spi_schedule_dv_device(struct scsi_device *sdev)
1019{
1020 struct work_queue_wrapper *wqw =
1021 kmalloc(sizeof(struct work_queue_wrapper), GFP_ATOMIC);
1022
1023 if (unlikely(!wqw))
1024 return;
1025
1026 if (unlikely(spi_dv_pending(sdev->sdev_target))) {
1027 kfree(wqw);
1028 return;
1029 }
1030 /* Set pending early (dv_device doesn't check it, only sets it) */
1031 spi_dv_pending(sdev->sdev_target) = 1;
1032 if (unlikely(scsi_device_get(sdev))) {
1033 kfree(wqw);
1034 spi_dv_pending(sdev->sdev_target) = 0;
1035 return;
1036 }
1037
David Howellsc4028952006-11-22 14:57:56 +00001038 INIT_WORK(&wqw->work, spi_dv_device_work_wrapper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 wqw->sdev = sdev;
1040
1041 schedule_work(&wqw->work);
1042}
1043EXPORT_SYMBOL(spi_schedule_dv_device);
1044
1045/**
1046 * spi_display_xfer_agreement - Print the current target transfer agreement
1047 * @starget: The target for which to display the agreement
1048 *
1049 * Each SPI port is required to maintain a transfer agreement for each
1050 * other port on the bus. This function prints a one-line summary of
1051 * the current agreement; more detailed information is available in sysfs.
1052 */
1053void spi_display_xfer_agreement(struct scsi_target *starget)
1054{
1055 struct spi_transport_attrs *tp;
1056 tp = (struct spi_transport_attrs *)&starget->starget_data;
1057
1058 if (tp->offset > 0 && tp->period > 0) {
1059 unsigned int picosec, kb100;
1060 char *scsi = "FAST-?";
1061 char tmp[8];
1062
1063 if (tp->period <= SPI_STATIC_PPR) {
1064 picosec = ppr_to_ps[tp->period];
1065 switch (tp->period) {
1066 case 7: scsi = "FAST-320"; break;
1067 case 8: scsi = "FAST-160"; break;
1068 case 9: scsi = "FAST-80"; break;
1069 case 10:
1070 case 11: scsi = "FAST-40"; break;
1071 case 12: scsi = "FAST-20"; break;
1072 }
1073 } else {
1074 picosec = tp->period * 4000;
1075 if (tp->period < 25)
1076 scsi = "FAST-20";
1077 else if (tp->period < 50)
1078 scsi = "FAST-10";
1079 else
1080 scsi = "FAST-5";
1081 }
1082
1083 kb100 = (10000000 + picosec / 2) / picosec;
1084 if (tp->width)
1085 kb100 *= 2;
1086 sprint_frac(tmp, picosec, 1000);
1087
1088 dev_info(&starget->dev,
James Bottomleyd872ebe2005-08-03 15:43:52 -05001089 "%s %sSCSI %d.%d MB/s %s%s%s%s%s%s%s%s (%s ns, offset %d)\n",
1090 scsi, tp->width ? "WIDE " : "", kb100/10, kb100 % 10,
1091 tp->dt ? "DT" : "ST",
1092 tp->iu ? " IU" : "",
1093 tp->qas ? " QAS" : "",
1094 tp->rd_strm ? " RDSTRM" : "",
1095 tp->rti ? " RTI" : "",
1096 tp->wr_flow ? " WRFLOW" : "",
1097 tp->pcomp_en ? " PCOMP" : "",
1098 tp->hold_mcs ? " HMCS" : "",
1099 tmp, tp->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 } else {
Matthew Wilcox493ff4e2005-11-17 11:13:43 -07001101 dev_info(&starget->dev, "%sasynchronous\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 tp->width ? "wide " : "");
1103 }
1104}
1105EXPORT_SYMBOL(spi_display_xfer_agreement);
1106
Matthew Wilcox6ea3c0b2006-02-07 07:54:46 -07001107int spi_populate_width_msg(unsigned char *msg, int width)
1108{
1109 msg[0] = EXTENDED_MESSAGE;
1110 msg[1] = 2;
1111 msg[2] = EXTENDED_WDTR;
1112 msg[3] = width;
1113 return 4;
1114}
James Bottomley38e14f82006-02-17 14:58:47 -08001115EXPORT_SYMBOL_GPL(spi_populate_width_msg);
Matthew Wilcox6ea3c0b2006-02-07 07:54:46 -07001116
1117int spi_populate_sync_msg(unsigned char *msg, int period, int offset)
1118{
1119 msg[0] = EXTENDED_MESSAGE;
1120 msg[1] = 3;
1121 msg[2] = EXTENDED_SDTR;
1122 msg[3] = period;
1123 msg[4] = offset;
1124 return 5;
1125}
James Bottomley38e14f82006-02-17 14:58:47 -08001126EXPORT_SYMBOL_GPL(spi_populate_sync_msg);
Matthew Wilcox6ea3c0b2006-02-07 07:54:46 -07001127
1128int spi_populate_ppr_msg(unsigned char *msg, int period, int offset,
1129 int width, int options)
1130{
1131 msg[0] = EXTENDED_MESSAGE;
1132 msg[1] = 6;
1133 msg[2] = EXTENDED_PPR;
1134 msg[3] = period;
1135 msg[4] = 0;
1136 msg[5] = offset;
1137 msg[6] = width;
1138 msg[7] = options;
1139 return 8;
1140}
James Bottomley38e14f82006-02-17 14:58:47 -08001141EXPORT_SYMBOL_GPL(spi_populate_ppr_msg);
Matthew Wilcox6ea3c0b2006-02-07 07:54:46 -07001142
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001143#ifdef CONFIG_SCSI_CONSTANTS
1144static const char * const one_byte_msgs[] = {
Matthew Wilcox72df0eb2006-03-10 07:18:22 -07001145/* 0x00 */ "Task Complete", NULL /* Extended Message */, "Save Pointers",
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001146/* 0x03 */ "Restore Pointers", "Disconnect", "Initiator Error",
Matthew Wilcox72df0eb2006-03-10 07:18:22 -07001147/* 0x06 */ "Abort Task Set", "Message Reject", "Nop", "Message Parity Error",
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001148/* 0x0a */ "Linked Command Complete", "Linked Command Complete w/flag",
Matthew Wilcox72df0eb2006-03-10 07:18:22 -07001149/* 0x0c */ "Target Reset", "Abort Task", "Clear Task Set",
1150/* 0x0f */ "Initiate Recovery", "Release Recovery",
1151/* 0x11 */ "Terminate Process", "Continue Task", "Target Transfer Disable",
1152/* 0x14 */ NULL, NULL, "Clear ACA", "LUN Reset"
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001153};
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001154
1155static const char * const two_byte_msgs[] = {
Matthew Wilcox47972152005-12-15 16:22:01 -05001156/* 0x20 */ "Simple Queue Tag", "Head of Queue Tag", "Ordered Queue Tag",
Matthew Wilcox72df0eb2006-03-10 07:18:22 -07001157/* 0x23 */ "Ignore Wide Residue", "ACA"
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001158};
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001159
1160static const char * const extended_msgs[] = {
1161/* 0x00 */ "Modify Data Pointer", "Synchronous Data Transfer Request",
Matthew Wilcoxef725822005-12-15 16:22:01 -05001162/* 0x02 */ "SCSI-I Extended Identify", "Wide Data Transfer Request",
Matthew Wilcoxfc253072006-02-18 20:52:31 -07001163/* 0x04 */ "Parallel Protocol Request", "Modify Bidirectional Data Pointer"
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001164};
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001165
Adrian Bunkbdd70f22006-01-05 23:39:18 +01001166static void print_nego(const unsigned char *msg, int per, int off, int width)
Matthew Wilcoxef725822005-12-15 16:22:01 -05001167{
1168 if (per) {
1169 char buf[20];
1170 period_to_str(buf, msg[per]);
1171 printk("period = %s ns ", buf);
1172 }
1173
1174 if (off)
1175 printk("offset = %d ", msg[off]);
1176 if (width)
1177 printk("width = %d ", 8 << msg[width]);
1178}
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001179
Matthew Wilcoxfc253072006-02-18 20:52:31 -07001180static void print_ptr(const unsigned char *msg, int msb, const char *desc)
1181{
1182 int ptr = (msg[msb] << 24) | (msg[msb+1] << 16) | (msg[msb+2] << 8) |
1183 msg[msb+3];
1184 printk("%s = %d ", desc, ptr);
1185}
1186
Matthew Wilcox1abfd372005-12-15 16:22:01 -05001187int spi_print_msg(const unsigned char *msg)
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001188{
Matthew Wilcox72df0eb2006-03-10 07:18:22 -07001189 int len = 1, i;
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001190 if (msg[0] == EXTENDED_MESSAGE) {
Matthew Wilcoxfc253072006-02-18 20:52:31 -07001191 len = 2 + msg[1];
1192 if (len == 2)
1193 len += 256;
Matthew Wilcoxb32aaff2005-12-15 16:22:01 -05001194 if (msg[2] < ARRAY_SIZE(extended_msgs))
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001195 printk ("%s ", extended_msgs[msg[2]]);
1196 else
1197 printk ("Extended Message, reserved code (0x%02x) ",
1198 (int) msg[2]);
1199 switch (msg[2]) {
1200 case EXTENDED_MODIFY_DATA_POINTER:
Matthew Wilcoxfc253072006-02-18 20:52:31 -07001201 print_ptr(msg, 3, "pointer");
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001202 break;
1203 case EXTENDED_SDTR:
Matthew Wilcoxef725822005-12-15 16:22:01 -05001204 print_nego(msg, 3, 4, 0);
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001205 break;
1206 case EXTENDED_WDTR:
Matthew Wilcoxef725822005-12-15 16:22:01 -05001207 print_nego(msg, 0, 0, 3);
1208 break;
1209 case EXTENDED_PPR:
1210 print_nego(msg, 3, 5, 6);
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001211 break;
Matthew Wilcoxfc253072006-02-18 20:52:31 -07001212 case EXTENDED_MODIFY_BIDI_DATA_PTR:
1213 print_ptr(msg, 3, "out");
1214 print_ptr(msg, 7, "in");
1215 break;
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001216 default:
1217 for (i = 2; i < len; ++i)
1218 printk("%02x ", msg[i]);
1219 }
1220 /* Identify */
1221 } else if (msg[0] & 0x80) {
1222 printk("Identify disconnect %sallowed %s %d ",
1223 (msg[0] & 0x40) ? "" : "not ",
1224 (msg[0] & 0x20) ? "target routine" : "lun",
1225 msg[0] & 0x7);
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001226 /* Normal One byte */
1227 } else if (msg[0] < 0x1f) {
Matthew Wilcox72df0eb2006-03-10 07:18:22 -07001228 if (msg[0] < ARRAY_SIZE(one_byte_msgs) && one_byte_msgs[msg[0]])
Matthew Wilcoxe24d8732006-02-07 08:05:26 -07001229 printk("%s ", one_byte_msgs[msg[0]]);
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001230 else
1231 printk("reserved (%02x) ", msg[0]);
Matthew Wilcox72df0eb2006-03-10 07:18:22 -07001232 } else if (msg[0] == 0x55) {
1233 printk("QAS Request ");
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001234 /* Two byte */
1235 } else if (msg[0] <= 0x2f) {
Matthew Wilcoxb32aaff2005-12-15 16:22:01 -05001236 if ((msg[0] - 0x20) < ARRAY_SIZE(two_byte_msgs))
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001237 printk("%s %02x ", two_byte_msgs[msg[0] - 0x20],
1238 msg[1]);
1239 else
1240 printk("reserved two byte (%02x %02x) ",
1241 msg[0], msg[1]);
1242 len = 2;
1243 } else
Matthew Wilcoxe24d8732006-02-07 08:05:26 -07001244 printk("reserved ");
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001245 return len;
1246}
Matthew Wilcox1abfd372005-12-15 16:22:01 -05001247EXPORT_SYMBOL(spi_print_msg);
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001248
1249#else /* ifndef CONFIG_SCSI_CONSTANTS */
1250
Matthew Wilcox1abfd372005-12-15 16:22:01 -05001251int spi_print_msg(const unsigned char *msg)
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001252{
Matthew Wilcox72df0eb2006-03-10 07:18:22 -07001253 int len = 1, i;
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001254
1255 if (msg[0] == EXTENDED_MESSAGE) {
Matthew Wilcoxfc253072006-02-18 20:52:31 -07001256 len = 2 + msg[1];
1257 if (len == 2)
1258 len += 256;
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001259 for (i = 0; i < len; ++i)
1260 printk("%02x ", msg[i]);
1261 /* Identify */
1262 } else if (msg[0] & 0x80) {
1263 printk("%02x ", msg[0]);
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001264 /* Normal One byte */
James Bottomley597705a2006-03-12 09:54:19 -06001265 } else if ((msg[0] < 0x1f) || (msg[0] == 0x55)) {
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001266 printk("%02x ", msg[0]);
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001267 /* Two byte */
1268 } else if (msg[0] <= 0x2f) {
1269 printk("%02x %02x", msg[0], msg[1]);
1270 len = 2;
1271 } else
1272 printk("%02x ", msg[0]);
1273 return len;
1274}
Matthew Wilcox1abfd372005-12-15 16:22:01 -05001275EXPORT_SYMBOL(spi_print_msg);
Matthew Wilcox410ca5c2005-12-15 16:22:01 -05001276#endif /* ! CONFIG_SCSI_CONSTANTS */
1277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278static int spi_device_match(struct attribute_container *cont,
1279 struct device *dev)
1280{
1281 struct scsi_device *sdev;
1282 struct Scsi_Host *shost;
James Bottomley10c1b882005-08-14 14:34:06 -05001283 struct spi_internal *i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 if (!scsi_is_sdev_device(dev))
1286 return 0;
1287
1288 sdev = to_scsi_device(dev);
1289 shost = sdev->host;
1290 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1291 != &spi_host_class.class)
1292 return 0;
1293 /* Note: this class has no device attributes, so it has
1294 * no per-HBA allocation and thus we don't need to distinguish
1295 * the attribute containers for the device */
James Bottomley10c1b882005-08-14 14:34:06 -05001296 i = to_spi_internal(shost->transportt);
1297 if (i->f->deny_binding && i->f->deny_binding(sdev->sdev_target))
1298 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 return 1;
1300}
1301
1302static int spi_target_match(struct attribute_container *cont,
1303 struct device *dev)
1304{
1305 struct Scsi_Host *shost;
James Bottomley10c1b882005-08-14 14:34:06 -05001306 struct scsi_target *starget;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 struct spi_internal *i;
1308
1309 if (!scsi_is_target_device(dev))
1310 return 0;
1311
1312 shost = dev_to_shost(dev->parent);
1313 if (!shost->transportt || shost->transportt->host_attrs.ac.class
1314 != &spi_host_class.class)
1315 return 0;
1316
1317 i = to_spi_internal(shost->transportt);
James Bottomley10c1b882005-08-14 14:34:06 -05001318 starget = to_scsi_target(dev);
1319
1320 if (i->f->deny_binding && i->f->deny_binding(starget))
1321 return 0;
1322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 return &i->t.target_attrs.ac == cont;
1324}
1325
1326static DECLARE_TRANSPORT_CLASS(spi_transport_class,
1327 "spi_transport",
1328 spi_setup_transport_attrs,
1329 NULL,
James Bottomley9b161a42008-01-05 10:18:27 -06001330 spi_target_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
1332static DECLARE_ANON_TRANSPORT_CLASS(spi_device_class,
1333 spi_device_match,
1334 spi_device_configure);
1335
James Bottomley9b161a42008-01-05 10:18:27 -06001336static struct attribute *host_attributes[] = {
1337 &class_device_attr_signalling.attr,
1338 NULL
1339};
1340
1341static struct attribute_group host_attribute_group = {
1342 .attrs = host_attributes,
1343};
1344
1345static int spi_host_configure(struct transport_container *tc,
1346 struct device *dev,
1347 struct class_device *cdev)
1348{
1349 struct kobject *kobj = &cdev->kobj;
1350 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1351 struct spi_internal *si = to_spi_internal(shost->transportt);
1352 struct attribute *attr = &class_device_attr_signalling.attr;
1353 int rc = 0;
1354
1355 if (si->f->set_signalling)
1356 rc = sysfs_chmod_file(kobj, attr, attr->mode | S_IWUSR);
1357
1358 return rc;
1359}
1360
1361/* returns true if we should be showing the variable. Also
1362 * overloads the return by setting 1<<1 if the attribute should
1363 * be writeable */
1364#define TARGET_ATTRIBUTE_HELPER(name) \
1365 (si->f->show_##name ? 1 : 0) + \
1366 (si->f->set_##name ? 2 : 0)
1367
1368static int target_attribute_is_visible(struct kobject *kobj,
1369 struct attribute *attr, int i)
1370{
1371 struct class_device *cdev =
1372 container_of(kobj, struct class_device, kobj);
1373 struct scsi_target *starget = transport_class_to_starget(cdev);
1374 struct Scsi_Host *shost = transport_class_to_shost(cdev);
1375 struct spi_internal *si = to_spi_internal(shost->transportt);
1376
1377 if (attr == &class_device_attr_period.attr &&
1378 spi_support_sync(starget))
1379 return TARGET_ATTRIBUTE_HELPER(period);
1380 else if (attr == &class_device_attr_min_period.attr &&
1381 spi_support_sync(starget))
1382 return TARGET_ATTRIBUTE_HELPER(period);
1383 else if (attr == &class_device_attr_offset.attr &&
1384 spi_support_sync(starget))
1385 return TARGET_ATTRIBUTE_HELPER(offset);
1386 else if (attr == &class_device_attr_max_offset.attr &&
1387 spi_support_sync(starget))
1388 return TARGET_ATTRIBUTE_HELPER(offset);
1389 else if (attr == &class_device_attr_width.attr &&
1390 spi_support_wide(starget))
1391 return TARGET_ATTRIBUTE_HELPER(width);
1392 else if (attr == &class_device_attr_max_width.attr &&
1393 spi_support_wide(starget))
1394 return TARGET_ATTRIBUTE_HELPER(width);
1395 else if (attr == &class_device_attr_iu.attr &&
1396 spi_support_ius(starget))
1397 return TARGET_ATTRIBUTE_HELPER(iu);
1398 else if (attr == &class_device_attr_dt.attr &&
1399 spi_support_dt(starget))
1400 return TARGET_ATTRIBUTE_HELPER(dt);
1401 else if (attr == &class_device_attr_qas.attr &&
1402 spi_support_qas(starget))
1403 return TARGET_ATTRIBUTE_HELPER(qas);
1404 else if (attr == &class_device_attr_wr_flow.attr &&
1405 spi_support_ius(starget))
1406 return TARGET_ATTRIBUTE_HELPER(wr_flow);
1407 else if (attr == &class_device_attr_rd_strm.attr &&
1408 spi_support_ius(starget))
1409 return TARGET_ATTRIBUTE_HELPER(rd_strm);
1410 else if (attr == &class_device_attr_rti.attr &&
1411 spi_support_ius(starget))
1412 return TARGET_ATTRIBUTE_HELPER(rti);
1413 else if (attr == &class_device_attr_pcomp_en.attr &&
1414 spi_support_ius(starget))
1415 return TARGET_ATTRIBUTE_HELPER(pcomp_en);
1416 else if (attr == &class_device_attr_hold_mcs.attr &&
1417 spi_support_ius(starget))
1418 return TARGET_ATTRIBUTE_HELPER(hold_mcs);
1419 else if (attr == &class_device_attr_revalidate.attr)
1420 return 1;
1421
1422 return 0;
1423}
1424
1425static struct attribute *target_attributes[] = {
1426 &class_device_attr_period.attr,
1427 &class_device_attr_min_period.attr,
1428 &class_device_attr_offset.attr,
1429 &class_device_attr_max_offset.attr,
1430 &class_device_attr_width.attr,
1431 &class_device_attr_max_width.attr,
1432 &class_device_attr_iu.attr,
1433 &class_device_attr_dt.attr,
1434 &class_device_attr_qas.attr,
1435 &class_device_attr_wr_flow.attr,
1436 &class_device_attr_rd_strm.attr,
1437 &class_device_attr_rti.attr,
1438 &class_device_attr_pcomp_en.attr,
1439 &class_device_attr_hold_mcs.attr,
1440 &class_device_attr_revalidate.attr,
1441 NULL
1442};
1443
1444static struct attribute_group target_attribute_group = {
1445 .attrs = target_attributes,
1446 .is_visible = target_attribute_is_visible,
1447};
1448
1449static int spi_target_configure(struct transport_container *tc,
1450 struct device *dev,
1451 struct class_device *cdev)
1452{
1453 struct kobject *kobj = &cdev->kobj;
1454 int i;
1455 struct attribute *attr;
1456 int rc;
1457
1458 for (i = 0; (attr = target_attributes[i]) != NULL; i++) {
1459 int j = target_attribute_group.is_visible(kobj, attr, i);
1460
1461 /* FIXME: as well as returning -EEXIST, which we'd like
1462 * to ignore, sysfs also does a WARN_ON and dumps a trace,
1463 * which is bad, so temporarily, skip attributes that are
1464 * already visible (the revalidate one) */
1465 if (j && attr != &class_device_attr_revalidate.attr)
1466 rc = sysfs_add_file_to_group(kobj, attr,
1467 target_attribute_group.name);
1468 /* and make the attribute writeable if we have a set
1469 * function */
1470 if ((j & 1))
1471 rc = sysfs_chmod_file(kobj, attr, attr->mode | S_IWUSR);
1472 }
1473
1474 return 0;
1475}
1476
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477struct scsi_transport_template *
1478spi_attach_transport(struct spi_function_template *ft)
1479{
Jes Sorensen24669f752006-01-16 10:31:18 -05001480 struct spi_internal *i = kzalloc(sizeof(struct spi_internal),
1481 GFP_KERNEL);
1482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 if (unlikely(!i))
1484 return NULL;
1485
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 i->t.target_attrs.ac.class = &spi_transport_class.class;
James Bottomley9b161a42008-01-05 10:18:27 -06001487 i->t.target_attrs.ac.grp = &target_attribute_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 i->t.target_attrs.ac.match = spi_target_match;
1489 transport_container_register(&i->t.target_attrs);
1490 i->t.target_size = sizeof(struct spi_transport_attrs);
1491 i->t.host_attrs.ac.class = &spi_host_class.class;
James Bottomley9b161a42008-01-05 10:18:27 -06001492 i->t.host_attrs.ac.grp = &host_attribute_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 i->t.host_attrs.ac.match = spi_host_match;
1494 transport_container_register(&i->t.host_attrs);
1495 i->t.host_size = sizeof(struct spi_host_attrs);
1496 i->f = ft;
1497
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 return &i->t;
1499}
1500EXPORT_SYMBOL(spi_attach_transport);
1501
1502void spi_release_transport(struct scsi_transport_template *t)
1503{
1504 struct spi_internal *i = to_spi_internal(t);
1505
1506 transport_container_unregister(&i->t.target_attrs);
1507 transport_container_unregister(&i->t.host_attrs);
1508
1509 kfree(i);
1510}
1511EXPORT_SYMBOL(spi_release_transport);
1512
1513static __init int spi_transport_init(void)
1514{
1515 int error = transport_class_register(&spi_transport_class);
1516 if (error)
1517 return error;
1518 error = anon_transport_class_register(&spi_device_class);
1519 return transport_class_register(&spi_host_class);
1520}
1521
1522static void __exit spi_transport_exit(void)
1523{
1524 transport_class_unregister(&spi_transport_class);
1525 anon_transport_class_unregister(&spi_device_class);
1526 transport_class_unregister(&spi_host_class);
1527}
1528
1529MODULE_AUTHOR("Martin Hicks");
1530MODULE_DESCRIPTION("SPI Transport Attributes");
1531MODULE_LICENSE("GPL");
1532
1533module_init(spi_transport_init);
1534module_exit(spi_transport_exit);