sas_port.c 9.6 KB

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