sas_port.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. static void sas_resume_port(struct asd_sas_phy *phy)
  38. {
  39. struct domain_device *dev;
  40. struct asd_sas_port *port = phy->port;
  41. struct sas_ha_struct *sas_ha = phy->ha;
  42. struct sas_internal *si = to_sas_internal(sas_ha->core.shost->transportt);
  43. if (si->dft->lldd_port_formed)
  44. si->dft->lldd_port_formed(phy);
  45. if (port->suspended)
  46. port->suspended = 0;
  47. else {
  48. /* we only need to handle "link returned" actions once */
  49. return;
  50. }
  51. /* if the port came back:
  52. * 1/ presume every device came back
  53. * 2/ force the next revalidation to check all expander phys
  54. */
  55. list_for_each_entry(dev, &port->dev_list, dev_list_node) {
  56. int i, rc;
  57. rc = sas_notify_lldd_dev_found(dev);
  58. if (rc) {
  59. sas_unregister_dev(port, dev);
  60. sas_destruct_devices(port);
  61. continue;
  62. }
  63. if (dev->dev_type == SAS_EDGE_EXPANDER_DEVICE || dev->dev_type == SAS_FANOUT_EXPANDER_DEVICE) {
  64. dev->ex_dev.ex_change_count = -1;
  65. for (i = 0; i < dev->ex_dev.num_phys; i++) {
  66. struct ex_phy *phy = &dev->ex_dev.ex_phy[i];
  67. phy->phy_change_count = -1;
  68. }
  69. }
  70. }
  71. sas_discover_event(port, DISCE_RESUME);
  72. }
  73. /**
  74. * sas_form_port -- add this phy to a port
  75. * @phy: the phy of interest
  76. *
  77. * This function adds this phy to an existing port, thus creating a wide
  78. * port, or it creates a port and adds the phy to the port.
  79. */
  80. static void sas_form_port(struct asd_sas_phy *phy)
  81. {
  82. int i;
  83. struct sas_ha_struct *sas_ha = phy->ha;
  84. struct asd_sas_port *port = phy->port;
  85. struct sas_internal *si =
  86. to_sas_internal(sas_ha->core.shost->transportt);
  87. unsigned long flags;
  88. if (port) {
  89. if (!phy_is_wideport_member(port, phy))
  90. sas_deform_port(phy, 0);
  91. else if (phy->suspended) {
  92. phy->suspended = 0;
  93. sas_resume_port(phy);
  94. /* phy came back, try to cancel the timeout */
  95. wake_up(&sas_ha->eh_wait_q);
  96. return;
  97. } else {
  98. SAS_DPRINTK("%s: phy%d belongs to port%d already(%d)!\n",
  99. __func__, phy->id, phy->port->id,
  100. phy->port->num_phys);
  101. return;
  102. }
  103. }
  104. /* see if the phy should be part of a wide port */
  105. spin_lock_irqsave(&sas_ha->phy_port_lock, flags);
  106. for (i = 0; i < sas_ha->num_phys; i++) {
  107. port = sas_ha->sas_port[i];
  108. spin_lock(&port->phy_list_lock);
  109. if (*(u64 *) port->sas_addr &&
  110. phy_is_wideport_member(port, phy) && port->num_phys > 0) {
  111. /* wide port */
  112. SAS_DPRINTK("phy%d matched wide port%d\n", phy->id,
  113. port->id);
  114. break;
  115. }
  116. spin_unlock(&port->phy_list_lock);
  117. }
  118. /* The phy does not match any existing port, create a new one */
  119. if (i == sas_ha->num_phys) {
  120. for (i = 0; i < sas_ha->num_phys; i++) {
  121. port = sas_ha->sas_port[i];
  122. spin_lock(&port->phy_list_lock);
  123. if (*(u64 *)port->sas_addr == 0
  124. && port->num_phys == 0) {
  125. memcpy(port->sas_addr, phy->sas_addr,
  126. SAS_ADDR_SIZE);
  127. break;
  128. }
  129. spin_unlock(&port->phy_list_lock);
  130. }
  131. }
  132. if (i >= sas_ha->num_phys) {
  133. printk(KERN_NOTICE "%s: couldn't find a free port, bug?\n",
  134. __func__);
  135. spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags);
  136. return;
  137. }
  138. /* add the phy to the port */
  139. list_add_tail(&phy->port_phy_el, &port->phy_list);
  140. sas_phy_set_target(phy, port->port_dev);
  141. phy->port = port;
  142. port->num_phys++;
  143. port->phy_mask |= (1U << phy->id);
  144. if (*(u64 *)port->attached_sas_addr == 0) {
  145. port->class = phy->class;
  146. memcpy(port->attached_sas_addr, phy->attached_sas_addr,
  147. SAS_ADDR_SIZE);
  148. port->iproto = phy->iproto;
  149. port->tproto = phy->tproto;
  150. port->oob_mode = phy->oob_mode;
  151. port->linkrate = phy->linkrate;
  152. } else
  153. port->linkrate = max(port->linkrate, phy->linkrate);
  154. spin_unlock(&port->phy_list_lock);
  155. spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags);
  156. if (!port->port) {
  157. port->port = sas_port_alloc(phy->phy->dev.parent, port->id);
  158. BUG_ON(!port->port);
  159. sas_port_add(port->port);
  160. }
  161. sas_port_add_phy(port->port, phy->phy);
  162. SAS_DPRINTK("%s added to %s, phy_mask:0x%x (%16llx)\n",
  163. dev_name(&phy->phy->dev), dev_name(&port->port->dev),
  164. port->phy_mask,
  165. SAS_ADDR(port->attached_sas_addr));
  166. if (port->port_dev)
  167. port->port_dev->pathways = port->num_phys;
  168. /* Tell the LLDD about this port formation. */
  169. if (si->dft->lldd_port_formed)
  170. si->dft->lldd_port_formed(phy);
  171. sas_discover_event(phy->port, DISCE_DISCOVER_DOMAIN);
  172. flush_workqueue(sas_ha->disco_q);
  173. }
  174. /**
  175. * sas_deform_port -- remove this phy from the port it belongs to
  176. * @phy: the phy of interest
  177. *
  178. * This is called when the physical link to the other phy has been
  179. * lost (on this phy), in Event thread context. We cannot delay here.
  180. */
  181. void sas_deform_port(struct asd_sas_phy *phy, int gone)
  182. {
  183. struct sas_ha_struct *sas_ha = phy->ha;
  184. struct asd_sas_port *port = phy->port;
  185. struct sas_internal *si =
  186. to_sas_internal(sas_ha->core.shost->transportt);
  187. struct domain_device *dev;
  188. unsigned long flags;
  189. if (!port)
  190. return; /* done by a phy event */
  191. dev = port->port_dev;
  192. if (dev)
  193. dev->pathways--;
  194. if (port->num_phys == 1) {
  195. sas_unregister_domain_devices(port, gone);
  196. sas_destruct_devices(port);
  197. sas_port_delete(port->port);
  198. port->port = NULL;
  199. } else {
  200. sas_port_delete_phy(port->port, phy->phy);
  201. sas_device_set_phy(dev, port->port);
  202. }
  203. if (si->dft->lldd_port_deformed)
  204. si->dft->lldd_port_deformed(phy);
  205. spin_lock_irqsave(&sas_ha->phy_port_lock, flags);
  206. spin_lock(&port->phy_list_lock);
  207. list_del_init(&phy->port_phy_el);
  208. sas_phy_set_target(phy, NULL);
  209. phy->port = NULL;
  210. port->num_phys--;
  211. port->phy_mask &= ~(1U << phy->id);
  212. if (port->num_phys == 0) {
  213. INIT_LIST_HEAD(&port->phy_list);
  214. memset(port->sas_addr, 0, SAS_ADDR_SIZE);
  215. memset(port->attached_sas_addr, 0, SAS_ADDR_SIZE);
  216. port->class = 0;
  217. port->iproto = 0;
  218. port->tproto = 0;
  219. port->oob_mode = 0;
  220. port->phy_mask = 0;
  221. }
  222. spin_unlock(&port->phy_list_lock);
  223. spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags);
  224. return;
  225. }
  226. /* ---------- SAS port events ---------- */
  227. void sas_porte_bytes_dmaed(struct work_struct *work)
  228. {
  229. struct asd_sas_event *ev = to_asd_sas_event(work);
  230. struct asd_sas_phy *phy = ev->phy;
  231. sas_form_port(phy);
  232. }
  233. void sas_porte_broadcast_rcvd(struct work_struct *work)
  234. {
  235. struct asd_sas_event *ev = to_asd_sas_event(work);
  236. struct asd_sas_phy *phy = ev->phy;
  237. unsigned long flags;
  238. u32 prim;
  239. spin_lock_irqsave(&phy->sas_prim_lock, flags);
  240. prim = phy->sas_prim;
  241. spin_unlock_irqrestore(&phy->sas_prim_lock, flags);
  242. SAS_DPRINTK("broadcast received: %d\n", prim);
  243. sas_discover_event(phy->port, DISCE_REVALIDATE_DOMAIN);
  244. if (phy->port)
  245. flush_workqueue(phy->port->ha->disco_q);
  246. }
  247. void sas_porte_link_reset_err(struct work_struct *work)
  248. {
  249. struct asd_sas_event *ev = to_asd_sas_event(work);
  250. struct asd_sas_phy *phy = ev->phy;
  251. sas_deform_port(phy, 1);
  252. }
  253. void sas_porte_timer_event(struct work_struct *work)
  254. {
  255. struct asd_sas_event *ev = to_asd_sas_event(work);
  256. struct asd_sas_phy *phy = ev->phy;
  257. sas_deform_port(phy, 1);
  258. }
  259. void sas_porte_hard_reset(struct work_struct *work)
  260. {
  261. struct asd_sas_event *ev = to_asd_sas_event(work);
  262. struct asd_sas_phy *phy = ev->phy;
  263. sas_deform_port(phy, 1);
  264. }
  265. /* ---------- SAS port registration ---------- */
  266. static void sas_init_port(struct asd_sas_port *port,
  267. struct sas_ha_struct *sas_ha, int i)
  268. {
  269. memset(port, 0, sizeof(*port));
  270. port->id = i;
  271. INIT_LIST_HEAD(&port->dev_list);
  272. INIT_LIST_HEAD(&port->disco_list);
  273. INIT_LIST_HEAD(&port->destroy_list);
  274. INIT_LIST_HEAD(&port->sas_port_del_list);
  275. spin_lock_init(&port->phy_list_lock);
  276. INIT_LIST_HEAD(&port->phy_list);
  277. port->ha = sas_ha;
  278. spin_lock_init(&port->dev_list_lock);
  279. }
  280. int sas_register_ports(struct sas_ha_struct *sas_ha)
  281. {
  282. int i;
  283. /* initialize the ports and discovery */
  284. for (i = 0; i < sas_ha->num_phys; i++) {
  285. struct asd_sas_port *port = sas_ha->sas_port[i];
  286. sas_init_port(port, sas_ha, i);
  287. sas_init_disc(&port->disc, port);
  288. }
  289. return 0;
  290. }
  291. void sas_unregister_ports(struct sas_ha_struct *sas_ha)
  292. {
  293. int i;
  294. for (i = 0; i < sas_ha->num_phys; i++)
  295. if (sas_ha->sas_phy[i]->port)
  296. sas_deform_port(sas_ha->sas_phy[i], 0);
  297. }
  298. const work_func_t sas_port_event_fns[PORT_NUM_EVENTS] = {
  299. [PORTE_BYTES_DMAED] = sas_porte_bytes_dmaed,
  300. [PORTE_BROADCAST_RCVD] = sas_porte_broadcast_rcvd,
  301. [PORTE_LINK_RESET_ERR] = sas_porte_link_reset_err,
  302. [PORTE_TIMER_EVENT] = sas_porte_timer_event,
  303. [PORTE_HARD_RESET] = sas_porte_hard_reset,
  304. };