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 <linux/blkdev.h> 26 #include <linux/mutex.h> 27 #include <linux/sysfs.h> 28 #include <linux/slab.h> 29 #include <scsi/scsi.h> 30 #include "scsi_priv.h" 31 #include <scsi/scsi_device.h> 32 #include <scsi/scsi_host.h> 33 #include <scsi/scsi_cmnd.h> 34 #include <scsi/scsi_eh.h> 35 #include <scsi/scsi_tcq.h> 36 #include <scsi/scsi_transport.h> 37 #include <scsi/scsi_transport_spi.h> 38 39 #define SPI_NUM_ATTRS 14 /* increase this if you add attributes */ 40 #define SPI_OTHER_ATTRS 1 /* Increase this if you add "always 41 * on" attributes */ 42 #define SPI_HOST_ATTRS 1 43 44 #define SPI_MAX_ECHO_BUFFER_SIZE 4096 45 46 #define DV_LOOPS 3 47 #define DV_TIMEOUT (10*HZ) 48 #define DV_RETRIES 3 /* should only need at most 49 * two cc/ua clears */ 50 51 /* Our blacklist flags */ 52 enum { 53 SPI_BLIST_NOIUS = (__force blist_flags_t)0x1, 54 }; 55 56 /* blacklist table, modelled on scsi_devinfo.c */ 57 static struct { 58 char *vendor; 59 char *model; 60 blist_flags_t flags; 61 } spi_static_device_list[] __initdata = { 62 {"HP", "Ultrium 3-SCSI", SPI_BLIST_NOIUS }, 63 {"IBM", "ULTRIUM-TD3", SPI_BLIST_NOIUS }, 64 {NULL, NULL, 0} 65 }; 66 67 /* Private data accessors (keep these out of the header file) */ 68 #define spi_dv_in_progress(x) (((struct spi_transport_attrs *)&(x)->starget_data)->dv_in_progress) 69 #define spi_dv_mutex(x) (((struct spi_transport_attrs *)&(x)->starget_data)->dv_mutex) 70 71 struct spi_internal { 72 struct scsi_transport_template t; 73 struct spi_function_template *f; 74 }; 75 76 #define to_spi_internal(tmpl) container_of(tmpl, struct spi_internal, t) 77 78 static const int ppr_to_ps[] = { 79 /* The PPR values 0-6 are reserved, fill them in when 80 * the committee defines them */ 81 -1, /* 0x00 */ 82 -1, /* 0x01 */ 83 -1, /* 0x02 */ 84 -1, /* 0x03 */ 85 -1, /* 0x04 */ 86 -1, /* 0x05 */ 87 -1, /* 0x06 */ 88 3125, /* 0x07 */ 89 6250, /* 0x08 */ 90 12500, /* 0x09 */ 91 25000, /* 0x0a */ 92 30300, /* 0x0b */ 93 50000, /* 0x0c */ 94 }; 95 /* The PPR values at which you calculate the period in ns by multiplying 96 * by 4 */ 97 #define SPI_STATIC_PPR 0x0c 98 99 static int sprint_frac(char *dest, int value, int denom) 100 { 101 int frac = value % denom; 102 int result = sprintf(dest, "%d", value / denom); 103 104 if (frac == 0) 105 return result; 106 dest[result++] = '.'; 107 108 do { 109 denom /= 10; 110 sprintf(dest + result, "%d", frac / denom); 111 result++; 112 frac %= denom; 113 } while (frac); 114 115 dest[result++] = '\0'; 116 return result; 117 } 118 119 static int spi_execute(struct scsi_device *sdev, const void *cmd, 120 enum dma_data_direction dir, 121 void *buffer, unsigned bufflen, 122 struct scsi_sense_hdr *sshdr) 123 { 124 int i, result; 125 unsigned char sense[SCSI_SENSE_BUFFERSIZE]; 126 struct scsi_sense_hdr sshdr_tmp; 127 128 if (!sshdr) 129 sshdr = &sshdr_tmp; 130 131 for(i = 0; i < DV_RETRIES; i++) { 132 result = scsi_execute(sdev, cmd, dir, buffer, bufflen, sense, 133 sshdr, DV_TIMEOUT, /* retries */ 1, 134 REQ_FAILFAST_DEV | 135 REQ_FAILFAST_TRANSPORT | 136 REQ_FAILFAST_DRIVER, 137 0, NULL); 138 if (!(driver_byte(result) & DRIVER_SENSE) || 139 sshdr->sense_key != UNIT_ATTENTION) 140 break; 141 } 142 return result; 143 } 144 145 static struct { 146 enum spi_signal_type value; 147 char *name; 148 } signal_types[] = { 149 { SPI_SIGNAL_UNKNOWN, "unknown" }, 150 { SPI_SIGNAL_SE, "SE" }, 151 { SPI_SIGNAL_LVD, "LVD" }, 152 { SPI_SIGNAL_HVD, "HVD" }, 153 }; 154 155 static inline const char *spi_signal_to_string(enum spi_signal_type type) 156 { 157 int i; 158 159 for (i = 0; i < ARRAY_SIZE(signal_types); i++) { 160 if (type == signal_types[i].value) 161 return signal_types[i].name; 162 } 163 return NULL; 164 } 165 static inline enum spi_signal_type spi_signal_to_value(const char *name) 166 { 167 int i, len; 168 169 for (i = 0; i < ARRAY_SIZE(signal_types); i++) { 170 len = strlen(signal_types[i].name); 171 if (strncmp(name, signal_types[i].name, len) == 0 && 172 (name[len] == '\n' || name[len] == '\0')) 173 return signal_types[i].value; 174 } 175 return SPI_SIGNAL_UNKNOWN; 176 } 177 178 static int spi_host_setup(struct transport_container *tc, struct device *dev, 179 struct device *cdev) 180 { 181 struct Scsi_Host *shost = dev_to_shost(dev); 182 183 spi_signalling(shost) = SPI_SIGNAL_UNKNOWN; 184 185 return 0; 186 } 187 188 static int spi_host_configure(struct transport_container *tc, 189 struct device *dev, 190 struct device *cdev); 191 192 static DECLARE_TRANSPORT_CLASS(spi_host_class, 193 "spi_host", 194 spi_host_setup, 195 NULL, 196 spi_host_configure); 197 198 static int spi_host_match(struct attribute_container *cont, 199 struct device *dev) 200 { 201 struct Scsi_Host *shost; 202 203 if (!scsi_is_host_device(dev)) 204 return 0; 205 206 shost = dev_to_shost(dev); 207 if (!shost->transportt || shost->transportt->host_attrs.ac.class 208 != &spi_host_class.class) 209 return 0; 210 211 return &shost->transportt->host_attrs.ac == cont; 212 } 213 214 static int spi_target_configure(struct transport_container *tc, 215 struct device *dev, 216 struct device *cdev); 217 218 static int spi_device_configure(struct transport_container *tc, 219 struct device *dev, 220 struct device *cdev) 221 { 222 struct scsi_device *sdev = to_scsi_device(dev); 223 struct scsi_target *starget = sdev->sdev_target; 224 blist_flags_t bflags; 225 226 bflags = scsi_get_device_flags_keyed(sdev, &sdev->inquiry[8], 227 &sdev->inquiry[16], 228 SCSI_DEVINFO_SPI); 229 230 /* Populate the target capability fields with the values 231 * gleaned from the device inquiry */ 232 233 spi_support_sync(starget) = scsi_device_sync(sdev); 234 spi_support_wide(starget) = scsi_device_wide(sdev); 235 spi_support_dt(starget) = scsi_device_dt(sdev); 236 spi_support_dt_only(starget) = scsi_device_dt_only(sdev); 237 spi_support_ius(starget) = scsi_device_ius(sdev); 238 if (bflags & SPI_BLIST_NOIUS) { 239 dev_info(dev, "Information Units disabled by blacklist\n"); 240 spi_support_ius(starget) = 0; 241 } 242 spi_support_qas(starget) = scsi_device_qas(sdev); 243 244 return 0; 245 } 246 247 static int spi_setup_transport_attrs(struct transport_container *tc, 248 struct device *dev, 249 struct device *cdev) 250 { 251 struct scsi_target *starget = to_scsi_target(dev); 252 253 spi_period(starget) = -1; /* illegal value */ 254 spi_min_period(starget) = 0; 255 spi_offset(starget) = 0; /* async */ 256 spi_max_offset(starget) = 255; 257 spi_width(starget) = 0; /* narrow */ 258 spi_max_width(starget) = 1; 259 spi_iu(starget) = 0; /* no IU */ 260 spi_max_iu(starget) = 1; 261 spi_dt(starget) = 0; /* ST */ 262 spi_qas(starget) = 0; 263 spi_max_qas(starget) = 1; 264 spi_wr_flow(starget) = 0; 265 spi_rd_strm(starget) = 0; 266 spi_rti(starget) = 0; 267 spi_pcomp_en(starget) = 0; 268 spi_hold_mcs(starget) = 0; 269 spi_dv_pending(starget) = 0; 270 spi_dv_in_progress(starget) = 0; 271 spi_initial_dv(starget) = 0; 272 mutex_init(&spi_dv_mutex(starget)); 273 274 return 0; 275 } 276 277 #define spi_transport_show_simple(field, format_string) \ 278 \ 279 static ssize_t \ 280 show_spi_transport_##field(struct device *dev, \ 281 struct device_attribute *attr, char *buf) \ 282 { \ 283 struct scsi_target *starget = transport_class_to_starget(dev); \ 284 struct spi_transport_attrs *tp; \ 285 \ 286 tp = (struct spi_transport_attrs *)&starget->starget_data; \ 287 return snprintf(buf, 20, format_string, tp->field); \ 288 } 289 290 #define spi_transport_store_simple(field, format_string) \ 291 \ 292 static ssize_t \ 293 store_spi_transport_##field(struct device *dev, \ 294 struct device_attribute *attr, \ 295 const char *buf, size_t count) \ 296 { \ 297 int val; \ 298 struct scsi_target *starget = transport_class_to_starget(dev); \ 299 struct spi_transport_attrs *tp; \ 300 \ 301 tp = (struct spi_transport_attrs *)&starget->starget_data; \ 302 val = simple_strtoul(buf, NULL, 0); \ 303 tp->field = val; \ 304 return count; \ 305 } 306 307 #define spi_transport_show_function(field, format_string) \ 308 \ 309 static ssize_t \ 310 show_spi_transport_##field(struct device *dev, \ 311 struct device_attribute *attr, char *buf) \ 312 { \ 313 struct scsi_target *starget = transport_class_to_starget(dev); \ 314 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \ 315 struct spi_transport_attrs *tp; \ 316 struct spi_internal *i = to_spi_internal(shost->transportt); \ 317 tp = (struct spi_transport_attrs *)&starget->starget_data; \ 318 if (i->f->get_##field) \ 319 i->f->get_##field(starget); \ 320 return snprintf(buf, 20, format_string, tp->field); \ 321 } 322 323 #define spi_transport_store_function(field, format_string) \ 324 static ssize_t \ 325 store_spi_transport_##field(struct device *dev, \ 326 struct device_attribute *attr, \ 327 const char *buf, size_t count) \ 328 { \ 329 int val; \ 330 struct scsi_target *starget = transport_class_to_starget(dev); \ 331 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \ 332 struct spi_internal *i = to_spi_internal(shost->transportt); \ 333 \ 334 if (!i->f->set_##field) \ 335 return -EINVAL; \ 336 val = simple_strtoul(buf, NULL, 0); \ 337 i->f->set_##field(starget, val); \ 338 return count; \ 339 } 340 341 #define spi_transport_store_max(field, format_string) \ 342 static ssize_t \ 343 store_spi_transport_##field(struct device *dev, \ 344 struct device_attribute *attr, \ 345 const char *buf, size_t count) \ 346 { \ 347 int val; \ 348 struct scsi_target *starget = transport_class_to_starget(dev); \ 349 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \ 350 struct spi_internal *i = to_spi_internal(shost->transportt); \ 351 struct spi_transport_attrs *tp \ 352 = (struct spi_transport_attrs *)&starget->starget_data; \ 353 \ 354 if (i->f->set_##field) \ 355 return -EINVAL; \ 356 val = simple_strtoul(buf, NULL, 0); \ 357 if (val > tp->max_##field) \ 358 val = tp->max_##field; \ 359 i->f->set_##field(starget, val); \ 360 return count; \ 361 } 362 363 #define spi_transport_rd_attr(field, format_string) \ 364 spi_transport_show_function(field, format_string) \ 365 spi_transport_store_function(field, format_string) \ 366 static DEVICE_ATTR(field, S_IRUGO, \ 367 show_spi_transport_##field, \ 368 store_spi_transport_##field); 369 370 #define spi_transport_simple_attr(field, format_string) \ 371 spi_transport_show_simple(field, format_string) \ 372 spi_transport_store_simple(field, format_string) \ 373 static DEVICE_ATTR(field, S_IRUGO, \ 374 show_spi_transport_##field, \ 375 store_spi_transport_##field); 376 377 #define spi_transport_max_attr(field, format_string) \ 378 spi_transport_show_function(field, format_string) \ 379 spi_transport_store_max(field, format_string) \ 380 spi_transport_simple_attr(max_##field, format_string) \ 381 static DEVICE_ATTR(field, S_IRUGO, \ 382 show_spi_transport_##field, \ 383 store_spi_transport_##field); 384 385 /* The Parallel SCSI Tranport Attributes: */ 386 spi_transport_max_attr(offset, "%d\n"); 387 spi_transport_max_attr(width, "%d\n"); 388 spi_transport_max_attr(iu, "%d\n"); 389 spi_transport_rd_attr(dt, "%d\n"); 390 spi_transport_max_attr(qas, "%d\n"); 391 spi_transport_rd_attr(wr_flow, "%d\n"); 392 spi_transport_rd_attr(rd_strm, "%d\n"); 393 spi_transport_rd_attr(rti, "%d\n"); 394 spi_transport_rd_attr(pcomp_en, "%d\n"); 395 spi_transport_rd_attr(hold_mcs, "%d\n"); 396 397 /* we only care about the first child device that's a real SCSI device 398 * so we return 1 to terminate the iteration when we find it */ 399 static int child_iter(struct device *dev, void *data) 400 { 401 if (!scsi_is_sdev_device(dev)) 402 return 0; 403 404 spi_dv_device(to_scsi_device(dev)); 405 return 1; 406 } 407 408 static ssize_t 409 store_spi_revalidate(struct device *dev, struct device_attribute *attr, 410 const char *buf, size_t count) 411 { 412 struct scsi_target *starget = transport_class_to_starget(dev); 413 414 device_for_each_child(&starget->dev, NULL, child_iter); 415 return count; 416 } 417 static DEVICE_ATTR(revalidate, S_IWUSR, NULL, store_spi_revalidate); 418 419 /* Translate the period into ns according to the current spec 420 * for SDTR/PPR messages */ 421 static int period_to_str(char *buf, int period) 422 { 423 int len, picosec; 424 425 if (period < 0 || period > 0xff) { 426 picosec = -1; 427 } else if (period <= SPI_STATIC_PPR) { 428 picosec = ppr_to_ps[period]; 429 } else { 430 picosec = period * 4000; 431 } 432 433 if (picosec == -1) { 434 len = sprintf(buf, "reserved"); 435 } else { 436 len = sprint_frac(buf, picosec, 1000); 437 } 438 439 return len; 440 } 441 442 static ssize_t 443 show_spi_transport_period_helper(char *buf, int period) 444 { 445 int len = period_to_str(buf, period); 446 buf[len++] = '\n'; 447 buf[len] = '\0'; 448 return len; 449 } 450 451 static ssize_t 452 store_spi_transport_period_helper(struct device *dev, const char *buf, 453 size_t count, int *periodp) 454 { 455 int j, picosec, period = -1; 456 char *endp; 457 458 picosec = simple_strtoul(buf, &endp, 10) * 1000; 459 if (*endp == '.') { 460 int mult = 100; 461 do { 462 endp++; 463 if (!isdigit(*endp)) 464 break; 465 picosec += (*endp - '0') * mult; 466 mult /= 10; 467 } while (mult > 0); 468 } 469 470 for (j = 0; j <= SPI_STATIC_PPR; j++) { 471 if (ppr_to_ps[j] < picosec) 472 continue; 473 period = j; 474 break; 475 } 476 477 if (period == -1) 478 period = picosec / 4000; 479 480 if (period > 0xff) 481 period = 0xff; 482 483 *periodp = period; 484 485 return count; 486 } 487 488 static ssize_t 489 show_spi_transport_period(struct device *dev, 490 struct device_attribute *attr, char *buf) 491 { 492 struct scsi_target *starget = transport_class_to_starget(dev); 493 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); 494 struct spi_internal *i = to_spi_internal(shost->transportt); 495 struct spi_transport_attrs *tp = 496 (struct spi_transport_attrs *)&starget->starget_data; 497 498 if (i->f->get_period) 499 i->f->get_period(starget); 500 501 return show_spi_transport_period_helper(buf, tp->period); 502 } 503 504 static ssize_t 505 store_spi_transport_period(struct device *cdev, struct device_attribute *attr, 506 const char *buf, size_t count) 507 { 508 struct scsi_target *starget = transport_class_to_starget(cdev); 509 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); 510 struct spi_internal *i = to_spi_internal(shost->transportt); 511 struct spi_transport_attrs *tp = 512 (struct spi_transport_attrs *)&starget->starget_data; 513 int period, retval; 514 515 if (!i->f->set_period) 516 return -EINVAL; 517 518 retval = store_spi_transport_period_helper(cdev, buf, count, &period); 519 520 if (period < tp->min_period) 521 period = tp->min_period; 522 523 i->f->set_period(starget, period); 524 525 return retval; 526 } 527 528 static DEVICE_ATTR(period, S_IRUGO, 529 show_spi_transport_period, 530 store_spi_transport_period); 531 532 static ssize_t 533 show_spi_transport_min_period(struct device *cdev, 534 struct device_attribute *attr, char *buf) 535 { 536 struct scsi_target *starget = transport_class_to_starget(cdev); 537 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); 538 struct spi_internal *i = to_spi_internal(shost->transportt); 539 struct spi_transport_attrs *tp = 540 (struct spi_transport_attrs *)&starget->starget_data; 541 542 if (!i->f->set_period) 543 return -EINVAL; 544 545 return show_spi_transport_period_helper(buf, tp->min_period); 546 } 547 548 static ssize_t 549 store_spi_transport_min_period(struct device *cdev, 550 struct device_attribute *attr, 551 const char *buf, size_t count) 552 { 553 struct scsi_target *starget = transport_class_to_starget(cdev); 554 struct spi_transport_attrs *tp = 555 (struct spi_transport_attrs *)&starget->starget_data; 556 557 return store_spi_transport_period_helper(cdev, buf, count, 558 &tp->min_period); 559 } 560 561 562 static DEVICE_ATTR(min_period, S_IRUGO, 563 show_spi_transport_min_period, 564 store_spi_transport_min_period); 565 566 567 static ssize_t show_spi_host_signalling(struct device *cdev, 568 struct device_attribute *attr, 569 char *buf) 570 { 571 struct Scsi_Host *shost = transport_class_to_shost(cdev); 572 struct spi_internal *i = to_spi_internal(shost->transportt); 573 574 if (i->f->get_signalling) 575 i->f->get_signalling(shost); 576 577 return sprintf(buf, "%s\n", spi_signal_to_string(spi_signalling(shost))); 578 } 579 static ssize_t store_spi_host_signalling(struct device *dev, 580 struct device_attribute *attr, 581 const char *buf, size_t count) 582 { 583 struct Scsi_Host *shost = transport_class_to_shost(dev); 584 struct spi_internal *i = to_spi_internal(shost->transportt); 585 enum spi_signal_type type = spi_signal_to_value(buf); 586 587 if (!i->f->set_signalling) 588 return -EINVAL; 589 590 if (type != SPI_SIGNAL_UNKNOWN) 591 i->f->set_signalling(shost, type); 592 593 return count; 594 } 595 static DEVICE_ATTR(signalling, S_IRUGO, 596 show_spi_host_signalling, 597 store_spi_host_signalling); 598 599 static ssize_t show_spi_host_width(struct device *cdev, 600 struct device_attribute *attr, 601 char *buf) 602 { 603 struct Scsi_Host *shost = transport_class_to_shost(cdev); 604 605 return sprintf(buf, "%s\n", shost->max_id == 16 ? "wide" : "narrow"); 606 } 607 static DEVICE_ATTR(host_width, S_IRUGO, 608 show_spi_host_width, NULL); 609 610 static ssize_t show_spi_host_hba_id(struct device *cdev, 611 struct device_attribute *attr, 612 char *buf) 613 { 614 struct Scsi_Host *shost = transport_class_to_shost(cdev); 615 616 return sprintf(buf, "%d\n", shost->this_id); 617 } 618 static DEVICE_ATTR(hba_id, S_IRUGO, 619 show_spi_host_hba_id, NULL); 620 621 #define DV_SET(x, y) \ 622 if(i->f->set_##x) \ 623 i->f->set_##x(sdev->sdev_target, y) 624 625 enum spi_compare_returns { 626 SPI_COMPARE_SUCCESS, 627 SPI_COMPARE_FAILURE, 628 SPI_COMPARE_SKIP_TEST, 629 }; 630 631 632 /* This is for read/write Domain Validation: If the device supports 633 * an echo buffer, we do read/write tests to it */ 634 static enum spi_compare_returns 635 spi_dv_device_echo_buffer(struct scsi_device *sdev, u8 *buffer, 636 u8 *ptr, const int retries) 637 { 638 int len = ptr - buffer; 639 int j, k, r, result; 640 unsigned int pattern = 0x0000ffff; 641 struct scsi_sense_hdr sshdr; 642 643 const char spi_write_buffer[] = { 644 WRITE_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0 645 }; 646 const char spi_read_buffer[] = { 647 READ_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0 648 }; 649 650 /* set up the pattern buffer. Doesn't matter if we spill 651 * slightly beyond since that's where the read buffer is */ 652 for (j = 0; j < len; ) { 653 654 /* fill the buffer with counting (test a) */ 655 for ( ; j < min(len, 32); j++) 656 buffer[j] = j; 657 k = j; 658 /* fill the buffer with alternating words of 0x0 and 659 * 0xffff (test b) */ 660 for ( ; j < min(len, k + 32); j += 2) { 661 u16 *word = (u16 *)&buffer[j]; 662 663 *word = (j & 0x02) ? 0x0000 : 0xffff; 664 } 665 k = j; 666 /* fill with crosstalk (alternating 0x5555 0xaaa) 667 * (test c) */ 668 for ( ; j < min(len, k + 32); j += 2) { 669 u16 *word = (u16 *)&buffer[j]; 670 671 *word = (j & 0x02) ? 0x5555 : 0xaaaa; 672 } 673 k = j; 674 /* fill with shifting bits (test d) */ 675 for ( ; j < min(len, k + 32); j += 4) { 676 u32 *word = (unsigned int *)&buffer[j]; 677 u32 roll = (pattern & 0x80000000) ? 1 : 0; 678 679 *word = pattern; 680 pattern = (pattern << 1) | roll; 681 } 682 /* don't bother with random data (test e) */ 683 } 684 685 for (r = 0; r < retries; r++) { 686 result = spi_execute(sdev, spi_write_buffer, DMA_TO_DEVICE, 687 buffer, len, &sshdr); 688 if(result || !scsi_device_online(sdev)) { 689 690 scsi_device_set_state(sdev, SDEV_QUIESCE); 691 if (scsi_sense_valid(&sshdr) 692 && sshdr.sense_key == ILLEGAL_REQUEST 693 /* INVALID FIELD IN CDB */ 694 && sshdr.asc == 0x24 && sshdr.ascq == 0x00) 695 /* This would mean that the drive lied 696 * to us about supporting an echo 697 * buffer (unfortunately some Western 698 * Digital drives do precisely this) 699 */ 700 return SPI_COMPARE_SKIP_TEST; 701 702 703 sdev_printk(KERN_ERR, sdev, "Write Buffer failure %x\n", result); 704 return SPI_COMPARE_FAILURE; 705 } 706 707 memset(ptr, 0, len); 708 spi_execute(sdev, spi_read_buffer, DMA_FROM_DEVICE, 709 ptr, len, NULL); 710 scsi_device_set_state(sdev, SDEV_QUIESCE); 711 712 if (memcmp(buffer, ptr, len) != 0) 713 return SPI_COMPARE_FAILURE; 714 } 715 return SPI_COMPARE_SUCCESS; 716 } 717 718 /* This is for the simplest form of Domain Validation: a read test 719 * on the inquiry data from the device */ 720 static enum spi_compare_returns 721 spi_dv_device_compare_inquiry(struct scsi_device *sdev, u8 *buffer, 722 u8 *ptr, const int retries) 723 { 724 int r, result; 725 const int len = sdev->inquiry_len; 726 const char spi_inquiry[] = { 727 INQUIRY, 0, 0, 0, len, 0 728 }; 729 730 for (r = 0; r < retries; r++) { 731 memset(ptr, 0, len); 732 733 result = spi_execute(sdev, spi_inquiry, DMA_FROM_DEVICE, 734 ptr, len, NULL); 735 736 if(result || !scsi_device_online(sdev)) { 737 scsi_device_set_state(sdev, SDEV_QUIESCE); 738 return SPI_COMPARE_FAILURE; 739 } 740 741 /* If we don't have the inquiry data already, the 742 * first read gets it */ 743 if (ptr == buffer) { 744 ptr += len; 745 --r; 746 continue; 747 } 748 749 if (memcmp(buffer, ptr, len) != 0) 750 /* failure */ 751 return SPI_COMPARE_FAILURE; 752 } 753 return SPI_COMPARE_SUCCESS; 754 } 755 756 static enum spi_compare_returns 757 spi_dv_retrain(struct scsi_device *sdev, u8 *buffer, u8 *ptr, 758 enum spi_compare_returns 759 (*compare_fn)(struct scsi_device *, u8 *, u8 *, int)) 760 { 761 struct spi_internal *i = to_spi_internal(sdev->host->transportt); 762 struct scsi_target *starget = sdev->sdev_target; 763 int period = 0, prevperiod = 0; 764 enum spi_compare_returns retval; 765 766 767 for (;;) { 768 int newperiod; 769 retval = compare_fn(sdev, buffer, ptr, DV_LOOPS); 770 771 if (retval == SPI_COMPARE_SUCCESS 772 || retval == SPI_COMPARE_SKIP_TEST) 773 break; 774 775 /* OK, retrain, fallback */ 776 if (i->f->get_iu) 777 i->f->get_iu(starget); 778 if (i->f->get_qas) 779 i->f->get_qas(starget); 780 if (i->f->get_period) 781 i->f->get_period(sdev->sdev_target); 782 783 /* Here's the fallback sequence; first try turning off 784 * IU, then QAS (if we can control them), then finally 785 * fall down the periods */ 786 if (i->f->set_iu && spi_iu(starget)) { 787 starget_printk(KERN_ERR, starget, "Domain Validation Disabling Information Units\n"); 788 DV_SET(iu, 0); 789 } else if (i->f->set_qas && spi_qas(starget)) { 790 starget_printk(KERN_ERR, starget, "Domain Validation Disabling Quick Arbitration and Selection\n"); 791 DV_SET(qas, 0); 792 } else { 793 newperiod = spi_period(starget); 794 period = newperiod > period ? newperiod : period; 795 if (period < 0x0d) 796 period++; 797 else 798 period += period >> 1; 799 800 if (unlikely(period > 0xff || period == prevperiod)) { 801 /* Total failure; set to async and return */ 802 starget_printk(KERN_ERR, starget, "Domain Validation Failure, dropping back to Asynchronous\n"); 803 DV_SET(offset, 0); 804 return SPI_COMPARE_FAILURE; 805 } 806 starget_printk(KERN_ERR, starget, "Domain Validation detected failure, dropping back\n"); 807 DV_SET(period, period); 808 prevperiod = period; 809 } 810 } 811 return retval; 812 } 813 814 static int 815 spi_dv_device_get_echo_buffer(struct scsi_device *sdev, u8 *buffer) 816 { 817 int l, result; 818 819 /* first off do a test unit ready. This can error out 820 * because of reservations or some other reason. If it 821 * fails, the device won't let us write to the echo buffer 822 * so just return failure */ 823 824 const char spi_test_unit_ready[] = { 825 TEST_UNIT_READY, 0, 0, 0, 0, 0 826 }; 827 828 const char spi_read_buffer_descriptor[] = { 829 READ_BUFFER, 0x0b, 0, 0, 0, 0, 0, 0, 4, 0 830 }; 831 832 833 /* We send a set of three TURs to clear any outstanding 834 * unit attention conditions if they exist (Otherwise the 835 * buffer tests won't be happy). If the TUR still fails 836 * (reservation conflict, device not ready, etc) just 837 * skip the write tests */ 838 for (l = 0; ; l++) { 839 result = spi_execute(sdev, spi_test_unit_ready, DMA_NONE, 840 NULL, 0, NULL); 841 842 if(result) { 843 if(l >= 3) 844 return 0; 845 } else { 846 /* TUR succeeded */ 847 break; 848 } 849 } 850 851 result = spi_execute(sdev, spi_read_buffer_descriptor, 852 DMA_FROM_DEVICE, buffer, 4, NULL); 853 854 if (result) 855 /* Device has no echo buffer */ 856 return 0; 857 858 return buffer[3] + ((buffer[2] & 0x1f) << 8); 859 } 860 861 static void 862 spi_dv_device_internal(struct scsi_device *sdev, u8 *buffer) 863 { 864 struct spi_internal *i = to_spi_internal(sdev->host->transportt); 865 struct scsi_target *starget = sdev->sdev_target; 866 struct Scsi_Host *shost = sdev->host; 867 int len = sdev->inquiry_len; 868 int min_period = spi_min_period(starget); 869 int max_width = spi_max_width(starget); 870 /* first set us up for narrow async */ 871 DV_SET(offset, 0); 872 DV_SET(width, 0); 873 874 if (spi_dv_device_compare_inquiry(sdev, buffer, buffer, DV_LOOPS) 875 != SPI_COMPARE_SUCCESS) { 876 starget_printk(KERN_ERR, starget, "Domain Validation Initial Inquiry Failed\n"); 877 /* FIXME: should probably offline the device here? */ 878 return; 879 } 880 881 if (!spi_support_wide(starget)) { 882 spi_max_width(starget) = 0; 883 max_width = 0; 884 } 885 886 /* test width */ 887 if (i->f->set_width && max_width) { 888 i->f->set_width(starget, 1); 889 890 if (spi_dv_device_compare_inquiry(sdev, buffer, 891 buffer + len, 892 DV_LOOPS) 893 != SPI_COMPARE_SUCCESS) { 894 starget_printk(KERN_ERR, starget, "Wide Transfers Fail\n"); 895 i->f->set_width(starget, 0); 896 /* Make sure we don't force wide back on by asking 897 * for a transfer period that requires it */ 898 max_width = 0; 899 if (min_period < 10) 900 min_period = 10; 901 } 902 } 903 904 if (!i->f->set_period) 905 return; 906 907 /* device can't handle synchronous */ 908 if (!spi_support_sync(starget) && !spi_support_dt(starget)) 909 return; 910 911 /* len == -1 is the signal that we need to ascertain the 912 * presence of an echo buffer before trying to use it. len == 913 * 0 means we don't have an echo buffer */ 914 len = -1; 915 916 retry: 917 918 /* now set up to the maximum */ 919 DV_SET(offset, spi_max_offset(starget)); 920 DV_SET(period, min_period); 921 922 /* try QAS requests; this should be harmless to set if the 923 * target supports it */ 924 if (spi_support_qas(starget) && spi_max_qas(starget)) { 925 DV_SET(qas, 1); 926 } else { 927 DV_SET(qas, 0); 928 } 929 930 if (spi_support_ius(starget) && spi_max_iu(starget) && 931 min_period < 9) { 932 /* This u320 (or u640). Set IU transfers */ 933 DV_SET(iu, 1); 934 /* Then set the optional parameters */ 935 DV_SET(rd_strm, 1); 936 DV_SET(wr_flow, 1); 937 DV_SET(rti, 1); 938 if (min_period == 8) 939 DV_SET(pcomp_en, 1); 940 } else { 941 DV_SET(iu, 0); 942 } 943 944 /* now that we've done all this, actually check the bus 945 * signal type (if known). Some devices are stupid on 946 * a SE bus and still claim they can try LVD only settings */ 947 if (i->f->get_signalling) 948 i->f->get_signalling(shost); 949 if (spi_signalling(shost) == SPI_SIGNAL_SE || 950 spi_signalling(shost) == SPI_SIGNAL_HVD || 951 !spi_support_dt(starget)) { 952 DV_SET(dt, 0); 953 } else { 954 DV_SET(dt, 1); 955 } 956 /* set width last because it will pull all the other 957 * parameters down to required values */ 958 DV_SET(width, max_width); 959 960 /* Do the read only INQUIRY tests */ 961 spi_dv_retrain(sdev, buffer, buffer + sdev->inquiry_len, 962 spi_dv_device_compare_inquiry); 963 /* See if we actually managed to negotiate and sustain DT */ 964 if (i->f->get_dt) 965 i->f->get_dt(starget); 966 967 /* see if the device has an echo buffer. If it does we can do 968 * the SPI pattern write tests. Because of some broken 969 * devices, we *only* try this on a device that has actually 970 * negotiated DT */ 971 972 if (len == -1 && spi_dt(starget)) 973 len = spi_dv_device_get_echo_buffer(sdev, buffer); 974 975 if (len <= 0) { 976 starget_printk(KERN_INFO, starget, "Domain Validation skipping write tests\n"); 977 return; 978 } 979 980 if (len > SPI_MAX_ECHO_BUFFER_SIZE) { 981 starget_printk(KERN_WARNING, starget, "Echo buffer size %d is too big, trimming to %d\n", len, SPI_MAX_ECHO_BUFFER_SIZE); 982 len = SPI_MAX_ECHO_BUFFER_SIZE; 983 } 984 985 if (spi_dv_retrain(sdev, buffer, buffer + len, 986 spi_dv_device_echo_buffer) 987 == SPI_COMPARE_SKIP_TEST) { 988 /* OK, the stupid drive can't do a write echo buffer 989 * test after all, fall back to the read tests */ 990 len = 0; 991 goto retry; 992 } 993 } 994 995 996 /** spi_dv_device - Do Domain Validation on the device 997 * @sdev: scsi device to validate 998 * 999 * Performs the domain validation on the given device in the 1000 * current execution thread. Since DV operations may sleep, 1001 * the current thread must have user context. Also no SCSI 1002 * related locks that would deadlock I/O issued by the DV may 1003 * be held. 1004 */ 1005 void 1006 spi_dv_device(struct scsi_device *sdev) 1007 { 1008 struct scsi_target *starget = sdev->sdev_target; 1009 u8 *buffer; 1010 const int len = SPI_MAX_ECHO_BUFFER_SIZE*2; 1011 1012 if (unlikely(spi_dv_in_progress(starget))) 1013 return; 1014 1015 if (unlikely(scsi_device_get(sdev))) 1016 return; 1017 spi_dv_in_progress(starget) = 1; 1018 1019 buffer = kzalloc(len, GFP_KERNEL); 1020 1021 if (unlikely(!buffer)) 1022 goto out_put; 1023 1024 /* We need to verify that the actual device will quiesce; the 1025 * later target quiesce is just a nice to have */ 1026 if (unlikely(scsi_device_quiesce(sdev))) 1027 goto out_free; 1028 1029 scsi_target_quiesce(starget); 1030 1031 spi_dv_pending(starget) = 1; 1032 mutex_lock(&spi_dv_mutex(starget)); 1033 1034 starget_printk(KERN_INFO, starget, "Beginning Domain Validation\n"); 1035 1036 spi_dv_device_internal(sdev, buffer); 1037 1038 starget_printk(KERN_INFO, starget, "Ending Domain Validation\n"); 1039 1040 mutex_unlock(&spi_dv_mutex(starget)); 1041 spi_dv_pending(starget) = 0; 1042 1043 scsi_target_resume(starget); 1044 1045 spi_initial_dv(starget) = 1; 1046 1047 out_free: 1048 kfree(buffer); 1049 out_put: 1050 spi_dv_in_progress(starget) = 0; 1051 scsi_device_put(sdev); 1052 } 1053 EXPORT_SYMBOL(spi_dv_device); 1054 1055 struct work_queue_wrapper { 1056 struct work_struct work; 1057 struct scsi_device *sdev; 1058 }; 1059 1060 static void 1061 spi_dv_device_work_wrapper(struct work_struct *work) 1062 { 1063 struct work_queue_wrapper *wqw = 1064 container_of(work, struct work_queue_wrapper, work); 1065 struct scsi_device *sdev = wqw->sdev; 1066 1067 kfree(wqw); 1068 spi_dv_device(sdev); 1069 spi_dv_pending(sdev->sdev_target) = 0; 1070 scsi_device_put(sdev); 1071 } 1072 1073 1074 /** 1075 * spi_schedule_dv_device - schedule domain validation to occur on the device 1076 * @sdev: The device to validate 1077 * 1078 * Identical to spi_dv_device() above, except that the DV will be 1079 * scheduled to occur in a workqueue later. All memory allocations 1080 * are atomic, so may be called from any context including those holding 1081 * SCSI locks. 1082 */ 1083 void 1084 spi_schedule_dv_device(struct scsi_device *sdev) 1085 { 1086 struct work_queue_wrapper *wqw = 1087 kmalloc(sizeof(struct work_queue_wrapper), GFP_ATOMIC); 1088 1089 if (unlikely(!wqw)) 1090 return; 1091 1092 if (unlikely(spi_dv_pending(sdev->sdev_target))) { 1093 kfree(wqw); 1094 return; 1095 } 1096 /* Set pending early (dv_device doesn't check it, only sets it) */ 1097 spi_dv_pending(sdev->sdev_target) = 1; 1098 if (unlikely(scsi_device_get(sdev))) { 1099 kfree(wqw); 1100 spi_dv_pending(sdev->sdev_target) = 0; 1101 return; 1102 } 1103 1104 INIT_WORK(&wqw->work, spi_dv_device_work_wrapper); 1105 wqw->sdev = sdev; 1106 1107 schedule_work(&wqw->work); 1108 } 1109 EXPORT_SYMBOL(spi_schedule_dv_device); 1110 1111 /** 1112 * spi_display_xfer_agreement - Print the current target transfer agreement 1113 * @starget: The target for which to display the agreement 1114 * 1115 * Each SPI port is required to maintain a transfer agreement for each 1116 * other port on the bus. This function prints a one-line summary of 1117 * the current agreement; more detailed information is available in sysfs. 1118 */ 1119 void spi_display_xfer_agreement(struct scsi_target *starget) 1120 { 1121 struct spi_transport_attrs *tp; 1122 tp = (struct spi_transport_attrs *)&starget->starget_data; 1123 1124 if (tp->offset > 0 && tp->period > 0) { 1125 unsigned int picosec, kb100; 1126 char *scsi = "FAST-?"; 1127 char tmp[8]; 1128 1129 if (tp->period <= SPI_STATIC_PPR) { 1130 picosec = ppr_to_ps[tp->period]; 1131 switch (tp->period) { 1132 case 7: scsi = "FAST-320"; break; 1133 case 8: scsi = "FAST-160"; break; 1134 case 9: scsi = "FAST-80"; break; 1135 case 10: 1136 case 11: scsi = "FAST-40"; break; 1137 case 12: scsi = "FAST-20"; break; 1138 } 1139 } else { 1140 picosec = tp->period * 4000; 1141 if (tp->period < 25) 1142 scsi = "FAST-20"; 1143 else if (tp->period < 50) 1144 scsi = "FAST-10"; 1145 else 1146 scsi = "FAST-5"; 1147 } 1148 1149 kb100 = (10000000 + picosec / 2) / picosec; 1150 if (tp->width) 1151 kb100 *= 2; 1152 sprint_frac(tmp, picosec, 1000); 1153 1154 dev_info(&starget->dev, 1155 "%s %sSCSI %d.%d MB/s %s%s%s%s%s%s%s%s (%s ns, offset %d)\n", 1156 scsi, tp->width ? "WIDE " : "", kb100/10, kb100 % 10, 1157 tp->dt ? "DT" : "ST", 1158 tp->iu ? " IU" : "", 1159 tp->qas ? " QAS" : "", 1160 tp->rd_strm ? " RDSTRM" : "", 1161 tp->rti ? " RTI" : "", 1162 tp->wr_flow ? " WRFLOW" : "", 1163 tp->pcomp_en ? " PCOMP" : "", 1164 tp->hold_mcs ? " HMCS" : "", 1165 tmp, tp->offset); 1166 } else { 1167 dev_info(&starget->dev, "%sasynchronous\n", 1168 tp->width ? "wide " : ""); 1169 } 1170 } 1171 EXPORT_SYMBOL(spi_display_xfer_agreement); 1172 1173 int spi_populate_width_msg(unsigned char *msg, int width) 1174 { 1175 msg[0] = EXTENDED_MESSAGE; 1176 msg[1] = 2; 1177 msg[2] = EXTENDED_WDTR; 1178 msg[3] = width; 1179 return 4; 1180 } 1181 EXPORT_SYMBOL_GPL(spi_populate_width_msg); 1182 1183 int spi_populate_sync_msg(unsigned char *msg, int period, int offset) 1184 { 1185 msg[0] = EXTENDED_MESSAGE; 1186 msg[1] = 3; 1187 msg[2] = EXTENDED_SDTR; 1188 msg[3] = period; 1189 msg[4] = offset; 1190 return 5; 1191 } 1192 EXPORT_SYMBOL_GPL(spi_populate_sync_msg); 1193 1194 int spi_populate_ppr_msg(unsigned char *msg, int period, int offset, 1195 int width, int options) 1196 { 1197 msg[0] = EXTENDED_MESSAGE; 1198 msg[1] = 6; 1199 msg[2] = EXTENDED_PPR; 1200 msg[3] = period; 1201 msg[4] = 0; 1202 msg[5] = offset; 1203 msg[6] = width; 1204 msg[7] = options; 1205 return 8; 1206 } 1207 EXPORT_SYMBOL_GPL(spi_populate_ppr_msg); 1208 1209 /** 1210 * spi_populate_tag_msg - place a tag message in a buffer 1211 * @msg: pointer to the area to place the tag 1212 * @cmd: pointer to the scsi command for the tag 1213 * 1214 * Notes: 1215 * designed to create the correct type of tag message for the 1216 * particular request. Returns the size of the tag message. 1217 * May return 0 if TCQ is disabled for this device. 1218 **/ 1219 int spi_populate_tag_msg(unsigned char *msg, struct scsi_cmnd *cmd) 1220 { 1221 if (cmd->flags & SCMD_TAGGED) { 1222 *msg++ = SIMPLE_QUEUE_TAG; 1223 *msg++ = cmd->request->tag; 1224 return 2; 1225 } 1226 1227 return 0; 1228 } 1229 EXPORT_SYMBOL_GPL(spi_populate_tag_msg); 1230 1231 #ifdef CONFIG_SCSI_CONSTANTS 1232 static const char * const one_byte_msgs[] = { 1233 /* 0x00 */ "Task Complete", NULL /* Extended Message */, "Save Pointers", 1234 /* 0x03 */ "Restore Pointers", "Disconnect", "Initiator Error", 1235 /* 0x06 */ "Abort Task Set", "Message Reject", "Nop", "Message Parity Error", 1236 /* 0x0a */ "Linked Command Complete", "Linked Command Complete w/flag", 1237 /* 0x0c */ "Target Reset", "Abort Task", "Clear Task Set", 1238 /* 0x0f */ "Initiate Recovery", "Release Recovery", 1239 /* 0x11 */ "Terminate Process", "Continue Task", "Target Transfer Disable", 1240 /* 0x14 */ NULL, NULL, "Clear ACA", "LUN Reset" 1241 }; 1242 1243 static const char * const two_byte_msgs[] = { 1244 /* 0x20 */ "Simple Queue Tag", "Head of Queue Tag", "Ordered Queue Tag", 1245 /* 0x23 */ "Ignore Wide Residue", "ACA" 1246 }; 1247 1248 static const char * const extended_msgs[] = { 1249 /* 0x00 */ "Modify Data Pointer", "Synchronous Data Transfer Request", 1250 /* 0x02 */ "SCSI-I Extended Identify", "Wide Data Transfer Request", 1251 /* 0x04 */ "Parallel Protocol Request", "Modify Bidirectional Data Pointer" 1252 }; 1253 1254 static void print_nego(const unsigned char *msg, int per, int off, int width) 1255 { 1256 if (per) { 1257 char buf[20]; 1258 period_to_str(buf, msg[per]); 1259 printk("period = %s ns ", buf); 1260 } 1261 1262 if (off) 1263 printk("offset = %d ", msg[off]); 1264 if (width) 1265 printk("width = %d ", 8 << msg[width]); 1266 } 1267 1268 static void print_ptr(const unsigned char *msg, int msb, const char *desc) 1269 { 1270 int ptr = (msg[msb] << 24) | (msg[msb+1] << 16) | (msg[msb+2] << 8) | 1271 msg[msb+3]; 1272 printk("%s = %d ", desc, ptr); 1273 } 1274 1275 int spi_print_msg(const unsigned char *msg) 1276 { 1277 int len = 1, i; 1278 if (msg[0] == EXTENDED_MESSAGE) { 1279 len = 2 + msg[1]; 1280 if (len == 2) 1281 len += 256; 1282 if (msg[2] < ARRAY_SIZE(extended_msgs)) 1283 printk ("%s ", extended_msgs[msg[2]]); 1284 else 1285 printk ("Extended Message, reserved code (0x%02x) ", 1286 (int) msg[2]); 1287 switch (msg[2]) { 1288 case EXTENDED_MODIFY_DATA_POINTER: 1289 print_ptr(msg, 3, "pointer"); 1290 break; 1291 case EXTENDED_SDTR: 1292 print_nego(msg, 3, 4, 0); 1293 break; 1294 case EXTENDED_WDTR: 1295 print_nego(msg, 0, 0, 3); 1296 break; 1297 case EXTENDED_PPR: 1298 print_nego(msg, 3, 5, 6); 1299 break; 1300 case EXTENDED_MODIFY_BIDI_DATA_PTR: 1301 print_ptr(msg, 3, "out"); 1302 print_ptr(msg, 7, "in"); 1303 break; 1304 default: 1305 for (i = 2; i < len; ++i) 1306 printk("%02x ", msg[i]); 1307 } 1308 /* Identify */ 1309 } else if (msg[0] & 0x80) { 1310 printk("Identify disconnect %sallowed %s %d ", 1311 (msg[0] & 0x40) ? "" : "not ", 1312 (msg[0] & 0x20) ? "target routine" : "lun", 1313 msg[0] & 0x7); 1314 /* Normal One byte */ 1315 } else if (msg[0] < 0x1f) { 1316 if (msg[0] < ARRAY_SIZE(one_byte_msgs) && one_byte_msgs[msg[0]]) 1317 printk("%s ", one_byte_msgs[msg[0]]); 1318 else 1319 printk("reserved (%02x) ", msg[0]); 1320 } else if (msg[0] == 0x55) { 1321 printk("QAS Request "); 1322 /* Two byte */ 1323 } else if (msg[0] <= 0x2f) { 1324 if ((msg[0] - 0x20) < ARRAY_SIZE(two_byte_msgs)) 1325 printk("%s %02x ", two_byte_msgs[msg[0] - 0x20], 1326 msg[1]); 1327 else 1328 printk("reserved two byte (%02x %02x) ", 1329 msg[0], msg[1]); 1330 len = 2; 1331 } else 1332 printk("reserved "); 1333 return len; 1334 } 1335 EXPORT_SYMBOL(spi_print_msg); 1336 1337 #else /* ifndef CONFIG_SCSI_CONSTANTS */ 1338 1339 int spi_print_msg(const unsigned char *msg) 1340 { 1341 int len = 1, i; 1342 1343 if (msg[0] == EXTENDED_MESSAGE) { 1344 len = 2 + msg[1]; 1345 if (len == 2) 1346 len += 256; 1347 for (i = 0; i < len; ++i) 1348 printk("%02x ", msg[i]); 1349 /* Identify */ 1350 } else if (msg[0] & 0x80) { 1351 printk("%02x ", msg[0]); 1352 /* Normal One byte */ 1353 } else if ((msg[0] < 0x1f) || (msg[0] == 0x55)) { 1354 printk("%02x ", msg[0]); 1355 /* Two byte */ 1356 } else if (msg[0] <= 0x2f) { 1357 printk("%02x %02x", msg[0], msg[1]); 1358 len = 2; 1359 } else 1360 printk("%02x ", msg[0]); 1361 return len; 1362 } 1363 EXPORT_SYMBOL(spi_print_msg); 1364 #endif /* ! CONFIG_SCSI_CONSTANTS */ 1365 1366 static int spi_device_match(struct attribute_container *cont, 1367 struct device *dev) 1368 { 1369 struct scsi_device *sdev; 1370 struct Scsi_Host *shost; 1371 struct spi_internal *i; 1372 1373 if (!scsi_is_sdev_device(dev)) 1374 return 0; 1375 1376 sdev = to_scsi_device(dev); 1377 shost = sdev->host; 1378 if (!shost->transportt || shost->transportt->host_attrs.ac.class 1379 != &spi_host_class.class) 1380 return 0; 1381 /* Note: this class has no device attributes, so it has 1382 * no per-HBA allocation and thus we don't need to distinguish 1383 * the attribute containers for the device */ 1384 i = to_spi_internal(shost->transportt); 1385 if (i->f->deny_binding && i->f->deny_binding(sdev->sdev_target)) 1386 return 0; 1387 return 1; 1388 } 1389 1390 static int spi_target_match(struct attribute_container *cont, 1391 struct device *dev) 1392 { 1393 struct Scsi_Host *shost; 1394 struct scsi_target *starget; 1395 struct spi_internal *i; 1396 1397 if (!scsi_is_target_device(dev)) 1398 return 0; 1399 1400 shost = dev_to_shost(dev->parent); 1401 if (!shost->transportt || shost->transportt->host_attrs.ac.class 1402 != &spi_host_class.class) 1403 return 0; 1404 1405 i = to_spi_internal(shost->transportt); 1406 starget = to_scsi_target(dev); 1407 1408 if (i->f->deny_binding && i->f->deny_binding(starget)) 1409 return 0; 1410 1411 return &i->t.target_attrs.ac == cont; 1412 } 1413 1414 static DECLARE_TRANSPORT_CLASS(spi_transport_class, 1415 "spi_transport", 1416 spi_setup_transport_attrs, 1417 NULL, 1418 spi_target_configure); 1419 1420 static DECLARE_ANON_TRANSPORT_CLASS(spi_device_class, 1421 spi_device_match, 1422 spi_device_configure); 1423 1424 static struct attribute *host_attributes[] = { 1425 &dev_attr_signalling.attr, 1426 &dev_attr_host_width.attr, 1427 &dev_attr_hba_id.attr, 1428 NULL 1429 }; 1430 1431 static struct attribute_group host_attribute_group = { 1432 .attrs = host_attributes, 1433 }; 1434 1435 static int spi_host_configure(struct transport_container *tc, 1436 struct device *dev, 1437 struct device *cdev) 1438 { 1439 struct kobject *kobj = &cdev->kobj; 1440 struct Scsi_Host *shost = transport_class_to_shost(cdev); 1441 struct spi_internal *si = to_spi_internal(shost->transportt); 1442 struct attribute *attr = &dev_attr_signalling.attr; 1443 int rc = 0; 1444 1445 if (si->f->set_signalling) 1446 rc = sysfs_chmod_file(kobj, attr, attr->mode | S_IWUSR); 1447 1448 return rc; 1449 } 1450 1451 /* returns true if we should be showing the variable. Also 1452 * overloads the return by setting 1<<1 if the attribute should 1453 * be writeable */ 1454 #define TARGET_ATTRIBUTE_HELPER(name) \ 1455 (si->f->show_##name ? S_IRUGO : 0) | \ 1456 (si->f->set_##name ? S_IWUSR : 0) 1457 1458 static umode_t target_attribute_is_visible(struct kobject *kobj, 1459 struct attribute *attr, int i) 1460 { 1461 struct device *cdev = container_of(kobj, struct device, kobj); 1462 struct scsi_target *starget = transport_class_to_starget(cdev); 1463 struct Scsi_Host *shost = transport_class_to_shost(cdev); 1464 struct spi_internal *si = to_spi_internal(shost->transportt); 1465 1466 if (attr == &dev_attr_period.attr && 1467 spi_support_sync(starget)) 1468 return TARGET_ATTRIBUTE_HELPER(period); 1469 else if (attr == &dev_attr_min_period.attr && 1470 spi_support_sync(starget)) 1471 return TARGET_ATTRIBUTE_HELPER(period); 1472 else if (attr == &dev_attr_offset.attr && 1473 spi_support_sync(starget)) 1474 return TARGET_ATTRIBUTE_HELPER(offset); 1475 else if (attr == &dev_attr_max_offset.attr && 1476 spi_support_sync(starget)) 1477 return TARGET_ATTRIBUTE_HELPER(offset); 1478 else if (attr == &dev_attr_width.attr && 1479 spi_support_wide(starget)) 1480 return TARGET_ATTRIBUTE_HELPER(width); 1481 else if (attr == &dev_attr_max_width.attr && 1482 spi_support_wide(starget)) 1483 return TARGET_ATTRIBUTE_HELPER(width); 1484 else if (attr == &dev_attr_iu.attr && 1485 spi_support_ius(starget)) 1486 return TARGET_ATTRIBUTE_HELPER(iu); 1487 else if (attr == &dev_attr_max_iu.attr && 1488 spi_support_ius(starget)) 1489 return TARGET_ATTRIBUTE_HELPER(iu); 1490 else if (attr == &dev_attr_dt.attr && 1491 spi_support_dt(starget)) 1492 return TARGET_ATTRIBUTE_HELPER(dt); 1493 else if (attr == &dev_attr_qas.attr && 1494 spi_support_qas(starget)) 1495 return TARGET_ATTRIBUTE_HELPER(qas); 1496 else if (attr == &dev_attr_max_qas.attr && 1497 spi_support_qas(starget)) 1498 return TARGET_ATTRIBUTE_HELPER(qas); 1499 else if (attr == &dev_attr_wr_flow.attr && 1500 spi_support_ius(starget)) 1501 return TARGET_ATTRIBUTE_HELPER(wr_flow); 1502 else if (attr == &dev_attr_rd_strm.attr && 1503 spi_support_ius(starget)) 1504 return TARGET_ATTRIBUTE_HELPER(rd_strm); 1505 else if (attr == &dev_attr_rti.attr && 1506 spi_support_ius(starget)) 1507 return TARGET_ATTRIBUTE_HELPER(rti); 1508 else if (attr == &dev_attr_pcomp_en.attr && 1509 spi_support_ius(starget)) 1510 return TARGET_ATTRIBUTE_HELPER(pcomp_en); 1511 else if (attr == &dev_attr_hold_mcs.attr && 1512 spi_support_ius(starget)) 1513 return TARGET_ATTRIBUTE_HELPER(hold_mcs); 1514 else if (attr == &dev_attr_revalidate.attr) 1515 return S_IWUSR; 1516 1517 return 0; 1518 } 1519 1520 static struct attribute *target_attributes[] = { 1521 &dev_attr_period.attr, 1522 &dev_attr_min_period.attr, 1523 &dev_attr_offset.attr, 1524 &dev_attr_max_offset.attr, 1525 &dev_attr_width.attr, 1526 &dev_attr_max_width.attr, 1527 &dev_attr_iu.attr, 1528 &dev_attr_max_iu.attr, 1529 &dev_attr_dt.attr, 1530 &dev_attr_qas.attr, 1531 &dev_attr_max_qas.attr, 1532 &dev_attr_wr_flow.attr, 1533 &dev_attr_rd_strm.attr, 1534 &dev_attr_rti.attr, 1535 &dev_attr_pcomp_en.attr, 1536 &dev_attr_hold_mcs.attr, 1537 &dev_attr_revalidate.attr, 1538 NULL 1539 }; 1540 1541 static struct attribute_group target_attribute_group = { 1542 .attrs = target_attributes, 1543 .is_visible = target_attribute_is_visible, 1544 }; 1545 1546 static int spi_target_configure(struct transport_container *tc, 1547 struct device *dev, 1548 struct device *cdev) 1549 { 1550 struct kobject *kobj = &cdev->kobj; 1551 1552 /* force an update based on parameters read from the device */ 1553 sysfs_update_group(kobj, &target_attribute_group); 1554 1555 return 0; 1556 } 1557 1558 struct scsi_transport_template * 1559 spi_attach_transport(struct spi_function_template *ft) 1560 { 1561 struct spi_internal *i = kzalloc(sizeof(struct spi_internal), 1562 GFP_KERNEL); 1563 1564 if (unlikely(!i)) 1565 return NULL; 1566 1567 i->t.target_attrs.ac.class = &spi_transport_class.class; 1568 i->t.target_attrs.ac.grp = &target_attribute_group; 1569 i->t.target_attrs.ac.match = spi_target_match; 1570 transport_container_register(&i->t.target_attrs); 1571 i->t.target_size = sizeof(struct spi_transport_attrs); 1572 i->t.host_attrs.ac.class = &spi_host_class.class; 1573 i->t.host_attrs.ac.grp = &host_attribute_group; 1574 i->t.host_attrs.ac.match = spi_host_match; 1575 transport_container_register(&i->t.host_attrs); 1576 i->t.host_size = sizeof(struct spi_host_attrs); 1577 i->f = ft; 1578 1579 return &i->t; 1580 } 1581 EXPORT_SYMBOL(spi_attach_transport); 1582 1583 void spi_release_transport(struct scsi_transport_template *t) 1584 { 1585 struct spi_internal *i = to_spi_internal(t); 1586 1587 transport_container_unregister(&i->t.target_attrs); 1588 transport_container_unregister(&i->t.host_attrs); 1589 1590 kfree(i); 1591 } 1592 EXPORT_SYMBOL(spi_release_transport); 1593 1594 static __init int spi_transport_init(void) 1595 { 1596 int error = scsi_dev_info_add_list(SCSI_DEVINFO_SPI, 1597 "SCSI Parallel Transport Class"); 1598 if (!error) { 1599 int i; 1600 1601 for (i = 0; spi_static_device_list[i].vendor; i++) 1602 scsi_dev_info_list_add_keyed(1, /* compatible */ 1603 spi_static_device_list[i].vendor, 1604 spi_static_device_list[i].model, 1605 NULL, 1606 spi_static_device_list[i].flags, 1607 SCSI_DEVINFO_SPI); 1608 } 1609 1610 error = transport_class_register(&spi_transport_class); 1611 if (error) 1612 return error; 1613 error = anon_transport_class_register(&spi_device_class); 1614 return transport_class_register(&spi_host_class); 1615 } 1616 1617 static void __exit spi_transport_exit(void) 1618 { 1619 transport_class_unregister(&spi_transport_class); 1620 anon_transport_class_unregister(&spi_device_class); 1621 transport_class_unregister(&spi_host_class); 1622 scsi_dev_info_remove_list(SCSI_DEVINFO_SPI); 1623 } 1624 1625 MODULE_AUTHOR("Martin Hicks"); 1626 MODULE_DESCRIPTION("SPI Transport Attributes"); 1627 MODULE_LICENSE("GPL"); 1628 1629 module_init(spi_transport_init); 1630 module_exit(spi_transport_exit); 1631