1 // SPDX-License-Identifier: GPL-2.0-only
3 * Serial Attached SCSI (SAS) Transport Layer initialization
5 * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
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>
20 #include "sas_internal.h"
22 #include "scsi_sas_internal.h"
24 static struct kmem_cache *sas_task_cache;
25 static struct kmem_cache *sas_event_cache;
27 struct sas_task *sas_alloc_task(gfp_t flags)
29 struct sas_task *task = kmem_cache_zalloc(sas_task_cache, flags);
32 spin_lock_init(&task->task_state_lock);
33 task->task_state_flags = SAS_TASK_STATE_PENDING;
38 EXPORT_SYMBOL_GPL(sas_alloc_task);
40 struct sas_task *sas_alloc_slow_task(gfp_t flags)
42 struct sas_task *task = sas_alloc_task(flags);
43 struct sas_task_slow *slow = kmalloc(sizeof(*slow), flags);
47 kmem_cache_free(sas_task_cache, task);
52 task->slow_task = slow;
54 timer_setup(&slow->timer, NULL, 0);
55 init_completion(&slow->completion);
59 EXPORT_SYMBOL_GPL(sas_alloc_slow_task);
61 void sas_free_task(struct sas_task *task)
64 kfree(task->slow_task);
65 kmem_cache_free(sas_task_cache, task);
68 EXPORT_SYMBOL_GPL(sas_free_task);
70 /*------------ SAS addr hash -----------*/
71 void sas_hash_addr(u8 *hashed, const u8 *sas_addr)
73 const u32 poly = 0x00DB2777;
77 for (i = 0; i < SAS_ADDR_SIZE; i++) {
80 for (b = (SAS_ADDR_SIZE - 1); b >= 0; b--) {
82 if ((1 << b) & sas_addr[i]) {
83 if (!(r & 0x01000000))
85 } else if (r & 0x01000000) {
91 hashed[0] = (r >> 16) & 0xFF;
92 hashed[1] = (r >> 8) & 0xFF;
96 int sas_register_ha(struct sas_ha_struct *sas_ha)
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);
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);
112 sas_ha->event_thres = SAS_PHY_SHUTDOWN_THRES;
114 error = sas_register_phys(sas_ha);
116 pr_notice("couldn't register sas phys:%d\n", error);
120 error = sas_register_ports(sas_ha);
122 pr_notice("couldn't register sas ports:%d\n", error);
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)
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)
137 INIT_LIST_HEAD(&sas_ha->eh_done_q);
138 INIT_LIST_HEAD(&sas_ha->eh_ata_q);
143 destroy_workqueue(sas_ha->event_q);
145 sas_unregister_ports(sas_ha);
150 EXPORT_SYMBOL_GPL(sas_register_ha);
152 static void sas_disable_events(struct sas_ha_struct *sas_ha)
154 /* Set the state to unregistered to avoid further unchained
155 * events to be queued, and flush any in-progress drainers
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);
165 int sas_unregister_ha(struct sas_ha_struct *sas_ha)
167 sas_disable_events(sas_ha);
168 sas_unregister_ports(sas_ha);
170 /* flush unregistration work */
171 mutex_lock(&sas_ha->drain_mutex);
172 __sas_drain_work(sas_ha);
173 mutex_unlock(&sas_ha->drain_mutex);
175 destroy_workqueue(sas_ha->disco_q);
176 destroy_workqueue(sas_ha->event_q);
180 EXPORT_SYMBOL_GPL(sas_unregister_ha);
182 static int sas_get_linkerrors(struct sas_phy *phy)
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);
191 return i->dft->lldd_control_phy(asd_phy, PHY_FUNC_GET_EVENTS, NULL);
194 return sas_smp_get_phy_events(phy);
197 int sas_try_ata_reset(struct asd_sas_phy *asd_phy)
199 struct domain_device *dev = NULL;
201 /* try to route user requested link resets through libata */
203 dev = asd_phy->port->port_dev;
205 /* validate that dev has been probed */
207 dev = sas_find_dev_by_rphy(dev->rphy);
209 if (dev && dev_is_sata(dev)) {
210 sas_ata_schedule_reset(dev);
211 sas_ata_wait_eh(dev);
219 * transport_sas_phy_reset - reset a phy and permit libata to manage the link
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
224 static int transport_sas_phy_reset(struct sas_phy *phy, int hard_reset)
226 enum phy_func reset_type;
229 reset_type = PHY_FUNC_HARD_RESET;
231 reset_type = PHY_FUNC_LINK_RESET;
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);
240 if (!hard_reset && sas_try_ata_reset(asd_phy) == 0)
242 return i->dft->lldd_control_phy(asd_phy, reset_type, NULL);
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);
248 if (ata_dev && !hard_reset) {
249 sas_ata_schedule_reset(ata_dev);
250 sas_ata_wait_eh(ata_dev);
253 return sas_smp_phy_control(ddev, phy->number, reset_type, NULL);
257 int sas_phy_enable(struct sas_phy *phy, int enable)
263 cmd = PHY_FUNC_LINK_RESET;
265 cmd = PHY_FUNC_DISABLE;
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);
275 ret = transport_sas_phy_reset(phy, 0);
277 ret = i->dft->lldd_control_phy(asd_phy, cmd, NULL);
279 struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
280 struct domain_device *ddev = sas_find_dev_by_rphy(rphy);
283 ret = transport_sas_phy_reset(phy, 0);
285 ret = sas_smp_phy_control(ddev, phy->number, cmd, NULL);
289 EXPORT_SYMBOL_GPL(sas_phy_enable);
291 int sas_phy_reset(struct sas_phy *phy, int hard_reset)
294 enum phy_func reset_type;
300 reset_type = PHY_FUNC_HARD_RESET;
302 reset_type = PHY_FUNC_LINK_RESET;
304 if (scsi_is_sas_phy_local(phy)) {
305 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
306 struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(shost);
307 struct asd_sas_phy *asd_phy = sas_ha->sas_phy[phy->number];
308 struct sas_internal *i =
309 to_sas_internal(sas_ha->core.shost->transportt);
311 ret = i->dft->lldd_control_phy(asd_phy, reset_type, NULL);
313 struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
314 struct domain_device *ddev = sas_find_dev_by_rphy(rphy);
315 ret = sas_smp_phy_control(ddev, phy->number, reset_type, NULL);
319 EXPORT_SYMBOL_GPL(sas_phy_reset);
321 int sas_set_phy_speed(struct sas_phy *phy,
322 struct sas_phy_linkrates *rates)
326 if ((rates->minimum_linkrate &&
327 rates->minimum_linkrate > phy->maximum_linkrate) ||
328 (rates->maximum_linkrate &&
329 rates->maximum_linkrate < phy->minimum_linkrate))
332 if (rates->minimum_linkrate &&
333 rates->minimum_linkrate < phy->minimum_linkrate_hw)
334 rates->minimum_linkrate = phy->minimum_linkrate_hw;
336 if (rates->maximum_linkrate &&
337 rates->maximum_linkrate > phy->maximum_linkrate_hw)
338 rates->maximum_linkrate = phy->maximum_linkrate_hw;
340 if (scsi_is_sas_phy_local(phy)) {
341 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
342 struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(shost);
343 struct asd_sas_phy *asd_phy = sas_ha->sas_phy[phy->number];
344 struct sas_internal *i =
345 to_sas_internal(sas_ha->core.shost->transportt);
347 ret = i->dft->lldd_control_phy(asd_phy, PHY_FUNC_SET_LINK_RATE,
350 struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
351 struct domain_device *ddev = sas_find_dev_by_rphy(rphy);
352 ret = sas_smp_phy_control(ddev, phy->number,
353 PHY_FUNC_LINK_RESET, rates);
360 void sas_prep_resume_ha(struct sas_ha_struct *ha)
364 set_bit(SAS_HA_REGISTERED, &ha->state);
365 set_bit(SAS_HA_RESUMING, &ha->state);
367 /* clear out any stale link events/data from the suspension path */
368 for (i = 0; i < ha->num_phys; i++) {
369 struct asd_sas_phy *phy = ha->sas_phy[i];
371 memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
372 phy->frame_rcvd_size = 0;
375 EXPORT_SYMBOL(sas_prep_resume_ha);
377 static int phys_suspended(struct sas_ha_struct *ha)
381 for (i = 0; i < ha->num_phys; i++) {
382 struct asd_sas_phy *phy = ha->sas_phy[i];
391 static void sas_resume_insert_broadcast_ha(struct sas_ha_struct *ha)
395 for (i = 0; i < ha->num_phys; i++) {
396 struct asd_sas_port *port = ha->sas_port[i];
397 struct domain_device *dev = port->port_dev;
399 if (dev && dev_is_expander(dev->dev_type)) {
400 struct asd_sas_phy *first_phy;
402 spin_lock(&port->phy_list_lock);
403 first_phy = list_first_entry_or_null(
404 &port->phy_list, struct asd_sas_phy,
406 spin_unlock(&port->phy_list_lock);
409 sas_notify_port_event(first_phy,
410 PORTE_BROADCAST_RCVD, GFP_KERNEL);
415 static void _sas_resume_ha(struct sas_ha_struct *ha, bool drain)
417 const unsigned long tmo = msecs_to_jiffies(25000);
420 /* deform ports on phys that did not resume
421 * at this point we may be racing the phy coming back (as posted
422 * by the lldd). So we post the event and once we are in the
423 * libsas context check that the phy remains suspended before
426 i = phys_suspended(ha);
428 dev_info(ha->dev, "waiting up to 25 seconds for %d phy%s to resume\n",
429 i, i > 1 ? "s" : "");
430 wait_event_timeout(ha->eh_wait_q, phys_suspended(ha) == 0, tmo);
431 for (i = 0; i < ha->num_phys; i++) {
432 struct asd_sas_phy *phy = ha->sas_phy[i];
434 if (phy->suspended) {
435 dev_warn(&phy->phy->dev, "resume timeout\n");
436 sas_notify_phy_event(phy, PHYE_RESUME_TIMEOUT,
441 /* all phys are back up or timed out, turn on i/o so we can
442 * flush out disks that did not return
444 scsi_unblock_requests(ha->core.shost);
447 clear_bit(SAS_HA_RESUMING, &ha->state);
449 sas_queue_deferred_work(ha);
450 /* send event PORTE_BROADCAST_RCVD to identify some new inserted
453 sas_resume_insert_broadcast_ha(ha);
456 void sas_resume_ha(struct sas_ha_struct *ha)
458 _sas_resume_ha(ha, true);
460 EXPORT_SYMBOL(sas_resume_ha);
462 /* A no-sync variant, which does not call sas_drain_ha(). */
463 void sas_resume_ha_no_sync(struct sas_ha_struct *ha)
465 _sas_resume_ha(ha, false);
467 EXPORT_SYMBOL(sas_resume_ha_no_sync);
469 void sas_suspend_ha(struct sas_ha_struct *ha)
473 sas_disable_events(ha);
474 scsi_block_requests(ha->core.shost);
475 for (i = 0; i < ha->num_phys; i++) {
476 struct asd_sas_port *port = ha->sas_port[i];
478 sas_discover_event(port, DISCE_SUSPEND);
481 /* flush suspend events while unregistered */
482 mutex_lock(&ha->drain_mutex);
483 __sas_drain_work(ha);
484 mutex_unlock(&ha->drain_mutex);
486 EXPORT_SYMBOL(sas_suspend_ha);
488 static void sas_phy_release(struct sas_phy *phy)
490 kfree(phy->hostdata);
491 phy->hostdata = NULL;
494 static void phy_reset_work(struct work_struct *work)
496 struct sas_phy_data *d = container_of(work, typeof(*d), reset_work.work);
498 d->reset_result = transport_sas_phy_reset(d->phy, d->hard_reset);
501 static void phy_enable_work(struct work_struct *work)
503 struct sas_phy_data *d = container_of(work, typeof(*d), enable_work.work);
505 d->enable_result = sas_phy_enable(d->phy, d->enable);
508 static int sas_phy_setup(struct sas_phy *phy)
510 struct sas_phy_data *d = kzalloc(sizeof(*d), GFP_KERNEL);
515 mutex_init(&d->event_lock);
516 INIT_SAS_WORK(&d->reset_work, phy_reset_work);
517 INIT_SAS_WORK(&d->enable_work, phy_enable_work);
524 static int queue_phy_reset(struct sas_phy *phy, int hard_reset)
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;
534 pm_runtime_get_sync(ha->dev);
535 /* libsas workqueue coordinates ata-eh reset with discovery */
536 mutex_lock(&d->event_lock);
538 d->hard_reset = hard_reset;
540 spin_lock_irq(&ha->lock);
541 sas_queue_work(ha, &d->reset_work);
542 spin_unlock_irq(&ha->lock);
544 rc = sas_drain_work(ha);
546 rc = d->reset_result;
547 mutex_unlock(&d->event_lock);
548 pm_runtime_put_sync(ha->dev);
553 static int queue_phy_enable(struct sas_phy *phy, int enable)
555 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
556 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
557 struct sas_phy_data *d = phy->hostdata;
563 pm_runtime_get_sync(ha->dev);
564 /* libsas workqueue coordinates ata-eh reset with discovery */
565 mutex_lock(&d->event_lock);
566 d->enable_result = 0;
569 spin_lock_irq(&ha->lock);
570 sas_queue_work(ha, &d->enable_work);
571 spin_unlock_irq(&ha->lock);
573 rc = sas_drain_work(ha);
575 rc = d->enable_result;
576 mutex_unlock(&d->event_lock);
577 pm_runtime_put_sync(ha->dev);
582 static struct sas_function_template sft = {
583 .phy_enable = queue_phy_enable,
584 .phy_reset = queue_phy_reset,
585 .phy_setup = sas_phy_setup,
586 .phy_release = sas_phy_release,
587 .set_phy_speed = sas_set_phy_speed,
588 .get_linkerrors = sas_get_linkerrors,
589 .smp_handler = sas_smp_handler,
592 static inline ssize_t phy_event_threshold_show(struct device *dev,
593 struct device_attribute *attr, char *buf)
595 struct Scsi_Host *shost = class_to_shost(dev);
596 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
598 return scnprintf(buf, PAGE_SIZE, "%u\n", sha->event_thres);
601 static inline ssize_t phy_event_threshold_store(struct device *dev,
602 struct device_attribute *attr,
603 const char *buf, size_t count)
605 struct Scsi_Host *shost = class_to_shost(dev);
606 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
608 sha->event_thres = simple_strtol(buf, NULL, 10);
610 /* threshold cannot be set too small */
611 if (sha->event_thres < 32)
612 sha->event_thres = 32;
617 DEVICE_ATTR(phy_event_threshold,
619 phy_event_threshold_show,
620 phy_event_threshold_store);
621 EXPORT_SYMBOL_GPL(dev_attr_phy_event_threshold);
623 struct scsi_transport_template *
624 sas_domain_attach_transport(struct sas_domain_function_template *dft)
626 struct scsi_transport_template *stt = sas_attach_transport(&sft);
627 struct sas_internal *i;
632 i = to_sas_internal(stt);
634 stt->create_work_queue = 1;
635 stt->eh_strategy_handler = sas_scsi_recover_host;
639 EXPORT_SYMBOL_GPL(sas_domain_attach_transport);
641 struct asd_sas_event *sas_alloc_event(struct asd_sas_phy *phy,
644 struct asd_sas_event *event;
645 struct sas_ha_struct *sas_ha = phy->ha;
646 struct sas_internal *i =
647 to_sas_internal(sas_ha->core.shost->transportt);
649 event = kmem_cache_zalloc(sas_event_cache, gfp_flags);
653 atomic_inc(&phy->event_nr);
655 if (atomic_read(&phy->event_nr) > phy->ha->event_thres) {
656 if (i->dft->lldd_control_phy) {
657 if (cmpxchg(&phy->in_shutdown, 0, 1) == 0) {
658 pr_notice("The phy%d bursting events, shut it down.\n",
660 sas_notify_phy_event(phy, PHYE_SHUTDOWN,
664 /* Do not support PHY control, stop allocating events */
665 WARN_ONCE(1, "PHY control not supported.\n");
666 kmem_cache_free(sas_event_cache, event);
667 atomic_dec(&phy->event_nr);
675 void sas_free_event(struct asd_sas_event *event)
677 struct asd_sas_phy *phy = event->phy;
679 kmem_cache_free(sas_event_cache, event);
680 atomic_dec(&phy->event_nr);
683 /* ---------- SAS Class register/unregister ---------- */
685 static int __init sas_class_init(void)
687 sas_task_cache = KMEM_CACHE(sas_task, SLAB_HWCACHE_ALIGN);
691 sas_event_cache = KMEM_CACHE(asd_sas_event, SLAB_HWCACHE_ALIGN);
692 if (!sas_event_cache)
697 kmem_cache_destroy(sas_task_cache);
702 static void __exit sas_class_exit(void)
704 kmem_cache_destroy(sas_task_cache);
705 kmem_cache_destroy(sas_event_cache);
709 MODULE_DESCRIPTION("SAS Transport Layer");
710 MODULE_LICENSE("GPL v2");
712 module_init(sas_class_init);
713 module_exit(sas_class_exit);