sas_event.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Serial Attached SCSI (SAS) Event processing
  3. *
  4. * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
  5. * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
  6. *
  7. * This file is licensed under GPLv2.
  8. *
  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.
  13. *
  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.
  18. *
  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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. */
  24. #include <linux/export.h>
  25. #include <scsi/scsi_host.h>
  26. #include "sas_internal.h"
  27. #include "sas_dump.h"
  28. int sas_queue_work(struct sas_ha_struct *ha, struct sas_work *sw)
  29. {
  30. /* it's added to the defer_q when draining so return succeed */
  31. int rc = 1;
  32. if (!test_bit(SAS_HA_REGISTERED, &ha->state))
  33. return 0;
  34. if (test_bit(SAS_HA_DRAINING, &ha->state)) {
  35. /* add it to the defer list, if not already pending */
  36. if (list_empty(&sw->drain_node))
  37. list_add_tail(&sw->drain_node, &ha->defer_q);
  38. } else
  39. rc = queue_work(ha->event_q, &sw->work);
  40. return rc;
  41. }
  42. static int sas_queue_event(int event, struct sas_work *work,
  43. struct sas_ha_struct *ha)
  44. {
  45. unsigned long flags;
  46. int rc;
  47. spin_lock_irqsave(&ha->lock, flags);
  48. rc = sas_queue_work(ha, work);
  49. spin_unlock_irqrestore(&ha->lock, flags);
  50. return rc;
  51. }
  52. void __sas_drain_work(struct sas_ha_struct *ha)
  53. {
  54. struct sas_work *sw, *_sw;
  55. int ret;
  56. set_bit(SAS_HA_DRAINING, &ha->state);
  57. /* flush submitters */
  58. spin_lock_irq(&ha->lock);
  59. spin_unlock_irq(&ha->lock);
  60. drain_workqueue(ha->event_q);
  61. drain_workqueue(ha->disco_q);
  62. spin_lock_irq(&ha->lock);
  63. clear_bit(SAS_HA_DRAINING, &ha->state);
  64. list_for_each_entry_safe(sw, _sw, &ha->defer_q, drain_node) {
  65. list_del_init(&sw->drain_node);
  66. ret = sas_queue_work(ha, sw);
  67. if (ret != 1)
  68. sas_free_event(to_asd_sas_event(&sw->work));
  69. }
  70. spin_unlock_irq(&ha->lock);
  71. }
  72. int sas_drain_work(struct sas_ha_struct *ha)
  73. {
  74. int err;
  75. err = mutex_lock_interruptible(&ha->drain_mutex);
  76. if (err)
  77. return err;
  78. if (test_bit(SAS_HA_REGISTERED, &ha->state))
  79. __sas_drain_work(ha);
  80. mutex_unlock(&ha->drain_mutex);
  81. return 0;
  82. }
  83. EXPORT_SYMBOL_GPL(sas_drain_work);
  84. void sas_disable_revalidation(struct sas_ha_struct *ha)
  85. {
  86. mutex_lock(&ha->disco_mutex);
  87. set_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state);
  88. mutex_unlock(&ha->disco_mutex);
  89. }
  90. void sas_enable_revalidation(struct sas_ha_struct *ha)
  91. {
  92. int i;
  93. mutex_lock(&ha->disco_mutex);
  94. clear_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state);
  95. for (i = 0; i < ha->num_phys; i++) {
  96. struct asd_sas_port *port = ha->sas_port[i];
  97. const int ev = DISCE_REVALIDATE_DOMAIN;
  98. struct sas_discovery *d = &port->disc;
  99. struct asd_sas_phy *sas_phy;
  100. if (!test_and_clear_bit(ev, &d->pending))
  101. continue;
  102. if (list_empty(&port->phy_list))
  103. continue;
  104. sas_phy = container_of(port->phy_list.next, struct asd_sas_phy,
  105. port_phy_el);
  106. ha->notify_port_event(sas_phy, PORTE_BROADCAST_RCVD);
  107. }
  108. mutex_unlock(&ha->disco_mutex);
  109. }
  110. static void sas_port_event_worker(struct work_struct *work)
  111. {
  112. struct asd_sas_event *ev = to_asd_sas_event(work);
  113. sas_port_event_fns[ev->event](work);
  114. sas_free_event(ev);
  115. }
  116. static void sas_phy_event_worker(struct work_struct *work)
  117. {
  118. struct asd_sas_event *ev = to_asd_sas_event(work);
  119. sas_phy_event_fns[ev->event](work);
  120. sas_free_event(ev);
  121. }
  122. static int sas_notify_port_event(struct asd_sas_phy *phy, enum port_event event)
  123. {
  124. struct asd_sas_event *ev;
  125. struct sas_ha_struct *ha = phy->ha;
  126. int ret;
  127. BUG_ON(event >= PORT_NUM_EVENTS);
  128. ev = sas_alloc_event(phy);
  129. if (!ev)
  130. return -ENOMEM;
  131. INIT_SAS_EVENT(ev, sas_port_event_worker, phy, event);
  132. ret = sas_queue_event(event, &ev->work, ha);
  133. if (ret != 1)
  134. sas_free_event(ev);
  135. return ret;
  136. }
  137. int sas_notify_phy_event(struct asd_sas_phy *phy, enum phy_event event)
  138. {
  139. struct asd_sas_event *ev;
  140. struct sas_ha_struct *ha = phy->ha;
  141. int ret;
  142. BUG_ON(event >= PHY_NUM_EVENTS);
  143. ev = sas_alloc_event(phy);
  144. if (!ev)
  145. return -ENOMEM;
  146. INIT_SAS_EVENT(ev, sas_phy_event_worker, phy, event);
  147. ret = sas_queue_event(event, &ev->work, ha);
  148. if (ret != 1)
  149. sas_free_event(ev);
  150. return ret;
  151. }
  152. int sas_init_events(struct sas_ha_struct *sas_ha)
  153. {
  154. sas_ha->notify_port_event = sas_notify_port_event;
  155. sas_ha->notify_phy_event = sas_notify_phy_event;
  156. return 0;
  157. }