^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Serial Attached SCSI (SAS) Event processing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <scsi/scsi_host.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include "sas_internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) int sas_queue_work(struct sas_ha_struct *ha, struct sas_work *sw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) /* it's added to the defer_q when draining so return succeed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) int rc = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) if (!test_bit(SAS_HA_REGISTERED, &ha->state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) if (test_bit(SAS_HA_DRAINING, &ha->state)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) /* add it to the defer list, if not already pending */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) if (list_empty(&sw->drain_node))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) list_add_tail(&sw->drain_node, &ha->defer_q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) rc = queue_work(ha->event_q, &sw->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) static int sas_queue_event(int event, struct sas_work *work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct sas_ha_struct *ha)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) spin_lock_irqsave(&ha->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) rc = sas_queue_work(ha, work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) spin_unlock_irqrestore(&ha->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) void __sas_drain_work(struct sas_ha_struct *ha)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct sas_work *sw, *_sw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) set_bit(SAS_HA_DRAINING, &ha->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) /* flush submitters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) spin_lock_irq(&ha->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) spin_unlock_irq(&ha->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) drain_workqueue(ha->event_q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) drain_workqueue(ha->disco_q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) spin_lock_irq(&ha->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) clear_bit(SAS_HA_DRAINING, &ha->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) list_for_each_entry_safe(sw, _sw, &ha->defer_q, drain_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) list_del_init(&sw->drain_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) ret = sas_queue_work(ha, sw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) if (ret != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) sas_free_event(to_asd_sas_event(&sw->work));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) spin_unlock_irq(&ha->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) int sas_drain_work(struct sas_ha_struct *ha)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) err = mutex_lock_interruptible(&ha->drain_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (test_bit(SAS_HA_REGISTERED, &ha->state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) __sas_drain_work(ha);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) mutex_unlock(&ha->drain_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) EXPORT_SYMBOL_GPL(sas_drain_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) void sas_disable_revalidation(struct sas_ha_struct *ha)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) mutex_lock(&ha->disco_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) set_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) mutex_unlock(&ha->disco_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) void sas_enable_revalidation(struct sas_ha_struct *ha)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) mutex_lock(&ha->disco_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) clear_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) for (i = 0; i < ha->num_phys; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) struct asd_sas_port *port = ha->sas_port[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) const int ev = DISCE_REVALIDATE_DOMAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) struct sas_discovery *d = &port->disc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) struct asd_sas_phy *sas_phy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) if (!test_and_clear_bit(ev, &d->pending))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (list_empty(&port->phy_list))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) sas_phy = container_of(port->phy_list.next, struct asd_sas_phy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) port_phy_el);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) sas_notify_port_event(sas_phy, PORTE_BROADCAST_RCVD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) mutex_unlock(&ha->disco_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) static void sas_port_event_worker(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) struct asd_sas_event *ev = to_asd_sas_event(work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) sas_port_event_fns[ev->event](work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) sas_free_event(ev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) static void sas_phy_event_worker(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) struct asd_sas_event *ev = to_asd_sas_event(work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) sas_phy_event_fns[ev->event](work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) sas_free_event(ev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) static int __sas_notify_port_event(struct asd_sas_phy *phy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) enum port_event event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) struct asd_sas_event *ev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) struct sas_ha_struct *ha = phy->ha;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) BUG_ON(event >= PORT_NUM_EVENTS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) INIT_SAS_EVENT(ev, sas_port_event_worker, phy, event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) ret = sas_queue_event(event, &ev->work, ha);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (ret != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) sas_free_event(ev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) int sas_notify_port_event_gfp(struct asd_sas_phy *phy, enum port_event event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) gfp_t gfp_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) struct asd_sas_event *ev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) ev = sas_alloc_event_gfp(phy, gfp_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (!ev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) return __sas_notify_port_event(phy, event, ev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) EXPORT_SYMBOL_GPL(sas_notify_port_event_gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) int sas_notify_port_event(struct asd_sas_phy *phy, enum port_event event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) struct asd_sas_event *ev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) ev = sas_alloc_event(phy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) if (!ev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) return __sas_notify_port_event(phy, event, ev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) EXPORT_SYMBOL_GPL(sas_notify_port_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) static inline int __sas_notify_phy_event(struct asd_sas_phy *phy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) enum phy_event event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) struct asd_sas_event *ev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) struct sas_ha_struct *ha = phy->ha;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) BUG_ON(event >= PHY_NUM_EVENTS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) INIT_SAS_EVENT(ev, sas_phy_event_worker, phy, event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) ret = sas_queue_event(event, &ev->work, ha);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) if (ret != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) sas_free_event(ev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) int sas_notify_phy_event_gfp(struct asd_sas_phy *phy, enum phy_event event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) gfp_t gfp_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) struct asd_sas_event *ev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) ev = sas_alloc_event_gfp(phy, gfp_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) if (!ev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) return __sas_notify_phy_event(phy, event, ev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) EXPORT_SYMBOL_GPL(sas_notify_phy_event_gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) int sas_notify_phy_event(struct asd_sas_phy *phy, enum phy_event event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) struct asd_sas_event *ev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) ev = sas_alloc_event(phy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) if (!ev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) return __sas_notify_phy_event(phy, event, ev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) EXPORT_SYMBOL_GPL(sas_notify_phy_event);