2 * Serial Attached SCSI (SAS) Transport Layer initialization
4 * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
5 * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
7 * This file is licensed under GPLv2.
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.
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.
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
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>
37 #include "sas_internal.h"
39 #include "../scsi_sas_internal.h"
41 static struct kmem_cache *sas_task_cache;
42 static struct kmem_cache *sas_event_cache;
44 struct sas_task *sas_alloc_task(gfp_t flags)
46 struct sas_task *task = kmem_cache_zalloc(sas_task_cache, flags);
49 spin_lock_init(&task->task_state_lock);
50 task->task_state_flags = SAS_TASK_STATE_PENDING;
55 EXPORT_SYMBOL_GPL(sas_alloc_task);
57 struct sas_task *sas_alloc_slow_task(gfp_t flags)
59 struct sas_task *task = sas_alloc_task(flags);
60 struct sas_task_slow *slow = kmalloc(sizeof(*slow), flags);
64 kmem_cache_free(sas_task_cache, task);
69 task->slow_task = slow;
71 timer_setup(&slow->timer, NULL, 0);
72 init_completion(&slow->completion);
76 EXPORT_SYMBOL_GPL(sas_alloc_slow_task);
78 void sas_free_task(struct sas_task *task)
81 kfree(task->slow_task);
82 kmem_cache_free(sas_task_cache, task);
85 EXPORT_SYMBOL_GPL(sas_free_task);
87 /*------------ SAS addr hash -----------*/
88 void sas_hash_addr(u8 *hashed, const u8 *sas_addr)
90 const u32 poly = 0x00DB2777;
94 for (i = 0; i < SAS_ADDR_SIZE; i++) {
97 for (b = (SAS_ADDR_SIZE - 1); b >= 0; b--) {
99 if ((1 << b) & sas_addr[i]) {
100 if (!(r & 0x01000000))
102 } else if (r & 0x01000000) {
108 hashed[0] = (r >> 16) & 0xFF;
109 hashed[1] = (r >> 8) & 0xFF;
110 hashed[2] = r & 0xFF;
113 int sas_register_ha(struct sas_ha_struct *sas_ha)
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);
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);
129 sas_ha->event_thres = SAS_PHY_SHUTDOWN_THRES;
131 error = sas_register_phys(sas_ha);
133 pr_notice("couldn't register sas phys:%d\n", error);
137 error = sas_register_ports(sas_ha);
139 pr_notice("couldn't register sas ports:%d\n", error);
143 error = sas_init_events(sas_ha);
145 pr_notice("couldn't start event thread:%d\n", error);
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)
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)
160 INIT_LIST_HEAD(&sas_ha->eh_done_q);
161 INIT_LIST_HEAD(&sas_ha->eh_ata_q);
166 destroy_workqueue(sas_ha->event_q);
168 sas_unregister_ports(sas_ha);
174 static void sas_disable_events(struct sas_ha_struct *sas_ha)
176 /* Set the state to unregistered to avoid further unchained
177 * events to be queued, and flush any in-progress drainers
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);
187 int sas_unregister_ha(struct sas_ha_struct *sas_ha)
189 sas_disable_events(sas_ha);
190 sas_unregister_ports(sas_ha);
192 /* flush unregistration work */
193 mutex_lock(&sas_ha->drain_mutex);
194 __sas_drain_work(sas_ha);
195 mutex_unlock(&sas_ha->drain_mutex);
197 destroy_workqueue(sas_ha->disco_q);
198 destroy_workqueue(sas_ha->event_q);
203 static int sas_get_linkerrors(struct sas_phy *phy)
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);
212 return i->dft->lldd_control_phy(asd_phy, PHY_FUNC_GET_EVENTS, NULL);
215 return sas_smp_get_phy_events(phy);
218 int sas_try_ata_reset(struct asd_sas_phy *asd_phy)
220 struct domain_device *dev = NULL;
222 /* try to route user requested link resets through libata */
224 dev = asd_phy->port->port_dev;
226 /* validate that dev has been probed */
228 dev = sas_find_dev_by_rphy(dev->rphy);
230 if (dev && dev_is_sata(dev)) {
231 sas_ata_schedule_reset(dev);
232 sas_ata_wait_eh(dev);
240 * transport_sas_phy_reset - reset a phy and permit libata to manage the link
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
245 static int transport_sas_phy_reset(struct sas_phy *phy, int hard_reset)
247 enum phy_func reset_type;
250 reset_type = PHY_FUNC_HARD_RESET;
252 reset_type = PHY_FUNC_LINK_RESET;
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);
261 if (!hard_reset && sas_try_ata_reset(asd_phy) == 0)
263 return i->dft->lldd_control_phy(asd_phy, reset_type, NULL);
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);
269 if (ata_dev && !hard_reset) {
270 sas_ata_schedule_reset(ata_dev);
271 sas_ata_wait_eh(ata_dev);
274 return sas_smp_phy_control(ddev, phy->number, reset_type, NULL);
278 static int sas_phy_enable(struct sas_phy *phy, int enable)
284 cmd = PHY_FUNC_LINK_RESET;
286 cmd = PHY_FUNC_DISABLE;
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);
296 ret = transport_sas_phy_reset(phy, 0);
298 ret = i->dft->lldd_control_phy(asd_phy, cmd, NULL);
300 struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
301 struct domain_device *ddev = sas_find_dev_by_rphy(rphy);
304 ret = transport_sas_phy_reset(phy, 0);
306 ret = sas_smp_phy_control(ddev, phy->number, cmd, NULL);
311 int sas_phy_reset(struct sas_phy *phy, int hard_reset)
314 enum phy_func reset_type;
320 reset_type = PHY_FUNC_HARD_RESET;
322 reset_type = PHY_FUNC_LINK_RESET;
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);
331 ret = i->dft->lldd_control_phy(asd_phy, reset_type, NULL);
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);
340 int sas_set_phy_speed(struct sas_phy *phy,
341 struct sas_phy_linkrates *rates)
345 if ((rates->minimum_linkrate &&
346 rates->minimum_linkrate > phy->maximum_linkrate) ||
347 (rates->maximum_linkrate &&
348 rates->maximum_linkrate < phy->minimum_linkrate))
351 if (rates->minimum_linkrate &&
352 rates->minimum_linkrate < phy->minimum_linkrate_hw)
353 rates->minimum_linkrate = phy->minimum_linkrate_hw;
355 if (rates->maximum_linkrate &&
356 rates->maximum_linkrate > phy->maximum_linkrate_hw)
357 rates->maximum_linkrate = phy->maximum_linkrate_hw;
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);
366 ret = i->dft->lldd_control_phy(asd_phy, PHY_FUNC_SET_LINK_RATE,
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);
379 void sas_prep_resume_ha(struct sas_ha_struct *ha)
383 set_bit(SAS_HA_REGISTERED, &ha->state);
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];
389 memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
390 phy->frame_rcvd_size = 0;
393 EXPORT_SYMBOL(sas_prep_resume_ha);
395 static int phys_suspended(struct sas_ha_struct *ha)
399 for (i = 0; i < ha->num_phys; i++) {
400 struct asd_sas_phy *phy = ha->sas_phy[i];
409 void sas_resume_ha(struct sas_ha_struct *ha)
411 const unsigned long tmo = msecs_to_jiffies(25000);
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
420 i = phys_suspended(ha);
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];
428 if (phy->suspended) {
429 dev_warn(&phy->phy->dev, "resume timeout\n");
430 sas_notify_phy_event(phy, PHYE_RESUME_TIMEOUT);
434 /* all phys are back up or timed out, turn on i/o so we can
435 * flush out disks that did not return
437 scsi_unblock_requests(ha->core.shost);
440 EXPORT_SYMBOL(sas_resume_ha);
442 void sas_suspend_ha(struct sas_ha_struct *ha)
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];
451 sas_discover_event(port, DISCE_SUSPEND);
454 /* flush suspend events while unregistered */
455 mutex_lock(&ha->drain_mutex);
456 __sas_drain_work(ha);
457 mutex_unlock(&ha->drain_mutex);
459 EXPORT_SYMBOL(sas_suspend_ha);
461 static void sas_phy_release(struct sas_phy *phy)
463 kfree(phy->hostdata);
464 phy->hostdata = NULL;
467 static void phy_reset_work(struct work_struct *work)
469 struct sas_phy_data *d = container_of(work, typeof(*d), reset_work.work);
471 d->reset_result = transport_sas_phy_reset(d->phy, d->hard_reset);
474 static void phy_enable_work(struct work_struct *work)
476 struct sas_phy_data *d = container_of(work, typeof(*d), enable_work.work);
478 d->enable_result = sas_phy_enable(d->phy, d->enable);
481 static int sas_phy_setup(struct sas_phy *phy)
483 struct sas_phy_data *d = kzalloc(sizeof(*d), GFP_KERNEL);
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);
497 static int queue_phy_reset(struct sas_phy *phy, int hard_reset)
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;
507 /* libsas workqueue coordinates ata-eh reset with discovery */
508 mutex_lock(&d->event_lock);
510 d->hard_reset = hard_reset;
512 spin_lock_irq(&ha->lock);
513 sas_queue_work(ha, &d->reset_work);
514 spin_unlock_irq(&ha->lock);
516 rc = sas_drain_work(ha);
518 rc = d->reset_result;
519 mutex_unlock(&d->event_lock);
524 static int queue_phy_enable(struct sas_phy *phy, int enable)
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 /* libsas workqueue coordinates ata-eh reset with discovery */
535 mutex_lock(&d->event_lock);
536 d->enable_result = 0;
539 spin_lock_irq(&ha->lock);
540 sas_queue_work(ha, &d->enable_work);
541 spin_unlock_irq(&ha->lock);
543 rc = sas_drain_work(ha);
545 rc = d->enable_result;
546 mutex_unlock(&d->event_lock);
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,
561 static inline ssize_t phy_event_threshold_show(struct device *dev,
562 struct device_attribute *attr, char *buf)
564 struct Scsi_Host *shost = class_to_shost(dev);
565 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
567 return scnprintf(buf, PAGE_SIZE, "%u\n", sha->event_thres);
570 static inline ssize_t phy_event_threshold_store(struct device *dev,
571 struct device_attribute *attr,
572 const char *buf, size_t count)
574 struct Scsi_Host *shost = class_to_shost(dev);
575 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
577 sha->event_thres = simple_strtol(buf, NULL, 10);
579 /* threshold cannot be set too small */
580 if (sha->event_thres < 32)
581 sha->event_thres = 32;
586 DEVICE_ATTR(phy_event_threshold,
588 phy_event_threshold_show,
589 phy_event_threshold_store);
590 EXPORT_SYMBOL_GPL(dev_attr_phy_event_threshold);
592 struct scsi_transport_template *
593 sas_domain_attach_transport(struct sas_domain_function_template *dft)
595 struct scsi_transport_template *stt = sas_attach_transport(&sft);
596 struct sas_internal *i;
601 i = to_sas_internal(stt);
603 stt->create_work_queue = 1;
604 stt->eh_strategy_handler = sas_scsi_recover_host;
608 EXPORT_SYMBOL_GPL(sas_domain_attach_transport);
611 struct asd_sas_event *sas_alloc_event(struct asd_sas_phy *phy)
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);
619 event = kmem_cache_zalloc(sas_event_cache, flags);
623 atomic_inc(&phy->event_nr);
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",
630 sas_notify_phy_event(phy, PHYE_SHUTDOWN);
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);
644 void sas_free_event(struct asd_sas_event *event)
646 struct asd_sas_phy *phy = event->phy;
648 kmem_cache_free(sas_event_cache, event);
649 atomic_dec(&phy->event_nr);
652 /* ---------- SAS Class register/unregister ---------- */
654 static int __init sas_class_init(void)
656 sas_task_cache = KMEM_CACHE(sas_task, SLAB_HWCACHE_ALIGN);
660 sas_event_cache = KMEM_CACHE(asd_sas_event, SLAB_HWCACHE_ALIGN);
661 if (!sas_event_cache)
666 kmem_cache_destroy(sas_task_cache);
671 static void __exit sas_class_exit(void)
673 kmem_cache_destroy(sas_task_cache);
674 kmem_cache_destroy(sas_event_cache);
677 MODULE_AUTHOR("Luben Tuikov <luben_tuikov@adaptec.com>");
678 MODULE_DESCRIPTION("SAS Transport Layer");
679 MODULE_LICENSE("GPL v2");
681 module_init(sas_class_init);
682 module_exit(sas_class_exit);
684 EXPORT_SYMBOL_GPL(sas_register_ha);
685 EXPORT_SYMBOL_GPL(sas_unregister_ha);