sas_port.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * Serial Attached SCSI (SAS) Port class
  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 "sas_internal.h"
  25. #include <scsi/scsi_transport.h>
  26. #include <scsi/scsi_transport_sas.h>
  27. #include "../scsi_sas_internal.h"
  28. /**
  29. * sas_form_port -- add this phy to a port
  30. * @phy: the phy of interest
  31. *
  32. * This function adds this phy to an existing port, thus creating a wide
  33. * port, or it creates a port and adds the phy to the port.
  34. */
  35. static void sas_form_port(struct asd_sas_phy *phy)
  36. {
  37. int i;
  38. struct sas_ha_struct *sas_ha = phy->ha;
  39. struct asd_sas_port *port = phy->port;
  40. struct sas_internal *si =
  41. to_sas_internal(sas_ha->core.shost->transportt);
  42. unsigned long flags;
  43. if (port) {
  44. if (memcmp(port->attached_sas_addr, phy->attached_sas_addr,
  45. SAS_ADDR_SIZE) != 0)
  46. sas_deform_port(phy);
  47. else {
  48. SAS_DPRINTK("%s: phy%d belongs to port%d already(%d)!\n",
  49. __func__, phy->id, phy->port->id,
  50. phy->port->num_phys);
  51. return;
  52. }
  53. }
  54. /* find a port */
  55. spin_lock_irqsave(&sas_ha->phy_port_lock, flags);
  56. for (i = 0; i < sas_ha->num_phys; i++) {
  57. port = sas_ha->sas_port[i];
  58. spin_lock(&port->phy_list_lock);
  59. if (*(u64 *) port->sas_addr &&
  60. memcmp(port->attached_sas_addr,
  61. phy->attached_sas_addr, SAS_ADDR_SIZE) == 0 &&
  62. port->num_phys > 0) {
  63. /* wide port */
  64. SAS_DPRINTK("phy%d matched wide port%d\n", phy->id,
  65. port->id);
  66. break;
  67. } else if (*(u64 *) port->sas_addr == 0 && port->num_phys==0) {
  68. memcpy(port->sas_addr, phy->sas_addr, SAS_ADDR_SIZE);
  69. break;
  70. }
  71. spin_unlock(&port->phy_list_lock);
  72. }
  73. if (i >= sas_ha->num_phys) {
  74. printk(KERN_NOTICE "%s: couldn't find a free port, bug?\n",
  75. __func__);
  76. spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags);
  77. return;
  78. }
  79. /* add the phy to the port */
  80. list_add_tail(&phy->port_phy_el, &port->phy_list);
  81. phy->port = port;
  82. port->num_phys++;
  83. port->phy_mask |= (1U << phy->id);
  84. if (!port->phy)
  85. port->phy = phy->phy;
  86. if (*(u64 *)port->attached_sas_addr == 0) {
  87. port->class = phy->class;
  88. memcpy(port->attached_sas_addr, phy->attached_sas_addr,
  89. SAS_ADDR_SIZE);
  90. port->iproto = phy->iproto;
  91. port->tproto = phy->tproto;
  92. port->oob_mode = phy->oob_mode;
  93. port->linkrate = phy->linkrate;
  94. } else
  95. port->linkrate = max(port->linkrate, phy->linkrate);
  96. spin_unlock(&port->phy_list_lock);
  97. spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags);
  98. if (!port->port) {
  99. port->port = sas_port_alloc(phy->phy->dev.parent, port->id);
  100. BUG_ON(!port->port);
  101. sas_port_add(port->port);
  102. }
  103. sas_port_add_phy(port->port, phy->phy);
  104. SAS_DPRINTK("%s added to %s, phy_mask:0x%x (%16llx)\n",
  105. dev_name(&phy->phy->dev), dev_name(&port->port->dev),
  106. port->phy_mask,
  107. SAS_ADDR(port->attached_sas_addr));
  108. if (port->port_dev)
  109. port->port_dev->pathways = port->num_phys;
  110. /* Tell the LLDD about this port formation. */
  111. if (si->dft->lldd_port_formed)
  112. si->dft->lldd_port_formed(phy);
  113. sas_discover_event(phy->port, DISCE_DISCOVER_DOMAIN);
  114. }
  115. /**
  116. * sas_deform_port -- remove this phy from the port it belongs to
  117. * @phy: the phy of interest
  118. *
  119. * This is called when the physical link to the other phy has been
  120. * lost (on this phy), in Event thread context. We cannot delay here.
  121. */
  122. void sas_deform_port(struct asd_sas_phy *phy)
  123. {
  124. struct sas_ha_struct *sas_ha = phy->ha;
  125. struct asd_sas_port *port = phy->port;
  126. struct sas_internal *si =
  127. to_sas_internal(sas_ha->core.shost->transportt);
  128. unsigned long flags;
  129. if (!port)
  130. return; /* done by a phy event */
  131. if (port->port_dev)
  132. port->port_dev->pathways--;
  133. if (port->num_phys == 1) {
  134. sas_unregister_domain_devices(port);
  135. sas_port_delete(port->port);
  136. port->port = NULL;
  137. } else
  138. sas_port_delete_phy(port->port, phy->phy);
  139. if (si->dft->lldd_port_deformed)
  140. si->dft->lldd_port_deformed(phy);
  141. spin_lock_irqsave(&sas_ha->phy_port_lock, flags);
  142. spin_lock(&port->phy_list_lock);
  143. list_del_init(&phy->port_phy_el);
  144. phy->port = NULL;
  145. port->num_phys--;
  146. port->phy_mask &= ~(1U << phy->id);
  147. if (port->num_phys == 0) {
  148. INIT_LIST_HEAD(&port->phy_list);
  149. memset(port->sas_addr, 0, SAS_ADDR_SIZE);
  150. memset(port->attached_sas_addr, 0, SAS_ADDR_SIZE);
  151. port->class = 0;
  152. port->iproto = 0;
  153. port->tproto = 0;
  154. port->oob_mode = 0;
  155. port->phy_mask = 0;
  156. }
  157. spin_unlock(&port->phy_list_lock);
  158. spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags);
  159. return;
  160. }
  161. /* ---------- SAS port events ---------- */
  162. void sas_porte_bytes_dmaed(struct work_struct *work)
  163. {
  164. struct asd_sas_event *ev =
  165. container_of(work, struct asd_sas_event, work);
  166. struct asd_sas_phy *phy = ev->phy;
  167. sas_begin_event(PORTE_BYTES_DMAED, &phy->ha->event_lock,
  168. &phy->port_events_pending);
  169. sas_form_port(phy);
  170. }
  171. void sas_porte_broadcast_rcvd(struct work_struct *work)
  172. {
  173. struct asd_sas_event *ev =
  174. container_of(work, struct asd_sas_event, work);
  175. struct asd_sas_phy *phy = ev->phy;
  176. unsigned long flags;
  177. u32 prim;
  178. sas_begin_event(PORTE_BROADCAST_RCVD, &phy->ha->event_lock,
  179. &phy->port_events_pending);
  180. spin_lock_irqsave(&phy->sas_prim_lock, flags);
  181. prim = phy->sas_prim;
  182. spin_unlock_irqrestore(&phy->sas_prim_lock, flags);
  183. SAS_DPRINTK("broadcast received: %d\n", prim);
  184. sas_discover_event(phy->port, DISCE_REVALIDATE_DOMAIN);
  185. }
  186. void sas_porte_link_reset_err(struct work_struct *work)
  187. {
  188. struct asd_sas_event *ev =
  189. container_of(work, struct asd_sas_event, work);
  190. struct asd_sas_phy *phy = ev->phy;
  191. sas_begin_event(PORTE_LINK_RESET_ERR, &phy->ha->event_lock,
  192. &phy->port_events_pending);
  193. sas_deform_port(phy);
  194. }
  195. void sas_porte_timer_event(struct work_struct *work)
  196. {
  197. struct asd_sas_event *ev =
  198. container_of(work, struct asd_sas_event, work);
  199. struct asd_sas_phy *phy = ev->phy;
  200. sas_begin_event(PORTE_TIMER_EVENT, &phy->ha->event_lock,
  201. &phy->port_events_pending);
  202. sas_deform_port(phy);
  203. }
  204. void sas_porte_hard_reset(struct work_struct *work)
  205. {
  206. struct asd_sas_event *ev =
  207. container_of(work, struct asd_sas_event, work);
  208. struct asd_sas_phy *phy = ev->phy;
  209. sas_begin_event(PORTE_HARD_RESET, &phy->ha->event_lock,
  210. &phy->port_events_pending);
  211. sas_deform_port(phy);
  212. }
  213. /* ---------- SAS port registration ---------- */
  214. static void sas_init_port(struct asd_sas_port *port,
  215. struct sas_ha_struct *sas_ha, int i)
  216. {
  217. memset(port, 0, sizeof(*port));
  218. port->id = i;
  219. INIT_LIST_HEAD(&port->dev_list);
  220. spin_lock_init(&port->phy_list_lock);
  221. INIT_LIST_HEAD(&port->phy_list);
  222. port->ha = sas_ha;
  223. spin_lock_init(&port->dev_list_lock);
  224. }
  225. int sas_register_ports(struct sas_ha_struct *sas_ha)
  226. {
  227. int i;
  228. /* initialize the ports and discovery */
  229. for (i = 0; i < sas_ha->num_phys; i++) {
  230. struct asd_sas_port *port = sas_ha->sas_port[i];
  231. sas_init_port(port, sas_ha, i);
  232. sas_init_disc(&port->disc, port);
  233. }
  234. return 0;
  235. }
  236. void sas_unregister_ports(struct sas_ha_struct *sas_ha)
  237. {
  238. int i;
  239. for (i = 0; i < sas_ha->num_phys; i++)
  240. if (sas_ha->sas_phy[i]->port)
  241. sas_deform_port(sas_ha->sas_phy[i]);
  242. }