port.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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->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. sci_object_set_association(scic_port, isci_port);
  95. isci_port->sci_port_handle = scic_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 =
  126. (struct isci_phy *)sci_object_get_association(phy);
  127. dev_dbg(&isci_host->pdev->dev,
  128. "%s: isci_phy = %p, sas_phy = %p\n",
  129. __func__,
  130. isci_phy,
  131. &isci_phy->sas_phy);
  132. isci_host->sas_ha.notify_port_event(
  133. &isci_phy->sas_phy,
  134. PORTE_BROADCAST_RCVD
  135. );
  136. scic_port_enable_broadcast_change_notification(port);
  137. }
  138. /**
  139. * isci_port_link_up() - This function is called by the sci core when a link
  140. * becomes active. the identify address frame is retrieved from the core and
  141. * a notify port event is sent to libsas.
  142. * @isci_host: This parameter specifies the isci host object.
  143. * @port: This parameter specifies the sci port with the active link.
  144. * @phy: This parameter specifies the sci phy with the active link.
  145. *
  146. */
  147. void isci_port_link_up(
  148. struct isci_host *isci_host,
  149. struct scic_sds_port *port,
  150. struct scic_sds_phy *phy)
  151. {
  152. unsigned long flags;
  153. struct scic_port_properties properties;
  154. struct isci_phy *isci_phy
  155. = (struct isci_phy *)sci_object_get_association(phy);
  156. struct isci_port *isci_port
  157. = (struct isci_port *)sci_object_get_association(port);
  158. enum sci_status call_status;
  159. unsigned long success = true;
  160. BUG_ON(isci_phy->isci_port != NULL);
  161. isci_phy->isci_port = isci_port;
  162. dev_dbg(&isci_host->pdev->dev,
  163. "%s: isci_port = %p\n",
  164. __func__, isci_port);
  165. spin_lock_irqsave(&isci_phy->sas_phy.frame_rcvd_lock, flags);
  166. isci_port_change_state(isci_phy->isci_port, isci_starting);
  167. scic_port_get_properties(port, &properties);
  168. if (properties.remote.protocols.u.bits.stp_target) {
  169. u64 attached_sas_address;
  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. attached_sas_address = properties.remote.sas_address.high;
  192. attached_sas_address <<= 32;
  193. attached_sas_address |= properties.remote.sas_address.low;
  194. swab64s(&attached_sas_address);
  195. memcpy(&isci_phy->sas_phy.attached_sas_addr,
  196. &attached_sas_address, sizeof(attached_sas_address));
  197. } else if (properties.remote.protocols.u.bits.ssp_target ||
  198. properties.remote.protocols.u.bits.smp_target) {
  199. struct scic_sas_phy_properties sas_phy_properties;
  200. isci_phy->sas_phy.oob_mode = SAS_OOB_MODE;
  201. /* Get a copy of the identify address frame for libsas */
  202. call_status = scic_sas_phy_get_properties(phy,
  203. &sas_phy_properties);
  204. BUG_ON(call_status != SCI_SUCCESS);
  205. memcpy(isci_phy->frame_rcvd.aif,
  206. &(sas_phy_properties.received_iaf),
  207. sizeof(struct sci_sas_identify_address_frame));
  208. isci_phy->sas_phy.frame_rcvd_size
  209. = sizeof(struct sci_sas_identify_address_frame);
  210. /* Copy the attached SAS address from the IAF */
  211. memcpy(isci_phy->sas_phy.attached_sas_addr,
  212. ((struct sas_identify_frame *)
  213. (&isci_phy->frame_rcvd.aif))->sas_addr,
  214. SAS_ADDR_SIZE);
  215. } else {
  216. dev_err(&isci_host->pdev->dev, "%s: unkown target\n", __func__);
  217. success = false;
  218. }
  219. isci_phy->sas_phy.phy->negotiated_linkrate = sci_phy_linkrate(phy);
  220. spin_unlock_irqrestore(&isci_phy->sas_phy.frame_rcvd_lock, flags);
  221. /* Notify libsas that we have an address frame, if indeed
  222. * we've found an SSP, SMP, or STP target */
  223. if (success)
  224. isci_host->sas_ha.notify_port_event(&isci_phy->sas_phy,
  225. PORTE_BYTES_DMAED);
  226. }
  227. /**
  228. * isci_port_link_down() - This function is called by the sci core when a link
  229. * becomes inactive.
  230. * @isci_host: This parameter specifies the isci host object.
  231. * @phy: This parameter specifies the isci phy with the active link.
  232. * @port: This parameter specifies the isci port with the active link.
  233. *
  234. */
  235. void isci_port_link_down(struct isci_host *isci_host, struct isci_phy *isci_phy,
  236. struct isci_port *isci_port)
  237. {
  238. struct isci_remote_device *isci_device;
  239. dev_dbg(&isci_host->pdev->dev,
  240. "%s: isci_port = %p\n", __func__, isci_port);
  241. if (isci_port) {
  242. /* check to see if this is the last phy on this port. */
  243. if (isci_phy->sas_phy.port
  244. && isci_phy->sas_phy.port->num_phys == 1) {
  245. /* change the state for all devices on this port.
  246. * The next task sent to this device will be returned
  247. * as SAS_TASK_UNDELIVERED, and the scsi mid layer
  248. * will remove the target
  249. */
  250. list_for_each_entry(isci_device,
  251. &isci_port->remote_dev_list,
  252. node) {
  253. dev_dbg(&isci_host->pdev->dev,
  254. "%s: isci_device = %p\n",
  255. __func__, isci_device);
  256. isci_remote_device_change_state(isci_device,
  257. isci_stopping);
  258. }
  259. }
  260. isci_port_change_state(isci_port, isci_stopping);
  261. }
  262. /* Notify libsas of the borken link, this will trigger calls to our
  263. * isci_port_deformed and isci_dev_gone functions.
  264. */
  265. sas_phy_disconnected(&isci_phy->sas_phy);
  266. isci_host->sas_ha.notify_phy_event(&isci_phy->sas_phy,
  267. PHYE_LOSS_OF_SIGNAL);
  268. isci_phy->isci_port = NULL;
  269. dev_dbg(&isci_host->pdev->dev,
  270. "%s: isci_port = %p - Done\n", __func__, isci_port);
  271. }
  272. /**
  273. * isci_port_deformed() - This function is called by libsas when a port becomes
  274. * inactive.
  275. * @phy: This parameter specifies the libsas phy with the inactive port.
  276. *
  277. */
  278. void isci_port_deformed(
  279. struct asd_sas_phy *phy)
  280. {
  281. pr_debug("%s: sas_phy = %p\n", __func__, phy);
  282. }
  283. /**
  284. * isci_port_formed() - This function is called by libsas when a port becomes
  285. * active.
  286. * @phy: This parameter specifies the libsas phy with the active port.
  287. *
  288. */
  289. void isci_port_formed(
  290. struct asd_sas_phy *phy)
  291. {
  292. pr_debug("%s: sas_phy = %p, sas_port = %p\n", __func__, phy, phy->port);
  293. }
  294. /**
  295. * isci_port_ready() - This function is called by the sci core when a link
  296. * becomes ready.
  297. * @isci_host: This parameter specifies the isci host object.
  298. * @port: This parameter specifies the sci port with the active link.
  299. *
  300. */
  301. void isci_port_ready(struct isci_host *isci_host, struct isci_port *isci_port)
  302. {
  303. dev_dbg(&isci_host->pdev->dev,
  304. "%s: isci_port = %p\n", __func__, isci_port);
  305. complete_all(&isci_port->start_complete);
  306. isci_port_change_state(isci_port, isci_ready);
  307. return;
  308. }
  309. /**
  310. * isci_port_not_ready() - This function is called by the sci core when a link
  311. * is not ready. All remote devices on this link will be removed if they are
  312. * in the stopping state.
  313. * @isci_host: This parameter specifies the isci host object.
  314. * @port: This parameter specifies the sci port with the active link.
  315. *
  316. */
  317. void isci_port_not_ready(struct isci_host *isci_host, struct isci_port *isci_port)
  318. {
  319. dev_dbg(&isci_host->pdev->dev,
  320. "%s: isci_port = %p\n", __func__, isci_port);
  321. }
  322. /**
  323. * isci_port_hard_reset_complete() - This function is called by the sci core
  324. * when the hard reset complete notification has been received.
  325. * @port: This parameter specifies the sci port with the active link.
  326. * @completion_status: This parameter specifies the core status for the reset
  327. * process.
  328. *
  329. */
  330. void isci_port_hard_reset_complete(struct isci_port *isci_port,
  331. enum sci_status completion_status)
  332. {
  333. dev_dbg(&isci_port->isci_host->pdev->dev,
  334. "%s: isci_port = %p, completion_status=%x\n",
  335. __func__, isci_port, completion_status);
  336. /* Save the status of the hard reset from the port. */
  337. isci_port->hard_reset_status = completion_status;
  338. complete_all(&isci_port->hard_reset_complete);
  339. }
  340. /**
  341. * isci_port_perform_hard_reset() - This function is one of the SAS Domain
  342. * Template functions. This is a phy management function.
  343. * @isci_port:
  344. * @isci_phy:
  345. *
  346. * status, TMF_RESP_FUNC_COMPLETE indicates success.
  347. */
  348. int isci_port_perform_hard_reset(
  349. struct isci_port *isci_port,
  350. struct isci_phy *isci_phy)
  351. {
  352. enum sci_status status;
  353. int ret = TMF_RESP_FUNC_COMPLETE;
  354. unsigned long flags;
  355. dev_dbg(&isci_port->isci_host->pdev->dev,
  356. "%s: isci_port = %p\n",
  357. __func__, isci_port);
  358. BUG_ON(isci_port == NULL);
  359. init_completion(&isci_port->hard_reset_complete);
  360. spin_lock_irqsave(&isci_port->isci_host->scic_lock, flags);
  361. #define ISCI_PORT_RESET_TIMEOUT SCIC_SDS_SIGNATURE_FIS_TIMEOUT
  362. status = scic_port_hard_reset(isci_port->sci_port_handle,
  363. ISCI_PORT_RESET_TIMEOUT);
  364. spin_unlock_irqrestore(&isci_port->isci_host->scic_lock, flags);
  365. if (status == SCI_SUCCESS) {
  366. wait_for_completion(&isci_port->hard_reset_complete);
  367. dev_dbg(&isci_port->isci_host->pdev->dev,
  368. "%s: isci_port = %p; hard reset completion\n",
  369. __func__, isci_port);
  370. if (isci_port->hard_reset_status != SCI_SUCCESS)
  371. ret = TMF_RESP_FUNC_FAILED;
  372. } else {
  373. ret = TMF_RESP_FUNC_FAILED;
  374. dev_err(&isci_port->isci_host->pdev->dev,
  375. "%s: isci_port = %p; scic_port_hard_reset call"
  376. " failed 0x%x\n",
  377. __func__, isci_port, status);
  378. }
  379. /* If the hard reset for the port has failed, consider this
  380. * the same as link failures on all phys in the port.
  381. */
  382. if (ret != TMF_RESP_FUNC_COMPLETE) {
  383. BUG_ON(isci_port->isci_host == NULL);
  384. dev_err(&isci_port->isci_host->pdev->dev,
  385. "%s: isci_port = %p; hard reset failed "
  386. "(0x%x) - sending link down to libsas for phy %p\n",
  387. __func__,
  388. isci_port,
  389. isci_port->hard_reset_status,
  390. isci_phy);
  391. isci_port_link_down(isci_port->isci_host,
  392. isci_phy,
  393. isci_port);
  394. }
  395. return ret;
  396. }
  397. /**
  398. * isci_port_invalid_link_up() - This function informs the SCI Core user that
  399. * a phy/link became ready, but the phy is not allowed in the port. In some
  400. * situations the underlying hardware only allows for certain phy to port
  401. * mappings. If these mappings are violated, then this API is invoked.
  402. * @controller: This parameter represents the controller which contains the
  403. * port.
  404. * @port: This parameter specifies the SCI port object for which the callback
  405. * is being invoked.
  406. * @phy: This parameter specifies the phy that came ready, but the phy can't be
  407. * a valid member of the port.
  408. *
  409. */
  410. void isci_port_invalid_link_up(struct scic_sds_controller *scic,
  411. struct scic_sds_port *sci_port,
  412. struct scic_sds_phy *phy)
  413. {
  414. struct isci_host *ihost =
  415. (struct isci_host *)sci_object_get_association(scic);
  416. dev_warn(&ihost->pdev->dev, "Invalid link up!\n");
  417. }
  418. void isci_port_stop_complete(struct scic_sds_controller *scic,
  419. struct scic_sds_port *sci_port,
  420. enum sci_status completion_status)
  421. {
  422. struct isci_host *ihost = sci_object_get_association(scic);
  423. dev_dbg(&ihost->pdev->dev, "Port stop complete\n");
  424. }