port.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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. memcpy(isci_phy->frame_rcvd.aif,
  201. &(sas_phy_properties.received_iaf),
  202. sizeof(struct sci_sas_identify_address_frame));
  203. isci_phy->sas_phy.frame_rcvd_size
  204. = sizeof(struct sci_sas_identify_address_frame);
  205. /* Copy the attached SAS address from the IAF */
  206. memcpy(isci_phy->sas_phy.attached_sas_addr,
  207. ((struct sas_identify_frame *)
  208. (&isci_phy->frame_rcvd.aif))->sas_addr,
  209. SAS_ADDR_SIZE);
  210. } else {
  211. dev_err(&isci_host->pdev->dev, "%s: unkown target\n", __func__);
  212. success = false;
  213. }
  214. isci_phy->sas_phy.phy->negotiated_linkrate = sci_phy_linkrate(phy);
  215. spin_unlock_irqrestore(&isci_phy->sas_phy.frame_rcvd_lock, flags);
  216. /* Notify libsas that we have an address frame, if indeed
  217. * we've found an SSP, SMP, or STP target */
  218. if (success)
  219. isci_host->sas_ha.notify_port_event(&isci_phy->sas_phy,
  220. PORTE_BYTES_DMAED);
  221. }
  222. /**
  223. * isci_port_link_down() - This function is called by the sci core when a link
  224. * becomes inactive.
  225. * @isci_host: This parameter specifies the isci host object.
  226. * @phy: This parameter specifies the isci phy with the active link.
  227. * @port: This parameter specifies the isci port with the active link.
  228. *
  229. */
  230. void isci_port_link_down(struct isci_host *isci_host, struct isci_phy *isci_phy,
  231. struct isci_port *isci_port)
  232. {
  233. struct isci_remote_device *isci_device;
  234. dev_dbg(&isci_host->pdev->dev,
  235. "%s: isci_port = %p\n", __func__, isci_port);
  236. if (isci_port) {
  237. /* check to see if this is the last phy on this port. */
  238. if (isci_phy->sas_phy.port
  239. && isci_phy->sas_phy.port->num_phys == 1) {
  240. /* change the state for all devices on this port.
  241. * The next task sent to this device will be returned
  242. * as SAS_TASK_UNDELIVERED, and the scsi mid layer
  243. * will remove the target
  244. */
  245. list_for_each_entry(isci_device,
  246. &isci_port->remote_dev_list,
  247. node) {
  248. dev_dbg(&isci_host->pdev->dev,
  249. "%s: isci_device = %p\n",
  250. __func__, isci_device);
  251. isci_remote_device_change_state(isci_device,
  252. isci_stopping);
  253. }
  254. }
  255. isci_port_change_state(isci_port, isci_stopping);
  256. }
  257. /* Notify libsas of the borken link, this will trigger calls to our
  258. * isci_port_deformed and isci_dev_gone functions.
  259. */
  260. sas_phy_disconnected(&isci_phy->sas_phy);
  261. isci_host->sas_ha.notify_phy_event(&isci_phy->sas_phy,
  262. PHYE_LOSS_OF_SIGNAL);
  263. isci_phy->isci_port = NULL;
  264. dev_dbg(&isci_host->pdev->dev,
  265. "%s: isci_port = %p - Done\n", __func__, isci_port);
  266. }
  267. /**
  268. * isci_port_deformed() - This function is called by libsas when a port becomes
  269. * inactive.
  270. * @phy: This parameter specifies the libsas phy with the inactive port.
  271. *
  272. */
  273. void isci_port_deformed(
  274. struct asd_sas_phy *phy)
  275. {
  276. pr_debug("%s: sas_phy = %p\n", __func__, phy);
  277. }
  278. /**
  279. * isci_port_formed() - This function is called by libsas when a port becomes
  280. * active.
  281. * @phy: This parameter specifies the libsas phy with the active port.
  282. *
  283. */
  284. void isci_port_formed(
  285. struct asd_sas_phy *phy)
  286. {
  287. pr_debug("%s: sas_phy = %p, sas_port = %p\n", __func__, phy, phy->port);
  288. }
  289. /**
  290. * isci_port_ready() - This function is called by the sci core when a link
  291. * becomes ready.
  292. * @isci_host: This parameter specifies the isci host object.
  293. * @port: This parameter specifies the sci port with the active link.
  294. *
  295. */
  296. void isci_port_ready(struct isci_host *isci_host, struct isci_port *isci_port)
  297. {
  298. dev_dbg(&isci_host->pdev->dev,
  299. "%s: isci_port = %p\n", __func__, isci_port);
  300. complete_all(&isci_port->start_complete);
  301. isci_port_change_state(isci_port, isci_ready);
  302. return;
  303. }
  304. /**
  305. * isci_port_not_ready() - This function is called by the sci core when a link
  306. * is not ready. All remote devices on this link will be removed if they are
  307. * in the stopping state.
  308. * @isci_host: This parameter specifies the isci host object.
  309. * @port: This parameter specifies the sci port with the active link.
  310. *
  311. */
  312. void isci_port_not_ready(struct isci_host *isci_host, struct isci_port *isci_port)
  313. {
  314. dev_dbg(&isci_host->pdev->dev,
  315. "%s: isci_port = %p\n", __func__, isci_port);
  316. }
  317. /**
  318. * isci_port_hard_reset_complete() - This function is called by the sci core
  319. * when the hard reset complete notification has been received.
  320. * @port: This parameter specifies the sci port with the active link.
  321. * @completion_status: This parameter specifies the core status for the reset
  322. * process.
  323. *
  324. */
  325. void isci_port_hard_reset_complete(struct isci_port *isci_port,
  326. enum sci_status completion_status)
  327. {
  328. dev_dbg(&isci_port->isci_host->pdev->dev,
  329. "%s: isci_port = %p, completion_status=%x\n",
  330. __func__, isci_port, completion_status);
  331. /* Save the status of the hard reset from the port. */
  332. isci_port->hard_reset_status = completion_status;
  333. complete_all(&isci_port->hard_reset_complete);
  334. }
  335. int isci_port_perform_hard_reset(struct isci_host *ihost, struct isci_port *iport,
  336. struct isci_phy *iphy)
  337. {
  338. unsigned long flags;
  339. enum sci_status status;
  340. int ret = TMF_RESP_FUNC_COMPLETE;
  341. dev_dbg(&ihost->pdev->dev, "%s: iport = %p\n",
  342. __func__, iport);
  343. init_completion(&iport->hard_reset_complete);
  344. spin_lock_irqsave(&ihost->scic_lock, flags);
  345. #define ISCI_PORT_RESET_TIMEOUT SCIC_SDS_SIGNATURE_FIS_TIMEOUT
  346. status = scic_port_hard_reset(iport->sci_port_handle,
  347. ISCI_PORT_RESET_TIMEOUT);
  348. spin_unlock_irqrestore(&ihost->scic_lock, flags);
  349. if (status == SCI_SUCCESS) {
  350. wait_for_completion(&iport->hard_reset_complete);
  351. dev_dbg(&ihost->pdev->dev,
  352. "%s: iport = %p; hard reset completion\n",
  353. __func__, iport);
  354. if (iport->hard_reset_status != SCI_SUCCESS)
  355. ret = TMF_RESP_FUNC_FAILED;
  356. } else {
  357. ret = TMF_RESP_FUNC_FAILED;
  358. dev_err(&ihost->pdev->dev,
  359. "%s: iport = %p; scic_port_hard_reset call"
  360. " failed 0x%x\n",
  361. __func__, iport, status);
  362. }
  363. /* If the hard reset for the port has failed, consider this
  364. * the same as link failures on all phys in the port.
  365. */
  366. if (ret != TMF_RESP_FUNC_COMPLETE) {
  367. dev_err(&ihost->pdev->dev,
  368. "%s: iport = %p; hard reset failed "
  369. "(0x%x) - sending link down to libsas for phy %p\n",
  370. __func__, iport, iport->hard_reset_status, iphy);
  371. isci_port_link_down(ihost, iphy, iport);
  372. }
  373. return ret;
  374. }
  375. /**
  376. * isci_port_invalid_link_up() - This function informs the SCI Core user that
  377. * a phy/link became ready, but the phy is not allowed in the port. In some
  378. * situations the underlying hardware only allows for certain phy to port
  379. * mappings. If these mappings are violated, then this API is invoked.
  380. * @controller: This parameter represents the controller which contains the
  381. * port.
  382. * @port: This parameter specifies the SCI port object for which the callback
  383. * is being invoked.
  384. * @phy: This parameter specifies the phy that came ready, but the phy can't be
  385. * a valid member of the port.
  386. *
  387. */
  388. void isci_port_invalid_link_up(struct scic_sds_controller *scic,
  389. struct scic_sds_port *sci_port,
  390. struct scic_sds_phy *phy)
  391. {
  392. struct isci_host *ihost = scic->ihost;
  393. dev_warn(&ihost->pdev->dev, "Invalid link up!\n");
  394. }
  395. void isci_port_stop_complete(struct scic_sds_controller *scic,
  396. struct scic_sds_port *sci_port,
  397. enum sci_status completion_status)
  398. {
  399. struct isci_host *ihost = scic->ihost;
  400. dev_dbg(&ihost->pdev->dev, "Port stop complete\n");
  401. }