1 // SPDX-License-Identifier: GPL-2.0
3 * Serial Attached SCSI (SAS) Event processing
5 * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
9 #include <linux/export.h>
10 #include <scsi/scsi_host.h>
11 #include "sas_internal.h"
13 int sas_queue_work(struct sas_ha_struct *ha, struct sas_work *sw)
15 /* it's added to the defer_q when draining so return succeed */
18 if (!test_bit(SAS_HA_REGISTERED, &ha->state))
21 if (test_bit(SAS_HA_DRAINING, &ha->state)) {
22 /* add it to the defer list, if not already pending */
23 if (list_empty(&sw->drain_node))
24 list_add_tail(&sw->drain_node, &ha->defer_q);
26 rc = queue_work(ha->event_q, &sw->work);
31 static int sas_queue_event(int event, struct sas_work *work,
32 struct sas_ha_struct *ha)
37 spin_lock_irqsave(&ha->lock, flags);
38 rc = sas_queue_work(ha, work);
39 spin_unlock_irqrestore(&ha->lock, flags);
44 void sas_queue_deferred_work(struct sas_ha_struct *ha)
46 struct sas_work *sw, *_sw;
49 spin_lock_irq(&ha->lock);
50 list_for_each_entry_safe(sw, _sw, &ha->defer_q, drain_node) {
51 list_del_init(&sw->drain_node);
52 ret = sas_queue_work(ha, sw);
54 pm_runtime_put(ha->dev);
55 sas_free_event(to_asd_sas_event(&sw->work));
58 spin_unlock_irq(&ha->lock);
61 void __sas_drain_work(struct sas_ha_struct *ha)
63 set_bit(SAS_HA_DRAINING, &ha->state);
64 /* flush submitters */
65 spin_lock_irq(&ha->lock);
66 spin_unlock_irq(&ha->lock);
68 drain_workqueue(ha->event_q);
69 drain_workqueue(ha->disco_q);
71 clear_bit(SAS_HA_DRAINING, &ha->state);
72 sas_queue_deferred_work(ha);
75 int sas_drain_work(struct sas_ha_struct *ha)
79 err = mutex_lock_interruptible(&ha->drain_mutex);
82 if (test_bit(SAS_HA_REGISTERED, &ha->state))
84 mutex_unlock(&ha->drain_mutex);
88 EXPORT_SYMBOL_GPL(sas_drain_work);
90 void sas_disable_revalidation(struct sas_ha_struct *ha)
92 mutex_lock(&ha->disco_mutex);
93 set_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state);
94 mutex_unlock(&ha->disco_mutex);
97 void sas_enable_revalidation(struct sas_ha_struct *ha)
101 mutex_lock(&ha->disco_mutex);
102 clear_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state);
103 for (i = 0; i < ha->num_phys; i++) {
104 struct asd_sas_port *port = ha->sas_port[i];
105 const int ev = DISCE_REVALIDATE_DOMAIN;
106 struct sas_discovery *d = &port->disc;
107 struct asd_sas_phy *sas_phy;
109 if (!test_and_clear_bit(ev, &d->pending))
112 spin_lock(&port->phy_list_lock);
113 if (list_empty(&port->phy_list)) {
114 spin_unlock(&port->phy_list_lock);
118 sas_phy = container_of(port->phy_list.next, struct asd_sas_phy,
120 spin_unlock(&port->phy_list_lock);
121 sas_notify_port_event(sas_phy,
122 PORTE_BROADCAST_RCVD, GFP_KERNEL);
124 mutex_unlock(&ha->disco_mutex);
128 static void sas_port_event_worker(struct work_struct *work)
130 struct asd_sas_event *ev = to_asd_sas_event(work);
131 struct asd_sas_phy *phy = ev->phy;
132 struct sas_ha_struct *ha = phy->ha;
134 sas_port_event_fns[ev->event](work);
135 pm_runtime_put(ha->dev);
139 static void sas_phy_event_worker(struct work_struct *work)
141 struct asd_sas_event *ev = to_asd_sas_event(work);
142 struct asd_sas_phy *phy = ev->phy;
143 struct sas_ha_struct *ha = phy->ha;
145 sas_phy_event_fns[ev->event](work);
146 pm_runtime_put(ha->dev);
150 /* defer works of new phys during suspend */
151 static bool sas_defer_event(struct asd_sas_phy *phy, struct asd_sas_event *ev)
153 struct sas_ha_struct *ha = phy->ha;
155 bool deferred = false;
157 spin_lock_irqsave(&ha->lock, flags);
158 if (test_bit(SAS_HA_RESUMING, &ha->state) && !phy->suspended) {
159 struct sas_work *sw = &ev->work;
161 list_add_tail(&sw->drain_node, &ha->defer_q);
164 spin_unlock_irqrestore(&ha->lock, flags);
168 int sas_notify_port_event(struct asd_sas_phy *phy, enum port_event event,
171 struct sas_ha_struct *ha = phy->ha;
172 struct asd_sas_event *ev;
175 BUG_ON(event >= PORT_NUM_EVENTS);
177 ev = sas_alloc_event(phy, gfp_flags);
181 /* Call pm_runtime_put() with pairs in sas_port_event_worker() */
182 pm_runtime_get_noresume(ha->dev);
184 INIT_SAS_EVENT(ev, sas_port_event_worker, phy, event);
186 if (sas_defer_event(phy, ev))
189 ret = sas_queue_event(event, &ev->work, ha);
191 pm_runtime_put(ha->dev);
197 EXPORT_SYMBOL_GPL(sas_notify_port_event);
199 int sas_notify_phy_event(struct asd_sas_phy *phy, enum phy_event event,
202 struct sas_ha_struct *ha = phy->ha;
203 struct asd_sas_event *ev;
206 BUG_ON(event >= PHY_NUM_EVENTS);
208 ev = sas_alloc_event(phy, gfp_flags);
212 /* Call pm_runtime_put() with pairs in sas_phy_event_worker() */
213 pm_runtime_get_noresume(ha->dev);
215 INIT_SAS_EVENT(ev, sas_phy_event_worker, phy, event);
217 if (sas_defer_event(phy, ev))
220 ret = sas_queue_event(event, &ev->work, ha);
222 pm_runtime_put(ha->dev);
228 EXPORT_SYMBOL_GPL(sas_notify_phy_event);