scsi_sysfs.c revision a341cd0f6a0fde1f85fec9aa8f81f824ea4a3f92
1/* 2 * scsi_sysfs.c 3 * 4 * SCSI sysfs interface routines. 5 * 6 * Created to pull SCSI mid layer sysfs routines into one file. 7 */ 8 9#include <linux/module.h> 10#include <linux/init.h> 11#include <linux/blkdev.h> 12#include <linux/device.h> 13 14#include <scsi/scsi.h> 15#include <scsi/scsi_device.h> 16#include <scsi/scsi_host.h> 17#include <scsi/scsi_tcq.h> 18#include <scsi/scsi_transport.h> 19#include <scsi/scsi_driver.h> 20 21#include "scsi_priv.h" 22#include "scsi_logging.h" 23 24static const struct { 25 enum scsi_device_state value; 26 char *name; 27} sdev_states[] = { 28 { SDEV_CREATED, "created" }, 29 { SDEV_RUNNING, "running" }, 30 { SDEV_CANCEL, "cancel" }, 31 { SDEV_DEL, "deleted" }, 32 { SDEV_QUIESCE, "quiesce" }, 33 { SDEV_OFFLINE, "offline" }, 34 { SDEV_BLOCK, "blocked" }, 35}; 36 37const char *scsi_device_state_name(enum scsi_device_state state) 38{ 39 int i; 40 char *name = NULL; 41 42 for (i = 0; i < ARRAY_SIZE(sdev_states); i++) { 43 if (sdev_states[i].value == state) { 44 name = sdev_states[i].name; 45 break; 46 } 47 } 48 return name; 49} 50 51static const struct { 52 enum scsi_host_state value; 53 char *name; 54} shost_states[] = { 55 { SHOST_CREATED, "created" }, 56 { SHOST_RUNNING, "running" }, 57 { SHOST_CANCEL, "cancel" }, 58 { SHOST_DEL, "deleted" }, 59 { SHOST_RECOVERY, "recovery" }, 60 { SHOST_CANCEL_RECOVERY, "cancel/recovery" }, 61 { SHOST_DEL_RECOVERY, "deleted/recovery", }, 62}; 63const char *scsi_host_state_name(enum scsi_host_state state) 64{ 65 int i; 66 char *name = NULL; 67 68 for (i = 0; i < ARRAY_SIZE(shost_states); i++) { 69 if (shost_states[i].value == state) { 70 name = shost_states[i].name; 71 break; 72 } 73 } 74 return name; 75} 76 77static int check_set(unsigned int *val, char *src) 78{ 79 char *last; 80 81 if (strncmp(src, "-", 20) == 0) { 82 *val = SCAN_WILD_CARD; 83 } else { 84 /* 85 * Doesn't check for int overflow 86 */ 87 *val = simple_strtoul(src, &last, 0); 88 if (*last != '\0') 89 return 1; 90 } 91 return 0; 92} 93 94static int scsi_scan(struct Scsi_Host *shost, const char *str) 95{ 96 char s1[15], s2[15], s3[15], junk; 97 unsigned int channel, id, lun; 98 int res; 99 100 res = sscanf(str, "%10s %10s %10s %c", s1, s2, s3, &junk); 101 if (res != 3) 102 return -EINVAL; 103 if (check_set(&channel, s1)) 104 return -EINVAL; 105 if (check_set(&id, s2)) 106 return -EINVAL; 107 if (check_set(&lun, s3)) 108 return -EINVAL; 109 if (shost->transportt->user_scan) 110 res = shost->transportt->user_scan(shost, channel, id, lun); 111 else 112 res = scsi_scan_host_selected(shost, channel, id, lun, 1); 113 return res; 114} 115 116/* 117 * shost_show_function: macro to create an attr function that can be used to 118 * show a non-bit field. 119 */ 120#define shost_show_function(name, field, format_string) \ 121static ssize_t \ 122show_##name (struct class_device *class_dev, char *buf) \ 123{ \ 124 struct Scsi_Host *shost = class_to_shost(class_dev); \ 125 return snprintf (buf, 20, format_string, shost->field); \ 126} 127 128/* 129 * shost_rd_attr: macro to create a function and attribute variable for a 130 * read only field. 131 */ 132#define shost_rd_attr2(name, field, format_string) \ 133 shost_show_function(name, field, format_string) \ 134static CLASS_DEVICE_ATTR(name, S_IRUGO, show_##name, NULL); 135 136#define shost_rd_attr(field, format_string) \ 137shost_rd_attr2(field, field, format_string) 138 139/* 140 * Create the actual show/store functions and data structures. 141 */ 142 143static ssize_t store_scan(struct class_device *class_dev, const char *buf, 144 size_t count) 145{ 146 struct Scsi_Host *shost = class_to_shost(class_dev); 147 int res; 148 149 res = scsi_scan(shost, buf); 150 if (res == 0) 151 res = count; 152 return res; 153}; 154static CLASS_DEVICE_ATTR(scan, S_IWUSR, NULL, store_scan); 155 156static ssize_t 157store_shost_state(struct class_device *class_dev, const char *buf, size_t count) 158{ 159 int i; 160 struct Scsi_Host *shost = class_to_shost(class_dev); 161 enum scsi_host_state state = 0; 162 163 for (i = 0; i < ARRAY_SIZE(shost_states); i++) { 164 const int len = strlen(shost_states[i].name); 165 if (strncmp(shost_states[i].name, buf, len) == 0 && 166 buf[len] == '\n') { 167 state = shost_states[i].value; 168 break; 169 } 170 } 171 if (!state) 172 return -EINVAL; 173 174 if (scsi_host_set_state(shost, state)) 175 return -EINVAL; 176 return count; 177} 178 179static ssize_t 180show_shost_state(struct class_device *class_dev, char *buf) 181{ 182 struct Scsi_Host *shost = class_to_shost(class_dev); 183 const char *name = scsi_host_state_name(shost->shost_state); 184 185 if (!name) 186 return -EINVAL; 187 188 return snprintf(buf, 20, "%s\n", name); 189} 190 191static CLASS_DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_shost_state, store_shost_state); 192 193static ssize_t 194show_shost_mode(unsigned int mode, char *buf) 195{ 196 ssize_t len = 0; 197 198 if (mode & MODE_INITIATOR) 199 len = sprintf(buf, "%s", "Initiator"); 200 201 if (mode & MODE_TARGET) 202 len += sprintf(buf + len, "%s%s", len ? ", " : "", "Target"); 203 204 len += sprintf(buf + len, "\n"); 205 206 return len; 207} 208 209static ssize_t show_shost_supported_mode(struct class_device *class_dev, char *buf) 210{ 211 struct Scsi_Host *shost = class_to_shost(class_dev); 212 unsigned int supported_mode = shost->hostt->supported_mode; 213 214 if (supported_mode == MODE_UNKNOWN) 215 /* by default this should be initiator */ 216 supported_mode = MODE_INITIATOR; 217 218 return show_shost_mode(supported_mode, buf); 219} 220 221static CLASS_DEVICE_ATTR(supported_mode, S_IRUGO | S_IWUSR, show_shost_supported_mode, NULL); 222 223static ssize_t show_shost_active_mode(struct class_device *class_dev, char *buf) 224{ 225 struct Scsi_Host *shost = class_to_shost(class_dev); 226 227 if (shost->active_mode == MODE_UNKNOWN) 228 return snprintf(buf, 20, "unknown\n"); 229 else 230 return show_shost_mode(shost->active_mode, buf); 231} 232 233static CLASS_DEVICE_ATTR(active_mode, S_IRUGO | S_IWUSR, show_shost_active_mode, NULL); 234 235shost_rd_attr(unique_id, "%u\n"); 236shost_rd_attr(host_busy, "%hu\n"); 237shost_rd_attr(cmd_per_lun, "%hd\n"); 238shost_rd_attr(can_queue, "%hd\n"); 239shost_rd_attr(sg_tablesize, "%hu\n"); 240shost_rd_attr(unchecked_isa_dma, "%d\n"); 241shost_rd_attr2(proc_name, hostt->proc_name, "%s\n"); 242 243static struct class_device_attribute *scsi_sysfs_shost_attrs[] = { 244 &class_device_attr_unique_id, 245 &class_device_attr_host_busy, 246 &class_device_attr_cmd_per_lun, 247 &class_device_attr_can_queue, 248 &class_device_attr_sg_tablesize, 249 &class_device_attr_unchecked_isa_dma, 250 &class_device_attr_proc_name, 251 &class_device_attr_scan, 252 &class_device_attr_state, 253 &class_device_attr_supported_mode, 254 &class_device_attr_active_mode, 255 NULL 256}; 257 258static void scsi_device_cls_release(struct class_device *class_dev) 259{ 260 struct scsi_device *sdev; 261 262 sdev = class_to_sdev(class_dev); 263 put_device(&sdev->sdev_gendev); 264} 265 266static void scsi_device_dev_release_usercontext(struct work_struct *work) 267{ 268 struct scsi_device *sdev; 269 struct device *parent; 270 struct scsi_target *starget; 271 struct list_head *this, *tmp; 272 unsigned long flags; 273 274 sdev = container_of(work, struct scsi_device, ew.work); 275 276 parent = sdev->sdev_gendev.parent; 277 starget = to_scsi_target(parent); 278 279 spin_lock_irqsave(sdev->host->host_lock, flags); 280 starget->reap_ref++; 281 list_del(&sdev->siblings); 282 list_del(&sdev->same_target_siblings); 283 list_del(&sdev->starved_entry); 284 spin_unlock_irqrestore(sdev->host->host_lock, flags); 285 286 cancel_work_sync(&sdev->event_work); 287 288 list_for_each_safe(this, tmp, &sdev->event_list) { 289 struct scsi_event *evt; 290 291 evt = list_entry(this, struct scsi_event, node); 292 list_del(&evt->node); 293 kfree(evt); 294 } 295 296 if (sdev->request_queue) { 297 sdev->request_queue->queuedata = NULL; 298 /* user context needed to free queue */ 299 scsi_free_queue(sdev->request_queue); 300 /* temporary expedient, try to catch use of queue lock 301 * after free of sdev */ 302 sdev->request_queue = NULL; 303 } 304 305 scsi_target_reap(scsi_target(sdev)); 306 307 kfree(sdev->inquiry); 308 kfree(sdev); 309 310 if (parent) 311 put_device(parent); 312} 313 314static void scsi_device_dev_release(struct device *dev) 315{ 316 struct scsi_device *sdp = to_scsi_device(dev); 317 execute_in_process_context(scsi_device_dev_release_usercontext, 318 &sdp->ew); 319} 320 321static struct class sdev_class = { 322 .name = "scsi_device", 323 .release = scsi_device_cls_release, 324}; 325 326/* all probing is done in the individual ->probe routines */ 327static int scsi_bus_match(struct device *dev, struct device_driver *gendrv) 328{ 329 struct scsi_device *sdp = to_scsi_device(dev); 330 if (sdp->no_uld_attach) 331 return 0; 332 return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0; 333} 334 335static int scsi_bus_uevent(struct device *dev, struct kobj_uevent_env *env) 336{ 337 struct scsi_device *sdev = to_scsi_device(dev); 338 339 add_uevent_var(env, "MODALIAS=" SCSI_DEVICE_MODALIAS_FMT, sdev->type); 340 return 0; 341} 342 343static int scsi_bus_suspend(struct device * dev, pm_message_t state) 344{ 345 struct device_driver *drv = dev->driver; 346 struct scsi_device *sdev = to_scsi_device(dev); 347 int err; 348 349 err = scsi_device_quiesce(sdev); 350 if (err) 351 return err; 352 353 if (drv && drv->suspend) { 354 err = drv->suspend(dev, state); 355 if (err) 356 return err; 357 } 358 359 return 0; 360} 361 362static int scsi_bus_resume(struct device * dev) 363{ 364 struct device_driver *drv = dev->driver; 365 struct scsi_device *sdev = to_scsi_device(dev); 366 int err = 0; 367 368 if (drv && drv->resume) 369 err = drv->resume(dev); 370 371 scsi_device_resume(sdev); 372 373 return err; 374} 375 376struct bus_type scsi_bus_type = { 377 .name = "scsi", 378 .match = scsi_bus_match, 379 .uevent = scsi_bus_uevent, 380 .suspend = scsi_bus_suspend, 381 .resume = scsi_bus_resume, 382}; 383 384int scsi_sysfs_register(void) 385{ 386 int error; 387 388 error = bus_register(&scsi_bus_type); 389 if (!error) { 390 error = class_register(&sdev_class); 391 if (error) 392 bus_unregister(&scsi_bus_type); 393 } 394 395 return error; 396} 397 398void scsi_sysfs_unregister(void) 399{ 400 class_unregister(&sdev_class); 401 bus_unregister(&scsi_bus_type); 402} 403 404/* 405 * sdev_show_function: macro to create an attr function that can be used to 406 * show a non-bit field. 407 */ 408#define sdev_show_function(field, format_string) \ 409static ssize_t \ 410sdev_show_##field (struct device *dev, struct device_attribute *attr, char *buf) \ 411{ \ 412 struct scsi_device *sdev; \ 413 sdev = to_scsi_device(dev); \ 414 return snprintf (buf, 20, format_string, sdev->field); \ 415} \ 416 417/* 418 * sdev_rd_attr: macro to create a function and attribute variable for a 419 * read only field. 420 */ 421#define sdev_rd_attr(field, format_string) \ 422 sdev_show_function(field, format_string) \ 423static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL); 424 425 426/* 427 * sdev_rd_attr: create a function and attribute variable for a 428 * read/write field. 429 */ 430#define sdev_rw_attr(field, format_string) \ 431 sdev_show_function(field, format_string) \ 432 \ 433static ssize_t \ 434sdev_store_##field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \ 435{ \ 436 struct scsi_device *sdev; \ 437 sdev = to_scsi_device(dev); \ 438 snscanf (buf, 20, format_string, &sdev->field); \ 439 return count; \ 440} \ 441static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field); 442 443/* Currently we don't export bit fields, but we might in future, 444 * so leave this code in */ 445#if 0 446/* 447 * sdev_rd_attr: create a function and attribute variable for a 448 * read/write bit field. 449 */ 450#define sdev_rw_attr_bit(field) \ 451 sdev_show_function(field, "%d\n") \ 452 \ 453static ssize_t \ 454sdev_store_##field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \ 455{ \ 456 int ret; \ 457 struct scsi_device *sdev; \ 458 ret = scsi_sdev_check_buf_bit(buf); \ 459 if (ret >= 0) { \ 460 sdev = to_scsi_device(dev); \ 461 sdev->field = ret; \ 462 ret = count; \ 463 } \ 464 return ret; \ 465} \ 466static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field); 467 468/* 469 * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1", 470 * else return -EINVAL. 471 */ 472static int scsi_sdev_check_buf_bit(const char *buf) 473{ 474 if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) { 475 if (buf[0] == '1') 476 return 1; 477 else if (buf[0] == '0') 478 return 0; 479 else 480 return -EINVAL; 481 } else 482 return -EINVAL; 483} 484#endif 485/* 486 * Create the actual show/store functions and data structures. 487 */ 488sdev_rd_attr (device_blocked, "%d\n"); 489sdev_rd_attr (queue_depth, "%d\n"); 490sdev_rd_attr (type, "%d\n"); 491sdev_rd_attr (scsi_level, "%d\n"); 492sdev_rd_attr (vendor, "%.8s\n"); 493sdev_rd_attr (model, "%.16s\n"); 494sdev_rd_attr (rev, "%.4s\n"); 495 496static ssize_t 497sdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf) 498{ 499 struct scsi_device *sdev; 500 sdev = to_scsi_device(dev); 501 return snprintf (buf, 20, "%d\n", sdev->timeout / HZ); 502} 503 504static ssize_t 505sdev_store_timeout (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) 506{ 507 struct scsi_device *sdev; 508 int timeout; 509 sdev = to_scsi_device(dev); 510 sscanf (buf, "%d\n", &timeout); 511 sdev->timeout = timeout * HZ; 512 return count; 513} 514static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout); 515 516static ssize_t 517store_rescan_field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) 518{ 519 scsi_rescan_device(dev); 520 return count; 521} 522static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field); 523 524static void sdev_store_delete_callback(struct device *dev) 525{ 526 scsi_remove_device(to_scsi_device(dev)); 527} 528 529static ssize_t sdev_store_delete(struct device *dev, struct device_attribute *attr, const char *buf, 530 size_t count) 531{ 532 int rc; 533 534 /* An attribute cannot be unregistered by one of its own methods, 535 * so we have to use this roundabout approach. 536 */ 537 rc = device_schedule_callback(dev, sdev_store_delete_callback); 538 if (rc) 539 count = rc; 540 return count; 541}; 542static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete); 543 544static ssize_t 545store_state_field(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) 546{ 547 int i; 548 struct scsi_device *sdev = to_scsi_device(dev); 549 enum scsi_device_state state = 0; 550 551 for (i = 0; i < ARRAY_SIZE(sdev_states); i++) { 552 const int len = strlen(sdev_states[i].name); 553 if (strncmp(sdev_states[i].name, buf, len) == 0 && 554 buf[len] == '\n') { 555 state = sdev_states[i].value; 556 break; 557 } 558 } 559 if (!state) 560 return -EINVAL; 561 562 if (scsi_device_set_state(sdev, state)) 563 return -EINVAL; 564 return count; 565} 566 567static ssize_t 568show_state_field(struct device *dev, struct device_attribute *attr, char *buf) 569{ 570 struct scsi_device *sdev = to_scsi_device(dev); 571 const char *name = scsi_device_state_name(sdev->sdev_state); 572 573 if (!name) 574 return -EINVAL; 575 576 return snprintf(buf, 20, "%s\n", name); 577} 578 579static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field); 580 581static ssize_t 582show_queue_type_field(struct device *dev, struct device_attribute *attr, char *buf) 583{ 584 struct scsi_device *sdev = to_scsi_device(dev); 585 const char *name = "none"; 586 587 if (sdev->ordered_tags) 588 name = "ordered"; 589 else if (sdev->simple_tags) 590 name = "simple"; 591 592 return snprintf(buf, 20, "%s\n", name); 593} 594 595static DEVICE_ATTR(queue_type, S_IRUGO, show_queue_type_field, NULL); 596 597static ssize_t 598show_iostat_counterbits(struct device *dev, struct device_attribute *attr, char *buf) 599{ 600 return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8); 601} 602 603static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL); 604 605#define show_sdev_iostat(field) \ 606static ssize_t \ 607show_iostat_##field(struct device *dev, struct device_attribute *attr, char *buf) \ 608{ \ 609 struct scsi_device *sdev = to_scsi_device(dev); \ 610 unsigned long long count = atomic_read(&sdev->field); \ 611 return snprintf(buf, 20, "0x%llx\n", count); \ 612} \ 613static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL) 614 615show_sdev_iostat(iorequest_cnt); 616show_sdev_iostat(iodone_cnt); 617show_sdev_iostat(ioerr_cnt); 618 619static ssize_t 620sdev_show_modalias(struct device *dev, struct device_attribute *attr, char *buf) 621{ 622 struct scsi_device *sdev; 623 sdev = to_scsi_device(dev); 624 return snprintf (buf, 20, SCSI_DEVICE_MODALIAS_FMT "\n", sdev->type); 625} 626static DEVICE_ATTR(modalias, S_IRUGO, sdev_show_modalias, NULL); 627 628#define DECLARE_EVT_SHOW(name, Cap_name) \ 629static ssize_t \ 630sdev_show_evt_##name(struct device *dev, struct device_attribute *attr, \ 631 char *buf) \ 632{ \ 633 struct scsi_device *sdev = to_scsi_device(dev); \ 634 int val = test_bit(SDEV_EVT_##Cap_name, sdev->supported_events);\ 635 return snprintf(buf, 20, "%d\n", val); \ 636} 637 638#define DECLARE_EVT_STORE(name, Cap_name) \ 639static ssize_t \ 640sdev_store_evt_##name(struct device *dev, struct device_attribute *attr, \ 641 const char *buf, size_t count) \ 642{ \ 643 struct scsi_device *sdev = to_scsi_device(dev); \ 644 int val = simple_strtoul(buf, NULL, 0); \ 645 if (val == 0) \ 646 clear_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \ 647 else if (val == 1) \ 648 set_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \ 649 else \ 650 return -EINVAL; \ 651 return count; \ 652} 653 654#define DECLARE_EVT(name, Cap_name) \ 655 DECLARE_EVT_SHOW(name, Cap_name) \ 656 DECLARE_EVT_STORE(name, Cap_name) \ 657 static DEVICE_ATTR(evt_##name, S_IRUGO, sdev_show_evt_##name, \ 658 sdev_store_evt_##name); 659#define REF_EVT(name) &dev_attr_evt_##name.attr 660 661DECLARE_EVT(media_change, MEDIA_CHANGE) 662 663/* Default template for device attributes. May NOT be modified */ 664static struct attribute *scsi_sdev_attrs[] = { 665 &dev_attr_device_blocked.attr, 666 &dev_attr_type.attr, 667 &dev_attr_scsi_level.attr, 668 &dev_attr_vendor.attr, 669 &dev_attr_model.attr, 670 &dev_attr_rev.attr, 671 &dev_attr_rescan.attr, 672 &dev_attr_delete.attr, 673 &dev_attr_state.attr, 674 &dev_attr_timeout.attr, 675 &dev_attr_iocounterbits.attr, 676 &dev_attr_iorequest_cnt.attr, 677 &dev_attr_iodone_cnt.attr, 678 &dev_attr_ioerr_cnt.attr, 679 &dev_attr_modalias.attr, 680 REF_EVT(media_change), 681 NULL 682}; 683 684static struct attribute_group scsi_sdev_attr_group = { 685 .attrs = scsi_sdev_attrs, 686}; 687 688static struct attribute_group *scsi_sdev_attr_groups[] = { 689 &scsi_sdev_attr_group, 690 NULL 691}; 692 693static ssize_t sdev_store_queue_depth_rw(struct device *dev, struct device_attribute *attr, const char *buf, 694 size_t count) 695{ 696 int depth, retval; 697 struct scsi_device *sdev = to_scsi_device(dev); 698 struct scsi_host_template *sht = sdev->host->hostt; 699 700 if (!sht->change_queue_depth) 701 return -EINVAL; 702 703 depth = simple_strtoul(buf, NULL, 0); 704 705 if (depth < 1) 706 return -EINVAL; 707 708 retval = sht->change_queue_depth(sdev, depth); 709 if (retval < 0) 710 return retval; 711 712 return count; 713} 714 715static struct device_attribute sdev_attr_queue_depth_rw = 716 __ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth, 717 sdev_store_queue_depth_rw); 718 719static ssize_t sdev_store_queue_type_rw(struct device *dev, struct device_attribute *attr, const char *buf, 720 size_t count) 721{ 722 struct scsi_device *sdev = to_scsi_device(dev); 723 struct scsi_host_template *sht = sdev->host->hostt; 724 int tag_type = 0, retval; 725 int prev_tag_type = scsi_get_tag_type(sdev); 726 727 if (!sdev->tagged_supported || !sht->change_queue_type) 728 return -EINVAL; 729 730 if (strncmp(buf, "ordered", 7) == 0) 731 tag_type = MSG_ORDERED_TAG; 732 else if (strncmp(buf, "simple", 6) == 0) 733 tag_type = MSG_SIMPLE_TAG; 734 else if (strncmp(buf, "none", 4) != 0) 735 return -EINVAL; 736 737 if (tag_type == prev_tag_type) 738 return count; 739 740 retval = sht->change_queue_type(sdev, tag_type); 741 if (retval < 0) 742 return retval; 743 744 return count; 745} 746 747static struct device_attribute sdev_attr_queue_type_rw = 748 __ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field, 749 sdev_store_queue_type_rw); 750 751/** 752 * scsi_sysfs_add_sdev - add scsi device to sysfs 753 * @sdev: scsi_device to add 754 * 755 * Return value: 756 * 0 on Success / non-zero on Failure 757 **/ 758int scsi_sysfs_add_sdev(struct scsi_device *sdev) 759{ 760 int error, i; 761 struct request_queue *rq = sdev->request_queue; 762 763 if ((error = scsi_device_set_state(sdev, SDEV_RUNNING)) != 0) 764 return error; 765 766 error = device_add(&sdev->sdev_gendev); 767 if (error) { 768 put_device(sdev->sdev_gendev.parent); 769 printk(KERN_INFO "error 1\n"); 770 return error; 771 } 772 error = class_device_add(&sdev->sdev_classdev); 773 if (error) { 774 printk(KERN_INFO "error 2\n"); 775 goto clean_device; 776 } 777 778 /* take a reference for the sdev_classdev; this is 779 * released by the sdev_class .release */ 780 get_device(&sdev->sdev_gendev); 781 782 /* create queue files, which may be writable, depending on the host */ 783 if (sdev->host->hostt->change_queue_depth) 784 error = device_create_file(&sdev->sdev_gendev, &sdev_attr_queue_depth_rw); 785 else 786 error = device_create_file(&sdev->sdev_gendev, &dev_attr_queue_depth); 787 if (error) { 788 __scsi_remove_device(sdev); 789 goto out; 790 } 791 if (sdev->host->hostt->change_queue_type) 792 error = device_create_file(&sdev->sdev_gendev, &sdev_attr_queue_type_rw); 793 else 794 error = device_create_file(&sdev->sdev_gendev, &dev_attr_queue_type); 795 if (error) { 796 __scsi_remove_device(sdev); 797 goto out; 798 } 799 800 error = bsg_register_queue(rq, &sdev->sdev_gendev, NULL); 801 802 if (error) 803 sdev_printk(KERN_INFO, sdev, 804 "Failed to register bsg queue, errno=%d\n", error); 805 806 /* we're treating error on bsg register as non-fatal, so pretend 807 * nothing went wrong */ 808 error = 0; 809 810 /* add additional host specific attributes */ 811 if (sdev->host->hostt->sdev_attrs) { 812 for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) { 813 error = device_create_file(&sdev->sdev_gendev, 814 sdev->host->hostt->sdev_attrs[i]); 815 if (error) { 816 __scsi_remove_device(sdev); 817 goto out; 818 } 819 } 820 } 821 822 transport_add_device(&sdev->sdev_gendev); 823 out: 824 return error; 825 826 clean_device: 827 scsi_device_set_state(sdev, SDEV_CANCEL); 828 829 device_del(&sdev->sdev_gendev); 830 transport_destroy_device(&sdev->sdev_gendev); 831 put_device(&sdev->sdev_gendev); 832 833 return error; 834} 835 836void __scsi_remove_device(struct scsi_device *sdev) 837{ 838 struct device *dev = &sdev->sdev_gendev; 839 840 if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0) 841 return; 842 843 bsg_unregister_queue(sdev->request_queue); 844 class_device_unregister(&sdev->sdev_classdev); 845 transport_remove_device(dev); 846 device_del(dev); 847 scsi_device_set_state(sdev, SDEV_DEL); 848 if (sdev->host->hostt->slave_destroy) 849 sdev->host->hostt->slave_destroy(sdev); 850 transport_destroy_device(dev); 851 put_device(dev); 852} 853 854/** 855 * scsi_remove_device - unregister a device from the scsi bus 856 * @sdev: scsi_device to unregister 857 **/ 858void scsi_remove_device(struct scsi_device *sdev) 859{ 860 struct Scsi_Host *shost = sdev->host; 861 862 mutex_lock(&shost->scan_mutex); 863 __scsi_remove_device(sdev); 864 mutex_unlock(&shost->scan_mutex); 865} 866EXPORT_SYMBOL(scsi_remove_device); 867 868static void __scsi_remove_target(struct scsi_target *starget) 869{ 870 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); 871 unsigned long flags; 872 struct scsi_device *sdev; 873 874 spin_lock_irqsave(shost->host_lock, flags); 875 starget->reap_ref++; 876 restart: 877 list_for_each_entry(sdev, &shost->__devices, siblings) { 878 if (sdev->channel != starget->channel || 879 sdev->id != starget->id || 880 sdev->sdev_state == SDEV_DEL) 881 continue; 882 spin_unlock_irqrestore(shost->host_lock, flags); 883 scsi_remove_device(sdev); 884 spin_lock_irqsave(shost->host_lock, flags); 885 goto restart; 886 } 887 spin_unlock_irqrestore(shost->host_lock, flags); 888 scsi_target_reap(starget); 889} 890 891static int __remove_child (struct device * dev, void * data) 892{ 893 if (scsi_is_target_device(dev)) 894 __scsi_remove_target(to_scsi_target(dev)); 895 return 0; 896} 897 898/** 899 * scsi_remove_target - try to remove a target and all its devices 900 * @dev: generic starget or parent of generic stargets to be removed 901 * 902 * Note: This is slightly racy. It is possible that if the user 903 * requests the addition of another device then the target won't be 904 * removed. 905 */ 906void scsi_remove_target(struct device *dev) 907{ 908 struct device *rdev; 909 910 if (scsi_is_target_device(dev)) { 911 __scsi_remove_target(to_scsi_target(dev)); 912 return; 913 } 914 915 rdev = get_device(dev); 916 device_for_each_child(dev, NULL, __remove_child); 917 put_device(rdev); 918} 919EXPORT_SYMBOL(scsi_remove_target); 920 921int scsi_register_driver(struct device_driver *drv) 922{ 923 drv->bus = &scsi_bus_type; 924 925 return driver_register(drv); 926} 927EXPORT_SYMBOL(scsi_register_driver); 928 929int scsi_register_interface(struct class_interface *intf) 930{ 931 intf->class = &sdev_class; 932 933 return class_interface_register(intf); 934} 935EXPORT_SYMBOL(scsi_register_interface); 936 937 938static struct class_device_attribute *class_attr_overridden( 939 struct class_device_attribute **attrs, 940 struct class_device_attribute *attr) 941{ 942 int i; 943 944 if (!attrs) 945 return NULL; 946 for (i = 0; attrs[i]; i++) 947 if (!strcmp(attrs[i]->attr.name, attr->attr.name)) 948 return attrs[i]; 949 return NULL; 950} 951 952static int class_attr_add(struct class_device *classdev, 953 struct class_device_attribute *attr) 954{ 955 struct class_device_attribute *base_attr; 956 957 /* 958 * Spare the caller from having to copy things it's not interested in. 959 */ 960 base_attr = class_attr_overridden(scsi_sysfs_shost_attrs, attr); 961 if (base_attr) { 962 /* extend permissions */ 963 attr->attr.mode |= base_attr->attr.mode; 964 965 /* override null show/store with default */ 966 if (!attr->show) 967 attr->show = base_attr->show; 968 if (!attr->store) 969 attr->store = base_attr->store; 970 } 971 972 return class_device_create_file(classdev, attr); 973} 974 975/** 976 * scsi_sysfs_add_host - add scsi host to subsystem 977 * @shost: scsi host struct to add to subsystem 978 * @dev: parent struct device pointer 979 **/ 980int scsi_sysfs_add_host(struct Scsi_Host *shost) 981{ 982 int error, i; 983 984 if (shost->hostt->shost_attrs) { 985 for (i = 0; shost->hostt->shost_attrs[i]; i++) { 986 error = class_attr_add(&shost->shost_classdev, 987 shost->hostt->shost_attrs[i]); 988 if (error) 989 return error; 990 } 991 } 992 993 for (i = 0; scsi_sysfs_shost_attrs[i]; i++) { 994 if (!class_attr_overridden(shost->hostt->shost_attrs, 995 scsi_sysfs_shost_attrs[i])) { 996 error = class_device_create_file(&shost->shost_classdev, 997 scsi_sysfs_shost_attrs[i]); 998 if (error) 999 return error; 1000 } 1001 } 1002 1003 transport_register_device(&shost->shost_gendev); 1004 return 0; 1005} 1006 1007static struct device_type scsi_dev_type = { 1008 .name = "scsi_device", 1009 .release = scsi_device_dev_release, 1010 .groups = scsi_sdev_attr_groups, 1011}; 1012 1013void scsi_sysfs_device_initialize(struct scsi_device *sdev) 1014{ 1015 unsigned long flags; 1016 struct Scsi_Host *shost = sdev->host; 1017 struct scsi_target *starget = sdev->sdev_target; 1018 1019 device_initialize(&sdev->sdev_gendev); 1020 sdev->sdev_gendev.bus = &scsi_bus_type; 1021 sdev->sdev_gendev.type = &scsi_dev_type; 1022 sprintf(sdev->sdev_gendev.bus_id,"%d:%d:%d:%d", 1023 sdev->host->host_no, sdev->channel, sdev->id, 1024 sdev->lun); 1025 1026 class_device_initialize(&sdev->sdev_classdev); 1027 sdev->sdev_classdev.dev = &sdev->sdev_gendev; 1028 sdev->sdev_classdev.class = &sdev_class; 1029 snprintf(sdev->sdev_classdev.class_id, BUS_ID_SIZE, 1030 "%d:%d:%d:%d", sdev->host->host_no, 1031 sdev->channel, sdev->id, sdev->lun); 1032 sdev->scsi_level = starget->scsi_level; 1033 transport_setup_device(&sdev->sdev_gendev); 1034 spin_lock_irqsave(shost->host_lock, flags); 1035 list_add_tail(&sdev->same_target_siblings, &starget->devices); 1036 list_add_tail(&sdev->siblings, &shost->__devices); 1037 spin_unlock_irqrestore(shost->host_lock, flags); 1038} 1039 1040int scsi_is_sdev_device(const struct device *dev) 1041{ 1042 return dev->type == &scsi_dev_type; 1043} 1044EXPORT_SYMBOL(scsi_is_sdev_device); 1045 1046/* A blank transport template that is used in drivers that don't 1047 * yet implement Transport Attributes */ 1048struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, }; 1049