1 /* 2 * Serial Attached SCSI (SAS) Transport Layer initialization 3 * 4 * Copyright (C) 2005 Adaptec, Inc. All rights reserved. 5 * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com> 6 * 7 * This file is licensed under GPLv2. 8 * 9 * This program is free software; you can redistribute it and/or 10 * modify it under the terms of the GNU General Public License as 11 * published by the Free Software Foundation; either version 2 of the 12 * License, or (at your option) any later version. 13 * 14 * This program is distributed in the hope that it will be useful, but 15 * WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 * General Public License for more details. 18 * 19 * You should have received a copy of the GNU General Public License 20 * along with this program; if not, write to the Free Software 21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 22 * USA 23 * 24 */ 25 26 #include <linux/module.h> 27 #include <linux/slab.h> 28 #include <linux/init.h> 29 #include <linux/device.h> 30 #include <linux/spinlock.h> 31 #include <scsi/sas_ata.h> 32 #include <scsi/scsi_host.h> 33 #include <scsi/scsi_device.h> 34 #include <scsi/scsi_transport.h> 35 #include <scsi/scsi_transport_sas.h> 36 37 #include "sas_internal.h" 38 39 #include "../scsi_sas_internal.h" 40 41 static struct kmem_cache *sas_task_cache; 42 static struct kmem_cache *sas_event_cache; 43 44 struct sas_task *sas_alloc_task(gfp_t flags) 45 { 46 struct sas_task *task = kmem_cache_zalloc(sas_task_cache, flags); 47 48 if (task) { 49 spin_lock_init(&task->task_state_lock); 50 task->task_state_flags = SAS_TASK_STATE_PENDING; 51 } 52 53 return task; 54 } 55 EXPORT_SYMBOL_GPL(sas_alloc_task); 56 57 struct sas_task *sas_alloc_slow_task(gfp_t flags) 58 { 59 struct sas_task *task = sas_alloc_task(flags); 60 struct sas_task_slow *slow = kmalloc(sizeof(*slow), flags); 61 62 if (!task || !slow) { 63 if (task) 64 kmem_cache_free(sas_task_cache, task); 65 kfree(slow); 66 return NULL; 67 } 68 69 task->slow_task = slow; 70 slow->task = task; 71 timer_setup(&slow->timer, NULL, 0); 72 init_completion(&slow->completion); 73 74 return task; 75 } 76 EXPORT_SYMBOL_GPL(sas_alloc_slow_task); 77 78 void sas_free_task(struct sas_task *task) 79 { 80 if (task) { 81 kfree(task->slow_task); 82 kmem_cache_free(sas_task_cache, task); 83 } 84 } 85 EXPORT_SYMBOL_GPL(sas_free_task); 86 87 /*------------ SAS addr hash -----------*/ 88 void sas_hash_addr(u8 *hashed, const u8 *sas_addr) 89 { 90 const u32 poly = 0x00DB2777; 91 u32 r = 0; 92 int i; 93 94 for (i = 0; i < SAS_ADDR_SIZE; i++) { 95 int b; 96 97 for (b = (SAS_ADDR_SIZE - 1); b >= 0; b--) { 98 r <<= 1; 99 if ((1 << b) & sas_addr[i]) { 100 if (!(r & 0x01000000)) 101 r ^= poly; 102 } else if (r & 0x01000000) { 103 r ^= poly; 104 } 105 } 106 } 107 108 hashed[0] = (r >> 16) & 0xFF; 109 hashed[1] = (r >> 8) & 0xFF; 110 hashed[2] = r & 0xFF; 111 } 112 113 int sas_register_ha(struct sas_ha_struct *sas_ha) 114 { 115 char name[64]; 116 int error = 0; 117 118 mutex_init(&sas_ha->disco_mutex); 119 spin_lock_init(&sas_ha->phy_port_lock); 120 sas_hash_addr(sas_ha->hashed_sas_addr, sas_ha->sas_addr); 121 122 set_bit(SAS_HA_REGISTERED, &sas_ha->state); 123 spin_lock_init(&sas_ha->lock); 124 mutex_init(&sas_ha->drain_mutex); 125 init_waitqueue_head(&sas_ha->eh_wait_q); 126 INIT_LIST_HEAD(&sas_ha->defer_q); 127 INIT_LIST_HEAD(&sas_ha->eh_dev_q); 128 129 sas_ha->event_thres = SAS_PHY_SHUTDOWN_THRES; 130 131 error = sas_register_phys(sas_ha); 132 if (error) { 133 pr_notice("couldn't register sas phys:%d\n", error); 134 return error; 135 } 136 137 error = sas_register_ports(sas_ha); 138 if (error) { 139 pr_notice("couldn't register sas ports:%d\n", error); 140 goto Undo_phys; 141 } 142 143 error = sas_init_events(sas_ha); 144 if (error) { 145 pr_notice("couldn't start event thread:%d\n", error); 146 goto Undo_ports; 147 } 148 149 error = -ENOMEM; 150 snprintf(name, sizeof(name), "%s_event_q", dev_name(sas_ha->dev)); 151 sas_ha->event_q = create_singlethread_workqueue(name); 152 if (!sas_ha->event_q) 153 goto Undo_ports; 154 155 snprintf(name, sizeof(name), "%s_disco_q", dev_name(sas_ha->dev)); 156 sas_ha->disco_q = create_singlethread_workqueue(name); 157 if (!sas_ha->disco_q) 158 goto Undo_event_q; 159 160 INIT_LIST_HEAD(&sas_ha->eh_done_q); 161 INIT_LIST_HEAD(&sas_ha->eh_ata_q); 162 163 return 0; 164 165 Undo_event_q: 166 destroy_workqueue(sas_ha->event_q); 167 Undo_ports: 168 sas_unregister_ports(sas_ha); 169 Undo_phys: 170 171 return error; 172 } 173 174 static void sas_disable_events(struct sas_ha_struct *sas_ha) 175 { 176 /* Set the state to unregistered to avoid further unchained 177 * events to be queued, and flush any in-progress drainers 178 */ 179 mutex_lock(&sas_ha->drain_mutex); 180 spin_lock_irq(&sas_ha->lock); 181 clear_bit(SAS_HA_REGISTERED, &sas_ha->state); 182 spin_unlock_irq(&sas_ha->lock); 183 __sas_drain_work(sas_ha); 184 mutex_unlock(&sas_ha->drain_mutex); 185 } 186 187 int sas_unregister_ha(struct sas_ha_struct *sas_ha) 188 { 189 sas_disable_events(sas_ha); 190 sas_unregister_ports(sas_ha); 191 192 /* flush unregistration work */ 193 mutex_lock(&sas_ha->drain_mutex); 194 __sas_drain_work(sas_ha); 195 mutex_unlock(&sas_ha->drain_mutex); 196 197 destroy_workqueue(sas_ha->disco_q); 198 destroy_workqueue(sas_ha->event_q); 199 200 return 0; 201 } 202 203 static int sas_get_linkerrors(struct sas_phy *phy) 204 { 205 if (scsi_is_sas_phy_local(phy)) { 206 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); 207 struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(shost); 208 struct asd_sas_phy *asd_phy = sas_ha->sas_phy[phy->number]; 209 struct sas_internal *i = 210 to_sas_internal(sas_ha->core.shost->transportt); 211 212 return i->dft->lldd_control_phy(asd_phy, PHY_FUNC_GET_EVENTS, NULL); 213 } 214 215 return sas_smp_get_phy_events(phy); 216 } 217 218 int sas_try_ata_reset(struct asd_sas_phy *asd_phy) 219 { 220 struct domain_device *dev = NULL; 221 222 /* try to route user requested link resets through libata */ 223 if (asd_phy->port) 224 dev = asd_phy->port->port_dev; 225 226 /* validate that dev has been probed */ 227 if (dev) 228 dev = sas_find_dev_by_rphy(dev->rphy); 229 230 if (dev && dev_is_sata(dev)) { 231 sas_ata_schedule_reset(dev); 232 sas_ata_wait_eh(dev); 233 return 0; 234 } 235 236 return -ENODEV; 237 } 238 239 /* 240 * transport_sas_phy_reset - reset a phy and permit libata to manage the link 241 * 242 * phy reset request via sysfs in host workqueue context so we know we 243 * can block on eh and safely traverse the domain_device topology 244 */ 245 static int transport_sas_phy_reset(struct sas_phy *phy, int hard_reset) 246 { 247 enum phy_func reset_type; 248 249 if (hard_reset) 250 reset_type = PHY_FUNC_HARD_RESET; 251 else 252 reset_type = PHY_FUNC_LINK_RESET; 253 254 if (scsi_is_sas_phy_local(phy)) { 255 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); 256 struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(shost); 257 struct asd_sas_phy *asd_phy = sas_ha->sas_phy[phy->number]; 258 struct sas_internal *i = 259 to_sas_internal(sas_ha->core.shost->transportt); 260 261 if (!hard_reset && sas_try_ata_reset(asd_phy) == 0) 262 return 0; 263 return i->dft->lldd_control_phy(asd_phy, reset_type, NULL); 264 } else { 265 struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent); 266 struct domain_device *ddev = sas_find_dev_by_rphy(rphy); 267 struct domain_device *ata_dev = sas_ex_to_ata(ddev, phy->number); 268 269 if (ata_dev && !hard_reset) { 270 sas_ata_schedule_reset(ata_dev); 271 sas_ata_wait_eh(ata_dev); 272 return 0; 273 } else 274 return sas_smp_phy_control(ddev, phy->number, reset_type, NULL); 275 } 276 } 277 278 static int sas_phy_enable(struct sas_phy *phy, int enable) 279 { 280 int ret; 281 enum phy_func cmd; 282 283 if (enable) 284 cmd = PHY_FUNC_LINK_RESET; 285 else 286 cmd = PHY_FUNC_DISABLE; 287 288 if (scsi_is_sas_phy_local(phy)) { 289 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); 290 struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(shost); 291 struct asd_sas_phy *asd_phy = sas_ha->sas_phy[phy->number]; 292 struct sas_internal *i = 293 to_sas_internal(sas_ha->core.shost->transportt); 294 295 if (enable) 296 ret = transport_sas_phy_reset(phy, 0); 297 else 298 ret = i->dft->lldd_control_phy(asd_phy, cmd, NULL); 299 } else { 300 struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent); 301 struct domain_device *ddev = sas_find_dev_by_rphy(rphy); 302 303 if (enable) 304 ret = transport_sas_phy_reset(phy, 0); 305 else 306 ret = sas_smp_phy_control(ddev, phy->number, cmd, NULL); 307 } 308 return ret; 309 } 310 311 int sas_phy_reset(struct sas_phy *phy, int hard_reset) 312 { 313 int ret; 314 enum phy_func reset_type; 315 316 if (!phy->enabled) 317 return -ENODEV; 318 319 if (hard_reset) 320 reset_type = PHY_FUNC_HARD_RESET; 321 else 322 reset_type = PHY_FUNC_LINK_RESET; 323 324 if (scsi_is_sas_phy_local(phy)) { 325 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); 326 struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(shost); 327 struct asd_sas_phy *asd_phy = sas_ha->sas_phy[phy->number]; 328 struct sas_internal *i = 329 to_sas_internal(sas_ha->core.shost->transportt); 330 331 ret = i->dft->lldd_control_phy(asd_phy, reset_type, NULL); 332 } else { 333 struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent); 334 struct domain_device *ddev = sas_find_dev_by_rphy(rphy); 335 ret = sas_smp_phy_control(ddev, phy->number, reset_type, NULL); 336 } 337 return ret; 338 } 339 340 int sas_set_phy_speed(struct sas_phy *phy, 341 struct sas_phy_linkrates *rates) 342 { 343 int ret; 344 345 if ((rates->minimum_linkrate && 346 rates->minimum_linkrate > phy->maximum_linkrate) || 347 (rates->maximum_linkrate && 348 rates->maximum_linkrate < phy->minimum_linkrate)) 349 return -EINVAL; 350 351 if (rates->minimum_linkrate && 352 rates->minimum_linkrate < phy->minimum_linkrate_hw) 353 rates->minimum_linkrate = phy->minimum_linkrate_hw; 354 355 if (rates->maximum_linkrate && 356 rates->maximum_linkrate > phy->maximum_linkrate_hw) 357 rates->maximum_linkrate = phy->maximum_linkrate_hw; 358 359 if (scsi_is_sas_phy_local(phy)) { 360 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); 361 struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(shost); 362 struct asd_sas_phy *asd_phy = sas_ha->sas_phy[phy->number]; 363 struct sas_internal *i = 364 to_sas_internal(sas_ha->core.shost->transportt); 365 366 ret = i->dft->lldd_control_phy(asd_phy, PHY_FUNC_SET_LINK_RATE, 367 rates); 368 } else { 369 struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent); 370 struct domain_device *ddev = sas_find_dev_by_rphy(rphy); 371 ret = sas_smp_phy_control(ddev, phy->number, 372 PHY_FUNC_LINK_RESET, rates); 373 374 } 375 376 return ret; 377 } 378 379 void sas_prep_resume_ha(struct sas_ha_struct *ha) 380 { 381 int i; 382 383 set_bit(SAS_HA_REGISTERED, &ha->state); 384 385 /* clear out any stale link events/data from the suspension path */ 386 for (i = 0; i < ha->num_phys; i++) { 387 struct asd_sas_phy *phy = ha->sas_phy[i]; 388 389 memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE); 390 phy->frame_rcvd_size = 0; 391 } 392 } 393 EXPORT_SYMBOL(sas_prep_resume_ha); 394 395 static int phys_suspended(struct sas_ha_struct *ha) 396 { 397 int i, rc = 0; 398 399 for (i = 0; i < ha->num_phys; i++) { 400 struct asd_sas_phy *phy = ha->sas_phy[i]; 401 402 if (phy->suspended) 403 rc++; 404 } 405 406 return rc; 407 } 408 409 void sas_resume_ha(struct sas_ha_struct *ha) 410 { 411 const unsigned long tmo = msecs_to_jiffies(25000); 412 int i; 413 414 /* deform ports on phys that did not resume 415 * at this point we may be racing the phy coming back (as posted 416 * by the lldd). So we post the event and once we are in the 417 * libsas context check that the phy remains suspended before 418 * tearing it down. 419 */ 420 i = phys_suspended(ha); 421 if (i) 422 dev_info(ha->dev, "waiting up to 25 seconds for %d phy%s to resume\n", 423 i, i > 1 ? "s" : ""); 424 wait_event_timeout(ha->eh_wait_q, phys_suspended(ha) == 0, tmo); 425 for (i = 0; i < ha->num_phys; i++) { 426 struct asd_sas_phy *phy = ha->sas_phy[i]; 427 428 if (phy->suspended) { 429 dev_warn(&phy->phy->dev, "resume timeout\n"); 430 sas_notify_phy_event(phy, PHYE_RESUME_TIMEOUT); 431 } 432 } 433 434 /* all phys are back up or timed out, turn on i/o so we can 435 * flush out disks that did not return 436 */ 437 scsi_unblock_requests(ha->core.shost); 438 sas_drain_work(ha); 439 } 440 EXPORT_SYMBOL(sas_resume_ha); 441 442 void sas_suspend_ha(struct sas_ha_struct *ha) 443 { 444 int i; 445 446 sas_disable_events(ha); 447 scsi_block_requests(ha->core.shost); 448 for (i = 0; i < ha->num_phys; i++) { 449 struct asd_sas_port *port = ha->sas_port[i]; 450 451 sas_discover_event(port, DISCE_SUSPEND); 452 } 453 454 /* flush suspend events while unregistered */ 455 mutex_lock(&ha->drain_mutex); 456 __sas_drain_work(ha); 457 mutex_unlock(&ha->drain_mutex); 458 } 459 EXPORT_SYMBOL(sas_suspend_ha); 460 461 static void sas_phy_release(struct sas_phy *phy) 462 { 463 kfree(phy->hostdata); 464 phy->hostdata = NULL; 465 } 466 467 static void phy_reset_work(struct work_struct *work) 468 { 469 struct sas_phy_data *d = container_of(work, typeof(*d), reset_work.work); 470 471 d->reset_result = transport_sas_phy_reset(d->phy, d->hard_reset); 472 } 473 474 static void phy_enable_work(struct work_struct *work) 475 { 476 struct sas_phy_data *d = container_of(work, typeof(*d), enable_work.work); 477 478 d->enable_result = sas_phy_enable(d->phy, d->enable); 479 } 480 481 static int sas_phy_setup(struct sas_phy *phy) 482 { 483 struct sas_phy_data *d = kzalloc(sizeof(*d), GFP_KERNEL); 484 485 if (!d) 486 return -ENOMEM; 487 488 mutex_init(&d->event_lock); 489 INIT_SAS_WORK(&d->reset_work, phy_reset_work); 490 INIT_SAS_WORK(&d->enable_work, phy_enable_work); 491 d->phy = phy; 492 phy->hostdata = d; 493 494 return 0; 495 } 496 497 static int queue_phy_reset(struct sas_phy *phy, int hard_reset) 498 { 499 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); 500 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost); 501 struct sas_phy_data *d = phy->hostdata; 502 int rc; 503 504 if (!d) 505 return -ENOMEM; 506 507 /* libsas workqueue coordinates ata-eh reset with discovery */ 508 mutex_lock(&d->event_lock); 509 d->reset_result = 0; 510 d->hard_reset = hard_reset; 511 512 spin_lock_irq(&ha->lock); 513 sas_queue_work(ha, &d->reset_work); 514 spin_unlock_irq(&ha->lock); 515 516 rc = sas_drain_work(ha); 517 if (rc == 0) 518 rc = d->reset_result; 519 mutex_unlock(&d->event_lock); 520 521 return rc; 522 } 523 524 static int queue_phy_enable(struct sas_phy *phy, int enable) 525 { 526 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); 527 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost); 528 struct sas_phy_data *d = phy->hostdata; 529 int rc; 530 531 if (!d) 532 return -ENOMEM; 533 534 /* libsas workqueue coordinates ata-eh reset with discovery */ 535 mutex_lock(&d->event_lock); 536 d->enable_result = 0; 537 d->enable = enable; 538 539 spin_lock_irq(&ha->lock); 540 sas_queue_work(ha, &d->enable_work); 541 spin_unlock_irq(&ha->lock); 542 543 rc = sas_drain_work(ha); 544 if (rc == 0) 545 rc = d->enable_result; 546 mutex_unlock(&d->event_lock); 547 548 return rc; 549 } 550 551 static struct sas_function_template sft = { 552 .phy_enable = queue_phy_enable, 553 .phy_reset = queue_phy_reset, 554 .phy_setup = sas_phy_setup, 555 .phy_release = sas_phy_release, 556 .set_phy_speed = sas_set_phy_speed, 557 .get_linkerrors = sas_get_linkerrors, 558 .smp_handler = sas_smp_handler, 559 }; 560 561 static inline ssize_t phy_event_threshold_show(struct device *dev, 562 struct device_attribute *attr, char *buf) 563 { 564 struct Scsi_Host *shost = class_to_shost(dev); 565 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost); 566 567 return scnprintf(buf, PAGE_SIZE, "%u\n", sha->event_thres); 568 } 569 570 static inline ssize_t phy_event_threshold_store(struct device *dev, 571 struct device_attribute *attr, 572 const char *buf, size_t count) 573 { 574 struct Scsi_Host *shost = class_to_shost(dev); 575 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost); 576 577 sha->event_thres = simple_strtol(buf, NULL, 10); 578 579 /* threshold cannot be set too small */ 580 if (sha->event_thres < 32) 581 sha->event_thres = 32; 582 583 return count; 584 } 585 586 DEVICE_ATTR(phy_event_threshold, 587 S_IRUGO|S_IWUSR, 588 phy_event_threshold_show, 589 phy_event_threshold_store); 590 EXPORT_SYMBOL_GPL(dev_attr_phy_event_threshold); 591 592 struct scsi_transport_template * 593 sas_domain_attach_transport(struct sas_domain_function_template *dft) 594 { 595 struct scsi_transport_template *stt = sas_attach_transport(&sft); 596 struct sas_internal *i; 597 598 if (!stt) 599 return stt; 600 601 i = to_sas_internal(stt); 602 i->dft = dft; 603 stt->create_work_queue = 1; 604 stt->eh_strategy_handler = sas_scsi_recover_host; 605 606 return stt; 607 } 608 EXPORT_SYMBOL_GPL(sas_domain_attach_transport); 609 610 611 struct asd_sas_event *sas_alloc_event(struct asd_sas_phy *phy) 612 { 613 struct asd_sas_event *event; 614 gfp_t flags = in_interrupt() ? GFP_ATOMIC : GFP_KERNEL; 615 struct sas_ha_struct *sas_ha = phy->ha; 616 struct sas_internal *i = 617 to_sas_internal(sas_ha->core.shost->transportt); 618 619 event = kmem_cache_zalloc(sas_event_cache, flags); 620 if (!event) 621 return NULL; 622 623 atomic_inc(&phy->event_nr); 624 625 if (atomic_read(&phy->event_nr) > phy->ha->event_thres) { 626 if (i->dft->lldd_control_phy) { 627 if (cmpxchg(&phy->in_shutdown, 0, 1) == 0) { 628 pr_notice("The phy%d bursting events, shut it down.\n", 629 phy->id); 630 sas_notify_phy_event(phy, PHYE_SHUTDOWN); 631 } 632 } else { 633 /* Do not support PHY control, stop allocating events */ 634 WARN_ONCE(1, "PHY control not supported.\n"); 635 kmem_cache_free(sas_event_cache, event); 636 atomic_dec(&phy->event_nr); 637 event = NULL; 638 } 639 } 640 641 return event; 642 } 643 644 void sas_free_event(struct asd_sas_event *event) 645 { 646 struct asd_sas_phy *phy = event->phy; 647 648 kmem_cache_free(sas_event_cache, event); 649 atomic_dec(&phy->event_nr); 650 } 651 652 /* ---------- SAS Class register/unregister ---------- */ 653 654 static int __init sas_class_init(void) 655 { 656 sas_task_cache = KMEM_CACHE(sas_task, SLAB_HWCACHE_ALIGN); 657 if (!sas_task_cache) 658 goto out; 659 660 sas_event_cache = KMEM_CACHE(asd_sas_event, SLAB_HWCACHE_ALIGN); 661 if (!sas_event_cache) 662 goto free_task_kmem; 663 664 return 0; 665 free_task_kmem: 666 kmem_cache_destroy(sas_task_cache); 667 out: 668 return -ENOMEM; 669 } 670 671 static void __exit sas_class_exit(void) 672 { 673 kmem_cache_destroy(sas_task_cache); 674 kmem_cache_destroy(sas_event_cache); 675 } 676 677 MODULE_AUTHOR("Luben Tuikov <luben_tuikov@adaptec.com>"); 678 MODULE_DESCRIPTION("SAS Transport Layer"); 679 MODULE_LICENSE("GPL v2"); 680 681 module_init(sas_class_init); 682 module_exit(sas_class_exit); 683 684 EXPORT_SYMBOL_GPL(sas_register_ha); 685 EXPORT_SYMBOL_GPL(sas_unregister_ha); 686