port.c 14 KB

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