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