port.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * This file is provided under a dual BSD/GPLv2 license. When using or
  3. * redistributing this file, you may do so under either license.
  4. *
  5. * GPL LICENSE SUMMARY
  6. *
  7. * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  21. * The full GNU General Public License is included in this distribution
  22. * in the file called LICENSE.GPL.
  23. *
  24. * BSD LICENSE
  25. *
  26. * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
  27. * All rights reserved.
  28. *
  29. * Redistribution and use in source and binary forms, with or without
  30. * modification, are permitted provided that the following conditions
  31. * are met:
  32. *
  33. * * Redistributions of source code must retain the above copyright
  34. * notice, this list of conditions and the following disclaimer.
  35. * * Redistributions in binary form must reproduce the above copyright
  36. * notice, this list of conditions and the following disclaimer in
  37. * the documentation and/or other materials provided with the
  38. * distribution.
  39. * * Neither the name of Intel Corporation nor the names of its
  40. * contributors may be used to endorse or promote products derived
  41. * from this software without specific prior written permission.
  42. *
  43. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  44. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  45. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  46. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  47. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  48. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  49. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  50. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  51. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  52. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  53. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  54. */
  55. #include <linux/workqueue.h>
  56. #include "isci.h"
  57. #include "scic_port.h"
  58. #include "port.h"
  59. #include "request.h"
  60. static void isci_port_change_state(struct isci_port *iport, enum isci_status status)
  61. {
  62. unsigned long flags;
  63. dev_dbg(&iport->isci_host->pdev->dev,
  64. "%s: iport = %p, state = 0x%x\n",
  65. __func__, iport, status);
  66. /* XXX pointless lock */
  67. spin_lock_irqsave(&iport->state_lock, flags);
  68. iport->status = status;
  69. spin_unlock_irqrestore(&iport->state_lock, flags);
  70. }
  71. void isci_port_init(struct isci_port *iport, struct isci_host *ihost, int index)
  72. {
  73. INIT_LIST_HEAD(&iport->remote_dev_list);
  74. INIT_LIST_HEAD(&iport->domain_dev_list);
  75. spin_lock_init(&iport->state_lock);
  76. init_completion(&iport->start_complete);
  77. iport->isci_host = ihost;
  78. isci_port_change_state(iport, isci_freed);
  79. }
  80. /**
  81. * isci_port_get_state() - This function gets the status of the port object.
  82. * @isci_port: This parameter points to the isci_port object
  83. *
  84. * status of the object as a isci_status enum.
  85. */
  86. enum isci_status isci_port_get_state(
  87. struct isci_port *isci_port)
  88. {
  89. return isci_port->status;
  90. }
  91. void isci_port_bc_change_received(struct isci_host *ihost,
  92. struct scic_sds_port *sci_port,
  93. struct scic_sds_phy *sci_phy)
  94. {
  95. struct isci_phy *iphy = sci_phy_to_iphy(sci_phy);
  96. dev_dbg(&ihost->pdev->dev, "%s: iphy = %p, sas_phy = %p\n",
  97. __func__, iphy, &iphy->sas_phy);
  98. ihost->sas_ha.notify_port_event(&iphy->sas_phy, PORTE_BROADCAST_RCVD);
  99. scic_port_enable_broadcast_change_notification(sci_port);
  100. }
  101. void isci_port_link_up(struct isci_host *isci_host,
  102. struct scic_sds_port *port,
  103. struct scic_sds_phy *phy)
  104. {
  105. unsigned long flags;
  106. struct scic_port_properties properties;
  107. struct isci_phy *isci_phy = sci_phy_to_iphy(phy);
  108. struct isci_port *isci_port = sci_port_to_iport(port);
  109. unsigned long success = true;
  110. BUG_ON(isci_phy->isci_port != NULL);
  111. isci_phy->isci_port = isci_port;
  112. dev_dbg(&isci_host->pdev->dev,
  113. "%s: isci_port = %p\n",
  114. __func__, isci_port);
  115. spin_lock_irqsave(&isci_phy->sas_phy.frame_rcvd_lock, flags);
  116. isci_port_change_state(isci_phy->isci_port, isci_starting);
  117. scic_port_get_properties(port, &properties);
  118. if (phy->protocol == SCIC_SDS_PHY_PROTOCOL_SATA) {
  119. u64 attached_sas_address;
  120. isci_phy->sas_phy.oob_mode = SATA_OOB_MODE;
  121. isci_phy->sas_phy.frame_rcvd_size = sizeof(struct dev_to_host_fis);
  122. /*
  123. * For direct-attached SATA devices, the SCI core will
  124. * automagically assign a SAS address to the end device
  125. * for the purpose of creating a port. This SAS address
  126. * will not be the same as assigned to the PHY and needs
  127. * to be obtained from struct scic_port_properties properties.
  128. */
  129. attached_sas_address = properties.remote.sas_address.high;
  130. attached_sas_address <<= 32;
  131. attached_sas_address |= properties.remote.sas_address.low;
  132. swab64s(&attached_sas_address);
  133. memcpy(&isci_phy->sas_phy.attached_sas_addr,
  134. &attached_sas_address, sizeof(attached_sas_address));
  135. } else if (phy->protocol == SCIC_SDS_PHY_PROTOCOL_SAS) {
  136. isci_phy->sas_phy.oob_mode = SAS_OOB_MODE;
  137. isci_phy->sas_phy.frame_rcvd_size = sizeof(struct sas_identify_frame);
  138. /* Copy the attached SAS address from the IAF */
  139. memcpy(isci_phy->sas_phy.attached_sas_addr,
  140. isci_phy->frame_rcvd.iaf.sas_addr, SAS_ADDR_SIZE);
  141. } else {
  142. dev_err(&isci_host->pdev->dev, "%s: unkown target\n", __func__);
  143. success = false;
  144. }
  145. isci_phy->sas_phy.phy->negotiated_linkrate = sci_phy_linkrate(phy);
  146. spin_unlock_irqrestore(&isci_phy->sas_phy.frame_rcvd_lock, flags);
  147. /* Notify libsas that we have an address frame, if indeed
  148. * we've found an SSP, SMP, or STP target */
  149. if (success)
  150. isci_host->sas_ha.notify_port_event(&isci_phy->sas_phy,
  151. PORTE_BYTES_DMAED);
  152. }
  153. /**
  154. * isci_port_link_down() - This function is called by the sci core when a link
  155. * becomes inactive.
  156. * @isci_host: This parameter specifies the isci host object.
  157. * @phy: This parameter specifies the isci phy with the active link.
  158. * @port: This parameter specifies the isci port with the active link.
  159. *
  160. */
  161. void isci_port_link_down(struct isci_host *isci_host, struct isci_phy *isci_phy,
  162. struct isci_port *isci_port)
  163. {
  164. struct isci_remote_device *isci_device;
  165. dev_dbg(&isci_host->pdev->dev,
  166. "%s: isci_port = %p\n", __func__, isci_port);
  167. if (isci_port) {
  168. /* check to see if this is the last phy on this port. */
  169. if (isci_phy->sas_phy.port
  170. && isci_phy->sas_phy.port->num_phys == 1) {
  171. /* change the state for all devices on this port.
  172. * The next task sent to this device will be returned
  173. * as SAS_TASK_UNDELIVERED, and the scsi mid layer
  174. * will remove the target
  175. */
  176. list_for_each_entry(isci_device,
  177. &isci_port->remote_dev_list,
  178. node) {
  179. dev_dbg(&isci_host->pdev->dev,
  180. "%s: isci_device = %p\n",
  181. __func__, isci_device);
  182. isci_remote_device_change_state(isci_device,
  183. isci_stopping);
  184. }
  185. }
  186. isci_port_change_state(isci_port, isci_stopping);
  187. }
  188. /* Notify libsas of the borken link, this will trigger calls to our
  189. * isci_port_deformed and isci_dev_gone functions.
  190. */
  191. sas_phy_disconnected(&isci_phy->sas_phy);
  192. isci_host->sas_ha.notify_phy_event(&isci_phy->sas_phy,
  193. PHYE_LOSS_OF_SIGNAL);
  194. isci_phy->isci_port = NULL;
  195. dev_dbg(&isci_host->pdev->dev,
  196. "%s: isci_port = %p - Done\n", __func__, isci_port);
  197. }
  198. /**
  199. * isci_port_deformed() - This function is called by libsas when a port becomes
  200. * inactive.
  201. * @phy: This parameter specifies the libsas phy with the inactive port.
  202. *
  203. */
  204. void isci_port_deformed(
  205. struct asd_sas_phy *phy)
  206. {
  207. pr_debug("%s: sas_phy = %p\n", __func__, phy);
  208. }
  209. /**
  210. * isci_port_formed() - This function is called by libsas when a port becomes
  211. * active.
  212. * @phy: This parameter specifies the libsas phy with the active port.
  213. *
  214. */
  215. void isci_port_formed(
  216. struct asd_sas_phy *phy)
  217. {
  218. pr_debug("%s: sas_phy = %p, sas_port = %p\n", __func__, phy, phy->port);
  219. }
  220. /**
  221. * isci_port_ready() - This function is called by the sci core when a link
  222. * becomes ready.
  223. * @isci_host: This parameter specifies the isci host object.
  224. * @port: This parameter specifies the sci port with the active link.
  225. *
  226. */
  227. void isci_port_ready(struct isci_host *isci_host, struct isci_port *isci_port)
  228. {
  229. dev_dbg(&isci_host->pdev->dev,
  230. "%s: isci_port = %p\n", __func__, isci_port);
  231. complete_all(&isci_port->start_complete);
  232. isci_port_change_state(isci_port, isci_ready);
  233. return;
  234. }
  235. /**
  236. * isci_port_not_ready() - This function is called by the sci core when a link
  237. * is not ready. All remote devices on this link will be removed if they are
  238. * in the stopping state.
  239. * @isci_host: This parameter specifies the isci host object.
  240. * @port: This parameter specifies the sci port with the active link.
  241. *
  242. */
  243. void isci_port_not_ready(struct isci_host *isci_host, struct isci_port *isci_port)
  244. {
  245. dev_dbg(&isci_host->pdev->dev,
  246. "%s: isci_port = %p\n", __func__, isci_port);
  247. }
  248. /**
  249. * isci_port_hard_reset_complete() - This function is called by the sci core
  250. * when the hard reset complete notification has been received.
  251. * @port: This parameter specifies the sci port with the active link.
  252. * @completion_status: This parameter specifies the core status for the reset
  253. * process.
  254. *
  255. */
  256. void isci_port_hard_reset_complete(struct isci_port *isci_port,
  257. enum sci_status completion_status)
  258. {
  259. dev_dbg(&isci_port->isci_host->pdev->dev,
  260. "%s: isci_port = %p, completion_status=%x\n",
  261. __func__, isci_port, completion_status);
  262. /* Save the status of the hard reset from the port. */
  263. isci_port->hard_reset_status = completion_status;
  264. complete_all(&isci_port->hard_reset_complete);
  265. }
  266. int isci_port_perform_hard_reset(struct isci_host *ihost, struct isci_port *iport,
  267. struct isci_phy *iphy)
  268. {
  269. unsigned long flags;
  270. enum sci_status status;
  271. int ret = TMF_RESP_FUNC_COMPLETE;
  272. dev_dbg(&ihost->pdev->dev, "%s: iport = %p\n",
  273. __func__, iport);
  274. init_completion(&iport->hard_reset_complete);
  275. spin_lock_irqsave(&ihost->scic_lock, flags);
  276. #define ISCI_PORT_RESET_TIMEOUT SCIC_SDS_SIGNATURE_FIS_TIMEOUT
  277. status = scic_port_hard_reset(&iport->sci, ISCI_PORT_RESET_TIMEOUT);
  278. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  279. if (status == SCI_SUCCESS) {
  280. wait_for_completion(&iport->hard_reset_complete);
  281. dev_dbg(&ihost->pdev->dev,
  282. "%s: iport = %p; hard reset completion\n",
  283. __func__, iport);
  284. if (iport->hard_reset_status != SCI_SUCCESS)
  285. ret = TMF_RESP_FUNC_FAILED;
  286. } else {
  287. ret = TMF_RESP_FUNC_FAILED;
  288. dev_err(&ihost->pdev->dev,
  289. "%s: iport = %p; scic_port_hard_reset call"
  290. " failed 0x%x\n",
  291. __func__, iport, status);
  292. }
  293. /* If the hard reset for the port has failed, consider this
  294. * the same as link failures on all phys in the port.
  295. */
  296. if (ret != TMF_RESP_FUNC_COMPLETE) {
  297. dev_err(&ihost->pdev->dev,
  298. "%s: iport = %p; hard reset failed "
  299. "(0x%x) - sending link down to libsas for phy %p\n",
  300. __func__, iport, iport->hard_reset_status, iphy);
  301. isci_port_link_down(ihost, iphy, iport);
  302. }
  303. return ret;
  304. }
  305. void isci_port_stop_complete(struct scic_sds_controller *scic,
  306. struct scic_sds_port *sci_port,
  307. enum sci_status completion_status)
  308. {
  309. dev_dbg(&scic_to_ihost(scic)->pdev->dev, "Port stop complete\n");
  310. }