sas_port.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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 <luben_tuikov@adaptec.com>
  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->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. memcpy(port->attached_sas_addr, phy->attached_sas_addr,
  72. SAS_ADDR_SIZE);
  73. port->iproto = phy->iproto;
  74. port->tproto = phy->tproto;
  75. port->oob_mode = phy->oob_mode;
  76. port->linkrate = phy->linkrate;
  77. } else {
  78. port->linkrate = max(port->linkrate, phy->linkrate);
  79. }
  80. }
  81. /**
  82. * sas_form_port - add this phy to a port
  83. * @phy: the phy of interest
  84. *
  85. * This function adds this phy to an existing port, thus creating a wide
  86. * port, or it creates a port and adds the phy to the port.
  87. */
  88. static void sas_form_port(struct asd_sas_phy *phy)
  89. {
  90. int i;
  91. struct sas_ha_struct *sas_ha = phy->ha;
  92. struct asd_sas_port *port = phy->port;
  93. struct domain_device *port_dev = NULL;
  94. struct sas_internal *si =
  95. to_sas_internal(sas_ha->shost->transportt);
  96. unsigned long flags;
  97. if (port) {
  98. if (!phy_is_wideport_member(port, phy))
  99. sas_deform_port(phy, 0);
  100. else if (phy->suspended) {
  101. phy->suspended = 0;
  102. sas_resume_port(phy);
  103. /* phy came back, try to cancel the timeout */
  104. wake_up(&sas_ha->eh_wait_q);
  105. return;
  106. } else {
  107. pr_info("%s: phy%d belongs to port%d already(%d)!\n",
  108. __func__, phy->id, phy->port->id,
  109. phy->port->num_phys);
  110. return;
  111. }
  112. }
  113. /* see if the phy should be part of a wide port */
  114. spin_lock_irqsave(&sas_ha->phy_port_lock, flags);
  115. for (i = 0; i < sas_ha->num_phys; i++) {
  116. port = sas_ha->sas_port[i];
  117. spin_lock(&port->phy_list_lock);
  118. if (*(u64 *) port->sas_addr &&
  119. phy_is_wideport_member(port, phy) && port->num_phys > 0) {
  120. /* wide port */
  121. port_dev = port->port_dev;
  122. sas_form_port_add_phy(port, phy, true);
  123. spin_unlock(&port->phy_list_lock);
  124. break;
  125. }
  126. spin_unlock(&port->phy_list_lock);
  127. }
  128. /* The phy does not match any existing port, create a new one */
  129. if (i == sas_ha->num_phys) {
  130. for (i = 0; i < sas_ha->num_phys; i++) {
  131. port = sas_ha->sas_port[i];
  132. spin_lock(&port->phy_list_lock);
  133. if (*(u64 *)port->sas_addr == 0
  134. && port->num_phys == 0) {
  135. port_dev = port->port_dev;
  136. sas_form_port_add_phy(port, phy, false);
  137. spin_unlock(&port->phy_list_lock);
  138. break;
  139. }
  140. spin_unlock(&port->phy_list_lock);
  141. }
  142. if (i >= sas_ha->num_phys) {
  143. pr_err("%s: couldn't find a free port, bug?\n",
  144. __func__);
  145. spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags);
  146. return;
  147. }
  148. }
  149. spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags);
  150. if (!port->port) {
  151. port->port = sas_port_alloc(phy->phy->dev.parent, port->id);
  152. BUG_ON(!port->port);
  153. sas_port_add(port->port);
  154. }
  155. sas_port_add_phy(port->port, phy->phy);
  156. pr_debug("%s added to %s, phy_mask:0x%x (%016llx)\n",
  157. dev_name(&phy->phy->dev), dev_name(&port->port->dev),
  158. port->phy_mask,
  159. SAS_ADDR(port->attached_sas_addr));
  160. if (port_dev)
  161. port_dev->pathways = port->num_phys;
  162. /* Tell the LLDD about this port formation. */
  163. if (si->dft->lldd_port_formed)
  164. si->dft->lldd_port_formed(phy);
  165. sas_discover_event(phy->port, DISCE_DISCOVER_DOMAIN);
  166. /* Only insert a revalidate event after initial discovery */
  167. if (port_dev && dev_is_expander(port_dev->dev_type)) {
  168. struct expander_device *ex_dev = &port_dev->ex_dev;
  169. ex_dev->ex_change_count = -1;
  170. sas_discover_event(port, DISCE_REVALIDATE_DOMAIN);
  171. }
  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. * @gone: whether or not the PHY is gone
  178. *
  179. * This is called when the physical link to the other phy has been
  180. * lost (on this phy), in Event thread context. We cannot delay here.
  181. */
  182. void sas_deform_port(struct asd_sas_phy *phy, int gone)
  183. {
  184. struct sas_ha_struct *sas_ha = phy->ha;
  185. struct asd_sas_port *port = phy->port;
  186. struct sas_internal *si =
  187. to_sas_internal(sas_ha->shost->transportt);
  188. struct domain_device *dev;
  189. unsigned long flags;
  190. if (!port)
  191. return; /* done by a phy event */
  192. dev = port->port_dev;
  193. if (dev)
  194. dev->pathways--;
  195. if (port->num_phys == 1) {
  196. sas_unregister_domain_devices(port, gone);
  197. sas_destruct_devices(port);
  198. sas_port_delete(port->port);
  199. port->port = NULL;
  200. } else {
  201. sas_port_delete_phy(port->port, phy->phy);
  202. sas_device_set_phy(dev, port->port);
  203. }
  204. if (si->dft->lldd_port_deformed)
  205. si->dft->lldd_port_deformed(phy);
  206. spin_lock_irqsave(&sas_ha->phy_port_lock, flags);
  207. spin_lock(&port->phy_list_lock);
  208. list_del_init(&phy->port_phy_el);
  209. sas_phy_set_target(phy, NULL);
  210. phy->port = NULL;
  211. port->num_phys--;
  212. port->phy_mask &= ~(1U << phy->id);
  213. if (port->num_phys == 0) {
  214. INIT_LIST_HEAD(&port->phy_list);
  215. memset(port->sas_addr, 0, SAS_ADDR_SIZE);
  216. memset(port->attached_sas_addr, 0, SAS_ADDR_SIZE);
  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. /* Only insert revalidate event if the port still has members */
  225. if (port->port && dev && dev_is_expander(dev->dev_type)) {
  226. struct expander_device *ex_dev = &dev->ex_dev;
  227. ex_dev->ex_change_count = -1;
  228. sas_discover_event(port, DISCE_REVALIDATE_DOMAIN);
  229. }
  230. flush_workqueue(sas_ha->disco_q);
  231. return;
  232. }
  233. /* ---------- SAS port events ---------- */
  234. void sas_porte_bytes_dmaed(struct work_struct *work)
  235. {
  236. struct asd_sas_event *ev = to_asd_sas_event(work);
  237. struct asd_sas_phy *phy = ev->phy;
  238. sas_form_port(phy);
  239. }
  240. void sas_porte_broadcast_rcvd(struct work_struct *work)
  241. {
  242. struct asd_sas_event *ev = to_asd_sas_event(work);
  243. struct asd_sas_phy *phy = ev->phy;
  244. unsigned long flags;
  245. u32 prim;
  246. spin_lock_irqsave(&phy->sas_prim_lock, flags);
  247. prim = phy->sas_prim;
  248. spin_unlock_irqrestore(&phy->sas_prim_lock, flags);
  249. pr_debug("broadcast received: %d\n", prim);
  250. sas_discover_event(phy->port, DISCE_REVALIDATE_DOMAIN);
  251. if (phy->port)
  252. flush_workqueue(phy->port->ha->disco_q);
  253. }
  254. void sas_porte_link_reset_err(struct work_struct *work)
  255. {
  256. struct asd_sas_event *ev = to_asd_sas_event(work);
  257. struct asd_sas_phy *phy = ev->phy;
  258. sas_deform_port(phy, 1);
  259. }
  260. void sas_porte_timer_event(struct work_struct *work)
  261. {
  262. struct asd_sas_event *ev = to_asd_sas_event(work);
  263. struct asd_sas_phy *phy = ev->phy;
  264. sas_deform_port(phy, 1);
  265. }
  266. void sas_porte_hard_reset(struct work_struct *work)
  267. {
  268. struct asd_sas_event *ev = to_asd_sas_event(work);
  269. struct asd_sas_phy *phy = ev->phy;
  270. sas_deform_port(phy, 1);
  271. }
  272. /* ---------- SAS port registration ---------- */
  273. static void sas_init_port(struct asd_sas_port *port,
  274. struct sas_ha_struct *sas_ha, int i)
  275. {
  276. memset(port, 0, sizeof(*port));
  277. port->id = i;
  278. INIT_LIST_HEAD(&port->dev_list);
  279. INIT_LIST_HEAD(&port->disco_list);
  280. INIT_LIST_HEAD(&port->destroy_list);
  281. INIT_LIST_HEAD(&port->sas_port_del_list);
  282. spin_lock_init(&port->phy_list_lock);
  283. INIT_LIST_HEAD(&port->phy_list);
  284. port->ha = sas_ha;
  285. spin_lock_init(&port->dev_list_lock);
  286. }
  287. int sas_register_ports(struct sas_ha_struct *sas_ha)
  288. {
  289. int i;
  290. /* initialize the ports and discovery */
  291. for (i = 0; i < sas_ha->num_phys; i++) {
  292. struct asd_sas_port *port = sas_ha->sas_port[i];
  293. sas_init_port(port, sas_ha, i);
  294. sas_init_disc(&port->disc, port);
  295. }
  296. return 0;
  297. }
  298. void sas_unregister_ports(struct sas_ha_struct *sas_ha)
  299. {
  300. int i;
  301. for (i = 0; i < sas_ha->num_phys; i++)
  302. if (sas_ha->sas_phy[i]->port)
  303. sas_deform_port(sas_ha->sas_phy[i], 0);
  304. }
  305. const work_func_t sas_port_event_fns[PORT_NUM_EVENTS] = {
  306. [PORTE_BYTES_DMAED] = sas_porte_bytes_dmaed,
  307. [PORTE_BROADCAST_RCVD] = sas_porte_broadcast_rcvd,
  308. [PORTE_LINK_RESET_ERR] = sas_porte_link_reset_err,
  309. [PORTE_TIMER_EVENT] = sas_porte_timer_event,
  310. [PORTE_HARD_RESET] = sas_porte_hard_reset,
  311. };