Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Parallel SCSI (SPI) transport specific attributes exported to sysfs. |
| 3 | * |
| 4 | * Copyright (c) 2003 Silicon Graphics, Inc. All rights reserved. |
| 5 | * Copyright (c) 2004, 2005 James Bottomley <James.Bottomley@SteelEye.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | */ |
| 21 | #include <linux/ctype.h> |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/workqueue.h> |
James Bottomley | 949bf79 | 2005-05-01 18:58:15 -0500 | [diff] [blame] | 25 | #include <linux/blkdev.h> |
Jes Sorensen | d158d26 | 2006-01-13 16:05:44 -0800 | [diff] [blame] | 26 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <scsi/scsi.h> |
| 28 | #include "scsi_priv.h" |
| 29 | #include <scsi/scsi_device.h> |
| 30 | #include <scsi/scsi_host.h> |
James Bottomley | 33aa687 | 2005-08-28 11:31:14 -0500 | [diff] [blame] | 31 | #include <scsi/scsi_cmnd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <scsi/scsi_eh.h> |
| 33 | #include <scsi/scsi_transport.h> |
| 34 | #include <scsi/scsi_transport_spi.h> |
| 35 | |
James Bottomley | d872ebe | 2005-08-03 15:43:52 -0500 | [diff] [blame] | 36 | #define SPI_NUM_ATTRS 14 /* increase this if you add attributes */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #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 Bottomley | 949bf79 | 2005-05-01 18:58:15 -0500 | [diff] [blame] | 43 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | /* Private data accessors (keep these out of the header file) */ |
James Bottomley | dfdc58b | 2006-09-20 12:00:18 -0400 | [diff] [blame] | 49 | #define spi_dv_in_progress(x) (((struct spi_transport_attrs *)&(x)->starget_data)->dv_in_progress) |
Jes Sorensen | d158d26 | 2006-01-13 16:05:44 -0800 | [diff] [blame] | 50 | #define spi_dv_mutex(x) (((struct spi_transport_attrs *)&(x)->starget_data)->dv_mutex) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
| 52 | struct spi_internal { |
| 53 | struct scsi_transport_template t; |
| 54 | struct spi_function_template *f; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | #define to_spi_internal(tmpl) container_of(tmpl, struct spi_internal, t) |
| 58 | |
| 59 | static 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 | |
| 80 | static 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 Bottomley | 33aa687 | 2005-08-28 11:31:14 -0500 | [diff] [blame] | 100 | static 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 Bottomley | 949bf79 | 2005-05-01 18:58:15 -0500 | [diff] [blame] | 104 | { |
James Bottomley | 33aa687 | 2005-08-28 11:31:14 -0500 | [diff] [blame] | 105 | int i, result; |
| 106 | unsigned char sense[SCSI_SENSE_BUFFERSIZE]; |
James Bottomley | 949bf79 | 2005-05-01 18:58:15 -0500 | [diff] [blame] | 107 | |
| 108 | for(i = 0; i < DV_RETRIES; i++) { |
James Bottomley | 33aa687 | 2005-08-28 11:31:14 -0500 | [diff] [blame] | 109 | 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 Bottomley | 949bf79 | 2005-05-01 18:58:15 -0500 | [diff] [blame] | 116 | |
James Bottomley | 4ed381e | 2006-12-11 09:47:06 -0600 | [diff] [blame] | 117 | if (scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, |
James Bottomley | 33aa687 | 2005-08-28 11:31:14 -0500 | [diff] [blame] | 118 | sshdr) |
| 119 | && sshdr->sense_key == UNIT_ATTENTION) |
James Bottomley | 949bf79 | 2005-05-01 18:58:15 -0500 | [diff] [blame] | 120 | continue; |
| 121 | } |
| 122 | break; |
| 123 | } |
James Bottomley | 33aa687 | 2005-08-28 11:31:14 -0500 | [diff] [blame] | 124 | return result; |
James Bottomley | 949bf79 | 2005-05-01 18:58:15 -0500 | [diff] [blame] | 125 | } |
| 126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | static 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 | |
| 137 | static inline const char *spi_signal_to_string(enum spi_signal_type type) |
| 138 | { |
| 139 | int i; |
| 140 | |
Tobias Klauser | 6391a11 | 2006-06-08 22:23:48 -0700 | [diff] [blame] | 141 | for (i = 0; i < ARRAY_SIZE(signal_types); i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | if (type == signal_types[i].value) |
| 143 | return signal_types[i].name; |
| 144 | } |
| 145 | return NULL; |
| 146 | } |
| 147 | static inline enum spi_signal_type spi_signal_to_value(const char *name) |
| 148 | { |
| 149 | int i, len; |
| 150 | |
Tobias Klauser | 6391a11 | 2006-06-08 22:23:48 -0700 | [diff] [blame] | 151 | for (i = 0; i < ARRAY_SIZE(signal_types); i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | 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 Bottomley | d0a7e57 | 2005-08-14 17:09:01 -0500 | [diff] [blame] | 160 | static int spi_host_setup(struct transport_container *tc, struct device *dev, |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 161 | struct device *cdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | { |
| 163 | struct Scsi_Host *shost = dev_to_shost(dev); |
| 164 | |
| 165 | spi_signalling(shost) = SPI_SIGNAL_UNKNOWN; |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
James Bottomley | 9b161a4 | 2008-01-05 10:18:27 -0600 | [diff] [blame] | 170 | static int spi_host_configure(struct transport_container *tc, |
| 171 | struct device *dev, |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 172 | struct device *cdev); |
James Bottomley | 9b161a4 | 2008-01-05 10:18:27 -0600 | [diff] [blame] | 173 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | static DECLARE_TRANSPORT_CLASS(spi_host_class, |
| 175 | "spi_host", |
| 176 | spi_host_setup, |
| 177 | NULL, |
James Bottomley | 9b161a4 | 2008-01-05 10:18:27 -0600 | [diff] [blame] | 178 | spi_host_configure); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
| 180 | static int spi_host_match(struct attribute_container *cont, |
| 181 | struct device *dev) |
| 182 | { |
| 183 | struct Scsi_Host *shost; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | |
| 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 Bottomley | 9b161a4 | 2008-01-05 10:18:27 -0600 | [diff] [blame] | 193 | return &shost->transportt->host_attrs.ac == cont; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | } |
| 195 | |
James Bottomley | 9b161a4 | 2008-01-05 10:18:27 -0600 | [diff] [blame] | 196 | static int spi_target_configure(struct transport_container *tc, |
| 197 | struct device *dev, |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 198 | struct device *cdev); |
James Bottomley | 9b161a4 | 2008-01-05 10:18:27 -0600 | [diff] [blame] | 199 | |
James Bottomley | d0a7e57 | 2005-08-14 17:09:01 -0500 | [diff] [blame] | 200 | static int spi_device_configure(struct transport_container *tc, |
| 201 | struct device *dev, |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 202 | struct device *cdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | { |
| 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 Bottomley | d0a7e57 | 2005-08-14 17:09:01 -0500 | [diff] [blame] | 220 | static int spi_setup_transport_attrs(struct transport_container *tc, |
| 221 | struct device *dev, |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 222 | struct device *cdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | { |
| 224 | struct scsi_target *starget = to_scsi_target(dev); |
| 225 | |
| 226 | spi_period(starget) = -1; /* illegal value */ |
James Bottomley | 62a8612 | 2005-05-06 18:05:20 -0500 | [diff] [blame] | 227 | spi_min_period(starget) = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | spi_offset(starget) = 0; /* async */ |
James Bottomley | 62a8612 | 2005-05-06 18:05:20 -0500 | [diff] [blame] | 229 | spi_max_offset(starget) = 255; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | spi_width(starget) = 0; /* narrow */ |
James Bottomley | 62a8612 | 2005-05-06 18:05:20 -0500 | [diff] [blame] | 231 | spi_max_width(starget) = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | 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 Bottomley | d872ebe | 2005-08-03 15:43:52 -0500 | [diff] [blame] | 239 | spi_hold_mcs(starget) = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | spi_dv_pending(starget) = 0; |
James Bottomley | dfdc58b | 2006-09-20 12:00:18 -0400 | [diff] [blame] | 241 | spi_dv_in_progress(starget) = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | spi_initial_dv(starget) = 0; |
Jes Sorensen | d158d26 | 2006-01-13 16:05:44 -0800 | [diff] [blame] | 243 | mutex_init(&spi_dv_mutex(starget)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | |
| 245 | return 0; |
| 246 | } |
| 247 | |
James Bottomley | 62a8612 | 2005-05-06 18:05:20 -0500 | [diff] [blame] | 248 | #define spi_transport_show_simple(field, format_string) \ |
| 249 | \ |
| 250 | static ssize_t \ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 251 | show_spi_transport_##field(struct device *dev, \ |
| 252 | struct device_attribute *attr, char *buf) \ |
James Bottomley | 62a8612 | 2005-05-06 18:05:20 -0500 | [diff] [blame] | 253 | { \ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 254 | struct scsi_target *starget = transport_class_to_starget(dev); \ |
James Bottomley | 62a8612 | 2005-05-06 18:05:20 -0500 | [diff] [blame] | 255 | 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 | \ |
| 263 | static ssize_t \ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 264 | store_spi_transport_##field(struct device *dev, \ |
| 265 | struct device_attribute *attr, \ |
| 266 | const char *buf, size_t count) \ |
James Bottomley | 62a8612 | 2005-05-06 18:05:20 -0500 | [diff] [blame] | 267 | { \ |
| 268 | int val; \ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 269 | struct scsi_target *starget = transport_class_to_starget(dev); \ |
James Bottomley | 62a8612 | 2005-05-06 18:05:20 -0500 | [diff] [blame] | 270 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | #define spi_transport_show_function(field, format_string) \ |
| 279 | \ |
| 280 | static ssize_t \ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 281 | show_spi_transport_##field(struct device *dev, \ |
| 282 | struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | { \ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 284 | struct scsi_target *starget = transport_class_to_starget(dev); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | 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) \ |
| 295 | static ssize_t \ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 296 | store_spi_transport_##field(struct device *dev, \ |
| 297 | struct device_attribute *attr, \ |
| 298 | const char *buf, size_t count) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | { \ |
| 300 | int val; \ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 301 | struct scsi_target *starget = transport_class_to_starget(dev); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \ |
| 303 | struct spi_internal *i = to_spi_internal(shost->transportt); \ |
| 304 | \ |
James Bottomley | 9b161a4 | 2008-01-05 10:18:27 -0600 | [diff] [blame] | 305 | if (!i->f->set_##field) \ |
| 306 | return -EINVAL; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | val = simple_strtoul(buf, NULL, 0); \ |
James Bottomley | 9b161a4 | 2008-01-05 10:18:27 -0600 | [diff] [blame] | 308 | i->f->set_##field(starget, val); \ |
James Bottomley | 62a8612 | 2005-05-06 18:05:20 -0500 | [diff] [blame] | 309 | return count; \ |
| 310 | } |
| 311 | |
| 312 | #define spi_transport_store_max(field, format_string) \ |
| 313 | static ssize_t \ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 314 | store_spi_transport_##field(struct device *dev, \ |
| 315 | struct device_attribute *attr, \ |
| 316 | const char *buf, size_t count) \ |
James Bottomley | 62a8612 | 2005-05-06 18:05:20 -0500 | [diff] [blame] | 317 | { \ |
| 318 | int val; \ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 319 | struct scsi_target *starget = transport_class_to_starget(dev); \ |
James Bottomley | 62a8612 | 2005-05-06 18:05:20 -0500 | [diff] [blame] | 320 | 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 Bottomley | 9b161a4 | 2008-01-05 10:18:27 -0600 | [diff] [blame] | 325 | if (i->f->set_##field) \ |
| 326 | return -EINVAL; \ |
James Bottomley | 62a8612 | 2005-05-06 18:05:20 -0500 | [diff] [blame] | 327 | val = simple_strtoul(buf, NULL, 0); \ |
| 328 | if (val > tp->max_##field) \ |
| 329 | val = tp->max_##field; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | 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 Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 337 | static DEVICE_ATTR(field, S_IRUGO, \ |
| 338 | show_spi_transport_##field, \ |
| 339 | store_spi_transport_##field); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | |
James Bottomley | 62a8612 | 2005-05-06 18:05:20 -0500 | [diff] [blame] | 341 | #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 Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 344 | static DEVICE_ATTR(field, S_IRUGO, \ |
| 345 | show_spi_transport_##field, \ |
| 346 | store_spi_transport_##field); |
James Bottomley | 62a8612 | 2005-05-06 18:05:20 -0500 | [diff] [blame] | 347 | |
| 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 Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 352 | static DEVICE_ATTR(field, S_IRUGO, \ |
| 353 | show_spi_transport_##field, \ |
| 354 | store_spi_transport_##field); |
James Bottomley | 62a8612 | 2005-05-06 18:05:20 -0500 | [diff] [blame] | 355 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | /* The Parallel SCSI Tranport Attributes: */ |
James Bottomley | 62a8612 | 2005-05-06 18:05:20 -0500 | [diff] [blame] | 357 | spi_transport_max_attr(offset, "%d\n"); |
| 358 | spi_transport_max_attr(width, "%d\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | spi_transport_rd_attr(iu, "%d\n"); |
| 360 | spi_transport_rd_attr(dt, "%d\n"); |
| 361 | spi_transport_rd_attr(qas, "%d\n"); |
| 362 | spi_transport_rd_attr(wr_flow, "%d\n"); |
| 363 | spi_transport_rd_attr(rd_strm, "%d\n"); |
| 364 | spi_transport_rd_attr(rti, "%d\n"); |
| 365 | spi_transport_rd_attr(pcomp_en, "%d\n"); |
James Bottomley | d872ebe | 2005-08-03 15:43:52 -0500 | [diff] [blame] | 366 | spi_transport_rd_attr(hold_mcs, "%d\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | |
gregkh@suse.de | 9a881f1 | 2005-03-25 15:52:00 -0800 | [diff] [blame] | 368 | /* we only care about the first child device so we return 1 */ |
| 369 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | static ssize_t |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 378 | store_spi_revalidate(struct device *dev, struct device_attribute *attr, |
| 379 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 381 | struct scsi_target *starget = transport_class_to_starget(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | |
gregkh@suse.de | 9a881f1 | 2005-03-25 15:52:00 -0800 | [diff] [blame] | 383 | device_for_each_child(&starget->dev, NULL, child_iter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | return count; |
| 385 | } |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 386 | static DEVICE_ATTR(revalidate, S_IWUSR, NULL, store_spi_revalidate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | |
| 388 | /* Translate the period into ns according to the current spec |
| 389 | * for SDTR/PPR messages */ |
Matthew Wilcox | ef72582 | 2005-12-15 16:22:01 -0500 | [diff] [blame] | 390 | static int period_to_str(char *buf, int period) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | int len, picosec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | |
James Bottomley | 62a8612 | 2005-05-06 18:05:20 -0500 | [diff] [blame] | 394 | if (period < 0 || period > 0xff) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | picosec = -1; |
James Bottomley | 62a8612 | 2005-05-06 18:05:20 -0500 | [diff] [blame] | 396 | } else if (period <= SPI_STATIC_PPR) { |
| 397 | picosec = ppr_to_ps[period]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | } else { |
James Bottomley | 62a8612 | 2005-05-06 18:05:20 -0500 | [diff] [blame] | 399 | picosec = period * 4000; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | if (picosec == -1) { |
| 403 | len = sprintf(buf, "reserved"); |
| 404 | } else { |
| 405 | len = sprint_frac(buf, picosec, 1000); |
| 406 | } |
| 407 | |
Matthew Wilcox | ef72582 | 2005-12-15 16:22:01 -0500 | [diff] [blame] | 408 | return len; |
| 409 | } |
| 410 | |
| 411 | static ssize_t |
Matthew Wilcox | ea697e4 | 2006-02-07 08:01:02 -0700 | [diff] [blame] | 412 | show_spi_transport_period_helper(char *buf, int period) |
Matthew Wilcox | ef72582 | 2005-12-15 16:22:01 -0500 | [diff] [blame] | 413 | { |
| 414 | int len = period_to_str(buf, period); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | buf[len++] = '\n'; |
| 416 | buf[len] = '\0'; |
| 417 | return len; |
| 418 | } |
| 419 | |
| 420 | static ssize_t |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 421 | store_spi_transport_period_helper(struct device *dev, const char *buf, |
James Bottomley | 62a8612 | 2005-05-06 18:05:20 -0500 | [diff] [blame] | 422 | size_t count, int *periodp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | 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 | 62a8612 | 2005-05-06 18:05:20 -0500 | [diff] [blame] | 452 | *periodp = period; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | |
| 454 | return count; |
| 455 | } |
| 456 | |
James Bottomley | 62a8612 | 2005-05-06 18:05:20 -0500 | [diff] [blame] | 457 | static ssize_t |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 458 | show_spi_transport_period(struct device *dev, |
| 459 | struct device_attribute *attr, char *buf) |
James Bottomley | 62a8612 | 2005-05-06 18:05:20 -0500 | [diff] [blame] | 460 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 461 | struct scsi_target *starget = transport_class_to_starget(dev); |
James Bottomley | 62a8612 | 2005-05-06 18:05:20 -0500 | [diff] [blame] | 462 | 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 Wilcox | ea697e4 | 2006-02-07 08:01:02 -0700 | [diff] [blame] | 470 | return show_spi_transport_period_helper(buf, tp->period); |
James Bottomley | 62a8612 | 2005-05-06 18:05:20 -0500 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | static ssize_t |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 474 | store_spi_transport_period(struct device *cdev, struct device_attribute *attr, |
| 475 | const char *buf, size_t count) |
James Bottomley | 62a8612 | 2005-05-06 18:05:20 -0500 | [diff] [blame] | 476 | { |
| 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 Bottomley | 9b161a4 | 2008-01-05 10:18:27 -0600 | [diff] [blame] | 484 | if (!i->f->set_period) |
| 485 | return -EINVAL; |
| 486 | |
James Bottomley | 62a8612 | 2005-05-06 18:05:20 -0500 | [diff] [blame] | 487 | 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 Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 497 | static DEVICE_ATTR(period, S_IRUGO, |
| 498 | show_spi_transport_period, |
| 499 | store_spi_transport_period); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | |
James Bottomley | 62a8612 | 2005-05-06 18:05:20 -0500 | [diff] [blame] | 501 | static ssize_t |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 502 | show_spi_transport_min_period(struct device *cdev, |
| 503 | struct device_attribute *attr, char *buf) |
James Bottomley | 62a8612 | 2005-05-06 18:05:20 -0500 | [diff] [blame] | 504 | { |
| 505 | struct scsi_target *starget = transport_class_to_starget(cdev); |
James Bottomley | 9b161a4 | 2008-01-05 10:18:27 -0600 | [diff] [blame] | 506 | struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); |
| 507 | struct spi_internal *i = to_spi_internal(shost->transportt); |
James Bottomley | 62a8612 | 2005-05-06 18:05:20 -0500 | [diff] [blame] | 508 | struct spi_transport_attrs *tp = |
| 509 | (struct spi_transport_attrs *)&starget->starget_data; |
| 510 | |
James Bottomley | 9b161a4 | 2008-01-05 10:18:27 -0600 | [diff] [blame] | 511 | if (!i->f->set_period) |
| 512 | return -EINVAL; |
| 513 | |
Matthew Wilcox | ea697e4 | 2006-02-07 08:01:02 -0700 | [diff] [blame] | 514 | return show_spi_transport_period_helper(buf, tp->min_period); |
James Bottomley | 62a8612 | 2005-05-06 18:05:20 -0500 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | static ssize_t |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 518 | store_spi_transport_min_period(struct device *cdev, |
| 519 | struct device_attribute *attr, |
| 520 | const char *buf, size_t count) |
James Bottomley | 62a8612 | 2005-05-06 18:05:20 -0500 | [diff] [blame] | 521 | { |
| 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 Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 531 | static DEVICE_ATTR(min_period, S_IRUGO, |
| 532 | show_spi_transport_min_period, |
| 533 | store_spi_transport_min_period); |
James Bottomley | 62a8612 | 2005-05-06 18:05:20 -0500 | [diff] [blame] | 534 | |
| 535 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 536 | static ssize_t show_spi_host_signalling(struct device *cdev, |
| 537 | struct device_attribute *attr, |
| 538 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | { |
| 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 Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 548 | static ssize_t store_spi_host_signalling(struct device *dev, |
| 549 | struct device_attribute *attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | const char *buf, size_t count) |
| 551 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 552 | struct Scsi_Host *shost = transport_class_to_shost(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | struct spi_internal *i = to_spi_internal(shost->transportt); |
| 554 | enum spi_signal_type type = spi_signal_to_value(buf); |
| 555 | |
James Bottomley | 9b161a4 | 2008-01-05 10:18:27 -0600 | [diff] [blame] | 556 | if (!i->f->set_signalling) |
| 557 | return -EINVAL; |
| 558 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | if (type != SPI_SIGNAL_UNKNOWN) |
| 560 | i->f->set_signalling(shost, type); |
| 561 | |
| 562 | return count; |
| 563 | } |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 564 | static DEVICE_ATTR(signalling, S_IRUGO, |
| 565 | show_spi_host_signalling, |
| 566 | store_spi_host_signalling); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | |
| 568 | #define DV_SET(x, y) \ |
| 569 | if(i->f->set_##x) \ |
| 570 | i->f->set_##x(sdev->sdev_target, y) |
| 571 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | enum 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 */ |
| 581 | static enum spi_compare_returns |
James Bottomley | 33aa687 | 2005-08-28 11:31:14 -0500 | [diff] [blame] | 582 | spi_dv_device_echo_buffer(struct scsi_device *sdev, u8 *buffer, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | u8 *ptr, const int retries) |
| 584 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | int len = ptr - buffer; |
James Bottomley | 33aa687 | 2005-08-28 11:31:14 -0500 | [diff] [blame] | 586 | int j, k, r, result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | unsigned int pattern = 0x0000ffff; |
James Bottomley | 33aa687 | 2005-08-28 11:31:14 -0500 | [diff] [blame] | 588 | struct scsi_sense_hdr sshdr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | |
| 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 Bottomley | 33aa687 | 2005-08-28 11:31:14 -0500 | [diff] [blame] | 633 | result = spi_execute(sdev, spi_write_buffer, DMA_TO_DEVICE, |
| 634 | buffer, len, &sshdr); |
| 635 | if(result || !scsi_device_online(sdev)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | |
| 637 | scsi_device_set_state(sdev, SDEV_QUIESCE); |
James Bottomley | 33aa687 | 2005-08-28 11:31:14 -0500 | [diff] [blame] | 638 | if (scsi_sense_valid(&sshdr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | && 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 Bottomley | 9ccfc75 | 2005-10-02 11:45:08 -0500 | [diff] [blame] | 650 | sdev_printk(KERN_ERR, sdev, "Write Buffer failure %x\n", result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | return SPI_COMPARE_FAILURE; |
| 652 | } |
| 653 | |
| 654 | memset(ptr, 0, len); |
James Bottomley | 33aa687 | 2005-08-28 11:31:14 -0500 | [diff] [blame] | 655 | spi_execute(sdev, spi_read_buffer, DMA_FROM_DEVICE, |
| 656 | ptr, len, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | 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 */ |
| 667 | static enum spi_compare_returns |
James Bottomley | 33aa687 | 2005-08-28 11:31:14 -0500 | [diff] [blame] | 668 | spi_dv_device_compare_inquiry(struct scsi_device *sdev, u8 *buffer, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | u8 *ptr, const int retries) |
| 670 | { |
James Bottomley | 33aa687 | 2005-08-28 11:31:14 -0500 | [diff] [blame] | 671 | int r, result; |
| 672 | const int len = sdev->inquiry_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | const char spi_inquiry[] = { |
| 674 | INQUIRY, 0, 0, 0, len, 0 |
| 675 | }; |
| 676 | |
| 677 | for (r = 0; r < retries; r++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | memset(ptr, 0, len); |
| 679 | |
James Bottomley | 33aa687 | 2005-08-28 11:31:14 -0500 | [diff] [blame] | 680 | result = spi_execute(sdev, spi_inquiry, DMA_FROM_DEVICE, |
| 681 | ptr, len, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | |
James Bottomley | 33aa687 | 2005-08-28 11:31:14 -0500 | [diff] [blame] | 683 | if(result || !scsi_device_online(sdev)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | 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 | |
| 703 | static enum spi_compare_returns |
James Bottomley | 33aa687 | 2005-08-28 11:31:14 -0500 | [diff] [blame] | 704 | spi_dv_retrain(struct scsi_device *sdev, u8 *buffer, u8 *ptr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | enum spi_compare_returns |
James Bottomley | 33aa687 | 2005-08-28 11:31:14 -0500 | [diff] [blame] | 706 | (*compare_fn)(struct scsi_device *, u8 *, u8 *, int)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | { |
James Bottomley | 33aa687 | 2005-08-28 11:31:14 -0500 | [diff] [blame] | 708 | struct spi_internal *i = to_spi_internal(sdev->host->transportt); |
James Bottomley | 9a8bc9b | 2005-05-31 18:35:39 -0500 | [diff] [blame] | 709 | struct scsi_target *starget = sdev->sdev_target; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | int period = 0, prevperiod = 0; |
| 711 | enum spi_compare_returns retval; |
| 712 | |
| 713 | |
| 714 | for (;;) { |
| 715 | int newperiod; |
James Bottomley | 33aa687 | 2005-08-28 11:31:14 -0500 | [diff] [blame] | 716 | retval = compare_fn(sdev, buffer, ptr, DV_LOOPS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | |
| 718 | if (retval == SPI_COMPARE_SUCCESS |
| 719 | || retval == SPI_COMPARE_SKIP_TEST) |
| 720 | break; |
| 721 | |
| 722 | /* OK, retrain, fallback */ |
James Bottomley | 9a8bc9b | 2005-05-31 18:35:39 -0500 | [diff] [blame] | 723 | if (i->f->get_iu) |
| 724 | i->f->get_iu(starget); |
| 725 | if (i->f->get_qas) |
| 726 | i->f->get_qas(starget); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | if (i->f->get_period) |
| 728 | i->f->get_period(sdev->sdev_target); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | |
James Bottomley | 9a8bc9b | 2005-05-31 18:35:39 -0500 | [diff] [blame] | 730 | /* 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 Bottomley | 9ccfc75 | 2005-10-02 11:45:08 -0500 | [diff] [blame] | 734 | starget_printk(KERN_ERR, starget, "Domain Validation Disabing Information Units\n"); |
James Bottomley | 9a8bc9b | 2005-05-31 18:35:39 -0500 | [diff] [blame] | 735 | DV_SET(iu, 0); |
| 736 | } else if (i->f->set_qas && spi_qas(starget)) { |
James Bottomley | 9ccfc75 | 2005-10-02 11:45:08 -0500 | [diff] [blame] | 737 | starget_printk(KERN_ERR, starget, "Domain Validation Disabing Quick Arbitration and Selection\n"); |
James Bottomley | 9a8bc9b | 2005-05-31 18:35:39 -0500 | [diff] [blame] | 738 | 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 Bottomley | 9ccfc75 | 2005-10-02 11:45:08 -0500 | [diff] [blame] | 749 | starget_printk(KERN_ERR, starget, "Domain Validation Failure, dropping back to Asynchronous\n"); |
James Bottomley | 9a8bc9b | 2005-05-31 18:35:39 -0500 | [diff] [blame] | 750 | DV_SET(offset, 0); |
| 751 | return SPI_COMPARE_FAILURE; |
| 752 | } |
James Bottomley | 9ccfc75 | 2005-10-02 11:45:08 -0500 | [diff] [blame] | 753 | starget_printk(KERN_ERR, starget, "Domain Validation detected failure, dropping back\n"); |
James Bottomley | 9a8bc9b | 2005-05-31 18:35:39 -0500 | [diff] [blame] | 754 | DV_SET(period, period); |
| 755 | prevperiod = period; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | } |
| 758 | return retval; |
| 759 | } |
| 760 | |
| 761 | static int |
James Bottomley | 33aa687 | 2005-08-28 11:31:14 -0500 | [diff] [blame] | 762 | spi_dv_device_get_echo_buffer(struct scsi_device *sdev, u8 *buffer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | { |
James Bottomley | 33aa687 | 2005-08-28 11:31:14 -0500 | [diff] [blame] | 764 | int l, result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | /* 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 Bottomley | 33aa687 | 2005-08-28 11:31:14 -0500 | [diff] [blame] | 786 | result = spi_execute(sdev, spi_test_unit_ready, DMA_NONE, |
| 787 | NULL, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | |
James Bottomley | 33aa687 | 2005-08-28 11:31:14 -0500 | [diff] [blame] | 789 | if(result) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | if(l >= 3) |
| 791 | return 0; |
| 792 | } else { |
| 793 | /* TUR succeeded */ |
| 794 | break; |
| 795 | } |
| 796 | } |
| 797 | |
James Bottomley | 33aa687 | 2005-08-28 11:31:14 -0500 | [diff] [blame] | 798 | result = spi_execute(sdev, spi_read_buffer_descriptor, |
| 799 | DMA_FROM_DEVICE, buffer, 4, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | |
James Bottomley | 33aa687 | 2005-08-28 11:31:14 -0500 | [diff] [blame] | 801 | if (result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | /* Device has no echo buffer */ |
| 803 | return 0; |
| 804 | |
| 805 | return buffer[3] + ((buffer[2] & 0x1f) << 8); |
| 806 | } |
| 807 | |
| 808 | static void |
James Bottomley | 33aa687 | 2005-08-28 11:31:14 -0500 | [diff] [blame] | 809 | spi_dv_device_internal(struct scsi_device *sdev, u8 *buffer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | { |
James Bottomley | 33aa687 | 2005-08-28 11:31:14 -0500 | [diff] [blame] | 811 | struct spi_internal *i = to_spi_internal(sdev->host->transportt); |
James Bottomley | 62a8612 | 2005-05-06 18:05:20 -0500 | [diff] [blame] | 812 | struct scsi_target *starget = sdev->sdev_target; |
James Bottomley | 60eef25 | 2006-06-10 10:51:23 -0500 | [diff] [blame] | 813 | struct Scsi_Host *shost = sdev->host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | int len = sdev->inquiry_len; |
James Bottomley | 2302827 | 2007-09-22 08:40:09 -0500 | [diff] [blame] | 815 | int min_period = spi_min_period(starget); |
| 816 | int max_width = spi_max_width(starget); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | /* first set us up for narrow async */ |
| 818 | DV_SET(offset, 0); |
| 819 | DV_SET(width, 0); |
James Bottomley | 2302827 | 2007-09-22 08:40:09 -0500 | [diff] [blame] | 820 | |
James Bottomley | 33aa687 | 2005-08-28 11:31:14 -0500 | [diff] [blame] | 821 | if (spi_dv_device_compare_inquiry(sdev, buffer, buffer, DV_LOOPS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | != SPI_COMPARE_SUCCESS) { |
James Bottomley | 9ccfc75 | 2005-10-02 11:45:08 -0500 | [diff] [blame] | 823 | starget_printk(KERN_ERR, starget, "Domain Validation Initial Inquiry Failed\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | /* FIXME: should probably offline the device here? */ |
| 825 | return; |
| 826 | } |
| 827 | |
James Bottomley | 2302827 | 2007-09-22 08:40:09 -0500 | [diff] [blame] | 828 | if (!scsi_device_wide(sdev)) { |
| 829 | spi_max_width(starget) = 0; |
| 830 | max_width = 0; |
| 831 | } |
| 832 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | /* test width */ |
James Bottomley | 2302827 | 2007-09-22 08:40:09 -0500 | [diff] [blame] | 834 | if (i->f->set_width && max_width) { |
James Bottomley | 9a8bc9b | 2005-05-31 18:35:39 -0500 | [diff] [blame] | 835 | i->f->set_width(starget, 1); |
James Bottomley | 62a8612 | 2005-05-06 18:05:20 -0500 | [diff] [blame] | 836 | |
James Bottomley | 33aa687 | 2005-08-28 11:31:14 -0500 | [diff] [blame] | 837 | if (spi_dv_device_compare_inquiry(sdev, buffer, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | buffer + len, |
| 839 | DV_LOOPS) |
| 840 | != SPI_COMPARE_SUCCESS) { |
James Bottomley | 9ccfc75 | 2005-10-02 11:45:08 -0500 | [diff] [blame] | 841 | starget_printk(KERN_ERR, starget, "Wide Transfers Fail\n"); |
James Bottomley | 9a8bc9b | 2005-05-31 18:35:39 -0500 | [diff] [blame] | 842 | i->f->set_width(starget, 0); |
James Bottomley | 2302827 | 2007-09-22 08:40:09 -0500 | [diff] [blame] | 843 | /* 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | } |
| 849 | } |
| 850 | |
| 851 | if (!i->f->set_period) |
| 852 | return; |
| 853 | |
| 854 | /* device can't handle synchronous */ |
James Bottomley | eb1dd68 | 2005-07-02 12:22:01 -0400 | [diff] [blame] | 855 | if (!scsi_device_sync(sdev) && !scsi_device_dt(sdev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | return; |
| 857 | |
James Bottomley | 349cd7c | 2005-11-28 15:41:58 -0600 | [diff] [blame] | 858 | /* 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | |
| 863 | retry: |
| 864 | |
| 865 | /* now set up to the maximum */ |
James Bottomley | 62a8612 | 2005-05-06 18:05:20 -0500 | [diff] [blame] | 866 | DV_SET(offset, spi_max_offset(starget)); |
James Bottomley | 2302827 | 2007-09-22 08:40:09 -0500 | [diff] [blame] | 867 | DV_SET(period, min_period); |
| 868 | |
James Bottomley | 9a8bc9b | 2005-05-31 18:35:39 -0500 | [diff] [blame] | 869 | /* try QAS requests; this should be harmless to set if the |
| 870 | * target supports it */ |
James Bottomley | dfdc58b | 2006-09-20 12:00:18 -0400 | [diff] [blame] | 871 | if (scsi_device_qas(sdev)) { |
James Bottomley | eb1dd68 | 2005-07-02 12:22:01 -0400 | [diff] [blame] | 872 | DV_SET(qas, 1); |
James Bottomley | dfdc58b | 2006-09-20 12:00:18 -0400 | [diff] [blame] | 873 | } else { |
| 874 | DV_SET(qas, 0); |
| 875 | } |
| 876 | |
James Bottomley | 2302827 | 2007-09-22 08:40:09 -0500 | [diff] [blame] | 877 | if (scsi_device_ius(sdev) && min_period < 9) { |
James Bottomley | dfdc58b | 2006-09-20 12:00:18 -0400 | [diff] [blame] | 878 | /* This u320 (or u640). Set IU transfers */ |
James Bottomley | eb1dd68 | 2005-07-02 12:22:01 -0400 | [diff] [blame] | 879 | DV_SET(iu, 1); |
James Bottomley | dfdc58b | 2006-09-20 12:00:18 -0400 | [diff] [blame] | 880 | /* Then set the optional parameters */ |
James Bottomley | 9a8bc9b | 2005-05-31 18:35:39 -0500 | [diff] [blame] | 881 | DV_SET(rd_strm, 1); |
| 882 | DV_SET(wr_flow, 1); |
| 883 | DV_SET(rti, 1); |
James Bottomley | 2302827 | 2007-09-22 08:40:09 -0500 | [diff] [blame] | 884 | if (min_period == 8) |
James Bottomley | 9a8bc9b | 2005-05-31 18:35:39 -0500 | [diff] [blame] | 885 | DV_SET(pcomp_en, 1); |
James Bottomley | dfdc58b | 2006-09-20 12:00:18 -0400 | [diff] [blame] | 886 | } else { |
| 887 | DV_SET(iu, 0); |
James Bottomley | 9a8bc9b | 2005-05-31 18:35:39 -0500 | [diff] [blame] | 888 | } |
James Bottomley | dfdc58b | 2006-09-20 12:00:18 -0400 | [diff] [blame] | 889 | |
James Bottomley | 60eef25 | 2006-06-10 10:51:23 -0500 | [diff] [blame] | 890 | /* 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 Bottomley | dfdc58b | 2006-09-20 12:00:18 -0400 | [diff] [blame] | 896 | spi_signalling(shost) == SPI_SIGNAL_HVD || |
| 897 | !scsi_device_dt(sdev)) { |
James Bottomley | 60eef25 | 2006-06-10 10:51:23 -0500 | [diff] [blame] | 898 | DV_SET(dt, 0); |
James Bottomley | dfdc58b | 2006-09-20 12:00:18 -0400 | [diff] [blame] | 899 | } else { |
| 900 | DV_SET(dt, 1); |
| 901 | } |
James Bottomley | 2302827 | 2007-09-22 08:40:09 -0500 | [diff] [blame] | 902 | /* set width last because it will pull all the other |
| 903 | * parameters down to required values */ |
| 904 | DV_SET(width, max_width); |
| 905 | |
James Bottomley | 349cd7c | 2005-11-28 15:41:58 -0600 | [diff] [blame] | 906 | /* 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | |
James Bottomley | 349cd7c | 2005-11-28 15:41:58 -0600 | [diff] [blame] | 913 | /* 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 Bottomley | 9ccfc75 | 2005-10-02 11:45:08 -0500 | [diff] [blame] | 922 | starget_printk(KERN_INFO, starget, "Domain Validation skipping write tests\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | return; |
| 924 | } |
| 925 | |
| 926 | if (len > SPI_MAX_ECHO_BUFFER_SIZE) { |
James Bottomley | 9ccfc75 | 2005-10-02 11:45:08 -0500 | [diff] [blame] | 927 | starget_printk(KERN_WARNING, starget, "Echo buffer size %d is too big, trimming to %d\n", len, SPI_MAX_ECHO_BUFFER_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | len = SPI_MAX_ECHO_BUFFER_SIZE; |
| 929 | } |
| 930 | |
James Bottomley | 33aa687 | 2005-08-28 11:31:14 -0500 | [diff] [blame] | 931 | if (spi_dv_retrain(sdev, buffer, buffer + len, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | 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 | */ |
| 951 | void |
| 952 | spi_dv_device(struct scsi_device *sdev) |
| 953 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | struct scsi_target *starget = sdev->sdev_target; |
| 955 | u8 *buffer; |
| 956 | const int len = SPI_MAX_ECHO_BUFFER_SIZE*2; |
| 957 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | if (unlikely(scsi_device_get(sdev))) |
James Bottomley | 33aa687 | 2005-08-28 11:31:14 -0500 | [diff] [blame] | 959 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | |
James Bottomley | dfdc58b | 2006-09-20 12:00:18 -0400 | [diff] [blame] | 961 | if (unlikely(spi_dv_in_progress(starget))) |
| 962 | return; |
| 963 | spi_dv_in_progress(starget) = 1; |
| 964 | |
Jes Sorensen | 24669f75 | 2006-01-16 10:31:18 -0500 | [diff] [blame] | 965 | buffer = kzalloc(len, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | |
| 967 | if (unlikely(!buffer)) |
| 968 | goto out_put; |
| 969 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | /* 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 Sorensen | d158d26 | 2006-01-13 16:05:44 -0800 | [diff] [blame] | 978 | mutex_lock(&spi_dv_mutex(starget)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | |
James Bottomley | 9ccfc75 | 2005-10-02 11:45:08 -0500 | [diff] [blame] | 980 | starget_printk(KERN_INFO, starget, "Beginning Domain Validation\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | |
James Bottomley | 33aa687 | 2005-08-28 11:31:14 -0500 | [diff] [blame] | 982 | spi_dv_device_internal(sdev, buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | |
James Bottomley | 9ccfc75 | 2005-10-02 11:45:08 -0500 | [diff] [blame] | 984 | starget_printk(KERN_INFO, starget, "Ending Domain Validation\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | |
Jes Sorensen | d158d26 | 2006-01-13 16:05:44 -0800 | [diff] [blame] | 986 | mutex_unlock(&spi_dv_mutex(starget)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | 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 Bottomley | dfdc58b | 2006-09-20 12:00:18 -0400 | [diff] [blame] | 996 | spi_dv_in_progress(starget) = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | scsi_device_put(sdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | } |
| 999 | EXPORT_SYMBOL(spi_dv_device); |
| 1000 | |
| 1001 | struct work_queue_wrapper { |
| 1002 | struct work_struct work; |
| 1003 | struct scsi_device *sdev; |
| 1004 | }; |
| 1005 | |
| 1006 | static void |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1007 | spi_dv_device_work_wrapper(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1009 | struct work_queue_wrapper *wqw = |
| 1010 | container_of(work, struct work_queue_wrapper, work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | 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 | */ |
| 1029 | void |
| 1030 | spi_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 Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1050 | INIT_WORK(&wqw->work, spi_dv_device_work_wrapper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | wqw->sdev = sdev; |
| 1052 | |
| 1053 | schedule_work(&wqw->work); |
| 1054 | } |
| 1055 | EXPORT_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 | */ |
| 1065 | void 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 Bottomley | d872ebe | 2005-08-03 15:43:52 -0500 | [diff] [blame] | 1101 | "%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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | } else { |
Matthew Wilcox | 493ff4e | 2005-11-17 11:13:43 -0700 | [diff] [blame] | 1113 | dev_info(&starget->dev, "%sasynchronous\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | tp->width ? "wide " : ""); |
| 1115 | } |
| 1116 | } |
| 1117 | EXPORT_SYMBOL(spi_display_xfer_agreement); |
| 1118 | |
Matthew Wilcox | 6ea3c0b | 2006-02-07 07:54:46 -0700 | [diff] [blame] | 1119 | int 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 Bottomley | 38e14f8 | 2006-02-17 14:58:47 -0800 | [diff] [blame] | 1127 | EXPORT_SYMBOL_GPL(spi_populate_width_msg); |
Matthew Wilcox | 6ea3c0b | 2006-02-07 07:54:46 -0700 | [diff] [blame] | 1128 | |
| 1129 | int 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 Bottomley | 38e14f8 | 2006-02-17 14:58:47 -0800 | [diff] [blame] | 1138 | EXPORT_SYMBOL_GPL(spi_populate_sync_msg); |
Matthew Wilcox | 6ea3c0b | 2006-02-07 07:54:46 -0700 | [diff] [blame] | 1139 | |
| 1140 | int 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 Bottomley | 38e14f8 | 2006-02-17 14:58:47 -0800 | [diff] [blame] | 1153 | EXPORT_SYMBOL_GPL(spi_populate_ppr_msg); |
Matthew Wilcox | 6ea3c0b | 2006-02-07 07:54:46 -0700 | [diff] [blame] | 1154 | |
Matthew Wilcox | 410ca5c | 2005-12-15 16:22:01 -0500 | [diff] [blame] | 1155 | #ifdef CONFIG_SCSI_CONSTANTS |
| 1156 | static const char * const one_byte_msgs[] = { |
Matthew Wilcox | 72df0eb | 2006-03-10 07:18:22 -0700 | [diff] [blame] | 1157 | /* 0x00 */ "Task Complete", NULL /* Extended Message */, "Save Pointers", |
Matthew Wilcox | 410ca5c | 2005-12-15 16:22:01 -0500 | [diff] [blame] | 1158 | /* 0x03 */ "Restore Pointers", "Disconnect", "Initiator Error", |
Matthew Wilcox | 72df0eb | 2006-03-10 07:18:22 -0700 | [diff] [blame] | 1159 | /* 0x06 */ "Abort Task Set", "Message Reject", "Nop", "Message Parity Error", |
Matthew Wilcox | 410ca5c | 2005-12-15 16:22:01 -0500 | [diff] [blame] | 1160 | /* 0x0a */ "Linked Command Complete", "Linked Command Complete w/flag", |
Matthew Wilcox | 72df0eb | 2006-03-10 07:18:22 -0700 | [diff] [blame] | 1161 | /* 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 Wilcox | 410ca5c | 2005-12-15 16:22:01 -0500 | [diff] [blame] | 1165 | }; |
Matthew Wilcox | 410ca5c | 2005-12-15 16:22:01 -0500 | [diff] [blame] | 1166 | |
| 1167 | static const char * const two_byte_msgs[] = { |
Matthew Wilcox | 4797215 | 2005-12-15 16:22:01 -0500 | [diff] [blame] | 1168 | /* 0x20 */ "Simple Queue Tag", "Head of Queue Tag", "Ordered Queue Tag", |
Matthew Wilcox | 72df0eb | 2006-03-10 07:18:22 -0700 | [diff] [blame] | 1169 | /* 0x23 */ "Ignore Wide Residue", "ACA" |
Matthew Wilcox | 410ca5c | 2005-12-15 16:22:01 -0500 | [diff] [blame] | 1170 | }; |
Matthew Wilcox | 410ca5c | 2005-12-15 16:22:01 -0500 | [diff] [blame] | 1171 | |
| 1172 | static const char * const extended_msgs[] = { |
| 1173 | /* 0x00 */ "Modify Data Pointer", "Synchronous Data Transfer Request", |
Matthew Wilcox | ef72582 | 2005-12-15 16:22:01 -0500 | [diff] [blame] | 1174 | /* 0x02 */ "SCSI-I Extended Identify", "Wide Data Transfer Request", |
Matthew Wilcox | fc25307 | 2006-02-18 20:52:31 -0700 | [diff] [blame] | 1175 | /* 0x04 */ "Parallel Protocol Request", "Modify Bidirectional Data Pointer" |
Matthew Wilcox | 410ca5c | 2005-12-15 16:22:01 -0500 | [diff] [blame] | 1176 | }; |
Matthew Wilcox | 410ca5c | 2005-12-15 16:22:01 -0500 | [diff] [blame] | 1177 | |
Adrian Bunk | bdd70f2 | 2006-01-05 23:39:18 +0100 | [diff] [blame] | 1178 | static void print_nego(const unsigned char *msg, int per, int off, int width) |
Matthew Wilcox | ef72582 | 2005-12-15 16:22:01 -0500 | [diff] [blame] | 1179 | { |
| 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 Wilcox | 410ca5c | 2005-12-15 16:22:01 -0500 | [diff] [blame] | 1191 | |
Matthew Wilcox | fc25307 | 2006-02-18 20:52:31 -0700 | [diff] [blame] | 1192 | static 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 Wilcox | 1abfd37 | 2005-12-15 16:22:01 -0500 | [diff] [blame] | 1199 | int spi_print_msg(const unsigned char *msg) |
Matthew Wilcox | 410ca5c | 2005-12-15 16:22:01 -0500 | [diff] [blame] | 1200 | { |
Matthew Wilcox | 72df0eb | 2006-03-10 07:18:22 -0700 | [diff] [blame] | 1201 | int len = 1, i; |
Matthew Wilcox | 410ca5c | 2005-12-15 16:22:01 -0500 | [diff] [blame] | 1202 | if (msg[0] == EXTENDED_MESSAGE) { |
Matthew Wilcox | fc25307 | 2006-02-18 20:52:31 -0700 | [diff] [blame] | 1203 | len = 2 + msg[1]; |
| 1204 | if (len == 2) |
| 1205 | len += 256; |
Matthew Wilcox | b32aaff | 2005-12-15 16:22:01 -0500 | [diff] [blame] | 1206 | if (msg[2] < ARRAY_SIZE(extended_msgs)) |
Matthew Wilcox | 410ca5c | 2005-12-15 16:22:01 -0500 | [diff] [blame] | 1207 | 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 Wilcox | fc25307 | 2006-02-18 20:52:31 -0700 | [diff] [blame] | 1213 | print_ptr(msg, 3, "pointer"); |
Matthew Wilcox | 410ca5c | 2005-12-15 16:22:01 -0500 | [diff] [blame] | 1214 | break; |
| 1215 | case EXTENDED_SDTR: |
Matthew Wilcox | ef72582 | 2005-12-15 16:22:01 -0500 | [diff] [blame] | 1216 | print_nego(msg, 3, 4, 0); |
Matthew Wilcox | 410ca5c | 2005-12-15 16:22:01 -0500 | [diff] [blame] | 1217 | break; |
| 1218 | case EXTENDED_WDTR: |
Matthew Wilcox | ef72582 | 2005-12-15 16:22:01 -0500 | [diff] [blame] | 1219 | print_nego(msg, 0, 0, 3); |
| 1220 | break; |
| 1221 | case EXTENDED_PPR: |
| 1222 | print_nego(msg, 3, 5, 6); |
Matthew Wilcox | 410ca5c | 2005-12-15 16:22:01 -0500 | [diff] [blame] | 1223 | break; |
Matthew Wilcox | fc25307 | 2006-02-18 20:52:31 -0700 | [diff] [blame] | 1224 | case EXTENDED_MODIFY_BIDI_DATA_PTR: |
| 1225 | print_ptr(msg, 3, "out"); |
| 1226 | print_ptr(msg, 7, "in"); |
| 1227 | break; |
Matthew Wilcox | 410ca5c | 2005-12-15 16:22:01 -0500 | [diff] [blame] | 1228 | 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 Wilcox | 410ca5c | 2005-12-15 16:22:01 -0500 | [diff] [blame] | 1238 | /* Normal One byte */ |
| 1239 | } else if (msg[0] < 0x1f) { |
Matthew Wilcox | 72df0eb | 2006-03-10 07:18:22 -0700 | [diff] [blame] | 1240 | if (msg[0] < ARRAY_SIZE(one_byte_msgs) && one_byte_msgs[msg[0]]) |
Matthew Wilcox | e24d873 | 2006-02-07 08:05:26 -0700 | [diff] [blame] | 1241 | printk("%s ", one_byte_msgs[msg[0]]); |
Matthew Wilcox | 410ca5c | 2005-12-15 16:22:01 -0500 | [diff] [blame] | 1242 | else |
| 1243 | printk("reserved (%02x) ", msg[0]); |
Matthew Wilcox | 72df0eb | 2006-03-10 07:18:22 -0700 | [diff] [blame] | 1244 | } else if (msg[0] == 0x55) { |
| 1245 | printk("QAS Request "); |
Matthew Wilcox | 410ca5c | 2005-12-15 16:22:01 -0500 | [diff] [blame] | 1246 | /* Two byte */ |
| 1247 | } else if (msg[0] <= 0x2f) { |
Matthew Wilcox | b32aaff | 2005-12-15 16:22:01 -0500 | [diff] [blame] | 1248 | if ((msg[0] - 0x20) < ARRAY_SIZE(two_byte_msgs)) |
Matthew Wilcox | 410ca5c | 2005-12-15 16:22:01 -0500 | [diff] [blame] | 1249 | 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 Wilcox | e24d873 | 2006-02-07 08:05:26 -0700 | [diff] [blame] | 1256 | printk("reserved "); |
Matthew Wilcox | 410ca5c | 2005-12-15 16:22:01 -0500 | [diff] [blame] | 1257 | return len; |
| 1258 | } |
Matthew Wilcox | 1abfd37 | 2005-12-15 16:22:01 -0500 | [diff] [blame] | 1259 | EXPORT_SYMBOL(spi_print_msg); |
Matthew Wilcox | 410ca5c | 2005-12-15 16:22:01 -0500 | [diff] [blame] | 1260 | |
| 1261 | #else /* ifndef CONFIG_SCSI_CONSTANTS */ |
| 1262 | |
Matthew Wilcox | 1abfd37 | 2005-12-15 16:22:01 -0500 | [diff] [blame] | 1263 | int spi_print_msg(const unsigned char *msg) |
Matthew Wilcox | 410ca5c | 2005-12-15 16:22:01 -0500 | [diff] [blame] | 1264 | { |
Matthew Wilcox | 72df0eb | 2006-03-10 07:18:22 -0700 | [diff] [blame] | 1265 | int len = 1, i; |
Matthew Wilcox | 410ca5c | 2005-12-15 16:22:01 -0500 | [diff] [blame] | 1266 | |
| 1267 | if (msg[0] == EXTENDED_MESSAGE) { |
Matthew Wilcox | fc25307 | 2006-02-18 20:52:31 -0700 | [diff] [blame] | 1268 | len = 2 + msg[1]; |
| 1269 | if (len == 2) |
| 1270 | len += 256; |
Matthew Wilcox | 410ca5c | 2005-12-15 16:22:01 -0500 | [diff] [blame] | 1271 | 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 Wilcox | 410ca5c | 2005-12-15 16:22:01 -0500 | [diff] [blame] | 1276 | /* Normal One byte */ |
James Bottomley | 597705a | 2006-03-12 09:54:19 -0600 | [diff] [blame] | 1277 | } else if ((msg[0] < 0x1f) || (msg[0] == 0x55)) { |
Matthew Wilcox | 410ca5c | 2005-12-15 16:22:01 -0500 | [diff] [blame] | 1278 | printk("%02x ", msg[0]); |
Matthew Wilcox | 410ca5c | 2005-12-15 16:22:01 -0500 | [diff] [blame] | 1279 | /* 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 Wilcox | 1abfd37 | 2005-12-15 16:22:01 -0500 | [diff] [blame] | 1287 | EXPORT_SYMBOL(spi_print_msg); |
Matthew Wilcox | 410ca5c | 2005-12-15 16:22:01 -0500 | [diff] [blame] | 1288 | #endif /* ! CONFIG_SCSI_CONSTANTS */ |
| 1289 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | static int spi_device_match(struct attribute_container *cont, |
| 1291 | struct device *dev) |
| 1292 | { |
| 1293 | struct scsi_device *sdev; |
| 1294 | struct Scsi_Host *shost; |
James Bottomley | 10c1b88 | 2005-08-14 14:34:06 -0500 | [diff] [blame] | 1295 | struct spi_internal *i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | |
| 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 Bottomley | 10c1b88 | 2005-08-14 14:34:06 -0500 | [diff] [blame] | 1308 | i = to_spi_internal(shost->transportt); |
| 1309 | if (i->f->deny_binding && i->f->deny_binding(sdev->sdev_target)) |
| 1310 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | return 1; |
| 1312 | } |
| 1313 | |
| 1314 | static int spi_target_match(struct attribute_container *cont, |
| 1315 | struct device *dev) |
| 1316 | { |
| 1317 | struct Scsi_Host *shost; |
James Bottomley | 10c1b88 | 2005-08-14 14:34:06 -0500 | [diff] [blame] | 1318 | struct scsi_target *starget; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1319 | 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 Bottomley | 10c1b88 | 2005-08-14 14:34:06 -0500 | [diff] [blame] | 1330 | starget = to_scsi_target(dev); |
| 1331 | |
| 1332 | if (i->f->deny_binding && i->f->deny_binding(starget)) |
| 1333 | return 0; |
| 1334 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | return &i->t.target_attrs.ac == cont; |
| 1336 | } |
| 1337 | |
| 1338 | static DECLARE_TRANSPORT_CLASS(spi_transport_class, |
| 1339 | "spi_transport", |
| 1340 | spi_setup_transport_attrs, |
| 1341 | NULL, |
James Bottomley | 9b161a4 | 2008-01-05 10:18:27 -0600 | [diff] [blame] | 1342 | spi_target_configure); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | |
| 1344 | static DECLARE_ANON_TRANSPORT_CLASS(spi_device_class, |
| 1345 | spi_device_match, |
| 1346 | spi_device_configure); |
| 1347 | |
James Bottomley | 9b161a4 | 2008-01-05 10:18:27 -0600 | [diff] [blame] | 1348 | static struct attribute *host_attributes[] = { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 1349 | &dev_attr_signalling.attr, |
James Bottomley | 9b161a4 | 2008-01-05 10:18:27 -0600 | [diff] [blame] | 1350 | NULL |
| 1351 | }; |
| 1352 | |
| 1353 | static struct attribute_group host_attribute_group = { |
| 1354 | .attrs = host_attributes, |
| 1355 | }; |
| 1356 | |
| 1357 | static int spi_host_configure(struct transport_container *tc, |
| 1358 | struct device *dev, |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 1359 | struct device *cdev) |
James Bottomley | 9b161a4 | 2008-01-05 10:18:27 -0600 | [diff] [blame] | 1360 | { |
| 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 Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 1364 | struct attribute *attr = &dev_attr_signalling.attr; |
James Bottomley | 9b161a4 | 2008-01-05 10:18:27 -0600 | [diff] [blame] | 1365 | 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 | |
| 1380 | static int target_attribute_is_visible(struct kobject *kobj, |
| 1381 | struct attribute *attr, int i) |
| 1382 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 1383 | struct device *cdev = container_of(kobj, struct device, kobj); |
James Bottomley | 9b161a4 | 2008-01-05 10:18:27 -0600 | [diff] [blame] | 1384 | 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 Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 1388 | if (attr == &dev_attr_period.attr && |
James Bottomley | 9b161a4 | 2008-01-05 10:18:27 -0600 | [diff] [blame] | 1389 | spi_support_sync(starget)) |
| 1390 | return TARGET_ATTRIBUTE_HELPER(period); |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 1391 | else if (attr == &dev_attr_min_period.attr && |
James Bottomley | 9b161a4 | 2008-01-05 10:18:27 -0600 | [diff] [blame] | 1392 | spi_support_sync(starget)) |
| 1393 | return TARGET_ATTRIBUTE_HELPER(period); |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 1394 | else if (attr == &dev_attr_offset.attr && |
James Bottomley | 9b161a4 | 2008-01-05 10:18:27 -0600 | [diff] [blame] | 1395 | spi_support_sync(starget)) |
| 1396 | return TARGET_ATTRIBUTE_HELPER(offset); |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 1397 | else if (attr == &dev_attr_max_offset.attr && |
James Bottomley | 9b161a4 | 2008-01-05 10:18:27 -0600 | [diff] [blame] | 1398 | spi_support_sync(starget)) |
| 1399 | return TARGET_ATTRIBUTE_HELPER(offset); |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 1400 | else if (attr == &dev_attr_width.attr && |
James Bottomley | 9b161a4 | 2008-01-05 10:18:27 -0600 | [diff] [blame] | 1401 | spi_support_wide(starget)) |
| 1402 | return TARGET_ATTRIBUTE_HELPER(width); |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 1403 | else if (attr == &dev_attr_max_width.attr && |
James Bottomley | 9b161a4 | 2008-01-05 10:18:27 -0600 | [diff] [blame] | 1404 | spi_support_wide(starget)) |
| 1405 | return TARGET_ATTRIBUTE_HELPER(width); |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 1406 | else if (attr == &dev_attr_iu.attr && |
James Bottomley | 9b161a4 | 2008-01-05 10:18:27 -0600 | [diff] [blame] | 1407 | spi_support_ius(starget)) |
| 1408 | return TARGET_ATTRIBUTE_HELPER(iu); |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 1409 | else if (attr == &dev_attr_dt.attr && |
James Bottomley | 9b161a4 | 2008-01-05 10:18:27 -0600 | [diff] [blame] | 1410 | spi_support_dt(starget)) |
| 1411 | return TARGET_ATTRIBUTE_HELPER(dt); |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 1412 | else if (attr == &dev_attr_qas.attr && |
James Bottomley | 9b161a4 | 2008-01-05 10:18:27 -0600 | [diff] [blame] | 1413 | spi_support_qas(starget)) |
| 1414 | return TARGET_ATTRIBUTE_HELPER(qas); |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 1415 | else if (attr == &dev_attr_wr_flow.attr && |
James Bottomley | 9b161a4 | 2008-01-05 10:18:27 -0600 | [diff] [blame] | 1416 | spi_support_ius(starget)) |
| 1417 | return TARGET_ATTRIBUTE_HELPER(wr_flow); |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 1418 | else if (attr == &dev_attr_rd_strm.attr && |
James Bottomley | 9b161a4 | 2008-01-05 10:18:27 -0600 | [diff] [blame] | 1419 | spi_support_ius(starget)) |
| 1420 | return TARGET_ATTRIBUTE_HELPER(rd_strm); |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 1421 | else if (attr == &dev_attr_rti.attr && |
James Bottomley | 9b161a4 | 2008-01-05 10:18:27 -0600 | [diff] [blame] | 1422 | spi_support_ius(starget)) |
| 1423 | return TARGET_ATTRIBUTE_HELPER(rti); |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 1424 | else if (attr == &dev_attr_pcomp_en.attr && |
James Bottomley | 9b161a4 | 2008-01-05 10:18:27 -0600 | [diff] [blame] | 1425 | spi_support_ius(starget)) |
| 1426 | return TARGET_ATTRIBUTE_HELPER(pcomp_en); |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 1427 | else if (attr == &dev_attr_hold_mcs.attr && |
James Bottomley | 9b161a4 | 2008-01-05 10:18:27 -0600 | [diff] [blame] | 1428 | spi_support_ius(starget)) |
| 1429 | return TARGET_ATTRIBUTE_HELPER(hold_mcs); |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 1430 | else if (attr == &dev_attr_revalidate.attr) |
James Bottomley | 9b161a4 | 2008-01-05 10:18:27 -0600 | [diff] [blame] | 1431 | return 1; |
| 1432 | |
| 1433 | return 0; |
| 1434 | } |
| 1435 | |
| 1436 | static struct attribute *target_attributes[] = { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 1437 | &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 Bottomley | 9b161a4 | 2008-01-05 10:18:27 -0600 | [diff] [blame] | 1452 | NULL |
| 1453 | }; |
| 1454 | |
| 1455 | static struct attribute_group target_attribute_group = { |
| 1456 | .attrs = target_attributes, |
| 1457 | .is_visible = target_attribute_is_visible, |
| 1458 | }; |
| 1459 | |
| 1460 | static int spi_target_configure(struct transport_container *tc, |
| 1461 | struct device *dev, |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 1462 | struct device *cdev) |
James Bottomley | 9b161a4 | 2008-01-05 10:18:27 -0600 | [diff] [blame] | 1463 | { |
| 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 Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame^] | 1476 | if (j && attr != &dev_attr_revalidate.attr) |
James Bottomley | 9b161a4 | 2008-01-05 10:18:27 -0600 | [diff] [blame] | 1477 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1488 | struct scsi_transport_template * |
| 1489 | spi_attach_transport(struct spi_function_template *ft) |
| 1490 | { |
Jes Sorensen | 24669f75 | 2006-01-16 10:31:18 -0500 | [diff] [blame] | 1491 | struct spi_internal *i = kzalloc(sizeof(struct spi_internal), |
| 1492 | GFP_KERNEL); |
| 1493 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1494 | if (unlikely(!i)) |
| 1495 | return NULL; |
| 1496 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | i->t.target_attrs.ac.class = &spi_transport_class.class; |
James Bottomley | 9b161a4 | 2008-01-05 10:18:27 -0600 | [diff] [blame] | 1498 | i->t.target_attrs.ac.grp = &target_attribute_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | 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 Bottomley | 9b161a4 | 2008-01-05 10:18:27 -0600 | [diff] [blame] | 1503 | i->t.host_attrs.ac.grp = &host_attribute_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1509 | return &i->t; |
| 1510 | } |
| 1511 | EXPORT_SYMBOL(spi_attach_transport); |
| 1512 | |
| 1513 | void 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 | } |
| 1522 | EXPORT_SYMBOL(spi_release_transport); |
| 1523 | |
| 1524 | static __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 | |
| 1533 | static 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 | |
| 1540 | MODULE_AUTHOR("Martin Hicks"); |
| 1541 | MODULE_DESCRIPTION("SPI Transport Attributes"); |
| 1542 | MODULE_LICENSE("GPL"); |
| 1543 | |
| 1544 | module_init(spi_transport_init); |
| 1545 | module_exit(spi_transport_exit); |