port.c 15 KB

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