port.c 15 KB

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