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> |
| 25 | #include <asm/semaphore.h> |
| 26 | #include <scsi/scsi.h> |
| 27 | #include "scsi_priv.h" |
| 28 | #include <scsi/scsi_device.h> |
| 29 | #include <scsi/scsi_host.h> |
| 30 | #include <scsi/scsi_request.h> |
| 31 | #include <scsi/scsi_eh.h> |
| 32 | #include <scsi/scsi_transport.h> |
| 33 | #include <scsi/scsi_transport_spi.h> |
| 34 | |
| 35 | #define SPI_PRINTK(x, l, f, a...) dev_printk(l, &(x)->dev, f , ##a) |
| 36 | |
| 37 | #define SPI_NUM_ATTRS 10 /* increase this if you add attributes */ |
| 38 | #define SPI_OTHER_ATTRS 1 /* Increase this if you add "always |
| 39 | * on" attributes */ |
| 40 | #define SPI_HOST_ATTRS 1 |
| 41 | |
| 42 | #define SPI_MAX_ECHO_BUFFER_SIZE 4096 |
| 43 | |
| 44 | /* Private data accessors (keep these out of the header file) */ |
| 45 | #define spi_dv_pending(x) (((struct spi_transport_attrs *)&(x)->starget_data)->dv_pending) |
| 46 | #define spi_dv_sem(x) (((struct spi_transport_attrs *)&(x)->starget_data)->dv_sem) |
| 47 | |
| 48 | struct spi_internal { |
| 49 | struct scsi_transport_template t; |
| 50 | struct spi_function_template *f; |
| 51 | /* The actual attributes */ |
| 52 | struct class_device_attribute private_attrs[SPI_NUM_ATTRS]; |
| 53 | /* The array of null terminated pointers to attributes |
| 54 | * needed by scsi_sysfs.c */ |
| 55 | struct class_device_attribute *attrs[SPI_NUM_ATTRS + SPI_OTHER_ATTRS + 1]; |
| 56 | struct class_device_attribute private_host_attrs[SPI_HOST_ATTRS]; |
| 57 | struct class_device_attribute *host_attrs[SPI_HOST_ATTRS + 1]; |
| 58 | }; |
| 59 | |
| 60 | #define to_spi_internal(tmpl) container_of(tmpl, struct spi_internal, t) |
| 61 | |
| 62 | static const int ppr_to_ps[] = { |
| 63 | /* The PPR values 0-6 are reserved, fill them in when |
| 64 | * the committee defines them */ |
| 65 | -1, /* 0x00 */ |
| 66 | -1, /* 0x01 */ |
| 67 | -1, /* 0x02 */ |
| 68 | -1, /* 0x03 */ |
| 69 | -1, /* 0x04 */ |
| 70 | -1, /* 0x05 */ |
| 71 | -1, /* 0x06 */ |
| 72 | 3125, /* 0x07 */ |
| 73 | 6250, /* 0x08 */ |
| 74 | 12500, /* 0x09 */ |
| 75 | 25000, /* 0x0a */ |
| 76 | 30300, /* 0x0b */ |
| 77 | 50000, /* 0x0c */ |
| 78 | }; |
| 79 | /* The PPR values at which you calculate the period in ns by multiplying |
| 80 | * by 4 */ |
| 81 | #define SPI_STATIC_PPR 0x0c |
| 82 | |
| 83 | static int sprint_frac(char *dest, int value, int denom) |
| 84 | { |
| 85 | int frac = value % denom; |
| 86 | int result = sprintf(dest, "%d", value / denom); |
| 87 | |
| 88 | if (frac == 0) |
| 89 | return result; |
| 90 | dest[result++] = '.'; |
| 91 | |
| 92 | do { |
| 93 | denom /= 10; |
| 94 | sprintf(dest + result, "%d", frac / denom); |
| 95 | result++; |
| 96 | frac %= denom; |
| 97 | } while (frac); |
| 98 | |
| 99 | dest[result++] = '\0'; |
| 100 | return result; |
| 101 | } |
| 102 | |
| 103 | static struct { |
| 104 | enum spi_signal_type value; |
| 105 | char *name; |
| 106 | } signal_types[] = { |
| 107 | { SPI_SIGNAL_UNKNOWN, "unknown" }, |
| 108 | { SPI_SIGNAL_SE, "SE" }, |
| 109 | { SPI_SIGNAL_LVD, "LVD" }, |
| 110 | { SPI_SIGNAL_HVD, "HVD" }, |
| 111 | }; |
| 112 | |
| 113 | static inline const char *spi_signal_to_string(enum spi_signal_type type) |
| 114 | { |
| 115 | int i; |
| 116 | |
| 117 | for (i = 0; i < sizeof(signal_types)/sizeof(signal_types[0]); i++) { |
| 118 | if (type == signal_types[i].value) |
| 119 | return signal_types[i].name; |
| 120 | } |
| 121 | return NULL; |
| 122 | } |
| 123 | static inline enum spi_signal_type spi_signal_to_value(const char *name) |
| 124 | { |
| 125 | int i, len; |
| 126 | |
| 127 | for (i = 0; i < sizeof(signal_types)/sizeof(signal_types[0]); i++) { |
| 128 | len = strlen(signal_types[i].name); |
| 129 | if (strncmp(name, signal_types[i].name, len) == 0 && |
| 130 | (name[len] == '\n' || name[len] == '\0')) |
| 131 | return signal_types[i].value; |
| 132 | } |
| 133 | return SPI_SIGNAL_UNKNOWN; |
| 134 | } |
| 135 | |
| 136 | static int spi_host_setup(struct device *dev) |
| 137 | { |
| 138 | struct Scsi_Host *shost = dev_to_shost(dev); |
| 139 | |
| 140 | spi_signalling(shost) = SPI_SIGNAL_UNKNOWN; |
| 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | static DECLARE_TRANSPORT_CLASS(spi_host_class, |
| 146 | "spi_host", |
| 147 | spi_host_setup, |
| 148 | NULL, |
| 149 | NULL); |
| 150 | |
| 151 | static int spi_host_match(struct attribute_container *cont, |
| 152 | struct device *dev) |
| 153 | { |
| 154 | struct Scsi_Host *shost; |
| 155 | struct spi_internal *i; |
| 156 | |
| 157 | if (!scsi_is_host_device(dev)) |
| 158 | return 0; |
| 159 | |
| 160 | shost = dev_to_shost(dev); |
| 161 | if (!shost->transportt || shost->transportt->host_attrs.ac.class |
| 162 | != &spi_host_class.class) |
| 163 | return 0; |
| 164 | |
| 165 | i = to_spi_internal(shost->transportt); |
| 166 | |
| 167 | return &i->t.host_attrs.ac == cont; |
| 168 | } |
| 169 | |
| 170 | static int spi_device_configure(struct device *dev) |
| 171 | { |
| 172 | struct scsi_device *sdev = to_scsi_device(dev); |
| 173 | struct scsi_target *starget = sdev->sdev_target; |
| 174 | |
| 175 | /* Populate the target capability fields with the values |
| 176 | * gleaned from the device inquiry */ |
| 177 | |
| 178 | spi_support_sync(starget) = scsi_device_sync(sdev); |
| 179 | spi_support_wide(starget) = scsi_device_wide(sdev); |
| 180 | spi_support_dt(starget) = scsi_device_dt(sdev); |
| 181 | spi_support_dt_only(starget) = scsi_device_dt_only(sdev); |
| 182 | spi_support_ius(starget) = scsi_device_ius(sdev); |
| 183 | spi_support_qas(starget) = scsi_device_qas(sdev); |
| 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | static int spi_setup_transport_attrs(struct device *dev) |
| 189 | { |
| 190 | struct scsi_target *starget = to_scsi_target(dev); |
| 191 | |
| 192 | spi_period(starget) = -1; /* illegal value */ |
| 193 | spi_offset(starget) = 0; /* async */ |
| 194 | spi_width(starget) = 0; /* narrow */ |
| 195 | spi_iu(starget) = 0; /* no IU */ |
| 196 | spi_dt(starget) = 0; /* ST */ |
| 197 | spi_qas(starget) = 0; |
| 198 | spi_wr_flow(starget) = 0; |
| 199 | spi_rd_strm(starget) = 0; |
| 200 | spi_rti(starget) = 0; |
| 201 | spi_pcomp_en(starget) = 0; |
| 202 | spi_dv_pending(starget) = 0; |
| 203 | spi_initial_dv(starget) = 0; |
| 204 | init_MUTEX(&spi_dv_sem(starget)); |
| 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | #define spi_transport_show_function(field, format_string) \ |
| 210 | \ |
| 211 | static ssize_t \ |
| 212 | show_spi_transport_##field(struct class_device *cdev, char *buf) \ |
| 213 | { \ |
| 214 | struct scsi_target *starget = transport_class_to_starget(cdev); \ |
| 215 | struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \ |
| 216 | struct spi_transport_attrs *tp; \ |
| 217 | struct spi_internal *i = to_spi_internal(shost->transportt); \ |
| 218 | tp = (struct spi_transport_attrs *)&starget->starget_data; \ |
| 219 | if (i->f->get_##field) \ |
| 220 | i->f->get_##field(starget); \ |
| 221 | return snprintf(buf, 20, format_string, tp->field); \ |
| 222 | } |
| 223 | |
| 224 | #define spi_transport_store_function(field, format_string) \ |
| 225 | static ssize_t \ |
| 226 | store_spi_transport_##field(struct class_device *cdev, const char *buf, \ |
| 227 | size_t count) \ |
| 228 | { \ |
| 229 | int val; \ |
| 230 | struct scsi_target *starget = transport_class_to_starget(cdev); \ |
| 231 | struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \ |
| 232 | struct spi_internal *i = to_spi_internal(shost->transportt); \ |
| 233 | \ |
| 234 | val = simple_strtoul(buf, NULL, 0); \ |
| 235 | i->f->set_##field(starget, val); \ |
| 236 | return count; \ |
| 237 | } |
| 238 | |
| 239 | #define spi_transport_rd_attr(field, format_string) \ |
| 240 | spi_transport_show_function(field, format_string) \ |
| 241 | spi_transport_store_function(field, format_string) \ |
| 242 | static CLASS_DEVICE_ATTR(field, S_IRUGO | S_IWUSR, \ |
| 243 | show_spi_transport_##field, \ |
| 244 | store_spi_transport_##field); |
| 245 | |
| 246 | /* The Parallel SCSI Tranport Attributes: */ |
| 247 | spi_transport_rd_attr(offset, "%d\n"); |
| 248 | spi_transport_rd_attr(width, "%d\n"); |
| 249 | spi_transport_rd_attr(iu, "%d\n"); |
| 250 | spi_transport_rd_attr(dt, "%d\n"); |
| 251 | spi_transport_rd_attr(qas, "%d\n"); |
| 252 | spi_transport_rd_attr(wr_flow, "%d\n"); |
| 253 | spi_transport_rd_attr(rd_strm, "%d\n"); |
| 254 | spi_transport_rd_attr(rti, "%d\n"); |
| 255 | spi_transport_rd_attr(pcomp_en, "%d\n"); |
| 256 | |
| 257 | static ssize_t |
| 258 | store_spi_revalidate(struct class_device *cdev, const char *buf, size_t count) |
| 259 | { |
| 260 | struct scsi_target *starget = transport_class_to_starget(cdev); |
| 261 | |
| 262 | /* FIXME: we're relying on an awful lot of device internals |
| 263 | * here. We really need a function to get the first available |
| 264 | * child */ |
| 265 | struct device *dev = container_of(starget->dev.children.next, struct device, node); |
| 266 | struct scsi_device *sdev = to_scsi_device(dev); |
| 267 | spi_dv_device(sdev); |
| 268 | return count; |
| 269 | } |
| 270 | static CLASS_DEVICE_ATTR(revalidate, S_IWUSR, NULL, store_spi_revalidate); |
| 271 | |
| 272 | /* Translate the period into ns according to the current spec |
| 273 | * for SDTR/PPR messages */ |
| 274 | static ssize_t show_spi_transport_period(struct class_device *cdev, char *buf) |
| 275 | |
| 276 | { |
| 277 | struct scsi_target *starget = transport_class_to_starget(cdev); |
| 278 | struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); |
| 279 | struct spi_transport_attrs *tp; |
| 280 | int len, picosec; |
| 281 | struct spi_internal *i = to_spi_internal(shost->transportt); |
| 282 | |
| 283 | tp = (struct spi_transport_attrs *)&starget->starget_data; |
| 284 | |
| 285 | if (i->f->get_period) |
| 286 | i->f->get_period(starget); |
| 287 | |
| 288 | if (tp->period < 0 || tp->period > 0xff) { |
| 289 | picosec = -1; |
| 290 | } else if (tp->period <= SPI_STATIC_PPR) { |
| 291 | picosec = ppr_to_ps[tp->period]; |
| 292 | } else { |
| 293 | picosec = tp->period * 4000; |
| 294 | } |
| 295 | |
| 296 | if (picosec == -1) { |
| 297 | len = sprintf(buf, "reserved"); |
| 298 | } else { |
| 299 | len = sprint_frac(buf, picosec, 1000); |
| 300 | } |
| 301 | |
| 302 | buf[len++] = '\n'; |
| 303 | buf[len] = '\0'; |
| 304 | return len; |
| 305 | } |
| 306 | |
| 307 | static ssize_t |
| 308 | store_spi_transport_period(struct class_device *cdev, const char *buf, |
| 309 | size_t count) |
| 310 | { |
| 311 | struct scsi_target *starget = transport_class_to_starget(cdev); |
| 312 | struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); |
| 313 | struct spi_internal *i = to_spi_internal(shost->transportt); |
| 314 | int j, picosec, period = -1; |
| 315 | char *endp; |
| 316 | |
| 317 | picosec = simple_strtoul(buf, &endp, 10) * 1000; |
| 318 | if (*endp == '.') { |
| 319 | int mult = 100; |
| 320 | do { |
| 321 | endp++; |
| 322 | if (!isdigit(*endp)) |
| 323 | break; |
| 324 | picosec += (*endp - '0') * mult; |
| 325 | mult /= 10; |
| 326 | } while (mult > 0); |
| 327 | } |
| 328 | |
| 329 | for (j = 0; j <= SPI_STATIC_PPR; j++) { |
| 330 | if (ppr_to_ps[j] < picosec) |
| 331 | continue; |
| 332 | period = j; |
| 333 | break; |
| 334 | } |
| 335 | |
| 336 | if (period == -1) |
| 337 | period = picosec / 4000; |
| 338 | |
| 339 | if (period > 0xff) |
| 340 | period = 0xff; |
| 341 | |
| 342 | i->f->set_period(starget, period); |
| 343 | |
| 344 | return count; |
| 345 | } |
| 346 | |
| 347 | static CLASS_DEVICE_ATTR(period, S_IRUGO | S_IWUSR, |
| 348 | show_spi_transport_period, |
| 349 | store_spi_transport_period); |
| 350 | |
| 351 | static ssize_t show_spi_host_signalling(struct class_device *cdev, char *buf) |
| 352 | { |
| 353 | struct Scsi_Host *shost = transport_class_to_shost(cdev); |
| 354 | struct spi_internal *i = to_spi_internal(shost->transportt); |
| 355 | |
| 356 | if (i->f->get_signalling) |
| 357 | i->f->get_signalling(shost); |
| 358 | |
| 359 | return sprintf(buf, "%s\n", spi_signal_to_string(spi_signalling(shost))); |
| 360 | } |
| 361 | static ssize_t store_spi_host_signalling(struct class_device *cdev, |
| 362 | const char *buf, size_t count) |
| 363 | { |
| 364 | struct Scsi_Host *shost = transport_class_to_shost(cdev); |
| 365 | struct spi_internal *i = to_spi_internal(shost->transportt); |
| 366 | enum spi_signal_type type = spi_signal_to_value(buf); |
| 367 | |
| 368 | if (type != SPI_SIGNAL_UNKNOWN) |
| 369 | i->f->set_signalling(shost, type); |
| 370 | |
| 371 | return count; |
| 372 | } |
| 373 | static CLASS_DEVICE_ATTR(signalling, S_IRUGO | S_IWUSR, |
| 374 | show_spi_host_signalling, |
| 375 | store_spi_host_signalling); |
| 376 | |
| 377 | #define DV_SET(x, y) \ |
| 378 | if(i->f->set_##x) \ |
| 379 | i->f->set_##x(sdev->sdev_target, y) |
| 380 | |
| 381 | #define DV_LOOPS 3 |
| 382 | #define DV_TIMEOUT (10*HZ) |
| 383 | #define DV_RETRIES 3 /* should only need at most |
| 384 | * two cc/ua clears */ |
| 385 | |
| 386 | enum spi_compare_returns { |
| 387 | SPI_COMPARE_SUCCESS, |
| 388 | SPI_COMPARE_FAILURE, |
| 389 | SPI_COMPARE_SKIP_TEST, |
| 390 | }; |
| 391 | |
| 392 | |
| 393 | /* This is for read/write Domain Validation: If the device supports |
| 394 | * an echo buffer, we do read/write tests to it */ |
| 395 | static enum spi_compare_returns |
| 396 | spi_dv_device_echo_buffer(struct scsi_request *sreq, u8 *buffer, |
| 397 | u8 *ptr, const int retries) |
| 398 | { |
| 399 | struct scsi_device *sdev = sreq->sr_device; |
| 400 | int len = ptr - buffer; |
| 401 | int j, k, r; |
| 402 | unsigned int pattern = 0x0000ffff; |
| 403 | |
| 404 | const char spi_write_buffer[] = { |
| 405 | WRITE_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0 |
| 406 | }; |
| 407 | const char spi_read_buffer[] = { |
| 408 | READ_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0 |
| 409 | }; |
| 410 | |
| 411 | /* set up the pattern buffer. Doesn't matter if we spill |
| 412 | * slightly beyond since that's where the read buffer is */ |
| 413 | for (j = 0; j < len; ) { |
| 414 | |
| 415 | /* fill the buffer with counting (test a) */ |
| 416 | for ( ; j < min(len, 32); j++) |
| 417 | buffer[j] = j; |
| 418 | k = j; |
| 419 | /* fill the buffer with alternating words of 0x0 and |
| 420 | * 0xffff (test b) */ |
| 421 | for ( ; j < min(len, k + 32); j += 2) { |
| 422 | u16 *word = (u16 *)&buffer[j]; |
| 423 | |
| 424 | *word = (j & 0x02) ? 0x0000 : 0xffff; |
| 425 | } |
| 426 | k = j; |
| 427 | /* fill with crosstalk (alternating 0x5555 0xaaa) |
| 428 | * (test c) */ |
| 429 | for ( ; j < min(len, k + 32); j += 2) { |
| 430 | u16 *word = (u16 *)&buffer[j]; |
| 431 | |
| 432 | *word = (j & 0x02) ? 0x5555 : 0xaaaa; |
| 433 | } |
| 434 | k = j; |
| 435 | /* fill with shifting bits (test d) */ |
| 436 | for ( ; j < min(len, k + 32); j += 4) { |
| 437 | u32 *word = (unsigned int *)&buffer[j]; |
| 438 | u32 roll = (pattern & 0x80000000) ? 1 : 0; |
| 439 | |
| 440 | *word = pattern; |
| 441 | pattern = (pattern << 1) | roll; |
| 442 | } |
| 443 | /* don't bother with random data (test e) */ |
| 444 | } |
| 445 | |
| 446 | for (r = 0; r < retries; r++) { |
| 447 | sreq->sr_cmd_len = 0; /* wait_req to fill in */ |
| 448 | sreq->sr_data_direction = DMA_TO_DEVICE; |
| 449 | scsi_wait_req(sreq, spi_write_buffer, buffer, len, |
| 450 | DV_TIMEOUT, DV_RETRIES); |
| 451 | if(sreq->sr_result || !scsi_device_online(sdev)) { |
| 452 | struct scsi_sense_hdr sshdr; |
| 453 | |
| 454 | scsi_device_set_state(sdev, SDEV_QUIESCE); |
| 455 | if (scsi_request_normalize_sense(sreq, &sshdr) |
| 456 | && sshdr.sense_key == ILLEGAL_REQUEST |
| 457 | /* INVALID FIELD IN CDB */ |
| 458 | && sshdr.asc == 0x24 && sshdr.ascq == 0x00) |
| 459 | /* This would mean that the drive lied |
| 460 | * to us about supporting an echo |
| 461 | * buffer (unfortunately some Western |
| 462 | * Digital drives do precisely this) |
| 463 | */ |
| 464 | return SPI_COMPARE_SKIP_TEST; |
| 465 | |
| 466 | |
| 467 | SPI_PRINTK(sdev->sdev_target, KERN_ERR, "Write Buffer failure %x\n", sreq->sr_result); |
| 468 | return SPI_COMPARE_FAILURE; |
| 469 | } |
| 470 | |
| 471 | memset(ptr, 0, len); |
| 472 | sreq->sr_cmd_len = 0; /* wait_req to fill in */ |
| 473 | sreq->sr_data_direction = DMA_FROM_DEVICE; |
| 474 | scsi_wait_req(sreq, spi_read_buffer, ptr, len, |
| 475 | DV_TIMEOUT, DV_RETRIES); |
| 476 | scsi_device_set_state(sdev, SDEV_QUIESCE); |
| 477 | |
| 478 | if (memcmp(buffer, ptr, len) != 0) |
| 479 | return SPI_COMPARE_FAILURE; |
| 480 | } |
| 481 | return SPI_COMPARE_SUCCESS; |
| 482 | } |
| 483 | |
| 484 | /* This is for the simplest form of Domain Validation: a read test |
| 485 | * on the inquiry data from the device */ |
| 486 | static enum spi_compare_returns |
| 487 | spi_dv_device_compare_inquiry(struct scsi_request *sreq, u8 *buffer, |
| 488 | u8 *ptr, const int retries) |
| 489 | { |
| 490 | int r; |
| 491 | const int len = sreq->sr_device->inquiry_len; |
| 492 | struct scsi_device *sdev = sreq->sr_device; |
| 493 | const char spi_inquiry[] = { |
| 494 | INQUIRY, 0, 0, 0, len, 0 |
| 495 | }; |
| 496 | |
| 497 | for (r = 0; r < retries; r++) { |
| 498 | sreq->sr_cmd_len = 0; /* wait_req to fill in */ |
| 499 | sreq->sr_data_direction = DMA_FROM_DEVICE; |
| 500 | |
| 501 | memset(ptr, 0, len); |
| 502 | |
| 503 | scsi_wait_req(sreq, spi_inquiry, ptr, len, |
| 504 | DV_TIMEOUT, DV_RETRIES); |
| 505 | |
| 506 | if(sreq->sr_result || !scsi_device_online(sdev)) { |
| 507 | scsi_device_set_state(sdev, SDEV_QUIESCE); |
| 508 | return SPI_COMPARE_FAILURE; |
| 509 | } |
| 510 | |
| 511 | /* If we don't have the inquiry data already, the |
| 512 | * first read gets it */ |
| 513 | if (ptr == buffer) { |
| 514 | ptr += len; |
| 515 | --r; |
| 516 | continue; |
| 517 | } |
| 518 | |
| 519 | if (memcmp(buffer, ptr, len) != 0) |
| 520 | /* failure */ |
| 521 | return SPI_COMPARE_FAILURE; |
| 522 | } |
| 523 | return SPI_COMPARE_SUCCESS; |
| 524 | } |
| 525 | |
| 526 | static enum spi_compare_returns |
| 527 | spi_dv_retrain(struct scsi_request *sreq, u8 *buffer, u8 *ptr, |
| 528 | enum spi_compare_returns |
| 529 | (*compare_fn)(struct scsi_request *, u8 *, u8 *, int)) |
| 530 | { |
| 531 | struct spi_internal *i = to_spi_internal(sreq->sr_host->transportt); |
| 532 | struct scsi_device *sdev = sreq->sr_device; |
| 533 | int period = 0, prevperiod = 0; |
| 534 | enum spi_compare_returns retval; |
| 535 | |
| 536 | |
| 537 | for (;;) { |
| 538 | int newperiod; |
| 539 | retval = compare_fn(sreq, buffer, ptr, DV_LOOPS); |
| 540 | |
| 541 | if (retval == SPI_COMPARE_SUCCESS |
| 542 | || retval == SPI_COMPARE_SKIP_TEST) |
| 543 | break; |
| 544 | |
| 545 | /* OK, retrain, fallback */ |
| 546 | if (i->f->get_period) |
| 547 | i->f->get_period(sdev->sdev_target); |
| 548 | newperiod = spi_period(sdev->sdev_target); |
| 549 | period = newperiod > period ? newperiod : period; |
| 550 | if (period < 0x0d) |
| 551 | period++; |
| 552 | else |
| 553 | period += period >> 1; |
| 554 | |
| 555 | if (unlikely(period > 0xff || period == prevperiod)) { |
| 556 | /* Total failure; set to async and return */ |
| 557 | SPI_PRINTK(sdev->sdev_target, KERN_ERR, "Domain Validation Failure, dropping back to Asynchronous\n"); |
| 558 | DV_SET(offset, 0); |
| 559 | return SPI_COMPARE_FAILURE; |
| 560 | } |
| 561 | SPI_PRINTK(sdev->sdev_target, KERN_ERR, "Domain Validation detected failure, dropping back\n"); |
| 562 | DV_SET(period, period); |
| 563 | prevperiod = period; |
| 564 | } |
| 565 | return retval; |
| 566 | } |
| 567 | |
| 568 | static int |
| 569 | spi_dv_device_get_echo_buffer(struct scsi_request *sreq, u8 *buffer) |
| 570 | { |
| 571 | int l; |
| 572 | |
| 573 | /* first off do a test unit ready. This can error out |
| 574 | * because of reservations or some other reason. If it |
| 575 | * fails, the device won't let us write to the echo buffer |
| 576 | * so just return failure */ |
| 577 | |
| 578 | const char spi_test_unit_ready[] = { |
| 579 | TEST_UNIT_READY, 0, 0, 0, 0, 0 |
| 580 | }; |
| 581 | |
| 582 | const char spi_read_buffer_descriptor[] = { |
| 583 | READ_BUFFER, 0x0b, 0, 0, 0, 0, 0, 0, 4, 0 |
| 584 | }; |
| 585 | |
| 586 | |
| 587 | sreq->sr_cmd_len = 0; |
| 588 | sreq->sr_data_direction = DMA_NONE; |
| 589 | |
| 590 | /* We send a set of three TURs to clear any outstanding |
| 591 | * unit attention conditions if they exist (Otherwise the |
| 592 | * buffer tests won't be happy). If the TUR still fails |
| 593 | * (reservation conflict, device not ready, etc) just |
| 594 | * skip the write tests */ |
| 595 | for (l = 0; ; l++) { |
| 596 | scsi_wait_req(sreq, spi_test_unit_ready, NULL, 0, |
| 597 | DV_TIMEOUT, DV_RETRIES); |
| 598 | |
| 599 | if(sreq->sr_result) { |
| 600 | if(l >= 3) |
| 601 | return 0; |
| 602 | } else { |
| 603 | /* TUR succeeded */ |
| 604 | break; |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | sreq->sr_cmd_len = 0; |
| 609 | sreq->sr_data_direction = DMA_FROM_DEVICE; |
| 610 | |
| 611 | scsi_wait_req(sreq, spi_read_buffer_descriptor, buffer, 4, |
| 612 | DV_TIMEOUT, DV_RETRIES); |
| 613 | |
| 614 | if (sreq->sr_result) |
| 615 | /* Device has no echo buffer */ |
| 616 | return 0; |
| 617 | |
| 618 | return buffer[3] + ((buffer[2] & 0x1f) << 8); |
| 619 | } |
| 620 | |
| 621 | static void |
| 622 | spi_dv_device_internal(struct scsi_request *sreq, u8 *buffer) |
| 623 | { |
| 624 | struct spi_internal *i = to_spi_internal(sreq->sr_host->transportt); |
| 625 | struct scsi_device *sdev = sreq->sr_device; |
| 626 | int len = sdev->inquiry_len; |
| 627 | /* first set us up for narrow async */ |
| 628 | DV_SET(offset, 0); |
| 629 | DV_SET(width, 0); |
| 630 | |
| 631 | if (spi_dv_device_compare_inquiry(sreq, buffer, buffer, DV_LOOPS) |
| 632 | != SPI_COMPARE_SUCCESS) { |
| 633 | SPI_PRINTK(sdev->sdev_target, KERN_ERR, "Domain Validation Initial Inquiry Failed\n"); |
| 634 | /* FIXME: should probably offline the device here? */ |
| 635 | return; |
| 636 | } |
| 637 | |
| 638 | /* test width */ |
| 639 | if (i->f->set_width && sdev->wdtr) { |
| 640 | i->f->set_width(sdev->sdev_target, 1); |
| 641 | |
| 642 | if (spi_dv_device_compare_inquiry(sreq, buffer, |
| 643 | buffer + len, |
| 644 | DV_LOOPS) |
| 645 | != SPI_COMPARE_SUCCESS) { |
| 646 | SPI_PRINTK(sdev->sdev_target, KERN_ERR, "Wide Transfers Fail\n"); |
| 647 | i->f->set_width(sdev->sdev_target, 0); |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | if (!i->f->set_period) |
| 652 | return; |
| 653 | |
| 654 | /* device can't handle synchronous */ |
| 655 | if(!sdev->ppr && !sdev->sdtr) |
| 656 | return; |
| 657 | |
| 658 | /* see if the device has an echo buffer. If it does we can |
| 659 | * do the SPI pattern write tests */ |
| 660 | |
| 661 | len = 0; |
| 662 | if (sdev->ppr) |
| 663 | len = spi_dv_device_get_echo_buffer(sreq, buffer); |
| 664 | |
| 665 | retry: |
| 666 | |
| 667 | /* now set up to the maximum */ |
| 668 | DV_SET(offset, 255); |
| 669 | DV_SET(period, 1); |
| 670 | |
| 671 | if (len == 0) { |
| 672 | SPI_PRINTK(sdev->sdev_target, KERN_INFO, "Domain Validation skipping write tests\n"); |
| 673 | spi_dv_retrain(sreq, buffer, buffer + len, |
| 674 | spi_dv_device_compare_inquiry); |
| 675 | return; |
| 676 | } |
| 677 | |
| 678 | if (len > SPI_MAX_ECHO_BUFFER_SIZE) { |
| 679 | SPI_PRINTK(sdev->sdev_target, KERN_WARNING, "Echo buffer size %d is too big, trimming to %d\n", len, SPI_MAX_ECHO_BUFFER_SIZE); |
| 680 | len = SPI_MAX_ECHO_BUFFER_SIZE; |
| 681 | } |
| 682 | |
| 683 | if (spi_dv_retrain(sreq, buffer, buffer + len, |
| 684 | spi_dv_device_echo_buffer) |
| 685 | == SPI_COMPARE_SKIP_TEST) { |
| 686 | /* OK, the stupid drive can't do a write echo buffer |
| 687 | * test after all, fall back to the read tests */ |
| 688 | len = 0; |
| 689 | goto retry; |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | |
| 694 | /** spi_dv_device - Do Domain Validation on the device |
| 695 | * @sdev: scsi device to validate |
| 696 | * |
| 697 | * Performs the domain validation on the given device in the |
| 698 | * current execution thread. Since DV operations may sleep, |
| 699 | * the current thread must have user context. Also no SCSI |
| 700 | * related locks that would deadlock I/O issued by the DV may |
| 701 | * be held. |
| 702 | */ |
| 703 | void |
| 704 | spi_dv_device(struct scsi_device *sdev) |
| 705 | { |
| 706 | struct scsi_request *sreq = scsi_allocate_request(sdev, GFP_KERNEL); |
| 707 | struct scsi_target *starget = sdev->sdev_target; |
| 708 | u8 *buffer; |
| 709 | const int len = SPI_MAX_ECHO_BUFFER_SIZE*2; |
| 710 | |
| 711 | if (unlikely(!sreq)) |
| 712 | return; |
| 713 | |
| 714 | if (unlikely(scsi_device_get(sdev))) |
| 715 | goto out_free_req; |
| 716 | |
| 717 | buffer = kmalloc(len, GFP_KERNEL); |
| 718 | |
| 719 | if (unlikely(!buffer)) |
| 720 | goto out_put; |
| 721 | |
| 722 | memset(buffer, 0, len); |
| 723 | |
| 724 | /* We need to verify that the actual device will quiesce; the |
| 725 | * later target quiesce is just a nice to have */ |
| 726 | if (unlikely(scsi_device_quiesce(sdev))) |
| 727 | goto out_free; |
| 728 | |
| 729 | scsi_target_quiesce(starget); |
| 730 | |
| 731 | spi_dv_pending(starget) = 1; |
| 732 | down(&spi_dv_sem(starget)); |
| 733 | |
| 734 | SPI_PRINTK(starget, KERN_INFO, "Beginning Domain Validation\n"); |
| 735 | |
| 736 | spi_dv_device_internal(sreq, buffer); |
| 737 | |
| 738 | SPI_PRINTK(starget, KERN_INFO, "Ending Domain Validation\n"); |
| 739 | |
| 740 | up(&spi_dv_sem(starget)); |
| 741 | spi_dv_pending(starget) = 0; |
| 742 | |
| 743 | scsi_target_resume(starget); |
| 744 | |
| 745 | spi_initial_dv(starget) = 1; |
| 746 | |
| 747 | out_free: |
| 748 | kfree(buffer); |
| 749 | out_put: |
| 750 | scsi_device_put(sdev); |
| 751 | out_free_req: |
| 752 | scsi_release_request(sreq); |
| 753 | } |
| 754 | EXPORT_SYMBOL(spi_dv_device); |
| 755 | |
| 756 | struct work_queue_wrapper { |
| 757 | struct work_struct work; |
| 758 | struct scsi_device *sdev; |
| 759 | }; |
| 760 | |
| 761 | static void |
| 762 | spi_dv_device_work_wrapper(void *data) |
| 763 | { |
| 764 | struct work_queue_wrapper *wqw = (struct work_queue_wrapper *)data; |
| 765 | struct scsi_device *sdev = wqw->sdev; |
| 766 | |
| 767 | kfree(wqw); |
| 768 | spi_dv_device(sdev); |
| 769 | spi_dv_pending(sdev->sdev_target) = 0; |
| 770 | scsi_device_put(sdev); |
| 771 | } |
| 772 | |
| 773 | |
| 774 | /** |
| 775 | * spi_schedule_dv_device - schedule domain validation to occur on the device |
| 776 | * @sdev: The device to validate |
| 777 | * |
| 778 | * Identical to spi_dv_device() above, except that the DV will be |
| 779 | * scheduled to occur in a workqueue later. All memory allocations |
| 780 | * are atomic, so may be called from any context including those holding |
| 781 | * SCSI locks. |
| 782 | */ |
| 783 | void |
| 784 | spi_schedule_dv_device(struct scsi_device *sdev) |
| 785 | { |
| 786 | struct work_queue_wrapper *wqw = |
| 787 | kmalloc(sizeof(struct work_queue_wrapper), GFP_ATOMIC); |
| 788 | |
| 789 | if (unlikely(!wqw)) |
| 790 | return; |
| 791 | |
| 792 | if (unlikely(spi_dv_pending(sdev->sdev_target))) { |
| 793 | kfree(wqw); |
| 794 | return; |
| 795 | } |
| 796 | /* Set pending early (dv_device doesn't check it, only sets it) */ |
| 797 | spi_dv_pending(sdev->sdev_target) = 1; |
| 798 | if (unlikely(scsi_device_get(sdev))) { |
| 799 | kfree(wqw); |
| 800 | spi_dv_pending(sdev->sdev_target) = 0; |
| 801 | return; |
| 802 | } |
| 803 | |
| 804 | INIT_WORK(&wqw->work, spi_dv_device_work_wrapper, wqw); |
| 805 | wqw->sdev = sdev; |
| 806 | |
| 807 | schedule_work(&wqw->work); |
| 808 | } |
| 809 | EXPORT_SYMBOL(spi_schedule_dv_device); |
| 810 | |
| 811 | /** |
| 812 | * spi_display_xfer_agreement - Print the current target transfer agreement |
| 813 | * @starget: The target for which to display the agreement |
| 814 | * |
| 815 | * Each SPI port is required to maintain a transfer agreement for each |
| 816 | * other port on the bus. This function prints a one-line summary of |
| 817 | * the current agreement; more detailed information is available in sysfs. |
| 818 | */ |
| 819 | void spi_display_xfer_agreement(struct scsi_target *starget) |
| 820 | { |
| 821 | struct spi_transport_attrs *tp; |
| 822 | tp = (struct spi_transport_attrs *)&starget->starget_data; |
| 823 | |
| 824 | if (tp->offset > 0 && tp->period > 0) { |
| 825 | unsigned int picosec, kb100; |
| 826 | char *scsi = "FAST-?"; |
| 827 | char tmp[8]; |
| 828 | |
| 829 | if (tp->period <= SPI_STATIC_PPR) { |
| 830 | picosec = ppr_to_ps[tp->period]; |
| 831 | switch (tp->period) { |
| 832 | case 7: scsi = "FAST-320"; break; |
| 833 | case 8: scsi = "FAST-160"; break; |
| 834 | case 9: scsi = "FAST-80"; break; |
| 835 | case 10: |
| 836 | case 11: scsi = "FAST-40"; break; |
| 837 | case 12: scsi = "FAST-20"; break; |
| 838 | } |
| 839 | } else { |
| 840 | picosec = tp->period * 4000; |
| 841 | if (tp->period < 25) |
| 842 | scsi = "FAST-20"; |
| 843 | else if (tp->period < 50) |
| 844 | scsi = "FAST-10"; |
| 845 | else |
| 846 | scsi = "FAST-5"; |
| 847 | } |
| 848 | |
| 849 | kb100 = (10000000 + picosec / 2) / picosec; |
| 850 | if (tp->width) |
| 851 | kb100 *= 2; |
| 852 | sprint_frac(tmp, picosec, 1000); |
| 853 | |
| 854 | dev_info(&starget->dev, |
| 855 | "%s %sSCSI %d.%d MB/s %s%s%s (%s ns, offset %d)\n", |
| 856 | scsi, tp->width ? "WIDE " : "", kb100/10, kb100 % 10, |
| 857 | tp->dt ? "DT" : "ST", tp->iu ? " IU" : "", |
| 858 | tp->qas ? " QAS" : "", tmp, tp->offset); |
| 859 | } else { |
| 860 | dev_info(&starget->dev, "%sasynchronous.\n", |
| 861 | tp->width ? "wide " : ""); |
| 862 | } |
| 863 | } |
| 864 | EXPORT_SYMBOL(spi_display_xfer_agreement); |
| 865 | |
| 866 | #define SETUP_ATTRIBUTE(field) \ |
| 867 | i->private_attrs[count] = class_device_attr_##field; \ |
| 868 | if (!i->f->set_##field) { \ |
| 869 | i->private_attrs[count].attr.mode = S_IRUGO; \ |
| 870 | i->private_attrs[count].store = NULL; \ |
| 871 | } \ |
| 872 | i->attrs[count] = &i->private_attrs[count]; \ |
| 873 | if (i->f->show_##field) \ |
| 874 | count++ |
| 875 | |
| 876 | #define SETUP_HOST_ATTRIBUTE(field) \ |
| 877 | i->private_host_attrs[count] = class_device_attr_##field; \ |
| 878 | if (!i->f->set_##field) { \ |
| 879 | i->private_host_attrs[count].attr.mode = S_IRUGO; \ |
| 880 | i->private_host_attrs[count].store = NULL; \ |
| 881 | } \ |
| 882 | i->host_attrs[count] = &i->private_host_attrs[count]; \ |
| 883 | count++ |
| 884 | |
| 885 | static int spi_device_match(struct attribute_container *cont, |
| 886 | struct device *dev) |
| 887 | { |
| 888 | struct scsi_device *sdev; |
| 889 | struct Scsi_Host *shost; |
| 890 | |
| 891 | if (!scsi_is_sdev_device(dev)) |
| 892 | return 0; |
| 893 | |
| 894 | sdev = to_scsi_device(dev); |
| 895 | shost = sdev->host; |
| 896 | if (!shost->transportt || shost->transportt->host_attrs.ac.class |
| 897 | != &spi_host_class.class) |
| 898 | return 0; |
| 899 | /* Note: this class has no device attributes, so it has |
| 900 | * no per-HBA allocation and thus we don't need to distinguish |
| 901 | * the attribute containers for the device */ |
| 902 | return 1; |
| 903 | } |
| 904 | |
| 905 | static int spi_target_match(struct attribute_container *cont, |
| 906 | struct device *dev) |
| 907 | { |
| 908 | struct Scsi_Host *shost; |
| 909 | struct spi_internal *i; |
| 910 | |
| 911 | if (!scsi_is_target_device(dev)) |
| 912 | return 0; |
| 913 | |
| 914 | shost = dev_to_shost(dev->parent); |
| 915 | if (!shost->transportt || shost->transportt->host_attrs.ac.class |
| 916 | != &spi_host_class.class) |
| 917 | return 0; |
| 918 | |
| 919 | i = to_spi_internal(shost->transportt); |
| 920 | |
| 921 | return &i->t.target_attrs.ac == cont; |
| 922 | } |
| 923 | |
| 924 | static DECLARE_TRANSPORT_CLASS(spi_transport_class, |
| 925 | "spi_transport", |
| 926 | spi_setup_transport_attrs, |
| 927 | NULL, |
| 928 | NULL); |
| 929 | |
| 930 | static DECLARE_ANON_TRANSPORT_CLASS(spi_device_class, |
| 931 | spi_device_match, |
| 932 | spi_device_configure); |
| 933 | |
| 934 | struct scsi_transport_template * |
| 935 | spi_attach_transport(struct spi_function_template *ft) |
| 936 | { |
| 937 | struct spi_internal *i = kmalloc(sizeof(struct spi_internal), |
| 938 | GFP_KERNEL); |
| 939 | int count = 0; |
| 940 | if (unlikely(!i)) |
| 941 | return NULL; |
| 942 | |
| 943 | memset(i, 0, sizeof(struct spi_internal)); |
| 944 | |
| 945 | |
| 946 | i->t.target_attrs.ac.class = &spi_transport_class.class; |
| 947 | i->t.target_attrs.ac.attrs = &i->attrs[0]; |
| 948 | i->t.target_attrs.ac.match = spi_target_match; |
| 949 | transport_container_register(&i->t.target_attrs); |
| 950 | i->t.target_size = sizeof(struct spi_transport_attrs); |
| 951 | i->t.host_attrs.ac.class = &spi_host_class.class; |
| 952 | i->t.host_attrs.ac.attrs = &i->host_attrs[0]; |
| 953 | i->t.host_attrs.ac.match = spi_host_match; |
| 954 | transport_container_register(&i->t.host_attrs); |
| 955 | i->t.host_size = sizeof(struct spi_host_attrs); |
| 956 | i->f = ft; |
| 957 | |
| 958 | SETUP_ATTRIBUTE(period); |
| 959 | SETUP_ATTRIBUTE(offset); |
| 960 | SETUP_ATTRIBUTE(width); |
| 961 | SETUP_ATTRIBUTE(iu); |
| 962 | SETUP_ATTRIBUTE(dt); |
| 963 | SETUP_ATTRIBUTE(qas); |
| 964 | SETUP_ATTRIBUTE(wr_flow); |
| 965 | SETUP_ATTRIBUTE(rd_strm); |
| 966 | SETUP_ATTRIBUTE(rti); |
| 967 | SETUP_ATTRIBUTE(pcomp_en); |
| 968 | |
| 969 | /* if you add an attribute but forget to increase SPI_NUM_ATTRS |
| 970 | * this bug will trigger */ |
| 971 | BUG_ON(count > SPI_NUM_ATTRS); |
| 972 | |
| 973 | i->attrs[count++] = &class_device_attr_revalidate; |
| 974 | |
| 975 | i->attrs[count] = NULL; |
| 976 | |
| 977 | count = 0; |
| 978 | SETUP_HOST_ATTRIBUTE(signalling); |
| 979 | |
| 980 | BUG_ON(count > SPI_HOST_ATTRS); |
| 981 | |
| 982 | i->host_attrs[count] = NULL; |
| 983 | |
| 984 | return &i->t; |
| 985 | } |
| 986 | EXPORT_SYMBOL(spi_attach_transport); |
| 987 | |
| 988 | void spi_release_transport(struct scsi_transport_template *t) |
| 989 | { |
| 990 | struct spi_internal *i = to_spi_internal(t); |
| 991 | |
| 992 | transport_container_unregister(&i->t.target_attrs); |
| 993 | transport_container_unregister(&i->t.host_attrs); |
| 994 | |
| 995 | kfree(i); |
| 996 | } |
| 997 | EXPORT_SYMBOL(spi_release_transport); |
| 998 | |
| 999 | static __init int spi_transport_init(void) |
| 1000 | { |
| 1001 | int error = transport_class_register(&spi_transport_class); |
| 1002 | if (error) |
| 1003 | return error; |
| 1004 | error = anon_transport_class_register(&spi_device_class); |
| 1005 | return transport_class_register(&spi_host_class); |
| 1006 | } |
| 1007 | |
| 1008 | static void __exit spi_transport_exit(void) |
| 1009 | { |
| 1010 | transport_class_unregister(&spi_transport_class); |
| 1011 | anon_transport_class_unregister(&spi_device_class); |
| 1012 | transport_class_unregister(&spi_host_class); |
| 1013 | } |
| 1014 | |
| 1015 | MODULE_AUTHOR("Martin Hicks"); |
| 1016 | MODULE_DESCRIPTION("SPI Transport Attributes"); |
| 1017 | MODULE_LICENSE("GPL"); |
| 1018 | |
| 1019 | module_init(spi_transport_init); |
| 1020 | module_exit(spi_transport_exit); |