sas_port.c 8.2 KB

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