port.c 13 KB

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