port.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. /**
  56. * This file contains the isci port implementation.
  57. *
  58. *
  59. */
  60. #include <linux/workqueue.h>
  61. #include "isci.h"
  62. #include "scic_io_request.h"
  63. #include "scic_phy.h"
  64. #include "scic_sds_phy.h"
  65. #include "scic_port.h"
  66. #include "port.h"
  67. #include "request.h"
  68. #include "core/scic_sds_controller.h"
  69. static void isci_port_change_state(struct isci_port *iport, enum isci_status status)
  70. {
  71. unsigned long flags;
  72. dev_dbg(&iport->isci_host->pdev->dev,
  73. "%s: iport = %p, state = 0x%x\n",
  74. __func__, iport, status);
  75. /* XXX pointless lock */
  76. spin_lock_irqsave(&iport->state_lock, flags);
  77. iport->status = status;
  78. spin_unlock_irqrestore(&iport->state_lock, flags);
  79. }
  80. void isci_port_init(struct isci_port *iport, struct isci_host *ihost, int index)
  81. {
  82. INIT_LIST_HEAD(&iport->remote_dev_list);
  83. INIT_LIST_HEAD(&iport->domain_dev_list);
  84. spin_lock_init(&iport->state_lock);
  85. init_completion(&iport->start_complete);
  86. iport->isci_host = ihost;
  87. isci_port_change_state(iport, isci_freed);
  88. }
  89. /**
  90. * isci_port_get_state() - This function gets the status of the port object.
  91. * @isci_port: This parameter points to the isci_port object
  92. *
  93. * status of the object as a isci_status enum.
  94. */
  95. enum isci_status isci_port_get_state(
  96. struct isci_port *isci_port)
  97. {
  98. return isci_port->status;
  99. }
  100. void isci_port_bc_change_received(struct isci_host *ihost,
  101. struct scic_sds_port *sci_port,
  102. struct scic_sds_phy *sci_phy)
  103. {
  104. struct isci_phy *iphy = sci_phy_to_iphy(sci_phy);
  105. dev_dbg(&ihost->pdev->dev, "%s: iphy = %p, sas_phy = %p\n",
  106. __func__, iphy, &iphy->sas_phy);
  107. ihost->sas_ha.notify_port_event(&iphy->sas_phy, PORTE_BROADCAST_RCVD);
  108. scic_port_enable_broadcast_change_notification(sci_port);
  109. }
  110. void isci_port_link_up(struct isci_host *isci_host,
  111. struct scic_sds_port *port,
  112. struct scic_sds_phy *phy)
  113. {
  114. unsigned long flags;
  115. struct scic_port_properties properties;
  116. struct isci_phy *isci_phy = sci_phy_to_iphy(phy);
  117. struct isci_port *isci_port = sci_port_to_iport(port);
  118. unsigned long success = true;
  119. BUG_ON(isci_phy->isci_port != NULL);
  120. isci_phy->isci_port = isci_port;
  121. dev_dbg(&isci_host->pdev->dev,
  122. "%s: isci_port = %p\n",
  123. __func__, isci_port);
  124. spin_lock_irqsave(&isci_phy->sas_phy.frame_rcvd_lock, flags);
  125. isci_port_change_state(isci_phy->isci_port, isci_starting);
  126. scic_port_get_properties(port, &properties);
  127. if (phy->protocol == SCIC_SDS_PHY_PROTOCOL_SATA) {
  128. u64 attached_sas_address;
  129. isci_phy->sas_phy.oob_mode = SATA_OOB_MODE;
  130. isci_phy->sas_phy.frame_rcvd_size = sizeof(struct dev_to_host_fis);
  131. /*
  132. * For direct-attached SATA devices, the SCI core will
  133. * automagically assign a SAS address to the end device
  134. * for the purpose of creating a port. This SAS address
  135. * will not be the same as assigned to the PHY and needs
  136. * to be obtained from struct scic_port_properties properties.
  137. */
  138. attached_sas_address = properties.remote.sas_address.high;
  139. attached_sas_address <<= 32;
  140. attached_sas_address |= properties.remote.sas_address.low;
  141. swab64s(&attached_sas_address);
  142. memcpy(&isci_phy->sas_phy.attached_sas_addr,
  143. &attached_sas_address, sizeof(attached_sas_address));
  144. } else if (phy->protocol == SCIC_SDS_PHY_PROTOCOL_SAS) {
  145. isci_phy->sas_phy.oob_mode = SAS_OOB_MODE;
  146. isci_phy->sas_phy.frame_rcvd_size = sizeof(struct sas_identify_frame);
  147. /* Copy the attached SAS address from the IAF */
  148. memcpy(isci_phy->sas_phy.attached_sas_addr,
  149. isci_phy->frame_rcvd.iaf.sas_addr, SAS_ADDR_SIZE);
  150. } else {
  151. dev_err(&isci_host->pdev->dev, "%s: unkown target\n", __func__);
  152. success = false;
  153. }
  154. isci_phy->sas_phy.phy->negotiated_linkrate = sci_phy_linkrate(phy);
  155. spin_unlock_irqrestore(&isci_phy->sas_phy.frame_rcvd_lock, flags);
  156. /* Notify libsas that we have an address frame, if indeed
  157. * we've found an SSP, SMP, or STP target */
  158. if (success)
  159. isci_host->sas_ha.notify_port_event(&isci_phy->sas_phy,
  160. PORTE_BYTES_DMAED);
  161. }
  162. /**
  163. * isci_port_link_down() - This function is called by the sci core when a link
  164. * becomes inactive.
  165. * @isci_host: This parameter specifies the isci host object.
  166. * @phy: This parameter specifies the isci phy with the active link.
  167. * @port: This parameter specifies the isci port with the active link.
  168. *
  169. */
  170. void isci_port_link_down(struct isci_host *isci_host, struct isci_phy *isci_phy,
  171. struct isci_port *isci_port)
  172. {
  173. struct isci_remote_device *isci_device;
  174. dev_dbg(&isci_host->pdev->dev,
  175. "%s: isci_port = %p\n", __func__, isci_port);
  176. if (isci_port) {
  177. /* check to see if this is the last phy on this port. */
  178. if (isci_phy->sas_phy.port
  179. && isci_phy->sas_phy.port->num_phys == 1) {
  180. /* change the state for all devices on this port.
  181. * The next task sent to this device will be returned
  182. * as SAS_TASK_UNDELIVERED, and the scsi mid layer
  183. * will remove the target
  184. */
  185. list_for_each_entry(isci_device,
  186. &isci_port->remote_dev_list,
  187. node) {
  188. dev_dbg(&isci_host->pdev->dev,
  189. "%s: isci_device = %p\n",
  190. __func__, isci_device);
  191. isci_remote_device_change_state(isci_device,
  192. isci_stopping);
  193. }
  194. }
  195. isci_port_change_state(isci_port, isci_stopping);
  196. }
  197. /* Notify libsas of the borken link, this will trigger calls to our
  198. * isci_port_deformed and isci_dev_gone functions.
  199. */
  200. sas_phy_disconnected(&isci_phy->sas_phy);
  201. isci_host->sas_ha.notify_phy_event(&isci_phy->sas_phy,
  202. PHYE_LOSS_OF_SIGNAL);
  203. isci_phy->isci_port = NULL;
  204. dev_dbg(&isci_host->pdev->dev,
  205. "%s: isci_port = %p - Done\n", __func__, isci_port);
  206. }
  207. /**
  208. * isci_port_deformed() - This function is called by libsas when a port becomes
  209. * inactive.
  210. * @phy: This parameter specifies the libsas phy with the inactive port.
  211. *
  212. */
  213. void isci_port_deformed(
  214. struct asd_sas_phy *phy)
  215. {
  216. pr_debug("%s: sas_phy = %p\n", __func__, phy);
  217. }
  218. /**
  219. * isci_port_formed() - This function is called by libsas when a port becomes
  220. * active.
  221. * @phy: This parameter specifies the libsas phy with the active port.
  222. *
  223. */
  224. void isci_port_formed(
  225. struct asd_sas_phy *phy)
  226. {
  227. pr_debug("%s: sas_phy = %p, sas_port = %p\n", __func__, phy, phy->port);
  228. }
  229. /**
  230. * isci_port_ready() - This function is called by the sci core when a link
  231. * becomes ready.
  232. * @isci_host: This parameter specifies the isci host object.
  233. * @port: This parameter specifies the sci port with the active link.
  234. *
  235. */
  236. void isci_port_ready(struct isci_host *isci_host, struct isci_port *isci_port)
  237. {
  238. dev_dbg(&isci_host->pdev->dev,
  239. "%s: isci_port = %p\n", __func__, isci_port);
  240. complete_all(&isci_port->start_complete);
  241. isci_port_change_state(isci_port, isci_ready);
  242. return;
  243. }
  244. /**
  245. * isci_port_not_ready() - This function is called by the sci core when a link
  246. * is not ready. All remote devices on this link will be removed if they are
  247. * in the stopping state.
  248. * @isci_host: This parameter specifies the isci host object.
  249. * @port: This parameter specifies the sci port with the active link.
  250. *
  251. */
  252. void isci_port_not_ready(struct isci_host *isci_host, struct isci_port *isci_port)
  253. {
  254. dev_dbg(&isci_host->pdev->dev,
  255. "%s: isci_port = %p\n", __func__, isci_port);
  256. }
  257. /**
  258. * isci_port_hard_reset_complete() - This function is called by the sci core
  259. * when the hard reset complete notification has been received.
  260. * @port: This parameter specifies the sci port with the active link.
  261. * @completion_status: This parameter specifies the core status for the reset
  262. * process.
  263. *
  264. */
  265. void isci_port_hard_reset_complete(struct isci_port *isci_port,
  266. enum sci_status completion_status)
  267. {
  268. dev_dbg(&isci_port->isci_host->pdev->dev,
  269. "%s: isci_port = %p, completion_status=%x\n",
  270. __func__, isci_port, completion_status);
  271. /* Save the status of the hard reset from the port. */
  272. isci_port->hard_reset_status = completion_status;
  273. complete_all(&isci_port->hard_reset_complete);
  274. }
  275. int isci_port_perform_hard_reset(struct isci_host *ihost, struct isci_port *iport,
  276. struct isci_phy *iphy)
  277. {
  278. unsigned long flags;
  279. enum sci_status status;
  280. int ret = TMF_RESP_FUNC_COMPLETE;
  281. dev_dbg(&ihost->pdev->dev, "%s: iport = %p\n",
  282. __func__, iport);
  283. init_completion(&iport->hard_reset_complete);
  284. spin_lock_irqsave(&ihost->scic_lock, flags);
  285. #define ISCI_PORT_RESET_TIMEOUT SCIC_SDS_SIGNATURE_FIS_TIMEOUT
  286. status = scic_port_hard_reset(&iport->sci, ISCI_PORT_RESET_TIMEOUT);
  287. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  288. if (status == SCI_SUCCESS) {
  289. wait_for_completion(&iport->hard_reset_complete);
  290. dev_dbg(&ihost->pdev->dev,
  291. "%s: iport = %p; hard reset completion\n",
  292. __func__, iport);
  293. if (iport->hard_reset_status != SCI_SUCCESS)
  294. ret = TMF_RESP_FUNC_FAILED;
  295. } else {
  296. ret = TMF_RESP_FUNC_FAILED;
  297. dev_err(&ihost->pdev->dev,
  298. "%s: iport = %p; scic_port_hard_reset call"
  299. " failed 0x%x\n",
  300. __func__, iport, status);
  301. }
  302. /* If the hard reset for the port has failed, consider this
  303. * the same as link failures on all phys in the port.
  304. */
  305. if (ret != TMF_RESP_FUNC_COMPLETE) {
  306. dev_err(&ihost->pdev->dev,
  307. "%s: iport = %p; hard reset failed "
  308. "(0x%x) - sending link down to libsas for phy %p\n",
  309. __func__, iport, iport->hard_reset_status, iphy);
  310. isci_port_link_down(ihost, iphy, iport);
  311. }
  312. return ret;
  313. }
  314. void isci_port_stop_complete(struct scic_sds_controller *scic,
  315. struct scic_sds_port *sci_port,
  316. enum sci_status completion_status)
  317. {
  318. dev_dbg(&scic_to_ihost(scic)->pdev->dev, "Port stop complete\n");
  319. }